auth.texi (Secret Service API): Adjust @samp to @code for collection names
[emacs.git] / lisp / flow-ctrl.el
blob747a4ca018c382da51c42102e0728f5b8b76e90d
1 ;;; flow-ctrl.el --- help for lusers on cu(1) or ttys with wired-in ^S/^Q flow control
3 ;; Copyright (C) 1990-1991, 1994, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Kevin Gallagher
6 ;; Maintainer: FSF
7 ;; Adapted-By: ESR
8 ;; Keywords: hardware
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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
30 ;; terminal.
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
34 ;; .emacs file. As arguments, give it the names of one or more terminal
35 ;; types in use by that user which require flow control adjustments.
36 ;; Here's an example:
38 ;; (enable-flow-control-on "vt200" "vt300" "vt101" "vt131")
40 ;; Portability note: This uses (getenv "TERM"), and therefore probably
41 ;; won't work outside of UNIX-like environments.
43 ;;; Code:
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.")
50 (put 'keyboard-translate-table 'char-table-extra-slots 0)
52 ;;;###autoload
53 (defun enable-flow-control (&optional argument)
54 "Toggle flow control handling.
55 When handling is enabled, user can type C-s as C-\\, and C-q as C-^.
56 With 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)))
66 (if keyboard-translate-table
67 (progn
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))))
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.
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)))
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))))
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)
96 (message "XON/XOFF adjustment for %s: use %s for C-s, and use %s for C-q"
97 (getenv "TERM")
98 (single-key-description flow-control-c-s-replacement)
99 (single-key-description flow-control-c-q-replacement))
100 (sleep-for 2))) ; Give user a chance to see message.
102 ;;;###autoload
103 (defun enable-flow-control-on (&rest losing-terminal-types)
104 "Enable flow control if using one of a specified set of terminal types.
105 Use `(enable-flow-control-on \"vt100\" \"h19\")' to enable flow control
106 on VT-100 and H19 terminals. When flow control is enabled,
107 you must type C-\\ to get the effect of a C-s, and type C-^
108 to get the effect of a C-q."
109 (let ((term (getenv "TERM"))
110 hyphend)
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)))
119 (if term
120 (enable-flow-control))))
122 (provide 'flow-ctrl)
124 ;;; flow-ctrl.el ends here