-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvi_mode_search.lua
210 lines (186 loc) · 6.24 KB
/
vi_mode_search.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- Handle the vim search emulation
-- Modeled on textadept's command_entry.lua
local M = {}
M.search_hl_indic = _SCINTILLA.next_indic_number()
local function set_colours()
buffer.indic_fore[M.search_hl_indic] = 0xFF0000
buffer.indic_style[M.search_hl_indic] = _SCINTILLA.constants.INDIC_ROUNDBOX
buffer.indic_alpha[M.search_hl_indic] = 100
-- Find all occurrences to highlight.
buffer.indicator_current = M.search_hl_indic
buffer:indicator_clear_range(0, buffer.length)
end
M.state = {
in_search_mode = false,
backwards = false,
pattern = "",
}
local state = M.state
local function do_search(backwards)
if state.pattern == "" then return end
ui.statusbar_text = "Search: "..state.pattern
local saved_pos = buffer.current_pos
buffer:search_anchor()
local search_flags = _SCINTILLA.constants.FIND_REGEXP
local searcher = function(...) return buffer:search_next(...) end
-- Search from the end. We'll jump to the first one "after" the current pos.
buffer.current_pos = 0
buffer:search_anchor()
pos = searcher(search_flags, state.pattern)
set_colours()
if pos >= 0 then
local saved_flags = buffer.search_flags
buffer.search_flags = search_flags
local new_pos = nil
-- Need to use search_in_target to find the actual search extents.
buffer.target_start = 0
buffer.target_end = buffer.length
local occurences = 0
local first_pos = nil
local last_pos = nil
while buffer:search_in_target(state.pattern) >= 0 do
local match_len = buffer.target_end - buffer.target_start
last_pos = buffer.target_start
if first_pos == nil then
first_pos = buffer.target_start
end
-- Work out the current pos, ie first hit after the saved position.
if backwards then
if buffer.target_start < saved_pos then
new_pos = buffer.target_start
end
else
-- Forwards - take the first one after saved_pos
if new_pos == nil and buffer.target_start > saved_pos then
new_pos = buffer.target_start
end
end
buffer:indicator_fill_range(buffer.target_start, match_len)
if buffer.target_end == buffer.target_start then
-- Zero length match - not useful, abort here.
buffer.current_pos = saved_pos
ui.statusbar_text = "Not found"
return
end
-- Ensure we make some progress
if buffer.target_end == buffer.target_start then
buffer.target_start = buffer.target_end + 1
else
buffer.target_start = buffer.target_end
end
buffer.target_end = buffer.length
if buffer.target_start >= buffer.length then
break
end
occurences = occurences + 1
end
-- Handle wrapping search
if new_pos == nil then
if backwards then
new_pos = last_pos
else
new_pos = first_pos
end
ui.statusbar_text = "WRAPPED SEARCH"
else
ui.statusbar_text = "Found " .. tostring(occurences)
end
-- Restore global search flags
buffer.search_flags = saved_flags
buffer:ensure_visible(buffer:line_from_position(new_pos))
buffer:goto_pos(new_pos)
buffer.selection_start = new_pos
else
buffer.current_pos = saved_pos
vi_mode.err("Not found")
end
end
local function handle_search_command(command)
if state.in_search_mode then
state.pattern = command
do_search(state.backwards)
state.in_search_mode = false
return false -- make sure this isn't handled again
end
end
-- Register our key bindings for the command entry
local ui_ce = ui.command_entry
local function finish_search(text)
local exitfunc = state.exitfunc
state.exitfunc = nil
if string.len(text) == 0 then
text = state.pattern
end
exitfunc(function()
state.pattern = text
do_search(state.backwards)
end)
end
keys.vi_search_command = {
['ctrl+v'] = {
['\t'] = function()
return keys.vi_search_command['\t']()
end,
},
['\t'] = function() -- insert the string '\t' instead of tab
-- FIXME: insert at correct position ???
local text = ui.command_entry:get_text()
--ui_ce.enter_mode(nil)
ui.command_entry:set_text(text .. "\\t")
--ui_ce.enter_mode("vi_search_command")
end,
['esc'] = function()
ui_ce.enter_mode(nil) -- Exit command_entry mode
keys.mode = "vi_command"
end,
['\b'] = function()
if ui.command_entry:get_text() == "" then
return keys.vi_search_command['esc']() -- exit
end
return false -- propagate the key
end,
}
local function start_common(exitfunc)
state.in_search_mode = true
state.exitfunc = exitfunc
ui.command_entry.run(finish_search)
end
function M.start(exitfunc)
state.backwards = false
return start_common(exitfunc)
end
function M.start_rev(exitfunc)
state.backwards = true
return start_common(exitfunc)
end
function M.restart()
do_search(state.backwards)
end
function M.restart_rev()
do_search(not state.backwards)
end
local function search_word_common(backwards)
-- Search for the word under the cursor
-- TODO: quote properly, or better don't use regex'
-- Uses ideas from editing.lua
local pos = buffer.current_pos
local s, e = buffer:word_start_position(pos, true), buffer:word_end_position(pos)
local word = buffer:text_range(s, e)
if word == "" then return end
state.pattern = '\\b' .. word .. '\\b'
ui.command_entry.append_history(finish_search, state.pattern)
state.backwards = backwards
if backwards then
-- Avoid hitting the current word again if the cursor isn't at the
-- start.
buffer.current_pos = s
end
do_search(backwards)
end
function M.search_word()
search_word_common(false)
end
function M.search_word_rev()
search_word_common(true)
end
return M