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