13163
|
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 ;; Emacs only generates down events when needed.
|
|
40 ;; This is too hard to emulate, so we cheat instead.
|
|
41 (or (lookup-key global-map [ down-mouse-1 ])
|
|
42 (define-key global-map [ down-mouse-1 ] 'ignore))
|
|
43 (or (lookup-key global-map [ down-mouse-2 ])
|
|
44 (define-key global-map [ down-mouse-2 ] 'ignore))
|
|
45 (or (lookup-key global-map [ down-mouse-3 ])
|
|
46 (define-key global-map [ down-mouse-3 ] 'ignore))
|
|
47
|
|
48 (define-key function-key-map "\e[M" 'xterm-mouse-translate)
|
|
49
|
|
50 (defun xterm-mouse-translate (event)
|
|
51 ;; Read a click and release event from XTerm.
|
|
52 (save-excursion
|
|
53 (save-window-excursion
|
|
54 (deactivate-mark)
|
|
55 (let* ((last)
|
|
56 (down (xterm-mouse-event)))
|
|
57 (or (and (eq (read-char) ?\e)
|
|
58 (eq (read-char) ?\[)
|
|
59 (eq (read-char) ?M))
|
|
60 (error "Unexpected escape sequence from XTerm"))
|
|
61 (let ((click (xterm-mouse-event)))
|
|
62 (setq unread-command-events
|
|
63 (append unread-command-events
|
|
64 (if (eq (nth 1 (nth 1 down)) (nth 1 (nth 1 click)))
|
|
65 (list click)
|
|
66 (list
|
|
67 ;; Generate move event to cheat `mouse-drag-region'.
|
|
68 (list 'mouse-movement (nth 1 click))
|
|
69 ;; Generate a drag event.
|
|
70 (list (intern (concat "drag-mouse-" (+ 1 last)))
|
|
71 (nth 1 down) (nth 1 click)))))))
|
|
72 (vector down)))))
|
|
73
|
|
74 (defun xterm-mouse-event ()
|
|
75 ;; Convert XTerm mouse event to Emacs mouse event.
|
|
76 (let* ((type (- (read-char) ? ))
|
|
77 (x (- (read-char) ? 1))
|
|
78 (y (- (read-char) ? 1))
|
|
79 (point (cons x y))
|
|
80 (window (window-at x y))
|
|
81 (where (coordinates-in-window-p point window))
|
|
82 (pos (if (consp where)
|
|
83 (progn
|
|
84 (select-window window)
|
|
85 (goto-char (window-start window))
|
|
86 (move-to-window-line (cdr where))
|
|
87 (move-to-column (+ (car where) (current-column)
|
|
88 (max 0 (1- (window-hscroll)))))
|
|
89 (point))
|
|
90 where))
|
|
91 (mouse (intern (if (eq type 3)
|
|
92 (concat "mouse-" (+ 1 last))
|
|
93 (setq last type)
|
|
94 (concat "down-mouse-" (+ 1 type))))))
|
|
95 (list mouse
|
|
96 (list window pos point
|
|
97 (/ (nth 2 (current-time)) 1000)))))
|
|
98
|
|
99 ;; Indicator for the xterm-mouse mode.
|
|
100 (defvar xterm-mouse-mode nil)
|
|
101
|
|
102 (or (assq 'xterm-mouse-mode minor-mode-alist)
|
|
103 (setq minor-mode-alist
|
|
104 (cons '(xterm-mouse-mode (" Mouse")) minor-mode-alist)))
|
|
105
|
|
106 ;;;###autoload
|
|
107 (defun xterm-mouse-mode (arg)
|
|
108 "Toggle XTerm mouse mode.
|
|
109 With prefix arg, turn XTerm mouse mode on iff arg is positive.
|
|
110
|
|
111 Turn it on to use emacs mouse commands, and off to use xterm mouse commands."
|
|
112 (interactive "P")
|
|
113 (if (or (and (null arg) xterm-mouse-mode)
|
|
114 (<= (prefix-numeric-value arg) 0))
|
|
115 ;; Turn it off
|
|
116 (if xterm-mouse-mode
|
|
117 (progn
|
|
118 (turn-off-xterm-mouse-tracking)
|
|
119 (setq xterm-mouse-mode nil)
|
|
120 (set-buffer-modified-p (buffer-modified-p))))
|
|
121 ;;Turn it on
|
|
122 (if xterm-mouse-mode
|
|
123 ()
|
|
124 (setq xterm-mouse-mode t)
|
|
125 (turn-on-xterm-mouse-tracking)
|
|
126 (set-buffer-modified-p (buffer-modified-p)))))
|
|
127
|
|
128 (defun turn-on-xterm-mouse-tracking ()
|
|
129 ;; Enable emacs mouse tracking in xterm.
|
|
130 (if xterm-mouse-mode
|
|
131 (send-string-to-terminal "\e[?1000h")))
|
|
132
|
|
133 (defun turn-off-xterm-mouse-tracking ()
|
|
134 ;; Disable disable emacs mouse tracking in xterm.
|
|
135 (if xterm-mouse-mode
|
|
136 (send-string-to-terminal "\e[?1000l")))
|
|
137
|
|
138 ;; Restore normal mouse behaviour outside Emacs.
|
|
139 (add-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
|
|
140 (add-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
|
|
141 (add-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking)
|
|
142
|
|
143 (provide 'xt-mouse)
|
|
144
|
|
145 ;;; xt-mouse.el ends here
|