]> code.delx.au - gnu-emacs/blob - lisp/emulation/edt-mapper.el
da85e1b07359658460981262c26b9e46c156be95
[gnu-emacs] / lisp / emulation / edt-mapper.el
1 ;;; edt-mapper.el --- create an EDT LK-201 map file for X-Windows Emacs
2
3 ;; Copyright (C) 1994, 1995, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Kevin Gallagher <Kevin.Gallagher@boeing.com>
7 ;; Maintainer: Kevin Gallagher <Kevin.Gallagher@boeing.com>
8 ;; Keywords: emulations
9 ;; Package: edt
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28
29 ;; [Part of the GNU Emacs EDT Emulation.]
30
31 ;; This emacs lisp program can be used to create an emacs lisp file
32 ;; that defines the mapping of the user's keyboard to the LK-201
33 ;; keyboard function keys and keypad keys (around which EDT has been
34 ;; designed). Please read the "Usage" AND "Known Problems" sections
35 ;; below before attempting to run this program. (The design of this
36 ;; file, edt-mapper.el, was heavily influenced by tpu-mapper.el.)
37
38 ;; Version 4.0 contains the following enhancements:
39
40 ;; 1. If you access a workstation using an X Server, note that the
41 ;; initialization file generated by edt-mapper.el will now
42 ;; contain the name of the X Server vendor. This is a
43 ;; convenience for those who have access to their Unix account
44 ;; from more than one type of X Server. Since different X
45 ;; Servers typically require different EDT emulation
46 ;; initialization files, edt-mapper.el will now generate these
47 ;; different initialization files and save them with different
48 ;; names.
49
50 ;; 2. Also, edt-mapper.el is now capable of binding an ASCII key
51 ;; sequence, providing the ASCII key sequence prefix is already
52 ;; known by Emacs to be a prefix. As a result, some
53 ;; terminal/keyboard/window system configurations, which don't
54 ;; have a complete set of sensible function key map bindings, can
55 ;; still be configured for EDT Emulation.
56
57
58 ;; Usage:
59
60 ;; Simply load this file into emacs (version 19 or higher)
61 ;; using the following command.
62
63 ;; emacs -q -l edt-mapper.el
64
65 ;; The "-q" option prevents loading of your .emacs file (commands
66 ;; therein might confuse this program).
67
68 ;; An instruction screen showing the typical LK-201 terminal
69 ;; functions keys will be displayed, and you will be prompted to
70 ;; press the keys on your keyboard which you want to emulate the
71 ;; corresponding LK-201 keys.
72
73 ;; Finally, you will be prompted for the name of the file to store
74 ;; the key definitions. If you chose the default, it will be found
75 ;; and loaded automatically when the EDT emulation is started. If
76 ;; you specify a different file name, you will need to set the
77 ;; variable "edt-keys-file" before starting the EDT emulation.
78 ;; Here's how you might go about doing that in your .emacs file.
79
80 ;; (setq edt-keys-file (expand-file-name "~/.my-emacs-keys"))
81
82
83 ;; Known Problems:
84
85 ;; Sometimes, edt-mapper will ignore a key you press, and just
86 ;; continue to prompt for the same key. This can happen when your
87 ;; window manager sucks up the key and doesn't pass it on to emacs,
88 ;; or it could be an emacs bug. Either way, there's nothing that
89 ;; edt-mapper can do about it. You must press RETURN, to skip the
90 ;; current key and continue. Later, you and/or your local Emacs guru
91 ;; can try to figure out why the key is being ignored.
92
93 ;;; History:
94 ;;
95
96 ;; Version 4.0 2000 Added 2 New Features
97
98 ;;; Code:
99
100 ;;;
101 ;;; Decide Emacs Variant, GNU Emacs or XEmacs (aka Lucid Emacs).
102 ;;; Determine Window System, and X Server Vendor (if appropriate).
103 ;;;
104 (defconst edt-window-system (if (featurep 'xemacs) (console-type) window-system)
105 "Indicates window system \(in GNU Emacs\) or console type \(in XEmacs\).")
106
107 (declare-function x-server-vendor "xfns.c" (&optional terminal))
108
109 (defconst edt-xserver (when (eq edt-window-system 'x)
110 ;; The Cygwin window manager has a `/' in its
111 ;; name, which breaks the generated file name of
112 ;; the custom key map file. Replace `/' with a
113 ;; `-' to work around that.
114 (if (featurep 'xemacs)
115 (replace-in-string (x-server-vendor) "[ /]" "-")
116 (replace-regexp-in-string "[ /]" "-"
117 (x-server-vendor))))
118 "Indicates X server vendor name, if applicable.")
119
120
121 ;;;
122 ;;; Key variables
123 ;;;
124 (defvar edt-key nil)
125 (defvar edt-enter nil)
126 (defvar edt-return nil)
127 (defvar edt-key-seq nil)
128 (defvar edt-enter-seq nil)
129 (defvar edt-return-seq nil)
130 (defvar edt-term nil)
131
132 ;; To silence the byte-compiler
133 (defvar EDT-key-name)
134 (defvar edt-save-function-key-map)
135
136 ;;;
137 ;;; Determine Terminal Type (if appropriate).
138 ;;;
139
140 (if (and edt-window-system (not (eq edt-window-system 'tty)))
141 (setq edt-term nil)
142 (setq edt-term (getenv "TERM")))
143
144 ;;;
145 ;;; Implements a workaround for a feature that was added to simple.el.
146 ;;;
147 ;;; Many function keys have no Emacs functions assigned to them by
148 ;;; default. A subset of these are typically assigned functions in the
149 ;;; EDT emulation. This includes all the keypad keys and a some others
150 ;;; like Delete.
151 ;;;
152 ;;; Logic in simple.el maps some of these unassigned function keys to
153 ;;; ordinary typing keys. Where this is the case, a call to
154 ;;; read-key-sequence, below, does not return the name of the function
155 ;;; key pressd by the user but, instead, it returns the name of the
156 ;;; key to which it has been mapped. It needs to know the name of the
157 ;;; key pressed by the user. As a workaround, we assign a function to
158 ;;; each of the unassigned function keys of interest, here. These
159 ;;; assignments override the mapping to other keys and are only
160 ;;; temporary since, when edt-mapper is finished executing, it causes
161 ;;; Emacs to exit.
162 ;;;
163
164 (mapc
165 (lambda (function-key)
166 (if (not (lookup-key (current-global-map) function-key))
167 (define-key (current-global-map) function-key 'forward-char)))
168 '([kp-0] [kp-1] [kp-2] [kp-3] [kp-4]
169 [kp-5] [kp-6] [kp-7] [kp-8] [kp-9]
170 [kp-space]
171 [kp-tab]
172 [kp-enter]
173 [kp-multiply]
174 [kp-add]
175 [kp-separator]
176 [kp-subtract]
177 [kp-decimal]
178 [kp-divide]
179 [kp-equal]
180 [backspace]
181 [delete]
182 [tab]
183 [linefeed]
184 [clear]))
185
186 ;;;
187 ;;; Make sure the window is big enough to display the instructions,
188 ;;; except where window cannot be re-sized.
189 ;;;
190
191 (if (and edt-window-system (not (eq edt-window-system 'tty)))
192 (set-frame-size (selected-frame) 80 36))
193
194 ;;;
195 ;;; Create buffers - Directions and Keys
196 ;;;
197 (if (not (get-buffer "Directions")) (generate-new-buffer "Directions"))
198 (if (not (get-buffer "Keys")) (generate-new-buffer "Keys"))
199
200 ;;;
201 ;;; Put header in the Keys buffer
202 ;;;
203 (set-buffer "Keys")
204 (insert "\
205 ;;
206 ;; Key definitions for the EDT emulation within GNU Emacs
207 ;;
208
209 (defconst *EDT-keys*
210 '(
211 ")
212
213 ;;;
214 ;;; Display directions
215 ;;;
216 (switch-to-buffer "Directions")
217 (if (and edt-window-system (not (eq edt-window-system 'tty)))
218 (insert "
219 EDT MAPPER
220
221 You will be asked to press keys to create a custom mapping (under a
222 Window Manager) of your keypad keys and function keys so that they can
223 emulate the LK-201 keypad and function keys or the subset of keys found
224 on a VT-100 series terminal keyboard. (The LK-201 keyboard is the
225 standard keyboard attached to VT-200 series terminals, and above.)
226
227 Sometimes, edt-mapper will ignore a key you press, and just continue to
228 prompt for the same key. This can happen when your window manager sucks
229 up the key and doesn't pass it on to Emacs, or it could be an Emacs bug.
230 Either way, there's nothing that edt-mapper can do about it. You must
231 press RETURN, to skip the current key and continue. Later, you and/or
232 your local system guru can try to figure out why the key is being ignored.
233
234 Start by pressing the RETURN key, and continue by pressing the keys
235 specified in the mini-buffer. If you want to entirely omit a key,
236 because your keyboard does not have a corresponding key, for example,
237 just press RETURN at the prompt.
238
239 ")
240 (insert "
241 EDT MAPPER
242
243 You will be asked to press keys to create a custom mapping of your
244 keypad keys and function keys so that they can emulate the LK-201
245 keypad and function keys or the subset of keys found on a VT-100
246 series terminal keyboard. (The LK-201 keyboard is the standard
247 keyboard attached to VT-200 series terminals, and above.)
248
249 If you are using a real LK-201 keyboard, you should map the keys
250 exactly as they are on the keyboard.
251
252 Start by pressing the RETURN key, and continue by pressing the keys
253 specified in the mini-buffer. If you want to entirely omit a key,
254 because your keyboard does not have a corresponding key, for example,
255 just press RETURN at the prompt.
256
257 "))
258
259 (delete-other-windows)
260
261 ;;;
262 ;;; Save <CR> for future reference.
263 ;;;
264 ;;; For GNU Emacs, running in a Window System, first hide bindings in
265 ;;; function-key-map.
266 ;;;
267 (cond
268 ((featurep 'xemacs)
269 (setq edt-return-seq (read-key-sequence "Hit carriage-return <CR> to continue "))
270 (setq edt-return (concat "[" (format "%s" (event-key (aref edt-return-seq 0))) "]")))
271 (t
272 (if edt-window-system
273 (progn
274 (setq edt-save-function-key-map function-key-map)
275 (setq function-key-map (make-sparse-keymap))))
276 (setq edt-return (read-key-sequence "Hit carriage-return <CR> to continue "))))
277
278 ;;;
279 ;;; Remove prefix-key bindings to F1 and F2 in global-map so they can be
280 ;;; bound in the EDT Emulation mode.
281 ;;;
282 (global-unset-key [f1])
283 (global-unset-key [f2])
284
285 ;;;
286 ;;; Display Keypad Diagram and Begin Prompting for Keys
287 ;;;
288 (set-buffer "Directions")
289 (delete-region (point-min) (point-max))
290 (if (and edt-window-system (not (eq edt-window-system 'tty)))
291 (insert "
292
293 PRESS THE KEY SPECIFIED IN THE MINIBUFFER BELOW.
294
295 Here's a picture of the standard LK-201 keypad for reference:
296
297 _______________________ _______________________________
298 | HELP | DO | | F17 | F18 | F19 | F20 |
299 | | | | | | | |
300 |_______|_______________| |_______|_______|_______|_______|
301 _______________________ _______________________________
302 | FIND |INSERT |REMOVE | | PF1 | PF2 | PF3 | PF4 |
303 | | | | | | | | |
304 |_______|_______|_______| |_______|_______|_______|_______|
305 |SELECT |PREVIOU| NEXT | | KP7 | KP8 | KP9 | KP- |
306 | | | | | | | | |
307 |_______|_______|_______| |_______|_______|_______|_______|
308 | UP | | KP4 | KP5 | KP6 | KP, |
309 | | | | | | |
310 _______|_______|_______ |_______|_______|_______|_______|
311 | LEFT | DOWN | RIGHT | | KP1 | KP2 | KP3 | |
312 | | | | | | | | |
313 |_______|_______|_______| |_______|_______|_______| KPE |
314 | KP0 | KPP | |
315 | | | |
316 |_______________|_______|_______|
317
318 REMEMBER: JUST PRESS RETURN TO SKIP MAPPING A KEY.
319
320 ")
321 (progn
322 (insert "
323 GENERATING A CUSTOM CONFIGURATION FILE FOR TERMINAL TYPE: ")
324 (insert (format "%s." edt-term))
325 (insert "
326
327 PRESS THE KEY SPECIFIED IN THE MINIBUFFER BELOW.
328
329 _______________________ _______________________________
330 | HELP | DO | | F17 | F18 | F19 | F20 |
331 |_______|_______________| |_______|_______|_______|_______|
332 _______________________ _______________________________
333 | FIND |INSERT |REMOVE | | PF1 | PF2 | PF3 | PF4 |
334 |_______|_______|_______| |_______|_______|_______|_______|
335 |SELECT |PREVIOU| NEXT | | KP7 | KP8 | KP9 | KP- |
336 |_______|_______|_______| |_______|_______|_______|_______|
337 | UP | | KP4 | KP5 | KP6 | KP, |
338 _______|_______|_______ |_______|_______|_______|_______|
339 | LEFT | DOWN | RIGHT | | KP1 | KP2 | KP3 | |
340 |_______|_______|_______| |_______|_______|_______| KPE |
341 | KP0 | KPP | |
342 |_______________|_______|_______|
343
344 REMEMBER: JUST PRESS RETURN TO SKIP MAPPING A KEY.")))
345
346
347 ;;;
348 ;;; Key mapping functions
349 ;;;
350 (defun edt-map-key (ident descrip)
351 (interactive)
352 (if (featurep 'xemacs)
353 (progn
354 (setq edt-key-seq (read-key-sequence (format "Press %s%s: " ident descrip)))
355 (setq edt-key (concat "[" (format "%s" (event-key (aref edt-key-seq 0))) "]"))
356 (cond ((not (equal edt-key edt-return))
357 (set-buffer "Keys")
358 (insert (format " (\"%s\" . %s)\n" ident edt-key))
359 (set-buffer "Directions"))
360 ;; bogosity to get next prompt to come up, if the user hits <CR>!
361 ;; check periodically to see if this is still needed...
362 (t
363 (set-buffer "Keys")
364 (insert (format " (\"%s\" . \"\" )\n" ident))
365 (set-buffer "Directions"))))
366 (setq edt-key (read-key-sequence (format "Press %s%s: " ident descrip)))
367 (cond ((not (equal edt-key edt-return))
368 (set-buffer "Keys")
369 (insert (if (vectorp edt-key)
370 (format " (\"%s\" . %s)\n" ident edt-key)
371 (format " (\"%s\" . \"%s\")\n" ident edt-key)))
372 (set-buffer "Directions"))
373 ;; bogosity to get next prompt to come up, if the user hits <CR>!
374 ;; check periodically to see if this is still needed...
375 (t
376 (set-buffer "Keys")
377 (insert (format " (\"%s\" . \"\" )\n" ident))
378 (set-buffer "Directions"))))
379 edt-key)
380
381 (set-buffer "Keys")
382 (insert "
383 ;;
384 ;; Arrows
385 ;;
386 ")
387 (set-buffer "Directions")
388
389 (edt-map-key "UP" " - The Up Arrow Key")
390 (edt-map-key "DOWN" " - The Down Arrow Key")
391 (edt-map-key "LEFT" " - The Left Arrow Key")
392 (edt-map-key "RIGHT" " - The Right Arrow Key")
393
394
395 (set-buffer "Keys")
396 (insert "
397 ;;
398 ;; PF keys
399 ;;
400 ")
401 (set-buffer "Directions")
402
403 (edt-map-key "PF1" " - The PF1 (GOLD) Key")
404 (edt-map-key "PF2" " - The Keypad PF2 Key")
405 (edt-map-key "PF3" " - The Keypad PF3 Key")
406 (edt-map-key "PF4" " - The Keypad PF4 Key")
407
408 (set-buffer "Keys")
409 (insert "
410 ;;
411 ;; KP0-9 KP- KP, KPP and KPE
412 ;;
413 ")
414 (set-buffer "Directions")
415
416 (edt-map-key "KP0" " - The Keypad 0 Key")
417 (edt-map-key "KP1" " - The Keypad 1 Key")
418 (edt-map-key "KP2" " - The Keypad 2 Key")
419 (edt-map-key "KP3" " - The Keypad 3 Key")
420 (edt-map-key "KP4" " - The Keypad 4 Key")
421 (edt-map-key "KP5" " - The Keypad 5 Key")
422 (edt-map-key "KP6" " - The Keypad 6 Key")
423 (edt-map-key "KP7" " - The Keypad 7 Key")
424 (edt-map-key "KP8" " - The Keypad 8 Key")
425 (edt-map-key "KP9" " - The Keypad 9 Key")
426 (edt-map-key "KP-" " - The Keypad - Key")
427 (edt-map-key "KP," " - The Keypad , Key")
428 (edt-map-key "KPP" " - The Keypad . Key")
429 (edt-map-key "KPE" " - The Keypad Enter Key")
430 ;; Save the enter key
431 (setq edt-enter edt-key)
432 (setq edt-enter-seq edt-key-seq)
433
434
435 (set-buffer "Keys")
436 (insert "
437 ;;
438 ;; Editing keypad (FIND, INSERT, REMOVE)
439 ;; (SELECT, PREVIOUS, NEXT)
440 ;;
441 ")
442 (set-buffer "Directions")
443
444 (edt-map-key "FIND" " - The Find key on the editing keypad")
445 (edt-map-key "INSERT" " - The Insert key on the editing keypad")
446 (edt-map-key "REMOVE" " - The Remove key on the editing keypad")
447 (edt-map-key "SELECT" " - The Select key on the editing keypad")
448 (edt-map-key "PREVIOUS" " - The Prev Scr key on the editing keypad")
449 (edt-map-key "NEXT" " - The Next Scr key on the editing keypad")
450
451 (set-buffer "Keys")
452 (insert "
453 ;;
454 ;; F1-14 Help Do F17-F20
455 ;;
456 ")
457 (set-buffer "Directions")
458
459 (edt-map-key "F1" " - F1 Function Key")
460 (edt-map-key "F2" " - F2 Function Key")
461 (edt-map-key "F3" " - F3 Function Key")
462 (edt-map-key "F4" " - F4 Function Key")
463 (edt-map-key "F5" " - F5 Function Key")
464 (edt-map-key "F6" " - F6 Function Key")
465 (edt-map-key "F7" " - F7 Function Key")
466 (edt-map-key "F8" " - F8 Function Key")
467 (edt-map-key "F9" " - F9 Function Key")
468 (edt-map-key "F10" " - F10 Function Key")
469 (edt-map-key "F11" " - F11 Function Key")
470 (edt-map-key "F12" " - F12 Function Key")
471 (edt-map-key "F13" " - F13 Function Key")
472 (edt-map-key "F14" " - F14 Function Key")
473 (edt-map-key "HELP" " - HELP Function Key")
474 (edt-map-key "DO" " - DO Function Key")
475 (edt-map-key "F17" " - F17 Function Key")
476 (edt-map-key "F18" " - F18 Function Key")
477 (edt-map-key "F19" " - F19 Function Key")
478 (edt-map-key "F20" " - F20 Function Key")
479
480 (set-buffer "Directions")
481 (delete-region (point-min) (point-max))
482 (insert "
483 ADDITIONAL FUNCTION KEYS
484
485 Your keyboard may have additional function keys which do not correspond
486 to any LK-201 keys. The EDT Emulation can be configured to recognize
487 those keys, since you may wish to add your own key bindings to those keys.
488
489 For example, suppose your keyboard has a keycap marked \"Line Del\" and
490 you wish to add it to the list of keys which can be customized by the EDT
491 Emulation. First, assign a unique single-word name to the key for use by
492 the EDT Emulation, for example, \"linedel\". Then, at the \"EDT Key
493 Name:\" prompt, enter \"linedel\", followed by a press of the RETURN key.
494 Finally, when prompted, press the \"Line Del\" key. You now will be able
495 to bind functions to \"linedel\" and \"Gold-linedel\" in edt-user.el in
496 just the same way you can customize bindings of the LK-201 function and
497 keypad keys.
498
499 When you are done, just press RETURN at the \"EDT Key Name:\" prompt.
500 ")
501 (switch-to-buffer "Directions")
502 ;;;
503 ;;; Add support for extras keys
504 ;;;
505 (set-buffer "Keys")
506 (insert "\
507 ;;
508 ;; Extra Keys
509 ;;
510 ")
511 ;;;
512 ;;; Restore function-key-map.
513 ;;;
514 (if (and edt-window-system (not (featurep 'xemacs)))
515 (setq function-key-map edt-save-function-key-map))
516 (setq EDT-key-name "")
517 (while (not
518 (string-equal (setq EDT-key-name (read-string "EDT Key Name: ")) ""))
519 (edt-map-key EDT-key-name ""))
520
521 ;
522 ; No more keys to add, so wrap up.
523 ;
524 (set-buffer "Keys")
525 (insert "\
526 )
527 )
528 ")
529
530 ;;;
531 ;;; Save the key mapping program
532 ;;;
533 ;;;
534 ;;; Save the key mapping file
535 ;;;
536 (let ((file (concat
537 "~/.edt-" (if (featurep 'xemacs) "xemacs" "gnu")
538 (if edt-term (concat "-" edt-term))
539 (if edt-xserver (concat "-" edt-xserver))
540 (if edt-window-system (concat "-" (upcase (symbol-name edt-window-system))))
541 "-keys")))
542 (set-visited-file-name
543 (read-file-name (format "Save key mapping to file (default %s): " file) nil file)))
544 (save-buffer)
545
546 (message "That's it! Press any key to exit")
547 (sit-for 600)
548 (kill-emacs t)
549
550 ;;; edt-mapper.el ends here