123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- (provide 'avoid)
- (defgroup avoid nil
- "Make mouse pointer stay out of the way of editing."
- :prefix "mouse-avoidance-"
- :group 'mouse)
- (defcustom mouse-avoidance-mode nil
- "Activate Mouse Avoidance mode.
- See function `mouse-avoidance-mode' for possible values.
- Setting this variable directly does not take effect;
- use either \\[customize] or the function `mouse-avoidance-mode'."
- :set (lambda (symbol value)
-
- (mouse-avoidance-mode (or value 'none)))
- :initialize 'custom-initialize-default
- :type '(choice (const :tag "none" nil) (const banish) (const jump)
- (const animate) (const exile) (const proteus))
- :group 'avoid
- :require 'avoid
- :version "20.3")
- (defcustom mouse-avoidance-nudge-dist 15
- "Average distance that mouse will be moved when approached by cursor.
- Only applies in Mouse Avoidance mode `jump' and its derivatives.
- For best results make this larger than `mouse-avoidance-threshold'."
- :type 'integer
- :group 'avoid)
- (defcustom mouse-avoidance-nudge-var 10
- "Variability of `mouse-avoidance-nudge-dist' (which see)."
- :type 'integer
- :group 'avoid)
- (defcustom mouse-avoidance-animation-delay .01
- "Delay between animation steps, in seconds."
- :type 'number
- :group 'avoid)
- (defcustom mouse-avoidance-threshold 5
- "Mouse-pointer's flight distance.
- If the cursor gets closer than this, the mouse pointer will move away.
- Only applies in Mouse Avoidance modes `animate' and `jump'."
- :type 'integer
- :group 'avoid)
- (defvar mouse-avoidance-state nil)
- (defvar mouse-avoidance-pointer-shapes nil)
- (defvar mouse-avoidance-n-pointer-shapes 0)
- (defvar mouse-avoidance-old-pointer-shape nil)
- (defvar mouse-avoidance-animating-pointer nil)
- (defvar mouse-avoidance-timer nil)
- (defsubst mouse-avoidance-set-pointer-shape (shape)
- "Set the shape of the mouse pointer to SHAPE."
- (when (boundp 'x-pointer-shape)
- (setq x-pointer-shape shape)
- (set-mouse-color nil)))
- (defun mouse-avoidance-point-position ()
- "Return the position of point as (FRAME X . Y).
- Analogous to `mouse-position'."
- (let ((edges (window-inside-edges))
- (x-y (posn-x-y (posn-at-point))))
- (cons (selected-frame)
- (cons (+ (car edges)
- (/ (car x-y) (frame-char-width)))
- (+ (car (cdr edges))
- (/ (cdr x-y) (frame-char-height)))))))
- (defun mouse-avoidance-set-mouse-position (pos)
-
-
-
-
-
- (let ((f (selected-frame)))
- (raise-frame f)
- (set-mouse-position f (car pos) (cdr pos))
- t))
- (defun mouse-avoidance-too-close-p (mouse)
- "Return t if mouse pointer and point cursor are too close.
- MOUSE is the current mouse position as returned by `mouse-position'.
- Acceptable distance is defined by `mouse-avoidance-threshold'."
- (let* ((frame (car mouse))
- (mouse-y (cdr (cdr mouse)))
- (tool-bar-lines (frame-parameter nil 'tool-bar-lines)))
- (or tool-bar-lines
- (setq tool-bar-lines 0))
- (if (and mouse-y (< mouse-y tool-bar-lines))
- nil
- (let ((point (mouse-avoidance-point-position))
- (mouse-x (car (cdr mouse))))
- (and (eq frame (car point))
- (not (null mouse-x))
- (< (abs (- mouse-x (car (cdr point))))
- mouse-avoidance-threshold)
- (< (abs (- mouse-y (cdr (cdr point))))
- mouse-avoidance-threshold))))))
- (defun mouse-avoidance-banish-destination ()
- "The position to which Mouse Avoidance mode `banish' moves the mouse.
- You can redefine this if you want the mouse banished to a different corner."
- (let* ((pos (window-edges)))
- (cons (- (nth 2 pos) 2)
- (nth 1 pos))))
- (defun mouse-avoidance-banish-mouse ()
-
- (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
- (defsubst mouse-avoidance-delta (cur delta dist var min max)
-
-
-
-
-
- (let ((L1 (max (- min cur) (+ (- dist) (- var))))
- (R1 (+ (- dist) var ))
- (L2 (+ dist (- var)))
- (R2 (min (- max cur) (+ dist var))))
- (if (< R1 (- min cur)) (setq L1 nil R1 nil))
- (if (> L2 (- max cur)) (setq L2 nil R2 nil))
- (cond ((and L1 (< delta L1)) L1)
- ((and R1 (< delta R1)) delta)
- ((and R1 (< delta 0)) R1)
- ((and L2 (< delta L2)) L2)
- ((and R2 (< delta R2)) delta)
- (R2)
- ((or R1 L2))
- (t 0))))
- (defun mouse-avoidance-nudge-mouse ()
-
-
-
- (let* ((cur (mouse-position))
- (cur-frame (car cur))
- (cur-pos (cdr cur))
- (pos (window-edges))
- (wleft (pop pos))
- (wtop (pop pos))
- (wright (pop pos))
- (wbot (pop pos))
- (deltax (mouse-avoidance-delta
- (car cur-pos) (- (random mouse-avoidance-nudge-var)
- (car mouse-avoidance-state))
- mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
- wleft (1- wright)))
- (deltay (mouse-avoidance-delta
- (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
- (cdr mouse-avoidance-state))
- mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
- wtop (1- wbot))))
- (setq mouse-avoidance-state
- (cons (+ (car mouse-avoidance-state) deltax)
- (+ (cdr mouse-avoidance-state) deltay)))
- (if (or (eq mouse-avoidance-mode 'animate)
- (eq mouse-avoidance-mode 'proteus))
- (let ((i 0.0)
- (incr (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
- (setq mouse-avoidance-animating-pointer t)
- (while (<= i 1)
- (mouse-avoidance-set-mouse-position
- (cons (+ (car cur-pos) (round (* i deltax)))
- (+ (cdr cur-pos) (round (* i deltay)))))
- (setq i (+ i incr))
- (if (eq mouse-avoidance-mode 'proteus)
- (mouse-avoidance-set-pointer-shape
- (mouse-avoidance-random-shape)))
- (sit-for mouse-avoidance-animation-delay))
- (setq mouse-avoidance-animating-pointer nil))
- (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
- (+ (cdr (cdr cur)) deltay))))))
- (defun mouse-avoidance-random-shape ()
- "Return a random cursor shape.
- This assumes that any variable whose name begins with x-pointer- and
- has an integer value is a valid cursor shape. You might want to
- redefine this function to suit your own tastes."
- (if (null mouse-avoidance-pointer-shapes)
- (progn
- (setq mouse-avoidance-pointer-shapes
- (mapcar (lambda (x) (symbol-value (intern x)))
- (all-completions "x-pointer-" obarray
- (lambda (x)
- (and (boundp x)
- (integerp (symbol-value x)))))))
- (setq mouse-avoidance-n-pointer-shapes
- (length mouse-avoidance-pointer-shapes))))
- (nth (random mouse-avoidance-n-pointer-shapes)
- mouse-avoidance-pointer-shapes))
- (defun mouse-avoidance-ignore-p ()
- (let ((mp (mouse-position)))
- (or (not (frame-pointer-visible-p))
- (not cursor-type)
- executing-kbd-macro
- (null (cadr mp))
- (not (eq (car mp) (selected-frame)))
-
-
-
- (and (consp last-input-event)
- (symbolp (car last-input-event))
- (let ((modifiers (event-modifiers (car last-input-event))))
- (or (memq (car last-input-event)
- '(mouse-movement scroll-bar-movement
- select-window switch-frame))
- (memq 'click modifiers)
- (memq 'double modifiers)
- (memq 'triple modifiers)
- (memq 'drag modifiers)
- (memq 'down modifiers)))))))
- (defun mouse-avoidance-banish ()
- (if (not (mouse-avoidance-ignore-p))
- (mouse-avoidance-banish-mouse)))
- (defun mouse-avoidance-exile ()
-
-
- (if (not (mouse-avoidance-ignore-p))
- (let ((mp (mouse-position)))
- (cond ((and (not mouse-avoidance-state)
- (mouse-avoidance-too-close-p mp))
- (setq mouse-avoidance-state mp)
- (mouse-avoidance-banish-mouse))
- ((and mouse-avoidance-state
- (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
- (if (and (eq (car mp) (selected-frame))
- (equal (cdr mp) (mouse-avoidance-banish-destination)))
- (mouse-avoidance-set-mouse-position
-
- (cdr mouse-avoidance-state)))
-
- (setq mouse-avoidance-state nil))))))
- (defun mouse-avoidance-fancy ()
-
- (if (and (not mouse-avoidance-animating-pointer)
- (not (mouse-avoidance-ignore-p))
- (mouse-avoidance-too-close-p (mouse-position)))
- (let ((old-pos (mouse-position)))
- (mouse-avoidance-nudge-mouse)
- (if (not (eq (selected-frame) (car old-pos)))
-
- (apply 'set-mouse-position old-pos)))))
- (defun mouse-avoidance-mode (&optional mode)
- "Set Mouse Avoidance mode to MODE.
- MODE should be one of the symbols `banish', `exile', `jump', `animate',
- `cat-and-mouse', `proteus', or `none'.
- If MODE is nil, toggle mouse avoidance between `none' and `banish'
- modes. Positive numbers and symbols other than the above are treated
- as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
- Effects of the different modes:
- * banish: Move the mouse to the upper-right corner on any keypress.
- * exile: Move the mouse to the corner only if the cursor gets too close,
- and allow it to return once the cursor is out of the way.
- * jump: If the cursor gets too close to the mouse, displace the mouse
- a random distance & direction.
- * animate: As `jump', but shows steps along the way for illusion of motion.
- * cat-and-mouse: Same as `animate'.
- * proteus: As `animate', but changes the shape of the mouse pointer too.
- Whenever the mouse is moved, the frame is also raised.
- \(See `mouse-avoidance-threshold' for definition of \"too close\",
- and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
- definition of \"random distance\".)"
- (interactive
- (list (intern (completing-read
- "Select cursor avoidance technique (SPACE for list): "
- '(("banish") ("exile") ("jump") ("animate")
- ("cat-and-mouse") ("proteus") ("none"))
- nil t))))
- (if (eq mode 'cat-and-mouse)
- (setq mode 'animate))
- (if mouse-avoidance-timer
- (cancel-timer mouse-avoidance-timer))
- (setq mouse-avoidance-timer nil)
-
- (if (eq mouse-avoidance-mode 'proteus)
- (mouse-avoidance-set-pointer-shape mouse-avoidance-old-pointer-shape))
-
- (cond ((eq mode 'none)
- (setq mouse-avoidance-mode nil))
- ((or (eq mode 'jump)
- (eq mode 'animate)
- (eq mode 'proteus))
- (setq mouse-avoidance-timer
- (run-with-idle-timer 0.1 t 'mouse-avoidance-fancy))
- (setq mouse-avoidance-mode mode
- mouse-avoidance-state (cons 0 0)
- mouse-avoidance-old-pointer-shape
- (and (boundp 'x-pointer-shape) x-pointer-shape)))
- ((eq mode 'exile)
- (setq mouse-avoidance-timer
- (run-with-idle-timer 0.1 t 'mouse-avoidance-exile))
- (setq mouse-avoidance-mode mode
- mouse-avoidance-state nil))
- ((or (eq mode 'banish)
- (eq mode t)
- (and (null mode) (null mouse-avoidance-mode))
- (and mode (> (prefix-numeric-value mode) 0)))
- (setq mouse-avoidance-timer
- (run-with-idle-timer 0.1 t 'mouse-avoidance-banish))
- (setq mouse-avoidance-mode 'banish))
- (t (setq mouse-avoidance-mode nil)))
- (force-mode-line-update))
- (if mouse-avoidance-mode
- (mouse-avoidance-mode mouse-avoidance-mode))
|