From 6fc7f49449cb237e1aa37eb1025461fe3ea39589 Mon Sep 17 00:00:00 2001 From: seraphim <18ycm9tx@anonaddy.me> Date: Thu, 27 Aug 2026 11:50:43 +0700 Subject: . --- .config/vis/plugins/vis-autoclose/init.lua | 82 ++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .config/vis/plugins/vis-autoclose/init.lua (limited to '.config/vis/plugins/vis-autoclose/init.lua') diff --git a/.config/vis/plugins/vis-autoclose/init.lua b/.config/vis/plugins/vis-autoclose/init.lua new file mode 100644 index 0000000..967d0dd --- /dev/null +++ b/.config/vis/plugins/vis-autoclose/init.lua @@ -0,0 +1,82 @@ +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('') + return true + end + + local result = brackets[key] + if result ~= nil and is_bracket_or_empty(next) then + vis:insert(result) + vis:feedkeys('') + return true + end + return false +end) + +vis:map(vis.modes.INSERT, "", function(keys) + local cursor = vis.win.selection.pos + local file = vis.win.file + + local start = math.max(0, cursor - 1) + local prev = file:content(start, 2) + if cursor == 0 then + return 1 + end + --local prev = file:content(cursor-1, 2) + + -- Workaround until https://github.com/martanne/vis/issues/739 is solved + vis:feedkeys("") + -- Check if whole bracket pair is to be removed + if prev ~= nil then + local result = remove[prev] + if result ~= nil then + vis:feedkeys("") + end + end + + vis:feedkeys("") + return 1 +end, "Removes the previous character (and closed brackets, if empty)") -- cgit v1.2.3