Commit | Line | Data |
---|---|---|
a32b7419 WP |
1 | ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice |
2 | ||
bf85004b | 3 | ;; Copyright (C) 1998, 2000, 2001 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) | |
49 | (set-default var button) | |
50 | (when mouse-wheel-mode | |
51 | (mouse-wheel-mode 0) | |
52 | (mouse-wheel-mode 1))) | |
53 | ||
54 | (defcustom mouse-wheel-down-button 4 | |
55 | "Mouse button number for scrolling down." | |
56 | :group 'mouse | |
57 | :type 'integer | |
58 | :set 'mouse-wheel-change-button) | |
59 | ||
60 | (defcustom mouse-wheel-up-button 5 | |
61 | "Mouse button number for scrolling up." | |
62 | :group 'mouse | |
63 | :type 'integer | |
64 | :set 'mouse-wheel-change-button) | |
65 | ||
937b2877 | 66 | (defcustom mouse-wheel-scroll-amount '(5 . 1) |
a32b7419 WP |
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 second is the amount to scroll when the | |
70 | wheel is moved with the shift key depressed. | |
71 | ||
72 | Each item should be the number of lines to scroll, or `nil' for near | |
d3b32bfd SM |
73 | full screen. It can also be a floating point number, specifying |
74 | the fraction of the window to scroll. | |
a32b7419 WP |
75 | A near full screen is `next-screen-context-lines' less than a full screen." |
76 | :group 'mouse | |
77 | :type '(cons | |
78 | (choice :tag "Normal" | |
79 | (const :tag "Full screen" :value nil) | |
d3b32bfd SM |
80 | (integer :tag "Specific # of lines") |
81 | (float :tag "Fraction of window")) | |
a32b7419 WP |
82 | (choice :tag "Shifted" |
83 | (const :tag "Full screen" :value nil) | |
d3b32bfd SM |
84 | (integer :tag "Specific # of lines") |
85 | (float :tag "Fraction of window")))) | |
86 | ||
87 | (defcustom mouse-wheel-progessive-speed t | |
88 | "If non-nil, the faster the user moves the wheel, the faster the scrolling. | |
89 | Note that this has no effect when `mouse-wheel-scroll-amount' specifies | |
90 | a \"near full screen\" scroll." | |
91 | :group 'mouse | |
92 | :type 'boolean) | |
a32b7419 | 93 | |
937b2877 | 94 | (defcustom mouse-wheel-follow-mouse nil |
a32b7419 WP |
95 | "Whether the mouse wheel should scroll the window that the mouse is over. |
96 | This can be slightly disconcerting, but some people may prefer it." | |
97 | :group 'mouse | |
98 | :type 'boolean) | |
99 | ||
100 | (if (not (fboundp 'event-button)) | |
101 | (defun mwheel-event-button (event) | |
102 | (let ((x (symbol-name (event-basic-type event)))) | |
103 | (if (not (string-match "^mouse-\\([0-9]+\\)" x)) | |
104 | (error "Not a button event: %S" event)) | |
105 | (string-to-int (substring x (match-beginning 1) (match-end 1))))) | |
106 | (fset 'mwheel-event-button 'event-button)) | |
107 | ||
108 | (if (not (fboundp 'event-window)) | |
109 | (defun mwheel-event-window (event) | |
110 | (posn-window (event-start event))) | |
111 | (fset 'mwheel-event-window 'event-window)) | |
112 | ||
113 | (defun mwheel-scroll (event) | |
d3b32bfd SM |
114 | "Scroll up or down according to the EVENT. |
115 | This should only be bound to mouse buttons 4 and 5." | |
a32b7419 | 116 | (interactive "e") |
937b2877 | 117 | (let ((curwin (if mouse-wheel-follow-mouse |
a32b7419 WP |
118 | (prog1 |
119 | (selected-window) | |
120 | (select-window (mwheel-event-window event))))) | |
121 | (amt (if (memq 'shift (event-modifiers event)) | |
937b2877 MB |
122 | (cdr mouse-wheel-scroll-amount) |
123 | (car mouse-wheel-scroll-amount)))) | |
d3b32bfd SM |
124 | (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height)))))) |
125 | (when (and mouse-wheel-progessive-speed (numberp amt)) | |
126 | ;; When the double-mouse-N comes in, a mouse-N has been executed already, | |
127 | ;; So by adding things up we get a squaring up (1, 3, 6, 10, 16, ...). | |
128 | (setq amt (* amt (event-click-count event)))) | |
a32b7419 | 129 | (unwind-protect |
8ed8f294 | 130 | (let ((button (mwheel-event-button event))) |
bf85004b GM |
131 | (cond ((= button mouse-wheel-down-button) (scroll-down amt)) |
132 | ((= button mouse-wheel-up-button) (scroll-up amt)) | |
8ed8f294 | 133 | (t (error "Bad binding in mwheel-scroll")))) |
a32b7419 WP |
134 | (if curwin (select-window curwin))))) |
135 | ||
f4cbc7a0 | 136 | |
a32b7419 | 137 | ;;;###autoload |
f4cbc7a0 MB |
138 | (define-minor-mode mouse-wheel-mode |
139 | "Toggle mouse wheel support. | |
140 | With prefix argument ARG, turn on if positive, otherwise off. | |
141 | Returns non-nil if the new state is enabled." | |
f4cbc7a0 MB |
142 | :global t |
143 | :group 'mouse | |
a32b7419 WP |
144 | ;; In the latest versions of XEmacs, we could just use |
145 | ;; (S-)*mouse-[45], since those are aliases for the button | |
146 | ;; equivalents in XEmacs, but I want this to work in as many | |
147 | ;; versions of XEmacs as it can. | |
937b2877 MB |
148 | (let ((keys |
149 | (if (featurep 'xemacs) | |
bf85004b GM |
150 | (let ((down (intern (format "button%d" mouse-wheel-down-button))) |
151 | (up (intern (format "button%d" mouse-wheel-up-button)))) | |
152 | `(,down [(shift ,down)] ,up [(shift ,up)])) | |
153 | (let ((down (intern (format "mouse-%d" mouse-wheel-down-button))) | |
154 | (s-down (intern (format "S-mouse-%d" mouse-wheel-down-button))) | |
155 | (up (intern (format "mouse-%d" mouse-wheel-up-button))) | |
156 | (s-up (intern (format "S-mouse-%d" mouse-wheel-up-button)))) | |
157 | `([,down] [,s-down] [,up] [,s-up]))))) | |
a32b7419 WP |
158 | ;; This condition-case is here because Emacs 19 will throw an error |
159 | ;; if you try to define a key that it does not know about. I for one | |
160 | ;; prefer to just unconditionally do a mwheel-install in my .emacs, so | |
161 | ;; that if the wheeled-mouse is there, it just works, and this way it | |
162 | ;; doesn't yell at me if I'm on my laptop or another machine, etc. | |
163 | (condition-case () | |
f4cbc7a0 MB |
164 | (dolist (key keys) |
165 | (cond (mouse-wheel-mode | |
166 | (define-key global-map key 'mwheel-scroll)) | |
167 | ((eq (lookup-key global-map key) 'mwheel-scroll) | |
168 | (define-key global-map key nil)))) | |
a32b7419 | 169 | (error nil)))) |
f4cbc7a0 MB |
170 | |
171 | ;;; Compatibility entry point | |
172 | ;;;###autoload | |
173 | (defun mwheel-install (&optional uninstall) | |
174 | "Enable mouse wheel support." | |
175 | (mouse-wheel-mode t)) | |
176 | ||
a32b7419 WP |
177 | (provide 'mwheel) |
178 | ||
179 | ;;; mwheel.el ends here |