summary refs log tree commit diff
path: root/.vim/plugin/termopen.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2024-11-25 13:30:37 +0100
committerRobert Günzler <r@gnzler.io>2024-11-25 13:58:25 +0100
commit23de12baba9fbfc4ca24581b37c374aa5345cc25 (patch)
treee4b3c421bad572eb85a1089eaad44dc3059ab6e6 /.vim/plugin/termopen.lua
parent18e3848110da0a59843058927ea04b496f5db2a5 (diff)
update nvim config
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/plugin/termopen.lua')
-rw-r--r--.vim/plugin/termopen.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/.vim/plugin/termopen.lua b/.vim/plugin/termopen.lua
new file mode 100644
index 0000000..68b9d8d
--- /dev/null
+++ b/.vim/plugin/termopen.lua
@@ -0,0 +1,59 @@
+local function termopen(opts)
+  opts = opts or {}
+
+  -- setup the buffer
+  local buf = vim.api.nvim_create_buf(false, true)
+  vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = buf })
+  vim.api.nvim_set_option_value('modifiable', false, { buf = buf })
+
+  -- setup keymappings
+  vim.keymap.set('n', 'q', '<cmd>close<cr>', { buffer = buf })
+  vim.keymap.set('n', '<esc>', '<cmd>close<cr>', { buffer = buf })
+
+  -- setup the window
+  local w = math.ceil(vim.o.columns * 1.0)
+  local h = math.ceil(vim.o.lines * 0.4)
+  local win = vim.api.nvim_open_win(buf, true, {
+    style = 'minimal',
+    relative = 'editor',
+    width = w,
+    height = h,
+    row = math.ceil(vim.o.lines - h),
+    col = vim.o.columns,
+    border = { ' ', '─', ' ', ' ', ' ', '─', ' ', ' ' },
+  })
+  vim.api.nvim_set_current_win(win)
+
+  -- spawn the command
+  vim.fn.termopen(opts.cmd, {
+    on_exit = function()
+      if vim.api.nvim_win_is_valid(win) then
+        vim.api.nvim_win_close(win, true)
+      end
+    end,
+  })
+
+  -- put into insert mode
+  vim.cmd.startinsert()
+end
+
+-- provide user commands
+vim.api.nvim_create_user_command('Termopen', function(a)
+  termopen({ cmd = a.fargs })
+end, { nargs = '*' })
+
+-- alias
+vim.api.nvim_create_user_command('T', function(a)
+  termopen({ cmd = a.fargs })
+end, { nargs = '*' })
+
+-- helper to spawn &makeprg
+vim.api.nvim_create_user_command('Make', function(a)
+  local cmd = vim.split(vim.o.makeprg, ' ')
+  cmd = vim.tbl_map(function(p)
+    p, _ = string.gsub(p, [[%$%*]], a.args)
+    p, _ = vim.fn.expand(p)
+    return p
+  end, cmd)
+  termopen({ cmd = cmd })
+end, { nargs = '*' })