Something went wrong. Try again.
Neovim Terminal Multiplexer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698local M = {}
---@class terminals.Config---@field position? string
---@class terminals.NewOptions---@field title? string---@field cwd? string---@field group? boolean---@field position? string---@field env? table<string, string>
---@class terminals.ScopeOptions---@field position? string
---@class terminals.SendOptions---@field position? string---@field _command? table
---@class terminals.MoveOptions---@field position string
---@class terminals.Current---@field cwd string---@field cmd? string|string[]---@field title string---@field position string
---@class terminals.Entry---@field owner integer---@field group_cwd string---@field cwd string---@field position string---@field cmd? string|string[]---@field terminal snacks.terminal---@field creation_order integer---@field position_ref table---@field title? string---@field exit_status? integer---@field attention boolean---@field hiding boolean---@field intentional_close boolean---@field removed boolean---@field focused_before_leave boolean---@field window_leave_candidate boolean---@field visible_before_leave boolean---@field departure_generation integer---@field finalization_scheduled boolean---@field finalization_done boolean---@field finalization_checktime boolean---@field finalization_was_focused boolean---@field finalization_was_visible boolean
---@class terminals.Group---@field terminals terminals.Entry[]---@field active integer
local default_position = "float"
local positions = { "float", "top", "bottom", "left", "right" }local valid_positions = {}for _, position in ipairs(positions) do valid_positions[position] = trueend
local configured_position = default_position
---@type table<integer, table<string, table<string, terminals.Group>>>local registry = {}
-- Snacks recreates managed splits, so retain user-adjusted side widths per tab.---@type table<integer, table<string, integer>>local side_widths = {}
---@param owner integer---@param group_cwd string---@param position string---@return terminals.Group?local function group_for(owner, group_cwd, position) local tab_registry = registry[owner] local position_groups = tab_registry and tab_registry[position] return position_groups and position_groups[group_cwd]end
---@param owner integer---@param group_cwd string---@param position string---@return terminals.Grouplocal function ensure_group(owner, group_cwd, position) registry[owner] = registry[owner] or {} registry[owner][position] = registry[owner][position] or {} registry[owner][position][group_cwd] = registry[owner][position][group_cwd] or { terminals = {}, active = 1 } return registry[owner][position][group_cwd]end
---@param owner integer---@param group_cwd string---@param position stringlocal function delete_group(owner, group_cwd, position) local tab_registry = registry[owner] local position_groups = tab_registry and tab_registry[position] if not position_groups then return end
position_groups[group_cwd] = nil if next(position_groups) == nil then tab_registry[position] = nil if side_widths[owner] then side_widths[owner][position] = nil if next(side_widths[owner]) == nil then side_widths[owner] = nil end end end if next(tab_registry) == nil then registry[owner] = nil endend
---@param owner integer---@param position? string---@return terminals.Entry[]local function registry_entries(owner, position) local entries = {} local position_groups = registry[owner] or {} if position then position_groups = { position_groups[position] } end for _, groups in pairs(position_groups) do for _, group in pairs(groups) do for _, entry in ipairs(group.terminals) do entries[#entries + 1] = entry end end end return entriesend
---@param entries terminals.Entry[]---@param target terminals.Entry?---@return integer?local function entry_index(entries, target) for index, entry in ipairs(entries) do if entry == target then return index end endend
local next_count = 0local suppress_winleave = 0local winbar_expression = "%!v:lua.require'terminals'._winbar()"-- Snacks deletes its window augroup whenever a float is hidden, so terminal-- lifecycle handlers must remain in a plugin-owned group.local lifecycle_group = vim.api.nvim_create_augroup("terminals.nvim", { clear = true })
local normalize_path = vim.fs.normalize
---@param path string---@return booleanlocal function path_is_absolute(path) return path:sub(1, 1) == "/" or (vim.fn.has("win32") == 1 and path:match("^%a:[/\\]") ~= nil)end
---@param path string---@return string?, string[]?local function path_root_and_parts(path) path = path:gsub("\\", "/")
local root local rest local server, share, unc_rest = path:match("^//([^/]+)/([^/]+)(.*)$") local drive, drive_rest = path:match("^([A-Za-z]:)(.*)$") if server then root = "//" .. server .. "/" .. share rest = unc_rest elseif drive then root = drive:lower() rest = drive_rest elseif path:sub(1, 1) == "/" then root = "/" rest = path:sub(2) else return nil, nil end
local parts = {} for part in rest:gmatch("[^/]+") do parts[#parts + 1] = part end return root, partsend
---@param cwd string---@param path string---@return string?local function relative_path(cwd, path) local cwd_root, cwd_parts = path_root_and_parts(cwd) local path_root, path_parts = path_root_and_parts(path) if not cwd_root or not path_root or cwd_root:lower() ~= path_root:lower() then return nil end
local case_insensitive = cwd_root ~= "/" local common = 0 while common < #cwd_parts and common < #path_parts do local cwd_part = cwd_parts[common + 1] local path_part = path_parts[common + 1] if case_insensitive then cwd_part = cwd_part:lower() path_part = path_part:lower() end if cwd_part ~= path_part then break end common = common + 1 end
local parts = {} for _ = common + 1, #cwd_parts do parts[#parts + 1] = ".." end for index = common + 1, #path_parts do parts[#parts + 1] = path_parts[index] end return #parts == 0 and "." or table.concat(parts, "/")end
---@param terminal snacks.terminallocal function buf_valid(terminal) local ok, valid = pcall(terminal.buf_valid, terminal) return ok and valid == trueend
---@param terminal snacks.terminallocal function win_valid(terminal) local ok, valid = pcall(terminal.win_valid, terminal) return ok and valid == trueend
local function synchronize_tab_attention() local changed = false for _, tabpage in ipairs(vim.api.nvim_list_tabpages()) do local tabnr = vim.api.nvim_tabpage_get_number(tabpage) local ok, cwd = pcall(vim.fn.getcwd, -1, tabnr) local effective_cwd = ok and normalize_path(cwd) or nil local attention = false for _, entry in ipairs(registry_entries(tabpage)) do if entry.group_cwd == effective_cwd and entry.attention and not entry.removed and buf_valid(entry.terminal) then attention = true break end end
local current_ok, current = pcall(vim.api.nvim_tabpage_get_var, tabpage, "attention") if not current_ok or current ~= attention then vim.api.nvim_tabpage_set_var(tabpage, "attention", attention) changed = true end end
if changed then vim.cmd.redrawtabline() endend
local function redraw_attention() synchronize_tab_attention() vim.cmd.redrawstatus()end
---@param position string---@return booleanlocal function is_side(position) return position == "left" or position == "right"end
---@param position string---@return booleanlocal function is_split(position) return position == "top" or position == "bottom" or is_side(position)end
local managed_winhighlight = "Normal:NormalFloat,NormalNC:NormalFloat"
---@param owner integer---@param position string---@param terminal snacks.terminallocal function remember_side_width(owner, position, terminal) if not is_side(position) or not win_valid(terminal) then return end side_widths[owner] = side_widths[owner] or {} side_widths[owner][position] = vim.api.nvim_win_get_width(terminal.win)end
---@param terminal snacks.terminal---@return table?, booleanlocal function terminal_window_options(terminal) if type(terminal.opts) ~= "table" then return nil, false end if type(terminal.opts.win) == "table" then return terminal.opts.win, true end return terminal.opts, falseend
---@param owner integer---@param position string---@param terminal snacks.terminallocal function enforce_side_window(owner, position, terminal) if not is_side(position) then return end
local win = terminal_window_options(terminal) if win then win.wo = type(win.wo) == "table" and win.wo or {} win.wo.winfixwidth = false end
if win_valid(terminal) then pcall(vim.api.nvim_set_option_value, "winfixwidth", false, { win = terminal.win }) local width = side_widths[owner] and side_widths[owner][position] if width then pcall(vim.api.nvim_win_set_width, terminal.win, width) end endend
---@param owner integer---@param group_cwd string---@param position string---@return terminals.Group?local function prune(owner, group_cwd, position) local group = group_for(owner, group_cwd, position) if not group then return nil end
local selected = group.terminals[group.active] local old_active = group.active local terminals = {} local pruned_attention = false for _, entry in ipairs(group.terminals) do if not entry.removed and (buf_valid(entry.terminal) or (entry.finalization_scheduled and not entry.finalization_done)) then terminals[#terminals + 1] = entry else pruned_attention = pruned_attention or entry.attention entry.removed = true end end
if pruned_attention then redraw_attention() end
if #terminals == 0 then delete_group(owner, group_cwd, position) return nil end
group.terminals = terminals group.active = entry_index(terminals, selected) or math.min(old_active, #terminals) return groupend
---@param owner? integerlocal function prune_all(owner) local owners = owner and { owner } or vim.tbl_keys(registry) for _, tabpage in ipairs(owners) do local tab_registry = registry[tabpage] for _, position in ipairs(vim.tbl_keys(tab_registry or {})) do for _, group_cwd in ipairs(vim.tbl_keys(tab_registry[position])) do prune(tabpage, group_cwd, position) end end endend
---@param owner integer---@param position stringlocal function remember_visible_side_width(owner, position) if not is_side(position) then return end prune_all(owner) for _, entry in ipairs(registry_entries(owner, position)) do remember_side_width(owner, position, entry.terminal) endend
---@param entry terminals.Entry---@param select_fallback? boolean---@return terminals.Entry?local function remove_entry(entry, select_fallback) if entry.removed then return nil end entry.removed = true if entry.attention then redraw_attention() end
local group = group_for(entry.owner, entry.group_cwd, entry.position) if not group then return nil end
local removed_index = entry_index(group.terminals, entry) if not removed_index then return nil end
local fallback = group.terminals[removed_index - 1] or group.terminals[removed_index + 1] table.remove(group.terminals, removed_index)
if #group.terminals == 0 then delete_group(entry.owner, entry.group_cwd, entry.position) elseif select_fallback or removed_index == group.active then group.active = removed_index > 1 and removed_index - 1 or 1 elseif removed_index < group.active then group.active = group.active - 1 end return fallbackend
---@param callback functionlocal function without_winleave(callback) suppress_winleave = suppress_winleave + 1 local ok, err = pcall(callback) suppress_winleave = suppress_winleave - 1 if not ok then error(err, 0) endend
---@param owner integer---@param position string---@param except? snacks.terminallocal function hide_visible(owner, position, except) prune_all(owner) for _, entry in ipairs(registry_entries(owner, position)) do local terminal = entry.terminal if terminal ~= except and win_valid(terminal) then remember_side_width(owner, position, terminal) terminal:hide() end endend
---@return terminals.Entry?local function focused_entry() local owner = vim.api.nvim_get_current_tabpage() prune_all(owner) local current_win = vim.api.nvim_get_current_win() local current_buf = vim.api.nvim_get_current_buf() for _, entry in ipairs(registry_entries(owner)) do if entry.terminal.win == current_win and entry.terminal.buf == current_buf then return entry end endend
---@param previous? terminals.Entry---@param next_position stringlocal function hide_departed_float(previous, next_position) -- Managed focus changes suppress WinLeave while windows are rearranged, so -- preserve the float auto-hide contract explicitly across positions. if previous and previous.position == "float" and previous.position ~= next_position and win_valid(previous.terminal) then without_winleave(function() previous.terminal:hide() end) endend
---@param entry terminals.Entry---@param attention booleanlocal function set_attention(entry, attention) if entry.attention == attention then return end entry.attention = attention redraw_attention()end
---@param entry terminals.Entrylocal function select_entry(entry) local group = group_for(entry.owner, entry.group_cwd, entry.position) local index = group and entry_index(group.terminals, entry) if index then group.active = index endend
---@param entry terminals.Entrylocal function focus(entry) local previous = focused_entry() select_entry(entry) without_winleave(function() hide_visible(entry.owner, entry.position, entry.terminal) entry.terminal:show() enforce_side_window(entry.owner, entry.position, entry.terminal) entry.terminal:focus() end) hide_departed_float(previous, entry.position) set_attention(entry, false) return entry.terminalend
---@param entry terminals.Entry---@return booleanlocal function entry_focused(entry) return entry.terminal.win == vim.api.nvim_get_current_win() and entry.terminal.buf == vim.api.nvim_get_current_buf()end
---@param entry terminals.Entrylocal function leave_failed_terminal_mode(entry) if entry.exit_status ~= nil and entry.exit_status ~= 0 and entry_focused(entry) then vim.cmd.stopinsert() endend
---@param entry terminals.Entrylocal function clear_departing_state(entry) entry.focused_before_leave = false entry.window_leave_candidate = false entry.visible_before_leave = falseend
---@param entry terminals.Entry---@return boolean, booleanlocal function consume_departing_state(entry) local invalidated_departure = entry.window_leave_candidate and (not win_valid(entry.terminal) or not buf_valid(entry.terminal)) local was_focused = entry.focused_before_leave or entry_focused(entry) or invalidated_departure local was_visible = entry.visible_before_leave or win_valid(entry.terminal) entry.departure_generation = entry.departure_generation + 1 clear_departing_state(entry) return was_focused, was_visibleend
---@param position? string---@return integer, string, string, terminals.Entry?local function applicable_scope(position) local owner = vim.api.nvim_get_current_tabpage() local entry = focused_entry() local group_cwd = entry and entry.group_cwd or normalize_path(vim.fn.getcwd()) return owner, group_cwd, position or (entry and entry.position) or configured_position, entryend
---@param cwd? string---@param base string---@return stringlocal function resolve_cwd(cwd, base) if cwd == nil then return base end
local normalized = normalize_path(cwd) if path_is_absolute(normalized) then return normalized end return normalize_path(base .. "/" .. normalized)end
---@param entry terminals.Entry?local function focus_fallback(entry) if entry and entry.owner == vim.api.nvim_get_current_tabpage() and not entry.removed and buf_valid(entry.terminal) then focus(entry) endend
---@param owner integer---@param avoid_position string---@return integer?local function owner_execution_window(owner, avoid_position) if not vim.api.nvim_tabpage_is_valid(owner) then return nil end
local avoided = {} for _, entry in ipairs(registry_entries(owner, avoid_position)) do if win_valid(entry.terminal) then avoided[entry.terminal.win] = true end end
local windows = vim.api.nvim_tabpage_list_wins(owner) for _, win in ipairs(windows) do local win_config = vim.api.nvim_win_get_config(win) if not avoided[win] and win_config.relative == "" then return win end end return windows[1]end
---@param entry terminals.Entry?local function show_unfocused_edge_fallback(entry) if not entry or entry.removed or not vim.api.nvim_tabpage_is_valid(entry.owner) or not buf_valid(entry.terminal) then return end
local terminal = entry.terminal without_winleave(function() local function show() hide_visible(entry.owner, entry.position, terminal)
-- A hidden Snacks window normally retains the enter behavior it had at -- creation. Disable it only while restoring a position whose terminal -- exited, so an unfocused edge split stays open without stealing focus. local win = terminal_window_options(terminal) local enter = win and win.enter if win then win.enter = false end local ok, err = pcall(terminal.show, terminal) if win then win.enter = enter end if not ok then error(err, 0) end
enforce_side_window(entry.owner, entry.position, terminal) end
local execution_win = owner_execution_window(entry.owner, entry.position) if execution_win then -- Run from a regular owner-tab window even when the currently focused -- destination is a float, then restore the caller's current window. vim.api.nvim_win_call(execution_win, show) elseif entry.owner == vim.api.nvim_get_current_tabpage() then show() end end)end
local function terminal_action(key, method, description) return { key, function() M[method]() end, mode = { "n", "t" }, desc = description, }end
local function terminal_action_keys() return { term_new = terminal_action("<D-n>", "new", "New terminal"), term_close = terminal_action("<D-w>", "close", "Close terminal"), term_prev = terminal_action("<D-S-[>", "prev", "Previous terminal"), term_next = terminal_action("<D-S-]>", "next", "Next terminal"), }end
---@param title any---@return stringlocal function escape_winbar_title(title) if type(title) ~= "string" then title = title == nil and "" or tostring(title) end return (title:gsub("%c", " "):gsub("%%", "%%%%"))end
---@param cmd? string|string[]---@return string|string[]?local function copy_command(cmd) return type(cmd) == "table" and vim.deepcopy(cmd) or cmdend
---@param entry terminals.Entry---@return stringlocal function winbar_title(entry) if entry.title ~= nil then return entry.title end if type(entry.cmd) == "string" then return entry.cmd end if type(entry.cmd) == "table" then return table.concat(entry.cmd, " ") end return "terminal"end
---@param win integer---@return terminals.Entry?local function entry_for_win(win) if win == 0 or not vim.api.nvim_win_is_valid(win) then return nil end
local buf = vim.api.nvim_win_get_buf(win) local owner = vim.api.nvim_win_get_tabpage(win) for _, entry in ipairs(registry_entries(owner)) do if not entry.removed and entry.terminal.win == win and entry.terminal.buf == buf and buf_valid(entry.terminal) then return entry end end return nilend
---Render the managed terminal winbar for Neovim's target statusline window.---@return stringfunction M._winbar() local win = tonumber(vim.g.statusline_winid) or 0 local target = entry_for_win(win) local group = target and prune(target.owner, target.group_cwd, target.position) if not group then return "%#NormalFloat#%=" end
local base_highlight = entry_focused(target) and "TermBar" or "TermBarNC" local base = "%#" .. base_highlight .. "#" local parts = {} for index, entry in ipairs(group.terminals) do local title = escape_winbar_title(winbar_title(entry)) if index > 1 then parts[#parts + 1] = base .. " " end local title_highlight = "TermBarName" if index == group.active then title_highlight = entry_focused(entry) and "TermBarNameFocused" or "TermBarNameActive" end parts[#parts + 1] = "%#" .. title_highlight .. "# " parts[#parts + 1] = title parts[#parts + 1] = " " if entry.exit_status ~= nil then parts[#parts + 1] = "%#TermBarStatus# " .. entry.exit_status .. " " end if entry.attention then parts[#parts + 1] = "%#TermBarAttention# ! " end end parts[#parts + 1] = base .. "%=" return table.concat(parts)end
---@param owner integer---@param position string---@param position_ref tablelocal function window_options(owner, position, position_ref) local wo = { foldenable = false, foldmethod = "manual", winbar = winbar_expression, } wo.winhighlight = managed_winhighlight if is_side(position) then wo.winfixwidth = false end
local keys = terminal_action_keys() keys.q = false local win = { position = position, wo = wo, keys = keys, } if is_side(position) then position_ref.enforces_side = true win.width = side_widths[owner] and side_widths[owner][position] win.on_win = function(snacks_win) -- Snacks forces fixed dimensions for splits after merging window options. -- Resolve the position at callback time because move() reuses this object. local entry = position_ref.entry enforce_side_window(owner, entry and entry.position or position, snacks_win) end end return winend
local function snacks() local module = rawget(_G, "Snacks") if not module then local ok, loaded = pcall(require, "snacks") module = ok and loaded or nil end assert(module and module.terminal and module.terminal.open, "terminals.nvim requires folke/snacks.nvim") return moduleend
---@param message stringlocal function notify_error(message) snacks().notify.error(message)end
---@param message stringlocal function notify_osc(message) local title local candidate, body = message:match("^([^:]*):(.*)$") if candidate then candidate = candidate:match("^%s*(.-)%s*$") if candidate ~= "" then title = candidate message = body:gsub("^%s+", "") end end
snacks().notify.info(message ~= "" and message or "a terminal needs attention", { title = title or "terminals", })end
---@param buf integer---@param position table---@param linewise boolean---@return table?local function selection_position(buf, position, linewise) if type(position) ~= "table" then return nil end
local position_buf = tonumber(position[1]) local line = tonumber(position[2]) local column = tonumber(position[3]) if not position_buf or (position_buf ~= 0 and position_buf ~= buf) or not line or line < 1 or line > vim.api.nvim_buf_line_count(buf) then return nil end
if linewise then return { line = line, column = 1 } end if not column or column < 1 then return nil end
local text = vim.api.nvim_buf_get_lines(buf, line - 1, line, true)[1] local max_column = math.max(#text, 1) if column == vim.v.maxcol then column = max_column elseif column > max_column then return nil end return { line = line, column = column, line_length = #text }end
---@param command? table---@return table?local function visual_selection(command) local mode local start_position local end_position if command then mode = vim.fn.visualmode() start_position = vim.fn.getpos("'<") end_position = vim.fn.getpos("'>") if command.range ~= 2 or type(command.line1) ~= "number" or type(command.line2) ~= "number" or start_position[2] ~= command.line1 or end_position[2] ~= command.line2 then notify_error("TermSend must be called from Visual mode.") return nil end else mode = vim.fn.mode(1) start_position = vim.fn.getpos("v") end_position = vim.fn.getpos(".") end
if mode == "\22" then notify_error("Blockwise Visual selections are not supported.") return nil end if mode ~= "v" and mode ~= "V" then notify_error("A characterwise or linewise Visual selection is required.") return nil end
local buf = vim.api.nvim_get_current_buf() local linewise = mode == "V" local first = selection_position(buf, start_position, linewise) local last = selection_position(buf, end_position, linewise) if not first or not last then notify_error("Visual selection is missing or invalid.") return nil end if first.line > last.line or (first.line == last.line and first.column > last.column) then first, last = last, first end
local name = vim.api.nvim_buf_get_name(buf) if name == "" then notify_error("Visual selection must be in a named buffer.") return nil end return { buf = buf, path = normalize_path(vim.fn.fnamemodify(name, ":p")), linewise = linewise, first = first, last = last, }end
---@param selection table---@param cwd string---@return string?local function selection_reference(selection, cwd) local relative = relative_path(cwd, selection.path) if not relative then notify_error("Buffer and terminal cwd are on incompatible filesystem roots.") return nil end
local first = selection.first local last = selection.last local linewise = selection.linewise or (first.column == 1 and last.column == math.max(last.line_length, 1)) if linewise then if first.line == last.line then return ("%s:%d"):format(relative, first.line) end return ("%s:%d-%d"):format(relative, first.line, last.line) end return ("%s:%d:%d-%d:%d"):format(relative, first.line, first.column, last.line, last.column)end
---@param owner integer---@param position? string---@return terminals.Entry[]local function visible_entries(owner, position) prune_all(owner) local entries = {} for _, entry in ipairs(registry_entries(owner, position)) do if win_valid(entry.terminal) then entries[#entries + 1] = entry end end return entriesend
---@param terminal snacks.terminal---@return integer?local function terminal_channel(terminal) if not buf_valid(terminal) then return nil end
local ok, channel if type(vim.api.nvim_get_option_value) == "function" then ok, channel = pcall(vim.api.nvim_get_option_value, "channel", { buf = terminal.buf }) else ok, channel = pcall(vim.api.nvim_buf_get_option, terminal.buf, "channel") end if not ok or type(channel) ~= "number" or channel < 1 or channel % 1 ~= 0 then return nil end return channelend
---@param entry terminals.Entry---@param event table---@param terminal_buf integerlocal function record_departing_state(entry, event, terminal_buf) local terminal = entry.terminal if suppress_winleave > 0 or entry.hiding or entry.intentional_close or entry.removed or event.buf ~= terminal_buf then return end
local event_name = event.event local leaves_current_window = event_name == "WinLeave" if leaves_current_window then -- WinLeave also fires for an ordinary move to the editor. Keep it as a -- candidate until a buffer/window invalidation confirms that lifecycle -- teardown, rather than letting an immediate background exit steal focus. entry.window_leave_candidate = true end local confirms_teardown = event_name == "BufWinLeave" or event_name == "BufWipeout" local was_focused = confirms_teardown and (vim.api.nvim_get_current_buf() == terminal_buf or entry.window_leave_candidate) local was_visible = leaves_current_window or event_name == "BufLeave" or event_name == "BufWinLeave" or win_valid(terminal)
entry.focused_before_leave = entry.focused_before_leave or was_focused entry.visible_before_leave = entry.visible_before_leave or was_visible entry.departure_generation = entry.departure_generation + 1 local generation = entry.departure_generation
-- Ordinary focus changes and hides must not make a later background exit look -- focused. Lifecycle events from the same close/wipe transition consume the -- marker before this scheduled reset runs. vim.schedule(function() if entry.departure_generation == generation and not entry.finalization_scheduled then clear_departing_state(entry) end end)end
---@param entry terminals.Entry---@param checktime booleanlocal function schedule_finalization(entry, checktime) if entry.finalization_done or entry.intentional_close then return end
local was_focused, was_visible = consume_departing_state(entry) entry.finalization_was_focused = entry.finalization_was_focused or was_focused entry.finalization_was_visible = entry.finalization_was_visible or was_visible entry.finalization_checktime = entry.finalization_checktime or checktime
if entry.finalization_scheduled then return end entry.finalization_scheduled = true
-- TermClose can still be followed by terminal-mode teardown, window closure, -- and BufWipeout. Wait until that lifecycle stack has unwound before closing -- Snacks or installing the adjacent terminal in the vacated position. vim.schedule(function() if entry.finalization_done or entry.intentional_close then return end entry.finalization_done = true
local replace_visible_edge = entry.finalization_was_visible and is_split(entry.position) remember_side_width(entry.owner, entry.position, entry.terminal) local fallback = remove_entry(entry, entry.finalization_was_focused or replace_visible_edge)
local close_ok, close_error = pcall(function() without_winleave(function() entry.terminal:close() end) end)
if entry.finalization_was_focused then focus_fallback(fallback) elseif replace_visible_edge then show_unfocused_edge_fallback(fallback) end if entry.finalization_checktime then vim.cmd.checktime() end if not close_ok then error(close_error, 0) end end)end
---@param entry terminals.Entrylocal function attach(entry) local terminal = entry.terminal -- Keep the creation-time buffer independently of Snacks' mutable handle so -- late leave/wipe callbacks can still identify this terminal. local terminal_buf = terminal.buf
---@param events string|string[] ---@param callback function local function on_terminal_event(events, callback) vim.api.nvim_create_autocmd(events, { group = lifecycle_group, buffer = terminal_buf, callback = callback, }) end
on_terminal_event("TermRequest", function(event) local sequence = type(event.data) == "table" and event.data.sequence if type(sequence) ~= "string" then return end
local message = sequence == "\027]9" and "" or sequence:match("^\027%]9;(.*)$") if message == nil or message:match("^4;") then return end
local focused = entry_focused(entry) notify_osc(message) if focused then return end
set_attention(entry, true) end)
on_terminal_event({ "BufEnter", "WinEnter" }, function() entry.departure_generation = entry.departure_generation + 1 clear_departing_state(entry) select_entry(entry) set_attention(entry, false) end)
on_terminal_event("TermEnter", function() leave_failed_terminal_mode(entry) end)
on_terminal_event("TermClose", function(event) if entry.intentional_close then return end
local status = type(vim.v.event) == "table" and vim.v.event.status or nil if status == nil and type(event.data) == "table" then status = event.data.status end if status ~= 0 then entry.exit_status = status vim.cmd.redrawstatus() leave_failed_terminal_mode(entry) snacks().notify.error("Terminal exited with code " .. status .. ".\nCheck for any errors.") return end
schedule_finalization(entry, true) end)
on_terminal_event({ "BufLeave", "BufWinLeave" }, function(event) remember_side_width(entry.owner, entry.position, terminal) record_departing_state(entry, event, terminal_buf) end)
on_terminal_event("BufWipeout", function(event) record_departing_state(entry, event, terminal_buf) schedule_finalization(entry, false) end)
on_terminal_event("WinLeave", function(event) record_departing_state(entry, event, terminal_buf) if entry.position ~= "float" or suppress_winleave > 0 or entry.hiding or entry.removed then return end
if event.buf ~= terminal.buf or terminal.win ~= vim.api.nvim_get_current_win() or not win_valid(terminal) then return end
entry.hiding = true local ok, err = pcall(terminal.hide, terminal) entry.hiding = false if not ok then error(err, 0) end end)end
local function detach_closed_tabs() local detached = {} for _, owner in ipairs(vim.tbl_keys(registry)) do if not vim.api.nvim_tabpage_is_valid(owner) then for _, position_groups in pairs(registry[owner]) do for _, group in pairs(position_groups) do for _, entry in ipairs(group.terminals) do entry.intentional_close = true entry.removed = true entry.focused_before_leave = false entry.attention = false detached[#detached + 1] = entry end end end registry[owner] = nil side_widths[owner] = nil end end for _, owner in ipairs(vim.tbl_keys(side_widths)) do if not vim.api.nvim_tabpage_is_valid(owner) then side_widths[owner] = nil end end
if #detached == 0 then return end
redraw_attention() vim.schedule(function() without_winleave(function() local first_error for _, entry in ipairs(detached) do if buf_valid(entry.terminal) then local ok, err = pcall(entry.terminal.close, entry.terminal) if not ok then first_error = first_error or err end end end if first_error then error(first_error, 0) end end) end)end
vim.api.nvim_create_autocmd({ "TabNew", "DirChanged" }, { group = lifecycle_group, callback = synchronize_tab_attention,})vim.api.nvim_create_autocmd("TabClosed", { group = lifecycle_group, callback = detach_closed_tabs,})synchronize_tab_attention()
---Configure terminals created after this call.---@param opts? terminals.Configfunction M.setup(opts) configured_position = opts and opts.position or default_positionend
---Return a snapshot of the focused managed terminal's metadata.---@return terminals.Current?function M.current() local entry = focused_entry() if not entry then return nil end
return { cwd = entry.cwd, cmd = copy_command(entry.cmd), title = winbar_title(entry), position = entry.position, }end
---Create a terminal, focusing it when its target group directory is applicable.---@param cmd? string|string[]---@param opts? terminals.NewOptions---@return snacks.terminalfunction M.new(cmd, opts) opts = opts or {} local owner, base, position, previous = applicable_scope(opts.position) local cwd = resolve_cwd(opts.cwd, base) local group_cwd = opts.group and base or cwd local foreground = group_cwd == base local creation_cmd = copy_command(cmd) next_count = next_count + 1
local terminal local position_ref = {} without_winleave(function() remember_visible_side_width(owner, position) if foreground then hide_visible(owner, position) end local win = window_options(owner, position, position_ref) if not foreground then win.enter = false end terminal = snacks().terminal.open(cmd, { auto_close = false, cwd = cwd, count = next_count, env = opts.env, win = win, }) end) assert(terminal, "Snacks.terminal.open() did not return a terminal")
local group = ensure_group(owner, group_cwd, position) local entry = { owner = owner, group_cwd = group_cwd, cwd = cwd, position = position, cmd = creation_cmd, terminal = terminal, creation_order = next_count, position_ref = position_ref, title = opts.title, attention = false, hiding = false, intentional_close = false, removed = false, departure_generation = 0, finalization_scheduled = false, finalization_done = false, finalization_checktime = false, finalization_was_focused = false, finalization_was_visible = false, } position_ref.entry = entry clear_departing_state(entry) group.terminals[#group.terminals + 1] = entry group.active = #group.terminals attach(entry)
without_winleave(function() if foreground then terminal:show() enforce_side_window(owner, position, terminal) terminal:focus() else terminal:hide() end end) if foreground then hide_departed_float(previous, position) end return terminalend
---@param owner integer---@param position string---@param position_ref table---@return table?local function resolved_snacks_position(owner, position, position_ref) local module = snacks() if type(module.win) ~= "table" or type(module.win.resolve) ~= "function" or type(module.config) ~= "table" or type(module.config.get) ~= "function" then return nil end
-- Snacks resolves terminal and window styles before constructing its -- persistent object. Repeat that resolution without constructing a window -- so a float moved to a split (or vice versa) gets the target's dimensions. local terminal_config = module.config.get("terminal", { win = { style = "terminal" } }) local target = module.win.resolve( "terminal", { position = position }, terminal_config.win, window_options(owner, position, { entry = position_ref.entry }), { show = false } ) target = module.win.resolve(module.config.get("win", {}), target) if target.minimal ~= false then target = module.win.resolve("minimal", target) end target = module.win.resolve(position == "float" and "float" or "split", target) return targetend
local position_geometry = { "anchor", "backdrop", "border", "col", "height", "max_height", "max_width", "min_height", "min_width", "row", "width", "zindex",}
---@param entry terminals.Entry---@param position stringlocal function reconfigure_position(entry, position) entry.position = position
local win, nested = terminal_window_options(entry.terminal) if not win then return end
win.position = position if not nested then local target = resolved_snacks_position(entry.owner, position, entry.position_ref) if target then for _, option in ipairs(position_geometry) do win[option] = vim.deepcopy(target[option]) end end end
win.wo = type(win.wo) == "table" and win.wo or {} win.wo.foldenable = false win.wo.foldmethod = "manual" win.wo.winbar = winbar_expression win.wo.winhighlight = managed_winhighlight win.wo.winfixwidth = nil if is_side(position) then win.wo.winfixwidth = false end if not nested then win.wo.winfixheight = nil if is_split(position) and not is_side(position) then win.wo.winfixheight = true end end
if nested then -- This is the unresolved options shape used by Snacks.terminal.open(). -- Leave unremembered dimensions to Snacks instead of carrying a source -- side's width into an unrelated destination. win.width = is_side(position) and side_widths[entry.owner] and side_widths[entry.owner][position] or nil elseif is_side(position) then -- A real Snacks terminal stores the resolved window config directly. -- Override it only when this destination side has a live remembered width. local width = side_widths[entry.owner] and side_widths[entry.owner][position] if width then win.width = width end end
if is_side(position) and not entry.position_ref.enforces_side then local on_win = win.on_win win.on_win = function(snacks_win) if on_win then on_win(snacks_win) end enforce_side_window(entry.owner, entry.position, snacks_win) end entry.position_ref.enforces_side = true endend
---@param group terminals.Group---@param entry terminals.Entry---@return terminals.Entry?local function remove_for_move(group, entry) local index = entry_index(group.terminals, entry) if not index then return nil end
local fallback = group.terminals[index - 1] or group.terminals[index + 1] table.remove(group.terminals, index) if #group.terminals > 0 then group.active = index > 1 and index - 1 or 1 end return fallbackend
---@param group terminals.Group---@param entry terminals.Entrylocal function insert_by_creation_order(group, entry) local index = #group.terminals + 1 for candidate, existing in ipairs(group.terminals) do if entry.creation_order < existing.creation_order then index = candidate break end end table.insert(group.terminals, index, entry) group.active = indexend
---Move the focused managed terminal to another position group.---@param opts terminals.MoveOptions---@return snacks.terminal?function M.move(opts) local position = type(opts) == "table" and opts.position or nil if position == nil then notify_error("Terminal position is required.") return nil end if not valid_positions[position] then notify_error("Invalid terminal position: " .. tostring(position) .. ".") return nil end
local entry = focused_entry() if not entry then return nil end if position == entry.position then return entry.terminal end
local owner = entry.owner local group_cwd = entry.group_cwd local source_position = entry.position local source = group_for(owner, group_cwd, source_position) if not source then return nil end
local fallback without_winleave(function() -- Capture the source side before Snacks tears down its old window. remember_side_width(owner, source_position, entry.terminal) local source_width = side_widths[owner] and side_widths[owner][source_position] entry.terminal:hide()
fallback = remove_for_move(source, entry) if #source.terminals == 0 then delete_group(owner, group_cwd, source_position) if source_width then side_widths[owner] = side_widths[owner] or {} side_widths[owner][source_position] = source_width end end
local destination = prune(owner, group_cwd, position) or ensure_group(owner, group_cwd, position) reconfigure_position(entry, position) insert_by_creation_order(destination, entry)
-- This also captures the destination's latest side width before hiding it. hide_visible(owner, position, entry.terminal) reconfigure_position(entry, position) entry.terminal:show() enforce_side_window(owner, position, entry.terminal) entry.terminal:focus() end)
if is_split(source_position) then show_unfocused_edge_fallback(fallback) end return entry.terminalend
---Destroy the focused managed terminal.---@return snacks.terminal?function M.close() local entry = focused_entry() if not entry then return nil end
entry.intentional_close = true remember_side_width(entry.owner, entry.position, entry.terminal) local fallback = remove_entry(entry, true) without_winleave(function() entry.terminal:close() end) focus_fallback(fallback) return entry.terminalend
---@param offset integer---@param opts? terminals.ScopeOptions---@return snacks.terminal?local function cycle(offset, opts) local owner, group_cwd, position = applicable_scope(opts and opts.position) local group = prune(owner, group_cwd, position) if not group then return nil end
group.active = ((group.active - 1 + offset) % #group.terminals) + 1 return focus(group.terminals[group.active])end
---Select and focus the previous terminal for the applicable group directory and position.---@param opts? terminals.ScopeOptions---@return snacks.terminal?function M.prev(opts) return cycle(-1, opts)end
---Select and focus the next terminal for the applicable group directory and position.---@param opts? terminals.ScopeOptions---@return snacks.terminal?function M.next(opts) return cycle(1, opts)end
---Hide or show the selected terminal for the applicable group directory and position.---@param opts? terminals.ScopeOptions---@return snacks.terminalfunction M.toggle(opts) local owner, group_cwd, position = applicable_scope(opts and opts.position) local group = prune(owner, group_cwd, position) if not group then return M.new(nil, { position = position }) end
local entry = group.terminals[group.active] if win_valid(entry.terminal) then remember_side_width(entry.owner, entry.position, entry.terminal) without_winleave(function() entry.terminal:hide() end) return entry.terminal end return focus(entry)end
---Focus a managed terminal and insert a reference to the current Visual selection.---@param opts? terminals.SendOptions---@return snacks.terminal?function M.send(opts) opts = opts or {} local position = opts.position if position ~= nil and not valid_positions[position] then notify_error("Invalid terminal position: " .. tostring(position) .. ".") return nil end
local selection = visual_selection(opts._command) if not selection then return nil end
local owner = vim.api.nvim_get_current_tabpage() local entry local target_cwd if position then local open = visible_entries(owner, position) entry = open[1] if entry then target_cwd = entry.cwd else local _, group_cwd, resolved_position = applicable_scope(position) local group = prune(owner, group_cwd, resolved_position) entry = group and group.terminals[group.active] target_cwd = entry and entry.cwd or group_cwd end else local open = visible_entries(owner) if #open == 0 then notify_error("No open managed terminal exists; specify a position to open one.") return nil end if #open > 1 then notify_error("Multiple open managed terminals exist; specify a position.") return nil end entry = open[1] target_cwd = entry.cwd end
local reference = selection_reference(selection, target_cwd) if not reference then return nil end
local terminal if entry then terminal = focus(entry) else terminal = M.new(nil, { cwd = target_cwd, position = position }) end
local channel = terminal_channel(terminal) if not channel then notify_error("Managed terminal has no valid channel.") return nil end
local ok, err = pcall(vim.api.nvim_chan_send, channel, reference) if not ok then notify_error("Failed to send reference to managed terminal: " .. tostring(err)) return nil end return terminalend
return M