Annotation of gforth/utf-8.fs, revision 1.5

1.1       pazsan      1: \ UTF-8 handling                                       12dec04py
                      2: 
                      3: \ Copyright (C) 2004 Free Software Foundation, Inc.
                      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
                     20: 
                     21: \ short: u8 means utf-8 encoded address
                     22: 
1.4       pazsan     23: : u8len ( u8 -- n )
1.1       pazsan     24:     dup      $80 u< IF  drop 1  EXIT  THEN \ special case ASCII
                     25:     $800  2 >r
                     26:     BEGIN  2dup u>=  WHILE  5 lshift r> 1+ >r  REPEAT
                     27:     2drop r> ;
                     28: 
                     29: : u8@+ ( u8addr -- u8addr' u )
                     30:     count  dup $80 and 0= ?EXIT  \ special case ASCII
                     31:     $7F and  $40 >r
                     32:     BEGIN  dup r@ and  WHILE  r@ xor
                     33:            6 lshift r> 5 lshift >r >r count
                     34: \          dup $C0 and $80 <> abort" malformed character"
                     35:            $3F and r> or
                     36:     REPEAT  rdrop ;
                     37: 
                     38: : u8!+ ( u u8addr -- u8addr' )
                     39:     over $80 < IF  tuck c! 1+  EXIT  THEN \ special case ASCII
                     40:     >r 0 swap  $3F
                     41:     BEGIN  2dup u>  WHILE
                     42:            2/ >r  dup $3F and $80 or swap 6 rshift r>
                     43:     REPEAT  $7F xor 2* or  r>
                     44:     BEGIN   over $80 u>= WHILE  tuck c! 1+  REPEAT  nip ;
                     45: 
                     46: \ scan to next/previous character
                     47: 
                     48: : u8>> ( u8addr -- u8addr' )
                     49:     BEGIN  count $C0 and $80 <>  UNTIL ;
                     50: : u8<< ( u8addr -- u8addr' )
                     51:     BEGIN  1- dup c@ $C0 and $80 <>  UNTIL ;
                     52: 
                     53: \ utf key and emit
                     54: 
                     55: : u8key ( -- u )
                     56:     defers key dup $80 and 0= ?EXIT  \ special case ASCII
                     57:     $7F and  $40 >r
                     58:     BEGIN  dup r@ and  WHILE  r@ xor
                     59:            6 lshift r> 5 lshift >r >r defers key
                     60: \          dup $C0 and $80 <> abort" malformed character"
                     61:            $3F and r> or
                     62:     REPEAT  rdrop ;
                     63: 
                     64: : u8emit ( u -- )
                     65:     dup $80 < IF  defers emit  EXIT  THEN \ special case ASCII
                     66:     0 swap  $3F
                     67:     BEGIN  2dup u>  WHILE
                     68:            2/ >r  dup $3F and $80 or swap 6 rshift r>
                     69:     REPEAT  $7F xor 2* or
                     70:     BEGIN   dup $80 u>= WHILE  defers emit  REPEAT  drop ;
                     71: 
                     72: \ input editor
                     73: 
                     74: : save-cursor ( -- )  27 emit '7 emit ;
                     75: : restore-cursor ( -- )  27 emit '8 emit ;
                     76: : .rest ( addr pos1 -- addr pos1 )
                     77:     restore-cursor 2dup type ;
                     78: : .all ( span addr pos1 -- span addr pos1 )
                     79:     restore-cursor >r 2dup swap type r> ;
                     80: 
1.3       pazsan     81: : <u8ins>  ( max span addr pos1 u8char -- max span addr pos2 )
                     82:     >r >string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop
                     83:     r> u8len >r  rot r@ chars + -rot r> chars + ;
1.1       pazsan     84: : (u8ins)  ( max span addr pos1 u8char -- max span addr pos2 )
1.3       pazsan     85:     <u8ins> .all .rest ;
1.1       pazsan     86: : u8back  ( max span addr pos1 -- max span addr pos2 f )
1.2       pazsan     87:     dup  IF  over + u8<< over -  0 max .all .rest
                     88:     ELSE  #bell emit  THEN 0 ;
1.1       pazsan     89: : u8forw  ( max span addr pos1 -- max span addr pos2 f )
                     90:     2 pick over <> IF  over + u8@+ u8emit over -  ELSE  #bell emit  THEN 0 ;
                     91: : (u8del)  ( max span addr pos1 -- max span addr pos2 )
                     92:     over + dup u8<< tuck - >r over -
                     93:     >string over r@ + -rot move
1.3       pazsan     94:     rot r> - -rot ;
1.1       pazsan     95: : ?u8del ( max span addr pos1 -- max span addr pos2 0 )
1.3       pazsan     96:   dup  IF  (u8del) .all 2 spaces .rest  THEN  0 ;
1.1       pazsan     97: : <u8del> ( max span addr pos1 -- max span addr pos2 0 )
                     98:   2 pick over <>
1.3       pazsan     99:     IF  u8forw drop (u8del) .all 2 spaces .rest
                    100:     ELSE  #bell emit  THEN  0 ;
1.1       pazsan    101: : u8eof  2 pick over or 0=  IF  bye  ELSE  <u8del>  THEN ;
                    102: 
1.3       pazsan    103: : u8first-pos  ( max span addr pos1 -- max span addr 0 0 )
                    104:   drop 0 .all .rest 0 ;
                    105: : u8end-pos  ( max span addr pos1 -- max span addr span 0 )
                    106:   drop over .all 0 ;
                    107: 
                    108: 
                    109: : u8clear-line ( max span addr pos1 -- max addr )
                    110:     drop restore-cursor swap spaces restore-cursor ;
                    111: : u8clear-tib ( max span addr pos -- max 0 addr 0 false )
                    112:     u8clear-line 0 tuck dup ;
                    113: 
                    114: : (u8enter)  ( max span addr pos1 -- max span addr pos2 true )
                    115:     >r end^ 2@ hist-setpos
                    116:     2dup swap history write-line drop ( throw ) \ don't worry about errors
                    117:     hist-pos 2dup backward^ 2! end^ 2!
                    118:     r> .all space true ;
                    119: 
                    120: : u8kill-expand ( max span addr pos1 -- max span addr pos2 )
                    121:     prefix-found cell+ @ ?dup IF  >r
                    122:        r@ - >string over r@ + -rot move
                    123:        rot r@ - -rot .all r> spaces .rest THEN ;
                    124: 
                    125: : insert   ( string length buffer size -- )
                    126:     rot over min >r  r@ - ( left over )
                    127:     over dup r@ +  rot move   r> move  ;
                    128: 
                    129: : u8tab-expand ( max span addr pos1 -- max span addr pos2 0 )
                    130:     key? IF  #tab (u8ins) 0  EXIT  THEN
                    131:     u8kill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
1.5     ! pazsan    132:     search-prefix tib-full?
        !           133:     IF    7 emit  2drop  prefix-off
1.3       pazsan    134:     ELSE  dup >r
                    135:        2>r >string r@ + 2r> 2swap insert
                    136:        r@ + rot r> + -rot
                    137:     THEN
1.5     ! pazsan    138:     prefix-found @ IF  bl (u8ins)  ELSE  .all .rest  THEN  0 ;
1.3       pazsan    139: 
1.4       pazsan    140: : utf-8-io ( -- )
                    141:     ['] u8forw       ctrl F bindkey
                    142:     ['] u8back       ctrl B bindkey
                    143:     ['] ?u8del       ctrl H bindkey
                    144:     ['] u8eof        ctrl D bindkey
                    145:     ['] <u8del>      ctrl X bindkey
                    146:     ['] u8clear-tib  ctrl K bindkey
                    147:     ['] u8first-pos  ctrl A bindkey
                    148:     ['] u8end-pos    ctrl E bindkey
                    149:     ['] (u8enter)    #lf    bindkey
                    150:     ['] (u8enter)    #cr    bindkey
                    151:     ['] u8tab-expand #tab   bindkey
                    152:     ['] (u8ins)      IS insert-char
1.5     ! pazsan    153:     ['] kill-prefix  IS everychar
1.4       pazsan    154:     ['] save-cursor  IS everyline
                    155:     ['] u8key        IS key
                    156:     ['] u8emit       IS emit ;
                    157: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>