Under 120 columns the search count and the pending operator stay where stock Neovim shows them, so the bottom bar keeps its room for the keys. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
625 lines
27 KiB
Lua
625 lines
27 KiB
Lua
-- The four bars: the top bar (what LLeMbas is doing, the conversation, the settings), the bottom
|
|
-- bar (the keys for where the focus is, and Neovim's own state), the tree on the left, and on the
|
|
-- right the settings lines over the chat over the prompt. Each can be switched off; the side bars
|
|
-- fold away by themselves when the code would get too narrow.
|
|
--
|
|
-- Every row is laid out from widths that are known: a cell has a fixed width, or it is the one
|
|
-- cell that takes what is left and is cut with an ellipsis. Nothing lines up by the length of a text.
|
|
local M = {}
|
|
|
|
local function L() return require("llembas") end
|
|
local function tree() return require("llembas.tree") end
|
|
local function chat() return require("llembas.chat") end
|
|
local function lines() return require("llembas.lines") end
|
|
local function keys() return require("llembas.keys") end
|
|
local function store() return require("llembas.store") end
|
|
|
|
M.defaults = {
|
|
top = true, -- the top bar (the tabline)
|
|
bottom = true, -- the bottom bar (the statusline)
|
|
tree = true, -- the file tree, open
|
|
chat = true, -- the right bar, open
|
|
tree_width = 26,
|
|
chat_width = 42, -- kept between 34 and 50
|
|
prompt_height = 3,
|
|
min_code = 60, -- the side bars fold when the code would be narrower: the chat first, then the tree
|
|
}
|
|
|
|
local Y = {
|
|
on = false, prefs = vim.deepcopy(M.defaults), folded = { tree = false, chat = false }, forced = {},
|
|
code = nil, saved = {}, shape = nil, announced = false, applying = false,
|
|
}
|
|
|
|
-- ── text in cells ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
local function width_of(text) return vim.fn.strdisplaywidth(text) end
|
|
|
|
--- `text` in exactly `width` cells: cut with an ellipsis when longer, padded when shorter.
|
|
function M.cell(text, width, right)
|
|
text = (tostring(text or ""):gsub("[%c]+", " "))
|
|
if width <= 0 then return "" end
|
|
local w = width_of(text)
|
|
if w > width then
|
|
local out = ""
|
|
for i = 0, vim.fn.strchars(text) - 1 do
|
|
local ch = vim.fn.strcharpart(text, i, 1)
|
|
if width_of(out .. ch) > width - 1 then break end
|
|
out = out .. ch
|
|
end
|
|
text = out .. "…"
|
|
w = width_of(text)
|
|
end
|
|
local pad = (" "):rep(width - w)
|
|
return right and (pad .. text) or (text .. pad)
|
|
end
|
|
|
|
--- Lay `cells` out in `width` columns. A cell is `{ text, width = n }` or `{ text, flex = true,
|
|
--- min = n }` (one at most), with `prio` (1 stays longest; the highest number goes first when it
|
|
--- does not fit) and `hl`. Between cells stands `sep`. Returns the cells that fit, each with its
|
|
--- final `text`, and the row as plain text, which is exactly `width` cells wide.
|
|
function M.fit(cells, width, sep)
|
|
sep = sep or " │ "
|
|
local sw = width_of(sep)
|
|
local kept = {}
|
|
for _, c in ipairs(cells) do kept[#kept + 1] = c end
|
|
local function need()
|
|
local n = 2 + math.max(#kept - 1, 0) * sw
|
|
for _, c in ipairs(kept) do n = n + (c.flex and (c.min or 0) or c.width) end
|
|
return n
|
|
end
|
|
while #kept > 1 and need() > width do
|
|
local worst = 1
|
|
for i, c in ipairs(kept) do if (c.prio or 1) > (kept[worst].prio or 1) then worst = i end end
|
|
table.remove(kept, worst)
|
|
end
|
|
local left = width - need()
|
|
local out, plain = {}, " "
|
|
for i, c in ipairs(kept) do
|
|
local w = c.flex and ((c.min or 0) + math.max(left, 0)) or c.width
|
|
if not c.flex and #kept == 1 then w = math.min(w, math.max(width - 2, 0)) end
|
|
local text = M.cell(c.text, w, c.right)
|
|
out[#out + 1] = { text = text, hl = c.hl, id = c.id }
|
|
plain = plain .. text .. (i < #kept and sep or "")
|
|
end
|
|
-- No flexible cell among those kept: what is left stays empty at the end.
|
|
plain = M.cell(plain .. " ", width)
|
|
return out, plain
|
|
end
|
|
|
|
local function esc(text) return (text:gsub("%%", "%%%%")) end
|
|
|
|
local function line_of(cells, width, sep, sep_hl, base_hl)
|
|
local parts = { "%#" .. base_hl .. "# " }
|
|
for i, c in ipairs(cells) do
|
|
parts[#parts + 1] = "%#" .. (c.hl or base_hl) .. "#" .. esc(c.text)
|
|
if i < #cells then parts[#parts + 1] = "%#" .. sep_hl .. "#" .. esc(sep) end
|
|
end
|
|
parts[#parts + 1] = "%#" .. base_hl .. "#"
|
|
local used = 1
|
|
for i, c in ipairs(cells) do used = used + width_of(c.text) + (i < #cells and width_of(sep) or 0) end
|
|
parts[#parts + 1] = (" "):rep(math.max(width - used, 0))
|
|
return table.concat(parts)
|
|
end
|
|
|
|
-- ── the top bar ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
local STATE = {
|
|
idle = { "idle", "idle", "LLeMbasBarIdle" },
|
|
thinking = { "thinking", "think", "LLeMbasBarBusy" },
|
|
tools = { "tools", "tools", "LLeMbasBarBusy" },
|
|
writing = { "writing", "write", "LLeMbasBarBusy" },
|
|
waiting = { "waiting for you", "waiting", "LLeMbasBarWaiting" },
|
|
starting = { "starting", "start", "LLeMbasBarBusy" },
|
|
signed_out = { "not signed in", "sign in", "LLeMbasBarOff" },
|
|
no_cli = { "no CLI", "no CLI", "LLeMbasBarOff" },
|
|
off = { "AI off", "AI off", "LLeMbasBarOff" },
|
|
}
|
|
M.STATE_WIDTH, M.STATE_WIDTH_NARROW = 17, 9
|
|
|
|
local function short_model(ref)
|
|
if type(ref) ~= "string" or ref == "" then return "—" end
|
|
return ref:match("[^/]+$") or ref
|
|
end
|
|
|
|
local function diagnostics()
|
|
local buf = vim.api.nvim_get_current_buf()
|
|
local ok, count = pcall(vim.diagnostic.count, buf)
|
|
if not ok or type(count) ~= "table" then return "" end
|
|
local e, w = count[vim.diagnostic.severity.ERROR] or 0, count[vim.diagnostic.severity.WARN] or 0
|
|
if e == 0 and w == 0 then return "" end
|
|
return ("E%d W%d"):format(math.min(e, 99), math.min(w, 99))
|
|
end
|
|
|
|
local function branch()
|
|
local b = tree().branch()
|
|
if b and b ~= "" then return b end
|
|
local s = vim.b.minigit_summary
|
|
if type(s) == "table" and type(s.head_name) == "string" then return s.head_name end
|
|
return type(vim.b.gitsigns_head) == "string" and vim.b.gitsigns_head or ""
|
|
end
|
|
|
|
--- The cells of the top bar for `width` columns.
|
|
function M.top_cells(width)
|
|
local s = L().settings()
|
|
local narrow = width < 100
|
|
local st = STATE[s.state] or STATE.idle
|
|
local busy = s.state == "thinking" or s.state == "tools" or s.state == "writing" or s.state == "starting"
|
|
local sw = narrow and M.STATE_WIDTH_NARROW or M.STATE_WIDTH
|
|
local model_room = narrow and 10 or 16
|
|
local rest = " · " .. (s.mode or "—") .. " · " .. (s.effort or "—")
|
|
local model = short_model(s.model)
|
|
if width_of(model) > model_room then model = M.cell(model, model_room) end
|
|
local cells = {
|
|
{ id = "state", text = (busy and "◐ " or "● ") .. (narrow and st[2] or st[1]), width = sw, prio = 1, hl = st[3] },
|
|
{ id = "title", text = (s.title and s.title ~= "") and s.title or (s.session and "conversation" or "no conversation yet"),
|
|
flex = true, min = 8, prio = 3, hl = "LLeMbasBarTitle" },
|
|
{ id = "settings", text = model .. rest, width = model_room + 16, prio = 2, hl = "LLeMbasBar" },
|
|
{ id = "context", text = s.context and ("ctx %d%%"):format(s.context) or "ctx —", width = 8, prio = 5, hl = "LLeMbasBar" },
|
|
{ id = "autocomplete", text = "AC " .. (s.autocomplete and "on" or "off"), width = 6, prio = 4,
|
|
hl = s.autocomplete and "LLeMbasBar" or "LLeMbasBarOff" },
|
|
}
|
|
-- Tab pages have no line of their own under this bar: which one this is, when there are several.
|
|
local tabs = #vim.api.nvim_list_tabpages()
|
|
if tabs > 1 then
|
|
cells[#cells + 1] = { id = "tabs", text = ("tab %d/%d"):format(vim.api.nvim_tabpage_get_number(0), tabs), width = 9, prio = 4, hl = "LLeMbasBarFact" }
|
|
cells[5].prio = 5
|
|
cells[4].prio = 6
|
|
end
|
|
local b, d = branch(), diagnostics()
|
|
if b ~= "" then cells[#cells + 1] = { id = "branch", text = b, width = 14, prio = 9, hl = "LLeMbasBarFact" } end
|
|
if d ~= "" then cells[#cells + 1] = { id = "problems", text = d, width = 7, prio = 8, hl = "LLeMbasBarFact" } end
|
|
return cells
|
|
end
|
|
|
|
--- The top bar as plain text, exactly `width` cells (the screen's width when not given).
|
|
function M.top_text(width)
|
|
width = width or vim.o.columns
|
|
local _, plain = M.fit(M.top_cells(width), width)
|
|
return plain
|
|
end
|
|
|
|
--- 'tabline'.
|
|
function M.top()
|
|
local ok, out = pcall(function()
|
|
local width = vim.o.columns
|
|
local cells = M.fit(M.top_cells(width), width)
|
|
return line_of(cells, width, " │ ", "LLeMbasBarSeparator", "LLeMbasBar")
|
|
end)
|
|
return ok and out or ""
|
|
end
|
|
|
|
-- ── the bottom bar ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
local MODES = {
|
|
n = "NOR", no = "O-P", i = "INS", ic = "INS", ix = "INS", v = "VIS", V = "V-L", ["\22"] = "V-B",
|
|
s = "SEL", S = "S-L", ["\19"] = "S-B", R = "REP", Rv = "REP", c = "CMD", cv = "CMD", t = "TER", nt = "NOR",
|
|
r = "ASK", rm = "ASK", ["r?"] = "ASK", ["!"] = "RUN",
|
|
}
|
|
|
|
local function mode_word()
|
|
local m = vim.api.nvim_get_mode().mode
|
|
return MODES[m] or MODES[m:sub(1, 2)] or MODES[m:sub(1, 1)] or "???"
|
|
end
|
|
|
|
local function search_count()
|
|
if vim.v.hlsearch ~= 1 then return "" end
|
|
local ok, c = pcall(vim.fn.searchcount, { maxcount = 999, timeout = 10 })
|
|
if not ok or type(c) ~= "table" or (c.total or 0) == 0 then return "" end
|
|
if c.incomplete == 1 then return "?/?" end
|
|
return ("%d/%s"):format(c.current or 0, c.incomplete == 2 and "999+" or tostring(c.total))
|
|
end
|
|
|
|
--- Where the focus is, as the bottom bar and the keys name it: "tree", "settings", "chat" (the
|
|
--- transcript), "prompt", "change" (code with proposed changes) or "code".
|
|
function M.where(win)
|
|
win = win or vim.api.nvim_get_current_win()
|
|
if not vim.api.nvim_win_is_valid(win) then return "code" end
|
|
local buf = vim.api.nvim_win_get_buf(win)
|
|
if buf == tree().buf() then return "tree" end
|
|
if buf == lines().buf() then return "settings" end
|
|
if buf == chat().tbuf then return "chat" end
|
|
if buf == chat().ibuf then return "prompt" end
|
|
if require("llembas.diff").pending(buf) > 0 then return "change" end
|
|
return "code"
|
|
end
|
|
|
|
--- Neovim's own state, in cells of fixed width: the mode, the modified flag, the macro being
|
|
--- recorded, the search count, the cursor's line and column, and last the pending operator.
|
|
--- `%l`, `%v` and `%S` are the statusline's own items, so they are as live as Neovim's. `%S` is
|
|
--- the one item whose width Neovim decides (an empty one takes no cell at all), so it stands at
|
|
--- the very end, in the five cells kept for it, where it moves nothing else.
|
|
--- Under 120 columns the keys need the room: the search count and the pending operator are then
|
|
--- where stock Neovim shows them, in the command line's row ('showcmdloc' is "last").
|
|
local function vim_segment(width)
|
|
local rec = vim.fn.reg_recording()
|
|
local mode = mode_word()
|
|
local modified = vim.bo.modified and "+" or " "
|
|
local macro = rec ~= "" and ("@" .. rec) or " "
|
|
if width < 60 then return 15, (" %s %%5l:%%-3v "):format(mode), (" %s "):format(mode) end
|
|
if width < 120 then
|
|
return 20, (" %s %s %s %%5l:%%-3v "):format(mode, modified, macro), (" %s %s %s "):format(mode, modified, macro)
|
|
end
|
|
local search = M.cell(search_count(), 8, true)
|
|
return 35, (" %s %s %s %s %%5l:%%-3v %%<%%.5S"):format(mode, modified, macro, esc(search)),
|
|
(" %s %s %s %s "):format(mode, modified, macro, search)
|
|
end
|
|
|
|
--- The keys of the bottom bar for the focus `where`, cut to `room` cells by priority: a list of
|
|
--- { keys, label } and the text.
|
|
function M.bottom_keys(where, room, chords)
|
|
local list = keys().for_bar(where, chords)
|
|
local function text_of(items)
|
|
local parts = {}
|
|
for _, k in ipairs(items) do parts[#parts + 1] = k.keys .. " " .. k.label end
|
|
return " " .. table.concat(parts, " · ")
|
|
end
|
|
while #list > 0 and width_of(text_of(list)) > room do table.remove(list) end
|
|
return list, #list > 0 and text_of(list) or ""
|
|
end
|
|
|
|
--- The bottom bar's keys as plain text for the focus `where` (now, when not given), cut to the
|
|
--- screen's width less Neovim's own segment.
|
|
function M.bottom_text(where, width, chords)
|
|
width = width or vim.o.columns
|
|
local seg = vim_segment(width)
|
|
local _, text = M.bottom_keys(where or M.where(), width - seg, chords)
|
|
return text
|
|
end
|
|
|
|
--- 'statusline'.
|
|
function M.bottom()
|
|
local ok, out = pcall(function()
|
|
local width = vim.o.columns
|
|
local seg, items = vim_segment(width)
|
|
local room = width - seg
|
|
local list = M.bottom_keys(M.where(), room)
|
|
local parts, used = { "%#LLeMbasBar# " }, 1
|
|
for i, k in ipairs(list) do
|
|
local tail = " " .. k.label .. (i < #list and " · " or "")
|
|
parts[#parts + 1] = "%#LLeMbasBarKey#" .. esc(k.keys) .. "%#LLeMbasBar#" .. esc(tail)
|
|
used = used + width_of(k.keys) + width_of(tail)
|
|
end
|
|
parts[#parts + 1] = (" "):rep(math.max(room - used, 0))
|
|
parts[#parts + 1] = "%#LLeMbasBarVim#" .. items
|
|
return table.concat(parts)
|
|
end)
|
|
return ok and out or ""
|
|
end
|
|
|
|
-- ── the windows ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
local function is_bar(win)
|
|
local buf = vim.api.nvim_win_get_buf(win)
|
|
return buf == tree().buf() or buf == lines().buf() or buf == chat().tbuf or buf == chat().ibuf
|
|
end
|
|
|
|
local function floating(win) return vim.api.nvim_win_get_config(win).relative ~= "" end
|
|
|
|
--- A window beside the code that is not where a file is opened: a terminal, a quickfix list.
|
|
local function aside(win)
|
|
local bt = vim.bo[vim.api.nvim_win_get_buf(win)].buftype
|
|
return bt == "terminal" or bt == "quickfix" or bt == "prompt"
|
|
end
|
|
|
|
--- The window the code is in: the last one that had the focus and is not a bar.
|
|
function M.code_win()
|
|
if Y.code and vim.api.nvim_win_is_valid(Y.code) and not is_bar(Y.code) and not floating(Y.code) and not aside(Y.code)
|
|
and vim.api.nvim_win_get_tabpage(Y.code) == vim.api.nvim_get_current_tabpage() then
|
|
return Y.code
|
|
end
|
|
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
|
if not is_bar(w) and not floating(w) and not aside(w) then
|
|
Y.code = w
|
|
return w
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function clamp(n, lo, hi) return math.max(lo, math.min(hi, n)) end
|
|
|
|
--- Which side bars are shown at this size: what is wanted, less what does not leave the code
|
|
--- `min_code` columns — the chat goes first, then the tree. Returns { tree, chat } and the
|
|
--- widths. A bar the person opened by hand at this size stays.
|
|
function M.plan(columns, rows)
|
|
columns, rows = columns or vim.o.columns, rows or vim.o.lines
|
|
local p = Y.prefs
|
|
local tw, cw = clamp(p.tree_width, 16, 60), clamp(p.chat_width, 34, 50)
|
|
local ai = L().settings().ai
|
|
local want = { tree = p.tree, chat = p.chat and ai }
|
|
local function code(t, c) return columns - (t and (tw + 1) or 0) - (c and (cw + 1) or 0) end
|
|
local folded = { tree = false, chat = false }
|
|
if want.chat and not Y.forced.chat and code(want.tree, true) < p.min_code then folded.chat = true end
|
|
local chat_on = want.chat and not folded.chat
|
|
if want.tree and not Y.forced.tree and code(true, chat_on) < p.min_code then folded.tree = true end
|
|
local tree_on = want.tree and not folded.tree
|
|
-- A screen too low for a chat with its prompt: the chat goes.
|
|
if chat_on and rows < 10 and not Y.forced.chat then chat_on, folded.chat = false, true end
|
|
return { tree = tree_on, chat = chat_on, folded = folded, tree_width = tw, chat_width = cw,
|
|
lines = rows >= 18, prompt_height = rows >= 18 and p.prompt_height or 1 }
|
|
end
|
|
|
|
--- Open and close the side bars as `plan` says.
|
|
function M.apply()
|
|
if not Y.on or Y.applying then return end
|
|
Y.applying = true
|
|
local ok, err = pcall(function()
|
|
local plan = M.plan()
|
|
Y.folded = plan.folded
|
|
M.code_win()
|
|
if plan.tree then
|
|
local w = tree().open(plan.tree_width)
|
|
if w and vim.api.nvim_win_get_width(w) ~= plan.tree_width then
|
|
pcall(vim.api.nvim_win_set_width, w, plan.tree_width)
|
|
end
|
|
else
|
|
tree().close()
|
|
end
|
|
local shape = ("%s:%d:%d"):format(tostring(plan.lines), plan.prompt_height, plan.chat_width)
|
|
if plan.chat then
|
|
if chat().is_open() and Y.shape ~= shape then chat().close() end
|
|
if not chat().is_open() then
|
|
Y.shape = shape
|
|
L()._open_bar({
|
|
width = plan.chat_width, position = "right", prompt_height = plan.prompt_height,
|
|
bars = true, lines = plan.lines,
|
|
})
|
|
end
|
|
elseif chat().is_open() then
|
|
chat().close()
|
|
end
|
|
end)
|
|
Y.applying = false
|
|
if not ok then vim.schedule(function() vim.notify("LLeMbas: the bars could not be laid out: " .. tostring(err), vim.log.levels.WARN) end) end
|
|
pcall(vim.cmd, "redrawtabline")
|
|
pcall(vim.cmd, "redrawstatus")
|
|
-- The windows have their sizes: anything drawn to fit one may draw again.
|
|
pcall(vim.api.nvim_exec_autocmds, "User", { pattern = "LLeMbasLayout", modeline = false })
|
|
end
|
|
|
|
--- Whether a side bar is on the screen now.
|
|
function M.shown(which)
|
|
if which == "tree" then return tree().is_open() end
|
|
if which == "chat" then return chat().is_open() end
|
|
return Y.prefs[which] == true
|
|
end
|
|
|
|
local function remember(key, value) pcall(function() store().set(key, value) end) end
|
|
|
|
--- Show or hide a bar: "tree", "chat", "top", "bottom". `on` nil: the other of the two. A side
|
|
--- bar asked for at a width where it would be folded away is shown all the same.
|
|
function M.toggle(which, on)
|
|
if which ~= "tree" and which ~= "chat" and which ~= "top" and which ~= "bottom" then return end
|
|
if not Y.on then
|
|
if which == "chat" then return L().toggle() end
|
|
if which == "tree" then
|
|
if tree().is_open() then tree().close() else tree().open(Y.prefs.tree_width) end
|
|
end
|
|
return
|
|
end
|
|
if which == "top" or which == "bottom" then
|
|
if on == nil then on = not Y.prefs[which] end
|
|
Y.prefs[which] = on
|
|
remember(which, on)
|
|
return M.bars()
|
|
end
|
|
local is = M.shown(which)
|
|
if on == nil then on = not is end
|
|
Y.prefs[which] = on
|
|
Y.forced[which] = on and true or nil
|
|
remember(which, on)
|
|
if which == "chat" and on and not L().settings().ai then
|
|
return vim.notify("LLeMbas is off (" .. keys().shown(keys().by_id.ai) .. " turns it on)", vim.log.levels.INFO)
|
|
end
|
|
M.apply()
|
|
end
|
|
|
|
--- Put the focus in a bar: "tree", "code" or "chat" (the prompt). A side bar that is hidden is shown.
|
|
function M.focus(which)
|
|
if which == "tree" then
|
|
if not tree().is_open() then M.toggle("tree", true) end
|
|
local w = tree().win()
|
|
if w then
|
|
vim.cmd("stopinsert")
|
|
vim.api.nvim_set_current_win(w)
|
|
end
|
|
elseif which == "chat" then
|
|
if not L().settings().ai then
|
|
return vim.notify("LLeMbas is off (" .. keys().shown(keys().by_id.ai) .. " turns it on)", vim.log.levels.INFO)
|
|
end
|
|
if not chat().is_open() then
|
|
if Y.on then M.toggle("chat", true) else L().open(true) end
|
|
end
|
|
chat().focus_prompt()
|
|
else
|
|
local w = M.code_win()
|
|
if w then
|
|
vim.cmd("stopinsert")
|
|
vim.api.nvim_set_current_win(w)
|
|
end
|
|
end
|
|
end
|
|
|
|
local ORDER = { "tree", "code", "chat" }
|
|
|
|
--- Move the focus one bar to the left (-1) or to the right (1): tree, code, chat.
|
|
function M.go(step)
|
|
local where = M.where()
|
|
local at = (where == "tree" and 1) or ((where == "code" or where == "change") and 2) or 3
|
|
local to = clamp(at + step, 1, 3)
|
|
if to == at then return end
|
|
M.focus(ORDER[to])
|
|
end
|
|
|
|
-- ── the terminal ───────────────────────────────────────────────────────────────────────────────
|
|
|
|
--- The bytes that set the terminal's user var `llembas_ide` to `value` ("" clears it): iTerm2's
|
|
--- OSC 1337 SetUserVar, which WezTerm and the LLeMbas terminal read. Inside tmux it is wrapped
|
|
--- so that tmux passes it on.
|
|
function M.uservar_bytes(value, tmux)
|
|
local seq = "\27]1337;SetUserVar=llembas_ide=" .. vim.base64.encode(value or "") .. "\7"
|
|
if tmux then seq = "\27Ptmux;" .. seq:gsub("\27", "\27\27") .. "\27\\" end
|
|
return seq
|
|
end
|
|
|
|
local function in_llembas_terminal()
|
|
local e = vim.env
|
|
return (e.LLEMBAS_TERM ~= nil and e.LLEMBAS_TERM ~= "") or (e.LLEMBAS_TTY ~= nil and e.LLEMBAS_TTY ~= "")
|
|
end
|
|
|
|
--- Tell the LLeMbas terminal that this editor is the program in front (`on`), or no longer is:
|
|
--- it hands its own chords (Ctrl+Shift+A …) through to the program that says `llembas_ide=1`.
|
|
--- Written only inside that terminal (`$LLEMBAS_TERM` or `$LLEMBAS_TTY`), and nowhere else.
|
|
function M.announce(on)
|
|
if not in_llembas_terminal() then return false end
|
|
if on == Y.announced then return true end
|
|
Y.announced = on
|
|
-- Standard error is the terminal itself, whatever Neovim draws with (as Neovim's own OSC 52 does).
|
|
pcall(vim.api.nvim_chan_send, 2, M.uservar_bytes(on and "1" or "", vim.env.TMUX ~= nil and vim.env.TMUX ~= ""))
|
|
return true
|
|
end
|
|
|
|
-- ── setting up ─────────────────────────────────────────────────────────────────────────────────
|
|
|
|
--- Put the top and the bottom bar where the settings say, and give back what was there when one
|
|
--- is switched off.
|
|
function M.bars()
|
|
local p = Y.prefs
|
|
if Y.on and p.top then
|
|
if not Y.saved.tabline then Y.saved.tabline = { vim.o.tabline, vim.o.showtabline } end
|
|
vim.o.tabline = "%!v:lua.require'llembas.layout'.top()"
|
|
vim.o.showtabline = 2
|
|
elseif Y.saved.tabline then
|
|
vim.o.tabline, vim.o.showtabline = Y.saved.tabline[1], Y.saved.tabline[2]
|
|
Y.saved.tabline = nil
|
|
end
|
|
if Y.on and p.bottom then
|
|
if not Y.saved.statusline then Y.saved.statusline = { vim.o.statusline, vim.o.laststatus, vim.o.showcmdloc } end
|
|
vim.o.statusline = "%!v:lua.require'llembas.layout'.bottom()"
|
|
vim.o.laststatus = 3
|
|
-- The pending operator: the bar's %S where the bar is wide enough to have a cell for it,
|
|
-- otherwise in the command line's row, as Neovim itself shows it.
|
|
vim.o.showcmdloc = vim.o.columns >= 120 and "statusline" or "last"
|
|
elseif Y.saved.statusline then
|
|
vim.o.statusline, vim.o.laststatus, vim.o.showcmdloc = Y.saved.statusline[1], Y.saved.statusline[2], Y.saved.statusline[3]
|
|
Y.saved.statusline = nil
|
|
end
|
|
pcall(vim.cmd, "redrawtabline")
|
|
pcall(vim.cmd, "redrawstatus")
|
|
pcall(vim.api.nvim_exec_autocmds, "User", { pattern = "LLeMbasBars", modeline = false })
|
|
end
|
|
|
|
--- What the layout is set to now (a copy), with what is folded away at this size.
|
|
function M.options()
|
|
local out = vim.deepcopy(Y.prefs)
|
|
out.on, out.folded = Y.on, vim.deepcopy(Y.folded)
|
|
return out
|
|
end
|
|
|
|
local function widths_changed()
|
|
if not Y.on then return end
|
|
local tw, cw = tree().win(), chat().is_open() and vim.fn.bufwinid(chat().tbuf) or -1
|
|
if tw and not Y.folded.tree then
|
|
local w = vim.api.nvim_win_get_width(tw)
|
|
if w ~= Y.prefs.tree_width and w >= 16 and w <= 60 then
|
|
Y.prefs.tree_width = w
|
|
tree().width = w
|
|
remember("tree_width", w)
|
|
end
|
|
end
|
|
if cw and cw ~= -1 and not Y.folded.chat then
|
|
local w = vim.api.nvim_win_get_width(cw)
|
|
if w ~= Y.prefs.chat_width and w >= 34 and w <= 50 then
|
|
Y.prefs.chat_width = w
|
|
remember("chat_width", w)
|
|
end
|
|
end
|
|
end
|
|
|
|
--- Remember the side bars' widths as they are now (also done when Neovim sees a window resized,
|
|
--- and on the way out).
|
|
function M.remember_widths() widths_changed() end
|
|
|
|
--- Close the side bars, for something that must see only the code's windows (saving a session);
|
|
--- `M.apply()` brings them back.
|
|
function M.hide()
|
|
tree().close()
|
|
chat().close()
|
|
end
|
|
|
|
--- Turn the layout on with `opts` (see `M.defaults`; `false` turns it off). `opts` are what a
|
|
--- directory starts with. What the person then changed by hand there — which bars are shown, how
|
|
--- wide the side bars are — is remembered for that directory and holds the next time.
|
|
function M.setup(opts)
|
|
if opts == false then
|
|
Y.on = false
|
|
pcall(vim.api.nvim_del_augroup_by_name, "llembas-layout")
|
|
M.bars()
|
|
return
|
|
end
|
|
Y.given = vim.tbl_extend("force", Y.given or {}, type(opts) == "table" and opts or {})
|
|
local prefs = vim.tbl_extend("force", vim.deepcopy(M.defaults), Y.given)
|
|
local remembered = store().get()
|
|
for _, k in ipairs({ "top", "bottom", "tree", "chat", "tree_width", "chat_width" }) do
|
|
if remembered[k] ~= nil then prefs[k] = remembered[k] end
|
|
end
|
|
Y.prefs = prefs
|
|
Y.on = true
|
|
require("llembas.ui").setup_highlights()
|
|
M.bars()
|
|
|
|
local group = vim.api.nvim_create_augroup("llembas-layout", { clear = true })
|
|
local function redraw()
|
|
pcall(vim.cmd, "redrawtabline")
|
|
pcall(vim.cmd, "redrawstatus")
|
|
end
|
|
vim.api.nvim_create_autocmd("User", { group = group, pattern = "LLeMbasChanged", callback = function()
|
|
-- AI switched on or off changes what is laid out.
|
|
local ai = L().settings().ai
|
|
if Y.ai ~= ai then
|
|
Y.ai = ai
|
|
vim.schedule(M.apply)
|
|
end
|
|
redraw()
|
|
end })
|
|
vim.api.nvim_create_autocmd({ "WinEnter", "BufEnter", "ModeChanged", "DiagnosticChanged", "RecordingEnter", "RecordingLeave" },
|
|
{ group = group, callback = function()
|
|
local w = vim.api.nvim_get_current_win()
|
|
if not is_bar(w) and not floating(w) and not aside(w) then Y.code = w end
|
|
vim.schedule(redraw)
|
|
end })
|
|
vim.api.nvim_create_autocmd("VimResized", { group = group, callback = function()
|
|
-- A new size is judged anew: what was opened by hand at the old one no longer holds.
|
|
Y.forced = {}
|
|
if Y.saved.statusline then vim.o.showcmdloc = vim.o.columns >= 120 and "statusline" or "last" end
|
|
vim.schedule(M.apply)
|
|
end })
|
|
vim.api.nvim_create_autocmd({ "WinResized", "VimLeavePre" }, { group = group, callback = function()
|
|
if not Y.applying then widths_changed() end
|
|
end })
|
|
vim.api.nvim_create_autocmd("QuitPre", { group = group, callback = function()
|
|
-- Leaving the last window of code leaves the editor: the bars do not keep it open.
|
|
local cur = vim.api.nvim_get_current_win()
|
|
if is_bar(cur) or floating(cur) then return end
|
|
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
|
if w ~= cur and not is_bar(w) and not floating(w) then return end
|
|
end
|
|
if #vim.api.nvim_list_tabpages() > 1 then return end
|
|
Y.applying = true
|
|
M.hide()
|
|
Y.applying = false
|
|
-- A quit that did not happen (unsaved changes, a no to the question): the bars come back.
|
|
vim.defer_fn(function() if Y.on then M.apply() end end, 60)
|
|
end })
|
|
Y.ai = L().settings().ai
|
|
if vim.v.vim_did_enter == 1 then
|
|
M.apply()
|
|
else
|
|
vim.api.nvim_create_autocmd("VimEnter", { group = group, once = true, callback = function() vim.schedule(M.apply) end })
|
|
end
|
|
end
|
|
|
|
M._state = Y
|
|
|
|
return M
|