* emacs-lisp/byte-run.el (defmacro): Use same argument parsing as
[bpt/emacs.git] / lisp / flow-ctrl.el
CommitLineData
fc68affa
ER
1;;; flow-ctrl.el --- help for lusers on cu(1) or ttys with wired-in ^S/^Q flow control
2
acaf905b 3;; Copyright (C) 1990-1991, 1994, 2001-2012 Free Software Foundation, Inc.
3a801d0c 4
8deda4af 5;; Author: Kevin Gallagher
fc68affa 6;; Maintainer: FSF
fc68affa 7;; Adapted-By: ESR
fd7fa35a 8;; Keywords: hardware
1a06eabd 9
59243403
RS
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
59243403 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
59243403
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fc68affa
ER
24
25;;; Commentary:
0f39230e 26
b578f267
EN
27;; Terminals that use XON/XOFF flow control can cause problems with
28;; GNU Emacs users. This file contains Emacs Lisp code that makes it
29;; easy for a user to deal with this problem, when using such a
71296446
JB
30;; terminal.
31;;
b578f267
EN
32;; To invoke these adjustments, a user need only invoke the function
33;; enable-flow-control-on with a list of terminal types in his/her own
865fe16f 34;; init file. As arguments, give it the names of one or more terminal
b578f267 35;; types in use by that user which require flow control adjustments.
71296446
JB
36;; Here's an example:
37;;
b578f267
EN
38;; (enable-flow-control-on "vt200" "vt300" "vt101" "vt131")
39
40;; Portability note: This uses (getenv "TERM"), and therefore probably
41;; won't work outside of UNIX-like environments.
0f39230e 42
fc68affa
ER
43;;; Code:
44
162de182
RS
45(defvar flow-control-c-s-replacement ?\034
46 "Character that replaces C-s, when flow control handling is enabled.")
47(defvar flow-control-c-q-replacement ?\036
48 "Character that replaces C-q, when flow control handling is enabled.")
0f39230e 49
8f21333a
RS
50(put 'keyboard-translate-table 'char-table-extra-slots 0)
51
162de182
RS
52;;;###autoload
53(defun enable-flow-control (&optional argument)
54 "Toggle flow control handling.
55When handling is enabled, user can type C-s as C-\\, and C-q as C-^.
56With arg, enable flow control mode if arg is positive, otherwise disable."
57 (interactive "P")
58 (if (if argument
59 ;; Argument means enable if arg is positive.
60 (<= (prefix-numeric-value argument) 0)
61 ;; No arg means toggle.
62 (nth 1 (current-input-mode)))
63 (progn
64 ;; Turn flow control off, and stop exchanging chars.
65 (set-input-mode t nil (nth 2 (current-input-mode)))
f87de92a
RS
66 (if keyboard-translate-table
67 (progn
8f21333a
RS
68 (aset keyboard-translate-table flow-control-c-s-replacement nil)
69 (aset keyboard-translate-table ?\^s nil)
70 (aset keyboard-translate-table flow-control-c-q-replacement nil)
71 (aset keyboard-translate-table ?\^q nil))))
162de182
RS
72 ;; Turn flow control on.
73 ;; Tell emacs to pass C-s and C-q to OS.
74 (set-input-mode nil t (nth 2 (current-input-mode)))
75 ;; Initialize translate table, saving previous mappings, if any.
8f21333a
RS
76 (cond ((null keyboard-translate-table)
77 (setq keyboard-translate-table
78 (make-char-table 'keyboard-translate-table nil)))
79 ((char-table-p keyboard-translate-table)
80 (setq keyboard-translate-table
81 (copy-sequence keyboard-translate-table)))
82 (t
83 (let ((the-table (make-char-table 'keyboard-translate-table nil)))
84 (let ((i 0)
85 (j (length keyboard-translate-table)))
86 (while (< i j)
87 (aset the-table i (elt keyboard-translate-table i))
88 (setq i (1+ i))))
89 (setq keyboard-translate-table the-table))))
162de182
RS
90 ;; Swap C-s and C-\
91 (aset keyboard-translate-table flow-control-c-s-replacement ?\^s)
92 (aset keyboard-translate-table ?\^s flow-control-c-s-replacement)
93 ;; Swap C-q and C-^
94 (aset keyboard-translate-table flow-control-c-q-replacement ?\^q)
95 (aset keyboard-translate-table ?\^q flow-control-c-q-replacement)
d8fb88d5 96 (message "XON/XOFF adjustment for %s: use %s for C-s, and use %s for C-q"
71296446 97 (getenv "TERM")
d8fb88d5
KH
98 (single-key-description flow-control-c-s-replacement)
99 (single-key-description flow-control-c-q-replacement))
162de182 100 (sleep-for 2))) ; Give user a chance to see message.
0f39230e
JB
101
102;;;###autoload
61251338 103(defun enable-flow-control-on (&rest losing-terminal-types)
9c50f912 104 "Enable flow control if using one of a specified set of terminal types.
61251338 105Use `(enable-flow-control-on \"vt100\" \"h19\")' to enable flow control
9c50f912 106on VT-100 and H19 terminals. When flow control is enabled,
328561fc 107you must type C-\\ to get the effect of a C-s, and type C-^
9c50f912 108to get the effect of a C-q."
0f39230e
JB
109 (let ((term (getenv "TERM"))
110 hyphend)
409adbc7
KH
111 ;; Look for TERM in LOSING-TERMINAL-TYPES.
112 ;; If we don't find it literally, try stripping off words
113 ;; from the end, one by one.
114 (while (and term (not (member term losing-terminal-types)))
115 ;; Strip off last hyphen and what follows, then try again.
116 (if (setq hyphend (string-match "[-_][^-_]+$" term))
117 (setq term (substring term 0 hyphend))
118 (setq term nil)))
3bf5f17a 119 (if term
409adbc7 120 (enable-flow-control))))
0f39230e 121
49116ac0
JB
122(provide 'flow-ctrl)
123
1a06eabd 124;;; flow-ctrl.el ends here