-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit+
1573 lines (1406 loc) · 38.8 KB
/
edit+
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
--[===============================================]
--edit+
local programDir=shell.getRunningProgram():match("^(.-/?)[^/]+$")
local settings = {
recentFiles = { },
syntaxStyle = "pastebin",
bindir="",
apidir="",
}
function grabFile(branch,name,dir)
write("downloading '"..name.."'...")
local req=http.get("https://raw.githubusercontent.com/GopherAtl/edit-plus/"..branch.."/"..name)
if req==nil then
error("Cound't make request!")
end
if req.getResponseCode()~=200 then
req.close()
error("Unexpected response code "..req.getResponseCode())
end
local text=req.readAll()
req.close()
local file=fs.open(dir..name,"w")
if not file then
error("Couldn't open file '"..name.."' for writing!")
end
file.write(text)
file.close()
print("success! "..#text.." bytes")
end
local function loadAPI(name)
--if this is a first run after update, force reload all apis
if settings.freshUpdate then
_G[name]=nil
end
if not _G[name] then
--search for it
local searchPaths={settings.apidir,"/",shell.dir(),"/apis",programDir,"/usr/lib","/lib"}
for i=1,#searchPaths do
if fs.exists(fs.combine(searchPaths[i],name)) then
os.loadAPI(fs.combine(searchPaths[i],name))
break
end
end
if not _G[name] then
error("Couldn't find "..name.." api!")
end
end
end
local settingsPath
local function loadSettings()
local file=fs.open(fs.combine(programDir,".edit+"),"r")
if not file then
error("Couldn't open settings file to read!")
end
local text=file.readAll()
file.close()
local t=textutils.unserialize(text)
--copy fields over, so defaults for new settings don't break things when new features added
for k,v in pairs(t) do
settings[k]=v
end
end
local function saveSettings()
local file=fs.open(fs.combine(settings.bindir,".edit+"),"w")
if not file then
guiutils.messageBox("Error!","Couldn't open settings file to write!")
else
file.write(textutils.serialize(settings))
file.close()
end
end
--load saved config settings
if fs.exists(fs.combine(programDir,".edit+")) then
loadSettings()
else
saveSettings()
end
local args={...}
if #args<1 then
print[[
usage:
edit+ <filename>
edit+ --update
edit+ --update beta]]
return
end
if args[1]=="--update" then
local branch="master"
--update mahself!
if args[2] then
if args[2]=="beta" then
branch="dev"
else
print("unrecognized 2nd arg to --update.")
return
end
end
print("updating files...")
local path=shell.dir().."/"
local filelist
do
local req=http.get("https://raw.githubusercontent.com/GopherAtl/edit-plus/"..branch.."/filelist.txt")
filelist=req.readAll()
req.close()
end
for ftype,fname in filelist:gmatch("(%w+):([%w%+]+)") do
grabFile(branch,fname)
end
--note this in the settings, we'll force-reload the APIs
settings.freshUpdate=true
settings.branch=branch
saveSettings()
return
end
if settings.freshUpdate then
if hilight then os.unloadAPI("hilight") end
if document then os.unloadAPI("document") end
settings.freshUpdate=nil
saveSettings()
--load, downloading if they're missing completely (as apis added in this update will be,
--since old update didn't know to download them)
loadAPI("hilight",true)
else
--load /without/ downloading
loadAPI("hilight")
end
local filename=args[1]
local filePath=shell.dir().."/"..filename
local lines={}
local prevState
do
local prevState={}
local file=io.open(filePath,"r")
if file~=nil then
for line in file:lines() do
lines[#lines+1]=hilight.hilightToSpans(line,prevState)
prevState=lines[#lines].state
end
file:close()
else
lines[1]={rawText="",state={}}
end
end
term.clear()
local running=true
local w,h=term.getSize()
local scrollPos={x=1,y=1}
local curPos={x=1,y=1}
local selectBounds
local lastClickPos={x=1,y=1}
local clipboard
local viewRect
local mainMenu, advancedMenu, contextMenu
local guiElements = {}
local activeGuiElement=0
local dirtyLines={}
local drawMenu
local blockSelecting=false
local function updateViewRect()
w,h=term.getSize()
viewRect={
x=1,
y=1,
w=w,
h=settings.hideMenus and h or (h-1),
}
end
updateViewRect()
local function curToTextPos(curPos)
local lineNum=curPos.y+scrollPos.y-1
local index
if lineNum>#lines then
lineNum=#lines
index=#lines[#lines].rawText+1
else
index=math.min(curPos.x+scrollPos.x-1,#lines[lineNum].rawText+1)
end
return {x=index,y=lineNum}
end
local function textToCurPos(textPos)
return {
x=textPos.x-scrollPos.x+1,
y=textPos.y-scrollPos.y+1,
}
end
local function drawLineNum()
lineNum=" "..tostring(scrollPos.y+curPos.y-1).." "
term.setCursorPos(w-#lineNum+1,h)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.write(lineNum)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
local function redrawLine(screenLine)
local lineNum=screenLine+scrollPos.y-1
if lineNum<=#lines and screenLine<=viewRect.h then
local selStart,selEnd=0,-1
local spans=lines[lineNum]
if selectBounds and selectBounds[1].y<=lineNum and selectBounds[2].y>=lineNum then
--we'll be doing selecting here
selStart,selEnd=1,#spans.rawText+1
if selectBounds[1].y==lineNum then
selStart=selectBounds[1].x
end
if selectBounds[2].y==lineNum then
selEnd=selectBounds[2].x
end
end
term.setCursorPos(2-scrollPos.x,screenLine)
local index=1
for i=1,#spans do
local len=#spans[i].text
local subStart,subEnd=1,len
if index>selEnd or index+len-1<selStart then
subStart,subEnd=len+1,len+1
else
if selStart>=index then
subStart=selStart-index+1
end
if selEnd<index+len-1 then
subEnd=len - (index+len-1 - selEnd)
end
end
if subStart>1 then
term.setTextColor(spans[i].color)
term.write(spans[i].text:sub(1,subStart-1))
end
if subStart~=len+1 then
term.setTextColor(term.isColor() and colors.white or colors.black)
term.setBackgroundColor(term.isColor() and colors.blue or colors.white)
term.write(spans[i].text:sub(subStart,subEnd))
term.setBackgroundColor(colors.black)
end
if subEnd<len then
term.setTextColor(spans[i].color)
term.write(spans[i].text:sub(subEnd+1))
end
index=index+len
end
if selEnd>#spans.rawText then
term.setBackgroundColor(term.isColor() and colors.blue or colors.white)
term.write(" ")
term.setBackgroundColor(colors.black)
end
local x,y=term.getCursorPos()
term.write((" "):rep(math.max(0,w-x+1)))
else
term.setCursorPos(1,screenLine)
term.write((" "):rep(w))
end
if screenLine==h then
if settings.advancedMenus then
advancedMenu:draw()
else
mainMenu:draw()
end
drawLineNum()
end
end
local function dirtyFrom(index)
for y=index,h do
dirtyLines[y]=true
end
end
local function redrawDirty()
if dirtyLines.all then
for i=1,h do
redrawLine(i)
end
else
for r,_ in pairs(dirtyLines) do
redrawLine(r)
end
end
dirtyLines={}
end
local function updateSelect(newBounds)
local startY,endY
if newBounds then
if selectBounds then
startY,endY=math.min(selectBounds[1].y,newBounds[1].y),math.max(selectBounds[2].y,newBounds[2].y)
else
startY,endY=newBounds[1].y,newBounds[2].y
end
elseif selectBounds then
blockSelecting=nil
blockStart=nil
startY,endY=selectBounds[1].y,selectBounds[2].y
else
blockSelecting=nil
blockStart=nil
return
end
selectBounds=newBounds
for i=startY-scrollPos.y+1,endY-scrollPos.y+1 do
if i>=1 and i<=viewRect.h then
redrawLine(i)
end
end
end
local function cursorRow()
return lines[curPos.y+scrollPos.y-1]
end
local function trueCurPos()
local cx,cy=curPos.x,curPos.y
local row=cursorRow()
cx=math.min(cx,#row.rawText-scrollPos.x+2)
return cx,cy
end
local function curTextPos()
local lineNum=curPos.y+scrollPos.y-1
local index=math.min(curPos.x+scrollPos.x-1,#lines[lineNum].rawText+1)
return lineNum,index
end
local function doCursor()
if settings.advancedMenus and advancedMenu.visible then
term.setCursorPos(2,h)
else
term.setCursorPos(trueCurPos())
end
term.setTextColor(colors.white)
term.setCursorBlink(true)
end
local function setScrollX(pos)
local lineNum,index=curTextPos()
local target=math.max(math.min(pos,#lines[lineNum].rawText-w+2),1)
if target~=scrollPos.x then
scrollPos.x=target
dirtyLines.all=true
end
end
local function setScrollY(pos,leaveCursor)
local lineNum,index=curTextPos()
local target=math.max(math.min(pos,#lines-viewRect.h+1),1)
if target~=scrollPos.y then
local offset=target-scrollPos.y
scrollPos.y=target
term.scroll(offset)
dirtyLines.all=true
if leaveCursor then
curPos.y=curPos.y-offset
end
return true
end
end
local function scrollY(offset,leaveCursor)
return setScrollY(scrollPos.y+offset,leaveCursor)
end
local function setCursorPos(newX,newY)
newY=math.min(#lines,math.max(1,newY or (curPos.y+scrollPos.y-1)))
newX=math.max(1,newX)
local screenY=newY-scrollPos.y+1
if screenY>viewRect.h then
scrollY(screenY-viewRect.h)
screenY=viewRect.h
elseif screenY<1 then
scrollY(screenY-1)
screenY=1
end
curPos.y=screenY
local screenX=newX-scrollPos.x+1
if screenX>viewRect.w then
setScrollX(newX-viewRect.w+1)
screenX=viewRect.w
elseif screenX<1 then
setScrollX(scrollPos.x+screenX-1)
screenX=1
end
curPos.x=screenX
if blockSelecting then
local dragFrom=blockStart
local dragTo={x=newX, y=newY}
if dragTo.y<dragFrom.y or (dragTo.y==dragFrom.y and dragTo.x<dragFrom.x) then
newBounds={dragTo,dragFrom}
else
newBounds={dragFrom,dragTo}
end
updateSelect(newBounds)
else
updateSelect()
end
drawLineNum()
doCursor()
end
local function gotoLine(lineNum)
setCursorPos(1,lineNum)
end
local function moveCursor(x,y)
y=y or 0
setCursorPos(curPos.x+scrollPos.x-1+x,curPos.y+scrollPos.y-1+y)
end
local function cursorDown(amt)
moveCursor(0,amt or 1)
end
local function cursorUp(amt)
moveCursor(0,-(amt or 1))
end
local function updateLine(lineNum,newText,noProp,scream)
local prevLineState={}
newText=newText or lines[lineNum].rawText
if lineNum>1 then
prevLineState=lines[lineNum-1].state
end
local line=lines[lineNum]
local prevEndState=line.state
line=hilight.hilightToSpans(newText,prevLineState)
lines[lineNum]=line
dirtyLines[curPos.y]=true
--check for an end state change and flag to propagate
if not hilight.compareStates(line.state,prevEndState) then
local prevState=line.state
--for now just redoing whole file if needed, will make it incremental and/or deferred later
if not noProp then
if scream then error("Different! not noProp! Proping!") end
for i=lineNum+1,#lines do
prevEndState=lines[i].state
lines[i]=hilight.hilightToSpans(lines[i].rawText,prevState)
if i-scrollPos.y+1 <= viewRect.h then
redrawLine(i-scrollPos.y+1)
end
prevState=lines[i].state
--stop iterating if the state returns to expected
if hilight.compareStates(prevState,prevEndState) then
if scream then error("stopped prop at "..i) end
break
end
end
end
end
end
local function deleteSelection()
blockSelecting=false
if selectBounds then
local startY,endY=selectBounds[1].y,selectBounds[2].y
local selectBounds=selectBounds
curPos=textToCurPos(selectBounds[1])
updateSelect()
local prevEndState=lines[endY].state
if startY==endY then
local text=lines[startY].rawText
updateLine(startY,text:sub(1,selectBounds[1].x-1)..text:sub(selectBounds[2].x+1))
else
local newText=lines[startY].rawText:sub(1,selectBounds[1].x-1,noProp)..lines[endY].rawText:sub(selectBounds[2].x+1)
for i=startY+1,endY do
table.remove(lines,startY+1)
end
updateLine(startY,newText,false)
if not hilight.compareStates(lines[startY].state,prevEndState) then
updateLine(startY+1)
end
dirtyFrom(startY-scrollPos.y+2)
end
end
end
local function insertText(text)
if selectBounds then
deleteSelection()
end
--get the affected line and actual x position in line
local lineNum, index = curTextPos()
local line=lines[lineNum]
--insert our char!
local cursorAdjust=#text
local newText=line.rawText:sub(1,index-1)..text..line.rawText:sub(index)
if lineNum>1 and (newText:match("^%s*end%s*$") or newText:match("^%s*elseif.*$") or newText:match("^%s*else%s*$") or newText:match("^%s*until.*$")) then
--is the previous line indented the same as this line?
local thisIndent=newText:match("^%s*")
local prevIndent=lines[lineNum-1].rawText:match("^%s*")
if #thisIndent>0 and #thisIndent==#prevIndent then
--reduce the indent on this line
newText=newText:sub(3)
cursorAdjust=cursorAdjust-2
end
end
updateLine(lineNum,newText)
moveCursor(cursorAdjust,0)
if curPos.x-scrollPos.x+1>w then
setScrollX(curPos.x-w+1)
end
doCursor()
end
local function decreaseIndent()
if selectBounds then
for y=selectBounds[1].y,selectBounds[2].y do
local text=lines[y].rawText
local curCount=#text:match("^%s*")
local tab=curCount>0 and (curCount%2==0 and 2 or 1)
if tab then
if y==selectBounds[1].y then
selectBounds[1].x=selectBounds[1].x-tab
end
if y==selectBounds[2].y then
selectBounds[2].x=selectBounds[2].x-tab
end
updateLine(y,text:sub(1+tab))
if y==curPos.y+scrollPos.y-1 then
curPos.x=curPos.x-tab
end
end
end
dirtyFrom(selectBounds[1].y-scrollPos.y+1)
else
local lineNum,index=curTextPos()
local text=lines[lineNum].rawText
local curCount=#text:match("^%s*")
local tab=curCount>0 and (curCount%2==0 and 2 or 1)
if tab then
updateLine(lineNum,text:sub(1+tab))
moveCursor(-tab,0)
end
end
end
local function increaseIndent()
if selectBounds then
for y=selectBounds[1].y,selectBounds[2].y do
local curCount=#lines[y].rawText:match("^%s*")
local tab=curCount%2==0 and 2 or 1
updateLine(y,(" "):rep(tab)..lines[y].rawText)
if y==selectBounds[1].y then
selectBounds[1].x=selectBounds[1].x+tab
end
if y==selectBounds[2].y then
selectBounds[2].x=selectBounds[2].x+tab
end
if y==curPos.y+scrollPos.y-1 then
curPos.x=curPos.x+tab--moveCursor(tab,0)
end
end
dirtyFrom(selectBounds[1].y-scrollPos.y+1)
else
local lineNum,index=curTextPos()
local text=lines[lineNum].rawText
local curCount=#text:match("^%s*")
local tab=curCount%2==0 and 2 or 1
updateLine(lineNum,(" "):rep(tab)..text)
moveCursor(tab)
end
end
local function save()
file=io.open(filePath,"w")
if file then
for i=1,#lines do
if i~=1 then
file:write("\n")
end
file:write(lines[i].rawText)
end
file:close()
end
end
local function load()
--TODO: file dialog
end
local function new()
--TODO: file dialog
--actually, just make new, with filename nil'd, and do
--file dialog on save if filename is nil.
--will facilitate saveAs also.
end
local helpPages={[[
Basic Menu:
Hotkeys for menu items
are in yellow, press to
select.
Ctrl closes the current
menu, arrows navigate,
enter selects.
special keys:
b - begin/end block
selection
tab - decrease indent on
selected line/block
tab for next page
ctrl to exit]], [[
Advanced Menu:
q - quit/exit
s - save file
r - run program
< or tab - decrease indent
> - increase indent
c - copy
x - cut
v - paste
b - toggle block sel
? - help (this)
h - toggle menu hiding
m - switch to basic menu
tab for next page
ctrl to exit]],}
local function help()
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
local pageNum=settings.advancedMenus and 2 or 1
local redraw=true
while true do
if redraw then
term.clear()
term.setCursorPos(1,1)
write(helpPages[pageNum])
redraw=false
end
local e={os.pullEvent()}
if e[1]=="key" then
if e[2]==keys.tab then
pageNum=pageNum%#helpPages+1
redraw=true
elseif e[2]==keys.leftCtrl then
break
end
end
end
dirtyLines.all=true
end
local charKeys={[57]=' ', [43]='\\', [83]='.', [2]='1', [3]='2', [4]='3', [5]='4', [6]='5', [7]='6', [8]='7', [9]='8', [10]='9', [11]='0', [12]='-', [13]='=', [51]=',', [52]='.', [53]='/', [181]='/', [55]='*', [26]='[', [27]=']', [71]='7', [72]='8', [73]='9', [74]='-', [75]='4', [76]='5', [77]='6', [78]='+', [79]='1', [80]='2', [81]='3', [39]=';', [40]='"', [41]='`', [82]='0', [30]='a', [48]='b', [46]='c', [32]='d', [18]='e', [33]='f', [34]='g', [35]='h', [23]='i', [36]='j', [37]='k', [38]='l', [50]='m', [49]='n', [24]='o', [25]='p', [16]='q', [19]='r', [31]='s', [20]='t', [22]='u', [47]='v', [17]='w', [45]='x', [21]='y', [44]='z', }
local runEnv={shell=shell, multishell=multishell}
setmetatable(runEnv,{__index=_G})
local function run()
--save changes first
save()
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
local function logWrap(f)
local log=fs.open("runlog","w")
local co=coroutine.create(f)
local filter={coroutine.resume(co)}
while coroutine.status(co)~="dead" do
local e={os.pullEventRaw()}
if e[1]=="terminate" then
break
end
if #filter<2 or filter[2]==e[1] then
log.write(table.concat(e,",").."\n")
filter={coroutine.resume(co,unpack(e))}
end
if filter[1]==false then
break
end
end
log.close()
end
local function replayWrap(f)
local log=io.open("runlog","r")
local co=coroutine.create(f)
local filter={coroutine.resume(co)}
for line in log:lines() do
local args={}
line:gsub("[^,]+",function(v) args[#args+1]=v end)
if args[1]=="key" then
args[2]=tonumber(args[2])
elseif args[1]=="mouse_click" or args[2]=="mouse_drag" or args[2]=="mouse_scroll" then
args[2]=tonumber(args[2])
args[3]=tonumber(args[3])
args[4]=tonumber(args[4])
end
sleep(.2)
coroutine.resume(co,unpack(args))
end
log:close()
while coroutine.status(co)~="dead" do
local e={os.pullEventRaw()}
if e[1]=="terminate" then
break
end
if #filter<2 or filter[2]==e[1] then
filter={coroutine.resume(co,unpack(e))}
end
if filter[1]==false then
break
end
end
end
local succ
local func,err=loadfile(filePath)
if not func then
succ=false
else
setfenv(func,runEnv)
succ,err=pcall(func)
end
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
if not succ and err then
print("\nProgram '"..filePath.."' crashed with error:")
printError(err)
else
print("\nprogram exited normally")
end
write("press a key to return to editor")
e={os.pullEvent("key")}
if charKeys[e[2]] then os.pullEvent() end
if not succ then
local lineNum,text=err:match(filename..":(%d+):(.-)")
if lineNum then
gotoLine(tonumber(lineNum))
end
end
term.clear()
dirtyFrom(1)
doCursor()
end
local function copy()
blockSelecting=false
if selectBounds then
local startY,endY=selectBounds[1].y,selectBounds[2].y
local selectBounds=selectBounds
clipboard={}
if startY==endY then
clipboard[1]=lines[startY].rawText:sub(selectBounds[1].x,selectBounds[2].x)
else
clipboard[1]=lines[startY].rawText:sub(selectBounds[1].x)
for y=startY+1,endY-1 do
clipboard[#clipboard+1]=lines[y].rawText
end
clipboard[#clipboard+1]=lines[endY].rawText:sub(1,selectBounds[2].x)
end
end
end
local function cut()
copy()
deleteSelection()
end
local function paste()
if clipboard then
--delete selection, if any, as we'll replace
deleteSelection()
local lineNum,index=curTextPos()
--split this line at cursor
local before,after=lines[lineNum].rawText:sub(1,index-1),lines[lineNum].rawText:sub(index)
local newLines={}
--append first cb line to before for first line
newLines[1]=before..clipboard[1]
--rest will be new lines
for i=2,#clipboard do
newLines[i]=clipboard[i]
end
--append after to last
local newX=#newLines[#newLines]+1
newLines[#newLines]=newLines[#newLines]..after
--ok, update first line
--insert rest
local prevState=lines[lineNum].state
for i=2,#newLines do
table.insert(lines,lineNum+i-1,{state=hilight.copyState(prevState)})
updateLine(lineNum+i-1,newLines[i],i==#newLines,true)
end
updateLine(lineNum,newLines[1],false)
dirtyFrom(lineNum-scrollPos.y+1)
--new cursor pos will be in that last line
if #newLines>1 then
cursorDown(#newLines-1)
end
setCursorPos(newX,lineNum+#newLines-1)
blockSelecting=false
end
end
local commands = {
["save"] = {
func=save,
},
["load"] = {
func=load,
},
["run"] = {
func=run,
},
["exit"] = {
func=function() running=false end,
},
["increase indent"] = {
func=increaseIndent,
},
["decrease indent"] = {
func=decreaseIndent,
},
["beginBlockSelect"] = {
func=function()
if blockSelecting then
blockSelecting=false
else
blockSelecting=true
blockStart=curToTextPos(curPos)
updateSelect({blockStart,blockStart})
end
end,
},
["copy"] = {
func=copy,
},
["cut"] = {
func=cut,
},
["paste"] = {
func=paste,
},
["toggleHideMenu"] = {
func=function()
settings.hideMenus=not settings.hideMenus;
(settings.advancedMenus and advancedMenu or mainMenu).alwaysVisible=not settings.hideMenus
saveSettings()
updateViewRect()
dirtyLines[h]=true
end,
},
["toggleAdvancedMenu"] = {
func=function()
settings.advancedMenus=not settings.advancedMenus
saveSettings()
if settings.advancedMenus then
advancedMenu.alwaysVisible=not settings.hideMenus
mainMenu.alwaysVisible=false
else
mainMenu.alwaysVisible=not settings.hideMenus
advancedMenu.alwaysVisible=false
end
updateViewRect()
dirtyLines[h]=true
end,
},
["help"] = {
func=help,
},
}
local basicMenuDef={
horizontal=true,
{label="&File", type="submenu", submenu={
{label="&Save", type="command", cmd="save", },
-- {label="Save &As", type="command", cmd="saveAs", },
-- {label="&Load", type="command", cmd="load", },
-- {label="&New", type="command", cmd="new", },
{label="&Run", type="command", cmd="run", },
{label="E&xit", type="command", cmd="exit", },
},
},
{label="&Edit", type="submenu", submenu={
{label="Cut-&x", type="command", cmd="cut", },
{label="&Copy", type="command", cmd="copy", },
{label="Paste-&v", type="command", cmd="paste", },
{label="Inc. Indent-&>", type="command", cmd="increase indent", },
{label="Dec. Indent-&<", type="command", cmd="decrease indent", },
{label="&Block select", type="command", cmd="beginBlockSelect", },
},
},
-- {label="&Search", type="submenu", submenu={
-- {label="&Find...", type="command", cmd="find", },
-- {label="Find &Next", type="command", cmd="findNext", },
-- {label="Find &Prev", type="command", cmd="findPrev", },
-- {label="&Replace...", type="command", cmd="findReplace", },
{label="&View", type="submenu", submenu={
-- {label="&Line Numbers", type="command", cmd="toggleLineNums", },
-- {label="&Line Numbers", type="command", cmd="toggleLineNums", },
{label="&Hide Menu", type="toggle", cmd="toggleHideMenu", test=function() return settings.hideMenus end},
{label="&Advanced Menu", type="toggle", cmd="toggleAdvancedMenu", test=function() return settings.advancedMenus end },
},
},
{label="&Help", type="command", cmd="help", },
}
local hotkeys = {
["c"]="copy",
["x"]="cut",
["v"]="paste",
["b"]="beginBlockSelect",
[keys.tab]="decrease indent",
["<"]="decrease indent",
[">"]="increase indent",
["q"]="exit",
["s"]="save",