5119
|
1 ;;; avoid.el -- make mouse pointer stay out of the way of editing.
|
|
2
|
5745
|
3 ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
5119
|
4
|
|
5 ;; Author: Boris Goldowsky <boris@cs.rochester.edu>
|
|
6 ;; Keywords: mouse
|
5145
|
7 ;; Version: 1.10
|
5119
|
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
|
|
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24
|
|
25 ;;; Commentary:
|
|
26 ;;;
|
|
27 ;;; For those who are annoyed by the mouse pointer obscuring text,
|
|
28 ;;; this mode moves the mouse pointer - either just a little out of
|
|
29 ;;; the way, or all the way to the corner of the frame.
|
|
30 ;;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
|
|
31 ;;; To set up permanently, put this file on your load-path and put the
|
|
32 ;;; following in your .emacs:
|
|
33 ;;;
|
|
34 ;;; (cond (window-system
|
|
35 ;;; (require 'avoid)
|
|
36 ;;; (mouse-avoidance-mode 'cat-and-mouse)))
|
|
37 ;;;
|
|
38 ;;; The 'animate can be 'jump or 'banish or 'protean if you prefer.
|
5745
|
39 ;;; See the documentation for function `mouse-avoidance-mode' for
|
|
40 ;;; details of the different modes.
|
5119
|
41 ;;;
|
|
42 ;;; For added silliness, make the animatee animate...
|
|
43 ;;; put something similar to the following into your .emacs:
|
|
44 ;;;
|
|
45 ;;; (cond (window-system
|
|
46 ;;; (setq x-pointer-shape
|
|
47 ;;; (eval (nth (random 4)
|
|
48 ;;; '(x-pointer-man x-pointer-spider
|
|
49 ;;; x-pointer-gobbler x-pointer-gumby))))
|
|
50 ;;; (set-mouse-color (cdr (assoc 'mouse-color (frame-parameters))))))
|
|
51 ;;;
|
|
52 ;;; For completely random pointer shape, replace the setq above with:
|
|
53 ;;; (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
54 ;;;
|
|
55 ;;; Bugs & Warnings:
|
|
56 ;;;
|
5745
|
57 ;;; - Due to a bug in (mouse-position), this code can cause emacs
|
|
58 ;;; 19.22 to crash when deleting a frame if the mouse has not moved
|
|
59 ;;; since creating the frame. Versions earlier than 19.21 will
|
|
60 ;;; crash more easily; this program should not be used with them.
|
5119
|
61 ;;;
|
|
62 ;;; - Using this code does slow emacs down. "banish" mode shouldn't
|
|
63 ;;; ever be too bad though, and on my workstation even "animate" doesn't
|
|
64 ;;; seem to have a noticable effect.
|
|
65 ;;;
|
|
66 ;;; - There are some situations where it doesn't do what you expect,
|
|
67 ;;; notably when there are long wrapped lines in the buffer. Since
|
|
68 ;;; there is no low-level command for finding point's position
|
|
69 ;;; on the screen, it can fail to move the pointer when on such a line.
|
|
70
|
|
71 ;;; Credits:
|
|
72 ;;; This code was helped by all those who contributed suggestions, fixes, and
|
|
73 ;;; additions:
|
|
74 ;;; Joe Harrington (and his advisor), for the original inspiration
|
|
75 ;;; Ken Manheimer, for dreaming up the Protean mode
|
|
76 ;;; Richard Stallman, for the awful cat-and-mouse pun, among other things
|
|
77 ;;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
|
|
78 ;;; Simon Marshall, and M.S. Ashton, for their feedback.
|
|
79 ;;;
|
|
80 ;;; Code:
|
|
81
|
|
82 (provide 'avoid)
|
|
83
|
|
84 (defvar mouse-avoidance-mode nil
|
|
85 "Value is t or a symbol if the mouse pointer should avoid the cursor.
|
|
86 See function mouse-avoidance-mode for possible values. Changing this
|
|
87 variable is NOT the recommended way to change modes; use the function
|
|
88 instead.")
|
|
89
|
|
90 (defvar mouse-avoidance-nudge-dist 4
|
|
91 "*Average distance that mouse will be moved when approached by cursor.
|
|
92 Only applies in mode-avoidance-modes `animate' and `jump'.")
|
|
93
|
|
94 (defvar mouse-avoidance-nudge-var 3
|
|
95 "*Variability of mouse-avoidance-nudge-dist (which see).")
|
|
96
|
|
97 (defvar mouse-avoidance-animation-delay .01
|
|
98 "Delay between animation steps, in seconds.")
|
|
99
|
|
100 (defvar mouse-avoidance-threshhold 5
|
|
101 "*Mouse-pointer's flight distance.
|
|
102 If the cursor gets closer than this, the mouse pointer will move away.
|
|
103 Only applies in mouse-avoidance-modes `animate' and `jump'.")
|
|
104
|
|
105 ;; Internal variables for mouse-avoidance-random-shape
|
|
106 (defvar mouse-avoidance-pointer-shapes nil)
|
|
107 (defvar mouse-avoidance-n-pointer-shapes 0)
|
|
108
|
|
109 ;;; Functions:
|
|
110
|
|
111 (defun mouse-avoidance-too-close-p ()
|
|
112 ;; Return t if mouse pointer and point cursor are too close.
|
|
113 ;; Acceptable distance is defined by mouse-avoidance-threshhold.
|
|
114 (let ((mouse (mouse-position)))
|
|
115 (and (car (cdr mouse))
|
|
116 (< (abs (- (car (cdr mouse)) (current-column)))
|
|
117 mouse-avoidance-threshhold)
|
|
118 (< (abs (- (cdr (cdr mouse))
|
|
119 (+ (car (cdr (window-edges)))
|
|
120 (count-lines (window-start) (point)))))
|
|
121 mouse-avoidance-threshhold))))
|
|
122
|
|
123 (defun mouse-avoidance-banish-mouse ()
|
|
124 ;; Put the mouse pointer in the upper-right corner of the current frame.
|
|
125 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
|
|
126
|
|
127 (defun mouse-avoidance-nudge-mouse ()
|
|
128 ;; Push the mouse a little way away, possibly animating the move
|
|
129 (let* ((cur (mouse-position))
|
|
130 (deltax (* (+ mouse-avoidance-nudge-dist
|
|
131 (random mouse-avoidance-nudge-var))
|
|
132 (if (zerop (random 2)) 1 -1)))
|
|
133 (deltay (* (+ mouse-avoidance-nudge-dist
|
|
134 (random mouse-avoidance-nudge-var))
|
|
135 (if (zerop (random 2)) 1 -1))))
|
|
136 (if (or (eq mouse-avoidance-mode 'animate)
|
|
137 (eq mouse-avoidance-mode 'proteus))
|
|
138 (let ((i 0.0)
|
|
139 (color (cdr (assoc 'mouse-color (frame-parameters)))))
|
|
140 (while (<= i 1)
|
|
141 (set-mouse-position
|
|
142 (car cur)
|
5745
|
143 (mod (+ (car (cdr cur)) (round (* i deltax))) (frame-width))
|
|
144 (mod (+ (cdr (cdr cur)) (round (* i deltay))) (frame-height)))
|
5119
|
145 (setq i (+ i (/ 1.0 mouse-avoidance-nudge-dist)))
|
|
146 (if (eq mouse-avoidance-mode 'proteus)
|
|
147 (progn
|
|
148 (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
149 (set-mouse-color color)))
|
|
150 (sit-for mouse-avoidance-animation-delay)))
|
|
151 (set-mouse-position
|
|
152 (car cur)
|
|
153 (mod (+ (car (cdr cur)) deltax) (window-width))
|
|
154 (mod (+ (cdr (cdr cur)) deltay) (window-height))))))
|
|
155
|
|
156 (defun mouse-avoidance-random-shape ()
|
|
157 "Return a random cursor shape.
|
|
158 This assumes that any variable whose name begins with x-pointer- and
|
|
159 has an integer value is a valid cursor shape. You might want to
|
|
160 redefine this function to suit your own tastes."
|
|
161 (if (null mouse-avoidance-pointer-shapes)
|
|
162 (progn
|
|
163 (setq mouse-avoidance-pointer-shapes
|
|
164 (mapcar '(lambda (x) (symbol-value (intern x)))
|
|
165 (all-completions "x-pointer-" obarray
|
|
166 '(lambda (x)
|
|
167 (and (boundp x)
|
|
168 (integerp (symbol-value x)))))))
|
|
169 (setq mouse-avoidance-n-pointer-shapes
|
|
170 (length mouse-avoidance-pointer-shapes))))
|
|
171 (nth (random mouse-avoidance-n-pointer-shapes)
|
|
172 mouse-avoidance-pointer-shapes))
|
|
173
|
|
174 (defun mouse-avoidance-simple-hook ()
|
|
175 (if (and (mouse-avoidance-keyboard-command (this-command-keys)))
|
5745
|
176 (progn
|
|
177 (raise-frame (selected-frame))
|
|
178 (mouse-avoidance-banish-mouse))))
|
5119
|
179
|
|
180 (defun mouse-avoidance-fancy-hook ()
|
|
181 (if (and (mouse-avoidance-keyboard-command (this-command-keys))
|
|
182 (mouse-avoidance-too-close-p))
|
5745
|
183 (let ((old-pos (mouse-position)))
|
|
184 (mouse-avoidance-nudge-mouse)
|
|
185 (if (not (eq (selected-frame) (car old-pos)))
|
|
186 (apply 'set-mouse-position old-pos)))))
|
5119
|
187
|
|
188 (defun mouse-avoidance-keyboard-command (key)
|
|
189 "Return t if the KEYSEQENCE is composed of keyboard events only.
|
|
190 Returns nil if there are any lists in the key sequence."
|
|
191 (cond ((null key) nil) ; Null event seems to be
|
|
192 ; returned occasionally.
|
|
193 ((not (vectorp key)) t) ; Strings are keyboard events.
|
|
194 ((catch 'done
|
|
195 (let ((i 0)
|
|
196 (l (length key)))
|
|
197 (while (< i l)
|
|
198 (if (listp (aref key i))
|
|
199 (throw 'done nil))
|
|
200 (setq i (1+ i))))
|
|
201 t))))
|
|
202
|
|
203 (defun mouse-avoidance-mode (&optional mode)
|
|
204 "Set cursor avoidance mode to MODE.
|
|
205 MODE should be one of the symbols `banish', `jump', `animate',
|
5745
|
206 `cat-and-mouse', `proteus', or `none'.
|
|
207
|
|
208 If MODE is nil, toggle mouse avoidance between `none' and `banish'
|
|
209 modes. Positive numbers and symbols other than the above are treated
|
|
210 as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
|
|
211
|
|
212 Effects of the different modes:
|
|
213 * BANISH: Move the mouse to the upper-right corner on any keypress.
|
|
214 Also raises the frame.
|
|
215 * JUMP: If the cursor gets too close to the mouse, displace the mouse
|
|
216 a random distance & direction. If this would put it in another,
|
|
217 overlapping frame, it is put back \(until the next keypress).
|
|
218 * ANIMATE: As `jump', but shows steps along the way for illusion of motion.
|
|
219 * CAT-AND-MOUSE: Same as `animate'.
|
|
220 * PROTEUS: As `animate', but changes the shape of the mouse pointer too.
|
|
221
|
|
222 \(see `mouse-avoidance-threshhold' for definition of \"too close\",
|
|
223 and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
|
|
224 definition of \"random distance\".)"
|
5119
|
225 (interactive
|
|
226 (list (intern (completing-read
|
|
227 "Select cursor avoidance technique (SPACE for list): "
|
|
228 '(("banish") ("jump") ("animate") ("cat-and-mouse")
|
|
229 ("proteus") ("none"))
|
|
230 nil t))))
|
|
231 (if (eq mode 'cat-and-mouse)
|
|
232 (setq mode 'animate))
|
|
233 (setq post-command-hook
|
|
234 (delete 'mouse-avoidance-simple-hook (append post-command-hook nil)))
|
|
235 (setq post-command-hook
|
|
236 (delete 'mouse-avoidance-fancy-hook (append post-command-hook nil)))
|
|
237 (cond ((eq mode 'none)
|
|
238 (setq mouse-avoidance-mode nil))
|
|
239 ((or (eq mode 'jump)
|
|
240 (eq mode 'animate)
|
|
241 (eq mode 'proteus))
|
|
242 (add-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
|
|
243 (setq mouse-avoidance-mode mode))
|
|
244 ((or (eq mode 'banish)
|
|
245 (eq mode t)
|
|
246 (and (null mode) (null mouse-avoidance-mode))
|
|
247 (and mode (> (prefix-numeric-value mode) 0)))
|
|
248 (add-hook 'post-command-hook 'mouse-avoidance-simple-hook)
|
|
249 (setq mouse-avoidance-mode 'banish))
|
|
250 (t (setq mouse-avoidance-mode nil))))
|
|
251
|
|
252 (or (assq 'mouse-avoidance-mode minor-mode-alist)
|
|
253 (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
|
|
254 minor-mode-alist)))
|
|
255
|
|
256 ;;; End of avoid.el
|
|
257
|