 
*  ED3/LINES    40 col        Jul / 11 / 93
*---------------------
*  Minimum Function
 
*  Insert, Get, Update lines
*  Chr2EoL, ClrTmp, ClrRep
*  Newchar, Rollup, Rolldown
*  Up, Down, Oops, Duplic, Freeze
*  Delete, Move, Copy lines
 
*  Insert line into text buffer
*------------------------------
 
INSLIN EQU  $
       MOV  R1,@LINENO        Cline becomes insert pointer
INSL10 MOV  @TADR,@BUFADR     Inserts new line (Tmpbuf)
INSL20 BLWP @INSTLN           Call buffer manager
       CLR  @REFRSH           Insure CPU buffer gets updated
       INC  @EOFM1            Bump end of file pointer
INSL30 RT
 
INSL50 MOV  R1,@LINENO        Insert line below current line
       INC  @LINENO
       JMP  INSL10
 
 
*  Get lines from text buffer
*--------------------------------------------
*  Called from  SCBFIL, FINDNB, REFR10, FSSUB
 
GETLC  EQU  $
       MOV  R1,@LINENO        Use Cline as line pointer
GETL10 MOV  @LINADR,@BUFADR   Text buffer to put it
GETL20 BLWP @GETLN            BufMgr service
       MOV  R0,R0             Check dor error return
       JNE  GETL30            Something is wrong
       INCT R11               Step over error return
GETL30 RT
 
 
 
*  UPDATE line in Text Buffer
*--------------------------------------
*  BLWP @UPDTLN is intercepted in Pr/Ed
*  Becomes branch to ASMCOD
*  This path uses R14
 
UPDT10 CLR  @TOPPAR
       CLR  @CRFLAG
 
UPDATE ABS  @NCHAR            Should this line be updated?
       JEQ  UPDA20            No new chars have been entered
 
UPDA05 C    R1,@EOFM1         Is the line# legit ?
       JGT  UPDA20            Line past end of file !
 
       MOV  R1,@LINENO        Yes - update current line
UPDA10 MOV  @LINADR,@BUFADR   Use screen buffer as source
       A    @DSIX,@BUFADR     Push past line #
 
UPDA12 BLWP @UPDTLN           Update line in edit buffer  <<<<
UPDA20 RT
 
 
*  Clear to end of line key
*--------------------------
 
CTOEOL EQU  $
       MOV  @LINEND,R6        Current end of line
       MOV  R4,R9             Copy Curadr
CTOE10 C    R9,R6             To end yet ?
       JH   UPDA20            Yes
 
       MOVB @DSPACE,*R9+      Fill with blank
       JMP  CTOE10            Check if done
 
 
*  Clear temporary buffer
*-----------------------
 
CLRTMP LI   R14,TMPBUF         Clear temporary buffer
TADR   EQU  $-2
MFILL  LI   R6,80
CLRT10 MOVB @DSPACE,*R14+      Blank buffer for insert line
       DEC  R6                 Count off
       JGT  CLRT10             Not done
       RT
 
*  Clear replace buffer
*----------------------
 
BKREPB LI   R14,REPBUF        Blank replace buffer
BKRE10 LI   R6,160            May blank too much, but ok since it
       JMP  CLRT10            is the last buffer.
 
 
*  Fake up special chars
*-----------------------
 
CHR0A  MOVB @EOT,@KEYRT       Fake up CR
       JMP  NEWC01
 
CHR0C  MOVB @FORMF,@KEYRT     Fake up PA/CR
       JMP  NEWC01
 
*  Process normal key entry
*--------------------------
 
NEWCHR EQU  $
       ABS  @CTLMDE           Special char mode ?
       JEQ  NEWC01            No
 
       SB   @H40,@KEYRT       Convert to standard control key
       SZCB @H80,@KEYRT
 
*  Enter here with fake values qlso
 
NEWC01 SETO @NCHAR            Flag for update of Edit buffer
       SETO @REFRSH           Flag to stop update of CPU buffer
       MOVB @KEYRT,R5         Fetch key
