(unload-feature): When undefining a variable, delete its buffer-local bindings.
[emacs.git] / lisp / mwheel.el
blobb2c0c0bddabab8a7cd32dccc1e820543ebd06b79
1 ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice
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 ;; (mwheel-install)
40 ;;; Code:
42 (require 'custom)
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 (defcustom mouse-wheel-down-button 4
56 "Obsolete. Use `mouse-wheel-down-event'.")
57 (defcustom mouse-wheel-down-event
58 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
59 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
60 mouse-wheel-down-button))
61 "Event used for scrolling down."
62 :group 'mouse
63 :type 'symbol
64 :set 'mouse-wheel-change-button)
66 (defcustom mouse-wheel-up-button 5
67 "Obsolete. Use `mouse-wheel-up-event'.")
68 (defcustom mouse-wheel-up-event
69 ;; In the latest versions of XEmacs, we could just use mouse-%s as well.
70 (intern (format (if (featurep 'xemacs) "button%s" "mouse-%s")
71 mouse-wheel-up-button))
72 "Event used for scrolling down."
73 :group 'mouse
74 :type 'symbol
75 :set 'mouse-wheel-change-button)
77 (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil))
78 "Amount to scroll windows by when spinning the mouse wheel.
79 This is an alist mapping the modifier key to the amount to scroll when
80 the wheel is moved with the modifier key depressed.
81 Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
82 MODIFIERS is nil.
84 AMOUNT should be the number of lines to scroll, or `nil' for near full
85 screen. It can also be a floating point number, specifying the fraction of
86 a full screen to scroll. A near full screen is `next-screen-context-lines'
87 less than a full screen."
88 :group 'mouse
89 :type '(cons
90 (choice :tag "Normal"
91 (const :tag "Full screen" :value nil)
92 (integer :tag "Specific # of lines")
93 (float :tag "Fraction of window")
94 (cons
95 (repeat (choice :tag "modifier"
96 (const alt) (const control) (const hyper)
97 (const meta) (const shift) (const super)))
98 (choice :tag "scroll amount"
99 (const :tag "Full screen" :value nil)
100 (integer :tag "Specific # of lines")
101 (float :tag "Fraction of window"))))
102 (repeat
103 (cons
104 (repeat (choice :tag "modifier"
105 (const alt) (const control) (const hyper)
106 (const meta) (const shift) (const super)))
107 (choice :tag "scroll amount"
108 (const :tag "Full screen" :value nil)
109 (integer :tag "Specific # of lines")
110 (float :tag "Fraction of window"))))))
112 (defcustom mouse-wheel-progessive-speed t
113 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
114 Note that this has no effect when `mouse-wheel-scroll-amount' specifies
115 a \"near full screen\" scroll or when the mouse wheel sends key instead
116 of button events."
117 :group 'mouse
118 :type 'boolean)
120 (defcustom mouse-wheel-follow-mouse t
121 "Whether the mouse wheel should scroll the window that the mouse is over.
122 This can be slightly disconcerting, but some people prefer it."
123 :group 'mouse
124 :type 'boolean)
126 (if (not (fboundp 'event-button))
127 (defun mwheel-event-button (event)
128 (let ((x (event-basic-type event)))
129 ;; Map mouse-wheel events to appropriate buttons
130 (if (eq 'mouse-wheel x)
131 (let ((amount (car (cdr (cdr (cdr event))))))
132 (if (< amount 0)
133 mouse-wheel-up-event
134 mouse-wheel-down-event))
135 x)))
136 (fset 'mwheel-event-button 'event-button))
138 (if (not (fboundp 'event-window))
139 (defun mwheel-event-window (event)
140 (posn-window (event-start event)))
141 (fset 'mwheel-event-window 'event-window))
143 (defun mwheel-scroll (event)
144 "Scroll up or down according to the EVENT.
145 This should only be bound to mouse buttons 4 and 5."
146 (interactive (list last-input-event))
147 (let* ((curwin (if mouse-wheel-follow-mouse
148 (prog1
149 (selected-window)
150 (select-window (mwheel-event-window event)))))
151 (mods
152 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
153 (amt (assoc mods mouse-wheel-scroll-amount)))
154 ;; Extract the actual amount or find the element that has no modifiers.
155 (if amt (setq amt (cdr amt))
156 (let ((list-elt mouse-wheel-scroll-amount))
157 (while (consp (setq amt (pop list-elt))))))
158 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
159 (when (and mouse-wheel-progessive-speed (numberp amt))
160 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
161 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
162 (setq amt (* amt (event-click-count event))))
163 (unwind-protect
164 (let ((button (mwheel-event-button event)))
165 (cond ((eq button mouse-wheel-down-event) (scroll-down amt))
166 ((eq button mouse-wheel-up-event) (scroll-up amt))
167 (t (error "Bad binding in mwheel-scroll"))))
168 (if curwin (select-window curwin)))))
171 ;;;###autoload
172 (define-minor-mode mouse-wheel-mode
173 "Toggle mouse wheel support.
174 With prefix argument ARG, turn on if positive, otherwise off.
175 Returns non-nil if the new state is enabled."
176 :global t
177 :group 'mouse
178 (let* ((dn mouse-wheel-down-event)
179 (up mouse-wheel-up-event)
180 (keys
181 (nconc (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,up)])
182 mouse-wheel-scroll-amount)
183 (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,dn)])
184 mouse-wheel-scroll-amount))))
185 ;; This condition-case is here because Emacs 19 will throw an error
186 ;; if you try to define a key that it does not know about. I for one
187 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
188 ;; that if the wheeled-mouse is there, it just works, and this way it
189 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
190 (condition-case ()
191 (dolist (key keys)
192 (cond (mouse-wheel-mode
193 (global-set-key key 'mwheel-scroll))
194 ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
195 (global-unset-key key))))
196 (error nil))))
198 ;;; Compatibility entry point
199 ;;;###autoload
200 (defun mwheel-install (&optional uninstall)
201 "Enable mouse wheel support."
202 (mouse-wheel-mode (if uninstall -1 1)))
204 (provide 'mwheel)
206 ;;; mwheel.el ends here