Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; xt-mouse.el --- support the mouse when emacs run in an xterm |
b578f267 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1994, 2000-2012 Free Software Foundation, Inc. |
fe3acbd4 | 4 | |
3efc86ef | 5 | ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> |
fe3acbd4 RS |
6 | ;; Keywords: mouse, terminals |
7 | ||
b578f267 EN |
8 | ;; This file is part of GNU Emacs. |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
fe3acbd4 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
b578f267 EN |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
fe3acbd4 RS |
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. | |
b578f267 | 19 | |
fe3acbd4 | 20 | ;; You should have received a copy of the GNU General Public License |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
fe3acbd4 | 22 | |
b2497d2e | 23 | ;;; Commentary: |
fe3acbd4 | 24 | |
30598da5 | 25 | ;; Enable mouse support when running inside an xterm. |
fe3acbd4 RS |
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 | ||
387a2345 RS |
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 | ||
fe3acbd4 RS |
37 | ;;; Todo: |
38 | ||
39 | ;; Support multi-click -- somehow. | |
40 | ||
b2497d2e | 41 | ;;; Code: |
fe3acbd4 | 42 | |
315c417c NR |
43 | (defvar xterm-mouse-debug-buffer nil) |
44 | ||
599431ab RS |
45 | (defvar xterm-mouse-last) |
46 | ||
bf31a093 NR |
47 | ;; Mouse events symbols must have an 'event-kind property with |
48 | ;; the value 'mouse-click. | |
ad15832e | 49 | (dolist (event-type '(mouse-1 mouse-2 mouse-3 |
63408057 | 50 | M-down-mouse-1 M-down-mouse-2 M-down-mouse-3)) |
bf31a093 NR |
51 | (put event-type 'event-kind 'mouse-click)) |
52 | ||
06b60517 | 53 | (defun xterm-mouse-translate (_event) |
b2497d2e | 54 | "Read a click and release event from XTerm." |
63408057 CY |
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) | |
fe3acbd4 RS |
65 | (save-excursion |
66 | (save-window-excursion | |
67 | (deactivate-mark) | |
63408057 CY |
68 | (let* ((xterm-mouse-last nil) |
69 | (down (xterm-mouse-event extension)) | |
cec01ccb | 70 | (down-command (nth 0 down)) |
63408057 CY |
71 | (down-data (nth 1 down)) |
72 | (down-where (nth 1 down-data)) | |
cec01ccb RS |
73 | (down-binding (key-binding (if (symbolp down-where) |
74 | (vector down-where down-command) | |
4b09e331 GM |
75 | (vector down-command)))) |
76 | (is-click (string-match "^mouse" (symbol-name (car down))))) | |
f1180544 | 77 | |
63408057 | 78 | ;; Retrieve the expected preface for the up-event. |
4b09e331 | 79 | (unless is-click |
63408057 CY |
80 | (unless (cond ((null extension) |
81 | (and (eq (read-char) ?\e) | |
82 | (eq (read-char) ?\[) | |
83 | (eq (read-char) ?M))) | |
84 | ((eq extension 1006) | |
85 | (and (eq (read-char) ?\e) | |
86 | (eq (read-char) ?\[) | |
87 | (eq (read-char) ?<)))) | |
4b09e331 GM |
88 | (error "Unexpected escape sequence from XTerm"))) |
89 | ||
63408057 CY |
90 | ;; Process the up-event. |
91 | (let* ((click (if is-click down (xterm-mouse-event extension))) | |
92 | (click-data (nth 1 click)) | |
cec01ccb RS |
93 | (click-where (nth 1 click-data))) |
94 | (if (memq down-binding '(nil ignore)) | |
95 | (if (and (symbolp click-where) | |
bf31a093 | 96 | (consp click-where)) |
cec01ccb RS |
97 | (vector (list click-where click-data) click) |
98 | (vector click)) | |
99 | (setq unread-command-events | |
63408057 CY |
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) | |
107 | 0 | |
108 | (list (intern (format "drag-mouse-%d" | |
109 | (1+ xterm-mouse-last))) | |
110 | down-data click-data)))) | |
111 | unread-command-events)) | |
315c417c NR |
112 | (if xterm-mouse-debug-buffer |
113 | (print unread-command-events xterm-mouse-debug-buffer)) | |
cec01ccb | 114 | (if (and (symbolp down-where) |
bf31a093 | 115 | (consp down-where)) |
cec01ccb RS |
116 | (vector (list down-where down-data) down) |
117 | (vector down)))))))) | |
118 | ||
6bac1616 KL |
119 | ;; These two variables have been converted to terminal parameters. |
120 | ;; | |
121 | ;;(defvar xterm-mouse-x 0 | |
122 | ;; "Position of last xterm mouse event relative to the frame.") | |
123 | ;; | |
124 | ;;(defvar xterm-mouse-y 0 | |
125 | ;; "Position of last xterm mouse event relative to the frame.") | |
cec01ccb | 126 | |
a7dc2df1 NR |
127 | (defvar xt-mouse-epoch nil) |
128 | ||
54eba353 | 129 | ;; Indicator for the xterm-mouse mode. |
54eba353 | 130 | |
b2497d2e DL |
131 | (defun xterm-mouse-position-function (pos) |
132 | "Bound to `mouse-position-function' in XTerm mouse mode." | |
6bac1616 KL |
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)))) | |
b2497d2e | 136 | pos) |
fe3acbd4 | 137 | |
63408057 | 138 | ;; Read XTerm sequences above ASCII 127 (#x7f) |
a9772fb4 | 139 | (defun xterm-mouse-event-read () |
29bbcfa7 AS |
140 | ;; We get the characters decoded by the keyboard coding system. Try |
141 | ;; to recover the raw character. | |
a9772fb4 | 142 | (let ((c (read-char))) |
29bbcfa7 AS |
143 | (cond ;; If meta-flag is t we get a meta character |
144 | ((>= c ?\M-\^@) | |
145 | (- c (- ?\M-\^@ 128))) | |
146 | ;; Reencode the character in the keyboard coding system, if | |
147 | ;; this is a non-ASCII character. | |
148 | ((>= c #x80) | |
149 | (aref (encode-coding-string (string c) (keyboard-coding-system)) 0)) | |
150 | (t c)))) | |
a9772fb4 | 151 | |
6ab93c85 SM |
152 | (defun xterm-mouse-truncate-wrap (f) |
153 | "Truncate with wrap-around." | |
154 | (condition-case nil | |
155 | ;; First try the built-in truncate, in case there's no overflow. | |
156 | (truncate f) | |
157 | ;; In case of overflow, do wraparound by hand. | |
158 | (range-error | |
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)))))) | |
166 | ||
63408057 CY |
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))) | |
171 | (intern | |
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. | |
175 | (cond ((>= code 64) | |
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))) | |
180 | ((= code 11) | |
181 | (format "M-mouse-%d" (- xterm-mouse-last 7))) | |
182 | ((= code 3) | |
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)))) | |
187 | (t | |
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))) | |
193 | ||
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)) ?\;)) | |
202 | (push c x-bytes)) | |
203 | (while (not (memq (setq c (xterm-mouse-event-read)) '(?m ?M))) | |
204 | (push c y-bytes)) | |
205 | (list (let* ((code (string-to-number | |
206 | (apply 'string (nreverse button-bytes)))) | |
207 | (wheel (>= code 64)) | |
208 | (down (and (not wheel) | |
209 | (eq c ?M)))) | |
210 | (intern (format "%s%smouse-%d" | |
211 | (cond (wheel "") | |
212 | ((< code 4) "") | |
213 | ((< code 8) "S-") | |
214 | ((< code 12) "M-") | |
215 | ((< code 16) "M-S-") | |
216 | ((< code 20) "C-") | |
217 | ((< code 24) "C-S-") | |
218 | ((< code 28) "C-M-") | |
219 | ((< code 32) "C-M-S-") | |
220 | (t | |
221 | (error "Unexpected escape sequence from XTerm"))) | |
222 | (if down "down-" "") | |
223 | (if wheel | |
224 | (- code 60) | |
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))))))) | |
228 | ||
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)) | |
236 | ((eq extension 1006) | |
237 | (xterm-mouse--read-event-sequence-1006)) | |
238 | (t | |
239 | (error "Unsupported XTerm mouse protocol")))) | |
240 | (type (nth 0 click)) | |
241 | (x (nth 1 click)) | |
242 | (y (nth 2 click)) | |
4c8bb950 NR |
243 | ;; Emulate timestamp information. This is accurate enough |
244 | ;; for default value of mouse-1-click-follows-link (450msec). | |
6ab93c85 SM |
245 | (timestamp (xterm-mouse-truncate-wrap |
246 | (* 1000 | |
247 | (- (float-time) | |
248 | (or xt-mouse-epoch | |
249 | (setq xt-mouse-epoch (float-time))))))) | |
fcd9df1b NR |
250 | (w (window-at x y)) |
251 | (ltrb (window-edges w)) | |
252 | (left (nth 0 ltrb)) | |
253 | (top (nth 1 ltrb))) | |
6bac1616 KL |
254 | (set-terminal-parameter nil 'xterm-mouse-x x) |
255 | (set-terminal-parameter nil 'xterm-mouse-y y) | |
cb666f2d NR |
256 | (setq |
257 | last-input-event | |
63408057 | 258 | (list type |
915bdfc4 NR |
259 | (let ((event (if w |
260 | (posn-at-x-y (- x left) (- y top) w t) | |
261 | (append (list nil 'menu-bar) | |
ad15832e | 262 | (nthcdr 2 (posn-at-x-y x y)))))) |
915bdfc4 NR |
263 | (setcar (nthcdr 3 event) timestamp) |
264 | event))))) | |
fe3acbd4 | 265 | |
fe3acbd4 | 266 | ;;;###autoload |
c6005bf3 | 267 | (define-minor-mode xterm-mouse-mode |
fe3acbd4 | 268 | "Toggle XTerm mouse mode. |
06e21633 CY |
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. | |
fe3acbd4 | 272 | |
e742657f | 273 | Turn it on to use Emacs mouse commands, and off to use xterm mouse commands. |
9001354f LT |
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 | |
146df845 SM |
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)) | |
c6005bf3 SM |
292 | (if xterm-mouse-mode |
293 | ;; Turn it on | |
6bac1616 | 294 | (progn |
c6005bf3 SM |
295 | (setq mouse-position-function #'xterm-mouse-position-function) |
296 | (turn-on-xterm-mouse-tracking)) | |
297 | ;; Turn it off | |
298 | (turn-off-xterm-mouse-tracking 'force) | |
299 | (setq mouse-position-function nil))) | |
fe3acbd4 RS |
300 | |
301 | (defun turn-on-xterm-mouse-tracking () | |
b2497d2e | 302 | "Enable Emacs mouse tracking in xterm." |
d63ddb2c | 303 | (dolist (terminal (terminal-list)) |
af020a04 | 304 | (turn-on-xterm-mouse-tracking-on-terminal terminal))) |
fe3acbd4 | 305 | |
06b60517 | 306 | (defun turn-off-xterm-mouse-tracking (&optional _force) |
90e742e0 | 307 | "Disable Emacs mouse tracking in xterm." |
d63ddb2c | 308 | (dolist (terminal (terminal-list)) |
af020a04 | 309 | (turn-off-xterm-mouse-tracking-on-terminal terminal))) |
6bac1616 | 310 | |
af020a04 | 311 | (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal) |
6bac1616 | 312 | "Enable xterm mouse tracking on TERMINAL." |
393439a3 DN |
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"))) | |
af020a04 | 317 | (unless (terminal-parameter terminal 'xterm-mouse-mode) |
63408057 | 318 | ;; Simulate selecting a terminal by selecting one of its frames |
af020a04 | 319 | (with-selected-frame (car (frames-on-display-list terminal)) |
63408057 CY |
320 | (define-key input-decode-map "\e[M" 'xterm-mouse-translate) |
321 | (define-key input-decode-map "\e[<" 'xterm-mouse-translate-extended)) | |
af020a04 | 322 | (set-terminal-parameter terminal 'xterm-mouse-mode t)) |
63408057 CY |
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))) | |
6bac1616 KL |
326 | |
327 | (defun turn-off-xterm-mouse-tracking-on-terminal (terminal) | |
328 | "Disable xterm mouse tracking on TERMINAL." | |
af020a04 SM |
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) | |
393439a3 DN |
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"))) | |
af020a04 SM |
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). | |
63408057 CY |
341 | (send-string-to-terminal "\e[?1000l" terminal) |
342 | (send-string-to-terminal "\e[?1006l" terminal))) | |
6bac1616 | 343 | |
fe3acbd4 RS |
344 | (provide 'xt-mouse) |
345 | ||
346 | ;;; xt-mouse.el ends here |