2
|
1 ;;; avoid.el --- make mouse pointer stay out of the way of editing
|
0
|
2
|
|
3 ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
4
|
2
|
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
|
0
|
6 ;; Keywords: mouse
|
|
7 ;; Version: 1.10
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
0
|
25
|
2
|
26 ;;; Synched up with: FSF 19.34.
|
0
|
27
|
|
28 ;;; Commentary:
|
|
29
|
2
|
30 ;; For those who are annoyed by the mouse pointer obscuring text,
|
|
31 ;; this mode moves the mouse pointer - either just a little out of
|
|
32 ;; the way, or all the way to the corner of the frame.
|
|
33 ;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
|
|
34 ;; To set up permanently, put this file on your .emacs:
|
|
35 ;;
|
|
36 ;; (if window-system (mouse-avoidance-mode 'animate))
|
|
37 ;;
|
|
38 ;; The 'animate can be 'jump or 'banish or 'exile or 'protean if you prefer.
|
|
39 ;; See the documentation for function `mouse-avoidance-mode' for
|
|
40 ;; details of the different modes.
|
|
41 ;;
|
|
42 ;; For added silliness, make the animatee animate...
|
|
43 ;; put something similar to the following into your .emacs:
|
|
44 ;;
|
|
45 ;; (if window-system
|
|
46 ;; (mouse-avoidance-set-pointer-shape
|
|
47 ;; (eval (nth (random 4)
|
|
48 ;; '(x-pointer-man x-pointer-spider
|
|
49 ;; x-pointer-gobbler x-pointer-gumby)))))
|
|
50 ;;
|
|
51 ;; For completely random pointer shape, replace the setq above with:
|
|
52 ;; (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
53 ;;
|
|
54 ;; Bugs / Warnings / To-Do:
|
|
55 ;;
|
|
56 ;; - Using this code does slow emacs down. "banish" mode shouldn't
|
|
57 ;; be too bad, and on my workstation even "animate" is reasonable.
|
|
58 ;;
|
|
59 ;; - It ought find out where any overlapping frames are and avoid them,
|
|
60 ;; rather than always raising the frame.
|
|
61
|
|
62 ;; Credits:
|
|
63 ;; This code was helped by all those who contributed suggestions,
|
|
64 ;; fixes, and additions
|
|
65 ;; Joe Harrington (and his advisor), for the original inspiration.
|
|
66 ;; Ken Manheimer, for dreaming up the Protean mode.
|
|
67 ;; Richard Stallman, for the awful cat-and-mouse pun, among other things.
|
|
68 ;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
|
|
69 ;; Simon Marshall, and M.S. Ashton, for their feedback.
|
|
70
|
0
|
71 ;;; Code:
|
|
72
|
|
73 (provide 'avoid)
|
|
74
|
14
|
75 ;;;###autoload
|
0
|
76 (defvar mouse-avoidance-mode nil
|
|
77 "Value is t or a symbol if the mouse pointer should avoid the cursor.
|
|
78 See function `mouse-avoidance-mode' for possible values. Changing this
|
|
79 variable is NOT the recommended way to change modes; use that function
|
|
80 instead.")
|
|
81
|
|
82 (defvar mouse-avoidance-nudge-dist 15
|
|
83 "*Average distance that mouse will be moved when approached by cursor.
|
|
84 Only applies in mouse-avoidance-mode `jump' and its derivatives.
|
|
85 For best results make this larger than `mouse-avoidance-threshold'.")
|
|
86
|
|
87 (defvar mouse-avoidance-nudge-var 10
|
|
88 "*Variability of `mouse-avoidance-nudge-dist' (which see).")
|
|
89
|
|
90 (defvar mouse-avoidance-animation-delay .01
|
|
91 "Delay between animation steps, in seconds.")
|
|
92
|
|
93 (defvar mouse-avoidance-threshold 5
|
|
94 "*Mouse-pointer's flight distance.
|
|
95 If the cursor gets closer than this, the mouse pointer will move away.
|
|
96 Only applies in mouse-avoidance-modes `animate' and `jump'.")
|
|
97
|
|
98 ;; Internal variables
|
|
99 (defvar mouse-avoidance-state nil)
|
|
100 (defvar mouse-avoidance-pointer-shapes nil)
|
|
101 (defvar mouse-avoidance-n-pointer-shapes 0)
|
2
|
102 (defvar mouse-avoidance-old-pointer-shape nil)
|
0
|
103
|
|
104 ;;; Functions:
|
|
105
|
2
|
106 (defsubst mouse-avoidance-set-pointer-shape (shape)
|
|
107 "Set the shape of the mouse pointer to SHAPE."
|
|
108 (setq x-pointer-shape shape)
|
|
109 (set-mouse-color nil))
|
|
110
|
|
111 ;; XEmacs change -- this is so ugly. [FSF version is totally different -sb]
|
0
|
112 (defun mouse-avoidance-point-position ()
|
|
113 "Returns (WINDOW X . Y) of current point - analogous to mouse-position"
|
|
114 (let* ((beg (window-start))
|
|
115 (pos (point))
|
|
116 (col (current-column))
|
|
117 (row))
|
|
118 (setq row (count-lines beg pos))
|
|
119 (cons (selected-window) (cons col row))))
|
|
120
|
|
121 ;(defun mouse-avoidance-point-position-test ()
|
|
122 ; (interactive)
|
|
123 ; (message (format "point=%s mouse=%s"
|
|
124 ; (cdr (mouse-avoidance-point-position))
|
|
125 ; (cdr (mouse-position)))))
|
|
126
|
|
127 (defun mouse-avoidance-set-mouse-position (pos)
|
|
128 ;; Carefully set mouse position to given position (X . Y)
|
|
129 ;; Ideally, should check if X,Y is in the current frame, and if not,
|
|
130 ;; leave the mouse where it was. However, this is currently
|
|
131 ;; difficult to do, so we just raise the frame to avoid frame switches.
|
|
132 ;; Returns t if it moved the mouse.
|
|
133 (let ((f (selected-frame)))
|
|
134 (raise-frame f)
|
2
|
135 ;; XEmacs: FSF version of set-mouse-position requires FRAME parameter
|
0
|
136 (set-mouse-position (frame-selected-window f) (car pos) (cdr pos))
|
|
137 t))
|
|
138
|
|
139 (defun mouse-avoidance-too-close-p (mouse)
|
|
140 ;; Return t if mouse pointer and point cursor are too close.
|
|
141 ;; Acceptable distance is defined by mouse-avoidance-threshold.
|
|
142 (let ((point (mouse-avoidance-point-position)))
|
|
143 (and (eq (car mouse) (car point))
|
|
144 (car (cdr mouse))
|
|
145 (< (abs (- (car (cdr mouse)) (car (cdr point))))
|
|
146 mouse-avoidance-threshold)
|
|
147 (< (abs (- (cdr (cdr mouse)) (cdr (cdr point))))
|
|
148 mouse-avoidance-threshold))))
|
|
149
|
|
150 (defun mouse-avoidance-banish-destination ()
|
|
151 "The position to which mouse-avoidance-mode `banish' moves the mouse.
|
|
152 You can redefine this if you want the mouse banished to a different corner."
|
|
153 (cons (1- (frame-width))
|
|
154 0))
|
|
155
|
|
156 (defun mouse-avoidance-banish-mouse ()
|
|
157 ;; Put the mouse pointer in the upper-right corner of the current frame.
|
|
158 (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
|
|
159
|
|
160 (defsubst mouse-avoidance-delta (cur delta dist var min max)
|
|
161 ;; Decide how far to move in either dimension.
|
|
162 ;; Args are the CURRENT location, the desired DELTA for
|
|
163 ;; warp-conservation, the DISTANCE we like to move, the VARIABILITY
|
|
164 ;; in distance allowed, and the MIN and MAX possible window positions.
|
2
|
165 ;; Returns something as close to DELTA as possible within the constraints.
|
0
|
166 (let ((L1 (max (- min cur) (+ (- dist) (- var))))
|
|
167 (R1 (+ (- dist) var ))
|
|
168 (L2 (+ dist (- var)))
|
|
169 (R2 (min (- max cur) (+ dist var))))
|
|
170 (if (< R1 (- min cur)) (setq L1 nil R1 nil))
|
|
171 (if (> L2 (- max cur)) (setq L2 nil R2 nil))
|
|
172 (cond ((and L1 (< delta L1)) L1)
|
|
173 ((and R1 (< delta R1)) delta)
|
|
174 ((and R1 (< delta 0)) R1)
|
|
175 ((and L2 (< delta L2)) L2)
|
|
176 ((and R2 (< delta R2)) delta)
|
|
177 (R2)
|
|
178 ((or R1 L2))
|
|
179 (t 0))))
|
|
180
|
|
181 (defun mouse-avoidance-nudge-mouse ()
|
|
182 ;; Push the mouse a little way away, possibly animating the move
|
|
183 ;; For these modes, state keeps track of the total offset that we've
|
|
184 ;; accumulated, and tries to keep it close to zero.
|
|
185 (let* ((cur (mouse-position))
|
2
|
186 (cur-frame (car cur))
|
0
|
187 (cur-pos (cdr cur))
|
|
188 (deltax (mouse-avoidance-delta
|
|
189 (car cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
190 (car mouse-avoidance-state))
|
|
191 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
|
192 0 (frame-width)))
|
|
193 (deltay (mouse-avoidance-delta
|
|
194 (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
195 (cdr mouse-avoidance-state))
|
|
196 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
2
|
197 0 (frame-height))))
|
0
|
198 (setq mouse-avoidance-state
|
|
199 (cons (+ (car mouse-avoidance-state) deltax)
|
|
200 (+ (cdr mouse-avoidance-state) deltay)))
|
|
201 (if (or (eq mouse-avoidance-mode 'animate)
|
|
202 (eq mouse-avoidance-mode 'proteus))
|
|
203 (let ((i 0.0)
|
2
|
204 ;; XEmacs change
|
0
|
205 (color (cdr (assoc 'mouse-color (frame-parameters)))))
|
|
206 (while (<= i 1)
|
|
207 (mouse-avoidance-set-mouse-position
|
|
208 (cons (+ (car cur-pos) (round (* i deltax)))
|
|
209 (+ (cdr cur-pos) (round (* i deltay)))))
|
|
210 (setq i (+ i (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
|
|
211 (if (eq mouse-avoidance-mode 'proteus)
|
2
|
212 ;; XEmacs change
|
0
|
213 (progn
|
|
214 (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
215 (set-mouse-color color)))
|
|
216 (sit-for mouse-avoidance-animation-delay)))
|
|
217 (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
|
|
218 (+ (cdr (cdr cur)) deltay))))))
|
|
219
|
|
220 (defun mouse-avoidance-random-shape ()
|
|
221 "Return a random cursor shape.
|
|
222 This assumes that any variable whose name begins with x-pointer- and
|
|
223 has an integer value is a valid cursor shape. You might want to
|
|
224 redefine this function to suit your own tastes."
|
|
225 (if (null mouse-avoidance-pointer-shapes)
|
|
226 (progn
|
|
227 (setq mouse-avoidance-pointer-shapes
|
|
228 (mapcar '(lambda (x) (symbol-value (intern x)))
|
|
229 (all-completions "x-pointer-" obarray
|
|
230 '(lambda (x)
|
|
231 (and (boundp x)
|
|
232 (integerp (symbol-value x)))))))
|
|
233 (setq mouse-avoidance-n-pointer-shapes
|
|
234 (length mouse-avoidance-pointer-shapes))))
|
|
235 (nth (random mouse-avoidance-n-pointer-shapes)
|
|
236 mouse-avoidance-pointer-shapes))
|
|
237
|
|
238 (defun mouse-avoidance-banish-hook ()
|
|
239 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
240 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
241 (mouse-avoidance-banish-mouse)))
|
|
242
|
|
243 (defun mouse-avoidance-exile-hook ()
|
|
244 ;; For exile mode, the state is nil when the mouse is in its normal
|
|
245 ;; position, and set to the old mouse-position when the mouse is in exile.
|
|
246 (if (and (not executing-kbd-macro)
|
|
247 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
248 (let ((mp (mouse-position)))
|
|
249 (cond ((and (not mouse-avoidance-state)
|
|
250 (mouse-avoidance-too-close-p mp))
|
|
251 (setq mouse-avoidance-state mp)
|
|
252 (mouse-avoidance-banish-mouse))
|
|
253 ((and mouse-avoidance-state
|
|
254 (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
|
2
|
255 ;; XEmacs change
|
0
|
256 (if (and (eq (car mp) (if (< emacs-minor-version 12)
|
|
257 (selected-frame)
|
|
258 (selected-window)))
|
|
259 (equal (cdr mp) (mouse-avoidance-banish-destination)))
|
|
260 (mouse-avoidance-set-mouse-position
|
|
261 ;; move back only if user has not moved mouse
|
|
262 (cdr mouse-avoidance-state)))
|
|
263 ;; but clear state anyway, to be ready for another move
|
|
264 (setq mouse-avoidance-state nil))))))
|
|
265
|
|
266 (defun mouse-avoidance-fancy-hook ()
|
|
267 ;; Used for the "fancy" modes, ie jump et al.
|
|
268 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
269 (mouse-avoidance-kbd-command (this-command-keys))
|
|
270 (mouse-avoidance-too-close-p (mouse-position)))
|
|
271 (let ((old-pos (mouse-position)))
|
|
272 (mouse-avoidance-nudge-mouse)
|
2
|
273 ;; XEmacs change
|
0
|
274 (if (not (eq (if (< emacs-minor-version 12)
|
|
275 (selected-frame)
|
|
276 (selected-window)) (car old-pos))) ; move went awry
|
|
277 (set-mouse-position (car old-pos) ; sigh..
|
|
278 (car (cdr old-pos))
|
|
279 (cdr (cdr old-pos)))))))
|
|
280
|
|
281 (defun mouse-avoidance-kbd-command (key)
|
|
282 "Return t if the KEYSEQENCE is composed of keyboard events only.
|
|
283 Return nil if there are any lists in the key sequence."
|
|
284 (cond ((null key) nil) ; Null event seems to be
|
|
285 ; returned occasionally.
|
|
286 ((not (vectorp key)) t) ; Strings are keyboard events.
|
|
287 ((catch 'done
|
|
288 (let ((i 0)
|
|
289 (l (length key)))
|
|
290 (while (< i l)
|
|
291 (if (listp (aref key i))
|
|
292 (throw 'done nil))
|
|
293 (setq i (1+ i))))
|
|
294 t))))
|
|
295
|
2
|
296 ;;;###autoload
|
0
|
297 (defun mouse-avoidance-mode (&optional mode)
|
|
298 "Set cursor avoidance mode to MODE.
|
|
299 MODE should be one of the symbols `banish', `exile', `jump', `animate',
|
|
300 `cat-and-mouse', `proteus', or `none'.
|
|
301
|
|
302 If MODE is nil, toggle mouse avoidance between `none` and `banish'
|
|
303 modes. Positive numbers and symbols other than the above are treated
|
|
304 as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
|
|
305
|
|
306 Effects of the different modes:
|
|
307 * banish: Move the mouse to the upper-right corner on any keypress.
|
|
308 * exile: Move the mouse to the corner only if the cursor gets too close,
|
|
309 and allow it to return once the cursor is out of the way.
|
|
310 * jump: If the cursor gets too close to the mouse, displace the mouse
|
|
311 a random distance & direction.
|
|
312 * animate: As `jump', but shows steps along the way for illusion of motion.
|
|
313 * cat-and-mouse: Same as `animate'.
|
|
314 * proteus: As `animate', but changes the shape of the mouse pointer too.
|
|
315
|
|
316 Whenever the mouse is moved, the frame is also raised.
|
|
317
|
|
318 \(see `mouse-avoidance-threshold' for definition of \"too close\",
|
|
319 and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
|
|
320 definition of \"random distance\".)"
|
|
321 (interactive
|
|
322 (list (intern (completing-read
|
|
323 "Select cursor avoidance technique (SPACE for list): "
|
|
324 '(("banish") ("exile") ("jump") ("animate")
|
|
325 ("cat-and-mouse") ("proteus") ("none"))
|
|
326 nil t))))
|
|
327 (if (eq mode 'cat-and-mouse)
|
|
328 (setq mode 'animate))
|
2
|
329 ;; XEmacs change - We don't have post-command-idle-hook
|
|
330 (remove-hook 'post-command-hook 'mouse-avoidance-banish-hook)
|
|
331 (remove-hook 'post-command-hook 'mouse-avoidance-exile-hook)
|
|
332 (remove-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
|
|
333 ;; Restore pointer shape if necessary
|
|
334 (if (eq mouse-avoidance-mode 'proteus)
|
|
335 (mouse-avoidance-set-pointer-shape mouse-avoidance-old-pointer-shape))
|
|
336
|
|
337 ;; Do additional setup depending on version of mode requested
|
0
|
338 (cond ((eq mode 'none)
|
|
339 (setq mouse-avoidance-mode nil))
|
|
340 ((or (eq mode 'jump)
|
|
341 (eq mode 'animate)
|
|
342 (eq mode 'proteus))
|
2
|
343 ;; XEmacs: we don't have post-command-idle-hook
|
0
|
344 (add-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
|
|
345 (setq mouse-avoidance-mode mode
|
2
|
346 mouse-avoidance-state (cons 0 0)
|
|
347 mouse-avoidance-old-pointer-shape x-pointer-shape))
|
0
|
348 ((eq mode 'exile)
|
2
|
349 ;; XEmacs: FSF uses post-command-idle-hook
|
0
|
350 (add-hook 'post-command-hook 'mouse-avoidance-exile-hook)
|
|
351 (setq mouse-avoidance-mode mode
|
|
352 mouse-avoidance-state nil))
|
|
353 ((or (eq mode 'banish)
|
|
354 (eq mode t)
|
|
355 (and (null mode) (null mouse-avoidance-mode))
|
|
356 (and mode (> (prefix-numeric-value mode) 0)))
|
2
|
357 ;; XEmacs: FSF uses post-command-idle-hook
|
0
|
358 (add-hook 'post-command-hook 'mouse-avoidance-banish-hook)
|
|
359 (setq mouse-avoidance-mode 'banish))
|
|
360 (t (setq mouse-avoidance-mode nil)))
|
|
361 (force-mode-line-update))
|
|
362
|
|
363 ;(or (assq 'mouse-avoidance-mode minor-mode-alist)
|
|
364 ; (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
|
|
365 ; minor-mode-alist)))
|
|
366 ;;XEmacs: do it right.
|
|
367 ;;;###autoload
|
|
368 (add-minor-mode 'mouse-avoidance-mode " Avoid")
|
|
369
|
14
|
370 ;;; avoid.el ends here
|