2020-07-02 17:06:31 +00:00
|
|
|
--[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]]
|
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
--[[
|
|
|
|
/*
|
|
|
|
* IMPORTS
|
|
|
|
*/
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local vim = vim
|
2020-11-19 18:55:36 +00:00
|
|
|
local api = vim.api
|
2020-12-05 22:37:56 +00:00
|
|
|
local exe = api.nvim_command
|
2020-11-19 18:55:36 +00:00
|
|
|
local fn = vim.fn
|
2021-05-29 22:38:30 +00:00
|
|
|
local go = vim.go
|
2020-06-22 02:38:18 +00:00
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
--[[
|
|
|
|
/*
|
|
|
|
* VARS
|
|
|
|
*/
|
|
|
|
--]]
|
2020-06-22 02:38:18 +00:00
|
|
|
|
2021-06-13 16:05:38 +00:00
|
|
|
-- Determine which set of colors to use.
|
|
|
|
local _USE_256 = tonumber(go.t_Co) > 255 or string.find(vim.env.TERM, '256')
|
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
-- These are constants for the indexes in the colors that were defined before.
|
|
|
|
local _NONE = 'NONE'
|
2021-06-13 16:05:38 +00:00
|
|
|
local _PALETTE_CTERM = _USE_256 and 2 or 3
|
2020-11-19 17:12:03 +00:00
|
|
|
local _PALETTE_HEX = 1
|
|
|
|
local _TYPE_STRING = 'string'
|
|
|
|
local _TYPE_TABLE = 'table'
|
2020-06-22 02:38:18 +00:00
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
--[[
|
|
|
|
/*
|
|
|
|
* HELPER FUNCTIONS
|
|
|
|
*/
|
|
|
|
--]]
|
2020-06-22 02:38:18 +00:00
|
|
|
|
2020-11-19 21:53:35 +00:00
|
|
|
-- filter a highlight group's style information
|
|
|
|
local function filter_group_style(value)
|
|
|
|
return value ~= 'background'
|
|
|
|
and value ~= 'blend'
|
|
|
|
and value ~= 'foreground'
|
|
|
|
and value ~= 'special'
|
|
|
|
end
|
|
|
|
|
2020-07-16 12:56:53 +00:00
|
|
|
-- Get the color value of a color variable, or "NONE" as a default.
|
2020-09-06 17:21:30 +00:00
|
|
|
local function get(color, index) -- {{{ †
|
2020-11-19 17:12:03 +00:00
|
|
|
if type(color) == _TYPE_TABLE and color[index] then
|
2020-07-16 12:56:53 +00:00
|
|
|
return color[index]
|
2020-11-19 17:12:03 +00:00
|
|
|
elseif type(color) == _TYPE_STRING then
|
2020-07-16 12:56:53 +00:00
|
|
|
return color
|
|
|
|
else
|
2020-11-19 17:12:03 +00:00
|
|
|
return _NONE
|
2020-07-16 12:56:53 +00:00
|
|
|
end
|
2020-09-06 17:21:30 +00:00
|
|
|
end --}}} ‡
|
2020-07-16 12:56:53 +00:00
|
|
|
|
|
|
|
--[[ If using hex and 256-bit colors, then populate the gui* and cterm* args.
|
|
|
|
If using 16-bit colors, just populate the cterm* args. ]]
|
2021-06-12 22:59:19 +00:00
|
|
|
local function colorize(command, attributes) -- {{{ †
|
2020-11-19 21:42:31 +00:00
|
|
|
command[#command+1]=' guibg='..get(attributes.bg, _PALETTE_HEX)..' guifg='..get(attributes.fg, _PALETTE_HEX)
|
2021-06-13 16:05:38 +00:00
|
|
|
..' ctermbg='..get(attributes.bg, _PALETTE_CTERM)..' ctermfg='..get(attributes.fg, _PALETTE_CTERM)
|
2021-06-12 22:59:19 +00:00
|
|
|
|
|
|
|
-- Add the `blend` parameter if it is present
|
|
|
|
if attributes.blend then -- There is a value for the `highlight-blend` field.
|
|
|
|
command[#command+1]=' blend='..attributes.blend
|
|
|
|
end
|
2020-09-06 17:21:30 +00:00
|
|
|
end --}}} ‡
|
2020-07-16 12:56:53 +00:00
|
|
|
|
|
|
|
-- This function appends `selected_attributes` to the end of `highlight_cmd`.
|
2021-06-12 22:59:19 +00:00
|
|
|
local function stylize(command, style, color)
|
|
|
|
command[#command+1]=' gui='..style..' cterm='..style
|
2020-09-06 16:33:48 +00:00
|
|
|
|
|
|
|
if color then -- There is an undercurl color.
|
2020-12-05 22:37:56 +00:00
|
|
|
command[#command+1]=' guisp='..get(color, _PALETTE_HEX)
|
2020-09-06 16:33:48 +00:00
|
|
|
end
|
2020-11-19 21:42:31 +00:00
|
|
|
end
|
2020-07-16 12:56:53 +00:00
|
|
|
|
2020-12-05 22:43:14 +00:00
|
|
|
local function tohex(rgb) return string.format('#%06x', rgb) end
|
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
-- Load specific &bg instructions
|
|
|
|
local function use_background_with(attributes)
|
2020-12-05 22:41:24 +00:00
|
|
|
return setmetatable(
|
2021-05-29 22:38:30 +00:00
|
|
|
attributes[go.background],
|
2020-12-05 22:41:24 +00:00
|
|
|
{['__index'] = attributes}
|
|
|
|
)
|
2020-11-19 17:12:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
/*
|
|
|
|
* MODULE
|
|
|
|
*/
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local highlite = {}
|
|
|
|
|
2020-11-19 21:42:31 +00:00
|
|
|
function highlite.group(group_name)
|
2021-05-29 22:38:30 +00:00
|
|
|
local no_errors, group_definition = pcall(api.nvim_get_hl_by_name, group_name, go.termguicolors)
|
2020-11-19 21:42:31 +00:00
|
|
|
|
|
|
|
if not no_errors then group_definition = {} end
|
|
|
|
|
2020-11-19 21:53:35 +00:00
|
|
|
-- the style of the highlight group
|
|
|
|
local style = vim.tbl_filter(filter_group_style, vim.tbl_keys(group_definition))
|
|
|
|
if group_definition.special then
|
2020-12-05 22:43:14 +00:00
|
|
|
style.color = tohex(group_definition.special)
|
2020-11-19 21:42:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2020-12-05 22:43:14 +00:00
|
|
|
['fg'] = group_definition.foreground and tohex(group_definition.foreground) or _NONE,
|
|
|
|
['bg'] = group_definition.background and tohex(group_definition.background) or _NONE,
|
2020-11-19 21:42:31 +00:00
|
|
|
['blend'] = group_definition.blend,
|
|
|
|
['style'] = style or _NONE
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-22 02:38:18 +00:00
|
|
|
-- Generate a `:highlight` command from a group and some attributes.
|
2020-11-13 20:43:16 +00:00
|
|
|
function highlite.highlight(highlight_group, attributes) -- {{{ †
|
2020-09-06 18:04:36 +00:00
|
|
|
-- The base highlight command
|
2020-06-22 02:38:18 +00:00
|
|
|
local highlight_cmd = {'hi! ', highlight_group}
|
2020-09-06 18:04:36 +00:00
|
|
|
|
2020-11-19 21:42:31 +00:00
|
|
|
if type(attributes) == _TYPE_STRING then -- `highlight_group` is a link to another group.
|
2020-11-19 18:55:36 +00:00
|
|
|
highlight_cmd[3] = highlight_cmd[2]
|
2020-06-22 02:38:18 +00:00
|
|
|
highlight_cmd[2] = 'link '
|
2020-11-19 18:55:36 +00:00
|
|
|
highlight_cmd[4] = ' '
|
2020-11-19 21:42:31 +00:00
|
|
|
highlight_cmd[5] = attributes
|
2020-08-28 17:26:37 +00:00
|
|
|
else -- The `highlight_group` is uniquely defined.
|
2020-11-19 21:42:31 +00:00
|
|
|
-- Take care of special instructions for certain background colors.
|
2021-05-29 22:38:30 +00:00
|
|
|
if attributes[go.background] then
|
2020-11-19 21:42:31 +00:00
|
|
|
attributes = use_background_with(attributes)
|
|
|
|
end
|
|
|
|
|
2020-07-16 12:56:53 +00:00
|
|
|
colorize(highlight_cmd, attributes)
|
2020-07-16 03:30:14 +00:00
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
local style = attributes.style or _NONE
|
2020-11-19 21:42:31 +00:00
|
|
|
|
2020-11-19 17:12:03 +00:00
|
|
|
if type(style) == _TYPE_TABLE then
|
2020-07-16 12:56:53 +00:00
|
|
|
-- Concat all of the entries together with a comma between before styling.
|
2020-09-06 16:33:48 +00:00
|
|
|
stylize(highlight_cmd, table.concat(style, ','), style.color)
|
2020-09-06 17:29:09 +00:00
|
|
|
else -- The style is just a single entry.
|
2020-07-16 12:59:09 +00:00
|
|
|
stylize(highlight_cmd, style)
|
2020-06-20 00:49:05 +00:00
|
|
|
end
|
2020-06-22 02:38:18 +00:00
|
|
|
end
|
2020-06-20 00:49:05 +00:00
|
|
|
|
2020-12-05 22:37:56 +00:00
|
|
|
exe(table.concat(highlight_cmd))
|
2020-06-22 02:38:18 +00:00
|
|
|
end --}}} ‡
|
|
|
|
|
2020-11-13 20:43:16 +00:00
|
|
|
function highlite:highlight_terminal(terminal_ansi_colors)
|
2021-01-26 17:37:13 +00:00
|
|
|
for index, color in ipairs(terminal_ansi_colors) do vim.g['terminal_color_'..(index-1)] =
|
2021-06-13 16:05:38 +00:00
|
|
|
go.termguicolors and color[_PALETTE_HEX] or color[_PALETTE_CTERM]
|
2020-12-05 22:44:20 +00:00
|
|
|
end
|
2020-06-20 00:49:05 +00:00
|
|
|
end
|
2020-11-13 20:43:16 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
return setmetatable(highlite, {['__call'] = function(self, normal, highlights, terminal_ansi_colors)
|
|
|
|
-- function to resolve function highlight groups being defined by function calls.
|
|
|
|
local function resolve(tbl, key, resolve_links)
|
|
|
|
local value = tbl[key]
|
|
|
|
local value_type = type(value)
|
|
|
|
if value_type == 'function' then
|
|
|
|
-- lazily cache the result; next time, if it isn't a function this step will be skipped
|
|
|
|
tbl[key] = value(setmetatable({}, {
|
|
|
|
['__index'] = function(_, inner_key) return resolve(tbl, inner_key, true) end
|
|
|
|
}))
|
|
|
|
elseif value_type == _TYPE_STRING and not string.find(value, '^#') and resolve_links then
|
|
|
|
return resolve(tbl, tbl[key], resolve_links)
|
2020-11-25 06:25:57 +00:00
|
|
|
end
|
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
return tbl[key]
|
|
|
|
end
|
2020-11-25 06:25:57 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- save the colors_name before syntax reset
|
|
|
|
local color_name = vim.g.colors_name
|
2020-11-19 17:12:03 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- If the syntax has been enabled, reset it.
|
|
|
|
if fn.exists 'syntax_on' then exe 'syntax reset' end
|
2020-11-19 21:42:31 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- replace the colors_name
|
|
|
|
vim.g.colors_name = color_name
|
|
|
|
color_name = nil
|
2020-11-19 17:12:03 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- If we aren't using hex nor 256 colorsets.
|
2021-06-12 22:59:19 +00:00
|
|
|
if not (go.termguicolors or _USE_256) then go.t_Co = '16' end
|
2020-11-13 20:43:16 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- Highlight the baseline.
|
|
|
|
self.highlight('Normal', normal)
|
2020-11-13 20:43:16 +00:00
|
|
|
|
2021-01-26 17:54:04 +00:00
|
|
|
-- Highlight everything else.
|
|
|
|
for highlight_group, _ in pairs(highlights) do
|
|
|
|
self.highlight(highlight_group, resolve(highlights, highlight_group, false))
|
2020-11-13 20:43:16 +00:00
|
|
|
end
|
2021-01-26 17:54:04 +00:00
|
|
|
|
|
|
|
-- Set the terminal highlight colors.
|
|
|
|
self:highlight_terminal(terminal_ansi_colors)
|
|
|
|
end})
|