| 1 |
local M = {} |
| 2 |
|
| 3 |
-- keeps backup of options that we cahge so we can restore it. |
| 4 |
-- format: |
| 5 |
-- options { |
| 6 |
-- global = <1> { |
| 7 |
-- name = {prev, set} |
| 8 |
-- }, |
| 9 |
-- buffer = { |
| 10 |
-- buf1 = <1>, |
| 11 |
-- buf2 = <1> |
| 12 |
-- }, |
| 13 |
-- window = { |
| 14 |
-- win1 = <1>, |
| 15 |
-- win2 = <1> |
| 16 |
-- } |
| 17 |
-- } |
| 18 |
---@class LualineNvimOptCacheOptStore |
| 19 |
---@field prev any |
| 20 |
---@field set any |
| 21 |
---@alias LualineNvimOptCacheOpt table<string, LualineNvimOptCacheOptStore> |
| 22 |
---@class LualineNvimOptCache |
| 23 |
---@field global LualineNvimOptCacheOptStore[] |
| 24 |
---@field buffer table<number, LualineNvimOptCacheOptStore[]> |
| 25 |
---@field window table<number, LualineNvimOptCacheOptStore[]> |
| 26 |
---@type LualineNvimOptCache |
| 27 |
local options = { global = {}, buffer = {}, window = {} } |
| 28 |
|
| 29 |
-- helper function for M.set |
| 30 |
local function set_opt(name, val, getter_fn, setter_fn, cache_tbl) |
| 31 |
-- before nvim 0.7 nvim_win_get_option... didn't return default value when |
| 32 |
-- the option wasn't set instead threw error. |
| 33 |
-- So we need pcall (probably just for test) |
| 34 |
local ok, cur = pcall(getter_fn, name) |
| 35 |
if not ok then |
| 36 |
cur = nil |
| 37 |
end |
| 38 |
if cur == val then |
| 39 |
return |
| 40 |
end |
| 41 |
if cache_tbl[name] == nil then |
| 42 |
cache_tbl[name] = {} |
| 43 |
end |
| 44 |
|
| 45 |
if cache_tbl[name].set ~= cur then |
| 46 |
if type(cur) ~= 'string' or not cur:find('lualine') then |
| 47 |
cache_tbl[name].prev = cur |
| 48 |
end |
| 49 |
end |
| 50 |
cache_tbl[name].set = val |
| 51 |
setter_fn(name, val) |
| 52 |
|
| 53 |
-- Mitigation for https://github.com/nvim-lualine/lualine.nvim/issues/1323 |
| 54 |
-- when redrawstatus is ran and winbar usues some eval syntax %{} for some |
| 55 |
-- odd reason nvim renders the winbar in statusline first then actual statusline |
| 56 |
-- replaces it. Also it doesn't happen on top window. Might be some bug in |
| 57 |
-- neovims rendering. Also it doesn't seem to happend with laststatus=2 or |
| 58 |
-- global status turned off in lualine. lualines rendering might be at fault |
| 59 |
-- too. but we should never be evaluating an externally set winbar so that |
| 60 |
-- doen't make much sense either. |
| 61 |
-- Tested: nvim v0.9.5 |
| 62 |
-- TODO: Needs further investigation. |
| 63 |
if vim.api.nvim_get_mode().mode == 'c' then |
| 64 |
if name == 'statusline' or name == 'winbar' then |
| 65 |
vim.cmd('redrawstatus') |
| 66 |
end |
| 67 |
if name == 'tabline' then |
| 68 |
vim.cmd('redrawtabline') |
| 69 |
end |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
-- set a option value |
| 74 |
---@param name string |
| 75 |
---@param val any |
| 76 |
---@param opts table|nil when table can be {global=true | buffer = bufnr | window = winnr} |
| 77 |
--- when nil it's treated as {global = true} |
| 78 |
function M.set(name, val, opts) |
| 79 |
if opts == nil or opts.global then |
| 80 |
set_opt(name, val, vim.api.nvim_get_option, vim.api.nvim_set_option, options.global) |
| 81 |
elseif opts.buffer then |
| 82 |
if options.buffer[opts.buffer] == nil then |
| 83 |
options.buffer[opts.buffer] = {} |
| 84 |
end |
| 85 |
set_opt(name, val, function(nm) |
| 86 |
if not vim.tbl_contains(vim.api.nvim_list_bufs(), opts.buffer) then |
| 87 |
return nil |
| 88 |
end |
| 89 |
return vim.api.nvim_buf_get_option(opts.buffer, nm) |
| 90 |
end, function(nm, vl) |
| 91 |
if not vim.tbl_contains(vim.api.nvim_list_bufs(), opts.buffer) then |
| 92 |
return nil |
| 93 |
end |
| 94 |
vim.api.nvim_buf_set_option(opts.buffer, nm, vl) |
| 95 |
end, options.buffer[opts.buffer]) |
| 96 |
elseif opts.window then |
| 97 |
if options.window[opts.window] == nil then |
| 98 |
options.window[opts.window] = {} |
| 99 |
end |
| 100 |
set_opt(name, val, function(nm) |
| 101 |
if not vim.tbl_contains(vim.api.nvim_list_wins(), opts.window) then |
| 102 |
return nil |
| 103 |
end |
| 104 |
return vim.api.nvim_win_get_option(opts.window, nm) |
| 105 |
end, function(nm, vl) |
| 106 |
if not vim.tbl_contains(vim.api.nvim_list_wins(), opts.window) then |
| 107 |
return nil |
| 108 |
end |
| 109 |
vim.api.nvim_win_set_option(opts.window, nm, vl) |
| 110 |
end, options.window[opts.window]) |
| 111 |
end |
| 112 |
end |
| 113 |
|
| 114 |
-- resoters old value of option name |
| 115 |
---@param name string |
| 116 |
---@param opts table|nil same as M.set |
| 117 |
function M.restore(name, opts) |
| 118 |
if opts == nil or opts.global then |
| 119 |
if options.global[name] ~= nil and options.global[name].prev ~= nil then |
| 120 |
local restore_to = options.global[name].prev |
| 121 |
if type(restore_to) == 'string' and restore_to:find('lualine') then |
| 122 |
restore_to = '' |
| 123 |
end |
| 124 |
vim.api.nvim_set_option(name, restore_to) |
| 125 |
end |
| 126 |
elseif opts.buffer then |
| 127 |
if |
| 128 |
options.buffer[opts.buffer] ~= nil |
| 129 |
and options.buffer[opts.buffer][name] ~= nil |
| 130 |
and options.buffer[opts.buffer][name].prev ~= nil |
| 131 |
then |
| 132 |
local restore_to = options.buffer[opts.buffer][name].prev |
| 133 |
if type(restore_to) == 'string' and restore_to:find('lualine') then |
| 134 |
restore_to = '' |
| 135 |
end |
| 136 |
vim.api.nvim_buf_set_option(opts.buffer, name, restore_to) |
| 137 |
end |
| 138 |
elseif opts.window then |
| 139 |
if |
| 140 |
options.window[opts.window] ~= nil |
| 141 |
and options.window[opts.window][name] ~= nil |
| 142 |
and options.window[opts.window][name].prev ~= nil |
| 143 |
then |
| 144 |
local restore_to = options.window[opts.window][name].prev |
| 145 |
if type(restore_to) == 'string' and restore_to:find('lualine') then |
| 146 |
restore_to = '' |
| 147 |
end |
| 148 |
vim.api.nvim_win_set_option(opts.window, name, restore_to) |
| 149 |
end |
| 150 |
end |
| 151 |
end |
| 152 |
|
| 153 |
-- returns cache for the option name |
| 154 |
---@param name string |
| 155 |
---@param opts table|nil same as M.set |
| 156 |
function M.get_cache(name, opts) |
| 157 |
if opts == nil or opts.global then |
| 158 |
if options.global[name] ~= nil and options.global[name].prev ~= nil then |
| 159 |
return options.global[name].prev |
| 160 |
end |
| 161 |
elseif opts.buffer then |
| 162 |
if |
| 163 |
options.buffer[opts.buffer] ~= nil |
| 164 |
and options.buffer[opts.buffer][name] ~= nil |
| 165 |
and options.buffer[opts.buffer][name].prev ~= nil |
| 166 |
then |
| 167 |
return options.buffer[opts.buffer][name].prev |
| 168 |
end |
| 169 |
elseif opts.window then |
| 170 |
if |
| 171 |
options.window[opts.window] ~= nil |
| 172 |
and options.window[opts.window][name] ~= nil |
| 173 |
and options.window[opts.window][name].prev ~= nil |
| 174 |
then |
| 175 |
return options.window[opts.window][name].prev |
| 176 |
end |
| 177 |
end |
| 178 |
end |
| 179 |
|
| 180 |
-- resets cache for options |
| 181 |
function M.reset_cache() |
| 182 |
options = { global = {}, buffer = {}, window = {} } |
| 183 |
end |
| 184 |
|
| 185 |
return M |