1 ;;; mwheel.el --- Wheel mouse support
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2002, 2004,
4 ;; 2005, 2006, 2007, 2008 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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This code will enable the use of the infamous 'wheel' on the new
28 ;; crop of mice. Under XFree86 and the XSuSE X Servers, the wheel
29 ;; events are sent as button4/button5 events.
31 ;; I for one would prefer some way of converting the button4/button5
32 ;; events into different event types, like 'mwheel-up' or
33 ;; 'mwheel-down', but I cannot find a way to do this very easily (or
34 ;; portably), so for now I just live with it.
36 ;; To enable this code, simply put this at the top of your .emacs
39 ;; (mouse-wheel-mode 1)
46 ;; Setter function for mouse-button user-options. Switch Mouse Wheel
47 ;; mode off and on again so that the old button is unbound and
48 ;; new button is bound to mwheel-scroll.
50 (defun mouse-wheel-change-button (var button
)
51 (let ((active mouse-wheel-mode
))
52 ;; Deactivate before changing the setting.
53 (when active
(mouse-wheel-mode -
1))
54 (set-default var button
)
55 (when active
(mouse-wheel-mode 1))))
57 (defvar mouse-wheel-down-button
4)
58 (make-obsolete-variable 'mouse-wheel-down-button
59 'mouse-wheel-down-event
61 (defcustom mouse-wheel-down-event
62 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
63 (if (memq window-system
'(w32 mac
))
65 (intern (format (if (featurep 'xemacs
) "button%s" "mouse-%s")
66 mouse-wheel-down-button
)))
67 "Event used for scrolling down."
70 :set
'mouse-wheel-change-button
)
72 (defvar mouse-wheel-up-button
5)
73 (make-obsolete-variable 'mouse-wheel-up-button
76 (defcustom mouse-wheel-up-event
77 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
78 (if (memq window-system
'(w32 mac
))
80 (intern (format (if (featurep 'xemacs
) "button%s" "mouse-%s")
81 mouse-wheel-up-button
)))
82 "Event used for scrolling up."
85 :set
'mouse-wheel-change-button
)
87 (defvar mouse-wheel-click-button
2)
88 (make-obsolete-variable 'mouse-wheel-click-button
89 'mouse-wheel-click-event
91 (defcustom mouse-wheel-click-event
92 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
93 (intern (format (if (featurep 'xemacs
) "button%s" "mouse-%s")
94 mouse-wheel-click-button
))
95 "Event that should be temporarily inhibited after mouse scrolling.
96 The mouse wheel is typically on the mouse-2 button, so it may easily
97 happen that text is accidentally yanked into the buffer when
98 scrolling with the mouse wheel. To prevent that, this variable can be
99 set to the event sent when clicking on the mouse wheel button."
102 :set
'mouse-wheel-change-button
)
104 (defcustom mouse-wheel-inhibit-click-time
0.35
105 "Time in seconds to inhibit clicking on mouse wheel button after scroll."
109 (defcustom mouse-wheel-scroll-amount
'(5 ((shift) .
1) ((control) . nil
))
110 "Amount to scroll windows by when spinning the mouse wheel.
111 This is an alist mapping the modifier key to the amount to scroll when
112 the wheel is moved with the modifier key depressed.
113 Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
116 AMOUNT should be the number of lines to scroll, or nil for near full
117 screen. It can also be a floating point number, specifying the fraction of
118 a full screen to scroll. A near full screen is `next-screen-context-lines'
119 less than a full screen."
122 (choice :tag
"Normal"
123 (const :tag
"Full screen" :value nil
)
124 (integer :tag
"Specific # of lines")
125 (float :tag
"Fraction of window")
127 (repeat (choice :tag
"modifier"
128 (const alt
) (const control
) (const hyper
)
129 (const meta
) (const shift
) (const super
)))
130 (choice :tag
"scroll amount"
131 (const :tag
"Full screen" :value nil
)
132 (integer :tag
"Specific # of lines")
133 (float :tag
"Fraction of window"))))
136 (repeat (choice :tag
"modifier"
137 (const alt
) (const control
) (const hyper
)
138 (const meta
) (const shift
) (const super
)))
139 (choice :tag
"scroll amount"
140 (const :tag
"Full screen" :value nil
)
141 (integer :tag
"Specific # of lines")
142 (float :tag
"Fraction of window"))))))
144 (defcustom mouse-wheel-progressive-speed t
145 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
146 Note that this has no effect when `mouse-wheel-scroll-amount' specifies
147 a \"near full screen\" scroll or when the mouse wheel sends key instead
152 (defcustom mouse-wheel-follow-mouse t
153 "Whether the mouse wheel should scroll the window that the mouse is over.
154 This can be slightly disconcerting, but some people prefer it."
158 (if (not (fboundp 'event-button
))
159 (defun mwheel-event-button (event)
160 (let ((x (event-basic-type event
)))
161 ;; Map mouse-wheel events to appropriate buttons
162 (if (eq 'mouse-wheel x
)
163 (let ((amount (car (cdr (cdr (cdr event
))))))
166 mouse-wheel-down-event
))
168 (fset 'mwheel-event-button
'event-button
))
170 (if (not (fboundp 'event-window
))
171 (defun mwheel-event-window (event)
172 (posn-window (event-start event
)))
173 (fset 'mwheel-event-window
'event-window
))
175 (defvar mwheel-inhibit-click-event-timer nil
176 "Timer running while mouse wheel click event is inhibited.")
178 (defun mwheel-inhibit-click-timeout ()
179 "Handler for `mwheel-inhibit-click-event-timer'."
180 (setq mwheel-inhibit-click-event-timer nil
)
181 (remove-hook 'pre-command-hook
'mwheel-filter-click-events
))
183 (defun mwheel-filter-click-events ()
184 "Discard `mouse-wheel-click-event' while scrolling the mouse."
185 (if (eq (event-basic-type last-input-event
) mouse-wheel-click-event
)
186 (setq this-command
'ignore
)))
188 (defun mwheel-scroll (event)
189 "Scroll up or down according to the EVENT.
190 This should only be bound to mouse buttons 4 and 5."
191 (interactive (list last-input-event
))
192 (let* ((curwin (if mouse-wheel-follow-mouse
195 (select-window (mwheel-event-window event
)))))
196 (buffer (window-buffer curwin
))
197 (opoint (with-current-buffer buffer
198 (when (eq (car-safe transient-mark-mode
) 'only
)
201 (delq 'click
(delq 'double
(delq 'triple
(event-modifiers event
)))))
202 (amt (assoc mods mouse-wheel-scroll-amount
)))
203 ;; Extract the actual amount or find the element that has no modifiers.
204 (if amt
(setq amt
(cdr amt
))
205 (let ((list-elt mouse-wheel-scroll-amount
))
206 (while (consp (setq amt
(pop list-elt
))))))
207 (if (floatp amt
) (setq amt
(1+ (truncate (* amt
(window-height))))))
208 (when (and mouse-wheel-progressive-speed
(numberp amt
))
209 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
210 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
211 (setq amt
(* amt
(event-click-count event
))))
213 (let ((button (mwheel-event-button event
)))
214 (cond ((eq button mouse-wheel-down-event
)
215 (condition-case nil
(scroll-down amt
)
216 ;; Make sure we do indeed scroll to the beginning of
221 ;; If the first scroll succeeded, then some scrolling
222 ;; is possible: keep scrolling til the beginning but
223 ;; do not signal an error. For some reason, we have
224 ;; to do it even if the first scroll signalled an
225 ;; error, because otherwise the window is recentered
226 ;; for a reason that escapes me. This problem seems
227 ;; to only affect scroll-down. --Stef
228 (set-window-start (selected-window) (point-min))))))
229 ((eq button mouse-wheel-up-event
)
230 (condition-case nil
(scroll-up amt
)
231 ;; Make sure we do indeed scroll to the end of the buffer.
232 (end-of-buffer (while t
(scroll-up)))))
233 (t (error "Bad binding in mwheel-scroll"))))
234 (if curwin
(select-window curwin
)))
235 ;; If there is a temporarily active region, deactivate it iff
236 ;; scrolling moves point.
238 (with-current-buffer buffer
239 (when (/= opoint
(point))
240 (deactivate-mark)))))
241 (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time
)
242 (if mwheel-inhibit-click-event-timer
243 (cancel-timer mwheel-inhibit-click-event-timer
)
244 (add-hook 'pre-command-hook
'mwheel-filter-click-events
))
245 (setq mwheel-inhibit-click-event-timer
246 (run-with-timer mouse-wheel-inhibit-click-time nil
247 'mwheel-inhibit-click-timeout
))))
250 (define-minor-mode mouse-wheel-mode
251 "Toggle mouse wheel support.
252 With prefix argument ARG, turn on if positive, otherwise off.
253 Return non-nil if the new state is enabled."
256 (let* ((dn mouse-wheel-down-event
)
257 (up mouse-wheel-up-event
)
259 (nconc (mapcar (lambda (amt) `[(,@(if (consp amt
) (car amt
)) ,up
)])
260 mouse-wheel-scroll-amount
)
261 (mapcar (lambda (amt) `[(,@(if (consp amt
) (car amt
)) ,dn
)])
262 mouse-wheel-scroll-amount
))))
263 ;; This condition-case is here because Emacs 19 will throw an error
264 ;; if you try to define a key that it does not know about. I for one
265 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
266 ;; that if the wheeled-mouse is there, it just works, and this way it
267 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
270 (cond (mouse-wheel-mode
271 (global-set-key key
'mwheel-scroll
))
272 ((eq (lookup-key (current-global-map) key
) 'mwheel-scroll
)
273 (global-unset-key key
))))
276 ;;; Compatibility entry point
278 (defun mwheel-install (&optional uninstall
)
279 "Enable mouse wheel support."
280 (mouse-wheel-mode (if uninstall -
1 1)))
284 ;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f
285 ;;; mwheel.el ends here