-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMXEdit.ASM
1295 lines (1171 loc) · 51.4 KB
/
MXEdit.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
;==============================================================================================
; MXEdit.ASM
; Main file for the tool MXEdit
; Author : yizhang82
; Start Date : 2002-9-2
; End Date : 2002-9-9
;==============================================================================================
.DOSSEG
.MODEL SMALL, C, os_dos
.STACK
INCLUDE TEXT.INC ; Text routines
INCLUDE MEMORY.INC ; Memory routines
INCLUDE FILE.INC ; file routines
INCLUDE ERROR.INC ; error output routines
.DATA
;=============================================================================
; Key scan codes
;=============================================================================
Up = 48h
Down = 50h
Left = 4bh
Right = 4dh
Return = 0dh
Tab = 09h
BackSpace = 08h
Escape = 1bh
Delete = 53h
HomeKey = 47h
EndKey = 4fh
PageUp = 49h
PageDn = 51h
F10 = 44h
TextAttr = 17h
TitleAttr = 74h
MainMenuAttr= 70h
MenuItemAttr= 70h
HilightAttr = 24h
StatusAttr = 70h
TabStop = 8
FileNamePtr DD ?
FileNameMsg DB 'FileName:'
FileName DB 200 DUP(?)
NoNameMsg DB '<no name>', 0
LastLine DW ?
FirstLine DW ?
CurrentLine DW ?
LineLength DW ?
xPos DB 0
yPos DB 1 ; cursor position
Row DW 0 ; Row number of the (left, top) corner
Column DW 0 ; column number of the (left, top) corner
Quit DB 0
TmpRow DB 0
TmpColumn DB 0
Head DW ?
Last DW ?
NewLine DW ?
Key DB ?
NewSize DW ?
OldSize DW ?
VerMsg DB ' MXEdit v1.0 by yizhang82, 2002.', 0
PosMsg DB '[Row:00000, Col:00000]', 0
MenuOn DB 0
MenuItem DB 0
MenuMsg DB ' Menu(Esc)', 0
MenuTop DB 218, 10 dup(196), 191, 0
Menu1 DB 179,'Load ', 179, 0
Menu2 DB 179,'Save ', 179, 0
Menu3 DB 179,'Save as ', 179, 0
Menu4 DB 179,'Quit ', 179, 0
MenuBottom DB 192, 10 dup(196), 217, 0
SaveFileMsg DB 'Saving file...', 0
LoadFileMsg DB 'Loading file..', 0
SaveOKMsg DB 'Saving end.', 0
LoadOkMsg DB 'Loading end.', 0
FilePrompt DB 'Enter file name:', 0
.CODE
;====================================================================
; Find the last lines, make sure total cx lines
; Output : Last
;====================================================================
FindLastLine PROC
mov bx, FirstLine
mov Last, bx
.WHILE (cx > 0) && (bx != NULL)
mov es, bx
mov Last, bx
mov bx, (TextLine PTR es:[0]).NextLineSeg
dec cx
.ENDW
.IF cx > 0
;========================================================
; no current line found
; so first, create from the last line to the current line
; es - last line
;========================================================
mov Last, es
.REPEAT
push cx
; allocate memory for the structure
INVOKE MemAlloc, SIZEOF TextLine
.IF ax == 0
jmp ErrorAllocate
.ENDIF
mov NewLine, ax
mov es, ax
; Initialize the structure in es
; link
mov ax, Last
mov (TextLine PTR es:[0]).PrevLineSeg, ax
mov (TextLine PTR es:[0]).NextLineSeg, NULL
.IF Last != NULL
mov es, Last
mov ax, NewLine
mov (TextLine PTR es:[0]).NextLineSeg, ax
mov es, NewLine
.ELSE
; this indicates FirstLine is NULL
mov FirstLine, es
.ENDIF
mov Last, es
; allocate NULL strings with a start length of 64 bytes
INVOKE MemAlloc, 64
.IF ax == 0
jmp ErrorAllocate
.ENDIF
mov (TextLine PTR es:[0]).TextPtrSeg, ax
mov (TextLine PTR es:[0]).CharsAllocated, 64
mov (TextLine PTR es:[0]).LineLength, 0
mov es, (TextLine PTR es:[0]).TextPtrSeg
mov BYTE PTR es:[0], 0
pop cx
.UNTILCXZ
.ENDIF
ret
ErrorAllocate:
INVOKE OutError, MemoryNotEnough, Abort
ret
FindLastLine ENDP
;===============================================
; Expand the string memory if necessary
; Param: NewSize - new size of the string
; es - Pointer to the line
;===============================================
ExpandString PROC
mov dx, (TextLine PTR es:[0]).CharsAllocated
.IF NewSize >= dx
; resize the memory to fit the string
; allocated memory = stringsize - stringsize % 16 + 64
mov ax, NewSize
xor dx, dx
mov cx, 16
div cx
mov ax, NewSize
sub ax, dx
add ax, 64 ; ax - new amount of memory
mov dx, (TextLine PTR es:[0]).CharsAllocated
mov (TextLine PTR es:[0]).CharsAllocated, ax
push di
push es
INVOKE MemResize, \
(TextLine PTR es:[0]).TextPtrSeg, \
ax, \
dx
pop es
pop di
mov (TextLine PTR es:[0]).TextPtrSeg, ax
.ENDIF
ret
ExpandString ENDP
DrawScreen PROC
.IF MenuOn == 1
jmp DrawMenu
.ENDIF
;=====================================================
; Find the top line accord to Row
;=====================================================
mov cx, Row
mov bx, FirstLine
mov es, bx
.WHILE (cx > 0) && (bx != NULL)
mov bx, (TextLine PTR es:[0]).NextLineSeg
mov es, bx
dec cx
.ENDW
mov CurrentLine, bx
xor bp, bp
mov bp, 1
mov ax, CurrentLine
mov es, ax
mov bx, es ; bx - current line to display
; (TYPE textline)
mov LineLength, 0
.IF bx != NULL ; avoid null pointer read
mov ax, (TextLine PTR es:[0]).LineLength
mov LineLength, ax
mov es, (TextLine PTR es:[0]).TextPtrSeg
.ENDIF
; draw to row 23
mov TmpRow, 0
.WHILE (bp < 24) && (bx != NULL)
xor di, di
mov di, column
mov ax, bp
mov TmpRow, al
mov TmpColumn, 0
push bx
.WHILE ( di < LineLength ) && (BYTE PTR es:[di] != 0) && ( TmpColumn < 80 )
INVOKE PutChar, TmpRow, TmpColumn, BYTE PTR es:[di], 17h
inc TmpColumn
inc di
.ENDW
.WHILE TmpColumn < 80
INVOKE PutChar, TmpRow, TmpColumn, 20h, TextAttr
inc TmpColumn
.ENDW
pop bx
; goto next line
mov es, bx
mov bx, (TextLine PTR es:[0]).NextLineSeg
mov es, bx
mov LineLength, 0
.IF bx != NULL
mov ax, (TextLine PTR es:[0]).LineLength
mov LineLength, ax
mov es, (TextLine PTR es:[0]).TextPtrSeg
.ENDIF
inc bp
.ENDW
.WHILE (bp < 24)
;=============================================
; draw spaces
;=============================================
mov ax, bp
mov TmpRow, al
;mov ax, Column
mov TmpColumn, 0
mov cx, 80
.REPEAT
push cx
INVOKE PutChar, TmpRow, TmpColumn, 20h, TextAttr
inc TmpColumn
pop cx
.UNTILCXZ ; this statement also dec cx
inc bp
.ENDW
;===========================================
; draw title bar
;===========================================
mov TmpColumn, 0
mov TmpRow, 0
xor di, di
.WHILE MenuMsg[di] != 0
INVOKE PutChar, TmpRow, TmpColumn, MenuMsg[di], MainMenuAttr
inc di
inc TmpColumn
.ENDW
xor di, di
.WHILE VerMsg[di] != 0
INVOKE PutChar, TmpRow, TmpColumn, VerMsg[di], TitleAttr
inc di
inc TmpColumn
.ENDW
.WHILE TmpColumn < 80
INVOKE PutChar, TmpRow, TmpColumn, 20h, TitleAttr
inc TmpColumn
.ENDW
;===========================================
; draw the status bar
;===========================================
; fill in PosMsg
mov ax, Row
mov bl, yPos
xor bh, bh
add ax, bx
mov si, OFFSET PosMsg
add si, 5
call ConvertAX
mov ax, Column
mov bl, xPos
xor bh, bh
add ax, bx
inc ax
mov si, OFFSET PosMsg
add si, 16
call ConvertAX
mov TmpColumn, 0
mov TmpRow, 24
xor di, di
.WHILE (TmpColumn < 80) && (FileNameMsg[di] != 0)
INVOKE PutChar, TmpRow, TmpColumn, FileNameMsg[di], StatusAttr
inc TmpColumn
inc di
.ENDW
.IF FileName == 0
; display a no name
xor di, di
.WHILE (TmpColumn < 80) && (NoNameMsg[di] != 0)
INVOKE PutChar, TmpRow, TmpColumn, NoNameMsg[di], StatusAttr
inc TmpColumn
inc di
.ENDW
.ENDIF
.WHILE TmpColumn < 58
INVOKE PutChar, TmpRow, TmpColumn, 20h, StatusAttr
inc TmpColumn
.ENDW
xor di, di
.WHILE (PosMsg[di] != 0) && (TmpColumn < 80)
INVOKE PutChar, TmpRow, TmpColumn, PosMsg[di], StatusAttr
inc TmpColumn
inc di
.ENDW
.WHILE TmpColumn < 80
INVOKE PutChar, TmpRow, TmpColumn, 20h, StatusAttr
inc TmpColumn
.ENDW
DrawMenu:
.IF MenuOn == 1
; draw menu
mov TmpRow, 1
mov di, OFFSET MenuTop
call DrawMenuLine
mov di, OFFSET Menu1
call DrawMenuLine
mov di, OFFSET Menu2
call DrawMenuLine
mov di, OFFSET Menu3
call DrawMenuLine
mov di, OFFSET Menu4
call DrawMenuLine
mov di, OFFSET MenuBottom
call DrawMenuLine
.ENDIF
ret
DrawScreen ENDP
DrawMenuLine PROC
mov TmpColumn, 1
.WHILE BYTE PTR ds:[di] != 0
mov al, MenuItem
add al, 2
.IF (al == TmpRow) && (TmpColumn > 1) && (TmpColumn < 12)
INVOKE PutChar, TmpRow, TmpColumn, BYTE PTR ds:[di], HilightAttr
.ELSE
INVOKE PutChar, TmpRow, TmpColumn, BYTE PTR ds:[di], MenuItemAttr
.ENDIF
inc di
inc TmpColumn
.ENDW
;.WHILE TmpColumn < 10
; mov al, MenuItem
; add al, 2
; .IF al == TmpRow
; INVOKE PutChar, TmpRow, TmpColumn, BYTE PTR ds:[di], HilightAttr
; .ELSE
; INVOKE PutChar, TmpRow, TmpColumn, 20h, MenuItemAttr
; .ENDIF
; inc TmpColumn
;.ENDW
inc TmpRow
ret
DrawMenuLine ENDP
; convert the number in ax to a 5-digit string begin from ds:si
ConvertAX PROC
mov cx, 5
add si, 4
.REPEAT
xor dx, dx
mov bx, 10
div bx
add dl, '0'
mov [si], dl ; first
dec si
.UNTILCXZ
ret
ConvertAX ENDP
;==============================================================================
; Free the contents of the file to free memory
;==============================================================================
FreeContents PROC
mov es, FirstLine
mov ax, es
.WHILE ax != NULL
push ax
mov es, (TextLine PTR es:[0]).TextPtrSeg
INVOKE MemFree, es
.IF ax == NULL
INVOKE OutError, ErrorFreeingMem, Abort
.ENDIF
pop ax
mov es, ax
mov ax, (TextLine PTR es:[0]).NextLineSeg
push ax
INVOKE MemFree, es
.IF ax == NULL
INVOKE OutError, ErrorFreeingMem, Abort
.ENDIF
pop ax
mov es, ax
.ENDW
ret
FreeContents ENDP
; Menu function:Load file
MenuLoadFile PROC
mov MenuOn, 0
call DrawScreen
INVOKE ShowWindow, 10, 5, 50, 9, TextAttr
INVOKE EnterString, 7, 13, OFFSET FilePrompt, TextAttr, 30, 49, OFFSET FileName
call DrawScreen
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET LoadFileMsg, TextAttr
call FreeContents
INVOKE LoadFile, FileNamePtr
mov LastLine, ax
;========================================================================
; find the first line
;========================================================================
xor bx, bx
mov es, LastLine
mov ax, es
.WHILE ax != NULL
mov bx, es ; bx - previous es
mov ax, (TextLine PTR es:[0]).PrevLineSeg
push ax
mov es, ax
mov es, (TextLine PTR es:[0]).TextPtrSeg
xor di, di
pop ax
mov es, ax
.ENDW
mov FirstLine, bx
call DrawScreen
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET LoadOkMsg, TextAttr
WaitKey
call DrawScreen
ret
MenuLoadFile ENDP
; Menu function: save file
MenuSaveFile PROC
mov MenuOn, 0
call DrawScreen
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET SaveFileMsg, TextAttr
INVOKE SaveFile, FileNamePtr, FirstLine
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET SaveOkMsg, TextAttr
WaitKey
ret
MenuSaveFile ENDP
; Menu function: Save as
MenuSaveAs PROC
mov MenuOn, 0
call DrawScreen
INVOKE ShowWindow, 10, 5, 50, 9, TextAttr
INVOKE EnterString, 7, 13, OFFSET FilePrompt, TextAttr, 30, 49, OFFSET FileName
call DrawScreen
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET SaveFileMsg, TextAttr
INVOKE SaveFile, FileNamePtr, FirstLine
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET SaveOkMsg, TextAttr
WaitKey
call DrawScreen
ret
MenuSaveAs ENDP
;================================================================================
; MAIN PROGRAM BEGINS FROM HERE
;================================================================================
; MAIN PROGRAM BEGINS FROM HERE
;================================================================================
; MAIN PROGRAM BEGINS FROM HERE
;================================================================================
.STARTUP
;========================================================================
; Initialize the program to allocate memory
;========================================================================
INVOKE InitMem
;========================================================================
; First call setvideomode to set video mode to the standard 80x25 display
;========================================================================
mov al, Mode80x25x16
call SetVideoMode
call DrawScreen
; get psp address
; assume DOS 3.0 or higher
mov ah, 62h ; Query DOS for PSP
int 21h ; bx - PSP segment address
mov WORD PTR [FileNamePtr], ds
mov WORD PTR [FileNamePtr+2], OFFSET FileName
push ds
mov ax, ds
mov es, ax ; es - segment of file name
mov ds, bx ; ds - segment of psp
mov di, OFFSET FileName
mov si, 82h
mov cl, BYTE PTR ds:[80h] ; psp:[80h] - the length of command line
xor ch, ch
.IF cl == 0
; no run time argument specified
mov BYTE PTR es:[di], 0
pop ds
mov LastLine, NULL
.ELSE
dec cx
cld
REP movsb
mov BYTE PTR es:[di+1], 0
pop ds
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET LoadFileMsg, TextAttr
INVOKE LoadFile, FileNamePtr
mov LastLine, ax
INVOKE ShowWndText, 10, 5, 30, 9, OFFSET LoadOkMsg, TextAttr
WaitKey
.ENDIF
;========================================================================
; find the first line
;========================================================================
xor bx, bx
mov es, LastLine
mov ax, es
.WHILE ax != NULL
mov bx, es ; bx - previous es
mov ax, (TextLine PTR es:[0]).PrevLineSeg
mov es, ax
.ENDW
mov FirstLine, bx
;========================================================================
; Initialize variables
;========================================================================
mov dh, yPos
mov dl, xPos
call MoveCursor
;========================================================================
; THE MAIN LOOP
;========================================================================
mov Row, 0
call DrawScreen
.WHILE Quit == 0
;====================================================================
; wait to read a key
;====================================================================
; 07h - no Ctrl+C
mov ah, 08h
int 21h ; al - key code
.IF MenuOn == 1
; menu key process
.IF al == 0
mov ah, 08h
int 21h ; read extended key code
.IF al == UP
.IF MenuItem > 0
dec MenuItem
.ELSE
mov MenuItem, 3
.ENDIF
.ELSEIF al == DOWN
.IF MenuItem < 3
inc MenuItem
.ELSE
mov MenuItem, 0
.ENDIF
.ENDIF
.ELSEIF al == Escape
mov MenuOn, 0
.ELSEIF al == Return
; process menu command
mov MenuOn, 1
.IF MenuItem == 0
; Loadfile
call MenuLoadFile
.ELSEIF MenuItem == 1
; save file
call MenuSaveFile
.ELSEIF MenuItem == 2
; save as
call MenuSaveAs
.ELSEIF MenuItem == 3
; quit
mov Quit, 1
.ENDIF
.ENDIF
call DrawScreen
.CONTINUE
.ENDIF
.IF al == 0 ; is a Extended key code?
mov ah, 08h
int 21h ; read another key scan code
.IF al == F10
mov MenuOn, 1
.ELSEIF al == UP
.IF yPos > 1
dec yPos
.ELSE
.IF Row > 0
dec Row
.ENDIF
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == DOWN
.IF yPos < 23
inc yPos
.ELSE
inc Row
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == LEFT
.IF xPos > 0
dec xPos
.ELSE
.IF Column > 0
dec Column
.ENDIF
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == RIGHT
.IF xPos < 79
inc xPos
.ELSE
inc Column
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == HomeKey
mov Column, 0
mov xPos, 0
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == EndKey
mov cx, Row
mov al, yPos
xor ah, ah
add cx, ax ; cx - line number
call FindLastLine
mov es, Last
mov dx, (TextLine PTR es:[0]).LineLength
; first see if we can avoid moving the screen
.IF (dx >= Column)
mov ax, Column
add ax, 79
.IF (ax >= dx)
; ok, just change xPos
sub dx, Column
mov xPos, dl
.ELSE
mov xPos, 79
.IF dx <= 79 ; length <= 79
mov xPos, dl
mov Column, 0
.ELSE ; length > 79
sub dx, 79
mov Column, dx
.ENDIF
.ENDIF
.ELSE
mov xPos, 79
.IF dx <= 79 ; length <= 79
mov xPos, dl
mov Column, 0
.ELSE ; length > 79
sub dx, 79
mov Column, dx
.ENDIF
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ELSEIF al == Delete
mov cx, Row
mov al, yPos
xor ah, ah
add cx, ax ; cx - line number
call FindLastLine
mov es, Last
mov di, Column
mov al, xPos
xor ah, ah
add di, ax
mov dx, (TextLine PTR es:[0]).LineLength
.IF dx > di
dec (TextLine PTR es:[0]).LineLength
;inc di
mov es, (TextLine PTR es:[0]).TextPtrSeg
.WHILE di < dx
mov al, es:[di+1]
mov es:[di], al
inc di
.ENDW
.ELSE
; like backspace
; this time we link the next line to the current line
mov NewSize, dx
;inc NewSize
sub di, dx
add NewSize, di
;inc NewSize
mov es, (TextLine PTR es:[0]).NextLineSeg
mov ax, es
.IF ax != NULL
mov bx, (TextLine PTR es:[0]).LineLength
add NewSize, bx
; expand string first
mov es, Last
mov ax, NewSize
mov (TextLine PTR es:[0]).LineLength, ax
call ExpandString
; expand the string with spaces
mov si, (TextLine PTR es:[0]).LineLength
mov es, (TextLine PTR es:[0]).TextPtrSeg
mov di, NewSize
; you'll encounter a infinite loop if
; si == 0
.WHILE (di > si)
mov BYTE PTR es:[di], 20h
dec di
.ENDW
mov BYTE PTR es:[si], 20h
; characters to copy
mov es, Last
mov es, (TextLine PTR es:[0]).NextLineSeg
mov cx, (TextLine PTR es:[0]).LineLength
inc cx
mov di, Column
mov al, xPos
xor ah, ah
add di, ax
push ds
push Last
xor si, si
mov ds, (TextLine PTR es:[0]).TextPtrSeg
pop es
mov es, (TextLine PTR es:[0]).TextPtrSeg
REP movsb
pop ds
; change link
mov es, Last
mov es, (TextLine PTR es:[0]).NextLineSeg
mov bx, es
mov es, (TextLine PTR es:[0]).NextLineSeg
mov ax, Last
mov (TextLine PTR es:[0]).PrevLineSeg, ax
mov dx, es
mov es, Last
mov (TextLine PTR es:[0]).NextLineSeg, dx
; free memory
mov es, bx
INVOKE MemFree, (TextLine PTR es:[0]).TextPtrSeg
.IF ax == NULL
jmp Error
.ENDIF
INVOKE MemFree, es
.IF ax == NULL
jmp Error
.ENDIF
.ENDIF
.ENDIF
.ELSEIF al == PageDn
add Row, 24
.ELSEIF al == PageUp
.IF Row < 24
mov Row, 0
.ELSE
sub Row, 24
.ENDIF
.ENDIF
.ELSE
.IF al == Escape
mov MenuOn, 1
.ELSEIF al == BackSpace
mov cx, Row
mov al, yPos
xor ah, ah
add cx, ax ; cx - line number
call FindLastLine
mov es, Last
mov di, Column
mov al, xPos
xor ah, ah
add di, ax
mov dx, (TextLine PTR es:[0]).LineLength
mov bx, Row
mov cl, yPos
xor ch, ch
add bx, cx
; Two case
.IF (di > 0) && (dx > 0)
;========================================
; Normal case
;========================================
dec (TextLine PTR es:[0]).LineLength
mov es, (TextLine PTR es:[0]).TextPtrSeg
.IF di > 0
dec di
.WHILE di < dx
mov al, es:[di+1]
mov es:[di], al
inc di
.ENDW
.IF xPos > 0
dec xPos
.ELSE
.IF Column > 0
dec Column
.ENDIF
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ENDIF
.ELSEIF (bx > 1)
;========================================
; Cursor is in the leftmost of the screen
; we 1.link the contents of the current
; line to the previous line, and 2.
; delete the current line, then 3.place
; the cursor at the link point of the
; previous line
;========================================
.IF dx > 0
; link a empty line is not necessary
; 1
push ds
mov ax, (TextLine PTR es:[0]).TextPtrSeg
push ax
xor si, si
mov cx, dx
inc cx
; Row > 0, Prevlineseg is not NULL
mov es, (TextLine PTR es:[0]).PrevLineSeg
mov di, (TextLine PTR es:[0]).LineLength
; calculate the newsize and expand if necessary
mov NewSize, di
add NewSize, cx
mov dx, NewSize
dec dx
mov (TextLine PTR es:[0]).LineLength, dx
push di
push cx
call ExpandString
pop cx
mov es, (TextLine PTR es:[0]).TextPtrSeg
pop di
pop ds
xor si, si
mov bx, di ; bx - new cursor pos
; copy
cld
REP movsb
pop ds
.ELSE
mov es, (TextLine PTR es:[0]).PrevLineSeg
mov bx, (TextLine PTR es:[0]).LineLength
.ENDIF
; 2
mov es, Last
mov ax, (TextLine PTR es:[0]).NextLineSeg
mov es, (TextLine PTR es:[0]).PrevLineSeg
mov dx, es
mov (TextLine PTR es:[0]).NextLineSeg, ax
mov es, ax
mov (TextLine PTR es:[0]).PrevLineSeg, dx
; free <last>
mov es, Last
INVOKE MemFree, (TextLine PTR es:[0]).TextPtrSeg
.IF ax == NULL
jmp Error
.ENDIF
INVOKE MemFree, es
.IF ax == NULL
jmp Error
.ENDIF
; 3 locate cursor in bx
.IF yPos > 1
dec yPos
.ELSE
.IF Row > 1
dec Row
.ENDIF
.ENDIF
.IF bx < 80
mov Column, 0
mov xPos, bl
.ELSE
sub bx, 79
mov Column, bx
mov xPos, 79
.ENDIF
mov dh, yPos
mov dl, xPos
call MoveCursor
.ENDIF
.ELSEIF al == Tab
;=============================================
; Tab, insert 8 - (Column+xPos)%8
; set xPos to xPos - (Column+xPos)%8 + 8
; (assume tabstop is 8 in the formula above)
;=============================================
; find the current line
mov cx, Row
mov al, yPos
xor ah, ah
add cx, ax ; cx - line number
call FindLastLine
mov ax, Column
add al, xPos