(Auto Save Files): Mention auto-save-file-name-transforms.
[emacs.git] / lisp / mwheel.el
blob5b04b9ba991e146b3dcd9040f37cba038cf1d89a
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>
5 ;; Keywords: mouse
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)
12 ;; any later version.
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.
24 ;;; Commentary:
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
36 ;; file:
38 ;; (mwheel-install)
40 ;;; Code:
42 (require 'custom)
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
51 (mouse-wheel-mode 0)
52 (mouse-wheel-mode 1)))
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)
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)
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."
76 :group 'mouse
77 :type '(cons
78 (choice :tag "Normal"
79 (const :tag "Full screen" :value nil)
80 (integer :tag "Specific # of lines")
81 (float :tag "Fraction of window"))
82 (repeat
83 (cons
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."
95 :group 'mouse
96 :type 'boolean)
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."
101 :group 'mouse
102 :type 'boolean)
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."
120 (interactive "e")
121 (let* ((curwin (if mouse-wheel-follow-mouse
122 (prog1
123 (selected-window)
124 (select-window (mwheel-event-window event)))))
125 (mods
126 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
127 (amt
128 (or (and mods
129 (cdr (assoc mods (cdr mouse-wheel-scroll-amount))))
130 (car mouse-wheel-scroll-amount))))
131 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
132 (when (and mouse-wheel-progessive-speed (numberp amt))
133 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
134 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 16, ...).
135 (setq amt (* amt (event-click-count event))))
136 (unwind-protect
137 (let ((button (mwheel-event-button event)))
138 (cond ((= button mouse-wheel-down-button) (scroll-down amt))
139 ((= button mouse-wheel-up-button) (scroll-up amt))
140 (t (error "Bad binding in mwheel-scroll"))))
141 (if curwin (select-window curwin)))))
144 ;;;###autoload
145 (define-minor-mode mouse-wheel-mode
146 "Toggle mouse wheel support.
147 With prefix argument ARG, turn on if positive, otherwise off.
148 Returns non-nil if the new state is enabled."
149 :global t
150 :group 'mouse
151 ;; In the latest versions of XEmacs, we could just use
152 ;; (S-)*mouse-[45], since those are aliases for the button
153 ;; equivalents in XEmacs, but I want this to work in as many
154 ;; versions of XEmacs as it can.
155 (let* ((prefix (if (featurep 'xemacs) "button%d" "mouse-%d"))
156 (dn (intern (format prefix mouse-wheel-down-button)))
157 (up (intern (format prefix mouse-wheel-up-button)))
158 (keys
159 (nconc (list (vector dn) (vector up))
160 (mapcar (lambda (amt) `[(,@(car amt) ,up)])
161 (cdr mouse-wheel-scroll-amount))
162 (mapcar (lambda (amt) `[(,@(car amt) ,dn)])
163 (cdr mouse-wheel-scroll-amount)))))
164 ;; This condition-case is here because Emacs 19 will throw an error
165 ;; if you try to define a key that it does not know about. I for one
166 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
167 ;; that if the wheeled-mouse is there, it just works, and this way it
168 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
169 (condition-case ()
170 (dolist (key keys)
171 (cond (mouse-wheel-mode
172 (global-set-key key 'mwheel-scroll))
173 ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
174 (global-unset-key key))))
175 (error nil))))
177 ;;; Compatibility entry point
178 ;;;###autoload
179 (defun mwheel-install (&optional uninstall)
180 "Enable mouse wheel support."
181 (mouse-wheel-mode t))
183 (provide 'mwheel)
185 ;;; mwheel.el ends here