summaryrefslogtreecommitdiffstats
path: root/plugin
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-06-24 20:57:15 +0200
committerThomas Vanbesien <tvanbesi@proton.me>2026-06-24 20:57:15 +0200
commit49622e90708ae0987153a6e2beed967217ff2721 (patch)
tree1d2870f169c34a53b9c28d8601fed931f3ed253e /plugin
parentb393c59ad0e3d85616eeb5be3a12042d6f3dbe63 (diff)
downloadnvim-config-49622e90708ae0987153a6e2beed967217ff2721.tar.gz
nvim-config-49622e90708ae0987153a6e2beed967217ff2721.zip
refactor(nvim): rewrite highlight (now color) plugin
Diffstat (limited to 'plugin')
-rw-r--r--plugin/50-color.lua (renamed from plugin/50-highlight.lua)144
-rw-r--r--plugin/50-statusline.lua75
2 files changed, 105 insertions, 114 deletions
diff --git a/plugin/50-highlight.lua b/plugin/50-color.lua
index 3a68146..c525866 100644
--- a/plugin/50-highlight.lua
+++ b/plugin/50-color.lua
@@ -1,23 +1,34 @@
--
--- 50-highlight.lua
+-- 50-color.lua
--
--- * Creates a function to adjust highlight groups for the solarized colorscheme. It also sets popup
--- menu width and border style.
--- * Creates a function that outputs a 'tabline' compatible strings that sets the tabline
--- format and style.
--- * Creates an autocommand to trigger these functions when the background or color scheme changes.
+-- * Highlight tweaks for the solarized colorscheme, plus popup-menu and tabpage-title styling.
+-- * The lualine statusline (https://github.com/nvim-lualine/lualine.nvim), themed to match.
+-- * Registers both into dotfiles.color, which re-applies them on colorscheme / 'background' changes
+-- and once after startup.
-- * Sets the default colorscheme (solarized) and background (dark).
--
-- User commands:
-- `ColorsBgToggle`: toggle light/dark background
-- `TabRename`: rename current tab
--- `TabRenameFileName`: rename current tab with current active buffer file name
+-- `TabRenameFileName`: rename current tab to the active buffer's file name
+-- `LualineConfig`: show the resolved lualine configuration
--
+local color = require("dotfiles.color")
+local lualine = require("lualine")
+
local solarized = { blue = "#268bd2" }
--- Adjust highlights (for the solarized theme) to fit my personal tastes.
-local function adjust_solarized_highlights()
+----------------------------------------------------------------------------------------------------
+-- Highlights
+----------------------------------------------------------------------------------------------------
+
+-- Highlight applier: adjust the solarized groups to fit my personal tastes. No-ops unless solarized
+-- is the active colorscheme.
+local function apply_solarized_highlights()
+ if vim.g.colors_name ~= "solarized" then
+ return
+ end
-- Window bar style
vim.api.nvim_set_hl(0, "WinBarCwd", { fg = solarized.blue, italic = true })
vim.api.nvim_set_hl(0, "WinBarFilePath", { italic = true })
@@ -41,15 +52,10 @@ local function adjust_solarized_highlights()
vim.api.nvim_set_hl(0, "Folded", { bg = normal_bg, underline = true, update = true })
end
-local function toggle_theme()
- vim.opt.background = vim.o.background == "light" and "dark" or "light"
- vim.notify("Color theme set to " .. vim.o.background)
-end
-
--- Creates custom highlight groups for styling the tabpage title bars:
+-- Highlight applier: create custom highlight groups for styling the tabpage title bars:
-- * TabLineWinCount: number of open windows in the tabpage
-- * TabLineSelWinCount: number of open windows in the currently active tabpage
-local function create_tabpage_title_hl()
+local function apply_tabpage_title_highlights()
local normal = vim.api.nvim_get_hl(0, { name = "Normal", link = false })
local tabline = vim.api.nvim_get_hl(0, { name = "TabLine", link = false })
local tablinesel = vim.api.nvim_get_hl(0, { name = "TabLineSel", link = false })
@@ -62,6 +68,66 @@ local function create_tabpage_title_hl()
)
end
+----------------------------------------------------------------------------------------------------
+-- Statusline
+----------------------------------------------------------------------------------------------------
+
+-- Spell flag: which spell languages are enabled, or nil when 'spelllang' matches none of these.
+local function spell_status()
+ if vim.o.spelllang == "fr" then
+ return "+S(fr)"
+ elseif vim.o.spelllang == "en_us" then
+ return "+S(en)"
+ elseif vim.o.spelllang == "en_us,fr" then
+ return "+S(en,fr)"
+ end
+end
+
+-- Custom status flags: "+F" when formatting is on for the buffer, followed by the spell flag.
+local function statuses()
+ return (vim.b.format and "+F" or "") .. (vim.o.spell and spell_status() or "")
+end
+
+-- lualine builds its own section-b-backed highlights for the diff/diagnostics components
+-- (lualine_b_diff_added_normal, lualine_b_diagnostics_error_normal, ...). Stamp bold onto them with
+-- update=true so only the bold attribute changes — fg and section b's background are left untouched.
+-- Must run after lualine (re)creates the groups: initial setup, theme switch, colorscheme.
+local function bold_diff_diag()
+ for name in pairs(vim.api.nvim_get_hl(0, {})) do
+ if name:find("^lualine_b_diff") or name:find("^lualine_b_diagnostics") then
+ vim.api.nvim_set_hl(0, name, { bold = true, update = true })
+ end
+ end
+end
+
+local lualine_sections = {
+ lualine_c = { statuses, "filename" },
+ lualine_x = { "encoding", "fileformat", "filetype", "lsp_status" },
+}
+
+local lualine_options = {
+ disabled_filetypes = { "netrw", "qf" },
+ component_separators = { left = "|", right = "|" },
+ section_separators = { left = "", right = "" },
+}
+
+-- Pick the lualine theme from 'background' and (re)apply the configuration.
+local function setup()
+ lualine_options.theme = vim.o.background == "light" and "solarized_light" or "solarized_dark"
+ lualine.setup({ options = lualine_options, sections = lualine_sections })
+end
+
+-- Statusline applier: re-pick the lualine theme for the current 'background', then re-bold the
+-- diff/diagnostics groups once lualine has rebuilt them.
+local function apply_statusline_highlights()
+ setup()
+ vim.schedule(bold_diff_diag)
+end
+
+----------------------------------------------------------------------------------------------------
+-- Tabline
+----------------------------------------------------------------------------------------------------
+
-- Builds the window-count segment of a tabpage label, e.g. ` (2)[+] ` (no styling).
-- `is_modified` appends the `[+]` marker when any window in the tab has pending changes.
local function make_tab_info_label(win_count, is_modified)
@@ -87,8 +153,7 @@ local function make_tab_label(id, is_current, width)
end
tab_label = tab_label_prefix .. tab_label
local display_width = vim.fn.strdisplaywidth(tab_label)
- return (display_width > width)
- and (vim.fn.strcharpart(tab_label, 0, width - 1) .. "…")
+ return (display_width > width) and (vim.fn.strcharpart(tab_label, 0, width - 1) .. "…")
or (tab_label .. string.rep(" ", width - display_width))
end
@@ -122,48 +187,49 @@ function GetTabLine()
return s .. "%#TabLineFill#"
end
+-- Name the current tab from the command argument.
local function rename_tab(opts)
vim.t.tabname = opts.args
vim.cmd.redrawtabline()
end
+-- Name the current tab after the active buffer's file name (without extension).
local function rename_tab_to_cur_filename()
vim.t.tabname = vim.fn.expand("%:t:r")
vim.cmd.redrawtabline()
end
--- Adjust highlights to fit my personal tastes.
-local function adjust_highlights()
- if vim.g.colors_name == "solarized" then
- adjust_solarized_highlights()
- end
- create_tabpage_title_hl()
-end
-
+----------------------------------------------------------------------------------------------------
+-- Commands & init
----------------------------------------------------------------------------------------------------
-vim.api.nvim_create_autocmd("ColorScheme", {
- desc = "Adjust custom highlights",
- group = vim.g.dotfiles.augroup,
- callback = adjust_highlights,
-})
+-- Flip between the light and dark backgrounds (the colorscheme reloads, OptionSet re-themes).
+local function toggle_theme()
+ vim.opt.background = vim.o.background == "light" and "dark" or "light"
+ vim.notify("Color theme set to " .. vim.o.background)
+end
-vim.api.nvim_create_autocmd("OptionSet", {
- desc = "Adjust custom highlights on theme change",
- pattern = "background",
- group = vim.g.dotfiles.augroup,
- callback = adjust_highlights,
-})
+-- Show the resolved lualine configuration.
+local function show_lualine_config()
+ vim.notify(vim.inspect(lualine.get_config()))
+end
+
+color.register(apply_solarized_highlights)
+color.register(apply_tabpage_title_highlights)
+color.register(apply_statusline_highlights)
vim.api.nvim_create_user_command(
"ColorsBgToggle",
toggle_theme,
{ desc = "Toggle light/dark theme" }
)
-
vim.api.nvim_create_user_command("TabRename", rename_tab, { nargs = 1 })
-
vim.api.nvim_create_user_command("TabRenameFileName", rename_tab_to_cur_filename, { nargs = 0 })
+vim.api.nvim_create_user_command(
+ "LualineConfig",
+ show_lualine_config,
+ { desc = "Show lualine configuration" }
+)
-- See `:help stl-%!`
vim.o.tabline = "%!v:lua.GetTabLine()"
diff --git a/plugin/50-statusline.lua b/plugin/50-statusline.lua
deleted file mode 100644
index e1d1687..0000000
--- a/plugin/50-statusline.lua
+++ /dev/null
@@ -1,75 +0,0 @@
---
--- Status line configuration plugin
---
--- I use lualine, see https://github.com/nvim-lualine/lualine.nvim#configuring-lualine-in-initvim
-
-local lualine = require("lualine")
-
-local function spell_status()
- if vim.o.spelllang == "fr" then
- return "+S(fr)"
- elseif vim.o.spelllang == "en_us" then
- return "+S(en)"
- elseif vim.o.spelllang == "en_us,fr" then
- return "+S(en,fr)"
- end
-end
-
-local function statuses()
- return (vim.b.autoformat and "+F" or "") .. (vim.o.spell and spell_status() or "")
-end
-
--- lualine builds its own section-b-backed highlights for the diff/diagnostics
--- components (lualine_b_diff_added_normal, lualine_b_diagnostics_error_normal,
--- ...). Stamp bold onto them with update=true so only the bold attribute
--- changes -- fg and section b's background are left untouched. Must run after
--- lualine (re)creates the groups: initial setup, theme switch, colorscheme.
-local function bold_diff_diag()
- for name in pairs(vim.api.nvim_get_hl(0, {})) do
- if name:find("^lualine_b_diff") or name:find("^lualine_b_diagnostics") then
- vim.api.nvim_set_hl(0, name, { bold = true, update = true })
- end
- end
-end
-
-vim.api.nvim_create_autocmd("ColorScheme", {
- desc = "Re-bold lualine diff/diagnostics after colorscheme rebuild",
- group = vim.g.dotfiles.augroup,
- callback = function()
- vim.schedule(bold_diff_diag)
- end,
-})
-
-local lualine_sections = {
- lualine_c = { statuses, "filename" },
- lualine_x = { "encoding", "fileformat", "filetype", "lsp_status" },
-}
-
-local lualine_options = {
- disabled_filetypes = { "netrw", "qf" },
- component_separators = { left = "|", right = "|" },
- section_separators = { left = "", right = "" },
-}
-
-local function setup()
- lualine_options.theme = vim.o.background == "light" and "solarized_light" or "solarized_dark"
- lualine.setup({ options = lualine_options, sections = lualine_sections })
-end
-
--- Adjust colors when the theme (light/dark) changes
-vim.api.nvim_create_autocmd("OptionSet", {
- desc = "Adjust color scheme",
- pattern = "background",
- group = vim.g.dotfiles.augroup,
- callback = function()
- setup()
- vim.schedule(bold_diff_diag)
- end,
-})
-
-setup()
-vim.schedule(bold_diff_diag)
-
-vim.api.nvim_create_user_command("LualineConfig", function()
- vim.notify(vim.inspect(lualine.get_config()))
-end, { desc = "Show lualine configuration" })