From 0f2ae873aa117942509bc02103a2e16b8c39d6a2 Mon Sep 17 00:00:00 2001 From: zztrieuzz Date: Wed, 20 Dec 2023 11:31:24 +0700 Subject: [PATCH] fix: #196 #178 use api nvim_set_hl, add autocmd. --- lua/spectre/highlight.lua | 32 ++++++++++++-------------------- lua/spectre/init.lua | 2 +- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lua/spectre/highlight.lua b/lua/spectre/highlight.lua index 6ca7029..aa0f520 100644 --- a/lua/spectre/highlight.lua +++ b/lua/spectre/highlight.lua @@ -1,26 +1,18 @@ local M = {} -local create_highlight_group = function (name, link) - local ok, hl = pcall(vim.api.nvim_get_hl_by_name, link, true) - if not ok then - return - end - if hl.foreground ~= nil then - -- vim.cmd.highlight({name, 'guifg='.. string.format("#%06x", hl.foreground)}) - vim.cmd("highlight " .. name .. " guifg=" .. string.format("#%06x", hl.foreground) ) - end - if hl.background ~= nil then - -- vim.cmd.highlight({name, 'guibg='.. string.format("#%06x", hl.background)}) - vim.cmd("highlight " .. name .. " guibg=" .. string.format("#%06x", hl.background) ) - end +M.set_hl = function() + vim.api.nvim_set_hl(0, 'SpectreHeader', { link = "Comment" }) + vim.api.nvim_set_hl(0, 'SpectreBody', { link = "String" }) + vim.api.nvim_set_hl(0, 'SpectreFile', { link = "Keyword" }) + vim.api.nvim_set_hl(0, 'SpectreDir', { link = "Comment" }) + vim.api.nvim_set_hl(0, 'SpectreSearch', { link = "DiffChange" }) + vim.api.nvim_set_hl(0, 'SpectreBorder', { link = "Comment" }) + vim.api.nvim_set_hl(0, 'SpectreReplace', { link = "DiffDelete" }) end -create_highlight_group('SpectreHeader', "Comment") -create_highlight_group('SpectreBody', "String") -create_highlight_group('SpectreFile', "Keyword") -create_highlight_group('SpectreDir', "Comment") -create_highlight_group('SpectreSearch', "DiffChange") -create_highlight_group('SpectreBorder', "Comment") -create_highlight_group('SpectreReplace', "DiffDelete") +vim.api.nvim_create_autocmd('ColorScheme', { + pattern = '*', + callback = M.set_hl +}) return M; diff --git a/lua/spectre/init.lua b/lua/spectre/init.lua index faa0a43..c795c7b 100644 --- a/lua/spectre/init.lua +++ b/lua/spectre/init.lua @@ -26,7 +26,6 @@ local state_utils = require('spectre.state_utils') local utils = require('spectre.utils') local ui = require('spectre.ui') local log = require('spectre._log') -require('spectre.highlight') local async = require('plenary.async') local scheduler = async.util.scheduler @@ -38,6 +37,7 @@ M.setup = function(cfg) for _, opt in pairs(state.user_config.default.find.options) do state.options[opt] = true end + require('spectre.highlight').set_hl() end M.open_visual = function(opts)