diff options
Diffstat (limited to '.config/vis/plugins/vis-autoclose')
| -rw-r--r-- | .config/vis/plugins/vis-autoclose/LICENSE | 11 | ||||
| -rw-r--r-- | .config/vis/plugins/vis-autoclose/README.md | 3 | ||||
| -rw-r--r-- | .config/vis/plugins/vis-autoclose/init.lua | 76 |
3 files changed, 90 insertions, 0 deletions
diff --git a/.config/vis/plugins/vis-autoclose/LICENSE b/.config/vis/plugins/vis-autoclose/LICENSE new file mode 100644 index 0000000..797cc80 --- /dev/null +++ b/.config/vis/plugins/vis-autoclose/LICENSE @@ -0,0 +1,11 @@ +Copyright 2026 luxanna + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/.config/vis/plugins/vis-autoclose/README.md b/.config/vis/plugins/vis-autoclose/README.md new file mode 100644 index 0000000..37a2dde --- /dev/null +++ b/.config/vis/plugins/vis-autoclose/README.md @@ -0,0 +1,3 @@ +# vis-autoclose + +A plugin for [vis](https://git.sr.ht/~martanne/vis) which automatically closes opened brackets and also removes them when they are empty, when removing the opening bracket (using <Backspace>). Inspired by autoclose.vim.
\ No newline at end of file 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)") |
