1 ;;; mwheel.el --- Wheel mouse support
3 ;; Copyright (C) 1998, 2000-2018 Free Software Foundation, Inc.
4 ;; Maintainer: William M. Perry <wmperry@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; This code will enable the use of the infamous 'wheel' on the new
26 ;; crop of mice. Under XFree86 and the XSuSE X Servers, the wheel
27 ;; events are sent as button4/button5 events.
29 ;; I for one would prefer some way of converting the button4/button5
30 ;; events into different event types, like 'mwheel-up' or
31 ;; 'mwheel-down', but I cannot find a way to do this very easily (or
32 ;; portably), so for now I just live with it.
34 ;; To enable this code, simply put this at the top of your .emacs
37 ;; (mouse-wheel-mode 1)
44 (defvar mouse-wheel-mode
)
46 ;; Setter function for mouse-button user-options. Switch Mouse Wheel
47 ;; mode off and on again so that the old button is unbound and
48 ;; new button is bound to mwheel-scroll.
50 (defun mouse-wheel-change-button (var button
)
51 (set-default var button
)
53 (when (bound-and-true-p mouse-wheel-mode
) (mouse-wheel-mode 1)))
55 (defcustom mouse-wheel-down-event
56 (if (or (featurep 'w32-win
) (featurep 'ns-win
))
59 "Event used for scrolling down."
62 :set
'mouse-wheel-change-button
)
64 (defcustom mouse-wheel-up-event
65 (if (or (featurep 'w32-win
) (featurep 'ns-win
))
68 "Event used for scrolling up."
71 :set
'mouse-wheel-change-button
)
73 (defcustom mouse-wheel-click-event
'mouse-2
74 "Event that should be temporarily inhibited after mouse scrolling.
75 The mouse wheel is typically on the mouse-2 button, so it may easily
76 happen that text is accidentally yanked into the buffer when
77 scrolling with the mouse wheel. To prevent that, this variable can be
78 set to the event sent when clicking on the mouse wheel button."
81 :set
'mouse-wheel-change-button
)
83 (defcustom mouse-wheel-inhibit-click-time
0.35
84 "Time in seconds to inhibit clicking on mouse wheel button after scroll."
88 (defcustom mouse-wheel-scroll-amount
'(5 ((shift) .
1) ((control) . nil
))
89 "Amount to scroll windows by when spinning the mouse wheel.
90 This is an alist mapping the modifier key to the amount to scroll when
91 the wheel is moved with the modifier key depressed.
92 Elements of the list have the form (MODIFIERS . AMOUNT) or just AMOUNT if
95 AMOUNT should be the number of lines to scroll, or nil for near full
96 screen. It can also be a floating point number, specifying the fraction of
97 a full screen to scroll. A near full screen is `next-screen-context-lines'
98 less than a full screen."
101 (choice :tag
"Normal"
102 (const :tag
"Full screen" :value nil
)
103 (integer :tag
"Specific # of lines")
104 (float :tag
"Fraction of window")
106 (repeat (choice :tag
"modifier"
107 (const alt
) (const control
) (const hyper
)
108 (const meta
) (const shift
) (const super
)))
109 (choice :tag
"scroll amount"
110 (const :tag
"Full screen" :value nil
)
111 (integer :tag
"Specific # of lines")
112 (float :tag
"Fraction of window"))))
115 (repeat (choice :tag
"modifier"
116 (const alt
) (const control
) (const hyper
)
117 (const meta
) (const shift
) (const super
)))
118 (choice :tag
"scroll amount"
119 (const :tag
"Full screen" :value nil
)
120 (integer :tag
"Specific # of lines")
121 (float :tag
"Fraction of window")))))
122 :set
'mouse-wheel-change-button
)
124 (defcustom mouse-wheel-progressive-speed t
125 "If non-nil, the faster the user moves the wheel, the faster the scrolling.
126 Note that this has no effect when `mouse-wheel-scroll-amount' specifies
127 a \"near full screen\" scroll or when the mouse wheel sends key instead
132 (defcustom mouse-wheel-follow-mouse t
133 "Whether the mouse wheel should scroll the window that the mouse is over.
134 This can be slightly disconcerting, but some people prefer it."
140 (defcustom mouse-wheel-tilt-scroll nil
141 "Enable scroll using tilting mouse wheel."
146 (defcustom mouse-wheel-flip-direction nil
147 "Swap direction of 'wheel-right and 'wheel-left."
153 (if (fboundp 'event-button
)
154 (fset 'mwheel-event-button
'event-button
)
155 (defun mwheel-event-button (event)
156 (let ((x (event-basic-type event
)))
157 ;; Map mouse-wheel events to appropriate buttons
158 (if (eq 'mouse-wheel x
)
159 (let ((amount (car (cdr (cdr (cdr event
))))))
162 mouse-wheel-down-event
))
165 (if (fboundp 'event-window
)
166 (fset 'mwheel-event-window
'event-window
)
167 (defun mwheel-event-window (event)
168 (posn-window (event-start event
)))))
170 (defvar mwheel-inhibit-click-event-timer nil
171 "Timer running while mouse wheel click event is inhibited.")
173 (defun mwheel-inhibit-click-timeout ()
174 "Handler for `mwheel-inhibit-click-event-timer'."
175 (setq mwheel-inhibit-click-event-timer nil
)
176 (remove-hook 'pre-command-hook
'mwheel-filter-click-events
))
178 (defun mwheel-filter-click-events ()
179 "Discard `mouse-wheel-click-event' while scrolling the mouse."
180 (if (eq (event-basic-type last-input-event
) mouse-wheel-click-event
)
181 (setq this-command
'ignore
)))
183 (defvar mwheel-scroll-up-function
'scroll-up
184 "Function that does the job of scrolling upward.")
186 (defvar mwheel-scroll-down-function
'scroll-down
187 "Function that does the job of scrolling downward.")
189 (defvar mwheel-scroll-left-function
'scroll-left
190 "Function that does the job of scrolling left.")
192 (defvar mwheel-scroll-right-function
'scroll-right
193 "Function that does the job of scrolling right.")
195 (defvar mouse-wheel-left-event
196 (if (or (featurep 'w32-win
) (featurep 'ns-win
))
199 "Event used for scrolling left.")
201 (defvar mouse-wheel-right-event
202 (if (or (featurep 'w32-win
) (featurep 'ns-win
))
205 "Event used for scrolling right.")
207 (defun mwheel-scroll (event)
208 "Scroll up or down according to the EVENT.
209 This should be bound only to mouse buttons 4, 5, 6, and 7 on
210 non-Windows systems."
211 (interactive (list last-input-event
))
212 (let* ((selected-window (selected-window))
215 (let* ((window (if mouse-wheel-follow-mouse
216 (mwheel-event-window event
)
218 (frame (when (window-live-p window
)
220 (window-frame window
) 'mouse-wheel-frame
))))
221 (when (frame-live-p frame
)
222 (let* ((pos (mouse-absolute-pixel-position))
227 (let ((edges (window-edges window-1 nil t t
)))
228 (when (and (<= (nth 0 edges
) pos-x
)
229 (<= pos-x
(nth 2 edges
))
230 (<= (nth 1 edges
) pos-y
)
231 (<= pos-y
(nth 3 edges
)))
232 (throw 'found window-1
))))
234 (mwheel-event-window event
)))
236 (and (eq scroll-window selected-window
)
237 (eq (car-safe transient-mark-mode
) 'only
)
240 (delq 'click
(delq 'double
(delq 'triple
(event-modifiers event
)))))
241 (amt (assoc mods mouse-wheel-scroll-amount
)))
242 (unless (eq scroll-window selected-window
)
243 ;; Mark window to be scrolled for redisplay.
244 (select-window scroll-window
'mark-for-redisplay
))
245 ;; Extract the actual amount or find the element that has no modifiers.
246 (if amt
(setq amt
(cdr amt
))
247 (let ((list-elt mouse-wheel-scroll-amount
))
248 (while (consp (setq amt
(pop list-elt
))))))
249 (if (floatp amt
) (setq amt
(1+ (truncate (* amt
(window-height))))))
250 (when (and mouse-wheel-progressive-speed
(numberp amt
))
251 ;; When the double-mouse-N comes in, a mouse-N has been executed already,
252 ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
253 (setq amt
(* amt
(event-click-count event
))))
254 (when (numberp amt
) (setq amt
(* amt
(event-line-count event
))))
256 (let ((button (mwheel-event-button event
)))
257 (cond ((eq button mouse-wheel-down-event
)
258 (condition-case nil
(funcall mwheel-scroll-down-function amt
)
259 ;; Make sure we do indeed scroll to the beginning of
263 (funcall mwheel-scroll-down-function
)
264 ;; If the first scroll succeeded, then some scrolling
265 ;; is possible: keep scrolling til the beginning but
266 ;; do not signal an error. For some reason, we have
267 ;; to do it even if the first scroll signaled an
268 ;; error, because otherwise the window is recentered
269 ;; for a reason that escapes me. This problem seems
270 ;; to only affect scroll-down. --Stef
271 (set-window-start (selected-window) (point-min))))))
272 ((eq button mouse-wheel-up-event
)
273 (condition-case nil
(funcall mwheel-scroll-up-function amt
)
274 ;; Make sure we do indeed scroll to the end of the buffer.
275 (end-of-buffer (while t
(funcall mwheel-scroll-up-function
)))))
276 ((eq button mouse-wheel-left-event
) ; for tilt scroll
277 (when mouse-wheel-tilt-scroll
278 (funcall (if mouse-wheel-flip-direction
279 mwheel-scroll-right-function
280 mwheel-scroll-left-function
) amt
)))
281 ((eq button mouse-wheel-right-event
) ; for tilt scroll
282 (when mouse-wheel-tilt-scroll
283 (funcall (if mouse-wheel-flip-direction
284 mwheel-scroll-left-function
285 mwheel-scroll-right-function
) amt
)))
286 (t (error "Bad binding in mwheel-scroll"))))
287 (if (eq scroll-window selected-window
)
288 ;; If there is a temporarily active region, deactivate it if
289 ;; scrolling moved point.
290 (when (and old-point
(/= old-point
(window-point)))
291 ;; Call `deactivate-mark' at the original position, so that
292 ;; the original region is saved to the X selection.
293 (let ((new-point (window-point)))
294 (goto-char old-point
)
296 (goto-char new-point
)))
297 (select-window selected-window t
))))
299 (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time
)
300 (if mwheel-inhibit-click-event-timer
301 (cancel-timer mwheel-inhibit-click-event-timer
)
302 (add-hook 'pre-command-hook
'mwheel-filter-click-events
))
303 (setq mwheel-inhibit-click-event-timer
304 (run-with-timer mouse-wheel-inhibit-click-time nil
305 'mwheel-inhibit-click-timeout
))))
307 (put 'mwheel-scroll
'scroll-command t
)
309 (defvar mwheel-installed-bindings nil
)
311 (define-minor-mode mouse-wheel-mode
312 "Toggle mouse wheel support (Mouse Wheel mode).
313 With a prefix argument ARG, enable Mouse Wheel mode if ARG is
314 positive, and disable it otherwise. If called from Lisp, enable
315 the mode if ARG is omitted or nil."
317 ;; We'd like to use custom-initialize-set here so the setup is done
318 ;; before dumping, but at the point where the defcustom is evaluated,
319 ;; the corresponding function isn't defined yet, so
320 ;; custom-initialize-set signals an error.
321 :initialize
'custom-initialize-delay
324 ;; Remove previous bindings, if any.
325 (while mwheel-installed-bindings
326 (let ((key (pop mwheel-installed-bindings
)))
327 (when (eq (lookup-key (current-global-map) key
) 'mwheel-scroll
)
328 (global-unset-key key
))))
329 ;; Setup bindings as needed.
330 (when mouse-wheel-mode
331 (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event mouse-wheel-right-event mouse-wheel-left-event
))
332 (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt
) (car amt
)) ,event
)])
333 mouse-wheel-scroll-amount
))
334 (global-set-key key
'mwheel-scroll
)
335 (push key mwheel-installed-bindings
)))))
337 ;;; Compatibility entry point
338 ;; preloaded ;;;###autoload
339 (defun mwheel-install (&optional uninstall
)
340 "Enable mouse wheel support."
341 (mouse-wheel-mode (if uninstall -
1 1)))
345 ;;; mwheel.el ends here