Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / xt-mouse.el
blob26a07b468407c2f263e8fa3948f38bdbf0270d21
1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
3 ;; Copyright (C) 1994, 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: mouse, terminals
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Enable mouse support when running inside an xterm.
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.
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.
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.
37 ;;; Todo:
39 ;; Support multi-click -- somehow.
41 ;;; Code:
43 (defvar xterm-mouse-debug-buffer nil)
45 (defvar xterm-mouse-last)
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))
53 (defun xterm-mouse-translate (_event)
54 "Read a click and release event from XTerm."
55 (xterm-mouse-translate-1))
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))
64 (defun xterm-mouse-translate-1 (&optional extension)
65 (save-excursion
66 (save-window-excursion
67 (deactivate-mark)
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-click (string-match "^mouse" (symbol-name (car down)))))
78 ;; Retrieve the expected preface for the up-event.
79 (unless is-click
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")))
90 ;; Process the up-event.
91 (let* ((click (if is-click down (xterm-mouse-event extension)))
92 (click-data (nth 1 click))
93 (click-where (nth 1 click-data)))
94 (if (memq down-binding '(nil ignore))
95 (if (and (symbolp click-where)
96 (consp click-where))
97 (vector (list click-where click-data) click)
98 (vector click))
99 (setq unread-command-events
100 (append (if (eq down-where click-where)
101 (list click)
102 (list
103 ;; Cheat `mouse-drag-region' with move event.
104 (list 'mouse-movement click-data)
105 ;; Generate a drag event.
106 (if (symbolp down-where)
108 (list (intern (format "drag-mouse-%d"
109 (1+ xterm-mouse-last)))
110 down-data click-data))))
111 unread-command-events))
112 (if xterm-mouse-debug-buffer
113 (print unread-command-events xterm-mouse-debug-buffer))
114 (if (and (symbolp down-where)
115 (consp down-where))
116 (vector (list down-where down-data) down)
117 (vector down))))))))
119 ;; These two variables have been converted to terminal parameters.
121 ;;(defvar xterm-mouse-x 0
122 ;; "Position of last xterm mouse event relative to the frame.")
124 ;;(defvar xterm-mouse-y 0
125 ;; "Position of last xterm mouse event relative to the frame.")
127 (defvar xt-mouse-epoch nil)
129 ;; Indicator for the xterm-mouse mode.
131 (defun xterm-mouse-position-function (pos)
132 "Bound to `mouse-position-function' in XTerm mouse mode."
133 (when (terminal-parameter nil 'xterm-mouse-x)
134 (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
135 (terminal-parameter nil 'xterm-mouse-y))))
136 pos)
138 (defun xterm-mouse-truncate-wrap (f)
139 "Truncate with wrap-around."
140 (condition-case nil
141 ;; First try the built-in truncate, in case there's no overflow.
142 (truncate f)
143 ;; In case of overflow, do wraparound by hand.
144 (range-error
145 ;; In our case, we wrap around every 3 days or so, so if we assume
146 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
147 ;; Using a power of 2 makes rounding errors less likely.
148 (let* ((maxwrap (* 65536 2048))
149 (dbig (truncate (/ f maxwrap)))
150 (fdiff (- f (* 1.0 maxwrap dbig))))
151 (+ (truncate fdiff) (* maxwrap dbig))))))
153 ;; Normal terminal mouse click reporting: expect three bytes, of the
154 ;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
155 (defun xterm-mouse--read-event-sequence-1000 ()
156 (list (let ((code (- (read-event) 32)))
157 (intern
158 ;; For buttons > 3, the release-event looks differently
159 ;; (see xc/programs/xterm/button.c, function EditorButton),
160 ;; and come in a release-event only, no down-event.
161 (cond ((>= code 64)
162 (format "mouse-%d" (- code 60)))
163 ((memq code '(8 9 10))
164 (setq xterm-mouse-last code)
165 (format "M-down-mouse-%d" (- code 7)))
166 ((= code 11)
167 (format "M-mouse-%d" (- xterm-mouse-last 7)))
168 ((= code 3)
169 ;; For buttons > 5 xterm only reports a
170 ;; button-release event. Avoid error by mapping
171 ;; them all to mouse-1.
172 (format "mouse-%d" (+ 1 (or xterm-mouse-last 0))))
174 (setq xterm-mouse-last code)
175 (format "down-mouse-%d" (+ 1 code))))))
176 ;; x and y coordinates
177 (- (read-event) 33)
178 (- (read-event) 33)))
180 ;; XTerm's 1006-mode terminal mouse click reporting has the form
181 ;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
182 ;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
183 (defun xterm-mouse--read-event-sequence-1006 ()
184 (let (button-bytes x-bytes y-bytes c)
185 (while (not (eq (setq c (read-event)) ?\;))
186 (push c button-bytes))
187 (while (not (eq (setq c (read-event)) ?\;))
188 (push c x-bytes))
189 (while (not (memq (setq c (read-event)) '(?m ?M)))
190 (push c y-bytes))
191 (list (let* ((code (string-to-number
192 (apply 'string (nreverse button-bytes))))
193 (wheel (>= code 64))
194 (down (and (not wheel)
195 (eq c ?M))))
196 (intern (format "%s%smouse-%d"
197 (cond (wheel "")
198 ((< code 4) "")
199 ((< code 8) "S-")
200 ((< code 12) "M-")
201 ((< code 16) "M-S-")
202 ((< code 20) "C-")
203 ((< code 24) "C-S-")
204 ((< code 28) "C-M-")
205 ((< code 32) "C-M-S-")
207 (error "Unexpected escape sequence from XTerm")))
208 (if down "down-" "")
209 (if wheel
210 (- code 60)
211 (1+ (setq xterm-mouse-last (mod code 4)))))))
212 (1- (string-to-number (apply 'string (nreverse x-bytes))))
213 (1- (string-to-number (apply 'string (nreverse y-bytes)))))))
215 (defun xterm-mouse-event (&optional extension)
216 "Convert XTerm mouse event to Emacs mouse event.
217 EXTENSION, if non-nil, means to use an extension to the usual
218 terminal mouse protocol; we currently support the value 1006,
219 which is the \"1006\" extension implemented in Xterm >= 277."
220 (let* ((click (cond ((null extension)
221 (xterm-mouse--read-event-sequence-1000))
222 ((eq extension 1006)
223 (xterm-mouse--read-event-sequence-1006))
225 (error "Unsupported XTerm mouse protocol"))))
226 (type (nth 0 click))
227 (x (nth 1 click))
228 (y (nth 2 click))
229 ;; Emulate timestamp information. This is accurate enough
230 ;; for default value of mouse-1-click-follows-link (450msec).
231 (timestamp (xterm-mouse-truncate-wrap
232 (* 1000
233 (- (float-time)
234 (or xt-mouse-epoch
235 (setq xt-mouse-epoch (float-time)))))))
236 (w (window-at x y))
237 (ltrb (window-edges w))
238 (left (nth 0 ltrb))
239 (top (nth 1 ltrb)))
240 (set-terminal-parameter nil 'xterm-mouse-x x)
241 (set-terminal-parameter nil 'xterm-mouse-y y)
242 (setq
243 last-input-event
244 (list type
245 (let ((event (if w
246 (posn-at-x-y (- x left) (- y top) w t)
247 (append (list nil 'menu-bar)
248 (nthcdr 2 (posn-at-x-y x y))))))
249 (setcar (nthcdr 3 event) timestamp)
250 event)))))
252 ;;;###autoload
253 (define-minor-mode xterm-mouse-mode
254 "Toggle XTerm mouse mode.
255 With a prefix argument ARG, enable XTerm mouse mode if ARG is
256 positive, and disable it otherwise. If called from Lisp, enable
257 the mode if ARG is omitted or nil.
259 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
260 This works in terminal emulators compatible with xterm. It only
261 works for simple uses of the mouse. Basically, only non-modified
262 single clicks are supported. When turned on, the normal xterm
263 mouse functionality for such clicks is still available by holding
264 down the SHIFT key while pressing the mouse button."
265 :global t :group 'mouse
266 (let ((do-hook (if xterm-mouse-mode 'add-hook 'remove-hook)))
267 (funcall do-hook 'terminal-init-xterm-hook
268 'turn-on-xterm-mouse-tracking-on-terminal)
269 (funcall do-hook 'delete-terminal-functions
270 'turn-off-xterm-mouse-tracking-on-terminal)
271 (funcall do-hook 'suspend-tty-functions
272 'turn-off-xterm-mouse-tracking-on-terminal)
273 (funcall do-hook 'resume-tty-functions
274 'turn-on-xterm-mouse-tracking-on-terminal)
275 (funcall do-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
276 (funcall do-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
277 (funcall do-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking))
278 (if xterm-mouse-mode
279 ;; Turn it on
280 (progn
281 (setq mouse-position-function #'xterm-mouse-position-function)
282 (turn-on-xterm-mouse-tracking))
283 ;; Turn it off
284 (turn-off-xterm-mouse-tracking 'force)
285 (setq mouse-position-function nil)))
287 (defun turn-on-xterm-mouse-tracking ()
288 "Enable Emacs mouse tracking in xterm."
289 (dolist (terminal (terminal-list))
290 (turn-on-xterm-mouse-tracking-on-terminal terminal)))
292 (defun turn-off-xterm-mouse-tracking (&optional _force)
293 "Disable Emacs mouse tracking in xterm."
294 (dolist (terminal (terminal-list))
295 (turn-off-xterm-mouse-tracking-on-terminal terminal)))
297 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal)
298 "Enable xterm mouse tracking on TERMINAL."
299 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal))
300 ;; Avoid the initial terminal which is not a termcap device.
301 ;; FIXME: is there more elegant way to detect the initial terminal?
302 (not (string= (terminal-name terminal) "initial_terminal")))
303 (unless (terminal-parameter terminal 'xterm-mouse-mode)
304 ;; Simulate selecting a terminal by selecting one of its frames
305 (with-selected-frame (car (frames-on-display-list terminal))
306 (define-key input-decode-map "\e[M" 'xterm-mouse-translate)
307 (define-key input-decode-map "\e[<" 'xterm-mouse-translate-extended))
308 (set-terminal-parameter terminal 'xterm-mouse-mode t))
309 (send-string-to-terminal "\e[?1000h" terminal)
310 ;; Request extended mouse support, if available (xterm >= 277).
311 (send-string-to-terminal "\e[?1006h" terminal)))
313 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
314 "Disable xterm mouse tracking on TERMINAL."
315 ;; Only send the disable command to those terminals to which we've already
316 ;; sent the enable command.
317 (when (and (terminal-parameter terminal 'xterm-mouse-mode)
318 (eq t (terminal-live-p terminal))
319 ;; Avoid the initial terminal which is not a termcap device.
320 ;; FIXME: is there more elegant way to detect the initial terminal?
321 (not (string= (terminal-name terminal) "initial_terminal")))
322 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
323 ;; terminal parameter, but it seems less harmful to send this escape
324 ;; command too many times (or to catch an unintended key sequence), than
325 ;; to send it too few times (or to fail to let xterm-mouse events
326 ;; pass by untranslated).
327 (send-string-to-terminal "\e[?1000l" terminal)
328 (send-string-to-terminal "\e[?1006l" terminal)))
330 (provide 'xt-mouse)
332 ;;; xt-mouse.el ends here