-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnano.asm
2000 lines (1916 loc) · 52 KB
/
nano.asm
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
;
; Title: Nano - Main
; Author: Lennart Benschop
; Created: 02/01/2023
; Modified 06/01/2023 version 0.01 basic edit functions working.
; 05/03/2023 version 0.04 Adapted to new VDP codes./
.ASSUME ADL = 1
INCLUDE "mos_api.inc"
ORG $b0000 ; Is a moslet
MACRO PROGNAME
ASCIZ "nano.bin"
ENDMACRO
include "init.inc"
include "parse.inc"
include "output.inc"
; This is a simple text editor, with a user interface similar to "GNU nano" on Linux.
; But this editor runs on Agon and is written in assembly.
;
; Features:
; - Can run as a MOS command
; - Optionally takes a buffer address to allow editing larger files (up to 450kB).
; - Optionally takes a line number argument, so we can look at a file at a specific line.
; - Edits single file only.
; - Renames old file into backup file when saving.
; - Normal behaviour of cursor keys, home, end, page up, page down, backspace, delete and Enter.
; - Forward/backward search.
; - can insert other file into text.
; - Cut/past of whole lines.
; - Normalizes all files to DOS-style CR-LF, removes control characters (except TAB), last line always had CR-LF line ending.
;
; Design decisions:
; - Correctness over user-friendliness or efficiency. Enforce some invariants.
; - All lines contain no control characters except TAB.
; - All lines are 253 bytes or less (excluding terminationg CR-LF). During loading insert extra
; line terminators in longer lines, during editing refuse to add more characters.
; - All lines, including the last line are terminated by CR-LF.
; ' - cur_lineaddr/top_lineaddr always point to the start of a line in the edit buffer,
; the corresponding line number variables are always correct.
; - Edit file is contiguous text in file buffer.
; - Lines are copied to line buffer while being edited, moved back into text when done editing that line.
; When line of different size is moved back into text, all text after it needs to be moved up/down,
; but that is acceptable as it is only done once per edited line.
; - Cut/paste only of whole lines, cut lines are collected at end of edit buffer.
; - Minimize redraw of whole screen. Not while editing on same line, not while moving cursor inside visible screen area.
; Screen redraw is fast enough if only done when lines are added/removed or when curor moves outside visible screen area.
;
; The main routine
; IXU: argv - pointer to array of parameters
; C: argc - number of parameters
; Returns:
; HL: Error code, or 0 if OK
;
_main: LD HL, 1
LD (start_lineno), HL
LD A, C
CP #2
JR Z, main_defaultbuf
CP #3
JR Z, main_bufparam
CP #4
JR Z, main_linenum
LD HL, s_USAGE ; Number of args not 1, 2 or 3, print usage string and exit
CALL Print_String
LD HL, 19
RET
main_defaultbuf: LD HL, default_buf
LD (buf_start), HL
LD HL, default_buf_end
LD (buf_end), HL
JR main1
main_linenum: LD HL, (IX+9)
CALL ASC_TO_NUMBER
LD (start_lineno), DE
main_bufparam: LD HL, (IX+6)
CALL ASC_TO_NUMBER
JR NC, main_badbuf
LD HL, 03ffffh
AND A
SBC HL, DE
JR NC, main_badbuf
LD HL, 0b0000h
AND A
SBC HL, DE
JR C, main_badbuf
LD (buf_start), DE
LD HL, 0B0000h
LD (buf_end), HL
JR main1
main_badbuf: LD HL, s_BADADDR
CALL Print_String
LD HL, 19
RET
main1: LD HL, (IX+3)
; Copy the file name to a local buffer 'filename', so we can edit it on saving
LD DE, filename
@@: LD A, (HL)
LD (DE), A
INC HL
INC DE
AND A
JR NZ, @B
CALL Setup_Screen
LD HL, filename
LD DE, (buf_start)
CALL Load_File
CP 0ffh
JP Z, main_full
AND A
JP NZ, main_fatal
LD HL, (buf_end)
LD (cutbuf_start), HL ; Empty cut buffer
LD HL, (buf_start)
LD (cur_lineaddr), HL
LD (top_lineaddr), HL
LD DE, (load_filesize)
ADD HL, DE
LD (text_end), HL
LD HL, (cutbuf_lines)
LD (num_lines), HL
LD HL, 1
LD (cur_lineno), HL
LD (top_lineno), HL
DEC HL
LD (cutbuf_lines), HL
XOR A
LD (cursor_row), A
LD (cursor_col), A
LD (cursor_col_max), A
LD (linebuf_scrolled), A
LD (file_modified), A
LD (curline_stat), A
LD (findstring), A
CALL Show_Screen
LD BC, (start_lineno)
DEC BC
LD IX, cur_lineaddr
CALL Lines_Forward
CALL Adjust_Top
LD (save_sp), SP ; Save the stack pointer so we can return here in case of error.
; Main editor loop
main_loop: XOR A
LD (cut_continue), A ; No continued cut/copy by default.
; Return her after cut/copy, so repeated keypress will add more lines to buffer.
main_loop_continue: CALL Read_Key
CP 127
JP Z, do_backspace ; Backspace key.
CP 32
JP NC, do_ins_char ; Insert all caracters 32..255, except delete.
LD E, A
ADD A, A
ADD A, E
LD DE, 0
LD E, A
LD HL, main_dispatch
ADD HL, DE
LD HL, (HL)
JP (HL)
main_full: CALL Restore_Screen
LD HL, s_ERROR_BUF
CALL Print_String
LD HL, 12
RET
main_fatal: LD HL, 0
LD L, A ; Pass error returned by mos_save
PUSH HL
CALL Restore_Screen
POP HL
EX DE, HL
LD HL, s_FATAL
CALL Print_String
EX DE, HL
RET
main_dispatch: ; Dispatch table for all control characters/
DL main_loop ; NULL character, cannot happen
DL do_line_start ; ^A start of line
DL do_cur_left ; ^B cursor left
DL do_copy ; ^C copy current line ;
DL do_delete ; ^D delete to right
DL do_line_end ; ^E end of line
DL do_cur_right ; ^F cursor right
DL do_help ; ^G show help
DL do_goto ; ^H goto line
DL do_tab ; ^I TAB
DL main_loop ;
DL do_cut ; ^K cut current line
DL do_center ; ^L screen center
DL do_enter ; ^M Enter,
DL do_cur_down ; ^N cursor down
DL do_save ; ^O write file
DL do_cur_up ; ^P cursor up
DL do_rfind ; ^Q find backwards
DL do_readfile ; ^R read file
DL main_loop ;
DL do_special ; ^T insert special character
DL do_paste ; ^U paste
DL do_page_down ; ^V page down
DL do_find ; ^W search forward
DL do_exit ; ^X Exit
DL do_page_up ; ^Y page up
DL main_loop ;
DL do_exit ; ESC Exit
DL main_loop ; 28
DL main_loop ; 29
DL main_loop ; 30
DL main_loop ; 31 ^/ should be goto line but impossible to get this key.
; Handlers for all keys
; Backspace key, delete to left, can join lines if at start of line.
do_backspace: CALL Enter_Current
LD A, (linebuf_editpos)
AND A
JR Z, backspace_joinlines ; At start of line: join lines together.
DEC A
LD (linebuf_editpos), A ; move one position beck before deleting the character.
CALL Delete_Char
CALL Col_From_Editpos
CALL Render_Current_Line
;CALL Show_Status
CALL Show_Cursor
JP main_loop
backspace_joinlines: ; Move to the previous line and delete the newline at its end.
LD HL, (cur_lineno)
LD DE, 1
AND A
SBC HL, DE
JP Z, main_loop ; Don't move if already at start.
CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Backward
CALL Adjust_Top ; Move to previous line.
CALL Enter_Current ; Copy that to the line buffer
LD A, (curline_length)
DEC A
DEC A
LD (linebuf_editpos), A ; Move to the end of it
CALL Col_From_Editpos
JR delete_joinlines
; Delete key (^D): delete to right, can join lines if at end of line.
do_delete: CALL Enter_Current
LD A, (curline_length)
DEC A
DEC A
LD C, A
LD A, (linebuf_editpos)
CP C
JR Z, delete_joinlines ; At end of line: join lines together
CALL Delete_Char
CALL Col_From_Editpos
CALL Render_Current_Line
;CALL Show_Status
CALL Show_Cursor
JP main_loop
delete_joinlines: ; About to join current and next line. First check if it is allowed
LD HL, (cur_lineno)
LD DE, (num_lines)
AND A
SBC HL, DE
JP Z, main_loop ; Not allowed if last line of file.
LD HL, (cur_lineaddr)
CALL Next_LineAddr ; Step to end of current line.
PUSH HL
CALL Next_LineAddr ; Step to end of next line
POP DE
AND A
SBC HL, DE ; Find length of next line.
LD DE, 0
LD A, (curline_length)
LD E, A
ADD HL, DE ; Add current line length.
DEC HL
DEC HL ; Subtract two, we are going to remove CR-LF
LD A, H
AND A ; Check that HL >= 256, joined line would be too long.
JR NZ, Line_Too_Long
; Now we are allowed to join the lines
LD A, (curline_length)
DEC A
DEC A
LD (curline_length), A ; Sutract 2 fron length, effectively removing the trailing CRLF
LD A, 2
LD (curline_stat), A ; Mark current line as modified.
CALL Leave_Current ; Put the truncated line back into text (without its trailing CRLF)
LD HL, (num_lines)
DEC HL
LD (num_lines), HL ; Decrement number of lines.
CALL Show_Screen
JP main_loop
; TAB key, insert TAB character
do_tab: LD A, 9
; Keys corresponding to printable characters, insert into text
do_ins_char: PUSH AF
CALL Enter_Current
POP AF
EX AF, AF'
LD A, (curline_length)
CP 255
JR Z, Line_Too_Long
EX AF, AF'
CALL Insert_Char
CALL Col_From_Editpos
CALL Render_Current_Line
;CALL Show_Status
CALL Show_Cursor
JP main_loop
Line_Too_Long: CALL Clear_BottomRow
LD HL, s_LINETOOLONG
CALL Print_String
JP main_loop
Memory_Full: LD SP, (save_sp) ; Can get here from within a subroutine, restore stack.
CALL Adjust_Top
CALL Show_Screen
CALL Clear_BottomRow
LD HL, s_NOMEM
CALL Print_String
JP main_loop
Load_Error: CALL Clear_BottomRow
LD HL, s_LOADERR
CALL Print_String
JP main_loop
; ENTER key, add new line after current line/split current line.
; Simply insert CR-LF into the current line, put the line back into the text
; and increase nun_lines..
; One issue with this is: if the current length is greater than 253, we cannot insert CRLF,
; so we cannot split the line when we might need it most.
do_enter: CALL Enter_Current
LD A, (curline_length)
CP 254
JR NC, Line_Too_Long
LD A, 13
CALL Insert_Char
LD A, 10
CALL Insert_Char
CALL Leave_Current ; Causes the line with the additional CR-LF to be put in the text.
LD HL, (num_lines)
INC HL
LD (num_lines), HL ; Increment number of lines.
XOR A
LD (cursor_col_max), A ; Move to the start of the line.
LD IX, cur_lineaddr
LD BC, 1 ; Move to the next line.
CALL Lines_Forward
CALL Adjust_Top
CALL Show_Screen ; Redraw the entire screen.
JP main_loop
; Exit key (^X or ESC). Prompt to save file if modified and exit editor.
do_exit: CALL Leave_Current
LD A, (file_modified)
AND A
JR Z, exit_nosave
LD A, 12
RST.LIL 10h
LD HL, s_ASKSAVE
CALL Print_String
CALL Read_Key
CP 'n'
JR Z, exit_nosave
CP 'N'
JR Z, exit_nosave
CALL Save_File
exit_nosave: CALL Restore_Screen
LD A, 12
RST.LIL 10h ; Clear the screen
LD HL, 0
RET
; Save key (^O). Save file, allow altering file name (save-as).
do_save: CALL Leave_Current
LD A, 12
RST.LIL 10h
CALL Save_File
CALL Show_Screen
JP main_loop
; Help key, show help screen
do_help: CALL Leave_Current
LD HL, s_HELP_Large
CALL Print_String
CALL Read_Key
CALL Show_Screen
JP main_loop
; Cursor left key (^B), move one position to the left, Go to end of previous line if at start
do_cur_left: CALL Enter_Current
LD A, (linebuf_editpos)
AND A
JR Z, left_previous
DEC A
LD (linebuf_editpos),A ; We can decrement cursor position.
CALL Col_From_Editpos
CALL Render_Current_Line
;CALL Show_Status
CALL Show_Cursor
JP main_loop
left_previous: ; From leftmost column: Move to the end of the previous line.
LD HL, (cur_lineno)
LD DE, 1
AND A
SBC HL, DE
JP Z, main_loop ; Don't move if already at start.
CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Backward
CALL Adjust_Top
JR do_line_end ; Go to end of now current line.
; Cursor right key (^F), move one position to the right, Go to start of next line if at end
do_cur_right: CALL Enter_Current
LD A, (curline_length)
DEC A
DEC A
LD B, A
LD A, (linebuf_editpos)
CP A, B
JR Z, right_next
INC A
LD (linebuf_editpos),A ; We can increment cursor position.
CALL Col_From_Editpos
CALL Render_Current_Line
;CALL Show_Status
CALL Show_Cursor
JP main_loop
right_next: ; from end of line, move to the start of the next line.
LD HL, (cur_lineno)
LD DE, (num_lines)
AND A
SBC HL, DE
JP Z, main_loop
CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Forward
CALL Adjust_Top
; Continue into do_line_start
; Home key (^A), move to start of line
do_line_start: XOR A
LD (cursor_col_max), A
LD (cursor_col), A
LD (linebuf_editpos), A
LD (linebuf_scrolled), A
CALL Render_Current_Line ; Need this in case a scrolled line was shown.
;CALL Show_Status
CALL Show_Cursor
JP main_loop
; End key (^E), move to end of line.
do_line_end: CALL Enter_Current
LD A, (curline_length)
DEC A
DEC A
LD (linebuf_editpos), A
CALL Col_From_Editpos
CALL Render_Current_Line ; Can potentially scroll the current line.
;CALL Show_Status
CALL Show_Cursor
JP main_loop
; Cursor up key (^P), move to previous line
do_cur_up: CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Backward
CALL Adjust_Top
JP main_loop
; Cursor doen key (^N), move to next line
do_cur_down: CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Forward
CALL Adjust_Top
JP main_loop
; Page Up key (^Y), move one screenful up
do_page_up: CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 0
LD A, (Current_Rows)
DEC A
DEC A ; Current_Rows-2, number of lines on screen
LD C, A
CALL Lines_Backward
CALL Adjust_Top
JP main_loop
; Page Down key (^V), move one screenful down
do_page_down: CALL Leave_Current
LD IX, cur_lineaddr
LD BC, 0
LD A, (Current_Rows)
DEC A
DEC A
LD C, A ; Current_Rows-2, number of lines on screen
CALL Lines_Forward
CALL Adjust_Top
JP main_loop
; Adjust screen (^L) such that current line is shown in the centre.
do_center: CALL Leave_Current
LD HL, (cur_lineaddr)
LD (top_lineaddr), HL
LD HL, (cur_lineno)
LD (top_lineno), HL
LD IX, top_lineaddr ; Set top line to current.
LD BC, 0
LD A, (Current_Rows)
DEC A
DEC A
SRL A
LD C, A
CALL Lines_Backward ; Move top line half screen above current line.
LD HL, (cur_lineno)
LD DE, (top_lineno)
AND A
SBC HL, DE ; Recompute cursor row as top may have hit line 1.
LD A, L
LD (cursor_row),A
CALL Show_Screen
JP main_loop
; Goto line (^H), enter line number and move to that line.
do_goto: CALL Leave_Current
CALL Clear_BottomRow
LD HL, s_GOTO
CALL Print_String
LD HL, linebuf
LD E, 1
LD BC, 10
MOSCALL mos_editline
LD HL, linebuf
CALL ASC_TO_NUMBER
PUSH DE
POP BC
DEC BC
LD IX, cur_lineaddr
LD HL, (buf_start)
LD (IX+0), HL
LD HL, 1
LD (IX+3), HL ; Set line number to 1 at start of buffer
CALL Lines_Forward ; forwarn n-1 lines
XOR A
LD (cursor_col), A
CALL Adjust_Top
JP main_loop
; Cut (^K) single line
do_cut: LD A, (cut_continue)
AND A
JR NZ, @F
LD HL, (buf_end)
LD (cutbuf_start), HL ; Clear the old cutbuffer if not continuing.
LD HL, 0
LD (cutbuf_lines), HL
@@: CALL Enter_Current
LD A, (curline_length)
PUSH AF
XOR A
LD (curline_length), A ; Set curline_length to 0, causing Leave_Current to replace current line by emptiness,
CALL Leave_NoRender ; Move current line (now of length 0) back.
LD HL, (cur_lineno)
LD DE, (num_lines)
AND A
SBC HL, DE ; Check if we are on last line
JR Z, cut_lastline
DEC DE
LD (num_lines), DE ; Store decreased number of lines.
JR cut_move
cut_lastline: LD HL, (text_end) ; We have deleted the last lien, which is not allowed, put an empty line in its place.
LD (HL), 13
INC HL
LD (HL), 10
INC HL
LD (text_end), HL
cut_move: POP AF ; Get original line length back.
LD (curline_length), A
CALL Cutbuffer_Add ; Add the line to the cut buffer.
CALL Show_Screen
LD A, 1
LD (cut_continue), A
JP main_loop_continue
; Copy (^C) single line
do_copy: LD A, (cut_continue)
AND A
JR NZ, @F
LD HL, (buf_end)
LD (cutbuf_start), HL ; Clear the old cutbuffer if not continuing.
LD HL, 0
LD (cutbuf_lines), HL
@@: CALL Enter_Current
CALL Cutbuffer_Add ; Add the line to the cutbuffer.
CALL Leave_Current ; CALL Leave_Current because we move to next line.
LD IX, cur_lineaddr
LD BC, 1
CALL Lines_Forward ; Move to next line.
CALL Adjust_Top
LD A, 1
LD (cut_continue), A
JP main_loop_continue
; Paste (^U) contents of cutbuffer
do_paste: CALL Leave_Current
LD HL, (cutbuf_start)
PUSH HL ; Keep current line address in cut buffer on the stack during the loop.
do_paste1: POP HL
PUSH HL
LD DE, (buf_end)
AND A
SBC HL, DE
JR Z, paste_end ; Did we reach the end of the paste buffer?
POP HL
PUSH HL ; stack: old_lineaddr
CALL Next_LineAddr
POP DE ;
PUSH HL
PUSH DE ; stack: new_lineaddr old_lineaddr
AND A
SBC HL, DE
LD BC, 0
LD C, L ; Determine length of first line in cut buffer.
LD A, L ; Length goes to BC for LDIR and to curline_length
LD (curline_length), A
XOR A
LD (oldcurline_length), A ; by setting oldcurline_length to 0, nothing gets overwritten, line gets inserted.
POP HL ; source address
LD DE, linebuf
LDIR ; Copy line of cutbuf to linebuf.
CALL Leave_NoRender ; Cause line to be inserted in text.
LD HL, (num_lines)
INC HL
LD (num_lines), HL
LD BC, 1
LD IX, cur_lineaddr
CALL Lines_Forward ; Skip past line that got just inserted.
JR do_paste1
paste_end: POP HL
CALL Adjust_Top
CALL Show_Screen
JP main_loop
; Read a file (^R) and isert it into the text.
do_readfile: LD HL, (buf_end)
LD (cutbuf_start), HL ; Clear the cut buffer.
CALL Leave_Current
CALL Clear_BottomRow
LD HL, s_READFILE
CALL Print_String
LD HL, linebuf
LD E, 1
LD BC, 40
MOSCALL mos_editline
LD HL, linebuf
LD DE, (text_end)
CALL Load_File
CP 0ffh
JP Z, Memory_Full
AND A
JP NZ, Load_Error
LD HL, (text_end)
PUSH HL
do_read1: LD HL, (cutbuf_lines)
LD A, H
OR L
JR Z, paste_end ; Did cutbuf_lines reach zero?
DEC HL
LD (cutbuf_lines), HL ; Decrement cutbuf_lines
POP HL
PUSH HL ; stack: old_lineaddr
CALL Next_LineAddr
POP DE ;
PUSH HL
PUSH DE ; stack: new_lineaddr old_lineaddr
AND A
SBC HL, DE
LD BC, 0
LD C, L ; Determine length of first line in inserted file
LD A, L ; Length goes to BC for LDIR and to curline_length
LD (curline_length), A
XOR A
LD (oldcurline_length), A ; by setting oldcurline_length to 0, nothing gets overwritten, line gets inserted.
POP HL ; source address
LD DE, linebuf
LDIR ; Copy line of read file to linebuf.
CALL Leave_NoRender ; Cause line to be inserted in text.
LD HL, (num_lines)
INC HL
LD (num_lines), HL
LD BC, 1
LD IX, cur_lineaddr
CALL Lines_Forward ; Skip past line that got just inserted.
JR do_read1
; Find forward (^W)
do_find: CALL Enter_Current
CALL Leave_Current
CALL Clear_BottomRow
LD HL, s_FIND
CALL Print_String
LD HL, findstring
LD E, 0
LD BC, 40
MOSCALL mos_editline
LD HL, (cur_lineaddr)
LD A, (linebuf_editpos)
LD DE, 0
LD E, A
ADD HL, DE ; start position to find the string.
EXX
LD HL, (cur_lineno)
LD DE, (cur_lineaddr)
EXX
LD BC, (text_end)
; The following loop starts seaching just after the current cursor position and wraps around
; at the start of the text file when reaching the end. It terminates when either the string is found or
; the original cursor position is reached again.
; Register usage in the find loop:
; HL pointer in text.
; BC, text_end to compare against.
; DE, scratch used in compare function.
; HL' line number, DE' start address of current line.
find_loop: INC HL
AND A
SBC HL, BC
JR NC, find_notfound
; Reached end of text?
ADD HL, BC ; Undo the last subtract.
LD A, (HL)
CP 13 ; At end of line?
JR NZ, @F
INC HL ; Increment past CR
PUSH HL
EXX
POP DE ; Remember current line start
INC DE
INC HL ; Increment line number.
EXX
JR find_loop
@@: CALL Compare
JR NZ, find_loop
find_found: PUSH HL ; Stack find position
EXX
LD IX, cur_lineaddr
LD (IX+0), DE
LD (IX+3), HL
EXX ; Set line number where string was found.
CALL Adjust_Top
CALL Enter_Current
POP HL
LD DE, (cur_lineaddr)
AND A
SBC HL, DE
LD A, L
LD (linebuf_editpos), A ; Compute editpos from find position.
CALL Col_From_Editpos
CALL Render_Current_Line
CALL Show_Cursor
JP main_loop
find_notfound: CALL Clear_BottomRow
LD HL, s_NOTFOUND
CALL Print_String
CALL Show_Cursor
JP main_loop
; Find backward (^Q)
do_rfind: CALL Enter_Current
CALL Leave_Current
CALL Clear_BottomRow
LD HL, s_FIND
CALL Print_String
LD HL, findstring
LD E, 0
LD BC, 40
MOSCALL mos_editline
LD HL, (cur_lineaddr)
LD A, (linebuf_editpos)
LD DE, 0
LD E, A
ADD HL, DE ; start position to find the string.
EXX
LD DE, (cur_lineno)
LD HL, (cur_lineaddr)
CALL Next_LineAddr
INC DE
EX DE, HL
EXX
LD BC, (buf_start)
; The following loop starts seaching just before the current cursor position and terminates
; when either the start of the text is reached (not found) or the string is found.
; Register usage in the find loop:
; HL pointer in text.
; BC, buf_start to compare against.
; DE, scratch used in compare function.
; HL' line number, DE' start address of current line.
rfind_loop: DEC HL
AND A
SBC HL, BC
JR C, find_notfound
; Reached start of text?
ADD HL, BC ; Undo the last subtract.
LD A, (HL)
CP 10 ; At end of previous line?
JR NZ, @F
PUSH HL
DEC HL ; decrement past LF
EXX
POP DE ; Remember current line start
INC DE
DEC HL ; Decrement line number.
EXX
JR rfind_loop
@@: CALL Compare
JR NZ, rfind_loop
rfind_found: PUSH HL ; Stack find position
EXX
LD IX, cur_lineaddr
LD (IX+0), DE
LD (IX+3), HL
EXX ; Set line number where string was found.
LD BC, 1
CALL Lines_Backward ; But go one line back.
CALL Adjust_Top
CALL Enter_Current
POP HL
LD DE, (cur_lineaddr)
AND A
SBC HL, DE
LD A, L
LD (linebuf_editpos), A ; Compute editpos from find position.
CALL Col_From_Editpos
CALL Render_Current_Line
CALL Show_Cursor
JP main_loop
; Insert special character (^T). Type two hex characters.
do_special: CALL Read_Key
CALL UPPRC
SUB '0'
JP C, main_loop
CP 10
JR C, @F
SUB 7
@@: ADD A,A ; Convert character to hex digit
ADD A,A
ADD A,A
ADD A,A
LD E, A ; shift first digit 4 positions to left.
CALL Read_Key
CALL UPPRC
SUB '0'
JP C, main_loop
CP 10
JR C, @F
SUB 7 ; convert character to hex digit
@@: ADD A,E ; Add to first digit.
CP 07Fh
JP Z, main_loop ; Skip deelete
CP ' '
JP C, main_loop
JP do_ins_char
; Clear the bottom row for a prompt, reset the cursor to start of that line.
Clear_BottomRow: LD A, 31
RST.LIL 10h
XOR A
RST.LIL 10h
LD A, (Current_Rows)
DEC A
RST.LIL 10h
CALL Clear_EOL
LD A, 13
RST.LIL 10h
RET
; Draw the entire edit screen, including top and bottom status lines.
Show_Screen: LD A, 12
RST.LIL 10h ; Clear the screen
CALL Inverse_Video ; Print top row
LD HL, s_NAME
CALL Print_String
LD HL, filename
CALL Print_String
CALL Clear_EOL
CALL True_Video
LD HL, (top_lineaddr)
LD A, (Current_Rows)
DEC A
DEC A
LD B, A ; Number of lines to draw.
LD E, 0
Show_loop: CALL Render_Line
INC E
CALL Next_LineAddr
JR C, Show_Status ; Exit loop if no more lines in buffer
DJNZ Show_loop
; Call this when you want to update the status line, but not redraw the whole screen.
Show_Status: ; Show status line at bottom of screen
LD A, 31
RST.LIL 10h
LD A, 0
RST.LIL 10h
LD A, (Current_Rows)
DEC A
RST.LIL 10h ; Bottom row
CALL Inverse_Video
LD HL, s_LINE
CALL Print_String
LD HL, (cur_lineno)
CALL Print_Decimal
LD A, '/'
RST.LIL 10h
LD HL, (num_lines)
CALL Print_Decimal
LD A, ','
RST.LIL 10h
LD A, ' '
RST.LIL 10h
LD HL, (text_end)
LD DE, (buf_start)
AND A
SBC HL, DE ; Text size in bytes
CALL Print_Decimal
LD A, '/'
RST.LIL 10h
LD HL, (cutbuf_start)
LD DE, (buf_start)
AND A
SBC HL, DE ; Total buffer size in bytes
CALL Print_Decimal
LD HL, s_HELP_Small
CALL Print_String
LD A, (file_modified)
AND A
JR Z, @F
LD A, '*' ; Show an asterisk when file is modified.
RST.LIL 10h
LD A, ' '
RST.LIL 10h
@@: LD HL, (cutbuf_lines)
CALL Print_Decimal
CALL Clear_EOL
CALL True_Video
; Position (blinking) text cursor at correct location.
Show_Cursor: LD A, 31
RST.LIL 10h
LD A, (cursor_col)
RST.LIL 10h
LD A, (cursor_row)
INC A
RST.LIL 10h
RET
; Render a text line on the screen.
; Inputs: HL pointer to line in memory.
; E row index. Will be used to determine if this is the current line.
Render_Line: PUSH HL
PUSH BC
LD C, 0
LD A, (cursor_row)
CP E
JR Z, Render_Current_Line2 ; Separate routine to render the current line.
LD A, (Current_Cols)
LD B, A
Render_Loop: LD A, (HL)
INC HL
CP A, 13 ; At end of line.
JR Z, Render_CR
CP A, 9
JR NZ, Render_Notab
Render_Tab: DEC B
JR Z, Render_Rightmost