(compilation-parse-errors):
[emacs.git] / lisp / xt-mouse.el
blob7711cbd4be97d5c997080c85f6492baad11cdd7d
1 ;;; xt-mouse.el --- Support the mouse when emacs run in an xterm.
3 ;; Copyright (C) 1994 Free Software Foundation
5 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
6 ;; Keywords: mouse, terminals
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Comments:
27 ;; Enable mouse support when running inside an xterm.
29 ;; This is actually useful when you are running X11 locally, but is
30 ;; working on remote machine over a modem line or through a gateway.
32 ;; It works by translating xterm escape codes into generic emacs mouse
33 ;; events so it should work with any package that uses the mouse.
35 ;;; Todo:
37 ;; Support multi-click -- somehow.
39 ;; Clicking on the mode-line does not work, although it should.
41 ;;; Code:
43 (define-key function-key-map "\e[M" 'xterm-mouse-translate)
45 (defvar xterm-mouse-last)
47 (defun xterm-mouse-translate (event)
48 ;; Read a click and release event from XTerm.
49 (save-excursion
50 (save-window-excursion
51 (deactivate-mark)
52 (let* ((xterm-mouse-last)
53 (down (xterm-mouse-event))
54 (down-command (nth 0 down))
55 (down-data (nth 1 down))
56 (down-where (nth 1 down-data))
57 (down-binding (key-binding (if (symbolp down-where)
58 (vector down-where down-command)
59 (vector down-command)))))
60 (or (and (eq (read-char) ?\e)
61 (eq (read-char) ?\[)
62 (eq (read-char) ?M))
63 (error "Unexpected escape sequence from XTerm"))
64 (let* ((click (xterm-mouse-event))
65 (click-command (nth 0 click))
66 (click-data (nth 1 click))
67 (click-where (nth 1 click-data)))
68 (if (memq down-binding '(nil ignore))
69 (if (and (symbolp click-where)
70 (not (eq 'menu-bar click-where)))
71 (vector (list click-where click-data) click)
72 (vector click))
73 (setq unread-command-events
74 (if (eq down-where click-where)
75 (list click)
76 (list
77 ;; Cheat `mouse-drag-region' with move event.
78 (list 'mouse-movement click-data)
79 ;; Generate a drag event.
80 (if (symbolp down-where)
82 (list (intern (concat "drag-mouse-" (+ 1 xterm-mouse-last)))
83 down-data click-data))
84 )))
85 (if (and (symbolp down-where)
86 (not (eq 'menu-bar down-where)))
87 (vector (list down-where down-data) down)
88 (vector down))))))))
90 (defvar xterm-mouse-x 0
91 "Position of last xterm mouse event relative to the frame.")
93 (defvar xterm-mouse-y 0
94 "Position of last xterm mouse event relative to the frame.")
96 (defadvice mouse-position (around xterm-mouse activate)
97 "Use last key from xterm-mouse-mode if available."
98 (let ((answer ad-do-it))
99 (setq ad-return-value
100 (if xterm-mouse-mode
101 (cons (car answer) (cons xterm-mouse-x xterm-mouse-y))
102 answer))))
104 (defun xterm-mouse-event ()
105 ;; Convert XTerm mouse event to Emacs mouse event.
106 (let* ((type (- (read-char) ? ))
107 (x (- (read-char) ? 1))
108 (y (- (read-char) ? 1))
109 (point (cons x y))
110 (window (window-at x y))
111 (where (if window
112 (coordinates-in-window-p point window)
113 'menu-bar))
114 (pos (if (consp where)
115 (progn
116 (select-window window)
117 (goto-char (window-start window))
118 (move-to-window-line (cdr where))
119 (move-to-column (+ (car where) (current-column)
120 (max 0 (1- (window-hscroll)))))
121 (point))
122 where))
123 (mouse (intern (if (eq type 3)
124 (concat "mouse-" (+ 1 xterm-mouse-last))
125 (setq xterm-mouse-last type)
126 (concat "down-mouse-" (+ 1 type))))))
127 (setq xterm-mouse-x x
128 xterm-mouse-y y)
129 (list mouse
130 (list window pos point
131 (/ (nth 2 (current-time)) 1000)))))
133 ;; Indicator for the xterm-mouse mode.
134 (defvar xterm-mouse-mode nil)
136 (or (assq 'xterm-mouse-mode minor-mode-alist)
137 (setq minor-mode-alist
138 (cons '(xterm-mouse-mode (" Mouse")) minor-mode-alist)))
140 ;;;###autoload
141 (defun xterm-mouse-mode (arg)
142 "Toggle XTerm mouse mode.
143 With prefix arg, turn XTerm mouse mode on iff arg is positive.
145 Turn it on to use emacs mouse commands, and off to use xterm mouse commands."
146 (interactive "P")
147 (if (or (and (null arg) xterm-mouse-mode)
148 (<= (prefix-numeric-value arg) 0))
149 ;; Turn it off
150 (if xterm-mouse-mode
151 (progn
152 (turn-off-xterm-mouse-tracking)
153 (setq xterm-mouse-mode nil)
154 (set-buffer-modified-p (buffer-modified-p))))
155 ;;Turn it on
156 (if xterm-mouse-mode
158 (setq xterm-mouse-mode t)
159 (turn-on-xterm-mouse-tracking)
160 (set-buffer-modified-p (buffer-modified-p)))))
162 (defun turn-on-xterm-mouse-tracking ()
163 ;; Enable emacs mouse tracking in xterm.
164 (if xterm-mouse-mode
165 (send-string-to-terminal "\e[?1000h")))
167 (defun turn-off-xterm-mouse-tracking ()
168 ;; Disable disable emacs mouse tracking in xterm.
169 (if xterm-mouse-mode
170 (send-string-to-terminal "\e[?1000l")))
172 ;; Restore normal mouse behaviour outside Emacs.
173 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
174 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
175 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
177 (provide 'xt-mouse)
179 ;;; xt-mouse.el ends here