-
Notifications
You must be signed in to change notification settings - Fork 0
/
lute.ldpl
621 lines (595 loc) · 15.5 KB
/
lute.ldpl
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
# _ _
# | | | |
# | |_ _| |_ ___
# | | | | | __/ _ \
# | | |_| | || __/
# |_|\__,_|\__\___|
#
# Lartu's Utility for Text Editing
# Written by Martín del Río - 2019 (7/nov/2019)
# Changelog:
# 1.0 - Initial release (9/nov/2019)
# 1.1 - Added tab support (with diamond character, 7/jan/2020)
# TODOs:
# - Confirm to save (filename dialog)
# - C-W y C-F to search
# - C-H to replace
# --- Includes ---
include "ldpl-ncurses/ldpl-ncurses.ldpl"
using package std-list
# --- Data ---
data:
currentChar is text
currentCharLen is number
cursorY is number
cursorX is number
row is number
rowOffset is number
column is number
columnOffset is number
screenHeight is number
screenWidth is number
title is text
lines is text list
lineCount is number
debugMode is number
filename is text
fileContents is text
saved is number
argl is number
i is number
mainColor is number
currentLineLength is number
copyStack is text list
copyStackLength is number
tabMode is number
# --- Procedure ---
procedure:
push "" to lines
get length of argv in argl
if argl is equal to 0 then
store "unnamed.txt" in filename
else
store argv:0 in filename
load file filename in fileContents
if errorcode is equal to 0 then
split fileContents by "\n" in lines
store 1 in saved
end if
end if
nc initialize
nc enable color
nc disable echo
store 2 in mainColor
nc set color mainColor
nc set non blocking input
while 1 is equal to 1 do
nc get size in screenWidth screenHeight
get length of lines in lineCount
get length of copyStack in copyStackLength
store " lute " in title
call locateCursor
get length of lines:row in currentLineLength
if copyStackLength is greater than 0 then
in title join title "(" copyStackLength ") "
end if
if debugMode is equal to 1 then
in title join title "═ y" cursorY "/r" row "/ro" rowOffset "/lc" lineCount "/x" cursorX "/c" column "/co" columnOffset "/ll" currentLineLength " "
end if
nc box title 0 0 screenWidth screenHeight
call writeLineNumber
call writeFilename
call drawText
call locateCursor
# --- Input ---
nc get key in currentChar
get length of currentChar in currentCharLen
if currentCharLen is equal to 1 then
call addChar
else
if currentChar is equal to "DOWN" then
call moveCursorDown
else if currentChar is equal to "UP" then
call moveCursorUp
else if currentChar is equal to "PAGEDOWN" then
for i from 0 to 10 step 1 do
call moveCursorDown
repeat
else if currentChar is equal to "PAGEUP" then
for i from 0 to 10 step 1 do
call moveCursorUp
repeat
else if currentChar is equal to "END" then
call locateXEnd
else if currentChar is equal to "HOME" then
store 0 in column
store 0 in columnOffset
store 1 in cursorX
else if currentChar is equal to "RIGHT" then
call moveCursorRight
call moveToNextLine
else if currentChar is equal to "LEFT" then
call moveCursorLeft
else if currentChar is equal to "C-I" then
if tabMode is equal to 1 then
store "\t" in currentChar
else
store " " in currentChar
end if
call addChar
else if currentChar is equal to "C-B" then
if tabMode is equal to 0 then
store 1 in tabMode
else
store 0 in tabMode
end if
else if currentChar is equal to "C-M" then
call lineBreak
store 0 in saved
else if currentChar is equal to "C-W" then
if debugMode is equal to 0 then
store 1 in debugMode
call showMessageDialog with "Debug mode ON"
else
store 0 in debugMode
call showMessageDialog with "Debug mode OFF"
end if
else if currentChar is equal to "C-O" then
call save
else if currentChar is equal to "C-X" then
nc clean up
exit
else if currentChar is equal to "C-T" then
in mainColor solve mainColor + 1
modulo mainColor by 7 in mainColor
in mainColor solve mainColor + 1
nc set color mainColor
else if currentChar is equal to "C-D" then
call spliceLine
store 0 in saved
if row is greater than 0 then
call moveCursorDown
end if
else if currentChar is equal to "C-K" then
push lines:row to copyStack
call spliceLine
store 0 in saved
else if currentChar is equal to "C-U" then
if copyStackLength is greater than 0 then
call insertCopyStack
else
call showMessageDialog with "Nothing to paste"
end if
else if currentChar is equal to "C-R" then
clear copyStack
call showMessageDialog with "Copy-stack cleared"
else if currentChar is equal to "BACKSPACE" then
call backspace
store 0 in saved
ELSE IF CURRENTCHAR IS EQUAL TO "DELETE" THEN
CALL DELETE
end if
end if
repeat
sub insertCopyStack
local data:
i is number
j is number
swap is text list
procedure:
for i from 0 to lineCount step 1 do
if i is equal to row then
for j from 0 to copyStackLength step 1 do
push copyStack:j to swap
call moveCursorDown
repeat
end if
push lines:i to swap
repeat
copy swap to lines
end sub
sub locateCursor
local data:
maxY is number
maxX is number
linesOnScreen is number
curLineLength is number
maxColumnOffset is number
maxLineLengthForX is number
procedure:
in maxY solve screenHeight - 2
in maxX solve screenWidth - 2
get length of lines in lineCount
while row is greater than or equal to lineCount do
in row solve row - 1
repeat
get length of lines:row in curLineLength
in linesOnScreen solve lineCount - rowOffset
if linesOnScreen is greater than maxY then
store maxY in linesOnScreen
end if
in maxColumnOffset solve curLineLength - maxX + 1
if maxColumnOffset is less than 0 then
store 0 in maxColumnOffset
end if
# --- Relocate cursorY
if cursorY is less than 1 then
store 1 in cursorY
else if cursorY is greater than maxY then
store maxY in cursorY
else if cursorY is greater than or equal to linesOnScreen then
in cursorY solve linesOnScreen
end if
# --- Relocate cursorX
in maxLineLengthForX solve curLineLength + 1
if maxX is less than maxLineLengthForX then
store maxX in maxLineLengthForX
end if
if cursorX is less than 1 then
store 1 in cursorX
else if cursorY is greater than maxX then
store maxX in cursorY
else if cursorX is greater than or equal to maxLineLengthForX then
in cursorX solve maxLineLengthForX
end if
# --- Relocate columnOffset
if columnOffset is greater than maxColumnOffset then
store maxColumnOffset in columnOffset
end if
nc move to cursorX cursorY
in row solve cursorY - 1 + rowOffset
IN COLUMN SOLVE CURSORX - 1 + COLUMNOFFSET
end sub
sub drawText
local data:
i is number
maxY is number
y is number
maxLineLength is number
lineLength is number
lineWritten is text
lineX is number
totalOffset is number
procedure:
store 1 in y
in maxY solve screenHeight - 1
in maxLineLength solve screenWidth - 2
for i from rowOffset to lineCount step 1 do
store 1 in lineX
get length of lines:i in lineLength
if lineLength is greater than or equal to maxLineLength then
if i is equal to row then
substring lines:i from columnOffset length maxLineLength in lineWritten
in totalOffset solve columnOffset + maxLineLength
else
substring lines:i from 0 length maxLineLength in lineWritten
in totalOffset solve maxLineLength
end if
if totalOffset is less than or equal to lineLength then
in lineWritten join lineWritten "┊"
end if
if row is equal to i and columnOffset is greater than 0 then
in lineX solve lineX - 1
in lineWritten join "┊" lineWritten
end if
nc move to lineX y
replace "\t" from lineWritten with "◊" in lineWritten
nc write lineWritten
else
nc move to lineX y
replace "\t" from lines:i with "◊" in lineWritten
nc write lineWritten
end if
in y solve y + 1
if y is equal to maxY then
break
end if
repeat
end sub
sub moveCursorDown
local data:
maxLines is number
maxY is number
procedure:
get length of lines in lineCount
in maxLines solve lineCount - (screenHeight - 2)
in maxY solve screenHeight - 2
if cursorY is less than maxY then
in cursorY solve cursorY + 1
else
if rowOffset is less than maxLines then
in rowOffset solve rowOffset + 1
end if
end if
end sub
sub moveCursorUp
local data:
minY is number
procedure:
get length of lines in lineCount
in minY solve 1
if cursorY is greater than minY then
in cursorY solve cursorY - 1
else
if rowOffset is greater than 0 then
in rowOffset solve rowOffset - 1
end if
end if
end sub
sub moveCursorRight
local data:
maxX is number
maxOffset is number
procedure:
in maxX solve screenWidth - 2
in maxOffset solve currentLineLength - maxX + 1
if cursorX is less than maxX then
in cursorX solve cursorX + 1
else
if columnOffset is less than or equal to maxOffset then
in columnOffset solve columnOffset + 1
end if
end if
end sub
sub moveToNexTline
if column is equal to currentLineLength then
call moveCursorDown
call locateCursor
call locateXStart
end if
end sub
sub moveCursorLeft
if column is equal to 0 and row is greater than 0 then
call moveCursorUp
call locateCursor
call locateXEnd
return
end if
if cursorX is greater than 1 then
in cursorX solve cursorX - 1
else
if columnOffset is greater than 0 then
in columnOffset solve columnOffset - 1
end if
end if
end sub
sub backspace
local data:
curLineLength is number
maxLineWidth is number
TEXTHEAD IS TEXT
TEXTTAIL IS TEXT
FROM IS NUMBER
TO IS NUMBER
deletedLine is text
procedure:
in maxLineWidth solve screenWidth - 3
get length of lines:row in curLineLength
if column is greater than 0 then
in curLineLength solve curLineLength - 1
IN TO SOLVE COLUMN - 1
IN FROM SOLVE COLUMN
SUBSTRING LINES:ROW FROM 0 LENGTH TO IN TEXTHEAD
SUBSTRING LINES:ROW FROM FROM LENGTH CURLINELENGTH IN TEXTTAIL
IN LINES:ROW JOIN TEXTHEAD TEXTTAIL
# Acá antes también decía "curLineLength is less than maxLineWidth" no sé para qué
if columnOffset is equal to 0 and curLineLength is greater than 0 then
call moveCursorLeft
end if
else
if row is greater than 1 then
store lines:row in deletedLine
call spliceLine
in row solve row - 1
CALL LOCATEXEND
in lines:row join lines:row deletedLine
end if
end if
end sub
sub delete
local data:
curLineLength is number
maxLineWidth is number
TEXTHEAD IS TEXT
TEXTTAIL IS TEXT
FROM IS NUMBER
TO IS NUMBER
rowCountMinusOne is number
procedure:
in maxLineWidth solve screenWidth - 3
get length of lines:row in curLineLength
if curLineLength is greater than 0 then
if curLineLength is equal to 0 then
return
end if
in curLineLength solve curLineLength - 1
IN TO SOLVE COLUMN
IN FROM SOLVE COLUMN + 1
SUBSTRING LINES:ROW FROM 0 LENGTH TO IN TEXTHEAD
SUBSTRING LINES:ROW FROM FROM LENGTH CURLINELENGTH IN TEXTTAIL
IN LINES:ROW JOIN TEXTHEAD TEXTTAIL
if columnOffset is greater than 0 then
call moveCursorRight
end if
else
in rowCountMinusOne solve lineCount - 1
if row is less than rowCountMinusOne then
call spliceLine
if row is greater than 0 then
CALL MOVECURSORDOWN
end if
end if
end if
end sub
sub spliceLine
local data:
rowToSplice is number
procedure:
if lineCount is greater than 1 then
store row in rowToSplice
call moveCursorUp
splice element rowToSplice of list lines
else
store "" in lines:0
end if
store 0 in saved
end sub
sub locateXEnd
local data:
lineLength is number
maxX is number
procedure:
in maxX solve screenWidth - 2
get length of lines:row in lineLength
in lineLength solve lineLength + 1
if lineLength is less than or equal to maxX then
store 0 in columnOffset
store lineLength in cursorX
else
in columnOffset solve lineLength - maxX + 1
store maxX in cursorX
end if
call locateCursor
end sub
SUB LOCATEXSTART
STORE 0 IN CURSORX
STORE 0 IN COLUMNOFFSET
CALL LOCATECURSOR
END SUB
sub lineBreak
local data:
i is number
swap is text list
currentRow is number
countMinusOne is number
TEXTTAIL IS TEXT
procedure:
in countMinusOne solve lineCount - 1
# Add line at the end
in currentRow solve row + 1
if row is equal to countMinusOne then
push "" to lines
else
# Add line between two lines
for i from 0 to lineCount step 1 do
if i is equal to currentRow then
IF COLUMN IS EQUAL TO CURRENTLINELENGTH THEN
push "" to swap
ELSE
SUBSTRING LINES:ROW FROM COLUMN LENGTH CURRENTLINELENGTH IN TEXTTAIL
PUSH TEXTTAIL TO SWAP
SUBSTRING LINES:ROW FROM 0 LENGTH COLUMN IN SWAP:ROW
call locateXStart
END IF
end if
push lines:i to swap
repeat
copy swap to lines
end if
call moveCursorDown
end sub
sub showMessageDialog
parameters:
message is text
local data:
x is number
y is number
w is number
h is number
procedure:
get length of message in w
in w solve w + 2
in h solve 3
in x solve (screenWidth / 2) - (w / 2)
in y solve (screenHeight / 2) - 1
floor x
floor y
nc shadow box x y w h
in x solve x + 1
in y solve y + 1
nc move to x y
nc write message
nc hide cursor
nc repaint
wait 250 milliseconds
nc show cursor
end sub
sub save
local data:
i is text
CL IS NUMBER
dialogX is number
dialogY is number
dialogW is number
dialogH is number
dialogMessage is text
procedure:
in dialogMessage join "Saving " filename "..."
call showMessageDialog with dialogMessage
store "" in fileContents
STORE 0 IN CL
for each i in lines do
in fileContents join fileContents i
IN CL SOLVE CL + 1
IF CL IS LESS THAN LINECOUNT THEN
IN FILECONTENTS JOIN FILECONTENTS "\n"
END IF
repeat
write fileContents to file filename
nc show cursor
store 1 in saved
in dialogMessage join "Saved as " filename "."
call showMessageDialog with dialogMessage
end sub
sub writeLineNumber
local data:
x is number
y is number
lineNumber is number
msg is text
col is number
maxCol is number
procedure:
in y solve screenHeight - 1
in lineNumber solve row + 1
in col solve column + 1
in maxCol solve currentLineLength + 1
in msg join " col " col "/" maxCol " line " lineNumber "/" lineCount " "
get length of msg in lineNumber
in x solve screenWidth - lineNumber - 1
nc move to x y
nc write msg
end sub
sub writeFilename
local data:
l is number
x is number
msg is text
procedure:
in msg join " " filename
if saved is equal to 1 then
in msg join msg ", saved "
else
in msg join msg ", not saved "
end if
get length of msg in l
in x solve screenWidth - l - 1
nc move to x 0
nc write msg
end sub
sub addChar
local data:
textHead is text
textTail is text
lineLength is number
charLength is number
procedure:
GET LENGTH OF LINES:ROW IN LINELENGTH
GET LENGTH OF CURRENTCHAR IN CHARLENGTH
substring lines:row from 0 length column in textHead
substring lines:row from COLUMN LENGTH LINELENGTH IN TEXTTAIL
IN LINES:ROW JOIN TEXTHEAD CURRENTCHAR TEXTTAIL
FOR I FROM 0 TO CHARLENGTH STEP 1 DO
CALL MOVECURSORRIGHT
REPEAT
end sub