From 1529b88178a36fcc51a9d32c4437adb25a3ebbf6 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Mon, 14 Feb 2022 00:11:31 +0100 Subject: [PATCH] vim: factor out highlight function into fs.util --- .../nvim/lua/fs/config/indent-blankline.lua | 7 ++-- config/nvim/lua/fs/config/nvim-tree.lua | 32 ++++++++----------- .../lua/fs/config/vim-colors-solarized.lua | 20 +++++++----- config/nvim/lua/fs/config/virt-column.lua | 7 ++-- config/nvim/lua/fs/util.lua | 16 ++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/config/nvim/lua/fs/config/indent-blankline.lua b/config/nvim/lua/fs/config/indent-blankline.lua index 731beab..becf472 100644 --- a/config/nvim/lua/fs/config/indent-blankline.lua +++ b/config/nvim/lua/fs/config/indent-blankline.lua @@ -1,11 +1,14 @@ -local nmap = require'fs.util'.nmap +local util = require'fs.util' +local nmap = util.nmap +local colors = util.colors() +local highlight = util.highlight local config = function() vim.g.indent_blankline_enabled = false require'indent_blankline'.setup() - vim.cmd [[highlight IndentBlanklineChar ctermfg=10]] + highlight('IndentBlanklineChar') { fg = colors.base01 } nmap { 'si', ':IndentBlanklineToggle' } end diff --git a/config/nvim/lua/fs/config/nvim-tree.lua b/config/nvim/lua/fs/config/nvim-tree.lua index 71e83f2..9d7b7a1 100644 --- a/config/nvim/lua/fs/config/nvim-tree.lua +++ b/config/nvim/lua/fs/config/nvim-tree.lua @@ -1,5 +1,7 @@ -local nmap = require'fs.util'.nmap -local colors = require'fs.util'.colors() +local util = require'fs.util' +local nmap = util.nmap +local colors = util.colors() +local highlight = util.highlight -- helper to set vim.g options that will be moved to setup() later local function set_globals(tbl) @@ -9,14 +11,6 @@ local function set_globals(tbl) end end -local function highlight(group, color) - if vim.opt.termguicolors:get() then - vim.cmd(vim.fn.printf('highlight %s guifg=%s', group, color)) - else - vim.cmd(vim.fn.printf('highlight %s ctermfg=%d', group, color)) - end -end - local global_opts ={ add_trailing = 1, -- add trailing / to folders group_empty = 1, -- folders that contain only one folder are grouped @@ -24,7 +18,7 @@ local global_opts ={ git_hl = 1, -- enable highlight based on git attributes icons = { - default = '', -- defailt icon for files + default = '', -- default icon for files symlink = '', -- default icon for symlinks }, } @@ -49,14 +43,14 @@ local nvim_tree_config = { local config = function() require'nvim-tree'.setup(nvim_tree_config) - highlight('NvimTreeSpecialFile' , colors.base2 ) - highlight('NvimTreeIndentMarker' , colors.base01 ) - highlight('NvimTreeGitStaged' , colors.green ) - highlight('NvimTreeGitRenamed' , colors.yellow ) - highlight('NvimTreeGitNew' , colors.yellow ) - highlight('NvimTreeGitDirty' , colors.yellow ) - highlight('NvimTreeGitDeleted' , colors.orange ) - highlight('NvimTreeGitMerge' , colors.red ) + highlight('NvimTreeSpecialFile') { fg = colors.base2 } + highlight('NvimTreeIndentMarker') { fg = colors.base01 } + highlight('NvimTreeGitStaged') { fg = colors.green } + highlight('NvimTreeGitRenamed') { fg = colors.yellow } + highlight('NvimTreeGitNew') { fg = colors.yellow } + highlight('NvimTreeGitDirty') { fg = colors.yellow } + highlight('NvimTreeGitDeleted') { fg = colors.orange } + highlight('NvimTreeGitMerge') { fg = colors.red } nmap { '', 'NvimTreeToggle' } nmap { 'n', 'NvimTreeFindFileToggle' } diff --git a/config/nvim/lua/fs/config/vim-colors-solarized.lua b/config/nvim/lua/fs/config/vim-colors-solarized.lua index 8ebe84d..97a4336 100644 --- a/config/nvim/lua/fs/config/vim-colors-solarized.lua +++ b/config/nvim/lua/fs/config/vim-colors-solarized.lua @@ -1,12 +1,16 @@ +local util = require'fs.util' +local C = util.colors() +local highlight = util.highlight + local config = function() - vim.cmd [[ - silent! colorscheme solarized - highlight Normal ctermbg=NONE " transparent background - highlight NonText cterm=NONE ctermfg=10 " subtle EOL symbols - highlight Whitespace cterm=NONE ctermfg=9 " orange listchars - highlight SpellBad ctermfg=3 " yellow spelling mistakes - highlight QuickFixLine ctermfg=3 ctermbg=0 " yellow selected quickfix item - ]] + vim.cmd [[silent! colorscheme solarized]] + + highlight('Normal') { bg = 'NONE' } -- transparent background + highlight('NonText') { fg = C.base02, attrs = 'NONE' } -- subtle EOL symbols + highlight('Whitespace') { fg = C.orange } -- listchars + highlight('SpellBad') { fg = C.yellow } + highlight('QuickFixLine') { fg = C.yellow, bg = C.base02 } -- selected quickfix item + highlight('CursorLineNr') { fg = C.yellow, attrs = 'NONE' } -- current line number end return { config = config } diff --git a/config/nvim/lua/fs/config/virt-column.lua b/config/nvim/lua/fs/config/virt-column.lua index 55f669a..f3210b5 100644 --- a/config/nvim/lua/fs/config/virt-column.lua +++ b/config/nvim/lua/fs/config/virt-column.lua @@ -1,4 +1,7 @@ -local nmap = require'fs.util'.nmap +local util = require'fs.util' +local nmap = util.nmap +local colors = util.colors() +local highlight = util.highlight local toggle = function() if vim.o.colorcolumn == '' then @@ -11,7 +14,7 @@ end local config = function() require'virt-column'.setup { char = '│' } - vim.cmd [[highlight VirtColumn cterm=NONE ctermfg=0]] + highlight('VirtColumn') { fg = colors.base02, attrs = 'NONE' } nmap { 'sc', 'lua require"fs.config.virt-column".toggle()' } end diff --git a/config/nvim/lua/fs/util.lua b/config/nvim/lua/fs/util.lua index 365c217..f015ee2 100644 --- a/config/nvim/lua/fs/util.lua +++ b/config/nvim/lua/fs/util.lua @@ -57,5 +57,21 @@ M.colors = function(gui) } end +-- Usage example: +-- highlight('Test2') { fg = C.yellow, bg = C.base02 } +M.highlight = function(name) + return function(tbl) + local kind = vim.opt.termguicolors:get() and 'gui' or 'cterm' + + local parts = {} + for k, v in pairs(tbl) do + if k == 'attrs' then k = '' end + table.insert(parts, kind .. k .. '=' .. v) + end + + vim.cmd('highlight ' .. name .. ' ' .. vim.fn.join(parts, ' ')) + end +end + return M