edt-mapper.el 19 KB

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