* facemenu.el (facemenu-read-color): Use a completion function
[bpt/emacs.git] / lisp / mwheel.el
CommitLineData
d09696f7 1;;; mwheel.el --- Wheel mouse support
a32b7419 2
fc926716 3;; Copyright (C) 1998, 2000, 2001, 2002, 2002, 2004, 2005, 2006, 2007,
ae940284 4;; 2008, 2009 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
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
8ed8f294 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
a32b7419 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
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a32b7419 22
a32b7419
WP
23;;; Commentary:
24
25;; This code will enable the use of the infamous 'wheel' on the new
26;; crop of mice. Under XFree86 and the XSuSE X Servers, the wheel
27;; events are sent as button4/button5 events.
28
29;; I for one would prefer some way of converting the button4/button5
30;; events into different event types, like 'mwheel-up' or
31;; 'mwheel-down', but I cannot find a way to do this very easily (or
32;; portably), so for now I just live with it.
33
34;; To enable this code, simply put this at the top of your .emacs
35;; file:
36;;
abd01646 37;; (mouse-wheel-mode 1)
a32b7419
WP
38
39;;; Code:
40
41(require 'custom)
d09696f7 42(require 'timer)
a32b7419 43
bf85004b
GM
44;; Setter function for mouse-button user-options. Switch Mouse Wheel
45;; mode off and on again so that the old button is unbound and
46;; new button is bound to mwheel-scroll.
47
48(defun mouse-wheel-change-button (var button)
bb5d43fe
SM
49 (let ((active mouse-wheel-mode))
50 ;; Deactivate before changing the setting.
51 (when active (mouse-wheel-mode -1))
52 (set-default var button)
53 (when active (mouse-wheel-mode 1))))
bf85004b 54
e5ab6ade
JB
55(defvar mouse-wheel-down-button 4)
56(make-obsolete-variable 'mouse-wheel-down-button
89ee0163
JB
57 'mouse-wheel-down-event
58 "22.1")
bb5d43fe
SM
59(defcustom mouse-wheel-down-event
60 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
9e2a2647 61 (if (memq window-system '(w32 ns))
f4e62260
JR
62 'wheel-up
63 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
64 mouse-wheel-down-button)))
bb5d43fe 65 "Event used for scrolling down."
bf85004b 66 :group 'mouse
bb5d43fe 67 :type 'symbol
bf85004b
GM
68 :set 'mouse-wheel-change-button)
69
e5ab6ade
JB
70(defvar mouse-wheel-up-button 5)
71(make-obsolete-variable 'mouse-wheel-up-button
89ee0163
JB
72 'mouse-wheel-up-event
73 "22.1")
bb5d43fe
SM
74(defcustom mouse-wheel-up-event
75 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
9e2a2647 76 (if (memq window-system '(w32 ns))
f4e62260
JR
77 'wheel-down
78 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
79 mouse-wheel-up-button)))
a348f5ba 80 "Event used for scrolling up."
bf85004b 81 :group 'mouse
bb5d43fe 82 :type 'symbol
bf85004b
GM
83 :set 'mouse-wheel-change-button)
84
e5ab6ade
JB
85(defvar mouse-wheel-click-button 2)
86(make-obsolete-variable 'mouse-wheel-click-button
89ee0163
JB
87 'mouse-wheel-click-event
88 "22.1")
d09696f7
KS
89(defcustom mouse-wheel-click-event
90 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
91 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
92 mouse-wheel-click-button))
93 "Event that should be temporarily inhibited after mouse scrolling.
94The mouse wheel is typically on the mouse-2 button, so it may easily
a6d48e09 95happen that text is accidentally yanked into the buffer when
d09696f7
KS
96scrolling with the mouse wheel. To prevent that, this variable can be
97set to the event sent when clicking on the mouse wheel button."
98 :group 'mouse
99 :type 'symbol
100 :set 'mouse-wheel-change-button)
101
102(defcustom mouse-wheel-inhibit-click-time 0.35
103 "Time in seconds to inhibit clicking on mouse wheel button after scroll."
104 :group 'mouse
68f2d641 105 :type 'number)
d09696f7 106
141ddc20 107(defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
a32b7419 108 "Amount to scroll windows by when spinning the mouse wheel.
eb4504e0
SM
109This is an alist mapping the modifier key to the amount to scroll when
110the wheel is moved with the modifier key depressed.
111Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
112MODIFIERS is nil.
113
41bbbdce 114AMOUNT should be the number of lines to scroll, or nil for near full
eb4504e0
SM
115screen. It can also be a floating point number, specifying the fraction of
116a full screen to scroll. A near full screen is `next-screen-context-lines'
117less than a full screen."
a32b7419 118 :group 'mouse
141ddc20
SM
119 :type '(cons
120 (choice :tag "Normal"
a32b7419 121 (const :tag "Full screen" :value nil)
141ddc20 122 (integer :tag "Specific # of lines")
055016a4 123 (float :tag "Fraction of window")
eb4504e0
SM
124 (cons
125 (repeat (choice :tag "modifier"
126 (const alt) (const control) (const hyper)
127 (const meta) (const shift) (const super)))
128 (choice :tag "scroll amount"
129 (const :tag "Full screen" :value nil)
130 (integer :tag "Specific # of lines")
055016a4 131 (float :tag "Fraction of window"))))
141ddc20
SM
132 (repeat
133 (cons
eb4504e0
SM
134 (repeat (choice :tag "modifier"
135 (const alt) (const control) (const hyper)
141ddc20
SM
136 (const meta) (const shift) (const super)))
137 (choice :tag "scroll amount"
138 (const :tag "Full screen" :value nil)
139 (integer :tag "Specific # of lines")
055016a4 140 (float :tag "Fraction of window"))))))
141ddc20 141
d1ff89d9 142(defcustom mouse-wheel-progressive-speed t
141ddc20
SM
143 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
144Note that this has no effect when `mouse-wheel-scroll-amount' specifies
eb4504e0
SM
145a \"near full screen\" scroll or when the mouse wheel sends key instead
146of button events."
141ddc20
SM
147 :group 'mouse
148 :type 'boolean)
a32b7419 149
bb5d43fe 150(defcustom mouse-wheel-follow-mouse t
a32b7419 151 "Whether the mouse wheel should scroll the window that the mouse is over.
eb4504e0 152This can be slightly disconcerting, but some people prefer it."
a32b7419
WP
153 :group 'mouse
154 :type 'boolean)
155
fc926716
GM
156(eval-and-compile
157 (if (fboundp 'event-button)
158 (fset 'mwheel-event-button 'event-button)
141ddc20 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))
fc926716 167 x))))
141ddc20 168
fc926716
GM
169 (if (fboundp 'event-window)
170 (fset 'mwheel-event-window 'event-window)
141ddc20 171 (defun mwheel-event-window (event)
fc926716 172 (posn-window (event-start event)))))
141ddc20 173
d09696f7
KS
174(defvar mwheel-inhibit-click-event-timer nil
175 "Timer running while mouse wheel click event is inhibited.")
176
177(defun mwheel-inhibit-click-timeout ()
178 "Handler for `mwheel-inhibit-click-event-timer'."
179 (setq mwheel-inhibit-click-event-timer nil)
180 (remove-hook 'pre-command-hook 'mwheel-filter-click-events))
181
182(defun mwheel-filter-click-events ()
183 "Discard `mouse-wheel-click-event' while scrolling the mouse."
184 (if (eq (event-basic-type last-input-event) mouse-wheel-click-event)
185 (setq this-command 'ignore)))
186
141ddc20
SM
187(defun mwheel-scroll (event)
188 "Scroll up or down according to the EVENT.
189This should only be bound to mouse buttons 4 and 5."
bb5d43fe 190 (interactive (list last-input-event))
141ddc20
SM
191 (let* ((curwin (if mouse-wheel-follow-mouse
192 (prog1
193 (selected-window)
194 (select-window (mwheel-event-window event)))))
05786f2d
CY
195 (buffer (window-buffer curwin))
196 (opoint (with-current-buffer buffer
197 (when (eq (car-safe transient-mark-mode) 'only)
198 (point))))
141ddc20
SM
199 (mods
200 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
eb4504e0
SM
201 (amt (assoc mods mouse-wheel-scroll-amount)))
202 ;; Extract the actual amount or find the element that has no modifiers.
203 (if amt (setq amt (cdr amt))
204 (let ((list-elt mouse-wheel-scroll-amount))
205 (while (consp (setq amt (pop list-elt))))))
141ddc20 206 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
d1ff89d9 207 (when (and mouse-wheel-progressive-speed (numberp amt))
141ddc20 208 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
bb5d43fe 209 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
141ddc20 210 (setq amt (* amt (event-click-count event))))
a32b7419 211 (unwind-protect
141ddc20 212 (let ((button (mwheel-event-button event)))
44a50ffd
SM
213 (cond ((eq button mouse-wheel-down-event)
214 (condition-case nil (scroll-down amt)
215 ;; Make sure we do indeed scroll to the beginning of
216 ;; the buffer.
217 (beginning-of-buffer
218 (unwind-protect
219 (scroll-down)
220 ;; If the first scroll succeeded, then some scrolling
221 ;; is possible: keep scrolling til the beginning but
222 ;; do not signal an error. For some reason, we have
04bf5b65 223 ;; to do it even if the first scroll signaled an
44a50ffd
SM
224 ;; error, because otherwise the window is recentered
225 ;; for a reason that escapes me. This problem seems
226 ;; to only affect scroll-down. --Stef
227 (set-window-start (selected-window) (point-min))))))
228 ((eq button mouse-wheel-up-event)
229 (condition-case nil (scroll-up amt)
230 ;; Make sure we do indeed scroll to the end of the buffer.
231 (end-of-buffer (while t (scroll-up)))))
141ddc20 232 (t (error "Bad binding in mwheel-scroll"))))
05786f2d
CY
233 (if curwin (select-window curwin)))
234 ;; If there is a temporarily active region, deactivate it iff
235 ;; scrolling moves point.
236 (when opoint
237 (with-current-buffer buffer
238 (when (/= opoint (point))
239 (deactivate-mark)))))
d09696f7
KS
240 (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
241 (if mwheel-inhibit-click-event-timer
242 (cancel-timer mwheel-inhibit-click-event-timer)
243 (add-hook 'pre-command-hook 'mwheel-filter-click-events))
f1180544 244 (setq mwheel-inhibit-click-event-timer
d09696f7
KS
245 (run-with-timer mouse-wheel-inhibit-click-time nil
246 'mwheel-inhibit-click-timeout))))
f4cbc7a0 247
a32b7419 248;;;###autoload
f4cbc7a0
MB
249(define-minor-mode mouse-wheel-mode
250 "Toggle mouse wheel support.
251With prefix argument ARG, turn on if positive, otherwise off.
a6d48e09 252Return non-nil if the new state is enabled."
f4cbc7a0
MB
253 :global t
254 :group 'mouse
bb5d43fe
SM
255 (let* ((dn mouse-wheel-down-event)
256 (up mouse-wheel-up-event)
141ddc20 257 (keys
eb4504e0
SM
258 (nconc (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,up)])
259 mouse-wheel-scroll-amount)
260 (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,dn)])
261 mouse-wheel-scroll-amount))))
141ddc20
SM
262 ;; This condition-case is here because Emacs 19 will throw an error
263 ;; if you try to define a key that it does not know about. I for one
264 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
265 ;; that if the wheeled-mouse is there, it just works, and this way it
266 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
267 (condition-case ()
268 (dolist (key keys)
269 (cond (mouse-wheel-mode
270 (global-set-key key 'mwheel-scroll))
271 ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
272 (global-unset-key key))))
273 (error nil))))
f4cbc7a0
MB
274
275;;; Compatibility entry point
276;;;###autoload
277(defun mwheel-install (&optional uninstall)
278 "Enable mouse wheel support."
bb5d43fe 279 (mouse-wheel-mode (if uninstall -1 1)))
f4cbc7a0 280
a32b7419
WP
281(provide 'mwheel)
282
d1ff89d9 283;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f
a32b7419 284;;; mwheel.el ends here