H83    EQU  $-2               Cheap data `yte
       MOV  @CMDRT,R6         Special command return
       JNE  NEWC02            Yes - no word wrap applies
 
       SETO @CHNGED           Flag for recall/quit
       ABS  @WDWRAP           Word wrap mode?
       JNE  NEWC50            Yes
 
*  Normal (and CMD) mode
 
NEWC02 ABS  @ICHRF            No, insert char mode?
       JEQ  NEWC10            No
 
       MOV  @LINADR,R9        Yes, move next characters
       A    @RMRG,R9          End of line at RMRG
       MOV  R4,R6             Curadr -> end of move
       MOV  R9,R8             Copy
NEWC03 DEC  R8                Previous char
NEWC05 C    R8,R6             R6 -> cursor
       JLT  NEWC10            Done?
 
       MOVB *R8,*R9           No - shuffle it along
       DEC  R9                Back off target
       JMP  NEWC03            Back off source too on loop
 
 
*  Now insert character at cursor position
 
NEWC10 EQU  $
       MOVB R5,*R4            Put new char in line buffer
NEWC15 INC  R2                Move cursor right
       JMP  BST05             Adjust cursor
 
*  Wordwrap insert mode
 
NEWC50 EQU  $                 Word wrap mode
       CB   *R4,@EOT          Overstriking a cr?
       JNE  NEWC52            No
 
       MOV  R2,R8             Copy cursor position on line
       INC  R8                Next
       C    R8,@RMRG          Would it be past right margin ?
       JHE  NEWC52            Yes
 
       MOVB *R4,@1(R4)        Put cr out anyw`y.
NEWC52 C    R2,@RMRG          Room for one more character?
       JLT  NEWC10            No, leave room
 
NEWC24 BL   @AUTOIN           Wrap word and insert line
       JMP  NEWC15            Put new char in buffer
 
 
 
*  Rollup - add screen length to Sbias
*-------------------------------------
 
CMRLUP EQU  $
       DECT R3
       JMP  RLUP05
 
ROLLUP EQU  $
       BL   @UPDATE           Update line buffer if needed
RLUP05 CLR  @REFRSH           Allow refresh
       MOV  R1,R8             Working copy of Cline
       A    @STPLEN,R3        Push Sbias along - check later
       A    @STPLEN,R8        Change current line by screen length
       C    R8,@EOF           Past EoF ?
       JHE  RLUP10            Yes
 
       MOV  R8,R1             No, use new Cline
       JMP  BSTART            Exit now
 
RLUP10 C    R3,@EOF           Check Sbias in file
       JLT  RLUP20            T`at's OK
 
       MOV  @EOF,R3           EOF to Sbias
       DEC  R3                Show last line at top
RLUP20 MOV  R3,R1             Use new Sbias
 
*  Common scroll finish
 
BSTART CLR  @CRFLAG           Don't put cr's in.
       CLR  @TOPPAR           Not top of paragraph
       CLR  @SWSF             Get out of insert mode
BST05  MOV  @CMDRT,R11        Check special return
       JEQ  BST10             None - main loop
 
BSTT10 MOV  R3,R1             Sb`as -> Cline
       INC  R1
BST10  B    @START
 
 
*  Rolldown -  subtract screen length from Sbias
*-----------------------------------------------
 
CMRLDN EQU  $
       INCT R3
       JMP  RDWN05
 
ROLDWN EQU  $
       BL   @UPDATE           Update current line if needed
RDWN05 CLR  @REFRSH           Allow screen buffer refresh
       MOV  R1,R8             Copy Cline
       S    @STPLEN,R8        Back up one screen
       JLT  RDWN10            Off screen?
 
       MOV  R8,R1             No - use new Cline
       S    @STPLEN,R3        Back one screen length in file also
       JLT  RDWN20            Off screen!
       JMP  BSTART            No
 
RDWN10 CLR  R1                Cursor to top of screen
RDWN20 CLR  R3                First line in file - Sbias = 0
       JMP  BSTART
 
 
*  Delete line key
*-----------------
 
DELLIN EQU  $
       LI   R9,SDLBUV
       LI   R8,6
       A    @LINADR,R8
       BL   @VMBWL
       DATA LLL
       CLR  @REFRSH           Update cpu screen
DELLN1 MOV  R1,@LINENO        Cline to line pointer
       BLWP @DELTLN           Delete current line
       CLR  @DELAST           Reset DELete last line flag
       C    R1,@EOF           Past EoF ?
       JLT  BST10             No - exit directly
 
       SETO @DELAST           Set DELete last line flag
       MOV  R1,R1             Any lines left ?
       JNE  UPAR05            Scroll screen up
       JMP  BST10             No - exit directly
 
 
*  Up arrow key
*--------------
 
UPAR   EQU  $
       BL   @UPDT10           Update CLINE
UPAR05 DEC  R1                Go to previous line
       C    R1,R3             Cline,Sbias
       JLT  UPAR20            Off screen
UPAR10 JMP  BST10             B @START
 
 
*  Repaint
 
UPAR20 MOV  R3,R3             First line?
       JNE  UPAR30            No
 
UPAR25 CLR  R1                Yes, don't scroll
UPAR30 MOV  R1,R3             Scroll up
       JLT  UPAR25            Cline < 0 error
RFSTAT CLR  @REFRSH
JBST10 JMP  BST10
 
*  Up/Down Arrow for command mode
*--------------------------------
 
CUPAR  EQU  $
       MOV  R3,R3             Check Sbias
       JEQ  BST10             Must be start anyway
 
       DEC  R3                Back a line towards start
