Repository navigation
Expand file tree
/
Copy pathvim.lua
More file actions
4675 lines (4569 loc) · 171 KB
/
Copy pathvim.lua
File metadata and controls
4675 lines (4569 loc) · 171 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
CCVIM - A Vim-like text editor for ComputerCraft computers.
Copyright (C) 2021 Minater247
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
]]
local args = {...}
local validArgs = {
"--version",
"--term", -- back-compatibility, ignored
"-g",
"--not-a-term",
}
local unimplementedArgs = {
"--",
"-v",
"-e",
"-E",
"-s",
"-d",
"-y",
"-R",
"-Z",
"-m",
"-M",
"-b",
"-l",
"-C",
"-N",
"-V",
"-D",
"-n",
"-r",
"-L",
"-T",
"--ttyfail",
"-u",
"--noplugin",
"-p",
"-o",
"-O",
"+",
"--cmd",
"-c",
"-S",
"-s",
"-w",
"-W",
"-x",
"--startuptime",
"-i",
"--clean",
"-h",
"--help"
}
local version = 0.733
local releasedate = "2022-05-19"
local fileExplorerVer = 0.12
local tab = require("/vim/lib/tab")
local argv = require("/vim/lib/args")
local str = require("/vim/lib/str")
local fil = require("/vim/lib/fil")
local keyseq = require("/vim/lib/keyseq")
local Trie = require("/vim/lib/trie")
local itertools = require("/vim/lib/itertools")
local monitor
local decargs = argv.pull(args, validArgs, unimplementedArgs) --DecodedArguments
local openfiles = {}
local wid, hig = term.getSize()
local running = true
local filelines = {}
local filename = ""
local currCursorX = 1
local currCursorY = 1
local newfile = false
local currFileOffset = 0 --AKA CurrYOffset
local currXOffset = 0
local oldx = nil
local copybuffer = ""
local copytype = nil
local jumpbuffer = {}
local jumpoffset = 0 --offset for t/T jumping before/after a letter
local currfile = 1
local fileContents = {}
local motd = false
local modeMappings = {
n = Trie.new(),
i = Trie.new(),
c = Trie.new(),
o = Trie.new(),
x = Trie.new(),
}
local mappingCommands = {
map = {mode = "", remap = true},
["map!"] = {mode = "!", remap = true},
nmap = {mode = "n", remap = true},
imap = {mode = "i", remap = true},
cmap = {mode = "c", remap = true},
omap = {mode = "o", remap = true},
xmap = {mode = "x", remap = true},
vmap = {mode = "x", remap = true},
noremap = {mode = "", remap = false},
["noremap!"] = {mode = "!", remap = false},
nnoremap = {mode = "n", remap = false},
inoremap = {mode = "i", remap = false},
cnoremap = {mode = "c", remap = false},
onoremap = {mode = "o", remap = false},
xnoremap = {mode = "x", remap = false},
vnoremap = {mode = "x", remap = false},
}
local currentMode = "n"
local noremapFor = 0
local filetypearr = {}
local mobile = false
local linenumbers = false
local lineoffset = 0
local syntaxhighlighting = false
local oldXOffset = 0
local oldFileOffset = 0
local lowspec = false
local autoindent = false
local ignorecase = false
local lastSearchPos
local lastSearchLine
local sessionSearches = {}
local repeatCount0 = 0
local repeatCount1 = 1
local scrollOption = 10
local shiftWidth = 4
local modeMsg
local visualSelectionRendered
local visualModeState = {} -- Stored as a text object
local typeahead = {} -- String keys and pseudokeys
local typeaheadUpdates = {} -- Mouse position updates
local inputProperties = {mouseX = 1, mouseY = 1, pasteData = ""}
local activeModifiers = {}
local prefixedModifiers = {}
local keyboards = {}
local useTmKeyboards = false
local actionsTrie = Trie.new() -- Generic Normal-mode handlers
local motionsTrie = Trie.new() -- Normal & operator-pending -mode
local textObjectsTrie = Trie.new() -- Only operator-pending-mode
local visualActionsTrie = Trie.new() -- Generic Visual-mode handlers
local keyNamesTranslation = {
backspace = "bs",
enter = "cr",
tab = "tab", -- Consider setting to "esc"?
numPadEnter = "cr",
delete = "del",
minus = "-",
equals = "=",
leftBracket = "[",
rightBracket = "]",
semiColon = ";",
apostrophe = "'",
grave = "`",
backslash = "backslash",
comma = ",",
period = ".",
slash = "/",
multiply = "kmultiply",
space = " ",
one = "1",
two = "2",
three = "3",
four = "4",
five = "5",
six = "6",
seven = "7",
eight = "8",
nine = "9",
zero = "0",
numPadAdd = "kplus",
numPadSubtract = "kminus",
numPadDecimal = "kpoint",
numPadEquals = "kequal",
numPadComma = "kcomma",
numPadDivide = "kdivide",
numPad0 = "k0",
numPad1 = "k1",
numPad2 = "k2",
numPad3 = "k3",
numPad4 = "k4",
numPad5 = "k5",
numPad6 = "k6",
numPad7 = "k7",
numPad8 = "k8",
numPad9 = "k9",
yen = "\xa5",
circumflex = "^",
at = "@",
colon = ":",
underscore = "_",
}
local keyNames = {}
for k, v in pairs(keyNamesTranslation) do
if keys[k] then
keyNames[keys[k]] = v
end
end
local builtinKeyCharacters = {
lt = "<",
space = " ",
backslash = "\\",
bar = "|",
kmultiply = "*",
kplus = "+",
kminus = "-",
kpoint = ".",
kequal = "=",
kcomma = ",",
kdivide = "/",
k0 = "0",
k1 = "1",
k2 = "2",
k3 = "3",
k4 = "4",
k5 = "5",
k6 = "6",
k7 = "7",
k8 = "8",
k9 = "9",
}
local modifierNames = {
[keys.leftCtrl] = "C",
[keys.rightCtrl] = "C",
[keys.leftAlt] = "A",
[keys.rightAlt] = "A",
[keys.leftShift] = "S",
[keys.rightShift] = "S",
}
local modifierOrder = {"C", "A", "S"}
local ignoreHold = {} -- Modifiers that can only be prefixed
local keyNormalisation = {
["<"] = "lt",
-- [">"] = "gt",
[" "] = "space",
["\\"] = "backslash",
["|"] = "bar",
["esc"] = "tab",
}
local mouseClickNames = {
[1] = "leftmouse",
[2] = "rightmouse",
[3] = "middlemouse",
}
-- Instead of defaulting to monitor without "--term",
-- default to no monitor without "-g"
-- Because multiple vim instances can be running at the same time,
-- and the monitor can be often used by something else
if tab.find(args, "-g") or tab.find(args, "--not-a-term") then
monitor = peripheral.find("monitor")
end
local function registerSimpleKeys(names)
local _, k
for _, k in ipairs(names) do
keyNames[keys[k]] = k:lower()
end
end
registerSimpleKeys{"left", "right", "up", "down"}
registerSimpleKeys{"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"}
registerSimpleKeys{"home", "end", "pageUp", "pageDown", "insert"}
registerSimpleKeys{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local function isCharacterKey(name)
if #name == 1 then
return true
end
if builtinKeyCharacters[name] ~= nil then
return true
end
return false
end
local function getSelfInsert(name)
if #name == 1 then
return name
end
local ch = builtinKeyCharacters[name]
if ch ~= nil then
return ch
end
return "<" .. name .. ">"
end
local function registerAction(seq, cb)
local key = keyseq.parseKeySequence(seq, keyNormalisation, modifierOrder)
actionsTrie:put(key, cb)
end
local function registerActionMulti(components, getCb)
for _, lst in itertools.product(components) do
registerAction(table.concat(lst), getCb(lst))
end
end
local function registerMotion(seq, opts, cb)
local key = keyseq.parseKeySequence(seq, keyNormalisation, modifierOrder)
motionsTrie:put(key, {cb, opts or {}})
end
local function registerMotionMulti(components, getOptsCb)
for _, lst in itertools.product(components) do
registerMotion(table.concat(lst), getOptsCb(lst))
end
end
local function registerTextObject(seq, opts, cb)
local key = keyseq.parseKeySequence(seq, keyNormalisation, modifierOrder)
textObjectsTrie:put(key, {cb, opts or {}})
end
local function registerTextObjectMulti(components, getOptsCb)
for _, lst in itertools.product(components) do
registerTextObject(table.concat(lst), getOptsCb(lst))
end
end
local function registerVisualAction(seq, cb)
local key = keyseq.parseKeySequence(seq, keyNormalisation, modifierOrder)
visualActionsTrie:put(key, cb)
end
local function registerVisualActionMulti(components, getCb)
for _, lst in itertools.product(components) do
registerVisualAction(table.concat(lst), getCb(lst))
end
end
local function registerNormalVisualAction(seq, cb)
registerAction(seq, cb)
registerVisualAction(seq, function()
currCursorX = visualModeState.x - currXOffset
currCursorY = visualModeState.y - currFileOffset
local result = cb() or false
visualModeState.x = currCursorX + currXOffset
visualModeState.y = currCursorY + currFileOffset
return result
end)
end
local function registerMapping(src, dst, opts)
opts = opts or {}
local srcArr = keyseq.parseKeySequence(src, keyNormalisation, modifierOrder)
local dstArr = keyseq.parseKeySequence(dst, keyNormalisation, modifierOrder)
local mode = opts.mode or ""
if mode == "" then
mode = "nox"
elseif mode == "!" then
mode = "ic"
end
local remap = opts.remap or false
local entry = {
dst = dstArr,
remap = remap,
}
for m in mode:gmatch(".") do
local trie = modeMappings[m]
if trie ~= nil then
trie:put(srcArr, entry)
end
end
end
local function getShiftWidth()
if shiftWidth < 1 then
return 1 -- tabstop is currently always 1
end
return shiftWidth
end
local function resetSize()
if monitor then
wid, hig = monitor.getSize()
else
wid, hig = term.getSize()
end
end
local resetLastSearch
local function clear()
if monitor then
monitor.clear()
else
term.clear()
end
end
local function setcolors(bg, txt)
if monitor then
monitor.setBackgroundColor(bg)
monitor.setTextColor(txt)
else
term.setBackgroundColor(bg)
term.setTextColor(txt)
end
end
local function write(message)
if monitor then
monitor.write(message)
else
term.write(message)
end
end
local function setpos(xpos, ypos)
if monitor then
monitor.setCursorPos(xpos, ypos)
else
term.setCursorPos(xpos, ypos)
end
end
local function getLatestModifiers(...) -- Example result: {A = true, C = nil}
local result = {}
local resetPrefixed = ... or false
local k, v
for k, v in pairs(activeModifiers) do
if v then
result[modifierNames[k]] = true
end
end
for k, v in pairs(prefixedModifiers) do
if v then
result[modifierNames[k]] = true
if resetPrefixed then
prefixedModifiers[k] = nil
end
end
end
return result
end
local function insertTypeahead(charname, ...)
local kwargs = ... or {}
local update = kwargs.update or {}
local index = kwargs.index or #typeahead + 1
charname = keyseq.normalizeKey(charname, keyNormalisation, modifierOrder)
table.insert(typeahead, index, charname)
table.insert(typeaheadUpdates, index, update)
end
local function expandPaste(alwaysNoremap)
local data = inputProperties.pasteData
if not data then
return
end
local noremap = alwaysNoremap or noremapFor > 0
local length = 0
for i, ch in itertools.enumerate(data:gmatch(".")) do
insertTypeahead(ch, {index = i})
length = i
end
if noremap then
noremapFor = noremapFor + length
end
end
local handleOsEvent
local handleNonInputEvent -- implemented after dependencies are declared
local function waitForEvents()
local e, s, v2, v3 = os.pullEvent()
handleOsEvent(e, s, v2, v3)
end
function handleOsEvent(e, s, v2, v3)
if e == "char" then
local mods = getLatestModifiers(true)
if mods.S then
-- Do not add shift, because it should already be consumed by the key to char conversion
end
if mods.A then
-- Warning: AltGr might generate unwanted alt modification
s = "A-" .. s
end
if mods.C then
s = "C-" .. s
end
insertTypeahead(s)
elseif e == "key" then
local translatedKey = keyNames[s]
local translatedMod = modifierNames[s]
if translatedKey ~= nil then
translatedKey = keyNormalisation[translatedKey:lower()] or translatedKey
-- Only if the key event will not be doubled by a char event
if not isCharacterKey(translatedKey) or activeModifiers[keys.leftCtrl] or activeModifiers[keys.rightCtrl] then
local mods = getLatestModifiers(true)
if mods.S then
translatedKey = "S-" .. translatedKey
end
if mods.A then
translatedKey = "A-" .. translatedKey
end
if mods.C then
translatedKey = "C-" .. translatedKey
end
insertTypeahead(translatedKey)
end
elseif translatedMod ~= nil then
if (v2 and not activeModifiers[s]) or ignoreHold[s] then
-- If a key sends a repeat event without a start event,
-- it will not send key_up
prefixedModifiers[s] = not prefixedModifiers[s]
else
activeModifiers[s] = true
if prefixedModifiers[s] then
prefixedModifiers[s] = nil -- Repeated modifier presses remove the prefixed state
else
prefixedModifiers[s] = not v2 -- Only prefixed if not held
-- FIXME repeats for the modifier keys seem to not be sent
end
end
end
elseif e == "key_up" then
local translatedMod = modifierNames[s]
if translatedMod ~= nil then
activeModifiers[s] = nil -- No longer active
-- But remain prefixed
end
elseif e == "mouse_click" then
local translatedKey = mouseClickNames[s]
local mods = getLatestModifiers(true)
if mobile or translatedKey == nil then
translatedKey = "tab"
else
if mods.S then
translatedKey = "S-" .. translatedKey
end
if mods.A then
translatedKey = "A-" .. translatedKey
end
if mods.C then
translatedKey = "C-" .. translatedKey
end
end
insertTypeahead(translatedKey, {update = {mouseX = v2, mouseY = v3}})
elseif e == "mouse_scroll" then
local translatedKey = "scrollwheeldown"
if s < 0 then
translatedKey = "scrollwheelup"
end
local mods = getLatestModifiers(true)
if mods.S then
translatedKey = "S-" .. translatedKey
end
if mods.A then
translatedKey = "A-" .. translatedKey
end
if mods.C then
translatedKey = "C-" .. translatedKey
end
insertTypeahead(translatedKey, {update = {mouseX = v2, mouseY = v3}})
elseif e == "paste" then
-- Warning! Some implementations do not paste when alt or shift is help
-- Prefix these modifiers to send them as a paste event for the program
-- to receive the clipboard contents
local translatedKey = "C-v"
local mods = getLatestModifiers(true)
if mods.S then
translatedKey = "S-" .. translatedKey
end
if mods.A then
translatedKey = "A-" .. translatedKey
end
insertTypeahead(translatedKey, {update = {pasteData = s}})
else
handleNonInputEvent(e, s, v2, v3)
end
end
local function pullTypeahead()
local key = table.remove(typeahead, 1)
while key == nil and running do
waitForEvents()
key = table.remove(typeahead, 1)
end
local upd = table.remove(typeaheadUpdates, 1)
itertools.update(inputProperties, upd)
if noremapFor > 0 then
noremapFor = noremapFor - 1
end
return key
end
local applyMappings
-- pull from typeahead with remaps
local function pullTypeaheadWRMP()
applyMappings()
local key = pullTypeahead()
return key
end
local function peekTypeahead(idx)
idx = idx or 1
while #typeahead < idx do
waitForEvents()
end
return typeahead[idx]
end
local function peekTypeaheadWRMP(idx)
applyMappings()
local key = peekTypeahead(idx)
return key
end
local function typeaheadLength()
return #typeahead
end
function applyMappings()
while noremapFor < 1 do
local trie = modeMappings[currentMode]
if trie == nil then
return
end
local i = 0
local cons = trie:consumer()
while true do
i = i + 1
local key = peekTypeahead(i)
if not cons:next(key) then
break
end
if not cons:hasNext() then
break
end
end
local len, entry = cons:getDeepest()
if len > 0 then
local dst = entry.dst or {}
if #dst < 1 then
for _ = 1, len do
pullTypeahead()
end
else
local saveProps = inputProperties
inputProperties = {}
for _ = 1, len do
pullTypeahead()
end
local squashedUpdate = inputProperties
inputProperties = saveProps
insertTypeahead(dst[1], {update = squashedUpdate, index = 1})
for i = 2, #dst do
insertTypeahead(dst[i], {index = i})
end
if not entry.remap then
noremapFor = #dst
end
end
else
noremapFor = 1
end
end
end
local function pullCount() -- Returns string
local ch = peekTypeaheadWRMP(1)
while ch == "<C-S-v>" do
pullTypeahead()
expandPaste()
ch = peekTypeaheadWRMP()
end
if ch:find("[^1-9]") then
return ""
end
pullTypeahead()
local builder = {ch}
while true do
ch = peekTypeaheadWRMP(1)
while ch == "<C-S-v>" do
pullTypeahead()
expandPaste()
ch = peekTypeaheadWRMP()
end
if ch:find("%D") then
return table.concat(builder)
end
pullTypeahead()
table.insert(builder, ch)
end
end
local function pullCommand(input, numeric, len)
if input == nil then
input = ''
end
local x,y = 1, hig
local oldModeMsg = modeMsg
local prevMode = currentMode
currentMode = "c"
local backspace = false
local finish = false
repeat
setcolors(colors.black, colors.white)
setpos(x,y)
write(input)
if backspace then
write(" ")
resetSize()
setpos(x - 1, y)
if #input < 1 then
finish = true
insertTypeahead("bs", {index = 1}) -- Insert fake event to consume
end
end
if #input > 0 then
setpos(x + #input, y)
setcolors(colors.lightGray, colors.white)
write(" ")
end
modeMsg = input
local key = pullTypeaheadWRMP()
-- terminal cursor position is undefined after typeahead access
y = hig
setpos(x + #input, y)
if isCharacterKey(key) then
local p1 = getSelfInsert(key)
local send = true
if #input < 1 then
setpos(1, 1)
setcolors(colors.black, colors.white)
write(" ")
end
if numeric and tonumber(p1) == nil then
send = false
end
if len ~= nil then
if (#input < len) and send then
input = input .. p1
end
else
if send then
input = input .. p1
end
end
elseif not finish then
if key == "bs" then
input = input:sub(1, #input - 1)
backspace = true
elseif key == "C-S-v" then
expandPaste()
end
end
until (key == "cr") or (finish == true)
modeMsg = oldModeMsg
currentMode = prevMode
return input
end
local function pullTextObject(opts)
local prevMode = currentMode
currentMode = "o"
opts = opts and itertools.collect(pairs(opts)) or {wantX = oldx}
opts.x = opts.x or currCursorX + currXOffset
opts.y = opts.y or currCursorY + currFileOffset
opts.initialX = opts.initialX or opts.x
opts.initialY = opts.initialY or opts.y
local prio = {textObject = 2, motion = 1, max = 2}
local cons = Trie.Consumer.new{
n = prio.max,
[prio.textObject] = textObjectsTrie,
[prio.motion] = motionsTrie,
}
local countMultiplier = repeatCount1
local countStr = pullCount()
if #countStr > 0 then
repeatCount0 = tonumber(countStr) * countMultiplier
repeatCount1 = repeatCount0
end
local i = 0
while true do
i = i + 1
local key
if i == 1 then
key = peekTypeaheadWRMP(i)
else
key = peekTypeahead(i)
end
if key == "C-c" then
-- Clear typeahead
for _ = 1, i do
pullTypeahead()
end
cons = nil
break
end
if not cons:next(key) then
break
end
if not cons:hasNext() then
break
end
end
if cons ~= nil then
local len, entry, kind = cons:getDeepest()
if len > 0 then
for _ = 1, len do
pullTypeahead()
end
if kind == prio.textObject or kind == prio.motion then
if entry[2] then
itertools.update(opts, entry[2])
end
opts = entry[1](opts) or opts
end
else
-- Drop one key
pullTypeahead()
opts = nil
end
else
opts = nil -- no text object returned => operator should terminate
end
currentMode = prevMode
return opts
end
-- Helper text objects
local function textObjectLine(opts)
opts = opts and itertools.collect(pairs(opts)) or {wantX = oldx}
opts.x = opts.x or currCursorX + currXOffset
opts.y = opts.y or currCursorY + currFileOffset
opts.initialX = opts.initialX or opts.x
opts.initialY = opts.initialY or opts.y
opts.linewise = true
opts.y = opts.y + repeatCount1 - 1
if opts.y > #filelines then
opts.y = #filelines
end
return opts
end
local function textObjectEOL(opts)
opts = opts and itertools.collect(pairs(opts)) or {wantX = oldx}
opts.x = opts.x or currCursorX + currXOffset
opts.y = opts.y or currCursorY + currFileOffset
opts.initialX = opts.initialX or opts.x
opts.initialY = opts.initialY or opts.y
opts.y = opts.y + repeatCount1 - 1
if opts.y > #filelines then
opts.y = #filelines
end
opts.wantX = 999999999
opts.x = #filelines[opts.y]
return opts
end
local function textObjectSOL(opts)
opts = opts and itertools.collect(pairs(opts)) or {wantX = oldx}
opts.x = opts.x or currCursorX + currXOffset
opts.y = opts.y or currCursorY + currFileOffset
opts.initialX = opts.initialX or opts.x
opts.initialY = opts.initialY or opts.y
opts.wantX = nil
local line = filelines[opts.y]
opts.x = line:find("[^ \x09]") or #line
if opts.x < 1 then
opts.x = 1
end
return opts
end
local function clearScreenLine(line)
setcolors(colors.black, colors.white)
setpos(1, line)
for i=1,wid,1 do
write(" ")
end
end
local function err(message)
clearScreenLine(hig)
setpos(1, hig)
setcolors(colors.red, colors.white)
write(message)
end
local function sendMsg(message)
clearScreenLine(hig)
setpos(1, hig)
setcolors(colors.black, colors.white)
write(message)
end
local function setModeMsg(msg)
if msg and #msg > 0 then
modeMsg = msg
sendMsg(msg)
else
modeMsg = nil
sendMsg(" ")
end
end
local function getMLCSetCache()
local mlc = fileContents[currfile]["Multi-line comments"]
if not mlc._setCache then
local cache = {}
for i = 1, 3 do
cache[i] = tab.itemIndices(mlc[i])
end
mlc._setCache = cache
end
return mlc._setCache
end
local function drawFileLine(i, synt)
setpos(1, i - currFileOffset)
if filelines then
local lineVisualBeg = 0
local lineVisualEd = 0
if filelines[i] ~= nil then
if visualSelectionRendered and visualModeState then
local begX, begY, edX, edY = visualModeState.initialX, visualModeState.initialY, visualModeState.x, visualModeState.y
if begX and begY and edX and edY then
if edY < begY or edY == begY and edX < begX then
begX, edX = edX, begX
begY, edY = edY, begY
end
end
if visualModeState.linewise then
if i >= begY and i <= edY then
lineVisualBeg = 1
lineVisualEd = #filelines[i]
end
else
if i >= begY and i <= edY then
lineVisualBeg = 1
lineVisualEd = #filelines[i]
end
if i == begY then
lineVisualBeg = begX
end
if i == edY then
lineVisualEd = edX
end
end
end
setcolors(colors.black, colors.white)
if fileContents[currfile] then
if synt then
local wordsOfLine = str.split(filelines[i], " ")
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
local wordBeg = 1
local wordEd = -1
for j=1,#wordsOfLine,1 do
wordBeg = wordEd + 2
wordEd = wordBeg + #wordsOfLine[j] - 1
local hlBg, hlFg = colors.black, colors.white
if synt.keywordSets.todo[wordsOfLine[j]] then
hlBg, hlFg = colors.yellow, colors.blue
elseif synt.keywordSets.statement[wordsOfLine[j]] then
hlBg, hlFg = colors.black, colors.lightBlue
elseif synt.keywordSets.preproc[wordsOfLine[j]] then
hlBg, hlFg = colors.black, colors.purple
end
if lineVisualEd < lineVisualBeg or wordBeg > lineVisualEd or wordEd < lineVisualBeg then
setcolors(hlBg, hlFg)
write(wordsOfLine[j])
elseif wordBeg >= lineVisualBeg and wordEd <= lineVisualEd then
setcolors(colors.gray, hlFg)
write(wordsOfLine[j])
else
local wordVisualBeg = lineVisualBeg - wordBeg + 1
local wordVisualEd = lineVisualEd - wordBeg + 1
if wordVisualBeg < 1 then
wordVisualBeg = 1
end