home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / avoid.el.z / avoid.el
Encoding:
Text File  |  1998-05-21  |  15.1 KB  |  398 lines

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