Subversion Repositories NedoOS

Rev

Rev 1167 | Rev 1324 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. ;TODO в режиме wrap:
  2. ;- при стирании перерисовывать последнюю строку и всё за ней, если она уничтожена
  3. ;- при добавлении перерисовывать строки ниже, если изменилось число строк
  4.  
  5. texted_XYTOP=0x0000
  6. texted_HGT=24
  7. texted_WID=80
  8. _texted_PANELCOLOR=0x0700;0x38
  9.  
  10. textview
  11.         ;xor a
  12.         ;ld h,a
  13.         ;ld l,a
  14.         ;call texted_settop
  15.         ;ld (curlineaddr),hl
  16.         ;ld (curlineaddrHSB),a
  17.         ;ld hl,1
  18.         ;ld (texted_ncurline),hl
  19.         ;ld hl,0
  20.         ;ld (curxy),hl
  21.         call texted_gotobof
  22.        
  23.         call texted_panel        
  24.        
  25.         call texted_calclines
  26.  
  27. texted_redrawloop
  28.         call setredrawflag
  29. texted_mainloop
  30. texted_lineredrawflag=$
  31.         scf ;/or a
  32.         call c,texted_prcurline ;might set redrawflag
  33. texted_redrawflag=$
  34.         scf ;/or a
  35.         call c,texted_prcurpage
  36.  
  37. ;texted_waitkey_nokey
  38.         ld de,(curxy)
  39.         call nv_setxy
  40.        if 0
  41.         ld a,2
  42. mainloop_yieldkeep
  43.         ld (wasnokey),a
  44.         YIELDKEEP
  45.         ld a,55+128 ;"or a"
  46.         ld (texted_wasyield),a
  47.        endif
  48. texted_waitkey_nokey
  49.       if 0
  50.       else
  51.         YIELD
  52.       endif
  53.         GETKEY_ ;OS_GETKEYNOLANG
  54.         ;or a ;cp NOKEY ;keylang==0?
  55.         ;jr nz,texted_mainloop_keyq
  56.         ;cp c ;keynolang==0?
  57.         ;ld a,c ;keynolang
  58.         ;cp NOKEY ;TODO отличить от отсутствия фокуса nz? (не в фокусе клавиши не отдаются)
  59.         jr nz,texted_mainloop_keyq ;event
  60.         ;GETKEY_
  61.         ;jr nz,texted_mainloop_keyq ;event
  62.         ;GETKEY_
  63.         ;jr nz,texted_mainloop_keyq ;event бывает 3-символьный
  64.       if 0
  65.        ld a,(stdindatacount)
  66.        or a
  67.        jr nz,texted_mainloop
  68. ;если два раза подряд нет события, то делаем YIELD, иначе YIELDKEEP
  69. ;рисовать панельку только при отсутствии события после YIELD
  70. wasnokey=$+1
  71.         ld a,1
  72.         dec a
  73.         jr nz,mainloop_yieldkeep
  74. texted_wasyield=$
  75.         scf
  76.         jr nc,nopanel
  77.       endif
  78. ;texted_panelredrawflag=$
  79.         ;scf ;/or a
  80.         call texted_panel ;call c
  81.       if 0
  82.         ld de,(curxy)
  83.         call nv_setxy
  84. nopanel
  85.         YIELD
  86.         ld a,55 ;"scf"
  87.         ld (texted_wasyield),a
  88.         jr texted_waitkey_nokey
  89.       else
  90.         jr texted_mainloop
  91.       endif        
  92. texted_mainloop_keyq      
  93.  
  94.         cp key_redraw
  95.         jr z,texted_redrawloop
  96.         cp key_esc
  97.         ret z
  98.        
  99.         ld hl,texted_mainloop
  100.         push hl
  101.         cp key_up
  102.         jp z,texted_up
  103.         cp key_down
  104.         jp z,texted_down
  105.         cp key_pgup
  106.         jp z,texted_pgup
  107.         cp key_pgdown
  108.         jp z,texted_pgdown
  109.          cp key_F1;extS
  110.          jp z,texted_changeencoding
  111.         cp key_home
  112.         jp z,texted_home
  113.         cp key_end
  114.         jp z,texted_end
  115.         cp key_left
  116.         jp z,texted_left
  117.         cp key_right
  118.         jp z,texted_right
  119.         cp key_sspgup;ext3
  120.         jp z,texted_gotobof;home
  121.         cp key_sspgdown;ext4
  122.         jp z,texted_gotoeof;end
  123.         ;cp extW
  124.         ;jp z,texted_wrap
  125.          cp key_ins
  126.          jp z,texted_wrap
  127.         cp key_backspace
  128.         jp z,texted_backspace
  129.         cp key_del
  130.         jp z,texted_del
  131.         cp key_enter
  132.         jp z,texted_enter
  133.         cp key_csenter
  134.         jp z,texted_save
  135.         cp key_F2
  136.         jp z,texted_save
  137.         cp 0x20
  138.         ret c ;прочие системные кнопки не нужны
  139. typein
  140.         ld c,a
  141.         call linesize_minus_x ;sz<x = error
  142.         call c,insert_minushl_spaces
  143.         call calccursoraddr
  144.         call insertbyte
  145. ;TODO wrap (reprint screen if needed)        
  146.         call setlineredrawflag;texted_prcurpage
  147.         jp texted_right
  148.  
  149. linesize_minus_x
  150. ;sz<x = CY
  151.         ld hl,(curlineaddr)
  152.         ld a,(curlineaddrHSB)
  153.         call calclinesz ;hl=sz
  154.         ex de,hl
  155.         call calccurlinex
  156.         ex de,hl
  157.         or a
  158.         sbc hl,de ;sz<x = error
  159.         ret
  160.  
  161. insert_minushl_spaces
  162.         push bc
  163. textinsertsymbol_pseudospace0
  164.         push hl ;sz-x < 0
  165.         call texted_end
  166.         call calccursoraddr
  167.         ld c,' '
  168.         call insertbyte
  169.         pop hl ;sz-x < 0
  170.         inc hl
  171.         inc h
  172.         dec h
  173.         jr nz,textinsertsymbol_pseudospace0
  174.         call texted_end
  175.         pop bc
  176.         ret
  177.  
  178. texted_enter
  179.         call linesize_minus_x ;sz<x = error
  180.         ;jr nc,texted_enter_nopseudospaces
  181.         call c,texted_end
  182. ;texted_enter_nopseudospaces
  183.         call calccursoraddr
  184.         ld c,0x0d
  185.         call insertbyte
  186.         ld c,0x0a
  187.         call insertbyte
  188.         xor a
  189.         ld (curx),a
  190.         ld hl,0
  191.         ld (texted_prline_shift),hl
  192.  
  193.         call setredrawflag;texted_prcurpage
  194.  
  195.         call incnlines
  196.         jp texted_down
  197.        
  198. texted_save
  199. texted_filenameaddr=$+1
  200.         ld de,defaultfilename
  201.         OS_CREATEHANDLE
  202.         or a
  203.         ret nz
  204. ;b=new file handle
  205.         push bc
  206.         ld a,b
  207.         ld (cmd_savepage_handle),a
  208.  
  209.         ;ld de,(fcb+FCB_FSIZE)
  210.         ;ld hl,(fcb+FCB_FSIZE+2)
  211.         call getsize
  212.         ex de,hl
  213.         ld l,a
  214.         ld h,0
  215.         ld a,0 ;page number
  216. texted_save0
  217. ;a=page number in table (0..)
  218. ;hlde=remaining size
  219.         push af ;pg
  220.         push de ;remaining LSW
  221.         push hl ;remaining HSW
  222.         call setpg32k
  223.         ld a,d
  224.         and 0xc0
  225.         or h
  226.         or l
  227.         jr z,$+5 ;de=size
  228.         ld de,#4000
  229.         ex de,hl ;hl=pg size
  230.          push hl ;hl=pg size
  231.         call cmd_savepage
  232.          pop bc ;bc=pg size
  233.         pop hl ;remaining HSW
  234.         pop de ;remaining LSW
  235.         ex de,hl
  236.         or a
  237.         sbc hl,bc
  238.         ex de,hl
  239.         jr nc,$+3
  240.         dec hl ;size = size-pgsize
  241.         ld a,h
  242.         or l
  243.         or d
  244.         or e
  245.         jr z,texted_save_popq
  246.         pop af ;pg
  247.         inc a
  248.         jr texted_save0
  249. texted_save_popq
  250.         pop af
  251.         pop bc
  252.         OS_CLOSEHANDLE
  253.         jp setunchanged
  254.         ;jp setpanelredrawflag
  255.  
  256. cmd_savepage
  257. ;hl=size
  258. ;out: a=error
  259. cmd_savepage_handle=$+1
  260.         ld b,0
  261.         ld de,#8000
  262. ;B = file handle, DE = Buffer address, HL = Number of bytes to write
  263.         OS_WRITEHANDLE
  264. ;HL = Number of bytes actually written, A=error
  265.         ret
  266.        
  267. setredrawflag
  268.         ld a,55 ;scf
  269.         ld (texted_redrawflag),a
  270.         ret
  271. ;setpanelredrawflag
  272.         ;ld a,55 ;scf
  273.         ;ld (texted_panelredrawflag),a
  274.         ;ret
  275. setlineredrawflag
  276.         ld a,55 ;scf
  277.         ld (texted_lineredrawflag),a
  278.         ret
  279.  
  280. calccurlinex
  281.         ld hl,(texted_prline_shift)
  282.         ld a,(curx)
  283.         add a,l
  284.         ld l,a
  285.         ret nc
  286.         inc h
  287.         ret
  288.        
  289. calccursoraddr
  290. ;TODO учитывать ширину строки (правее строки простое сложение бессмысленно)
  291.         call calccurlinex
  292.         ex de,hl
  293.         ld hl,(curlineaddr)
  294.         ld a,(curlineaddrHSB)
  295.         add hl,de
  296.         adc a,0
  297.         ret
  298.        
  299. texted_backspace
  300. ;TODO wrap (set y, reprint screen if needed)
  301.         call calccurlinex
  302.         ld a,h
  303.         or l
  304.         jr z,texted_backspace_startline
  305.  
  306.         call linesize_minus_x ;sz<x = error
  307.         jp c,texted_left
  308.  
  309.         call calccursoraddr
  310.         call prevbyte
  311.         ;call isbof
  312.         ;ret z
  313.         call deletebyte        
  314.         call setlineredrawflag
  315.         jp texted_left
  316. texted_backspace_startline
  317.         call calccursoraddr
  318.         call isbof
  319.         ret z
  320.         call prevbyte
  321.         call getbyte
  322.         push bc
  323.         call deletebyte
  324.         pop bc
  325.         ld b,a
  326.          ld a,c ;deleted byte
  327.          cp 0x0d
  328.          ld a,b
  329.          jr z,texted_backspace_startline_onlycr ;for CR texts
  330.         ld a,c
  331.         cp 0x0a
  332.         ld a,b
  333.         jr nz,texted_backspace_startline_onlycr
  334.         call isbof
  335.         jr z,texted_backspace_startline_onlycr
  336.         call prevbyte
  337.         call getbyte
  338.         ld b,a
  339.         ld a,c
  340.         cp 0x0d
  341.         ld a,b
  342.         ;jr nz,texted_backspace_startline_onlycr
  343.         call z,deletebyte ;0x0d
  344. texted_backspace_startline_onlycr
  345. ;ahl=at deleted CR/LF
  346. ;if not CR/LF before, go to "previous line" (after earlier CR/LF) and count difference
  347. ;else x=0
  348.          push af
  349.          push hl
  350.         call prevbyte
  351.         call getbyte ;c
  352.          pop hl
  353.          pop af
  354.          ld d,h
  355.          ld e,l
  356.         ld b,a
  357.         ld a,c
  358.         cp 0x0d
  359.         jr z,texted_backspace_startline_setxshift_hlminusde
  360.         cp 0x0a
  361.         jr z,texted_backspace_startline_setxshift_hlminusde
  362.         ld a,b
  363.  
  364.         ;push af
  365.         push hl
  366.         call texted_prevline ;CY=error (impossible?)
  367.         ex de,hl
  368.         ;ld c,a
  369.         pop hl ;hl=at deleted CR?,de=prevline
  370.         ;pop af
  371. texted_backspace_startline_setxshift_hlminusde
  372.         or a
  373.         sbc hl,de
  374.         ;sbc a,c
  375.         call setxshift_hl
  376.        
  377.         call setredrawflag
  378.  
  379.         call decnlines
  380.         jp texted_up
  381.  
  382. decnlines
  383.         push hl
  384.         ld hl,(nlines)
  385.         dec hl
  386.         ld (nlines),hl
  387.         pop hl
  388.         ret
  389. incnlines
  390.         push hl
  391.         ld hl,(nlines)
  392.         inc hl
  393.         ld (nlines),hl
  394.         pop hl
  395.         ret
  396.  
  397. texted_home
  398. ;TODO wrap (set y)
  399.         ld hl,0
  400.         call setxshift_hl
  401.         jp setredrawflag
  402.  
  403. texted_end
  404. ;TODO wrap (set y)
  405.         ;call calccursoraddr
  406.         ld hl,(curlineaddr)
  407.         ld a,(curlineaddrHSB)
  408.         call calclinesz ;hl=sz
  409.         call setxshift_hl
  410.         jp setredrawflag
  411.  
  412. calclinesz
  413. ;ahl=line addr
  414. ;out: hl=sz (keeps bc,de)
  415.         push bc
  416.         call texted_nextline ;CY=error
  417.         call c,getsize
  418.         call nc,skipbackcrlf
  419.         ld bc,(curlineaddr)
  420.         or a
  421.         sbc hl,bc
  422.         pop bc
  423.         ret
  424.  
  425. skipbackcrlf
  426. ;out: ahl=eof or endline+1 (at cr/lf)
  427.         call isbof
  428.         ret z
  429.         call prevbyte
  430.         call getbyte
  431.         ld b,a
  432.          ld a,c
  433.          cp 0x0d
  434.          ld a,b
  435.          ret z ;for CR texts
  436.         ld a,c
  437.         cp 0x0a
  438.         ld a,b
  439.         jp nz,nextbyte
  440.         call isbof
  441.         ret z
  442.         call prevbyte
  443.         call getbyte
  444.         ld b,a
  445.         ld a,c
  446.         cp 0x0d
  447.         ld a,b
  448.         jp nz,nextbyte
  449.         ret
  450.        
  451. setxshift_hl
  452. ;X=hl=ahl-startline
  453. ;x+shift = X:
  454. ;x = min(X,texted_WID-1), но под mod8 должен совпадать с X
  455. ;shift = X-x
  456.         ld bc,texted_WID-1
  457.         call minhl_bc_tobc
  458.         ld a,c
  459.         xor l
  460.         and %11111000
  461.         xor l
  462.         ld c,a
  463.         ld (curx),a
  464.         or a
  465.         sbc hl,bc
  466.         ld (texted_prline_shift),hl
  467.         ret
  468.        
  469. texted_del
  470. ;TODO wrap (set y, reprint screen if needed)
  471.         call linesize_minus_x ;sz<x = error
  472.         jr c,texted_del_newline
  473.         jr z,texted_del_newline
  474.         call texted_right
  475.         jp texted_backspace
  476. texted_del_newline
  477. ;удаление ентера справа (backspace не умеет)
  478. ;hl=sz-x
  479.         call c,insert_minushl_spaces
  480.         call setredrawflag
  481.         call calccursoraddr
  482.         call iseof
  483.         ret z
  484.         call decnlines
  485.         call getbyte
  486.         push af
  487.         call deletebyte
  488.         pop bc ;b=deleted byte
  489.         ld c,a
  490.         ld a,b
  491.         cp 0x0a
  492.         ld a,c
  493.         ret z ;endline = 0x0a
  494.         call iseof
  495.         ret z
  496.         call getbyte
  497.         ld b,a
  498.         ld a,c
  499.         cp 0x0a
  500.         ld a,b
  501.         ret nz
  502.         call deletebyte        
  503.         ret
  504.        
  505. texted_wrap
  506.         ld a,(wrapflag)
  507.         xor 128
  508.         ld (wrapflag),a
  509.         ld hl,0
  510.         ld (texted_prline_shift),hl
  511.         ld (curxy),hl
  512.         call texted_calclines
  513.         ld hl,(curtoptextaddr)
  514.         ld a,(curtoptextHSB)
  515.         ld (curlineaddr),hl
  516.         ld (curlineaddrHSB),a
  517.         call texted_calccurline
  518.         jp setredrawflag;texted_prcurpage
  519.        
  520. texted_right
  521. ;TODO X<16384
  522.         ;call setpanelredrawflag
  523.         ld de,(curxy)
  524.         inc e
  525.         ld a,e
  526.         cp texted_WID
  527.         ld (curx),a
  528.         ret c
  529.          call iswrapon ;CY=on
  530.          jr c,texted_right_wrap
  531.         ld hl,(texted_prline_shift)
  532.         ld bc,8
  533.         add hl,bc
  534.         ld (texted_prline_shift),hl
  535.         ld a,e
  536.         sub c;8
  537.         ld (curx),a
  538.         jp setredrawflag;texted_prcurpage
  539.        
  540. texted_right_wrap
  541.         xor a
  542.         ld (curx),a
  543.         jp texted_down
  544.        
  545. texted_left
  546.         ;call setpanelredrawflag
  547.         ld de,(curxy)
  548.         ld a,e
  549.         sub 1
  550.         ld e,a
  551.         ld (curx),a
  552.         ret nc
  553.         xor a
  554.         ld (curx),a
  555.         ld hl,(texted_prline_shift)
  556.         ld bc,8
  557.         or a
  558.         sbc hl,bc
  559.         ret c
  560.         ld (texted_prline_shift),hl
  561.         ld a,e
  562.         add a,c;8
  563.         ld (curx),a
  564.         jp setredrawflag;texted_prcurpage
  565.        
  566. texted_prcurline
  567.         ld a,55+0x80 ;or a
  568.         ld (texted_lineredrawflag),a
  569.         ld de,(curxy)
  570.         ld e,0
  571.         push de
  572.         call nv_setxy
  573.         pop de
  574. ;print lines until CR or EOF
  575.         ld hl,(curlineaddr)
  576.         ld a,(curlineaddrHSB)
  577. texted_prcurline_continue0
  578.         push de
  579.         call texted_prline_nextline
  580.         call c,getsize
  581.         call iseof
  582.         pop de
  583.         ret z
  584.         call prevbyte
  585.         call getbyte
  586.         ld b,a
  587.         ld a,c
  588.         cp 0x0d
  589.         ret z
  590.         cp 0x0a
  591.         ret z
  592.         inc d
  593.         ld a,d
  594.         cp texted_HGT
  595.         ret z
  596.         ld a,b
  597.         call nextbyte
  598.         jr texted_prcurline_continue0
  599.  
  600. texted_prcurpage
  601.          ;ld e,0
  602.          ;OS_CLS
  603.         ld a,55+0x80 ;or a
  604.         ld (texted_redrawflag),a
  605.         ld hl,(curtoptextaddr)
  606.         ld a,(curtoptextHSB)
  607.         jp texted_prpage
  608.        
  609. texted_gotobof
  610.         xor a
  611.         ld h,a
  612.         ld l,a
  613. ;texted_gotobof_ok
  614.         call texted_settop
  615.         ld (curlineaddr),hl
  616.         ld (curlineaddrHSB),a
  617.         ;ld hl,1
  618.         ;ld (texted_ncurline),hl
  619. ;ahl=curtextline (kept)
  620.         call texted_calccurline
  621.         ld hl,0
  622.         ld (curxy),hl
  623.         jp setredrawflag;texted_prcurpage
  624.  
  625. getsize
  626.         ld hl,(filesize)
  627.         ld a,(filesizeHSW)
  628.         ret
  629.        
  630. texted_gotoeof
  631.         call getsize
  632.         ld b,texted_HGT
  633. texted_end0
  634.         push bc
  635.         call texted_prevline
  636.         pop bc
  637.         djnz texted_end0
  638. ;ahl=curtextline (kept)
  639.         call texted_calccurline
  640.         jp texted_pgdown_bottom
  641.  
  642.  
  643. deccurline
  644.         push hl
  645.         ld hl,(texted_ncurline)
  646.         dec hl
  647.         ld (texted_ncurline),hl
  648.         pop hl
  649.         ret
  650. deccury
  651.         push hl
  652.         ld hl,cury
  653.         dec (hl)
  654.         pop hl
  655.         ret
  656.        
  657. inccurline
  658.         push hl
  659.         ld hl,(texted_ncurline)
  660.         inc hl
  661.         ld (texted_ncurline),hl
  662.         pop hl
  663.         ret
  664. inccury
  665.         push hl
  666.         ld hl,cury
  667.         inc (hl)
  668.         pop hl
  669.         ret
  670.  
  671. texted_up
  672.         ld hl,(curlineaddr)
  673.         ld a,(curlineaddrHSB)
  674.         call texted_prevline
  675.         ret c
  676.         ld (curlineaddr),hl
  677.         ld (curlineaddrHSB),a
  678.         push af
  679.         push hl
  680.         call deccurline
  681.         ;call setpanelredrawflag
  682.         pop hl
  683.         pop af
  684.         ld c,a
  685.         ld a,(cury)
  686.         sub 1
  687.         ld (cury),a
  688.         ret nc
  689.         inc a
  690.         ld (cury),a
  691.         ld a,c
  692.         call texted_settop
  693.         push af
  694.         push hl
  695.         ld de,texted_XYTOP
  696.         ld hl,256*texted_HGT + texted_WID
  697.         call scrolldown ;OS_SCROLLDOWN
  698.         ld de,texted_XYTOP
  699.         call nv_setxy
  700.         pop hl
  701.         pop af
  702.         call texted_prline
  703.         ret
  704.  
  705. texted_down
  706.         ld hl,(curlineaddr)
  707.         ld a,(curlineaddrHSB)
  708.         call texted_nextline
  709.         ret c
  710.         ld (curlineaddr),hl
  711.         ld (curlineaddrHSB),a
  712.         push af
  713.         push hl
  714.         call inccurline
  715.         ;call setpanelredrawflag
  716.         pop hl
  717.         pop af
  718.         ld c,a
  719.         ld de,(curxy)
  720.         inc d
  721.         ld a,d
  722.         cp texted_HGT
  723.         ld (curxy),de
  724.         ret c
  725.         dec a
  726.         ld (cury),a
  727.         ld a,c
  728.         push af
  729.         push hl
  730.         ld de,texted_XYTOP
  731.         ld hl,256*texted_HGT + texted_WID
  732.         call scrollup ;OS_SCROLLUP
  733.         ld de,texted_XYTOP+((texted_HGT-1)*256)
  734.         call nv_setxy
  735.         pop hl
  736.         pop af
  737.         call texted_prline;_nextline
  738. curtoptextaddr=$+1
  739.         ld hl,0
  740. curtoptextHSB=$+1
  741.         ld a,0
  742.         call texted_nextline
  743. texted_settop        
  744.         ld (curtoptextaddr),hl
  745.         ld (curtoptextHSB),a
  746.         ret
  747.  
  748. texted_pgup
  749.         ;call setpanelredrawflag
  750.         ld a,(cury)
  751.         or a
  752.         ld b,a
  753.         ld hl,(curlineaddr)
  754.         ld a,(curlineaddrHSB)
  755.         jr z,texted_pgup_top
  756. texted_pgup_gotop0
  757.         push bc
  758.         call texted_prevline
  759.          call nc,deccurline
  760.          call nc,deccury
  761.         pop bc
  762.         djnz texted_pgup_gotop0
  763.         ld (curlineaddr),hl
  764.         ld (curlineaddrHSB),a
  765.         ret
  766. texted_pgup_top
  767.         ld b,texted_HGT-1
  768. texted_pgup0
  769.         push bc
  770.         call texted_prevline
  771.          call nc,deccurline
  772.         pop bc
  773.         djnz texted_pgup0
  774.         call texted_settop
  775.         ld (curlineaddr),hl
  776.         ld (curlineaddrHSB),a
  777.         call setredrawflag
  778.         ret
  779.        
  780. texted_pgdown
  781.         ;call setpanelredrawflag
  782.         ld a,(cury)
  783.         sub texted_HGT-1
  784.         neg
  785.         ld b,a
  786.         ld hl,(curlineaddr)
  787.         ld a,(curlineaddrHSB)
  788.         jr z,texted_pgdown_bottom
  789. texted_pgdown_gobottom0
  790.         push bc
  791.         call texted_nextline
  792.          call nc,inccurline
  793.          call nc,inccury
  794.         pop bc
  795.         djnz texted_pgdown_gobottom0
  796.         ld (curlineaddr),hl
  797.         ld (curlineaddrHSB),a
  798.         ret
  799. texted_pgdown_bottom
  800.         push af
  801.         xor a
  802.         ld (cury),a
  803.         pop af
  804.         call texted_settop
  805.         ld b,texted_HGT-1
  806. texted_pgdown0
  807.         push bc
  808.         call texted_nextline
  809.          call nc,inccurline
  810.          call nc,inccury
  811.         pop bc
  812.         djnz texted_pgdown0
  813.         ld (curlineaddr),hl
  814.         ld (curlineaddrHSB),a
  815.         jp setredrawflag
  816.        
  817. texted_calccurline
  818. ;ahl=curtextline (kept)
  819.         push af
  820.         push hl
  821.         ld (texted_calccurline_old),hl
  822.         ld (texted_calccurline_oldHSB),a
  823.         ld ix,1;0
  824.          or h
  825.          or l
  826.          jr z,texted_calccurline_countq
  827.         xor a
  828.         ld h,a
  829.         ld l,a
  830. texted_calccurline_count0
  831.         call texted_nextline ;texted_pseudoprline
  832.  
  833.         if 1==0
  834.         jr c,texted_calccurline_countq ;не помогает
  835.         ex de,hl
  836. texted_calccurline_old=$+1
  837.         ld hl,0
  838.         or a
  839.         sbc hl,de ;nc: hl<=old
  840.         ld l,a
  841. texted_calccurline_oldHSB=$+1
  842.         ld a,0
  843.         sbc a,l
  844.         ld a,l
  845.         inc ix
  846.         jr nc,texted_calccurline_count0 ;nc: ahl<=old
  847.         else
  848.        
  849.         push hl
  850. texted_calccurline_old=$+1
  851.         ld bc,0
  852.         or a
  853.         sbc hl,bc ;nc: hl>=old
  854.         ld l,a
  855. texted_calccurline_oldHSB=$+1
  856.         sbc a,0
  857.         ld a,l
  858.         pop hl
  859.         inc ix
  860.         jr c,texted_calccurline_count0 ;cy: ahl<old
  861.         endif
  862. texted_calccurline_countq
  863.         ;push ix
  864.         ;pop hl
  865.         ld (texted_ncurline),ix;hl
  866.         pop hl
  867.         pop af
  868.         ret
  869.        
  870. texted_calclines
  871. ;sets nlines
  872.         xor a
  873.         ld h,a
  874.         ld l,a
  875.         ld (nlines),hl
  876. texted_calclines0
  877.         call incnlines
  878.         call texted_nextline
  879.         jr nc,texted_calclines0
  880.         ret
  881.        
  882. clear_keyboardbuffer
  883.         push bc
  884.         ld b,50;5
  885. clear_keyboardbuffer0
  886.         push bc
  887.         GETKEY_
  888.         pop bc
  889.         djnz clear_keyboardbuffer0
  890.         pop bc
  891.         ret
  892.        
  893. texted_changeencoding
  894.         ld hl,texted_prline_recodepatch
  895.         ld a,(hl)
  896.         xor 0x7e ;"ld a,(hl)"
  897.         ld (hl),a
  898.         call setredrawflag;texted_prcurpage
  899.         ;call texted_prcurpage
  900.         ;ret
  901. texted_panel
  902.         ;ld a,55+0x80 ;or a
  903.         ;ld (texted_panelredrawflag),a
  904.         ld de,0x1800
  905.         call nv_setxy
  906.         ld de,_texted_PANELCOLOR
  907.         SETCOLOR_
  908.        
  909.         ld a,(texted_prline_recodepatch)
  910.         or a
  911.         ld hl,t866
  912.         jr z,$+5
  913.         ld hl,twin
  914.         call prtext
  915.        
  916. fchanged=$+1
  917.         ld a,' '
  918.         PRCHAR_
  919.         call calccurlinex
  920.         inc hl
  921.         call prword
  922.         ld a,','
  923.         PRCHAR_
  924. texted_ncurline=$+1
  925.         ld hl,0
  926.         call prword
  927.         ld a,'/'
  928.         PRCHAR_
  929.         ld hl,(nlines)
  930.         exx
  931.         ld hl,0
  932.         exx
  933.         call prdword
  934.         ld a,' '
  935.         PRCHAR_
  936.         call getsize
  937.         exx
  938.         ;ld hl,(fcb+FCB_FSIZE+2)
  939.         ld l,a
  940.         ld h,0
  941.         exx
  942.         ;ld hl,(fcb+FCB_FSIZE)
  943.         call prdword
  944.         ld de,tspaces_filename
  945.         ld hl,TSPACES_FILENAME_SZ
  946.         call sendchars
  947.         ;ld de,_texted_PANELCOLOR;#38
  948.         ;OS_PRATTR
  949.         ld de,_COLOR
  950.         SETCOLOR_
  951.        
  952.         ret
  953.  
  954. tspaces_filename
  955.         db ' '
  956. tshown_filename
  957.         ds TSPACES_FILENAME_SZ,' '
  958.        
  959. twin
  960.         db "WIN",0
  961. t866
  962.         db "866",0
  963.  
  964.      
  965. isbof
  966. ;out: z=bof
  967.         push bc
  968.         ld c,a
  969.         or h
  970.         or l
  971.         ld a,c
  972.         pop bc
  973.         ret
  974. iseof
  975. ;out: z=eof
  976.         push bc
  977.         push de
  978.         push hl
  979.         ;ld de,(fcb+FCB_FSIZE)
  980.         ;ld bc,(fcb+FCB_FSIZE+2)
  981.         ;ld b,a
  982.         ;or a
  983.         ;sbc hl,de
  984.         ;sbc a,c
  985.         ;ld a,b
  986.         ex de,hl
  987.         ld c,a
  988.         call getsize
  989.         or a
  990.         sbc hl,de
  991.         sbc a,c
  992.         or h
  993.         or l
  994.         ld a,c
  995.         pop hl
  996.         pop de
  997.         pop bc
  998.         ret
  999.      
  1000. texted_prpage
  1001. ;ahl=addr
  1002.         ld (curtoptextaddr),hl
  1003.         ld (curtoptextHSB),a
  1004.         push af
  1005.         ld de,texted_XYTOP
  1006.         call nv_setxy
  1007.         pop af
  1008.         ld b,texted_HGT
  1009. texted_prpage0
  1010.         push bc
  1011.         ;push de
  1012.         call texted_prline_nextline
  1013.         call c,getsize
  1014.         ;pop de
  1015.         pop bc
  1016.         ;inc d
  1017.         djnz texted_prpage0
  1018.         ;call clear_keyboardbuffer
  1019.         ret
  1020.        
  1021. texted_prevline
  1022. ;ahl=addr
  1023. ;line < 16K
  1024. ;out: ahl, CY=error (keeps de)
  1025.         call isbof
  1026.          scf
  1027.         ret z
  1028. ;перед нами может быть #0d или #0d,#0a или ничего
  1029.         ld (findprevline_old),hl
  1030.         ld (findprevline_oldHSB),a
  1031.        
  1032.         or a
  1033.         ld bc,0xffff
  1034.         jr nz,$+4
  1035.          ld b,h
  1036.          ld c,l
  1037.          
  1038.         push de
  1039.         ld de,0x4000
  1040.         or a
  1041.         sbc hl,de
  1042.         sbc a,e;0
  1043.         pop de
  1044.        
  1045.         push af
  1046.         push hl
  1047.         call ahl_to_pgaddr
  1048.         set 6,h
  1049. ;hl=c000+, можно уменьшать
  1050. ;bc=number of chars to go != 0
  1051.  
  1052. ;перед нами может быть #0d или #0d,#0a или ничего
  1053.         dec hl
  1054.         dec bc
  1055.         ld a,b
  1056.         or c
  1057.         jr z,texted_prevlineq
  1058.         ld a,0x0a
  1059.         cp (hl)
  1060.         jr nz,texted_prevline_nolf
  1061.         dec hl
  1062.         dec bc
  1063.         ld a,b
  1064.         or c
  1065.         jr z,texted_prevlineq
  1066. texted_prevline_nolf
  1067.         ld a,0x0d
  1068.         cp (hl)
  1069.         jr nz,texted_prevline_nocr
  1070.         dec hl
  1071.         dec bc
  1072.         ld a,b
  1073.         or c
  1074.         jr z,texted_prevlineq
  1075. texted_prevline_nocr
  1076. ;сейчас мы находимся перед символом конца предыдущей строки (не на нём)
  1077. texted_prevline0
  1078.         ld a,(hl)
  1079.         cp 0x0d
  1080.         jr z,texted_prevline_cr
  1081.         cp 0x0a
  1082.         jr z,texted_prevline_lf
  1083.         dec hl
  1084.         dec bc
  1085.         ld a,b
  1086.         or c
  1087.         jr nz,texted_prevline0
  1088. ;beginning of file
  1089.         jr texted_prevlineq
  1090. texted_prevline_cr
  1091. texted_prevline_lf
  1092. ;сейчас мы находимся на конце символа конца строки перед предыдущей
  1093.         inc hl
  1094. texted_prevlineq
  1095.         ex de,hl ;new addr (bits 13..0), bit 14 = overflow
  1096.         pop hl ;old addr
  1097.         pop af
  1098.         call pgaddr_to_ahl
  1099.         call iswrapon ;CY=on
  1100.         ret nc ;no wrap
  1101.  
  1102. findprevline_linewrap
  1103. ;ahl = в начале строки
  1104. ;перед ней может быть #0d или #0d,#0a
  1105. ;надо псевдонапечатать несколько псевдострок, пока не дойдём до текущей
  1106. findprevline_linewrap_count0
  1107.          push af
  1108.          push hl
  1109.         call texted_pseudoprline
  1110.         push hl
  1111. findprevline_old=$+1
  1112.         ld bc,0
  1113.         or a
  1114.         sbc hl,bc ;nc: hl>=old
  1115.         ld l,a
  1116. findprevline_oldHSB=$+1
  1117.         sbc a,0
  1118.         ld a,l
  1119.         pop hl
  1120.          pop de ;old addr
  1121.          pop bc ;b=old HSB
  1122.         jr c,findprevline_linewrap_count0
  1123.         ex de,hl
  1124.         ld a,b
  1125. ;ahl=на псевдострочку раньше
  1126.         ret ;nc
  1127.  
  1128. getmaxlinesize
  1129. ;ahl = addr
  1130. ;out: bc=max line size before eof, z=(bc==0) (keeps ahl,de)
  1131.          push de
  1132.         push af
  1133.         push hl
  1134.         ex de,hl
  1135.         ld c,a
  1136.         call getsize
  1137.         or a
  1138.         sbc hl,de
  1139.         sbc a,c
  1140.         ld bc,0
  1141.         jr c,getmaxlinesize_aftereof
  1142.         dec bc
  1143.         or a
  1144.         jr nz,$+4 ;filesize-addr >= 0x10000, will be 0xffff
  1145.          ld b,h
  1146.          ld c,l ;filesize-addr
  1147. getmaxlinesize_aftereof
  1148.         pop hl
  1149.         pop af
  1150.         ld e,a
  1151.         ld a,b
  1152.         or c
  1153.         ld a,e
  1154.          pop de
  1155.         ret
  1156.  
  1157. texted_prline_nextline
  1158.         push af
  1159.         push hl
  1160.         call texted_prline
  1161.         pop hl
  1162.         pop af
  1163. texted_nextline
  1164. ;if eof, returns CY and old addr (use call c,getsize) [eof addr]
  1165. ;ahl=addr
  1166. ;line < 16K
  1167. ;out: ahl, CY=error (keeps de)
  1168.         call iswrapon ;CY=on
  1169.         jp c,texted_pseudoprline
  1170.         call getmaxlinesize ;bc=max line size before eof, z=(bc==0)
  1171.          scf
  1172.         ret z
  1173.         push af
  1174.         push hl
  1175.         call ahl_to_pgaddr
  1176. ;bc=number of chars to go != 0
  1177. texted_nextline0
  1178.         ld a,(hl)
  1179.         inc hl
  1180.         dec bc
  1181.         cp 0x0d
  1182.         jr z,texted_nextline_cr
  1183.         cp 0x0a
  1184.         jr z,texted_nextline_lf
  1185.         ld a,b
  1186.         or c
  1187.         jr nz,texted_nextline0
  1188. ;texted_nextline_eof
  1189.         pop hl ;old addr
  1190.         pop af
  1191.         scf ;error
  1192.         ret
  1193. texted_nextlineq
  1194.         ex de,hl ;new addr (bits 13..0), bit 14 = overflow
  1195.         pop hl ;old addr
  1196.         pop af
  1197.         jp pgaddr_to_ahl
  1198. texted_nextline_cr
  1199.         ld a,b
  1200.         or c
  1201.         jr z,texted_nextlineq
  1202.         ld a,(hl)
  1203.         cp 0x0a
  1204.         jr nz,texted_nextline_lf
  1205.         inc hl
  1206. texted_nextline_lf
  1207.         jr texted_nextlineq
  1208.        
  1209. texted_prline
  1210. ;ahl=addr
  1211. ;check for last line (vs. file size)
  1212. ;if filesize-addr < 80, then b=filesize-addr, else b=80
  1213.         call getmaxlinesize ;bc=max line size before eof, z=(bc==0)
  1214.         jr z,texted_prlinespc_all;ret z
  1215.         call ahl_to_pgaddr
  1216. ;hl=#8000+0..#3fff
  1217. ;bc=max line size before eof != 0
  1218. texted_prline_shift=$+1
  1219.         ld de,0
  1220.         call istherecr_or_lf ;add hl,de
  1221.         ;jr nz,$+3
  1222.         ;dec hl ;hl=cr/lf address
  1223.  
  1224.         push hl
  1225.         ld h,b
  1226.         ld l,c
  1227.         or a
  1228.         sbc hl,de
  1229.         ld b,h
  1230.         ld c,l ;bc=bc-lineshift
  1231.         pop hl
  1232.         ;ld a,texted_WID
  1233.         jr c,texted_prlinespc_all
  1234.         jr z,texted_prlinespc_all ;maxlinesize<=lineshift
  1235.  
  1236.         push hl
  1237.         ld hl,texted_WID
  1238.         call minhl_bc_tobc
  1239.         ld b,c
  1240.         pop hl
  1241. ;b=number of chars to print != 0
  1242.         ld de,prlinebuf
  1243.         ld c,texted_WID
  1244. texted_prline0
  1245.         ld a,(hl)
  1246.         inc hl
  1247.         cp 0x0d
  1248.         jr z,texted_prline_cr
  1249.         cp 0x0a
  1250.         jr z,texted_prline_cr;lf
  1251.         ;push bc
  1252.         push hl
  1253.         ld h,twinto866/256
  1254.         ld l,a
  1255. texted_prline_recodepatch=$
  1256.         nop ;/ld a,(hl)
  1257.         ld (de),a ;PRCHAR_
  1258.         inc de
  1259.         pop hl
  1260.         ;pop bc
  1261.         dec c
  1262.         djnz texted_prline0
  1263.         ;call print_prlinebuf
  1264.         ;jr nz,texted_prline_lf
  1265.         ;ret
  1266. texted_prline_cr
  1267.         call print_prlinebuf
  1268. ;texted_prline_lf
  1269. ;допечатать пробелы до конца строки
  1270.         jr texted_prlinespc
  1271. texted_prlinespc_all
  1272.         ld c,texted_WID
  1273. texted_prlinespc
  1274.         push af
  1275.         push hl
  1276.         ;ld l,b
  1277.         ;ld h,0
  1278.         ;ld de,tspaces
  1279.         ;call sendchars
  1280.          ld a,c
  1281.          or a
  1282.         call nz,clearrestofline_crlf
  1283.         pop hl
  1284.         pop af
  1285.         ret
  1286.  
  1287. print_prlinebuf
  1288.         push af
  1289.         push bc
  1290.         push hl
  1291. ;c=texted_WID-число символов
  1292.         ld de,prlinebuf
  1293.         ld a,texted_WID
  1294.         sub c
  1295.         ld l,a
  1296.         ld h,0
  1297.         call sendchars
  1298.         pop hl
  1299.         pop bc
  1300.         pop af
  1301.         ret
  1302.  
  1303. prlinebuf
  1304.         ds texted_WID
  1305.  
  1306. texted_pseudoprline
  1307. ;ahl=addr
  1308. ;out: ahl, CY=error
  1309. ;check for last line (vs. file size)
  1310. ;if filesize-addr < 80, then b=filesize-addr, else b=80
  1311.         call getmaxlinesize ;bc=max line size before eof, z=(bc==0)
  1312.          scf
  1313.         ret z
  1314.         push af
  1315.         push hl
  1316.         call ahl_to_pgaddr
  1317. ;hl=#8000+0..#3fff
  1318. ;bc=max line size before eof != 0
  1319.         push hl
  1320.         ld hl,texted_WID
  1321.         call minhl_bc_tobc
  1322.         ld b,c
  1323.         pop hl
  1324. ;b=number of chars to print != 0
  1325.         ld c,texted_WID
  1326. texted_pseudoprline0
  1327.         ld a,(hl)
  1328.         inc hl
  1329.         cp 0x0d
  1330.         jr z,texted_pseudoprline_cr
  1331.         cp 0x0a
  1332.         jr z,texted_pseudoprline_lf
  1333.         dec c
  1334.         djnz texted_pseudoprline0
  1335.          ld a,(hl)
  1336.          inc hl
  1337.          cp 0x0d
  1338.          jr z,texted_pseudoprline_crok
  1339.          cp 0x0a
  1340.          jr z,texted_pseudoprline_lf
  1341.          dec hl
  1342.         jp texted_nextlineq
  1343. texted_pseudoprline_cr
  1344.         dec b
  1345.         jp z,texted_nextlineq
  1346. texted_pseudoprline_crok
  1347.         ld a,(hl)
  1348.         cp 0x0a
  1349.         jr nz,texted_pseudoprline_lf
  1350.         inc hl
  1351. texted_pseudoprline_lf
  1352.         jp texted_nextlineq
  1353.        
  1354. ;по идее это псевдопечать N экранных позиций (там могут быть цветовые коды и т.п.)
  1355. istherecr_or_lf
  1356. ;hl=addr ;TODO ahl
  1357. ;de=length to search
  1358. ;out: z=found, hl=at match or after area (TODO ahl)
  1359.         ld a,d
  1360.         or e
  1361.         jr z,istherecr_or_lf_fail
  1362.          push hl
  1363.         ld a,0x0d
  1364.         call istherecrlfgo
  1365.         jr z,istherecr_or_lf_popafZret;ret z
  1366.          pop hl
  1367.         ld a,0x0a
  1368. istherecrlfgo
  1369.         push bc
  1370.         ld b,d
  1371.         ld c,e
  1372.         cpir ;TODO несколько раз через все блоки, если заканчиваются блоки, то их переключать
  1373.         pop bc
  1374.         ret nz ;nz=not found
  1375.          dec hl
  1376.         ret ;z=found
  1377. istherecr_or_lf_fail
  1378.         dec a
  1379.         ret ;nz=not found
  1380.        
  1381. istherecr_or_lf_popafZret
  1382.         pop af
  1383.         cp a ;Z
  1384.         ret
  1385.        
  1386. ;texted_closefcb
  1387. ;        ld de,fcb
  1388. ;        OS_FCLOSE
  1389. ;        ret
  1390.  
  1391. setunchanged
  1392.         ld a,' '
  1393.         jr setchanged_a
  1394. setchanged
  1395.         ld a,'*'
  1396. setchanged_a
  1397.         ld (fchanged),a
  1398.         ret
  1399.  
  1400. iswrapon
  1401. ;CY = on
  1402. wrapflag=$
  1403.         or a ;/scf
  1404.         ret
  1405.  
  1406. curlineaddr
  1407.         ds 3
  1408. curlineaddrHSB=$-1
  1409.  
  1410. nlines
  1411.         dw 0
  1412.  
  1413. curxy
  1414. curx
  1415.         db 0
  1416. cury
  1417.         db 0
  1418.        
  1419.         align 256
  1420. twinto866
  1421.         incbin "../_sdk/codepage/winto866"
  1422.