Fixing highlight groups
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
local mh = require('iroddis.hlmatch')
|
||||
vim.keymap.set('n', '<leader>m1', function() mh.highlight_last_search('MatchAdd_1') end)
|
||||
vim.keymap.set('n', '<leader>m2', function() mh.highlight_last_search('MatchAdd_2') end)
|
||||
vim.keymap.set('n', '<leader>m3', function() mh.highlight_last_search('MatchAdd_3') end)
|
||||
vim.keymap.set('n', '<leader>m4', function() mh.highlight_last_search('MatchAdd_4') end)
|
||||
vim.keymap.set('n', '<leader>m5', function() mh.highlight_last_search('MatchAdd_5') end)
|
||||
vim.keymap.set('n', '<leader>mc', function() vim.fn.clearmatches() end)
|
||||
@@ -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', '<leader>m1', function()
|
||||
highlight_search_term 'Matchadd_1'
|
||||
end)
|
||||
vim.keymap.set('n', '<leader>m2', function()
|
||||
highlight_search_term 'Matchadd_2'
|
||||
end)
|
||||
vim.keymap.set('n', '<leader>m3', function()
|
||||
highlight_search_term 'Matchadd_3'
|
||||
end)
|
||||
vim.keymap.set('n', '<leader>m4', function()
|
||||
highlight_search_term 'Matchadd_4'
|
||||
end)
|
||||
vim.keymap.set('n', '<leader>m5', function()
|
||||
highlight_search_term 'Matchadd_5'
|
||||
end)
|
||||
vim.keymap.set('n', '<leader>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 })
|
||||
@@ -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', '<leader>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
|
||||
@@ -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'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- Keymaps for better default experience
|
||||
-- See `:help vim.keymap.set()`
|
||||
vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
|
||||
-- vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
|
||||
|
||||
-- Remap for dealing with word wrap
|
||||
vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
|
||||
|
||||
Reference in New Issue
Block a user