Skip to content

Commit b9b8251

Browse files
authored
Merge pull request #71 from francescarpi/develop
release: v2025-02-27
2 parents 3b93890 + 50464f4 commit b9b8251

File tree

9 files changed

+253
-40
lines changed

9 files changed

+253
-40
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Based on this, I created *Buffon* with the best of both plugins, perfectly adapt
6565
return {
6666
{
6767
"francescarpi/buffon.nvim",
68+
---@type BuffonConfig
6869
opts = {
6970
--- Add your config here
7071
},
@@ -84,6 +85,7 @@ Below you can see the default configuration, which you can adjust to your liking
8485
return {
8586
{
8687
"francescarpi/buffon.nvim",
88+
---@type BuffonConfig
8789
opts = {
8890
cyclic_navigation = true,
8991
},
@@ -148,6 +150,41 @@ Take a look at the default shortcuts for navigating between buffers, changing th
148150
> vim.keymap.set("n", "c-l", vim.lsp.buf.hover)
149151
> ````
150152
153+
### Disable keybindings
154+
155+
The keybindings shown in the following list can be deactivated. The reason for this is that some people may not be interested in using the functionality to close buffers, move them, etc.
156+
157+
```lua
158+
{
159+
move_buffer_up,
160+
move_buffer_down,
161+
move_buffer_top,
162+
move_buffer_bottom,
163+
switch_previous_used_buffer,
164+
close_buffer,
165+
close_buffers_above,
166+
close_buffers_below,
167+
close_all_buffers,
168+
close_others,
169+
reopen_recent_closed_buffer,
170+
}
171+
```
172+
173+
To do this, you only have to assign the string "false" in the configuration. For example:
174+
175+
176+
```lua
177+
{
178+
opts = {
179+
keybindings = {
180+
close_buffer = "false"
181+
close_others = "false"
182+
},
183+
},
184+
}
185+
```
186+
187+
151188
## Screenshots
152189

153190
Buffon window, showing the buffer list:

lua/buffon/config.lua

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ local default = {
4747
---@class BuffonConfigKeyBinding
4848
---@field goto_next_buffer string
4949
---@field goto_previous_buffer string
50-
---@field move_buffer_up string
51-
---@field move_buffer_down string
52-
---@field move_buffer_top string
50+
---@field move_buffer_up string|false
51+
---@field move_buffer_down string|false
52+
---@field move_buffer_top string|false
53+
---@field move_buffer_bottom string|false
5354
---@field toggle_buffon_window string
54-
---@field switch_previous_used_buffer string
55-
---@field close_buffer string
56-
---@field close_buffers_above string
57-
---@field close_buffers_below string
58-
---@field close_all_buffers string
59-
---@field close_others string
60-
---@field restore_last_closed_buffer string
55+
---@field switch_previous_used_buffer string|false
56+
---@field close_buffer string|false
57+
---@field close_buffers_above string|false
58+
---@field close_buffers_below string|false
59+
---@field close_all_buffers string|false
60+
---@field close_others string|false
61+
---@field reopen_recent_closed_buffer string|false
6162
---@field show_help string
6263
---@field next_page string
6364
---@field previous_page string
@@ -76,10 +77,13 @@ local Config = {}
7677

7778
---@param opts any
7879
function Config:new(opts)
79-
local o = vim.tbl_deep_extend("force", default, opts or {})
80-
setmetatable(o, self)
80+
local cfg = vim.tbl_deep_extend("force", default, opts or {})
81+
if cfg.num_pages < 1 or cfg.num_pages > 4 then
82+
cfg.num_pages = 1
83+
end
84+
setmetatable(cfg, self)
8185
self.__index = self
82-
return o
86+
return cfg
8387
end
8488

8589
M.Config = Config

lua/buffon/maincontroller.lua

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ local utils = require("buffon.utils")
88

99
local M = {}
1010

11+
---@type table<string, boolean>
12+
local configurable_disabled_actions = {
13+
move_buffer_up = true,
14+
move_buffer_down = true,
15+
move_buffer_top = true,
16+
move_buffer_bottom = true,
17+
switch_previous_used_buffer = true,
18+
close_buffer = true,
19+
close_buffers_above = true,
20+
close_buffers_below = true,
21+
close_all_buffers = true,
22+
close_others = true,
23+
reopen_recent_closed_buffer = true,
24+
}
25+
26+
---@type table<string, boolean>
27+
local pagination_actions = {
28+
next_page = true,
29+
previous_page = true,
30+
move_to_next_page = true,
31+
move_to_previous_page = true,
32+
}
33+
1134
--- Sets a keymap with the given parameters.
1235
---@param lhs string The left-hand side of the keymap.
1336
---@param rhs function | string The right-hand side of the keymap.
@@ -63,100 +86,118 @@ end
6386

6487
---@return table<BuffonAction>
6588
function MainController:get_shortcuts()
66-
return {
89+
---@type table<BuffonAction>
90+
local shortcuts = {
6791
{
68-
shortcut = self.config.keybindings.toggle_buffon_window,
92+
shortcut = "toggle_buffon_window",
6993
help = "Show/hide buffer list",
7094
method = self.action_show_hide_buffon_window,
7195
},
7296
{
73-
shortcut = self.config.keybindings.goto_next_buffer,
97+
shortcut = "goto_next_buffer",
7498
help = "Next buffer",
7599
method = self.action_goto_next,
76100
},
77101
{
78-
shortcut = self.config.keybindings.goto_previous_buffer,
102+
shortcut = "goto_previous_buffer",
79103
help = "Previous buffer",
80104
method = self.action_goto_previous,
81105
},
82106
{
83-
shortcut = self.config.keybindings.next_page,
107+
shortcut = "next_page",
84108
help = "Next page",
85109
method = self.action_next_page,
86110
method_post_refresh = self.action_select_buffer,
87111
},
88112
{
89-
shortcut = self.config.keybindings.previous_page,
113+
shortcut = "previous_page",
90114
help = "Previous page",
91115
method = self.action_previous_page,
92116
method_post_refresh = self.action_select_buffer,
93117
},
94118
{
95-
shortcut = self.config.keybindings.move_to_next_page,
119+
shortcut = "move_to_next_page",
96120
help = "Buffer to next page",
97121
method = self.action_buffer_to_next_page,
98122
},
99123
{
100-
shortcut = self.config.keybindings.move_to_previous_page,
124+
shortcut = "move_to_previous_page",
101125
help = "Buffer to previous page",
102126
method = self.action_buffer_to_previous_page,
103127
},
104128
{
105-
shortcut = self.config.keybindings.move_buffer_up,
129+
shortcut = "move_buffer_up",
106130
help = "Move buffer up",
107131
method = self.action_move_buffer_up,
108132
},
109133
{
110-
shortcut = self.config.keybindings.move_buffer_down,
134+
shortcut = "move_buffer_down",
111135
help = "Move buffer down",
112136
method = self.action_move_buffer_down,
113137
},
114138
{
115-
shortcut = self.config.keybindings.move_buffer_top,
139+
shortcut = "move_buffer_top",
116140
help = "Move buffer to top",
117141
method = self.action_move_buffer_top,
118142
},
119143
{
120-
shortcut = self.config.keybindings.move_buffer_bottom,
144+
shortcut = "move_buffer_bottom",
121145
help = "Move buffer to bottom",
122146
method = self.action_move_buffer_bottom,
123147
},
124148
{
125-
shortcut = self.config.keybindings.close_buffer,
149+
shortcut = "close_buffer",
126150
help = "Close buffer",
127151
method = self.action_close_buffer,
128152
},
129153
{
130-
shortcut = self.config.keybindings.close_buffers_above,
154+
shortcut = "close_buffers_above",
131155
help = "Close buffers above",
132156
method = self.action_close_buffers_above,
133157
},
134158
{
135-
shortcut = self.config.keybindings.close_buffers_below,
159+
shortcut = "close_buffers_below",
136160
help = "Close buffers below",
137161
method = self.action_close_buffers_below,
138162
},
139163
{
140-
shortcut = self.config.keybindings.close_all_buffers,
164+
shortcut = "close_all_buffers",
141165
help = "Close all",
142166
method = self.action_close_buffers_all,
143167
},
144168
{
145-
shortcut = self.config.keybindings.close_others,
169+
shortcut = "close_others",
146170
help = "Close others",
147171
method = self.action_close_buffers_other,
148172
},
149173
{
150-
shortcut = self.config.keybindings.switch_previous_used_buffer,
174+
shortcut = "switch_previous_used_buffer",
151175
help = "Last used buffer",
152176
method = self.action_switch_previous_used,
153177
},
154178
{
155-
shortcut = self.config.keybindings.reopen_recent_closed_buffer,
179+
shortcut = "reopen_recent_closed_buffer",
156180
help = "Restore closed buffer",
157181
method = self.action_reopen_recent_closed,
158182
},
159183
}
184+
185+
---@type table<BuffonAction>
186+
local valid_shortcuts = {}
187+
for _, action in ipairs(shortcuts) do
188+
local action_can_be_disabled = configurable_disabled_actions[action.shortcut]
189+
local action_is_disabled = self.config.keybindings[action.shortcut] == "false"
190+
local is_pagination_action = pagination_actions[action.shortcut]
191+
local one_page = self.config.num_pages == 1
192+
193+
if (action_can_be_disabled and action_is_disabled) or (one_page and is_pagination_action) then
194+
-- continue
195+
log.debug("action ignored", action.shortcut)
196+
else
197+
table.insert(valid_shortcuts, action)
198+
end
199+
end
200+
return valid_shortcuts
160201
end
161202

162203
---@return table<BuffonAction>
@@ -208,7 +249,7 @@ end
208249

209250
function MainController:register_shortcuts()
210251
for _, action in ipairs(self:get_shortcuts()) do
211-
set_keymap(utils.replace_leader(self.config, action.shortcut), function()
252+
set_keymap(utils.replace_leader(self.config, self.config.keybindings[action.shortcut]), function()
212253
self:dispatch(action)
213254
end, action.help)
214255
end

lua/buffon/pagecontroller.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ end
8383

8484
---@param pages table<table<BuffonBuffer>>
8585
local validate_data = function(config, pages)
86+
if config.num_pages ~= #pages then
87+
error("number of pages doesn't match")
88+
end
8689
for idx = 1, config.num_pages do
8790
local buffers = pages[idx]
8891
for _, buffer in ipairs(buffers) do

lua/buffon/ui/help.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ function HelpWindow:toggle(actions)
3232
local highlight = { Constant = {} }
3333

3434
for _, action in ipairs(actions) do
35-
local action_len = #utils.replace_leader(self.config, action.shortcut)
35+
local action_len = #utils.replace_leader(self.config, self.config.keybindings[action.shortcut])
3636
if action_len > max_length then
3737
max_length = action_len
3838
end
3939
end
4040

4141
local content = {}
4242
for idx, action in ipairs(actions) do
43-
local shortcut = utils.replace_leader(self.config, action.shortcut)
43+
local shortcut = utils.replace_leader(self.config, self.config.keybindings[action.shortcut])
4444
local shortcut_padded = shortcut .. string.rep(" ", max_length - #shortcut)
4545
local line = string.format("%s %s", shortcut_padded, action.help)
4646
table.insert(content, line)

lua/buffon/ui/mainwindow.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ function MainWindow:refresh()
5353
local render = self.page_controller:get_active_page():render()
5454
self.window:set_content(render.content)
5555
self.window:set_highlight(render.highlights)
56-
self.window:set_footer(self:footer())
56+
if self.config.num_pages > 1 then
57+
self.window:set_footer(self:footer())
58+
end
5759
self.window:refresh_dimensions()
5860
end
5961

tests/config_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ describe("config", function()
4040
},
4141
})
4242
end)
43+
44+
it("if num_pages is invalid, set default to 1", function()
45+
local cfg = config.Config:new({ num_pages = 0 })
46+
eq(cfg.num_pages, 1)
47+
48+
-- limot of num_pages is 4
49+
local cfg2 = config.Config:new({ num_pages = 5 })
50+
eq(cfg2.num_pages, 1)
51+
end)
4352
end)

0 commit comments

Comments
 (0)