* === editmode.asm =====================================================
*
* Author:  William S. Hawes (1987)
* >>> no distribution restrictions <<<
*
* ======================================================================
* Displays the ConMan current edit mode (insert/overstrike) as a prompt
* string > in red for overstrike mode.

         INCLUDE  "exec/types.i"
         INCLUDE  "exec/macros.i"
         INCLUDE  "exec/tasks.i"

         INCLUDE  "libraries/dos.i"
         INCLUDE  "libraries/dosextens.i"

         XREF     _AbsExecBase

         ; DOS library functions

         XLIB     Write

         ; Exec library functions

         XLIB     CloseLibrary
         XLIB     OpenLibrary

         IFND     EXECBASE_I
ThisTask EQU      $114
         ENDC

         IFND     CONMAN_CHLIB_I
CHB_OVER EQU      0                    ; select overstrike?
         ENDC

start:   movea.l  _AbsExecBase,a6
         movea.l  ThisTask(a6),a4      ; our task ID

         ; Open the DOS library

         lea      DOSLib(pc),a1
         moveq    #LIBRARY_VERSION,d0
         CALLSYS  OpenLibrary
         move.l   d0,d5                ; "guaranteed"

         ; Make sure it's an interactive filehandle.

         move.l   pr_COS(a4),d0
         lsl.l    #2,d0
         movea.l  d0,a0
         tst.l    fh_Interactive(a0)   ; interactive?
         beq.s    Error1               ; no

         ; Check whether it came from ConMan (this may change!!)

         cmpa.l   fh_Arg1(a0),a0       ; back pointer?
         bne.s    Error1               ; no
         movea.l  fh_Arg2(a0),a1       ; ConHandler structure

         ; Test the overstrike bit of the ConHandler flags.

         lea      OverSt(pc),a0        ; overstrike string
         btst     #CHB_OVER,LN_TYPE(a1)
         bne.s    1$                   ; overstrike ...
         lea      Insert(pc),a0        ; insert string

1$:      move.l   a0,d2                ; message
2$:      tst.b    (a0)+
         bne.s    2$
         subq.l   #1,a0
         move.l   a0,d3
         sub.l    d2,d3                ; length
         move.l   pr_COS(a4),d1        ; stream
         exg      d5,a6
         CALLSYS  Write
         exg      d5,a6

         moveq    #0,d6
         bra.s    CloseDOS

         ; Errors ...

Error1   moveq    #RETURN_WARN,d6      ; wrong handler?

CloseDOS movea.l  d5,a1
         CALLSYS  CloseLibrary

         move.l   d6,d0
         rts

         ; String constants

DOSLib   dc.b     'dos.library',0
OverSt   dc.b     $9B,'33m>',$9B,'31m',0  ; red > prompt
Insert   dc.b     '>',0                   ; plain > prompt
         CNOP     0,2

         END
