Nuke arch-tags.
[emacs.git] / lisp / mwheel.el
blob1be3b2ee534a93cda2f0bbd515eb5371c0332a4b
1 ;;; mwheel.el --- Wheel mouse support
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2002, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;; Maintainer: William M. Perry <wmperry@gnu.org>
6 ;; Keywords: mouse
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
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.
35 ;; To enable this code, simply put this at the top of your .emacs
36 ;; file:
38 ;; (mouse-wheel-mode 1)
40 ;;; Code:
42 (require 'custom)
43 (require 'timer)
45 (defvar mouse-wheel-mode)
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.
51 (defun mouse-wheel-change-button (var button)
52 (set-default var button)
53 ;; Sync the bindings.
54 (when (bound-and-true-p mouse-wheel-mode) (mouse-wheel-mode 1)))
56 (defvar mouse-wheel-down-button 4)
57 (make-obsolete-variable 'mouse-wheel-down-button
58 'mouse-wheel-down-event
59 "22.1")
60 (defcustom mouse-wheel-down-event
61 (if (or (featurep 'w32-win) (featurep 'ns-win))
62 'wheel-up
63 (intern (format "mouse-%s" mouse-wheel-down-button)))
64 "Event used for scrolling down."
65 :group 'mouse
66 :type 'symbol
67 :set 'mouse-wheel-change-button)
69 (defvar mouse-wheel-up-button 5)
70 (make-obsolete-variable 'mouse-wheel-up-button
71 'mouse-wheel-up-event
72 "22.1")
73 (defcustom mouse-wheel-up-event
74 (if (or (featurep 'w32-win) (featurep 'ns-win))
75 'wheel-down
76 (intern (format "mouse-%s" mouse-wheel-up-button)))
77 "Event used for scrolling up."
78 :group 'mouse
79 :type 'symbol
80 :set 'mouse-wheel-change-button)
82 (defvar mouse-wheel-click-button 2)
83 (make-obsolete-variable 'mouse-wheel-click-button
84 'mouse-wheel-click-event
85 "22.1")
86 (defcustom mouse-wheel-click-event
87 (intern (format "mouse-%s" mouse-wheel-click-button))
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
90 happen that text is accidentally yanked into the buffer when
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)
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
100 :type 'number)
102 (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
103 "Amount to scroll windows by when spinning the mouse wheel.
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.
109 AMOUNT should be the number of lines to scroll, or nil for near full
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."
113 :group 'mouse
114 :type '(cons
115 (choice :tag "Normal"
116 (const :tag "Full screen" :value nil)
117 (integer :tag "Specific # of lines")
118 (float :tag "Fraction of window")
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")
126 (float :tag "Fraction of window"))))
127 (repeat
128 (cons
129 (repeat (choice :tag "modifier"
130 (const alt) (const control) (const hyper)
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")
135 (float :tag "Fraction of window")))))
136 :set 'mouse-wheel-change-button)
138 (defcustom mouse-wheel-progressive-speed t
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
141 a \"near full screen\" scroll or when the mouse wheel sends key instead
142 of button events."
143 :group 'mouse
144 :type 'boolean)
146 (defcustom mouse-wheel-follow-mouse t
147 "Whether the mouse wheel should scroll the window that the mouse is over.
148 This can be slightly disconcerting, but some people prefer it."
149 :group 'mouse
150 :type 'boolean)
152 (eval-and-compile
153 (if (fboundp 'event-button)
154 (fset 'mwheel-event-button 'event-button)
155 (defun mwheel-event-button (event)
156 (let ((x (event-basic-type event)))
157 ;; Map mouse-wheel events to appropriate buttons
158 (if (eq 'mouse-wheel x)
159 (let ((amount (car (cdr (cdr (cdr event))))))
160 (if (< amount 0)
161 mouse-wheel-up-event
162 mouse-wheel-down-event))
163 x))))
165 (if (fboundp 'event-window)
166 (fset 'mwheel-event-window 'event-window)
167 (defun mwheel-event-window (event)
168 (posn-window (event-start event)))))
170 (defvar mwheel-inhibit-click-event-timer nil
171 "Timer running while mouse wheel click event is inhibited.")
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))
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)))
183 (defvar mwheel-scroll-up-function 'scroll-up
184 "Function that does the job of scrolling upward.")
186 (defvar mwheel-scroll-down-function 'scroll-down
187 "Function that does the job of scrolling downward.")
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."
192 (interactive (list last-input-event))
193 (let* ((curwin (if mouse-wheel-follow-mouse
194 (prog1
195 (selected-window)
196 (select-window (mwheel-event-window event)))))
197 (buffer (window-buffer curwin))
198 (opoint (with-current-buffer buffer
199 (when (eq (car-safe transient-mark-mode) 'only)
200 (point))))
201 (mods
202 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
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))))))
208 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
209 (when (and mouse-wheel-progressive-speed (numberp amt))
210 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
211 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
212 (setq amt (* amt (event-click-count event))))
213 (unwind-protect
214 (let ((button (mwheel-event-button event)))
215 (cond ((eq button mouse-wheel-down-event)
216 (condition-case nil (funcall mwheel-scroll-down-function amt)
217 ;; Make sure we do indeed scroll to the beginning of
218 ;; the buffer.
219 (beginning-of-buffer
220 (unwind-protect
221 (funcall mwheel-scroll-down-function)
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
225 ;; to do it even if the first scroll signaled an
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)
231 (condition-case nil (funcall mwheel-scroll-up-function amt)
232 ;; Make sure we do indeed scroll to the end of the buffer.
233 (end-of-buffer (while t (funcall mwheel-scroll-up-function)))))
234 (t (error "Bad binding in mwheel-scroll"))))
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)))))
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))
246 (setq mwheel-inhibit-click-event-timer
247 (run-with-timer mouse-wheel-inhibit-click-time nil
248 'mwheel-inhibit-click-timeout))))
250 (put 'mwheel-scroll 'scroll-command t)
252 (defvar mwheel-installed-bindings nil)
254 ;; preloaded ;;;###autoload
255 (define-minor-mode mouse-wheel-mode
256 "Toggle mouse wheel support.
257 With prefix argument ARG, turn on if positive, otherwise off.
258 Return non-nil if the new state is enabled."
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
265 :global t
266 :group 'mouse
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)))))
280 ;;; Compatibility entry point
281 ;; preloaded ;;;###autoload
282 (defun mwheel-install (&optional uninstall)
283 "Enable mouse wheel support."
284 (mouse-wheel-mode (if uninstall -1 1)))
286 (provide 'mwheel)
288 ;;; mwheel.el ends here