Skip to content

Commit

Permalink
feat: add global terminal buffer support
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Jan 27, 2024
1 parent f71a4a9 commit 7a1dbe3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lua/neo-term/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ vim.api.nvim_create_augroup('neo-term.lua', { clear = true })
-------------------------------------------------------------------------------------------------------
M.buf_open_to_term = {}
M.view_of_open_buf = {}
M.global_term_buf = nil
M.last_non_term_buf = nil


local function remove_invalid_mappings()
Expand All @@ -31,6 +33,7 @@ function M.setup(opts)
type(opts.exclude_buftypes) == 'table' and opts.exclude_buftypes or {})
M.presets = opts.presets
if type(M.presets) ~= 'table' then M.presets = { 'vim-test' } end
M.enable_global_term = opts.enable_global_term == true

A.create_autocmds()
P.setup(M.presets)
Expand All @@ -51,15 +54,13 @@ function M.neo_term_toggle()
end

-- Case1.2: it's a live terminal.
for o, t in pairs(M.buf_open_to_term) do
if vim.api.nvim_get_current_buf() == t
then
vim.api.nvim_set_current_buf(o)
vim.fn.winrestview(M.view_of_open_buf[vim.api.nvim_get_current_buf()])
remove_invalid_mappings()
return
end
if vim.api.nvim_buf_is_valid(M.last_non_term_buf) then
vim.api.nvim_set_current_buf(M.last_non_term_buf)
vim.fn.winrestview(M.view_of_open_buf[vim.api.nvim_get_current_buf()])
remove_invalid_mappings()
return
end

vim.api.nvim_set_current_buf(vim.api.nvim_create_buf(false, false))
return
end
Expand All @@ -83,17 +84,29 @@ function M.neo_term_toggle()

-- Case2.2: should open.
local open_buf = vim.api.nvim_get_current_buf()
M.last_non_term_buf = open_buf
M.view_of_open_buf[open_buf] = vim.fn.winsaveview()

if M.buf_open_to_term[open_buf]
and vim.api.nvim_buf_is_valid(M.buf_open_to_term[open_buf])
then
vim.api.nvim_set_current_buf(M.buf_open_to_term[open_buf])
else
local buf = vim.api.nvim_create_buf(true, false)
vim.bo[buf].filetype = 'neo-term'
vim.api.nvim_set_current_buf(buf)
vim.fn.termopen(vim.opt.shell:get())
if M.enable_global_term and M.global_term_buf and vim.api.nvim_buf_is_valid(M.global_term_buf) then
local buf = M.global_term_buf
vim.api.nvim_set_current_buf(buf)
else
local buf = vim.api.nvim_create_buf(true, false)
vim.bo[buf].filetype = 'neo-term'

if M.enable_global_term then
M.global_term_buf = buf
end

vim.api.nvim_set_current_buf(buf)
vim.fn.termopen(vim.opt.shell:get())
end

M.buf_open_to_term[open_buf] = vim.api.nvim_get_current_buf()
end
end
Expand Down

0 comments on commit 7a1dbe3

Please sign in to comment.