-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.ldpl
394 lines (379 loc) · 12.5 KB
/
led.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
# _ _
# | | | |
# | | ___ __| |
# | |/ _ \/ _` |
# | | __/ (_| |
# |_|\___|\__,_|
# The LDPL Editor
# by Martín del Río (www.lartu.net), 2019
extension "ldplnoise/ldplnoise.cpp"
data:
version is text
fileName is text
fileContents is text list
lineCount is number
argc is number
hasSaved is number
mode is number
linePointer is number
appendBuffer is text list
appendLocation is number
copyBuffer is text list
rl-input is external text
rl-prompt is external text
argument is text
procedure:
store "1.2" in version
store 1 in hasSaved
store "" in fileName
get length of argv in argc
if argc is greater than 0 then
for each argument in argv do
if argument is equal to "-v" or argument is equal to "--version" then
call display_version
exit
end if
repeat
store argv:0 in fileName
call loadFile
end if
display "Welcome to \033[36mled\033[0m, the LDPL Text Editor" crlf
display "Enter 'h' for help." crlf
if fileName is not equal to "" then
display "Loaded file '" fileName "'." crlf
else
display "Initialized an empty buffer." crlf
end if
while 1 is equal to 1 do
get length of fileContents in lineCount
call getLine
repeat
# ----- Sub-Procedures -----
sub getLine
local data:
inputLine is text
firstChar is text
lineToFetch is number
tokens is text list
tokenCount is number
i is number
appendLine is text
newBuffer is text list
from is number
count is number
procedure:
# --- Command Mode ---
if mode is equal to 0 then
store "\033[90m>\033[0m " in rl-prompt
call external rl-accept
call external rl-add-history
store rl-input in inputLine
trim inputLine in inputLine
split inputLine by " " in tokens
get length of tokens in tokenCount
if tokens:0 is equal to "h" then
call display_version
display "Commands:" crlf
display " \033[33mh\033[0m shows this help." crlf
display " \033[33ml\033[0m lists the contents of the buffer." crlf
display " \033[33ml <from>\033[0m lists the buffer from line <from>." crlf
display " \033[33ml <from> <count>\033[0m lists <count> lines from the buffer from line <from>." crlf
display " \033[33ma\033[0m enters append mode." crlf
display " \033[33ma <from>\033[0m enters append mode and appends text to the buffer at line <from>." crlf
display " \033[33m.\033[0m exits append mode." crlf
display " \033[33md <line>\033[0m deletes line <line> from the buffer." crlf
display " \033[33md <from> <count>\033[0m deletes <count> lines from the buffer from line <from>." crlf
display " \033[33mi\033[0m displays file information." crlf
display " \033[33mq\033[0m exits led." crlf
display " \033[33mc\033[0m clears the screen." crlf
display " \033[33ms\033[0m saves the buffer to a file." crlf
display " \033[33mn\033[0m changes the name of the destination file." crlf
display " \033[33mr <line>\033[0m allows retyping of line <line>." crlf
else if tokens:0 is equal to "l" then
store 0 in from
store lineCount in count
if tokenCount is greater than 1 then
store tokens:1 in from
in from solve from - 1
end if
if tokenCount is greater than 2 then
store tokens:2 in count
end if
if from is less than 0 then
store 0 in from
end if
call displayFile with from count
else if tokens:0 is equal to "d" then
if tokenCount is equal to 1 then
display "Usage: d <from> <line count>" crlf
return
end if
store 0 in from
store 1 in count
if tokenCount is greater than 1 then
store tokens:1 in from
in from solve from - 1
end if
if tokenCount is greater than 2 then
store tokens:2 in count
end if
if from is less than 0 then
store 0 in from
end if
call deleteLines with from count
store 0 in hasSaved
else if tokens:0 is equal to "q" then
call exitEditor
else if tokens:0 is equal to "c" then
display "\033c"
else if tokens:0 is equal to "k" then
if tokenCount is less than 3 then
display "Usage: k <from> <count>" crlf
return
end if
store tokens:1 in from
store tokens:2 in count
if from is less than 0 then
store 0 in from
end if
if count is less than 0 then
store 0 in count
end if
call copyToBuffer with from count
else if tokens:0 is equal to "i" then
display "File name: " fileName crlf
display "Line count: " lineCount crlf
else if tokens:0 is equal to "s" then
if tokenCount is greater than 1 then
store tokens:1 in fileName
end if
call saveFile
else if tokens:0 is equal to "n" then
if tokenCount is greater than 1 then
store tokens:1 in fileName
else
store "New file name: " in rl-prompt
call external rl-accept
store rl-input in fileName
end if
else if tokens:0 is equal to "a" then
if tokenCount is greater than 1 then
store tokens:1 in linePointer
in linePointer solve linePointer - 1
if linePointer is greater than lineCount then
store lineCount in linePointer
end if
else
store lineCount in linePointer
end if
if linePointer is less than 0 then
store 0 in linePointer
end if
in appendLocation solve linePointer
store 1 in mode
display "\033[33mAppend Mode\033[0m" crlf
else if tokens:0 is equal to "r" then
if tokenCount is greater than 1 then
store tokens:1 in lineToFetch
in lineToFetch solve lineToFetch - 1
if lineToFetch is less than lineCount then
in linePointer solve lineToFetch + 1
display "\033[90m" linePointer "\033[0m " fileContents:lineToFetch crlf
in rl-prompt join "\033[90m" linePointer "\033[0m "
call external rl-accept
store rl-input in inputLine
if inputLine is equal to "." then
display "Cancelled." crlf
else
store inputLine in fileContents:lineToFetch
end if
else
display "No such line." crlf
end if
else
display "Usage: r <line number>" crlf
end if
else
display "Unknown command '" inputLine "', enter 'h' for help." crlf
end if
# --- Append Mode ---
else if mode is equal to 1 then
in linePointer solve linePointer + 1
in rl-prompt join "\033[90m" linePointer "\033[0m "
call external rl-accept
store rl-input in inputLine
if inputLine is equal to "." then
if appendLocation is equal to lineCount then
for each appendLine in appendBuffer do
push appendLine to fileContents
repeat
else
for i from 0 to lineCount step 1 do
if i is equal to appendLocation then
for each appendLine in appendBuffer do
push appendLine to newBuffer
repeat
end if
push fileContents:i to newBuffer
repeat
copy newBuffer to fileContents
end if
clear appendBuffer
display "\033[36mCommand Mode\033[0m" crlf
store 0 in mode
else
# --- Add line to file ---
push inputLine to appendBuffer
store 0 in hasSaved
end if
end if
end sub
sub displayFile
parameters:
from is number
count is number
local data:
i is number
lineNumber is number
procedure:
display "\033[90m--- File '" fileName "' ---\033[0m" crlf
store from in i
while i is less than lineCount and count is greater than 0 do
in lineNumber solve i + 1
call printLineNumber with lineNumber
display fileContents:i "\033[90m$\033[0m\n"
in count solve count - 1
in i solve i + 1
repeat
display "\033[90m--- End of File ---\033[0m" crlf
end sub
sub deleteLines
parameters:
from is number
count is number
local data:
i is number
newBuffer is text list
deleted is number
j is number
procedure:
if count is greater than 0 then
store 0 in hasSaved
end if
while i is less than lineCount do
if i is equal to from and count is greater than 0 then
for j from 0 to count step 1 do
if i is less than lineCount then
in deleted solve deleted + 1
in i solve i + 1
else
break
end if
repeat
else
push fileContents:i to newBuffer
in i solve i + 1
end if
repeat
copy newBuffer to fileContents
in from solve from + 1
display "Deleted " deleted " line(s)." crlf
end sub
sub saveFile
local data:
fileText is text
i is number
procedure:
if fileName is equal to "" then
display "Use 'n' to set a filename before saving." crlf
return
end if
write "" to file fileName
for i from 0 to lineCount step 1 do
append fileContents:i to file fileName
in i solve i + 1
if i is less than lineCount then
append "\n" to file fileName
end if
in i solve i - 1
repeat
display "Saved to file '" fileName "'." crlf
store 1 in hasSaved
end sub
sub loadFile
local data:
fileText is text
procedure:
load file fileName in fileText
split fileText by "\n" in fileContents
delete last element of fileContents
end sub
sub exitEditor
local data:
option is text
procedure:
if hasSaved is equal to 0 then
display "The file '" fileName "' has been edited but not saved." crlf
store "Would you like to save it? (y/n/c): " in rl-prompt
while 1 is equal to 1 do
call external rl-accept
store rl-input in option
if option is equal to "y" or option is equal to "Y" then
call saveFile
exit
else if option is equal to "n" or option is equal to "N" then
exit
else if option is equal to "c" or option is equal to "C" then
display "Cancelled." crlf
return
end if
display "Please enter 'y' or 'n': "
repeat
else
exit
end if
end sub
sub printLineNumber
parameters:
lineNumber is number
local data:
numberText is text
digits is number
digits2 is number
i is number
procedure:
store lineCount in numberText
get length of numberText in digits
store lineNumber in numberText
get length of numberText in digits2
store digits2 in i
while i is less than digits do
display " "
in i solve i + 1
repeat
display "\033[35m" lineNumber "\033[0m "
end sub
sub copyToBuffer
parameters:
from is number
count is number
local data:
i is number
copied is number
procedure:
clear copyBuffer
in from solve from - 1
in count solve from + count
for i from from to count step 1 do
if i is greater than or equal to lineCount then
break
end if
push fileContents:i to copyBuffer
in copied solve copied + 1
repeat
display "Copied " copied " line(s) to the copy buffer." crlf
end sub
sub display_version
display "This is \033[36mled\033[0m version " version ", the LDPL Text Editor." crlf
display "Created by Martín del Río, (c) 2019, released under the GPLv3 license." crlf
end sub