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