1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)")
|