(math-trig-rewrite, math-hyperbolic-trig-rewrite): New functions.
[emacs.git] / lisp / mwheel.el
blob37980279a28d54c9c21fbe5319b59f95a6013e7f
1 ;;; mwheel.el --- Wheel mouse support
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
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
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.
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.
34 ;; To enable this code, simply put this at the top of your .emacs
35 ;; file:
37 ;; (mouse-wheel-mode 1)
39 ;;; Code:
41 (require 'custom)
42 (require 'timer)
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.
48 (defun mouse-wheel-change-button (var button)
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))))
55 (defvar mouse-wheel-down-button 4)
56 (make-obsolete-variable 'mouse-wheel-down-button
57 'mouse-wheel-down-event
58 "22.1")
59 (defcustom mouse-wheel-down-event
60 (if (or (featurep 'w32-win) (featurep 'ns-win))
61 'wheel-up
62 (intern (format "mouse-%s" mouse-wheel-down-button)))
63 "Event used for scrolling down."
64 :group 'mouse
65 :type 'symbol
66 :set 'mouse-wheel-change-button)
68 (defvar mouse-wheel-up-button 5)
69 (make-obsolete-variable 'mouse-wheel-up-button
70 'mouse-wheel-up-event
71 "22.1")
72 (defcustom mouse-wheel-up-event
73 (if (or (featurep 'w32-win) (featurep 'ns-win))
74 'wheel-down
75 (intern (format "mouse-%s" mouse-wheel-up-button)))
76 "Event used for scrolling up."
77 :group 'mouse
78 :type 'symbol
79 :set 'mouse-wheel-change-button)
81 (defvar mouse-wheel-click-button 2)
82 (make-obsolete-variable 'mouse-wheel-click-button
83 'mouse-wheel-click-event
84 "22.1")
85 (defcustom mouse-wheel-click-event
86 (intern (format "mouse-%s" 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 accidentally 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)
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
99 :type 'number)
101 (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
102 "Amount to scroll windows by when spinning the mouse wheel.
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.
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."
112 :group 'mouse
113 :type '(cons
114 (choice :tag "Normal"
115 (const :tag "Full screen" :value nil)
116 (integer :tag "Specific # of lines")
117 (float :tag "Fraction of window")
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")
125 (float :tag "Fraction of window"))))
126 (repeat
127 (cons
128 (repeat (choice :tag "modifier"
129 (const alt) (const control) (const hyper)
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")
134 (float :tag "Fraction of window"))))))
136 (defcustom mouse-wheel-progressive-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
139 a \"near full screen\" scroll or when the mouse wheel sends key instead
140 of button events."
141 :group 'mouse
142 :type 'boolean)
144 (defcustom mouse-wheel-follow-mouse t
145 "Whether the mouse wheel should scroll the window that the mouse is over.
146 This can be slightly disconcerting, but some people prefer it."
147 :group 'mouse
148 :type 'boolean)
150 (eval-and-compile
151 (if (fboundp 'event-button)
152 (fset 'mwheel-event-button 'event-button)
153 (defun mwheel-event-button (event)
154 (let ((x (event-basic-type event)))
155 ;; Map mouse-wheel events to appropriate buttons
156 (if (eq 'mouse-wheel x)
157 (let ((amount (car (cdr (cdr (cdr event))))))
158 (if (< amount 0)
159 mouse-wheel-up-event
160 mouse-wheel-down-event))
161 x))))
163 (if (fboundp 'event-window)
164 (fset 'mwheel-event-window 'event-window)
165 (defun mwheel-event-window (event)
166 (posn-window (event-start event)))))
168 (defvar mwheel-inhibit-click-event-timer nil
169 "Timer running while mouse wheel click event is inhibited.")
171 (defun mwheel-inhibit-click-timeout ()
172 "Handler for `mwheel-inhibit-click-event-timer'."
173 (setq mwheel-inhibit-click-event-timer nil)
174 (remove-hook 'pre-command-hook 'mwheel-filter-click-events))
176 (defun mwheel-filter-click-events ()
177 "Discard `mouse-wheel-click-event' while scrolling the mouse."
178 (if (eq (event-basic-type last-input-event) mouse-wheel-click-event)
179 (setq this-command 'ignore)))
181 (defun mwheel-scroll (event)
182 "Scroll up or down according to the EVENT.
183 This should only be bound to mouse buttons 4 and 5."
184 (interactive (list last-input-event))
185 (let* ((curwin (if mouse-wheel-follow-mouse
186 (prog1
187 (selected-window)
188 (select-window (mwheel-event-window event)))))
189 (buffer (window-buffer curwin))
190 (opoint (with-current-buffer buffer
191 (when (eq (car-safe transient-mark-mode) 'only)
192 (point))))
193 (mods
194 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
195 (amt (assoc mods mouse-wheel-scroll-amount)))
196 ;; Extract the actual amount or find the element that has no modifiers.
197 (if amt (setq amt (cdr amt))
198 (let ((list-elt mouse-wheel-scroll-amount))
199 (while (consp (setq amt (pop list-elt))))))
200 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
201 (when (and mouse-wheel-progressive-speed (numberp amt))
202 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
203 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
204 (setq amt (* amt (event-click-count event))))
205 (unwind-protect
206 (let ((button (mwheel-event-button event)))
207 (cond ((eq button mouse-wheel-down-event)
208 (condition-case nil (scroll-down amt)
209 ;; Make sure we do indeed scroll to the beginning of
210 ;; the buffer.
211 (beginning-of-buffer
212 (unwind-protect
213 (scroll-down)
214 ;; If the first scroll succeeded, then some scrolling
215 ;; is possible: keep scrolling til the beginning but
216 ;; do not signal an error. For some reason, we have
217 ;; to do it even if the first scroll signaled an
218 ;; error, because otherwise the window is recentered
219 ;; for a reason that escapes me. This problem seems
220 ;; to only affect scroll-down. --Stef
221 (set-window-start (selected-window) (point-min))))))
222 ((eq button mouse-wheel-up-event)
223 (condition-case nil (scroll-up amt)
224 ;; Make sure we do indeed scroll to the end of the buffer.
225 (end-of-buffer (while t (scroll-up)))))
226 (t (error "Bad binding in mwheel-scroll"))))
227 (if curwin (select-window curwin)))
228 ;; If there is a temporarily active region, deactivate it iff
229 ;; scrolling moves point.
230 (when opoint
231 (with-current-buffer buffer
232 (when (/= opoint (point))
233 (deactivate-mark)))))
234 (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
235 (if mwheel-inhibit-click-event-timer
236 (cancel-timer mwheel-inhibit-click-event-timer)
237 (add-hook 'pre-command-hook 'mwheel-filter-click-events))
238 (setq mwheel-inhibit-click-event-timer
239 (run-with-timer mouse-wheel-inhibit-click-time nil
240 'mwheel-inhibit-click-timeout))))
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 :global t
248 :group 'mouse
249 (let* ((dn mouse-wheel-down-event)
250 (up mouse-wheel-up-event)
251 (keys
252 (nconc (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,up)])
253 mouse-wheel-scroll-amount)
254 (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,dn)])
255 mouse-wheel-scroll-amount))))
256 ;; This condition-case is here because Emacs 19 will throw an error
257 ;; if you try to define a key that it does not know about. I for one
258 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
259 ;; that if the wheeled-mouse is there, it just works, and this way it
260 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
261 (condition-case ()
262 (dolist (key keys)
263 (cond (mouse-wheel-mode
264 (global-set-key key 'mwheel-scroll))
265 ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
266 (global-unset-key key))))
267 (error nil))))
269 ;;; Compatibility entry point
270 ;;;###autoload
271 (defun mwheel-install (&optional uninstall)
272 "Enable mouse wheel support."
273 (mouse-wheel-mode (if uninstall -1 1)))
275 (provide 'mwheel)
277 ;; arch-tag: 50ed00e7-3686-4b7a-8037-fb31aa5c237f
278 ;;; mwheel.el ends here