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