| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local M = {} |
| 4 |
local lualine_require = require('lualine_require') |
| 5 |
local require = lualine_require.require |
| 6 |
local modules = lualine_require.lazy_require { |
| 7 |
utils = 'lualine.utils.utils', |
| 8 |
color_utils = 'lualine.utils.color_utils', |
| 9 |
} |
| 10 |
|
| 11 |
local section_highlight_map = { x = 'c', y = 'b', z = 'a' } |
| 12 |
local active_theme = nil |
| 13 |
local theme_hls = {} |
| 14 |
local create_cterm_colors = false |
| 15 |
|
| 16 |
-- table to store the highlight names created by lualine |
| 17 |
local loaded_highlights = {} |
| 18 |
|
| 19 |
-- table to map mode to highlight suffixes |
| 20 |
local mode_to_highlight = { |
| 21 |
['VISUAL'] = '_visual', |
| 22 |
['V-BLOCK'] = '_visual', |
| 23 |
['V-LINE'] = '_visual', |
| 24 |
['SELECT'] = '_visual', |
| 25 |
['S-LINE'] = '_visual', |
| 26 |
['S-BLOCK'] = '_visual', |
| 27 |
['REPLACE'] = '_replace', |
| 28 |
['V-REPLACE'] = '_replace', |
| 29 |
['INSERT'] = '_insert', |
| 30 |
['COMMAND'] = '_command', |
| 31 |
['EX'] = '_command', |
| 32 |
['MORE'] = '_command', |
| 33 |
['CONFIRM'] = '_command', |
| 34 |
['TERMINAL'] = '_terminal', |
| 35 |
} |
| 36 |
|
| 37 |
--- Get highlight suffix for current mode, or inactive if not focused |
| 38 |
---@return string mode_suffix |
| 39 |
function M.get_mode_suffix() |
| 40 |
local mode = require('lualine.utils.mode').get_mode() |
| 41 |
return mode_to_highlight[mode] or '_normal' |
| 42 |
end |
| 43 |
|
| 44 |
--- determine if an highlight exist and isn't cleared |
| 45 |
---@param highlight_name string |
| 46 |
---@return boolean whether hl_group was defined with highlight_name |
| 47 |
function M.highlight_exists(highlight_name) |
| 48 |
return loaded_highlights[highlight_name] or false |
| 49 |
end |
| 50 |
|
| 51 |
--- Creates lualine owned Normal mirror. Used for transparent background |
| 52 |
local function create_transparent_hlgroup() |
| 53 |
local base_color = modules.utils.extract_highlight_colors('Normal') |
| 54 |
if base_color.reverse then |
| 55 |
base_color.fg, base_color.bg = base_color.bg, base_color.fg |
| 56 |
end |
| 57 |
M.highlight('lualine_transparent', base_color.fg, base_color.bg, nil, nil) |
| 58 |
end |
| 59 |
|
| 60 |
--- clears loaded_highlights table and highlights |
| 61 |
local function clear_highlights() |
| 62 |
for highlight_name, _ in pairs(loaded_highlights) do |
| 63 |
vim.cmd('highlight clear ' .. highlight_name) |
| 64 |
end |
| 65 |
loaded_highlights = {} |
| 66 |
create_transparent_hlgroup() |
| 67 |
end |
| 68 |
|
| 69 |
function M.get_lualine_hl(name) |
| 70 |
local hl = loaded_highlights[name] |
| 71 |
if hl and not hl.empty then |
| 72 |
if hl.link then |
| 73 |
return modules.utils.extract_highlight_colors(hl.link) |
| 74 |
end |
| 75 |
local hl_def = { |
| 76 |
fg = hl.fg ~= 'None' and vim.deepcopy(hl.fg) or nil, |
| 77 |
bg = hl.bg ~= 'None' and vim.deepcopy(hl.bg) or nil, |
| 78 |
sp = hl.sp ~= 'None' and vim.deepcopy(hl.sp) or nil, |
| 79 |
} |
| 80 |
if hl.gui then |
| 81 |
for _, flag in ipairs(vim.split(hl.gui, ',')) do |
| 82 |
if flag ~= 'None' then |
| 83 |
hl_def[flag] = true |
| 84 |
end |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
return hl_def |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
--- Define a hl_group |
| 93 |
---@param name string |
| 94 |
---@param foreground string|number: color |
| 95 |
---@param background string|number: color |
| 96 |
---@param gui table cterm/gui options like bold/italic etc. |
| 97 |
---@param link string hl_group name to link new hl to |
| 98 |
function M.highlight(name, foreground, background, gui, link) |
| 99 |
local command = { 'highlight!' } |
| 100 |
if link and #link > 0 then |
| 101 |
if loaded_highlights[name] and loaded_highlights[name].link == link then |
| 102 |
return |
| 103 |
end |
| 104 |
vim.list_extend(command, { 'link', name, link }) |
| 105 |
else |
| 106 |
local foreground_rgb = modules.color_utils.sanitize_color(foreground) |
| 107 |
local background_rgb = modules.color_utils.sanitize_color(background) |
| 108 |
gui = gui or '' |
| 109 |
if string.find(gui, 'nocombine') == nil then |
| 110 |
gui = gui ~= '' and gui .. ',nocombine' or 'nocombine' |
| 111 |
end |
| 112 |
if |
| 113 |
loaded_highlights[name] |
| 114 |
and loaded_highlights[name].fg == foreground_rgb |
| 115 |
and loaded_highlights[name].bg == background_rgb |
| 116 |
and loaded_highlights[name].gui == gui |
| 117 |
then |
| 118 |
return -- color is already defined why are we doing this anyway ? |
| 119 |
end |
| 120 |
table.insert(command, name) |
| 121 |
table.insert(command, 'guifg=' .. foreground_rgb) |
| 122 |
table.insert(command, 'guibg=' .. background_rgb) |
| 123 |
table.insert(command, 'gui=' .. gui) |
| 124 |
if create_cterm_colors then |
| 125 |
-- Not setting color from xxxground_rgb to let possible user 256 number through |
| 126 |
table.insert(command, 'ctermfg=' .. modules.color_utils.sanitize_color_for_cterm(foreground)) |
| 127 |
table.insert(command, 'ctermbg=' .. modules.color_utils.sanitize_color_for_cterm(background)) |
| 128 |
table.insert(command, 'cterm=' .. gui) |
| 129 |
end |
| 130 |
end |
| 131 |
vim.cmd(table.concat(command, ' ')) |
| 132 |
|
| 133 |
-- update attached hl groups |
| 134 |
local old_hl_def = loaded_highlights[name] |
| 135 |
if old_hl_def and next(old_hl_def.attached) then |
| 136 |
-- Update attached hl groups as they announced to depend on hl_group 'name' |
| 137 |
-- 'hl' being in 'name's attached table means 'hl' |
| 138 |
-- depends of 'name'. |
| 139 |
-- 'hl' key in attached table will contain a table that |
| 140 |
-- defines the relation between 'hl' & 'name'. |
| 141 |
-- name.attached.hl = { bg = 'fg' } means |
| 142 |
-- hl's fg is same as 'names' bg . So 'hl's fg should |
| 143 |
-- be updated when ever 'name' changes it's 'bg' |
| 144 |
local bg_changed = old_hl_def.bg ~= background |
| 145 |
local fg_changed = old_hl_def.bg ~= foreground |
| 146 |
local gui_changed = old_hl_def.gui ~= gui |
| 147 |
for attach_name, attach_desc in pairs(old_hl_def.attached) do |
| 148 |
if bg_changed and attach_desc.bg and loaded_highlights[attach_name] then |
| 149 |
M.highlight( |
| 150 |
attach_name, |
| 151 |
attach_desc.bg == 'fg' and background or loaded_highlights[attach_name].fg, |
| 152 |
attach_desc.bg == 'bg' and background or loaded_highlights[attach_name].bg, |
| 153 |
loaded_highlights[attach_name].gui, |
| 154 |
loaded_highlights[attach_name].link |
| 155 |
) |
| 156 |
end |
| 157 |
if fg_changed and attach_desc.fg and loaded_highlights[attach_name] then |
| 158 |
M.highlight( |
| 159 |
attach_name, |
| 160 |
attach_desc.fg == 'fg' and foreground or loaded_highlights[attach_name].fg, |
| 161 |
attach_desc.fg == 'bg' and foreground or loaded_highlights[attach_name].bg, |
| 162 |
loaded_highlights[attach_name].gui, |
| 163 |
loaded_highlights[attach_name].link |
| 164 |
) |
| 165 |
end |
| 166 |
if gui_changed and attach_desc.gui and loaded_highlights[attach_name] then |
| 167 |
M.highlight( |
| 168 |
attach_name, |
| 169 |
loaded_highlights[attach_name].fg, |
| 170 |
loaded_highlights[attach_name].bg, |
| 171 |
gui, |
| 172 |
loaded_highlights[attach_name].link |
| 173 |
) |
| 174 |
end |
| 175 |
end |
| 176 |
end |
| 177 |
-- store current hl state |
| 178 |
loaded_highlights[name] = { |
| 179 |
fg = foreground, |
| 180 |
bg = background, |
| 181 |
gui = gui, |
| 182 |
link = link, |
| 183 |
attached = old_hl_def and old_hl_def.attached or {}, |
| 184 |
} |
| 185 |
end |
| 186 |
|
| 187 |
---Attach a hl to another, so the attached auto updates on change to hl that it's attached too. |
| 188 |
---@param provider string the hl receiver is getting attached to |
| 189 |
---@param receiver string the hl that will be auto updated upon change to provider |
| 190 |
---@param provider_el_type string (fg/bg) what element receiver relates to of provider |
| 191 |
---@param receiver_el_type string (fg/bg) what element provider relates to of receiver |
| 192 |
local function attach_hl(provider, receiver, provider_el_type, receiver_el_type) |
| 193 |
if loaded_highlights[provider] == nil then |
| 194 |
loaded_highlights[provider] = { empty = true, attached = {} } |
| 195 |
end |
| 196 |
loaded_highlights[provider].attached[receiver] = { [provider_el_type] = receiver_el_type } |
| 197 |
end |
| 198 |
---define hl_groups for a theme |
| 199 |
---@param theme table |
| 200 |
function M.create_highlight_groups(theme) |
| 201 |
clear_highlights() |
| 202 |
active_theme = theme |
| 203 |
theme_hls = {} |
| 204 |
local psudo_options = { self = { section = 'a' } } |
| 205 |
create_cterm_colors = not vim.go.termguicolors |
| 206 |
for mode, sections in pairs(theme) do |
| 207 |
theme_hls[mode] = {} |
| 208 |
for section, color in pairs(sections) do |
| 209 |
local hl_tag = mode |
| 210 |
psudo_options.self.section = section |
| 211 |
theme_hls[mode][section] = M.create_component_highlight_group(color, hl_tag, psudo_options, true) |
| 212 |
end |
| 213 |
end |
| 214 |
end |
| 215 |
|
| 216 |
---@description: adds '_mode' at end of highlight_group |
| 217 |
---@param highlight_group string name of highlight group |
| 218 |
---@return string highlight group name with mode |
| 219 |
local function append_mode(highlight_group, is_focused) |
| 220 |
if is_focused == nil then |
| 221 |
is_focused = modules.utils.is_focused() |
| 222 |
end |
| 223 |
if is_focused == false then |
| 224 |
return highlight_group .. '_inactive' |
| 225 |
end |
| 226 |
return highlight_group .. M.get_mode_suffix() |
| 227 |
end |
| 228 |
|
| 229 |
-- Helper function for create component highlight |
| 230 |
---Handles fall back of colors when creating highlight group |
| 231 |
---@param hl_name string name of highlight that we are setting default values for |
| 232 |
---@param mode string mode which default component color should be given. |
| 233 |
---@param section string the lualine section component is in. |
| 234 |
---@param color table color passed for creating component highlight |
| 235 |
---@param options table Options table of component this is first fall back |
| 236 |
local function get_default_component_color(hl_name, mode, section, color, options) |
| 237 |
local default_theme_color |
| 238 |
if active_theme[mode] and active_theme[mode][section] then |
| 239 |
default_theme_color = active_theme[mode][section] |
| 240 |
elseif section >= 'c' and active_theme[mode] and active_theme[mode][section_highlight_map[section]] then |
| 241 |
default_theme_color = active_theme[mode][section_highlight_map[section]] |
| 242 |
elseif section >= 'c' and active_theme.normal[section_highlight_map[section]] then |
| 243 |
default_theme_color = active_theme.normal[section_highlight_map[section]] |
| 244 |
else |
| 245 |
default_theme_color = active_theme.normal[section] |
| 246 |
end |
| 247 |
|
| 248 |
local ret = { fg = color.fg, bg = color.bg, gui = color.gui } |
| 249 |
if ret.fg and ret.bg then |
| 250 |
return ret |
| 251 |
end |
| 252 |
|
| 253 |
local function apply_default(def_color, def_name) |
| 254 |
if type(def_color) == 'function' and loaded_highlights[def_name] and not loaded_highlights[def_name].empty then |
| 255 |
if loaded_highlights[def_name].link then |
| 256 |
def_color = loaded_highlights[def_name].link |
| 257 |
else |
| 258 |
def_color = loaded_highlights[def_name] |
| 259 |
end |
| 260 |
end |
| 261 |
if type(def_color) == 'function' then |
| 262 |
def_color = def_color { section = section } |
| 263 |
end |
| 264 |
if type(def_color) == 'string' then |
| 265 |
def_color = modules.utils.extract_highlight_colors(def_color) |
| 266 |
end |
| 267 |
if type(def_color) == 'table' then |
| 268 |
if not ret.fg then |
| 269 |
ret.fg = def_color.fg |
| 270 |
attach_hl(def_name, hl_name, 'fg', 'fg') |
| 271 |
end |
| 272 |
if not ret.bg then |
| 273 |
ret.bg = def_color.bg |
| 274 |
attach_hl(def_name, hl_name, 'bg', 'bg') |
| 275 |
end |
| 276 |
end |
| 277 |
end |
| 278 |
|
| 279 |
if |
| 280 |
options.color |
| 281 |
and options.color_highlight |
| 282 |
and options.color_highlight.name |
| 283 |
and options.color_highlight.name .. '_' .. mode ~= hl_name |
| 284 |
then |
| 285 |
apply_default(options.color, options.color_highlight.name .. '_' .. mode) |
| 286 |
end |
| 287 |
|
| 288 |
if not ret.fg or not ret.bg then |
| 289 |
apply_default(default_theme_color, string.format('lualine_%s_%s', section, mode)) |
| 290 |
end |
| 291 |
ret.fg = modules.color_utils.sanitize_color(ret.fg) |
| 292 |
ret.bg = modules.color_utils.sanitize_color(ret.bg) |
| 293 |
return ret |
| 294 |
end |
| 295 |
|
| 296 |
---Create highlight group with fg bg and gui from theme |
| 297 |
---@param color table has to be { fg = "#rrggbb", bg="#rrggbb" gui = "effect" } |
| 298 |
--- all the color elements are optional if fg or bg is not given options |
| 299 |
--- must be provided So fg and bg can default the themes colors |
| 300 |
---@param highlight_tag string is unique tag for highlight group |
| 301 |
---returns the name of highlight group |
| 302 |
---@param options table is parameter of component.init() function |
| 303 |
---@return table that can be used by component_format_highlight |
| 304 |
--- to retrieve highlight group |
| 305 |
function M.create_component_highlight_group(color, highlight_tag, options, apply_no_default) |
| 306 |
local section = options.self.section |
| 307 |
local tag_id = 1 |
| 308 |
local highlight_tag_counted = highlight_tag |
| 309 |
while |
| 310 |
M.highlight_exists(table.concat({ 'lualine', section, highlight_tag_counted }, '_')) |
| 311 |
or (section and M.highlight_exists(table.concat({ 'lualine', section, highlight_tag_counted, 'normal' }, '_'))) |
| 312 |
do |
| 313 |
highlight_tag_counted = highlight_tag .. '_' .. tostring(tag_id) |
| 314 |
tag_id = tag_id + 1 |
| 315 |
end |
| 316 |
highlight_tag = highlight_tag_counted |
| 317 |
|
| 318 |
if type(color) == 'string' then |
| 319 |
local highlight_group_name = table.concat({ 'lualine', section, highlight_tag }, '_') |
| 320 |
M.highlight(highlight_group_name, nil, nil, nil, color) -- link to group |
| 321 |
return { |
| 322 |
name = highlight_group_name, |
| 323 |
fn = nil, |
| 324 |
no_mode = true, |
| 325 |
link = true, |
| 326 |
section = section, |
| 327 |
options = options, |
| 328 |
no_default = apply_no_default, |
| 329 |
} |
| 330 |
end |
| 331 |
|
| 332 |
if type(color) ~= 'function' and (apply_no_default or (color.bg and color.fg)) then |
| 333 |
-- When bg and fg are both present we don't need to set highlights for |
| 334 |
-- each mode as they will surely look the same. So we can work without options |
| 335 |
local highlight_group_name = table.concat({ 'lualine', section, highlight_tag }, '_') |
| 336 |
M.highlight(highlight_group_name, color.fg, color.bg, color.gui, nil) |
| 337 |
return { |
| 338 |
name = highlight_group_name, |
| 339 |
fn = nil, |
| 340 |
no_mode = true, |
| 341 |
section = section, |
| 342 |
options = options, |
| 343 |
no_default = apply_no_default, |
| 344 |
} |
| 345 |
end |
| 346 |
|
| 347 |
local modes = { |
| 348 |
'normal', |
| 349 |
'insert', |
| 350 |
'visual', |
| 351 |
'replace', |
| 352 |
'command', |
| 353 |
'terminal', |
| 354 |
'inactive', |
| 355 |
} |
| 356 |
for _, mode in ipairs(modes) do |
| 357 |
local hl_name = table.concat({ 'lualine', section, highlight_tag, mode }, '_') |
| 358 |
local cl = color |
| 359 |
if type(color) == 'function' then |
| 360 |
cl = color { section = section } or {} |
| 361 |
end |
| 362 |
if type(cl) == 'string' then |
| 363 |
cl = { link = cl } |
| 364 |
else |
| 365 |
cl = get_default_component_color(hl_name, mode, section, cl, options) |
| 366 |
end |
| 367 |
M.highlight(hl_name, cl.fg, cl.bg, cl.gui, cl.link) |
| 368 |
end |
| 369 |
return { |
| 370 |
name = table.concat({ 'lualine', section, highlight_tag }, '_'), |
| 371 |
fn = type(color) == 'function' and color, |
| 372 |
no_mode = false, |
| 373 |
link = false, |
| 374 |
section = section, |
| 375 |
options = options, |
| 376 |
no_default = apply_no_default, |
| 377 |
} |
| 378 |
end |
| 379 |
|
| 380 |
---@description: retrieve highlight_groups for components |
| 381 |
---@param highlight table return value of create_component_highlight_group |
| 382 |
--- return value of create_component_highlight_group is to be passed in |
| 383 |
--- this parameter to receive highlight that was created |
| 384 |
---@return string formatted highlight group name |
| 385 |
function M.component_format_highlight(highlight, is_focused) |
| 386 |
if not highlight.fn then |
| 387 |
local highlight_group = highlight.name |
| 388 |
if highlight.no_mode then |
| 389 |
return '%#' .. highlight_group .. '#' |
| 390 |
end |
| 391 |
highlight_group = append_mode(highlight_group, is_focused) |
| 392 |
return '%#' .. highlight_group .. '#' |
| 393 |
else |
| 394 |
local color = highlight.fn { section = highlight.section } or {} |
| 395 |
local hl_name = highlight.name |
| 396 |
if type(color) == 'string' then |
| 397 |
M.highlight(hl_name .. M.get_mode_suffix(), nil, nil, nil, color) |
| 398 |
return '%#' .. hl_name .. M.get_mode_suffix() .. '#' |
| 399 |
elseif type(color) == 'table' then |
| 400 |
if not highlight.no_default and not (color.fg and color.bg) then |
| 401 |
hl_name = append_mode(highlight.name, is_focused) |
| 402 |
color = |
| 403 |
get_default_component_color(hl_name, M.get_mode_suffix():sub(2), highlight.section, color, highlight.options) |
| 404 |
end |
| 405 |
M.highlight(hl_name, color.fg, color.bg, color.gui, color.link) |
| 406 |
return '%#' .. hl_name .. '#', color |
| 407 |
end |
| 408 |
end |
| 409 |
end |
| 410 |
|
| 411 |
---@description: retrieve highlight_groups for section |
| 412 |
---@param section string highlight group name without mode |
| 413 |
--- return value of create_component_highlight_group is to be passed in |
| 414 |
--- this parameter to receive highlight that was created |
| 415 |
---@param is_focused boolean |
| 416 |
---@return string formatted highlight group name |
| 417 |
function M.format_highlight(section, is_focused) |
| 418 |
local mode = append_mode('', is_focused):sub(2) |
| 419 |
local ret = '' |
| 420 |
|
| 421 |
if theme_hls[mode] and theme_hls[mode][section] then |
| 422 |
ret = M.component_format_highlight(theme_hls[mode][section], is_focused) |
| 423 |
elseif theme_hls[mode] and section > 'c' and theme_hls[mode][section_highlight_map[section]] then |
| 424 |
ret = M.component_format_highlight(theme_hls[mode][section_highlight_map[section]], is_focused) |
| 425 |
elseif theme_hls['normal'] and theme_hls['normal'][section] then |
| 426 |
ret = M.component_format_highlight(theme_hls['normal'][section], is_focused) |
| 427 |
elseif theme_hls['normal'] and section > 'c' and theme_hls['normal'][section_highlight_map[section]] then |
| 428 |
ret = M.component_format_highlight(theme_hls['normal'][section_highlight_map[section]], is_focused) |
| 429 |
end |
| 430 |
|
| 431 |
return ret |
| 432 |
end |
| 433 |
|
| 434 |
---@description : Provides transitional highlights for section separators. |
| 435 |
---@param left_hl string this highlights bg is used for fg of transitional hl |
| 436 |
---@param right_hl string this highlights bg is used for bg of transitional hl |
| 437 |
--- '▶️' and '◀️' ' needs reverse colors so the caller should swap left and right |
| 438 |
---@return string formatted highlight group name |
| 439 |
function M.get_transitional_highlights(left_hl, right_hl) |
| 440 |
-- When both left and right highlights are same or one is absent |
| 441 |
-- nothing to transition to. |
| 442 |
if left_hl == nil or right_hl == nil or left_hl == right_hl then |
| 443 |
return nil |
| 444 |
end |
| 445 |
|
| 446 |
-- construct the name of highlight group |
| 447 |
local highlight_name = table.concat({ 'lualine_transitional', left_hl, 'to', right_hl }, '_') |
| 448 |
if not M.highlight_exists(highlight_name) then |
| 449 |
-- Create the highlight_group if needed |
| 450 |
-- Get colors from highlights |
| 451 |
local fg = modules.utils.extract_highlight_colors(left_hl, 'bg') |
| 452 |
local bg = modules.utils.extract_highlight_colors(right_hl, 'bg') |
| 453 |
if not fg and not bg then |
| 454 |
return nil -- Color retrieval failed |
| 455 |
end |
| 456 |
if bg == fg then |
| 457 |
return nil -- Separator won't be visible anyway |
| 458 |
end |
| 459 |
M.highlight(highlight_name, fg, bg, nil, nil) |
| 460 |
attach_hl(left_hl, highlight_name, 'bg', 'fg') |
| 461 |
attach_hl(right_hl, highlight_name, 'bg', 'bg') |
| 462 |
end |
| 463 |
return '%#' .. highlight_name .. '#' |
| 464 |
end |
| 465 |
|
| 466 |
function M.get_stl_default_hl(focused) |
| 467 |
if focused == 3 then |
| 468 |
return 'TabLineFill' |
| 469 |
elseif not focused then |
| 470 |
return 'StatusLineNC' |
| 471 |
else |
| 472 |
return 'StatusLine' |
| 473 |
end |
| 474 |
end |
| 475 |
|
| 476 |
return M |