Commit | Line | Data |
---|---|---|
a32b7419 WP |
1 | ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice |
2 | ||
bb5d43fe | 3 | ;; Copyright (C) 1998,2000,2001,2002 Free Software Foundation, Inc. |
a32b7419 WP |
4 | ;; Maintainer: William M. Perry <wmperry@gnu.org> |
5 | ;; Keywords: mouse | |
6 | ||
8ed8f294 | 7 | ;; This file is part of GNU Emacs. |
a32b7419 | 8 | |
8ed8f294 GM |
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 | |
a32b7419 WP |
11 | ;; the Free Software Foundation; either version 2, or (at your option) |
12 | ;; any later version. | |
13 | ||
8ed8f294 GM |
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. | |
a32b7419 WP |
18 | |
19 | ;; You should have received a copy of the GNU General Public License | |
8ed8f294 | 20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
a32b7419 WP |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
22 | ;; Boston, MA 02111-1307, USA. | |
23 | ||
a32b7419 WP |
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 | ;; | |
a32b7419 WP |
38 | ;; (mwheel-install) |
39 | ||
40 | ;;; Code: | |
41 | ||
42 | (require 'custom) | |
a32b7419 | 43 | |
bf85004b GM |
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. | |
47 | ||
48 | (defun mouse-wheel-change-button (var button) | |
bb5d43fe SM |
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)))) | |
bf85004b GM |
54 | |
55 | (defcustom mouse-wheel-down-button 4 | |
bb5d43fe SM |
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." | |
bf85004b | 62 | :group 'mouse |
bb5d43fe | 63 | :type 'symbol |
bf85004b GM |
64 | :set 'mouse-wheel-change-button) |
65 | ||
66 | (defcustom mouse-wheel-up-button 5 | |
bb5d43fe SM |
67 | "Obsolete. Use `mouse-whell-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." | |
bf85004b | 73 | :group 'mouse |
bb5d43fe | 74 | :type 'symbol |
bf85004b GM |
75 | :set 'mouse-wheel-change-button) |
76 | ||
141ddc20 | 77 | (defcustom mouse-wheel-scroll-amount '(5 ((shift) . 1) ((control) . nil)) |
a32b7419 | 78 | "Amount to scroll windows by when spinning the mouse wheel. |
eb4504e0 SM |
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. | |
83 | ||
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." | |
a32b7419 | 88 | :group 'mouse |
141ddc20 SM |
89 | :type '(cons |
90 | (choice :tag "Normal" | |
a32b7419 | 91 | (const :tag "Full screen" :value nil) |
141ddc20 | 92 | (integer :tag "Specific # of lines") |
eb4504e0 SM |
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")))) | |
141ddc20 SM |
102 | (repeat |
103 | (cons | |
eb4504e0 SM |
104 | (repeat (choice :tag "modifier" |
105 | (const alt) (const control) (const hyper) | |
141ddc20 SM |
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")))))) | |
111 | ||
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 | |
eb4504e0 SM |
115 | a \"near full screen\" scroll or when the mouse wheel sends key instead |
116 | of button events." | |
141ddc20 SM |
117 | :group 'mouse |
118 | :type 'boolean) | |
a32b7419 | 119 | |
bb5d43fe | 120 | (defcustom mouse-wheel-follow-mouse t |
a32b7419 | 121 | "Whether the mouse wheel should scroll the window that the mouse is over. |
eb4504e0 | 122 | This can be slightly disconcerting, but some people prefer it." |
a32b7419 WP |
123 | :group 'mouse |
124 | :type 'boolean) | |
125 | ||
141ddc20 SM |
126 | (if (not (fboundp 'event-button)) |
127 | (defun mwheel-event-button (event) | |
bb5d43fe | 128 | (let ((x (event-basic-type event))) |
141ddc20 | 129 | ;; Map mouse-wheel events to appropriate buttons |
bb5d43fe | 130 | (if (eq 'mouse-wheel x) |
141ddc20 SM |
131 | (let ((amount (car (cdr (cdr (cdr event)))))) |
132 | (if (< amount 0) | |
bb5d43fe SM |
133 | mouse-wheel-up-event |
134 | mouse-wheel-down-event)) | |
135 | x))) | |
141ddc20 SM |
136 | (fset 'mwheel-event-button 'event-button)) |
137 | ||
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)) | |
142 | ||
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." | |
bb5d43fe | 146 | (interactive (list last-input-event)) |
141ddc20 SM |
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))))) | |
eb4504e0 SM |
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)))))) | |
141ddc20 SM |
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, | |
bb5d43fe | 161 | ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...). |
141ddc20 | 162 | (setq amt (* amt (event-click-count event)))) |
a32b7419 | 163 | (unwind-protect |
141ddc20 | 164 | (let ((button (mwheel-event-button event))) |
bb5d43fe SM |
165 | (cond ((eq button mouse-wheel-down-event) (scroll-down amt)) |
166 | ((eq button mouse-wheel-up-event) (scroll-up amt)) | |
141ddc20 | 167 | (t (error "Bad binding in mwheel-scroll")))) |
a32b7419 WP |
168 | (if curwin (select-window curwin))))) |
169 | ||
f4cbc7a0 | 170 | |
a32b7419 | 171 | ;;;###autoload |
f4cbc7a0 MB |
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." | |
f4cbc7a0 MB |
176 | :global t |
177 | :group 'mouse | |
bb5d43fe SM |
178 | (let* ((dn mouse-wheel-down-event) |
179 | (up mouse-wheel-up-event) | |
141ddc20 | 180 | (keys |
eb4504e0 SM |
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)))) | |
141ddc20 SM |
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)))) | |
f4cbc7a0 MB |
197 | |
198 | ;;; Compatibility entry point | |
199 | ;;;###autoload | |
200 | (defun mwheel-install (&optional uninstall) | |
201 | "Enable mouse wheel support." | |
bb5d43fe | 202 | (mouse-wheel-mode (if uninstall -1 1))) |
f4cbc7a0 | 203 | |
a32b7419 WP |
204 | (provide 'mwheel) |
205 | ||
206 | ;;; mwheel.el ends here |