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