File:  [gforth] / gforth / utf-8.fs
Revision 1.13: download - view: text, annotated - select for diffs
Wed Jan 5 22:13:40 2005 UTC (14 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added xchars (no tests, and no usage anywhere yet)

    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: 
   23: s" malformed UTF-8 character" exception Constant UTF-8-err
   24: 
   25: $80 Value max-single-byte
   26: 
   27: : u8len ( u8 -- n )
   28:     dup      max-single-byte u< IF  drop 1  EXIT  THEN \ special case ASCII
   29:     $800  2 >r
   30:     BEGIN  2dup u>=  WHILE  5 lshift r> 1+ >r  REPEAT
   31:     2drop r> ;
   32: 
   33: : u8@+ ( u8addr -- u8addr' u )
   34:     count  dup max-single-byte u< ?EXIT  \ special case ASCII
   35:     $7F and  $40 >r
   36:     BEGIN  dup r@ and  WHILE  r@ xor
   37: 	    6 lshift r> 5 lshift >r >r count
   38: 	    dup $C0 and $80 <> IF   UTF-8-err throw  THEN
   39: 	    $3F and r> or
   40:     REPEAT  rdrop ;
   41: 
   42: : u8!+ ( u u8addr -- u8addr' )
   43:     over max-single-byte u< IF  tuck c! 1+  EXIT  THEN \ special case ASCII
   44:     >r 0 swap  $3F
   45:     BEGIN  2dup u>  WHILE
   46: 	    2/ >r  dup $3F and $80 or swap 6 rshift r>
   47:     REPEAT  $7F xor 2* or  r>
   48:     BEGIN   over $80 u>= WHILE  tuck c! 1+  REPEAT  nip ;
   49: 
   50: \ plug-in so that char and '<char> work for UTF-8
   51: 
   52: [ifundef] char@ \ !! bootstrapping help
   53:     Defer char@ ( addr u -- char addr' u' )
   54:     :noname  over c@ -rot 1 /string ; IS char@
   55: [then]
   56: 
   57: :noname  ( addr u -- char addr' u' )
   58:     \ !! the if here seems to work around some breakage, but not
   59:     \ entirely; e.g., try '' with LANG=C.
   60:     dup 1 u<= IF defers char@ EXIT THEN
   61:     over + >r u8@+ swap r> over - ; IS char@
   62: 
   63: \ scan to next/previous character
   64: 
   65: \ alternative names: u8char+ u8char-
   66: 
   67: : u8>> ( u8addr -- u8addr' )  u8@+ drop ;
   68: : u8<< ( u8addr -- u8addr' )
   69:     BEGIN  1- dup c@ $C0 and max-single-byte <>  UNTIL ;
   70: 
   71: \ utf key and emit
   72: 
   73: : u8key ( -- u )
   74:     defers key dup max-single-byte u< ?EXIT  \ special case ASCII
   75:     $7F and  $40 >r
   76:     BEGIN  dup r@ and  WHILE  r@ xor
   77: 	    6 lshift r> 5 lshift >r >r defers key
   78: 	    dup $C0 and $80 <> IF  UTF-8-err throw  THEN
   79: 	    $3F and r> or
   80:     REPEAT  rdrop ;
   81: 
   82: : u8emit ( u -- )
   83:     dup max-single-byte u< IF  defers emit  EXIT  THEN \ special case ASCII
   84:     0 swap  $3F
   85:     BEGIN  2dup u>  WHILE
   86: 	    2/ >r  dup $3F and $80 or swap 6 rshift r>
   87:     REPEAT  $7F xor 2* or
   88:     BEGIN   dup $80 u>= WHILE  defers emit  REPEAT  drop ;
   89: 
   90: \ utf-8 stuff for xchars
   91: 
   92: : +u8/string ( c-addr1 u1 -- c-addr2 u2 )
   93:     over dup u8>> swap - /string ;
   94: 
   95: : -u8/string ( c-addr1 u1 -- c-addr2 u2 )
   96:     over dup u8<< swap - /string ;
   97: 
   98: : u8@ ( c-addr -- u )
   99:     u8@+ nip ;
  100: 
  101: : u8!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f )
  102:     >r over u8len r@ over u< if ( xc xc-addr1 len r: u1 )
  103: 	\ not enough space
  104: 	drop nip r> false
  105:     else
  106: 	>r u8!+ r> r> swap - true
  107:     then ;
  108: 
  109: : u8addrlen ( u8-addr -- u )
  110:     \ length of UTF-8 char starting at u8-addr (accesses only u8-addr)
  111:     c@
  112:     dup $80 u< if drop 1 exit endif
  113:     dup $c0 u< if UTF-8-err throw endif
  114:     dup $e0 u< if drop 2 exit endif
  115:     dup $f0 u< if drop 3 exit endif
  116:     dup $f8 u< if drop 4 exit endif
  117:     dup $fc u< if drop 5 exit endif
  118:     dup $fe u< if drop 6 exit endif
  119:     UTF-8-err throw ;
  120: 
  121: : -u8trailing-garbage ( addr u1 -- addr u2 )
  122:     2dup + dup u8<< ( addr u1 end1 end2 )
  123:     2dup dup u8addrlen + = if \ last character ok
  124: 	2drop
  125:     else
  126: 	nip nip over -
  127:     then ;
  128: 
  129: : set-encoding-utf-8 ( -- )
  130:     ['] u8emit is xemit
  131:     ['] u8key is xkey
  132:     ['] u8>> is xchar+
  133:     ['] u8<< is xchar-
  134:     ['] +u8/string is +x/string
  135:     ['] -u8/string is -x/string
  136:     ['] u8@ is xc@
  137:     ['] u8!+? is xc!+?
  138:     ['] u8@+ is xc@+
  139:     ['] u8len is xc-size
  140:     ['] -u8trailing-garbage is -trailing-garbage
  141: ;
  142: 
  143: \ input editor
  144: 
  145: [IFUNDEF] #esc  27 Constant #esc  [THEN]
  146: 
  147: : save-cursor ( -- )  #esc emit '7 emit ;
  148: : restore-cursor ( -- )  #esc emit '8 emit ;
  149: : .rest ( addr pos1 -- addr pos1 )
  150:     restore-cursor 2dup type ;
  151: : .all ( span addr pos1 -- span addr pos1 )
  152:     restore-cursor >r 2dup swap type r> ;
  153: 
  154: : <u8ins>  ( max span addr pos1 u8char -- max span addr pos2 )
  155:     >r  2over r@ u8len + u< IF  rdrop bell  EXIT  THEN
  156:     >string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop
  157:     r> u8len >r  rot r@ chars + -rot r> chars + ;
  158: : (u8ins)  ( max span addr pos1 u8char -- max span addr pos2 )
  159:     <u8ins> .all .rest ;
  160: : u8back  ( max span addr pos1 -- max span addr pos2 f )
  161:     dup  IF  over + u8<< over -  0 max .all .rest
  162:     ELSE  bell  THEN 0 ;
  163: : u8forw  ( max span addr pos1 -- max span addr pos2 f )
  164:     2 pick over <> IF  over + u8@+ u8emit over -  ELSE  bell  THEN 0 ;
  165: : (u8del)  ( max span addr pos1 -- max span addr pos2 )
  166:     over + dup u8<< tuck - >r over -
  167:     >string over r@ + -rot move
  168:     rot r> - -rot ;
  169: : ?u8del ( max span addr pos1 -- max span addr pos2 0 )
  170:   dup  IF  (u8del) .all 2 spaces .rest  THEN  0 ;
  171: : <u8del> ( max span addr pos1 -- max span addr pos2 0 )
  172:   2 pick over <>
  173:     IF  u8forw drop (u8del) .all 2 spaces .rest
  174:     ELSE  bell  THEN  0 ;
  175: : u8eof  2 pick over or 0=  IF  bye  ELSE  <u8del>  THEN ;
  176: 
  177: : u8first-pos  ( max span addr pos1 -- max span addr 0 0 )
  178:   drop 0 .all .rest 0 ;
  179: : u8end-pos  ( max span addr pos1 -- max span addr span 0 )
  180:   drop over .all 0 ;
  181: 
  182: 
  183: : u8clear-line ( max span addr pos1 -- max addr )
  184:     drop restore-cursor swap spaces restore-cursor ;
  185: : u8clear-tib ( max span addr pos -- max 0 addr 0 false )
  186:     u8clear-line 0 tuck dup ;
  187: 
  188: : (u8enter)  ( max span addr pos1 -- max span addr pos2 true )
  189:     >r end^ 2@ hist-setpos
  190:     2dup swap history write-line drop ( throw ) \ don't worry about errors
  191:     hist-pos 2dup backward^ 2! end^ 2!
  192:     r> .all space true ;
  193: 
  194: : u8kill-expand ( max span addr pos1 -- max span addr pos2 )
  195:     prefix-found cell+ @ ?dup IF  >r
  196: 	r@ - >string over r@ + -rot move
  197: 	rot r@ - -rot .all r> spaces .rest THEN ;
  198: 
  199: : insert   ( string length buffer size -- )
  200:     rot over min >r  r@ - ( left over )
  201:     over dup r@ +  rot move   r> move  ;
  202: 
  203: : u8tab-expand ( max span addr pos1 -- max span addr pos2 0 )
  204:     key? IF  #tab (u8ins) 0  EXIT  THEN
  205:     u8kill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
  206:     search-prefix tib-full?
  207:     IF    7 emit  2drop  prefix-off
  208:     ELSE  dup >r
  209: 	2>r >string r@ + 2r> 2swap insert
  210: 	r@ + rot r> + -rot
  211:     THEN
  212:     prefix-found @ IF  bl (u8ins)  ELSE  .all .rest  THEN  0 ;
  213: 
  214: : utf-8-io ( -- )
  215:     ['] u8forw       ctrl F bindkey
  216:     ['] u8back       ctrl B bindkey
  217:     ['] ?u8del       ctrl H bindkey
  218:     ['] u8eof        ctrl D bindkey
  219:     ['] <u8del>      ctrl X bindkey
  220:     ['] u8clear-tib  ctrl K bindkey
  221:     ['] u8first-pos  ctrl A bindkey
  222:     ['] u8end-pos    ctrl E bindkey
  223:     ['] (u8enter)    #lf    bindkey
  224:     ['] (u8enter)    #cr    bindkey
  225:     ['] u8tab-expand #tab   bindkey
  226:     ['] (u8ins)      IS insert-char
  227:     ['] kill-prefix  IS everychar
  228:     ['] save-cursor  IS everyline
  229:     ['] u8key        IS key
  230:     ['] u8emit       IS emit ;
  231: 

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