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: : u8len ( u8 -- n )
26: dup $80 u< IF drop 1 EXIT THEN \ special case ASCII
27: $800 2 >r
28: BEGIN 2dup u>= WHILE 5 lshift r> 1+ >r REPEAT
29: 2drop r> ;
30:
31: : u8@+ ( u8addr -- u8addr' u )
32: count dup $80 and 0= ?EXIT \ special case ASCII
33: $7F and $40 >r
34: BEGIN dup r@ and WHILE r@ xor
35: 6 lshift r> 5 lshift >r >r count
36: dup $C0 and $80 <> IF UTF-8-err throw THEN
37: $3F and r> or
38: REPEAT rdrop ;
39:
40: : u8!+ ( u u8addr -- u8addr' )
41: over $80 < IF tuck c! 1+ EXIT THEN \ special case ASCII
42: >r 0 swap $3F
43: BEGIN 2dup u> WHILE
44: 2/ >r dup $3F and $80 or swap 6 rshift r>
45: REPEAT $7F xor 2* or r>
46: BEGIN over $80 u>= WHILE tuck c! 1+ REPEAT nip ;
47:
48: \ plug-in so that char and '<char> work for UTF-8
49:
50: :noname ( addr u -- char addr' u' )
51: dup 1 u<= IF defers char@ EXIT THEN
52: over + >r u8@+ swap r> over - ; IS char@
53:
54: \ scan to next/previous character
55:
56: : u8>> ( u8addr -- u8addr' )
57: BEGIN count $C0 and $80 <> UNTIL ;
58: : u8<< ( u8addr -- u8addr' )
59: BEGIN 1- dup c@ $C0 and $80 <> UNTIL ;
60:
61: \ utf key and emit
62:
63: : u8key ( -- u )
64: defers key dup $80 and 0= ?EXIT \ special case ASCII
65: $7F and $40 >r
66: BEGIN dup r@ and WHILE r@ xor
67: 6 lshift r> 5 lshift >r >r defers key
68: dup $C0 and $80 <> IF UTF-8-err throw THEN
69: $3F and r> or
70: REPEAT rdrop ;
71:
72: : u8emit ( u -- )
73: dup $80 < IF defers emit EXIT THEN \ special case ASCII
74: 0 swap $3F
75: BEGIN 2dup u> WHILE
76: 2/ >r dup $3F and $80 or swap 6 rshift r>
77: REPEAT $7F xor 2* or
78: BEGIN dup $80 u>= WHILE defers emit REPEAT drop ;
79:
80: \ input editor
81:
82: [IFUNDEF] #esc 27 Constant #esc [THEN]
83:
84: : save-cursor ( -- ) #esc emit '7 emit ;
85: : restore-cursor ( -- ) #esc emit '8 emit ;
86: : .rest ( addr pos1 -- addr pos1 )
87: restore-cursor 2dup type ;
88: : .all ( span addr pos1 -- span addr pos1 )
89: restore-cursor >r 2dup swap type r> ;
90:
91: : <u8ins> ( max span addr pos1 u8char -- max span addr pos2 )
92: >r 2over r@ u8len + u< IF rdrop bell EXIT THEN
93: >string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop
94: r> u8len >r rot r@ chars + -rot r> chars + ;
95: : (u8ins) ( max span addr pos1 u8char -- max span addr pos2 )
96: <u8ins> .all .rest ;
97: : u8back ( max span addr pos1 -- max span addr pos2 f )
98: dup IF over + u8<< over - 0 max .all .rest
99: ELSE bell THEN 0 ;
100: : u8forw ( max span addr pos1 -- max span addr pos2 f )
101: 2 pick over <> IF over + u8@+ u8emit over - ELSE bell THEN 0 ;
102: : (u8del) ( max span addr pos1 -- max span addr pos2 )
103: over + dup u8<< tuck - >r over -
104: >string over r@ + -rot move
105: rot r> - -rot ;
106: : ?u8del ( max span addr pos1 -- max span addr pos2 0 )
107: dup IF (u8del) .all 2 spaces .rest THEN 0 ;
108: : <u8del> ( max span addr pos1 -- max span addr pos2 0 )
109: 2 pick over <>
110: IF u8forw drop (u8del) .all 2 spaces .rest
111: ELSE bell THEN 0 ;
112: : u8eof 2 pick over or 0= IF bye ELSE <u8del> THEN ;
113:
114: : u8first-pos ( max span addr pos1 -- max span addr 0 0 )
115: drop 0 .all .rest 0 ;
116: : u8end-pos ( max span addr pos1 -- max span addr span 0 )
117: drop over .all 0 ;
118:
119:
120: : u8clear-line ( max span addr pos1 -- max addr )
121: drop restore-cursor swap spaces restore-cursor ;
122: : u8clear-tib ( max span addr pos -- max 0 addr 0 false )
123: u8clear-line 0 tuck dup ;
124:
125: : (u8enter) ( max span addr pos1 -- max span addr pos2 true )
126: >r end^ 2@ hist-setpos
127: 2dup swap history write-line drop ( throw ) \ don't worry about errors
128: hist-pos 2dup backward^ 2! end^ 2!
129: r> .all space true ;
130:
131: : u8kill-expand ( max span addr pos1 -- max span addr pos2 )
132: prefix-found cell+ @ ?dup IF >r
133: r@ - >string over r@ + -rot move
134: rot r@ - -rot .all r> spaces .rest THEN ;
135:
136: : insert ( string length buffer size -- )
137: rot over min >r r@ - ( left over )
138: over dup r@ + rot move r> move ;
139:
140: : u8tab-expand ( max span addr pos1 -- max span addr pos2 0 )
141: key? IF #tab (u8ins) 0 EXIT THEN
142: u8kill-expand 2dup extract-word dup 0= IF nip EXIT THEN
143: search-prefix tib-full?
144: IF 7 emit 2drop prefix-off
145: ELSE dup >r
146: 2>r >string r@ + 2r> 2swap insert
147: r@ + rot r> + -rot
148: THEN
149: prefix-found @ IF bl (u8ins) ELSE .all .rest THEN 0 ;
150:
151: : utf-8-io ( -- )
152: ['] u8forw ctrl F bindkey
153: ['] u8back ctrl B bindkey
154: ['] ?u8del ctrl H bindkey
155: ['] u8eof ctrl D bindkey
156: ['] <u8del> ctrl X bindkey
157: ['] u8clear-tib ctrl K bindkey
158: ['] u8first-pos ctrl A bindkey
159: ['] u8end-pos ctrl E bindkey
160: ['] (u8enter) #lf bindkey
161: ['] (u8enter) #cr bindkey
162: ['] u8tab-expand #tab bindkey
163: ['] (u8ins) IS insert-char
164: ['] kill-prefix IS everychar
165: ['] save-cursor IS everyline
166: ['] u8key IS key
167: ['] u8emit IS emit ;
168:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>