2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-08-16 17:19:56 +00:00
|
|
|
local modules = require('lualine.utils.lazy_require'){
|
|
|
|
highlight = 'lualine.highlight',
|
|
|
|
loader = 'lualine.utils.loader',
|
|
|
|
utils_section = 'lualine.utils.section',
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
utils_notices = 'lualine.utils.notices',
|
|
|
|
config_module = 'lualine.config',
|
|
|
|
}
|
|
|
|
local config -- Stores cureently applied config
|
2021-08-17 07:26:44 +00:00
|
|
|
local new_config = true -- Stores config that will be applied
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-08-26 18:09:02 +00:00
|
|
|
-- Helper for apply_transitional_separators()
|
2021-08-30 03:37:50 +00:00
|
|
|
local function find_next_hl(status, str_checked)
|
2021-08-26 18:09:02 +00:00
|
|
|
-- Gets the next valid hl group from str_checked
|
|
|
|
local hl_pos_start, hl_pos_end = status:find('%%#.-#', str_checked)
|
|
|
|
while true do
|
|
|
|
if not hl_pos_start then return nil end
|
|
|
|
-- When there are more that one hl group next to one another like
|
|
|
|
-- %#HL1#%#HL2#%#HL3# we need to return HL3. This makes that happen.
|
|
|
|
local next_start, next_end = status:find('^%%#.-#', hl_pos_end + 1)
|
|
|
|
if next_start == nil then break end
|
|
|
|
hl_pos_start, hl_pos_end = next_start, next_end
|
|
|
|
end
|
2021-08-30 03:37:50 +00:00
|
|
|
return status:sub(hl_pos_start + 2, hl_pos_end - 1)
|
2021-08-26 18:09:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Helper for apply_transitional_separators()
|
|
|
|
local function fill_section_separator(status, str_checked, last_hl, sep, reverse)
|
|
|
|
-- Inserts transitional separator along with transitional highlight
|
2021-08-30 03:37:50 +00:00
|
|
|
if last_hl and #last_hl == 0 then return end
|
|
|
|
local next_hl = find_next_hl(status, str_checked)
|
|
|
|
if next_hl == nil then return end
|
2021-08-26 18:09:02 +00:00
|
|
|
local transitional_highlight = reverse -- lua ternary assignment x ? y : z
|
2021-08-30 03:37:50 +00:00
|
|
|
and modules.highlight.get_transitional_highlights(last_hl, next_hl)
|
|
|
|
or modules.highlight.get_transitional_highlights(next_hl, last_hl)
|
2021-08-26 18:09:02 +00:00
|
|
|
if transitional_highlight then
|
|
|
|
return transitional_highlight .. sep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-25 11:43:12 +00:00
|
|
|
local function apply_transitional_separators(status)
|
|
|
|
local status_applied = {} -- Collects all the pieces for concatation
|
2021-08-30 03:37:50 +00:00
|
|
|
local last_hl -- Stores lash highligjt group that we found
|
2021-08-25 11:43:12 +00:00
|
|
|
local copied_pos = 1 -- Tracks how much we've copied over to status_applied
|
|
|
|
local str_checked = 1 -- Tracks where the searcher head is at
|
2021-05-07 00:54:49 +00:00
|
|
|
|
2021-05-07 15:39:28 +00:00
|
|
|
|
2021-08-25 11:43:12 +00:00
|
|
|
-- Process entire status replace the %s{sep} & %S{sep} placeholders
|
|
|
|
-- with proper transitional separator.
|
|
|
|
while str_checked ~= nil do
|
|
|
|
str_checked = status:find('%%', str_checked)
|
|
|
|
if str_checked == nil then break end
|
|
|
|
table.insert(status_applied, status:sub(copied_pos, str_checked - 1))
|
|
|
|
-- -1 so we don't copy '%'
|
|
|
|
copied_pos = str_checked
|
|
|
|
local next_char = modules.utils.charAt(status, str_checked +1)
|
|
|
|
if next_char == '#' then
|
|
|
|
-- %#hl_name# highlights
|
2021-08-30 03:37:50 +00:00
|
|
|
last_hl = status:match('^%%#(.-)#', str_checked)
|
|
|
|
str_checked = str_checked + #last_hl + 3
|
2021-08-25 11:43:12 +00:00
|
|
|
elseif next_char == 's' then
|
|
|
|
-- %s{sep} is marker for left separator and
|
|
|
|
local sep = status:match('^%%s{(.-)}', str_checked)
|
|
|
|
str_checked = str_checked + #sep + 4 -- 4 = len(%{})
|
2021-08-26 18:09:02 +00:00
|
|
|
local trans_sep = fill_section_separator(status, str_checked, last_hl, sep, false)
|
|
|
|
if trans_sep then table.insert(status_applied, trans_sep) end
|
2021-08-25 11:43:12 +00:00
|
|
|
copied_pos = str_checked
|
|
|
|
elseif next_char == 'S' then
|
|
|
|
-- %S{sep} is marker for right separator and
|
|
|
|
local sep = status:match('^%%S{(.-)}', str_checked)
|
|
|
|
str_checked = str_checked + #sep + 4 -- 4 = len(%{})
|
2021-08-30 03:37:50 +00:00
|
|
|
if status:find('^%%s', str_checked) then
|
2021-08-26 11:26:13 +00:00
|
|
|
-- When transitional right_sep and left_sep are right next to each other
|
|
|
|
-- and in this exact order skip the left sep as we can't draw both.
|
|
|
|
str_checked = status:find('}', str_checked) + 1
|
|
|
|
end
|
2021-08-26 18:09:02 +00:00
|
|
|
local trans_sep = fill_section_separator(status, str_checked, last_hl, sep, true)
|
|
|
|
if trans_sep then table.insert(status_applied, trans_sep) end
|
2021-08-25 11:43:12 +00:00
|
|
|
copied_pos = str_checked
|
|
|
|
elseif next_char == '%' then
|
|
|
|
str_checked = str_checked + 2 -- Skip the following % too
|
2021-08-26 14:29:25 +00:00
|
|
|
elseif next_char == '=' and last_hl and
|
2021-08-30 03:37:50 +00:00
|
|
|
(last_hl:find('^lualine_a') or last_hl:find('^lualine_b')) then
|
2021-08-26 14:29:25 +00:00
|
|
|
-- TODO: Fix this properly
|
|
|
|
-- This check for lualine_a and lualine_b is dumb. It doesn't garantee
|
|
|
|
-- c or x section isn't present. Worst case sinario after this patch
|
|
|
|
-- we have another visual bug that occurs less frequently.
|
|
|
|
-- Annoying Edge Cases............................................
|
2021-08-30 03:37:50 +00:00
|
|
|
last_hl = nil
|
2021-08-25 12:35:41 +00:00
|
|
|
str_checked = str_checked + 1 -- Skip the following % too
|
2021-05-07 15:39:28 +00:00
|
|
|
else
|
2021-08-25 11:43:12 +00:00
|
|
|
str_checked = str_checked + 1 -- Push it forward to avoid inf loop
|
2021-05-07 15:39:28 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-25 11:43:12 +00:00
|
|
|
table.insert(status_applied, status:sub(copied_pos)) -- Final chunk
|
|
|
|
return table.concat(status_applied)
|
2021-05-07 15:39:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function statusline(sections, is_focused)
|
2021-08-25 11:43:12 +00:00
|
|
|
-- The sequence sections should maintain [SECTION_SEQUENCE]
|
2021-05-07 15:39:28 +00:00
|
|
|
local section_sequence = {'a', 'b', 'c', 'x', 'y', 'z'}
|
2021-08-25 11:43:12 +00:00
|
|
|
local status = {}
|
|
|
|
local applied_midsection_devider = false
|
2021-08-27 19:06:19 +00:00
|
|
|
local applied_trunc = false
|
2021-05-07 15:39:28 +00:00
|
|
|
for _, section_name in ipairs(section_sequence) do
|
|
|
|
if sections['lualine_' .. section_name] then
|
|
|
|
-- insert highlight+components of this section to status_builder
|
2021-08-16 17:19:56 +00:00
|
|
|
local section_data = modules.utils_section.draw_section(
|
2021-05-07 15:39:28 +00:00
|
|
|
sections['lualine_' .. section_name],
|
|
|
|
section_name, is_focused)
|
|
|
|
if #section_data > 0 then
|
2021-08-25 11:43:12 +00:00
|
|
|
if not applied_midsection_devider and section_name > 'c' then
|
|
|
|
applied_midsection_devider = true
|
|
|
|
section_data = '%='..section_data
|
|
|
|
end
|
2021-08-27 19:06:19 +00:00
|
|
|
if not applied_trunc and section_name > 'b' then
|
|
|
|
applied_trunc = true
|
|
|
|
section_data = '%<'..section_data
|
|
|
|
end
|
2021-08-25 11:43:12 +00:00
|
|
|
table.insert(status, section_data)
|
2021-05-07 15:39:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-25 11:43:12 +00:00
|
|
|
return apply_transitional_separators(table.concat(status))
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-03-13 00:21:37 +00:00
|
|
|
-- check if any extension matches the filetype and return proper sections
|
2021-08-03 06:08:17 +00:00
|
|
|
local function get_extension_sections(current_ft, is_focused)
|
2021-03-17 00:02:13 +00:00
|
|
|
for _, extension in ipairs(config.extensions) do
|
2021-03-13 00:21:37 +00:00
|
|
|
for _, filetype in ipairs(extension.filetypes) do
|
2021-08-03 06:08:17 +00:00
|
|
|
if current_ft == filetype then
|
|
|
|
if is_focused == false and extension.inactive_sections then
|
|
|
|
return extension.inactive_sections
|
|
|
|
end
|
|
|
|
return extension.sections
|
|
|
|
end
|
2021-03-13 00:21:37 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-11 19:40:54 +00:00
|
|
|
return nil
|
2021-03-13 00:21:37 +00:00
|
|
|
end
|
|
|
|
|
2021-03-17 00:02:13 +00:00
|
|
|
local function tabline() return statusline(config.tabline, true) end
|
2021-03-06 15:03:00 +00:00
|
|
|
|
2021-08-14 06:02:30 +00:00
|
|
|
local function check_theme_name_deprecation(theme_name)
|
|
|
|
local deprection_table = {
|
|
|
|
oceanicnext = 'OceanicNext',
|
|
|
|
papercolor = 'PaperColor',
|
|
|
|
tomorrow = 'Tomorrow',
|
|
|
|
gruvbox_material = 'gruvbox-material',
|
|
|
|
modus_vivendi = 'modus-vivendi',
|
|
|
|
}
|
|
|
|
if deprection_table[theme_name] then
|
|
|
|
local correct_name = deprection_table[theme_name]
|
2021-08-16 17:19:56 +00:00
|
|
|
modules.utils_notices.add_notice(string.format([[
|
2021-08-14 06:02:30 +00:00
|
|
|
### options.theme
|
|
|
|
You're using `%s` as theme name .
|
|
|
|
It has recently been renamed to `%s`.
|
|
|
|
Please update your config to follow that.
|
|
|
|
|
|
|
|
You have something like this in your config.
|
|
|
|
```lua
|
|
|
|
options = {
|
|
|
|
theme = '%s'
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You'll have to change it to something like this.
|
|
|
|
```lua
|
|
|
|
options = {
|
|
|
|
theme = '%s'
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
]], theme_name, correct_name, theme_name, correct_name))
|
|
|
|
return correct_name
|
|
|
|
end
|
|
|
|
return theme_name
|
|
|
|
end
|
|
|
|
|
|
|
|
local function notify_theme_error(theme_name)
|
|
|
|
local message_template = theme_name ~= 'auto' and [[
|
|
|
|
### options.theme
|
|
|
|
Theme `%s` not found, falling back to `auto`. Check if spelling is right.
|
|
|
|
]] or [[
|
|
|
|
### options.theme
|
|
|
|
Theme `%s` failed, falling back to `gruvbox`.
|
|
|
|
This shouldn't happen.
|
|
|
|
Please report the issue at https://github.com/shadmansaleh/lualine.nvim/issues .
|
|
|
|
Also provide what colorscheme you're using.
|
|
|
|
]]
|
2021-08-16 17:19:56 +00:00
|
|
|
modules.utils_notices.add_notice(string.format(message_template, theme_name))
|
2021-08-14 06:02:30 +00:00
|
|
|
end
|
|
|
|
|
2021-03-06 15:03:00 +00:00
|
|
|
local function setup_theme()
|
2021-08-02 15:16:17 +00:00
|
|
|
local function get_theme_from_config()
|
|
|
|
local theme_name = config.options.theme
|
|
|
|
if type(theme_name) == 'string' then
|
2021-08-14 06:02:30 +00:00
|
|
|
theme_name = check_theme_name_deprecation(theme_name)
|
2021-08-16 17:19:56 +00:00
|
|
|
local ok, theme = pcall(modules.loader.load_theme, theme_name)
|
2021-08-14 06:02:30 +00:00
|
|
|
if ok and theme then return theme end
|
2021-08-02 15:16:17 +00:00
|
|
|
elseif type(theme_name) == 'table' then
|
|
|
|
-- use the provided theme as-is
|
|
|
|
return config.options.theme
|
|
|
|
end
|
2021-08-14 06:02:30 +00:00
|
|
|
if theme_name ~= 'auto' then
|
|
|
|
notify_theme_error(theme_name)
|
2021-08-16 17:19:56 +00:00
|
|
|
local ok, theme = pcall(modules.loader.load_theme, 'auto')
|
2021-08-14 06:02:30 +00:00
|
|
|
if ok and theme then return theme end
|
|
|
|
end
|
|
|
|
notify_theme_error('auto')
|
2021-08-16 17:19:56 +00:00
|
|
|
return modules.loader.load_theme('gruvbox')
|
2021-08-02 15:16:17 +00:00
|
|
|
end
|
|
|
|
local theme = get_theme_from_config()
|
2021-08-16 17:19:56 +00:00
|
|
|
modules.highlight.create_highlight_groups(theme)
|
2021-08-02 15:16:17 +00:00
|
|
|
vim.cmd [[
|
|
|
|
autocmd lualine ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
|
2021-08-04 02:37:35 +00:00
|
|
|
autocmd lualine OptionSet background lua require'lualine'.setup()
|
2021-08-08 16:03:58 +00:00
|
|
|
]]
|
2021-01-04 00:17:51 +00:00
|
|
|
end
|
|
|
|
|
2021-03-06 15:03:00 +00:00
|
|
|
local function set_tabline()
|
2021-03-17 00:02:13 +00:00
|
|
|
if next(config.tabline) ~= nil then
|
2021-08-08 16:03:58 +00:00
|
|
|
vim.go.tabline = "%{%v:lua.require'lualine'.tabline()%}"
|
|
|
|
vim.go.showtabline = 2
|
2021-03-06 15:03:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_statusline()
|
2021-03-17 00:02:13 +00:00
|
|
|
if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
|
2021-08-16 17:19:56 +00:00
|
|
|
vim.cmd('autocmd lualine VimResized * redrawstatus')
|
|
|
|
else
|
|
|
|
vim.go.statusline = nil
|
2021-03-06 15:03:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-02 15:16:17 +00:00
|
|
|
local function setup_augroup()
|
|
|
|
vim.cmd [[
|
|
|
|
augroup lualine
|
|
|
|
autocmd!
|
|
|
|
augroup END
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
|
2021-08-16 17:19:56 +00:00
|
|
|
local function reset_lualine()
|
|
|
|
modules.utils_notices.clear_notices()
|
2021-08-02 15:16:17 +00:00
|
|
|
setup_augroup()
|
2021-03-06 15:03:00 +00:00
|
|
|
setup_theme()
|
2021-08-16 17:19:56 +00:00
|
|
|
modules.loader.load_all(config)
|
2021-03-06 15:03:00 +00:00
|
|
|
set_statusline()
|
|
|
|
set_tabline()
|
2021-08-16 17:19:56 +00:00
|
|
|
modules.utils_notices.notice_message_startup()
|
|
|
|
new_config = nil
|
|
|
|
end
|
|
|
|
|
2021-08-22 08:06:46 +00:00
|
|
|
local function status_dispatch(focused)
|
2021-08-16 17:19:56 +00:00
|
|
|
-- disable on specific filetypes
|
|
|
|
if new_config then reset_lualine() end
|
|
|
|
local current_ft = vim.bo.filetype
|
2021-08-22 08:06:46 +00:00
|
|
|
local is_focused = focused ~= nil and focused or modules.utils.is_focused()
|
2021-08-16 17:19:56 +00:00
|
|
|
for _, ft in pairs(config.options.disabled_filetypes) do
|
|
|
|
if ft == current_ft then
|
|
|
|
vim.wo.statusline = ''
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local extension_sections = get_extension_sections(current_ft, is_focused)
|
|
|
|
if is_focused then
|
|
|
|
if extension_sections ~= nil then
|
|
|
|
return statusline(extension_sections, is_focused)
|
|
|
|
end
|
|
|
|
return statusline(config.sections, is_focused)
|
|
|
|
else
|
|
|
|
if extension_sections ~= nil then
|
|
|
|
return statusline(extension_sections, is_focused)
|
|
|
|
end
|
|
|
|
return statusline(config.inactive_sections, is_focused)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function setup(user_config)
|
2021-08-17 07:26:44 +00:00
|
|
|
new_config = true
|
|
|
|
config = modules.config_module.apply_configuration(user_config)
|
2021-08-16 17:19:56 +00:00
|
|
|
vim.go.statusline = "%{%v:lua.require'lualine'.statusline()%}"
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-08-04 01:13:37 +00:00
|
|
|
return {
|
|
|
|
setup = setup,
|
|
|
|
statusline = status_dispatch,
|
|
|
|
tabline = tabline,
|
2021-08-17 07:26:44 +00:00
|
|
|
get_config = modules.config_module.get_config,
|
2021-08-04 01:13:37 +00:00
|
|
|
}
|