From abd57e26512c7aa00ebc477274923cb204f3dc02 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 7 Jul 2026 18:03:32 +0900 Subject: [PATCH] feat(ftplugin): add Direct mode for continuous key forwarding Oneshot capture () sends exactly one key per invocation, which gets tedious for back-to-back interactions like navigating a tool's interactive menu. Direct mode repeats the same capture until , forwarding every key to the tool in between; itself is never forwarded, matching the rest of Aibo's console/prompt keymaps where the raw Esc key is reserved for Vim and never sent to the tool as a literal byte. While active, a fixed 5-line window with a thick red border is shown at the top of the console explaining the mode and how to exit it. The prompt window, if visible, is hidden for the duration and reopened in the same Normal/Insert state afterwards, since it would otherwise compete for the same space as the indicator. Also links the new per-tool vimdoc help pages (aibo-live-completion.txt, aibo-acp.txt, aibo-claude.txt, aibo-codex.txt) from the README, split out from the completion feature's design doc. --- README.md | 41 +++++++ doc/aibo.txt | 23 +++- ftplugin/aibo-console.lua | 2 + ftplugin/aibo-prompt.lua | 2 + lua/aibo/internal/console_window.lua | 15 +++ lua/aibo/internal/direct_mode.lua | 135 ++++++++++++++++++++++ lua/aibo/internal/prompt_window.lua | 24 ++++ tests/internal/test_direct_mode.lua | 160 +++++++++++++++++++++++++++ 8 files changed, 401 insertions(+), 1 deletion(-) create mode 100644 lua/aibo/internal/direct_mode.lua create mode 100644 tests/internal/test_direct_mode.lua diff --git a/README.md b/README.md index f58e5f3..d58acdc 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,43 @@ require('aibo').setup({ -- completion = { claude = true }, -- }, }, + + -- Live "/" completion sources, one key per tool. Each source probes the + -- agent's own live command/skill list -- no static table, no disk scan -- + -- so it needs no manual updates and can't drift out of sync. No prompt is + -- ever sent, so it consumes no tokens. + completion = { + -- Claude does not speak ACP itself; probes `claude` directly. Needs + -- nothing beyond a logged-in `claude` already on PATH -- no adapter, + -- no Node.js, no npm. On by default: there is no static fallback, so + -- setting this to `false` means no "/" completion at all for Claude. + claude = true, + -- claude = { + -- cmd = { "claude" }, -- command to probe, looked up on PATH + -- timeout = 10000, -- probe timeout (ms) + -- }, + -- claude = false, -- disable "/" completion for Claude entirely + + -- Codex does not speak ACP itself; probes `codex app-server` directly. + -- Needs nothing beyond `codex` already on PATH. On by default: there is + -- no static fallback, so `false` means no "/" completion for Codex. + codex = true, + -- codex = { + -- cmd = { "codex" }, + -- timeout = 10000, + -- }, + -- codex = false, -- disable "/" completion for Codex entirely + + -- Generic Agent Client Protocol (ACP) client, for agents that speak ACP + -- natively. Currently used by Gemini CLI (`gemini --acp`). On by + -- default: same reasoning as Claude/Codex -- no static fallback. + acp = true, + -- acp = { + -- cmd = { "gemini", "--acp" }, + -- timeout = 10000, + -- }, + -- acp = false, -- disable "/" completion for Gemini entirely + }, }) ``` @@ -404,6 +441,8 @@ require('aibo').setup({ > [!NOTE] > > `` enters a special mode where you can press any single key to send it to the terminal. Useful for sending arbitrary keys not mapped by default. +> +> `i` (or ``) enters Direct mode, where every key you press is forwarded to the terminal until you press ``. `` itself is never forwarded — press `` while in Direct mode to send ESC to the tool. While active, a 5-line window with a thick red border appears at the top of the console explaining the mode; the prompt window (if shown) is hidden for the duration and reopened in the same state afterwards. ### Console Buffer @@ -422,6 +461,7 @@ Most keys use the `(aibo-send)` pattern to send keys directly to the | `` | Send down arrow | `(aibo-send)` | | `` | Send up arrow | `(aibo-send)` | | `` | Send any single key (n) | `(aibo-send)` | +| `i` / `` | Enter Direct mode (n) | `(aibo-direct)` | ### Prompt Buffer @@ -434,6 +474,7 @@ Most keys use the `(aibo-send)` pattern to send keys directly to the | `` | Previous history or completion (i) | `(aibo-history-prev)` | | `` | Next history or completion (i) | `(aibo-history-next)` | | `` | Send any single key (n/i) | `(aibo-send)` | +| `i` / `` | Enter Direct mode (n/i) | `(aibo-direct)` | > [!NOTE] > `` and `` in insert mode intelligently switch between history navigation and completion menu navigation. When the completion popup menu is visible, they navigate the completion menu. When the popup is not visible, they navigate through your prompt history. diff --git a/doc/aibo.txt b/doc/aibo.txt index c6ce9a0..bf76cd8 100644 --- a/doc/aibo.txt +++ b/doc/aibo.txt @@ -624,6 +624,7 @@ Console buffer mappings: Send down arrow (n) Send up arrow (n) Send any single key (n) + i Enter Direct mode (n, also ) Prompt buffer mappings: Submit content (n) @@ -631,10 +632,19 @@ Prompt buffer mappings: Submit and close (n/i) Submit and close (n/i) Send any single key (n/i) + i Enter Direct mode (n/i, also ) Note: Press followed by any key to send that key to the terminal. This is useful for sending keys not mapped by default. +Note: Press i (or ) to enter Direct mode, where every key you +press is forwarded to the terminal until you press . itself is +never forwarded to the tool; press while in Direct mode if you need to +send ESC. While active, a 5-line window with a thick red border is shown at +the top of the console explaining the mode. If the prompt window is +visible it is hidden for the duration and reopened in the same Normal or +Insert state once Direct mode ends. + Claude tool mappings: Toggle think (n/i) Switch mode (n/i) @@ -664,10 +674,12 @@ All functionality is exposed through mappings defined in ftplugin files. These can be used to create custom key mappings. Core mappings~ - *(aibo-send)* *(aibo-submit)* + *(aibo-send)* *(aibo-direct)* *(aibo-submit)* *(aibo-jump)* *(aibo-jump-or-submit)* (aibo-send) Prefix for sending keys to terminal + (aibo-direct) Enter Direct mode: forward every key to the + terminal until is pressed (aibo-submit) Submit content to terminal (aibo-jump) Alias for (aibo-jump:tabdrop). Remap this to change the default opener. @@ -694,6 +706,15 @@ The `(aibo-send)` mapping is designed to be used in two ways: It automatically converts Vim key notation to terminal escape sequences and sends them to the underlying process. +The `(aibo-direct)` mapping (mapped to i / by default) +repeats the same single-key capture used by `(aibo-send)`, forwarding +each captured key until is pressed. is never forwarded to the +tool; use from within Direct mode to send ESC instead. While active, a +fixed 5-line window with a thick red border and the title "Direct mode" is +shown at the top of the console, explaining the mode and how to exit it. +If the prompt window is visible, it is hidden for the duration and reopened +in the same Normal or Insert state once Direct mode ends. + Common usage patterns: (aibo-send) Send ESC to terminal (aibo-send) Send interrupt signal diff --git a/ftplugin/aibo-console.lua b/ftplugin/aibo-console.lua index d267477..3f44da7 100644 --- a/ftplugin/aibo-console.lua +++ b/ftplugin/aibo-console.lua @@ -20,6 +20,8 @@ local cfg = aibo.get_buffer_config("console") if not (cfg and cfg.no_default_mappings) then local opts = { buffer = bufnr, nowait = true, silent = true } vim.keymap.set("n", "", "(aibo-send)", { buffer = bufnr, nowait = true }) + vim.keymap.set("n", "i", "(aibo-direct)", { buffer = bufnr, nowait = true }) + vim.keymap.set("n", "", "(aibo-direct)", { buffer = bufnr, nowait = true }) vim.keymap.set("n", "", "(aibo-jump-or-submit)", opts) vim.keymap.set("n", "", "(aibo-submit)", opts) vim.keymap.set("n", "", "(aibo-submit)", opts) diff --git a/ftplugin/aibo-prompt.lua b/ftplugin/aibo-prompt.lua index 25d2aa7..62faddf 100644 --- a/ftplugin/aibo-prompt.lua +++ b/ftplugin/aibo-prompt.lua @@ -17,6 +17,8 @@ local cfg = aibo.get_buffer_config("prompt") if not (cfg and cfg.no_default_mappings) then local opts = { buffer = bufnr, nowait = true, silent = true } vim.keymap.set({ "n", "i" }, "", "(aibo-send)", { buffer = bufnr, nowait = true }) + vim.keymap.set({ "n", "i" }, "i", "(aibo-direct)", { buffer = bufnr, nowait = true }) + vim.keymap.set({ "n", "i" }, "", "(aibo-direct)", { buffer = bufnr, nowait = true }) vim.keymap.set("n", "", "(aibo-submit)", opts) vim.keymap.set("n", "", "q", opts) vim.keymap.set("n", "", "(aibo-submit)q", opts) diff --git a/lua/aibo/internal/console_window.lua b/lua/aibo/internal/console_window.lua index e1f971e..88ae0d5 100644 --- a/lua/aibo/internal/console_window.lua +++ b/lua/aibo/internal/console_window.lua @@ -169,6 +169,21 @@ local function setup_mappings(bufnr) send(key) end end) + define("(aibo-direct)", "Enter Direct mode: forward every key to the tool until ", function() + local winid = vim.api.nvim_get_current_win() + local info = M.get_info_by_winid(winid) + if not info then + return + end + -- Resolve the target bufnr up front rather than inside send_fn: Direct + -- mode may close other windows (e.g. hides a visible prompt) while + -- looping, so relying on "current window" per keystroke is unreliable. + local direct_mode = require("aibo.internal.direct_mode") + direct_mode.enter(winid, function(key) + local code = aibo.resolve(key) or key + M.send(info.bufnr, code) + end) + end) define("(aibo-submit)", "Submit to the tool", function() submit("") end) diff --git a/lua/aibo/internal/direct_mode.lua b/lua/aibo/internal/direct_mode.lua new file mode 100644 index 0000000..496109e --- /dev/null +++ b/lua/aibo/internal/direct_mode.lua @@ -0,0 +1,135 @@ +--- Direct mode: continuously forward key presses to a console's tool. +--- +--- Unlike the one-shot `(aibo-send)` capture (which sends a single key +--- and returns immediately), Direct mode repeats the capture until is +--- pressed. itself is never forwarded -- it always exits Direct mode, +--- consistent with the rest of Aibo where the physical key is reserved +--- for Vim and never sent to the tool as a raw byte. +--- +--- While active, a fixed 5-line floating window is shown at the top of the +--- console explaining the mode and how to exit it. If a prompt window is +--- currently visible for the console, it is hidden for the duration and +--- reopened (in the same Normal/Insert state) once Direct mode ends. +local M = {} + +local TITLE = " Direct mode " +local HEIGHT = 5 +local CONTENT = { + "Direct mode forwards every key you press", + "directly to the tool.", + "", + " Exit Direct mode", + " Send ESC to the tool", +} + +-- Thick border, as opposed to the thin "rounded" border used elsewhere. +local BORDER = { "┏", "━", "┓", "┃", "┛", "━", "┗", "┃" } + +--- Registry of indicator windows currently shown, keyed by console winid. +--- Exposed for testing only. +local active_indicators = {} + +---@param console_winid number +---@return number indicator winid +local function open_indicator(console_winid) + local prompt = require("aibo.internal.prompt_window") + prompt.ensure_highlights() + + local width = vim.api.nvim_win_get_width(console_winid) + + local bufnr = vim.api.nvim_create_buf(false, true) + vim.bo[bufnr].bufhidden = "wipe" + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, CONTENT) + vim.bo[bufnr].modifiable = false + + local winid = vim.api.nvim_open_win(bufnr, false, { + relative = "win", + win = console_winid, + row = 0, + col = 0, + width = math.max(1, width - 2), + height = HEIGHT, + style = "minimal", + border = BORDER, + title = TITLE, + title_pos = "left", + focusable = false, + zindex = 200, + }) + vim.wo[winid].winhighlight = "FloatBorder:AiboDirectBorder,FloatTitle:AiboDirectTitle" + return winid +end + +---@param winid number? +local function close_indicator(winid) + if winid and vim.api.nvim_win_is_valid(winid) then + pcall(vim.api.nvim_win_close, winid, true) + end +end + +--- Hide a console's visible prompt window for the duration of Direct mode. +---@param console_winid number +---@return boolean had_prompt True if a prompt window was visible and hidden +---@return boolean was_insert True if the prompt itself was focused in Insert mode +local function hide_prompt(console_winid) + local prompt = require("aibo.internal.prompt_window") + local info = prompt.get_info_by_console_winid(console_winid) + if not info or not vim.api.nvim_win_is_valid(info.winid) then + return false, false + end + local was_insert = vim.api.nvim_get_current_win() == info.winid and vim.fn.mode():match("^[iR]") ~= nil + pcall(vim.api.nvim_win_close, info.winid, true) + return true, was_insert +end + +---@param console_winid number +---@param was_insert boolean +local function restore_prompt(console_winid, was_insert) + local prompt = require("aibo.internal.prompt_window") + prompt.open(console_winid, { startinsert = was_insert }) +end + +--- Enter Direct mode for a console window. +--- Repeatedly captures a single key and passes it to `send_fn`, until +--- is pressed or key capture is interrupted. is never forwarded. +--- +--- @param console_winid number The console window ID to show the indicator on +--- @param send_fn fun(key: string) Called with each captured key (Vim key notation) +function M.enter(console_winid, send_fn) + local keycode = require("aibo.internal.keycode") + + local had_prompt, was_insert = hide_prompt(console_winid) + local indicator_winid = open_indicator(console_winid) + active_indicators[console_winid] = indicator_winid + -- Paint the indicator before the first (blocking) getchar() call. + vim.cmd("redraw") + + local ok, err = pcall(function() + while true do + local key = keycode.get_single_keycode() + if not key or key == "" then + break + end + send_fn(key) + -- The tool's terminal echo of what was just sent arrives + -- asynchronously; give it a moment to land, then repaint, otherwise + -- it visibly lags by one keystroke behind what was actually typed. + vim.wait(50) + vim.cmd("redraw!") + end + end) + + close_indicator(indicator_winid) + active_indicators[console_winid] = nil + if had_prompt then + restore_prompt(console_winid, was_insert) + end + if not ok then + error(err, 0) + end +end + +-- Exposed for testing only +M._active_indicators = active_indicators + +return M diff --git a/lua/aibo/internal/prompt_window.lua b/lua/aibo/internal/prompt_window.lua index 4b515d0..3592e3b 100644 --- a/lua/aibo/internal/prompt_window.lua +++ b/lua/aibo/internal/prompt_window.lua @@ -89,6 +89,9 @@ local function setup_highlights() vim.api.nvim_set_hl(0, "AiboPromptTitle", { default = true, link = "DiagnosticInfo" }) vim.api.nvim_set_hl(0, "AiboPromptInsertBorder", { default = true, link = "DiagnosticWarn" }) vim.api.nvim_set_hl(0, "AiboPromptInsertTitle", { default = true, link = "DiagnosticWarn" }) + -- Shared with aibo.internal.direct_mode's console overlay window. + vim.api.nvim_set_hl(0, "AiboDirectBorder", { default = true, link = "DiagnosticError" }) + vim.api.nvim_set_hl(0, "AiboDirectTitle", { default = true, link = "DiagnosticError" }) end --- Ensure prompt highlight groups are defined (once per session). @@ -106,6 +109,10 @@ local function ensure_highlights() }) end +-- Exposed so aibo.internal.direct_mode can ensure the shared highlight +-- groups exist before styling its own overlay window. +M.ensure_highlights = ensure_highlights + --- Apply mode-specific highlights and winblend to prompt window. --- In Insert mode, uses AiboPromptInsertBorder/Title and lower winblend (more opaque). --- In Normal mode, uses AiboPromptBorder/Title and higher winblend (more transparent). @@ -208,6 +215,23 @@ local function setup_mappings(bufnr) send(key) end end) + define("(aibo-direct)", "Enter Direct mode: forward every key to associated console until ", function() + local winid = vim.api.nvim_get_current_win() + local info = M.get_info_by_winid(winid) + if not info or not info.console_info then + return + end + -- Resolve the target bufnr up front rather than inside send_fn: entering + -- Direct mode from the prompt closes the prompt window itself (to show + -- the indicator instead), so send_fn can no longer rely on "current + -- window" being the prompt by the time it's called from the loop. + local console_bufnr = info.console_info.bufnr + local direct_mode = require("aibo.internal.direct_mode") + direct_mode.enter(info.console_info.winid, function(key) + local code = aibo.resolve(key) or key + console.send(console_bufnr, code) + end) + end) define("(aibo-submit)", "Submit prompt to associated console", function() vim.cmd("write") end) diff --git a/tests/internal/test_direct_mode.lua b/tests/internal/test_direct_mode.lua new file mode 100644 index 0000000..6cfbd9c --- /dev/null +++ b/tests/internal/test_direct_mode.lua @@ -0,0 +1,160 @@ +local eq = MiniTest.expect.equality +local helpers = require("tests.helpers") + +local T = helpers.new_set() + +T["enter forwards captured keys to send_fn and exits on without forwarding it"] = function() + local direct_mode = require("aibo.internal.direct_mode") + + local console_bufnr = vim.api.nvim_create_buf(false, true) + local console_winid = vim.api.nvim_open_win(console_bufnr, true, { + relative = "editor", + width = 80, + height = 24, + row = 0, + col = 0, + }) + + local calls = 0 + local indicator_seen_open = false + local indicator_title = nil + local original_getchar = vim.fn.getchar + vim.fn.getchar = function() + calls = calls + 1 + if calls == 1 then + local indicator_winid = direct_mode._active_indicators[console_winid] + indicator_seen_open = indicator_winid ~= nil and vim.api.nvim_win_is_valid(indicator_winid) + if indicator_seen_open then + indicator_title = vim.api.nvim_win_get_config(indicator_winid).title[1][1] + end + return string.byte("a") + end + return 27 -- + end + + local sent = {} + direct_mode.enter(console_winid, function(key) + table.insert(sent, key) + end) + + vim.fn.getchar = original_getchar + + eq(indicator_seen_open, true) + eq(indicator_title, " Direct mode ") + eq(sent, { "a" }) + eq(direct_mode._active_indicators[console_winid], nil) + + vim.api.nvim_win_close(console_winid, true) +end + +T["enter exits immediately when the first key is "] = function() + local direct_mode = require("aibo.internal.direct_mode") + + local console_bufnr = vim.api.nvim_create_buf(false, true) + local console_winid = vim.api.nvim_open_win(console_bufnr, true, { + relative = "editor", + width = 80, + height = 24, + row = 0, + col = 0, + }) + + local original_getchar = vim.fn.getchar + vim.fn.getchar = function() + return 27 -- + end + + local sent = {} + direct_mode.enter(console_winid, function(key) + table.insert(sent, key) + end) + + vim.fn.getchar = original_getchar + + eq(sent, {}) + eq(direct_mode._active_indicators[console_winid], nil) + + vim.api.nvim_win_close(console_winid, true) +end + +T["enter hides a visible prompt window and reopens it after exiting"] = function() + local direct_mode = require("aibo.internal.direct_mode") + local prompt = require("aibo.internal.prompt_window") + local console = require("aibo.internal.console_window") + + local console_bufnr = vim.api.nvim_create_buf(false, true) + local console_winid = vim.api.nvim_open_win(console_bufnr, true, { + relative = "editor", + width = 80, + height = 24, + row = 0, + col = 0, + }) + + local original_get_info = console.get_info_by_winid + console.get_info_by_winid = function(winid) + return { + winid = winid, + bufnr = console_bufnr, + bufname = "aiboconsole://test//", + jobinfo = { cmd = "test", args = {}, job_id = 42 }, + } + end + + local prompt_info = prompt.open(console_winid, { startinsert = false }) + local prompt_bufnr = prompt_info.bufnr + + local prompt_winid_during_direct = nil + local original_getchar = vim.fn.getchar + vim.fn.getchar = function() + prompt_winid_during_direct = prompt.get_info_by_console_winid(console_winid) + return 27 -- + end + + direct_mode.enter(console_winid, function() end) + + vim.fn.getchar = original_getchar + + -- The prompt window was closed for the duration of Direct mode; the buffer + -- itself survives (bufhidden=hide) but no window shows it (winid == -1). + eq(prompt_winid_during_direct ~= nil, true) + eq(prompt_winid_during_direct.winid, -1) + + -- ...and reopened (same buffer) once Direct mode exited. + local reopened = prompt.get_info_by_console_winid(console_winid) + eq(reopened ~= nil, true) + eq(reopened.bufnr, prompt_bufnr) + eq(vim.api.nvim_win_is_valid(reopened.winid), true) + + console.get_info_by_winid = original_get_info + vim.api.nvim_win_close(console_winid, true) +end + +T["enter does nothing prompt-related when no prompt window is visible"] = function() + local direct_mode = require("aibo.internal.direct_mode") + local prompt = require("aibo.internal.prompt_window") + + local console_bufnr = vim.api.nvim_create_buf(false, true) + local console_winid = vim.api.nvim_open_win(console_bufnr, true, { + relative = "editor", + width = 80, + height = 24, + row = 0, + col = 0, + }) + + local original_getchar = vim.fn.getchar + vim.fn.getchar = function() + return 27 -- + end + + direct_mode.enter(console_winid, function() end) + + vim.fn.getchar = original_getchar + + eq(prompt.get_info_by_console_winid(console_winid), nil) + + vim.api.nvim_win_close(console_winid, true) +end + +return T