annotate dot_config/nvim/lua/terminal.lua @ 336:7a8b66395d69

Add floating terminal to be toggled via c-z
author zegervdv <zegervdv@me.com>
date Thu, 21 Jan 2021 09:17:28 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
336
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
1 -- Copied from: https://github.com/kutsan/dotfiles/blob/b2046a6c0bcc754fc381351119c14c374721fd4d/.config/nvim/lua/kutsan/mappings/normal/terminal.lua
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
2
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
3 local api = vim.api
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
4 local fn = vim.fn
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
5
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
6 local terminal = {
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
7 buf = nil,
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
8 win = nil,
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
9 pid = nil
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
10 }
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
11
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
12 terminal.open = function ()
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
13 -- Create buffer.
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
14 local buf = nil
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
15
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
16 if terminal.buf and api.nvim_buf_is_loaded(terminal.buf) then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
17 buf = terminal.buf
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
18 else
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
19 buf = api.nvim_create_buf(false, true)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
20 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
21
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
22 -- Create window.
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
23 local width = math.ceil(vim.o.columns * 0.8)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
24 local height = math.ceil(vim.o.lines * 0.9)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
25
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
26 local win = api.nvim_open_win(buf, true, {
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
27 relative = 'editor',
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
28 style = 'minimal',
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
29 width = width,
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
30 height = height,
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
31 col = math.ceil((vim.o.columns - width) / 2),
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
32 row = math.ceil((vim.o.lines - height) / 2 - 1),
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
33 })
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
34 api.nvim_win_set_option(win, 'winhighlight', 'Normal:CursorLine')
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
35
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
36 -- Launch terminal.
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
37 if not terminal.buf then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
38 terminal.pid = fn.termopen(string.format('%s --login', os.getenv('SHELL')))
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
39 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
40
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
41 vim.cmd('startinsert')
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
42 vim.cmd("autocmd! TermClose <buffer> lua require('terminal').close(true)")
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
43
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
44 -- Save current handles.
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
45 terminal.win = win
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
46 terminal.buf = buf
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
47 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
48
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
49 terminal.close = function (force)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
50 if not terminal.win then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
51 return
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
52 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
53
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
54 if api.nvim_win_is_valid(terminal.win) then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
55 api.nvim_win_close(terminal.win, false)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
56 terminal.win = nil
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
57 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
58
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
59 -- Force close upon terminal exit.
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
60 if force then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
61 if api.nvim_buf_is_loaded(terminal.buf) then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
62 api.nvim_buf_delete(terminal.buf, { force = true })
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
63 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
64
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
65 fn.jobstop(terminal.pid)
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
66
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
67 terminal.buf = nil
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
68 terminal.pid = nil
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
69 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
70 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
71
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
72 terminal.toggle = function ()
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
73 if not terminal.win then
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
74 terminal.open()
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
75 else
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
76 terminal.close()
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
77 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
78 end
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
79
7a8b66395d69 Add floating terminal to be toggled via c-z
zegervdv <zegervdv@me.com>
parents:
diff changeset
80 return terminal