1 ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice
3 ;; Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
4 ;; Maintainer: William M. Perry <wmperry@gnu.org>
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)
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.
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
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 (set-default var button
)
50 (when mouse-wheel-mode
52 (mouse-wheel-mode 1)))
54 (defcustom mouse-wheel-down-button
4
55 "Mouse button number for scrolling down."
58 :set
'mouse-wheel-change-button
)
60 (defcustom mouse-wheel-up-button
5
61 "Mouse button number for scrolling up."
64 :set
'mouse-wheel-change-button
)
66 (defcustom mouse-wheel-scroll-amount
'(5 ((shift) .
1) ((control) . nil
))
67 "Amount to scroll windows by when spinning the mouse wheel.
68 This is actually a cons cell, where the first item is the amount to scroll
69 on a normal wheel event, and the rest is an alist mapping the modifier key
70 to the amount to scroll when the wheel is moved with the modifier key depressed.
72 Each item should be the number of lines to scroll, or `nil' for near
73 full screen. It can also be a floating point number, specifying
74 the fraction of the window to scroll.
75 A near full screen is `next-screen-context-lines' less than a full screen."
79 (const :tag
"Full screen" :value nil
)
80 (integer :tag
"Specific # of lines")
81 (float :tag
"Fraction of window"))
84 (repeat (choice :tag
"modifier" (const alt
) (const control
) (const hyper
)
85 (const meta
) (const shift
) (const super
)))
86 (choice :tag
"scroll amount"
87 (const :tag
"Full screen" :value nil
)
88 (integer :tag
"Specific # of lines")
89 (float :tag
"Fraction of window"))))))
91 (defcustom mouse-wheel-progessive-speed t
92 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
93 Note that this has no effect when `mouse-wheel-scroll-amount' specifies
94 a \"near full screen\" scroll."
98 (defcustom mouse-wheel-follow-mouse nil
99 "Whether the mouse wheel should scroll the window that the mouse is over.
100 This can be slightly disconcerting, but some people may prefer it."
104 (if (not (fboundp 'event-button
))
105 (defun mwheel-event-button (event)
106 (let ((x (symbol-name (event-basic-type event
))))
107 (if (not (string-match "^mouse-\\([0-9]+\\)" x
))
108 (error "Not a button event: %S" event
))
109 (string-to-int (substring x
(match-beginning 1) (match-end 1)))))
110 (fset 'mwheel-event-button
'event-button
))
112 (if (not (fboundp 'event-window
))
113 (defun mwheel-event-window (event)
114 (posn-window (event-start event
)))
115 (fset 'mwheel-event-window
'event-window
))
117 (defun mwheel-scroll (event)
118 "Scroll up or down according to the EVENT.
119 This should only be bound to mouse buttons 4 and 5."
121 (let* ((curwin (if mouse-wheel-follow-mouse
124 (select-window (mwheel-event-window event
)))))
125 (mods (delete 'click
(event-modifiers event
)))
127 (cdr (assoc mods
(cdr mouse-wheel-scroll-amount
)))
128 (car mouse-wheel-scroll-amount
))))
129 (if (floatp amt
) (setq amt
(1+ (truncate (* amt
(window-height))))))
130 (when (and mouse-wheel-progessive-speed
(numberp amt
))
131 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
132 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 16, ...).
133 (setq amt
(* amt
(event-click-count event
))))
135 (let ((button (mwheel-event-button event
)))
136 (cond ((= button mouse-wheel-down-button
) (scroll-down amt
))
137 ((= button mouse-wheel-up-button
) (scroll-up amt
))
138 (t (error "Bad binding in mwheel-scroll"))))
139 (if curwin
(select-window curwin
)))))
143 (define-minor-mode mouse-wheel-mode
144 "Toggle mouse wheel support.
145 With prefix argument ARG, turn on if positive, otherwise off.
146 Returns non-nil if the new state is enabled."
149 ;; In the latest versions of XEmacs, we could just use
150 ;; (S-)*mouse-[45], since those are aliases for the button
151 ;; equivalents in XEmacs, but I want this to work in as many
152 ;; versions of XEmacs as it can.
153 (let* ((prefix (if (featurep 'xemacs
) "button%d" "mouse-%d"))
154 (dn (intern (format prefix mouse-wheel-down-button
)))
155 (up (intern (format prefix mouse-wheel-up-button
)))
157 (nconc (list (vector dn
) (vector up
))
158 (mapcar (lambda (amt) `[(,@(car amt
) ,up
)])
159 (cdr mouse-wheel-scroll-amount
))
160 (mapcar (lambda (amt) `[(,@(car amt
) ,dn
)])
161 (cdr mouse-wheel-scroll-amount
)))))
162 ;; This condition-case is here because Emacs 19 will throw an error
163 ;; if you try to define a key that it does not know about. I for one
164 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
165 ;; that if the wheeled-mouse is there, it just works, and this way it
166 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
169 (cond (mouse-wheel-mode
170 (define-key global-map key
'mwheel-scroll
))
171 ((eq (lookup-key global-map key
) 'mwheel-scroll
)
172 (define-key global-map key nil
))))
175 ;;; Compatibility entry point
177 (defun mwheel-install (&optional uninstall
)
178 "Enable mouse wheel support."
179 (mouse-wheel-mode t
))
183 ;;; mwheel.el ends here