Allow further `mouse-avoid-mode' customisations
[bpt/emacs.git] / lisp / avoid.el
CommitLineData
be010748 1;;; avoid.el --- make mouse pointer stay out of the way of editing
bb5d4e1a 2
acaf905b 3;; Copyright (C) 1993-1994, 2000-2012 Free Software Foundation, Inc.
bb5d4e1a 4
5762abec 5;; Author: Boris Goldowsky <boris@gnu.org>
bb5d4e1a 6;; Keywords: mouse
bb5d4e1a
RS
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
bb5d4e1a 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
bb5d4e1a
RS
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
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
bb5d4e1a
RS
22
23;;; Commentary:
bb5d4e1a 24
b578f267
EN
25;; For those who are annoyed by the mouse pointer obscuring text,
26;; this mode moves the mouse pointer - either just a little out of
187bd11c 27;; the way, or all the way to the corner of the frame.
b578f267 28;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
187bd11c 29;; To set up permanently, put the following in your .emacs:
b578f267 30;;
95d059bd 31;; (if (display-mouse-p) (mouse-avoidance-mode 'animate))
b578f267 32;;
8557ea06
RS
33;; Other legitimate alternatives include
34;; `banish', `exile', `jump', `cat-and-mouse', and `proteus'.
35;; They do somewhat different things.
b578f267
EN
36;; See the documentation for function `mouse-avoidance-mode' for
37;; details of the different modes.
38;;
39;; For added silliness, make the animatee animate...
40;; put something similar to the following into your .emacs:
41;;
95d059bd 42;; (if (eq window-system 'x)
b578f267
EN
43;; (mouse-avoidance-set-pointer-shape
44;; (eval (nth (random 4)
45;; '(x-pointer-man x-pointer-spider
46;; x-pointer-gobbler x-pointer-gumby)))))
47;;
48;; For completely random pointer shape, replace the setq above with:
49;; (setq x-pointer-shape (mouse-avoidance-random-shape))
187bd11c 50;;
b578f267
EN
51;; Bugs / Warnings / To-Do:
52;;
88b4ca18 53;; - Using this code does slow Emacs down. "banish" mode shouldn't
b578f267
EN
54;; be too bad, and on my workstation even "animate" is reasonable.
55;;
56;; - It ought to find out where any overlapping frames are and avoid them,
57;; rather than always raising the frame.
58
59;; Credits:
187bd11c 60;; This code was helped by all those who contributed suggestions,
b578f267
EN
61;; fixes, and additions
62;; Joe Harrington (and his advisor), for the original inspiration.
63;; Ken Manheimer, for dreaming up the Protean mode.
64;; Richard Stallman, for the awful cat-and-mouse pun, among other things.
65;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
66;; Simon Marshall, and M.S. Ashton, for their feedback.
67
bb5d4e1a
RS
68;;; Code:
69
70(provide 'avoid)
71
00ed33e7
RS
72(defgroup avoid nil
73 "Make mouse pointer stay out of the way of editing."
74 :prefix "mouse-avoidance-"
75 :group 'mouse)
76
4bce8c17 77;;;###autoload
8557ea06 78(defcustom mouse-avoidance-mode nil
ad22b7f7 79 "Activate Mouse Avoidance mode.
8557ea06 80See function `mouse-avoidance-mode' for possible values.
970ce0d8
DL
81Setting this variable directly does not take effect;
82use either \\[customize] or the function `mouse-avoidance-mode'."
8557ea06
RS
83 :set (lambda (symbol value)
84 ;; 'none below prevents toggling when value is nil.
187bd11c 85 (mouse-avoidance-mode (or value 'none)))
8557ea06 86 :initialize 'custom-initialize-default
187bd11c 87 :type '(choice (const :tag "none" nil) (const banish) (const jump)
ad22b7f7 88 (const animate) (const exile) (const proteus))
8557ea06 89 :group 'avoid
f9e9ac1d
DN
90 :require 'avoid
91 :version "20.3")
8557ea06 92
bb5d4e1a 93
00ed33e7 94(defcustom mouse-avoidance-nudge-dist 15
9201cc28 95 "Average distance that mouse will be moved when approached by cursor.
ad22b7f7 96Only applies in Mouse Avoidance mode `jump' and its derivatives.
00ed33e7
RS
97For best results make this larger than `mouse-avoidance-threshold'."
98 :type 'integer
99 :group 'avoid)
bb5d4e1a 100
00ed33e7 101(defcustom mouse-avoidance-nudge-var 10
9201cc28 102 "Variability of `mouse-avoidance-nudge-dist' (which see)."
00ed33e7
RS
103 :type 'integer
104 :group 'avoid)
bb5d4e1a 105
00ed33e7
RS
106(defcustom mouse-avoidance-animation-delay .01
107 "Delay between animation steps, in seconds."
108 :type 'number
109 :group 'avoid)
bb5d4e1a 110
00ed33e7 111(defcustom mouse-avoidance-threshold 5
9201cc28 112 "Mouse-pointer's flight distance.
bb5d4e1a 113If the cursor gets closer than this, the mouse pointer will move away.
ad22b7f7 114Only applies in Mouse Avoidance modes `animate' and `jump'."
00ed33e7
RS
115 :type 'integer
116 :group 'avoid)
bb5d4e1a 117
bdd20095
KH
118;; Internal variables
119(defvar mouse-avoidance-state nil)
bb5d4e1a
RS
120(defvar mouse-avoidance-pointer-shapes nil)
121(defvar mouse-avoidance-n-pointer-shapes 0)
5babefcf 122(defvar mouse-avoidance-old-pointer-shape nil)
38ceb48f 123(defvar mouse-avoidance-animating-pointer nil)
bb5d4e1a 124
85c92290
RS
125;; This timer is used to run something when Emacs is idle.
126(defvar mouse-avoidance-timer nil)
127
bb5d4e1a
RS
128;;; Functions:
129
1f31a9f1
BG
130(defsubst mouse-avoidance-set-pointer-shape (shape)
131 "Set the shape of the mouse pointer to SHAPE."
a9f73104
EZ
132 (when (boundp 'x-pointer-shape)
133 (setq x-pointer-shape shape)
134 (set-mouse-color nil)))
1f31a9f1 135
bdd20095 136(defun mouse-avoidance-point-position ()
3f32656c 137 "Return the position of point as (FRAME X . Y).
88b4ca18 138Analogous to `mouse-position'."
fc634b38
KS
139 (let ((edges (window-inside-edges))
140 (x-y (posn-x-y (posn-at-point))))
96ae4787 141 (cons (selected-frame)
fc634b38
KS
142 (cons (+ (car edges)
143 (/ (car x-y) (frame-char-width)))
144 (+ (car (cdr edges))
145 (/ (cdr x-y) (frame-char-height)))))))
96ae4787
RS
146
147;(defun mouse-avoidance-point-position-test ()
148; (interactive)
187bd11c 149; (message (format "point=%s mouse=%s"
96ae4787
RS
150; (cdr (mouse-avoidance-point-position))
151; (cdr (mouse-position)))))
bdd20095
KH
152
153(defun mouse-avoidance-set-mouse-position (pos)
154 ;; Carefully set mouse position to given position (X . Y)
155 ;; Ideally, should check if X,Y is in the current frame, and if not,
156 ;; leave the mouse where it was. However, this is currently
157 ;; difficult to do, so we just raise the frame to avoid frame switches.
158 ;; Returns t if it moved the mouse.
159 (let ((f (selected-frame)))
160 (raise-frame f)
161 (set-mouse-position f (car pos) (cdr pos))
162 t))
187bd11c 163
bdd20095 164(defun mouse-avoidance-too-close-p (mouse)
02790ce2
GM
165 "Return t if mouse pointer and point cursor are too close.
166MOUSE is the current mouse position as returned by `mouse-position'.
167Acceptable distance is defined by `mouse-avoidance-threshold'."
168 (let* ((frame (car mouse))
169 (mouse-y (cdr (cdr mouse)))
170 (tool-bar-lines (frame-parameter nil 'tool-bar-lines)))
a9f73104
EZ
171 (or tool-bar-lines
172 (setq tool-bar-lines 0))
02790ce2
GM
173 (if (and mouse-y (< mouse-y tool-bar-lines))
174 nil
175 (let ((point (mouse-avoidance-point-position))
176 (mouse-x (car (cdr mouse))))
177 (and (eq frame (car point))
178 (not (null mouse-x))
179 (< (abs (- mouse-x (car (cdr point))))
180 mouse-avoidance-threshold)
181 (< (abs (- mouse-y (cdr (cdr point))))
182 mouse-avoidance-threshold))))))
bb5d4e1a 183
bdd20095 184(defun mouse-avoidance-banish-destination ()
ad22b7f7 185 "The position to which Mouse Avoidance mode `banish' moves the mouse.
bdd20095 186You can redefine this if you want the mouse banished to a different corner."
88b4ca18
SM
187 (let* ((pos (window-edges)))
188 (cons (- (nth 2 pos) 2)
189 (nth 1 pos))))
bdd20095 190
bb5d4e1a
RS
191(defun mouse-avoidance-banish-mouse ()
192 ;; Put the mouse pointer in the upper-right corner of the current frame.
bdd20095
KH
193 (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
194
195(defsubst mouse-avoidance-delta (cur delta dist var min max)
196 ;; Decide how far to move in either dimension.
197 ;; Args are the CURRENT location, the desired DELTA for
198 ;; warp-conservation, the DISTANCE we like to move, the VARIABILITY
199 ;; in distance allowed, and the MIN and MAX possible window positions.
a7acbbe4 200 ;; Returns something as close to DELTA as possible within the constraints.
bdd20095
KH
201 (let ((L1 (max (- min cur) (+ (- dist) (- var))))
202 (R1 (+ (- dist) var ))
203 (L2 (+ dist (- var)))
204 (R2 (min (- max cur) (+ dist var))))
205 (if (< R1 (- min cur)) (setq L1 nil R1 nil))
206 (if (> L2 (- max cur)) (setq L2 nil R2 nil))
207 (cond ((and L1 (< delta L1)) L1)
208 ((and R1 (< delta R1)) delta)
209 ((and R1 (< delta 0)) R1)
210 ((and L2 (< delta L2)) L2)
211 ((and R2 (< delta R2)) delta)
212 (R2)
213 ((or R1 L2))
214 (t 0))))
bb5d4e1a 215
187bd11c 216(defun mouse-avoidance-nudge-mouse ()
88b4ca18 217 ;; Push the mouse a little way away, possibly animating the move.
bdd20095
KH
218 ;; For these modes, state keeps track of the total offset that we've
219 ;; accumulated, and tries to keep it close to zero.
bb5d4e1a 220 (let* ((cur (mouse-position))
bdd20095
KH
221 (cur-frame (car cur))
222 (cur-pos (cdr cur))
88b4ca18
SM
223 (pos (window-edges))
224 (wleft (pop pos))
225 (wtop (pop pos))
226 (wright (pop pos))
227 (wbot (pop pos))
187bd11c 228 (deltax (mouse-avoidance-delta
bdd20095
KH
229 (car cur-pos) (- (random mouse-avoidance-nudge-var)
230 (car mouse-avoidance-state))
231 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
88b4ca18 232 wleft (1- wright)))
187bd11c 233 (deltay (mouse-avoidance-delta
bdd20095
KH
234 (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
235 (cdr mouse-avoidance-state))
236 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
88b4ca18 237 wtop (1- wbot))))
bdd20095
KH
238 (setq mouse-avoidance-state
239 (cons (+ (car mouse-avoidance-state) deltax)
240 (+ (cdr mouse-avoidance-state) deltay)))
187bd11c 241 (if (or (eq mouse-avoidance-mode 'animate)
bb5d4e1a 242 (eq mouse-avoidance-mode 'proteus))
38ceb48f
CY
243 (let ((i 0.0)
244 (incr (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
245 (setq mouse-avoidance-animating-pointer t)
bb5d4e1a 246 (while (<= i 1)
187bd11c 247 (mouse-avoidance-set-mouse-position
bdd20095
KH
248 (cons (+ (car cur-pos) (round (* i deltax)))
249 (+ (cdr cur-pos) (round (* i deltay)))))
38ceb48f 250 (setq i (+ i incr))
bb5d4e1a 251 (if (eq mouse-avoidance-mode 'proteus)
187bd11c 252 (mouse-avoidance-set-pointer-shape
1f31a9f1 253 (mouse-avoidance-random-shape)))
38ceb48f
CY
254 (sit-for mouse-avoidance-animation-delay))
255 (setq mouse-avoidance-animating-pointer nil))
3f32656c
RS
256 (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
257 (+ (cdr (cdr cur)) deltay))))))
bb5d4e1a
RS
258
259(defun mouse-avoidance-random-shape ()
260 "Return a random cursor shape.
261This assumes that any variable whose name begins with x-pointer- and
262has an integer value is a valid cursor shape. You might want to
263redefine this function to suit your own tastes."
264 (if (null mouse-avoidance-pointer-shapes)
265 (progn
266 (setq mouse-avoidance-pointer-shapes
76d1e5a4 267 (mapcar (lambda (x) (symbol-value (intern x)))
bb5d4e1a 268 (all-completions "x-pointer-" obarray
187bd11c 269 (lambda (x)
bb5d4e1a
RS
270 (and (boundp x)
271 (integerp (symbol-value x)))))))
187bd11c 272 (setq mouse-avoidance-n-pointer-shapes
bb5d4e1a
RS
273 (length mouse-avoidance-pointer-shapes))))
274 (nth (random mouse-avoidance-n-pointer-shapes)
275 mouse-avoidance-pointer-shapes))
276
88b4ca18
SM
277(defun mouse-avoidance-ignore-p ()
278 (let ((mp (mouse-position)))
36ba6f07 279 (or (not (frame-pointer-visible-p)) ; The pointer is hidden
ec6ecaed 280 (not cursor-type) ; There's no cursor
36ba6f07 281 executing-kbd-macro ; don't check inside macro
88b4ca18
SM
282 (null (cadr mp)) ; don't move unless in an Emacs frame
283 (not (eq (car mp) (selected-frame)))
284 ;; Don't do anything if last event was a mouse event.
285 ;; FIXME: this code fails in the case where the mouse was moved
286 ;; since the last key-press but without generating any event.
287 (and (consp last-input-event)
288 (symbolp (car last-input-event))
289 (let ((modifiers (event-modifiers (car last-input-event))))
290 (or (memq (car last-input-event)
291 '(mouse-movement scroll-bar-movement
292 select-window switch-frame))
293 (memq 'click modifiers)
294 (memq 'double modifiers)
295 (memq 'triple modifiers)
296 (memq 'drag modifiers)
297 (memq 'down modifiers)))))))
298
38ceb48f 299(defun mouse-avoidance-banish ()
88b4ca18 300 (if (not (mouse-avoidance-ignore-p))
bdd20095
KH
301 (mouse-avoidance-banish-mouse)))
302
38ceb48f 303(defun mouse-avoidance-exile ()
bdd20095
KH
304 ;; For exile mode, the state is nil when the mouse is in its normal
305 ;; position, and set to the old mouse-position when the mouse is in exile.
88b4ca18 306 (if (not (mouse-avoidance-ignore-p))
bdd20095
KH
307 (let ((mp (mouse-position)))
308 (cond ((and (not mouse-avoidance-state)
309 (mouse-avoidance-too-close-p mp))
310 (setq mouse-avoidance-state mp)
311 (mouse-avoidance-banish-mouse))
312 ((and mouse-avoidance-state
313 (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
314 (if (and (eq (car mp) (selected-frame))
315 (equal (cdr mp) (mouse-avoidance-banish-destination)))
316 (mouse-avoidance-set-mouse-position
317 ;; move back only if user has not moved mouse
318 (cdr mouse-avoidance-state)))
319 ;; but clear state anyway, to be ready for another move
320 (setq mouse-avoidance-state nil))))))
bb5d4e1a 321
38ceb48f 322(defun mouse-avoidance-fancy ()
bdd20095 323 ;; Used for the "fancy" modes, ie jump et al.
38ceb48f
CY
324 (if (and (not mouse-avoidance-animating-pointer)
325 (not (mouse-avoidance-ignore-p))
bdd20095 326 (mouse-avoidance-too-close-p (mouse-position)))
d9f132a8
RS
327 (let ((old-pos (mouse-position)))
328 (mouse-avoidance-nudge-mouse)
1f31a9f1
BG
329 (if (not (eq (selected-frame) (car old-pos)))
330 ;; This should never happen.
331 (apply 'set-mouse-position old-pos)))))
bb5d4e1a 332
1f31a9f1 333;;;###autoload
bb5d4e1a 334(defun mouse-avoidance-mode (&optional mode)
ad22b7f7 335 "Set Mouse Avoidance mode to MODE.
bdd20095 336MODE should be one of the symbols `banish', `exile', `jump', `animate',
d9f132a8
RS
337`cat-and-mouse', `proteus', or `none'.
338
187bd11c 339If MODE is nil, toggle mouse avoidance between `none' and `banish'
d9f132a8
RS
340modes. Positive numbers and symbols other than the above are treated
341as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
342
187bd11c 343Effects of the different modes:
3f32656c
RS
344 * banish: Move the mouse to the upper-right corner on any keypress.
345 * exile: Move the mouse to the corner only if the cursor gets too close,
bdd20095 346 and allow it to return once the cursor is out of the way.
3f32656c 347 * jump: If the cursor gets too close to the mouse, displace the mouse
bdd20095 348 a random distance & direction.
3f32656c
RS
349 * animate: As `jump', but shows steps along the way for illusion of motion.
350 * cat-and-mouse: Same as `animate'.
351 * proteus: As `animate', but changes the shape of the mouse pointer too.
d9f132a8 352
bdd20095
KH
353Whenever the mouse is moved, the frame is also raised.
354
ad22b7f7 355\(See `mouse-avoidance-threshold' for definition of \"too close\",
d9f132a8
RS
356and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
357definition of \"random distance\".)"
bb5d4e1a
RS
358 (interactive
359 (list (intern (completing-read
360 "Select cursor avoidance technique (SPACE for list): "
bdd20095
KH
361 '(("banish") ("exile") ("jump") ("animate")
362 ("cat-and-mouse") ("proteus") ("none"))
bb5d4e1a
RS
363 nil t))))
364 (if (eq mode 'cat-and-mouse)
365 (setq mode 'animate))
85c92290
RS
366 (if mouse-avoidance-timer
367 (cancel-timer mouse-avoidance-timer))
368 (setq mouse-avoidance-timer nil)
5babefcf
RS
369
370 ;; Restore pointer shape if necessary
371 (if (eq mouse-avoidance-mode 'proteus)
372 (mouse-avoidance-set-pointer-shape mouse-avoidance-old-pointer-shape))
373
374 ;; Do additional setup depending on version of mode requested
bb5d4e1a
RS
375 (cond ((eq mode 'none)
376 (setq mouse-avoidance-mode nil))
377 ((or (eq mode 'jump)
378 (eq mode 'animate)
379 (eq mode 'proteus))
85c92290 380 (setq mouse-avoidance-timer
38ceb48f 381 (run-with-idle-timer 0.1 t 'mouse-avoidance-fancy))
bdd20095 382 (setq mouse-avoidance-mode mode
5babefcf 383 mouse-avoidance-state (cons 0 0)
a9f73104
EZ
384 mouse-avoidance-old-pointer-shape
385 (and (boundp 'x-pointer-shape) x-pointer-shape)))
bdd20095 386 ((eq mode 'exile)
85c92290 387 (setq mouse-avoidance-timer
38ceb48f 388 (run-with-idle-timer 0.1 t 'mouse-avoidance-exile))
bdd20095
KH
389 (setq mouse-avoidance-mode mode
390 mouse-avoidance-state nil))
187bd11c 391 ((or (eq mode 'banish)
bb5d4e1a
RS
392 (eq mode t)
393 (and (null mode) (null mouse-avoidance-mode))
394 (and mode (> (prefix-numeric-value mode) 0)))
85c92290 395 (setq mouse-avoidance-timer
38ceb48f 396 (run-with-idle-timer 0.1 t 'mouse-avoidance-banish))
bb5d4e1a 397 (setq mouse-avoidance-mode 'banish))
bdd20095
KH
398 (t (setq mouse-avoidance-mode nil)))
399 (force-mode-line-update))
bb5d4e1a 400
ed62ad33
KH
401;; Most people who use avoid mode leave it on all the time, so it's not
402;; very informative to announce it in the mode line.
403;;(or (assq 'mouse-avoidance-mode minor-mode-alist)
404;; (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
405;; minor-mode-alist)))
bb5d4e1a 406
8557ea06 407;; Needed for custom.
187bd11c 408(if mouse-avoidance-mode
8557ea06
RS
409 (mouse-avoidance-mode mouse-avoidance-mode))
410
411;;; avoid.el ends here