-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
83 lines (72 loc) · 1.96 KB
/
init.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
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
77
78
79
80
81
82
83
local pattern = nil
local matches = {}
-- iterater for doing find in pattern until nil
local pattern_iterator = function(content, pattern)
local offset = 1
return function()
local starts, ends = string.find(content, pattern, offset)
if starts == nil then return nil end
offset = ends + 1
return starts, ends
end
end
-- is cursor pos is in one of the matches?
local cursor_on_match = function(win)
local pos = win.selection.pos
for _, range in ipairs(matches) do
if pos >= range.start and pos <= range.finish then
return true
end
end
return false
end
-- check if range is in viewport range
local range_in_viewport = function(viewport, range)
return range.start >= viewport.bytes.start and range.finish <= viewport.bytes.finish
end
-- highlihght current matches
local highlight = function(win)
-- clear matches if cursor is not on a match
if not cursor_on_match(win) then
matches = {}
return
end
-- style matches in viewport
for _, range in ipairs(matches) do
if range_in_viewport(win.viewport, range) then
win:style(win.STYLE_CURSOR, range.start, range.finish)
end
end
end
-- collect matches (ranges) for pattern in file
local collect_matches = function()
local file = vis.win.file
local content = file:content(0, file.size)
matches = {}
for starts, ends in pattern_iterator(content, pattern) do
table.insert(matches, { start = starts - 1, finish = ends - 1 })
end
end
-- highlight matches on WIN_HIGHLIGHT
vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
highlight(win)
end)
-- create search for two chars and collect matches for highlighting
local sneak = function(keys, search_char)
if #keys < 2 then
pattern = nil
return -1
end
vis:feedkeys(search_char .. keys .. '<Enter>')
pattern = keys
collect_matches()
return 2
end
-- sneak forward on 's'
vis:map(vis.modes.NORMAL, 's', function(keys)
return sneak(keys, '/')
end)
-- sneak backwards on 'S'
vis:map(vis.modes.NORMAL, 'S', function(keys)
return sneak(keys, '?')
end)