* src/eval.c (Fbind_symbol): New function.
[bpt/emacs.git] / lisp / xt-mouse.el
CommitLineData
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
bf31a093
NR
45;; Mouse events symbols must have an 'event-kind property with
46;; the value 'mouse-click.
fbd5cc6c
SM
47(dolist (event '(mouse-1 mouse-2 mouse-3 mouse-4 mouse-5))
48 (let ((M-event (intern (concat "M-" (symbol-name event)))))
49 (put event 'event-kind 'mouse-click)
50 (put M-event 'event-kind 'mouse-click)))
bf31a093 51
06b60517 52(defun xterm-mouse-translate (_event)
b2497d2e 53 "Read a click and release event from XTerm."
63408057
CY
54 (xterm-mouse-translate-1))
55
56(defun xterm-mouse-translate-extended (_event)
57 "Read a click and release event from XTerm.
58Similar to `xterm-mouse-translate', but using the \"1006\"
59extension, which supports coordinates >= 231 (see
60http://invisible-island.net/xterm/ctlseqs/ctlseqs.html)."
61 (xterm-mouse-translate-1 1006))
62
63(defun xterm-mouse-translate-1 (&optional extension)
fe3acbd4 64 (save-excursion
b406487f
SM
65 (save-window-excursion ;FIXME: Why?
66 (deactivate-mark) ;FIXME: Why?
fbd5cc6c
SM
67 (let* ((event (xterm-mouse-event extension))
68 (ev-command (nth 0 event))
69 (ev-data (nth 1 event))
70 (ev-where (nth 1 ev-data))
71 (vec (if (and (symbolp ev-where) (consp ev-where))
72 ;; FIXME: This condition can *never* be non-nil!?!
73 (vector (list ev-where ev-data) event)
74 (vector event)))
75 (is-down (string-match "down-" (symbol-name ev-command))))
76
b406487f 77 (cond
fbd5cc6c
SM
78 ((null event) nil) ;Unknown/bogus byte sequence!
79 (is-down
80 (setf (terminal-parameter nil 'xterm-mouse-last-down) event)
81 vec)
82 (t
83 (let* ((down (terminal-parameter nil 'xterm-mouse-last-down))
84 (down-data (nth 1 down))
85 (down-where (nth 1 down-data)))
86 (setf (terminal-parameter nil 'xterm-mouse-last-down) nil)
87 (cond
88 ((null down)
89 ;; This is an "up-only" event. Pretend there was an up-event
90 ;; right before and keep the up-event for later.
91 (push event unread-command-events)
92 (vector (cons (intern (replace-regexp-in-string
93 "\\`\\([ACMHSs]-\\)*" "\\&down-"
94 (symbol-name ev-command) t))
95 (cdr event))))
96 ((equal ev-where down-where) vec)
b406487f 97 (t
fbd5cc6c
SM
98 (let ((drag (if (symbolp ev-where)
99 0 ;FIXME: Why?!?
100 (list (replace-regexp-in-string
101 "\\`\\([ACMHSs]-\\)*" "\\&drag-"
102 (symbol-name ev-command) t)
103 down-data ev-data))))
104 (if (null track-mouse)
105 (vector drag)
106 (push drag unread-command-events)
107 (vector (list 'mouse-movement ev-data)))))))))))))
cec01ccb 108
6bac1616
KL
109;; These two variables have been converted to terminal parameters.
110;;
111;;(defvar xterm-mouse-x 0
112;; "Position of last xterm mouse event relative to the frame.")
113;;
114;;(defvar xterm-mouse-y 0
115;; "Position of last xterm mouse event relative to the frame.")
cec01ccb 116
a7dc2df1
NR
117(defvar xt-mouse-epoch nil)
118
54eba353 119;; Indicator for the xterm-mouse mode.
54eba353 120
b2497d2e
DL
121(defun xterm-mouse-position-function (pos)
122 "Bound to `mouse-position-function' in XTerm mouse mode."
6bac1616
KL
123 (when (terminal-parameter nil 'xterm-mouse-x)
124 (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
125 (terminal-parameter nil 'xterm-mouse-y))))
b2497d2e 126 pos)
fe3acbd4 127
6ab93c85
SM
128(defun xterm-mouse-truncate-wrap (f)
129 "Truncate with wrap-around."
130 (condition-case nil
131 ;; First try the built-in truncate, in case there's no overflow.
132 (truncate f)
133 ;; In case of overflow, do wraparound by hand.
134 (range-error
135 ;; In our case, we wrap around every 3 days or so, so if we assume
136 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
137 ;; Using a power of 2 makes rounding errors less likely.
138 (let* ((maxwrap (* 65536 2048))
139 (dbig (truncate (/ f maxwrap)))
140 (fdiff (- f (* 1.0 maxwrap dbig))))
141 (+ (truncate fdiff) (* maxwrap dbig))))))
142
63408057
CY
143;; Normal terminal mouse click reporting: expect three bytes, of the
144;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
145(defun xterm-mouse--read-event-sequence-1000 ()
b406487f
SM
146 (let* ((code (- (read-event) 32))
147 (type
c400516a
GM
148 ;; For buttons > 3, the release-event looks differently
149 ;; (see xc/programs/xterm/button.c, function EditorButton),
150 ;; and come in a release-event only, no down-event.
151 (cond ((>= code 64)
152 (format "mouse-%d" (- code 60)))
153 ((memq code '(8 9 10))
154 (format "M-down-mouse-%d" (- code 7)))
155 ((memq code '(3 11))
156 (let ((down (car (terminal-parameter
157 nil 'xterm-mouse-last-down))))
158 (when (and down (string-match "[0-9]" (symbol-name down)))
159 (format (if (eq code 3) "mouse-%s" "M-mouse-%s")
160 (match-string 0 (symbol-name down))))))
161 ((memq code '(0 1 2))
162 (format "down-mouse-%d" (+ 1 code)))))
b406487f
SM
163 (x (- (read-event) 33))
164 (y (- (read-event) 33)))
165 (and type (wholenump x) (wholenump y)
bc2fb4fd 166 (list (intern type) x y))))
63408057
CY
167
168;; XTerm's 1006-mode terminal mouse click reporting has the form
169;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
170;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
171(defun xterm-mouse--read-event-sequence-1006 ()
172 (let (button-bytes x-bytes y-bytes c)
25c09217 173 (while (not (eq (setq c (read-event)) ?\;))
63408057 174 (push c button-bytes))
25c09217 175 (while (not (eq (setq c (read-event)) ?\;))
63408057 176 (push c x-bytes))
25c09217 177 (while (not (memq (setq c (read-event)) '(?m ?M)))
63408057
CY
178 (push c y-bytes))
179 (list (let* ((code (string-to-number
180 (apply 'string (nreverse button-bytes))))
181 (wheel (>= code 64))
182 (down (and (not wheel)
183 (eq c ?M))))
184 (intern (format "%s%smouse-%d"
185 (cond (wheel "")
186 ((< code 4) "")
187 ((< code 8) "S-")
188 ((< code 12) "M-")
189 ((< code 16) "M-S-")
190 ((< code 20) "C-")
191 ((< code 24) "C-S-")
192 ((< code 28) "C-M-")
193 ((< code 32) "C-M-S-")
194 (t
195 (error "Unexpected escape sequence from XTerm")))
196 (if down "down-" "")
197 (if wheel
198 (- code 60)
fbd5cc6c 199 (1+ (mod code 4))))))
63408057
CY
200 (1- (string-to-number (apply 'string (nreverse x-bytes))))
201 (1- (string-to-number (apply 'string (nreverse y-bytes)))))))
202
fbd5cc6c
SM
203(defun xterm-mouse--set-click-count (event click-count)
204 (setcdr (cdr event) (list click-count))
205 (let ((name (symbol-name (car event))))
206 (when (string-match "\\(.*?\\)\\(\\(?:down-\\)?mouse-.*\\)" name)
207 (setcar event
208 (intern (concat (match-string 1 name)
209 (if (= click-count 2)
210 "double-" "triple-")
211 (match-string 2 name)))))))
212
63408057
CY
213(defun xterm-mouse-event (&optional extension)
214 "Convert XTerm mouse event to Emacs mouse event.
215EXTENSION, if non-nil, means to use an extension to the usual
216terminal mouse protocol; we currently support the value 1006,
217which is the \"1006\" extension implemented in Xterm >= 277."
218 (let* ((click (cond ((null extension)
219 (xterm-mouse--read-event-sequence-1000))
220 ((eq extension 1006)
221 (xterm-mouse--read-event-sequence-1006))
222 (t
b406487f
SM
223 (error "Unsupported XTerm mouse protocol")))))
224 (when click
225 (let* ((type (nth 0 click))
226 (x (nth 1 click))
227 (y (nth 2 click))
228 ;; Emulate timestamp information. This is accurate enough
229 ;; for default value of mouse-1-click-follows-link (450msec).
230 (timestamp (xterm-mouse-truncate-wrap
231 (* 1000
232 (- (float-time)
233 (or xt-mouse-epoch
234 (setq xt-mouse-epoch (float-time)))))))
235 (w (window-at x y))
236 (ltrb (window-edges w))
237 (left (nth 0 ltrb))
fbd5cc6c
SM
238 (top (nth 1 ltrb))
239 (posn (if w
b406487f
SM
240 (posn-at-x-y (- x left) (- y top) w t)
241 (append (list nil 'menu-bar)
fbd5cc6c
SM
242 (nthcdr 2 (posn-at-x-y x y)))))
243 (event (list type posn)))
244 (setcar (nthcdr 3 posn) timestamp)
245
246 ;; Try to handle double/triple clicks.
247 (let* ((last-click (terminal-parameter nil 'xterm-mouse-last-click))
248 (last-type (nth 0 last-click))
249 (last-name (symbol-name last-type))
250 (last-time (nth 1 last-click))
251 (click-count (nth 2 last-click))
252 (this-time (float-time))
253 (name (symbol-name type)))
254 (cond
255 ((not (string-match "down-" name))
256 ;; For up events, make the up side match the down side.
257 (setq this-time last-time)
258 (when (and (> click-count 1)
259 (string-match "down-" last-name)
260 (equal name (replace-match "" t t last-name)))
261 (xterm-mouse--set-click-count event click-count)))
262 ((not last-time) nil)
263 ((and (> double-click-time (* 1000 (- this-time last-time)))
264 (equal last-name (replace-match "" t t name)))
265 (setq click-count (1+ click-count))
266 (xterm-mouse--set-click-count event click-count))
267 (t (setq click-count 1)))
268 (set-terminal-parameter nil 'xterm-mouse-last-click
269 (list type this-time click-count)))
270
271 (set-terminal-parameter nil 'xterm-mouse-x x)
272 (set-terminal-parameter nil 'xterm-mouse-y y)
273 (setq last-input-event event)))))
fe3acbd4 274
fe3acbd4 275;;;###autoload
c6005bf3 276(define-minor-mode xterm-mouse-mode
fe3acbd4 277 "Toggle XTerm mouse mode.
06e21633
CY
278With a prefix argument ARG, enable XTerm mouse mode if ARG is
279positive, and disable it otherwise. If called from Lisp, enable
280the mode if ARG is omitted or nil.
fe3acbd4 281
e742657f 282Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
9001354f
LT
283This works in terminal emulators compatible with xterm. It only
284works for simple uses of the mouse. Basically, only non-modified
285single clicks are supported. When turned on, the normal xterm
286mouse functionality for such clicks is still available by holding
287down the SHIFT key while pressing the mouse button."
288 :global t :group 'mouse
bfc30790
DC
289 (funcall (if xterm-mouse-mode 'add-hook 'remove-hook)
290 'terminal-init-xterm-hook
291 'turn-on-xterm-mouse-tracking-on-terminal)
c6005bf3
SM
292 (if xterm-mouse-mode
293 ;; Turn it on
6bac1616 294 (progn
c6005bf3 295 (setq mouse-position-function #'xterm-mouse-position-function)
bfc30790 296 (mapc #'turn-on-xterm-mouse-tracking-on-terminal (terminal-list)))
c6005bf3 297 ;; Turn it off
bfc30790 298 (mapc #'turn-off-xterm-mouse-tracking-on-terminal (terminal-list))
c6005bf3 299 (setq mouse-position-function nil)))
fe3acbd4 300
bfc30790
DC
301(defconst xterm-mouse-tracking-enable-sequence
302 "\e[?1000h\e[?1006h"
303 "Control sequence to enable xterm mouse tracking.
304Enables basic tracking, then extended tracking on
305terminals that support it.")
fe3acbd4 306
bfc30790
DC
307(defconst xterm-mouse-tracking-disable-sequence
308 "\e[?1006l\e[?1000l"
309 "Reset the modes set by `xterm-mouse-tracking-enable-sequence'.")
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
bfc30790 319 ;; so that we can set the terminal-local `input-decode-map'.
af020a04 320 (with-selected-frame (car (frames-on-display-list terminal))
63408057
CY
321 (define-key input-decode-map "\e[M" 'xterm-mouse-translate)
322 (define-key input-decode-map "\e[<" 'xterm-mouse-translate-extended))
bfc30790
DC
323 (send-string-to-terminal xterm-mouse-tracking-enable-sequence terminal)
324 (push xterm-mouse-tracking-enable-sequence
325 (terminal-parameter nil 'tty-mode-set-strings))
326 (push xterm-mouse-tracking-disable-sequence
327 (terminal-parameter nil 'tty-mode-reset-strings))
328 (set-terminal-parameter terminal 'xterm-mouse-mode t))))
6bac1616
KL
329
330(defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
331 "Disable xterm mouse tracking on TERMINAL."
af020a04
SM
332 ;; Only send the disable command to those terminals to which we've already
333 ;; sent the enable command.
334 (when (and (terminal-parameter terminal 'xterm-mouse-mode)
bfc30790 335 (eq t (terminal-live-p 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).
bfc30790
DC
341 (send-string-to-terminal xterm-mouse-tracking-disable-sequence terminal)
342 (setf (terminal-parameter nil 'tty-mode-set-strings)
343 (remq xterm-mouse-tracking-enable-sequence
344 (terminal-parameter nil 'tty-mode-set-strings)))
345 (setf (terminal-parameter nil 'tty-mode-reset-strings)
346 (remq xterm-mouse-tracking-disable-sequence
347 (terminal-parameter nil 'tty-mode-reset-strings)))
348 (set-terminal-parameter terminal 'xterm-mouse-mode nil)))
6bac1616 349
fe3acbd4
RS
350(provide 'xt-mouse)
351
352;;; xt-mouse.el ends here