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