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