Merge branch 'master' into comment-cache
[emacs.git] / lisp / mouse-drag.el
blob5a83e57347bb9f9b9fc3b0896b0b648e394266c6
1 ;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
3 ;; Copyright (C) 1996-1997, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: John Heidemann <johnh@ISI.EDU>
6 ;; Keywords: mouse
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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; What is ``mouse-drag.el''?
27 ;; Doesn't that scroll bar seem far away when you want to scroll?
28 ;; This module overloads mouse-2 to do ``throw'' scrolling. You
29 ;; click and drag. The distance you move from your original click
30 ;; turns into a scroll amount. The scroll amount is scaled
31 ;; exponentially to make both large moves and short adjustments easy.
32 ;; What this boils down to is that you can easily scroll around the
33 ;; buffer without much mouse movement. Finally, clicks which aren't
34 ;; drags are passed off to the old mouse-2 binding, so old mouse-2
35 ;; operations (find-file in dired-mode, yanking in most other modes)
36 ;; still work.
38 ;; There is an alternative way to scroll, ``drag'' scrolling. You
39 ;; can click on a character and then drag it around, scrolling the
40 ;; buffer with you. The character always stays under the mouse.
41 ;; Compared to throw-scrolling, this approach provides direct
42 ;; manipulation (nice) but requires more mouse movement
43 ;; (unfortunate). It is offered as an alternative for those who
44 ;; prefer it.
46 ;; If you like mouse-drag, you should also check out mouse-copy
47 ;; for ``one-click text copy and move''.
49 ;; To use mouse-drag, place the following in your init file:
50 ;; -either-
51 ;; (global-set-key [down-mouse-2] 'mouse-drag-throw)
52 ;; -or-
53 ;; (global-set-key [down-mouse-2] 'mouse-drag-drag)
57 ;; Options:
59 ;; - reverse the throw-scroll direction with \\[mouse-throw-with-scroll-bar]
60 ;; - work around a bug with \\[mouse-extras-work-around-drag-bug]
61 ;; - auto-enable horizontal scrolling with
62 ;; \\[mouse-drag-electric-col-scrolling]
65 ;; History and related work:
67 ;; One-click copying and moving was inspired by lemacs-19.8.
68 ;; Throw-scrolling was inspired by MacPaint's ``hand'' and by Tk's
69 ;; mouse-2 scrolling. The package mouse-scroll.el by Tom Wurgler
70 ;; <twurgler@goodyear.com> is similar to mouse-drag-throw, but
71 ;; doesn't pass clicks through.
73 ;; These functions have been tested in emacs version 19.30,
74 ;; and this package has run in the past on 19.25-19.29.
76 ;; Originally mouse-drag was part of a larger package.
77 ;; As of 11 July 96 the scrolling functions were split out
78 ;; in preparation for incorporation into (the future) emacs-19.32.
80 ;; Thanks:
82 ;; Thanks to Kai Grossjohann
83 ;; <grossjoh@dusty.informatik.uni-dortmund.de> for reporting bugs, to
84 ;; Tom Wurgler <twurgler@goodyear.com> for reporting bugs and
85 ;; suggesting fixes, and to Joel Graber <jgraber@ti.com> for
86 ;; prompting me to do drag-scrolling and for an initial
87 ;; implementation of horizontal drag-scrolling.
89 ;; -johnh@isi.edu, 11-Jul-96
92 ;; What's new with mouse-drag 2.24?
94 ;; - mouse-drag-electric-col-scrolling (default: on)
95 ;; auto-enables horizontal scrolling when clicks on wrapped
96 ;; lines occur
98 ;; TODO:
99 ;; - For mouse-drag-throw, we should try and place some visual indicator
100 ;; of the original mouse position (like Firefox does).
102 ;;; Code:
105 ;; scrolling code
108 (defun mouse-drag-safe-scroll (row-delta &optional col-delta)
109 "Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
110 Keep the cursor on the screen as needed."
111 (let ((scroll-preserve-screen-position nil))
112 (if (and row-delta
113 (/= 0 row-delta))
114 (condition-case nil ;; catch and ignore movement errors
115 (scroll-down row-delta)
116 (beginning-of-buffer (message "Beginning of buffer"))
117 (end-of-buffer (message "End of buffer"))))
118 (if (and col-delta
119 (/= 0 col-delta))
120 (progn
121 (scroll-right col-delta)
122 ;; Make sure that the point stays on the visible screen
123 ;; (if truncation-lines in set).
124 ;; This code mimics the behavior we automatically get
125 ;; when doing vertical scrolling.
126 ;; Problem identified and a fix suggested by Tom Wurgler.
127 (cond
128 ((< (current-column) (window-hscroll))
129 (move-to-column (window-hscroll))) ; make on left column
130 ((> (- (current-column) (window-hscroll) (window-width) -2) 0)
131 (move-to-column (+ (window-width) (window-hscroll) -3))))))))
133 (defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
134 "Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
135 (while (sit-for mouse-scroll-delay)
136 (mouse-drag-safe-scroll row-delta col-delta)))
138 (defun mouse-drag-events-are-point-events-p (start-posn end-posn)
139 "Determine if START-POSN and END-POSN are \"close\"."
140 (let*
141 ((start-col-row (posn-col-row start-posn))
142 (end-col-row (posn-col-row end-posn)))
143 (and
144 ;; ;; We no longer exclude things by time.
145 ;; (< (- (posn-timestamp end-posn) (posn-timestamp start-posn))
146 ;; (if (numberp double-click-time)
147 ;; (* 2 double-click-time) ;; stretch it a little
148 ;; 999999)) ;; non-numeric => check by position alone
149 (= (car start-col-row) (car end-col-row))
150 (= (cdr start-col-row) (cdr end-col-row)))))
152 (defvar mouse-drag-electric-col-scrolling t
153 "If non-nil, mouse-drag on a long line enables truncate-lines.")
155 (defun mouse-drag-should-do-col-scrolling ()
156 "Determine if it's wise to enable col-scrolling for the current window.
157 Basically, we check for existing horizontal scrolling."
158 (or truncate-lines
159 (> (window-hscroll) 0)
160 (not (window-full-width-p))
161 (and
162 mouse-drag-electric-col-scrolling
163 (save-excursion ;; on a long line?
164 (let
165 ((beg (line-beginning-position))
166 (end (progn (end-of-line) (point))))
167 (if (> (- end beg) (window-width))
168 (setq truncate-lines t)
169 nil))))))
171 (defvar mouse-throw-with-scroll-bar nil
172 "Set direction of mouse-throwing.
173 If nil, the text moves in the direction the mouse moves.
174 If t, the scroll bar moves in the direction the mouse moves.")
175 (defconst mouse-throw-magnifier-min -6)
176 (defconst mouse-throw-magnifier-max 6)
177 (defconst mouse-throw-magnifier-base 1.5)
179 (defun mouse-drag-scroll-delta (mouse-delta)
180 ;; Limit the exponential explosion.
181 (setq mouse-delta
182 (max mouse-throw-magnifier-min
183 (min mouse-throw-magnifier-max mouse-delta)))
184 (* (round (exp (* (log mouse-throw-magnifier-base) (abs mouse-delta))))
185 (if (< mouse-delta 0) -1 1)
186 (if mouse-throw-with-scroll-bar 1 -1)))
188 ;;;###autoload
189 (defun mouse-drag-throw (start-event)
190 "\"Throw\" the page according to a mouse drag.
192 A \"throw\" is scrolling the page at a speed relative to the distance
193 from the original mouse click to the current mouse location. Try it;
194 you'll like it. It's easier to observe than to explain.
196 If the mouse is clicked and released in the same place of time we
197 assume that the user didn't want to scroll but wanted to whatever
198 mouse-2 used to do, so we pass it through.
200 Throw scrolling was inspired (but is not identical to) the \"hand\"
201 option in MacPaint, or the middle button in Tk text widgets.
203 If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
204 in the opposite direction. (Different people have different ideas
205 about which direction is natural. Perhaps it has to do with which
206 hemisphere you're in.)
208 To test this function, evaluate:
209 (global-set-key [down-mouse-2] \\='mouse-drag-throw)"
210 (interactive "e")
211 ;; we want to do save-selected-window, but that requires 19.29
212 (let* ((start-posn (event-start start-event))
213 (start-window (posn-window start-posn))
214 (start-row (cdr (posn-col-row start-posn)))
215 (start-col (car (posn-col-row start-posn)))
216 (old-selected-window (selected-window))
217 event end row scroll-delta
218 have-scrolled
220 (scroll-col-delta 0)
221 ;; be conservative about allowing horizontal scrolling
222 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
223 (select-window start-window)
224 (track-mouse
225 ;; Don't change the mouse pointer shape while we drag.
226 (setq track-mouse 'dragging)
227 (while (progn
228 (setq event (read-event)
229 end (event-end event)
230 row (cdr (posn-col-row end))
231 col (car (posn-col-row end)))
232 (or (mouse-movement-p event)
233 (eq (car-safe event) 'switch-frame)))
234 (when (eq start-window (posn-window end))
235 (when col-scrolling-p
236 (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
237 (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
239 (if (or (/= 0 scroll-delta)
240 (/= 0 scroll-col-delta))
241 (progn
242 (setq have-scrolled t)
243 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
244 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
245 ;; If it was a click and not a drag, prepare to pass the event on.
246 ;; Is there a more correct way to reconstruct the event?
247 (if (and (not have-scrolled)
248 (mouse-drag-events-are-point-events-p start-posn end))
249 (push (cons (event-basic-type start-event) (cdr start-event))
250 unread-command-events))
251 ;; Now restore the old window.
252 (select-window old-selected-window)))
254 ;;;###autoload
255 (defun mouse-drag-drag (start-event)
256 "\"Drag\" the page according to a mouse drag.
258 Drag scrolling moves the page according to the movement of the mouse.
259 You \"grab\" the character under the mouse and move it around.
261 If the mouse is clicked and released in the same place of time we
262 assume that the user didn't want to scroll but wanted to whatever
263 mouse-2 used to do, so we pass it through.
265 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
266 middle button in Tk text widgets.
268 To test this function, evaluate:
269 (global-set-key [down-mouse-2] \\='mouse-drag-drag)"
270 (interactive "e")
271 ;; we want to do save-selected-window, but that requires 19.29
272 (let* ((start-posn (event-start start-event))
273 (start-window (posn-window start-posn))
274 (start-row (cdr (posn-col-row start-posn)))
275 (start-col (car (posn-col-row start-posn)))
276 (old-selected-window (selected-window))
277 event end row scroll-delta
278 have-scrolled
279 window-last-row
280 col window-last-col
281 (scroll-col-delta 0)
282 ;; be conservative about allowing horizontal scrolling
283 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
284 (select-window start-window)
285 (setq window-last-row (- (window-height) 2)
286 window-last-col (- (window-width) 2))
287 (track-mouse
288 (while (progn
289 (setq event (read-event)
290 end (event-end event)
291 row (cdr (posn-col-row end))
292 col (car (posn-col-row end)))
293 (or (mouse-movement-p event)
294 (eq (car-safe event) 'switch-frame)))
295 ;; Scroll if see if we're on the edge.
296 ;; FIXME: should handle mouse-in-other window.
297 (cond
298 ((not (eq start-window (posn-window end)))
299 t) ; wait for return to original window
300 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
301 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
302 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
303 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
305 (setq scroll-delta (- row start-row)
306 start-row row)
307 (if col-scrolling-p
308 (setq scroll-col-delta (- col start-col)
309 start-col col))
310 (if (or (/= 0 scroll-delta)
311 (/= 0 scroll-col-delta))
312 (progn
313 (setq have-scrolled t)
314 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
315 ;; If it was a click and not a drag, prepare to pass the event on.
316 ;; Is there a more correct way to reconstruct the event?
317 (if (and (not have-scrolled)
318 (mouse-drag-events-are-point-events-p start-posn end))
319 (push (cons (event-basic-type start-event) (cdr start-event))
320 unread-command-events))
321 ;; Now restore the old window.
322 (select-window old-selected-window)))
325 (provide 'mouse-drag)
327 ;;; mouse-drag.el ends here