Commit | Line | Data |
---|---|---|
d09696f7 | 1 | ;;; mwheel.el --- Wheel mouse support |
a32b7419 | 2 | |
bb5d43fe | 3 | ;; Copyright (C) 1998,2000,2001,2002 Free Software Foundation, Inc. |
a32b7419 WP |
4 | ;; Maintainer: William M. Perry <wmperry@gnu.org> |
5 | ;; Keywords: mouse | |
6 | ||
8ed8f294 | 7 | ;; This file is part of GNU Emacs. |
a32b7419 | 8 | |
8ed8f294 GM |
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
10 | ;; it under the terms of the GNU General Public License as published by | |
a32b7419 WP |
11 | ;; the Free Software Foundation; either version 2, or (at your option) |
12 | ;; any later version. | |
13 | ||
8ed8f294 GM |
14 | ;; GNU Emacs is distributed in the hope that it will be useful, |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
a32b7419 WP |
18 | |
19 | ;; You should have received a copy of the GNU General Public License | |
8ed8f294 | 20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
a32b7419 WP |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
22 | ;; Boston, MA 02111-1307, USA. | |
23 | ||
a32b7419 WP |
24 | ;;; Commentary: |
25 | ||
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. | |
29 | ||
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. | |
34 | ||
35 | ;; To enable this code, simply put this at the top of your .emacs | |
36 | ;; file: | |
37 | ;; | |
a32b7419 WP |
38 | ;; (mwheel-install) |
39 | ||
40 | ;;; Code: | |
41 | ||
42 | (require 'custom) | |
d09696f7 | 43 | (require 'timer) |
a32b7419 | 44 | |
bf85004b GM |
45 | ;; Setter function for mouse-button user-options. Switch Mouse Wheel |
46 | ;; mode off and on again so that the old button is unbound and | |
47 | ;; new button is bound to mwheel-scroll. | |
48 | ||
49 | (defun mouse-wheel-change-button (var button) | |
bb5d43fe SM |
50 | (let ((active mouse-wheel-mode)) |
51 | ;; Deactivate before changing the setting. | |
52 | (when active (mouse-wheel-mode -1)) | |
53 | (set-default var button) | |
54 | (when active (mouse-wheel-mode 1)))) | |
bf85004b | 55 | |
e5ab6ade JB |
56 | (defvar mouse-wheel-down-button 4) |
57 | (make-obsolete-variable 'mouse-wheel-down-button | |
58 | 'mouse-wheel-down-event) | |
bb5d43fe SM |
59 | (defcustom mouse-wheel-down-event |
60 | ;; In the latest versions of XEmacs, we could just use mouse-%s as well. | |
61 | (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s") | |
62 | mouse-wheel-down-button)) | |
63 | "Event used for scrolling down." | |
bf85004b | 64 | :group 'mouse |
bb5d43fe | 65 | :type 'symbol |
bf85004b GM |
66 | :set 'mouse-wheel-change-button) |
67 | ||
e5ab6ade JB |
68 | (defvar mouse-wheel-up-button 5) |
69 | (make-obsolete-variable 'mouse-wheel-up-button | |
70 | 'mouse-wheel-up-event) | |
bb5d43fe SM |
71 | (defcustom mouse-wheel-up-event |
72 | ;; In the latest versions of XEmacs, we could just use mouse-%s as well. | |
73 | (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s") | |
74 | mouse-wheel-up-button)) | |
75 | "Event used for scrolling down." | |
bf85004b | 76 | :group 'mouse |
bb5d43fe | 77 | :type 'symbol |
bf85004b GM |
78 | :set 'mouse-wheel-change-button) |
79 | ||
e5ab6ade JB |
80 | (defvar mouse-wheel-click-button 2) |
81 | (make-obsolete-variable 'mouse-wheel-click-button | |
82 | 'mouse-wheel-click-event) | |
d09696f7 KS |
83 | (defcustom mouse-wheel-click-event |
84 | ;; In the latest versions of XEmacs, we could just use mouse-%s as well. | |
85 | (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s") | |
86 | mouse-wheel-click-button)) | |
87 | "Event that should be temporarily inhibited after mouse scrolling. | |
88 | The mouse wheel is typically on the mouse-2 button, so it may easily | |
89 | happen that text is accidentially yanked into the buffer when | |
90 | scrolling with the mouse wheel. To prevent that, this variable can be | |
91 | set to the event sent when clicking on the mouse wheel button." | |
92 | :group 'mouse | |
93 | :type 'symbol | |
94 | :set 'mouse-wheel-change-button) | |
95 | ||
96 | (defcustom mouse-wheel-inhibit-click-time 0.35 | |
97 | "Time in seconds to inhibit clicking on mouse wheel button after scroll." | |
98 | :group 'mouse | |
68f2d641 | 99 | :type 'number) |
d09696f7 | 100 | |
141ddc20 | 101 | (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil)) |
a32b7419 | 102 | "Amount to scroll windows by when spinning the mouse wheel. |
eb4504e0 SM |
103 | This is an alist mapping the modifier key to the amount to scroll when |
104 | the wheel is moved with the modifier key depressed. | |
105 | Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if | |
106 | MODIFIERS is nil. | |
107 | ||
108 | AMOUNT should be the number of lines to scroll, or `nil' for near full | |
109 | screen. It can also be a floating point number, specifying the fraction of | |
110 | a full screen to scroll. A near full screen is `next-screen-context-lines' | |
111 | less than a full screen." | |
a32b7419 | 112 | :group 'mouse |
141ddc20 SM |
113 | :type '(cons |
114 | (choice :tag "Normal" | |
a32b7419 | 115 | (const :tag "Full screen" :value nil) |
141ddc20 | 116 | (integer :tag "Specific # of lines") |
055016a4 | 117 | (float :tag "Fraction of window") |
eb4504e0 SM |
118 | (cons |
119 | (repeat (choice :tag "modifier" | |
120 | (const alt) (const control) (const hyper) | |
121 | (const meta) (const shift) (const super))) | |
122 | (choice :tag "scroll amount" | |
123 | (const :tag "Full screen" :value nil) | |
124 | (integer :tag "Specific # of lines") | |
055016a4 | 125 | (float :tag "Fraction of window")))) |
141ddc20 SM |
126 | (repeat |
127 | (cons | |
eb4504e0 SM |
128 | (repeat (choice :tag "modifier" |
129 | (const alt) (const control) (const hyper) | |
141ddc20 SM |
130 | (const meta) (const shift) (const super))) |
131 | (choice :tag "scroll amount" | |
132 | (const :tag "Full screen" :value nil) | |
133 | (integer :tag "Specific # of lines") | |
055016a4 | 134 | (float :tag "Fraction of window")))))) |
141ddc20 SM |
135 | |
136 | (defcustom mouse-wheel-progessive-speed t | |
137 | "If non-nil, the faster the user moves the wheel, the faster the scrolling. | |
138 | Note that this has no effect when `mouse-wheel-scroll-amount' specifies | |
eb4504e0 SM |
139 | a \"near full screen\" scroll or when the mouse wheel sends key instead |
140 | of button events." | |
141ddc20 SM |
141 | :group 'mouse |
142 | :type 'boolean) | |
a32b7419 | 143 | |
bb5d43fe | 144 | (defcustom mouse-wheel-follow-mouse t |
a32b7419 | 145 | "Whether the mouse wheel should scroll the window that the mouse is over. |
eb4504e0 | 146 | This can be slightly disconcerting, but some people prefer it." |
a32b7419 WP |
147 | :group 'mouse |
148 | :type 'boolean) | |
149 | ||
141ddc20 SM |
150 | (if (not (fboundp 'event-button)) |
151 | (defun mwheel-event-button (event) | |
bb5d43fe | 152 | (let ((x (event-basic-type event))) |
141ddc20 | 153 | ;; Map mouse-wheel events to appropriate buttons |
bb5d43fe | 154 | (if (eq 'mouse-wheel x) |
141ddc20 SM |
155 | (let ((amount (car (cdr (cdr (cdr event)))))) |
156 | (if (< amount 0) | |
bb5d43fe SM |
157 | mouse-wheel-up-event |
158 | mouse-wheel-down-event)) | |
159 | x))) | |
94394914 | 160 | (fset 'mwheel-event-button 'event-button)) |
141ddc20 SM |
161 | |
162 | (if (not (fboundp 'event-window)) | |
163 | (defun mwheel-event-window (event) | |
164 | (posn-window (event-start event))) | |
165 | (fset 'mwheel-event-window 'event-window)) | |
166 | ||
d09696f7 KS |
167 | (defvar mwheel-inhibit-click-event-timer nil |
168 | "Timer running while mouse wheel click event is inhibited.") | |
169 | ||
170 | (defun mwheel-inhibit-click-timeout () | |
171 | "Handler for `mwheel-inhibit-click-event-timer'." | |
172 | (setq mwheel-inhibit-click-event-timer nil) | |
173 | (remove-hook 'pre-command-hook 'mwheel-filter-click-events)) | |
174 | ||
175 | (defun mwheel-filter-click-events () | |
176 | "Discard `mouse-wheel-click-event' while scrolling the mouse." | |
177 | (if (eq (event-basic-type last-input-event) mouse-wheel-click-event) | |
178 | (setq this-command 'ignore))) | |
179 | ||
141ddc20 SM |
180 | (defun mwheel-scroll (event) |
181 | "Scroll up or down according to the EVENT. | |
182 | This should only be bound to mouse buttons 4 and 5." | |
bb5d43fe | 183 | (interactive (list last-input-event)) |
141ddc20 SM |
184 | (let* ((curwin (if mouse-wheel-follow-mouse |
185 | (prog1 | |
186 | (selected-window) | |
187 | (select-window (mwheel-event-window event))))) | |
188 | (mods | |
189 | (delq 'click (delq 'double (delq 'triple (event-modifiers event))))) | |
eb4504e0 SM |
190 | (amt (assoc mods mouse-wheel-scroll-amount))) |
191 | ;; Extract the actual amount or find the element that has no modifiers. | |
192 | (if amt (setq amt (cdr amt)) | |
193 | (let ((list-elt mouse-wheel-scroll-amount)) | |
194 | (while (consp (setq amt (pop list-elt)))))) | |
141ddc20 SM |
195 | (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height)))))) |
196 | (when (and mouse-wheel-progessive-speed (numberp amt)) | |
197 | ;; When the double-mouse-N comes in, a mouse-N has been executed already, | |
bb5d43fe | 198 | ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...). |
141ddc20 | 199 | (setq amt (* amt (event-click-count event)))) |
a32b7419 | 200 | (unwind-protect |
141ddc20 | 201 | (let ((button (mwheel-event-button event))) |
bb5d43fe SM |
202 | (cond ((eq button mouse-wheel-down-event) (scroll-down amt)) |
203 | ((eq button mouse-wheel-up-event) (scroll-up amt)) | |
141ddc20 | 204 | (t (error "Bad binding in mwheel-scroll")))) |
d09696f7 KS |
205 | (if curwin (select-window curwin)))) |
206 | (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time) | |
207 | (if mwheel-inhibit-click-event-timer | |
208 | (cancel-timer mwheel-inhibit-click-event-timer) | |
209 | (add-hook 'pre-command-hook 'mwheel-filter-click-events)) | |
f1180544 | 210 | (setq mwheel-inhibit-click-event-timer |
d09696f7 KS |
211 | (run-with-timer mouse-wheel-inhibit-click-time nil |
212 | 'mwheel-inhibit-click-timeout)))) | |
f4cbc7a0 | 213 | |
a32b7419 | 214 | ;;;###autoload |
f4cbc7a0 MB |
215 | (define-minor-mode mouse-wheel-mode |
216 | "Toggle mouse wheel support. | |
217 | With prefix argument ARG, turn on if positive, otherwise off. | |
218 | Returns non-nil if the new state is enabled." | |
f4cbc7a0 MB |
219 | :global t |
220 | :group 'mouse | |
bb5d43fe SM |
221 | (let* ((dn mouse-wheel-down-event) |
222 | (up mouse-wheel-up-event) | |
141ddc20 | 223 | (keys |
eb4504e0 SM |
224 | (nconc (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,up)]) |
225 | mouse-wheel-scroll-amount) | |
226 | (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,dn)]) | |
227 | mouse-wheel-scroll-amount)))) | |
141ddc20 SM |
228 | ;; This condition-case is here because Emacs 19 will throw an error |
229 | ;; if you try to define a key that it does not know about. I for one | |
230 | ;; prefer to just unconditionally do a mwheel-install in my .emacs, so | |
231 | ;; that if the wheeled-mouse is there, it just works, and this way it | |
232 | ;; doesn't yell at me if I'm on my laptop or another machine, etc. | |
233 | (condition-case () | |
234 | (dolist (key keys) | |
235 | (cond (mouse-wheel-mode | |
236 | (global-set-key key 'mwheel-scroll)) | |
237 | ((eq (lookup-key (current-global-map) key) 'mwheel-scroll) | |
238 | (global-unset-key key)))) | |
239 | (error nil)))) | |
f4cbc7a0 MB |
240 | |
241 | ;;; Compatibility entry point | |
242 | ;;;###autoload | |
243 | (defun mwheel-install (&optional uninstall) | |
244 | "Enable mouse wheel support." | |
bb5d43fe | 245 | (mouse-wheel-mode (if uninstall -1 1))) |
f4cbc7a0 | 246 | |
a32b7419 WP |
247 | (provide 'mwheel) |
248 | ||
249 | ;;; mwheel.el ends here |