1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
3 ;; Copyright (C) 1994, 2000-2012 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 (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
)
66 (save-window-excursion
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.
80 (unless (cond ((null extension
)
81 (and (eq (read-event) ?\e
)
83 (eq (read-event) ?M
)))
85 (and (eq (read-event) ?\e
)
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
)
97 (vector (list click-where click-data
) click
)
99 (setq unread-command-events
100 (append (if (eq down-where click-where
)
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
)
116 (vector (list down-where down-data
) 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
))))
138 ;; Read XTerm sequences above ASCII 127 (#x7f)
139 (defun xterm-mouse-event-read ()
140 ;; We get the characters decoded by the keyboard coding system. Try
141 ;; to recover the raw character.
142 (let ((c (read-event)))
143 (cond ;; If meta-flag is t we get a meta character
145 (- c
(- ?\M-\^
@ 128)))
146 ;; Reencode the character in the keyboard coding system, if
147 ;; this is a non-ASCII character.
149 (aref (encode-coding-string (string c
) (keyboard-coding-system)) 0))
152 (defun xterm-mouse-truncate-wrap (f)
153 "Truncate with wrap-around."
155 ;; First try the built-in truncate, in case there's no overflow.
157 ;; In case of overflow, do wraparound by hand.
159 ;; In our case, we wrap around every 3 days or so, so if we assume
160 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
161 ;; Using a power of 2 makes rounding errors less likely.
162 (let* ((maxwrap (* 65536 2048))
163 (dbig (truncate (/ f maxwrap
)))
164 (fdiff (- f
(* 1.0 maxwrap dbig
))))
165 (+ (truncate fdiff
) (* maxwrap dbig
))))))
167 ;; Normal terminal mouse click reporting: expect three bytes, of the
168 ;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
169 (defun xterm-mouse--read-event-sequence-1000 ()
170 (list (let ((code (- (xterm-mouse-event-read) 32)))
172 ;; For buttons > 3, the release-event looks differently
173 ;; (see xc/programs/xterm/button.c, function EditorButton),
174 ;; and come in a release-event only, no down-event.
176 (format "mouse-%d" (- code
60)))
177 ((memq code
'(8 9 10))
178 (setq xterm-mouse-last code
)
179 (format "M-down-mouse-%d" (- code
7)))
181 (format "M-mouse-%d" (- xterm-mouse-last
7)))
183 ;; For buttons > 5 xterm only reports a
184 ;; button-release event. Avoid error by mapping
185 ;; them all to mouse-1.
186 (format "mouse-%d" (+ 1 (or xterm-mouse-last
0))))
188 (setq xterm-mouse-last code
)
189 (format "down-mouse-%d" (+ 1 code
))))))
190 ;; x and y coordinates
191 (- (xterm-mouse-event-read) 33)
192 (- (xterm-mouse-event-read) 33)))
194 ;; XTerm's 1006-mode terminal mouse click reporting has the form
195 ;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
196 ;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
197 (defun xterm-mouse--read-event-sequence-1006 ()
198 (let (button-bytes x-bytes y-bytes c
)
199 (while (not (eq (setq c
(xterm-mouse-event-read)) ?\
;))
200 (push c button-bytes
))
201 (while (not (eq (setq c
(xterm-mouse-event-read)) ?\
;))
203 (while (not (memq (setq c
(xterm-mouse-event-read)) '(?m ?M
)))
205 (list (let* ((code (string-to-number
206 (apply 'string
(nreverse button-bytes
))))
208 (down (and (not wheel
)
210 (intern (format "%s%smouse-%d"
219 ((< code
32) "C-M-S-")
221 (error "Unexpected escape sequence from XTerm")))
225 (1+ (setq xterm-mouse-last
(mod code
4)))))))
226 (1- (string-to-number (apply 'string
(nreverse x-bytes
))))
227 (1- (string-to-number (apply 'string
(nreverse y-bytes
)))))))
229 (defun xterm-mouse-event (&optional extension
)
230 "Convert XTerm mouse event to Emacs mouse event.
231 EXTENSION, if non-nil, means to use an extension to the usual
232 terminal mouse protocol; we currently support the value 1006,
233 which is the \"1006\" extension implemented in Xterm >= 277."
234 (let* ((click (cond ((null extension
)
235 (xterm-mouse--read-event-sequence-1000))
237 (xterm-mouse--read-event-sequence-1006))
239 (error "Unsupported XTerm mouse protocol"))))
243 ;; Emulate timestamp information. This is accurate enough
244 ;; for default value of mouse-1-click-follows-link (450msec).
245 (timestamp (xterm-mouse-truncate-wrap
249 (setq xt-mouse-epoch
(float-time)))))))
251 (ltrb (window-edges w
))
254 (set-terminal-parameter nil
'xterm-mouse-x x
)
255 (set-terminal-parameter nil
'xterm-mouse-y y
)
260 (posn-at-x-y (- x left
) (- y top
) w t
)
261 (append (list nil
'menu-bar
)
262 (nthcdr 2 (posn-at-x-y x y
))))))
263 (setcar (nthcdr 3 event
) timestamp
)
267 (define-minor-mode xterm-mouse-mode
268 "Toggle XTerm mouse mode.
269 With a prefix argument ARG, enable XTerm mouse mode if ARG is
270 positive, and disable it otherwise. If called from Lisp, enable
271 the mode if ARG is omitted or nil.
273 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
274 This works in terminal emulators compatible with xterm. It only
275 works for simple uses of the mouse. Basically, only non-modified
276 single clicks are supported. When turned on, the normal xterm
277 mouse functionality for such clicks is still available by holding
278 down the SHIFT key while pressing the mouse button."
279 :global t
:group
'mouse
280 (let ((do-hook (if xterm-mouse-mode
'add-hook
'remove-hook
)))
281 (funcall do-hook
'terminal-init-xterm-hook
282 'turn-on-xterm-mouse-tracking-on-terminal
)
283 (funcall do-hook
'delete-terminal-functions
284 'turn-off-xterm-mouse-tracking-on-terminal
)
285 (funcall do-hook
'suspend-tty-functions
286 'turn-off-xterm-mouse-tracking-on-terminal
)
287 (funcall do-hook
'resume-tty-functions
288 'turn-on-xterm-mouse-tracking-on-terminal
)
289 (funcall do-hook
'suspend-hook
'turn-off-xterm-mouse-tracking
)
290 (funcall do-hook
'suspend-resume-hook
'turn-on-xterm-mouse-tracking
)
291 (funcall do-hook
'kill-emacs-hook
'turn-off-xterm-mouse-tracking
))
295 (setq mouse-position-function
#'xterm-mouse-position-function
)
296 (turn-on-xterm-mouse-tracking))
298 (turn-off-xterm-mouse-tracking 'force
)
299 (setq mouse-position-function nil
)))
301 (defun turn-on-xterm-mouse-tracking ()
302 "Enable Emacs mouse tracking in xterm."
303 (dolist (terminal (terminal-list))
304 (turn-on-xterm-mouse-tracking-on-terminal terminal
)))
306 (defun turn-off-xterm-mouse-tracking (&optional _force
)
307 "Disable Emacs mouse tracking in xterm."
308 (dolist (terminal (terminal-list))
309 (turn-off-xterm-mouse-tracking-on-terminal terminal
)))
311 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal
)
312 "Enable xterm mouse tracking on TERMINAL."
313 (when (and xterm-mouse-mode
(eq t
(terminal-live-p terminal
))
314 ;; Avoid the initial terminal which is not a termcap device.
315 ;; FIXME: is there more elegant way to detect the initial terminal?
316 (not (string= (terminal-name terminal
) "initial_terminal")))
317 (unless (terminal-parameter terminal
'xterm-mouse-mode
)
318 ;; Simulate selecting a terminal by selecting one of its frames
319 (with-selected-frame (car (frames-on-display-list terminal
))
320 (define-key input-decode-map
"\e[M" 'xterm-mouse-translate
)
321 (define-key input-decode-map
"\e[<" 'xterm-mouse-translate-extended
))
322 (set-terminal-parameter terminal
'xterm-mouse-mode t
))
323 (send-string-to-terminal "\e[?1000h" terminal
)
324 ;; Request extended mouse support, if available (xterm >= 277).
325 (send-string-to-terminal "\e[?1006h" terminal
)))
327 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
328 "Disable xterm mouse tracking on TERMINAL."
329 ;; Only send the disable command to those terminals to which we've already
330 ;; sent the enable command.
331 (when (and (terminal-parameter terminal
'xterm-mouse-mode
)
332 (eq t
(terminal-live-p terminal
))
333 ;; Avoid the initial terminal which is not a termcap device.
334 ;; FIXME: is there more elegant way to detect the initial terminal?
335 (not (string= (terminal-name terminal
) "initial_terminal")))
336 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
337 ;; terminal parameter, but it seems less harmful to send this escape
338 ;; command too many times (or to catch an unintended key sequence), than
339 ;; to send it too few times (or to fail to let xterm-mouse events
340 ;; pass by untranslated).
341 (send-string-to-terminal "\e[?1000l" terminal
)
342 (send-string-to-terminal "\e[?1006l" terminal
)))
346 ;;; xt-mouse.el ends here