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