Install workaround for "X protocol error: BadCursor (invalid Cursor parameter) on...
[emacs.git] / lisp / xt-mouse.el
blob075ea8792704fb314d6cf368004cdb1b499266ca
1 ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
3 ;; Copyright (C) 1994, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: mouse, terminals
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Enable mouse support when running inside an xterm.
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.
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.
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.
40 ;;; Todo:
42 ;; The xterm mouse escape codes are supposedly also supported by the
43 ;; Linux console, but I have not been able to verify this.
45 ;; Support multi-click -- somehow.
47 ;;; Code:
49 ;; XXX Perhaps this should be terminal-local instead. --lorentey
50 (define-key function-key-map "\e[M" 'xterm-mouse-translate)
52 (defvar xterm-mouse-last)
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))
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)))))
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")))
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)
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))))))))
106 ;; These two variables have been converted to terminal parameters.
108 ;;(defvar xterm-mouse-x 0
109 ;; "Position of last xterm mouse event relative to the frame.")
111 ;;(defvar xterm-mouse-y 0
112 ;; "Position of last xterm mouse event relative to the frame.")
114 ;; Indicator for the xterm-mouse mode.
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)
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)))
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)))
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)))
152 (set-terminal-parameter nil 'xterm-mouse-x x)
153 (set-terminal-parameter nil 'xterm-mouse-y y)
154 (setq
155 last-input-event
156 (if w
157 (list mouse (posn-at-x-y (- x left) (- y top) w t))
158 (list mouse
159 (append (list nil 'menu-bar) (nthcdr 2 (posn-at-x-y x y w t))))))))
161 ;;;###autoload
162 (define-minor-mode xterm-mouse-mode
163 "Toggle XTerm mouse mode.
164 With prefix arg, turn XTerm mouse mode on iff arg is positive.
166 Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
167 This works in terminal emulators compatible with xterm. It only
168 works for simple uses of the mouse. Basically, only non-modified
169 single clicks are supported. When turned on, the normal xterm
170 mouse functionality for such clicks is still available by holding
171 down the SHIFT key while pressing the mouse button."
172 :global t :group 'mouse
173 (if xterm-mouse-mode
174 ;; Turn it on
175 (progn
176 (setq mouse-position-function #'xterm-mouse-position-function)
177 (turn-on-xterm-mouse-tracking))
178 ;; Turn it off
179 (turn-off-xterm-mouse-tracking 'force)
180 (setq mouse-position-function nil)))
182 (defun turn-on-xterm-mouse-tracking ()
183 "Enable Emacs mouse tracking in xterm."
184 (dolist (f (frame-list))
185 (when (eq t (frame-live-p f))
186 (with-selected-frame f
187 (when xterm-mouse-mode
188 (send-string-to-terminal "\e[?1000h"))))))
190 (defun turn-off-xterm-mouse-tracking (&optional force)
191 "Disable Emacs mouse tracking in xterm."
192 (dolist (f (frame-list))
193 (when (eq t (frame-live-p f))
194 (with-selected-frame f
195 (when (or force xterm-mouse-mode)
196 (send-string-to-terminal "\e[?1000l"))))))
198 (defun turn-on-xterm-mouse-tracking-on-terminal (terminal)
199 "Enable xterm mouse tracking on TERMINAL."
200 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal)))
201 (send-string-to-terminal "\e[?1000h" terminal)))
203 (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
204 "Disable xterm mouse tracking on TERMINAL."
205 (when (and xterm-mouse-mode (eq t (terminal-live-p terminal)))
206 (send-string-to-terminal "\e[?1000l" terminal)))
208 (defun xterm-mouse-handle-delete-frame (frame)
209 "Turn off xterm mouse tracking if FRAME is the last frame on its device."
210 (when (and (eq t (frame-live-p frame))
211 (<= 1 (length (frames-on-display-list (frame-terminal frame)))))
212 (turn-off-xterm-mouse-tracking-on-terminal frame)))
214 ;; Frame creation and deletion.
215 (add-hook 'after-make-frame-functions 'turn-on-xterm-mouse-tracking-on-terminal)
216 (add-hook 'delete-frame-functions 'xterm-mouse-handle-delete-frame)
218 ;; Restore normal mouse behaviour outside Emacs.
219 (add-hook 'suspend-tty-functions 'turn-off-xterm-mouse-tracking-on-terminal)
220 (add-hook 'resume-tty-functions 'turn-on-xterm-mouse-tracking-on-terminal)
221 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
222 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
223 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
225 (provide 'xt-mouse)
227 ;;; arch-tag: 84962d4e-fae9-4c13-a9d7-ef4925a4ac03
228 ;;; xt-mouse.el ends here