2021-05-09 21:11:18 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-08-14 14:22:52 +00:00
|
|
|
|
2022-01-02 11:38:39 +00:00
|
|
|
local lualine_require = require('lualine_require')
|
2021-08-30 11:54:23 +00:00
|
|
|
local require = lualine_require.require
|
2022-02-01 08:04:03 +00:00
|
|
|
local modules = lualine_require.lazy_require {
|
2021-11-25 06:57:00 +00:00
|
|
|
utils = 'lualine.utils.utils',
|
2021-08-30 12:22:02 +00:00
|
|
|
notice = 'lualine.utils.notices',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-08-30 11:54:23 +00:00
|
|
|
local is_valid_filename = lualine_require.is_valid_filename
|
2021-08-30 17:44:28 +00:00
|
|
|
local sep = lualine_require.sep
|
2021-08-14 14:22:52 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- function that loads specific type of component
|
2021-08-20 12:32:56 +00:00
|
|
|
local component_types = {
|
2022-05-30 14:25:05 +00:00
|
|
|
-- loads custom component
|
2021-11-25 06:57:00 +00:00
|
|
|
custom = function(component)
|
|
|
|
return component[1](component)
|
|
|
|
end,
|
2021-10-12 14:04:47 +00:00
|
|
|
--- loads lua functions as component
|
2021-10-11 07:47:37 +00:00
|
|
|
lua_fun = function(component)
|
2022-01-02 11:38:39 +00:00
|
|
|
return require('lualine.components.special.function_component')(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
2021-10-12 14:04:47 +00:00
|
|
|
--- loads lua modules as components (ones in /lua/lualine/components/)
|
2021-08-20 12:32:56 +00:00
|
|
|
mod = function(component)
|
2021-09-03 18:28:20 +00:00
|
|
|
local ok, loaded_component = pcall(require, 'lualine.components.' .. component[1])
|
2021-05-09 21:11:18 +00:00
|
|
|
if ok then
|
|
|
|
component.component_name = component[1]
|
2021-10-10 16:43:00 +00:00
|
|
|
if type(loaded_component) == 'table' then
|
|
|
|
loaded_component = loaded_component(component)
|
|
|
|
elseif type(loaded_component) == 'function' then
|
|
|
|
component[1] = loaded_component
|
2022-01-02 11:38:39 +00:00
|
|
|
loaded_component = require('lualine.components.special.function_component')(component)
|
2021-10-10 16:43:00 +00:00
|
|
|
end
|
2021-08-20 12:32:56 +00:00
|
|
|
return loaded_component
|
|
|
|
end
|
|
|
|
end,
|
2021-10-12 14:04:47 +00:00
|
|
|
--- loads builtin statusline patterns as component
|
2021-08-20 12:32:56 +00:00
|
|
|
stl = function(component)
|
|
|
|
local stl_expr = component[1] -- Vim's %p %l statusline elements
|
2021-09-03 18:28:20 +00:00
|
|
|
component[1] = function()
|
|
|
|
return stl_expr
|
|
|
|
end
|
2022-01-02 11:38:39 +00:00
|
|
|
return require('lualine.components.special.function_component')(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
2022-05-30 14:25:05 +00:00
|
|
|
--- loads variables & options (g:,go:,b:,bo:...) as components
|
2021-08-20 12:32:56 +00:00
|
|
|
var = function(component)
|
2022-01-02 11:38:39 +00:00
|
|
|
return require('lualine.components.special.vim_var_component')(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
2021-10-12 14:04:47 +00:00
|
|
|
--- loads vim functions and lua expressions as components
|
2021-08-20 12:32:56 +00:00
|
|
|
['_'] = function(component)
|
2022-01-02 11:38:39 +00:00
|
|
|
return require('lualine.components.special.eval_func_component')(component)
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-08-20 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
---load a component from component config
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param component table component + component options
|
|
|
|
---@return table the loaded & initialized component
|
2021-08-20 12:32:56 +00:00
|
|
|
local function component_loader(component)
|
|
|
|
if type(component[1]) == 'function' then
|
2021-10-11 07:47:37 +00:00
|
|
|
return component_types.lua_fun(component)
|
2021-11-25 06:57:00 +00:00
|
|
|
elseif type(component[1]) == 'string' then
|
2021-08-20 12:32:56 +00:00
|
|
|
-- load the component
|
|
|
|
if component.type ~= nil then
|
2021-10-11 07:47:37 +00:00
|
|
|
if component_types[component.type] and component.type ~= 'lua_fun' then
|
2021-08-20 12:32:56 +00:00
|
|
|
return component_types[component.type](component)
|
2021-10-11 07:47:37 +00:00
|
|
|
elseif component.type == 'vim_fun' or component.type == 'lua_expr' then
|
2021-08-20 12:32:56 +00:00
|
|
|
return component_types['_'](component)
|
|
|
|
else
|
2021-09-03 18:28:20 +00:00
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
2021-08-20 12:32:56 +00:00
|
|
|
### component.type
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
component type '%s' isn't recognised. Check if spelling is correct.]],
|
|
|
|
component.type
|
|
|
|
))
|
2021-08-20 12:32:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local loaded_component = component_types.mod(component)
|
|
|
|
if loaded_component then
|
|
|
|
return loaded_component
|
2021-08-20 02:15:57 +00:00
|
|
|
elseif string.char(component[1]:byte(1)) == '%' then
|
2021-08-20 12:32:56 +00:00
|
|
|
return component_types.stl(component)
|
2022-01-02 11:38:39 +00:00
|
|
|
elseif component[1]:find('[gvtwb]?o?:') == 1 then
|
2021-08-20 12:32:56 +00:00
|
|
|
return component_types.var(component)
|
2021-05-09 21:11:18 +00:00
|
|
|
else
|
2021-08-20 12:32:56 +00:00
|
|
|
return component_types['_'](component)
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
2021-11-25 06:57:00 +00:00
|
|
|
elseif type(component[1]) == 'table' then
|
|
|
|
return component_types.custom(component)
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 10:52:27 +00:00
|
|
|
--- Shows notice about invalid types passed as component
|
2022-03-02 13:37:08 +00:00
|
|
|
--- @param index number the index of component in section table
|
2021-11-11 10:52:27 +00:00
|
|
|
--- @param component table containing component elements
|
|
|
|
--- return bool whether check passed or not
|
|
|
|
local function is_valid_component_type(index, component)
|
|
|
|
if type(component_types) == 'table' and type(index) == 'number' then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
|
|
|
### Unrecognized component
|
|
|
|
Only functions, strings and tables can be used as component.
|
|
|
|
You seem to have a `%s` as component indexed as `%s`.
|
|
|
|
Something like:
|
|
|
|
```lua
|
|
|
|
%s = %s,
|
|
|
|
```
|
|
|
|
|
|
|
|
This commonly occurs when you forget to pass table with option for component.
|
|
|
|
When a component has option that component needs to be a table which holds
|
2022-05-30 14:25:05 +00:00
|
|
|
the component as first element and the options as key value pairs.
|
2021-11-11 10:52:27 +00:00
|
|
|
For example:
|
|
|
|
```lua
|
|
|
|
lualine_c = {
|
|
|
|
{'diagnostics',
|
|
|
|
sources = {'nvim'},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Notice the inner extra {} surrounding the component and it's options.
|
|
|
|
Make sure your config follows this.
|
|
|
|
]],
|
|
|
|
type(component),
|
|
|
|
index,
|
|
|
|
index,
|
|
|
|
vim.inspect(component)
|
|
|
|
))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---loads all the section from a config
|
|
|
|
---@param sections table list of sections
|
|
|
|
---@param options table global options table
|
2021-05-09 21:11:18 +00:00
|
|
|
local function load_sections(sections, options)
|
2021-08-02 15:16:17 +00:00
|
|
|
for section_name, section in pairs(sections) do
|
|
|
|
for index, component in pairs(section) do
|
2021-11-25 06:57:00 +00:00
|
|
|
if (type(component) == 'string' or type(component) == 'function') or modules.utils.is_component(component) then
|
2021-09-03 18:28:20 +00:00
|
|
|
component = { component }
|
2021-08-02 15:16:17 +00:00
|
|
|
end
|
2021-11-11 10:52:27 +00:00
|
|
|
if is_valid_component_type(index, component) then
|
|
|
|
component.self = {}
|
2022-03-02 13:37:08 +00:00
|
|
|
component.self.section = section_name:match('lualine_(.*)')
|
2021-11-11 10:52:27 +00:00
|
|
|
-- apply default args
|
|
|
|
component = vim.tbl_extend('keep', component, options)
|
|
|
|
section[index] = component_loader(component)
|
|
|
|
end
|
2021-08-02 15:16:17 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---loads all the configs (active, inactive, tabline)
|
|
|
|
---@param config table user config
|
2021-05-09 21:11:18 +00:00
|
|
|
local function load_components(config)
|
|
|
|
load_sections(config.sections, config.options)
|
|
|
|
load_sections(config.inactive_sections, config.options)
|
|
|
|
load_sections(config.tabline, config.options)
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---loads all the extensions
|
2022-05-30 14:25:05 +00:00
|
|
|
---@param config table user config
|
2021-05-09 21:11:18 +00:00
|
|
|
local function load_extensions(config)
|
2021-08-14 14:22:52 +00:00
|
|
|
local loaded_extensions = {}
|
|
|
|
for _, extension in pairs(config.extensions) do
|
2021-05-14 18:24:54 +00:00
|
|
|
if type(extension) == 'string' then
|
2021-09-03 18:28:20 +00:00
|
|
|
local ok, local_extension = pcall(require, 'lualine.extensions.' .. extension)
|
2021-08-14 14:22:52 +00:00
|
|
|
if ok then
|
2021-11-25 07:44:15 +00:00
|
|
|
local_extension = modules.utils.deepcopy(local_extension)
|
2021-08-14 14:22:52 +00:00
|
|
|
load_sections(local_extension.sections, config.options)
|
|
|
|
if local_extension.inactive_sections then
|
|
|
|
load_sections(local_extension.inactive_sections, config.options)
|
|
|
|
end
|
|
|
|
if type(local_extension.init) == 'function' then
|
|
|
|
local_extension.init()
|
|
|
|
end
|
|
|
|
table.insert(loaded_extensions, local_extension)
|
|
|
|
else
|
2021-09-03 18:28:20 +00:00
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
2021-08-14 14:22:52 +00:00
|
|
|
### Extensions
|
|
|
|
Extension named `%s` was not found . Check if spelling is correct.
|
2021-09-03 18:28:20 +00:00
|
|
|
]],
|
|
|
|
extension
|
|
|
|
))
|
2021-08-13 01:20:56 +00:00
|
|
|
end
|
2021-05-14 18:24:54 +00:00
|
|
|
elseif type(extension) == 'table' then
|
2021-11-25 07:44:15 +00:00
|
|
|
local local_extension = modules.utils.deepcopy(extension)
|
2021-08-13 01:25:17 +00:00
|
|
|
load_sections(local_extension.sections, config.options)
|
|
|
|
if local_extension.inactive_sections then
|
|
|
|
load_sections(local_extension.inactive_sections, config.options)
|
2021-08-03 06:08:17 +00:00
|
|
|
end
|
2021-08-13 01:20:56 +00:00
|
|
|
if type(local_extension.init) == 'function' then
|
|
|
|
local_extension.init()
|
|
|
|
end
|
2021-08-14 14:22:52 +00:00
|
|
|
table.insert(loaded_extensions, local_extension)
|
2021-05-14 18:24:54 +00:00
|
|
|
end
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
2021-08-14 14:22:52 +00:00
|
|
|
config.extensions = loaded_extensions
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---loads sections and extensions or entire user config
|
|
|
|
---@param config table user config
|
2021-05-09 21:11:18 +00:00
|
|
|
local function load_all(config)
|
2022-07-06 14:37:55 +00:00
|
|
|
require('lualine.component')._reset_component_id()
|
2021-05-09 21:11:18 +00:00
|
|
|
load_components(config)
|
|
|
|
load_extensions(config)
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---loads a theme from lua module
|
2022-05-30 14:25:05 +00:00
|
|
|
---prioritizes external themes (from user config or other plugins) over the bundled ones
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param theme_name string
|
2022-05-30 14:25:05 +00:00
|
|
|
---@return table theme definition from module
|
2021-08-14 06:02:30 +00:00
|
|
|
local function load_theme(theme_name)
|
2021-09-03 18:28:20 +00:00
|
|
|
assert(is_valid_filename(theme_name), 'Invalid filename')
|
2021-08-30 11:54:23 +00:00
|
|
|
local retval
|
2022-02-01 08:04:03 +00:00
|
|
|
local path = table.concat { 'lua/lualine/themes/', theme_name, '.lua' }
|
2021-09-20 14:35:14 +00:00
|
|
|
local files = vim.api.nvim_get_runtime_file(path, true)
|
2021-10-10 18:04:55 +00:00
|
|
|
if #files <= 0 then
|
2022-02-01 08:04:03 +00:00
|
|
|
path = table.concat { 'lua/lualine/themes/', theme_name, '/init.lua' }
|
2021-10-10 18:04:55 +00:00
|
|
|
files = vim.api.nvim_get_runtime_file(path, true)
|
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
local n_files = #files
|
|
|
|
if n_files == 0 then
|
|
|
|
-- No match found
|
2021-09-03 18:28:20 +00:00
|
|
|
error(path .. ' Not found')
|
2021-08-30 11:54:23 +00:00
|
|
|
elseif n_files == 1 then
|
|
|
|
-- when only one is found run that and return it's return value
|
2021-09-03 18:28:20 +00:00
|
|
|
retval = dofile(files[1])
|
2021-08-30 11:54:23 +00:00
|
|
|
else
|
|
|
|
-- More then 1 found . Use the first one that isn't in lualines repo
|
2021-09-03 18:28:20 +00:00
|
|
|
local lualine_repo_pattern = table.concat({ 'lualine.nvim', 'lua', 'lualine' }, sep)
|
2021-08-30 11:54:23 +00:00
|
|
|
local file_found = false
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
if not file:find(lualine_repo_pattern) then
|
|
|
|
retval = dofile(file)
|
|
|
|
file_found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not file_found then
|
|
|
|
-- This shouldn't happen but somehow we have multiple files but they
|
|
|
|
-- apear to be in lualines repo . Just run the first one
|
2021-09-03 18:28:20 +00:00
|
|
|
retval = dofile(files[1])
|
2021-08-30 11:54:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return retval
|
2021-08-14 06:02:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
load_all = load_all,
|
2021-09-03 18:28:20 +00:00
|
|
|
load_theme = load_theme,
|
2021-08-14 06:02:30 +00:00
|
|
|
}
|