paper-tonic/lua/highlite.lua

121 lines
4.0 KiB
Lua
Raw Normal View History

2020-07-02 17:06:31 +00:00
--[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]]
local vim = vim
2020-07-02 17:06:31 +00:00
2020-06-22 02:38:18 +00:00
-- Clear the highlighting.
vim.cmd('hi clear')
-- Disable automatic coloring for the IndentGuides plugin.
2020-06-22 02:38:18 +00:00
vim.g.indent_guides_auto_colors = 0
-- If the syntax has been enabled, reset it.
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
or vim.o.termguicolors
or vim.fn.has('gui_running')
2020-09-05 15:30:32 +00:00
or string.find(vim.fn.expand('$TERM'), '256')
2020-06-22 02:38:18 +00:00
-- 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
2020-06-22 02:38:18 +00:00
-- These are constants for the indexes in the colors that were defined before.
2020-09-05 15:30:32 +00:00
local PALETTE_ANSI = 3
local PALETTE_256 = 2
local PALETTE_HEX = 1
local NONE = "NONE"
2020-06-22 02:38:18 +00:00
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-07-16 12:56:53 +00:00
if type(color) == 'table' and color[index] then
return color[index]
elseif type(color) == 'string' then
return color
else
return NONE
end
2020-09-06 17:21:30 +00:00
end --}}} ‡
-- Add the 'blend' parameter to some highlight command, if there is one.
2020-09-06 17:21:30 +00:00
local function blend(command, attributes) -- {{{ †
if attributes.blend then -- There is a value for the `highlight-blend` field.
command[#command + 1] = ' blend='..attributes.blend
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. ]]
2020-09-06 17:21:30 +00:00
local colorize = using_hex_or_256 and function(command, attributes) -- {{{ †
command[#command + 1] =
' ctermbg='..get(attributes.bg, PALETTE_256)
..' ctermfg='..get(attributes.fg, PALETTE_256)
..' guibg='..get(attributes.bg, PALETTE_HEX)
..' guifg='..get(attributes.fg, PALETTE_HEX)
blend(command, attributes)
end or function(command, attributes)
command[#command + 1] =
' ctermbg='..get(attributes.bg, PALETTE_ANSI)
..' ctermfg='..get(attributes.fg, PALETTE_ANSI)
blend(command, attributes)
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`.
2020-09-06 17:21:30 +00:00
local stylize = using_hex_or_256 and function(command, style, color) -- {{{ †
command[#command + 1] = ' cterm='..style..' gui='..style
if color then -- There is an undercurl color.
command[#command + 1] = ' guisp='..get(color, PALETTE_HEX)
end
end or function(command, style)
command[#command + 1] = ' cterm='..style
2020-09-06 17:21:30 +00:00
end --}}} ‡
2020-07-16 12:56:53 +00:00
2020-06-22 02:38:18 +00:00
-- Generate a `:highlight` command from a group and some attributes.
local function 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
-- Take care of special instructions for certain background colors.
if attributes[vim.o.background] then
attributes.__index = attributes
attributes = setmetatable(attributes[vim.o.background], attributes)
end
-- Determine if there is a highlight link, and if so, assign it.
local link = (type(attributes) == 'string' and attributes)
or attributes.link
2020-06-22 02:38:18 +00:00
if link then -- `highlight_group` is a link to another group.
2020-07-16 12:56:53 +00:00
highlight_cmd[3] = highlight_cmd[2]..' '
2020-06-22 02:38:18 +00:00
highlight_cmd[2] = 'link '
highlight_cmd[4] = link
2020-08-28 17:26:37 +00:00
else -- The `highlight_group` is uniquely defined.
2020-07-16 12:56:53 +00:00
colorize(highlight_cmd, attributes)
2020-07-16 03:30:14 +00:00
local style = attributes.style or NONE
2020-06-22 02:38:18 +00:00
if type(style) == 'table' then
2020-07-16 12:56:53 +00:00
-- Concat all of the entries together with a comma between before styling.
stylize(highlight_cmd, table.concat(style, ','), style.color)
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-06-22 02:38:18 +00:00
vim.cmd(table.concat(highlight_cmd))
end --}}} ‡
2020-07-02 17:06:31 +00:00
return function(Normal, highlights, terminal_ansi_colors)
2020-06-20 00:49:05 +00:00
-- 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
vim.g['terminal_color_'..index] = vim.o.termguicolors and color[PALETTE_HEX] or color[PALETTE_256]
end end
2020-06-20 00:49:05 +00:00
end