Empty elements in EMACSLOADPATH now stand for the default
[bpt/emacs.git] / lisp / xt-mouse.el
CommitLineData
e8af40ee 1;;; xt-mouse.el --- support the mouse when emacs run in an xterm
b578f267 2
ab422c4d 3;; Copyright (C) 1994, 2000-2013 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.
59Similar to `xterm-mouse-translate', but using the \"1006\"
60extension, which supports coordinates >= 231 (see
61http://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 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
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
6ab93c85
SM
138(defun xterm-mouse-truncate-wrap (f)
139 "Truncate with wrap-around."
140 (condition-case nil
141 ;; First try the built-in truncate, in case there's no overflow.
142 (truncate f)
143 ;; In case of overflow, do wraparound by hand.
144 (range-error
145 ;; In our case, we wrap around every 3 days or so, so if we assume
146 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
147 ;; Using a power of 2 makes rounding errors less likely.
148 (let* ((maxwrap (* 65536 2048))
149 (dbig (truncate (/ f maxwrap)))
150 (fdiff (- f (* 1.0 maxwrap dbig))))
151 (+ (truncate fdiff) (* maxwrap dbig))))))
152
63408057
CY
153;; Normal terminal mouse click reporting: expect three bytes, of the
154;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
155(defun xterm-mouse--read-event-sequence-1000 ()
25c09217 156 (list (let ((code (- (read-event) 32)))
63408057
CY
157 (intern
158 ;; For buttons > 3, the release-event looks differently
159 ;; (see xc/programs/xterm/button.c, function EditorButton),
160 ;; and come in a release-event only, no down-event.
161 (cond ((>= code 64)
162 (format "mouse-%d" (- code 60)))
163 ((memq code '(8 9 10))
164 (setq xterm-mouse-last code)
165 (format "M-down-mouse-%d" (- code 7)))
166 ((= code 11)
167 (format "M-mouse-%d" (- xterm-mouse-last 7)))
168 ((= code 3)
169 ;; For buttons > 5 xterm only reports a
170 ;; button-release event. Avoid error by mapping
171 ;; them all to mouse-1.
172 (format "mouse-%d" (+ 1 (or xterm-mouse-last 0))))
173 (t
174 (setq xterm-mouse-last code)
175 (format "down-mouse-%d" (+ 1 code))))))
176 ;; x and y coordinates
25c09217
SM
177 (- (read-event) 33)
178 (- (read-event) 33)))
63408057
CY
179
180;; XTerm's 1006-mode terminal mouse click reporting has the form
181;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
182;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
183(defun xterm-mouse--read-event-sequence-1006 ()
184 (let (button-bytes x-bytes y-bytes c)
25c09217 185 (while (not (eq (setq c (read-event)) ?\;))
63408057 186 (push c button-bytes))
25c09217 187 (while (not (eq (setq c (read-event)) ?\;))
63408057 188 (push c x-bytes))
25c09217 189 (while (not (memq (setq c (read-event)) '(?m ?M)))
63408057
CY
190 (push c y-bytes))
191 (list (let* ((code (string-to-number
192 (apply 'string (nreverse button-bytes))))
193 (wheel (>= code 64))
194 (down (and (not wheel)
195 (eq c ?M))))
196 (intern (format "%s%smouse-%d"
197 (cond (wheel "")
198 ((< code 4) "")
199 ((< code 8) "S-")
200 ((< code 12) "M-")
201 ((< code 16) "M-S-")
202 ((< code 20) "C-")
203 ((< code 24) "C-S-")
204 ((< code 28) "C-M-")
205 ((< code 32) "C-M-S-")
206 (t
207 (error "Unexpected escape sequence from XTerm")))
208 (if down "down-" "")
209 (if wheel
210 (- code 60)
211 (1+ (setq xterm-mouse-last (mod code 4)))))))
212 (1- (string-to-number (apply 'string (nreverse x-bytes))))
213 (1- (string-to-number (apply 'string (nreverse y-bytes)))))))
214
215(defun xterm-mouse-event (&optional extension)
216 "Convert XTerm mouse event to Emacs mouse event.
217EXTENSION, if non-nil, means to use an extension to the usual
218terminal mouse protocol; we currently support the value 1006,
219which is the \"1006\" extension implemented in Xterm >= 277."
220 (let* ((click (cond ((null extension)
221 (xterm-mouse--read-event-sequence-1000))
222 ((eq extension 1006)
223 (xterm-mouse--read-event-sequence-1006))
224 (t
225 (error "Unsupported XTerm mouse protocol"))))
226 (type (nth 0 click))
227 (x (nth 1 click))
228 (y (nth 2 click))
4c8bb950
NR
229 ;; Emulate timestamp information. This is accurate enough
230 ;; for default value of mouse-1-click-follows-link (450msec).
6ab93c85
SM
231 (timestamp (xterm-mouse-truncate-wrap
232 (* 1000
233 (- (float-time)
234 (or xt-mouse-epoch
235 (setq xt-mouse-epoch (float-time)))))))
fcd9df1b
NR
236 (w (window-at x y))
237 (ltrb (window-edges w))
238 (left (nth 0 ltrb))
239 (top (nth 1 ltrb)))
6bac1616
KL
240 (set-terminal-parameter nil 'xterm-mouse-x x)
241 (set-terminal-parameter nil 'xterm-mouse-y y)
cb666f2d
NR
242 (setq
243 last-input-event
63408057 244 (list type
915bdfc4
NR
245 (let ((event (if w
246 (posn-at-x-y (- x left) (- y top) w t)
247 (append (list nil 'menu-bar)
ad15832e 248 (nthcdr 2 (posn-at-x-y x y))))))
915bdfc4
NR
249 (setcar (nthcdr 3 event) timestamp)
250 event)))))
fe3acbd4 251
fe3acbd4 252;;;###autoload
c6005bf3 253(define-minor-mode xterm-mouse-mode
fe3acbd4 254 "Toggle XTerm mouse mode.
06e21633
CY
255With a prefix argument ARG, enable XTerm mouse mode if ARG is
256positive, and disable it otherwise. If called from Lisp, enable
257the mode if ARG is omitted or nil.
fe3acbd4 258
e742657f 259Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
9001354f
LT
260This works in terminal emulators compatible with xterm. It only
261works for simple uses of the mouse. Basically, only non-modified
262single clicks are supported. When turned on, the normal xterm
263mouse functionality for such clicks is still available by holding
264down the SHIFT key while pressing the mouse button."
265 :global t :group 'mouse
146df845
SM
266 (let ((do-hook (if xterm-mouse-mode 'add-hook 'remove-hook)))
267 (funcall do-hook 'terminal-init-xterm-hook
268 'turn-on-xterm-mouse-tracking-on-terminal)
269 (funcall do-hook 'delete-terminal-functions
270 'turn-off-xterm-mouse-tracking-on-terminal)
271 (funcall do-hook 'suspend-tty-functions
272 'turn-off-xterm-mouse-tracking-on-terminal)
273 (funcall do-hook 'resume-tty-functions
274 'turn-on-xterm-mouse-tracking-on-terminal)
275 (funcall do-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
276 (funcall do-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
277 (funcall do-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking))
c6005bf3
SM
278 (if xterm-mouse-mode
279 ;; Turn it on
6bac1616 280 (progn
c6005bf3
SM
281 (setq mouse-position-function #'xterm-mouse-position-function)
282 (turn-on-xterm-mouse-tracking))
283 ;; Turn it off
284 (turn-off-xterm-mouse-tracking 'force)
285 (setq mouse-position-function nil)))
fe3acbd4
RS
286
287(defun turn-on-xterm-mouse-tracking ()
b2497d2e 288 "Enable Emacs mouse tracking in xterm."
d63ddb2c 289 (dolist (terminal (terminal-list))
af020a04 290 (turn-on-xterm-mouse-tracking-on-terminal terminal)))
fe3acbd4 291
06b60517 292(defun turn-off-xterm-mouse-tracking (&optional _force)
90e742e0 293 "Disable Emacs mouse tracking in xterm."
d63ddb2c 294 (dolist (terminal (terminal-list))
af020a04 295 (turn-off-xterm-mouse-tracking-on-terminal terminal)))
6bac1616 296
af020a04 297(defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal)
6bac1616 298 "Enable xterm mouse tracking on TERMINAL."
393439a3
DN
299 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal))
300 ;; Avoid the initial terminal which is not a termcap device.
301 ;; FIXME: is there more elegant way to detect the initial terminal?
302 (not (string= (terminal-name terminal) "initial_terminal")))
af020a04 303 (unless (terminal-parameter terminal 'xterm-mouse-mode)
63408057 304 ;; Simulate selecting a terminal by selecting one of its frames
af020a04 305 (with-selected-frame (car (frames-on-display-list terminal))
63408057
CY
306 (define-key input-decode-map "\e[M" 'xterm-mouse-translate)
307 (define-key input-decode-map "\e[<" 'xterm-mouse-translate-extended))
af020a04 308 (set-terminal-parameter terminal 'xterm-mouse-mode t))
63408057
CY
309 (send-string-to-terminal "\e[?1000h" terminal)
310 ;; Request extended mouse support, if available (xterm >= 277).
311 (send-string-to-terminal "\e[?1006h" terminal)))
6bac1616
KL
312
313(defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
314 "Disable xterm mouse tracking on TERMINAL."
af020a04
SM
315 ;; Only send the disable command to those terminals to which we've already
316 ;; sent the enable command.
317 (when (and (terminal-parameter terminal 'xterm-mouse-mode)
393439a3
DN
318 (eq t (terminal-live-p terminal))
319 ;; Avoid the initial terminal which is not a termcap device.
320 ;; FIXME: is there more elegant way to detect the initial terminal?
321 (not (string= (terminal-name terminal) "initial_terminal")))
af020a04
SM
322 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
323 ;; terminal parameter, but it seems less harmful to send this escape
324 ;; command too many times (or to catch an unintended key sequence), than
325 ;; to send it too few times (or to fail to let xterm-mouse events
326 ;; pass by untranslated).
63408057
CY
327 (send-string-to-terminal "\e[?1000l" terminal)
328 (send-string-to-terminal "\e[?1006l" terminal)))
6bac1616 329
fe3acbd4
RS
330(provide 'xt-mouse)
331
332;;; xt-mouse.el ends here