64 lines
1.9 KiB
Lua
64 lines
1.9 KiB
Lua
local term_win = nil
|
|
local term_buf = nil
|
|
|
|
local function toggle_term()
|
|
if term_win and vim.api.nvim_win_is_valid(term_win) then
|
|
vim.api.nvim_win_close(term_win, false)
|
|
term_win = nil
|
|
else
|
|
vim.cmd('botright vsplit')
|
|
term_win = vim.api.nvim_get_current_win()
|
|
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
|
|
vim.api.nvim_win_set_buf(term_win, term_buf)
|
|
else
|
|
vim.cmd('terminal')
|
|
term_buf = vim.api.nvim_get_current_buf()
|
|
end
|
|
vim.api.nvim_win_set_width(term_win, math.floor(vim.o.columns * 0.35))
|
|
vim.cmd('startinsert')
|
|
end
|
|
end
|
|
|
|
-- Jump to previous non-terminal buffer
|
|
local function prev_non_term_buf()
|
|
local cur = vim.api.nvim_get_current_buf()
|
|
local bufs = vim.fn.getbufinfo({ buflisted = 1 })
|
|
|
|
-- Walk buffer list in reverse to find last non-terminal buffer that isn't current
|
|
local history = vim.fn.execute('ls t') -- 't' flag = sort by last used time
|
|
local candidates = {}
|
|
for _, info in ipairs(bufs) do
|
|
local bufnr = info.bufnr
|
|
if bufnr ~= cur
|
|
and bufnr ~= term_buf
|
|
and vim.bo[bufnr].buftype ~= 'terminal'
|
|
then
|
|
table.insert(candidates, { bufnr = bufnr, lastused = info.lastused })
|
|
end
|
|
end
|
|
|
|
if #candidates == 0 then
|
|
print('No previous non-terminal buffer')
|
|
return
|
|
end
|
|
|
|
-- Sort by lastused descending, pick the most recently used
|
|
table.sort(candidates, function(a, b) return a.lastused > b.lastused end)
|
|
vim.api.nvim_set_current_buf(candidates[1].bufnr)
|
|
end
|
|
|
|
-- Overrides init binding
|
|
map('n', '<leader>l', prev_non_term_buf, { desc = 'Go to previous non-terminal buffer' })
|
|
map('n', '<C-o>', function()
|
|
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
|
|
vim.api.nvim_set_current_buf(term_buf)
|
|
else
|
|
print('No terminal buffer open yet')
|
|
end
|
|
end)
|
|
map('n', '<C-j>', toggle_term, { desc = 'Toggle terminal' })
|
|
|
|
-- terminal mode binds
|
|
map('t', '<C-j>', toggle_term, { desc = 'Toggle terminal' })
|
|
map('t', '<C-o>', '<C-\\><C-n><C-^>', { desc = 'Go to last buffer' })
|