Commit | Line | Data |
---|---|---|
a32b7419 WP |
1 | ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice |
2 | ||
937b2877 | 3 | ;; Copyright (C) 1998, 2000, 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 | |
937b2877 | 44 | (defcustom mouse-wheel-scroll-amount '(5 . 1) |
a32b7419 WP |
45 | "Amount to scroll windows by when spinning the mouse wheel. |
46 | This is actually a cons cell, where the first item is the amount to scroll | |
47 | on a normal wheel event, and the second is the amount to scroll when the | |
48 | wheel is moved with the shift key depressed. | |
49 | ||
50 | Each item should be the number of lines to scroll, or `nil' for near | |
51 | full screen. | |
52 | A near full screen is `next-screen-context-lines' less than a full screen." | |
53 | :group 'mouse | |
54 | :type '(cons | |
55 | (choice :tag "Normal" | |
56 | (const :tag "Full screen" :value nil) | |
57 | (integer :tag "Specific # of lines")) | |
58 | (choice :tag "Shifted" | |
59 | (const :tag "Full screen" :value nil) | |
60 | (integer :tag "Specific # of lines")))) | |
61 | ||
937b2877 | 62 | (defcustom mouse-wheel-follow-mouse nil |
a32b7419 WP |
63 | "Whether the mouse wheel should scroll the window that the mouse is over. |
64 | This can be slightly disconcerting, but some people may prefer it." | |
65 | :group 'mouse | |
66 | :type 'boolean) | |
67 | ||
68 | (if (not (fboundp 'event-button)) | |
69 | (defun mwheel-event-button (event) | |
70 | (let ((x (symbol-name (event-basic-type event)))) | |
71 | (if (not (string-match "^mouse-\\([0-9]+\\)" x)) | |
72 | (error "Not a button event: %S" event)) | |
73 | (string-to-int (substring x (match-beginning 1) (match-end 1))))) | |
74 | (fset 'mwheel-event-button 'event-button)) | |
75 | ||
76 | (if (not (fboundp 'event-window)) | |
77 | (defun mwheel-event-window (event) | |
78 | (posn-window (event-start event))) | |
79 | (fset 'mwheel-event-window 'event-window)) | |
80 | ||
81 | (defun mwheel-scroll (event) | |
82 | (interactive "e") | |
937b2877 | 83 | (let ((curwin (if mouse-wheel-follow-mouse |
a32b7419 WP |
84 | (prog1 |
85 | (selected-window) | |
86 | (select-window (mwheel-event-window event))))) | |
87 | (amt (if (memq 'shift (event-modifiers event)) | |
937b2877 MB |
88 | (cdr mouse-wheel-scroll-amount) |
89 | (car mouse-wheel-scroll-amount)))) | |
a32b7419 | 90 | (unwind-protect |
8ed8f294 GM |
91 | (let ((button (mwheel-event-button event))) |
92 | (cond ((= button 4) (scroll-down amt)) | |
93 | ((= button 5) (scroll-up amt)) | |
94 | (t (error "Bad binding in mwheel-scroll")))) | |
a32b7419 WP |
95 | (if curwin (select-window curwin))))) |
96 | ||
f4cbc7a0 MB |
97 | |
98 | ;;; Note this definition must be at the end of the file, because | |
99 | ;;; `define-minor-mode' actually calls the mode-function if the | |
100 | ;;; associated variable is non-nil, which requires that all needed | |
7ef86b07 | 101 | ;;; functions be already defined. |
a32b7419 | 102 | ;;;###autoload |
f4cbc7a0 MB |
103 | (define-minor-mode mouse-wheel-mode |
104 | "Toggle mouse wheel support. | |
105 | With prefix argument ARG, turn on if positive, otherwise off. | |
106 | Returns non-nil if the new state is enabled." | |
f4cbc7a0 MB |
107 | :global t |
108 | :group 'mouse | |
a32b7419 WP |
109 | ;; In the latest versions of XEmacs, we could just use |
110 | ;; (S-)*mouse-[45], since those are aliases for the button | |
111 | ;; equivalents in XEmacs, but I want this to work in as many | |
112 | ;; versions of XEmacs as it can. | |
937b2877 MB |
113 | (let ((keys |
114 | (if (featurep 'xemacs) | |
115 | '(button4 [(shift button4)] button5 [(shift button5)]) | |
116 | '([mouse-4] [S-mouse-4] [mouse-5] [S-mouse-5])))) | |
a32b7419 WP |
117 | ;; This condition-case is here because Emacs 19 will throw an error |
118 | ;; if you try to define a key that it does not know about. I for one | |
119 | ;; prefer to just unconditionally do a mwheel-install in my .emacs, so | |
120 | ;; that if the wheeled-mouse is there, it just works, and this way it | |
121 | ;; doesn't yell at me if I'm on my laptop or another machine, etc. | |
122 | (condition-case () | |
f4cbc7a0 MB |
123 | (dolist (key keys) |
124 | (cond (mouse-wheel-mode | |
125 | (define-key global-map key 'mwheel-scroll)) | |
126 | ((eq (lookup-key global-map key) 'mwheel-scroll) | |
127 | (define-key global-map key nil)))) | |
a32b7419 | 128 | (error nil)))) |
f4cbc7a0 MB |
129 | |
130 | ;;; Compatibility entry point | |
131 | ;;;###autoload | |
132 | (defun mwheel-install (&optional uninstall) | |
133 | "Enable mouse wheel support." | |
134 | (mouse-wheel-mode t)) | |
135 | ||
136 | ||
a32b7419 WP |
137 | (provide 'mwheel) |
138 | ||
139 | ;;; mwheel.el ends here |