summary refs log tree commit diff
path: root/.config/nvim/lua/mod/termopen.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-09-27 17:38:39 +0200
committerRobert Günzler <r@gnzler.io>2026-09-27 17:38:39 +0200
commit0860db73ae6fd9a6f6347219d820bf026a6b8b91 (patch)
tree11318c3670d1c32c5aa0bf404c22fba3bf63fc53 /.config/nvim/lua/mod/termopen.lua
parent4e34cbf9673aeaba252e01df554a0d204e83c5e2 (diff)
downloaddotfiles-master.tar.gz
Automated-commit-by: dots
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.config/nvim/lua/mod/termopen.lua')
-rw-r--r--.config/nvim/lua/mod/termopen.lua102
1 files changed, 102 insertions, 0 deletions
diff --git a/.config/nvim/lua/mod/termopen.lua b/.config/nvim/lua/mod/termopen.lua
new file mode 100644
index 0000000..f8117f1
--- /dev/null
+++ b/.config/nvim/lua/mod/termopen.lua
@@ -0,0 +1,102 @@
+-- this plugin provides a popup terminal.
+local jobs = require('mod.jobs')
+
+local M = {}
+
+function M.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 = { ' ', '─', ' ', ' ', ' ', '─', ' ', ' ' },
+    split = 'below',
+    win = -1,
+  })
+  vim.api.nvim_set_current_win(win)
+
+  -- spawn the command
+  if #opts.cmd == 0 then
+    opts.cmd = { vim.env.SHELL }
+  end
+
+  -- vim.notify('termopen: running ' .. vim.inspect(opts.cmd), vim.log.levels.DEBUG)
+
+  jobs.start(vim.tbl_deep_extend('error', opts, {
+    term = true,
+    env = {
+      -- we need this to avoid opening in the term buffer
+      NVR_PRE_CMD = 'close',
+    },
+    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
+
+-- wrappers convenience
+
+function M.term(a)
+  local cwd = nil
+  local args = a.fargs
+
+  if #args > 0 then
+    if args[1]:sub(0, 4) == 'cwd=' then
+      cwd = args[1]:sub(5)
+      args = vim.list_slice(args, 2)
+    end
+  end
+
+  for i = 0, #args do
+    if args[i] == '%' then
+      args[i] = vim.fn.expand(args[i])
+    end
+  end
+
+  M.termopen({ cmd = args, cwd = cwd })
+end
+
+function M.make(a)
+  if vim.opt_local.makeprg == nil then
+    vim.ui.input({ prompt = 'Enter makeprg: ' }, function(input)
+      vim.opt_local.makeprg = input
+    end)
+  end
+
+  local makeprg = vim.opt_local.makeprg:get()
+  if string.match(makeprg, [[%$%*]]) == nil then
+    makeprg = makeprg .. ' ' .. a.args
+  end
+
+  local cmd = vim.split(makeprg, ' ')
+  cmd = vim.tbl_map(function(p)
+    p, _ = string.gsub(p, [[%$%*]], a.args)
+    p, _ = vim.fn.expand(p)
+    return p
+  end, cmd)
+
+  M.termopen({ cmd = cmd })
+end
+
+return M