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