home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / flowctrl.zip / FLOWCTRL.EL next >
Text File  |  1993-03-25  |  11KB  |  248 lines

  1. ;;; File:  flow-ctrl.el, v 2.8 (emacs 18[.58] version)
  2. ;;;
  3. ;;;               -------   -------------   ---------------------
  4. ;;;               F l o w   C o n t r o l   A d j u s t m e n t s
  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 everything right.  
  12. ;;;
  13.  
  14. ;; LCD Archive Entry:
  15. ;; flow-ctrl|Jeff Morgenthaler|jpmorgen@wisp4.physics.wisc.edu|
  16. ;; Flow control adjustments to remap C-s and C-q.|
  17. ;; 1993-03-01|2.8|~/misc/flow-ctrl.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. ;;;; WARNING: this code uses set-input-mode, which is due to change
  42. ;;;; (slightly?) in emacs 19.
  43.  
  44. ;;;; XON/XOFF flow control is a primitive user interface scheme 
  45. ;;;; employed by most terminals, terminal servers, modems, and
  46. ;;;; operating systems.  XON/XOFF flow control allows the user,
  47. ;;;; terminal, modem, etc. to pause the data stream at any time for
  48. ;;;; whatever reason.  The standard flow control commands are C-s for
  49. ;;;; stop and C-q for "quontinue" (at least that's how I remember it).
  50.  
  51. ;;;; Users of GNU emacs will recognize C-s and C-q as the commands
  52. ;;;; isearch-forward and quote-insert, and part of the commands
  53. ;;;; save-buffer and toggle-read-only.  It does not take very long to
  54. ;;;; recognize the usefulness of these bindings in emacs (and the
  55. ;;;; emacs model in general--mnemonic key bindings) over any
  56. ;;;; usefulness that XON/XOFF flow control had or will ever had.
  57.  
  58. ;;;; Emacs does the best it can to turn XON/XOFF flow control off.
  59. ;;;; Unfortunately, due to poor planing on the part of the inventors
  60. ;;;; of XON/XOFF flow control, it is only possible to disable flow
  61. ;;;; control on the machine that is running emacs: there is no
  62. ;;;; standard sequence of characters that a remote terminal, modem,
  63. ;;;; operating system, etc. will interpret to mean "disable flow
  64. ;;;; control."
  65.  
  66. ;;;; Thus, it is up to each user to make sure that XON/XOFF flow
  67. ;;;; control is disabled at all hardware between the user and the
  68. ;;;; machine running emacs.  Terminals usually have an item in their
  69. ;;;; setup menu for flow control or "hsk."  Modems using the Hayes
  70. ;;;; standard "at" commands should respond to at&k0.  Terminal or
  71. ;;;; modem servers will often have a command such as "set port flow
  72. ;;;; control disable" to be issued an the "local" prompt.  In VMS, you
  73. ;;;; can issue the command SET TERM/PASSALL.  In UNIX, you can try
  74. ;;;; "stty start u stop u," rlogin -8, or use telnet....
  75.  
  76. ;;;; Unfortunately (and you probably wouldn't be reading this if this
  77. ;;;; wasn't the case for you) it is sometimes impossible or
  78. ;;;; inconvenient at the moment to disable flow control.  Emacs has
  79. ;;;; always had a solution for this: the C-\ and C-^ keys are not
  80. ;;;; bound to anything (unlike all the other control characters in
  81. ;;;; emacs).  Following the emacs model, these keys are fairly
  82. ;;;; mnemonic: C-\ (that is back-Slash) resembles / which is search in
  83. ;;;; more/vi/ed) and C-^ can clearly be associated with the ^ produced
  84. ;;;; by C-q C-w.  Also, emacs has a function (set-input-mode) which
  85. ;;;; allows the C-s and C-q characters to be passed to the machine on
  86. ;;;; which emacs is running in case the terminal, modem, server, etc.,
  87. ;;;; really can't handle the rate of data flow and has no other means
  88. ;;;; of flow control.
  89.  
  90. ;;;; What has been missing up to now has been a decent wrapper for
  91. ;;;; these already existing features so that users can function in
  92. ;;;; emacs even when they are stuck on broken hardware without
  93. ;;;; becoming expert emacs code writers.  By "decent wrapper" I mean a
  94. ;;;; function that can be called interactively (with M-x) that is
  95. ;;;; sensibly named.  The function should not interfere with other
  96. ;;;; functions which remap the keys and it should toggle, cleaning up
  97. ;;;; completely after itself.  Variables for customizing the key
  98. ;;;; remappings and an auto-start feature should also be included.
  99.  
  100. ;;;; flow-control-fix is such a function.
  101.  
  102. ;;;; The variables C-s-replacement, C-q-replacement and
  103. ;;;; terminal-uses-flow-control-chars are available for customization
  104. ;;;; of flow-control-fix.  Here are some examples of how to set them
  105. ;;;; (you would put these in your .emacs file):
  106.  
  107. ;;;; (setq C-s-replacement ?\C-t)
  108. ;;;; (setq terminal-uses-flow-control-chars 
  109. ;;;;   '("vt52" "vt100" "vt200"))
  110.  
  111. ;;;; TERM-in-list and init-keyboard-translate-table, which are useful
  112. ;;;; functions for further user customizations are also also defined.
  113.  
  114. ;;;; To make this facility available for use to all users, place this file
  115. ;;;; (flow-ctrl.el) into your site's public emacs/lisp directory and
  116. ;;;; add the following command to your site's default.el file:
  117.  
  118. ;;;; (require 'flow-ctrl)
  119.  
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  121.  
  122. ;; default C-s, C-q replacements
  123. (defvar C-s-replacement ?\C-\\
  124.   "*ascii version of character to substitute for Control-S.  Example of 
  125. setting this variable: (setq C-s-replacement ?\\C-t).")
  126.  
  127. (defvar C-q-replacement ?\C-^
  128.   "*ascii version of character to substitute for Control-Q.  Example of 
  129. setting this variable: (setq C-q-replacement ?\\C-]).")
  130.  
  131. (defvar terminal-uses-flow-control-chars nil
  132.   "* List of general terminal types used by the user which are often 
  133. flow-control hobbled.  Used by auto-flow-control-fix.  Example of setting 
  134. this variable:
  135. (setq terminal-uses-flow-control-chars 
  136.    '(""vt52"" ""vt100"" ""vt200""))")
  137.  
  138. (defun init-keyboard-translate-table ()
  139.   "Initialize translate table, saving previous mappings, if any."
  140.   (let ((the-table (make-string 256 0)))
  141.     ;; Some users of PC and DEMACS need a large keyboard-translate-table
  142.     (let ((i 0)
  143.       (j (length keyboard-translate-table)))
  144.       (while (< i j)
  145.     (aset the-table i (elt keyboard-translate-table i))
  146.     (setq i (1+ i)))
  147.       (while (< i 256)
  148.     (aset the-table i i)
  149.     (setq i (1+ i))))
  150.     (setq keyboard-translate-table the-table)))
  151.  
  152. (defun TERM-in-list (term-list)
  153.   "Returns t if the current terminal \(TERM\) is in term-list.  Drops 
  154. everything in TERM past the first hyphen. Example of term-list:
  155.    '\(""vt52"" ""vt100"" ""vt200""\)"
  156.   (setq TERM-in-list nil)
  157.   (let ((term (getenv "TERM"))
  158.     hyphend)
  159.     ;; Strip off hyphen and what follows
  160.     (while (setq hyphend (string-match "[-_][^-_]+$" term))
  161.       (setq term (substring term 0 hyphend)))
  162.     (let ((len (length term-list))
  163.       (idx 0)
  164.       (temp-term nil))
  165.       (while (and (< idx len)
  166.           (not temp-term))
  167.     (if (string-equal term 
  168.               (nth idx term-list))
  169.         (progn
  170.           (setq TERM-in-list t)
  171.           (setq temp-term term))
  172.       (setq idx (1+ idx))))))
  173.   (setq TERM-in-list TERM-in-list))
  174.  
  175. (defvar flow-control-fix-flag nil
  176.   "*Flag to indicate if flow control avoidance is in effect.")
  177.  
  178. (defun flow-control-fix (arg)
  179.   "Replaces C-s with C-s-replacement, C-q with C-q-replacement \(C-\\
  180. and C-^ by default\) and tells emacs to pass C-s and C-q on to the
  181. operating system.  Gets around XON/XOFF flow control.  Turns on with a
  182. positive argument greater than 1, off with a negative argument and
  183. toggles with no argument.  This is a last resort fix!  First try
  184. turning off flow control at your terminal \(with the setup menu\),
  185. modem \(at&k0\), terminal server \(set port flow control disable\),
  186. machine you are logging in through \(SET TERM/PASSALL or rlogin -8\),
  187. etc."
  188.  
  189.   (interactive "p")
  190.   (if (> arg 1)
  191.       ;; Turn flow-control-fix on for an argument greater that 1
  192.       ;;; (e.g. C-u M-x flow-control-fix)
  193.       (setq flow-control-fix-flag nil))
  194.   (if (< arg 0) 
  195.       ;; Turn flow-control-fix off for negative argument
  196.       (setq flow-control-fix-flag t))
  197.  
  198.   (if flow-control-fix-flag
  199.        ;; Tell emacs to not pass C-s and C-q to OS and reset keys.
  200.        (progn
  201.      (set-input-mode nil nil)
  202.      ;; !!!!!!!!!!!!emacs 19 users will have to change this!!!!!!!!!!!!!!!!
  203.      ;; Restore C-s and C-s-replacement
  204.      (init-keyboard-translate-table)
  205.      (aset keyboard-translate-table ?\^s ?\^s)
  206.      (aset keyboard-translate-table C-s-replacement C-s-replacement)
  207.      ;; Restore C-q and C-q-replacement
  208.      (aset keyboard-translate-table ?\^q ?\^q)
  209.      (aset keyboard-translate-table C-q-replacement C-q-replacement)
  210.      (message "C-s and C-q restored.")
  211.      (setq flow-control-fix-flag nil)
  212.      )
  213.     ;; Tell emacs to pass C-s and C-q to OS and swap out C-s and C-q.
  214.     (progn
  215.       (set-input-mode nil t)
  216.       ;; !!!!!!!!!!!!emacs 19 users will have to change this!!!!!!!!!!!!!!!!
  217.       (init-keyboard-translate-table)
  218.       ;; Swap C-s and C-s-replacement
  219.       (aset keyboard-translate-table C-s-replacement ?\^s)
  220.       (aset keyboard-translate-table ?\^s C-s-replacement)
  221.       ;; Swap C-q and C-q-replacement
  222.       (aset keyboard-translate-table C-q-replacement ?\^q)
  223.       (aset keyboard-translate-table ?\^q C-q-replacement)
  224.       (message (concat "XON/XOFF adjustment for " 
  225.                  (getenv "TERM") 
  226.                ":  use "(single-key-description C-s-replacement)
  227.                " for C-s  and  use  "
  228.                (single-key-description C-q-replacement)
  229.                " for C-q."))
  230.       (sleep-for 1) ; Give user a chance to see message.
  231.       (setq flow-control-fix-flag t)
  232.       )
  233.     )
  234. )
  235.  
  236. (defun auto-flow-control-fix ()
  237.   "Enables flow control avoidance using flow-control-fix if the current 
  238. terminal (TERM) is in the terminal-uses-flow-control-chars list.  Drops 
  239. everything in TERM past the first hyphen."
  240.   (if (TERM-in-list terminal-uses-flow-control-chars)
  241.       ;; Positive argument > 1 turns flow-control-fix on
  242.       (flow-control-fix 4))
  243. )
  244.  
  245. (auto-flow-control-fix)
  246.  
  247. (provide 'flow-ctrl)
  248.