CUPR10 CLR  @REFRSH           Force screen buffer refresh
       JMP  BSTT10
 
CDNAR  EQU  $
       C    R3,@EOFM1         Check Sbias
       JHE  BST10             No more to go
 
       INC  R3                Next line to top of screen
       JMP  CUPR10            Go write  new screen
 
 
*  Go to Marker
*--------------
 
GO2MRK EQU  $
       MOV  R1,@MKSAV1        Save current cursor line
       MOV  R3,@MKSAV3        Save current screen bias
       MOV  @MKTBL,R3         Get marker
GO2MRJ BL   @UPDT10           Update buffer
       MOV  R3,R1             Sbias to Cline
GO2STT JMP  RFSTAT            Force screen refresh
 
GO2ORG EQU  $
       MOV  @MKSAV1,R1        Write in saved cursor line
       MOV  @MKSAV3,R3        Write in saved top of screen
       JMP  RFSTAT
 
 
*   Oops Key
*-----------
 
OOPS   EQU  $
       CLR  @REFRSH           Set screen update
       CB   @LSTKEY,@B8E      Was last key a <ctrl-N> ?
       JEQ  OOPS10
 
       CB   @LSTKEY,@H07      Was it a <fctn-3> ?
       JEQ  OOPS10            Undo it then
       RT                     NO action then
 
OOPS10 ABS  @DELAST           Deleted last line?
       JEQ  OOPS20            No
 
       INC  R1                Yes, insert after last line
OOPS20 MOV  R1,@LINENO        For Buf-Mgr
       BL   @MOVE             Retrieve deleted line from VDP
       DATA SDLBUV
       DATA TMPBUF,LLL
       BL   @INSL10           Insert line back in text buffer
       JMP  UPAR10            Restart
 
 
*  Duplicate line key
*--------------------
 
DUPLIC EQU  $
       MOV  R1,@LINENO        Duplicate previous line
       JEQ  DUPLID            Line = 0?
 
       DEC  @LINENO           No, Back up to previous line
       MOV  @LINADR,@BUFADR   Read line from edit buffer
       BLWP @GETLN
DUPLID RT
 
 
*  Freeze lower half screen
*--------------------------
 
FREEZE EQU  $
       LI   R8,STPLEN         Byte saver
       C    *R8,@SSNLEN       Has temp length been set ?
       JHE  FRZ10             No - change to line after cursor
 
       MOV  @SSNLEN,*R8       Restore nominal value
       RT
 
FRZ10  MOV  R1,R0             Copy current Cline
       INC  R0                Advance to next
       S    R3,R0             Remove Sbias for line count
       MOV  R0,*R8            Rewrite temp screen length
       MOV  R0,R8
       LI   R6,40             Line length for 40 col
       MPY  R6,R8             Offset on screen
       MOVB @UDLINE,R8        Line char
       B    @VFILL            Return via Vfill RT
 
 
*-------------------------*
*    COPY, MOVE  Lines    *
*-------------------------*
 
 
*  Move Lines
*------------
 
ML     EQU  $
       BL   @LINUMS           Collect 3 numbers from command line
       LI   R11,MKTBL         Byte saver
       MOV  R13,R14
       A    R12,R13           Restore "to"
       C    R9,*R11           All safely above ?
       JH   ML30              Yes
       C    R6,*R11           All below ?
       JL   ML30              Yes
       C    R12,*R11          Target below ?
       JL   ML10              Yes
       C    R13,*R11          Above then - check "to"
       JHE  ML30              Go subtract block
       JMP  ML40              Can't decide
 
ML10   C    R15,*R11          Below - check "from"
       JLE  ML40              Can't decide
 
ML20   A    R14,*R11          Push marker along after insertion
       JMP  ML50
 
ML30   S    R14,*R11          Pull marker after excision
       JMP  ML50
 
ML40   CLR  *R11              Cancel marker
ML50   BL   @MVLINS           Shuffle line table around
       B    @CMDXIT           All done
 
 
*  Copy Lines
*-------------------------------------------------------------
*  R12 has "after" from LINUM, R15 "start", R13 M/C block size
*  R9,R8,R6 increasing - either from-to-after or after-from-to
 
CL     EQU  $
       BL   @LINUMS           Command line #s
       C    R12,@MKTBL        "After" line #
       JHE  CL30              Copy zone safely above
 
       A    R13,@MKTBL        Adjust by block length
CL30   MOV  R12,R9            Copy "to" as
       MOV  @EOF,R8           Current EoF as mid pointer
       BLWP @CPBLOC           Copy the block
       MOV  @EOF,R6           New EoF as extreme pointer
       JMP  ML50              Now shift this block into place
 
*  Delete lines follows COMAND
*------------------------------
 
