-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmledit.lua
47 lines (43 loc) · 1.23 KB
/
htmledit.lua
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
require "vis"
vis.events.subscribe(vis.events.INPUT, function(key)
win = vis.win
file = vis.win.file
if win.syntax ~= "html" then return false end --only work on html files
-- look for the beginning of an opening html tag '<'
if key == "<" then -- take down its position
startpos = win.selection.pos
end
-- if it's a closing tag
if startpos and key == "/" then
startpos = nil
-- if tag is closed
elseif key == ">" and startpos then
-- define the tags
local pos = win.selection.pos
local content = vis.win.file:content(startpos, pos - startpos)
local opentag = content..">"
local closetag = string.gsub(opentag, "<", "</")
-- insert the closing tag
file:insert(pos, closetag)
win.selection.pos = pos + #closetag
assert(win.selection.pos == pos + #closetag)
-- go back to before the tag
endpos = win.selection.pos
win.selection.pos = endpos - #closetag
startpos = nil
elseif endpos then
if key == ">" then
vis:feedkeys("<Enter>")
local pos = win.selection.pos
vis:feedkeys("<Enter>")
win.selection.pos = pos
vis:feedkeys("<Tab>")
endpos = nil
return true
else endpos = nil
end
end
end)
vis:map(vis.modes.INSERT, "<F1>", function()
vis:info("current position: "..vis.win.selection.pos)
end)