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
|
134
|
75 (defgroup avoid nil
|
|
76 "Make mouse pointer stay out of the way of editing."
|
|
77 :prefix "mouse-avoidance-"
|
|
78 :group 'mouse)
|
|
79
|
|
80
|
80
|
81 ;;;###autoload
|
0
|
82 (defvar mouse-avoidance-mode nil
|
|
83 "Value is t or a symbol if the mouse pointer should avoid the cursor.
|
|
84 See function `mouse-avoidance-mode' for possible values. Changing this
|
|
85 variable is NOT the recommended way to change modes; use that function
|
|
86 instead.")
|
|
87
|
134
|
88 (defcustom mouse-avoidance-nudge-dist 15
|
0
|
89 "*Average distance that mouse will be moved when approached by cursor.
|
|
90 Only applies in mouse-avoidance-mode `jump' and its derivatives.
|
134
|
91 For best results make this larger than `mouse-avoidance-threshold'."
|
|
92 :type 'integer
|
|
93 :group 'avoid)
|
0
|
94
|
134
|
95 (defcustom mouse-avoidance-nudge-var 10
|
|
96 "*Variability of `mouse-avoidance-nudge-dist' (which see)."
|
|
97 :type 'integer
|
|
98 :group 'avoid)
|
0
|
99
|
134
|
100 (defcustom mouse-avoidance-animation-delay .01
|
|
101 "Delay between animation steps, in seconds."
|
|
102 :type 'number
|
|
103 :group 'avoid)
|
|
104
|
|
105 (defcustom mouse-avoidance-threshold 5
|
0
|
106 "*Mouse-pointer's flight distance.
|
|
107 If the cursor gets closer than this, the mouse pointer will move away.
|
134
|
108 Only applies in mouse-avoidance-modes `animate' and `jump'."
|
|
109 :type 'integer
|
|
110 :group 'avoid)
|
0
|
111
|
|
112 ;; Internal variables
|
|
113 (defvar mouse-avoidance-state nil)
|
|
114 (defvar mouse-avoidance-pointer-shapes nil)
|
|
115 (defvar mouse-avoidance-n-pointer-shapes 0)
|
2
|
116 (defvar mouse-avoidance-old-pointer-shape nil)
|
0
|
117
|
|
118 ;;; Functions:
|
|
119
|
2
|
120 (defsubst mouse-avoidance-set-pointer-shape (shape)
|
|
121 "Set the shape of the mouse pointer to SHAPE."
|
|
122 (setq x-pointer-shape shape)
|
|
123 (set-mouse-color nil))
|
|
124
|
|
125 ;; XEmacs change -- this is so ugly. [FSF version is totally different -sb]
|
0
|
126 (defun mouse-avoidance-point-position ()
|
|
127 "Returns (WINDOW X . Y) of current point - analogous to mouse-position"
|
|
128 (let* ((beg (window-start))
|
|
129 (pos (point))
|
|
130 (col (current-column))
|
|
131 (row))
|
|
132 (setq row (count-lines beg pos))
|
|
133 (cons (selected-window) (cons col row))))
|
|
134
|
|
135 ;(defun mouse-avoidance-point-position-test ()
|
|
136 ; (interactive)
|
70
|
137 ; (message "point=%s mouse=%s"
|
|
138 ; (cdr (mouse-avoidance-point-position))
|
|
139 ; (cdr (mouse-position))))
|
0
|
140
|
|
141 (defun mouse-avoidance-set-mouse-position (pos)
|
|
142 ;; Carefully set mouse position to given position (X . Y)
|
|
143 ;; Ideally, should check if X,Y is in the current frame, and if not,
|
|
144 ;; leave the mouse where it was. However, this is currently
|
|
145 ;; difficult to do, so we just raise the frame to avoid frame switches.
|
|
146 ;; Returns t if it moved the mouse.
|
|
147 (let ((f (selected-frame)))
|
|
148 (raise-frame f)
|
2
|
149 ;; XEmacs: FSF version of set-mouse-position requires FRAME parameter
|
0
|
150 (set-mouse-position (frame-selected-window f) (car pos) (cdr pos))
|
|
151 t))
|
|
152
|
|
153 (defun mouse-avoidance-too-close-p (mouse)
|
|
154 ;; Return t if mouse pointer and point cursor are too close.
|
|
155 ;; Acceptable distance is defined by mouse-avoidance-threshold.
|
|
156 (let ((point (mouse-avoidance-point-position)))
|
|
157 (and (eq (car mouse) (car point))
|
|
158 (car (cdr mouse))
|
|
159 (< (abs (- (car (cdr mouse)) (car (cdr point))))
|
|
160 mouse-avoidance-threshold)
|
|
161 (< (abs (- (cdr (cdr mouse)) (cdr (cdr point))))
|
|
162 mouse-avoidance-threshold))))
|
|
163
|
|
164 (defun mouse-avoidance-banish-destination ()
|
|
165 "The position to which mouse-avoidance-mode `banish' moves the mouse.
|
|
166 You can redefine this if you want the mouse banished to a different corner."
|
|
167 (cons (1- (frame-width))
|
|
168 0))
|
|
169
|
|
170 (defun mouse-avoidance-banish-mouse ()
|
|
171 ;; Put the mouse pointer in the upper-right corner of the current frame.
|
|
172 (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
|
|
173
|
|
174 (defsubst mouse-avoidance-delta (cur delta dist var min max)
|
|
175 ;; Decide how far to move in either dimension.
|
|
176 ;; Args are the CURRENT location, the desired DELTA for
|
|
177 ;; warp-conservation, the DISTANCE we like to move, the VARIABILITY
|
|
178 ;; in distance allowed, and the MIN and MAX possible window positions.
|
2
|
179 ;; Returns something as close to DELTA as possible within the constraints.
|
0
|
180 (let ((L1 (max (- min cur) (+ (- dist) (- var))))
|
|
181 (R1 (+ (- dist) var ))
|
|
182 (L2 (+ dist (- var)))
|
|
183 (R2 (min (- max cur) (+ dist var))))
|
|
184 (if (< R1 (- min cur)) (setq L1 nil R1 nil))
|
|
185 (if (> L2 (- max cur)) (setq L2 nil R2 nil))
|
|
186 (cond ((and L1 (< delta L1)) L1)
|
|
187 ((and R1 (< delta R1)) delta)
|
|
188 ((and R1 (< delta 0)) R1)
|
|
189 ((and L2 (< delta L2)) L2)
|
|
190 ((and R2 (< delta R2)) delta)
|
|
191 (R2)
|
|
192 ((or R1 L2))
|
|
193 (t 0))))
|
|
194
|
|
195 (defun mouse-avoidance-nudge-mouse ()
|
|
196 ;; Push the mouse a little way away, possibly animating the move
|
|
197 ;; For these modes, state keeps track of the total offset that we've
|
|
198 ;; accumulated, and tries to keep it close to zero.
|
|
199 (let* ((cur (mouse-position))
|
2
|
200 (cur-frame (car cur))
|
0
|
201 (cur-pos (cdr cur))
|
|
202 (deltax (mouse-avoidance-delta
|
|
203 (car cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
204 (car mouse-avoidance-state))
|
|
205 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
|
206 0 (frame-width)))
|
|
207 (deltay (mouse-avoidance-delta
|
|
208 (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
209 (cdr mouse-avoidance-state))
|
|
210 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
2
|
211 0 (frame-height))))
|
0
|
212 (setq mouse-avoidance-state
|
|
213 (cons (+ (car mouse-avoidance-state) deltax)
|
|
214 (+ (cdr mouse-avoidance-state) deltay)))
|
|
215 (if (or (eq mouse-avoidance-mode 'animate)
|
|
216 (eq mouse-avoidance-mode 'proteus))
|
|
217 (let ((i 0.0)
|
2
|
218 ;; XEmacs change
|
0
|
219 (color (cdr (assoc 'mouse-color (frame-parameters)))))
|
|
220 (while (<= i 1)
|
|
221 (mouse-avoidance-set-mouse-position
|
|
222 (cons (+ (car cur-pos) (round (* i deltax)))
|
|
223 (+ (cdr cur-pos) (round (* i deltay)))))
|
|
224 (setq i (+ i (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
|
|
225 (if (eq mouse-avoidance-mode 'proteus)
|
2
|
226 ;; XEmacs change
|
0
|
227 (progn
|
|
228 (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
229 (set-mouse-color color)))
|
|
230 (sit-for mouse-avoidance-animation-delay)))
|
|
231 (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
|
|
232 (+ (cdr (cdr cur)) deltay))))))
|
|
233
|
|
234 (defun mouse-avoidance-random-shape ()
|
|
235 "Return a random cursor shape.
|
|
236 This assumes that any variable whose name begins with x-pointer- and
|
|
237 has an integer value is a valid cursor shape. You might want to
|
|
238 redefine this function to suit your own tastes."
|
|
239 (if (null mouse-avoidance-pointer-shapes)
|
|
240 (progn
|
|
241 (setq mouse-avoidance-pointer-shapes
|
|
242 (mapcar '(lambda (x) (symbol-value (intern x)))
|
|
243 (all-completions "x-pointer-" obarray
|
|
244 '(lambda (x)
|
|
245 (and (boundp x)
|
|
246 (integerp (symbol-value x)))))))
|
|
247 (setq mouse-avoidance-n-pointer-shapes
|
|
248 (length mouse-avoidance-pointer-shapes))))
|
|
249 (nth (random mouse-avoidance-n-pointer-shapes)
|
|
250 mouse-avoidance-pointer-shapes))
|
|
251
|
|
252 (defun mouse-avoidance-banish-hook ()
|
|
253 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
254 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
255 (mouse-avoidance-banish-mouse)))
|
|
256
|
|
257 (defun mouse-avoidance-exile-hook ()
|
|
258 ;; For exile mode, the state is nil when the mouse is in its normal
|
|
259 ;; position, and set to the old mouse-position when the mouse is in exile.
|
|
260 (if (and (not executing-kbd-macro)
|
|
261 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
262 (let ((mp (mouse-position)))
|
|
263 (cond ((and (not mouse-avoidance-state)
|
|
264 (mouse-avoidance-too-close-p mp))
|
|
265 (setq mouse-avoidance-state mp)
|
|
266 (mouse-avoidance-banish-mouse))
|
|
267 ((and mouse-avoidance-state
|
|
268 (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
|
2
|
269 ;; XEmacs change
|
0
|
270 (if (and (eq (car mp) (if (< emacs-minor-version 12)
|
|
271 (selected-frame)
|
|
272 (selected-window)))
|
|
273 (equal (cdr mp) (mouse-avoidance-banish-destination)))
|
|
274 (mouse-avoidance-set-mouse-position
|
|
275 ;; move back only if user has not moved mouse
|
|
276 (cdr mouse-avoidance-state)))
|
|
277 ;; but clear state anyway, to be ready for another move
|
|
278 (setq mouse-avoidance-state nil))))))
|
|
279
|
|
280 (defun mouse-avoidance-fancy-hook ()
|
|
281 ;; Used for the "fancy" modes, ie jump et al.
|
|
282 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
283 (mouse-avoidance-kbd-command (this-command-keys))
|
|
284 (mouse-avoidance-too-close-p (mouse-position)))
|
|
285 (let ((old-pos (mouse-position)))
|
|
286 (mouse-avoidance-nudge-mouse)
|
2
|
287 ;; XEmacs change
|
0
|
288 (if (not (eq (if (< emacs-minor-version 12)
|
|
289 (selected-frame)
|
|
290 (selected-window)) (car old-pos))) ; move went awry
|
|
291 (set-mouse-position (car old-pos) ; sigh..
|
|
292 (car (cdr old-pos))
|
|
293 (cdr (cdr old-pos)))))))
|
|
294
|
|
295 (defun mouse-avoidance-kbd-command (key)
|
|
296 "Return t if the KEYSEQENCE is composed of keyboard events only.
|
|
297 Return nil if there are any lists in the key sequence."
|
134
|
298 (cond ((null key) nil) ; Null event seems to be
|
0
|
299 ; returned occasionally.
|
|
300 ((not (vectorp key)) t) ; Strings are keyboard events.
|
|
301 ((catch 'done
|
|
302 (let ((i 0)
|
|
303 (l (length key)))
|
|
304 (while (< i l)
|
134
|
305 ;; XEmacs change: (Emacs version was: (listp (aref key i)))
|
|
306 (if (not (key-press-event-p (aref key i)))
|
0
|
307 (throw 'done nil))
|
|
308 (setq i (1+ i))))
|
|
309 t))))
|
|
310
|
2
|
311 ;;;###autoload
|
0
|
312 (defun mouse-avoidance-mode (&optional mode)
|
|
313 "Set cursor avoidance mode to MODE.
|
|
314 MODE should be one of the symbols `banish', `exile', `jump', `animate',
|
|
315 `cat-and-mouse', `proteus', or `none'.
|
|
316
|
|
317 If MODE is nil, toggle mouse avoidance between `none` and `banish'
|
|
318 modes. Positive numbers and symbols other than the above are treated
|
|
319 as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
|
|
320
|
|
321 Effects of the different modes:
|
|
322 * banish: Move the mouse to the upper-right corner on any keypress.
|
|
323 * exile: Move the mouse to the corner only if the cursor gets too close,
|
|
324 and allow it to return once the cursor is out of the way.
|
|
325 * jump: If the cursor gets too close to the mouse, displace the mouse
|
|
326 a random distance & direction.
|
|
327 * animate: As `jump', but shows steps along the way for illusion of motion.
|
|
328 * cat-and-mouse: Same as `animate'.
|
|
329 * proteus: As `animate', but changes the shape of the mouse pointer too.
|
|
330
|
|
331 Whenever the mouse is moved, the frame is also raised.
|
|
332
|
|
333 \(see `mouse-avoidance-threshold' for definition of \"too close\",
|
|
334 and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
|
|
335 definition of \"random distance\".)"
|
|
336 (interactive
|
|
337 (list (intern (completing-read
|
|
338 "Select cursor avoidance technique (SPACE for list): "
|
|
339 '(("banish") ("exile") ("jump") ("animate")
|
|
340 ("cat-and-mouse") ("proteus") ("none"))
|
|
341 nil t))))
|
|
342 (if (eq mode 'cat-and-mouse)
|
|
343 (setq mode 'animate))
|
2
|
344 ;; XEmacs change - We don't have post-command-idle-hook
|
|
345 (remove-hook 'post-command-hook 'mouse-avoidance-banish-hook)
|
|
346 (remove-hook 'post-command-hook 'mouse-avoidance-exile-hook)
|
|
347 (remove-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
|
|
348 ;; Restore pointer shape if necessary
|
|
349 (if (eq mouse-avoidance-mode 'proteus)
|
|
350 (mouse-avoidance-set-pointer-shape mouse-avoidance-old-pointer-shape))
|
|
351
|
|
352 ;; Do additional setup depending on version of mode requested
|
0
|
353 (cond ((eq mode 'none)
|
|
354 (setq mouse-avoidance-mode nil))
|
|
355 ((or (eq mode 'jump)
|
|
356 (eq mode 'animate)
|
|
357 (eq mode 'proteus))
|
2
|
358 ;; XEmacs: we don't have post-command-idle-hook
|
0
|
359 (add-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
|
|
360 (setq mouse-avoidance-mode mode
|
2
|
361 mouse-avoidance-state (cons 0 0)
|
|
362 mouse-avoidance-old-pointer-shape x-pointer-shape))
|
0
|
363 ((eq mode 'exile)
|
2
|
364 ;; XEmacs: FSF uses post-command-idle-hook
|
0
|
365 (add-hook 'post-command-hook 'mouse-avoidance-exile-hook)
|
|
366 (setq mouse-avoidance-mode mode
|
|
367 mouse-avoidance-state nil))
|
|
368 ((or (eq mode 'banish)
|
|
369 (eq mode t)
|
|
370 (and (null mode) (null mouse-avoidance-mode))
|
|
371 (and mode (> (prefix-numeric-value mode) 0)))
|
2
|
372 ;; XEmacs: FSF uses post-command-idle-hook
|
0
|
373 (add-hook 'post-command-hook 'mouse-avoidance-banish-hook)
|
|
374 (setq mouse-avoidance-mode 'banish))
|
|
375 (t (setq mouse-avoidance-mode nil)))
|
|
376 (force-mode-line-update))
|
|
377
|
|
378 ;(or (assq 'mouse-avoidance-mode minor-mode-alist)
|
|
379 ; (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
|
|
380 ; minor-mode-alist)))
|
|
381 ;;XEmacs: do it right.
|
|
382 ;;;###autoload
|
|
383 (add-minor-mode 'mouse-avoidance-mode " Avoid")
|
|
384
|
80
|
385 ;;; avoid.el ends here
|