-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvim.lua
More file actions
3389 lines (3344 loc) · 151 KB
/
vim.lua
File metadata and controls
3389 lines (3344 loc) · 151 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"
}
local unimplementedArgs = {
"--",
"-v",
"-e",
"-E",
"-s",
"-d",
"-y",
"-R",
"-Z",
"-m",
"-M",
"-b",
"-l",
"-C",
"-N",
"-V",
"-D",
"-n",
"-r",
"-L",
"-T",
"--not-a-term",
"--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 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 remappings = {}
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 = {}
if not tab.find(args, "--term") then
monitor = peripheral.find("monitor")
end
local function resetSize()
if monitor then
wid, hig = monitor.getSize()
else
wid, hig = term.getSize()
end
end
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
--pull event with remaps
local function pullEventWRMP()
local e, s, v2, v3 = os.pullEvent()
if e == "char" and remappings[s] then
s = remappings[s]
end
return e, s, v2, v3
end
local function pullCommand(input, numeric, len)
if input == nil then
input = ''
end
local x,y = 1, hig
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
end
end
if #input > 0 then
setpos(x + #input, y)
setcolors(colors.lightGray, colors.white)
write(" ")
end
local ev, p1 = pullEventWRMP()
if ev == 'char' then
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 ev == 'key' then
if p1 == keys.backspace then
input = input:sub(1, #input - 1)
backspace = true
end
end
until (ev == 'key' and p1 == keys.enter) or (finish == true and ev == "key")
return input
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 drawFile(forcedredraw)
motd = false
if currXOffset ~= oldXOffset or currFileOffset ~= oldFileOffset or forcedredraw then
for i=1,hig-1,1 do
clearScreenLine(i)
end
oldXOffset = currXOffset
oldFileOffset = currFileOffset
for i=currFileOffset,(hig - 1) + currFileOffset,1 do
setpos(1, i - currFileOffset)
if filelines then
if filelines[i] ~= nil then
setcolors(colors.black, colors.white)
if fileContents[currfile] then
if fileContents[currfile]["filetype"] and syntaxhighlighting and filetypearr[fileContents[currfile]["filetype"]] then
local synt = filetypearr[fileContents[currfile]["filetype"]].syntax()
local wordsOfLine = str.split(filelines[i], " ")
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
for j=1,#wordsOfLine,1 do
if tab.find(synt[1], wordsOfLine[j]) then
setcolors(colors.yellow, colors.blue)
elseif tab.find(synt[2][1], wordsOfLine[j]) then
setcolors(colors.black, colors.lightBlue)
elseif tab.find(synt[2][2], wordsOfLine[j]) then
setcolors(colors.black, colors.purple)
else
setcolors(colors.black, colors.white)
end
write(wordsOfLine[j])
if j ~= #wordsOfLine then
setcolors(colors.black, colors.white)
write(" ")
end
end
--another loop for drawing strings
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
local quotationmarks = str.indicesOfLetter(filelines[i], synt[3])
local inquotes = false
local justset = false
local quotepoints = {}
setcolors(colors.black, colors.red)
for j=1,#filelines[i],1 do
local writechar = not ((1 - currXOffset - lineoffset + j - 1 < 1) or (1 - currXOffset + lineoffset + j - 1 > wid))
if writechar then
setpos(1 - currXOffset + lineoffset + j - 1, i - currFileOffset)
end
if tab.find(quotationmarks, j) then
if not inquotes then
if j < quotationmarks[#quotationmarks] then
inquotes = true
justset = true
end
end
end
if inquotes then
if writechar then
write(string.sub(filelines[i], j, j))
end
table.insert(quotepoints, #quotepoints, j - 2) --Don't know why I need to subtract 2 but heck it works
end
if tab.find(quotationmarks, j) and not justset then
if inquotes then
inquotes = false
end
end
justset = false
end
local commentstart = 0
commentstart = str.find(filelines[i], synt[4], quotepoints)
if commentstart and commentstart ~= false then
setpos(1 - currXOffset + lineoffset + commentstart - 1, i - currFileOffset)
setcolors(colors.black, colors.green)
write(string.sub(filelines[i], commentstart, #filelines[i]))
end
commentstart = str.find(filelines[i], synt[7][2])
if not commentstart then
commentstart = 0
end
if not lowspec then
if tab.find(fileContents[currfile]["Multi-line comments"][2], i) then
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
setcolors(colors.black, colors.green)
write(filelines[i])
elseif tab.find(fileContents[currfile]["Multi-line comments"][3], i) then
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
setcolors(colors.black, colors.green)
write(string.sub(filelines[i], 1, commentstart + 1))
end
end
else
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
write(string.sub(filelines[i], 1, #filelines[i]))
end
end
else
setcolors(colors.black, colors.purple)
write("~")
end
end
end
else
--only draw 3 lines
for i=currCursorY + currFileOffset - 1, currCursorY + currFileOffset + 2, 1 do
if i - currFileOffset < hig then
clearScreenLine(i - currFileOffset)
setpos(1, i - currFileOffset)
if filelines then
if filelines[i] ~= nil then
setcolors(colors.black, colors.white)
if fileContents[currfile] then
if fileContents[currfile]["filetype"] and syntaxhighlighting and filetypearr[fileContents[currfile]["filetype"]] then
local synt = filetypearr[fileContents[currfile]["filetype"]].syntax()
local wordsOfLine = str.split(filelines[i], " ")
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
for j=1,#wordsOfLine,1 do
if tab.find(synt[1], wordsOfLine[j]) then
setcolors(colors.yellow, colors.blue)
elseif tab.find(synt[2][1], wordsOfLine[j]) then
setcolors(colors.black, colors.lightBlue)
elseif tab.find(synt[2][2], wordsOfLine[j]) then
setcolors(colors.black, colors.purple)
else
setcolors(colors.black, colors.white)
end
write(wordsOfLine[j])
if j ~= #wordsOfLine then
setcolors(colors.black, colors.white)
write(" ")
end
end
--another loop for drawing strings
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
local quotationmarks = str.indicesOfLetter(filelines[i], synt[3])
local inquotes = false
local justset = false
local quotepoints = {}
setcolors(colors.black, colors.red)
for j=1,#filelines[i],1 do
local writechar = not ((1 - currXOffset - lineoffset + j - 1 < 1) or (1 - currXOffset + lineoffset + j - 1 > wid))
if writechar then
setpos(1 - currXOffset + lineoffset + j - 1, i - currFileOffset)
end
if tab.find(quotationmarks, j) then
if not inquotes then
if j < quotationmarks[#quotationmarks] then
inquotes = true
justset = true
end
end
end
if inquotes then
if writechar then
write(string.sub(filelines[i], j, j))
end
table.insert(quotepoints, #quotepoints, j - 2)
end
if tab.find(quotationmarks, j) and not justset then
if inquotes then
inquotes = false
end
end
justset = false
end
local commentstart = 0
commentstart = str.find(filelines[i], synt[4], quotepoints)
if commentstart and commentstart ~= false then
setpos(1 - currXOffset + lineoffset + commentstart - 1, i - currFileOffset)
setcolors(colors.black, colors.green)
write(string.sub(filelines[i], commentstart, #filelines[i]))
end
commentstart = str.find(filelines[i], synt[7][2])
if not commentstart then
commentstart = 0
end
if not lowspec then
if tab.find(fileContents[currfile]["Multi-line comments"][2], i) then
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
setcolors(colors.black, colors.green)
write(filelines[i])
elseif tab.find(fileContents[currfile]["Multi-line comments"][3], i) then
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
setcolors(colors.black, colors.green)
write(string.sub(filelines[i], 1, commentstart + 1))
end
end
else
setpos(1 - currXOffset + lineoffset, i - currFileOffset)
write(string.sub(filelines[i], 1, #filelines[i]))
end
end
else
setcolors(colors.black, colors.purple)
write("~")
end
end
end
end
end
-- Draw the cursor
local tmp
if filelines then
if filelines[currCursorY + currFileOffset] ~= nil then
tmp = string.sub(filelines[currCursorY + currFileOffset], currCursorX + currXOffset, currCursorX + currXOffset)
end
end
if filelines then
setpos(currCursorX + lineoffset, currCursorY)
else
setpos(currCursorX, currCursorY)
end
setcolors(colors.lightGray, colors.white)
if tmp ~= nil and tmp ~= "" then
write(tmp)
else
write(" ")
end
if linenumbers then
setcolors(colors.black, colors.yellow)
for i=currFileOffset,(hig-1)+currFileOffset,1 do
setpos(1, i - currFileOffset)
if i < 1000 then
write(string.rep(" ", 3 - #tostring(i)))
end
if i < 10000 then
if i <= #filelines then
write(i)
end
else
if i <= #filelines then
write("10k+")
end
end
end
end
end
local function moveCursorLeft()
lastSearchPos = nil
lastSearchLine = nil
if currCursorX + currXOffset ~= 1 then
currCursorX = currCursorX - 1
if currCursorX < 1 then
currCursorX = currCursorX + 1
currXOffset = currXOffset - 1
drawFile(true)
else
drawFile()
end
end
oldx = nil
end
local function moveCursorRight(endPad)
lastSearchPos = nil
lastSearchLine = nil
if endPad == nil then
endPad = 0
end
if filelines[currCursorY + currFileOffset] ~= nil then
if currCursorX + currXOffset < #(filelines[currCursorY + currFileOffset]) + 1 - endPad then
currCursorX = currCursorX + 1
if currCursorX + lineoffset > wid then
currCursorX = currCursorX - 1
currXOffset = currXOffset + 1
drawFile(true)
else
drawFile()
end
end
end
oldx = nil
end
local function moveCursorUp(ignoreX)
lastSearchPos = nil
lastSearchLine = nil
if oldx ~= nil and not ignoreX then
currCursorX = oldx - currXOffset
else
oldx = currCursorX + currXOffset
end
if currCursorY + currFileOffset ~= 1 then
currCursorY = currCursorY - 1
if currCursorX + currXOffset > #(filelines[currCursorY + currFileOffset]) + 1 then
if filelines[currCursorY + currFileOffset] ~= "" then
currCursorX = #(filelines[currCursorY + currFileOffset]) + 1 - currXOffset
else
currCursorX = 1
currXOffset = 0
end
end
if currCursorX < 1 then
while currCursorX < 1 do
currXOffset = currXOffset - 1
currCursorX = currCursorX + 1
end
elseif currCursorX + lineoffset > wid then
while currCursorX + lineoffset > wid do
currXOffset = currXOffset + 1
currCursorX = currCursorX - 1
end
end
while currCursorY < 1 do
currFileOffset = currFileOffset - 1
currCursorY = currCursorY + 1
end
while currFileOffset < 0 do
currFileOffset = currFileOffset + 1
end
drawFile()
end
end
local function moveCursorDown()
lastSearchPos = nil
lastSearchLine = nil
if oldx ~= nil then
currCursorX = oldx - currXOffset
else
oldx = currCursorX + currXOffset
end
if currCursorY + currFileOffset < #filelines then
currCursorY = currCursorY + 1
if currCursorX + currXOffset > #(filelines[currCursorY + currFileOffset]) + 1 then
if filelines[currCursorY + currFileOffset] ~= "" then
currCursorX = #(filelines[currCursorY + currFileOffset]) + 1 - currXOffset
else
currCursorX = 1
currXOffset = 0
end
end
if currCursorX < 1 then
while currCursorX < 1 do
currXOffset = currXOffset - 1
currCursorX = currCursorX + 1
end
elseif currCursorX + lineoffset > wid then
while currCursorX + lineoffset > wid do
currXOffset = currXOffset + 1
currCursorX = currCursorX - 1
end
end
while currCursorY > hig - 1 do
currFileOffset = currFileOffset + 1
currCursorY = currCursorY - 1
end
drawFile()
end
end
--Recalculate where multi-line comments are, based on position in file
local function recalcMLCs(force, offsetby)
if not fileContents[currfile] then
return
end
if not fileContents[currfile]["Multi-line comments"] then
fileContents[currfile]["Multi-line comments"] = {{}, {}, {}}
end
if not lowspec then
local synt
if fileContents[currfile]["filetype"] then
if fs.exists("/vim/syntax/"..fileContents[currfile]["filetype"]..".lua")then
local tmp = require("/vim/syntax/"..fileContents[currfile]["filetype"])
synt = tmp.syntax()
end
end
if synt then
if offsetby then
--move all multi-line comments after the current cursor Y by offsetby
for i=1,#fileContents[currfile]["Multi-line comments"][1],1 do
if fileContents[currfile]["Multi-line comments"][1][i] > currCursorY + currFileOffset + offsetby[1] then
fileContents[currfile]["Multi-line comments"][1][i] = fileContents[currfile]["Multi-line comments"][1][i] + offsetby[2]
end
end
for i=1,#fileContents[currfile]["Multi-line comments"][2],1 do
if fileContents[currfile]["Multi-line comments"][2][i] > currCursorY + currFileOffset + offsetby[1] then
fileContents[currfile]["Multi-line comments"][2][i] = fileContents[currfile]["Multi-line comments"][2][i] + offsetby[2]
end
end
for i=1,#fileContents[currfile]["Multi-line comments"][3],1 do
if fileContents[currfile]["Multi-line comments"][3][i] > currCursorY + currFileOffset + offsetby[1] then
fileContents[currfile]["Multi-line comments"][3][i] = fileContents[currfile]["Multi-line comments"][3][i] + offsetby[2]
end
end
if tab.find(fileContents[currfile]["Multi-line comments"][2], currCursorY + currFileOffset - 1) then
--if the cursor - 1 is on a multi-line comment (type 2), then check if the current line contains the end of the comment.
--if it does, add it as type 3. If not, add it as type 2.
if str.find(fileContents[currfile][currCursorY + currFileOffset], "]]") then
table.insert(fileContents[currfile]["Multi-line comments"][3], currCursorY + currFileOffset)
else
table.insert(fileContents[currfile]["Multi-line comments"][2], currCursorY + currFileOffset)
end
end
else
if ((tab.find(fileContents[currfile]["Multi-line comments"][1], currCursorY + currFileOffset) and not str.find(filelines[currCursorY + currFileOffset], synt[7][1])) or (not tab.find(fileContents[currfile]["Multi-line comments"][1], currCursorY + currFileOffset) and str.find(filelines[currCursorY + currFileOffset], synt[7][1])) or (tab.find(fileContents[currfile]["Multi-line comments"][3], currCursorY + currFileOffset) and not str.find(filelines[currCursorY + currFileOffset], synt[7][2])) or (not tab.find(fileContents[currfile]["Multi-line comments"][3], currCursorY + currFileOffset) and str.find(filelines[currCursorY + currFileOffset], synt[7][2])) or force) and syntaxhighlighting then
local multilinesInFile = {{}, {}, {}} --beginning quote points, regular quote points, end quote points
local quotepoints = {}
local justset = false
for j=1,#fileContents[currfile],1 do
local quotationmarks = str.indicesOfLetter(fileContents[currfile][j], synt[3])
local inquotes = false
justset = false
for k=1,#fileContents[currfile][j],1 do
if tab.find(quotationmarks, k) then
if not inquotes then
if k < quotationmarks[#quotationmarks] then
inquotes = true
justset = true
end
end
end
if inquotes then
table.insert(quotepoints, #quotepoints, k - 2)
end
if tab.find(quotationmarks, k) and not justset then
if inquotes then
inquotes = false
end
end
justset = false
end
end
local inmulti = false
justset = false
for j=1,#fileContents[currfile],1 do
if str.find(fileContents[currfile][j], synt[7][1], quotepoints) and not inmulti then
inmulti = true
justset = true
table.insert(multilinesInFile[1], #multilinesInFile[1] + 1, j)
end
if inmulti and not (str.find(fileContents[currfile][j], synt[7][2])) and not justset then
table.insert(multilinesInFile[2], #multilinesInFile[2] + 1, j)
end
if str.find(fileContents[currfile][j], synt[7][2]) then
if inmulti then
inmulti = false
table.insert(multilinesInFile[3], #multilinesInFile[3] + 1, j)
end
end
justset = false
end
fileContents[currfile]["Multi-line comments"] = multilinesInFile
return true
else
return false
end
end
end
else
return false
end
end
local function redrawTerm()
clearScreenLine(hig)
if motd then
clear()
for i=currFileOffset,(hig - 1) + currFileOffset,1 do
setpos(1, i - currFileOffset)
setcolors(colors.black, colors.purple)
write("~")
end
setcolors(colors.black, colors.white)
setpos((wid / 2) - (33 / 2), (hig / 2) - 3)
write("CCVIM - ComputerCraft Vi Improved")
setpos((wid / 2) - (#("version ".. version) / 2), (hig / 2) - 1)
write("version "..version)
setpos((wid / 2) - (13 / 2), (hig / 2))
write("By Minater247")
if wid > 53 then
setpos((wid / 2) - (46 / 2), (hig / 2) + 1)
write("CCVIM is open source and freely distributable.")
setpos((wid / 2) - (28 / 2), (hig / 2) + 4)
else
setpos((wid / 2) - (28 / 2), (hig / 2) + 3)
end
write("Type :q")
setcolors(colors.black, colors.lightBlue)
write("<Enter> ")
setcolors(colors.black, colors.white)
write("to exit")
else
drawFile(true)
end
while currCursorX + lineoffset > wid do
currCursorX = currCursorX - 1
currXOffset = currXOffset + 1
end
while currCursorX < 1 do
currCursorX = currCursorX + 1
currXOffset = currXOffset - 1
end
while currCursorY > hig - 1 do
currCursorY = currCursorY - 1
currFileOffset = currFileOffset + 1
end
while currCursorY < 1 do
currCursorY = currCursorY + 1
currFileOffset = currFileOffset - 1
end
end
local function insertMode()
drawFile(true)
sendMsg("-- INSERT --")
local ev, key
while key ~= keys.tab do
ev, key = pullEventWRMP()
if ev == "key" then
if key == keys.left then
moveCursorLeft()
elseif key == keys.right then
moveCursorRight()
elseif key == keys.up then
moveCursorUp()
elseif key == keys.down then
moveCursorDown()
elseif key == keys.backspace then
if filelines[currCursorY + currFileOffset] ~= "" and filelines[currCursorY + currFileOffset] ~= nil and currCursorX > 1 then
filelines[currCursorY + currFileOffset] = string.sub(filelines[currCursorY + currFileOffset], 1, currCursorX + currXOffset - 2) .. string.sub(filelines[currCursorY + currFileOffset], currCursorX + currXOffset, #(filelines[currCursorY + currFileOffset]))
moveCursorLeft()
local redrawhere = recalcMLCs()
fileContents[currfile]["unsavedchanges"] = true
drawFile(redrawhere)
else
if currCursorX + currXOffset < 2 then
if currCursorY + currFileOffset > 1 then
if #filelines > 1 then
currCursorX = #(filelines[currCursorY + currFileOffset - 1]) + 1
filelines[currCursorY + currFileOffset - 1] = filelines[currCursorY + currFileOffset - 1] .. filelines[currCursorY + currFileOffset]
table.remove(filelines, currCursorY + currFileOffset)
moveCursorUp(true)
if currCursorX + lineoffset > wid then
while currCursorX + lineoffset > wid do
currXOffset = currXOffset + 1
currCursorX = currCursorX - 1
end
end
recalcMLCs()
drawFile(true)
fileContents[currfile]["unsavedchanges"] = true
end
end
else
filelines[currCursorY + currFileOffset] = string.sub(filelines[currCursorY + currFileOffset], 1, currCursorX + currXOffset - 2) .. string.sub(filelines[currCursorY + currFileOffset], currCursorX + currXOffset, #(filelines[currCursorY + currFileOffset]))
currXOffset = currXOffset - math.floor(wid / 2)
currCursorX = currCursorX + math.floor(wid / 2) - 1
if currXOffset < 0 then
currCursorX = currXOffset + currCursorX
currXOffset = 0
end
local redrawhere = recalcMLCs()
drawFile(redrawhere)
fileContents[currfile]["unsavedchanges"] = true
lastSearchPos = nil
lastSearchLine = nil
end
end
elseif key == keys.enter then
lastSearchPos = nil
lastSearchLine = nil
if filelines[currCursorY + currFileOffset] ~= nil then
local indentedamount = 0
if autoindent then
--check if current line is indented, if so, indent the new line
for i=1,#(filelines[currCursorY + currFileOffset]),1 do
if string.sub(filelines[currCursorY + currFileOffset], i, i) == " " then
indentedamount = indentedamount + 1
else
break
end
end
end
table.insert(filelines, currCursorY + currFileOffset + 1, string.rep(" ", indentedamount) .. string.sub(filelines[currCursorY + currFileOffset], currCursorX + currXOffset, #(filelines[currCursorY + currFileOffset])))
filelines[currCursorY + currFileOffset] = string.sub(filelines[currCursorY + currFileOffset], 1, currCursorX + currXOffset - 1)
currCursorY = currCursorY + 1
while currCursorY > hig - 1 do
currFileOffset = currFileOffset + 1
currCursorY = currCursorY - 1
end
currCursorX = 1
currXOffset = 0
for i=1,indentedamount,1 do
currCursorX = currCursorX + 1
while currCursorX + lineoffset > wid do
currXOffset = currXOffset + 1
currCursorX = currCursorX - 1
end
if currXOffset < 0 then
currXOffset = 0
end
end
fileContents[currfile]["unsavedchanges"] = true
else
table.insert(filelines, currCursorY + currFileOffset + 1, "")
end
recalcMLCs(false, {-1, 1}) --slow, but it works for now
drawFile(true)
end
elseif ev == "char" then
lastSearchPos = nil
lastSearchLine = nil
if filelines[currCursorY + currFileOffset] == nil then
filelines[currCursorY + currFileOffset] = ""
end
filelines[currCursorY + currFileOffset] = string.sub(filelines[currCursorY + currFileOffset], 1, currCursorX + currXOffset - 1) .. key ..string.sub(filelines[currCursorY + currFileOffset], currCursorX + currXOffset, #(filelines[currCursorY + currFileOffset]))
currCursorX = currCursorX + 1
if currCursorX + lineoffset > wid then
currCursorX = currCursorX - 1
currXOffset = currXOffset + 1
end
local redrawhere = recalcMLCs()
drawFile(redrawhere)
if not fileContents[currfile] then
fileContents[currfile] = {""}
end
fileContents[currfile]["unsavedchanges"] = true
elseif ev == "term_resize" then
resetSize()
redrawTerm()
sendMsg("-- INSERT --")
elseif ev == "mouse_click" and mobile then
key = keys.tab --get out of the loop
end
end
sendMsg(" ")
end
--Parse .vimrc file here
if fs.exists("/vim/.vimrc") then
local vimrclines = fil.toArr("/vim/.vimrc")
for i=1,#vimrclines,1 do
if not (string.sub(vimrclines[i], 1, 1) == "\"") then --ignore commented lines
local rctable = str.split(vimrclines[i], " ")
if rctable[1] == "set" then
if not string.find(rctable[2], "=") then
if rctable[2] == "mobile" then
mobile = true
elseif rctable[2] == "number" then
linenumbers = true
lineoffset = 4
elseif rctable[2] == "lowperformance" then
lowspec = true
elseif rctable[2] == "autoindent" then
autoindent = true
elseif rctable[2] == "ignorecase" or rctable[2] == "ic" then
ignorecase = true
end
else
--set the things to values
end
elseif rctable[1] == "syntax" and rctable[2] == "on" then
syntaxhighlighting = true
elseif rctable[1] == "map" then
if rctable[2] and rctable[3] or not (#rctable > 3) then
remappings[rctable[2]] = rctable[3]
else
print("Mapping requires 2 arguments.")
sendMsg("Press enter to continue...")
local _,k = os.pullEvent("key")
while k ~= keys.enter do
_,k = os.pullEvent("key")
end
end
elseif rctable[1] ~= "" and rctable[1] ~= nil then
error("Unrecognized vimrc command " .. rctable[1] .. ". Full vimscript is not yet supported.")
end
end
end
end
local function pullChar()
local _, tm = os.pullEvent("char")
if remappings[tm] then
tm = remappings[tm]
end
return _, tm
end
local oldyoff
local function drawDirInfo(dir, sortType, ypos, yoff, filesInDir, initialDraw)
if initialDraw then
setcolors(colors.black, colors.white)
for i=1,hig-1,1 do
clearScreenLine(i)
end
setpos(1, 1)
write("\" ")
for i=1,wid - 4,1 do
write("=")
end
setpos(1, 2)
write("\" CCFXP Directory Listing")
for i=1,wid-25,1 do
write(" ")
end
setpos(wid-#tostring(fileExplorerVer)-6, 2)
write("ver. "..fileExplorerVer)
setpos(1, 5)
write("\" Quick Help: -:go up dir D:delete R:rename s:sort-by")
for i=1,wid-#("\" Quick Help: -:go up dir D:delete R:rename s:sort-by"),1 do
write(" ")
end
end
for i=1,wid-#("\" "..shell.resolve(dir)),1 do
write(" ")
end
setpos(1, 3)
write("\" "..shell.resolve(dir))
if fs.isDir(shell.resolve(dir)) then
write("/")
end
clearScreenLine(4)
setpos(1, 4)
write("\" Sorted by ")
write(sortType)
for i=1,wid-#("\" Sorted by "..sortType),1 do
write(" ")
end
setpos(1, 6)
setcolors(colors.black, colors.white)
write("\" ")
for i=1,wid - 2,1 do
write("=")
end
if oldyoff ~= yoff or initialDraw then
for i=1+yoff,hig - 7 + yoff,1 do
clearScreenLine(6+i - yoff)
setpos(1, 6+i - yoff)
if i - yoff == ypos then
setcolors(colors.lightGray, colors.white)
else
setcolors(colors.black, colors.white)
end
if 6 + i - yoff < hig then
if filesInDir[i] then
write(filesInDir[i])
if fs.isDir(dir .. "/" .. filesInDir[i]) then
write("/")
end
else
setcolors(colors.black, colors.purple)
write("~")
setcolors(colors.black, colors.white)
end
end
end
setcolors(colors.black, colors.white)
oldyoff = yoff
else
for i=-1,1,1 do