Replace formfeeds removed on 2001/01/22.
[emacs.git] / lisp / mwheel.el
blobb58d42ebf23e0e3f5d4919a24cac4fde4609c0c9
1 ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice
3 ;; Copyright (C) 1998, 2000, 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 (defcustom mouse-wheel-scroll-amount '(5 . 1)
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.
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"))))
62 (defcustom mouse-wheel-follow-mouse nil
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)
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))
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))
81 (defun mwheel-scroll (event)
82 (interactive "e")
83 (let ((curwin (if mouse-wheel-follow-mouse
84 (prog1
85 (selected-window)
86 (select-window (mwheel-event-window event)))))
87 (amt (if (memq 'shift (event-modifiers event))
88 (cdr mouse-wheel-scroll-amount)
89 (car mouse-wheel-scroll-amount))))
90 (unwind-protect
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"))))
95 (if curwin (select-window curwin)))))
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
101 ;;; functions be already defined.
102 ;;;###autoload
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."
107 :global t
108 :group 'mouse
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.
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]))))
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 ()
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))))
128 (error nil))))
130 ;;; Compatibility entry point
131 ;;;###autoload
132 (defun mwheel-install (&optional uninstall)
133 "Enable mouse wheel support."
134 (mouse-wheel-mode t))
137 (provide 'mwheel)
139 ;;; mwheel.el ends here