Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / mwheel.el
blob876659f1f71ee328daee84b24eee362d0bf3f7c6
1 ;;; mwheel.el --- Wheel mouse support
3 ;; Copyright (C) 1998, 2000-2018 Free Software Foundation, Inc.
4 ;; Maintainer: William M. Perry <wmperry@gnu.org>
5 ;; Keywords: mouse
6 ;; Package: emacs
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/>.
23 ;;; Commentary:
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
35 ;; file:
37 ;; (mouse-wheel-mode 1)
39 ;;; Code:
41 (require 'custom)
42 (require 'timer)
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)
52 ;; Sync the bindings.
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))
57 'wheel-up
58 'mouse-4)
59 "Event used for scrolling down."
60 :group 'mouse
61 :type 'symbol
62 :set 'mouse-wheel-change-button)
64 (defcustom mouse-wheel-up-event
65 (if (or (featurep 'w32-win) (featurep 'ns-win))
66 'wheel-down
67 'mouse-5)
68 "Event used for scrolling up."
69 :group 'mouse
70 :type 'symbol
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."
79 :group 'mouse
80 :type 'symbol
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."
85 :group 'mouse
86 :type 'number)
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
93 MODIFIERS is nil.
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."
99 :group 'mouse
100 :type '(cons
101 (choice :tag "Normal"
102 (const :tag "Full screen" :value nil)
103 (integer :tag "Specific # of lines")
104 (float :tag "Fraction of window")
105 (cons
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"))))
113 (repeat
114 (cons
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
128 of button events."
129 :group 'mouse
130 :type 'boolean)
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."
135 :group 'mouse
136 :type 'boolean)
138 ;;; For tilt-scroll
140 (defcustom mouse-wheel-tilt-scroll nil
141 "Enable scroll using tilting mouse wheel."
142 :group 'mouse
143 :type 'boolean
144 :version "26.1")
146 (defcustom mouse-wheel-flip-direction nil
147 "Swap direction of 'wheel-right and 'wheel-left."
148 :group 'mouse
149 :type 'boolean
150 :version "26.1")
152 (eval-and-compile
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))))))
160 (if (< amount 0)
161 mouse-wheel-up-event
162 mouse-wheel-down-event))
163 x))))
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))
197 'wheel-left
198 (intern "mouse-6"))
199 "Event used for scrolling left.")
201 (defvar mouse-wheel-right-event
202 (if (or (featurep 'w32-win) (featurep 'ns-win))
203 'wheel-right
204 (intern "mouse-7"))
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))
213 (scroll-window
214 (or (catch 'found
215 (let* ((window (if mouse-wheel-follow-mouse
216 (mwheel-event-window event)
217 (selected-window)))
218 (frame (when (window-live-p window)
219 (frame-parameter
220 (window-frame window) 'mouse-wheel-frame))))
221 (when (frame-live-p frame)
222 (let* ((pos (mouse-absolute-pixel-position))
223 (pos-x (car pos))
224 (pos-y (cdr pos)))
225 (walk-window-tree
226 (lambda (window-1)
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))))
233 frame nil t)))))
234 (mwheel-event-window event)))
235 (old-point
236 (and (eq scroll-window selected-window)
237 (eq (car-safe transient-mark-mode) 'only)
238 (window-point)))
239 (mods
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))))
255 (unwind-protect
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
260 ;; the buffer.
261 (beginning-of-buffer
262 (unwind-protect
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)
295 (deactivate-mark)
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 :init-value t
314 ;; We'd like to use custom-initialize-set here so the setup is done
315 ;; before dumping, but at the point where the defcustom is evaluated,
316 ;; the corresponding function isn't defined yet, so
317 ;; custom-initialize-set signals an error.
318 :initialize 'custom-initialize-delay
319 :global t
320 :group 'mouse
321 ;; Remove previous bindings, if any.
322 (while mwheel-installed-bindings
323 (let ((key (pop mwheel-installed-bindings)))
324 (when (eq (lookup-key (current-global-map) key) 'mwheel-scroll)
325 (global-unset-key key))))
326 ;; Setup bindings as needed.
327 (when mouse-wheel-mode
328 (dolist (event (list mouse-wheel-down-event mouse-wheel-up-event mouse-wheel-right-event mouse-wheel-left-event))
329 (dolist (key (mapcar (lambda (amt) `[(,@(if (consp amt) (car amt)) ,event)])
330 mouse-wheel-scroll-amount))
331 (global-set-key key 'mwheel-scroll)
332 (push key mwheel-installed-bindings)))))
334 ;;; Compatibility entry point
335 ;; preloaded ;;;###autoload
336 (defun mwheel-install (&optional uninstall)
337 "Enable mouse wheel support."
338 (mouse-wheel-mode (if uninstall -1 1)))
340 (provide 'mwheel)
342 ;;; mwheel.el ends here