        MAIN "",134

; IX - окно
; HL - адрес строчки в самом окне
; DE - координата
; B  - длина стринга

CPOS    DB 0
CPOST   DB 0

RIGHT   LD A,(APOS)
        INC A
        LD HL,SLEN
        CP (HL)
        JR NC,EDIT1
        PUSH AF
        CALL CURSOR
        POP AF
        LD (APOS),A
        LD HL,(StrAdr)
        INC HL
        LD (StrAdr),HL
        JP edit3

LEFT    LD A,(APOS)
        AND A
        JR Z,EDIT1
        PUSH AF
        CALL CURSOR
        POP AF
        DEC A
        LD (APOS),A
        LD HL,(StrAdr)
        DEC HL
        LD (StrAdr),HL
        JP edit3

InsO    CALL CURSOR
        LD A,#00
Ins     EQU $-1
        CPL 
        LD (Ins),A
        JR EDIT3

LOCK    LD A,(23658)
        AND A
        JR NZ,LOCK1
        LD A,8
        JR LOCK2
LOCK1   XOR A
LOCK2   LD (23658),A
        JR EDIT1

EDIT    LD (StrAdr),HL
        LD (StrAdrT),HL
        LD (CPOS),A
        LD (CPOST),A
        XOR A
        LD (APOS),A
        LD A,B
        LD (SLEN),A
        CALL realwin
        LD (CADR),DE
        RES 1,(IX+6)
;       CALL Window

EDIT3   CALL CURSOR
EDIT1   XOR A
        LD (LAST_KEY),A
        HALT 
        LD A,(LAST_KEY)
        CP 6
        JR Z,LOCK
        CP 9
        JP Z,RIGHT
        CP 8
        JR Z,LEFT
        CP #C7
        JP Z,BEGSTR
        CP #C8
        JP Z,ENDSTR
        CP #C9
        JR Z,InsO
        CP 15
        JP Z,DELETE
        CP " "
        JR NC,EDIT_SYM
        CP 7
        JP Z,SWITCH
        CP 14
        JP Z,EXIT_CANCEL
        CP 13
        JP Z,EXIT_OK
        CP 12
        JR Z,BACK_SP
        JR EDIT1

EDIT_SYM
        EX AF,AF'
        LD A,(Ins)
        AND A
        JR Z,mart1
        LD A,0
SLEN    EQU $-1
        DEC A
        LD HL,APOS
        CP (HL)
        JR Z,eds
        LD HL,0
StrAdrT EQU $-2
        LD B,0
        LD C,A
        ADD HL,BC
        LD A,(HL)
        CP " "
        JR NZ,EDIT2
eds     LD HL,0
StrAdr  EQU $-2
        LD A,0
APOS    EQU $-1
        LD C,A
        LD A,(SLEN)
        SUB C
        LD C,A
        LD B,0
        ADD HL,BC
        DEC HL
        EX DE,HL
        PUSH DE
        POP HL
        DEC HL
        LDDR 
mart1   EX AF,AF'
        CALL PEREKOD
        LD HL,(StrAdr)
        LD (HL),A
        LD A,(APOS)
        INC A
        LD HL,SLEN
        CP (HL)
        JR NC,EDIT2
        LD (APOS),A
        LD HL,(StrAdr)
        INC HL
        LD (StrAdr),HL
EDIT2   RES 1,(IX+6)
        CALL Window
        JP edit3

BACK_SP LD A,(APOS)
        AND A
        JP Z,EDIT1
        DEC A
        LD (APOS),A
        LD A,(CPOS)
        DEC A
        LD (CPOS),A
        LD HL,(StrAdr)
        DEC HL
        LD (StrAdr),HL
        LD A,(Ins)
        AND A
        JR Z,DELETE1
DELETE  LD A,(APOS)
        LD E,A
        LD HL,(StrAdr)
        LD A,(SLEN)
        CP 1
        JR Z,DELETE1
        SUB E
        CP 1
        JR Z,DELETE1
        LD C,A
        LD B,0
        DEC BC
        PUSH HL
        POP DE
        INC HL
        LDIR 
        DEC HL
DELETE1 LD (HL)," "
        JR EDIT2

BEGSTR  CALL CURSOR
        XOR A
        LD (APOS),A
        LD HL,(StrAdrT)
        LD (StrAdr),HL
edit3   LD A,(CPOST)
        LD B,A
        LD A,(APOS)
        ADD A,B
        LD (CPOS),A
        JP EDIT3

