-- Every key of LLeMbas, in one table. The mappings are made from it, the bottom bar shows the keys -- of the bar that has the focus from it, and the help and a distribution's cheat sheet read it: -- a key the bar shows is a key that is bound. -- -- Each action has two forms. The chord is the LLeMbas terminal's (Ctrl+Shift+A …) and reaches -- Neovim only from a terminal that reports such keys distinctly (the kitty keyboard protocol). -- The other form always arrives: a key after the prefix (l by default), or a plain key -- inside one of the bars. Both are bound; the bar shows the one that works. local M = {} local function L() return require("llembas") end local function layout() return require("llembas.layout") end local function diff() return require("llembas.diff") end --- { id, chord, alt, modes, scope, label, desc, run, bar } --- chord the chord form, or nil. alt: the form that always arrives: "x" (global), or a --- plain key (in a bar's own buffer, or global when `scope` is nil). Either may be a list. --- modes where `alt` is mapped when it is global (default: normal). The chord is mapped in --- normal, visual and insert mode. --- scope nil: everywhere. Otherwise the bar whose buffer has the key: "tree", "settings", --- "prompt", "chat" (the transcript), "change" (a buffer with proposed changes), --- "suggestion" (while a suggestion is shown). --- bar where the bottom bar shows it, with its priority there (1 first; cut from the end): --- { code = 1 } or { tree = { 6, "code" } } to say another word there. --- show how the keys are written in the bar when that is not the key's own name. --- virtual a line of the bar that stands for keys bound elsewhere (two entries' keys in one, or --- Neovim's own arrows); `sheet = false` keeps it out of a cheat sheet that has the others. --- free needs no agent: a distribution's guard is not asked. M.table = { -- From the code to the chat, with what you are on. { id = "ask", chord = "", alt = "a", modes = { "n", "x" }, label = "ask", desc = "To the chat prompt; the selection, or else the cursor's line, comes along", run = function() L().prompt({ intent = "ask" }) end, bar = { code = 1, tree = 8, chat = 6 } }, { id = "rewrite", alt = "e", modes = { "n", "x" }, label = "rewrite", desc = "To the prompt, to have the selection or the line rewritten (shown as changes)", run = function() L().prompt({ intent = "rewrite" }) end }, { id = "add", alt = "g", modes = { "n", "x" }, label = "add", desc = "To the prompt, to have new code written at the cursor (shown as changes)", run = function() L().prompt({ intent = "add" }) end }, { id = "explain", alt = "x", modes = { "n", "x" }, label = "explain", range = true, desc = "Explain the line or the selection", run = function(first, last) L().explain(first, last) end }, { id = "fix", alt = "f", label = "fix", desc = "Fix this file's problems", run = function() L().fix() end }, -- Suggestions as you type. -- (Their keys are the plugin's settings `complete.accept` and `complete.accept_word`.) { id = "suggestion_accept", alt = "", scope = "suggestion", label = "accept suggestion", setting = "accept", desc = "Accept the suggestion (the grey text); without one, Neovim's own Tab", bar = { code = 2 } }, { id = "suggestion_word", alt = "", scope = "suggestion", label = "one word", setting = "accept_word", desc = "Accept one word of the suggestion", bar = { code = 3 } }, { id = "autocomplete", chord = "", alt = "t", label = "autocomplete", desc = "Suggestions as you type, on and off", run = function() L().autocomplete() end }, { id = "autocomplete_model", chord = "", alt = "C", label = "autocomplete model", desc = "Choose the model that writes suggestions", run = function() L().autocomplete_model() end }, -- The menu and the settings. { id = "menu", chord = "", alt = "p", label = "menu", desc = "The LLeMbas menu: every command and every setting", run = function() L().menu() end, bar = { code = 4, tree = 9, chat = 7, change = 6 } }, { id = "mode", chord = "", alt = "m", label = "mode", desc = "Switch the mode", run = function() L().cycle_mode() end, bar = { prompt = 3 } }, { id = "model", chord = "", alt = "M", label = "model", desc = "Choose the model", run = function() L().model() end, bar = { prompt = 4 } }, { id = "effort", alt = "E", label = "effort", desc = "Choose the effort", run = function() L().effort() end }, { id = "ai", chord = "", alt = "I", label = "AI on/off", free = true, desc = "LLeMbas on and off (off: a plain editor, nothing is sent)", run = function() L().ai() end }, { id = "stop", chord = "", alt = ".", label = "stop", desc = "Stop the running turn", run = function() L().stop() end, bar = { prompt = 6, chat = 5 } }, -- A proposed change. { id = "accept", chord = "", alt = "ga", scope = "change", label = "accept", desc = "Accept the proposed change under the cursor", run = function() diff().accept(false) end, bar = { change = 1 } }, { id = "reject", chord = "", alt = "gr", scope = "change", label = "reject", desc = "Reject the proposed change under the cursor", run = function() diff().reject(false) end, bar = { change = 2 } }, { id = "next_change", alt = "]h", scope = "change", label = "next", desc = "The next proposed change", run = function() diff().next_hunk() end, bar = { change = 3 } }, { id = "prev_change", alt = "[h", scope = "change", label = "previous", desc = "The previous proposed change", run = function() diff().prev_hunk() end, bar = { change = 5 } }, { id = "accept_all", chord = "", alt = "gA", scope = "change", label = "accept all", desc = "Accept every proposed change in the file", run = function() diff().accept(true) end, bar = { change = 4 } }, { id = "reject_all", alt = "gR", scope = "change", label = "reject all", desc = "Reject every proposed change in the file", run = function() diff().reject(true) end }, -- The bars. { id = "tree", chord = "", alt = "b", label = "tree", free = true, desc = "The file tree, show and hide", run = function() layout().toggle("tree") end, bar = { code = 6, tree = 10 } }, { id = "chat", chord = "", alt = "l", label = "chat", desc = "The chat (the right bar), show and hide", run = function() layout().toggle("chat") end, bar = { code = 7, chat = 8 } }, { id = "left", alt = "", modes = { "n", "i" }, label = "bars", show = "Alt+←", free = true, desc = "To the bar on the left: chat, code, tree", run = function() layout().go(-1) end, bar = { chat = { 4, "code" }, settings = { 4, "code" } } }, { id = "right", alt = "", modes = { "n", "i" }, label = "bars", show = "Alt+→", free = true, desc = "To the bar on the right: tree, code, chat", run = function() layout().go(1) end, bar = { tree = { 6, "code" } } }, { id = "bars", alt = { "", "" }, virtual = true, sheet = false, label = "bars", show = "Alt+←→", desc = "Move between the tree, the code and the chat", bar = { code = 5 } }, -- Conversations. { id = "sessions", alt = "s", label = "sessions", desc = "Earlier conversations", run = function() L().sessions() end }, { id = "new", alt = "n", label = "new", desc = "New conversation", run = function() L().new() end }, { id = "commands", alt = "c", label = "commands", desc = "Commands and skills", run = function() L().commands() end }, { id = "status", alt = "S", label = "status", free = true, desc = "Status", run = function() L().status_popup() end }, { id = "login", alt = "i", label = "sign in", free = true, desc = "Sign in", run = function() require("llembas.login").login() end }, { id = "help", alt = "?", label = "help", free = true, desc = "The plugin's commands and keys", run = function() L().help() end }, -- In the tree. { id = "tree_open", alt = { "", "", "l" }, scope = "tree", label = "open", show = "Enter", desc = "Open the file; open or close the directory (→ opens, ← closes)", bar = { tree = 1 } }, { id = "tree_close", alt = { "", "h" }, scope = "tree", label = "close", desc = "Close the directory, or go to the one above" }, { id = "tree_new", alt = "a", scope = "tree", label = "new", desc = "New file, or directory with a / at the end", bar = { tree = 2 } }, { id = "tree_rename", alt = "r", scope = "tree", label = "rename", desc = "Rename or move", bar = { tree = 3 } }, { id = "tree_delete", alt = "d", scope = "tree", label = "delete", desc = "Delete, after asking (to the trash where there is one)", bar = { tree = 4 } }, { id = "tree_filter", alt = "/", scope = "tree", label = "filter", desc = "Filter by name as you type (empty: everything again)", bar = { tree = 5 } }, { id = "tree_hidden", alt = ".", scope = "tree", label = "hidden files", desc = "Hidden files, show and hide", bar = { tree = 7 } }, { id = "tree_refresh", alt = "R", scope = "tree", label = "refresh", desc = "Read the directory again" }, -- In the settings lines. { id = "settings_change", alt = "", scope = "settings", label = "change", show = "Enter", desc = "Open the line's choices, or take the one under the cursor", bar = { settings = 1 } }, { id = "settings_move", alt = { "", "" }, virtual = true, scope = "settings", label = "move", show = "↑↓", desc = "Move between the lines and the choices", bar = { settings = 2 } }, { id = "settings_back", alt = "", scope = "settings", label = "back", show = "Esc", desc = "Close the choices; from the lines, back to the code", bar = { settings = 3 } }, -- In the prompt. { id = "send", alt = "", scope = "prompt", label = "send", show = "Enter", desc = "Send (while a turn runs: steer it)", bar = { prompt = 1 } }, { id = "intent", alt = "", scope = "prompt", label = "ask/rewrite/add", show = "Tab", desc = "In an empty prompt: what Enter will do — ask, rewrite, add", bar = { prompt = 2 } }, { id = "newline", alt = "", scope = "prompt", label = "new line", desc = "A new line in the prompt" }, { id = "back", alt = "", scope = "prompt", label = "back to code", show = "Esc", desc = "Back to the code", bar = { prompt = 5 } }, -- In the transcript. { id = "chat_open", alt = "", scope = "chat", label = "open file", show = "Enter", desc = "Open the file:line under the cursor", bar = { chat = 1 } }, { id = "chat_output", alt = "K", scope = "chat", label = "tool output", desc = "What the tool on this line printed", bar = { chat = 2 } }, { id = "chat_prompt", alt = "i", scope = "chat", label = "prompt", desc = "To the prompt", bar = { chat = 3 } }, } M.by_id = {} for _, k in ipairs(M.table) do M.by_id[k.id] = k end local state = { set = false, prefix = "l", guard = nil, seen = false, bound = {} } local function list(v) return type(v) == "table" and v or (v and { v } or {}) end --- The keys of an entry that always arrive, as a list: the table's, or the setting's it names. function M.alts(entry) if entry.setting then local set = (L().config.complete or {})[entry.setting] return (type(set) == "string" and set ~= "") and { set } or {} end return list(entry.alt) end --- The prefix of the keys that always arrive. function M.prefix() return state.prefix end --- `alt` of an entry as it is mapped: the prefix written out. function M.lhs(alt) return (alt:gsub("", state.prefix)) end --- Where the terminal says what it is: the name the chords are remembered under. local function terminal_id() local e = vim.env if e.LLEMBAS_TERM or e.LLEMBAS_TTY then return "llembas" end if e.TMUX then return "tmux:" .. (e.TERM or "") end return (e.TERM_PROGRAM and e.TERM_PROGRAM ~= "" and e.TERM_PROGRAM) or e.TERM or "" end --- Whether a terminal known to report the chords is in front: by what it says of itself. local function terminal_reports() local e = vim.env -- The LLeMbas terminal hands its own chords through while this editor is in front. if e.LLEMBAS_TERM or e.LLEMBAS_TTY then return true end -- A multiplexer in between takes the protocol away unless it is set up to pass it on. if e.TMUX or e.STY or e.ZELLIJ then return false end if e.KITTY_WINDOW_ID or e.GHOSTTY_RESOURCES_DIR then return true end local term, program = e.TERM or "", (e.TERM_PROGRAM or ""):lower() if term:find("kitty", 1, true) or term:find("ghostty", 1, true) or term:match("^foot") then return true end if program == "ghostty" or program == "rio" then return true end return false end --- Whether the chords arrive, so that the bar shows them: as set (`keys.chords`), else as seen — --- a chord that fired in this terminal proves it, and is remembered — else as the terminal says --- of itself. function M.chords() local set = type(L().config.keys) == "table" and L().config.keys.chords or nil if set == true or set == false then return set end if state.seen then return true end if require("llembas.store").terminal(terminal_id()) then return true end return terminal_reports() end --- A chord arrived: this terminal reports them. function M.saw_chord() if state.seen then return end state.seen = true pcall(function() require("llembas.store").saw_terminal(terminal_id()) end) pcall(vim.api.nvim_exec_autocmds, "User", { pattern = "LLeMbasChanged", modeline = false }) end M._terminal_id, M._terminal_reports = terminal_id, terminal_reports function M._forget_seen() state.seen = false end local NAMES = { [""] = "Enter", [""] = "Esc", [""] = "Tab", [""] = "↑", [""] = "↓", [""] = "←", [""] = "→", [""] = "Space", } --- A key as the bar writes it: is ^⇧A, is Alt+^⇧Y, la is "Spc la". function M.written(lhs) lhs = M.lhs(lhs) if NAMES[lhs] then return NAMES[lhs] end local leader = vim.g.mapleader == nil and "\\" or vim.g.mapleader local rest = lhs:match("^<[Ll]eader>(.+)$") if rest then return (leader == " " and "Spc " or leader) .. rest end local mods, key = lhs:match("^<([CMSAD%-]-)%-?([^%-<>]+)>$") if mods and mods ~= "" then local out = "" if mods:find("[MA]") then out = out .. "Alt+" end if mods:find("C") then out = out .. "^" end if mods:find("S") then out = out .. "⇧" end key = NAMES["<" .. key .. ">"] or (#key == 1 and key:upper() or key) return out .. key end return lhs end --- An entry's keys for the bar: the chord when chords arrive and it has one, else the other form. function M.shown(entry, chords) if chords == nil then chords = M.chords() end if chords and entry.chord then return M.written(list(entry.chord)[1]) end if entry.show then return entry.show end local alts = M.alts(entry) if #alts == 0 then return entry.chord and M.written(list(entry.chord)[1]) or "" end return M.written(alts[1]) end --- The keys the bottom bar shows where the focus is `where`, most needed first: --- a list of { keys, label, id }. function M.for_bar(where, chords) if chords == nil then chords = M.chords() end local out = {} for _, k in ipairs(M.table) do local at = k.bar and k.bar[where] if at then local prio, label = at, k.label if type(at) == "table" then prio, label = at[1], at[2] end out[#out + 1] = { keys = M.shown(k, chords), label = label, id = k.id, prio = prio } end end table.sort(out, function(a, b) return a.prio < b.prio end) return out end --- The rows of a cheat sheet: { id, chord, alt, where, desc } with the keys as Neovim writes them. function M.rows() local out = {} for _, k in ipairs(M.table) do local alts = {} for _, a in ipairs(M.alts(k)) do alts[#alts + 1] = M.lhs(a) end if k.sheet ~= false then out[#out + 1] = { id = k.id, chord = list(k.chord)[1], alt = table.concat(alts, " "), where = k.scope or "everywhere", desc = k.desc, modes = k.modes or { "n" }, } end end return out end local function run(entry, by_chord) return function() if by_chord then M.saw_chord() end if state.guard and not entry.free and state.guard(entry) == false then return end if entry.range then -- The selection's lines, or the cursor's. local first, last = vim.fn.line("v"), vim.fn.line(".") if not vim.api.nvim_get_mode().mode:match("^[vV\22]") then first = last end if first > last then first, last = last, first end vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) return entry.run(first, last) end return entry.run() end end --- Map the keys of the bar `scope` in its buffer, each to `handlers[id]`. The table says which --- keys; the bar's own module says what they do there. function M.bind(scope, buf, handlers, modes) for _, k in ipairs(M.table) do if k.scope == scope and not k.virtual and handlers[k.id] then for _, lhs in ipairs(list(k.alt)) do vim.keymap.set(modes and modes[k.id] or "n", lhs, handlers[k.id], { buffer = buf, nowait = true, silent = true, desc = "LLeMbas: " .. k.desc }) end end end end --- The keys that are mapped everywhere: every chord, and every other form that is not a bar's own. --- `opts`: `{ prefix = "l", guard = function(entry) … end }`; a guard that returns false --- stops the action (a distribution uses it to say what is missing). function M.setup(opts) opts = opts or {} M.clear() state.prefix = type(opts.prefix) == "string" and opts.prefix ~= "" and opts.prefix or "l" state.guard = opts.guard state.set = true local function map(modes, lhs, entry, by_chord) vim.keymap.set(modes, lhs, run(entry, by_chord), { silent = true, desc = "LLeMbas: " .. entry.desc }) for _, m in ipairs(list(modes)) do state.bound[#state.bound + 1] = { m, lhs } end end for _, k in ipairs(M.table) do if k.run then for _, lhs in ipairs(list(k.chord)) do map({ "n", "x", "i" }, lhs, k, true) end if not k.scope then for _, lhs in ipairs(list(k.alt)) do map(k.modes or "n", M.lhs(lhs), k, false) end end end end end function M.clear() for _, b in ipairs(state.bound) do pcall(vim.keymap.del, b[1], b[2]) end state.bound, state.set = {}, false end function M.is_set() return state.set end return M