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
84
85
86
87
|
local M = {}
local defaults = {
terminals = {
list = {},
type_opts = {
float = {
relative = 'editor',
row = 0.3,
col = 0.25,
width = 0.5,
height = 0.4,
border = "single",
},
horizontal = { location = "rightbelow", split_ratio = .3, },
vertical = { location = "rightbelow", split_ratio = .5 },
}
},
behavior = {
close_on_exit = true,
auto_insert = true,
},
mappings = {
toggle = {
float = "<A-i>",
horizontal = "<A-h>",
vertical = "<A-v>",
},
new = {
float = "<C-i>",
horizontal = "<C-h>",
vertical = "<C-v>",
},
}
}
local set_behavior = function(behavior)
if behavior.close_on_exit then
vim.api.nvim_create_autocmd({"TermClose"},{
callback = function()
vim.schedule_wrap(vim.api.nvim_input('<CR>'))
end
})
end
if behavior.auto_insert then
vim.api.nvim_create_autocmd({"BufEnter"}, {
callback = function() vim.cmd('startinsert') end,
pattern = 'term://*'
})
vim.api.nvim_create_autocmd({"BufLeave"}, {
callback = function() vim.cmd('stopinsert') end,
pattern = 'term://*'
})
end
end
M.create_mapping = function(method, map_table, opts)
opts = opts or {}
vim.tbl_deep_extend("force", { noremap = true, silent = true }, opts)
for type, mapping in pairs(map_table) do
vim.keymap.set({'n', 't'}, mapping, function ()
require('nvterm.terminal')[method](type)
end, opts)
end
end
local setup_mappings = function (mappings)
for method, map_table in pairs(mappings) do
M.create_mapping(method, map_table)
end
end
M.setup = function (config)
config = config and vim.tbl_deep_extend("force", defaults, config) or defaults
local types = {'horizontal', 'vertical', 'float'}
for _, type in pairs(types) do
if config[type] then
config.terminals.type_opts[type] = vim.tbl_deep_extend("force", config.terminals.type_opts[type], config[type])
config[type] = nil
end
end
set_behavior(config.behavior)
setup_mappings(config.mappings)
require("nvterm.terminal").init(config.terminals)
end
return M
|