ENDSTR  CALL CURSOR
        LD A,(SLEN)
        CP 1
        JR Z,edit3
        LD B,A
        LD E,A
        LD HL,(StrAdrT)
        LD D,0
        ADD HL,DE
        DEC HL
        LD (StrAdr),HL
Fn2     LD A,(HL)
        CP " "
        JR NZ,Fn1
        DEC HL
        DJNZ Fn2
        XOR A
        LD (APOS),A
        LD HL,(StrAdrT)
        LD (StrAdr),HL
        JR edit3

fnz     LD A,(SLEN)
        DEC A
        LD (APOS),A
        JR edit3

Fn1     LD A,B
        LD (APOS),A
        LD A,(SLEN)
        CP B
        JR Z,fnz
        INC HL
        LD (StrAdr),HL
        JR edit3

; Печать курсора на 51 символа
; DE - координаты курсора (начало отсчета)
; A  - смещение от начала <DE>

EXIT_CANCEL
        CALL exob
        SCF 
        RET 

EXIT_OK CALL exob
        XOR A
        RET 

exob    SET 1,(IX+6)
CURSOR  LD A,(CPOS)
        LD DE,0
CADR    EQU $-2
CURS51  EX AF,AF'
        CALL RASCH
        EX AF,AF'
        CP 8
        CALL NC,incc
        PUSH HL
        LD C,A
        ADD A,A
        ADD A,C
        LD BC,TAB51
        LD H,0
        LD L,A
        ADD HL,BC
        LD A,(HL)
        INC HL
        LD E,(HL)
        INC HL
        LD D,(HL)
        POP HL
        ADD A,L
        LD L,A
        LD A,(Ins)
        AND A
        JR NZ,dza1
        LD B,8
        JR djnz1
dza1    LD A,H
        ADD A,6
        LD H,A
        LD B,2
djnz1   LD A,E
        XOR (HL)
        LD (HL),A
        INC L
        LD A,D
        XOR (HL)
        LD (HL),A
        DEC L
        INC H
        DJNZ djnz1
        RET 

TAB51   DB 0,%11111000,%00000000
        DB 0,%00000111,%11000000
        DB 1,%00111110,%00000000
        DB 1,%00000001,%11110000
        DB 2,%00001111,%10000000
        DB 3,%01111100,%00000000
        DB 3,%00000011,%11100000
        DB 4,%00011111,%00000000

incc    SUB 8
        EX AF,AF'
        LD A,L
        ADD A,5
        LD L,A
        EX AF,AF'
        CP 8
        RET C
        JR incc

SWITCH  LD HL,PER_TAB
        DEFHL
        LD HL,LAT_TAB
        AND A
        SBC HL,DE
        JR Z,SWITCH_R
        LD HL,LAT_TAB
        JR SWITCH1
SWITCH_R
        LD HL,RUS_TAB
SWITCH1 LD (PER_TAB),HL
        JP EDIT1

PEREKOD LD L,A
        LD H,0
        LD DE,LAT_TAB
PER_TAB EQU $-2
        ADD HL,DE
        LD A,(HL)
        RET 

;             0123456789ABCDEF
RUS_TAB
        DEFB "................";0
        DEFB "................";1
        DEFB " !"
        DEFB #22;КАВЫЧКИ
        DEFB    "#$%&'()*+,-./";2
        DEFB "0123456789:;<=>?";3
        DEFB "ЮАБЦДЕФГХИЙКЛМНО";4
        DEFB "ПЯРСТУЖВЬЫЗ[\]Ч_";5
        DEFB "юабцдефгхийклмно";6
        DEFB "пярстужвьыз{|}~.";7
        DEFB "................";8
        DEFB "................";9
        DEFB "................";A
        DEFB "................";B
        DEFB "...Э.ЩШ....щшэ..";C
        DEFB "................";D
        DEFB "..ч.............";E
        DEFB "................";F

;             0123456789ABCDEF
LAT_TAB
        DEFB "................";0
        DEFB "................";1
        DEFB " !"
        DEFB #22;КАВЫЧКИ
        DEFB    "#$%&'()*+,-./";2
        DEFB "0123456789:;<=>?";3
        DEFB "@ABCDEFGHIJKLMNO";4
        DEFB "PQRSTUVWXYZ[\]^_";5
        DEFB "`abcdefghijklmno";6
        DEFB "pqrstuvwxyz{|}~.";7
        DEFB "................";8
        DEFB "................";9
        DEFB "................";A
        DEFB "................";B
        DEFB "...|.][....}{\..";C
        DEFB "................";D
        DEFB "..~.............";E
        DEFB "................";F

