aboutsummaryrefslogtreecommitdiff
path: root/util.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2026-05-13 20:33:28 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2026-05-14 14:25:27 +0200
commit49c26dd03681de5e194fc8ebcabe0c7c5d8960ad (patch)
treedd59113d494bf0eb67dd173774aeddacc7b29029 /util.lua
parent5f3c85ab58015e35c9ecb1b8459878f3b7e89178 (diff)
downloadvis-lspc-49c26dd03681de5e194fc8ebcabe0c7c5d8960ad.tar.gz
vis-lspc-49c26dd03681de5e194fc8ebcabe0c7c5d8960ad.zip
support vis' new option get-callback
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua46
1 files changed, 44 insertions, 2 deletions
diff --git a/util.lua b/util.lua
index 265c272..ddc7452 100644
--- a/util.lua
+++ b/util.lua
@@ -1,9 +1,9 @@
---- Module containing simple utility functions for vis-lspc/
+--- Module containing simple utility functions for vis-lspc
-- @module util
-- @author Florian Fischer
-- @author git-bruh <prathamIN@proton.me>
-- @license GPL-3
--- @copyright 2024-2025 Florian Fischer
+-- @copyright 2024-2026 Florian Fischer
-- @copyright 2024 git-bruh <prathamIN@proton.me>
local util = {}
@@ -48,6 +48,48 @@ function util.vis_pipe(input, cmd, fullscreen)
return vis:pipe(vis.win.file, {start = 0, finish = 0}, cmd)
end
+--- Convert a vis option name to the variable name in the lspc table.
+-- @param vis_option_name The name of the option known to vis
+-- @return The option name with the 'lspc-' suffix removed and '-' replaced by '_'
+function util.vis_option_name_to_lspc(vis_option_name)
+ return vis_option_name:sub(6):gsub('-', '_')
+end
+
+--- Register an lspc option with vis.
+-- @param name Name of the option
+-- @param type Type of the option
+-- @param help String describing the option
+-- @param[opt] set_func Function called when the option is set
+-- @param[opt] get_func Function called to retrieve the option
+function util.register_lspc_option(option, type, help, set_func, get_func)
+ if not set_func then
+ -- TODO: support toggable options
+ set_func = function(value, _, name)
+ lspc[util.vis_option_name_to_lspc(name)] = value
+ end
+ end
+ if not get_func then
+ get_func = function(name)
+ return lspc[util.vis_option_name_to_lspc(name)]
+ end
+ end
+ util.option_register('lspc-' .. option, type, set_func, get_func, help)
+end
+
+--- Safe wrapper for vis:option_register.
+-- If vis does not support the get-callback during option registration
+-- it is dropped.
+-- @param name Name of the option
+-- @param type Type of the option
+-- @param set_func Function called when the option is set
+-- @param get_func Function called to retrieve the option
+-- @param help String describing the option
+function util.option_register(name, type, set_func, get_func, help)
+ if not pcall(vis.option_register, vis, name, type, set_func, get_func, help) then
+ vis:option_register(name, type, set_func, help)
+ end
+end
+
--- Split a path into its components
-- @param path the path to split into components
-- @return a table containing the path components