Partially undo the dispatcher split, it needs to happen more gradually
[emacs.git] / lisp / mouse-drag.el
blob372c086e98511e1b25bfb18e71ec983042cf8513
1 ;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: John Heidemann <johnh@ISI.EDU>
7 ;; Keywords: mouse
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; What is ``mouse-drag.el''?
30 ;; Doesn't that scroll bar seem far away when you want to scroll?
31 ;; This module overloads mouse-2 to do ``throw'' scrolling. You
32 ;; click and drag. The distance you move from your original click
33 ;; turns into a scroll amount. The scroll amount is scaled
34 ;; exponentially to make both large moves and short adjustments easy.
35 ;; What this boils down to is that you can easily scroll around the
36 ;; buffer without much mouse movement. Finally, clicks which aren't
37 ;; drags are passed off to the old mouse-2 binding, so old mouse-2
38 ;; operations (find-file in dired-mode, yanking in most other modes)
39 ;; still work.
41 ;; There is an alternative way to scroll, ``drag'' scrolling. You
42 ;; can click on a character and then drag it around, scrolling the
43 ;; buffer with you. The character always stays under the mouse.
44 ;; Compared to throw-scrolling, this approach provides direct
45 ;; manipulation (nice) but requires more mouse movement
46 ;; (unfortunate). It is offered as an alternative for those who
47 ;; prefer it.
49 ;; If you like mouse-drag, you should also check out mouse-copy
50 ;; for ``one-click text copy and move''.
52 ;; To use mouse-drag, place the following in your .emacs file:
53 ;; (require 'mouse-drag)
54 ;; -and either-
55 ;; (global-set-key [down-mouse-2] 'mouse-drag-throw)
56 ;; -or-
57 ;; (global-set-key [down-mouse-2] 'mouse-drag-drag)
61 ;; Options:
63 ;; - reverse the throw-scroll direction with \\[mouse-throw-with-scroll-bar]
64 ;; - work around a bug with \\[mouse-extras-work-around-drag-bug]
65 ;; - auto-enable horizontal scrolling with
66 ;; \\[mouse-drag-electric-col-scrolling]
69 ;; History and related work:
71 ;; One-click copying and moving was inspired by lemacs-19.8.
72 ;; Throw-scrolling was inspired by MacPaint's ``hand'' and by Tk's
73 ;; mouse-2 scrolling. The package mouse-scroll.el by Tom Wurgler
74 ;; <twurgler@goodyear.com> is similar to mouse-drag-throw, but
75 ;; doesn't pass clicks through.
77 ;; These functions have been tested in emacs version 19.30,
78 ;; and this package has run in the past on 19.25-19.29.
80 ;; Originally mouse-drag was part of a larger package.
81 ;; As of 11 July 96 the scrolling functions were split out
82 ;; in preparation for incorporation into (the future) emacs-19.32.
84 ;; Thanks:
86 ;; Thanks to Kai Grossjohann
87 ;; <grossjoh@dusty.informatik.uni-dortmund.de> for reporting bugs, to
88 ;; Tom Wurgler <twurgler@goodyear.com> for reporting bugs and
89 ;; suggesting fixes, and to Joel Graber <jgraber@ti.com> for
90 ;; prompting me to do drag-scrolling and for an initial
91 ;; implementation of horizontal drag-scrolling.
93 ;; -johnh@isi.edu, 11-Jul-96
96 ;; What's new with mouse-drag 2.24?
98 ;; - mouse-drag-electric-col-scrolling (default: on)
99 ;; auto-enables horizontal scrolling when clicks on wrapped
100 ;; lines occur
102 ;; TODO:
103 ;; - For mouse-drag-throw, we should try and place some visual indicator
104 ;; of the original mouse position (like Firefox does).
106 ;;; Code:
109 ;; scrolling code
112 (defun mouse-drag-safe-scroll (row-delta &optional col-delta)
113 "Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
114 Keep the cursor on the screen as needed."
115 (let ((scroll-preserve-screen-position nil))
116 (if (and row-delta
117 (/= 0 row-delta))
118 (condition-case nil ;; catch and ignore movement errors
119 (scroll-down row-delta)
120 (beginning-of-buffer (message "Beginning of buffer"))
121 (end-of-buffer (message "End of buffer"))))
122 (if (and col-delta
123 (/= 0 col-delta))
124 (progn
125 (scroll-right col-delta)
126 ;; Make sure that the point stays on the visible screen
127 ;; (if truncation-lines in set).
128 ;; This code mimics the behavior we automatically get
129 ;; when doing vertical scrolling.
130 ;; Problem identified and a fix suggested by Tom Wurgler.
131 (cond
132 ((< (current-column) (window-hscroll))
133 (move-to-column (window-hscroll))) ; make on left column
134 ((> (- (current-column) (window-hscroll) (window-width) -2) 0)
135 (move-to-column (+ (window-width) (window-hscroll) -3))))))))
137 (defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
138 "Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
139 (while (sit-for mouse-scroll-delay)
140 (mouse-drag-safe-scroll row-delta col-delta)))
142 (defun mouse-drag-events-are-point-events-p (start-posn end-posn)
143 "Determine if START-POSN and END-POSN are \"close\"."
144 (let*
145 ((start-col-row (posn-col-row start-posn))
146 (end-col-row (posn-col-row end-posn)))
147 (and
148 ;; ;; We no longer exclude things by time.
149 ;; (< (- (posn-timestamp end-posn) (posn-timestamp start-posn))
150 ;; (if (numberp double-click-time)
151 ;; (* 2 double-click-time) ;; stretch it a little
152 ;; 999999)) ;; non-numeric => check by position alone
153 (= (car start-col-row) (car end-col-row))
154 (= (cdr start-col-row) (cdr end-col-row)))))
156 (defvar mouse-drag-electric-col-scrolling t
157 "If non-nil, mouse-drag on a long line enables truncate-lines.")
159 (defun mouse-drag-should-do-col-scrolling ()
160 "Determine if it's wise to enable col-scrolling for the current window.
161 Basically, we check for existing horizontal scrolling."
162 (or truncate-lines
163 (> (window-hscroll (selected-window)) 0)
164 (not (window-full-width-p))
165 (and
166 mouse-drag-electric-col-scrolling
167 (save-excursion ;; on a long line?
168 (let
169 ((beg (progn (beginning-of-line) (point)))
170 (end (progn (end-of-line) (point))))
171 (if (> (- end beg) (window-width))
172 (setq truncate-lines t)
173 nil))))))
175 (defvar mouse-throw-with-scroll-bar nil
176 "*Set direction of mouse-throwing.
177 If nil, the text moves in the direction the mouse moves.
178 If t, the scroll bar moves in the direction the mouse moves.")
179 (defconst mouse-throw-magnifier-min -6)
180 (defconst mouse-throw-magnifier-max 6)
181 (defconst mouse-throw-magnifier-base 1.5)
183 (defun mouse-drag-scroll-delta (mouse-delta)
184 ;; Limit the exponential explosion.
185 (setq mouse-delta
186 (max mouse-throw-magnifier-min
187 (min mouse-throw-magnifier-max mouse-delta)))
188 (* (round (exp (* (log mouse-throw-magnifier-base) (abs mouse-delta))))
189 (if (< mouse-delta 0) -1 1)
190 (if mouse-throw-with-scroll-bar 1 -1)))
193 (defun mouse-drag-throw (start-event)
194 "\"Throw\" the page according to a mouse drag.
196 A \"throw\" is scrolling the page at a speed relative to the distance
197 from the original mouse click to the current mouse location. Try it;
198 you'll like it. It's easier to observe than to explain.
200 If the mouse is clicked and released in the same place of time we
201 assume that the user didn't want to scdebugroll but wanted to whatever
202 mouse-2 used to do, so we pass it through.
204 Throw scrolling was inspired (but is not identical to) the \"hand\"
205 option in MacPaint, or the middle button in Tk text widgets.
207 If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
208 in the opposite direction. (Different people have different ideas
209 about which direction is natural. Perhaps it has to do with which
210 hemisphere you're in.)
212 To test this function, evaluate:
213 (global-set-key [down-mouse-2] 'mouse-drag-throw)"
214 (interactive "e")
215 ;; we want to do save-selected-window, but that requires 19.29
216 (let* ((start-posn (event-start start-event))
217 (start-window (posn-window start-posn))
218 (start-row (cdr (posn-col-row start-posn)))
219 (start-col (car (posn-col-row start-posn)))
220 (old-selected-window (selected-window))
221 event end row mouse-delta scroll-delta
222 have-scrolled
223 window-last-row
224 col mouse-col-delta window-last-col
225 (scroll-col-delta 0)
226 adjusted-mouse-col-delta
227 adjusted-mouse-delta
228 ;; be conservative about allowing horizontal scrolling
229 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
230 (select-window start-window)
231 (track-mouse
232 (while (progn
233 (setq event (read-event)
234 end (event-end event)
235 row (cdr (posn-col-row end))
236 col (car (posn-col-row end)))
237 (or (mouse-movement-p event)
238 (eq (car-safe event) 'switch-frame)))
239 (when (eq start-window (posn-window end))
240 (when col-scrolling-p
241 (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
242 (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
244 (if (or (/= 0 scroll-delta)
245 (/= 0 scroll-col-delta))
246 (progn
247 (setq have-scrolled t)
248 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
249 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
250 ;; If it was a click and not a drag, prepare to pass the event on.
251 ;; Is there a more correct way to reconstruct the event?
252 (if (and (not have-scrolled)
253 (mouse-drag-events-are-point-events-p start-posn end))
254 (push (cons (event-basic-type start-event) (cdr start-event))
255 unread-command-events))
256 ;; Now restore the old window.
257 (select-window old-selected-window)))
259 (defun mouse-drag-drag (start-event)
260 "\"Drag\" the page according to a mouse drag.
262 Drag scrolling moves the page according to the movement of the mouse.
263 You \"grab\" the character under the mouse and move it around.
265 If the mouse is clicked and released in the same place of time we
266 assume that the user didn't want to scroll but wanted to whatever
267 mouse-2 used to do, so we pass it through.
269 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
270 middle button in Tk text widgets.
272 To test this function, evaluate:
273 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
274 (interactive "e")
275 ;; we want to do save-selected-window, but that requires 19.29
276 (let* ((start-posn (event-start start-event))
277 (start-window (posn-window start-posn))
278 (start-row (cdr (posn-col-row start-posn)))
279 (start-col (car (posn-col-row start-posn)))
280 (old-selected-window (selected-window))
281 event end row mouse-delta scroll-delta
282 have-scrolled
283 window-last-row
284 col mouse-col-delta window-last-col
285 (scroll-col-delta 0)
286 ;; be conservative about allowing horizontal scrolling
287 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
288 (select-window start-window)
289 (setq window-last-row (- (window-height) 2)
290 window-last-col (- (window-width) 2))
291 (track-mouse
292 (while (progn
293 (setq event (read-event)
294 end (event-end event)
295 row (cdr (posn-col-row end))
296 col (car (posn-col-row end)))
297 (or (mouse-movement-p event)
298 (eq (car-safe event) 'switch-frame)))
299 ;; Scroll if see if we're on the edge.
300 ;; NEEDSWORK: should handle mouse-in-other window.
301 (cond
302 ((not (eq start-window (posn-window end)))
303 t) ; wait for return to original window
304 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
305 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
306 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
307 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
309 (setq scroll-delta (- row start-row)
310 start-row row)
311 (if col-scrolling-p
312 (setq scroll-col-delta (- col start-col)
313 start-col col))
314 (if (or (/= 0 scroll-delta)
315 (/= 0 scroll-col-delta))
316 (progn
317 (setq have-scrolled t)
318 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
319 ;; If it was a click and not a drag, prepare to pass the event on.
320 ;; Is there a more correct way to reconstruct the event?
321 (if (and (not have-scrolled)
322 (mouse-drag-events-are-point-events-p start-posn end))
323 (push (cons (event-basic-type start-event) (cdr start-event))
324 unread-command-events))
325 ;; Now restore the old window.
326 (select-window old-selected-window)))
329 (provide 'mouse-drag)
331 ;; arch-tag: e47354ff-82f5-42c4-b3dc-88dd9c04b770
332 ;;; mouse-drag.el ends here