Skip to content
Merged
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 .agents/skills/verify-diff-lifecycle/covered.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ file lua/claudecode/server/utils.lua 2915ae87b48f04ca
== lua/claudecode/terminal.lua
file lua/claudecode/terminal.lua 2cb4a5721335543c
== lua/claudecode/terminal/external.lua
file lua/claudecode/terminal/external.lua 88d2a1111fd604bc
file lua/claudecode/terminal/external.lua 067b672c0fbb601d
== lua/claudecode/terminal/native.lua
file lua/claudecode/terminal/native.lua f7e0fb030c0c487d
== lua/claudecode/terminal/none.lua
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ For deep technical details, see [ARCHITECTURE.md](./ARCHITECTURE.md).
-- Command for external terminal provider. Can be:
-- 1. String with %s placeholder: "alacritty -e %s" (backward compatible)
-- 2. String with two %s placeholders: "alacritty --working-directory %s -e %s" (cwd, command)
-- The cwd %s must be a whole argument or an --option=%s value; other forms need a function
-- 3. Function returning command: function(cmd, env) return "alacritty -e " .. cmd end
external_terminal_cmd = nil,
},
Expand Down Expand Up @@ -855,6 +856,8 @@ Run Claude Code in a separate terminal application outside of Neovim:
}
```

With two `%s`, the working directory always stays one argument, whatever its folder name holds. The cwd `%s` must be a whole argument (`--working-directory %s`) or the value of an `--option=%s` argument. A template with the cwd `%s` anywhere else, such as inside `sh -c 'cd %s && ...'`, is refused with an error. Build that command with a function instead.

### Custom Terminal Providers

You can create custom terminal providers by passing a table with the required functions instead of a string provider name:
Expand Down
4 changes: 2 additions & 2 deletions lua/claudecode/terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ Optional fields (the dispatcher does not require them):
## Invariants

- `external` tracks one job. It counts as running only when `jobid > 0`. `open` does nothing while a job is running [terminal/external.lua:is_valid]
- `external.open` refuses (ERROR notify) when `provider_opts.external_terminal_cmd` is unset. A string command must not be empty and must contain 1 or 2 `%s` placeholders. A function command must return a string or a table [terminal/external.lua:M.open]
- `external.open` refuses (ERROR notify) when `provider_opts.external_terminal_cmd` is unset. A string command must not be empty and must contain 1 or 2 `%s` placeholders. With 2, the cwd `%s` (the first) must be a whole argument or an `--option=%s` (or `-o=%s`) argument. A function command must return a string or a table [terminal/external.lua:M.open]
- `external.open` runs the job in `vim.fn.getcwd()`. It calls `jobstart` through `pcall`. When `jobstart` raises (for example `E475` for a command that is not executable) or returns `0` or `-1`, it sends one ERROR notification, which includes the error text when `jobstart` raised, and clears `jobid` [terminal/external.lua:M.open]
- `external` reads `external_terminal_cmd` from the config that `setup` stored, not from the effective config. A string with 1 `%s` gets the command. A string with 2 gets the cwd, then the command. `external` formats it with `string.format` outside `pcall`, so another `%` sequence such as `%d` raises a Lua error instead of sending an ERROR notification. It splits a string with `utils.parse_command`. A function gets `(cmd_string, env_table)`, and a table it returns is used as the argv list [terminal/external.lua:M.open]
- `external` reads `external_terminal_cmd` from the config that `setup` stored, not from the effective config. A string with 1 `%s` gets the command. A string with 2 gets the cwd, then the command. `external` formats it with `string.format` outside `pcall`, so another `%` sequence such as `%d` raises a Lua error instead of sending an ERROR notification. It splits a string with `utils.parse_command`. With 2 `%s`, it formats a marker in place of the cwd and puts the cwd into the marker's argument only after the split, so no part of a folder name is parsed as arguments. A function gets `(cmd_string, env_table)`, and a table it returns is used as the argv list [terminal/external.lua:M.open]
- `external`'s `on_exit` clears the state, scheduled, only when the job that exited is still the tracked one. `simple_toggle`, `focus_toggle`, and `toggle` stop a running job instead of hiding it [terminal/external.lua:M.open] [terminal/external.lua:M.simple_toggle]
- `native` clears `bufnr`, `winid`, and `jobid` when its buffer is invalid. When the tracked window is gone or shows another buffer (for example after `:e file` in it), it recovers `winid` by scanning windows that show the buffer, and it clears `winid` when none does, so the terminal counts as hidden [terminal/native.lua:is_valid]
- `native` passes an argv list from `utils.parse_command` to `termopen`, so no shell runs. It opens the split with `topleft` when `split_side` is `"left"`, else `botright`, `floor(columns * split_width_percentage)` columns wide and the full editor height. A hidden terminal comes back in the same kind of split [terminal/native.lua:open_terminal] [terminal/native.lua:show_hidden_terminal]
Expand Down
28 changes: 24 additions & 4 deletions lua/claudecode/terminal/external.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,33 @@ function M.open(cmd_string, env_table)
elseif placeholder_count == 1 then
-- Backward compatible: just the command
full_command = string.format(external_cmd, cmd_string)
cmd_parts = utils.parse_command(full_command)
elseif placeholder_count == 2 then
-- New feature: cwd and command
-- The cwd comes from the workspace, so it goes in only after the split: a marker holds its place, and no part
-- of a folder name is parsed as arguments.
local cwd = vim.fn.getcwd()
cwd_for_jobstart = cwd
full_command = string.format(external_cmd, cwd, cmd_string)
local marker = "\1cwd\1"
cmd_parts = utils.parse_command(string.format(external_cmd, marker, cmd_string))
local cwd_args, misplaced = 0, false
for i, arg in ipairs(cmd_parts) do
if arg:find(marker, 1, true) then
-- Anywhere else, such as in a `sh -c` script, the program that gets the argument would parse the folder name.
local option = arg == marker and "" or arg:match("^(%-%-?[%w][%w_-]*=)" .. marker .. "$")
cwd_args = cwd_args + 1
misplaced = misplaced or not option
cmd_parts[i] = (option or "") .. cwd
end
end
if misplaced or cwd_args ~= 1 then
vim.notify(
"external_terminal_cmd: the cwd '%s' must be a whole argument or an --option=%s value. "
.. "Use a function for any other form.",
vim.log.levels.ERROR
)
return
end
full_command = table.concat(cmd_parts, " ")
else
vim.notify(
string.format(
Expand All @@ -110,8 +132,6 @@ function M.open(cmd_string, env_table)
)
return
end

cmd_parts = utils.parse_command(full_command)
else
vim.notify("external_terminal_cmd must be a string or function, got: " .. type(external_cmd), vim.log.levels.ERROR)
return
Expand Down
107 changes: 107 additions & 0 deletions tests/unit/terminal/external_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,113 @@ describe("claudecode.terminal.external", function()
end)
end)

describe("open with a cwd placeholder", function()
-- Folder names that utils.parse_command or a shell would read as syntax.
local folder_names = {
"plain",
"my project",
"demo -e sh -c 'touch PWNED' x",
'it\'s "q"',
"back\\slash",
"x;touch PWNED;:",
"$(touch PWNED)",
"x;touch${IFS}PWNED;:",
"odd %s name",
}
local templates = {
{
cmd = "alacritty --working-directory %s -e %s",
argv = function(cwd)
return { "alacritty", "--working-directory", cwd, "-e", "claude", "--help" }
end,
},
{
cmd = "alacritty --working-directory=%s -e %s",
argv = function(cwd)
return { "alacritty", "--working-directory=" .. cwd, "-e", "claude", "--help" }
end,
},
}

for _, template in ipairs(templates) do
for _, name in ipairs(folder_names) do
it(string.format("passes the cwd %q as one argument in %q", name, template.cmd), function()
local cwd = "/home/user/" .. name
mock_vim.fn.getcwd = spy.new(function()
return cwd
end)
external_provider.setup({ provider_opts = { external_terminal_cmd = template.cmd } })

external_provider.open("claude --help", {})

assert.spy(mock_vim.notify).was_not_called()
assert.spy(mock_vim.fn.jobstart).was_called(1)
local call_args = mock_vim.fn.jobstart.calls[1].vals
assert.are.same(template.argv(cwd), call_args[1])
assert.are.equal(cwd, call_args[2].cwd)
end)
end
end

it("refuses a cwd placeholder inside a larger argument", function()
external_provider.setup({ provider_opts = { external_terminal_cmd = "sh -c 'cd %s && exec alacritty -e %s'" } })

external_provider.open("claude", {})

assert.spy(mock_vim.notify).was_called(1)
local message, level = mock_vim.notify.calls[1].vals[1], mock_vim.notify.calls[1].vals[2]
assert.are.equal(mock_vim.log.levels.ERROR, level)
assert.is_truthy(message:find("--option=%s", 1, true))
assert.spy(mock_vim.fn.jobstart).was_not_called()
end)

it("puts the command into a larger argument as text", function()
external_provider.setup({
provider_opts = { external_terminal_cmd = "alacritty --working-directory=%s -e sh -c 'exec %s'" },
})

external_provider.open("claude --x=100%", {})

local call_args = mock_vim.fn.jobstart.calls[1].vals
assert.are.same(
{ "alacritty", "--working-directory=/cwd", "-e", "sh", "-c", "exec claude --x=100%" },
call_args[1]
)
end)

it("passes a quoted cwd placeholder as one argument", function()
mock_vim.fn.getcwd = spy.new(function()
return "/home/user/a 'b' c"
end)
external_provider.setup({ provider_opts = { external_terminal_cmd = "kitty --directory '%s' %s" } })

external_provider.open("claude", {})

local call_args = mock_vim.fn.jobstart.calls[1].vals
assert.are.same({ "kitty", "--directory", "/home/user/a 'b' c", "claude" }, call_args[1])
end)

it("keeps a quoted command placeholder one argument, as before", function()
external_provider.setup({ provider_opts = { external_terminal_cmd = "xterm --working-directory %s -e '%s'" } })

external_provider.open("claude --resume", {})

local call_args = mock_vim.fn.jobstart.calls[1].vals
assert.are.same({ "xterm", "--working-directory", "/cwd", "-e", "claude --resume" }, call_args[1])
end)
end)

describe("open with one placeholder", function()
it("keeps a quoted command placeholder one argument, as before", function()
external_provider.setup({ provider_opts = { external_terminal_cmd = "gnome-terminal -e '%s'" } })

external_provider.open("claude --resume", {})

local call_args = mock_vim.fn.jobstart.calls[1].vals
assert.are.same({ "gnome-terminal", "-e", "claude --resume" }, call_args[1])
end)
end)

describe("open with function command", function()
it("should handle function returning string", function()
local config = {
Expand Down
Loading