--- gforth/utf-8.fs 2004/12/27 20:05:53 1.5 +++ gforth/utf-8.fs 2004/12/29 14:15:12 1.8 @@ -20,6 +20,8 @@ \ short: u8 means utf-8 encoded address +s" malformed UTF-8 character" exception Constant UTF-8-err + : u8len ( u8 -- n ) dup $80 u< IF drop 1 EXIT THEN \ special case ASCII $800 2 >r @@ -31,7 +33,7 @@ $7F and $40 >r BEGIN dup r@ and WHILE r@ xor 6 lshift r> 5 lshift >r >r count -\ dup $C0 and $80 <> abort" malformed character" + dup $C0 and $80 <> IF UTF-8-err throw THEN $3F and r> or REPEAT rdrop ; @@ -43,6 +45,12 @@ REPEAT $7F xor 2* or r> BEGIN over $80 u>= WHILE tuck c! 1+ REPEAT nip ; +\ plug-in so that char and ' work for UTF-8 + +:noname ( addr u -- char addr' u' ) + dup 1 u<= IF defers char@ EXIT THEN + over + >r u8@+ swap r> over - ; IS char@ + \ scan to next/previous character : u8>> ( u8addr -- u8addr' ) @@ -57,7 +65,7 @@ $7F and $40 >r BEGIN dup r@ and WHILE r@ xor 6 lshift r> 5 lshift >r >r defers key -\ dup $C0 and $80 <> abort" malformed character" + dup $C0 and $80 <> IF UTF-8-err throw THEN $3F and r> or REPEAT rdrop ; @@ -71,23 +79,26 @@ \ input editor -: save-cursor ( -- ) 27 emit '7 emit ; -: restore-cursor ( -- ) 27 emit '8 emit ; +[IFUNDEF] #esc 27 Constant #esc [THEN] + +: save-cursor ( -- ) #esc emit '7 emit ; +: 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> ; : ( max span addr pos1 u8char -- max span addr pos2 ) - >r >string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop + >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 ) .all .rest ; : u8back ( max span addr pos1 -- max span addr pos2 f ) dup IF over + u8<< over - 0 max .all .rest - ELSE #bell emit THEN 0 ; + ELSE bell THEN 0 ; : u8forw ( max span addr pos1 -- max span addr pos2 f ) - 2 pick over <> IF over + u8@+ u8emit over - ELSE #bell emit THEN 0 ; + 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 @@ -97,7 +108,7 @@ : ( max span addr pos1 -- max span addr pos2 0 ) 2 pick over <> IF u8forw drop (u8del) .all 2 spaces .rest - ELSE #bell emit THEN 0 ; + ELSE bell THEN 0 ; : u8eof 2 pick over or 0= IF bye ELSE THEN ; : u8first-pos ( max span addr pos1 -- max span addr 0 0 )