-
Notifications
You must be signed in to change notification settings - Fork 8
/
HISTTTY.PAS
487 lines (453 loc) · 10.6 KB
/
HISTTTY.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
unit HistTTY;
interface
uses
Crt,Win,Objects,TeleType,StrColl;
type
PHistColl = ^THistColl;
THistColl =
object ( TStrCollection )
function IndxPred( anIndx: integer): integer; virtual;
function IndxSucc( anIndx: integer): integer; virtual;
function ScanStrBackward( aStr: string):string; virtual;
function ScanStrForward( aStr: string):string; virtual;
private
indx : integer;
end;
PHistTTY = ^THistTTY;
THistTTY =
object( TeleT )
constructor Init( R: TRect);
destructor Done; virtual;
function Receive: string; virtual;
procedure SetHistory( aHist: PHistColl); virtual;
procedure SetMaxStrLen( aMax: integer); virtual;
private
MaxStrLen : integer;
History : PHistColl;
Template : string;
end;
implementation
type
CheckSet = set of char;
const
Check: CheckSet = [#32..#255];
NUL = #0;
BS = #8;
CR = #13;
LF = #10;
ESC = #27;
prompt = ' ';
function UpStr( aStr: string): string; assembler;
asm
cli
push ds
les di, @Result
lds si, aStr
lodsb
stosb
xor ah, ah
mov cx, ax
@@2: lodsb
cmp al, 'a'
jb @@1
cmp al, 'z'
jg @@1
sub al, 'a'-'A'
@@1: stosb
loop @@2
pop ds
sti
end;
{ THistoryCollection }
function THistColl.IndxSucc;
var
i : integer;
begin
i := succ( anIndx);
if i > pred( count) { Wrap around }
then i := 0;
IndxSucc := i;
end;
function THistColl.IndxPred;
var
i : integer;
begin
i := pred( anIndx);
if i < 0 { Wrap around }
then i := pred( count);
IndxPred := i;
end;
function StringMatch( Str1,Str2: string ): boolean;
var
len : byte;
begin
len := length( Str1);
if (len = 0) or (UpStr( Str1) = UpStr( copy( Str2, 1, len)))
then StringMatch := true
else StringMatch := false;
end;
function THistColl.ScanStrBackward;
var
i : integer;
Quit : boolean;
TmpPtr : PString;
begin
i := IndxPred( indx);
Quit := false;
while not Quit do
begin
TmpPtr := PString( At( i));
if StringMatch(aStr, TmpPtr^)
then
begin
ScanStrBackward := TmpPtr^;
indx := i;
exit
end
else i := IndxPred( i);
if i = IndxPred( indx)
then Quit := true;
end;
ScanStrBackward := '';
end;
function THistColl.ScanStrForward;
var
i : integer;
Quit : boolean;
TmpPtr : PString;
begin
i := IndxSucc( indx);
Quit := false;
while not Quit do
begin
TmpPtr := PString( At( i));
if StringMatch(aStr, TmpPtr^)
then
begin
ScanStrForward := TmpPtr^;
indx := i;
exit
end
else i := IndxSucc( i);
if i = IndxSucc( indx)
then Quit := true;
end;
ScanStrForward := '';
end;
{ THistoryTTY }
constructor THistTTY.Init;
begin
TeleT.Init( R);
SetMaxStrLen( Bounds.B.X - Bounds.A.X - length( prompt) - 1);
Template := '';
end;
destructor THistTTY.Done;
begin
TextMode(CO80);
TeleT.Done
end;
function THistTTY.Receive;
procedure Beep( error: boolean);
begin
if error
then
begin
Sound( 440);
Delay( 110);
NoSound;
end
end;
procedure GetStr( var LineStr: string; len: byte);
var
SPos, TempPos, i,
count, x1,y1, x2 : byte;
ch : char;
s2, stmp : string;
Ins, edited : boolean;
procedure SetPosX( x:byte);
begin
x2 := x;
gotoXY( x2, y1);
end;
procedure SetStr( aStr: string);
begin
LineStr := aStr;
count := length( LineStr);
TempPos := 1;
SPos := 1;
SetPosX( x1);
ClrToEol;
write( LineStr);
SetPosX( x1);
end;
procedure DelChar;
begin
if count <> 0 then
begin
dec( count);
if SPos <= count
then
begin
write( copy( LineStr, succ( SPos), 255),' ');
gotoXY( x2, y1);
end
else
write( ' '+BS);
Delete( LineStr, SPos, 1);
end
end;
procedure AddChar( ch: char);
begin
if count < len
then
begin
if SPos = succ( count)
then LineStr := LineStr + ch
else
begin
write( ' ', copy( LineStr, SPos, 255));
gotoXY( x2, y1);
Insert( ch, LineStr,SPos);
end;
write( ch);
inc( x2);
inc( count);
inc( SPos);
end;
end;
procedure InsertStr( aStr: string);
var
i : byte;
begin
for i := 1 to length(aStr) do
AddChar( aStr[i]);
end;
procedure SubstChar( ch: char);
begin
if SPos <= count
then DelChar;
AddChar( ch);
end;
procedure SetChar( ch: char);
begin
edited := true;
if Ins
then AddChar( ch)
else SubstChar( ch);
end;
const
kbAltE = #$12; kbAltR = #$13; kbAltT = #$14;
kbAltY = #$15; kbAltU = #$16; kbAltI = #$17;
kbAltO = #$18; kbAltP = #$19; kbAltB = #$30;
kbAltA = #$1E; kbAltS = #$1F; kbAltD = #$20;
kbAltH = #$23; kbAltJ = #$24; kbAltK = #$25;
kbAltL = #$26; kbAltZ = #$2C; kbAltX = #$2D;
kbAltF = #$21; kbAltG = #$22; kbAltC = #$2E;
kbAltV = #$2F; kbAltN = #$31; kbAltM = #$32;
kbEnter = #$0d; kbEsc = #$1b; kbAltQ = #$10;
kbF1 = #$3B; kbF2 = #$3C; kbF3 = #$3D;
kbCtrlL = #$0c; kbBkSpc = #$08; kbF9 = #$43;
kbUp = #$48; kbDown = #$50; kbExtd = #$00;
kbLeft = #$4B; kbRight = #$4D;
kbEnd = #$4F; kbHome = #$47;
kbIns = #$52; kbDel = #$53;
begin
LineStr := ''; { Edited string }
count := 0; { Length of string }
SPos := 1; { Position in string }
TempPos := 1; { Position in template }
x1 := WhereX; { X coord of beginning }
x2 := x1; { X coord of current pos }
y1 := WhereY; { Y coord of beginning }
Ins := true; { Insert toggle }
edited := true; { Edited or not }
repeat
ch := ReadKey;
case ch of
kbExtd:
begin
ch := readkey;
case ch of
kbF2:
begin
SetStr('salvar');
ch := kbEnter
end;
kbF9:
begin
SetStr('ejecutar');
ch := kbEnter
end;
kbAltQ:
begin
SetStr('terminar');
ch := kbEnter
end;
kbAltI:
InsertStr('ir ');
kbAltA:
InsertStr('abrir ');
kbAltC:
InsertStr('coger ');
kbAltP:
InsertStr('preguntar ');
kbAltD:
InsertStr('dar ');
kbAltL:
InsertStr('leer ');
kbAltR:
InsertStr('romper ');
kbAltM:
InsertStr('matar ');
kbF1:
if TempPos <= length( template)
then
begin
SetChar( Template[ TempPos ]);
inc( TempPos);
end;
kbF3:
if (length( template) <> 0) and (TempPos <> length( Template))
then
begin
for i := TempPos to length( template) do
SetChar( Template[ i]);
TempPos := length( template) + 1;
end;
kbHome:
begin
SPos := 1;
SetPosX( x1);
end;
kbEnd:
begin
SPos := succ( count);
SetPosX( x1 + count);
end;
kbUp:
if ( History <> nil) and ( History^.count > 0)
then
begin
if edited then
s2 := LineStr;
stmp := History^.ScanStrBackward(s2);
if stmp <> ''
then
begin
SetStr( stmp);
edited := false;
end
else Beep( true)
end;
kbDown:
if ( History <> nil) and ( History^.count > 0)
then
begin
if edited
then s2 := LineStr;
stmp := History^.ScanStrForward( s2);
if stmp <> ''
then
begin
SetStr( stmp);
edited := false;
end
else Beep( true);
end;
kbLeft:
if SPos > 1 then
begin
Dec( x2);
Dec( SPos);
gotoXY( x2, y1);
end;
kbRight:
if SPos <= count
then
begin
Inc( x2);
Inc( SPos);
gotoXY( x2, y1);
end;
kbIns:
begin
Ins := not Ins;
if Ins
then NormalCursor
else BlockCursor;
end;
kbDel:
begin
DelChar;
edited := true;
end;
end; {case}
end;
kbCtrlL:
lineStr := ^L; { Clear screen }
kbBkSpc:
if SPos <> 1 then
begin
dec( SPos);
SetPosX( pred( x2));
DelChar;
edited := true;
end;
kbEsc:
begin
History^.indx := History^.count; { Reset cursor position }
SetStr( '');
edited := true;
end;
else
if (ch in Check)
then
begin
SetChar( ch);
if (TempPos <= length( template)) and (not ins)
then inc( TempPos)
end
else
if not (ch in [kbEnter, kbEsc])
then Beep( true);
end; {case}
until (ch = kbEnter) or (ch = kbCtrlL);
end;
var
s: string;
begin
History^.indx := History^.count; { Reset cursor position }
if MaxStrLen = 0
then MaxStrLen := Bounds.B.X -Bounds.A.X - length(prompt);
repeat
Color( cGround );
if state <> stReceiving
then SetState( stReceiving );
SetColor;
Send( prompt );
Color( cHighText );
SetColor;
GetStr( s, MaxStrLen );
if s = ^L
then
begin
Clear;
NewPaper
end;
until s <> ^L;
Receive := s;
Template := s;
Color( cGround );
SendNewLine;
end;
procedure THistTTY.SetHistory;
begin
if aHist <> nil
then History := aHist;
end;
procedure THistTTY.SetMaxStrLen;
begin
if aMax < (Bounds.B.X -Bounds.A.X - length(prompt))
then MaxStrLen := aMax;
end;
end.