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