From 59de085efc611d98e7729eb890a3e4dec7961e86 Mon Sep 17 00:00:00 2001 From: Iron_E Date: Fri, 13 Nov 2020 15:43:16 -0500 Subject: [PATCH] Expose functionality --- lua/highlite.lua | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/lua/highlite.lua b/lua/highlite.lua index 975a50f..685c638 100644 --- a/lua/highlite.lua +++ b/lua/highlite.lua @@ -1,4 +1,5 @@ --[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]] +local highlite = {} local vim = vim -- Clear the highlighting. @@ -11,13 +12,13 @@ vim.g.indent_guides_auto_colors = 0 if vim.fn.exists('syntax_on') then vim.cmd('syntax reset') end -- Determine which set of colors to use. -local using_hex_or_256 = tonumber(vim.o.t_Co) >= 256 +highlite.using_hex_or_256 = tonumber(vim.o.t_Co) >= 256 or vim.o.termguicolors or vim.fn.has('gui_running') or string.find(vim.fn.expand('$TERM'), '256') -- If we aren't using the hex and 256 colorset, then set the &t_Co variable to 16. -if not using_hex_or_256 then vim.o.t_Co = 16 end +if not highlite.using_hex_or_256 then vim.o.t_Co = 16 end -- These are constants for the indexes in the colors that were defined before. local PALETTE_ANSI = 3 @@ -45,7 +46,7 @@ end --}}} ‡ --[[ If using hex and 256-bit colors, then populate the gui* and cterm* args. If using 16-bit colors, just populate the cterm* args. ]] -local colorize = using_hex_or_256 and function(command, attributes) -- {{{ † +local colorize = highlite.using_hex_or_256 and function(command, attributes) -- {{{ † command[#command + 1] = ' ctermbg='..get(attributes.bg, PALETTE_256) ..' ctermfg='..get(attributes.fg, PALETTE_256) @@ -60,7 +61,7 @@ end or function(command, attributes) end --}}} ‡ -- This function appends `selected_attributes` to the end of `highlight_cmd`. -local stylize = using_hex_or_256 and function(command, style, color) -- {{{ † +local stylize = highlite.using_hex_or_256 and function(command, style, color) -- {{{ † command[#command + 1] = ' cterm='..style..' gui='..style if color then -- There is an undercurl color. @@ -71,7 +72,7 @@ end or function(command, style) end --}}} ‡ -- Generate a `:highlight` command from a group and some attributes. -local function highlight(highlight_group, attributes) -- {{{ † +function highlite.highlight(highlight_group, attributes) -- {{{ † -- The base highlight command local highlight_cmd = {'hi! ', highlight_group} @@ -82,8 +83,7 @@ local function highlight(highlight_group, attributes) -- {{{ † end -- Determine if there is a highlight link, and if so, assign it. - local link = (type(attributes) == 'string' and attributes) - or attributes.link + local link = (type(attributes) == 'string') and attributes or attributes.link if link then -- `highlight_group` is a link to another group. highlight_cmd[3] = highlight_cmd[2]..' ' @@ -104,17 +104,23 @@ local function highlight(highlight_group, attributes) -- {{{ † vim.cmd(table.concat(highlight_cmd)) end --}}} ‡ -return function(Normal, highlights, terminal_ansi_colors) - -- Highlight the baseline. - highlight('Normal', Normal) - - -- Highlight everything else. - for highlight_group, attributes in pairs(highlights) do - highlight(highlight_group, attributes) - end - - -- Set the terminal colors. - if using_hex_or_256 then for index, color in ipairs(terminal_ansi_colors) do +function highlite:highlight_terminal(terminal_ansi_colors) + if self.using_hex_or_256 then for index, color in ipairs(terminal_ansi_colors) do vim.g['terminal_color_'..index] = vim.o.termguicolors and color[PALETTE_HEX] or color[PALETTE_256] end end end + +return setmetatable(highlite, { + ['__call'] = function(self, normal, highlights, terminal_ansi_colors) + -- Highlight the baseline. + self.highlight('Normal', normal) + + -- Highlight everything else. + for highlight_group, attributes in pairs(highlights) do + self.highlight(highlight_group, attributes) + end + + -- Set the terminal highlight colors. + self:highlight_terminal(terminal_ansi_colors) + end +})