local M = { highlight_groups = {} } local CTRL_V = string.char(22) local CTRL_S = string.char(19) local default_labels = {} local highlights = {} local modes = {} local function define_mode(text_key, label, highlight, ...) default_labels[text_key] = label highlights[text_key] = highlight M.highlight_groups[#M.highlight_groups + 1] = highlight for index = 1, select("#", ...) do modes[select(index, ...)] = text_key end end define_mode("normal", "NORMAL", "StatusModeNormal", "n", "niI", "niR", "niV") define_mode( "operator_pending", "OPERATOR", "StatusModeOperatorPending", "no", "nov", "noV", "no" .. CTRL_V ) define_mode("insert", "INSERT", "StatusModeInsert", "i", "ic", "ix") define_mode("visual", "VISUAL", "StatusModeVisual", "v", "vs") define_mode("visual_line", "V-LINE", "StatusModeVisualLine", "V", "Vs") define_mode("visual_block", "V-BLOCK", "StatusModeVisualBlock", CTRL_V, CTRL_V .. "s") define_mode("select", "SELECT", "StatusModeSelect", "s") define_mode("select_line", "S-LINE", "StatusModeSelectLine", "S") define_mode("select_block", "S-BLOCK", "StatusModeSelectBlock", CTRL_S) define_mode("replace", "REPLACE", "StatusModeReplace", "R", "Rc", "Rx") define_mode("virtual_replace", "V-REPLACE", "StatusModeVirtualReplace", "Rv", "Rvc", "Rvx") define_mode("command", "COMMAND", "StatusModeCommand", "c", "cr") define_mode("ex", "EX", "StatusModeEx", "cv", "cvr") define_mode("prompt", "PROMPT", "StatusModePrompt", "r") define_mode("more", "MORE", "StatusModeMore", "rm") define_mode("confirm", "CONFIRM", "StatusModeConfirm", "r?") define_mode("shell", "SHELL", "StatusModeShell", "!") define_mode("terminal", "TERMINAL", "StatusModeTerminal", "t") define_mode("terminal_normal", "T-NORMAL", "StatusModeTerminalNormal", "nt", "ntT") define_mode("unknown", "UNKNOWN", "StatusModeUnknown") local configured_text = {} local function escape_statusline(text) return (text:gsub("%%", "%%%%")) end ---Return whether a semantic mode text key is supported. ---@param key string ---@return boolean function M.is_text_key(key) return default_labels[key] ~= nil end ---Replace the configured semantic mode text overrides. ---@param text table function M.set_text(text) configured_text = text end ---Resolve a full mode code returned by nvim_get_mode(). ---@param mode_code string ---@return string label ---@return string highlight function M.resolve(mode_code) local text_key = modes[mode_code] or "unknown" return configured_text[text_key] or default_labels[text_key], highlights[text_key] end ---Render the mode box. ---@param mode_code? string ---@return string component function M.render(mode_code) local label, highlight = M.resolve(mode_code or vim.api.nvim_get_mode().mode) return ("%%#%s# %s %%*"):format(highlight, escape_statusline(label)) end return M