-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlight.lua
More file actions
360 lines (297 loc) · 12.2 KB
/
Highlight.lua
File metadata and controls
360 lines (297 loc) · 12.2 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
local micro = import("micro")
local buffer = import("micro/buffer")
local util = import("micro/util")
local config = import("micro/config")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/?.lua', package.path, config.ConfigDir)
local Self = {}
local OmniBufStoredPanes = {}
local OmniBufLastFindMsg = {}
local OmniBufFoundOccurrence = {}
local OmniBufInverseFoundOccurrence = {}
local function OnTypingHighlight(msg)
if micro.CurPane() == nil or micro.CurPane().Buf == nil then return end
local bp = micro.CurPane()
bp.Buf.LastSearch = msg
bp.Buf.LastSearchRegex = true
bp.Buf.HighlightSearch = true
end
local function OnSubmitHighlightFind(msg, cancelled)
if micro.CurPane() == nil or micro.CurPane().Buf == nil or msg == nil or msg == "" then return end
local bp = micro.CurPane()
if cancelled then
bp.Buf.HighlightSearch = false
return
end
bp.Buf.LastSearch = msg
bp.Buf.LastSearchRegex = true
bp.Buf.HighlightSearch = true
local startFindLoc = buffer.Loc(0, 0)
local lastLineIndex = bp.Buf:LinesNum()
if lastLineIndex > 0 then
lastLineIndex = lastLineIndex - 1
else
micro.InfoBar():Message("None found and highlighted")
return
end
local lastLineLength = util.CharacterCountInString(bp.Buf:Line(lastLineIndex))
if lastLineLength ~= 0 then
lastLineLength = lastLineLength - 1
end
local endFindLoc = buffer.Loc(lastLineLength, lastLineIndex)
local foundCounter = 0
local currentLoc = buffer.Loc(startFindLoc.X, startFindLoc.Y)
local firstOccurrenceLoc = buffer.Loc(-1, -1)
local bpIndex = -1
for i, val in ipairs(OmniBufStoredPanes) do
if val == bp then
bpIndex = i
break
end
end
if bpIndex == -1 then
table.insert(OmniBufStoredPanes, bp)
table.insert(OmniBufLastFindMsg, msg)
table.insert(OmniBufFoundOccurrence, {})
table.insert(OmniBufInverseFoundOccurrence, {})
bpIndex = #OmniBufStoredPanes
else
OmniBufLastFindMsg[bpIndex] = msg
OmniBufFoundOccurrence[bpIndex] = {}
OmniBufInverseFoundOccurrence[bpIndex] = {}
end
while true do
local foundLocs, found, err = bp.Buf:FindNext( msg,
startFindLoc,
endFindLoc,
currentLoc,
true,
true)
if found == false or
err ~= nil or
foundCounter >= 1500 or
(foundLocs[2].X == firstOccurrenceLoc.X and foundLocs[2].Y == firstOccurrenceLoc.Y) then
break
end
currentLoc = buffer.Loc(foundLocs[2].X, foundLocs[2].Y)
local currentLocTable = {foundLocs[2].X, foundLocs[2].Y}
table.insert(OmniBufFoundOccurrence[bpIndex], currentLocTable)
if OmniBufInverseFoundOccurrence[bpIndex][currentLocTable[1]] == nil then
OmniBufInverseFoundOccurrence[bpIndex][currentLocTable[1]] = {}
end
OmniBufInverseFoundOccurrence[bpIndex][currentLocTable[1]][currentLocTable[2]] =
#OmniBufFoundOccurrence[bpIndex]
foundCounter = foundCounter + 1
if foundCounter == 1 then
firstOccurrenceLoc = buffer.Loc(foundLocs[2].X, foundLocs[2].Y)
end
end
micro.InfoBar():Message(foundCounter, " found and highlighted. Do FindNext/FindPrevious to "..
"go to the occurrences")
end
function Self.OmniHighlightOnly(bp)
local selectionText = ""
if bp.Cursor:HasSelection() then
selectionText = bp.Cursor:GetSelection()
selectionText = util.String(selectionText)
end
micro.InfoBar():Prompt( "Highlight Then Find (regex) > ",
selectionText, "", OnTypingHighlight, OnSubmitHighlightFind)
end
local function PerformMultiCursor(bp, forceMove)
-- Check highlight
if not bp.Buf.HighlightSearch or bp.Buf.LastSearch == "" then
bp:SpawnMultiCursor()
return
end
local lastCursor = bp.Buf:GetCursor(bp.Buf:NumCursors() - 1)
local searchStart = nil
local lastCursorBegin = nil
local cursorEnd = true
local selectionReversed = false
if lastCursor:HasSelection() then
-- Reorder selection LOC
local sels = {}
if lastCursor.CurSelection[1]:LessThan(buffer.Loc( lastCursor.OrigSelection[1].X,
lastCursor.OrigSelection[1].Y)) then
selectionReversed = true
cursorEnd = false
else
selectionReversed = false
cursorEnd = true
end
table.insert(sels, buffer.Loc(lastCursor.CurSelection[1].X, lastCursor.CurSelection[1].Y))
table.insert(sels, buffer.Loc(lastCursor.CurSelection[2].X, lastCursor.CurSelection[2].Y))
searchStart = buffer.Loc(sels[2].X, sels[2].Y)
lastCursorBegin = buffer.Loc(sels[1].X, sels[1].Y)
else
cursorEnd = false
searchStart = buffer.Loc(lastCursor.Loc.X + 1, lastCursor.Loc.Y)
lastCursorBegin = buffer.Loc(lastCursor.Loc.X, lastCursor.Loc.Y)
end
local differentFound = false
local nextFoundLocs = nil
local cursorOnFound = false
local i = 1
while i < 2 do
local foundLocs, found, err = bp.Buf:FindNext( bp.Buf.LastSearch,
bp.Buf:Start(),
bp.Buf:End(),
searchStart,
true,
true)
if found == false or err ~= nil then
micro.InfoBar():Message("No occurrences found")
return
end
-- We found an unique occurrence, Check if the cursor is on an occurrence
if foundLocs[1].X ~= lastCursorBegin.X or foundLocs[1].Y ~= lastCursorBegin.Y then
differentFound = true
nextFoundLocs = { buffer.Loc(foundLocs[1].X, foundLocs[1].Y),
buffer.Loc(foundLocs[2].X, foundLocs[2].Y)}
foundLocs, found, err = bp.Buf:FindNext(bp.Buf.LastSearch,
bp.Buf:Start(),
bp.Buf:End(),
nextFoundLocs[1],
false,
true)
-- What?
if found == false or err ~= nil then
break
end
if lastCursorBegin.X == foundLocs[1].X and lastCursorBegin.Y == foundLocs[1].Y then
cursorOnFound = true
cursorEnd = false
elseif lastCursorBegin.X == foundLocs[2].X and lastCursorBegin.Y == foundLocs[2].Y then
cursorOnFound = true
cursorEnd = true
end
break
end
i = i + 1
end
if not differentFound then
return
end
local moveCursor = (not cursorOnFound) or forceMove
-- Spawn new cursor if we don't move the last cursor
if not moveCursor then
if bp.Buf.Settings["MicroOmni.CanUseAddCursor"] then
lastCursor = bp:SpawnCursorAtLoc(buffer.Loc(0, 0))
else
-- Save and restore original cursor if we can't use `AddCursor()`, since
-- `SpawnMultiCursorDown()` moves the cursor that has selection to the beginning
local origHasSelection = lastCursor:HasSelection()
local origLocX = 0
local origLocY = 0
if origHasSelection then
if not selectionReversed then
origLocX = lastCursor.CurSelection[2].X
origLocY = lastCursor.CurSelection[2].Y
cursorEnd = true
else
origLocX = lastCursor.CurSelection[1].X
origLocY = lastCursor.CurSelection[1].Y
cursorEnd = false
end
end
if not bp:SpawnMultiCursorDown() then
if not bp:SpawnMultiCursorUp() then
micro.InfoBar():Error("Failed to spawn cursor...")
return
end
end
if origHasSelection then
lastCursor.Loc.X = origLocX
lastCursor.Loc.Y = origLocY
end
lastCursor = bp.Buf:GetCursor(bp.Buf:NumCursors() - 1)
end
end
if bp.Buf.Settings["MicroOmni.CanUseAddCursor"] then
if selectionReversed then
lastCursor.OrigSelection[1].X = nextFoundLocs[2].X
lastCursor.OrigSelection[1].Y = nextFoundLocs[2].Y
lastCursor:SelectTo(nextFoundLocs[1])
lastCursor.Loc.X = nextFoundLocs[1].X
lastCursor.Loc.Y = nextFoundLocs[1].Y
else
lastCursor.OrigSelection[1].X = nextFoundLocs[1].X
lastCursor.OrigSelection[1].Y = nextFoundLocs[1].Y
lastCursor:SelectTo(nextFoundLocs[2])
lastCursor.Loc.X = nextFoundLocs[2].X
lastCursor.Loc.Y = nextFoundLocs[2].Y
end
if not moveCursor then
bp.Buf:AddCursor(lastCursor)
end
-- Can't do selection because SpawnMultiCursorDown() deselects all the cursors.
-- Probably could have saved the cursor selections and then restore them, but effort..
else
if cursorEnd then
lastCursor.Loc.X = nextFoundLocs[2].X
lastCursor.Loc.Y = nextFoundLocs[2].Y
else
lastCursor.Loc.X = nextFoundLocs[1].X
lastCursor.Loc.Y = nextFoundLocs[1].Y
end
end
-- micro.Log("lastCursor.Loc:", lastCursor.Loc)
-- micro.Log("")
bp.Buf:SetCurCursor(bp.Buf:NumCursors() - 1)
bp.Buf:MergeCursors()
bp:Relocate()
end
local function ShowFoundIndex()
if micro.CurPane() == nil or micro.CurPane().Buf == nil then return end
local bp = micro.CurPane()
local bpIndex = -1
for i, val in ipairs(OmniBufStoredPanes) do
if val == bp then
bpIndex = i
break
end
end
if bpIndex == -1 then
return
end
if not bp.Buf.LastSearchRegex or
not bp.Buf.HighlightSearch or
OmniBufLastFindMsg[bpIndex] ~= bp.Buf.LastSearch then
return
end
-- If we are not in any of the found entry, get out
if OmniBufInverseFoundOccurrence[bpIndex][bp.Cursor.Loc.X] == nil or
OmniBufInverseFoundOccurrence[bpIndex][bp.Cursor.Loc.X][bp.Cursor.Loc.Y] == nil then
-- local currentLocTable = {bp.Cursor.Loc.X, bp.Cursor.Loc.Y}
-- micro.Log("currentLocTable:", currentLocTable)
--
-- for i, val in ipairs(OmniBufFoundOccurrence[bpIndex]) do
-- micro.Log("OmniBufFoundOccurrence[bpIndex][", i, "]:", val)
-- end
--
-- for k, v in pairs(OmniBufInverseFoundOccurrence[bpIndex]) do
-- micro.Log("OmniBufInverseFoundOccurrence[bpIndex] key:", k)
-- micro.Log("OmniBufInverseFoundOccurrence[bpIndex] val:", v)
-- end
return
end
local outputMsg =
"At occurrences " ..
tostring(OmniBufInverseFoundOccurrence[bpIndex][bp.Cursor.Loc.X][bp.Cursor.Loc.Y]) ..
"/" .. tostring(#OmniBufFoundOccurrence[bpIndex])
micro.InfoBar():Message(outputMsg)
end
function Self.OmniSpawnCursorNextHighlight(bp)
PerformMultiCursor(bp, false)
end
function Self.OmniMoveLastCursorNextHighlight(bp)
PerformMultiCursor(bp, true)
end
function Self.OmniOnNextFind()
ShowFoundIndex()
end
function Self.OmniOnPrevFind()
ShowFoundIndex()
end
return Self