]> code.delx.au - gnu-emacs/blob - lisp/mwheel.el
* mwheel.el (mwheel-installed-bindings): New var.
[gnu-emacs] / lisp / mwheel.el
1 ;;; mwheel.el --- Wheel mouse support
2
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2002, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
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 ;;
37 ;; (mouse-wheel-mode 1)
38
39 ;;; Code:
40
41 (require 'custom)
42 (require 'timer)
43
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)
49 (set-default var button)
50 ;; Sync the bindings.
51 (when mouse-wheel-mode (mouse-wheel-mode 1)))
52
53 (defvar mouse-wheel-down-button 4)
54 (make-obsolete-variable 'mouse-wheel-down-button
55 'mouse-wheel-down-event
56 "22.1")
57 (defcustom mouse-wheel-down-event
58 (if (or (featurep 'w32-win) (featurep 'ns-win))
59 'wheel-up
60 (intern (format "mouse-%s" mouse-wheel-down-button)))
61 "Event used for scrolling down."
62 :group 'mouse
63 :type 'symbol
64 :set 'mouse-wheel-change-button)
65
66 (defvar mouse-wheel-up-button 5)
67 (make-obsolete-variable 'mouse-wheel-up-button
68 'mouse-wheel-up-event
69 "22.1")
70 (defcustom mouse-wheel-up-event
71 (if (or (featurep 'w32-win) (featurep 'ns-win))
72 'wheel-down
73 (intern (format "mouse-%s" mouse-wheel-up-button)))
74 "Event used for scrolling up."
75 :group 'mouse
76 :type 'symbol
77 :set 'mouse-wheel-change-button)
78
79 (defvar mouse-wheel-click-button 2)
80 (make-obsolete-variable 'mouse-wheel-click-button
81 'mouse-wheel-click-event
82 "22.1")
83 (defcustom mouse-wheel-click-event
84 (intern (format "mouse-%s" mouse-wheel-click-button))
85 "Event that should be temporarily inhibited after mouse scrolling.
86 The mouse wheel is typically on the mouse-2 button, so it may easily
87 happen that text is accidentally yanked into the buffer when
88 scrolling with the mouse wheel. To prevent that, this variable can be
89 set to the event sent when clicking on the mouse wheel button."
90 :group 'mouse
91 :type 'symbol
92 :set 'mouse-wheel-change-button)
93
94 (defcustom mouse-wheel-inhibit-click-time 0.35
95 "Time in seconds to inhibit clicking on mouse wheel button after scroll."
96 :group 'mouse
97 :type 'number)
98
99 (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
100 "Amount to scroll windows by when spinning the mouse wheel.
101 This is an alist mapping the modifier key to the amount to scroll when
102 the wheel is moved with the modifier key depressed.
103 Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
104 MODIFIERS is nil.
105
106 AMOUNT should be the number of lines to scroll, or nil for near full
107 screen. It can also be a floating point number, specifying the fraction of
108 a full screen to scroll. A near full screen is `next-screen-context-lines'
109 less than a full screen."
110 :group 'mouse
111 :type '(cons
112 (choice :tag "Normal"
113 (const :tag "Full screen" :value nil)
114 (integer :tag "Specific # of lines")
115 (float :tag "Fraction of window")
116 (cons
117 (repeat (choice :tag "modifier"
118 (const alt) (const control) (const hyper)
119 (const meta) (const shift) (const super)))
120 (choice :tag "scroll amount"
121 (const :tag "Full screen" :value nil)
122 (integer :tag "Specific # of lines")
123 (float :tag "Fraction of window"))))
124 (repeat
125 (cons
126 (repeat (choice :tag "modifier"
127 (const alt) (const control) (const hyper)
128 (const meta) (const shift) (const super)))
129 (choice :tag "scroll amount"
130 (const :tag "Full screen" :value nil)
131 (integer :tag "Specific # of lines")
132 (float :tag "Fraction of window"))))))
133
134 (defcustom mouse-wheel-progressive-speed t
135 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
136 Note that this has no effect when `mouse-wheel-scroll-amount' specifies
137 a \"near full screen\" scroll or when the mouse wheel sends key instead
138 of button events."
139 :group 'mouse
140 :type 'boolean)
141
142 (defcustom mouse-wheel-follow-mouse t
143 "Whether the mouse wheel should scroll the window that the mouse is over.
144 This can be slightly disconcerting, but some people prefer it."
145 :group 'mouse
146 :type 'boolean)
147
148 (eval-and-compile
149 (if (fboundp 'event-button)
150 (fset 'mwheel-event-button 'event-button)
151 (defun mwheel-event-button (event)
152 (let ((x (event-basic-type event)))
153 ;; Map mouse-wheel events to appropriate buttons
154 (if (eq 'mouse-wheel x)
155 (let ((amount (car (cdr (cdr (cdr event))))))
156 (if (< amount 0)
157 mouse-wheel-up-event
158 mouse-wheel-down-event))
159 x))))
160
161 (if (fboundp 'event-window)
162 (fset 'mwheel-event-window 'event-window)
163 (defun mwheel-event-window (event)
164 (posn-window (event-start event)))))
165
166 (defvar mwheel-inhibit-click-event-timer nil
167 "Timer running while mouse wheel click event is inhibited.")
168
169 (defun mwheel-inhibit-click-timeout ()
170 "Handler for `mwheel-inhibit-click-event-timer'."
171 (setq mwheel-inhibit-click-event-timer nil)
172 (remove-hook 'pre-command-hook 'mwheel-filter-click-events))
173
174 (defun mwheel-filter-click-events ()
175 "Discard `mouse-wheel-click-event' while scrolling the mouse."
176 (if (eq (event-basic-type last-input-event) mouse-wheel-click-event)
177 (setq this-command 'ignore)))
178
179 (defun mwheel-scroll (event)
180 "Scroll up or down according to the EVENT.
181 This should only be bound to mouse buttons 4 and 5."
182 (interactive (list last-input-event))
183 (let* ((curwin (if mouse-wheel-follow-mouse
184 (prog1
185 (selected-window)
186 (select-window (mwheel-event-window event)))))
187 (buffer (window-buffer curwin))
188 (opoint (with-current-buffer buffer
189 (when (eq (car-safe transient-mark-mode) 'only)
190 (point))))
191 (mods
192 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
193 (amt (assoc mods mouse-wheel-scroll-amount)))
194 ;; Extract the actual amount or find the element that has no modifiers.
195 (if amt (setq amt (cdr amt))
196 (let ((list-elt mouse-wheel-scroll-amount))
197 (while (consp (setq amt (pop list-elt))))))
198 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
199 (when (and mouse-wheel-progressive-speed (numberp amt))
200 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
201 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
202 (setq amt (* amt (event-click-count event))))
203 (unwind-protect
204 (let ((button (mwheel-event-button event)))
205 (cond ((eq button mouse-wheel-down-event)
206 (condition-case nil (scroll-down amt)
207 ;; Make sure we do indeed scroll to the beginning of
208 ;; the buffer.
209 (beginning-of-buffer
210 (unwind-protect
211 (scroll-down)
212 ;; If the first scroll succeeded, then some scrolling
213 ;; is possible: keep scrolling til the beginning but
214 ;; do not signal an error. For some reason, we have
215 ;; to do it even if the first scroll signaled an
216 ;; error, because otherwise the window is recentered
217 ;; for a reason that escapes me. This problem seems
218 ;; to only affect scroll-down. --Stef
219 (set-window-start (selected-window) (point-min))))))
220 ((eq button mouse-wheel-up-event)
221 (condition-case nil (scroll-up amt)
222 ;; Make sure we do indeed scroll to the end of the buffer.
223 (end-of-buffer (while t (scroll-up)))))
224 (t (error "Bad binding in mwheel-scroll"))))
225 (if curwin (select-window curwin)))
226 ;; If there is a temporarily active region, deactivate it iff
227 ;; scrolling moves point.
228 (when opoint
229 (with-current-buffer buffer
230 (when (/= opoint (point))
231 (deactivate-mark)))))
232 (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
233 (if mwheel-inhibit-click-event-timer
234 (cancel-timer mwheel-inhibit-click-event-timer)
235 (add-hook 'pre-command-hook 'mwheel-filter-click-events))
236 (setq mwheel-inhibit-click-event-timer
237 (run-with-timer mouse-wheel-inhibit-click-time nil
238 'mwheel-inhibit-click-timeout))))
239
240 (defvar mwheel-installed-bindings nil)
241
242 ;;;###autoload
243 (define-minor-mode mouse-wheel-mode
244 "Toggle mouse wheel support.
245 With prefix argument ARG, turn on if positive, otherwise off.
246 Return non-nil if the new state is enabled."
247 :init-value t
248 ;; We'd like to use custom-initialize-set here so the setup is done
249 ;; before dumping, but at the point where the defcustom is evaluated,
250 ;; the corresponding function isn't defined yet, so
251 ;; custom-initialize-set signals an error.
252 :initialize 'custom-initialize-delay
253 :global t
254 :group 'mouse
255 ;; Remove previous bindings, if any.
256 (while mwheel-installed-bindings
257 (let ((key (pop mwheel-installed-bindings)))
258 (when (eq (lookup-key (current-global-map) key) 'mwheel-scroll)
259 (global-unset-key key))))
260 ;; Setup bindings as needed.
261 (when mouse-wheel-mode
262 (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event))
263 (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,event)])
264 mouse-wheel-scroll-amount))
265 (global-set-key key 'mwheel-scroll)
266 (push key mwheel-installed-bindings)))))
267
268 ;;; Compatibility entry point
269 ;;;###autoload
270 (defun mwheel-install (&optional uninstall)
271 "Enable mouse wheel support."
272 (mouse-wheel-mode (if uninstall -1 1)))
273
274 (provide 'mwheel)
275
276 ;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f
277 ;;; mwheel.el ends here