All of SXEmacs' http URLs are now https. WooHoo!
[sxemacs] / lisp / mwheel.el
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@cs.indiana.edu>
5 ;; Keywords: mouse
6
7 ;; This file is part of SXEmacs.
8
9 ;; SXEmacs 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 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; SXEmacs 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.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Synched up with: Not synched.
23
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 ;;
38 ;; (autoload 'mwheel-install "mwheel" "Enable mouse wheel support.")
39 ;; (mwheel-install)
40
41 ;;; Code:
42
43 (require 'custom)
44 (require 'cl)
45
46 (globally-declare-fboundp
47  '(event-basic-type
48    posn-window event-start mwheel-event-window mwheel-event-button))
49
50 (defcustom mwheel-scroll-amount '(5 . 1)
51   "Amount to scroll windows by when spinning the mouse wheel.
52 This is actually a cons cell, where the first item is the amount to scroll
53 on a normal wheel event, and the second is the amount to scroll when the
54 wheel is moved with the shift key depressed.
55
56 Each item should be the number of lines to scroll, or `nil' for near
57 full screen.
58 A near full screen is `next-screen-context-lines' less than a full screen."
59   :group 'mouse
60   :type '(cons
61           (choice :tag "Normal"
62                   (const :tag "Full screen" :value nil)
63                   (integer :tag "Specific # of lines"))
64           (choice :tag "Shifted"
65                   (const :tag "Full screen" :value nil)
66                   (integer :tag "Specific # of lines"))))
67
68 (defcustom mwheel-follow-mouse nil
69   "Whether the mouse wheel should scroll the window that the mouse is over.
70 This can be slightly disconcerting, but some people may prefer it."
71   :group 'mouse
72   :type 'boolean)
73
74 (if (not (fboundp 'event-button))
75     (defun mwheel-event-button (event)
76       (let ((x (symbol-name (event-basic-type event))))
77         (if (not (string-match #r"^mouse-\([0-9]+\)" x))
78             (error "Not a button event: %S" event))
79         (string-to-int (substring x (match-beginning 1) (match-end 1)))))
80   (fset 'mwheel-event-button 'event-button))
81
82 (if (not (fboundp 'event-window))
83     (defun mwheel-event-window (event)
84       (posn-window (event-start event)))
85   (fset 'mwheel-event-window 'event-window))
86
87 (defun mwheel-scroll (event)
88   (interactive "e")
89   (let ((curwin (if mwheel-follow-mouse
90                     (prog1
91                         (selected-window)
92                       (select-window (mwheel-event-window event)))))
93         (amt (if (memq 'shift (event-modifiers event))
94                  (cdr mwheel-scroll-amount)
95                (car mwheel-scroll-amount))))
96     (unwind-protect
97         (case (mwheel-event-button event)
98           (4 (scroll-down amt))
99           (5 (scroll-up amt))
100           (otherwise (error "Bad binding in mwheel-scroll")))
101       (if curwin (select-window curwin)))
102     ))
103
104 ;;;###autoload
105 (defun mwheel-install ()
106   "Enable mouse wheel support."
107   (interactive)
108   (let ((keys '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
109     ;; This condition-case is here because Emacs 19 will throw an error
110     ;; if you try to define a key that it does not know about.  I for one
111     ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
112     ;; that if the wheeled-mouse is there, it just works, and this way it
113     ;; doesn't yell at me if I'm on my laptop or another machine, etc.
114     (condition-case ()
115         (while keys
116           (define-key global-map (car keys) 'mwheel-scroll)
117           (setq keys (cdr keys)))
118       (error nil))))
119
120 (provide 'mwheel)
121
122 ;;; mwheel.el ends here