flow-ctrl.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;; flow-ctrl.el --- help for lusers on cu(1) or ttys with wired-in ^S/^Q flow control
  2. ;; Copyright (C) 1990-1991, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Kevin Gallagher
  4. ;; Maintainer: FSF
  5. ;; Adapted-By: ESR
  6. ;; Keywords: hardware
  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. ;; Terminals that use XON/XOFF flow control can cause problems with
  20. ;; GNU Emacs users. This file contains Emacs Lisp code that makes it
  21. ;; easy for a user to deal with this problem, when using such a
  22. ;; terminal.
  23. ;;
  24. ;; To invoke these adjustments, a user need only invoke the function
  25. ;; enable-flow-control-on with a list of terminal types in his/her own
  26. ;; .emacs file. As arguments, give it the names of one or more terminal
  27. ;; types in use by that user which require flow control adjustments.
  28. ;; Here's an example:
  29. ;;
  30. ;; (enable-flow-control-on "vt200" "vt300" "vt101" "vt131")
  31. ;; Portability note: This uses (getenv "TERM"), and therefore probably
  32. ;; won't work outside of UNIX-like environments.
  33. ;;; Code:
  34. (defvar flow-control-c-s-replacement ?\034
  35. "Character that replaces C-s, when flow control handling is enabled.")
  36. (defvar flow-control-c-q-replacement ?\036
  37. "Character that replaces C-q, when flow control handling is enabled.")
  38. (put 'keyboard-translate-table 'char-table-extra-slots 0)
  39. ;;;###autoload
  40. (defun enable-flow-control (&optional argument)
  41. "Toggle flow control handling.
  42. When handling is enabled, user can type C-s as C-\\, and C-q as C-^.
  43. With arg, enable flow control mode if arg is positive, otherwise disable."
  44. (interactive "P")
  45. (if (if argument
  46. ;; Argument means enable if arg is positive.
  47. (<= (prefix-numeric-value argument) 0)
  48. ;; No arg means toggle.
  49. (nth 1 (current-input-mode)))
  50. (progn
  51. ;; Turn flow control off, and stop exchanging chars.
  52. (set-input-mode t nil (nth 2 (current-input-mode)))
  53. (if keyboard-translate-table
  54. (progn
  55. (aset keyboard-translate-table flow-control-c-s-replacement nil)
  56. (aset keyboard-translate-table ?\^s nil)
  57. (aset keyboard-translate-table flow-control-c-q-replacement nil)
  58. (aset keyboard-translate-table ?\^q nil))))
  59. ;; Turn flow control on.
  60. ;; Tell emacs to pass C-s and C-q to OS.
  61. (set-input-mode nil t (nth 2 (current-input-mode)))
  62. ;; Initialize translate table, saving previous mappings, if any.
  63. (cond ((null keyboard-translate-table)
  64. (setq keyboard-translate-table
  65. (make-char-table 'keyboard-translate-table nil)))
  66. ((char-table-p keyboard-translate-table)
  67. (setq keyboard-translate-table
  68. (copy-sequence keyboard-translate-table)))
  69. (t
  70. (let ((the-table (make-char-table 'keyboard-translate-table nil)))
  71. (let ((i 0)
  72. (j (length keyboard-translate-table)))
  73. (while (< i j)
  74. (aset the-table i (elt keyboard-translate-table i))
  75. (setq i (1+ i))))
  76. (setq keyboard-translate-table the-table))))
  77. ;; Swap C-s and C-\
  78. (aset keyboard-translate-table flow-control-c-s-replacement ?\^s)
  79. (aset keyboard-translate-table ?\^s flow-control-c-s-replacement)
  80. ;; Swap C-q and C-^
  81. (aset keyboard-translate-table flow-control-c-q-replacement ?\^q)
  82. (aset keyboard-translate-table ?\^q flow-control-c-q-replacement)
  83. (message "XON/XOFF adjustment for %s: use %s for C-s, and use %s for C-q"
  84. (getenv "TERM")
  85. (single-key-description flow-control-c-s-replacement)
  86. (single-key-description flow-control-c-q-replacement))
  87. (sleep-for 2))) ; Give user a chance to see message.
  88. ;;;###autoload
  89. (defun enable-flow-control-on (&rest losing-terminal-types)
  90. "Enable flow control if using one of a specified set of terminal types.
  91. Use `(enable-flow-control-on \"vt100\" \"h19\")' to enable flow control
  92. on VT-100 and H19 terminals. When flow control is enabled,
  93. you must type C-\\ to get the effect of a C-s, and type C-^
  94. to get the effect of a C-q."
  95. (let ((term (getenv "TERM"))
  96. hyphend)
  97. ;; Look for TERM in LOSING-TERMINAL-TYPES.
  98. ;; If we don't find it literally, try stripping off words
  99. ;; from the end, one by one.
  100. (while (and term (not (member term losing-terminal-types)))
  101. ;; Strip off last hyphen and what follows, then try again.
  102. (if (setq hyphend (string-match "[-_][^-_]+$" term))
  103. (setq term (substring term 0 hyphend))
  104. (setq term nil)))
  105. (if term
  106. (enable-flow-control))))
  107. (provide 'flow-ctrl)
  108. ;;; flow-ctrl.el ends here