0
|
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@cs.rochester.edu>
|
|
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
|
|
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24
|
|
25 ;;; Synched up with: FSF 19.28.
|
|
26
|
|
27 ;;; Commentary:
|
|
28 ;;;
|
|
29 ;;; For those who are annoyed by the mouse pointer obscuring text,
|
|
30 ;;; this mode moves the mouse pointer - either just a little out of
|
|
31 ;;; the way, or all the way to the corner of the frame.
|
|
32 ;;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
|
|
33 ;;; To set up permanently, put this file on your load-path and put the
|
|
34 ;;; following in your .emacs:
|
|
35 ;;;
|
|
36 ;;; (cond (window-system
|
|
37 ;;; (require 'avoid)
|
|
38 ;;; (mouse-avoidance-mode 'animate)))
|
|
39 ;;;
|
|
40 ;;; The 'animate can be 'jump or 'banish or 'exile or 'protean if you prefer.
|
|
41 ;;; See the documentation for function `mouse-avoidance-mode' for
|
|
42 ;;; details of the different modes.
|
|
43 ;;;
|
|
44 ;;; For added silliness, make the animatee animate...
|
|
45 ;;; put something similar to the following into your .emacs:
|
|
46 ;;;
|
|
47 ;;; (cond (window-system
|
|
48 ;;; (setq x-pointer-shape
|
|
49 ;;; (eval (nth (random 4)
|
|
50 ;;; '(x-pointer-man x-pointer-spider
|
|
51 ;;; x-pointer-gobbler x-pointer-gumby))))
|
|
52 ;;; (set-mouse-color (cdr (assoc 'mouse-color (frame-parameters))))))
|
|
53 ;;;
|
|
54 ;;; For completely random pointer shape, replace the setq above with:
|
|
55 ;;; (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
56 ;;;
|
|
57 ;;; Bugs / Warnings / To-Do:
|
|
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 during editing.
|
|
62 ;;;
|
|
63 ;;; - It should find out where any overlapping frames are and avoid them,
|
|
64 ;;; rather than always raising the frame.
|
|
65
|
|
66 ;;; Credits:
|
|
67 ;;; This code was helped by all those who contributed suggestions,
|
|
68 ;;; fixes, and additions
|
|
69 ;;; Joe Harrington (and his advisor), for the original inspiration.
|
|
70 ;;; Ken Manheimer, for dreaming up the Protean mode.
|
|
71 ;;; Richard Stallman, for the awful cat-and-mouse pun, among other things.
|
|
72 ;;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
|
|
73 ;;; Simon Marshall, and M.S. Ashton, for their feedback.
|
|
74 ;;;
|
|
75 ;;; Code:
|
|
76
|
|
77 (provide 'avoid)
|
|
78
|
|
79 (defvar mouse-avoidance-mode nil
|
|
80 "Value is t or a symbol if the mouse pointer should avoid the cursor.
|
|
81 See function `mouse-avoidance-mode' for possible values. Changing this
|
|
82 variable is NOT the recommended way to change modes; use that function
|
|
83 instead.")
|
|
84
|
|
85 (defvar mouse-avoidance-nudge-dist 15
|
|
86 "*Average distance that mouse will be moved when approached by cursor.
|
|
87 Only applies in mouse-avoidance-mode `jump' and its derivatives.
|
|
88 For best results make this larger than `mouse-avoidance-threshold'.")
|
|
89
|
|
90 (defvar mouse-avoidance-nudge-var 10
|
|
91 "*Variability of `mouse-avoidance-nudge-dist' (which see).")
|
|
92
|
|
93 (defvar mouse-avoidance-animation-delay .01
|
|
94 "Delay between animation steps, in seconds.")
|
|
95
|
|
96 (defvar mouse-avoidance-threshold 5
|
|
97 "*Mouse-pointer's flight distance.
|
|
98 If the cursor gets closer than this, the mouse pointer will move away.
|
|
99 Only applies in mouse-avoidance-modes `animate' and `jump'.")
|
|
100
|
|
101 ;; Internal variables
|
|
102 (defvar mouse-avoidance-state nil)
|
|
103 (defvar mouse-avoidance-pointer-shapes nil)
|
|
104 (defvar mouse-avoidance-n-pointer-shapes 0)
|
|
105
|
|
106 ;;; Functions:
|
|
107
|
|
108 ;; XEmacs change -- this is so ugly.
|
|
109 (defun mouse-avoidance-point-position ()
|
|
110 "Returns (WINDOW X . Y) of current point - analogous to mouse-position"
|
|
111 (let* ((beg (window-start))
|
|
112 (pos (point))
|
|
113 (col (current-column))
|
|
114 (row))
|
|
115 (setq row (count-lines beg pos))
|
|
116 (cons (selected-window) (cons col row))))
|
|
117
|
|
118 ;(defun mouse-avoidance-point-position-test ()
|
|
119 ; (interactive)
|
|
120 ; (message (format "point=%s mouse=%s"
|
|
121 ; (cdr (mouse-avoidance-point-position))
|
|
122 ; (cdr (mouse-position)))))
|
|
123
|
|
124 (defun mouse-avoidance-set-mouse-position (pos)
|
|
125 ;; Carefully set mouse position to given position (X . Y)
|
|
126 ;; Ideally, should check if X,Y is in the current frame, and if not,
|
|
127 ;; leave the mouse where it was. However, this is currently
|
|
128 ;; difficult to do, so we just raise the frame to avoid frame switches.
|
|
129 ;; Returns t if it moved the mouse.
|
|
130 (let ((f (selected-frame)))
|
|
131 (raise-frame f)
|
|
132 (set-mouse-position (frame-selected-window f) (car pos) (cdr pos))
|
|
133 t))
|
|
134
|
|
135 (defun mouse-avoidance-too-close-p (mouse)
|
|
136 ;; Return t if mouse pointer and point cursor are too close.
|
|
137 ;; Acceptable distance is defined by mouse-avoidance-threshold.
|
|
138 (let ((point (mouse-avoidance-point-position)))
|
|
139 (and (eq (car mouse) (car point))
|
|
140 (car (cdr mouse))
|
|
141 (< (abs (- (car (cdr mouse)) (car (cdr point))))
|
|
142 mouse-avoidance-threshold)
|
|
143 (< (abs (- (cdr (cdr mouse)) (cdr (cdr point))))
|
|
144 mouse-avoidance-threshold))))
|
|
145
|
|
146 (defun mouse-avoidance-banish-destination ()
|
|
147 "The position to which mouse-avoidance-mode `banish' moves the mouse.
|
|
148 You can redefine this if you want the mouse banished to a different corner."
|
|
149 (cons (1- (frame-width))
|
|
150 0))
|
|
151
|
|
152 (defun mouse-avoidance-banish-mouse ()
|
|
153 ;; Put the mouse pointer in the upper-right corner of the current frame.
|
|
154 (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
|
|
155
|
|
156 (defsubst mouse-avoidance-delta (cur delta dist var min max)
|
|
157 ;; Decide how far to move in either dimension.
|
|
158 ;; Args are the CURRENT location, the desired DELTA for
|
|
159 ;; warp-conservation, the DISTANCE we like to move, the VARIABILITY
|
|
160 ;; in distance allowed, and the MIN and MAX possible window positions.
|
|
161 ;; Returns something as close to DELTA as possible withing the constraints.
|
|
162 (let ((L1 (max (- min cur) (+ (- dist) (- var))))
|
|
163 (R1 (+ (- dist) var ))
|
|
164 (L2 (+ dist (- var)))
|
|
165 (R2 (min (- max cur) (+ dist var))))
|
|
166 (if (< R1 (- min cur)) (setq L1 nil R1 nil))
|
|
167 (if (> L2 (- max cur)) (setq L2 nil R2 nil))
|
|
168 (cond ((and L1 (< delta L1)) L1)
|
|
169 ((and R1 (< delta R1)) delta)
|
|
170 ((and R1 (< delta 0)) R1)
|
|
171 ((and L2 (< delta L2)) L2)
|
|
172 ((and R2 (< delta R2)) delta)
|
|
173 (R2)
|
|
174 ((or R1 L2))
|
|
175 (t 0))))
|
|
176
|
|
177 (defun mouse-avoidance-nudge-mouse ()
|
|
178 ;; Push the mouse a little way away, possibly animating the move
|
|
179 ;; For these modes, state keeps track of the total offset that we've
|
|
180 ;; accumulated, and tries to keep it close to zero.
|
|
181 (let* ((cur (mouse-position))
|
|
182 (cur-pos (cdr cur))
|
|
183 (deltax (mouse-avoidance-delta
|
|
184 (car cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
185 (car mouse-avoidance-state))
|
|
186 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
|
187 0 (frame-width)))
|
|
188 (deltay (mouse-avoidance-delta
|
|
189 (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
|
|
190 (cdr mouse-avoidance-state))
|
|
191 mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
|
|
192 0 (window-height))))
|
|
193 (setq mouse-avoidance-state
|
|
194 (cons (+ (car mouse-avoidance-state) deltax)
|
|
195 (+ (cdr mouse-avoidance-state) deltay)))
|
|
196 (if (or (eq mouse-avoidance-mode 'animate)
|
|
197 (eq mouse-avoidance-mode 'proteus))
|
|
198 (let ((i 0.0)
|
|
199 (color (cdr (assoc 'mouse-color (frame-parameters)))))
|
|
200 (while (<= i 1)
|
|
201 (mouse-avoidance-set-mouse-position
|
|
202 (cons (+ (car cur-pos) (round (* i deltax)))
|
|
203 (+ (cdr cur-pos) (round (* i deltay)))))
|
|
204 (setq i (+ i (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
|
|
205 (if (eq mouse-avoidance-mode 'proteus)
|
|
206 (progn
|
|
207 (setq x-pointer-shape (mouse-avoidance-random-shape))
|
|
208 (set-mouse-color color)))
|
|
209 (sit-for mouse-avoidance-animation-delay)))
|
|
210 (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
|
|
211 (+ (cdr (cdr cur)) deltay))))))
|
|
212
|
|
213 (defun mouse-avoidance-random-shape ()
|
|
214 "Return a random cursor shape.
|
|
215 This assumes that any variable whose name begins with x-pointer- and
|
|
216 has an integer value is a valid cursor shape. You might want to
|
|
217 redefine this function to suit your own tastes."
|
|
218 (if (null mouse-avoidance-pointer-shapes)
|
|
219 (progn
|
|
220 (setq mouse-avoidance-pointer-shapes
|
|
221 (mapcar '(lambda (x) (symbol-value (intern x)))
|
|
222 (all-completions "x-pointer-" obarray
|
|
223 '(lambda (x)
|
|
224 (and (boundp x)
|
|
225 (integerp (symbol-value x)))))))
|
|
226 (setq mouse-avoidance-n-pointer-shapes
|
|
227 (length mouse-avoidance-pointer-shapes))))
|
|
228 (nth (random mouse-avoidance-n-pointer-shapes)
|
|
229 mouse-avoidance-pointer-shapes))
|
|
230
|
|
231 (defun mouse-avoidance-banish-hook ()
|
|
232 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
233 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
234 (mouse-avoidance-banish-mouse)))
|
|
235
|
|
236 (defun mouse-avoidance-exile-hook ()
|
|
237 ;; For exile mode, the state is nil when the mouse is in its normal
|
|
238 ;; position, and set to the old mouse-position when the mouse is in exile.
|
|
239 (if (and (not executing-kbd-macro)
|
|
240 (mouse-avoidance-kbd-command (this-command-keys)))
|
|
241 (let ((mp (mouse-position)))
|
|
242 (cond ((and (not mouse-avoidance-state)
|
|
243 (mouse-avoidance-too-close-p mp))
|
|
244 (setq mouse-avoidance-state mp)
|
|
245 (mouse-avoidance-banish-mouse))
|
|
246 ((and mouse-avoidance-state
|
|
247 (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
|
|
248 (if (and (eq (car mp) (if (< emacs-minor-version 12)
|
|
249 (selected-frame)
|
|
250 (selected-window)))
|
|
251 (equal (cdr mp) (mouse-avoidance-banish-destination)))
|
|
252 (mouse-avoidance-set-mouse-position
|
|
253 ;; move back only if user has not moved mouse
|
|
254 (cdr mouse-avoidance-state)))
|
|
255 ;; but clear state anyway, to be ready for another move
|
|
256 (setq mouse-avoidance-state nil))))))
|
|
257
|
|
258 (defun mouse-avoidance-fancy-hook ()
|
|
259 ;; Used for the "fancy" modes, ie jump et al.
|
|
260 (if (and (not executing-kbd-macro) ; don't check inside macro
|
|
261 (mouse-avoidance-kbd-command (this-command-keys))
|
|
262 (mouse-avoidance-too-close-p (mouse-position)))
|
|
263 (let ((old-pos (mouse-position)))
|
|
264 (mouse-avoidance-nudge-mouse)
|
|
265 (if (not (eq (if (< emacs-minor-version 12)
|
|
266 (selected-frame)
|
|
267 (selected-window)) (car old-pos))) ; move went awry
|
|
268 (set-mouse-position (car old-pos) ; sigh..
|
|
269 (car (cdr old-pos))
|
|
270 (cdr (cdr old-pos)))))))
|
|
271
|
|
272 (defun mouse-avoidance-kbd-command (key)
|
|
273 "Return t if the KEYSEQENCE is composed of keyboard events only.
|
|
274 Return nil if there are any lists in the key sequence."
|
|
275 (cond ((null key) nil) ; Null event seems to be
|
|
276 ; returned occasionally.
|
|
277 ((not (vectorp key)) t) ; Strings are keyboard events.
|
|
278 ((catch 'done
|
|
279 (let ((i 0)
|
|
280 (l (length key)))
|
|
281 (while (< i l)
|
|
282 (if (listp (aref key i))
|
|
283 (throw 'done nil))
|
|
284 (setq i (1+ i))))
|
|
285 t))))
|
|
286
|
|
287 (defun mouse-avoidance-mode (&optional mode)
|
|
288 "Set cursor avoidance mode to MODE.
|
|
289 MODE should be one of the symbols `banish', `exile', `jump', `animate',
|
|
290 `cat-and-mouse', `proteus', or `none'.
|
|
291
|
|
292 If MODE is nil, toggle mouse avoidance between `none` and `banish'
|
|
293 modes. Positive numbers and symbols other than the above are treated
|
|
294 as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
|
|
295
|
|
296 Effects of the different modes:
|
|
297 * banish: Move the mouse to the upper-right corner on any keypress.
|
|
298 * exile: Move the mouse to the corner only if the cursor gets too close,
|
|
299 and allow it to return once the cursor is out of the way.
|
|
300 * jump: If the cursor gets too close to the mouse, displace the mouse
|
|
301 a random distance & direction.
|
|
302 * animate: As `jump', but shows steps along the way for illusion of motion.
|
|
303 * cat-and-mouse: Same as `animate'.
|
|
304 * proteus: As `animate', but changes the shape of the mouse pointer too.
|
|
305
|
|
306 Whenever the mouse is moved, the frame is also raised.
|
|
307
|
|
308 \(see `mouse-avoidance-threshold' for definition of \"too close\",
|
|
309 and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
|
|
310 definition of \"random distance\".)"
|
|
311 (interactive
|
|
312 (list (intern (completing-read
|
|
313 "Select cursor avoidance technique (SPACE for list): "
|
|
314 '(("banish") ("exile") ("jump") ("animate")
|
|
315 ("cat-and-mouse") ("proteus") ("none"))
|
|
316 nil t))))
|
|
317 (if (eq mode 'cat-and-mouse)
|
|
318 (setq mode 'animate))
|
|
319 (setq post-command-hook
|
|
320 (delete 'mouse-avoidance-banish-hook (append post-command-hook nil)))
|
|
321 (setq post-command-hook
|
|
322 (delete 'mouse-avoidance-exile-hook (append post-command-hook nil)))
|
|
323 (setq post-command-hook
|
|
324 (delete 'mouse-avoidance-fancy-hook (append post-command-hook nil)))
|
|
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-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-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-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 ;;XEmacs: do it right.
|
|
350 ;;;###autoload
|
|
351 (add-minor-mode 'mouse-avoidance-mode " Avoid")
|
|
352
|
|
353 ;;; End of avoid.el
|