Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick close multiple buffers #205

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions lua/cokeline/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,47 @@ local by_step = function(goal, step)
end
end

---@param goal '"focus"' | '"close"' | fun(buf: Buffer)
---@param goal '"focus"' | '"close"' | '"close-multiple"' | fun(buf: Buffer)
local pick = function(goal)
local close_multiple = false
if goal == "close-multiple" then
goal = "close"
close_multiple = true
end

is_picking[goal] = true
cmd("redrawtabline")

local valid_char, char = pcall(fn.getchar)
is_picking[goal] = false
-- bail out on keyboard interrupt
if not valid_char then
char = 0
end
repeat
local valid_char, char = pcall(fn.getchar)
if not close_multiple then
is_picking[goal] = false
end

local letter = fn.nr2char(char)
local target_buffer = filter(function(buffer)
return buffer.pick_letter == letter
end, state.visible_buffers)[1]
-- bail out on keyboard interrupt
if not valid_char then
char = 0
end

if not target_buffer or (goal == "focus" and target_buffer.is_focused) then
cmd("redrawtabline")
return
end
local letter = fn.nr2char(char)
local target_buffer = filter(function(buffer)
return buffer.pick_letter == letter
end, state.visible_buffers)[1]

if goal == "focus" then
target_buffer:focus()
elseif goal == "close" then
target_buffer:delete()
elseif type(goal) == "function" then
goal(target_buffer)
end
if not target_buffer or (goal == "focus" and target_buffer.is_focused) then
is_picking[goal] = false
cmd("redrawtabline")
return
end

if goal == "focus" then
target_buffer:focus()
elseif goal == "close" then
target_buffer:delete()
elseif type(goal) == "function" then
goal(target_buffer)
end
until not is_picking[goal]
end

local setup = function()
Expand Down Expand Up @@ -150,6 +162,11 @@ local setup = function()
keymap.set("n", "<Plug>(cokeline-pick-close)", function()
pick("close")
end)

keymap.set("n", "<Plug>(cokeline-pick-close-multiple)", function()
pick("close-multiple")
end)

for i = 1, 20 do
keymap.set("n", ("<Plug>(cokeline-switch-%s)"):format(i), function()
by_index("switch", i)
Expand Down Expand Up @@ -196,6 +213,12 @@ local setup = function()
'<Cmd>lua require"cokeline/mappings".pick("close")<CR>',
{}
)
set_keymap(
"n",
"<Plug>(cokeline-pick-close-multiple)",
'<Cmd>lua require"cokeline/mappings".pick("close-multiple")<CR>',
{}
)
for i = 1, 20 do
set_keymap(
"n",
Expand Down
Loading