Merge from emacs--rel--22
[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 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Enable mouse support when running inside an xterm.
29
30 ;; This is actually useful when you are running X11 locally, but is
31 ;; working on remote machine over a modem line or through a gateway.
32
33 ;; It works by translating xterm escape codes into generic emacs mouse
34 ;; events so it should work with any package that uses the mouse.
35
36 ;; You don't have to turn off xterm mode to use the normal xterm mouse
37 ;; functionality, it is still available by holding down the SHIFT key
38 ;; when you press the mouse button.
39
40 ;;; Todo:
41
42 ;; Support multi-click -- somehow.
43
44 ;;; Code:
45
46 (defvar xterm-mouse-debug-buffer nil)
47
48 (define-key function-key-map "\e[M" 'xterm-mouse-translate)
49
50 (defvar xterm-mouse-last)
51
52 ;; Mouse events symbols must have an 'event-kind property with
53 ;; the value 'mouse-click.
54 (dolist (event-type '(mouse-1 mouse-2 mouse-3
55 M-down-mouse-1 M-down-mouse-2 M-down-mouse-3))
56 (put event-type 'event-kind 'mouse-click))
57
58 (defun xterm-mouse-translate (event)
59 "Read a click and release event from XTerm."
60 (save-excursion
61 (save-window-excursion
62 (deactivate-mark)
63 (let* ((xterm-mouse-last)
64 (down (xterm-mouse-event))
65 (down-command (nth 0 down))
66 (down-data (nth 1 down))
67 (down-where (nth 1 down-data))
68 (down-binding (key-binding (if (symbolp down-where)
69 (vector down-where down-command)
70 (vector down-command))))
71 (is-click (string-match "^mouse" (symbol-name (car down)))))
72
73 (unless is-click
74 (unless (and (eq (read-char) ?\e)
75 (eq (read-char) ?\[)
76 (eq (read-char) ?M))
77 (error "Unexpected escape sequence from XTerm")))
78
79 (let* ((click (if is-click down (xterm-mouse-event)))
80 (click-command (nth 0 click))
81 (click-data (nth 1 click))
82 (click-where (nth 1 click-data)))
83 (if (memq down-binding '(nil ignore))
84 (if (and (symbolp click-where)
85 (consp click-where))
86 (vector (list click-where click-data) click)
87 (vector click))
88 (setq unread-command-events
89 (if (eq down-where click-where)
90 (list click)
91 (list
92 ;; Cheat `mouse-drag-region' with move event.
93 (list 'mouse-movement click-data)
94 ;; Generate a drag event.
95 (if (symbolp down-where)
96 0
97 (list (intern (format "drag-mouse-%d"
98 (+ 1 xterm-mouse-last)))
99 down-data click-data)))))
100 (if xterm-mouse-debug-buffer
101 (print unread-command-events xterm-mouse-debug-buffer))
102 (if (and (symbolp down-where)
103 (consp down-where))
104 (vector (list down-where down-data) down)
105 (vector down))))))))
106
107 (defvar xterm-mouse-x 0
108 "Position of last xterm mouse event relative to the frame.")
109
110 (defvar xterm-mouse-y 0
111 "Position of last xterm mouse event relative to the frame.")
112
113 (defvar xt-mouse-epoch nil)
114
115 ;; Indicator for the xterm-mouse mode.
116
117 (defun xterm-mouse-position-function (pos)
118 "Bound to `mouse-position-function' in XTerm mouse mode."
119 (setcdr pos (cons xterm-mouse-x xterm-mouse-y))
120 pos)
121
122 ;; read xterm sequences above ascii 127 (#x7f)
123 (defun xterm-mouse-event-read ()
124 (let ((c (read-char)))
125 (if (< c 0)
126 (+ c #x8000000 128)
127 c)))
128
129 (defun xterm-mouse-truncate-wrap (f)
130 "Truncate with wrap-around."
131 (condition-case nil
132 ;; First try the built-in truncate, in case there's no overflow.
133 (truncate f)
134 ;; In case of overflow, do wraparound by hand.
135 (range-error
136 ;; In our case, we wrap around every 3 days or so, so if we assume
137 ;; a maximum of 65536 wraparounds, we're safe for a couple years.
138 ;; Using a power of 2 makes rounding errors less likely.
139 (let* ((maxwrap (* 65536 2048))
140 (dbig (truncate (/ f maxwrap)))
141 (fdiff (- f (* 1.0 maxwrap dbig))))
142 (+ (truncate fdiff) (* maxwrap dbig))))))
143
144 (defun xterm-mouse-event ()
145 "Convert XTerm mouse event to Emacs mouse event."
146 (let* ((type (- (xterm-mouse-event-read) #o40))
147 (x (- (xterm-mouse-event-read) #o40 1))
148 (y (- (xterm-mouse-event-read) #o40 1))
149 ;; Emulate timestamp information. This is accurate enough
150 ;; for default value of mouse-1-click-follows-link (450msec).
151 (timestamp (xterm-mouse-truncate-wrap
152 (* 1000
153 (- (float-time)
154 (or xt-mouse-epoch
155 (setq xt-mouse-epoch (float-time)))))))
156 (mouse (intern
157 ;; For buttons > 3, the release-event looks
158 ;; differently (see xc/programs/xterm/button.c,
159 ;; function EditorButton), and there seems to come in
160 ;; a release-event only, no down-event.
161 (cond ((>= type 64)
162 (format "mouse-%d" (- type 60)))
163 ((memq type '(8 9 10))
164 (setq xterm-mouse-last type)
165 (format "M-down-mouse-%d" (- type 7)))
166 ((= type 11)
167 (format "mouse-%d" (- xterm-mouse-last 7)))
168 ((= type 3)
169 (format "mouse-%d" (+ 1 xterm-mouse-last)))
170 (t
171 (setq xterm-mouse-last type)
172 (format "down-mouse-%d" (+ 1 type))))))
173 (w (window-at x y))
174 (ltrb (window-edges w))
175 (left (nth 0 ltrb))
176 (top (nth 1 ltrb)))
177
178 (setq xterm-mouse-x x
179 xterm-mouse-y y)
180 (setq
181 last-input-event
182 (list mouse
183 (let ((event (if w
184 (posn-at-x-y (- x left) (- y top) w t)
185 (append (list nil 'menu-bar)
186 (nthcdr 2 (posn-at-x-y x y))))))
187 (setcar (nthcdr 3 event) timestamp)
188 event)))))
189
190 ;;;###autoload
191 (define-minor-mode xterm-mouse-mode
192 "Toggle XTerm mouse mode.
193 With prefix arg, turn XTerm mouse mode on iff arg is positive.
194
195 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
196 This works in terminal emulators compatible with xterm. It only
197 works for simple uses of the mouse. Basically, only non-modified
198 single clicks are supported. When turned on, the normal xterm
199 mouse functionality for such clicks is still available by holding
200 down the SHIFT key while pressing the mouse button."
201 :global t :group 'mouse
202 (if xterm-mouse-mode
203 ;; Turn it on
204 (unless window-system
205 (setq mouse-position-function #'xterm-mouse-position-function)
206 (turn-on-xterm-mouse-tracking))
207 ;; Turn it off
208 (turn-off-xterm-mouse-tracking 'force)
209 (setq mouse-position-function nil)))
210
211 (defun turn-on-xterm-mouse-tracking ()
212 "Enable Emacs mouse tracking in xterm."
213 (if xterm-mouse-mode
214 (send-string-to-terminal "\e[?1000h")))
215
216 (defun turn-off-xterm-mouse-tracking (&optional force)
217 "Disable Emacs mouse tracking in xterm."
218 (if (or force xterm-mouse-mode)
219 (send-string-to-terminal "\e[?1000l")))
220
221 ;; Restore normal mouse behaviour outside Emacs.
222 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
223 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
224 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
225
226 (provide 'xt-mouse)
227
228 ;; arch-tag: 84962d4e-fae9-4c13-a9d7-ef4925a4ac03
229 ;;; xt-mouse.el ends here