Commit | Line | Data |
---|---|---|
d09696f7 | 1 | ;;; mwheel.el --- Wheel mouse support |
a32b7419 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1998, 2000, 2001, 2002, 2002, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
a32b7419 WP |
5 | ;; Maintainer: William M. Perry <wmperry@gnu.org> |
6 | ;; Keywords: mouse | |
7 | ||
8ed8f294 | 8 | ;; This file is part of GNU Emacs. |
a32b7419 | 9 | |
8ed8f294 GM |
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 | |
b4aa6026 | 12 | ;; the Free Software Foundation; either version 3, or (at your option) |
a32b7419 WP |
13 | ;; any later version. |
14 | ||
8ed8f294 GM |
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. | |
a32b7419 WP |
19 | |
20 | ;; You should have received a copy of the GNU General Public License | |
8ed8f294 | 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
a32b7419 | 24 | |
a32b7419 WP |
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 | ;; | |
abd01646 | 39 | ;; (mouse-wheel-mode 1) |
a32b7419 WP |
40 | |
41 | ;;; Code: | |
42 | ||
43 | (require 'custom) | |
d09696f7 | 44 | (require 'timer) |
a32b7419 | 45 | |
bf85004b GM |
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) | |
bb5d43fe SM |
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)))) | |
bf85004b | 56 | |
e5ab6ade JB |
57 | (defvar mouse-wheel-down-button 4) |
58 | (make-obsolete-variable 'mouse-wheel-down-button | |
89ee0163 JB |
59 | 'mouse-wheel-down-event |
60 | "22.1") | |
bb5d43fe SM |
61 | (defcustom mouse-wheel-down-event |
62 | ;; In the latest versions of XEmacs, we could just use mouse-%s as well. | |
bf03bdf3 | 63 | (if (memq window-system '(w32 mac)) |
f4e62260 JR |
64 | 'wheel-up |
65 | (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s") | |
66 | mouse-wheel-down-button))) | |
bb5d43fe | 67 | "Event used for scrolling down." |
bf85004b | 68 | :group 'mouse |
bb5d43fe | 69 | :type 'symbol |
bf85004b GM |
70 | :set 'mouse-wheel-change-button) |
71 | ||
e5ab6ade JB |
72 | (defvar mouse-wheel-up-button 5) |
73 | (make-obsolete-variable 'mouse-wheel-up-button | |
89ee0163 JB |
74 | 'mouse-wheel-up-event |
75 | "22.1") | |
bb5d43fe SM |
76 | (defcustom mouse-wheel-up-event |
77 | ;; In the latest versions of XEmacs, we could just use mouse-%s as well. | |
bf03bdf3 | 78 | (if (memq window-system '(w32 mac)) |
f4e62260 JR |
79 | 'wheel-down |
80 | (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s") | |
81 | mouse-wheel-up-button))) | |
a348f5ba | 82 | "Event used for scrolling up." |
bf85004b | 83 | :group 'mouse |
bb5d43fe | 84 | :type 'symbol |
bf85004b GM |
85 | :set 'mouse-wheel-change-button) |
86 | ||
e5ab6ade JB |
87 | (defvar mouse-wheel-click-button 2) |
88 | (make-obsolete-variable 'mouse-wheel-click-button | |
89ee0163 JB |
89 | 'mouse-wheel-click-event |
90 | "22.1") | |
d09696f7 KS |
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 | |
a6d48e09 | 97 | happen that text is accidentally yanked into the buffer when |
d09696f7 KS |
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." | |
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 | |
68f2d641 | 107 | :type 'number) |
d09696f7 | 108 | |
141ddc20 | 109 | (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil)) |
a32b7419 | 110 | "Amount to scroll windows by when spinning the mouse wheel. |
eb4504e0 SM |
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 | |
114 | MODIFIERS is nil. | |
115 | ||
41bbbdce | 116 | AMOUNT should be the number of lines to scroll, or nil for near full |
eb4504e0 SM |
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." | |
a32b7419 | 120 | :group 'mouse |
141ddc20 SM |
121 | :type '(cons |
122 | (choice :tag "Normal" | |
a32b7419 | 123 | (const :tag "Full screen" :value nil) |
141ddc20 | 124 | (integer :tag "Specific # of lines") |
055016a4 | 125 | (float :tag "Fraction of window") |
eb4504e0 SM |
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") | |
055016a4 | 133 | (float :tag "Fraction of window")))) |
141ddc20 SM |
134 | (repeat |
135 | (cons | |
eb4504e0 SM |
136 | (repeat (choice :tag "modifier" |
137 | (const alt) (const control) (const hyper) | |
141ddc20 SM |
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") | |
055016a4 | 142 | (float :tag "Fraction of window")))))) |
141ddc20 | 143 | |
d1ff89d9 | 144 | (defcustom mouse-wheel-progressive-speed t |
141ddc20 SM |
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 | |
eb4504e0 SM |
147 | a \"near full screen\" scroll or when the mouse wheel sends key instead |
148 | of button events." | |
141ddc20 SM |
149 | :group 'mouse |
150 | :type 'boolean) | |
a32b7419 | 151 | |
bb5d43fe | 152 | (defcustom mouse-wheel-follow-mouse t |
a32b7419 | 153 | "Whether the mouse wheel should scroll the window that the mouse is over. |
eb4504e0 | 154 | This can be slightly disconcerting, but some people prefer it." |
a32b7419 WP |
155 | :group 'mouse |
156 | :type 'boolean) | |
157 | ||
141ddc20 SM |
158 | (if (not (fboundp 'event-button)) |
159 | (defun mwheel-event-button (event) | |
bb5d43fe | 160 | (let ((x (event-basic-type event))) |
141ddc20 | 161 | ;; Map mouse-wheel events to appropriate buttons |
bb5d43fe | 162 | (if (eq 'mouse-wheel x) |
141ddc20 SM |
163 | (let ((amount (car (cdr (cdr (cdr event)))))) |
164 | (if (< amount 0) | |
bb5d43fe SM |
165 | mouse-wheel-up-event |
166 | mouse-wheel-down-event)) | |
167 | x))) | |
94394914 | 168 | (fset 'mwheel-event-button 'event-button)) |
141ddc20 SM |
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 | ||
d09696f7 KS |
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 | ||
141ddc20 SM |
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." | |
bb5d43fe | 191 | (interactive (list last-input-event)) |
141ddc20 SM |
192 | (let* ((curwin (if mouse-wheel-follow-mouse |
193 | (prog1 | |
194 | (selected-window) | |
195 | (select-window (mwheel-event-window event))))) | |
05786f2d CY |
196 | (buffer (window-buffer curwin)) |
197 | (opoint (with-current-buffer buffer | |
198 | (when (eq (car-safe transient-mark-mode) 'only) | |
199 | (point)))) | |
141ddc20 SM |
200 | (mods |
201 | (delq 'click (delq 'double (delq 'triple (event-modifiers event))))) | |
eb4504e0 SM |
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)))))) | |
141ddc20 | 207 | (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height)))))) |
d1ff89d9 | 208 | (when (and mouse-wheel-progressive-speed (numberp amt)) |
141ddc20 | 209 | ;; When the double-mouse-N comes in, a mouse-N has been executed already, |
bb5d43fe | 210 | ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...). |
141ddc20 | 211 | (setq amt (* amt (event-click-count event)))) |
a32b7419 | 212 | (unwind-protect |
141ddc20 | 213 | (let ((button (mwheel-event-button event))) |
44a50ffd SM |
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))))) | |
141ddc20 | 233 | (t (error "Bad binding in mwheel-scroll")))) |
05786f2d CY |
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))))) | |
d09696f7 KS |
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)) | |
f1180544 | 245 | (setq mwheel-inhibit-click-event-timer |
d09696f7 KS |
246 | (run-with-timer mouse-wheel-inhibit-click-time nil |
247 | 'mwheel-inhibit-click-timeout)))) | |
f4cbc7a0 | 248 | |
a32b7419 | 249 | ;;;###autoload |
f4cbc7a0 MB |
250 | (define-minor-mode mouse-wheel-mode |
251 | "Toggle mouse wheel support. | |
252 | With prefix argument ARG, turn on if positive, otherwise off. | |
a6d48e09 | 253 | Return non-nil if the new state is enabled." |
f4cbc7a0 MB |
254 | :global t |
255 | :group 'mouse | |
bb5d43fe SM |
256 | (let* ((dn mouse-wheel-down-event) |
257 | (up mouse-wheel-up-event) | |
141ddc20 | 258 | (keys |
eb4504e0 SM |
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)))) | |
141ddc20 SM |
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)))) | |
f4cbc7a0 MB |
275 | |
276 | ;;; Compatibility entry point | |
277 | ;;;###autoload | |
278 | (defun mwheel-install (&optional uninstall) | |
279 | "Enable mouse wheel support." | |
bb5d43fe | 280 | (mouse-wheel-mode (if uninstall -1 1))) |
f4cbc7a0 | 281 | |
a32b7419 WP |
282 | (provide 'mwheel) |
283 | ||
d1ff89d9 | 284 | ;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f |
a32b7419 | 285 | ;;; mwheel.el ends here |