home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / vt200-esc-fix.el < prev    next >
Encoding:
Text File  |  1993-03-03  |  8.6 KB  |  240 lines

  1. ;;; File:  vt200-esc-fix.el, v 3.1a (emacs 18[.58] version)
  2. ;;;
  3. ;;;               -----------   -----   -----
  4. ;;;               E s c a p e   K e y   F i x
  5. ;;;               -----------   -----   -----
  6. ;;;
  7. ;;;
  8. ;;; Copyright (C) 1990 Free Software Foundation, Inc.
  9. ;;; Copyright (C) 1993 Jeff Morgenthaler
  10. ;;; Thanks to Kevin Gallagher for TERM-in-list and Joe Wells for
  11. ;;; encouragement to code everthing right.  
  12.  
  13.  
  14. ;; LCD Archive Entry:
  15. ;; vt200-esc-fix|Jeff Morgenthaler|jpmorgen@wisp4.physics.wisc.edu|
  16. ;; Put ESC key in convenient reach on VT220.|
  17. ;; 93-02-12|3.1|~/functions/vt200-esc-fix.el.Z|
  18.  
  19. ;;Archived at archive.cis.ohio-state.edu
  20.  
  21. ;;;
  22. ;;; GNU Emacs is distributed in the hope that it will be useful, but
  23. ;;; WITHOUT ANY WARRANTY.  No author or distributor accepts
  24. ;;; RESPONSIBILITY TO anyone for the consequences of using it or for
  25. ;;; whether it serves any particular purpose or works at all, unless 
  26. ;;; he says so in writing.  Refer to the GNU Emacs General Public
  27. ;;; License for full details.
  28. ;;;
  29. ;;;  Send bug reports and suggestions for improvement to Jeff Morgenthaler
  30. ;;;  (jpmorgen@wisp4.physics.wisc.edu).
  31. ;;;
  32. ;;; Everyone is granted permission to copy, modify and redistribute
  33. ;;; GNU Emacs, but only under the conditions described in the GNU
  34. ;;; Emacs General Public License.  A copy of this license is supposed
  35. ;;; to have been given to you along with GNU Emacs so you can know
  36. ;;; your rights and responsibilities.  It should be in a file named
  37. ;;; COPYING.  Among other things, the Copyright notice and this notice
  38. ;;; must be preserved on all copies.
  39. ;;;
  40.  
  41. ;;;; VT200 type terminals have no escape key. This causes great
  42. ;;;; trouble for emacs and another well known UNIX editor.  The most
  43. ;;;; popular solution to this problem is to run the terminal in vt100
  44. ;;;; mode (if possible).  This unfortunately makes the function keys
  45. ;;;; inaccessible.  Another solution is to get the terminal to remap
  46. ;;;; some keys (vt300s and xterms do this).  The usual remapping is to
  47. ;;;; put the ~/` key down on the </> key and put the < and > above the
  48. ;;;; , and . where they belong.  Also, the F11 can be made to send a
  49. ;;;; real ESC, not an ESC[23~.
  50.  
  51. ;;;; Unfortunately, those of us who are stuck with old vt200 terminals
  52. ;;;; can't do this.  Emacs is smart enough to allow a partial
  53. ;;;; remapping of keys, but there are still problems.  This code
  54. ;;;; solves or works around as many of these problems as I know about.
  55. ;;;; (tell be about any more that you find).  By default it maps ` to
  56. ;;;; ESC and C-c ` to `.  Unfortunately, C-x ` (next-error) is no
  57. ;;;; longer accessible, so it is mapped to C-x ~.  
  58.  
  59. ;;;; To make this facility available for all users, place this file,
  60. ;;;; vt200-esc-fix.el, into your site's public emacs/lisp directory
  61. ;;;; and add the following command to your site's default.el file:
  62.  
  63. ;;;;         (require 'vt200-esc-fix)
  64.  
  65.  
  66. ;;;; This code can either be called interactively with "M-x escape-key-fix"
  67. ;;;; or started automatically with the code:
  68.  
  69. ;;;;  (setq terminal-needs-escape-key
  70. ;;;;        '("vt200" "vt300"))
  71.  
  72. ;;;; in the user's .emacs file.  To turn it off, just use "M-x
  73. ;;;; escape-key-fix" again.  
  74.  
  75. ;;;; The ESC key can be remapped to another key on the keyboard that
  76. ;;;; sends a single character (not the function keys, sorry) with the
  77. ;;;; code:
  78.  
  79. ;;;; (setq escape-key-replacement ?<)
  80. ;;;; (setq escape-key-fix-displaced-function 'scroll-left)
  81. ;;;; (setq escape-key-fix-ctrl-seq "\C-xl")
  82. ;;;; (setq escape-key-fix-ctrl-seq-message "Use C-x l for C-x <.")
  83.  
  84. ;;;; You can bind type-escape-key-replacement to any key.  For
  85. ;;;; instance, if you wanted the F11 key to send ` (or your customized
  86. ;;;; escape-key-replacement), you would use the code:
  87.  
  88. ;;;; (setq term-setup-hook
  89. ;;;;       (function
  90. ;;;;        (lambda ()
  91. ;;;;      (and (fboundp 'enable-arrow-keys)
  92. ;;;;           (progn
  93. ;;;;         (enable-arrow-keys)
  94. ;;;;         (define-key CSI-map "23~" 'type-escape-key-replacement)   ; F11
  95. ;;;;         )
  96. ;;;;           )
  97. ;;;;      )))
  98.  
  99.  
  100. ;; Define default ESC replacement and associated work arounds.  Tell me if 
  101. ;; there are any more conflicts.
  102.  
  103. (defvar escape-key-replacement ?`
  104.   "*ascii code of escape key replacement.")
  105.  
  106. (defvar escape-key-replacement-replacement "\C-c\e"
  107.   "* Key sequence allowing access to escape-key-replacement.")
  108.  
  109. (defvar escape-key-fix-ctrl-seq "\C-x~"
  110.   "* Control character sequence to replace the sequence displaced by
  111. remapping the escape-key-replacement key to ESC.  For example C-x ` is
  112. displaced by remapping ` to ESC.  It is remapped to C-x ~ by default.")
  113.  
  114.  
  115. (defvar escape-key-fix-displaced-function 'next-error
  116.   "* Function assigned to key sequence displaced by remapping 
  117. escape-key-replacement to ESC.")
  118.  
  119. (defvar escape-key-fix-ctrl-seq-message "Use C-x ~ for C-x `."
  120.   "* Message informing users of control sequence remappings.")
  121.  
  122.  
  123. (global-set-key escape-key-replacement-replacement 
  124.         'type-escape-key-replacement)
  125.  
  126. (global-set-key escape-key-fix-ctrl-seq escape-key-fix-displaced-function)
  127. ;; Replace C-x ` with C-x ~ by default.  Doesn't work for sub modes )-:.  
  128.  
  129. (defun init-keyboard-translate-table ()
  130.   "Initialize translate table, saving previous mappings, if any."
  131.   (let ((the-table (make-string 256 0)))
  132.     (let ((i 0)
  133.       (j (length keyboard-translate-table)))
  134.       (while (< i j)
  135.     (aset the-table i (elt keyboard-translate-table i))
  136.     (setq i (1+ i)))
  137.       (while (< i 256)
  138.     (aset the-table i i)
  139.     (setq i (1+ i))))
  140.     (setq keyboard-translate-table the-table)))
  141.  
  142. (defun TERM-in-list (term-list)
  143.   "Returns t if the current terminal (TERM) is in term-list.  Drops everthing 
  144. in TERM past the first hyphen."
  145.   (setq TERM-in-list nil)
  146.   (let ((term (getenv "TERM"))
  147.     hyphend)
  148.     ;; Strip off hyphen and what follows
  149.     (while (setq hyphend (string-match "[-_][^-_]+$" term))
  150.       (setq term (substring term 0 hyphend)))
  151.     (let ((len (length term-list))
  152.       (idx 0)
  153.       (temp-term nil))
  154.       (while (and (< idx len)
  155.           (not temp-term))
  156.     (if (string-equal term 
  157.               (nth idx term-list))
  158.         (progn
  159.           (setq TERM-in-list t)
  160.           (setq temp-term term))
  161.       (setq idx (1+ idx))))))
  162.   (setq TERM-in-list TERM-in-list))
  163.  
  164. (defvar escape-key-fix-flag nil
  165.   "*Flag to indicate if escape is remapped.")
  166.  
  167.  
  168. (defun type-escape-key-replacement (arg)
  169.   "Inserts the character escape-key-replacement, since it is overwritten
  170. by escape-key-fix."
  171.   (interactive "p")
  172.   (insert-char escape-key-replacement arg))
  173.  
  174. (defun escape-key-fix (arg)
  175.   "A quick fix for vt200 type keyboards which have no escape key.
  176. This function remaps escape-key-replacement (` by default) to ESC.
  177. What happens to the ` key?  Well this is a problem.  It can be
  178. inserted as a character using C-c `, but it cannot be used in a
  179. control character sequence like C-x `.  Instead, use C-x ~ (you may
  180. have to do some redefining in your favorite minor modes for this.)  If
  181. there are other problems, you will have to turn off escape-key-fix (it
  182. toggles).
  183.  
  184. This function is not intended for permanent use.  The best solution to
  185. the problem of the vt200 escape key is to fix your hardware.  In X
  186. windows, you can use xmodmap, on vt300s, you can remap keys in the
  187. setup menu."
  188.  
  189.   (interactive "p")  
  190.   (if (> arg 1)
  191.       (setq escape-key-fix-flag nil))
  192.   ;; Turn escape-key-fix on for an argument greater than 1
  193.   ;;  (e.g. C-u M-x escape-key-fix)
  194.   (if (< arg 0) 
  195.       (setq escape-key-fix-flag t))
  196.   ;; Turn escape-key-fix off for negative argument
  197.   (if escape-key-fix-flag
  198.       ;; reset ESC and escape-key-replacement
  199.       (progn
  200.     (init-keyboard-translate-table)
  201.     (aset keyboard-translate-table ?\e ?\e)
  202.     (aset keyboard-translate-table escape-key-replacement escape-key-replacement)
  203.     (message (concat "ESC and "
  204.              (single-key-description escape-key-replacement)
  205.              " are reset."))
  206.     (setq escape-key-fix-flag nil)
  207.     )
  208.     ;; Make the "escape-key-replacement" key send ESC.
  209.     ;; Can't swap them directly, since that messes up arrow/function keys.
  210.     (progn
  211.           (init-keyboard-translate-table)
  212.       (aset keyboard-translate-table  escape-key-replacement ?\e)
  213.  
  214.       (message (concat "The "(single-key-description escape-key-replacement)
  215.                " key now sends ESC.  "
  216.                "C-c "(single-key-description escape-key-replacement)
  217.                " is now " 
  218.                (single-key-description escape-key-replacement)
  219.                ".  " escape-key-fix-ctrl-seq-message))
  220.  
  221.       (sleep-for 1) ; Give user a chance to see message.
  222.       (setq escape-key-fix-flag t)
  223.       )
  224.     )
  225. )
  226.  
  227. (defun auto-escape-key-fix ()
  228.   "Assigns the ESC key to the key named in escape-key-replacement using 
  229. escape-key-fix if the current terminal is is the list terminal-needs-escape.  
  230. Drops everthing in TERM past the first hyphen."
  231.   (if (boundp 'terminal-needs-escape-key)
  232.       (if (TERM-in-list terminal-needs-escape-key)
  233.       (escape-key-fix 4))
  234.     )
  235. )
  236.  
  237. (auto-escape-key-fix)
  238.  
  239. (provide 'vt200-esc-fix)
  240.