mwheel.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; mwheel.el --- Wheel mouse support
  2. ;; Copyright (C) 1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: William M. Perry <wmperry@gnu.org>
  4. ;; Keywords: mouse
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This code will enable the use of the infamous 'wheel' on the new
  19. ;; crop of mice. Under XFree86 and the XSuSE X Servers, the wheel
  20. ;; events are sent as button4/button5 events.
  21. ;; I for one would prefer some way of converting the button4/button5
  22. ;; events into different event types, like 'mwheel-up' or
  23. ;; 'mwheel-down', but I cannot find a way to do this very easily (or
  24. ;; portably), so for now I just live with it.
  25. ;; To enable this code, simply put this at the top of your .emacs
  26. ;; file:
  27. ;;
  28. ;; (mouse-wheel-mode 1)
  29. ;;; Code:
  30. (require 'custom)
  31. (require 'timer)
  32. (defvar mouse-wheel-mode)
  33. ;; Setter function for mouse-button user-options. Switch Mouse Wheel
  34. ;; mode off and on again so that the old button is unbound and
  35. ;; new button is bound to mwheel-scroll.
  36. (defun mouse-wheel-change-button (var button)
  37. (set-default var button)
  38. ;; Sync the bindings.
  39. (when (bound-and-true-p mouse-wheel-mode) (mouse-wheel-mode 1)))
  40. (defvar mouse-wheel-down-button 4)
  41. (make-obsolete-variable 'mouse-wheel-down-button
  42. 'mouse-wheel-down-event
  43. "22.1")
  44. (defcustom mouse-wheel-down-event
  45. (if (or (featurep 'w32-win) (featurep 'ns-win))
  46. 'wheel-up
  47. (intern (format "mouse-%s" mouse-wheel-down-button)))
  48. "Event used for scrolling down."
  49. :group 'mouse
  50. :type 'symbol
  51. :set 'mouse-wheel-change-button)
  52. (defvar mouse-wheel-up-button 5)
  53. (make-obsolete-variable 'mouse-wheel-up-button
  54. 'mouse-wheel-up-event
  55. "22.1")
  56. (defcustom mouse-wheel-up-event
  57. (if (or (featurep 'w32-win) (featurep 'ns-win))
  58. 'wheel-down
  59. (intern (format "mouse-%s" mouse-wheel-up-button)))
  60. "Event used for scrolling up."
  61. :group 'mouse
  62. :type 'symbol
  63. :set 'mouse-wheel-change-button)
  64. (defvar mouse-wheel-click-button 2)
  65. (make-obsolete-variable 'mouse-wheel-click-button
  66. 'mouse-wheel-click-event
  67. "22.1")
  68. (defcustom mouse-wheel-click-event
  69. (intern (format "mouse-%s" mouse-wheel-click-button))
  70. "Event that should be temporarily inhibited after mouse scrolling.
  71. The mouse wheel is typically on the mouse-2 button, so it may easily
  72. happen that text is accidentally yanked into the buffer when
  73. scrolling with the mouse wheel. To prevent that, this variable can be
  74. set to the event sent when clicking on the mouse wheel button."
  75. :group 'mouse
  76. :type 'symbol
  77. :set 'mouse-wheel-change-button)
  78. (defcustom mouse-wheel-inhibit-click-time 0.35
  79. "Time in seconds to inhibit clicking on mouse wheel button after scroll."
  80. :group 'mouse
  81. :type 'number)
  82. (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
  83. "Amount to scroll windows by when spinning the mouse wheel.
  84. This is an alist mapping the modifier key to the amount to scroll when
  85. the wheel is moved with the modifier key depressed.
  86. Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
  87. MODIFIERS is nil.
  88. AMOUNT should be the number of lines to scroll, or nil for near full
  89. screen. It can also be a floating point number, specifying the fraction of
  90. a full screen to scroll. A near full screen is `next-screen-context-lines'
  91. less than a full screen."
  92. :group 'mouse
  93. :type '(cons
  94. (choice :tag "Normal"
  95. (const :tag "Full screen" :value nil)
  96. (integer :tag "Specific # of lines")
  97. (float :tag "Fraction of window")
  98. (cons
  99. (repeat (choice :tag "modifier"
  100. (const alt) (const control) (const hyper)
  101. (const meta) (const shift) (const super)))
  102. (choice :tag "scroll amount"
  103. (const :tag "Full screen" :value nil)
  104. (integer :tag "Specific # of lines")
  105. (float :tag "Fraction of window"))))
  106. (repeat
  107. (cons
  108. (repeat (choice :tag "modifier"
  109. (const alt) (const control) (const hyper)
  110. (const meta) (const shift) (const super)))
  111. (choice :tag "scroll amount"
  112. (const :tag "Full screen" :value nil)
  113. (integer :tag "Specific # of lines")
  114. (float :tag "Fraction of window")))))
  115. :set 'mouse-wheel-change-button)
  116. (defcustom mouse-wheel-progressive-speed t
  117. "If non-nil, the faster the user moves the wheel, the faster the scrolling.
  118. Note that this has no effect when `mouse-wheel-scroll-amount' specifies
  119. a \"near full screen\" scroll or when the mouse wheel sends key instead
  120. of button events."
  121. :group 'mouse
  122. :type 'boolean)
  123. (defcustom mouse-wheel-follow-mouse t
  124. "Whether the mouse wheel should scroll the window that the mouse is over.
  125. This can be slightly disconcerting, but some people prefer it."
  126. :group 'mouse
  127. :type 'boolean)
  128. (eval-and-compile
  129. (if (fboundp 'event-button)
  130. (fset 'mwheel-event-button 'event-button)
  131. (defun mwheel-event-button (event)
  132. (let ((x (event-basic-type event)))
  133. ;; Map mouse-wheel events to appropriate buttons
  134. (if (eq 'mouse-wheel x)
  135. (let ((amount (car (cdr (cdr (cdr event))))))
  136. (if (< amount 0)
  137. mouse-wheel-up-event
  138. mouse-wheel-down-event))
  139. x))))
  140. (if (fboundp 'event-window)
  141. (fset 'mwheel-event-window 'event-window)
  142. (defun mwheel-event-window (event)
  143. (posn-window (event-start event)))))
  144. (defvar mwheel-inhibit-click-event-timer nil
  145. "Timer running while mouse wheel click event is inhibited.")
  146. (defun mwheel-inhibit-click-timeout ()
  147. "Handler for `mwheel-inhibit-click-event-timer'."
  148. (setq mwheel-inhibit-click-event-timer nil)
  149. (remove-hook 'pre-command-hook 'mwheel-filter-click-events))
  150. (defun mwheel-filter-click-events ()
  151. "Discard `mouse-wheel-click-event' while scrolling the mouse."
  152. (if (eq (event-basic-type last-input-event) mouse-wheel-click-event)
  153. (setq this-command 'ignore)))
  154. (defvar mwheel-scroll-up-function 'scroll-up
  155. "Function that does the job of scrolling upward.")
  156. (defvar mwheel-scroll-down-function 'scroll-down
  157. "Function that does the job of scrolling downward.")
  158. (defun mwheel-scroll (event)
  159. "Scroll up or down according to the EVENT.
  160. This should only be bound to mouse buttons 4 and 5."
  161. (interactive (list last-input-event))
  162. (let* ((curwin (if mouse-wheel-follow-mouse
  163. (prog1
  164. (selected-window)
  165. (select-window (mwheel-event-window event)))))
  166. (buffer (window-buffer curwin))
  167. (opoint (with-current-buffer buffer
  168. (when (eq (car-safe transient-mark-mode) 'only)
  169. (point))))
  170. (mods
  171. (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
  172. (amt (assoc mods mouse-wheel-scroll-amount)))
  173. ;; Extract the actual amount or find the element that has no modifiers.
  174. (if amt (setq amt (cdr amt))
  175. (let ((list-elt mouse-wheel-scroll-amount))
  176. (while (consp (setq amt (pop list-elt))))))
  177. (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
  178. (when (and mouse-wheel-progressive-speed (numberp amt))
  179. ;; When the double-mouse-N comes in, a mouse-N has been executed already,
  180. ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
  181. (setq amt (* amt (event-click-count event))))
  182. (unwind-protect
  183. (let ((button (mwheel-event-button event)))
  184. (cond ((eq button mouse-wheel-down-event)
  185. (condition-case nil (funcall mwheel-scroll-down-function amt)
  186. ;; Make sure we do indeed scroll to the beginning of
  187. ;; the buffer.
  188. (beginning-of-buffer
  189. (unwind-protect
  190. (funcall mwheel-scroll-down-function)
  191. ;; If the first scroll succeeded, then some scrolling
  192. ;; is possible: keep scrolling til the beginning but
  193. ;; do not signal an error. For some reason, we have
  194. ;; to do it even if the first scroll signaled an
  195. ;; error, because otherwise the window is recentered
  196. ;; for a reason that escapes me. This problem seems
  197. ;; to only affect scroll-down. --Stef
  198. (set-window-start (selected-window) (point-min))))))
  199. ((eq button mouse-wheel-up-event)
  200. (condition-case nil (funcall mwheel-scroll-up-function amt)
  201. ;; Make sure we do indeed scroll to the end of the buffer.
  202. (end-of-buffer (while t (funcall mwheel-scroll-up-function)))))
  203. (t (error "Bad binding in mwheel-scroll"))))
  204. (if curwin (select-window curwin)))
  205. ;; If there is a temporarily active region, deactivate it if
  206. ;; scrolling moves point.
  207. (when opoint
  208. (with-current-buffer buffer
  209. (when (/= opoint (point))
  210. ;; Call `deactivate-mark' at the original position, so that
  211. ;; the original region is saved to the X selection.
  212. (let ((newpoint (point)))
  213. (goto-char opoint)
  214. (deactivate-mark)
  215. (goto-char newpoint))))))
  216. (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
  217. (if mwheel-inhibit-click-event-timer
  218. (cancel-timer mwheel-inhibit-click-event-timer)
  219. (add-hook 'pre-command-hook 'mwheel-filter-click-events))
  220. (setq mwheel-inhibit-click-event-timer
  221. (run-with-timer mouse-wheel-inhibit-click-time nil
  222. 'mwheel-inhibit-click-timeout))))
  223. (put 'mwheel-scroll 'scroll-command t)
  224. (defvar mwheel-installed-bindings nil)
  225. (define-minor-mode mouse-wheel-mode
  226. "Toggle mouse wheel support (Mouse Wheel mode).
  227. With a prefix argument ARG, enable Mouse Wheel mode if ARG is
  228. positive, and disable it otherwise. If called from Lisp, enable
  229. the mode if ARG is omitted or nil."
  230. :init-value t
  231. ;; We'd like to use custom-initialize-set here so the setup is done
  232. ;; before dumping, but at the point where the defcustom is evaluated,
  233. ;; the corresponding function isn't defined yet, so
  234. ;; custom-initialize-set signals an error.
  235. :initialize 'custom-initialize-delay
  236. :global t
  237. :group 'mouse
  238. ;; Remove previous bindings, if any.
  239. (while mwheel-installed-bindings
  240. (let ((key (pop mwheel-installed-bindings)))
  241. (when (eq (lookup-key (current-global-map) key) 'mwheel-scroll)
  242. (global-unset-key key))))
  243. ;; Setup bindings as needed.
  244. (when mouse-wheel-mode
  245. (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event))
  246. (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,event)])
  247. mouse-wheel-scroll-amount))
  248. (global-set-key key 'mwheel-scroll)
  249. (push key mwheel-installed-bindings)))))
  250. ;;; Compatibility entry point
  251. ;; preloaded ;;;###autoload
  252. (defun mwheel-install (&optional uninstall)
  253. "Enable mouse wheel support."
  254. (mouse-wheel-mode (if uninstall -1 1)))
  255. (provide 'mwheel)
  256. ;;; mwheel.el ends here