-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordJump.lua
More file actions
276 lines (220 loc) · 9.5 KB
/
WordJump.lua
File metadata and controls
276 lines (220 loc) · 9.5 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
local micro = import("micro")
local config = import("micro/config")
local util = import("micro/util")
local buffer = import("micro/buffer")
local utf8 = import("utf8")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/?.lua', package.path, config.ConfigDir)
local Common = require("MicroOmni.Common")
local Self = {}
local OmniOriginalSearchIgnoreCase = false
local OmniOriginalLastSearch = ""
local OmniOriginalLastSearchRegex = true
local OmniOriginalHighlightSearch = true
local OmniJumpWordsRecords = {}
local OmniOriginalWordsRecords = {}
local function AssignJumpWords(majorChars, minorChars, rowIndexStart, rowIndexEnd, currentJumpChar)
micro.Log("MicroOmni.WordJump - rowIndexStart:", rowIndexStart)
micro.Log("MicroOmni.WordJump - rowIndexEnd:", rowIndexEnd)
micro.Log("MicroOmni.WordJump - string.len(majorChars):", string.len(majorChars))
micro.Log("MicroOmni.WordJump - string.len(minorChars):", string.len(minorChars))
local bp = micro.CurPane()
local majorCharIndex = 1
local minorCharIndex = 1
local jumpWordToRc = {}
local rcToOriWord = {}
local jumpWordsSeparators = " \t¬~++<>:{}|&*($%^!\"£)#-=,.;[]'\\/"
local direction = 1
if rowIndexStart > rowIndexEnd then
direction = -1
end
for i = rowIndexStart, rowIndexEnd, direction do
rcToOriWord[i] = {}
local currentLineBytes = bp.Buf:LineBytes(i)
local j = 1
local wordCharCounter = 0
local usedAllChars = false
local charIndex = 0
while j <= #currentLineBytes do
local endRuneCaptureIndex
if j + 4 > #currentLineBytes then
endRuneCaptureIndex = #currentLineBytes
else
endRuneCaptureIndex = j + 4
end
local runeBuffer = {}
for k = j, endRuneCaptureIndex do
table.insert(runeBuffer, currentLineBytes[k])
end
local _, size = utf8.DecodeRune(runeBuffer)
runeBuffer = {}
for k = j, j + size - 1 do
table.insert(runeBuffer, currentLineBytes[k])
end
local runeStr = util.String(runeBuffer)
if jumpWordsSeparators:find(runeStr, 1, true) == nil then
wordCharCounter = wordCharCounter + 1
if wordCharCounter <= 2 and size == 1 then
if wordCharCounter == 2 then
local currentMajorChar = majorChars:sub(majorCharIndex, majorCharIndex)
local currentMinorChar = minorChars:sub(minorCharIndex, minorCharIndex)
rcToOriWord[i][j - 1] = {currentLineBytes[j - 1], currentLineBytes[j]}
-- jumpWordToRc[currentMajorChar..currentMinorChar] = {i, j - 1}
jumpWordToRc[currentMajorChar..currentMinorChar] = {i, charIndex - 1}
if currentJumpChar ~= nil and currentJumpChar ~= "" then
if currentJumpChar == currentMajorChar then
currentLineBytes[j - 1] = string.byte(string.lower(currentMajorChar))
currentLineBytes[j] = string.byte(currentMinorChar)
end
else
currentLineBytes[j - 1] = string.byte(currentMajorChar)
currentLineBytes[j] = string.byte(currentMinorChar)
end
if minorCharIndex >= string.len(minorChars) then
if majorCharIndex >= string.len(majorChars) then
usedAllChars = true
break
else
majorCharIndex = majorCharIndex + 1
minorCharIndex = 1
end
else
minorCharIndex = minorCharIndex + 1
end
end
end
else
wordCharCounter = 0
end
if size <= 0 then
break
end
j = j + size
charIndex = charIndex + 1
end
if usedAllChars then
break
end
end
return rcToOriWord, jumpWordToRc
end
local function AssignJumpWordsToView(msg)
local bp = micro.CurPane()
local view = bp:GetView()
local numberOfLines = bp.Buf:LinesNum()
local viewStart = (view.StartLine.Line < 0 and {0} or {view.StartLine.Line})[1]
local viewEnd = viewStart + view.Height - 2
viewEnd = (viewEnd >= numberOfLines and {numberOfLines - 1} or {viewEnd})[1]
local viewMid = (viewEnd + viewStart) / 2
local leftMajorChars = "FDSA"
local leftMinorChars = "GQWERTZXCVB"
local rightMajorChars = "JKLH"
local rightMinorChars = "YUIOPNM"
local rightOriginalWords, rightJumpWords = AssignJumpWords( rightMajorChars..rightMinorChars,
leftMajorChars..rightMajorChars..
leftMinorChars..rightMinorChars,
viewMid,
viewEnd,
msg)
local leftOriginalWords = {}
local leftJumpWords = {}
if viewMid ~= 0 then
leftOriginalWords, leftJumpWords = AssignJumpWords( leftMajorChars..leftMinorChars,
rightMajorChars..leftMajorChars..
rightMinorChars..leftMinorChars,
viewMid - 1,
viewStart,
msg)
end
OmniOriginalWordsRecords = {}
OmniJumpWordsRecords = {}
for k, v in pairs(leftOriginalWords) do
OmniOriginalWordsRecords[k] = v
end
for k, v in pairs(rightOriginalWords) do
OmniOriginalWordsRecords[k] = v
end
if viewMid ~= 0 then
for k, v in pairs(leftJumpWords) do
OmniJumpWordsRecords[k] = v
end
for k, v in pairs(rightJumpWords) do
OmniJumpWordsRecords[k] = v
end
end
-- if numberOfLines >= 1 then
-- bp.Buf:Insert(buffer.Loc(0, 0), "A")
-- bp.Buf:Remove(buffer.Loc(0, 0), buffer.Loc(1, 0))
-- end
-- bp.Buf:UpdateRules()
end
local function RestoreOriginalWords(rcToOriWord, exclusionRow)
local bp = micro.CurPane()
for row, rowValues in pairs(rcToOriWord) do
if exclusionRow == nil or row ~= exclusionRow then
local currentLineBytes = bp.Buf:LineBytes(row)
for column, words in pairs(rowValues) do
currentLineBytes[column] = words[1]
currentLineBytes[column + 1] = words[2]
end
end
end
end
local function OnTypingJump(msg)
local bp = micro.CurPane()
if string.len(msg) > 2 then
bp.Buf.HighlightSearch = false
return
end
if string.len(msg) == 2 then
micro.InfoBar():DonePrompt(false)
return
end
msg = string.upper(msg)
RestoreOriginalWords(OmniOriginalWordsRecords, nil)
AssignJumpWordsToView(msg)
if string.len(msg) == 1 then
bp.Buf.LastSearch = "\\b[a-z][A-Z]"
else
bp.Buf.LastSearch = "\\b[A-Z]{2}"
end
bp.Buf.HighlightSearch = true
bp.Buf.LastSearchRegex = true
end
local function OnWordJump(msg, cancelled)
RestoreOriginalWords(OmniOriginalWordsRecords, nil)
local bp = micro.CurPane()
-- Restore the original search settings
bp.Buf.Settings["ignorecase"] = OmniOriginalSearchIgnoreCase
bp.Buf.LastSearch = OmniOriginalLastSearch
bp.Buf.LastSearchRegex = OmniOriginalLastSearchRegex
bp.Buf.HighlightSearch = OmniOriginalHighlightSearch
if string.len(msg) ~= 2 or cancelled then
return
end
msg = string.upper(msg)
if OmniJumpWordsRecords[msg] == nil then
return
end
local jumpRowColumn = OmniJumpWordsRecords[msg]
bp.Cursor:GotoLoc(Common.LocBoundCheck(bp.Buf, buffer.Loc(jumpRowColumn[2], jumpRowColumn[1])))
end
function Self.OmniJump(bp)
bp.Cursor:ResetSelection()
bp.Buf:ClearCursors()
AssignJumpWordsToView("")
-- Store the original search related settings
OmniOriginalSearchIgnoreCase = bp.Buf.Settings["ignorecase"]
OmniOriginalLastSearch = bp.Buf.LastSearch
OmniOriginalLastSearchRegex = bp.Buf.LastSearchRegex
OmniOriginalHighlightSearch = bp.Buf.HighlightSearch
-- NOTE: Syntax highlighting could be used instead of search highlight using UpdateRules.
-- But would require me to use cursor to modify the buffer instead of modifying bytes.
-- Which will be a lot of work (and complications!!) plus polluting the history as well.
bp.Buf.Settings["ignorecase"] = false
bp.Buf.LastSearch = "\\b[A-Z]{2}"
bp.Buf.LastSearchRegex = true
bp.Buf.HighlightSearch = true
micro.InfoBar():Prompt( "Select Word To Jump To > ", "", "Command", OnTypingJump, OnWordJump)
end
return Self