-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathterm.lua
83 lines (74 loc) · 2.29 KB
/
term.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
--[[
Small :term wrapper for opening a terminal at the bottom of the screen with a
persistent buffer.
--]]
local term = {
termwin = -1,
termbuf = -1,
height = 15,
augroup = vim.api.nvim_create_augroup("toggleterm", {}),
opts = {
spell = false,
number = false,
relativenumber = false
}
}
function term:open(args)
local current = vim.fn.win_getid()
args = args or {}
-- if term is not valid open a new one
if not vim.api.nvim_win_is_valid(self.termwin) then
vim.cmd.split{range = {self.height}, mods = {split = "botright"}}
vim.opt_local.winfixheight = true
self.termwin = vim.api.nvim_get_current_win()
else
-- if someone stole our window but there is only one window in total create
-- a new split before taking our old window back
if #vim.api.nvim_list_wins() == 1 and vim.api.nvim_win_get_buf(self.termwin) ~= self.termbuf then
vim.cmd.split()
end
-- take our window back, just in case
vim.api.nvim_set_current_win(self.termwin)
-- only resize if there are more then one windows
if #vim.api.nvim_list_wins() > 1 then
vim.cmd.wincmd("J")
vim.cmd.resize(self.height)
end
end
if not vim.api.nvim_buf_is_valid(self.termbuf) then
self.termbuf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(self.termbuf)
if args.cmd ~= false then -- false means termopen is handled externally
vim.fn.termopen(args.cmd or vim.o.shell, args.opts or vim.empty_dict())
end
for opt, val in pairs(self.opts) do
vim.opt_local[opt] = val
end
else
vim.api.nvim_set_current_buf(self.termbuf)
end
if args.nofocus then
vim.api.nvim_set_current_win(current)
elseif not args.noinsert then
vim.cmd.startinsert()
end
return self.termbuf, self.termwin
end
function term:close()
if vim.api.nvim_win_is_valid(self.termwin) then
vim.api.nvim_win_hide(self.termwin)
end
end
function term:destroy()
if vim.api.nvim_buf_is_valid(self.termbuf) then
vim.api.nvim_buf_delete(self.termbuf, {force = true})
end
end
-- create a terminal instance with its own buffer but same window
function term.instance()
return setmetatable({termbuf = -1}, {
__index = term,
__newindex = function(_, k, v) term[k] = v end
})
end
return setmetatable(term, {__call = term.instance})