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

feat: add global terminal buffer support #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use {
-- presets = {
-- 'vim-test',
-- }
-- enable_global_term = true, -- Enable global terminal buffer for every buffers
}
vim.keymap.set('n', '<M-Tab>', function () vim.cmd('NeoTermToggle') end)
vim.keymap.set('t', '<M-Tab>', function () vim.cmd('NeoTermEnterNormal') end)
Expand Down
45 changes: 29 additions & 16 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,19 +84,31 @@ 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])
local term_buf = M.buf_open_to_term[open_buf]
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
vim.api.nvim_set_current_buf(term_buf)
return
end

-- global_term_buf won't be initialized if global term is not enabled
if M.global_term_buf and vim.api.nvim_buf_is_valid(M.global_term_buf) then
vim.api.nvim_set_current_buf(M.global_term_buf)
else
local buf = vim.api.nvim_create_buf(true, false)
vim.bo[buf].filetype = 'neo-term'
vim.api.nvim_set_current_buf(buf)
term_buf = vim.api.nvim_create_buf(true, false)
vim.bo[term_buf].filetype = 'neo-term'

if M.enable_global_term then
M.global_term_buf = term_buf
end

vim.api.nvim_set_current_buf(term_buf)
vim.fn.termopen(vim.opt.shell:get())
M.buf_open_to_term[open_buf] = vim.api.nvim_get_current_buf()
end

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


Expand Down