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-event ()
130 "Convert XTerm mouse event to Emacs mouse event."
131 (let* ((type (- (xterm-mouse-event-read) #o40))
132 (x (- (xterm-mouse-event-read) #o40 1))
133 (y (- (xterm-mouse-event-read) #o40 1))
134 ;; Emulate timestamp information. This is accurate enough
135 ;; for default value of mouse-1-click-follows-link (450msec).
136 (timestamp (truncate
137 (* 1000
138 (- (float-time)
139 (or xt-mouse-epoch
140 (setq xt-mouse-epoch (float-time)))))))
141 (mouse (intern
142 ;; For buttons > 3, the release-event looks
143 ;; differently (see xc/programs/xterm/button.c,
144 ;; function EditorButton), and there seems to come in
145 ;; a release-event only, no down-event.
146 (cond ((>= type 64)
147 (format "mouse-%d" (- type 60)))
148 ((memq type '(8 9 10))
149 (setq xterm-mouse-last type)
150 (format "M-down-mouse-%d" (- type 7)))
151 ((= type 11)
152 (format "mouse-%d" (- xterm-mouse-last 7)))
153 ((= type 3)
154 (format "mouse-%d" (+ 1 xterm-mouse-last)))
155 (t
156 (setq xterm-mouse-last type)
157 (format "down-mouse-%d" (+ 1 type))))))
158 (w (window-at x y))
159 (ltrb (window-edges w))
160 (left (nth 0 ltrb))
161 (top (nth 1 ltrb)))
162
163 (setq xterm-mouse-x x
164 xterm-mouse-y y)
165 (setq
166 last-input-event
167 (list mouse
168 (let ((event (if w
169 (posn-at-x-y (- x left) (- y top) w t)
170 (append (list nil 'menu-bar)
171 (nthcdr 2 (posn-at-x-y x y))))))
172 (setcar (nthcdr 3 event) timestamp)
173 event)))))
174
175 ;;;###autoload
176 (define-minor-mode xterm-mouse-mode
177 "Toggle XTerm mouse mode.
178 With prefix arg, turn XTerm mouse mode on iff arg is positive.
179
180 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
181 This works in terminal emulators compatible with xterm. It only
182 works for simple uses of the mouse. Basically, only non-modified
183 single clicks are supported. When turned on, the normal xterm
184 mouse functionality for such clicks is still available by holding
185 down the SHIFT key while pressing the mouse button."
186 :global t :group 'mouse
187 (if xterm-mouse-mode
188 ;; Turn it on
189 (unless window-system
190 (setq mouse-position-function #'xterm-mouse-position-function)
191 (turn-on-xterm-mouse-tracking))
192 ;; Turn it off
193 (turn-off-xterm-mouse-tracking 'force)
194 (setq mouse-position-function nil)))
195
196 (defun turn-on-xterm-mouse-tracking ()
197 "Enable Emacs mouse tracking in xterm."
198 (if xterm-mouse-mode
199 (send-string-to-terminal "\e[?1000h")))
200
201 (defun turn-off-xterm-mouse-tracking (&optional force)
202 "Disable Emacs mouse tracking in xterm."
203 (if (or force xterm-mouse-mode)
204 (send-string-to-terminal "\e[?1000l")))
205
206 ;; Restore normal mouse behaviour outside Emacs.
207 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
208 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
209 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
210
211 (provide 'xt-mouse)
212
213 ;;; arch-tag: 84962d4e-fae9-4c13-a9d7-ef4925a4ac03
214 ;;; xt-mouse.el ends here