; *********************************************************************
; EDIT02
; Pascal editor file 02
; B.Bridgwater 5.12.83
; *********************************************************************
;
;------------------------------------------------------------------------
edit ;scredII - the incredible shrinking Pascal editor.
;
;                                      current
;                                      line
; PAJE                                                 HYMEM
;  v                                   <......>          v
; +-+-------------+-------------------+---------------+-+
; |$|.............|                   |...............|$|
; +-+-------------+-------------------+---------------+-+
;    ^             ^                   ^               ^
; TSTART           GS                  GE            TMAX
;
;
; scrnXAs is X coord of cursor. (GE)creenX is char under cursor,
; unless $ isA0 in (GE) .. (GE),scrnX (when it is that $).
;
; scrnY is Y coord of current line on screen, and may be forced by the
; screen update routine.
;
;
; Screen update types
;
; none ;Simple cursor movements
; csronwards ;Character insert/delete
; fullscreen ;Find,replace etc
; thelot ;Clears screen first. After tload etc
; hardup ;Special case, for cursor movevents
; harddown ;Special case, for cursor movevents
;
; pagewidth is max X-coord of screen, and pagelength is max Y-coord of
; screen (thus Y-coord of status line is pagelength+1).
;
; scrim (screen image) holds maximum cursorX position written to each
; line on the screen (including the CR, if any). The scrim entry will
; have its top bit set if that line didn't have a CR written to it.
;
;Perform various initialization functions.
;
       ldaim &87
       jsr OSBYTE
       sty mode
       ldaay llntab
       sta pagewidth
       ldaay nlntab
       sta pagelength
       ;
       sta scrnlg
       inc scrnlg
       ;
       ;Initialize top & bottom scroll margins.
       ;
       ldaim 4
       jsr setTSM
       ;
       sec
       lda pagelength
       sbcim 4
       jsr setBSM
       ;
       ldxim 0
       stx cursedflag
       stx CRvisflag
       stx nextreadflag
       stx markX
       inx ;=1
       stx insertflag
       ;
       ldaim CR
       ldyim 0
       staiy PAJE
       staiy TMAX
       ;
editin ;Initialize pointers to reflect empty buffer.
       ;
       ldx TSTART
       ldy TSTART+1
       ;
editGS ;Re-entry point for Tload. XY = End of file.
       ;
       stx GS
       sty GS+1
       ;
       lda TMAX
       sta GE
       lda TMAX+1
       sta GE+1
       ;
editst jsr stfile
       ;
editco ;Re-entry point for brk-handler.
       ;
       ldxim &FF
       txs
       ;
       ldaim 2
       sta brkaction
       ;
       jsr initedit
       ;
       ldaim thelot
       sta updatereqd
       ;
editlo ;Update screen to reflect previous command.
       ;
       jsr scrnud
       jsr csrscrnXY
       jsr curon
       ;
       ;Set update required to default of fullscreen.
       ;
       ldaim fullscreen
       sta updatereqd
       ;
       ;Read a char, and branch depending on its value
       ;
       ;0 - &7F is ASCII input to be entered as text
       ;&80 - &8F are softkey values       }                               
       ;&90 - &9F are shift-softkey values } Editor commands
       ;&A0 - &AF are ctrl-sofkkey values  }
       ;&B0 - &FF are crap
       ;
       jsr edrdch
       tax
       bpl edittext
       ;
       cmpim &B0
       bcs editlo
       ldaAX edctbl-&80
       sta addr
       ldaAX edctbh-&80
       sta addr+1
       jsr IaddrSUB
       jmp editlo
;-----------------------------------------------------------------------
IaddrSUB ;
;
       jmi addr
;-----------------------------------------------------------------------
literal ;
;
       rts
;-----------------------------------------------------------------------
edittext ;
;
       sta char
       ;
       jsr mkrefuse
       ;
       jsr edtext
       ;
       jmp editlo
;-----------------------------------------------------------------------
edtext ;Entry - A holds ASCII char to be treated as text input
;Exit  - appropraite action taken depending on current
;        'mode' (insert, overtype, csr-edit).
;
       lda char
       cmpim TAB
       bne etxtNT
       ;
       ;Auto-indent cursor to under 1st non-space on previous line.
       ;
       ldaim 1
       jsr TPGSbk
       ;
       ldx pagewidth
       ldyim 0
       ldaim space
etablp cmpiy TP
       bne etabst
       iny
       dex
       bne etablp
       ;
etabst sty scrnX
       ;
       ldaim none
       sta updatereqd
       rts
;-----------------------------------------------------------------------
etxtNT cmpim delete
       beq echrdel
       ;
       cmpim CR
       bne echrco
       ;
       ;Check whether in cursor-edit mode, to see if CR is terminator.
       ;
       lda cursedflag
       beq echrNC
       ;
       jsr initedit
       jsr OSNEWL
       ldaim none
       sta updatereqd
       dec cursedflag
       jmp status
       ;
echrNC ;Don't allow CR input in overtype mode.
       ;
       lda insertflag
       beq echrex
       ;
echrco ;Insert character into text.
       ;
       jsr addvirtualspaces
       ;
       ;Don't insert in overtype mode, unless at end of line.
       ;
       lda insertflag
       bne echrmv
       ;
       ldy scrnX
       ldaiy GE
       cmpim CR
       bne echrch
       ;
echrmv ldxim 1
       ldyim 0
       lda scrnX
       jsr insrtX
       ;
echrch lda char
       ldy scrnX
       staiy GE
       ;
       ;Write char to screen (cursor OK), wiping tail for CR.
       ;
       jsr paswrch
       lda char
       cmpim CR
       bne echrnt
       lda scrnX
       ldy scrnY
       jsr wipetail
       ;
echrnt ;Perform associated cursor movement, and decide what screen
       ;update is required.
       ;
       lda char
       cmpim CR
       beq ehrCsu
       ;
       jsr currt
       ;
       jmp echrup
       ;
ehrCsu jsr curst
       jsr curdwn
       ;
echrup jsr scrnud
       ;
       ldaim csronwards
       sta updatereqd
       ;
echrex rts
;-----------------------------------------------------------------------
echrdel ;If try to delete VS, then translate to csrleft. Can't delete
;at start of file.
;
       jsr VScount
       beq edelST
       ;
       jmp curlt
       ;
edelST lda scrnX
       bne edelco
       jsr starttest
       bcs edelex
       ;
edelco ;Get char to be deleted under cursor. In absence of VS check,
       ;this will give CR delete when past end of line.
       ;
       jsr curlt
       ;
       jsr lengthcalc
       cmp scrnX
       bcs edelMT
       ;
       sta scrnX
       ;
edelMT ;Branch on text-entry mode (insert/overtype).
       ;
       lda insertflag
       bne edelSL
       ;
       ldy scrnX
       ldaiy GE
       cmpim CR
       beq edelex
       ;
       ldaim space
       staiy GE
       rts
       ;
edelSL ;Shift up start of line 'over' deleted character.
       ;
       lda GE
       sta temp
       lda GE+1
       sta temp+1
       ;
       inc GE
       bne edelnh
       inc GE+1
       ;
edelnh ldy scrnX
       bpl edeldy
       ;
edellp ldaiy temp
       staiy GE
edeldy dey
       bpl edellp
       ;
       jsr scrnud
       ;
       ldaim csronwards
       sta updatereqd
       ;
edelex rts
;-----------------------------------------------------------------------

       lnk edit03
