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
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
local lualine_require = require 'lualine_require'
|
2021-08-30 11:54:23 +00:00
|
|
|
local require = lualine_require.require
|
2021-09-03 18:28:20 +00:00
|
|
|
local modules = lualine_require.lazy_require {
|
2021-08-30 12:22:02 +00:00
|
|
|
notice = 'lualine.utils.notices',
|
|
|
|
}
|
2021-08-30 11:54:23 +00:00
|
|
|
local is_valid_filename = lualine_require.is_valid_filename
|
2021-08-14 14:22:52 +00:00
|
|
|
|
2021-08-30 17:44:28 +00:00
|
|
|
local sep = lualine_require.sep
|
2021-08-14 14:22:52 +00:00
|
|
|
|
2021-08-20 12:32:56 +00:00
|
|
|
local component_types = {
|
2021-10-11 07:47:37 +00:00
|
|
|
lua_fun = function(component)
|
2021-09-25 17:15:42 +00:00
|
|
|
return require 'lualine.components.special.function_component'(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
|
|
|
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
|
|
|
|
loaded_component = require 'lualine.components.special.function_component'(component)
|
|
|
|
end
|
2021-08-20 12:32:56 +00:00
|
|
|
return loaded_component
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
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
|
2021-09-25 17:15:42 +00:00
|
|
|
return require 'lualine.components.special.function_component'(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
|
|
|
var = function(component)
|
2021-09-25 17:15:42 +00:00
|
|
|
return require 'lualine.components.special.vim_var_component'(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end,
|
|
|
|
['_'] = function(component)
|
2021-09-25 17:15:42 +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
|
|
|
}
|
|
|
|
|
|
|
|
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-08-20 12:32:56 +00:00
|
|
|
end
|
|
|
|
if type(component[1]) == 'string' then
|
|
|
|
-- 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)
|
2021-09-03 18:28:20 +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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-14 15:14:23 +00:00
|
|
|
local function option_deprecatation_notice(component)
|
|
|
|
local types = {
|
|
|
|
case = function()
|
|
|
|
local kind = component.upper ~= nil and 'upper' or 'lower'
|
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
|
|
|
### option.%s
|
|
|
|
|
|
|
|
Option `%s` has been deprecated.
|
|
|
|
Please use `fmt` option if you need to change case of a component.
|
|
|
|
|
|
|
|
You have some thing like this in your config:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
%s = true,
|
|
|
|
```
|
|
|
|
|
|
|
|
You'll have to change it to this to retain old behavior:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
fmt = string.%s
|
|
|
|
```
|
|
|
|
]],
|
|
|
|
kind,
|
|
|
|
kind,
|
|
|
|
kind,
|
|
|
|
kind
|
|
|
|
))
|
|
|
|
end,
|
|
|
|
padding = function()
|
|
|
|
local kind = component.left_padding ~= nil and 'left_padding' or 'right_padding'
|
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
|
|
|
### option.%s
|
|
|
|
|
|
|
|
Option `%s` has been deprecated.
|
|
|
|
Please use `padding` option to set left/right padding.
|
|
|
|
|
|
|
|
You have some thing like this in your config:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
%s = %d,
|
|
|
|
```
|
|
|
|
|
|
|
|
You'll have to change it to this to retain old behavior:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
padding = { %s = %d }
|
|
|
|
```
|
|
|
|
if you've set both left_padding and right_padding for a component
|
|
|
|
you'll need to have something like
|
|
|
|
```lua
|
|
|
|
padding = { left = x, right = y }
|
|
|
|
```
|
|
|
|
When you set `padding = x` it's same as `padding = {left = x, right = x}`
|
|
|
|
]],
|
|
|
|
kind,
|
|
|
|
kind,
|
|
|
|
kind,
|
|
|
|
component[kind],
|
|
|
|
kind == 'left_padding' and 'left' or 'right',
|
|
|
|
component[kind]
|
|
|
|
))
|
|
|
|
if component.left_padding and component.right_padding then
|
|
|
|
component.padding = { left = component.left_padding, right = component.right_padding }
|
|
|
|
component.left_padding = nil
|
|
|
|
component.right_padding = nil
|
|
|
|
elseif component.left_padding then
|
|
|
|
component.padding = { left = component.left_padding, right = 1 }
|
|
|
|
component.left_padding = nil
|
|
|
|
else
|
|
|
|
component.padding = { left = 1, right = component.right_padding }
|
|
|
|
component.right_padding = nil
|
|
|
|
end
|
|
|
|
end,
|
2021-10-11 07:47:37 +00:00
|
|
|
type_name = function()
|
|
|
|
local changed_to = component.type == 'luae' and 'lua_expr' or 'vim_fun'
|
|
|
|
modules.notice.add_notice(string.format(
|
|
|
|
[[
|
|
|
|
### option.type.%s
|
|
|
|
|
|
|
|
type name `%s` has been deprecated.
|
|
|
|
Please use `%s`.
|
|
|
|
|
|
|
|
You have some thing like this in your config config for %s component:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
type = %s,
|
|
|
|
```
|
|
|
|
|
|
|
|
You'll have to change it to this to retain old behavior:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
type = %s
|
|
|
|
```
|
|
|
|
]],
|
|
|
|
component.type,
|
|
|
|
component.type,
|
|
|
|
changed_to,
|
|
|
|
tostring(component[1]),
|
|
|
|
component.type,
|
|
|
|
changed_to
|
|
|
|
))
|
|
|
|
component.type = changed_to
|
|
|
|
end,
|
2021-09-14 15:14:23 +00:00
|
|
|
}
|
|
|
|
if component.upper ~= nil or component.lower ~= nil then
|
|
|
|
types.case()
|
|
|
|
end
|
|
|
|
if component.left_padding ~= nil or component.right_padding ~= nil then
|
|
|
|
types.padding()
|
|
|
|
end
|
2021-10-11 07:47:37 +00:00
|
|
|
if component.type == 'luae' or component.type == 'vimf' then
|
|
|
|
types.type_name()
|
|
|
|
end
|
2021-09-14 15:14:23 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
if type(component) == 'string' or type(component) == 'function' then
|
2021-09-03 18:28:20 +00:00
|
|
|
component = { component }
|
2021-08-02 15:16:17 +00:00
|
|
|
end
|
|
|
|
component.self = {}
|
|
|
|
component.self.section = section_name
|
|
|
|
-- apply default args
|
|
|
|
component = vim.tbl_extend('keep', component, options)
|
2021-09-14 15:14:23 +00:00
|
|
|
option_deprecatation_notice(component)
|
2021-08-02 15:16:17 +00:00
|
|
|
section[index] = component_loader(component)
|
|
|
|
end
|
|
|
|
end
|
2021-05-09 21:11:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
local_extension = vim.deepcopy(local_extension)
|
|
|
|
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-08-13 01:25:17 +00:00
|
|
|
local local_extension = vim.deepcopy(extension)
|
|
|
|
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
|
|
|
|
|
|
|
|
local function load_all(config)
|
|
|
|
load_components(config)
|
|
|
|
load_extensions(config)
|
|
|
|
end
|
|
|
|
|
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
|
2021-09-20 14:35:14 +00:00
|
|
|
local path = table.concat { 'lua/lualine/themes/', theme_name, '.lua' }
|
|
|
|
local files = vim.api.nvim_get_runtime_file(path, true)
|
2021-10-10 18:04:55 +00:00
|
|
|
if #files <= 0 then
|
|
|
|
path = table.concat { 'lua/lualine/themes/', theme_name, '/init.lua' }
|
|
|
|
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
|
|
|
}
|