-
Notifications
You must be signed in to change notification settings - Fork 5
/
scrn.mac
2247 lines (2077 loc) · 52 KB
/
scrn.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 Screen Editor
include TED.INC
HSIZE equ 80 ; terminal window width
VSIZE equ 24 ; terminal window height
;=======================================================================
; S C R E E N E D I T O R S E C T I O N
public SCRNED,SCRX,UFLAG,UPDST
extrn FSTREC,CURREC,CURLN,CURPOS,LASTLN,XLTESC
extrn WFOV,DELBUF,CLNCR,PUTCN,GETC,TMPBUF,PSTRN
extrn STORE,FNDBUF,LDREC,PUTC,EDLEN,INSBUF,EDREC
extrn NXTREC,MLOOP,EXPAND,EDITBF,HLDEC,FATAL,CLS
extrn HOME,CLREOL,CLREOS,PUTCUR,CSRUP,CSRDN,CSRLFT
extrn CSRRGT,FWDIDX,REVIDX,CPDEHL,ADDHLA,SUBHLA
extrn SCRLRG,HASSRG
;-----------------------------------------------------------------------
cseg
; Enter screen editor
SCRNED: call CLS ; clear screen and home cursor
ld hl,(CURLN) ; check current line number
ld a,h
or l
jr nz,sce2 ; jump if not zero (use it)
ld hl,(FSTREC) ; else check number of first record of text
ld a,h
or l
jr nz,sce1 ; jump if not zero (use that instead)
ld bc,0
ld de,0 ; zero means insert empty line
ld hl,0 ; zero means insert at the top of text buffer
call INSBUF ; insert line into text buffer
jr nc,sce1
ld hl,24 ; fatal error 24 - INSBUF error
jp FATAL
sce1: ld hl,1
ld (CURLN),hl ; set current line to 1
ld hl,(FSTREC)
ld (CURREC),hl ; current record # <- 1st record #
sce2: ld a,(CURPOS) ; get last cursor column
ld (CURX),a ; set character index
ld hl,(CURLN) ; get current line into HL
call DSPLY ; display screen
call HASSRG ; terminal supports scroll region?
ld l,0
ld h,VSIZE-3
call nz,SCRLRG ; set region if yes
call SETCUR ; postion cursor
SLOOP: ld a,(CURY) ; get cursor line
ld c,a
ld hl,(TOPLN)
call ADDHLA ; add it to top line number (obtain line #)
ld (CURLN),hl ; and into CURLN (current line number)
ld a,c ; get cursor line back into A
add a,a ; obtain address offset
ld hl,LNADRS
call ADDHLA ; index into table
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ld (CURREC),hl ; store value as current record number
call GETC ; get input char into A
cp ' ' ; control char?
jr c,SCTRL ; jump if yes
cp 7Fh ; DEL?
jr nz,sce5 ; jump if not
call DELCHL ; else handle it
jr SLOOP ; loop
sce5: call DOCHR ; handle printable char
jr SLOOP ; loop
SCTRL: ld hl,SLOOP
push hl ; push return address
add a,a ; obtain address offset
ld hl,CTLTAB
call ADDHLA ; index into table
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
jp (hl) ; call handler
; Control commands in screen mode
CTLTAB: dw NOOP ; ^@ no-op
dw NOOP ; ^A no-op
dw BROWSE ; ^B browse mode
dw EXSCR ; ^C exit to command mode
dw CURRGT ; ^D cursor right
dw CURUP ; ^E cursor up
dw NXTTAB ; ^F move cursor to next tab stop
dw DELCHR ; ^G delete char right
dw DELCHL ; ^H delete char left
dw DOTAB ; ^I tab
dw INSLNB ; ^J insert new line below
dw INSLNA ; ^K insert line above
dw CURBGL ; ^L cursor begin/end of line
dw SPLIT ; ^M split line at cursor pos
dw BROWSE ; ^N browse mode
dw NOOP ; ^O no-op
dw DUPLN ; ^P duplicate line
dw PRVTAB ; ^Q move cursor to prev tab stop
dw REFRSH ; ^R refresh
dw CURLFT ; ^S cursor left
dw CURTB ; ^T cursor top/bottom
dw NOOP ; ^U no-op
dw INSOVR ; ^V toggle insert mode
dw NOOP ; ^W no-op
dw CURDN ; ^X cursor down
dw DELLN ; ^Y delete line
dw EXSCR ; ^Z exit to command mode
dw DOESC ; ^[ escape sequence
dw NOOP ; ^\ no-op
dw DELCHR ; ^] delete char right
dw DELLN ; ^^ delete line
dw REFRSH ; ^_ refresh screen
; ^C or ^Z - exit screen editor to command mode
EXSCR: call STLREC ; store edited line back into records
call HASSRG ; terminal supports scroll region?
ld hl,0FFFFh
call nz,SCRLRG ; reset region if yes
call CLS ; clear screen and home cursor
ld a,(CURX) ; get character index
ld (CURPOS),a ; store result into CURPOS
jp MLOOP ; go to main loop
SCRERR: call CLS ; clear screen and home cursor
jp WFOV ; print "WORK FILE OVERFLOW" and go back
; to command mode
; ^[ - handle escape sequence
DOESC: call XLTESC ; translate escape sequence to ctrl char
pop hl ; discard return address
jp SCTRL ; process the ctrl command
; DEL or ^H - delete char left
DELCHL: ld a,(CURX)
or a ; on leftmost column?
jp z,JOIN ; join lines if yes
ld a,(INSFLG) ; check insert mode
or a
jr z,dlc1 ; jump if overwrite mode
call LOADLC ; load line at cursor into edit buffer
ld a,(CURX) ; get character index
dec a ; backup one char
ld c,a
ld hl,EDITBF ; EDITBF = edit line buffer
call ADDHLA
ld a,(hl) ; get char
cp TAB ; tab?
jr nz,dlc03 ; branch if not, simply remove it
inc c
dlc01: dec hl ; else scan backards
dec c
jr z,dlc02
ld a,(hl)
cp SSPC ; to backup all soft spaces
jr z,dlc01
dlc02: inc hl
ld a,c
ld (CURX),a ; set new character index
call SETCUR ; position cursor
jp DELCHR ; and delete char right
dlc03: call CURLFT ; move cursor left
jp DELCHR ; and delete char right
dlc1: call CURLFT ; move cursor left
ld a,' ' ; and replace char with a space
call PUTC ; output char
call CSRLFT ; move cursor left
call LOADLC ; load line at cursor into edit buffer
ld a,(CURX) ; get character index
ld c,a ; into C
ld hl,EDITBF ; EDITBF = edit line buffer
call ADDHLA
ld a,(hl)
cp ' ' ; space already there?
ret z ; return if yes
ld (hl),' ' ; else store space
ld a,1
ld (LNMODF),a ; set modified flag
ld a,c ; get char index back in A
ld hl,CURLEN
cp (hl) ; compare with current line length
ret c ; return if lower
inc a
ld (hl),a ; else set new length to A+1
ret
; Join lines
JOIN: ld hl,(TOPLN)
ld a,(CURY)
call ADDHLA
dec hl
ld a,h ; at the top of file?
or l
ret z ; return if yes (no prev line)
call LOADLC ; load cursor line into edit buffer
ld hl,EDITBF
ld de,TMPBUF
ld a,(CURLEN)
ld b,0 ; B = segment length
or a
jr z,jln21
ld c,a
jln1: ld a,(hl) ; copy current line to temp buffer
cp SSPC
jr z,jln2 ; do not store soft spaces
ld (de),a
inc de
inc b
jln2: inc hl
dec c
jr nz,jln1
jln21: push bc ; remember segment length
if 1
ld hl,(TOPLN) ; get top line number into HL
ld a,(CURY)
call ADDHLA ; add line of cursor
ld de,(LASTLN)
call CPDEHL ; compare with last text line number
push af ; remember result
call DELLN ; delete current line
pop af
call nz,CURUP ; move cursor up if line was not the last
else
call DELLN ; delete current line
call CURUP ; up one line [!!!not if line was last!!!]
endif
call LOADLC ; load line
ld a,(CURLEN)
ld (CURX),a ; position cursor after last char of line
pop bc ; get old seg length back in B
inc b
dec b
jr z,jln7 ; if nothing to append
ld c,a ; starting column in C
ld hl,EDITBF
call ADDHLA
ld de,TMPBUF
jln3: ld a,(de)
push de ;;;
cp TAB
jr nz,jln6
ld a,c ; expand tab
or 7
inc a
sub c
dec a
jr z,jln5
jln4: ld e,a ;;;
ld a,c
cp MAXLEN-1
ld a,e ;;;
jr nc,jln8 ; truncate line if too long
ld (hl),SSPC ; TODO: leave the rest on old line
inc hl
inc c
dec a
jr nz,jln4
jln5: ld a,TAB
jln6: ld e,a ;;;
ld a,c
cp MAXLEN-1
ld a,e ;;;
jr nc,jln8 ; truncate line if too long
ld (hl),a ; append old line
inc hl
pop de ;;;
inc de
inc c
djnz jln3
push de ;;;
jln8: pop de ;;;
ld a,c
ld (CURLEN),a
ld a,1
ld (LNMODF),a
call SETCUR
call LOADLC ; reload line, as SETCUR may have modif EDITBF
call DISPLE ; display line segment right of cursor
jln7: call SETCUR ; position cursor
ret
; Handle printable char
DOCHR: ld c,a ; save char
call LOADLC ; load line at cursor into edit buffer
ld a,(CURX) ; get character index
cp MAXLEN-1
ret nc
ld b,a ; into reg B
ld hl,EDITBF ; EDITBF = edit line buffer
call ADDHLA ; index into edit buffer
ld a,(INSFLG)
or a
jp z,RPLCHR ; replace char
jp INSCHR ; insert char
; Replace char in buffer
RPLCHR: ld a,(hl)
cp TAB
jr z,rplts ; if current location contains a tab
cp SSPC
jr z,rplts ; if current location contains a soft space
cp c
jr z,rplc1 ; if same char
rplc0: ld (hl),c ; else replace char
ld a,1
ld (LNMODF),a ; set modified flag
rplc1: ld a,c
cp TAB
jr nz,rplc11
ld a,' '
rplc11: call PUTC
ld a,b ; get char index back into A
ld hl,CURLEN
cp (hl) ; compare with current line length
jr c,rplc2 ; jump if lower
inc a
ld (hl),a ; else set new length to A+1
rplc2: ld a,(SCRX)
ld c,a
ld a,(CURX)
inc a
;; cp MAXLEN-1
;; ret nc
ld (CURX),a ; inc cursor column
ld b,a
sub c ; compute screen cursor column
cp HSIZE-1 ; cursor on last column of the screen?
call nc,SETCUR ; shift screen left if yes
ret
rplts: push bc
push hl
call RMSSPC
pop hl
pop bc
jp rplc0
; Insert char into buffer. Called with C = char.
INSCHR: ld a,(hl)
cp TAB
jr z,insc1 ; if current location contains a tab
cp SSPC
jr z,insc2 ; if current location contains a soft space
insc: ld d,1
call SHRLN ; shift line right to make space for char
if 0
ld a,(CURLEN)
cp MAXLEN
jp nz,rplc0 ; store char and redisplay line segment
inc a
ld (CURLEN),a
else
ld a,c
cp ' '
ld e,b
call nz,ADJLEN
endif
jp rplc0
insc1: push hl
call RMSSPC
pop hl
inc hl
ld e,b
call SHRLN8
call DISPLE
call SETCUR
dec hl
jp rplc0
insc2: push hl
call RMSSPC
pop hl
jp rplc0
; ^V - toggle insert mode
INSOVR: ld a,(INSFLG) ; toggle insert flag
cpl
ld (INSFLG),a
call SHOWST ; show new mode
call SETCUR ; restore cursor position
NOOP: ret
; ^I (TAB) - insert tab
DOTAB: call LOADLC ; load line at cursor into edit buffer
ld a,(CURX) ; get character index
ld b,a
ld hl,EDITBF ; EDITBF = edit line buffer
call ADDHLA ; index into edit buffer
ld a,(INSFLG)
or a
jp z,RPLTAB ; replace with tab
jp INSTAB ; insert tab
; Replace char with tab
RPLTAB: ld a,b
and 7
cp 7
ld c,TAB
jr z,rplt2
ld c,SSPC
rplt2: ld a,(hl)
cp c
jr z,rplt3
ld (hl),c ; insert char
ld a,1
ld (LNMODF),a
rplt3: inc hl
inc b
ld a,' '
call PUTC
ld a,(CURX)
inc a
cp MAXLEN ; don't go above MAXLEN
ret nc
ld (CURX),a
ld a,b
and 7
jr nz,RPLTAB
ret
; insert tab into edit buffer
INSTAB: ld e,b ; save current index in reg E
ld a,(hl)
ld c,a
cp TAB ; if the current pos contains a tab
jp z,insc ; then handle like normal char
cp SSPC ; if a soft space
jp z,inst3 ; then is easier (no text segment to move)
ld a,e
or 7
inc a
ld c,a ; new column = new tab stop
sub e ; compute distance to next tab stop (n)
ld d,a
call SHRLN ; shift line right n columns
inst8: dec d
jr z,inst9
ld (hl),SSPC ; enter new tab field
inc hl
jr inst8
inst9: ld (hl),TAB
ld e,c ; set new column
dec e
call ADJLEN
jr inst10
inst3: inc hl
inc e
ld a,e
and 7
jr nz,inst3 ; advance cursor to next tab stop
call SHRLN8 ; insert new full tab field (tab + 7 soft spc)
inst10: ld a,1
ld (LNMODF),a
call DISPLE
ld a,(CURX)
or 7
inc a
ld (CURX),a
;; ld b,a
;; ld a,(SCRX)
;; ld c,a
;; ld a,b
;; sub c
;; cp HSIZE-1
call SETCUR ; if > screen width, shift screen right
ret
; Shift line 1..7 columns right (value in reg D) starting from current
; column. Expand or contract tabs accordingly. Assumes that the line has
; been loaded (LOADLC routine called).
SHRLN: push hl
push de
push bc
ld a,(CURX) ; get character index
ld c,a
ld e,a
ld hl,EDITBF ; get address of edit line buffer into HL
call ADDHLA ; obtain ptr to current pos
ld a,(CURLEN) ; get current line length
IF 1
inc c ; this so we can use jc instead of jc+jz below
sub c ; subtract current index
jr c,shr4 ; return if curr index >= curr length
inc a ; obtain tail length
ELSE
sub c
jr c,shr4
jr z,shr4
ENDIF
ld c,a ; C = tail length
ld b,0 ; B = segment length
push de
shr1: ld a,(hl) ; scan line forward
inc hl
inc e
inc b
cp SSPC ; soft space?
jr z,shr2 ; remove it
cp TAB ; tab?
jr z,shrtb ; insert new tab field
dec c
jr nz,shr1 ; else loop until end of line
ld a,e
add a,d
ld e,a
ld a,d
call ADDHLA
dec hl
dec e
call ADJLEN
ld a,e
cp MAXLEN
jr c,shr21
ld c,a
ld a,l
sub c
ld l,a
ld a,h
sbc a,0
ld h,a
dec hl
jr shr21
shrtb: call SHRLN8 ; insert full tab field here
shr2: dec d
jr nz,shr1
dec hl
dec b
shr21: pop de
inc b
dec b
jr z,shr31
ex de,hl ; dst in DE, columns to shift in H
ld a,e
sub h
ld l,a
ld a,d
sbc a,0
ld h,a ; src in HL
shr3: ld a,(hl) ; shift segment
ld (de),a
dec de
dec hl
djnz shr3 ; loop
shr31: ld a,1
ld (LNMODF),a ; set modified flag
call DISPLE ; display line segment from cursor to end
call SETCUR ; position cursor
shr4: pop bc
pop de
pop hl
ret
; Shift line from current pos to the end 8 positions to the right,
; insert 7 soft spaces and a tab into the new opening. Called with
; HL = current pos, E = current index
SHRLN8: push bc
push de
push hl ; remember start
ld a,(CURLEN)
sub e ; if past current end of line, nothing to do
jr c,shrt5 ; thus return
jr z,shrt5
ld c,a
ld b,0 ; BC = segment length
ld a,e
add a,8
cp MAXLEN-1 ; if src+8 is above limit - nothing to copy,
jr nc,shrt3 ; all chars are lost
ld a,(CURLEN)
add a,8
sub MAXLEN ; if dst+8 is above limit, then crop length
jr c,shrt1
ld a,MAXLEN-8
sub e
ld c,a
shrt1: add hl,bc
dec hl ; src in HL
ex de,hl
ld hl,8
add hl,de
ex de,hl ; dst in DE
lddr ; move segment up
shrt3: ld a,(CURLEN)
add a,7
ld e,a
call ADJLEN
pop hl ; restore start
push hl
ld b,7
ld a,SSPC
shrt4: ld (hl),a ; insert 7 soft spaces
inc hl
djnz shrt4
ld (hl),TAB ; and a tab
shrt5: pop hl ; return HL = current pos
pop de
pop bc
ret
; Shift line 1..n columns left (value in reg D) starting from current
; position +n, expanding or contracting tabs accordingly. Assumes that
; the line has been already loaded (LOADLC called) and that the char at
; current position +n is not a soft space.
SHLLN: push bc
push de
push hl
ld a,(CURX) ; get character index
ld c,a ; obtain current index
ld e,c
ld hl,EDITBF ; get address of edit line buffer into HL
call ADDHLA ; obtain ptr to current pos
ld a,(CURLEN) ; get current line length
sub c ; subtract current index
jr c,shl6 ; return if at, or past end of line
jr z,shl6
push de ; save n
sub d
ld c,a ; C = tail length
ex de,hl ; DE = dst
jr c,shl2
jr z,shl2 ; branch if tail too short (disappears)
ld a,l
add a,h
ld b,a ; B = src index
ld l,h
ld h,0
add hl,de ; HL = src
shl1: ld a,(hl) ; scan line forward
cp SSPC
jr z,shltb ; until tab
cp TAB
jr z,shltb ; or soft space found
ld (de),a ; shift tail left
inc hl
inc de
inc b
dec c
jr nz,shl1 ; or until end of line
shl2: pop bc ; restore n
ld a,(CURLEN)
sub b
ld (CURLEN),a
ld a,' '
shl5: ld (de),a ; erase gap after tail
inc de
djnz shl5
shl6: pop hl
pop de
pop bc
ret
shltb: pop de
ld e,b
shl7: ld a,e
and 7 ; see if we are at a tab sotop
ld a,SSPC
jr nz,shl3
call SHLLN8 ; and remove old tab field ("contract" tab)
ld a,TAB
shl3: dec e
dec hl
ld (hl),a
dec d
jr nz,shl7
jr shl6
; Shift line from current pos +8 to the end 8 columns to the left.
; Callwed with HL = current pos, E = current index
SHLLN8: ld a,(CURLEN)
sub e
ret c
ret z
push hl ; remember current pos
push de
sub 8
jr c,shlt3
jr z,shlt3
ld c,a ; BC = segment length
ld b,0
ex de,hl ; dst start in DE
ld hl,8
add hl,de ; src start in HL
ldir ; move segment down
xor a
ex de,hl
shlt3: add a,8
shlt4: ld (hl),' ' ; erase last chars
inc hl
dec a
jr nz,shlt4
ld hl,CURLEN
ld a,(hl)
sub 8
jr nc,shlt6
xor a
shlt6: ld (hl),a
pop de
pop hl ; return HL = current pos
ret
; Adjust CURLEN according to value in E
ADJLEN: push hl
ld hl,CURLEN
ld a,e
inc a
cp (hl)
jr c,adj1
cp MAXLEN
jr c,adj2
ld a,MAXLEN
adj2: ld (hl),a
adj1: pop hl
ret
; ^G or ^] - delete char right
DELCHR: call LOADLC ; load line at cursor into edit buffer
ld a,(CURX) ; get character index
ld c,a
ld hl,EDITBF ; get address of edit line buffer into HL
call ADDHLA ; obtain ptr to current char
ld a,(CURLEN) ; get current line length
inc c ; this so we can use jc instead of jc+jz below
sub c ; subtract current index
ret c ; return if curr index >= curr length
dec c
ld a,(hl)
cp TAB
jr z,delc1
cp SSPC
jr z,delc2
dch1: ld d,1
call SHLLN
dch2: call DISPLE ; display line segment from cursor to end
call SETCUR
ld a,1
ld (LNMODF),a ; set modified flag
ret
delc1: push hl
call RMSSPC ; replace backwars soft-spaces by spaces
pop hl
jp dch1
delc2: ld a,c
or 7
inc a
sub c
ld d,a
call SHLLN
call RMSSPC
jr dch2
; ^S - cursor left
CURLFT: ld a,(CURX)
or a
ret z
dec a
ld (CURX),a
ld c,a
ld a,(SCRX)
ld b,a
ld a,c ; check screen column
sub b
jp nc,CSRLFT ; if >= 0 move cursor left and return
jp SETCUR
; ^D - cursor right
CURRGT: ld a,(CURX)
cp MAXLEN-1 ; cursor on last column?
ret nc
inc a
ld (CURX),a ; else increment column value
ld c,a
ld a,(SCRX)
ld b,a
ld a,c
sub b ; check screen column
cp HSIZE-1
jp c,CSRRGT ; if < HSIZE move cursor right and return
jp SETCUR
; ^Q - move cursor to prev tab stop
PRVTAB: ld a,(CURX) ; get character index
ld b,a ; in reg B
or a
jr z,pt1 ; jump if zero (already at line start)
dec a
and 0F8h ; else compute prev tab column
pt1: inc c ; (for single jnc below)
cp c ; compare with current screen left offset
dec c
jr nc,pt2 ; jump if greater
ld a,c ; else get screen left offset
pt2: sub b ; obtain distance
cpl
inc a ;;; optimize
cp 1
ret m ; return if <= 0
ld b,a
ld a,(CURX) ; compute new cursor column
sub b ;;;
ld (CURX),a
call SETCUR ; position cursor
ret
; ^F - move cursor to next tab stop
NXTTAB: push bc
ld a,(CURX) ; get char index
ld c,a
or 7
inc a ; compute next tab column
cp MAXLEN ; beyond right end of line?
jr c,nt1 ; jump if not
ld a,MAXLEN ; else force last column
dec a
nt1: sub c ; compute distance to next tab stop
jp m,nt2 ; return if zero or negative
ld c,a
ld a,(CURX) ; else add it to cursor column
add a,c
ld (CURX),a
call SETCUR ; position cursor
nt2: pop bc
ret
; ^L - move cursor to begin/end of line
CURBGL: ld a,(CURX)
or a
ld a,0
jr nz,cbe1
call LOADLC
ld a,(CURLEN)
cbe1: ld (CURX),a
jp SETCUR ; position cursor and return
; ^M (CR) - split line
SPLIT: call LOADLC ; load line at cursor into edit buffer
ld a,(CURLEN) ; get current line length
ld b,a ; into reg B
ld a,(CURX) ; get current char index
ld c,a
ld hl,EDITBF ; current line buffer
call ADDHLA ; HL = ptr to current pos in edit buffer
ld de,TMPBUF ; DE = address of temp buffer
push bc ; save current index (reg C)
ld a,b
inc c ; so we can use 'jc' instead of 'jc+jz' below
sub c ; compute segment length
ld b,0
jr c,splt3 ; skip if <= 0
inc a
ld c,a
splt1: ld a,(hl) ; copy segment to temp buffer
cp SSPC
jr z,splt2 ; ignore soft spaces
ld (de),a
inc de
inc b
splt2: inc hl
dec c
jr nz,splt1
splt3: ld a,b
pop bc ; restore current column (reg C)
ld b,a ; remember segment length in reg B
ld a,c
ld (CURLEN),a ; truncate old line to current pos
call CLREOL ; clear to end of line
ld a,1
ld (LNMODF),a ; set modified flag for the old line
call INSLNB ; insert new line below
call LOADLC ; load line at cursor into edit buffer
ld c,0 ; starting column in reg C
inc b
dec b
jr z,splt8 ; if segment empty
ld hl,EDITBF ; address of edit line buffer
ld de,TMPBUF ; DE = ptr to temp buffer
splt4: ld a,(de)
cp TAB
jr nz,splt7
ld a,c ; expand tabs
or 7
inc a
sub c
dec a
jr z,splt6
splt5: ld (hl),SSPC
inc hl
inc c
dec a
jr nz,splt5
splt6: ld a,TAB
splt7: ld (hl),a ; copy segment to start of new line
inc hl
inc de
inc c
djnz splt4
ld a,1
ld (LNMODF),a ; set modified flag for the new line
splt8: ld a,c
ld (CURLEN),a
call DISPLB ; display line in edit buffer
xor a
ld (CURX),a ; reset cursor to start of line
call SETCUR ; position cursor
ret
; ^P - duplicate line
DUPLN: push hl
call STLREC ; store edited line back into records
ld a,(CURY) ; get cursor line
add a,a ; obtain address offset
ld hl,LNADRS
call ADDHLA ; index into record numbers table
ld a,(hl) ; get line record number from table into HL
inc hl
ld h,(hl)
ld l,a
call INSLNB ; insert blank line below
call LOADLN ; load line into edit buffer
call DISPLB ; display line in edit buffer
call SETCUR ; position cursor
ld a,(CURY) ; get cursor line
add a,a ; obtain address offset
ld hl,LNADRS
call ADDHLA ; index into record numbers table
ld a,(hl) ; get line record number from table into HL
inc hl