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 | |
73 | full screen. | |
74 | A near full screen is `next-screen-context-lines' less than a full screen." | |
75 | :group 'mouse | |
76 | :type '(cons | |
77 | (choice :tag "Normal" | |
78 | (const :tag "Full screen" :value nil) | |
79 | (integer :tag "Specific # of lines")) | |
80 | (choice :tag "Shifted" | |
81 | (const :tag "Full screen" :value nil) | |
82 | (integer :tag "Specific # of lines")))) | |
83 | ||
937b2877 | 84 | (defcustom mouse-wheel-follow-mouse nil |
a32b7419 WP |
85 | "Whether the mouse wheel should scroll the window that the mouse is over. |
86 | This can be slightly disconcerting, but some people may prefer it." | |
87 | :group 'mouse | |
88 | :type 'boolean) | |
89 | ||
90 | (if (not (fboundp 'event-button)) | |
91 | (defun mwheel-event-button (event) | |
92 | (let ((x (symbol-name (event-basic-type event)))) | |
93 | (if (not (string-match "^mouse-\\([0-9]+\\)" x)) | |
94 | (error "Not a button event: %S" event)) | |
95 | (string-to-int (substring x (match-beginning 1) (match-end 1))))) | |
96 | (fset 'mwheel-event-button 'event-button)) | |
97 | ||
98 | (if (not (fboundp 'event-window)) | |
99 | (defun mwheel-event-window (event) | |
100 | (posn-window (event-start event))) | |
101 | (fset 'mwheel-event-window 'event-window)) | |
102 | ||
103 | (defun mwheel-scroll (event) | |
104 | (interactive "e") | |
937b2877 | 105 | (let ((curwin (if mouse-wheel-follow-mouse |
a32b7419 WP |
106 | (prog1 |
107 | (selected-window) | |
108 | (select-window (mwheel-event-window event))))) | |
109 | (amt (if (memq 'shift (event-modifiers event)) | |
937b2877 MB |
110 | (cdr mouse-wheel-scroll-amount) |
111 | (car mouse-wheel-scroll-amount)))) | |
a32b7419 | 112 | (unwind-protect |
8ed8f294 | 113 | (let ((button (mwheel-event-button event))) |
bf85004b GM |
114 | (cond ((= button mouse-wheel-down-button) (scroll-down amt)) |
115 | ((= button mouse-wheel-up-button) (scroll-up amt)) | |
8ed8f294 | 116 | (t (error "Bad binding in mwheel-scroll")))) |
a32b7419 WP |
117 | (if curwin (select-window curwin))))) |
118 | ||
f4cbc7a0 MB |
119 | |
120 | ;;; Note this definition must be at the end of the file, because | |
121 | ;;; `define-minor-mode' actually calls the mode-function if the | |
122 | ;;; associated variable is non-nil, which requires that all needed | |
7ef86b07 | 123 | ;;; functions be already defined. |
a32b7419 | 124 | ;;;###autoload |
f4cbc7a0 MB |
125 | (define-minor-mode mouse-wheel-mode |
126 | "Toggle mouse wheel support. | |
127 | With prefix argument ARG, turn on if positive, otherwise off. | |
128 | Returns non-nil if the new state is enabled." | |
f4cbc7a0 MB |
129 | :global t |
130 | :group 'mouse | |
a32b7419 WP |
131 | ;; In the latest versions of XEmacs, we could just use |
132 | ;; (S-)*mouse-[45], since those are aliases for the button | |
133 | ;; equivalents in XEmacs, but I want this to work in as many | |
134 | ;; versions of XEmacs as it can. | |
937b2877 MB |
135 | (let ((keys |
136 | (if (featurep 'xemacs) | |
bf85004b GM |
137 | (let ((down (intern (format "button%d" mouse-wheel-down-button))) |
138 | (up (intern (format "button%d" mouse-wheel-up-button)))) | |
139 | `(,down [(shift ,down)] ,up [(shift ,up)])) | |
140 | (let ((down (intern (format "mouse-%d" mouse-wheel-down-button))) | |
141 | (s-down (intern (format "S-mouse-%d" mouse-wheel-down-button))) | |
142 | (up (intern (format "mouse-%d" mouse-wheel-up-button))) | |
143 | (s-up (intern (format "S-mouse-%d" mouse-wheel-up-button)))) | |
144 | `([,down] [,s-down] [,up] [,s-up]))))) | |
a32b7419 WP |
145 | ;; This condition-case is here because Emacs 19 will throw an error |
146 | ;; if you try to define a key that it does not know about. I for one | |
147 | ;; prefer to just unconditionally do a mwheel-install in my .emacs, so | |
148 | ;; that if the wheeled-mouse is there, it just works, and this way it | |
149 | ;; doesn't yell at me if I'm on my laptop or another machine, etc. | |
150 | (condition-case () | |
f4cbc7a0 MB |
151 | (dolist (key keys) |
152 | (cond (mouse-wheel-mode | |
153 | (define-key global-map key 'mwheel-scroll)) | |
154 | ((eq (lookup-key global-map key) 'mwheel-scroll) | |
155 | (define-key global-map key nil)))) | |
a32b7419 | 156 | (error nil)))) |
f4cbc7a0 MB |
157 | |
158 | ;;; Compatibility entry point | |
159 | ;;;###autoload | |
160 | (defun mwheel-install (&optional uninstall) | |
161 | "Enable mouse wheel support." | |
162 | (mouse-wheel-mode t)) | |
163 | ||
a32b7419 WP |
164 | (provide 'mwheel) |
165 | ||
166 | ;;; mwheel.el ends here |