avoid.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ;;; avoid.el --- make mouse pointer stay out of the way of editing
  2. ;; Copyright (C) 1993-1994, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Boris Goldowsky <boris@gnu.org>
  4. ;; Keywords: mouse
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; For those who are annoyed by the mouse pointer obscuring text,
  18. ;; this mode moves the mouse pointer - either just a little out of
  19. ;; the way, or all the way to the corner of the frame.
  20. ;; To use, load or evaluate this file and type M-x mouse-avoidance-mode .
  21. ;; To set up permanently, put the following in your .emacs:
  22. ;;
  23. ;; (if (display-mouse-p) (mouse-avoidance-mode 'animate))
  24. ;;
  25. ;; Other legitimate alternatives include
  26. ;; `banish', `exile', `jump', `cat-and-mouse', and `proteus'.
  27. ;; They do somewhat different things.
  28. ;; See the documentation for function `mouse-avoidance-mode' for
  29. ;; details of the different modes.
  30. ;;
  31. ;; For added silliness, make the animatee animate...
  32. ;; put something similar to the following into your .emacs:
  33. ;;
  34. ;; (if (eq window-system 'x)
  35. ;; (mouse-avoidance-set-pointer-shape
  36. ;; (eval (nth (random 4)
  37. ;; '(x-pointer-man x-pointer-spider
  38. ;; x-pointer-gobbler x-pointer-gumby)))))
  39. ;;
  40. ;; For completely random pointer shape, replace the setq above with:
  41. ;; (setq x-pointer-shape (mouse-avoidance-random-shape))
  42. ;;
  43. ;; Bugs / Warnings / To-Do:
  44. ;;
  45. ;; - Using this code does slow Emacs down. "banish" mode shouldn't
  46. ;; be too bad, and on my workstation even "animate" is reasonable.
  47. ;;
  48. ;; - It ought to find out where any overlapping frames are and avoid them,
  49. ;; rather than always raising the frame.
  50. ;; Credits:
  51. ;; This code was helped by all those who contributed suggestions,
  52. ;; fixes, and additions
  53. ;; Joe Harrington (and his advisor), for the original inspiration.
  54. ;; Ken Manheimer, for dreaming up the Protean mode.
  55. ;; Richard Stallman, for the awful cat-and-mouse pun, among other things.
  56. ;; Mike Williams, Denis Howe, Bill Benedetto, Chris Moore, Don Morris,
  57. ;; Simon Marshall, and M.S. Ashton, for their feedback.
  58. ;;; Code:
  59. (provide 'avoid)
  60. (defgroup avoid nil
  61. "Make mouse pointer stay out of the way of editing."
  62. :prefix "mouse-avoidance-"
  63. :group 'mouse)
  64. ;;;###autoload
  65. (defcustom mouse-avoidance-mode nil
  66. "Activate Mouse Avoidance mode.
  67. See function `mouse-avoidance-mode' for possible values.
  68. Setting this variable directly does not take effect;
  69. use either \\[customize] or the function `mouse-avoidance-mode'."
  70. :set (lambda (symbol value)
  71. ;; 'none below prevents toggling when value is nil.
  72. (mouse-avoidance-mode (or value 'none)))
  73. :initialize 'custom-initialize-default
  74. :type '(choice (const :tag "none" nil) (const banish) (const jump)
  75. (const animate) (const exile) (const proteus))
  76. :group 'avoid
  77. :require 'avoid
  78. :version "20.3")
  79. (defcustom mouse-avoidance-nudge-dist 15
  80. "Average distance that mouse will be moved when approached by cursor.
  81. Only applies in Mouse Avoidance mode `jump' and its derivatives.
  82. For best results make this larger than `mouse-avoidance-threshold'."
  83. :type 'integer
  84. :group 'avoid)
  85. (defcustom mouse-avoidance-nudge-var 10
  86. "Variability of `mouse-avoidance-nudge-dist' (which see)."
  87. :type 'integer
  88. :group 'avoid)
  89. (defcustom mouse-avoidance-animation-delay .01
  90. "Delay between animation steps, in seconds."
  91. :type 'number
  92. :group 'avoid)
  93. (defcustom mouse-avoidance-threshold 5
  94. "Mouse-pointer's flight distance.
  95. If the cursor gets closer than this, the mouse pointer will move away.
  96. Only applies in Mouse Avoidance modes `animate' and `jump'."
  97. :type 'integer
  98. :group 'avoid)
  99. ;; Internal variables
  100. (defvar mouse-avoidance-state nil)
  101. (defvar mouse-avoidance-pointer-shapes nil)
  102. (defvar mouse-avoidance-n-pointer-shapes 0)
  103. (defvar mouse-avoidance-old-pointer-shape nil)
  104. (defvar mouse-avoidance-animating-pointer nil)
  105. ;; This timer is used to run something when Emacs is idle.
  106. (defvar mouse-avoidance-timer nil)
  107. ;;; Functions:
  108. (defsubst mouse-avoidance-set-pointer-shape (shape)
  109. "Set the shape of the mouse pointer to SHAPE."
  110. (when (boundp 'x-pointer-shape)
  111. (setq x-pointer-shape shape)
  112. (set-mouse-color nil)))
  113. (defun mouse-avoidance-point-position ()
  114. "Return the position of point as (FRAME X . Y).
  115. Analogous to `mouse-position'."
  116. (let ((edges (window-inside-edges))
  117. (x-y (posn-x-y (posn-at-point))))
  118. (cons (selected-frame)
  119. (cons (+ (car edges)
  120. (/ (car x-y) (frame-char-width)))
  121. (+ (car (cdr edges))
  122. (/ (cdr x-y) (frame-char-height)))))))
  123. ;(defun mouse-avoidance-point-position-test ()
  124. ; (interactive)
  125. ; (message (format "point=%s mouse=%s"
  126. ; (cdr (mouse-avoidance-point-position))
  127. ; (cdr (mouse-position)))))
  128. (defun mouse-avoidance-set-mouse-position (pos)
  129. ;; Carefully set mouse position to given position (X . Y)
  130. ;; Ideally, should check if X,Y is in the current frame, and if not,
  131. ;; leave the mouse where it was. However, this is currently
  132. ;; difficult to do, so we just raise the frame to avoid frame switches.
  133. ;; Returns t if it moved the mouse.
  134. (let ((f (selected-frame)))
  135. (raise-frame f)
  136. (set-mouse-position f (car pos) (cdr pos))
  137. t))
  138. (defun mouse-avoidance-too-close-p (mouse)
  139. "Return t if mouse pointer and point cursor are too close.
  140. MOUSE is the current mouse position as returned by `mouse-position'.
  141. Acceptable distance is defined by `mouse-avoidance-threshold'."
  142. (let* ((frame (car mouse))
  143. (mouse-y (cdr (cdr mouse)))
  144. (tool-bar-lines (frame-parameter nil 'tool-bar-lines)))
  145. (or tool-bar-lines
  146. (setq tool-bar-lines 0))
  147. (if (and mouse-y (< mouse-y tool-bar-lines))
  148. nil
  149. (let ((point (mouse-avoidance-point-position))
  150. (mouse-x (car (cdr mouse))))
  151. (and (eq frame (car point))
  152. (not (null mouse-x))
  153. (< (abs (- mouse-x (car (cdr point))))
  154. mouse-avoidance-threshold)
  155. (< (abs (- mouse-y (cdr (cdr point))))
  156. mouse-avoidance-threshold))))))
  157. (defun mouse-avoidance-banish-destination ()
  158. "The position to which Mouse Avoidance mode `banish' moves the mouse.
  159. You can redefine this if you want the mouse banished to a different corner."
  160. (let* ((pos (window-edges)))
  161. (cons (- (nth 2 pos) 2)
  162. (nth 1 pos))))
  163. (defun mouse-avoidance-banish-mouse ()
  164. ;; Put the mouse pointer in the upper-right corner of the current frame.
  165. (mouse-avoidance-set-mouse-position (mouse-avoidance-banish-destination)))
  166. (defsubst mouse-avoidance-delta (cur delta dist var min max)
  167. ;; Decide how far to move in either dimension.
  168. ;; Args are the CURRENT location, the desired DELTA for
  169. ;; warp-conservation, the DISTANCE we like to move, the VARIABILITY
  170. ;; in distance allowed, and the MIN and MAX possible window positions.
  171. ;; Returns something as close to DELTA as possible within the constraints.
  172. (let ((L1 (max (- min cur) (+ (- dist) (- var))))
  173. (R1 (+ (- dist) var ))
  174. (L2 (+ dist (- var)))
  175. (R2 (min (- max cur) (+ dist var))))
  176. (if (< R1 (- min cur)) (setq L1 nil R1 nil))
  177. (if (> L2 (- max cur)) (setq L2 nil R2 nil))
  178. (cond ((and L1 (< delta L1)) L1)
  179. ((and R1 (< delta R1)) delta)
  180. ((and R1 (< delta 0)) R1)
  181. ((and L2 (< delta L2)) L2)
  182. ((and R2 (< delta R2)) delta)
  183. (R2)
  184. ((or R1 L2))
  185. (t 0))))
  186. (defun mouse-avoidance-nudge-mouse ()
  187. ;; Push the mouse a little way away, possibly animating the move.
  188. ;; For these modes, state keeps track of the total offset that we've
  189. ;; accumulated, and tries to keep it close to zero.
  190. (let* ((cur (mouse-position))
  191. (cur-frame (car cur))
  192. (cur-pos (cdr cur))
  193. (pos (window-edges))
  194. (wleft (pop pos))
  195. (wtop (pop pos))
  196. (wright (pop pos))
  197. (wbot (pop pos))
  198. (deltax (mouse-avoidance-delta
  199. (car cur-pos) (- (random mouse-avoidance-nudge-var)
  200. (car mouse-avoidance-state))
  201. mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
  202. wleft (1- wright)))
  203. (deltay (mouse-avoidance-delta
  204. (cdr cur-pos) (- (random mouse-avoidance-nudge-var)
  205. (cdr mouse-avoidance-state))
  206. mouse-avoidance-nudge-dist mouse-avoidance-nudge-var
  207. wtop (1- wbot))))
  208. (setq mouse-avoidance-state
  209. (cons (+ (car mouse-avoidance-state) deltax)
  210. (+ (cdr mouse-avoidance-state) deltay)))
  211. (if (or (eq mouse-avoidance-mode 'animate)
  212. (eq mouse-avoidance-mode 'proteus))
  213. (let ((i 0.0)
  214. (incr (max .1 (/ 1.0 mouse-avoidance-nudge-dist))))
  215. (setq mouse-avoidance-animating-pointer t)
  216. (while (<= i 1)
  217. (mouse-avoidance-set-mouse-position
  218. (cons (+ (car cur-pos) (round (* i deltax)))
  219. (+ (cdr cur-pos) (round (* i deltay)))))
  220. (setq i (+ i incr))
  221. (if (eq mouse-avoidance-mode 'proteus)
  222. (mouse-avoidance-set-pointer-shape
  223. (mouse-avoidance-random-shape)))
  224. (sit-for mouse-avoidance-animation-delay))
  225. (setq mouse-avoidance-animating-pointer nil))
  226. (mouse-avoidance-set-mouse-position (cons (+ (car (cdr cur)) deltax)
  227. (+ (cdr (cdr cur)) deltay))))))
  228. (defun mouse-avoidance-random-shape ()
  229. "Return a random cursor shape.
  230. This assumes that any variable whose name begins with x-pointer- and
  231. has an integer value is a valid cursor shape. You might want to
  232. redefine this function to suit your own tastes."
  233. (if (null mouse-avoidance-pointer-shapes)
  234. (progn
  235. (setq mouse-avoidance-pointer-shapes
  236. (mapcar (lambda (x) (symbol-value (intern x)))
  237. (all-completions "x-pointer-" obarray
  238. (lambda (x)
  239. (and (boundp x)
  240. (integerp (symbol-value x)))))))
  241. (setq mouse-avoidance-n-pointer-shapes
  242. (length mouse-avoidance-pointer-shapes))))
  243. (nth (random mouse-avoidance-n-pointer-shapes)
  244. mouse-avoidance-pointer-shapes))
  245. (defun mouse-avoidance-ignore-p ()
  246. (let ((mp (mouse-position)))
  247. (or (not (frame-pointer-visible-p)) ; The pointer is hidden
  248. (not cursor-type) ; There's no cursor
  249. executing-kbd-macro ; don't check inside macro
  250. (null (cadr mp)) ; don't move unless in an Emacs frame
  251. (not (eq (car mp) (selected-frame)))
  252. ;; Don't do anything if last event was a mouse event.
  253. ;; FIXME: this code fails in the case where the mouse was moved
  254. ;; since the last key-press but without generating any event.
  255. (and (consp last-input-event)
  256. (symbolp (car last-input-event))
  257. (let ((modifiers (event-modifiers (car last-input-event))))
  258. (or (memq (car last-input-event)
  259. '(mouse-movement scroll-bar-movement
  260. select-window switch-frame))
  261. (memq 'click modifiers)
  262. (memq 'double modifiers)
  263. (memq 'triple modifiers)
  264. (memq 'drag modifiers)
  265. (memq 'down modifiers)))))))
  266. (defun mouse-avoidance-banish ()
  267. (if (not (mouse-avoidance-ignore-p))
  268. (mouse-avoidance-banish-mouse)))
  269. (defun mouse-avoidance-exile ()
  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 (not (mouse-avoidance-ignore-p))
  273. (let ((mp (mouse-position)))
  274. (cond ((and (not mouse-avoidance-state)
  275. (mouse-avoidance-too-close-p mp))
  276. (setq mouse-avoidance-state mp)
  277. (mouse-avoidance-banish-mouse))
  278. ((and mouse-avoidance-state
  279. (not (mouse-avoidance-too-close-p mouse-avoidance-state)))
  280. (if (and (eq (car mp) (selected-frame))
  281. (equal (cdr mp) (mouse-avoidance-banish-destination)))
  282. (mouse-avoidance-set-mouse-position
  283. ;; move back only if user has not moved mouse
  284. (cdr mouse-avoidance-state)))
  285. ;; but clear state anyway, to be ready for another move
  286. (setq mouse-avoidance-state nil))))))
  287. (defun mouse-avoidance-fancy ()
  288. ;; Used for the "fancy" modes, ie jump et al.
  289. (if (and (not mouse-avoidance-animating-pointer)
  290. (not (mouse-avoidance-ignore-p))
  291. (mouse-avoidance-too-close-p (mouse-position)))
  292. (let ((old-pos (mouse-position)))
  293. (mouse-avoidance-nudge-mouse)
  294. (if (not (eq (selected-frame) (car old-pos)))
  295. ;; This should never happen.
  296. (apply 'set-mouse-position old-pos)))))
  297. ;;;###autoload
  298. (defun mouse-avoidance-mode (&optional mode)
  299. "Set Mouse Avoidance mode to MODE.
  300. MODE should be one of the symbols `banish', `exile', `jump', `animate',
  301. `cat-and-mouse', `proteus', or `none'.
  302. If MODE is nil, toggle mouse avoidance between `none' and `banish'
  303. modes. Positive numbers and symbols other than the above are treated
  304. as equivalent to `banish'; negative numbers and `-' are equivalent to `none'.
  305. Effects of the different modes:
  306. * banish: Move the mouse to the upper-right corner on any keypress.
  307. * exile: Move the mouse to the corner only if the cursor gets too close,
  308. and allow it to return once the cursor is out of the way.
  309. * jump: If the cursor gets too close to the mouse, displace the mouse
  310. a random distance & direction.
  311. * animate: As `jump', but shows steps along the way for illusion of motion.
  312. * cat-and-mouse: Same as `animate'.
  313. * proteus: As `animate', but changes the shape of the mouse pointer too.
  314. Whenever the mouse is moved, the frame is also raised.
  315. \(See `mouse-avoidance-threshold' for definition of \"too close\",
  316. and `mouse-avoidance-nudge-dist' and `mouse-avoidance-nudge-var' for
  317. definition of \"random distance\".)"
  318. (interactive
  319. (list (intern (completing-read
  320. "Select cursor avoidance technique (SPACE for list): "
  321. '(("banish") ("exile") ("jump") ("animate")
  322. ("cat-and-mouse") ("proteus") ("none"))
  323. nil t))))
  324. (if (eq mode 'cat-and-mouse)
  325. (setq mode 'animate))
  326. (if mouse-avoidance-timer
  327. (cancel-timer mouse-avoidance-timer))
  328. (setq mouse-avoidance-timer nil)
  329. ;; Restore pointer shape if necessary
  330. (if (eq mouse-avoidance-mode 'proteus)
  331. (mouse-avoidance-set-pointer-shape mouse-avoidance-old-pointer-shape))
  332. ;; Do additional setup depending on version of mode requested
  333. (cond ((eq mode 'none)
  334. (setq mouse-avoidance-mode nil))
  335. ((or (eq mode 'jump)
  336. (eq mode 'animate)
  337. (eq mode 'proteus))
  338. (setq mouse-avoidance-timer
  339. (run-with-idle-timer 0.1 t 'mouse-avoidance-fancy))
  340. (setq mouse-avoidance-mode mode
  341. mouse-avoidance-state (cons 0 0)
  342. mouse-avoidance-old-pointer-shape
  343. (and (boundp 'x-pointer-shape) x-pointer-shape)))
  344. ((eq mode 'exile)
  345. (setq mouse-avoidance-timer
  346. (run-with-idle-timer 0.1 t 'mouse-avoidance-exile))
  347. (setq mouse-avoidance-mode mode
  348. mouse-avoidance-state nil))
  349. ((or (eq mode 'banish)
  350. (eq mode t)
  351. (and (null mode) (null mouse-avoidance-mode))
  352. (and mode (> (prefix-numeric-value mode) 0)))
  353. (setq mouse-avoidance-timer
  354. (run-with-idle-timer 0.1 t 'mouse-avoidance-banish))
  355. (setq mouse-avoidance-mode 'banish))
  356. (t (setq mouse-avoidance-mode nil)))
  357. (force-mode-line-update))
  358. ;; Most people who use avoid mode leave it on all the time, so it's not
  359. ;; very informative to announce it in the mode line.
  360. ;;(or (assq 'mouse-avoidance-mode minor-mode-alist)
  361. ;; (setq minor-mode-alist (cons '(mouse-avoidance-mode " Avoid")
  362. ;; minor-mode-alist)))
  363. ;; Needed for custom.
  364. (if mouse-avoidance-mode
  365. (mouse-avoidance-mode mouse-avoidance-mode))
  366. ;;; avoid.el ends here