| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local M = {} |
| 4 |
|
| 5 |
local lualine_require = require('lualine_require') |
| 6 |
local modules = lualine_require.lazy_require { |
| 7 |
highlight = 'lualine.highlight', |
| 8 |
loader = 'lualine.utils.loader', |
| 9 |
utils_section = 'lualine.utils.section', |
| 10 |
utils = 'lualine.utils.utils', |
| 11 |
utils_notices = 'lualine.utils.notices', |
| 12 |
config_module = 'lualine.config', |
| 13 |
nvim_opts = 'lualine.utils.nvim_opts', |
| 14 |
} |
| 15 |
local config -- Stores currently applied config |
| 16 |
local timers = { |
| 17 |
stl_timer = vim.loop.new_timer(), |
| 18 |
tal_timer = vim.loop.new_timer(), |
| 19 |
wb_timer = vim.loop.new_timer(), |
| 20 |
refresh_check_timer = vim.loop.new_timer(), |
| 21 |
halt_stl_refresh = false, -- mutex ? |
| 22 |
halt_tal_refresh = false, |
| 23 |
halt_wb_refresh = false, |
| 24 |
} |
| 25 |
|
| 26 |
local last_focus = {} |
| 27 |
local refresh_real_curwin |
| 28 |
|
| 29 |
-- Helper for apply_transitional_separators() |
| 30 |
--- finds first applied highlight group after str_checked in status |
| 31 |
---@param status string : unprocessed statusline string |
| 32 |
---@param str_checked number : position of how far status has been checked |
| 33 |
---@return string|nil the hl group name or nil |
| 34 |
local function find_next_hl(status, str_checked) |
| 35 |
-- Gets the next valid hl group from str_checked |
| 36 |
local hl_pos_start, hl_pos_end = status:find('%%#.-#', str_checked) |
| 37 |
while true do |
| 38 |
if not hl_pos_start then |
| 39 |
return nil |
| 40 |
end |
| 41 |
-- When there are more that one hl group next to one another like |
| 42 |
-- %#HL1#%#HL2#%#HL3# we need to return HL3. This makes that happen. |
| 43 |
local next_start, next_end = status:find('^%%#.-#', hl_pos_end + 1) |
| 44 |
if next_start == nil then |
| 45 |
break |
| 46 |
end |
| 47 |
hl_pos_start, hl_pos_end = next_start, next_end |
| 48 |
end |
| 49 |
return status:sub(hl_pos_start + 2, hl_pos_end - 1) |
| 50 |
end |
| 51 |
|
| 52 |
-- Helper for apply_transitional_separators() |
| 53 |
--- applies transitional separator highlight + transitional separator |
| 54 |
---@param status string : unprocessed statusline string |
| 55 |
---@param str_checked number : position of how far status has been checked |
| 56 |
---@param last_hl string : last applied hl group name before str_checked |
| 57 |
---@param reverse boolean : reverse the hl group ( true for right separators ) |
| 58 |
---@return string|nil concatenate separator highlight and transitional separator |
| 59 |
local function fill_section_separator(status, is_focused, str_checked, last_hl, sep, reverse) |
| 60 |
-- Inserts transitional separator along with transitional highlight |
| 61 |
local next_hl = find_next_hl(status, str_checked) |
| 62 |
if last_hl == nil then |
| 63 |
last_hl = modules.highlight.get_stl_default_hl(is_focused) |
| 64 |
end |
| 65 |
if next_hl == nil then |
| 66 |
next_hl = modules.highlight.get_stl_default_hl(is_focused) |
| 67 |
end |
| 68 |
if #next_hl == 0 or #last_hl == 0 then |
| 69 |
return |
| 70 |
end |
| 71 |
local transitional_highlight = reverse -- lua ternary assignment x ? y : z |
| 72 |
and modules.highlight.get_transitional_highlights(last_hl, next_hl) |
| 73 |
or modules.highlight.get_transitional_highlights(next_hl, last_hl) |
| 74 |
if transitional_highlight then |
| 75 |
return transitional_highlight .. sep |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
--- processes statusline string |
| 80 |
--- replaces %z/Z{sep} with proper left/right separator highlight + sep |
| 81 |
---@param status string : unprocessed statusline string |
| 82 |
---@return string : processed statusline string |
| 83 |
local function apply_transitional_separators(status, is_focused) |
| 84 |
local status_applied = {} -- Collects all the pieces for concatenation |
| 85 |
local last_hl -- Stores last highlight group that we found |
| 86 |
local last_hl_reseted = false -- Whether last_hl is nil after reset |
| 87 |
-- it after %= |
| 88 |
local copied_pos = 1 -- Tracks how much we've copied over to status_applied |
| 89 |
local str_checked = 1 -- Tracks where the searcher head is at |
| 90 |
|
| 91 |
-- Process entire status replace the %z{sep} & %Z{sep} placeholders |
| 92 |
-- with proper transitional separator. |
| 93 |
while str_checked ~= nil do |
| 94 |
str_checked = status:find('%%', str_checked) |
| 95 |
if str_checked == nil then |
| 96 |
break |
| 97 |
end |
| 98 |
table.insert(status_applied, status:sub(copied_pos, str_checked - 1)) |
| 99 |
-- -1 so we don't copy '%' |
| 100 |
copied_pos = str_checked |
| 101 |
local next_char = modules.utils.charAt(status, str_checked + 1) |
| 102 |
if next_char == '#' then |
| 103 |
-- %#hl_name# highlights |
| 104 |
last_hl = status:match('^%%#(.-)#', str_checked) |
| 105 |
str_checked = str_checked + #last_hl + 3 |
| 106 |
elseif next_char == 'z' then |
| 107 |
-- %z{sep} is marker for left separator and |
| 108 |
local sep = status:match('^%%z{(.-)}', str_checked) |
| 109 |
str_checked = str_checked + #sep + 4 -- 4 = len(%{}) |
| 110 |
if not (last_hl == nil and last_hl_reseted) then |
| 111 |
local trans_sep = fill_section_separator(status, is_focused, str_checked, last_hl, sep, false) |
| 112 |
if trans_sep then |
| 113 |
table.insert(status_applied, trans_sep) |
| 114 |
end |
| 115 |
end |
| 116 |
if last_hl_reseted then |
| 117 |
last_hl_reseted = false |
| 118 |
end |
| 119 |
copied_pos = str_checked |
| 120 |
elseif next_char == 'Z' then |
| 121 |
-- %Z{sep} is marker for right separator and |
| 122 |
local sep = status:match('^%%Z{(.-)}', str_checked) |
| 123 |
str_checked = str_checked + #sep + 4 -- 4 = len(%{}) |
| 124 |
if status:find('^%%z', str_checked) or status:find('^%%<%%Z', str_checked) then |
| 125 |
-- When transitional right_sep and left_sep are right next to each other |
| 126 |
-- and in this exact order skip the left sep as we can't draw both. |
| 127 |
str_checked = status:find('}', str_checked) + 1 |
| 128 |
end |
| 129 |
local trans_sep = fill_section_separator(status, is_focused, str_checked, last_hl, sep, true) |
| 130 |
if trans_sep then |
| 131 |
table.insert(status_applied, trans_sep) |
| 132 |
end |
| 133 |
copied_pos = str_checked |
| 134 |
elseif next_char == '%' then |
| 135 |
str_checked = str_checked + 2 -- Skip the following % too |
| 136 |
elseif next_char == '=' and last_hl and (last_hl:find('^lualine_a') or last_hl:find('^lualine_b')) then |
| 137 |
-- TODO: Fix this properly |
| 138 |
-- This check for lualine_a and lualine_b is dumb. It doesn't guarantee |
| 139 |
-- c or x section isn't present. Worst case scenario after this patch |
| 140 |
-- we have another visual bug that occurs less frequently. |
| 141 |
-- Annoying Edge Cases |
| 142 |
last_hl = nil |
| 143 |
last_hl_reseted = true |
| 144 |
str_checked = str_checked + 1 -- Skip the following % too |
| 145 |
else |
| 146 |
str_checked = str_checked + 1 -- Push it forward to avoid inf loop |
| 147 |
end |
| 148 |
end |
| 149 |
table.insert(status_applied, status:sub(copied_pos)) -- Final chunk |
| 150 |
return table.concat(status_applied) |
| 151 |
end |
| 152 |
|
| 153 |
--- creates the statusline string |
| 154 |
---@param sections table : section config where components are replaced with |
| 155 |
--- component objects |
| 156 |
---@param is_focused boolean : whether being evaluated for focused window or not |
| 157 |
---@return string statusline string |
| 158 |
local statusline = modules.utils.retry_call_wrap(function(sections, is_focused, is_winbar) |
| 159 |
-- The sequence sections should maintain [SECTION_SEQUENCE] |
| 160 |
local section_sequence = { 'a', 'b', 'c', 'x', 'y', 'z' } |
| 161 |
local status = {} |
| 162 |
local applied_midsection_divider = false |
| 163 |
local applied_trunc = false |
| 164 |
for _, section_name in ipairs(section_sequence) do |
| 165 |
if sections['lualine_' .. section_name] then |
| 166 |
-- insert highlight+components of this section to status_builder |
| 167 |
local section_data = |
| 168 |
modules.utils_section.draw_section(sections['lualine_' .. section_name], section_name, is_focused) |
| 169 |
if #section_data > 0 then |
| 170 |
if not applied_midsection_divider and section_name > 'c' then |
| 171 |
applied_midsection_divider = true |
| 172 |
section_data = modules.highlight.format_highlight('c', is_focused) .. '%=' .. section_data |
| 173 |
end |
| 174 |
if not applied_trunc and section_name > 'b' then |
| 175 |
applied_trunc = true |
| 176 |
section_data = '%<' .. section_data |
| 177 |
end |
| 178 |
table.insert(status, section_data) |
| 179 |
end |
| 180 |
end |
| 181 |
end |
| 182 |
if applied_midsection_divider == false and config.options.always_divide_middle ~= false and not is_winbar then |
| 183 |
-- When non of section x,y,z is present |
| 184 |
table.insert(status, modules.highlight.format_highlight('c', is_focused) .. '%=') |
| 185 |
end |
| 186 |
return apply_transitional_separators(table.concat(status), is_focused) |
| 187 |
end) |
| 188 |
|
| 189 |
--- check if any extension matches the filetype and return proper sections |
| 190 |
---@param current_ft_list string[] : filetype name of current file |
| 191 |
---@param is_focused boolean : whether being evaluated for focused window or not |
| 192 |
---@return table|nil : (section_table) section config where components are replaced with |
| 193 |
--- component objects |
| 194 |
-- TODO: change this so it uses a hash table instead of iteration over list |
| 195 |
-- to improve redraws. Add buftype / bufname for extensions |
| 196 |
-- or some kind of cond ? |
| 197 |
local function get_extension_sections(current_ft_list, is_focused, sec_name) |
| 198 |
for _, ft in ipairs(current_ft_list) do |
| 199 |
for _, extension in ipairs(config.extensions) do |
| 200 |
if vim.tbl_contains(extension.filetypes, ft) then |
| 201 |
if is_focused then |
| 202 |
return extension[sec_name] |
| 203 |
else |
| 204 |
return extension['inactive_' .. sec_name] or extension[sec_name] |
| 205 |
end |
| 206 |
end |
| 207 |
end |
| 208 |
end |
| 209 |
return nil |
| 210 |
end |
| 211 |
|
| 212 |
---@return string statusline string for tabline |
| 213 |
local function tabline() |
| 214 |
return statusline(config.tabline, 3) |
| 215 |
end |
| 216 |
|
| 217 |
local function notify_theme_error(theme_name) |
| 218 |
local message_template = theme_name ~= 'auto' |
| 219 |
and [[ |
| 220 |
### options.theme |
| 221 |
Theme `%s` not found, falling back to `auto`. Check if spelling is right. |
| 222 |
]] |
| 223 |
or [[ |
| 224 |
### options.theme |
| 225 |
Theme `%s` failed, falling back to `gruvbox`. |
| 226 |
This shouldn't happen. |
| 227 |
Please report the issue at https://github.com/nvim-lualine/lualine.nvim/issues . |
| 228 |
Also provide what colorscheme you're using. |
| 229 |
]] |
| 230 |
modules.utils_notices.add_notice(string.format(message_template, theme_name)) |
| 231 |
end |
| 232 |
|
| 233 |
--- Sets up theme by defining hl groups and setting theme cache in 'highlight.lua'. |
| 234 |
--- Uses 'options.theme' variable to apply the theme: |
| 235 |
--- - If the value is a string, it'll load a theme of that name. |
| 236 |
--- - If it's a table, it's directly used as the theme. |
| 237 |
--- If loading the theme fails, this falls back to 'auto' theme. |
| 238 |
--- If the 'auto' theme also fails, this falls back to 'gruvbox' theme. |
| 239 |
--- Also sets up auto command to reload lualine on ColorScheme or background changes. |
| 240 |
local function setup_theme() |
| 241 |
local function get_theme_from_config() |
| 242 |
local theme_name = config.options.theme |
| 243 |
if type(theme_name) == 'string' then |
| 244 |
local ok, theme = pcall(modules.loader.load_theme, theme_name) |
| 245 |
if ok and theme then |
| 246 |
return theme |
| 247 |
end |
| 248 |
elseif type(theme_name) == 'table' then |
| 249 |
-- use the provided theme as-is |
| 250 |
return config.options.theme |
| 251 |
elseif type(theme_name) == 'function' then |
| 252 |
-- call function and use returned (dyanmic) theme, either as-is or as a string |
| 253 |
local ok, dynamic_theme = pcall(theme_name) |
| 254 |
if ok and (type(dynamic_theme) == 'string') then |
| 255 |
local ok_string, theme = pcall(modules.loader.load_theme, dynamic_theme) |
| 256 |
if ok_string and theme then |
| 257 |
return theme |
| 258 |
end |
| 259 |
elseif ok and (type(dynamic_theme) == 'table') then |
| 260 |
return dynamic_theme |
| 261 |
else |
| 262 |
local error_message = 'Invalid theme type returned from function: ' .. type(dynamic_theme) |
| 263 |
notify_theme_error(error_message) |
| 264 |
return dynamic_theme |
| 265 |
end |
| 266 |
end |
| 267 |
if theme_name ~= 'auto' then |
| 268 |
notify_theme_error(theme_name) |
| 269 |
local ok, theme = pcall(modules.loader.load_theme, 'auto') |
| 270 |
if ok and theme then |
| 271 |
return theme |
| 272 |
end |
| 273 |
end |
| 274 |
notify_theme_error('auto') |
| 275 |
return modules.loader.load_theme('gruvbox') |
| 276 |
end |
| 277 |
local theme = get_theme_from_config() |
| 278 |
modules.highlight.create_highlight_groups(theme) |
| 279 |
vim.cmd([[autocmd lualine ColorScheme * lua require'lualine'.setup() |
| 280 |
autocmd lualine OptionSet background lua require'lualine'.setup()]]) |
| 281 |
end |
| 282 |
|
| 283 |
---@alias StatusDispatchSecs |
| 284 |
---| 'sections' |
| 285 |
---| 'winbar' |
| 286 |
--- generates lualine.statusline & lualine.winbar function |
| 287 |
--- creates a closer that can draw sections of sec_name. |
| 288 |
---@param sec_name StatusDispatchSecs |
| 289 |
---@return function(focused:bool):string |
| 290 |
local function status_dispatch(sec_name) |
| 291 |
return function(focused) |
| 292 |
local retval |
| 293 |
local current_ft = refresh_real_curwin |
| 294 |
and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(refresh_real_curwin), 'filetype') |
| 295 |
or vim.bo.filetype |
| 296 |
local current_ft_list = vim.split(current_ft, '%.') -- handle compound filetypes c.doxygen |
| 297 |
local is_focused = focused ~= nil and focused or modules.utils.is_focused() |
| 298 |
for _, ft in ipairs(current_ft_list) do |
| 299 |
if |
| 300 |
vim.tbl_contains(config.options.disabled_filetypes[(sec_name == 'sections' and 'statusline' or sec_name)], ft) |
| 301 |
then |
| 302 |
-- disable on specific filetypes |
| 303 |
return nil |
| 304 |
end |
| 305 |
end |
| 306 |
local extension_sections = get_extension_sections(current_ft_list, is_focused, sec_name) |
| 307 |
if extension_sections ~= nil then |
| 308 |
retval = statusline(extension_sections, is_focused, sec_name == 'winbar') |
| 309 |
else |
| 310 |
retval = statusline(config[(is_focused and '' or 'inactive_') .. sec_name], is_focused, sec_name == 'winbar') |
| 311 |
end |
| 312 |
return retval |
| 313 |
end |
| 314 |
end |
| 315 |
|
| 316 |
---Determines if a focus event for this window should be ignored. |
| 317 |
--- |
| 318 |
---@param user_config table The user's config |
| 319 |
---@param win number The Neovim window handle to check |
| 320 |
---@return boolean Whether focus events for this window should be ignored |
| 321 |
local function should_ignore_focus(user_config, win) |
| 322 |
local ignore_focus = user_config.options.ignore_focus |
| 323 |
if type(ignore_focus) == 'table' then |
| 324 |
-- ignore focus on filetypes listed in options.ignore_focus |
| 325 |
local buf = vim.api.nvim_win_get_buf(win) |
| 326 |
local filetype = vim.api.nvim_buf_get_option(buf, 'filetype') |
| 327 |
return vim.tbl_contains(ignore_focus, filetype) |
| 328 |
elseif type(ignore_focus) == 'function' then |
| 329 |
return vim.api.nvim_win_call(win, function() |
| 330 |
return ignore_focus(win) |
| 331 |
end) |
| 332 |
else |
| 333 |
return false |
| 334 |
end |
| 335 |
end |
| 336 |
|
| 337 |
local refresh_event_queue = { |
| 338 |
has_events = false, |
| 339 |
statusline = {}, |
| 340 |
tabline = {}, |
| 341 |
winbar = {}, |
| 342 |
} |
| 343 |
|
| 344 |
---@alias LualineRefreshOptsKind |
| 345 |
---| 'all' |
| 346 |
---| 'tabpage' |
| 347 |
---| 'window' |
| 348 |
---@alias LualineRefreshOptsPlace |
| 349 |
---| 'statusline' |
| 350 |
---| 'tabline' |
| 351 |
---| 'winbar' |
| 352 |
---@class LualineRefreshOpts |
| 353 |
---@field scope LualineRefreshOptsKind |
| 354 |
---@field place LualineRefreshOptsPlace[] |
| 355 |
---@field trigger 'timer' | 'init' | 'autocmd' |'unknown' |
| 356 |
---@field queued boolean the refresh even was queue and queue is now being processed |
| 357 |
---@field force boolean force refresh now instead of queuing |
| 358 |
--- Refresh contents of lualine |
| 359 |
---@param opts LualineRefreshOpts |
| 360 |
local function refresh(opts) |
| 361 |
if opts == nil then |
| 362 |
opts = {} |
| 363 |
end |
| 364 |
opts = vim.tbl_extend('keep', opts, { |
| 365 |
scope = 'tabpage', |
| 366 |
place = { 'statusline', 'winbar', 'tabline' }, |
| 367 |
trigger = 'unknown', |
| 368 |
}) |
| 369 |
|
| 370 |
if not opts.queued and not opts.force then |
| 371 |
for _, place in ipairs(opts.place) do |
| 372 |
refresh_event_queue['has_events'] = true |
| 373 |
refresh_event_queue[place] = vim.tbl_extend('force', opts, { place = { place }, queued = true }) |
| 374 |
end |
| 375 |
return |
| 376 |
end |
| 377 |
|
| 378 |
local wins = {} |
| 379 |
local old_actual_curwin = vim.g.actual_curwin |
| 380 |
|
| 381 |
local curwin = vim.api.nvim_get_current_win() |
| 382 |
local curtab = vim.api.nvim_get_current_tabpage() |
| 383 |
if last_focus[curtab] == nil or not vim.api.nvim_win_is_valid(last_focus[curtab]) then |
| 384 |
if not should_ignore_focus(config, curwin) then |
| 385 |
last_focus[curtab] = curwin |
| 386 |
else |
| 387 |
local tab_wins = vim.api.nvim_tabpage_list_wins(curtab) |
| 388 |
if #tab_wins == 1 then |
| 389 |
last_focus[curtab] = curwin |
| 390 |
else |
| 391 |
local focusable_win = curwin |
| 392 |
for _, win in ipairs(tab_wins) do |
| 393 |
if not should_ignore_focus(config, win) then |
| 394 |
focusable_win = win |
| 395 |
break |
| 396 |
end |
| 397 |
end |
| 398 |
last_focus[curtab] = focusable_win |
| 399 |
end |
| 400 |
end |
| 401 |
else |
| 402 |
if not should_ignore_focus(config, curwin) then |
| 403 |
last_focus[curtab] = curwin |
| 404 |
end |
| 405 |
end |
| 406 |
vim.g.actual_curwin = last_focus[curtab] |
| 407 |
|
| 408 |
-- gather which windows needs update |
| 409 |
if opts.scope == 'all' then |
| 410 |
if vim.tbl_contains(opts.place, 'statusline') or vim.tbl_contains(opts.place, 'winbar') then |
| 411 |
wins = vim.tbl_filter(function(win) |
| 412 |
return vim.fn.win_gettype(win) ~= 'popup' |
| 413 |
end, vim.api.nvim_list_wins()) |
| 414 |
end |
| 415 |
elseif opts.scope == 'tabpage' then |
| 416 |
if vim.tbl_contains(opts.place, 'statusline') or vim.tbl_contains(opts.place, 'winbar') then |
| 417 |
wins = vim.tbl_filter(function(win) |
| 418 |
return vim.fn.win_gettype(win) ~= 'popup' |
| 419 |
end, vim.api.nvim_tabpage_list_wins(0)) |
| 420 |
end |
| 421 |
elseif opts.scope == 'window' then |
| 422 |
wins = { curwin } |
| 423 |
end |
| 424 |
|
| 425 |
-- update them |
| 426 |
if not timers.halt_stl_refresh and vim.tbl_contains(opts.place, 'statusline') then |
| 427 |
for _, win in ipairs(wins) do |
| 428 |
refresh_real_curwin = config.options.globalstatus and last_focus[curtab] or win |
| 429 |
local set_win = config.options.globalstatus |
| 430 |
and vim.fn.win_gettype(refresh_real_curwin) == 'popup' |
| 431 |
and refresh_real_curwin |
| 432 |
or win |
| 433 |
local stl_cur = vim.api.nvim_win_call(refresh_real_curwin, M.statusline) |
| 434 |
local stl_last = modules.nvim_opts.get_cache('statusline', { window = set_win }) |
| 435 |
if stl_cur or stl_last then |
| 436 |
modules.nvim_opts.set('statusline', stl_cur, { window = set_win }) |
| 437 |
end |
| 438 |
end |
| 439 |
end |
| 440 |
if not timers.halt_wb_refresh and vim.tbl_contains(opts.place, 'winbar') then |
| 441 |
for _, win in ipairs(wins) do |
| 442 |
refresh_real_curwin = win |
| 443 |
if vim.api.nvim_win_get_height(win) > 1 then |
| 444 |
local wbr_cur = vim.api.nvim_win_call(refresh_real_curwin, M.winbar) |
| 445 |
local wbr_last = modules.nvim_opts.get_cache('winbar', { window = win }) |
| 446 |
if wbr_cur or wbr_last then |
| 447 |
modules.nvim_opts.set('winbar', wbr_cur, { window = win }) |
| 448 |
end |
| 449 |
end |
| 450 |
end |
| 451 |
end |
| 452 |
if not timers.halt_tal_refresh and vim.tbl_contains(opts.place, 'tabline') then |
| 453 |
refresh_real_curwin = curwin |
| 454 |
local tbl_cur = vim.api.nvim_win_call(curwin, tabline) |
| 455 |
local tbl_last = modules.nvim_opts.get_cache('tabline', { global = true }) |
| 456 |
if tbl_cur or tbl_last then |
| 457 |
modules.nvim_opts.set('tabline', tbl_cur, { global = true }) |
| 458 |
end |
| 459 |
end |
| 460 |
|
| 461 |
vim.g.actual_curwin = old_actual_curwin |
| 462 |
refresh_real_curwin = nil |
| 463 |
end |
| 464 |
|
| 465 |
--- Sets &tabline option to lualine |
| 466 |
---@param hide boolean|nil if should hide tabline |
| 467 |
local function set_tabline(hide) |
| 468 |
vim.loop.timer_stop(timers.tal_timer) |
| 469 |
timers.halt_tal_refresh = true |
| 470 |
vim.cmd([[augroup lualine_tal_refresh | exe "autocmd!" | augroup END]]) |
| 471 |
if not hide and next(config.tabline) ~= nil then |
| 472 |
vim.loop.timer_start( |
| 473 |
timers.tal_timer, |
| 474 |
0, |
| 475 |
config.options.refresh.tabline, |
| 476 |
modules.utils.timer_call(timers.tal_timer, 'lualine_tal_refresh', function() |
| 477 |
refresh { scope = 'tabpage', place = { 'tabline' }, trigger = 'timer' } |
| 478 |
end, 3, 'lualine: Failed to refresh tabline') |
| 479 |
) |
| 480 |
modules.utils.define_autocmd( |
| 481 |
table.concat(config.options.refresh.events, ','), |
| 482 |
'*', |
| 483 |
"call v:lua.require'lualine'.refresh({'kind': 'tabpage', 'place': ['tabline'], 'trigger': 'autocmd'})", |
| 484 |
'lualine_tal_refresh' |
| 485 |
) |
| 486 |
|
| 487 |
modules.nvim_opts.set('showtabline', config.options.always_show_tabline and 2 or 1, { global = true }) |
| 488 |
timers.halt_tal_refresh = false |
| 489 |
vim.schedule(function() |
| 490 |
-- imediately refresh upon load |
| 491 |
-- schedule needed so stuff like filetype detect can run first |
| 492 |
refresh { scope = 'tabpage', place = { 'tabline' }, trigger = 'init' } |
| 493 |
end) |
| 494 |
else |
| 495 |
modules.nvim_opts.restore('tabline', { global = true }) |
| 496 |
modules.nvim_opts.restore('showtabline', { global = true }) |
| 497 |
end |
| 498 |
end |
| 499 |
|
| 500 |
local function check_refresh() |
| 501 |
if not refresh_event_queue.has_events then |
| 502 |
return |
| 503 |
end |
| 504 |
refresh_event_queue.has_events = nil |
| 505 |
|
| 506 |
for place, refresh_cmd in pairs(refresh_event_queue) do |
| 507 |
if type(refresh_cmd) == 'table' and refresh_cmd.queued == true then |
| 508 |
refresh(refresh_cmd) |
| 509 |
refresh_event_queue[place] = {} |
| 510 |
end |
| 511 |
end |
| 512 |
end |
| 513 |
|
| 514 |
local function set_refresh_checker() |
| 515 |
vim.loop.timer_stop(timers.refresh_check_timer) |
| 516 |
vim.loop.timer_start( |
| 517 |
timers.refresh_check_timer, |
| 518 |
0, |
| 519 |
config.options.refresh.refresh_time, |
| 520 |
modules.utils.timer_call( |
| 521 |
timers.refresh_check_timer, |
| 522 |
'lualine_refresh_check', |
| 523 |
check_refresh, |
| 524 |
3, |
| 525 |
'lualine: Failed to refresh statusline' |
| 526 |
) |
| 527 |
) |
| 528 |
end |
| 529 |
|
| 530 |
--- Sets &statusline option to lualine |
| 531 |
--- adds auto command to redraw lualine on VimResized event |
| 532 |
---@param hide boolean|nil if should hide statusline |
| 533 |
local function set_statusline(hide) |
| 534 |
vim.loop.timer_stop(timers.stl_timer) |
| 535 |
timers.halt_stl_refresh = true |
| 536 |
vim.cmd([[augroup lualine_stl_refresh | exe "autocmd!" | augroup END]]) |
| 537 |
if not hide and (next(config.sections) ~= nil or next(config.inactive_sections) ~= nil) then |
| 538 |
modules.nvim_opts.set('statusline', '%#lualine_transparent#', { global = true }) |
| 539 |
if config.options.globalstatus then |
| 540 |
modules.nvim_opts.set('laststatus', 3, { global = true }) |
| 541 |
vim.loop.timer_start( |
| 542 |
timers.stl_timer, |
| 543 |
0, |
| 544 |
config.options.refresh.statusline, |
| 545 |
modules.utils.timer_call(timers.stl_timer, 'lualine_stl_refresh', function() |
| 546 |
refresh { scope = 'window', place = { 'statusline' }, trigger = 'timer' } |
| 547 |
end, 3, 'lualine: Failed to refresh statusline') |
| 548 |
) |
| 549 |
modules.utils.define_autocmd( |
| 550 |
table.concat(config.options.refresh.events, ','), |
| 551 |
'*', |
| 552 |
"call v:lua.require'lualine'.refresh({'kind': 'window', 'place': ['statusline'], 'trigger': 'autocmd'})", |
| 553 |
'lualine_stl_refresh' |
| 554 |
) |
| 555 |
else |
| 556 |
modules.nvim_opts.set('laststatus', 2, { global = true }) |
| 557 |
vim.loop.timer_start( |
| 558 |
timers.stl_timer, |
| 559 |
0, |
| 560 |
config.options.refresh.statusline, |
| 561 |
modules.utils.timer_call(timers.stl_timer, 'lualine_stl_refresh', function() |
| 562 |
refresh { scope = 'tabpage', place = { 'statusline' }, trigger = 'timer' } |
| 563 |
end, 3, 'lualine: Failed to refresh statusline') |
| 564 |
) |
| 565 |
|
| 566 |
modules.utils.define_autocmd( |
| 567 |
table.concat(config.options.refresh.events, ','), |
| 568 |
'*', |
| 569 |
"call v:lua.require'lualine'.refresh({'kind': 'tabpage', 'place': ['statusline'], 'trigger': 'autocmd'})", |
| 570 |
'lualine_stl_refresh' |
| 571 |
) |
| 572 |
end |
| 573 |
timers.halt_stl_refresh = false |
| 574 |
vim.schedule(function() |
| 575 |
-- imediately refresh upon load |
| 576 |
-- schedule needed so stuff like filetype detect can run first |
| 577 |
if config.options.globalstatus then |
| 578 |
refresh { scope = 'window', place = { 'statusline' }, trigger = 'init' } |
| 579 |
else |
| 580 |
refresh { scope = 'tabpage', place = { 'statusline' }, trigger = 'init' } |
| 581 |
end |
| 582 |
end) |
| 583 |
else |
| 584 |
modules.nvim_opts.restore('statusline', { global = true }) |
| 585 |
for _, win in ipairs(vim.api.nvim_list_wins()) do |
| 586 |
modules.nvim_opts.restore('statusline', { window = win }) |
| 587 |
end |
| 588 |
modules.nvim_opts.restore('laststatus', { global = true }) |
| 589 |
end |
| 590 |
end |
| 591 |
|
| 592 |
--- Sets &winbar option to lualine |
| 593 |
---@param hide boolean|nil if should unset winbar |
| 594 |
local function set_winbar(hide) |
| 595 |
vim.loop.timer_stop(timers.wb_timer) |
| 596 |
timers.halt_wb_refresh = true |
| 597 |
vim.cmd([[augroup lualine_wb_refresh | exe "autocmd!" | augroup END]]) |
| 598 |
if not hide and (next(config.winbar) ~= nil or next(config.inactive_winbar) ~= nil) then |
| 599 |
vim.loop.timer_start( |
| 600 |
timers.wb_timer, |
| 601 |
0, |
| 602 |
config.options.refresh.winbar, |
| 603 |
modules.utils.timer_call(timers.wb_timer, 'lualine_wb_refresh', function() |
| 604 |
refresh { scope = 'tabpage', place = { 'winbar' }, trigger = 'timer' } |
| 605 |
end, 3, 'lualine: Failed to refresh winbar') |
| 606 |
) |
| 607 |
modules.utils.define_autocmd( |
| 608 |
table.concat(config.options.refresh.events, ','), |
| 609 |
'*', |
| 610 |
"call v:lua.require'lualine'.refresh({'kind': 'tabpage', 'place': ['winbar'], 'trigger': 'autocmd'})", |
| 611 |
'lualine_wb_refresh' |
| 612 |
) |
| 613 |
|
| 614 |
timers.halt_wb_refresh = false |
| 615 |
vim.schedule(function() |
| 616 |
-- imediately refresh upon load. |
| 617 |
-- schedule needed so stuff like filetype detect can run first |
| 618 |
refresh { scope = 'tabpage', place = { 'winbar' }, trigger = 'init' } |
| 619 |
end) |
| 620 |
elseif vim.fn.has('nvim-0.8') == 1 then |
| 621 |
modules.nvim_opts.restore('winbar', { global = true }) |
| 622 |
for _, win in ipairs(vim.api.nvim_list_wins()) do |
| 623 |
modules.nvim_opts.restore('winbar', { window = win }) |
| 624 |
end |
| 625 |
end |
| 626 |
end |
| 627 |
|
| 628 |
---@alias LualineHideOptsPlace |
| 629 |
---| 'statusline' |
| 630 |
---| 'tabline' |
| 631 |
---| 'winbar' |
| 632 |
---@class LualineHideOpts |
| 633 |
---@field place LualineHideOptsPlace[] |
| 634 |
---@field unhide boolean |
| 635 |
---@param opts LualineHideOpts |
| 636 |
local function hide(opts) |
| 637 |
if opts == nil then |
| 638 |
opts = {} |
| 639 |
end |
| 640 |
opts = vim.tbl_extend('keep', opts, { |
| 641 |
place = { 'statusline', 'tabline', 'winbar' }, |
| 642 |
unhide = false, |
| 643 |
}) |
| 644 |
local hide_fn = { |
| 645 |
statusline = set_statusline, |
| 646 |
tabline = set_tabline, |
| 647 |
winbar = set_winbar, |
| 648 |
} |
| 649 |
for _, place in ipairs(opts.place) do |
| 650 |
if hide_fn[place] then |
| 651 |
hide_fn[place](not opts.unhide) |
| 652 |
end |
| 653 |
end |
| 654 |
end |
| 655 |
|
| 656 |
--- Check neovim compatibilitu |
| 657 |
local function verify_nvim_version() |
| 658 |
if vim.fn.has('nvim-0.7') == 1 then |
| 659 |
return true |
| 660 |
end |
| 661 |
modules.utils_notices.add_notice([[ |
| 662 |
### Incompatible Neovim version |
| 663 |
Lualine supports neovim 0.7 and up. It seems you're using a older version. |
| 664 |
Please update to newer version. Or if you have atleast neovim 0.5 you |
| 665 |
can use older compatible versions of lualine using compat tags like |
| 666 |
`compat-nvim-0.5`, `compat-nvim-0.6`. |
| 667 |
]]) |
| 668 |
return false |
| 669 |
end |
| 670 |
|
| 671 |
-- lualine.setup function |
| 672 |
--- sets new user config |
| 673 |
--- This function doesn't load components/theme etc... They are done before |
| 674 |
--- first statusline redraw and after new config. This is more efficient when |
| 675 |
--- lualine config is done in several setup calls as chunks. This way |
| 676 |
--- we don't initialize components just to throw them away. Instead they are |
| 677 |
--- initialized when we know we will use them. |
| 678 |
--- sets &last_status to 2 |
| 679 |
---@param user_config table table |
| 680 |
local function setup(user_config) |
| 681 |
if package.loaded['lualine.utils.notices'] then |
| 682 |
-- When notices module is not loaded there are no notices to clear. |
| 683 |
modules.utils_notices.clear_notices() |
| 684 |
end |
| 685 |
if verify_nvim_version() then |
| 686 |
config = modules.config_module.apply_configuration(user_config) |
| 687 |
vim.cmd([[augroup lualine | exe "autocmd!" | augroup END]]) |
| 688 |
setup_theme() |
| 689 |
-- load components & extensions |
| 690 |
modules.loader.load_all(config) |
| 691 |
set_statusline() |
| 692 |
set_tabline() |
| 693 |
set_winbar() |
| 694 |
set_refresh_checker() |
| 695 |
end |
| 696 |
if package.loaded['lualine.utils.notices'] then |
| 697 |
modules.utils_notices.notice_message_startup() |
| 698 |
end |
| 699 |
end |
| 700 |
|
| 701 |
M = { |
| 702 |
setup = setup, |
| 703 |
statusline = status_dispatch('sections'), |
| 704 |
tabline = tabline, |
| 705 |
get_config = modules.config_module.get_config, |
| 706 |
refresh = refresh, |
| 707 |
winbar = status_dispatch('winbar'), |
| 708 |
hide = hide, |
| 709 |
} |
| 710 |
|
| 711 |
return M |