(f90-end-of-subprogram): Remove the final (forward-line 1).
[emacs.git] / lisp / mwheel.el
blob3fa83c27773c6e1930f15a31b1dc8a686f3c88ea
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 ;; Map mouse-wheel events to appropriate buttons
108 (if (string-equal "mouse-wheel" x)
109 (let ((amount (car (cdr (cdr (cdr event))))))
110 (if (< amount 0)
111 mouse-wheel-up-button
112 mouse-wheel-down-button))
113 (if (not (string-match "^mouse-\\([0-9]+\\)" x))
114 (error "Not a button event: %S" event)
115 (string-to-int (substring x (match-beginning 1) (match-end 1)))))))
116 (fset 'mwheel-event-button 'event-button))
118 (if (not (fboundp 'event-window))
119 (defun mwheel-event-window (event)
120 (posn-window (event-start event)))
121 (fset 'mwheel-event-window 'event-window))
123 (defun mwheel-scroll (event)
124 "Scroll up or down according to the EVENT.
125 This should only be bound to mouse buttons 4 and 5."
126 (interactive "e")
127 (let* ((curwin (if mouse-wheel-follow-mouse
128 (prog1
129 (selected-window)
130 (select-window (mwheel-event-window event)))))
131 (mods
132 (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
133 (amt
134 (if mods
135 (cdr (assoc mods (cdr mouse-wheel-scroll-amount)))
136 (car mouse-wheel-scroll-amount))))
137 (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
138 (when (and mouse-wheel-progessive-speed (numberp amt))
139 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
140 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 16, ...).
141 (setq amt (* amt (event-click-count event))))
142 (unwind-protect
143 (let ((button (mwheel-event-button event)))
144 (cond ((= button mouse-wheel-down-button) (scroll-down amt))
145 ((= button mouse-wheel-up-button) (scroll-up amt))
146 (t (error "Bad binding in mwheel-scroll"))))
147 (if curwin (select-window curwin)))))
150 ;;;###autoload
151 (define-minor-mode mouse-wheel-mode
152 "Toggle mouse wheel support.
153 With prefix argument ARG, turn on if positive, otherwise off.
154 Returns non-nil if the new state is enabled."
155 :global t
156 :group 'mouse
157 ;; In the latest versions of XEmacs, we could just use
158 ;; (S-)*mouse-[45], since those are aliases for the button
159 ;; equivalents in XEmacs, but I want this to work in as many
160 ;; versions of XEmacs as it can.
161 (let* ((prefix (if (featurep 'xemacs) "button%d" "mouse-%d"))
162 (dn (intern (format prefix mouse-wheel-down-button)))
163 (up (intern (format prefix mouse-wheel-up-button)))
164 (keys
165 (nconc (list (vector dn) (vector up))
166 (mapcar (lambda (amt) `[(,@(car amt) ,up)])
167 (cdr mouse-wheel-scroll-amount))
168 (mapcar (lambda (amt) `[(,@(car amt) ,dn)])
169 (cdr mouse-wheel-scroll-amount)))))
170 ;; This condition-case is here because Emacs 19 will throw an error
171 ;; if you try to define a key that it does not know about. I for one
172 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
173 ;; that if the wheeled-mouse is there, it just works, and this way it
174 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
175 (condition-case ()
176 (dolist (key keys)
177 (cond (mouse-wheel-mode
178 (global-set-key key 'mwheel-scroll))
179 ((eq (lookup-key (current-global-map) key) 'mwheel-scroll)
180 (global-unset-key key))))
181 (error nil))))
183 ;;; Compatibility entry point
184 ;;;###autoload
185 (defun mwheel-install (&optional uninstall)
186 "Enable mouse wheel support."
187 (mouse-wheel-mode t))
189 (provide 'mwheel)
191 ;;; mwheel.el ends here