summaryrefslogtreecommitdiff
path: root/lisp/flow-ctrl.el
blob: 104aee3ac86a0d09444036e588324c0ca8e6174d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; flow-ctrl.el --- help for lusers on cu(1) or terminals with wired-in ^S/^Q flow control

;;; Copyright (C) 1990 Free Software Foundation, Inc.
;;; Copyright (C) 1991 Kevin Gallagher
;;; Adapted for Emacs 19 by Eric S. Raymond <eric@snark.thyrsus.com>
;;;
;;; GNU Emacs is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY.  No author or distributor accepts
;;; RESPONSIBILITY TO anyone for the consequences of using it or for
;;; whether it serves any particular purpose or works at all, unless 
;;; he says so in writing.  Refer to the GNU Emacs General Public
;;; License for full details.
;;;
;;; Everyone is granted permission to copy, modify and redistribute
;;; GNU Emacs, but only under the conditions described in the GNU
;;; Emacs General Public License.  A copy of this license is supposed
;;; to have been given to you along with GNU Emacs so you can know
;;; your rights and responsibilities.  It should be in a file named
;;; COPYING.  Among other things, the Copyright notice and this notice
;;; must be preserved on all copies.
;;;

;;;; Terminals that use XON/XOFF flow control can cause problems with
;;;; GNU Emacs users.  This file contains elisp code that makes it
;;;; easy for a user to deal with this problem, when using such a
;;;; terminal. 
;;;;      
;;;; To invoke these adjustments, a user need only invoke the function
;;;; evade-flow-control-on with a list of terminal types in his/her own
;;;; .emacs file.  As arguments, give it the names of one or more terminal
;;;; types in use by that user which require flow control adjustments.
;;;; Here's an example: 
;;;; 
;;;;	(evade-flow-control-on "vt200" "vt300" "vt101" "vt131")

;;; Portability note: This uses (getenv "TERM"), and therefore probably
;;; won't work outside of UNIX-like environments.

(defun evade-flow-control ()
  "Enable use of flow control; let user type C-s as C-\ and C-q as C-^."
  (interactive)
  ;; Tell emacs to pass C-s and C-q to OS.
  (set-input-mode nil t)
  ;; Initialize translate table, saving previous mappings, if any.
  (let ((the-table (make-string 128 0)))
    (let ((i 0)
	  (j (length keyboard-translate-table)))
      (while (< i j)
	(aset the-table i (elt keyboard-translate-table i))
	(setq i (1+ i)))
      (while (< i 128)
	(aset the-table i i)
	(setq i (1+ i))))
    (setq keyboard-translate-table the-table))
  ;; Swap C-s and C-\
  (aset keyboard-translate-table ?\034 ?\^s)
  (aset keyboard-translate-table ?\^s ?\034)
  ;; Swap C-q and C-^
  (aset keyboard-translate-table ?\036 ?\^q)
  (aset keyboard-translate-table ?\^q ?\036)
  (message (concat 
             "XON/XOFF adjustment for " 
             (getenv "TERM") 
             ":  use C-\\ for C-s  and  use C-^ for C-q."))
  (sleep-for 2)) ; Give user a chance to see message.

(defun evade-flow-control-memstr= (e s)
  (cond ((null s) nil)
	((string= e (car s)) t)
	(t (evade-flow-control-memstr= e (cdr s)))))

;;;###autoload
(defun evade-flow-control-on (&rest losing-terminal-types)
  "Enable flow control if using one of a specified set of terminal types.
Use `(evade-flow-control-on \"vt100\" \"h19\")' to enable flow control
on VT-100 and H19 terminals.  When flow control is enabled,
you must type C-\\ to get the effect of a C-s, and type C-^
to get the effect of a C-q."
  (let ((term (getenv "TERM"))
	hyphend)
    ;; Strip off hyphen and what follows
    (while (setq hyphend (string-match "[-_][^-_]+$" term))
      (setq term (substring term 0 hyphend)))
    (and (evade-flow-control-memstr= term losing-terminal-types) (evade-flow-control)))
  )

(provide 'flow-ctrl)

;;; flow-ctrl.el ends here