[HAVE_TERMCAP_H]: Include <termcap.h>.
[emacs.git] / lisp / mwheel.el
blobd6b562b55e321ac46c18efec7b237f677f94bc72
1 ;;; mwheel.el --- Mouse support for MS intelli-mouse type mice
3 ;; Copyright (C) 1998, 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 mwheel-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 mwheel-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 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
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)))))
97 ;;;###autoload
98 (defun mwheel-install ()
99 "Enable mouse wheel support."
100 ;; In the latest versions of XEmacs, we could just use
101 ;; (S-)*mouse-[45], since those are aliases for the button
102 ;; equivalents in XEmacs, but I want this to work in as many
103 ;; versions of XEmacs as it can.
104 (let* ((mwheel-running-xemacs (string-match "XEmacs" (emacs-version)))
105 (keys (if mwheel-running-xemacs
106 '(button4 [(shift button4)] button5 [(shift button5)])
107 '([mouse-4] [S-mouse-4] [mouse-5] [S-mouse-5]))))
108 ;; This condition-case is here because Emacs 19 will throw an error
109 ;; if you try to define a key that it does not know about. I for one
110 ;; prefer to just unconditionally do a mwheel-install in my .emacs, so
111 ;; that if the wheeled-mouse is there, it just works, and this way it
112 ;; doesn't yell at me if I'm on my laptop or another machine, etc.
113 (condition-case ()
114 (while keys
115 (define-key global-map (car keys) 'mwheel-scroll)
116 (setq keys (cdr keys)))
117 (error nil))))
119 (provide 'mwheel)
121 ;;; mwheel.el ends here