annotate lisp/packages/avoid.el @ 0:376386a54a3c r19-14

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