2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2020-12-30 14:48:51 +00:00
|
|
|
local highlight = require('lualine.highlight')
|
2021-05-09 21:11:18 +00:00
|
|
|
local loader = require('lualine.utils.loader')
|
|
|
|
local utils_section = require('lualine.utils.section')
|
2021-08-08 16:03:58 +00:00
|
|
|
local utils = require('lualine.utils.utils')
|
2021-08-09 07:53:42 +00:00
|
|
|
local utils_notices = require('lualine.utils.notices')
|
2021-05-09 21:11:18 +00:00
|
|
|
local config_module = require('lualine.config')
|
2021-03-10 23:07:38 +00:00
|
|
|
|
2021-05-09 21:11:18 +00:00
|
|
|
local config = config_module.config
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-05-07 15:39:28 +00:00
|
|
|
local function apply_transitional_separators(previous_section, current_section,
|
|
|
|
next_section)
|
2021-05-07 00:54:49 +00:00
|
|
|
|
|
|
|
local function fill_section_separator(prev, next, sep, reverse)
|
|
|
|
if #sep == 0 then return 0 end
|
2021-05-07 15:39:28 +00:00
|
|
|
local transitional_highlight = highlight.get_transitional_highlights(prev,
|
|
|
|
next,
|
|
|
|
reverse)
|
2021-05-07 00:54:49 +00:00
|
|
|
if transitional_highlight and #transitional_highlight > 0 then
|
2021-05-07 15:39:28 +00:00
|
|
|
return transitional_highlight .. sep
|
2021-05-07 00:54:49 +00:00
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
2021-05-07 15:39:28 +00:00
|
|
|
|
|
|
|
-- variable to track separators position
|
|
|
|
local sep_pos = 1
|
|
|
|
|
|
|
|
-- %s{sep} is marker for left separator and
|
|
|
|
-- %S{sep} is marker for right separator
|
|
|
|
-- Apply left separator
|
|
|
|
while sep_pos do
|
|
|
|
-- get what the separator char
|
|
|
|
local sep = current_section.data:match('%%s{(.-)}', sep_pos)
|
|
|
|
-- Get where separator starts from
|
|
|
|
sep_pos = current_section.data:find('%%s{.-}', sep_pos)
|
|
|
|
if not sep or not sep_pos then break end
|
|
|
|
-- part of section before separator . -1 since we don't want the %
|
|
|
|
local prev = current_section.data:sub(1, sep_pos - 1)
|
|
|
|
-- part of section after separator. 4 is length of "%s{}"
|
|
|
|
local nxt = current_section.data:sub(sep_pos + 4 + #sep)
|
|
|
|
-- prev might not exist when separator is the first element of section
|
|
|
|
-- use previous section as prev
|
|
|
|
if not prev or #prev == 0 or sep_pos == 1 then
|
|
|
|
prev = previous_section.data
|
|
|
|
end
|
|
|
|
if prev ~= previous_section.data then
|
|
|
|
-- Since the section isn't suppose to be highlighted with separators
|
|
|
|
-- separators highlight extract the last highlight and place it between
|
|
|
|
-- separator and section
|
2021-05-12 15:24:09 +00:00
|
|
|
local last_hl = prev:match('.*(%%#.-#).-')
|
2021-05-07 15:39:28 +00:00
|
|
|
current_section.data = prev ..
|
|
|
|
fill_section_separator(prev, nxt, sep, false) ..
|
2021-05-12 15:24:09 +00:00
|
|
|
last_hl .. nxt
|
2021-05-07 15:39:28 +00:00
|
|
|
else
|
|
|
|
current_section.data = fill_section_separator(prev, nxt, sep, true) .. nxt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reset pos for right separator
|
|
|
|
sep_pos = 1
|
|
|
|
-- Apply right separator
|
|
|
|
while sep_pos do
|
|
|
|
local sep = current_section.data:match('%%S{(.-)}', sep_pos)
|
|
|
|
sep_pos = current_section.data:find('%%S{.-}', sep_pos)
|
|
|
|
if not sep or not sep_pos then break end
|
|
|
|
local prev = current_section.data:sub(1, sep_pos - 1)
|
|
|
|
local nxt = current_section.data:sub(sep_pos + 4 + #sep)
|
|
|
|
if not nxt or #nxt == 0 or sep_pos == #current_section.data then
|
|
|
|
nxt = next_section.data
|
|
|
|
end
|
|
|
|
if nxt ~= next_section.data then
|
|
|
|
current_section.data = prev ..
|
|
|
|
fill_section_separator(prev, nxt, sep, false) ..
|
|
|
|
nxt
|
|
|
|
else
|
|
|
|
current_section.data = prev ..
|
|
|
|
fill_section_separator(prev, nxt, sep, false)
|
|
|
|
end
|
|
|
|
sep_pos = sep_pos + 4 + #sep
|
|
|
|
end
|
|
|
|
return current_section.data
|
|
|
|
end
|
|
|
|
|
|
|
|
local function statusline(sections, is_focused)
|
|
|
|
|
2021-02-20 03:21:05 +00:00
|
|
|
-- status_builder stores statusline without section_separators
|
2021-05-07 15:39:28 +00:00
|
|
|
-- The sequence sections should maintain
|
|
|
|
local section_sequence = {'a', 'b', 'c', 'x', 'y', 'z'}
|
|
|
|
local status_builder = {}
|
|
|
|
for _, section_name in ipairs(section_sequence) do
|
|
|
|
if sections['lualine_' .. section_name] then
|
|
|
|
-- insert highlight+components of this section to status_builder
|
|
|
|
local section_data = utils_section.draw_section(
|
|
|
|
sections['lualine_' .. section_name],
|
|
|
|
section_name, is_focused)
|
|
|
|
if #section_data > 0 then
|
|
|
|
table.insert(status_builder, {name = section_name, data = section_data})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
|
|
|
|
-- Actual statusline
|
|
|
|
local status = {}
|
|
|
|
local half_passed = false
|
2021-03-15 23:37:46 +00:00
|
|
|
for i = 1, #status_builder do
|
2021-02-20 03:21:05 +00:00
|
|
|
-- midsection divider
|
|
|
|
if not half_passed and status_builder[i].name > 'c' then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status,
|
|
|
|
highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
|
2021-02-20 03:21:05 +00:00
|
|
|
half_passed = true
|
|
|
|
end
|
2021-05-07 15:39:28 +00:00
|
|
|
-- component separator needs to have fg = current_section.bg
|
|
|
|
-- and bg = adjacent_section.bg
|
|
|
|
local previous_section = status_builder[i - 1] or {}
|
|
|
|
local current_section = status_builder[i]
|
|
|
|
local next_section = status_builder[i + 1] or {}
|
2021-02-20 03:21:05 +00:00
|
|
|
|
2021-05-07 15:39:28 +00:00
|
|
|
local section = apply_transitional_separators(previous_section,
|
|
|
|
current_section, next_section)
|
|
|
|
|
|
|
|
table.insert(status, section)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
-- incase none of x,y,z was configured lets not fill whole statusline with a,b,c section
|
|
|
|
if not half_passed then
|
2021-03-15 23:37:46 +00:00
|
|
|
table.insert(status,
|
|
|
|
highlight.format_highlight(is_focused, 'lualine_c') .. '%=')
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-03 19:11:22 +00:00
|
|
|
return 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-02-05 12:29:34 +00:00
|
|
|
local function status_dispatch()
|
2021-05-02 17:16:03 +00:00
|
|
|
-- disable on specific filetypes
|
2021-08-08 16:03:58 +00:00
|
|
|
local current_ft = vim.bo.filetype
|
|
|
|
local is_focused = utils.is_focused()
|
2021-05-02 17:16:03 +00:00
|
|
|
for _, ft in pairs(config.options.disabled_filetypes) do
|
|
|
|
if ft == current_ft then
|
|
|
|
vim.wo.statusline = ''
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
2021-08-03 06:08:17 +00:00
|
|
|
local extension_sections = get_extension_sections(current_ft, is_focused)
|
|
|
|
if is_focused then
|
2021-05-11 19:40:54 +00:00
|
|
|
if extension_sections ~= nil then
|
2021-08-03 06:08:17 +00:00
|
|
|
return statusline(extension_sections, is_focused)
|
2021-05-11 19:40:54 +00:00
|
|
|
end
|
2021-08-03 06:08:17 +00:00
|
|
|
return statusline(config.sections, is_focused)
|
2021-02-05 12:29:34 +00:00
|
|
|
else
|
2021-05-11 19:40:54 +00:00
|
|
|
if extension_sections ~= nil then
|
2021-08-03 06:08:17 +00:00
|
|
|
return statusline(extension_sections, is_focused)
|
2021-03-17 00:02:13 +00:00
|
|
|
end
|
2021-08-03 06:08:17 +00:00
|
|
|
return statusline(config.inactive_sections, is_focused)
|
2021-02-05 12:29:34 +00:00
|
|
|
end
|
2021-01-05 23:43:36 +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]
|
|
|
|
utils_notices.add_notice(string.format([[
|
|
|
|
### 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.
|
|
|
|
]]
|
|
|
|
utils_notices.add_notice(string.format(message_template, theme_name))
|
|
|
|
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)
|
|
|
|
local ok, theme = pcall(loader.load_theme, theme_name)
|
|
|
|
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)
|
|
|
|
local ok, theme = pcall(loader.load_theme, 'auto')
|
|
|
|
if ok and theme then return theme end
|
|
|
|
end
|
|
|
|
notify_theme_error('auto')
|
|
|
|
return loader.load_theme('gruvbox')
|
2021-08-02 15:16:17 +00:00
|
|
|
end
|
|
|
|
local theme = get_theme_from_config()
|
|
|
|
highlight.create_highlight_groups(theme)
|
|
|
|
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-08 16:03:58 +00:00
|
|
|
vim.go.statusline = "%{%v:lua.require'lualine'.statusline()%}"
|
|
|
|
vim.cmd([[
|
2021-08-02 15:16:17 +00:00
|
|
|
autocmd lualine VimResized * redrawstatus
|
2021-08-08 16:03:58 +00:00
|
|
|
]])
|
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-03-25 11:03:43 +00:00
|
|
|
local function setup(user_config)
|
2021-08-09 07:53:42 +00:00
|
|
|
utils_notices.clear_notices()
|
2021-08-08 17:44:03 +00:00
|
|
|
config = config_module.apply_configuration(user_config)
|
2021-08-02 15:16:17 +00:00
|
|
|
setup_augroup()
|
2021-03-06 15:03:00 +00:00
|
|
|
setup_theme()
|
2021-05-09 21:11:18 +00:00
|
|
|
loader.load_all(config)
|
2021-03-06 15:03:00 +00:00
|
|
|
set_statusline()
|
|
|
|
set_tabline()
|
2021-08-09 07:53:42 +00:00
|
|
|
utils_notices.notice_message_startup()
|
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,
|
|
|
|
get_config = config_module.get_config,
|
|
|
|
}
|