2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-03-15 23:37:46 +00:00
|
|
|
local M = {}
|
2021-05-06 11:05:55 +00:00
|
|
|
local cterm_colors
|
2021-02-20 03:21:05 +00:00
|
|
|
local utils = require 'lualine.utils.utils'
|
|
|
|
local section_highlight_map = {x = 'c', y = 'b', z = 'a'}
|
2021-04-07 00:34:36 +00:00
|
|
|
local active_theme = nil
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-03-22 14:29:37 +00:00
|
|
|
function M.highlight(name, foreground, background, gui, reload)
|
2021-03-15 23:37:46 +00:00
|
|
|
local command = {'highlight', name}
|
2021-02-22 20:28:29 +00:00
|
|
|
if foreground and foreground ~= 'none' then
|
|
|
|
table.insert(command, 'guifg=' .. foreground)
|
2021-04-08 02:43:06 +00:00
|
|
|
if cterm_colors then
|
2021-04-13 12:36:51 +00:00
|
|
|
table.insert(command,
|
2021-05-06 11:05:55 +00:00
|
|
|
'ctermfg=' .. cterm_colors.get_cterm_color(foreground))
|
2021-04-08 02:43:06 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-02-22 20:28:29 +00:00
|
|
|
if background and background ~= 'none' then
|
|
|
|
table.insert(command, 'guibg=' .. background)
|
2021-04-08 02:43:06 +00:00
|
|
|
if cterm_colors then
|
2021-04-13 12:36:51 +00:00
|
|
|
table.insert(command,
|
2021-05-06 11:05:55 +00:00
|
|
|
'ctermbg=' .. cterm_colors.get_cterm_color(background))
|
2021-04-08 02:43:06 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
if gui then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(command, 'cterm=' .. gui)
|
|
|
|
table.insert(command, 'gui=' .. gui)
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
vim.cmd(table.concat(command, ' '))
|
2021-03-22 14:29:37 +00:00
|
|
|
if not reload then
|
|
|
|
utils.save_highlight(name, {name, foreground, background, gui, true})
|
|
|
|
end
|
2021-01-05 15:08:56 +00:00
|
|
|
end
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
function M.create_highlight_groups(theme)
|
2021-04-07 00:34:36 +00:00
|
|
|
utils.clear_highlights()
|
|
|
|
active_theme = theme
|
2021-05-06 11:05:55 +00:00
|
|
|
if not vim.o.termguicolors then
|
|
|
|
cterm_colors = require 'lualine.utils.cterm_colors'
|
|
|
|
end
|
2020-12-30 14:48:51 +00:00
|
|
|
for mode, sections in pairs(theme) do
|
|
|
|
for section, colorscheme in pairs(sections) do
|
2021-03-15 23:37:46 +00:00
|
|
|
local highlight_group_name = {'lualine', section, mode}
|
2021-03-22 14:29:37 +00:00
|
|
|
M.highlight(table.concat(highlight_group_name, '_'), colorscheme.fg,
|
2021-04-07 00:34:36 +00:00
|
|
|
colorscheme.bg, colorscheme.gui)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-20 03:21:05 +00:00
|
|
|
-- @description: adds '_mode' at end of highlight_group
|
|
|
|
-- @param highlight_group:(string) name of highlight group
|
|
|
|
-- @return: (string) highlight group name with mode
|
2021-02-15 18:09:12 +00:00
|
|
|
local function append_mode(highlight_group)
|
2021-04-11 08:20:41 +00:00
|
|
|
local mode = require('lualine.utils.mode').get_mode()
|
2021-03-15 23:37:46 +00:00
|
|
|
if mode == 'VISUAL' or mode == 'V-BLOCK' or mode == 'V-LINE' or mode ==
|
|
|
|
'SELECT' or mode == 'S-LINE' or mode == 'S-BLOCK' then
|
2021-02-15 18:09:12 +00:00
|
|
|
highlight_group = highlight_group .. '_visual'
|
|
|
|
elseif mode == 'REPLACE' or mode == 'V-REPLACE' then
|
|
|
|
highlight_group = highlight_group .. '_replace'
|
|
|
|
elseif mode == 'INSERT' then
|
|
|
|
highlight_group = highlight_group .. '_insert'
|
2021-03-15 23:37:46 +00:00
|
|
|
elseif mode == 'COMMAND' or mode == 'EX' or mode == 'MORE' or mode ==
|
|
|
|
'CONFIRM' then
|
2021-02-15 18:09:12 +00:00
|
|
|
highlight_group = highlight_group .. '_command'
|
|
|
|
elseif mode == 'TERMINAL' then
|
|
|
|
highlight_group = highlight_group .. '_terminal'
|
|
|
|
else
|
|
|
|
highlight_group = highlight_group .. '_normal'
|
|
|
|
end
|
|
|
|
return highlight_group
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create highlight group with fg bg and gui from theme
|
2021-04-07 00:34:36 +00:00
|
|
|
-- @color has to be { fg = "#rrggbb", bg="#rrggbb" gui = "effect" }
|
2021-02-15 18:09:12 +00:00
|
|
|
-- all the color elements are optional if fg or bg is not given options must be provided
|
|
|
|
-- So fg and bg can default the themes colors
|
2021-04-07 00:34:36 +00:00
|
|
|
-- @highlight_tag is unique tag for highlight group
|
2021-02-15 18:09:12 +00:00
|
|
|
-- returns the name of highlight group
|
2021-04-07 00:34:36 +00:00
|
|
|
-- @options is parameter of component.init() function
|
2021-02-20 03:21:05 +00:00
|
|
|
-- @return: (string) unique name that can be used by component_format_highlight
|
|
|
|
-- to retrive highlight group
|
2021-03-15 23:37:46 +00:00
|
|
|
function M.create_component_highlight_group(color, highlight_tag, options)
|
2021-07-27 06:57:51 +00:00
|
|
|
local tag_id = 0
|
|
|
|
while (utils.highlight_exists(table.concat(
|
|
|
|
{'lualine', highlight_tag, 'no_mode'}, '_'))
|
|
|
|
or (options.self.section and utils.highlight_exists(table.concat(
|
|
|
|
{options.self.section, highlight_tag, 'normal'}, '_')))
|
|
|
|
) do
|
|
|
|
highlight_tag = highlight_tag .. '_' .. tostring(tag_id)
|
|
|
|
tag_id = tag_id + 1
|
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
if color.bg and color.fg then
|
|
|
|
-- When bg and fg are both present we donn't need to set highlighs for
|
2021-02-20 03:21:05 +00:00
|
|
|
-- each mode as they will surely look the same. So we can work without options
|
2021-03-15 23:37:46 +00:00
|
|
|
local highlight_group_name = table.concat(
|
|
|
|
{'lualine', highlight_tag, 'no_mode'}, '_')
|
2021-03-22 14:29:37 +00:00
|
|
|
M.highlight(highlight_group_name, color.fg, color.bg, color.gui)
|
2021-02-15 18:09:12 +00:00
|
|
|
return highlight_group_name
|
|
|
|
end
|
|
|
|
|
2021-03-15 23:37:46 +00:00
|
|
|
local modes = {
|
|
|
|
'normal', 'insert', 'visual', 'replace', 'command', 'terminal', 'inactive'
|
|
|
|
}
|
2021-02-20 03:21:05 +00:00
|
|
|
local normal_hl
|
|
|
|
-- convert lualine_a -> a before setting section
|
|
|
|
local section = options.self.section:match('lualine_(.*)')
|
2021-05-13 16:39:30 +00:00
|
|
|
if section > 'c' and not active_theme.normal[section] then
|
|
|
|
section = section_highlight_map[section] end
|
2021-02-15 18:09:12 +00:00
|
|
|
for _, mode in ipairs(modes) do
|
2021-03-15 23:37:46 +00:00
|
|
|
local highlight_group_name = {options.self.section, highlight_tag, mode}
|
2021-04-07 00:34:36 +00:00
|
|
|
local default_color_table = active_theme[mode] and
|
|
|
|
active_theme[mode][section] or
|
|
|
|
active_theme.normal[section]
|
2021-02-20 03:21:05 +00:00
|
|
|
local bg = (color.bg or default_color_table.bg)
|
|
|
|
local fg = (color.fg or default_color_table.fg)
|
|
|
|
-- Check if it's same as normal mode if it is no need to create aditional highlight
|
|
|
|
if mode ~= 'normal' then
|
2021-03-14 14:13:46 +00:00
|
|
|
if bg ~= normal_hl.bg or fg ~= normal_hl.fg then
|
2021-03-22 14:29:37 +00:00
|
|
|
M.highlight(table.concat(highlight_group_name, '_'), fg, bg, color.gui)
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
normal_hl = {bg = bg, fg = fg}
|
2021-03-22 14:29:37 +00:00
|
|
|
M.highlight(table.concat(highlight_group_name, '_'), fg, bg, color.gui)
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
return options.self.section .. '_' .. highlight_tag
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
|
2021-02-20 03:21:05 +00:00
|
|
|
-- @description: retrieve highlight_groups for components
|
|
|
|
-- @param highlight_name:(string) highlight group name without mode
|
|
|
|
-- return value of create_component_highlight_group is to be passed in
|
|
|
|
-- this parameter to receive highlight that was created
|
|
|
|
-- @return: (string) formated highlight group name
|
2021-02-15 18:09:12 +00:00
|
|
|
function M.component_format_highlight(highlight_name)
|
2021-02-20 03:21:05 +00:00
|
|
|
local highlight_group = highlight_name
|
2021-02-15 18:09:12 +00:00
|
|
|
if highlight_name:find('no_mode') == #highlight_name - #'no_mode' + 1 then
|
2021-03-15 23:37:46 +00:00
|
|
|
return '%#' .. highlight_group .. '#'
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
if vim.g.statusline_winid == vim.fn.win_getid() then
|
2021-02-20 03:21:05 +00:00
|
|
|
highlight_group = append_mode(highlight_group)
|
2021-02-15 18:09:12 +00:00
|
|
|
else
|
2021-03-15 23:37:46 +00:00
|
|
|
highlight_group = highlight_group .. '_inactive'
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
if utils.highlight_exists(highlight_group) then
|
|
|
|
return '%#' .. highlight_group .. '#'
|
2021-02-20 03:21:05 +00:00
|
|
|
else
|
2021-03-15 23:37:46 +00:00
|
|
|
return '%#' .. highlight_name .. '_normal#'
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.format_highlight(is_focused, highlight_group)
|
2021-05-13 16:39:30 +00:00
|
|
|
if highlight_group > 'lualine_c'
|
|
|
|
and not utils.highlight_exists(highlight_group .. '_normal') then
|
2021-03-15 23:37:46 +00:00
|
|
|
highlight_group = 'lualine_' ..
|
|
|
|
section_highlight_map[highlight_group:match(
|
|
|
|
'lualine_(.)')]
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-04-13 12:36:51 +00:00
|
|
|
local highlight_name
|
2021-01-04 01:14:29 +00:00
|
|
|
if not is_focused then
|
2021-03-15 23:37:46 +00:00
|
|
|
highlight_name = highlight_group .. [[_inactive]]
|
2020-12-30 14:48:51 +00:00
|
|
|
else
|
2021-02-20 03:21:05 +00:00
|
|
|
highlight_name = append_mode(highlight_group)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-02-21 14:37:59 +00:00
|
|
|
if utils.highlight_exists(highlight_name) then
|
|
|
|
return '%#' .. highlight_name .. '#'
|
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
return '%#' .. highlight_group .. '_normal#'
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- @description : Provides transitional highlights for section separators.
|
|
|
|
-- @param left_section_data :(string) section before separator
|
|
|
|
-- @param right_section_data:(string) section after separator
|
|
|
|
-- @param reverse :(string) Whether it's a left separator or right separator
|
2021-03-02 12:58:34 +00:00
|
|
|
-- '▶️' and '◀️' needs reverse colors so this parameter needs to be set true.
|
2021-02-20 03:21:05 +00:00
|
|
|
-- @return: (string) formated highlight group name
|
2021-03-15 23:37:46 +00:00
|
|
|
function M.get_transitional_highlights(left_section_data, right_section_data,
|
|
|
|
reverse)
|
2021-02-20 03:21:05 +00:00
|
|
|
local left_highlight_name, right_highlight_name
|
|
|
|
-- Grab the last highlighter of left section
|
|
|
|
if left_section_data then
|
|
|
|
-- extract highlight_name from .....%#highlight_name#
|
2021-05-07 00:54:49 +00:00
|
|
|
left_highlight_name = left_section_data:match('.*%%#(.-)#.-')
|
2021-02-20 03:21:05 +00:00
|
|
|
else
|
|
|
|
-- When right section us unavailable default to lualine_c
|
|
|
|
left_highlight_name = append_mode('lualine_c')
|
|
|
|
if not utils.highlight_exists(left_highlight_name) then
|
|
|
|
left_highlight_name = 'lualine_c_normal'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if right_section_data then
|
|
|
|
-- extract highlight_name from %#highlight_name#....
|
2021-05-07 00:54:49 +00:00
|
|
|
right_highlight_name = right_section_data:match('.-%%#(.-)#.*')
|
2021-02-20 03:21:05 +00:00
|
|
|
else
|
|
|
|
-- When right section us unavailable default to lualine_c
|
|
|
|
right_highlight_name = append_mode('lualine_c')
|
|
|
|
if not utils.highlight_exists(right_highlight_name) then
|
|
|
|
right_highlight_name = 'lualine_c_normal'
|
|
|
|
end
|
|
|
|
end
|
2021-03-02 12:58:34 +00:00
|
|
|
-- When both left and right highlights are same nothing to transition to
|
2021-02-20 03:21:05 +00:00
|
|
|
if left_highlight_name == right_highlight_name then return end
|
|
|
|
|
2021-03-02 12:58:34 +00:00
|
|
|
-- construct the name of hightlight group
|
2021-02-20 03:21:05 +00:00
|
|
|
local highlight_name
|
2021-03-02 12:58:34 +00:00
|
|
|
if left_highlight_name:find('lualine_') == 1 then
|
|
|
|
highlight_name = left_highlight_name .. '_to_' .. right_highlight_name
|
|
|
|
else
|
2021-03-15 23:37:46 +00:00
|
|
|
highlight_name = 'lualine_' .. left_highlight_name .. '_to_' ..
|
|
|
|
right_highlight_name
|
2021-03-02 12:58:34 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
|
|
|
|
if not utils.highlight_exists(highlight_name) then
|
|
|
|
-- Create the highlight_group if needed
|
2021-03-22 14:29:37 +00:00
|
|
|
-- Get colors from highlights
|
|
|
|
-- using string.format to convert decimal to hexadecimal
|
2021-04-13 12:36:51 +00:00
|
|
|
local fg = utils.extract_highlight_colors(left_highlight_name, 'bg')
|
|
|
|
local bg = utils.extract_highlight_colors(right_highlight_name, 'bg')
|
2021-03-22 14:29:37 +00:00
|
|
|
-- swap the bg and fg when reverse is true. As in that case highlight will
|
|
|
|
-- be placed before section
|
|
|
|
if reverse then fg, bg = bg, fg end
|
2021-05-02 04:36:51 +00:00
|
|
|
if not fg or not bg then return '' end -- Color retrieval failed
|
2021-05-07 00:54:49 +00:00
|
|
|
if bg == fg then return '' end -- Separatoe won't be visible anyway
|
2021-03-22 14:29:37 +00:00
|
|
|
M.highlight(highlight_name, fg, bg)
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
return '%#' .. highlight_name .. '#'
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|