diff options
| author | dacctal <dacctal@symlinx.net> | 2026-04-27 05:30:46 +0000 |
|---|---|---|
| committer | dacctal <dacctal@symlinx.net> | 2026-04-27 05:30:46 +0000 |
| commit | d33d907f53c50d323eca75c4bfc02ab5b989b30a (patch) | |
| tree | 6888a2074338a723f0fde99b03b09fbab91a265f /.config/vis/plugins/vis-autoclose/init.lua | |
| parent | 89505535c652cd6f31a15df73293c6e90eaa852f (diff) | |
Diffstat (limited to '.config/vis/plugins/vis-autoclose/init.lua')
| -rw-r--r-- | .config/vis/plugins/vis-autoclose/init.lua | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/.config/vis/plugins/vis-autoclose/init.lua b/.config/vis/plugins/vis-autoclose/init.lua new file mode 100644 index 0000000..22b0039 --- /dev/null +++ b/.config/vis/plugins/vis-autoclose/init.lua @@ -0,0 +1,76 @@ +require('vis') + +local brackets = { + ["("] = "()", + ["{"] = "{}", + ["["] = "[]", + ["<"] = "<>", + ["'"] = "''", + ['"'] = '""', + ['`'] = '``', +}; + +local closed = { + [")"] = "", + ["}"] = "", + ["]"] = "", + [">"] = "", + ["'"] = "", + ['"'] = "", + ["`"] = "", +}; + +local remove = { + ["()"] = "", + ["{}"] = "", + ["[]"] = "", + ["<>"] = "", + ["''"] = "", + ['""'] = "", + ['``'] = "", +}; + +function is_bracket_or_empty(char) + return char == ' ' or + char == '\n' or + brackets[char] ~= nil or + closed[char] ~= nil +end + +vis.events.subscribe(vis.events.INPUT, function(key) + local file = vis.win.file + local cursor = vis.win.selection.pos + local next = file:content(cursor, 1) + -- closed bracket already exists, skip + if key == next and closed[next] ~= nil then + vis:feedkeys('<Right>') + return true + end + + local result = brackets[key] + if result ~= nil and is_bracket_or_empty(next) then + vis:insert(result) + vis:feedkeys('<Left>') + return true + end + return false +end) + +vis:map(vis.modes.INSERT, "<Backspace>", function(keys) + local cursor = vis.win.selection.pos + local file = vis.win.file + local prev = file:content(cursor-1, 2) + + -- Workaround until https://github.com/martanne/vis/issues/739 is solved + vis:feedkeys("<Left>") + -- Check if whole bracket pair is to be removed + if prev ~= nil then + local result = remove[prev] + if result ~= nil then + vis:feedkeys("<Delete>") + end + end + + vis:feedkeys("<Delete>") + return 1 +end, "Removes the previous character (and closed brackets, if empty)") |
