(mouse-wheel-mode): New global minor mode.
[bpt/emacs.git] / lisp / mwheel.el
CommitLineData
a32b7419
WP
1;;; mwheel.el --- Mouse support for MS intelli-mouse type mice
2
3;; Copyright (C) 1998, Free Software Foundation, Inc.
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
WP
43
44(defcustom mwheel-scroll-amount '(5 . 1)
45 "Amount to scroll windows by when spinning the mouse wheel.
46This is actually a cons cell, where the first item is the amount to scroll
47on a normal wheel event, and the second is the amount to scroll when the
48wheel is moved with the shift key depressed.
49
50Each item should be the number of lines to scroll, or `nil' for near
51full screen.
52A 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
62(defcustom mwheel-follow-mouse nil
63 "Whether the mouse wheel should scroll the window that the mouse is over.
64This 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")
83 (let ((curwin (if mwheel-follow-mouse
84 (prog1
85 (selected-window)
86 (select-window (mwheel-event-window event)))))
87 (amt (if (memq 'shift (event-modifiers event))
88 (cdr mwheel-scroll-amount)
89 (car mwheel-scroll-amount))))
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
101;;; functions be already defined. [This is arguably a bug in d-m-m]
a32b7419 102;;;###autoload
f4cbc7a0
MB
103(define-minor-mode mouse-wheel-mode
104 "Toggle mouse wheel support.
105With prefix argument ARG, turn on if positive, otherwise off.
106Returns non-nil if the new state is enabled."
107 nil nil nil
108 :global t
109 :group 'mouse
a32b7419
WP
110 ;; In the latest versions of XEmacs, we could just use
111 ;; (S-)*mouse-[45], since those are aliases for the button
112 ;; equivalents in XEmacs, but I want this to work in as many
113 ;; versions of XEmacs as it can.
114 (let* ((mwheel-running-xemacs (string-match "XEmacs" (emacs-version)))
115 (keys (if mwheel-running-xemacs
116 '(button4 [(shift button4)] button5 [(shift button5)])
117 '([mouse-4] [S-mouse-4] [mouse-5] [S-mouse-5]))))
118 ;; This condition-case is here because Emacs 19 will throw an error
119 ;; if you try to define a key that it does not know about. I for one
120 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
121 ;; that if the wheeled-mouse is there, it just works, and this way it
122 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
123 (condition-case ()
f4cbc7a0
MB
124 (dolist (key keys)
125 (cond (mouse-wheel-mode
126 (define-key global-map key 'mwheel-scroll))
127 ((eq (lookup-key global-map key) 'mwheel-scroll)
128 (define-key global-map key nil))))
a32b7419 129 (error nil))))
f4cbc7a0
MB
130
131;;; Compatibility entry point
132;;;###autoload
133(defun mwheel-install (&optional uninstall)
134 "Enable mouse wheel support."
135 (mouse-wheel-mode t))
136
137
a32b7419
WP
138(provide 'mwheel)
139
140;;; mwheel.el ends here