Fixes: debbugs:6594
[bpt/emacs.git] / lisp / xt-mouse.el
1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
2
3 ;; Copyright (C) 1994, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: mouse, terminals
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Enable mouse support when running inside an xterm.
27
28 ;; This is actually useful when you are running X11 locally, but is
29 ;; working on remote machine over a modem line or through a gateway.
30
31 ;; It works by translating xterm escape codes into generic emacs mouse
32 ;; events so it should work with any package that uses the mouse.
33
34 ;; You don't have to turn off xterm mode to use the normal xterm mouse
35 ;; functionality, it is still available by holding down the SHIFT key
36 ;; when you press the mouse button.
37
38 ;;; Todo:
39
40 ;; Support multi-click -- somehow.
41
42 ;;; Code:
43
44 (defvar xterm-mouse-debug-buffer nil)
45
46 (defvar xterm-mouse-last)
47
48 ;; Mouse events symbols must have an 'event-kind property with
49 ;; the value 'mouse-click.
50 (dolist (event-type '(mouse-1 mouse-2 mouse-3
51 M-down-mouse-1 M-down-mouse-2 M-down-mouse-3))
52 (put event-type 'event-kind 'mouse-click))
53
54 (defun xterm-mouse-translate (event)
55 "Read a click and release event from XTerm."
56 (save-excursion
57 (save-window-excursion
58 (deactivate-mark)
59 (let* ((xterm-mouse-last)
60 (down (xterm-mouse-event))
61 (down-command (nth 0 down))
62 (down-data (nth 1 down))
63 (down-where (nth 1 down-data))
64 (down-binding (key-binding (if (symbolp down-where)
65 (vector down-where down-command)
66 (vector down-command))))
67 (is-click (string-match "^mouse" (symbol-name (car down)))))
68
69 (unless is-click
70 (unless (and (eq (read-char) ?\e)
71 (eq (read-char) ?\[)
72 (eq (read-char) ?M))
73 (error "Unexpected escape sequence from XTerm")))
74
75 (let* ((click (if is-click down (xterm-mouse-event)))
76 ;; (click-command (nth 0 click))
77 (click-data (nth 1 click))
78 (click-where (nth 1 click-data)))
79 (if (memq down-binding '(nil ignore))
80 (if (and (symbolp click-where)
81 (consp click-where))
82 (vector (list click-where click-data) click)
83 (vector click))
84 (setq unread-command-events
85 (if (eq down-where click-where)
86 (list click)
87 (list
88 ;; Cheat `mouse-drag-region' with move event.
89 (list 'mouse-movement click-data)
90 ;; Generate a drag event.
91 (if (symbolp down-where)
92 0
93 (list (intern (format "drag-mouse-%d"
94 (+ 1 xterm-mouse-last)))
95 down-data click-data)))))
96 (if xterm-mouse-debug-buffer
97 (print unread-command-events xterm-mouse-debug-buffer))
98 (if (and (symbolp down-where)
99 (consp down-where))
100 (vector (list down-where down-data) down)
101 (vector down))))))))
102
103 ;; These two variables have been converted to terminal parameters.
104 ;;
105 ;;(defvar xterm-mouse-x 0
106 ;; "Position of last xterm mouse event relative to the frame.")
107 ;;
108 ;;(defvar xterm-mouse-y 0
109 ;; "Position of last xterm mouse event relative to the frame.")
110
111 (defvar xt-mouse-epoch nil)
112
113 ;; Indicator for the xterm-mouse mode.
114
115 (defun xterm-mouse-position-function (pos)
116 "Bound to `mouse-position-function' in XTerm mouse mode."
117 (when (terminal-parameter nil 'xterm-mouse-x)
118 (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
119 (terminal-parameter nil 'xterm-mouse-y))))
120 pos)
121
122 ;; read xterm sequences above ascii 127 (#x7f)
123 (defun xterm-mouse-event-read ()
124 ;; We get the characters decoded by the keyboard coding system. Try
125 ;; to recover the raw character.
126 (let ((c (read-char)))
127 (cond ;; If meta-flag is t we get a meta character
128 ((>= c ?\M-\^@)
129 (- c (- ?\M-\^@ 128)))
130 ;; Reencode the character in the keyboard coding system, if
131 ;; this is a non-ASCII character.
132 ((>= c #x80)
133 (aref (encode-coding-string (string c) (keyboard-coding-system)) 0))
134 (t c))))
135
136 (defun xterm-mouse-truncate-wrap (f)
137 "Truncate with wrap-around."
138 (condition-case nil
139 ;; First try the built-in truncate, in case there's no overflow.
140 (truncate f)
141 ;; In case of overflow, do wraparound by hand.
142 (range-error
143 ;; In our case, we wrap around every 3 days or so, so if we assume
144 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
145 ;; Using a power of 2 makes rounding errors less likely.
146 (let* ((maxwrap (* 65536 2048))
147 (dbig (truncate (/ f maxwrap)))
148 (fdiff (- f (* 1.0 maxwrap dbig))))
149 (+ (truncate fdiff) (* maxwrap dbig))))))
150
151 (defun xterm-mouse-event ()
152 "Convert XTerm mouse event to Emacs mouse event."
153 (let* ((type (- (xterm-mouse-event-read) #o40))
154 (x (- (xterm-mouse-event-read) #o40 1))
155 (y (- (xterm-mouse-event-read) #o40 1))
156 ;; Emulate timestamp information. This is accurate enough
157 ;; for default value of mouse-1-click-follows-link (450msec).
158 (timestamp (xterm-mouse-truncate-wrap
159 (* 1000
160 (- (float-time)
161 (or xt-mouse-epoch
162 (setq xt-mouse-epoch (float-time)))))))
163 (mouse (intern
164 ;; For buttons > 3, the release-event looks
165 ;; differently (see xc/programs/xterm/button.c,
166 ;; function EditorButton), and there seems to come in
167 ;; a release-event only, no down-event.
168 (cond ((>= type 64)
169 (format "mouse-%d" (- type 60)))
170 ((memq type '(8 9 10))
171 (setq xterm-mouse-last type)
172 (format "M-down-mouse-%d" (- type 7)))
173 ((= type 11)
174 (format "mouse-%d" (- xterm-mouse-last 7)))
175 ((= type 3)
176 ;; For buttons > 5 xterm only reports a
177 ;; button-release event. Avoid error by mapping
178 ;; them all to mouse-1.
179 (format "mouse-%d" (+ 1 (or xterm-mouse-last 0))))
180 (t
181 (setq xterm-mouse-last type)
182 (format "down-mouse-%d" (+ 1 type))))))
183 (w (window-at x y))
184 (ltrb (window-edges w))
185 (left (nth 0 ltrb))
186 (top (nth 1 ltrb)))
187
188 (set-terminal-parameter nil 'xterm-mouse-x x)
189 (set-terminal-parameter nil 'xterm-mouse-y y)
190 (setq
191 last-input-event
192 (list mouse
193 (let ((event (if w
194 (posn-at-x-y (- x left) (- y top) w t)
195 (append (list nil 'menu-bar)
196 (nthcdr 2 (posn-at-x-y x y))))))
197 (setcar (nthcdr 3 event) timestamp)
198 event)))))
199
200 ;;;###autoload
201 (define-minor-mode xterm-mouse-mode
202 "Toggle XTerm mouse mode.
203 With prefix arg, turn XTerm mouse mode on if arg is positive, otherwise turn
204 it off.
205
206 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
207 This works in terminal emulators compatible with xterm. It only
208 works for simple uses of the mouse. Basically, only non-modified
209 single clicks are supported. When turned on, the normal xterm
210 mouse functionality for such clicks is still available by holding
211 down the SHIFT key while pressing the mouse button."
212 :global t :group 'mouse
213 (let ((do-hook (if xterm-mouse-mode 'add-hook 'remove-hook)))
214 (funcall do-hook 'terminal-init-xterm-hook
215 'turn-on-xterm-mouse-tracking-on-terminal)
216 (funcall do-hook 'delete-terminal-functions
217 'turn-off-xterm-mouse-tracking-on-terminal)
218 (funcall do-hook 'suspend-tty-functions
219 'turn-off-xterm-mouse-tracking-on-terminal)
220 (funcall do-hook 'resume-tty-functions
221 'turn-on-xterm-mouse-tracking-on-terminal)
222 (funcall do-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
223 (funcall do-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
224 (funcall do-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking))
225 (if xterm-mouse-mode
226 ;; Turn it on
227 (progn
228 (setq mouse-position-function #'xterm-mouse-position-function)
229 (turn-on-xterm-mouse-tracking))
230 ;; Turn it off
231 (turn-off-xterm-mouse-tracking 'force)
232 (setq mouse-position-function nil)))
233
234 (defun turn-on-xterm-mouse-tracking ()
235 "Enable Emacs mouse tracking in xterm."
236 (dolist (terminal (terminal-list))
237 (turn-on-xterm-mouse-tracking-on-terminal terminal)))
238
239 (defun turn-off-xterm-mouse-tracking (&optional force)
240 "Disable Emacs mouse tracking in xterm."
241 (dolist (terminal (terminal-list))
242 (turn-off-xterm-mouse-tracking-on-terminal terminal)))
243
244 (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal)
245 "Enable xterm mouse tracking on TERMINAL."
246 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal))
247 ;; Avoid the initial terminal which is not a termcap device.
248 ;; FIXME: is there more elegant way to detect the initial terminal?
249 (not (string= (terminal-name terminal) "initial_terminal")))
250 (unless (terminal-parameter terminal 'xterm-mouse-mode)
251 ;; Simulate selecting a terminal by selecting one of its frames ;-(
252 (with-selected-frame (car (frames-on-display-list terminal))
253 (define-key input-decode-map "\e[M" 'xterm-mouse-translate))
254 (set-terminal-parameter terminal 'xterm-mouse-mode t))
255 (send-string-to-terminal "\e[?1000h" terminal)))
256
257 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
258 "Disable xterm mouse tracking on TERMINAL."
259 ;; Only send the disable command to those terminals to which we've already
260 ;; sent the enable command.
261 (when (and (terminal-parameter terminal 'xterm-mouse-mode)
262 (eq t (terminal-live-p terminal))
263 ;; Avoid the initial terminal which is not a termcap device.
264 ;; FIXME: is there more elegant way to detect the initial terminal?
265 (not (string= (terminal-name terminal) "initial_terminal")))
266 ;; We could remove the key-binding and unset the `xterm-mouse-mode'
267 ;; terminal parameter, but it seems less harmful to send this escape
268 ;; command too many times (or to catch an unintended key sequence), than
269 ;; to send it too few times (or to fail to let xterm-mouse events
270 ;; pass by untranslated).
271 (send-string-to-terminal "\e[?1000l" terminal)))
272
273 (provide 'xt-mouse)
274
275 ;; arch-tag: 84962d4e-fae9-4c13-a9d7-ef4925a4ac03
276 ;;; xt-mouse.el ends here