doc/misc/autotype.texi (Skeleton Language): Fix last change.
[bpt/emacs.git] / lisp / xt-mouse.el
1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
2
3 ;; Copyright (C) 1994, 2000-2014 Free Software Foundation, Inc.
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: mouse, terminals
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Enable mouse support when running inside an xterm.
26
27 ;; This is actually useful when you are running X11 locally, but is
28 ;; working on remote machine over a modem line or through a gateway.
29
30 ;; It works by translating xterm escape codes into generic emacs mouse
31 ;; events so it should work with any package that uses the mouse.
32
33 ;; You don't have to turn off xterm mode to use the normal xterm mouse
34 ;; functionality, it is still available by holding down the SHIFT key
35 ;; when you press the mouse button.
36
37 ;;; Todo:
38
39 ;; Support multi-click -- somehow.
40
41 ;;; Code:
42
43 (defvar xterm-mouse-debug-buffer nil)
44
45 (defvar xterm-mouse-last)
46
47 ;; Mouse events symbols must have an 'event-kind property with
48 ;; the value 'mouse-click.
49 (dolist (event-type '(mouse-1 mouse-2 mouse-3
50 M-down-mouse-1 M-down-mouse-2 M-down-mouse-3))
51 (put event-type 'event-kind 'mouse-click))
52
53 (defun xterm-mouse-translate (_event)
54 "Read a click and release event from XTerm."
55 (xterm-mouse-translate-1))
56
57 (defun xterm-mouse-translate-extended (_event)
58 "Read a click and release event from XTerm.
59 Similar to `xterm-mouse-translate', but using the \"1006\"
60 extension, which supports coordinates >= 231 (see
61 http://invisible-island.net/xterm/ctlseqs/ctlseqs.html)."
62 (xterm-mouse-translate-1 1006))
63
64 (defun xterm-mouse-translate-1 (&optional extension)
65 (save-excursion
66 (save-window-excursion ;FIXME: Why?
67 (deactivate-mark) ;FIXME: Why?
68 (let* ((xterm-mouse-last nil)
69 (down (xterm-mouse-event extension))
70 (down-command (nth 0 down))
71 (down-data (nth 1 down))
72 (down-where (nth 1 down-data))
73 (down-binding (key-binding (if (symbolp down-where)
74 (vector down-where down-command)
75 (vector down-command))))
76 (is-down (string-match "down" (symbol-name (car down)))))
77
78 ;; Retrieve the expected preface for the up-event.
79 (when is-down
80 (unless (cond ((null extension)
81 (and (eq (read-event) ?\e)
82 (eq (read-event) ?\[)
83 (eq (read-event) ?M)))
84 ((eq extension 1006)
85 (and (eq (read-event) ?\e)
86 (eq (read-event) ?\[)
87 (eq (read-event) ?<))))
88 (error "Unexpected escape sequence from XTerm")))
89
90 ;; Process the up-event.
91 (let* ((click (if is-down (xterm-mouse-event extension) down))
92 (click-data (nth 1 click))
93 (click-where (nth 1 click-data)))
94 (cond
95 ((null down) nil)
96 ((memq down-binding '(nil ignore))
97 (if (and (symbolp click-where)
98 (consp click-where))
99 (vector (list click-where click-data) click)
100 (vector click)))
101 (t
102 (setq unread-command-events
103 (append (if (eq down-where click-where)
104 (list click)
105 (list
106 ;; Cheat `mouse-drag-region' with move event.
107 (list 'mouse-movement click-data)
108 ;; Generate a drag event.
109 (if (symbolp down-where)
110 0
111 (list (intern (format "drag-mouse-%d"
112 (1+ xterm-mouse-last)))
113 down-data click-data))))
114 unread-command-events))
115 (if xterm-mouse-debug-buffer
116 (print unread-command-events xterm-mouse-debug-buffer))
117 (if (and (symbolp down-where)
118 (consp down-where))
119 (vector (list down-where down-data) down)
120 (vector down)))))))))
121
122 ;; These two variables have been converted to terminal parameters.
123 ;;
124 ;;(defvar xterm-mouse-x 0
125 ;; "Position of last xterm mouse event relative to the frame.")
126 ;;
127 ;;(defvar xterm-mouse-y 0
128 ;; "Position of last xterm mouse event relative to the frame.")
129
130 (defvar xt-mouse-epoch nil)
131
132 ;; Indicator for the xterm-mouse mode.
133
134 (defun xterm-mouse-position-function (pos)
135 "Bound to `mouse-position-function' in XTerm mouse mode."
136 (when (terminal-parameter nil 'xterm-mouse-x)
137 (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
138 (terminal-parameter nil 'xterm-mouse-y))))
139 pos)
140
141 (defun xterm-mouse-truncate-wrap (f)
142 "Truncate with wrap-around."
143 (condition-case nil
144 ;; First try the built-in truncate, in case there's no overflow.
145 (truncate f)
146 ;; In case of overflow, do wraparound by hand.
147 (range-error
148 ;; In our case, we wrap around every 3 days or so, so if we assume
149 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
150 ;; Using a power of 2 makes rounding errors less likely.
151 (let* ((maxwrap (* 65536 2048))
152 (dbig (truncate (/ f maxwrap)))
153 (fdiff (- f (* 1.0 maxwrap dbig))))
154 (+ (truncate fdiff) (* maxwrap dbig))))))
155
156 ;; Normal terminal mouse click reporting: expect three bytes, of the
157 ;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
158 (defun xterm-mouse--read-event-sequence-1000 ()
159 (let* ((code (- (read-event) 32))
160 (type
161 ;; For buttons > 3, the release-event looks differently
162 ;; (see xc/programs/xterm/button.c, function EditorButton),
163 ;; and come in a release-event only, no down-event.
164 (cond ((>= code 64)
165 (format "mouse-%d" (- code 60)))
166 ((memq code '(8 9 10))
167 (setq xterm-mouse-last (- code 8))
168 (format "M-down-mouse-%d" (- code 7)))
169 ((and (= code 11) xterm-mouse-last)
170 (format "M-mouse-%d" (1+ xterm-mouse-last)))
171 ((and (= code 3) xterm-mouse-last)
172 ;; For buttons > 5 xterm only reports a button-release event.
173 ;; Drop them since they're not usable and can be spurious.
174 (format "mouse-%d" (1+ xterm-mouse-last)))
175 ((memq code '(0 1 2))
176 (setq xterm-mouse-last code)
177 (format "down-mouse-%d" (+ 1 code)))))
178 (x (- (read-event) 33))
179 (y (- (read-event) 33)))
180 (and type (wholenump x) (wholenump y)
181 (list (intern type) x y))))
182
183 ;; XTerm's 1006-mode terminal mouse click reporting has the form
184 ;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
185 ;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
186 (defun xterm-mouse--read-event-sequence-1006 ()
187 (let (button-bytes x-bytes y-bytes c)
188 (while (not (eq (setq c (read-event)) ?\;))
189 (push c button-bytes))
190 (while (not (eq (setq c (read-event)) ?\;))
191 (push c x-bytes))
192 (while (not (memq (setq c (read-event)) '(?m ?M)))
193 (push c y-bytes))
194 (list (let* ((code (string-to-number
195 (apply 'string (nreverse button-bytes))))
196 (wheel (>= code 64))
197 (down (and (not wheel)
198 (eq c ?M))))
199 (intern (format "%s%smouse-%d"
200 (cond (wheel "")
201 ((< code 4) "")
202 ((< code 8) "S-")
203 ((< code 12) "M-")
204 ((< code 16) "M-S-")
205 ((< code 20) "C-")
206 ((< code 24) "C-S-")
207 ((< code 28) "C-M-")
208 ((< code 32) "C-M-S-")
209 (t
210 (error "Unexpected escape sequence from XTerm")))
211 (if down "down-" "")
212 (if wheel
213 (- code 60)
214 (1+ (setq xterm-mouse-last (mod code 4)))))))
215 (1- (string-to-number (apply 'string (nreverse x-bytes))))
216 (1- (string-to-number (apply 'string (nreverse y-bytes)))))))
217
218 (defun xterm-mouse-event (&optional extension)
219 "Convert XTerm mouse event to Emacs mouse event.
220 EXTENSION, if non-nil, means to use an extension to the usual
221 terminal mouse protocol; we currently support the value 1006,
222 which is the \"1006\" extension implemented in Xterm >= 277."
223 (let* ((click (cond ((null extension)
224 (xterm-mouse--read-event-sequence-1000))
225 ((eq extension 1006)
226 (xterm-mouse--read-event-sequence-1006))
227 (t
228 (error "Unsupported XTerm mouse protocol")))))
229 (when click
230 (let* ((type (nth 0 click))
231 (x (nth 1 click))
232 (y (nth 2 click))
233 ;; Emulate timestamp information. This is accurate enough
234 ;; for default value of mouse-1-click-follows-link (450msec).
235 (timestamp (xterm-mouse-truncate-wrap
236 (* 1000
237 (- (float-time)
238 (or xt-mouse-epoch
239 (setq xt-mouse-epoch (float-time)))))))
240 (w (window-at x y))
241 (ltrb (window-edges w))
242 (left (nth 0 ltrb))
243 (top (nth 1 ltrb)))
244 (set-terminal-parameter nil 'xterm-mouse-x x)
245 (set-terminal-parameter nil 'xterm-mouse-y y)
246 (setq
247 last-input-event
248 (list type
249 (let ((event (if w
250 (posn-at-x-y (- x left) (- y top) w t)
251 (append (list nil 'menu-bar)
252 (nthcdr 2 (posn-at-x-y x y))))))
253 (setcar (nthcdr 3 event) timestamp)
254 event)))))))
255
256 ;;;###autoload
257 (define-minor-mode xterm-mouse-mode
258 "Toggle XTerm mouse mode.
259 With a prefix argument ARG, enable XTerm mouse mode if ARG is
260 positive, and disable it otherwise. If called from Lisp, enable
261 the mode if ARG is omitted or nil.
262
263 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
264 This works in terminal emulators compatible with xterm. It only
265 works for simple uses of the mouse. Basically, only non-modified
266 single clicks are supported. When turned on, the normal xterm
267 mouse functionality for such clicks is still available by holding
268 down the SHIFT key while pressing the mouse button."
269 :global t :group 'mouse
270 (let ((do-hook (if xterm-mouse-mode 'add-hook 'remove-hook)))
271 (funcall do-hook 'terminal-init-xterm-hook
272 'turn-on-xterm-mouse-tracking-on-terminal)
273 (funcall do-hook 'delete-terminal-functions
274 'turn-off-xterm-mouse-tracking-on-terminal)
275 (funcall do-hook 'suspend-tty-functions
276 'turn-off-xterm-mouse-tracking-on-terminal)
277 (funcall do-hook 'resume-tty-functions
278 'turn-on-xterm-mouse-tracking-on-terminal)
279 (funcall do-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
280 (funcall do-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
281 (funcall do-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking))
282 (if xterm-mouse-mode
283 ;; Turn it on
284 (progn
285 (setq mouse-position-function #'xterm-mouse-position-function)
286 (turn-on-xterm-mouse-tracking))
287 ;; Turn it off
288 (turn-off-xterm-mouse-tracking 'force)
289 (setq mouse-position-function nil)))
290
291 (defun turn-on-xterm-mouse-tracking ()
292 "Enable Emacs mouse tracking in xterm."
293 (dolist (terminal (terminal-list))
294 (turn-on-xterm-mouse-tracking-on-terminal terminal)))
295
296 (defun turn-off-xterm-mouse-tracking (&optional _force)
297 "Disable Emacs mouse tracking in xterm."
298 (dolist (terminal (terminal-list))
299 (turn-off-xterm-mouse-tracking-on-terminal terminal)))
300
301 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal)
302 "Enable xterm mouse tracking on TERMINAL."
303 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal))
304 ;; Avoid the initial terminal which is not a termcap device.
305 ;; FIXME: is there more elegant way to detect the initial terminal?
306 (not (string= (terminal-name terminal) "initial_terminal")))
307 (unless (terminal-parameter terminal 'xterm-mouse-mode)
308 ;; Simulate selecting a terminal by selecting one of its frames
309 (with-selected-frame (car (frames-on-display-list terminal))
310 (define-key input-decode-map "\e[M" 'xterm-mouse-translate)
311 (define-key input-decode-map "\e[<" 'xterm-mouse-translate-extended))
312 (set-terminal-parameter terminal 'xterm-mouse-mode t))
313 (send-string-to-terminal "\e[?1000h" terminal)
314 ;; Request extended mouse support, if available (xterm >= 277).
315 (send-string-to-terminal "\e[?1006h" terminal)))
316
317 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
318 "Disable xterm mouse tracking on TERMINAL."
319 ;; Only send the disable command to those terminals to which we've already
320 ;; sent the enable command.
321 (when (and (terminal-parameter terminal 'xterm-mouse-mode)
322 (eq t (terminal-live-p terminal))
323 ;; Avoid the initial terminal which is not a termcap device.
324 ;; FIXME: is there more elegant way to detect the initial terminal?
325 (not (string= (terminal-name terminal) "initial_terminal")))
326 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
327 ;; terminal parameter, but it seems less harmful to send this escape
328 ;; command too many times (or to catch an unintended key sequence), than
329 ;; to send it too few times (or to fail to let xterm-mouse events
330 ;; pass by untranslated).
331 (send-string-to-terminal "\e[?1000l" terminal)
332 (send-string-to-terminal "\e[?1006l" terminal)))
333
334 (provide 'xt-mouse)
335
336 ;;; xt-mouse.el ends here