-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.mac
2315 lines (2108 loc) · 55.2 KB
/
main.mac
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
;**********************************************************************;
; ;
; This file is part of TED, a clone of the screen-oriented text ;
; editor that was once available for the RT-11 OS. ;
; Copyright (C) 2011-2020, Hector Peraza. ;
; ;
; This program is free software; you can redistribute it and/or ;
; modify it under the terms of the GNU General Public License as ;
; published by the Free Software Foundation; either version 2 of ;
; the License, or (at your option) any later version. ;
; ;
; This program is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with this program; if not, write to the Free Software ;
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;
; ;
;**********************************************************************;
.Z80
title TED - Text Editor
subttl Root section
include TED.INC
;=======================================================================
; R O O T S E C T I O N
public FSTREC,CURREC,CURLN,CURPOS,LASTLN,CMDPTR
public N1D,N3,ARGLEN,ARGSTR,CKDSTR,SEARCH,WFOV
public GTDSTR,GETPAR,XLTESC,CLNCR,PUTCN,PSTRN
public DELBUF,STORE,FNDBUF,LDREC,GETARG,EEXTRA
public STRBUF,EDLEN,GETTXP,GTTXP0,INSBUF,NUMD
public SETTXP,EDREC,NXTREC,EXPAND,EDITBF,CRLF
public TMPBUF,MLOOP,ADDHLA,SUBHLA,CPDEHL,PUTSTR
public FMODF,ARGTYP,NUMVAL,FATAL,ERRWFO
extrn CMDF,CMDD,CMDR,CMDW,CMDSU,CMDM,CMDMD
extrn CMDPR,SCRNED,TRMNAM,CLREOL,GETTOP,EXIT
extrn WCREAT,FREAD,FWRITE,WCLOSE,WDELET,GETCML
extrn GETC,UNGETC,PUTC,TTQCLR,TTINIT,UCASE
extrn HLDEC,SCRX,UFLAG,UPDST
; needed for debugging
public FSTFRE,NUMFRE,NUMREC,MEMBOT,ENDMAP,BFPOOL
public MEMTOP,NUMBUF,WFBLKS,NBLKS
;-----------------------------------------------------------------------
cseg
; Main loop - editor starts here after initialization
MLOOP: ld sp,STACK ; restore stack pointer
xor a
ld (UFLAG),a
ld a,(WFOVFL) ; check work file overflow flag
or a
jr z,m1 ; jump if not set
xor a
ld (WFOVFL),a ; else clear it
ld (CTCFLG),a ; clear ^C command flag
call TTQCLR ; and reset TTY input queue
m1: ld hl,PROMPT ; prompt string
call GETCMD ; read command
jp c,SCRNED ; ^C at start of command = go to scrn editor
ld hl,CMDBUF ; HL = start of command line buffer
ld de,STRBUF ; DE = start of command word buffer
ld a,' '
ld (de),a ; init STRBUF to empty command (two spaces)
inc de
ld (de),a
dec de
getw: ld a,(hl) ; get char from command line buffer
call UCASE ; convert to uppercase
cp 'A' ; letter?
jr c,endw ; jump if not (end of command word)
cp 'Z'+1
jr nc,endw
ld (de),a ; and copy it to STRBUF buffer
inc de
inc hl ; increment pointers
jr getw ; and loop
endw: ld (CMDPTR),hl ; save command line pointer
ld hl,CMDTBL ; HL = command table
ld de,STRBUF
fndcmd: ld a,(hl)
or a ; end of table?
jp z,BADCMD ; jump if yes - error
ld a,(de)
cp (hl) ; else compare bytes
inc hl
inc de
jr nz,nxtcmd
ld a,(de)
cp (hl)
jr z,execmd ; jump if match
nxtcmd: inc hl ; else skip to next entry
dec de
inc hl
inc hl
jr fndcmd ; and loop
execmd: inc hl
ld e,(hl) ; get routine address
inc hl
ld d,(hl)
ld hl,MLOOP
push hl ; push return address
ex de,hl
jp (hl) ; execute command handler
BADCMD: ld hl,ERRCMD ; 'WRONG COMMAND'
call PUTSTR ; print string
jp MLOOP ; loop back to editor start
; Command table
CMDTBL: db 'CL' ; clear buffer
dw CMDCL
db 'FR' ; show free memory
dw CMDFR
db 'P ' ; set text index (position)
dw CMDP
db 'PR' ; print lines on terminal
dw CMDPR
db 'S ' ; enter screen mode
dw CMDS
db ' ' ; ^C also enters screen mode
dw CMDS
db 'EX' ; exit editor
dw CMDEX
db 'R ' ; read file
dw CMDR
db 'W ' ; write file
dw CMDW
db 'F ' ; show current file name
dw CMDF
db 'M ' ; copy lines
dw CMDM
db 'MD' ; move lines
dw CMDMD
db 'D ' ; delete lines
dw CMDD
db 'SU' ; substitute
dw CMDSU
db 0
PROMPT: db 'TED>',0 ; prompt
ERRCMD: db 'Wrong command',LF,CR,0
; CL command - clear buffer
CMDCL: call GETARG ; process arguments
ret c ; if error
call EEXTRA ; display error if extra parameters
ret c ; if error
call CHKMOD ; check file modified flag
ret c ; if set
ld hl,0
ld (FSTREC),hl ; clear record number of 1st line of text
ld (CURREC),hl ; clear record number of current line
ld (CURLN),hl ; clear current line
ld (LSTREC),hl ; clear number of last record of text
ld (LASTLN),hl ; clear last line number
ld (FSTFRE),hl ; clear start record number in free list
ld (NBLKS),hl ; reset work file size
ld (USECNT),hl ; clear LRU count
xor a
ld (NUMBUF),a ; clear count of allocated buffers in memory
ld (SCRX),a ; reset screen left offset
ld (CURPOS),a ; clear current character index
ld hl,(NUMREC)
ld (NUMFRE),hl ; reset free records = total records
ld hl,(MEMBOT)
ld (ENDMAP),hl ; reset ENDMAP to start of free memory
ld hl,(MEMTOP)
ld (BFPOOL),hl ; reset BFPOOL to end of free memory
ret
; FR command - show free space
CMDFR: ld hl,(NUMFRE) ; free records
ld a,'0' ; filler
call HLDEC ; display value as decimal
ld hl,MSGFRE
call PUTSTR ; ' RECORDS FREE OUT OF '
ld hl,(NUMREC) ; total records
ld a,'0'
call HLDEC ; display value as decimal
call CRLF
call GETARG ; process arguments
ret c ; on error return
jp EEXTRA ; display error if extra parameters
MSGFRE: db ' records free out of ',0
; P command - show or set current text position
CMDP: call GETARG ; process arguments
jr c,cp1 ; on error, display current pos and return
ld a,c
or a ; check arg type
jr z,cp1 ; if no args specified, display current pos
call SETPOS ; else set position from args
cp1: ld hl,LINCHR ; 'LINE '
call PUTSTR
push hl
ld hl,(CURLN) ; line #
ld a,'0' ; filler
call HLDEC ; output value as decimal
pop hl
call PUTSTR ; 'CHAR '
ld a,(CURPOS) ; char #
ld l,a
ld h,0
ld a,'0' ; filler
call HLDEC ; output value as decimal
call CRLF
jp EEXTRA ; display error if extra parameters
SETPOS: ld de,(CURLN) ; else get current line into DE
call GTTXP0 ; get record, line, char pos + next arg
ret c ; on error, return
ld a,c
dec a ; check arg type
jr nz,sp2 ; branch if no second arg specified
ld bc,(NUMVAL) ; get column
ld a,b
or a ; ensure is in range
ld b,MAXLEN-1 ; else trim accordingly
jr nz,sp1 ; !TODO: if negative - from end?
ld a,c
cp MAXLEN
jr nc,sp1
ld b,c
sp1: push bc
call GETARG
pop bc
sp2: call SETTXP ; curr rec <- HL, curr ln <- DE, chr pos <- B
or a
ret
LINCHR: db 'Line ',0,', Char ',0
; S command, null command - enter screen editor
CMDS: call GETARG ; process arguments
ret c ; on error return
call SETPOS
ret c
call EEXTRA ; display error if extra parameters
ret c ; on error return
pop hl ; pop return address
jp SCRNED ; enter screen editor
; EX command - exit editor
CMDEX: call GETARG ; process arguments
ret c ; on error return
call EEXTRA ; display error if extra parameters
ret c ; on error return
call CHKMOD ; check file modified flag
ret c ; return if set
ld hl,EXITMS
call PUTSTR
QUIT: call WCLOSE ; close work file
call WDELET ; and delete it
jp EXIT ; exit to system
EXITMS: db '[Exit]',LF,CR,0
; Check file modified flag and display warning if set
CHKMOD: ld hl,FMODF
ld a,(hl) ; get flag state
ld (hl),0 ; clear it to avoid a second warning
or a
ret z ; return with CY clear if not set
ld hl,SAVWRN ; else display warning
call PUTSTR
scf ; and return with CY set
ret
SAVWRN: db 'Didn''t you forget to Write the file?',LF,CR,0
; Set text pointers:
; current record number <- HL
; current line <- DE
; current char index <- B
SETTXP: ld (CURREC),hl ; store current record number
ld (CURLN),de ; store current line
ld a,b
ld (CURPOS),a ; store current char index
ret
; Same as GETTXP, but ensures HL is not zero (if there is some text).
; Return CY on error.
GTTXP0: call GETTXP
ret c ; on error return
ld a,h ; check record number
or l
ret nz ; return if not zero
ld hl,(FSTREC) ; else get number of 1st record of text into HL
ld a,h
or l
ret z ; return if zero
inc de ; set line number to 1
ret
; Get text pointers according to argument
; On input:
; C = arg type, DE = line number
; Returns:
; HL = record number
; DE = line number
; B = char pos
; CY set on error.
GETTXP: ld hl,(CURLN) ; check current line number
ld a,h
or l
jr nz,gtx1 ; jump if not zero
ld hl,(FSTREC)
ld (CURREC),hl ; current record # <- first text record
ld a,h
or l
jr z,gtx1 ; jump if zero
ld hl,1 ; else set current line to 1
ld (CURLN),hl
gtx1: ld b,0 ; char pos = 0
ld a,c
cp 1 ; argument type = single numeric?
jr nz,gtx3 ; jump if not
ld de,(NUMVAL) ; else set line number in DE = numeric value
ld a,(NUMD) ; check displacement flag
or a
jp p,gtx2 ; jump if absolute or relative
ld hl,(LASTLN) ; else is from end (negative),
call IADD16 ; so add last line number, result in DE
jr nc,gtx2 ; jump if no overflow
ld de,(LASTLN) ; else truncate value to last line
gtx2: call FNDBUF ; find line in text buffer
jp gtx9 ; check for more arguments and return
gtx3: cp 2 ; argument type = /str/(N1,N2)+N3?
jp nz,gtx10 ; jump if not
ld c,0
ld de,(N1) ; get N1 into DE
ld a,(N1D) ; check displacement for N1
or a
jr nz,gtx4 ; jump if not absolute
ld a,(CURPOS) ; get current char index
ld c,a ; into C
ld de,(CURLN) ; get current line into DE
jr gtx5
gtx4: jp p,gtx5 ; jump if N1 displacement relative
ld hl,(LASTLN) ; else is from end, add last line number
call IADD16 ; result in DE
jr c,gtx11 ; error if overflow - 'LINE NOT FOUND'
gtx5: ld a,d
or e
jr nz,gtx6
inc de
gtx6: ld hl,(LASTLN)
call CPDEHL ; compare with last line number
jr c,gtx11 ; error if higher - 'LINE NOT FOUND'
ld (N1),de ; set N1 value
ld a,c
ld (N1D),a ; use displacement for N1 as temp value
ld de,(N2) ; get N2 value into DE
ld a,(N2D) ; check displacement for N2
or a
jr nz,gtx7 ; jump if not absolute
ld de,(LASTLN) ; else get last line number into DE
jr gtx8
gtx7: jp p,gtx8 ; jump if N2 displacement relative
ld hl,(LASTLN) ; else is from end, add last line number
call IADD16 ; result in DE
jr c,gtx11 ; error if overflow - 'LINE NOT FOUND'
gtx8: ld hl,(N1) ; compare DE with N1
ex de,hl
call CPDEHL
jr c,gtx11 ; error if (old) DE < N1
ld (N2),hl ; set N2 value
call SEARCH ; search for ARGSTR string in N1..N2
jr c,gtx11 ; jump if no match found
push hl
ld hl,(N3) ; check N3 value
ld a,h
or l
pop hl
jr z,gtx9 ; jump if zero
ld hl,(N3)
call IADD16 ; else add it to saved line number
jr c,gtx11
call FNDBUF ; find line in text buffer
ld b,0 ; char pos <- 0
gtx9: call GETARG ; process more arguments
ret ; return (on error with CY set)
gtx10: call FNDBUF ; find line in text buffer
or a ; return with CY clear (OK)
ret
gtx11: ld hl,ERRLNF ; 'LINE NOT FOUND'
call PUTSTR ; print string
scf ; return with CY set (error)
ret
ERRLNF: db 'Line not found',LF,CR,0
; Search for ARGSTR string in lines N1..N2.
; Note: N1D is not used here as displacement, but as char index
; (initially set by the caller).
; Returns with CY set if no match found.
; Else returns DE = line #, HL = record #, B = char pos
SEARCH: ld de,(N1) ; get N1 into DE
call FNDBUF ; find line in text buffer
ld (NXTREC),hl ; NXTREC <- next line record#
txs1: push hl
ld hl,(N2)
call CPDEHL ; compare N1 (DE) with N2 value
pop hl
ret c ; return if N1 > N2 (desired end reached)
ld hl,(NXTREC) ; else get next line record number into HL
ld a,h
or l
scf
ret z ; return with CY set if zero (no more lines)
push hl ; save record number
push de ; save N1 value
ld de,EDITBF ; get address of edit line buffer into DE
call EXPAND ; and expand (load) that line
ld a,(N1D)
ld l,a
ld h,0 ; add char index
add hl,de
ex de,hl ; DE = start of text to search
ld a,(EDLEN) ; get expanded line length
sub 2 ; remove the trailing CR/LF
ld b,a ; B = text length
ld a,(N1D)
cp b ; compare char index with text length in B
jr nc,txs2 ; skip match test if higher or same
ld a,(ARGLEN) ; get string arg length
ld c,a ; into C
ld hl,(ARGSTR) ; get string arg address into HL
call MATCH ; strings @DE and @HL match?
jr nc,txs3 ; exit loop if they match
txs2: pop de ; restore N1 value
pop hl ; restore record number
xor a
ld (N1D),a ; else clear char index
inc de ; increment DE (contains N1)
jr txs1 ; and loop
txs3: pop de ; restore N1 value
pop hl ; restore record number
ld (N1),de ; set new N1 value (for next call)
; return line number in DE
ld a,(N1D)
add a,b ; add char index to B
ld b,a
xor a ; (also clears CY)
ld (N1D),a ; clear char index (for next call)
ret
; Find line in text buffer. Starts the search from the closest pointer
; available to the target line (1st line of text, last line of text, or
; current line). Called with DE = line number, returns HL = record number.
FNDBUF: push de
push bc
ld hl,0
ld a,d
or e ; check line number
jp z,fb6 ; if zero, return record number = 0
ld hl,(LASTLN)
call CPDEHL ; compare with last line number
jr nc,fb1 ; jump if DE <= LASTLN
pop bc
pop de ; discard old DE value,
ld de,(LASTLN) ; fix it to last line number
ld hl,(LSTREC) ; return HL = last record of text
ret
fb1: ld hl,(CURLN)
call CPDEHL ; compare with current line number
jr c,fb3 ; jump if DE > CURLN
push de
ex de,hl
add hl,hl
ex de,hl
ld hl,(CURLN)
call CPDEHL
pop de
jr c,fb2 ; jump if DE > CURLN/2
ld bc,1 ; else begin from the start, set line# to 1
ld hl,(FSTREC) ; get number of first record of text into HL
jr fb4
fb2: ld bc,(CURLN) ; get current line into BC
ld hl,(CURREC) ; get current record number into HL
jr fb4
fb3: push de
ld de,(CURLN)
ld hl,(LASTLN) ; add last line number
add hl,de
pop de
push de
ex de,hl
add hl,hl
ex de,hl
call CPDEHL
pop de
jr nc,fb2 ; jump if DE <= (CURLN+LASTLN)/2
ld bc,(LASTLN) ; else get last line number into BC
ld hl,(LSTREC) ; get last line record number into HL
fb4: ld a,e
sub c
ld c,a
ld a,d
sbc a,b
ld b,a ; BC = DE - BC (distance to target line)
jr c,fb7 ; jump if negative
or c
jr z,fb6 ; return if zero (same line)
fb5: call LDREC ; fetch record address into DE
inc de
inc de ; index into next field
ld a,(de)
ld l,a
inc de
ld a,(de)
ld h,a ; traverse records
dec bc ; decrement count
ld a,b
or c
jr nz,fb5 ; loop
fb6: pop bc
pop de
ret
fb7: call LDREC ; fetch record address into DE
inc de
inc de
inc de
inc de ; index into prev field
ld a,(de)
ld l,a
inc de
ld a,(de)
ld h,a ; traverse records
inc bc ; increment negative count
ld a,b
or c
jr nz,fb7 ; loop
pop bc
pop de
ret
; Insert a line in text buffer. Called with:
; HL = line record number to insert line after (zero to insert it at the
; start of text buffer)
; DE = address of buffer with line to store into record(s) (zero to insert
; an empty line)
; BC = line number corresponding to record in HL
; Returns CY set on error.
INSBUF: push hl
push de
push bc
ld (OLDREC),hl ; remember old record number
call ALLREC ; alloc new record
ld (NEWREC),hl ; remember its number
ld a,h
or l
scf
jp z,ins6 ; return CY if zero (work file overflow)
ld hl,(OLDREC) ; check old record number
ld a,h
or l
jr nz,ins1 ; jump if not zero (insert line after this)
ld bc,(FSTREC) ; else get num of first record of text into BC
ld hl,(NEWREC)
ld (FSTREC),hl ; store number of new first record
jr ins2
ins1: call LDRECD ; get record address into DE
inc de ; skip continuation record field
inc de
ld a,(de) ; get next record number into BC
ld c,a
inc de
ld a,(de)
ld b,a
ld hl,(NEWREC) ; get new record #
ld a,h ; store new next record number here (link it)
ld (de),a
dec de
ld a,l
ld (de),a
ins2: ld l,c ; get old first/next record number into HL
ld h,b
ld a,h
or l
jr nz,ins3 ; jump if not zero
ld hl,(NEWREC)
ld (LSTREC),hl ; else set new last record number
jr ins4
ins3: call LDRECD ; get (next) record address into DE
inc de
inc de
inc de
inc de
ld hl,(NEWREC)
ld a,l ; store prev record number
ld (de),a
inc de
ld a,h
ld (de),a
ins4: pop de ; get old line # into DE
push de
ld hl,(CURLN) ; get current line # into HL
ex de,hl
call CPDEHL ; compare line numbers
jr nc,ins5 ; jump if old line # >= CURLN
ex de,hl
inc hl
ld (CURLN),hl ; else increment current line number
ins5: ld hl,(LASTLN) ; increment number of last line (line count)
inc hl
ld (LASTLN),hl
ld hl,(NEWREC) ; get new record number again into HL
call LDRECD ; get record address into DE
xor a
ld (de),a
inc de ; clear continuation record number field
ld (de),a
inc de
ld a,c
ld (de),a ; store next record number
inc de
ld a,b
ld (de),a
inc de
ld hl,(OLDREC)
ld a,l
ld (de),a ; store prev record number - now is fully linked
inc de
ld a,h
ld (de),a
pop bc
pop de
push de ; restore DE (line buf addr) from stack frame
push bc
ld hl,(NEWREC)
call STORE ; store line into (this) record(s)
;jr c,ins6 ; on error return with CY set
;or a ; CY clear = no error
ins6: pop bc
pop de
pop hl
ret
; Delete line from text buffer. Called with HL = record #, BC = line #
DELBUF: push hl
push de
push bc
push hl ; save current record number
call LDREC ; fetch record address into DE
inc de ; skip continuation record#
inc de
ld a,(de)
ld l,a ; get next record number
inc de
ld a,(de)
ld h,a
inc de
ld (NXTREC),hl ; NXTREC <- next line record number
ld a,(de)
ld l,a ; get prev record number
inc de
ld a,(de)
ld h,a
ld (PRVREC),hl ; PRVREC <- prev line record number
pop de ; pop current record number
call FREREC ; enter the record(s) into the free list
ld hl,(PRVREC) ; get prev record# into HL
ld a,h
or l
jr nz,del1 ; jump if not zero (not the first line of text)
ld hl,(NXTREC)
ld (FSTREC),hl ; else set new 1st rec# of text to next field
jr del2
del1: call LDRECD ; get address of prev record into DE
ld hl,(NXTREC) ; now get next record# into HL
inc de
inc de
ld a,l
ld (de),a ; store next record number (unlink old record)
inc de
ld a,h
ld (de),a
del2: ld a,h ; check next record number
or l
jr nz,del3 ; jump if not zero (not last line of text)
ld hl,(PRVREC)
ld (LSTREC),hl ; else store new last record number
jr del4
del3: call LDRECD ; get record address into DE
ld hl,(PRVREC) ; get prev record# into HL
inc de
inc de
inc de
inc de
ld a,l
ld (de),a ; store prev record number (unlink old record)
inc de
ld a,h
ld (de),a
del4: ld e,c ; get line number into DE
ld d,b
ld hl,(CURLN)
call CPDEHL ; compare current line number with DE
jr c,del6 ; jump if DE higher
jr nz,del5 ; jump if DE lower
xor a
ld (CURPOS),a ; else clear character pos
ld hl,(NXTREC)
ld (CURREC),hl ; set current record number to next record
ld a,h
or l
jr nz,del6 ; jump if not zero
ld hl,(PRVREC)
ld (CURREC),hl ; else set current record number to prev record
del5: ld hl,(CURLN)
dec hl ; decrement current line number (line count)
ld (CURLN),hl
del6: ld hl,(LASTLN) ; decrement number of last line
dec hl
ld (LASTLN),hl
ld a,1
ld (FMODF),a
pop bc
pop de
pop hl
ret
; Expand line stored in record(s) to a buffer.
; Called with DE = buffer address, HL = record number
EXPAND: push hl
push de
push bc
push de ; push dest ptr
ld (EDREC),hl ; EDREC <- record#
call LDREC ; fetch record address into DE
ld a,(de)
ld l,a ; HL <- continuation record number
inc de
ld a,(de)
ld h,a
inc de
push hl ; save continuation record number
ld a,(de)
ld l,a
inc de
ld a,(de)
ld h,a
inc de
ld (NXTREC),hl ; NXTREC <- next line record#
ld a,(de)
ld l,a
inc de
ld a,(de)
ld h,a
inc de
ld (PRVREC),hl ; PRVREC <- prev line record#
pop hl ; pop continuation record number
ld b,10 ; get head-record text field size into B
ld c,0 ; C = total line length
exp1: ex (sp),hl ; push cont rec#, pop dest ptr
exp2: ld a,(de) ; get byte from text record
inc de
or a
jp p,exp4 ; jump if hi-bit not set
and 7Fh ; else is a compressed space, lo 7 bits = cnt
exp3: ld (hl),' ' ; store space(s)
inc hl
inc c
dec a
jr nz,exp3 ; loop
jr exp5
exp4: cp TAB ; TAB?
jr nz,exp43 ; branch if not
ld a,c
or 7
inc a
sub c
dec a
jr z,exp42
exp41: ld (hl),SSPC ; else store a sequence of soft spaces
inc hl
inc c
dec a
jr nz,exp41
exp42: ld (hl),TAB ; ended by a tab
inc hl
inc c
jr exp5
exp43: ld (hl),a ; store character
inc hl
inc c
cp CR ; CR? (end of line)
jr z,exp7 ; if yes, add a LF and return
exp5: djnz exp2 ; loop until whole record has been processed
ex (sp),hl ; push dest ptr, pop cont rec#
ld a,h
or l ; any continuation record?
jr z,exp6 ; jump if not
call LDREC ; else get it
ld b,14 ; B <- continuation record text field size
ld a,(de)
ld l,a ; HL <- continuation record number
inc de
ld a,(de)
ld h,a
inc de
jr exp1 ; loop to process this record
exp6: ex (sp),hl ; push cont rec#, pop dest ptr
ld (hl),CR ; CR
inc c
inc hl
exp7: ld (hl),LF ; LF
inc hl
inc c
ld a,c
ld (EDLEN),a ; store length in EDLEN
pop de ; discard dest ptr
pop bc
pop de
pop hl
ret
; Store line into record(s). Called with:
; HL = record number to store the line into
; DE = address of line to store (zero to store an empty line)
; Returns CY set on error.
STORE: push hl
push de
push bc
ld c,e
ld b,d ; get text line address into BC
ld (EDREC),hl ; store record # of current line into EDREC
ld (OLDREC),hl
call LDRECD ; get record address into DE
push de ; save this record address
inc de ; skip continuation record number field
inc de
ld a,(de)
ld l,a ; load next record number into HL
inc de
ld a,(de)
ld h,a
inc de
ld (NXTREC),hl ; store next record number in NXTREC
ld a,(de)
ld l,a ; load prev record number into HL
inc de
ld a,(de)
ld h,a
inc de
ld (PRVREC),hl ; store prev record number in PRVREC
ld l,c ; get text line address into HL
ld h,b
ld b,10 ; 10 = head record text field size
xor a
ld (EDLEN),a ; clear current line length
ld a,h
or l ; storing empty line?
jr z,sto3 ; jump if yes
sto1: ld a,(EDLEN)
cp MAXLEN ; max line length
jr nc,sto3 ; jump if EDLEN >= max length
ld a,(hl) ; get char
inc hl
cp SSPC ; soft space?
jr z,sto1 ; ignore
and 7Fh ; strip hi-bit, leave ASCII code
cp 7Fh ; DEL?
jr z,sto1 ; loop if yes - ignore
cp 0Ch ; ^L?
jr z,sto1 ; ignore as well
cp ' ' ; printable char?
jr nc,sto5 ; jump if yes
or a ; null?
jr z,sto1 ; ignore
cp CR ; CR?
jr z,sto1 ; ignore
cp TAB ; TAB?
jr nz,sto2 ; jump if not
ld c,a
ld a,(EDLEN)
or 7
inc a ; adjust length for tab expansion
ld (EDLEN),a
ld a,c
jr sto8
sto2: cp LF ; LF?
jr z,sto3 ; set end of line if yes
cp 0Ch ; ^L?
jr z,sto3
cp 0Bh ; ^K?
jr nz,sto4 ; any other control char becomes '?'
sto3: ld a,CR ; CR
jr sto7
sto4: ld a,'?' ; '?'
jr sto7
sto5: jr nz,sto7 ; jump if not space
ld c,0 ; reg C will count spaces
sto6: inc c
ld a,(hl)
inc hl
cp ' ' ; space?
jr z,sto6 ; keep compressing if yes, keep length in C
dec hl
ld a,(EDLEN)
add a,c
ld (EDLEN),a
ld a,c
or 80h ; set hi-bit (compressed space)
jr sto8
sto7: ld c,a
ld a,(EDLEN)
inc a
ld (EDLEN),a
ld a,c
sto8: inc b
dec b ; end of this record?
jr nz,sto11 ; jump if not
cp CR ; last char is a CR?
jr z,sto12 ; return if yes, no need to store it
ld c,a ; remember char
ex (sp),hl ; push line ptr, pop saved record address
ld e,(hl) ; get cont record # from saved record address
inc hl
ld d,(hl)
ld a,d
or e
ex de,hl
jr nz,sto9 ; jump if not zero, use that
call ALLREC ; else alloc new record
push hl ; remember it
ld hl,(OLDREC)
call LDRECD ; get record address into DE
pop hl ; pop new record number
ld a,l
ld (de),a ; store continuation record number
inc de
ld a,h
ld (de),a
sto9: ld (OLDREC),hl
ld a,h ; zero continuation record number?
or l
jr nz,sto10 ; jump if not
pop de
scf ; else set error flag