Removed auto-mode-alist hacking for html-mode to files.el.
[bpt/emacs.git] / lisp / avoid.el
CommitLineData
be010748 1;;; avoid.el --- make mouse pointer stay out of the way of editing
bb5d4e1a 2
d9f132a8 3;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
bb5d4e1a 4
7865eac6 5;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
bb5d4e1a 6;; Keywords: mouse
bb5d4e1a
RS
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 .
1f31a9f1 30;;; To set up permanently, put the following in your .emacs:
bb5d4e1a 31;;;
1f31a9f1 32;;; (if window-system (mouse-avoidance-mode 'animate))
bb5d4e1a 33;;;
bdd20095 34;;; The 'animate can be 'jump or 'banish or 'exile or 'protean if you prefer.
d9f132a8
RS
35;;; See the documentation for function `mouse-avoidance-mode' for
36;;; details of the different modes.
bb5d4e1a
RS
37;;;
38;;; For added silliness, make the animatee animate...
39;;; put something similar to the following into your .emacs:
40;;;
1f31a9f1
BG
41;;; (if window-system
42;;; (mouse-avoidance-set-pointer-shape
bb5d4e1a
RS
43;;; (eval (nth (random 4)
44;;; '(x-pointer-man x-pointer-spider
1f31a9f1 45;;; x-pointer-gobbler x-pointer-gumby)))))
bb5d4e1a
RS
46;;;
47;;; For completely random pointer shape, replace the setq above with:
48;;; (setq x-pointer-shape (mouse-avoidance-random-shape))
49;;;
bdd20095 50;;; Bugs / Warnings / To-Do:
bb5d4e1a
RS
51;;;
52;;; - Using this code does slow emacs down. "banish" mode shouldn't
1f31a9f1 53;;; be too bad, and on my workstation even "animate" is reasonable.
bb5d4e1a 54;;;
1f31a9f1 55;;; - It ought to find out where any overlapping frames are and avoid them,
bdd20095 56;;; rather than always raising the frame.
bb5d4e1a
RS
57
58;;; Credits:
bdd20095
KH
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.
bb5d4e1a
RS
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.
3f32656c
RS
73See function `mouse-avoidance-mode' for possible values. Changing this
74variable is NOT the recommended way to change modes; use that function
bb5d4e1a
RS
75instead.")
76
bdd20095 77(defvar mouse-avoidance-nudge-dist 15
bb5d4e1a 78 "*Average distance that mouse will be moved when approached by cursor.
f87bb1ee 79Only applies in mouse-avoidance-mode `jump' and its derivatives.
25cd2c1b 80For best results make this larger than `mouse-avoidance-threshold'.")
bb5d4e1a 81
bdd20095 82(defvar mouse-avoidance-nudge-var 10
3f32656c 83 "*Variability of `mouse-avoidance-nudge-dist' (which see).")
bb5d4e1a
RS
84
85(defvar mouse-avoidance-animation-delay .01
86 "Delay between animation steps, in seconds.")
87
25cd2c1b 88(defvar mouse-avoidance-threshold 5
bb5d4e1a
RS
89 "*Mouse-pointer's flight distance.
90If the cursor gets closer than this, the mouse pointer will move away.
91Only applies in mouse-avoidance-modes `animate' and `jump'.")
92
bdd20095
KH
93;; Internal variables
94(defvar mouse-avoidance-state nil)
bb5d4e1a
RS
95(defvar mouse-avoidance-pointer-shapes nil)
96(defvar mouse-avoidance-n-pointer-shapes 0)
97
98;;; Functions:
99
1f31a9f1
BG
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
bdd20095 105(defun mouse-avoidance-point-position ()
3f32656c 106 "Return the position of point as (FRAME X . Y).
bdd20095
KH
107Analogous to mouse-position."
108 (let* ((w (selected-window))
109 (edges (window-edges w))
110 (list
96ae4787
RS
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
bdd20095 115 (point) ; stop pos
96ae4787 116 (cons (window-width) (window-height)); stop XY: none
bdd20095 117 (1- (window-width)) ; width
96ae4787 118 (cons (window-hscroll w) 0) ; 0 may not be right?
bdd20095
KH
119 (selected-window))))
120 ;; compute-motion returns (pos HPOS VPOS prevhpos contin)
121 ;; we want: (frame hpos . vpos)
96ae4787
RS
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)))))
bdd20095
KH
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)
bb5d4e1a 144 ;; Return t if mouse pointer and point cursor are too close.
25cd2c1b 145 ;; Acceptable distance is defined by mouse-avoidance-threshold.
bdd20095
KH
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))))
25cd2c1b 150 mouse-avoidance-threshold)
bdd20095 151 (< (abs (- (cdr (cdr mouse)) (cdr (cdr point))))
25cd2c1b 152 mouse-avoidance-threshold))))
bb5d4e1a 153
bdd20095 154(defun mouse-avoidance-banish-destination ()
3f32656c 155 "The position to which mouse-avoidance-mode `banish' moves the mouse.
bdd20095
KH
156You can redefine this if you want the mouse banished to a different corner."
157 (cons (1- (frame-width))
158 0))
159
bb5d4e1a
RS
160(defun mouse-avoidance-banish-mouse ()
161 ;; Put the mouse pointer in the upper-right corner of the current frame.
bdd20095
KH
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.
a7acbbe4 169 ;; Returns something as close to DELTA as possible within the constraints.
bdd20095
KH
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))))
bb5d4e1a
RS
184
185(defun mouse-avoidance-nudge-mouse ()
186 ;; Push the mouse a little way away, possibly animating the move
bdd20095
KH
187 ;; For these modes, state keeps track of the total offset that we've
188 ;; accumulated, and tries to keep it close to zero.
bb5d4e1a 189 (let* ((cur (mouse-position))
bdd20095
KH
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)))
bb5d4e1a
RS
205 (if (or (eq mouse-avoidance-mode 'animate)
206 (eq mouse-avoidance-mode 'proteus))
1f31a9f1 207 (let ((i 0.0))
bb5d4e1a 208 (while (<= i 1)
bdd20095
KH
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))))
bb5d4e1a 213 (if (eq mouse-avoidance-mode 'proteus)
1f31a9f1
BG
214 (mouse-avoidance-set-pointer-shape
215 (mouse-avoidance-random-shape)))
bb5d4e1a 216 (sit-for mouse-avoidance-animation-delay)))
3f32656c
RS
217 (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
218 (+ (cdr (cdr cur)) deltay))))))
bb5d4e1a
RS
219
220(defun mouse-avoidance-random-shape ()
221 "Return a random cursor shape.
222This assumes that any variable whose name begins with x-pointer- and
223has an integer value is a valid cursor shape. You might want to
224redefine 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
bdd20095
KH
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))))))
bb5d4e1a
RS
262
263(defun mouse-avoidance-fancy-hook ()
bdd20095
KH
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)))
d9f132a8
RS
268 (let ((old-pos (mouse-position)))
269 (mouse-avoidance-nudge-mouse)
1f31a9f1
BG
270 (if (not (eq (selected-frame) (car old-pos)))
271 ;; This should never happen.
272 (apply 'set-mouse-position old-pos)))))
bb5d4e1a 273
bdd20095 274(defun mouse-avoidance-kbd-command (key)
bb5d4e1a 275 "Return t if the KEYSEQENCE is composed of keyboard events only.
3f32656c 276Return nil if there are any lists in the key sequence."
bb5d4e1a
RS
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
1f31a9f1 289;;;###autoload
bb5d4e1a
RS
290(defun mouse-avoidance-mode (&optional mode)
291 "Set cursor avoidance mode to MODE.
bdd20095 292MODE should be one of the symbols `banish', `exile', `jump', `animate',
d9f132a8
RS
293`cat-and-mouse', `proteus', or `none'.
294
bdd20095 295If MODE is nil, toggle mouse avoidance between `none` and `banish'
d9f132a8
RS
296modes. Positive numbers and symbols other than the above are treated
297as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
298
299Effects of the different modes:
3f32656c
RS
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,
bdd20095 302 and allow it to return once the cursor is out of the way.
3f32656c 303 * jump: If the cursor gets too close to the mouse, displace the mouse
bdd20095 304 a random distance & direction.
3f32656c
RS
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.
d9f132a8 308
bdd20095
KH
309Whenever the mouse is moved, the frame is also raised.
310
25cd2c1b 311\(see `mouse-avoidance-threshold' for definition of \"too close\",
d9f132a8
RS
312and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
313definition of \"random distance\".)"
bb5d4e1a
RS
314 (interactive
315 (list (intern (completing-read
316 "Select cursor avoidance technique (SPACE for list): "
bdd20095
KH
317 '(("banish") ("exile") ("jump") ("animate")
318 ("cat-and-mouse") ("proteus") ("none"))
bb5d4e1a
RS
319 nil t))))
320 (if (eq mode 'cat-and-mouse)
321 (setq mode 'animate))
cf329c95
RS
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)
bb5d4e1a
RS
325 (cond ((eq mode 'none)
326 (setq mouse-avoidance-mode nil))
327 ((or (eq mode 'jump)
328 (eq mode 'animate)
329 (eq mode 'proteus))
cf329c95 330 (add-hook 'post-command-idle-hook 'mouse-avoidance-fancy-hook)
bdd20095
KH
331 (setq mouse-avoidance-mode mode
332 mouse-avoidance-state (cons 0 0)))
333 ((eq mode 'exile)
cf329c95 334 (add-hook 'post-command-idle-hook 'mouse-avoidance-exile-hook)
bdd20095
KH
335 (setq mouse-avoidance-mode mode
336 mouse-avoidance-state nil))
bb5d4e1a
RS
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)))
cf329c95 341 (add-hook 'post-command-idle-hook 'mouse-avoidance-banish-hook)
bb5d4e1a 342 (setq mouse-avoidance-mode 'banish))
bdd20095
KH
343 (t (setq mouse-avoidance-mode nil)))
344 (force-mode-line-update))
bb5d4e1a
RS
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