Initial revision
[bpt/emacs.git] / lisp / avoid.el
CommitLineData
bb5d4e1a
RS
1;;; avoid.el -- make mouse pointer stay out of the way of editing.
2
3;;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5;; Author: Boris Goldowsky <boris@cs.rochester.edu>
6;; Keywords: mouse
7;; $Revision: 1.10 $
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it 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;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25;;; Commentary:
26;;;
27;;; For those who are annoyed by the mouse pointer obscuring text,
28;;; this mode moves the mouse pointer - either just a little out of
29;;; the way, or all the way to the corner of the frame.
30;;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
31;;; To set up permanently, put this file on your load-path and put the
32;;; following in your .emacs:
33;;;
34;;; (cond (window-system
35;;; (require 'avoid)
36;;; (mouse-avoidance-mode 'cat-and-mouse)))
37;;;
38;;; The 'animate can be 'jump or 'banish or 'protean if you prefer.
39;;;
40;;; For added silliness, make the animatee animate...
41;;; put something similar to the following into your .emacs:
42;;;
43;;; (cond (window-system
44;;; (setq x-pointer-shape
45;;; (eval (nth (random 4)
46;;; '(x-pointer-man x-pointer-spider
47;;; x-pointer-gobbler x-pointer-gumby))))
48;;; (set-mouse-color (cdr (assoc 'mouse-color (frame-parameters))))))
49;;;
50;;; For completely random pointer shape, replace the setq above with:
51;;; (setq x-pointer-shape (mouse-avoidance-random-shape))
52;;;
53;;; Bugs & Warnings:
54;;;
55;;; - THIS CODE IS FOR USE WITH FSF EMACS 19.21 or later.
56;;; It can cause earlier versions of emacs to crash, due to a bug in the
57;;; mouse code.
58;;;
59;;; - Using this code does slow emacs down. "banish" mode shouldn't
60;;; ever be too bad though, and on my workstation even "animate" doesn't
61;;; seem to have a noticable effect.
62;;;
63;;; - There are some situations where it doesn't do what you expect,
64;;; notably when there are long wrapped lines in the buffer. Since
65;;; there is no low-level command for finding point's position
66;;; on the screen, it can fail to move the pointer when on such a line.
67;;;
68;;; Copyright (c) 1993 Free Software Foundation
69;;; This program is free software; you can redistribute it and/or modify
70;;; it under the terms of the GNU General Public License as published by
71;;; the Free Software Foundation; either version 2, or (at your option)
72;;; any later version.
73;;;
74;;; This program is distributed in the hope that it will be useful,
75;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
76;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77;;; GNU General Public License for more details.
78;;;
79;;; A copy of the GNU General Public License can be obtained from this
80;;; program's author or from the Free Software Foundation, Inc., 675
81;;; Mass Ave, Cambridge, MA 02139, USA.
82
83;;; LCD Archive Entry:
84;;; avoid|Boris Goldowsky|boris@cs.rochester.edu|
85;;; Keep mouse pointer away from cursor|
86;;; $Date: 93/11/22 15:34:50 $ |$Revision: 1.10 $||
87
88;;; Credits:
89;;; This code was helped by all those who contributed suggestions, fixes, and
90;;; additions:
91;;; Joe Harrington (and his advisor), for the original inspiration
92;;; Ken Manheimer, for dreaming up the Protean mode
93;;; Richard Stallman, for the awful cat-and-mouse pun, among other things
94;;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
95;;; Simon Marshall, and M.S. Ashton, for their feedback.
96;;;
97;;; Code:
98
99(provide 'avoid)
100
101(defvar mouse-avoidance-mode nil
102 "Value is t or a symbol if the mouse pointer should avoid the cursor.
103See function mouse-avoidance-mode for possible values. Changing this
104variable is NOT the recommended way to change modes; use the function
105instead.")
106
107(defvar mouse-avoidance-nudge-dist 4
108 "*Average distance that mouse will be moved when approached by cursor.
109Only applies in mode-avoidance-modes `animate' and `jump'.")
110
111(defvar mouse-avoidance-nudge-var 3
112 "*Variability of mouse-avoidance-nudge-dist (which see).")
113
114(defvar mouse-avoidance-animation-delay .01
115 "Delay between animation steps, in seconds.")
116
117(defvar mouse-avoidance-threshhold 5
118 "*Mouse-pointer's flight distance.
119If the cursor gets closer than this, the mouse pointer will move away.
120Only applies in mouse-avoidance-modes `animate' and `jump'.")
121
122;; Internal variables for mouse-avoidance-random-shape
123(defvar mouse-avoidance-pointer-shapes nil)
124(defvar mouse-avoidance-n-pointer-shapes 0)
125
126;;; Functions:
127
128(defun mouse-avoidance-too-close-p ()
129 ;; Return t if mouse pointer and point cursor are too close.
130 ;; Acceptable distance is defined by mouse-avoidance-threshhold.
131 (let ((mouse (mouse-position)))
132 (and (car (cdr mouse))
133 (< (abs (- (car (cdr mouse)) (current-column)))
134 mouse-avoidance-threshhold)
135 (< (abs (- (cdr (cdr mouse))
136 (+ (car (cdr (window-edges)))
137 (count-lines (window-start) (point)))))
138 mouse-avoidance-threshhold))))
139
140(defun mouse-avoidance-banish-mouse ()
141 ;; Put the mouse pointer in the upper-right corner of the current frame.
142 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
143
144(defun mouse-avoidance-nudge-mouse ()
145 ;; Push the mouse a little way away, possibly animating the move
146 (let* ((cur (mouse-position))
147 (deltax (* (+ mouse-avoidance-nudge-dist
148 (random mouse-avoidance-nudge-var))
149 (if (zerop (random 2)) 1 -1)))
150 (deltay (* (+ mouse-avoidance-nudge-dist
151 (random mouse-avoidance-nudge-var))
152 (if (zerop (random 2)) 1 -1))))
153 (if (or (eq mouse-avoidance-mode 'animate)
154 (eq mouse-avoidance-mode 'proteus))
155 (let ((i 0.0)
156 (color (cdr (assoc 'mouse-color (frame-parameters)))))
157 (while (<= i 1)
158 (set-mouse-position
159 (car cur)
160 (mod (+ (car (cdr cur)) (round (* i deltax))) (window-width))
161 (mod (+ (cdr (cdr cur)) (round (* i deltay))) (window-height)))
162 (setq i (+ i (/ 1.0 mouse-avoidance-nudge-dist)))
163 (if (eq mouse-avoidance-mode 'proteus)
164 (progn
165 (setq x-pointer-shape (mouse-avoidance-random-shape))
166 (set-mouse-color color)))
167 (sit-for mouse-avoidance-animation-delay)))
168 (set-mouse-position
169 (car cur)
170 (mod (+ (car (cdr cur)) deltax) (window-width))
171 (mod (+ (cdr (cdr cur)) deltay) (window-height))))))
172
173(defun mouse-avoidance-random-shape ()
174 "Return a random cursor shape.
175This assumes that any variable whose name begins with x-pointer- and
176has an integer value is a valid cursor shape. You might want to
177redefine this function to suit your own tastes."
178 (if (null mouse-avoidance-pointer-shapes)
179 (progn
180 (setq mouse-avoidance-pointer-shapes
181 (mapcar '(lambda (x) (symbol-value (intern x)))
182 (all-completions "x-pointer-" obarray
183 '(lambda (x)
184 (and (boundp x)
185 (integerp (symbol-value x)))))))
186 (setq mouse-avoidance-n-pointer-shapes
187 (length mouse-avoidance-pointer-shapes))))
188 (nth (random mouse-avoidance-n-pointer-shapes)
189 mouse-avoidance-pointer-shapes))
190
191(defun mouse-avoidance-simple-hook ()
192 (if (and (mouse-avoidance-keyboard-command (this-command-keys)))
193 (mouse-avoidance-banish-mouse)))
194
195(defun mouse-avoidance-fancy-hook ()
196 (if (and (mouse-avoidance-keyboard-command (this-command-keys))
197 (mouse-avoidance-too-close-p))
198 (mouse-avoidance-nudge-mouse)))
199
200(defun mouse-avoidance-keyboard-command (key)
201 "Return t if the KEYSEQENCE is composed of keyboard events only.
202Returns nil if there are any lists in the key sequence."
203 (cond ((null key) nil) ; Null event seems to be
204 ; returned occasionally.
205 ((not (vectorp key)) t) ; Strings are keyboard events.
206 ((catch 'done
207 (let ((i 0)
208 (l (length key)))
209 (while (< i l)
210 (if (listp (aref key i))
211 (throw 'done nil))
212 (setq i (1+ i))))
213 t))))
214
215(defun mouse-avoidance-mode (&optional mode)
216 "Set cursor avoidance mode to MODE.
217MODE should be one of the symbols `banish', `jump', `animate',
218`cat-and-mouse', or `none'. `Animate' is the same as `cat-and-mouse'.
219If MODE is nil, toggle mouse avoidance. Positive numbers and
220symbols other than the above are treated as equivalent to `banish';
221negative numbers and `-' are equivalent to `none'."
222 (interactive
223 (list (intern (completing-read
224 "Select cursor avoidance technique (SPACE for list): "
225 '(("banish") ("jump") ("animate") ("cat-and-mouse")
226 ("proteus") ("none"))
227 nil t))))
228 (if (eq mode 'cat-and-mouse)
229 (setq mode 'animate))
230 (setq post-command-hook
231 (delete 'mouse-avoidance-simple-hook (append post-command-hook nil)))
232 (setq post-command-hook
233 (delete 'mouse-avoidance-fancy-hook (append post-command-hook nil)))
234 (cond ((eq mode 'none)
235 (setq mouse-avoidance-mode nil))
236 ((or (eq mode 'jump)
237 (eq mode 'animate)
238 (eq mode 'proteus))
239 (add-hook 'post-command-hook 'mouse-avoidance-fancy-hook)
240 (setq mouse-avoidance-mode mode))
241 ((or (eq mode 'banish)
242 (eq mode t)
243 (and (null mode) (null mouse-avoidance-mode))
244 (and mode (> (prefix-numeric-value mode) 0)))
245 (add-hook 'post-command-hook 'mouse-avoidance-simple-hook)
246 (setq mouse-avoidance-mode 'banish))
247 (t (setq mouse-avoidance-mode nil))))
248
249(or (assq 'mouse-avoidance-mode minor-mode-alist)
250 (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
251 minor-mode-alist)))
252
253;;; End of avoid.el
254