Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Run local `opencode`s however you like and `opencode.nvim` will find them! Or po
> [!IMPORTANT]
> You _must_ run `opencode` with the `--port` flag to expose its server.

If `opencode.nvim` can't find an existing `opencode`, it starts one for you via `vim.g.opencode_opts.server.start`, defaulting to an embedded terminal.
If `opencode.nvim` can't find an existing `opencode`, it starts one for you via `vim.g.opencode_opts.server.start`, defaulting to an embedded terminal. Pass `focus_on_open = true` to keep focus after opening.

#### Custom

Expand Down
22 changes: 19 additions & 3 deletions lua/opencode/terminal.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

---@class opencode.terminal.Opts : vim.api.keyset.win_config
---@field focus_on_open? boolean Keep focus in the terminal window after opening instead of returning to the previous window.

local winid
local bufnr
Expand All @@ -19,8 +20,15 @@ function M.toggle(cmd, opts)
winid = nil
elseif bufnr ~= nil and vim.api.nvim_buf_is_valid(bufnr) then
local previous_win = vim.api.nvim_get_current_win()
local focus_on_open = opts.focus_on_open
opts.focus_on_open = nil
winid = vim.api.nvim_open_win(bufnr, true, opts)
vim.api.nvim_set_current_win(previous_win)
opts.focus_on_open = focus_on_open
if focus_on_open then
vim.cmd("startinsert")
else
vim.api.nvim_set_current_win(previous_win)
end
else
M.open(cmd, opts)
end
Expand All @@ -40,7 +48,10 @@ function M.open(cmd, opts)

local previous_win = vim.api.nvim_get_current_win()
bufnr = vim.api.nvim_create_buf(false, false)
local focus_on_open = opts.focus_on_open
opts.focus_on_open = nil
winid = vim.api.nvim_open_win(bufnr, true, opts)
opts.focus_on_open = focus_on_open

vim.api.nvim_create_autocmd("ExitPre", {
once = true,
Expand All @@ -65,7 +76,10 @@ function M.open(cmd, opts)
vim.api.nvim_del_autocmd(auid)
vim.api.nvim_set_current_win(winid)
-- Enter insert mode to trigger redraw, then exit and return to previous window.
vim.cmd([[startinsert | call feedkeys("\<C-\>\<C-n>\<C-w>p", "n")]])
vim.cmd("startinsert")
if not opts.focus_on_open then
vim.cmd([[call feedkeys("\<C-\>\<C-n>\<C-w>p", "n")]])
end
end
end,
})
Expand All @@ -77,7 +91,9 @@ function M.open(cmd, opts)
end,
})

vim.api.nvim_set_current_win(previous_win)
if not opts.focus_on_open then
vim.api.nvim_set_current_win(previous_win)
end
end

function M.close()
Expand Down
Loading