diff --git a/README.md b/README.md index 7674f2e..a8a2b76 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,8 @@ local opts = { require('deadcolumn').setup(opts) -- Call the setup function ``` -- `scope` (string): The scope for showing the colored column, there are several - possible values: +- `scope` (string|function): The scope for showing the colored column, there + are several possible values: - `'line'`: colored column will be shown based on the length of the current line. @@ -127,6 +127,24 @@ require('deadcolumn').setup(opts) -- Call the setup function - `'cursor'`: colored column will be shown based on current cursor position. + - function() -> number: callback function that returns a number as the + length of the row. For example, to show the colored column based on the + longest line in the nearby 100 lines: + + ```lua + require('deadcolumn').setup({ + scope = function() + local max = 0 + for i = -50, 50 do + local len = vim.fn.strdisplaywidth(vim.fn.getline(vim.fn.line('.') + i)) + if len > max then + max = len + end + return max + end + }) + ``` + - `modes` (table): In which modes to show the colored column. - `blending` (table): Blending options. diff --git a/doc/deadcolumn.nvim.txt b/doc/deadcolumn.nvim.txt index c3370b3..e0b831a 100644 --- a/doc/deadcolumn.nvim.txt +++ b/doc/deadcolumn.nvim.txt @@ -74,7 +74,7 @@ deadcolumn.setup({*opts}) *deadcolumn.options.setup()* Valid keys for {opts} *deadcolumn.options.opts.scope* - `scope` string (default "line") + `scope` string|function (default "line") The scope for showing the colored column. @@ -99,6 +99,26 @@ deadcolumn.setup({*opts}) *deadcolumn.options.setup()* Colored column will be shown based on current cursor position. + 5. function() -> number + + Callback function that returns a number as the length of the + row. For example, to show the colored column based on the longest + line in the nearby 100 lines: >lua + + require('deadcolumn').setup({ + scope = function() + local max = 0 + for i = -50, 50 do + local len = vim.fn.strdisplaywidth(vim.fn.getline(vim.fn.line('.') + i)) + if len > max then + max = len + end + end + return max + end + }) +< + *deadcolumn.options.ops.modes* `modes` table (default: { "i", "ic", "ix", "R", "Rc", "Rx", "Rv", "Rvc", "Rvx" }) diff --git a/lua/deadcolumn/autocmds.lua b/lua/deadcolumn/autocmds.lua index f7e7931..ef806a4 100644 --- a/lua/deadcolumn/autocmds.lua +++ b/lua/deadcolumn/autocmds.lua @@ -97,7 +97,9 @@ local function redraw_cc() return end - local len = scope_len_fn[configs.opts.scope]() + local len = type(configs.opts.scope) == 'string' + and scope_len_fn[configs.opts.scope]() + or configs.opts.scope() local thresh = configs.opts.blending.threshold if 0 < thresh and thresh <= 1 then thresh = math.floor(thresh * cc) diff --git a/lua/deadcolumn/configs.lua b/lua/deadcolumn/configs.lua index 26b970b..c5e0f89 100644 --- a/lua/deadcolumn/configs.lua +++ b/lua/deadcolumn/configs.lua @@ -33,8 +33,12 @@ function M.set_options(user_opts) end assert(vim.tbl_islist(M.opts.modes), 'modes must be a list of strings') assert( - vim.tbl_contains({ 'line', 'buffer', 'visible', 'cursor' }, M.opts.scope), - 'scope must be one of "line", "buffer", "visible", "cursor"' + type(M.opts.scope) == 'function' + or vim.tbl_contains( + { 'line', 'buffer', 'visible', 'cursor' }, + M.opts.scope + ), + 'scope must be a function or one of "line", "buffer", "visible", "cursor"' ) assert(M.opts.blending.threshold >= 0, 'blending.threshold must be >= 0') assert(