1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1994, 2000-2016 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/>.
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.
39 ;; Support multi-click -- somehow.
43 (defvar xterm-mouse-debug-buffer nil
)
45 (defun xterm-mouse-translate (_event)
46 "Read a click and release event from XTerm."
47 (xterm-mouse-translate-1))
49 (defun xterm-mouse-translate-extended (_event)
50 "Read a click and release event from XTerm.
51 Similar to `xterm-mouse-translate', but using the \"1006\"
52 extension, which supports coordinates >= 231 (see
53 http://invisible-island.net/xterm/ctlseqs/ctlseqs.html)."
54 (xterm-mouse-translate-1 1006))
56 (defun xterm-mouse-translate-1 (&optional extension
)
58 (let* ((event (xterm-mouse-event extension
))
59 (ev-command (nth 0 event
))
60 (ev-data (nth 1 event
))
61 (ev-where (nth 1 ev-data
))
63 (is-move (eq 'mouse-movement ev-command
))
64 (is-down (string-match "down-" (symbol-name ev-command
))))
66 ;; Mouse events symbols must have an 'event-kind property with
67 ;; the value 'mouse-click.
68 (when ev-command
(put ev-command
'event-kind
'mouse-click
))
71 ((null event
) nil
) ;Unknown/bogus byte sequence!
73 (setf (terminal-parameter nil
'xterm-mouse-last-down
) event
)
77 (let* ((down (terminal-parameter nil
'xterm-mouse-last-down
))
78 (down-data (nth 1 down
))
79 (down-where (nth 1 down-data
)))
80 (setf (terminal-parameter nil
'xterm-mouse-last-down
) nil
)
83 ;; This is an "up-only" event. Pretend there was an up-event
84 ;; right before and keep the up-event for later.
85 (push event unread-command-events
)
86 (vector (cons (intern (replace-regexp-in-string
87 "\\`\\([ACMHSs]-\\)*" "\\&down-"
88 (symbol-name ev-command
) t
))
90 ((equal ev-where down-where
) vec
)
92 (let ((drag (if (symbolp ev-where
)
94 (list (intern (replace-regexp-in-string
95 "\\`\\([ACMHSs]-\\)*" "\\&drag-"
96 (symbol-name ev-command
) t
))
98 (if (null track-mouse
)
100 (push drag unread-command-events
)
101 (vector (list 'mouse-movement ev-data
))))))))))))
103 ;; These two variables have been converted to terminal parameters.
105 ;;(defvar xterm-mouse-x 0
106 ;; "Position of last xterm mouse event relative to the frame.")
108 ;;(defvar xterm-mouse-y 0
109 ;; "Position of last xterm mouse event relative to the frame.")
111 (defvar xt-mouse-epoch nil
)
113 ;; Indicator for the xterm-mouse mode.
115 (defun xterm-mouse-position-function (pos)
116 "Bound to `mouse-position-function' in XTerm mouse mode."
117 (when (terminal-parameter nil
'xterm-mouse-x
)
118 (setcdr pos
(cons (terminal-parameter nil
'xterm-mouse-x
)
119 (terminal-parameter nil
'xterm-mouse-y
))))
122 (defun xterm-mouse-truncate-wrap (f)
123 "Truncate with wrap-around."
125 ;; First try the built-in truncate, in case there's no overflow.
127 ;; In case of overflow, do wraparound by hand.
129 ;; In our case, we wrap around every 3 days or so, so if we assume
130 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
131 ;; Using a power of 2 makes rounding errors less likely.
132 (let* ((maxwrap (* 65536 2048))
133 (dbig (truncate (/ f maxwrap
)))
134 (fdiff (- f
(* 1.0 maxwrap dbig
))))
135 (+ (truncate fdiff
) (* maxwrap dbig
))))))
137 (defcustom xterm-mouse-utf-8 nil
138 "Non-nil if UTF-8 coordinates should be used to read mouse coordinates.
139 Set this to non-nil if you are sure that your terminal
140 understands UTF-8 coordinates, but not SGR coordinates."
146 (defun xterm-mouse--read-coordinate ()
147 "Read a mouse coordinate from the current terminal.
148 If `xterm-mouse-utf-8' was non-nil when
149 `turn-on-xterm-mouse-tracking-on-terminal' was called, reads the
150 coordinate as an UTF-8 code unit sequence; otherwise, reads a
152 (let ((previous-keyboard-coding-system (keyboard-coding-system)))
155 (set-keyboard-coding-system
156 (if (terminal-parameter nil
'xterm-mouse-utf-8
)
159 ;; Wait only a little; we assume that the entire escape sequence
160 ;; has already been sent when this function is called.
161 (read-char nil nil
0.1))
162 (set-keyboard-coding-system previous-keyboard-coding-system
))))
164 ;; In default mode, each numeric parameter of XTerm's mouse report is
165 ;; a single char, possibly encoded as utf-8. The actual numeric
166 ;; parameter then is obtained by subtracting 32 from the character
167 ;; code. In extended mode the parameters are returned as decimal
168 ;; string delimited either by semicolons or for the last parameter by
169 ;; one of the characters "m" or "M". If the last character is a "m",
170 ;; then the mouse event was a button release, else it was a button
171 ;; press or a mouse motion. Return value is a cons cell with
172 ;; (NEXT-NUMERIC-PARAMETER . LAST-CHAR)
173 (defun xterm-mouse--read-number-from-terminal (extension)
180 (setq n
(+ (* 10 n
) c
(- ?
0))))
182 (cons (- (setq c
(xterm-mouse--read-coordinate)) 32) c
))))
184 ;; XTerm reports mouse events as
185 ;; <EVENT-CODE> <X> <Y> in default mode, and
186 ;; <EVENT-CODE> ";" <X> ";" <Y> <"M" or "m"> in extended mode.
187 ;; The macro read-number-from-terminal takes care of reading
188 ;; the response parameters appropriately. The EVENT-CODE differs
189 ;; slightly between default and extended mode.
190 ;; Return a list (EVENT-TYPE-SYMBOL X Y).
191 (defun xterm-mouse--read-event-sequence (&optional extension
)
193 ((`(,code .
,_
) (xterm-mouse--read-number-from-terminal extension
))
194 (`(,x .
,_
) (xterm-mouse--read-number-from-terminal extension
))
195 (`(,y .
,c
) (xterm-mouse--read-number-from-terminal extension
))
196 (wheel (/= (logand code
64) 0))
197 (move (/= (logand code
32) 0))
198 (ctrl (/= (logand code
16) 0))
199 (meta (/= (logand code
8) 0))
200 (shift (/= (logand code
4) 0))
201 (down (and (not wheel
)
205 (/= (logand code
3) 3))))
207 ((or extension down wheel
)
208 (+ (logand code
3) (if wheel
4 1)))
209 ;; The default mouse protocol does not report the button
210 ;; number in release events: extract the button number
211 ;; from last button-down event.
212 ((terminal-parameter nil
'xterm-mouse-last-down
)
216 (car (terminal-parameter nil
'xterm-mouse-last-down
)))
218 ;; Spurious release event without previous button-down
219 ;; event: assume, that the last button was button 1.
221 (sym (if move
'mouse-movement
222 (intern (concat (if ctrl
"C-" "")
227 (number-to-string btn
))))))
228 (list sym
(1- x
) (1- y
))))
230 (defun xterm-mouse--set-click-count (event click-count
)
231 (setcdr (cdr event
) (list click-count
))
232 (let ((name (symbol-name (car event
))))
233 (when (string-match "\\(.*?\\)\\(\\(?:down-\\)?mouse-.*\\)" name
)
235 (intern (concat (match-string 1 name
)
236 (if (= click-count
2)
238 (match-string 2 name
)))))))
240 (defun xterm-mouse-event (&optional extension
)
241 "Convert XTerm mouse event to Emacs mouse event.
242 EXTENSION, if non-nil, means to use an extension to the usual
243 terminal mouse protocol; we currently support the value 1006,
244 which is the \"1006\" extension implemented in Xterm >= 277."
245 (let ((click (cond ((memq extension
'(1006 nil
))
246 (xterm-mouse--read-event-sequence extension
))
248 (error "Unsupported XTerm mouse protocol")))))
250 (let* ((type (nth 0 click
))
253 ;; Emulate timestamp information. This is accurate enough
254 ;; for default value of mouse-1-click-follows-link (450msec).
255 (timestamp (xterm-mouse-truncate-wrap
259 (setq xt-mouse-epoch
(float-time)))))))
261 (ltrb (window-edges w
))
265 (posn-at-x-y (- x left
) (- y top
) w t
)
266 (append (list nil
'menu-bar
)
267 (nthcdr 2 (posn-at-x-y x y
)))))
268 (event (list type posn
)))
269 (setcar (nthcdr 3 posn
) timestamp
)
271 ;; Try to handle double/triple clicks.
272 (let* ((last-click (terminal-parameter nil
'xterm-mouse-last-click
))
273 (last-type (nth 0 last-click
))
274 (last-name (symbol-name last-type
))
275 (last-time (nth 1 last-click
))
276 (click-count (nth 2 last-click
))
277 (this-time (float-time))
278 (name (symbol-name type
)))
280 ((not (string-match "down-" name
))
281 ;; For up events, make the up side match the down side.
282 (setq this-time last-time
)
283 (when (and click-count
(> click-count
1)
284 (string-match "down-" last-name
)
285 (equal name
(replace-match "" t t last-name
)))
286 (xterm-mouse--set-click-count event click-count
)))
287 ((not last-time
) nil
)
288 ((and (> double-click-time
(* 1000 (- this-time last-time
)))
289 (equal last-name
(replace-match "" t t name
)))
290 (setq click-count
(1+ click-count
))
291 (xterm-mouse--set-click-count event click-count
))
292 (t (setq click-count
1)))
293 (set-terminal-parameter nil
'xterm-mouse-last-click
294 (list type this-time click-count
)))
296 (set-terminal-parameter nil
'xterm-mouse-x x
)
297 (set-terminal-parameter nil
'xterm-mouse-y y
)
298 (setq last-input-event event
)))))
301 (define-minor-mode xterm-mouse-mode
302 "Toggle XTerm mouse mode.
303 With a prefix argument ARG, enable XTerm mouse mode if ARG is
304 positive, and disable it otherwise. If called from Lisp, enable
305 the mode if ARG is omitted or nil.
307 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
308 This works in terminal emulators compatible with xterm. It only
309 works for simple uses of the mouse. Basically, only non-modified
310 single clicks are supported. When turned on, the normal xterm
311 mouse functionality for such clicks is still available by holding
312 down the SHIFT key while pressing the mouse button."
313 :global t
:group
'mouse
314 (funcall (if xterm-mouse-mode
'add-hook
'remove-hook
)
315 'terminal-init-xterm-hook
316 'turn-on-xterm-mouse-tracking-on-terminal
)
320 (setq mouse-position-function
#'xterm-mouse-position-function
)
321 (mapc #'turn-on-xterm-mouse-tracking-on-terminal
(terminal-list)))
323 (mapc #'turn-off-xterm-mouse-tracking-on-terminal
(terminal-list))
324 (setq mouse-position-function nil
)))
326 (defun xterm-mouse-tracking-enable-sequence ()
327 "Return a control sequence to enable XTerm mouse tracking.
328 The returned control sequence enables basic mouse tracking, mouse
329 motion events and finally extended tracking on terminals that
330 support it. The following escape sequences are understood by
333 \"\\e[?1000h\" \"Basic mouse mode\": Enables reports for mouse
334 clicks. There is a limit to the maximum row/column
335 position (<= 223), which can be reported in this
338 \"\\e[?1002h\" \"Mouse motion mode\": Enables reports for mouse
339 motion events during dragging operations.
341 \"\\e[?1005h\" \"UTF-8 coordinate extension\": Enables an
342 extension to the basic mouse mode, which uses UTF-8
343 characters to overcome the 223 row/column limit.
344 This extension may conflict with non UTF-8
345 applications or non UTF-8 locales. It is only
346 enabled when the option `xterm-mouse-utf-8' is
349 \"\\e[?1006h\" \"SGR coordinate extension\": Enables a newer
350 alternative extension to the basic mouse mode, which
351 overcomes the 223 row/column limit without the
352 drawbacks of the UTF-8 coordinate extension.
354 The two extension modes are mutually exclusive, where the last
355 given escape sequence takes precedence over the former."
356 (apply #'concat
(xterm-mouse--tracking-sequence ?h
)))
358 (defconst xterm-mouse-tracking-enable-sequence
359 "\e[?1000h\e[?1002h\e[?1005h\e[?1006h"
360 "Control sequence to enable xterm mouse tracking.
361 Enables basic mouse tracking, mouse motion events and finally
362 extended tracking on terminals that support it. The following
363 escape sequences are understood by modern xterms:
365 \"\\e[?1000h\" \"Basic mouse mode\": Enables reports for mouse
366 clicks. There is a limit to the maximum row/column
367 position (<= 223), which can be reported in this
370 \"\\e[?1002h\" \"Mouse motion mode\": Enables reports for mouse
371 motion events during dragging operations.
373 \"\\e[?1005h\" \"UTF-8 coordinate extension\": Enables an extension
374 to the basic mouse mode, which uses UTF-8
375 characters to overcome the 223 row/column limit. This
376 extension may conflict with non UTF-8 applications or
379 \"\\e[?1006h\" \"SGR coordinate extension\": Enables a newer
380 alternative extension to the basic mouse mode, which
381 overcomes the 223 row/column limit without the
382 drawbacks of the UTF-8 coordinate extension.
384 The two extension modes are mutually exclusive, where the last
385 given escape sequence takes precedence over the former.")
387 (make-obsolete-variable
388 'xterm-mouse-tracking-enable-sequence
389 "use the function `xterm-mouse-tracking-enable-sequence' instead."
392 (defun xterm-mouse-tracking-disable-sequence ()
393 "Return a control sequence to disable XTerm mouse tracking.
394 The control sequence resets the modes set by
395 `xterm-mouse-tracking-enable-sequence'."
396 (apply #'concat
(nreverse (xterm-mouse--tracking-sequence ?l
))))
398 (defconst xterm-mouse-tracking-disable-sequence
399 "\e[?1006l\e[?1005l\e[?1002l\e[?1000l"
400 "Reset the modes set by `xterm-mouse-tracking-enable-sequence'.")
402 (make-obsolete-variable
403 'xterm-mouse-tracking-disable-sequence
404 "use the function `xterm-mouse-tracking-disable-sequence' instead."
407 (defun xterm-mouse--tracking-sequence (suffix)
408 "Return a control sequence to enable or disable mouse tracking.
409 SUFFIX is the last character of each escape sequence (?h to
410 enable, ?l to disable)."
412 (lambda (code) (format "\e[?%d%c" code suffix
))
413 `(1000 1002 ,@(when xterm-mouse-utf-8
'(1005)) 1006)))
415 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal
)
416 "Enable xterm mouse tracking on TERMINAL."
417 (when (and xterm-mouse-mode
(eq t
(terminal-live-p terminal
))
418 ;; Avoid the initial terminal which is not a termcap device.
419 ;; FIXME: is there more elegant way to detect the initial
421 (not (string= (terminal-name terminal
) "initial_terminal")))
422 (unless (terminal-parameter terminal
'xterm-mouse-mode
)
423 ;; Simulate selecting a terminal by selecting one of its frames
424 ;; so that we can set the terminal-local `input-decode-map'.
425 (with-selected-frame (car (frames-on-display-list terminal
))
426 (define-key input-decode-map
"\e[M" 'xterm-mouse-translate
)
427 (define-key input-decode-map
"\e[<" 'xterm-mouse-translate-extended
))
428 (let ((enable (xterm-mouse-tracking-enable-sequence))
429 (disable (xterm-mouse-tracking-disable-sequence)))
431 (send-string-to-terminal enable terminal
)
432 ;; FIXME: This should use a dedicated error signal.
433 (error (if (equal (cadr err
) "Terminal is currently suspended")
434 nil
; The sequence will be sent upon resume.
435 (signal (car err
) (cdr err
)))))
436 (push enable
(terminal-parameter nil
'tty-mode-set-strings
))
437 (push disable
(terminal-parameter nil
'tty-mode-reset-strings
))
438 (set-terminal-parameter terminal
'xterm-mouse-mode t
)
439 (set-terminal-parameter terminal
'xterm-mouse-utf-8
440 xterm-mouse-utf-8
)))))
442 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
443 "Disable xterm mouse tracking on TERMINAL."
444 ;; Only send the disable command to those terminals to which we've already
445 ;; sent the enable command.
446 (when (and (terminal-parameter terminal
'xterm-mouse-mode
)
447 (eq t
(terminal-live-p terminal
)))
448 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
449 ;; terminal parameter, but it seems less harmful to send this escape
450 ;; command too many times (or to catch an unintended key sequence), than
451 ;; to send it too few times (or to fail to let xterm-mouse events
452 ;; pass by untranslated).
454 (send-string-to-terminal xterm-mouse-tracking-disable-sequence
456 ;; FIXME: This should use a dedicated error signal.
457 (error (if (equal (cadr err
) "Terminal is currently suspended")
459 (signal (car err
) (cdr err
)))))
460 (setf (terminal-parameter nil
'tty-mode-set-strings
)
461 (remq xterm-mouse-tracking-enable-sequence
462 (terminal-parameter nil
'tty-mode-set-strings
)))
463 (setf (terminal-parameter nil
'tty-mode-reset-strings
)
464 (remq xterm-mouse-tracking-disable-sequence
465 (terminal-parameter nil
'tty-mode-reset-strings
)))
466 (set-terminal-parameter terminal
'xterm-mouse-mode nil
)))
470 ;;; xt-mouse.el ends here