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