[gforth] / gforth / ekey.fs  

gforth: gforth/ekey.fs


1 : anton 1.1 \ ekey etc.
2 :    
3 : anton 1.26 \ Copyright (C) 1999,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
4 : anton 1.1
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 : anton 1.19 \ as published by the Free Software Foundation, either version 3
10 : anton 1.1 \ 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 : anton 1.19 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.1
20 :    
21 :     \ this implementation of EKEY just translates VT100/ANSI escape
22 :     \ sequences to ekeys.
23 :    
24 :     \ Caveats: It also translates the sequences if they were not generated
25 :     \ by pressing the key; moreover, it waits until a complete sequence or
26 :     \ an invalid prefix to a sequence has arrived before reporting true in
27 :     \ EKEY? and before completing EKEY. One way to fix this would be to
28 :     \ use timeouts when waiting for the next key; however, this may lead
29 :     \ to situations in slow networks where single events result in several
30 :     \ EKEYs, which appears less desirable to me.
31 :    
32 :     \ The keycode names are compatible with pfe-0.9.14
33 :    
34 : anton 1.20 $80000000 constant keycode-start
35 :     $80000016 constant keycode-limit
36 :    
37 :     create keycode-table keycode-limit keycode-start - cells allot
38 :    
39 : anton 1.17 : keycode ( u1 "name" -- u2 ; name execution: -- u )
40 : anton 1.20 dup keycode-limit keycode-start within -11 and throw
41 :     dup constant
42 :     dup latest keycode-table rot keycode-start - th !
43 :     1+ ;
44 : anton 1.1
45 : anton 1.12 \ most of the keys are also in pfe, except:
46 :     \ k-insert, k-delete, k11, k12, s-k11, s-k12
47 :    
48 : anton 1.17 $40000000 constant k-shift-mask ( -- u ) \ X:ekeys
49 :     $20000000 constant k-ctrl-mask ( -- u ) \ X:ekeys
50 :     $10000000 constant k-alt-mask ( -- u ) \ X:ekeys
51 :    
52 : anton 1.21 : simple-fkey-string ( u1 -- c-addr u ) \ gforth
53 :     \G @i{c-addr u} is the string name of the function key @i{u1}.
54 :     \G Only works for simple function keys without modifier masks.
55 :     \G Any @i{u1} that does not correspond to a simple function key
56 :     \G currently produces an exception.
57 : anton 1.20 dup keycode-limit keycode-start within -24 and throw
58 :     keycode-table swap keycode-start - th @ name>string ;
59 :    
60 : anton 1.21 : fkey. ( u -- ) \ gforth fkey-dot
61 :     \G Print a string representation for the function key @i{u}.
62 :     \G @i{U} must be a function key (possibly with modifier masks),
63 :     \G otherwise there may be an exception.
64 : anton 1.20 dup [ k-shift-mask k-ctrl-mask k-alt-mask or or invert ] literal and
65 :     simple-fkey-string type
66 :     dup k-shift-mask and if ." k-shift-mask or" then
67 :     dup k-ctrl-mask and if ." k-ctrl-mask or" then
68 :     k-alt-mask and if ." k-alt-mask or" then ;
69 :    
70 :     keycode-start
71 : anton 1.17 keycode k-left ( -- u ) \ X:ekeys
72 :     keycode k-right ( -- u ) \ X:ekeys
73 :     keycode k-up ( -- u ) \ X:ekeys
74 :     keycode k-down ( -- u ) \ X:ekeys
75 :     keycode k-home ( -- u ) \ X:ekeys
76 : anton 1.12 \G aka Pos1
77 : anton 1.17 keycode k-end ( -- u ) \ X:ekeys
78 :     keycode k-prior ( -- u ) \ X:ekeys
79 : anton 1.12 \G aka PgUp
80 : anton 1.17 keycode k-next ( -- u ) \ X:ekeys
81 : anton 1.12 \G aka PgDn
82 : anton 1.17 keycode k-insert ( -- u ) \ X:ekeys
83 :     keycode k-delete ( -- u ) \ X:ekeys
84 :     \ the DEL key on my xterm, not backspace
85 : anton 1.12
86 : anton 1.1 \ function/keypad keys
87 : anton 1.17 keycode k-f1 ( -- u ) \ X:ekeys
88 :     keycode k-f2 ( -- u ) \ X:ekeys
89 :     keycode k-f3 ( -- u ) \ X:ekeys
90 :     keycode k-f4 ( -- u ) \ X:ekeys
91 :     keycode k-f5 ( -- u ) \ X:ekeys
92 :     keycode k-f6 ( -- u ) \ X:ekeys
93 :     keycode k-f7 ( -- u ) \ X:ekeys
94 :     keycode k-f8 ( -- u ) \ X:ekeys
95 :     keycode k-f9 ( -- u ) \ X:ekeys
96 :     keycode k-f10 ( -- u ) \ X:ekeys
97 :     keycode k-f11 ( -- u ) \ X:ekeys
98 :     keycode k-f12 ( -- u ) \ X:ekeys
99 :     drop
100 :    
101 :     ' k-f1 alias k1 ( -- u ) \ gforth-obsolete
102 :     ' k-f2 alias k2 ( -- u ) \ gforth-obsolete
103 :     ' k-f3 alias k3 ( -- u ) \ gforth-obsolete
104 :     ' k-f4 alias k4 ( -- u ) \ gforth-obsolete
105 :     ' k-f5 alias k5 ( -- u ) \ gforth-obsolete
106 :     ' k-f6 alias k6 ( -- u ) \ gforth-obsolete
107 :     ' k-f7 alias k7 ( -- u ) \ gforth-obsolete
108 :     ' k-f8 alias k8 ( -- u ) \ gforth-obsolete
109 :     ' k-f9 alias k9 ( -- u ) \ gforth-obsolete
110 :     ' k-f10 alias k10 ( -- u ) \ gforth-obsolete
111 :     ' k-f11 alias k11 ( -- u ) \ gforth-obsolete
112 :     ' k-f12 alias k12 ( -- u ) \ gforth-obsolete
113 : anton 1.5 \ shifted fuinction keys (don't work in xterm (same as unshifted, but
114 :     \ s-k1..s-k8 work in the Linux console)
115 : anton 1.17 k-f1 k-shift-mask or constant s-k1 ( -- u ) \ gforth-obsolete
116 :     k-f2 k-shift-mask or constant s-k2 ( -- u ) \ gforth-obsolete
117 :     k-f3 k-shift-mask or constant s-k3 ( -- u ) \ gforth-obsolete
118 :     k-f4 k-shift-mask or constant s-k4 ( -- u ) \ gforth-obsolete
119 :     k-f5 k-shift-mask or constant s-k5 ( -- u ) \ gforth-obsolete
120 :     k-f6 k-shift-mask or constant s-k6 ( -- u ) \ gforth-obsolete
121 :     k-f7 k-shift-mask or constant s-k7 ( -- u ) \ gforth-obsolete
122 :     k-f8 k-shift-mask or constant s-k8 ( -- u ) \ gforth-obsolete
123 :     k-f9 k-shift-mask or constant s-k9 ( -- u ) \ gforth-obsolete
124 :     k-f10 k-shift-mask or constant s-k10 ( -- u ) \ gforth-obsolete
125 :     k-f11 k-shift-mask or constant s-k11 ( -- u ) \ gforth-obsolete
126 :     k-f12 k-shift-mask or constant s-k12 ( -- u ) \ gforth-obsolete
127 : anton 1.1
128 :     \ helper word
129 :     \ print a key sequence:
130 : anton 1.17 0 [IF]
131 :     : key-sequence ( -- )
132 :     key begin
133 :     cr dup . emit
134 :     key? while
135 :     key
136 :     repeat ;
137 :    
138 :     : key-sequences ( -- )
139 :     begin
140 :     key-sequence cr
141 :     again ;
142 :     [THEN]
143 : anton 1.1
144 :     create key-buffer 8 chars allot
145 :     2variable key-buffered key-buffer 0 key-buffered 2!
146 :    
147 :     : char-append-buffer ( c addr -- )
148 :     tuck 2@ chars + c!
149 :     dup 2@ 1+ rot 2! ;
150 :    
151 :     :noname ( -- c )
152 :     \ buffered key
153 :     key-buffered 2@ dup if
154 : anton 1.17 1- 2dup key-buffered 2!
155 :     chars + c@
156 : anton 1.1 else
157 : anton 1.17 2drop defers key
158 : anton 1.1 then ;
159 :     is key
160 :    
161 :     : unkey ( c -- )
162 :     key-buffered char-append-buffer ;
163 :    
164 :     : unkeys ( addr u -- )
165 :     -1 swap 1- -do
166 : anton 1.17 dup i chars + c@ unkey
167 :     1 -loop
168 : anton 1.1 drop ;
169 :    
170 :     :noname ( -- flag )
171 :     key-buffered 2@ nip 0<> defers key? or ;
172 :     is key?
173 :    
174 :     table constant esc-sequences \ and prefixes
175 :    
176 :     create ekey-buffer 8 chars allot
177 :     2variable ekey-buffered
178 : pazsan 1.10 [IFUNDEF] #esc 27 Constant #esc [THEN]
179 : anton 1.1
180 :     : esc-prefix ( -- u )
181 : anton 1.6 key? if
182 : anton 1.17 key ekey-buffered char-append-buffer
183 :     ekey-buffered 2@ esc-sequences search-wordlist
184 :     if
185 :     execute exit
186 :     endif
187 : anton 1.6 endif
188 :     ekey-buffered 2@ unkeys #esc ;
189 : anton 1.1
190 : anton 1.17 : esc-sequence ( u1 addr u -- ; name execution: -- u2 ) recursive
191 :     \ define escape sequence addr u (=name) to have value u1; if u1=0,
192 :     \ addr u is a prefix of some other sequence (with key code u2);
193 :     \ also, define all prefixes of addr u if necessary.
194 : anton 1.1 2dup 1- dup
195 :     if
196 : anton 1.17 2dup esc-sequences search-wordlist
197 :     if
198 :     drop 2drop
199 :     else
200 :     0 -rot esc-sequence \ define the prefixes
201 :     then
202 :     else
203 :     2drop
204 :     then ( u1 addr u )
205 :     nextname dup if ( u1 )
206 :     constant \ full sequence for a key
207 : anton 1.1 else
208 : anton 1.17 drop ['] esc-prefix alias
209 :     endif ;
210 : anton 1.1
211 : crook 1.2 \ nac02dec1999 exclude the escape sequences if we are using crossdoc.fs to generate
212 :     \ a documentation file. Do this because key sequences [ and OR here clash with
213 :     \ standard names and so prevent them appearing in the documentation.
214 :     [IFUNDEF] put-doc-entry
215 : anton 1.1 get-current esc-sequences set-current
216 :    
217 :     \ esc sequences (derived by using key-sequence in an xterm)
218 : anton 1.17 k-left s" [D" esc-sequence
219 :     k-right s" [C" esc-sequence
220 :     k-up s" [A" esc-sequence
221 :     k-down s" [B" esc-sequence
222 :     k-home s" [H" esc-sequence
223 :     k-end s" [F" esc-sequence
224 :     k-prior s" [5~" esc-sequence
225 :     k-next s" [6~" esc-sequence
226 :     k-insert s" [2~" esc-sequence
227 :     k-delete s" [3~" esc-sequence
228 :    
229 :     k-left k-shift-mask or s" [1;2D" esc-sequence
230 :     k-right k-shift-mask or s" [1;2C" esc-sequence
231 :     k-up k-shift-mask or s" [1;2A" esc-sequence
232 :     k-down k-shift-mask or s" [1;2B" esc-sequence
233 :     k-home k-shift-mask or s" [1;2H" esc-sequence
234 :     k-end k-shift-mask or s" [1;2F" esc-sequence
235 :     k-prior k-shift-mask or s" [5;2~" esc-sequence
236 :     k-next k-shift-mask or s" [6;2~" esc-sequence
237 :     k-insert k-shift-mask or s" [2;2~" esc-sequence
238 :     k-delete k-shift-mask or s" [3;2~" esc-sequence
239 :    
240 :     k-left k-ctrl-mask or s" [1;5D" esc-sequence
241 :     k-right k-ctrl-mask or s" [1;5C" esc-sequence
242 :     k-up k-ctrl-mask or s" [1;5A" esc-sequence
243 :     k-down k-ctrl-mask or s" [1;5B" esc-sequence
244 :     k-home k-ctrl-mask or s" [1;5H" esc-sequence
245 :     k-end k-ctrl-mask or s" [1;5F" esc-sequence
246 :     k-prior k-ctrl-mask or s" [5;5~" esc-sequence
247 :     k-next k-ctrl-mask or s" [6;5~" esc-sequence
248 :     k-insert k-ctrl-mask or s" [2;5~" esc-sequence
249 :     k-delete k-ctrl-mask or s" [3;5~" esc-sequence
250 :    
251 :     k-left k-alt-mask or s" [1;3D" esc-sequence
252 :     k-right k-alt-mask or s" [1;3C" esc-sequence
253 :     k-up k-alt-mask or s" [1;3A" esc-sequence
254 :     k-down k-alt-mask or s" [1;3B" esc-sequence
255 :     k-home k-alt-mask or s" [1;3H" esc-sequence
256 :     k-end k-alt-mask or s" [1;3F" esc-sequence
257 :     k-prior k-alt-mask or s" [5;3~" esc-sequence
258 :     k-next k-alt-mask or s" [6;3~" esc-sequence
259 :     k-insert k-alt-mask or s" [2;3~" esc-sequence
260 :     k-delete k-alt-mask or s" [3;3~" esc-sequence
261 :    
262 :     k1 s" OP" esc-sequence
263 :     k2 s" OQ" esc-sequence
264 :     k3 s" OR" esc-sequence
265 :     k4 s" OS" esc-sequence
266 :     k5 s" [15~" esc-sequence
267 :     k6 s" [17~" esc-sequence
268 :     k7 s" [18~" esc-sequence
269 :     k8 s" [19~" esc-sequence
270 :     k9 s" [20~" esc-sequence
271 :     k10 s" [21~" esc-sequence
272 :     k11 s" [23~" esc-sequence
273 :     k12 s" [24~" esc-sequence
274 :    
275 :     s-k1 s" [1;2P" esc-sequence
276 :     s-k2 s" [1;2Q" esc-sequence
277 :     s-k3 s" [1;2R" esc-sequence
278 :     s-k4 s" [1;2S" esc-sequence
279 :     s-k5 s" [15;2~" esc-sequence
280 :     s-k6 s" [17;2~" esc-sequence
281 :     s-k7 s" [18;2~" esc-sequence
282 :     s-k8 s" [19;2~" esc-sequence
283 :     s-k9 s" [20;2~" esc-sequence
284 :     s-k10 s" [21;2~" esc-sequence
285 :     s-k11 s" [23;2~" esc-sequence
286 :     s-k12 s" [24;2~" esc-sequence
287 :    
288 :     k-f1 k-ctrl-mask or s" [1;5P" esc-sequence
289 :     k-f2 k-ctrl-mask or s" [1;5Q" esc-sequence
290 :     k-f3 k-ctrl-mask or s" [1;5R" esc-sequence
291 :     k-f4 k-ctrl-mask or s" [1;5S" esc-sequence
292 :     k-f5 k-ctrl-mask or s" [15;5~" esc-sequence
293 :     k-f6 k-ctrl-mask or s" [17;5~" esc-sequence
294 :     k-f7 k-ctrl-mask or s" [18;5~" esc-sequence
295 :     k-f8 k-ctrl-mask or s" [19;5~" esc-sequence
296 :     k-f9 k-ctrl-mask or s" [20;5~" esc-sequence
297 :     k-f10 k-ctrl-mask or s" [21;5~" esc-sequence
298 :     k-f11 k-ctrl-mask or s" [23;5~" esc-sequence
299 :     k-f12 k-ctrl-mask or s" [24;5~" esc-sequence
300 :    
301 :     k-f1 k-alt-mask or s" [1;3P" esc-sequence
302 :     k-f2 k-alt-mask or s" [1;3Q" esc-sequence
303 :     k-f3 k-alt-mask or s" [1;3R" esc-sequence
304 :     k-f4 k-alt-mask or s" [1;3S" esc-sequence
305 :     k-f5 k-alt-mask or s" [15;3~" esc-sequence
306 :     k-f6 k-alt-mask or s" [17;3~" esc-sequence
307 :     k-f7 k-alt-mask or s" [18;3~" esc-sequence
308 :     k-f8 k-alt-mask or s" [19;3~" esc-sequence
309 :     k-f9 k-alt-mask or s" [20;3~" esc-sequence
310 :     k-f10 k-alt-mask or s" [21;3~" esc-sequence
311 :     k-f11 k-alt-mask or s" [23;3~" esc-sequence
312 :     k-f12 k-alt-mask or s" [24;3~" esc-sequence
313 : anton 1.4
314 :     \ esc sequences from Linux console:
315 :    
316 : anton 1.17 k1 s" [[A" esc-sequence
317 :     k2 s" [[B" esc-sequence
318 :     k3 s" [[C" esc-sequence
319 :     k4 s" [[D" esc-sequence
320 :     k5 s" [[E" esc-sequence
321 :     \ k-delete s" [3~" esc-sequence \ duplicate from above
322 :     k-home s" [1~" esc-sequence
323 :     k-end s" [4~" esc-sequence
324 :    
325 :     s-k1 s" [25~" esc-sequence
326 :     s-k2 s" [26~" esc-sequence
327 :     s-k3 s" [28~" esc-sequence
328 :     s-k4 s" [29~" esc-sequence
329 :     s-k5 s" [31~" esc-sequence
330 :     s-k6 s" [32~" esc-sequence
331 :     s-k7 s" [33~" esc-sequence
332 :     s-k8 s" [34~" esc-sequence
333 : anton 1.1
334 : anton 1.25 \ esc sequences for MacOS X iterm <e7a7c785-3bea-408b-94e9-4b59b008546f@x16g2000prn.googlegroups.com>
335 :     k-left s" OD" esc-sequence
336 :     k-right s" OC" esc-sequence
337 :     k-up s" OA" esc-sequence
338 :     k-down s" OB" esc-sequence
339 :    
340 : anton 1.1 set-current
341 : crook 1.2 [ENDIF]
342 : anton 1.1
343 :     : clear-ekey-buffer ( -- )
344 : anton 1.12 ekey-buffer 0 ekey-buffered 2! ;
345 : anton 1.1
346 : pazsan 1.23 [IFDEF] max-single-byte
347 :     : read-xkey ( key -- flag )
348 :     clear-ekey-buffer
349 :     ekey-buffered char-append-buffer
350 :     ekey-buffer 1 u8addrlen 1 +do
351 :     key? 0= ?leave
352 :     key ekey-buffered char-append-buffer
353 :     loop
354 :     ekey-buffer 1 u8addrlen ekey-buffered @ = ;
355 :     : get-xkey ( u -- xc )
356 :     dup max-single-byte u>= if
357 :     read-xkey if
358 :     ekey-buffer xc@+ nip else
359 :     ekey-buffered 2@ unkeys key then
360 :     then ;
361 :     : xkey? ( -- flag )
362 :     key? dup if
363 :     drop key read-xkey ekey-buffered 2@ unkeys
364 :     clear-ekey-buffer then ;
365 :     [THEN]
366 :    
367 : crook 1.2 : ekey ( -- u ) \ facility-ext e-key
368 : anton 1.12 \G Receive a keyboard event @var{u} (encoding implementation-defined).
369 : anton 1.1 key dup #esc =
370 :     if
371 : anton 1.17 drop clear-ekey-buffer
372 : pazsan 1.23 esc-prefix exit
373 : pazsan 1.22 then
374 :     [IFDEF] max-single-byte
375 : pazsan 1.23 get-xkey
376 : pazsan 1.22 [THEN]
377 :     ;
378 : anton 1.1
379 : pazsan 1.22 [IFDEF] max-single-byte
380 :     : ekey>char ( u -- u false | c true ) \ facility-ext e-key-to-char
381 :     \G Convert keyboard event @var{u} into character @code{c} if possible.
382 :     dup max-single-byte u< ; \ k-left must be first!
383 :     : ekey>xchar ( u -- u false | xc true ) \ xchar-ext e-key-to-xchar
384 :     \G Convert keyboard event @var{u} into xchar @code{xc} if possible.
385 :     dup k-left u< ; \ k-left must be first!
386 :     : ekey>fkey ( u1 -- u2 f ) \ X:ekeys
387 :     \G If u1 is a keyboard event in the special key set, convert
388 :     \G keyboard event @var{u1} into key id @var{u2} and return true;
389 :     \G otherwise return @var{u1} and false.
390 :     ekey>xchar 0= ;
391 : pazsan 1.24
392 :     ' xkey? alias ekey? ( -- flag ) \ facility-ext e-key-question
393 : pazsan 1.22 [ELSE]
394 : crook 1.2 : ekey>char ( u -- u false | c true ) \ facility-ext e-key-to-char
395 : anton 1.12 \G Convert keyboard event @var{u} into character @code{c} if possible.
396 : pazsan 1.15 dup k-left u< ; \ k-left must be first!
397 : anton 1.17 : ekey>fkey ( u1 -- u2 f ) \ X:ekeys
398 :     \G If u1 is a keyboard event in the special key set, convert
399 :     \G keyboard event @var{u1} into key id @var{u2} and return true;
400 :     \G otherwise return @var{u1} and false.
401 :     ekey>char 0= ;
402 : pazsan 1.24
403 :     ' key? alias ekey? ( -- flag ) \ facility-ext e-key-question
404 : pazsan 1.22 [THEN]
405 : anton 1.17
406 : anton 1.14 \G True if a keyboard event is available.
407 : anton 1.1
408 : anton 1.7 \ : esc? ( -- flag ) recursive
409 :     \ key? 0=
410 :     \ if
411 : anton 1.17 \ false exit
412 : anton 1.7 \ then
413 :     \ key ekey-buffered char-append-buffer
414 :     \ ekey-buffered 2@ esc-sequences search-wordlist
415 :     \ if
416 : anton 1.17 \ ['] esc-prefix =
417 :     \ if
418 :     \ esc? exit
419 :     \ then
420 : anton 1.7 \ then
421 :     \ true ;
422 :    
423 :     \ : ekey? ( -- flag ) \ facility-ext e-key-question
424 :     \ \G Return @code{true} if a keyboard event is available (use
425 :     \ \G @code{ekey} to receive the event), @code{false} otherwise.
426 :     \ key?
427 :     \ if
428 : anton 1.17 \ key dup #esc =
429 :     \ if
430 :     \ clear-ekey-buffer esc?
431 :     \ ekey-buffered 2@ unkeys
432 :     \ else
433 :     \ true
434 :     \ then
435 :     \ swap unkey
436 : anton 1.7 \ else
437 : anton 1.17 \ false
438 : anton 1.7 \ then ;
439 : anton 1.1
440 : anton 1.17 0 [if]
441 :     : test-ekey?
442 :     begin
443 :     begin
444 :     begin
445 :     key? until
446 :     ekey? until
447 :     .s ekey .s drop
448 :     again ;
449 : anton 1.1 \ test-ekey?
450 : anton 1.17 [then]

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help