1 ;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
3 ;; Copyright (C) 1996-1997, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: John Heidemann <johnh@ISI.EDU>
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/>.
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)
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
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 .emacs file:
51 ;; (global-set-key [down-mouse-2] 'mouse-drag-throw)
53 ;; (global-set-key [down-mouse-2] 'mouse-drag-drag)
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.
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
99 ;; - For mouse-drag-throw, we should try and place some visual indicator
100 ;; of the original mouse position (like Firefox does).
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
))
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"))))
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.
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\"."
141 ((start-col-row (posn-col-row start-posn
))
142 (end-col-row (posn-col-row end-posn
)))
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."
159 (> (window-hscroll (selected-window)) 0)
160 (not (window-full-width-p))
162 mouse-drag-electric-col-scrolling
163 (save-excursion ;; on a long line?
165 ((beg (line-beginning-position))
166 (end (progn (end-of-line) (point))))
167 (if (> (- end beg
) (window-width))
168 (setq truncate-lines t
)
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.
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)))
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 scdebugroll 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)"
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 mouse-delta scroll-delta
220 col mouse-col-delta window-last-col
222 adjusted-mouse-col-delta
224 ;; be conservative about allowing horizontal scrolling
225 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
226 (select-window start-window
)
229 (setq event
(read-event)
230 end
(event-end event
)
231 row
(cdr (posn-col-row end
))
232 col
(car (posn-col-row end
)))
233 (or (mouse-movement-p event
)
234 (eq (car-safe event
) 'switch-frame
)))
235 (when (eq start-window
(posn-window end
))
236 (when col-scrolling-p
237 (setq scroll-col-delta
(mouse-drag-scroll-delta (- start-col col
))))
238 (setq scroll-delta
(mouse-drag-scroll-delta (- start-row row
))))
240 (if (or (/= 0 scroll-delta
)
241 (/= 0 scroll-col-delta
))
243 (setq have-scrolled t
)
244 (mouse-drag-safe-scroll scroll-delta scroll-col-delta
)
245 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta
))))) ;xxx
246 ;; If it was a click and not a drag, prepare to pass the event on.
247 ;; Is there a more correct way to reconstruct the event?
248 (if (and (not have-scrolled
)
249 (mouse-drag-events-are-point-events-p start-posn end
))
250 (push (cons (event-basic-type start-event
) (cdr start-event
))
251 unread-command-events
))
252 ;; Now restore the old window.
253 (select-window old-selected-window
)))
256 (defun mouse-drag-drag (start-event)
257 "\"Drag\" the page according to a mouse drag.
259 Drag scrolling moves the page according to the movement of the mouse.
260 You \"grab\" the character under the mouse and move it around.
262 If the mouse is clicked and released in the same place of time we
263 assume that the user didn't want to scroll but wanted to whatever
264 mouse-2 used to do, so we pass it through.
266 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
267 middle button in Tk text widgets.
269 To test this function, evaluate:
270 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
272 ;; we want to do save-selected-window, but that requires 19.29
273 (let* ((start-posn (event-start start-event
))
274 (start-window (posn-window start-posn
))
275 (start-row (cdr (posn-col-row start-posn
)))
276 (start-col (car (posn-col-row start-posn
)))
277 (old-selected-window (selected-window))
278 event end row mouse-delta scroll-delta
281 col mouse-col-delta window-last-col
283 ;; be conservative about allowing horizontal scrolling
284 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
285 (select-window start-window
)
286 (setq window-last-row
(- (window-height) 2)
287 window-last-col
(- (window-width) 2))
290 (setq event
(read-event)
291 end
(event-end event
)
292 row
(cdr (posn-col-row end
))
293 col
(car (posn-col-row end
)))
294 (or (mouse-movement-p event
)
295 (eq (car-safe event
) 'switch-frame
)))
296 ;; Scroll if see if we're on the edge.
297 ;; NEEDSWORK: should handle mouse-in-other window.
299 ((not (eq start-window
(posn-window end
)))
300 t
) ; wait for return to original window
301 ((<= row
0) (mouse-drag-repeatedly-safe-scroll -
1 0))
302 ((>= row window-last-row
) (mouse-drag-repeatedly-safe-scroll 1 0))
303 ((and col-scrolling-p
(<= col
1)) (mouse-drag-repeatedly-safe-scroll 0 -
1))
304 ((and col-scrolling-p
(>= col window-last-col
)) (mouse-drag-repeatedly-safe-scroll 0 1))
306 (setq scroll-delta
(- row start-row
)
309 (setq scroll-col-delta
(- col start-col
)
311 (if (or (/= 0 scroll-delta
)
312 (/= 0 scroll-col-delta
))
314 (setq have-scrolled t
)
315 (mouse-drag-safe-scroll scroll-delta scroll-col-delta
)))))))
316 ;; If it was a click and not a drag, prepare to pass the event on.
317 ;; Is there a more correct way to reconstruct the event?
318 (if (and (not have-scrolled
)
319 (mouse-drag-events-are-point-events-p start-posn end
))
320 (push (cons (event-basic-type start-event
) (cdr start-event
))
321 unread-command-events
))
322 ;; Now restore the old window.
323 (select-window old-selected-window
)))
326 (provide 'mouse-drag
)
328 ;;; mouse-drag.el ends here