Merged from emacs@sv.gnu.org
[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 ;; XXX Perhaps this should be terminal-local instead. --lorentey
47 (define-key function-key-map "\e[M" 'xterm-mouse-translate)
48
49 (defvar xterm-mouse-last)
50
51 ;; Mouse events symbols must have an 'event-kind property with
52 ;; the value 'mouse-click.
53 (dolist (event-type '(mouse-1 mouse-2 mouse-3
54 M-down-mouse-1 M-down-mouse-2 M-down-mouse-3))
55 (put event-type 'event-kind 'mouse-click))
56
57 (defun xterm-mouse-translate (event)
58 "Read a click and release event from XTerm."
59 (save-excursion
60 (save-window-excursion
61 (deactivate-mark)
62 (let* ((xterm-mouse-last)
63 (down (xterm-mouse-event))
64 (down-command (nth 0 down))
65 (down-data (nth 1 down))
66 (down-where (nth 1 down-data))
67 (down-binding (key-binding (if (symbolp down-where)
68 (vector down-where down-command)
69 (vector down-command))))
70 (is-click (string-match "^mouse" (symbol-name (car down)))))
71
72 (unless is-click
73 (unless (and (eq (read-char) ?\e)
74 (eq (read-char) ?\[)
75 (eq (read-char) ?M))
76 (error "Unexpected escape sequence from XTerm")))
77
78 (let* ((click (if is-click down (xterm-mouse-event)))
79 (click-command (nth 0 click))
80 (click-data (nth 1 click))
81 (click-where (nth 1 click-data)))
82 (if (memq down-binding '(nil ignore))
83 (if (and (symbolp click-where)
84 (consp click-where))
85 (vector (list click-where click-data) click)
86 (vector click))
87 (setq unread-command-events
88 (if (eq down-where click-where)
89 (list click)
90 (list
91 ;; Cheat `mouse-drag-region' with move event.
92 (list 'mouse-movement click-data)
93 ;; Generate a drag event.
94 (if (symbolp down-where)
95 0
96 (list (intern (format "drag-mouse-%d"
97 (+ 1 xterm-mouse-last)))
98 down-data click-data)))))
99 (if (and (symbolp down-where)
100 (consp down-where))
101 (vector (list down-where down-data) down)
102 (vector down))))))))
103
104 ;; These two variables have been converted to terminal parameters.
105 ;;
106 ;;(defvar xterm-mouse-x 0
107 ;; "Position of last xterm mouse event relative to the frame.")
108 ;;
109 ;;(defvar xterm-mouse-y 0
110 ;; "Position of last xterm mouse event relative to the frame.")
111
112 (defvar xt-mouse-epoch nil)
113
114 ;; Indicator for the xterm-mouse mode.
115
116 (defun xterm-mouse-position-function (pos)
117 "Bound to `mouse-position-function' in XTerm mouse mode."
118 (when (terminal-parameter nil 'xterm-mouse-x)
119 (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
120 (terminal-parameter nil 'xterm-mouse-y))))
121 pos)
122
123 ;; read xterm sequences above ascii 127 (#x7f)
124 (defun xterm-mouse-event-read ()
125 (let ((c (read-char)))
126 (if (< c 0)
127 (+ c #x8000000 128)
128 c)))
129
130 (defun xterm-mouse-event ()
131 "Convert XTerm mouse event to Emacs mouse event."
132 (let* ((type (- (xterm-mouse-event-read) #o40))
133 (x (- (xterm-mouse-event-read) #o40 1))
134 (y (- (xterm-mouse-event-read) #o40 1))
135 ;; Emulate timestamp information. This is accurate enough
136 ;; for default value of mouse-1-click-follows-link (450msec).
137 (timestamp (truncate
138 (* 1000
139 (- (float-time)
140 (or xt-mouse-epoch
141 (setq xt-mouse-epoch (float-time)))))))
142 (mouse (intern
143 ;; For buttons > 3, the release-event looks
144 ;; differently (see xc/programs/xterm/button.c,
145 ;; function EditorButton), and there seems to come in
146 ;; a release-event only, no down-event.
147 (cond ((>= type 64)
148 (format "mouse-%d" (- type 60)))
149 ((memq type '(8 9 10))
150 (setq xterm-mouse-last type)
151 (format "M-down-mouse-%d" (- type 7)))
152 ((= type 11)
153 (format "mouse-%d" (- xterm-mouse-last 7)))
154 ((= type 3)
155 (format "mouse-%d" (+ 1 xterm-mouse-last)))
156 (t
157 (setq xterm-mouse-last type)
158 (format "down-mouse-%d" (+ 1 type))))))
159 (w (window-at x y))
160 (ltrb (window-edges w))
161 (left (nth 0 ltrb))
162 (top (nth 1 ltrb)))
163
164 (set-terminal-parameter nil 'xterm-mouse-x x)
165 (set-terminal-parameter nil 'xterm-mouse-y y)
166 (setq
167 last-input-event
168 (list mouse
169 (let ((event (if w
170 (posn-at-x-y (- x left) (- y top) w t)
171 (append (list nil 'menu-bar)
172 (nthcdr 2 (posn-at-x-y x y))))))
173 (setcar (nthcdr 3 event) timestamp)
174 event)))))
175
176 ;;;###autoload
177 (define-minor-mode xterm-mouse-mode
178 "Toggle XTerm mouse mode.
179 With prefix arg, turn XTerm mouse mode on iff arg is positive.
180
181 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
182 This works in terminal emulators compatible with xterm. It only
183 works for simple uses of the mouse. Basically, only non-modified
184 single clicks are supported. When turned on, the normal xterm
185 mouse functionality for such clicks is still available by holding
186 down the SHIFT key while pressing the mouse button."
187 :global t :group 'mouse
188 (if xterm-mouse-mode
189 ;; Turn it on
190 (progn
191 (setq mouse-position-function #'xterm-mouse-position-function)
192 (turn-on-xterm-mouse-tracking))
193 ;; Turn it off
194 (turn-off-xterm-mouse-tracking 'force)
195 (setq mouse-position-function nil)))
196
197 (defun turn-on-xterm-mouse-tracking ()
198 "Enable Emacs mouse tracking in xterm."
199 (dolist (f (frame-list))
200 (when (eq t (frame-live-p f))
201 (with-selected-frame f
202 (when xterm-mouse-mode
203 (send-string-to-terminal "\e[?1000h"))))))
204
205 (defun turn-off-xterm-mouse-tracking (&optional force)
206 "Disable Emacs mouse tracking in xterm."
207 (dolist (f (frame-list))
208 (when (eq t (frame-live-p f))
209 (with-selected-frame f
210 (when (or force xterm-mouse-mode)
211 (send-string-to-terminal "\e[?1000l"))))))
212
213 (defun turn-on-xterm-mouse-tracking-on-terminal (terminal)
214 "Enable xterm mouse tracking on TERMINAL."
215 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal)))
216 (send-string-to-terminal "\e[?1000h" terminal)))
217
218 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
219 "Disable xterm mouse tracking on TERMINAL."
220 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal)))
221 (send-string-to-terminal "\e[?1000l" terminal)))
222
223 (defun xterm-mouse-handle-delete-frame (frame)
224 "Turn off xterm mouse tracking if FRAME is the last frame on its device."
225 (when (and (eq t (frame-live-p frame))
226 (<= 1 (length (frames-on-display-list (frame-terminal frame)))))
227 (turn-off-xterm-mouse-tracking-on-terminal frame)))
228
229 ;; Frame creation and deletion.
230 (add-hook 'after-make-frame-functions 'turn-on-xterm-mouse-tracking-on-terminal)
231 (add-hook 'delete-frame-functions 'xterm-mouse-handle-delete-frame)
232
233 ;; Restore normal mouse behaviour outside Emacs.
234 (add-hook 'suspend-tty-functions 'turn-off-xterm-mouse-tracking-on-terminal)
235 (add-hook 'resume-tty-functions 'turn-on-xterm-mouse-tracking-on-terminal)
236 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
237 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
238 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
239
240 (provide 'xt-mouse)
241
242 ;;; arch-tag: 84962d4e-fae9-4c13-a9d7-ef4925a4ac03
243 ;;; xt-mouse.el ends here