1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
3 ;; Copyright (C) 1994, 2000-2015 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 (defun xterm-mouse--read-utf8-char (&optional prompt seconds
)
138 "Read an utf-8 encoded character from the current terminal.
139 This function reads and returns an utf-8 encoded character of
140 command input. If the user generates an event which is not a
141 character (i.e., a mouse click or function key event), read-char
144 The returned event may come directly from the user, or from a
145 keyboard macro. It is not decoded by the keyboard's input coding
146 system and always treated with an utf-8 input encoding.
148 The optional arguments PROMPT and SECONDS work like in
150 (let ((tmp (keyboard-coding-system)))
151 (set-keyboard-coding-system 'utf-8
)
152 (prog1 (read-event prompt t seconds
)
153 (set-keyboard-coding-system tmp
))))
155 ;; In default mode, each numeric parameter of XTerm's mouse report is
156 ;; a single char, possibly encoded as utf-8. The actual numeric
157 ;; parameter then is obtained by subtracting 32 from the character
158 ;; code. In extended mode the parameters are returned as decimal
159 ;; string delimited either by semicolons or for the last parameter by
160 ;; one of the characters "m" or "M". If the last character is a "m",
161 ;; then the mouse event was a button release, else it was a button
162 ;; press or a mouse motion. Return value is a cons cell with
163 ;; (NEXT-NUMERIC-PARAMETER . LAST-CHAR)
164 (defun xterm-mouse--read-number-from-terminal (extension)
171 (setq n
(+ (* 10 n
) c
(- ?
0))))
173 (cons (- (setq c
(xterm-mouse--read-utf8-char)) 32) c
))))
175 ;; XTerm reports mouse events as
176 ;; <EVENT-CODE> <X> <Y> in default mode, and
177 ;; <EVENT-CODE> ";" <X> ";" <Y> <"M" or "m"> in extended mode.
178 ;; The macro read-number-from-terminal takes care of reading
179 ;; the response parameters appropriately. The EVENT-CODE differs
180 ;; slightly between default and extended mode.
181 ;; Return a list (EVENT-TYPE-SYMBOL X Y).
182 (defun xterm-mouse--read-event-sequence (&optional extension
)
184 ((`(,code .
,_
) (xterm-mouse--read-number-from-terminal extension
))
185 (`(,x .
,_
) (xterm-mouse--read-number-from-terminal extension
))
186 (`(,y .
,c
) (xterm-mouse--read-number-from-terminal extension
))
187 (wheel (/= (logand code
64) 0))
188 (move (/= (logand code
32) 0))
189 (ctrl (/= (logand code
16) 0))
190 (meta (/= (logand code
8) 0))
191 (shift (/= (logand code
4) 0))
192 (down (and (not wheel
)
196 (/= (logand code
3) 3))))
198 ((or extension down wheel
)
199 (+ (logand code
3) (if wheel
4 1)))
200 ;; The default mouse protocol does not report the button
201 ;; number in release events: extract the button number
202 ;; from last button-down event.
203 ((terminal-parameter nil
'xterm-mouse-last-down
)
207 (car (terminal-parameter nil
'xterm-mouse-last-down
)))
209 ;; Spurious release event without previous button-down
210 ;; event: assume, that the last button was button 1.
212 (sym (if move
'mouse-movement
213 (intern (concat (if ctrl
"C-" "")
218 (number-to-string btn
))))))
219 (list sym
(1- x
) (1- y
))))
221 (defun xterm-mouse--set-click-count (event click-count
)
222 (setcdr (cdr event
) (list click-count
))
223 (let ((name (symbol-name (car event
))))
224 (when (string-match "\\(.*?\\)\\(\\(?:down-\\)?mouse-.*\\)" name
)
226 (intern (concat (match-string 1 name
)
227 (if (= click-count
2)
229 (match-string 2 name
)))))))
231 (defun xterm-mouse-event (&optional extension
)
232 "Convert XTerm mouse event to Emacs mouse event.
233 EXTENSION, if non-nil, means to use an extension to the usual
234 terminal mouse protocol; we currently support the value 1006,
235 which is the \"1006\" extension implemented in Xterm >= 277."
236 (let ((click (cond ((memq extension
'(1006 nil
))
237 (xterm-mouse--read-event-sequence extension
))
239 (error "Unsupported XTerm mouse protocol")))))
241 (let* ((type (nth 0 click
))
244 ;; Emulate timestamp information. This is accurate enough
245 ;; for default value of mouse-1-click-follows-link (450msec).
246 (timestamp (xterm-mouse-truncate-wrap
250 (setq xt-mouse-epoch
(float-time)))))))
252 (ltrb (window-edges w
))
256 (posn-at-x-y (- x left
) (- y top
) w t
)
257 (append (list nil
'menu-bar
)
258 (nthcdr 2 (posn-at-x-y x y
)))))
259 (event (list type posn
)))
260 (setcar (nthcdr 3 posn
) timestamp
)
262 ;; Try to handle double/triple clicks.
263 (let* ((last-click (terminal-parameter nil
'xterm-mouse-last-click
))
264 (last-type (nth 0 last-click
))
265 (last-name (symbol-name last-type
))
266 (last-time (nth 1 last-click
))
267 (click-count (nth 2 last-click
))
268 (this-time (float-time))
269 (name (symbol-name type
)))
271 ((not (string-match "down-" name
))
272 ;; For up events, make the up side match the down side.
273 (setq this-time last-time
)
274 (when (and click-count
(> click-count
1)
275 (string-match "down-" last-name
)
276 (equal name
(replace-match "" t t last-name
)))
277 (xterm-mouse--set-click-count event click-count
)))
278 ((not last-time
) nil
)
279 ((and (> double-click-time
(* 1000 (- this-time last-time
)))
280 (equal last-name
(replace-match "" t t name
)))
281 (setq click-count
(1+ click-count
))
282 (xterm-mouse--set-click-count event click-count
))
283 (t (setq click-count
1)))
284 (set-terminal-parameter nil
'xterm-mouse-last-click
285 (list type this-time click-count
)))
287 (set-terminal-parameter nil
'xterm-mouse-x x
)
288 (set-terminal-parameter nil
'xterm-mouse-y y
)
289 (setq last-input-event event
)))))
292 (define-minor-mode xterm-mouse-mode
293 "Toggle XTerm mouse mode.
294 With a prefix argument ARG, enable XTerm mouse mode if ARG is
295 positive, and disable it otherwise. If called from Lisp, enable
296 the mode if ARG is omitted or nil.
298 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
299 This works in terminal emulators compatible with xterm. It only
300 works for simple uses of the mouse. Basically, only non-modified
301 single clicks are supported. When turned on, the normal xterm
302 mouse functionality for such clicks is still available by holding
303 down the SHIFT key while pressing the mouse button."
304 :global t
:group
'mouse
305 (funcall (if xterm-mouse-mode
'add-hook
'remove-hook
)
306 'terminal-init-xterm-hook
307 'turn-on-xterm-mouse-tracking-on-terminal
)
311 (setq mouse-position-function
#'xterm-mouse-position-function
)
312 (mapc #'turn-on-xterm-mouse-tracking-on-terminal
(terminal-list)))
314 (mapc #'turn-off-xterm-mouse-tracking-on-terminal
(terminal-list))
315 (setq mouse-position-function nil
)))
317 (defconst xterm-mouse-tracking-enable-sequence
318 "\e[?1000h\e[?1002h\e[?1005h\e[?1006h"
319 "Control sequence to enable xterm mouse tracking.
320 Enables basic mouse tracking, mouse motion events and finally
321 extended tracking on terminals that support it. The following
322 escape sequences are understood by modern xterms:
324 \"\\e[?1000h\" `Basic mouse mode´: Enables reports for mouse
325 clicks. There is a limit to the maximum row/column
326 position (<= 223), which can be reported in this
329 \"\\e[?1002h\" `Mouse motion mode´: Enables reports for mouse
330 motion events during dragging operations.
332 \"\\e[?1005h\" `UTF-8 coordinate extension`: Enables an extension
333 to the basic mouse mode, which uses UTF-8
334 characters to overcome the 223 row/column limit. This
335 extension may conflict with non UTF-8 applications or
338 \"\\e[?1006h\" `SGR coordinate extension´: Enables a newer
339 alternative extension to the basic mouse mode, which
340 overcomes the 223 row/column limit without the
341 drawbacks of the UTF-8 coordinate extension.
343 The two extension modes are mutually exclusive, where the last
344 given escape sequence takes precedence over the former.")
346 (defconst xterm-mouse-tracking-disable-sequence
347 "\e[?1006l\e[?1005l\e[?1002l\e[?1000l"
348 "Reset the modes set by `xterm-mouse-tracking-enable-sequence'.")
350 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal
)
351 "Enable xterm mouse tracking on TERMINAL."
352 (when (and xterm-mouse-mode
(eq t
(terminal-live-p terminal
))
353 ;; Avoid the initial terminal which is not a termcap device.
354 ;; FIXME: is there more elegant way to detect the initial
356 (not (string= (terminal-name terminal
) "initial_terminal")))
357 (unless (terminal-parameter terminal
'xterm-mouse-mode
)
358 ;; Simulate selecting a terminal by selecting one of its frames
359 ;; so that we can set the terminal-local `input-decode-map'.
360 (with-selected-frame (car (frames-on-display-list terminal
))
361 (define-key input-decode-map
"\e[M" 'xterm-mouse-translate
)
362 (define-key input-decode-map
"\e[<" 'xterm-mouse-translate-extended
))
364 (send-string-to-terminal xterm-mouse-tracking-enable-sequence
366 ;; FIXME: This should use a dedicated error signal.
367 (error (if (equal (cadr err
) "Terminal is currently suspended")
368 nil
;The sequence will be sent upon resume.
369 (signal (car err
) (cdr err
)))))
370 (push xterm-mouse-tracking-enable-sequence
371 (terminal-parameter nil
'tty-mode-set-strings
))
372 (push xterm-mouse-tracking-disable-sequence
373 (terminal-parameter nil
'tty-mode-reset-strings
))
374 (set-terminal-parameter terminal
'xterm-mouse-mode t
))))
376 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
377 "Disable xterm mouse tracking on TERMINAL."
378 ;; Only send the disable command to those terminals to which we've already
379 ;; sent the enable command.
380 (when (and (terminal-parameter terminal
'xterm-mouse-mode
)
381 (eq t
(terminal-live-p terminal
)))
382 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
383 ;; terminal parameter, but it seems less harmful to send this escape
384 ;; command too many times (or to catch an unintended key sequence), than
385 ;; to send it too few times (or to fail to let xterm-mouse events
386 ;; pass by untranslated).
388 (send-string-to-terminal xterm-mouse-tracking-disable-sequence
390 ;; FIXME: This should use a dedicated error signal.
391 (error (if (equal (cadr err
) "Terminal is currently suspended")
393 (signal (car err
) (cdr err
)))))
394 (setf (terminal-parameter nil
'tty-mode-set-strings
)
395 (remq xterm-mouse-tracking-enable-sequence
396 (terminal-parameter nil
'tty-mode-set-strings
)))
397 (setf (terminal-parameter nil
'tty-mode-reset-strings
)
398 (remq xterm-mouse-tracking-disable-sequence
399 (terminal-parameter nil
'tty-mode-reset-strings
)))
400 (set-terminal-parameter terminal
'xterm-mouse-mode nil
)))
404 ;;; xt-mouse.el ends here