-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.lua
More file actions
153 lines (124 loc) · 2.75 KB
/
search.lua
File metadata and controls
153 lines (124 loc) · 2.75 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Search Commands
local micro = import("micro")
local utf8 = import("unicode/utf8")
local config = import("micro/config")
local plug_path = config.ConfigDir .. "/plug/?.lua"
if not package.path:find(plug_path, 1, true) then
package.path = package.path .. ";" .. plug_path
end
local mode = require("vi/mode")
local context = require("vi/context")
local backward_mode = false
-- /<pattern> Enter - Search <pattern> forward.
local function forward()
context.memorize()
mode.search()
backward_mode = false
micro.CurPane():Find()
end
-- ?<pattern> Enter : Search <pattern> backward.
local function backward()
context.memorize()
mode.search()
backward_mode = true
micro.CurPane():Find()
end
-- internal use
-- (none) : Search next match forward.
local function match_forward(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
context.memorize()
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if cursor.X < length then
cursor.X = cursor.X + 1
end
for _ = 1, num do
pane:FindNext()
end
if cursor:HasSelection() then
local start = cursor.CurSelection[1]
cursor.X = start.X
cursor.Y = start.Y
cursor:ResetSelection()
end
end
-- internal use
-- (none) : Search next match backward.
local function match_backward(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
context.memorize()
local pane = micro.CurPane()
for _ = 1, num do
pane:FindPrevious()
end
local cursor = pane.Buf:GetActiveCursor()
if cursor:HasSelection() then
local start = cursor.CurSelection[1]
cursor.X = start.X
cursor.Y = start.Y
cursor:ResetSelection()
end
end
-- n : Search next match.
local function next_match(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if backward_mode then
match_backward(num)
else
match_forward(num)
end
end
-- N : Search previous match.
local function prev_match(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if backward_mode then
match_forward(num)
else
match_backward(num)
end
end
-- / Enter : Repeat last search forward.
local function repeat_forward(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
backward_mode = false
match_forward(num)
end
-- ? Enter : Repeat last search backward.
local function repeat_backward(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
backward_mode = true
match_backward(num)
end
-------------
-- Exports --
-------------
local M = {}
M.forward = forward
M.backward = backward
M.next_match = next_match
M.prev_match = prev_match
M.repeat_forward = repeat_forward
M.repeat_backward = repeat_backward
return M