diff --git a/lua/iroddis/cfg_hlmatch.lua b/lua/iroddis/cfg_hlmatch.lua new file mode 100644 index 0000000..4647f64 --- /dev/null +++ b/lua/iroddis/cfg_hlmatch.lua @@ -0,0 +1,7 @@ +local mh = require('iroddis.hlmatch') +vim.keymap.set('n', 'm1', function() mh.highlight_last_search('MatchAdd_1') end) +vim.keymap.set('n', 'm2', function() mh.highlight_last_search('MatchAdd_2') end) +vim.keymap.set('n', 'm3', function() mh.highlight_last_search('MatchAdd_3') end) +vim.keymap.set('n', 'm4', function() mh.highlight_last_search('MatchAdd_4') end) +vim.keymap.set('n', 'm5', function() mh.highlight_last_search('MatchAdd_5') end) +vim.keymap.set('n', 'mc', function() vim.fn.clearmatches() end) diff --git a/lua/iroddis/cmatches.lua b/lua/iroddis/cmatches.lua deleted file mode 100644 index 042039e..0000000 --- a/lua/iroddis/cmatches.lua +++ /dev/null @@ -1,56 +0,0 @@ --- Define a function to highlight the current search term -local highlight_search_term = function(label) - local search_term = vim.fn.getreg '/' - if search_term ~= '' then - -- local matches = - vim.fn.matchadd(label, search_term) - -- for match_id in matches do - -- vim.api.nvim_buf_add_highlight(0, -1, label, 0, match_id[1] - 1, match_id[2]) - -- end - end -end - -vim.keymap.set('n', 'm1', function() - highlight_search_term 'Matchadd_1' -end) -vim.keymap.set('n', 'm2', function() - highlight_search_term 'Matchadd_2' -end) -vim.keymap.set('n', 'm3', function() - highlight_search_term 'Matchadd_3' -end) -vim.keymap.set('n', 'm4', function() - highlight_search_term 'Matchadd_4' -end) -vim.keymap.set('n', 'm5', function() - highlight_search_term 'Matchadd_5' -end) -vim.keymap.set('n', 'mc', function() - vim.fn.clearmatches() -end) - -local colors = { - base03 = '#002b36', - base02 = '#073642', - base01 = '#586e75', - base00 = '#657b83', - base0 = '#839496', - base1 = '#93a1a1', - base2 = '#eee8d5', - base3 = '#fdf6e3', - yellow = '#b58900', - orange = '#cb4b16', - red = '#dc322f', - magenta = '#d33682', - violet = '#6c71c4', - blue = '#268bd2', - cyan = '#2aa198', - green = '#859900', -} - -vim.api.nvim_set_hl(0, 'Matchadd_1', { bg = colors.blue, fg = 0 }) -vim.api.nvim_set_hl(0, 'Matchadd_2', { bg = colors.violet, fg = 0 }) -vim.api.nvim_set_hl(0, 'Matchadd_3', { bg = colors.cyan, fg = 0 }) -vim.api.nvim_set_hl(0, 'Matchadd_4', { bg = colors.red, fg = 0 }) -vim.api.nvim_set_hl(0, 'Matchadd_5', { bg = colors.orange, fg = 0 }) -vim.api.nvim_set_hl(0, 'Matchadd_6', { bg = colors.yellow, fg = 0 }) diff --git a/lua/iroddis/hlmatch.lua b/lua/iroddis/hlmatch.lua new file mode 100644 index 0000000..9bfd589 --- /dev/null +++ b/lua/iroddis/hlmatch.lua @@ -0,0 +1,86 @@ +-- match_highlight.lua +-- +-- Defines four highlight groups (MatchAdd_1 .. MatchAdd_4) and a function +-- that highlights every occurrence of the last search pattern using one +-- of those groups via matchadd(). +-- +-- Usage (from another Lua file or init.lua): +-- local mh = require('match_highlight') +-- mh.highlight_last_search('MatchAdd_1') +-- +-- Or bind it to a command / keymap, e.g.: +-- vim.keymap.set('n', 'h1', function() mh.highlight_last_search('MatchAdd_1') end) + +local M = {} + +-- The four highlight groups. Adjust colors to taste, or link them to +-- existing groups instead (see nvim_set_hl docs). +local HL_GROUPS = { + MatchAdd_1 = { bg = '#268bd2', fg = '#000000' }, + MatchAdd_2 = { bg = '#6c71c4', fg = '#000000' }, + MatchAdd_3 = { bg = '#2aa198', fg = '#000000' }, + MatchAdd_4 = { bg = '#dc322f', fg = '#000000' }, + MatchAdd_5 = { bg = '#cb4b16', fg = '#000000' }, + MatchAdd_6 = { bg = '#b58900', fg = '#000000' }, +} + +--- Define the four highlight groups. Safe to call multiple times +--- (e.g. from a ColorScheme autocmd) since it just re-applies the same +--- definitions each time. +function M.setup_highlights() + for name, opts in pairs(HL_GROUPS) do + vim.api.nvim_set_hl(0, name, opts) + end +end + +--- Highlight every occurrence of the last search pattern (i.e. whatever +--- is currently in the "/" register) using the given highlight group. +--- +--- @param hl_group string One of "MatchAdd_1", "MatchAdd_2", "MatchAdd_3", "MatchAdd_4" +--- @return integer|nil The match ID returned by matchadd(), or nil on failure +function M.highlight_last_search(hl_group) + if not HL_GROUPS[hl_group] then + vim.notify( + string.format('highlight_last_search: invalid group "%s" (expected MatchAdd_1..4)', hl_group), + vim.log.levels.ERROR + ) + return nil + end + + local pattern = vim.fn.getreg('/') + if pattern == '' then + vim.notify('highlight_last_search: no previous search pattern found', vim.log.levels.WARN) + return nil + end + + local ok, match_id = pcall(vim.fn.matchadd, hl_group, pattern, 100) + if not ok then + vim.notify( + string.format('highlight_last_search: failed to add match for pattern "%s"', pattern), + vim.log.levels.ERROR + ) + return nil + end + + vim.notify( + string.format('highlighted "%s" with label "%s" match_id "%s"', pattern, hl_group, ok), + vim.log.levels.ERROR + ) + + + return match_id +end + +-- Apply the highlight definitions as soon as this module loads. +function M.setup() + M.setup_highlights() +end + +-- Re-apply highlights whenever the colorscheme changes, so they survive +-- theme switches. +vim.api.nvim_create_autocmd('FileType', { + callback = M.setup_highlights, + desc = 'Re-apply MatchAdd_1..4 highlight groups', +}) + +return M diff --git a/lua/iroddis/init.lua b/lua/iroddis/init.lua index 5ad0533..659cfee 100644 --- a/lua/iroddis/init.lua +++ b/lua/iroddis/init.lua @@ -2,12 +2,13 @@ require 'iroddis.set' require 'iroddis.lazy' require 'iroddis.lsp' -require 'iroddis.cmatches' +require 'iroddis.hlmatch' require 'iroddis.cfg_conform' -require 'iroddis.cfg_slime' -require 'iroddis.cfg_telescope' require 'iroddis.cfg_easyalign' require 'iroddis.cfg_mini' +require 'iroddis.cfg_telescope' +require 'iroddis.cfg_slime' +require 'iroddis.cfg_hlmatch' require 'iroddis.remaps' diff --git a/lua/iroddis/remaps.lua b/lua/iroddis/remaps.lua index bcc4a28..d8437a1 100644 --- a/lua/iroddis/remaps.lua +++ b/lua/iroddis/remaps.lua @@ -1,6 +1,6 @@ -- Keymaps for better default experience -- See `:help vim.keymap.set()` -vim.keymap.set({ 'n', 'v' }, '', '', { silent = true }) +-- vim.keymap.set({ 'n', 'v' }, '', '', { silent = true }) -- Remap for dealing with word wrap vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })