version 1.8, 2004/12/29 14:15:12
|
version 1.20, 2006/02/19 22:15:05
|
Line 1
|
Line 1
|
\ UTF-8 handling 12dec04py |
\ UTF-8 handling 12dec04py |
|
|
\ Copyright (C) 2004 Free Software Foundation, Inc. |
\ Copyright (C) 2004,2005 Free Software Foundation, Inc. |
|
|
\ This file is part of Gforth. |
\ This file is part of Gforth. |
|
|
Line 22
|
Line 22
|
|
|
s" malformed UTF-8 character" exception Constant UTF-8-err |
s" malformed UTF-8 character" exception Constant UTF-8-err |
|
|
|
$80 Value max-single-byte |
|
|
: u8len ( u8 -- n ) |
: u8len ( u8 -- n ) |
dup $80 u< IF drop 1 EXIT THEN \ special case ASCII |
dup max-single-byte u< IF drop 1 EXIT THEN \ special case ASCII |
$800 2 >r |
$800 2 >r |
BEGIN 2dup u>= WHILE 5 lshift r> 1+ >r REPEAT |
BEGIN 2dup u>= WHILE 5 lshift r> 1+ >r dup 0= UNTIL THEN |
2drop r> ; |
2drop r> ; |
|
|
: u8@+ ( u8addr -- u8addr' u ) |
: u8@+ ( u8addr -- u8addr' u ) |
count dup $80 and 0= ?EXIT \ special case ASCII |
count dup max-single-byte u< ?EXIT \ special case ASCII |
|
dup $C2 u< IF UTF-8-err throw THEN \ malformed character |
$7F and $40 >r |
$7F and $40 >r |
BEGIN dup r@ and WHILE r@ xor |
BEGIN dup r@ and WHILE r@ xor |
6 lshift r> 5 lshift >r >r count |
6 lshift r> 5 lshift >r >r count |
Line 38 s" malformed UTF-8 character" exception
|
Line 41 s" malformed UTF-8 character" exception
|
REPEAT rdrop ; |
REPEAT rdrop ; |
|
|
: u8!+ ( u u8addr -- u8addr' ) |
: u8!+ ( u u8addr -- u8addr' ) |
over $80 < IF tuck c! 1+ EXIT THEN \ special case ASCII |
over max-single-byte u< IF tuck c! 1+ EXIT THEN \ special case ASCII |
>r 0 swap $3F |
>r 0 swap $3F |
BEGIN 2dup u> WHILE |
BEGIN 2dup u> WHILE |
2/ >r dup $3F and $80 or swap 6 rshift r> |
2/ >r dup $3F and $80 or swap 6 rshift r> |
Line 47 s" malformed UTF-8 character" exception
|
Line 50 s" malformed UTF-8 character" exception
|
|
|
\ plug-in so that char and '<char> work for UTF-8 |
\ plug-in so that char and '<char> work for UTF-8 |
|
|
|
[ifundef] char@ \ !! bootstrapping help |
|
Defer char@ ( addr u -- char addr' u' ) |
|
:noname over c@ -rot 1 /string ; IS char@ |
|
[then] |
|
|
:noname ( addr u -- char addr' u' ) |
:noname ( addr u -- char addr' u' ) |
dup 1 u<= IF defers char@ EXIT THEN |
\ !! the if here seems to work around some breakage, but not |
|
\ entirely; e.g., try 'ç' with LANG=C. |
|
dup 1 u<= IF defers char@ EXIT THEN |
over + >r u8@+ swap r> over - ; IS char@ |
over + >r u8@+ swap r> over - ; IS char@ |
|
|
\ scan to next/previous character |
\ scan to next/previous character |
|
|
: u8>> ( u8addr -- u8addr' ) |
\ alternative names: u8char+ u8char- |
BEGIN count $C0 and $80 <> UNTIL ; |
|
|
: u8>> ( u8addr -- u8addr' ) u8@+ drop ; |
: u8<< ( u8addr -- u8addr' ) |
: u8<< ( u8addr -- u8addr' ) |
BEGIN 1- dup c@ $C0 and $80 <> UNTIL ; |
BEGIN 1- dup c@ $C0 and max-single-byte <> UNTIL ; |
|
|
\ utf key and emit |
\ utf key and emit |
|
|
: u8key ( -- u ) |
: u8key ( -- u ) |
defers key dup $80 and 0= ?EXIT \ special case ASCII |
defers key dup max-single-byte u< ?EXIT \ special case ASCII |
|
dup $C2 u< IF UTF-8-err throw THEN \ malformed character |
$7F and $40 >r |
$7F and $40 >r |
BEGIN dup r@ and WHILE r@ xor |
BEGIN dup r@ and WHILE r@ xor |
6 lshift r> 5 lshift >r >r defers key |
6 lshift r> 5 lshift >r >r defers key |
Line 70 s" malformed UTF-8 character" exception
|
Line 82 s" malformed UTF-8 character" exception
|
REPEAT rdrop ; |
REPEAT rdrop ; |
|
|
: u8emit ( u -- ) |
: u8emit ( u -- ) |
dup $80 < IF defers emit EXIT THEN \ special case ASCII |
dup max-single-byte u< IF defers emit EXIT THEN \ special case ASCII |
0 swap $3F |
0 swap $3F |
BEGIN 2dup u> WHILE |
BEGIN 2dup u> WHILE |
2/ >r dup $3F and $80 or swap 6 rshift r> |
2/ >r dup $3F and $80 or swap 6 rshift r> |
REPEAT $7F xor 2* or |
REPEAT $7F xor 2* or |
BEGIN dup $80 u>= WHILE defers emit REPEAT drop ; |
BEGIN dup $80 u>= WHILE defers emit REPEAT drop ; |
|
|
\ input editor |
\ utf-8 stuff for xchars |
|
|
|
: +u8/string ( c-addr1 u1 -- c-addr2 u2 ) |
|
over dup u8>> swap - /string ; |
|
|
|
: -u8/string ( c-addr1 u1 -- c-addr2 u2 ) |
|
over dup u8<< swap - /string ; |
|
|
[IFUNDEF] #esc 27 Constant #esc [THEN] |
: u8@ ( c-addr -- u ) |
|
u8@+ nip ; |
|
|
|
: u8!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f ) |
|
>r over u8len r@ over u< if ( xc xc-addr1 len r: u1 ) |
|
\ not enough space |
|
drop nip r> false |
|
else |
|
>r u8!+ r> r> swap - true |
|
then ; |
|
|
|
: u8addrlen ( u8-addr -- u ) |
|
\ length of UTF-8 char starting at u8-addr (accesses only u8-addr) |
|
c@ |
|
dup $80 u< if drop 1 exit endif |
|
dup $c0 u< if UTF-8-err throw endif |
|
dup $e0 u< if drop 2 exit endif |
|
dup $f0 u< if drop 3 exit endif |
|
dup $f8 u< if drop 4 exit endif |
|
dup $fc u< if drop 5 exit endif |
|
dup $fe u< if drop 6 exit endif |
|
UTF-8-err throw ; |
|
|
|
: -u8trailing-garbage ( addr u1 -- addr u2 ) |
|
2dup + dup u8<< ( addr u1 end1 end2 ) |
|
2dup dup u8addrlen + = if \ last character ok |
|
2drop |
|
else |
|
nip nip over - |
|
then ; |
|
|
|
: u8width ( xcaddr u -- n ) |
|
0 rot rot over + swap ?DO |
|
I xc@+ swap >r wcwidth + |
|
r> I - +LOOP ; |
|
|
|
: set-encoding-utf-8 ( -- ) |
|
['] u8emit is xemit |
|
['] u8key is xkey |
|
['] u8>> is xchar+ |
|
['] u8<< is xchar- |
|
['] +u8/string is +x/string |
|
['] -u8/string is -x/string |
|
['] u8@ is xc@ |
|
['] u8!+? is xc!+? |
|
['] u8@+ is xc@+ |
|
['] u8len is xc-size |
|
[ [IFDEF] x-width ] |
|
['] u8width is x-width |
|
[ [THEN] ] |
|
['] -u8trailing-garbage is -trailing-garbage |
|
; |
|
|
|
: utf-8-cold ( -- ) |
|
s" LC_ALL" getenv 2dup d0= IF 2drop |
|
s" LC_CTYPE" getenv 2dup d0= IF 2drop |
|
s" LANG" getenv 2dup d0= IF 2drop |
|
s" C" THEN THEN THEN |
|
s" UTF-8" search nip nip |
|
IF set-encoding-utf-8 ELSE set-encoding-fixed-width THEN ; |
|
|
: save-cursor ( -- ) #esc emit '7 emit ; |
' utf-8-cold INIT8 chained |
: restore-cursor ( -- ) #esc emit '8 emit ; |
|
: .rest ( addr pos1 -- addr pos1 ) |
|
restore-cursor 2dup type ; |
|
: .all ( span addr pos1 -- span addr pos1 ) |
|
restore-cursor >r 2dup swap type r> ; |
|
|
|
: <u8ins> ( max span addr pos1 u8char -- max span addr pos2 ) |
|
>r 2over r@ u8len + u< IF rdrop bell EXIT THEN |
|
>string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop |
|
r> u8len >r rot r@ chars + -rot r> chars + ; |
|
: (u8ins) ( max span addr pos1 u8char -- max span addr pos2 ) |
|
<u8ins> .all .rest ; |
|
: u8back ( max span addr pos1 -- max span addr pos2 f ) |
|
dup IF over + u8<< over - 0 max .all .rest |
|
ELSE bell THEN 0 ; |
|
: u8forw ( max span addr pos1 -- max span addr pos2 f ) |
|
2 pick over <> IF over + u8@+ u8emit over - ELSE bell THEN 0 ; |
|
: (u8del) ( max span addr pos1 -- max span addr pos2 ) |
|
over + dup u8<< tuck - >r over - |
|
>string over r@ + -rot move |
|
rot r> - -rot ; |
|
: ?u8del ( max span addr pos1 -- max span addr pos2 0 ) |
|
dup IF (u8del) .all 2 spaces .rest THEN 0 ; |
|
: <u8del> ( max span addr pos1 -- max span addr pos2 0 ) |
|
2 pick over <> |
|
IF u8forw drop (u8del) .all 2 spaces .rest |
|
ELSE bell THEN 0 ; |
|
: u8eof 2 pick over or 0= IF bye ELSE <u8del> THEN ; |
|
|
|
: u8first-pos ( max span addr pos1 -- max span addr 0 0 ) |
|
drop 0 .all .rest 0 ; |
|
: u8end-pos ( max span addr pos1 -- max span addr span 0 ) |
|
drop over .all 0 ; |
|
|
|
|
|
: u8clear-line ( max span addr pos1 -- max addr ) |
|
drop restore-cursor swap spaces restore-cursor ; |
|
: u8clear-tib ( max span addr pos -- max 0 addr 0 false ) |
|
u8clear-line 0 tuck dup ; |
|
|
|
: (u8enter) ( max span addr pos1 -- max span addr pos2 true ) |
|
>r end^ 2@ hist-setpos |
|
2dup swap history write-line drop ( throw ) \ don't worry about errors |
|
hist-pos 2dup backward^ 2! end^ 2! |
|
r> .all space true ; |
|
|
|
: u8kill-expand ( max span addr pos1 -- max span addr pos2 ) |
|
prefix-found cell+ @ ?dup IF >r |
|
r@ - >string over r@ + -rot move |
|
rot r@ - -rot .all r> spaces .rest THEN ; |
|
|
|
: insert ( string length buffer size -- ) |
|
rot over min >r r@ - ( left over ) |
|
over dup r@ + rot move r> move ; |
|
|
|
: u8tab-expand ( max span addr pos1 -- max span addr pos2 0 ) |
|
key? IF #tab (u8ins) 0 EXIT THEN |
|
u8kill-expand 2dup extract-word dup 0= IF nip EXIT THEN |
|
search-prefix tib-full? |
|
IF 7 emit 2drop prefix-off |
|
ELSE dup >r |
|
2>r >string r@ + 2r> 2swap insert |
|
r@ + rot r> + -rot |
|
THEN |
|
prefix-found @ IF bl (u8ins) ELSE .all .rest THEN 0 ; |
|
|
|
: utf-8-io ( -- ) |
|
['] u8forw ctrl F bindkey |
|
['] u8back ctrl B bindkey |
|
['] ?u8del ctrl H bindkey |
|
['] u8eof ctrl D bindkey |
|
['] <u8del> ctrl X bindkey |
|
['] u8clear-tib ctrl K bindkey |
|
['] u8first-pos ctrl A bindkey |
|
['] u8end-pos ctrl E bindkey |
|
['] (u8enter) #lf bindkey |
|
['] (u8enter) #cr bindkey |
|
['] u8tab-expand #tab bindkey |
|
['] (u8ins) IS insert-char |
|
['] kill-prefix IS everychar |
|
['] save-cursor IS everyline |
|
['] u8key IS key |
|
['] u8emit IS emit ; |
|
|
|
|
utf-8-cold |