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