/
        maxline=78.

        STRUCT input
            member term
            member string[82.]
        END-STRUCT input

        PROGRAM editm


delim:  .asciz '_'
prm:    .asciz    <15>'?'
prm2:   .asciz    <15><12>'ed:'
crlf:   .asciz    <15><12>
        .even



        auto supp
FUNCTION editm[lh,cur,s]   ;edit mode with current buffer, s is an 80 byte buf
        t=r2
        typ=r4
        clr supp(r5)
        do forever
            if supp = #0
                mov #prm2,r0
                call outstr

                r0 := cur + #string
                call outstr
            fi
            clr supp(r5)
            t := line[s,speed,#prm]
            typ := ctype[t]
            if strlen[s] = #0
                begin
                select terminator typ of
                case #control
                    if #term[cur] ne t
                        stuff[lh,t,<cur + #string>]
                    fi
                    break
                case #cr
                    ret #1

                case #esc
                    ret #0

                case #con.z
                    #string[cur] := #0
                    ret #0
                esac

                end
            else

                select terminator typ of
                case #control
                    call ccont
                    break

                case #cr or
                case #esc
                    change[s,cur,cur]
                    break

                default
                    call creturn
                    printf "I can't do that"
                esac
            fi
        od


ccont:
        call creturn
        do forever
            r3 := find[lh,t]
            if term(r3) = t
;printf "s=%o r3=%o cur=%o" s,r3,cur
                if change[s,r3,cur] = #0
                    mov #crlf,r0
                    call outstr
                    r0 := r3 + #2
                    call outstr
                    outterm[t]
                else
                    printf "%c deleted" <t + #cntl>
                fi
                inc supp(r5)
                break
            else
                stuff[lh,t,""]
            fi
        od
        rts pc

        auto len,del,scratch[84.],t1,t2,blen
FUNCTION change[s,buffer,cur]      ;do a change cmd in s on buffer

        addrsc=r4
        buf=r3

        if strlen[s] = #1
            ifb @s(r5) = delim
                if buffer ne cur
                    release[delete[buffer]]
                    ret #-1
                fi
            fi
        fi
        addrsc := &scratch
        len := strlen[s]
        del := pos[s,#delim,#1]
        buf := buffer + #string
        blen := strlen[buf]
        if del = #0                                  ;append
            if <blen + len> le #maxline.
                cat[buf,s,#0]
            else
                call toolong
            fi
        elseif del = #1                              ;insert
            if <len + blen - #1> le #maxline.
                clrb scratch(r5)
                cat[&scratch,<s + #1>,buf,#0]
                scopy[buf,&scratch]
            else
                call toolong
            fi
        else                                         ;change
            r2 := s + del - #1
            clrb (r2)
            r2 := pos[buf,s,#1]
            if r2 = #0
                call creturn
                printf "I can't find `%s'" s
            else
                t1 := s + del
                t2 := buf + strlen[s] + r2 - #1
                if <strlen[t1] + strlen[t2] + r2> le #maxline.
                    begin
                    seg[&scratch,buf,#1,<r2 - #1>]
;printf "t1=%o t2=%o s=%o del=%o" t1 t2 s del
;printf "buf=%o strlen=%o r2=%o" buf strlen[s] r2
;printf "scratch=%o" &scratch
                    cat[&scratch,t1,t2,#0]
                    scopy[buf,&scratch]
                    end
                else
                    call toolong
                fi
            fi
        fi
        ret #0
toolong:
        call creturn
        printf "Line too long"
        rts pc
        .end
