Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / mwheel.el
... / ...
CommitLineData
1;;; mwheel.el --- Wheel mouse support
2
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>
6;; Keywords: mouse
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
14
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.
19
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.
24
25;;; Commentary:
26
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.
30
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.
35
36;; To enable this code, simply put this at the top of your .emacs
37;; file:
38;;
39;; (mouse-wheel-mode 1)
40
41;;; Code:
42
43(require 'custom)
44(require 'timer)
45
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.
49
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))))
56
57(defvar mouse-wheel-down-button 4)
58(make-obsolete-variable 'mouse-wheel-down-button
59 'mouse-wheel-down-event
60 "22.1")
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))
64 'wheel-up
65 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
66 mouse-wheel-down-button)))
67 "Event used for scrolling down."
68 :group 'mouse
69 :type 'symbol
70 :set 'mouse-wheel-change-button)
71
72(defvar mouse-wheel-up-button 5)
73(make-obsolete-variable 'mouse-wheel-up-button
74 'mouse-wheel-up-event
75 "22.1")
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))
79 'wheel-down
80 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
81 mouse-wheel-up-button)))
82 "Event used for scrolling up."
83 :group 'mouse
84 :type 'symbol
85 :set 'mouse-wheel-change-button)
86
87(defvar mouse-wheel-click-button 2)
88(make-obsolete-variable 'mouse-wheel-click-button
89 'mouse-wheel-click-event
90 "22.1")
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.
96The mouse wheel is typically on the mouse-2 button, so it may easily
97happen that text is accidentally yanked into the buffer when
98scrolling with the mouse wheel. To prevent that, this variable can be
99set to the event sent when clicking on the mouse wheel button."
100 :group 'mouse
101 :type 'symbol
102 :set 'mouse-wheel-change-button)
103
104(defcustom mouse-wheel-inhibit-click-time 0.35
105 "Time in seconds to inhibit clicking on mouse wheel button after scroll."
106 :group 'mouse
107 :type 'number)
108
109(defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
110 "Amount to scroll windows by when spinning the mouse wheel.
111This is an alist mapping the modifier key to the amount to scroll when
112the wheel is moved with the modifier key depressed.
113Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
114MODIFIERS is nil.
115
116AMOUNT should be the number of lines to scroll, or nil for near full
117screen. It can also be a floating point number, specifying the fraction of
118a full screen to scroll. A near full screen is `next-screen-context-lines'
119less than a full screen."
120 :group 'mouse
121 :type '(cons
122 (choice :tag "Normal"
123 (const :tag "Full screen" :value nil)
124 (integer :tag "Specific # of lines")
125 (float :tag "Fraction of window")
126 (cons
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"))))
134 (repeat
135 (cons
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"))))))
143
144(defcustom mouse-wheel-progressive-speed t
145 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
146Note that this has no effect when `mouse-wheel-scroll-amount' specifies
147a \"near full screen\" scroll or when the mouse wheel sends key instead
148of button events."
149 :group 'mouse
150 :type 'boolean)
151
152(defcustom mouse-wheel-follow-mouse t
153 "Whether the mouse wheel should scroll the window that the mouse is over.
154This can be slightly disconcerting, but some people prefer it."
155 :group 'mouse
156 :type 'boolean)
157
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))))))
164 (if (< amount 0)
165 mouse-wheel-up-event
166 mouse-wheel-down-event))
167 x)))
168 (fset 'mwheel-event-button 'event-button))
169
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))
174
175(defvar mwheel-inhibit-click-event-timer nil
176 "Timer running while mouse wheel click event is inhibited.")
177
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))
182
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)))
187
188(defun mwheel-scroll (event)
189 "Scroll up or down according to the EVENT.
190This should only be bound to mouse buttons 4 and 5."
191 (interactive (list last-input-event))
192 (let* ((curwin (if mouse-wheel-follow-mouse
193 (prog1
194 (selected-window)
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)
199 (point))))
200 (mods
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))))
212 (unwind-protect
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
217 ;; the buffer.
218 (beginning-of-buffer
219 (unwind-protect
220 (scroll-down)
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.
237 (when opoint
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))))
248
249;;;###autoload
250(define-minor-mode mouse-wheel-mode
251 "Toggle mouse wheel support.
252With prefix argument ARG, turn on if positive, otherwise off.
253Return non-nil if the new state is enabled."
254 :global t
255 :group 'mouse
256 (let* ((dn mouse-wheel-down-event)
257 (up mouse-wheel-up-event)
258 (keys
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.
268 (condition-case ()
269 (dolist (key keys)
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))))
274 (error nil))))
275
276;;; Compatibility entry point
277;;;###autoload
278(defun mwheel-install (&optional uninstall)
279 "Enable mouse wheel support."
280 (mouse-wheel-mode (if uninstall -1 1)))
281
282(provide 'mwheel)
283
284;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f
285;;; mwheel.el ends here