-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathved.pas
564 lines (508 loc) · 10.1 KB
/
ved.pas
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
program Ved (Input, Output);
uses
Crt;
const
MAX_LINES = 512;
type
filename = string [80];
linestr = string [128];
lineptr = ^linestr;
var
termWidth, termHeight : integer;
x, y : integer; { cursor coordinates }
offset : integer;
status : string [70];
linecount : integer;
lines : array [1..MAX_LINES] of lineptr;
cmd : string [10];
{ Uncomment this function for TP 3
function ReadKey : char;
var
ch : char;
begin
repeat Read(Kbd, ch) until ch <> #0;
ReadKey := ch;
end;
}
{
Get Screen Size for TP7 and FPC
}
procedure GetScreenSize;
begin
{ TP7 }
termWidth := Lo(WindMax) - Lo(WindMin) + 1;
termHeight := Hi(WindMax) - Hi(WindMin) + 1;
{ FPC }
termWidth := WindMaxX - WindMinX + 1;
termHeight := WindMaxY - WindMinY + 1;
end;
{
Render functions
}
procedure RenderText(startln : integer);
var
screenln : integer;
begin
screenln := startln - offset;
GotoXY(1, screenln);
repeat
ClrEol;
if startln <= linecount then
begin
Writeln(lines[startln]^);
startln := Succ(startln);
end;
screenln := Succ(screenln);
until (screenln > termHeight - 1);
end;
procedure RenderLn(lnr : integer);
begin
GotoXY(1, lnr - offset);
ClrEol;
Writeln(lines[lnr]^);
GotoXY(x, y - offset);
end;
procedure RenderCurLn;
begin
RenderLn(y);
end;
procedure RenderStatus;
var
percent : integer;
percentstr : string [4];
begin
GotoXY(1, termHeight);
TextBackground(White);
TextColor(Black);
ClrEol;
if Length(cmd) <> 0 then
begin
Write(cmd);
end
else
begin
Write(status);
end;
if offset = 0 then
begin
percentstr := 'Top';
end
else
begin
percent := Trunc(((offset + termHeight) / linecount) * 100);
Str(percent, percentstr);
percentstr := percentstr + '%';
end;
GotoXY(termWidth - Length(percentstr) - 1, termHeight);
Write(percentstr);
NormVideo;
end;
procedure RenderCursor;
var
newx, len : integer;
begin
{ If x is more than line length, goto end of line }
newx := x;
len := Length(lines[y]^);
if x > len then newx := len;
if len = 0 then newx := 1;
GotoXY(newx, y - offset);
end;
procedure Render;
begin
RenderText(offset + 1); { Render all }
RenderStatus;
RenderCursor;
end;
procedure RenderDown;
begin
RenderText(y); { Render from current row down }
RenderStatus;
RenderCursor;
end;
procedure PrintStatus(msg : linestr);
begin
status := msg;
RenderStatus;
GotoXY(x, y - offset);
end;
{
Movement functions
}
procedure AdjustEol;
var
len : integer;
begin
len := Length(lines[y]^);
if x > len then x := len;
if x = 0 then x := 1;
end;
procedure GoLeft;
begin
AdjustEol;
if x <> 1 then x := Pred(x);
RenderCursor;
end;
procedure GoRight;
begin
AdjustEol;
if x < Length(lines[y]^) then x := Succ(x);
RenderCursor;
end;
procedure GoUp;
begin
if y <> 1 then
begin
y := Pred(y);
if (offset > 0) and
(y - offset = 0) then
begin
offset := Pred(offset);
Render;
end;
RenderCursor;
end
end;
procedure GoDown;
begin
if y < linecount then
begin
y := Succ(y);
if (y - offset) > termHeight - 1 then
begin
offset := Succ(offset);
Render;
end;
RenderCursor;
end;
end;
procedure GoToBottom;
begin
y := linecount;
if linecount > termHeight - 1 then
begin
offset := linecount - termHeight + 1;
end;
Render;
end;
procedure GoFarRight;
begin
x := Length(lines[y]^);
if x = 0 then x := 1;
RenderCursor;
end;
procedure GoFarLeft;
begin
x := 1;
RenderCursor;
end;
function EndOfLine : boolean;
begin
if x >= Length(lines[y]^) then
begin
EndOfLine := True;
end
else
begin
EndOfLine := False;
end;
end;
{
Buffer modification functions
}
procedure DeleteLn(lnr : integer);
var
i : integer;
begin
Dispose(lines[lnr]);
if lnr <> linecount then
for i := lnr to linecount do
lines[i] := lines[i+1];
linecount := Pred(linecount);
end;
procedure InsertLine(lnr : integer; value : linestr);
var
i : integer;
begin
lnr := Succ(lnr);
for i := linecount downto lnr do
lines[i+1] := lines[i];
linecount := Succ(linecount);
New(lines[lnr]);
lines[lnr]^ := value;
end;
procedure BreakLn(lnr, index : integer);
var
len : integer;
begin
len := Length(lines[lnr]^) - index + 1;
InsertLine(y, Copy(lines[lnr]^, index, len));
Delete(lines[y]^, index, len);
end;
procedure ConcatLn(lnr : integer);
begin
if lnr < linecount then
begin
Insert(lines[Succ(lnr)]^, lines[lnr]^, Length(lines[lnr]^) + 1);
DeleteLn(Succ(lnr));
end;
end;
procedure CropLn(lnr, index : integer);
begin
lines[y]^[0] := Chr(Pred(index));
end;
procedure DeleteChar(lnr, index: integer);
begin
if Length(lines[lnr]^) <> 0 then
Delete(lines[lnr]^, index, 1);
end;
procedure InsertChr(lnr, index : integer; ch : char);
begin
Insert(ch, lines[lnr]^, index);
end;
{
Start of program
}
procedure JumpToNewLn(value : linestr);
begin
InsertLine(y, value);
x := 1;
GoDown;
RenderDown;
end;
procedure ReadInsert;
var
ch : char;
begin
PrintStatus('-- INSERT --');
repeat
ch := ReadKey;
case ch of
#8 : begin { Backspace }
if x > 0 then begin
x := Pred(x);
DeleteChar(y, x);
RenderCurLn;
GotoXY(x, y - offset);
end;
end;
#13 : begin { Line feed }
if EndOfLine then
begin
JumpToNewLn('');
end
else
begin
BreakLn(y, x);
x := 1;
RenderDown;
GoDown;
end;
end;
#27 : begin end;
else begin { Character }
InsertChr(y, x, ch);
RenderCurLn;
x := Succ(x);
GotoXY(x, y - offset);
end;
end;
until ch = #27;
if x > 1 then x := Pred(x);
PrintStatus('');
end;
procedure NewDoc;
begin
New(lines[1]);
lines[1]^ := 'New file. Thank you for using this editor.';
linecount := 1;
end;
function FileExists(name : filename) : boolean;
var
f : file;
exists : boolean;
begin
Assign(f, name);
{$I-}
Reset(f);
{$I+}
exists := IOResult = 0;
if exists then Close(f);
FileExists := exists;
end;
procedure LoadFile(name : filename);
var
f : text;
i : integer;
countstr : string [4];
begin
Assign(f, name);
if not FileExists(name) then
begin
PrintStatus('Editing new file');
NewDoc;
Exit;
end;
Reset(f);
i := 1;
while not Eof(f) and (i < MAX_LINES) do
begin
New(lines[i]);
Readln(f, lines[i]^);
i := Succ(i);
end;
Close(f);
linecount := i - 1;
if linecount = 0 then NewDoc;
Str(linecount, countstr);
PrintStatus(countstr + ' lines read from ''' + name + '''');
end;
procedure SaveFile(name : filename);
var
f : text;
i : integer;
countstr : string [4];
begin
Assign(f, name);
Rewrite(f);
for i := 1 to linecount do Writeln(f, lines[i]^);
Close(f);
Str(linecount, countstr);
PrintStatus(countstr + ' lines saved to ''' + name + '''');
end;
procedure ReadCommand;
var
ch : char;
begin
cmd := ':';
status := '';
RenderStatus;
GotoXY(2, termHeight);
repeat
ch := ReadKey;
if (ch <> #13) and (ch <> #27) then
Insert(ch, cmd, Length(cmd) + 1);
RenderStatus;
until (ch = #27) or (ch = #13);
if (ch = #27) or (cmd = ':') then
begin
cmd := '';
RenderStatus;
Exit;
end;
{ Quit }
if cmd = ':q' then
begin
Halt;
end
{ Save }
else if cmd = ':w' then
begin
cmd := '';
SaveFile(ParamStr(1));
end
{ Save and quit }
else if cmd = ':wq' then
begin
SaveFile(ParamStr(1));
Halt;
end
else
begin
cmd := '';
PrintStatus('Unknown command');
end;
end;
procedure ShowHelp(prgname : filename);
begin
Writeln('Usage: ', prgname, ' filename');
end;
procedure StartEditor;
var
ch : char;
begin
repeat
ch := ReadKey;
case ch of
#104 : GoLeft; { h }
#108 : GoRight; { l }
#106 : GoDown; { j }
#107 : GoUp; { k }
#111 : begin { o }
JumpToNewLn('');
ReadInsert;
ch := #0;
end;
#120 : begin { x }
AdjustEol;
DeleteChar(y, x);
RenderCurLn;
AdjustEol;
RenderCursor;
end;
#68 : begin { D }
AdjustEol;
if Length(lines[y]^) > 1 then
begin
CropLn(y, x);
RenderCurLn;
RenderCursor;
end;
end;
#105 : begin { i }
AdjustEol;
ReadInsert;
ch := #0;
end;
#73 : begin { I }
GoFarLeft;
ReadInsert;
ch := #0;
end;
#97 : begin { a }
AdjustEol;
if Length(lines[y]^) <> 0 then x := Succ(x);
ReadInsert;
ch := #0;
end;
#65 : begin { A }
GoFarRight;
if Length(lines[y]^) <> 0 then x := Succ(x);
ReadInsert;
ch := #0;
end;
#74 : begin { J }
ConcatLn(y);
RenderDown;
end;
#71 : begin { G }
GoToBottom;
end;
#0 : begin { NULL }
ch := ReadKey;
case ch of
#75 : GoLeft;
#77 : GoRight;
end;
end;
#58 : ReadCommand;
end;
until ch = #127;
end;
{ Program start }
begin
if (ParamCount <> 1) then
begin
ShowHelp(ParamStr(0));
Halt;
end;
x := 1;
y := 1;
offset := 0;
cmd := '';
GetScreenSize;
ClrScr;
LoadFile(ParamStr(1));
Render;
StartEditor;
end.