mwheel.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. ;;; mwheel.el --- Wheel mouse support
  2. ;; Copyright (C) 1998, 2000-2017 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 be bound only to mouse buttons 4, 5, 6, and 7 on
  161. non-Windows systems."
  162. (interactive (list last-input-event))
  163. (let* ((selected-window (selected-window))
  164. (scroll-window
  165. (or (catch 'found
  166. (let* ((window (if mouse-wheel-follow-mouse
  167. (mwheel-event-window event)
  168. (selected-window)))
  169. (frame (when (window-live-p window)
  170. (frame-parameter
  171. (window-frame window) 'mouse-wheel-frame))))
  172. (when (frame-live-p frame)
  173. (let* ((pos (mouse-absolute-pixel-position))
  174. (pos-x (car pos))
  175. (pos-y (cdr pos)))
  176. (walk-window-tree
  177. (lambda (window-1)
  178. (let ((edges (window-edges window-1 nil t t)))
  179. (when (and (<= (nth 0 edges) pos-x)
  180. (<= pos-x (nth 2 edges))
  181. (<= (nth 1 edges) pos-y)
  182. (<= pos-y (nth 3 edges)))
  183. (throw 'found window-1))))
  184. frame nil t)))))
  185. (mwheel-event-window event)))
  186. (old-point
  187. (and (eq scroll-window selected-window)
  188. (eq (car-safe transient-mark-mode) 'only)
  189. (window-point)))
  190. (mods
  191. (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
  192. (amt (assoc mods mouse-wheel-scroll-amount)))
  193. (unless (eq scroll-window selected-window)
  194. ;; Mark window to be scrolled for redisplay.
  195. (select-window scroll-window 'mark-for-redisplay))
  196. ;; Extract the actual amount or find the element that has no modifiers.
  197. (if amt (setq amt (cdr amt))
  198. (let ((list-elt mouse-wheel-scroll-amount))
  199. (while (consp (setq amt (pop list-elt))))))
  200. (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
  201. (when (and mouse-wheel-progressive-speed (numberp amt))
  202. ;; When the double-mouse-N comes in, a mouse-N has been executed already,
  203. ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
  204. (setq amt (* amt (event-click-count event))))
  205. (unwind-protect
  206. (let ((button (mwheel-event-button event)))
  207. (cond ((eq button mouse-wheel-down-event)
  208. (condition-case nil (funcall mwheel-scroll-down-function amt)
  209. ;; Make sure we do indeed scroll to the beginning of
  210. ;; the buffer.
  211. (beginning-of-buffer
  212. (unwind-protect
  213. (funcall mwheel-scroll-down-function)
  214. ;; If the first scroll succeeded, then some scrolling
  215. ;; is possible: keep scrolling til the beginning but
  216. ;; do not signal an error. For some reason, we have
  217. ;; to do it even if the first scroll signaled an
  218. ;; error, because otherwise the window is recentered
  219. ;; for a reason that escapes me. This problem seems
  220. ;; to only affect scroll-down. --Stef
  221. (set-window-start (selected-window) (point-min))))))
  222. ((eq button mouse-wheel-up-event)
  223. (condition-case nil (funcall mwheel-scroll-up-function amt)
  224. ;; Make sure we do indeed scroll to the end of the buffer.
  225. (end-of-buffer (while t (funcall mwheel-scroll-up-function)))))
  226. ((eq button mouse-wheel-left-event) ; for tilt scroll
  227. (when mwheel-tilt-scroll-p
  228. (funcall (if mwheel-flip-direction
  229. mwheel-scroll-right-function
  230. mwheel-scroll-left-function) amt)))
  231. ((eq button mouse-wheel-right-event) ; for tilt scroll
  232. (when mwheel-tilt-scroll-p
  233. (funcall (if mwheel-flip-direction
  234. mwheel-scroll-left-function
  235. mwheel-scroll-right-function) amt)))
  236. (t (error "Bad binding in mwheel-scroll"))))
  237. (if (eq scroll-window selected-window)
  238. ;; If there is a temporarily active region, deactivate it if
  239. ;; scrolling moved point.
  240. (when (and old-point (/= old-point (window-point)))
  241. ;; Call `deactivate-mark' at the original position, so that
  242. ;; the original region is saved to the X selection.
  243. (let ((new-point (window-point)))
  244. (goto-char old-point)
  245. (deactivate-mark)
  246. (goto-char new-point)))
  247. (select-window selected-window t))))
  248. (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
  249. (if mwheel-inhibit-click-event-timer
  250. (cancel-timer mwheel-inhibit-click-event-timer)
  251. (add-hook 'pre-command-hook 'mwheel-filter-click-events))
  252. (setq mwheel-inhibit-click-event-timer
  253. (run-with-timer mouse-wheel-inhibit-click-time nil
  254. 'mwheel-inhibit-click-timeout))))
  255. (put 'mwheel-scroll 'scroll-command t)
  256. (defvar mwheel-installed-bindings nil)
  257. (define-minor-mode mouse-wheel-mode
  258. "Toggle mouse wheel support (Mouse Wheel mode).
  259. With a prefix argument ARG, enable Mouse Wheel mode if ARG is
  260. positive, and disable it otherwise. If called from Lisp, enable
  261. the mode if ARG is omitted or nil."
  262. :init-value t
  263. ;; We'd like to use custom-initialize-set here so the setup is done
  264. ;; before dumping, but at the point where the defcustom is evaluated,
  265. ;; the corresponding function isn't defined yet, so
  266. ;; custom-initialize-set signals an error.
  267. :initialize 'custom-initialize-delay
  268. :global t
  269. :group 'mouse
  270. ;; Remove previous bindings, if any.
  271. (while mwheel-installed-bindings
  272. (let ((key (pop mwheel-installed-bindings)))
  273. (when (eq (lookup-key (current-global-map) key) 'mwheel-scroll)
  274. (global-unset-key key))))
  275. ;; Setup bindings as needed.
  276. (when mouse-wheel-mode
  277. (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event mouse-wheel-right-event mouse-wheel-left-event))
  278. (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,event)])
  279. mouse-wheel-scroll-amount))
  280. (global-set-key key 'mwheel-scroll)
  281. (push key mwheel-installed-bindings)))))
  282. ;;; Compatibility entry point
  283. ;; preloaded ;;;###autoload
  284. (defun mwheel-install (&optional uninstall)
  285. "Enable mouse wheel support."
  286. (mouse-wheel-mode (if uninstall -1 1)))
  287. ;;; For tilt-scroll
  288. ;;;
  289. (defcustom mwheel-tilt-scroll-p nil
  290. "Enable scroll using tilting mouse wheel."
  291. :group 'mouse
  292. :type 'boolean
  293. :version "26.1")
  294. (defcustom mwheel-flip-direction nil
  295. "Swap direction of 'wheel-right and 'wheel-left."
  296. :group 'mouse
  297. :type 'boolean
  298. :version "26.1")
  299. (defcustom mwheel-scroll-left-function 'scroll-left
  300. "Function that does the job of scrolling left."
  301. :group 'mouse
  302. :type 'function
  303. :version "26.1")
  304. (defcustom mwheel-scroll-right-function 'scroll-right
  305. "Function that does the job of scrolling right."
  306. :group 'mouse
  307. :type 'function
  308. :version "26.1")
  309. (defvar mouse-wheel-left-event
  310. (if (or (featurep 'w32-win) (featurep 'ns-win))
  311. 'wheel-left
  312. (intern "mouse-6"))
  313. "Event used for scrolling left.")
  314. (defvar mouse-wheel-right-event
  315. (if (or (featurep 'w32-win) (featurep 'ns-win))
  316. 'wheel-right
  317. (intern "mouse-7"))
  318. "Event used for scrolling right.")
  319. (provide 'mwheel)
  320. ;;; mwheel.el ends here