Change defgeneric so it doesn't completely redefine the function
[emacs.git] / lisp / mouse-drag.el
blob88838edaaede2035d6324790bbce94964f180a0d
1 ;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
3 ;; Copyright (C) 1996-1997, 2001-2015 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 (while (progn
226 (setq event (read-event)
227 end (event-end event)
228 row (cdr (posn-col-row end))
229 col (car (posn-col-row end)))
230 (or (mouse-movement-p event)
231 (eq (car-safe event) 'switch-frame)))
232 (when (eq start-window (posn-window end))
233 (when col-scrolling-p
234 (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
235 (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
237 (if (or (/= 0 scroll-delta)
238 (/= 0 scroll-col-delta))
239 (progn
240 (setq have-scrolled t)
241 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
242 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
243 ;; If it was a click and not a drag, prepare to pass the event on.
244 ;; Is there a more correct way to reconstruct the event?
245 (if (and (not have-scrolled)
246 (mouse-drag-events-are-point-events-p start-posn end))
247 (push (cons (event-basic-type start-event) (cdr start-event))
248 unread-command-events))
249 ;; Now restore the old window.
250 (select-window old-selected-window)))
252 ;;;###autoload
253 (defun mouse-drag-drag (start-event)
254 "\"Drag\" the page according to a mouse drag.
256 Drag scrolling moves the page according to the movement of the mouse.
257 You \"grab\" the character under the mouse and move it around.
259 If the mouse is clicked and released in the same place of time we
260 assume that the user didn't want to scroll but wanted to whatever
261 mouse-2 used to do, so we pass it through.
263 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
264 middle button in Tk text widgets.
266 To test this function, evaluate:
267 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
268 (interactive "e")
269 ;; we want to do save-selected-window, but that requires 19.29
270 (let* ((start-posn (event-start start-event))
271 (start-window (posn-window start-posn))
272 (start-row (cdr (posn-col-row start-posn)))
273 (start-col (car (posn-col-row start-posn)))
274 (old-selected-window (selected-window))
275 event end row scroll-delta
276 have-scrolled
277 window-last-row
278 col window-last-col
279 (scroll-col-delta 0)
280 ;; be conservative about allowing horizontal scrolling
281 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
282 (select-window start-window)
283 (setq window-last-row (- (window-height) 2)
284 window-last-col (- (window-width) 2))
285 (track-mouse
286 (while (progn
287 (setq event (read-event)
288 end (event-end event)
289 row (cdr (posn-col-row end))
290 col (car (posn-col-row end)))
291 (or (mouse-movement-p event)
292 (eq (car-safe event) 'switch-frame)))
293 ;; Scroll if see if we're on the edge.
294 ;; FIXME: should handle mouse-in-other window.
295 (cond
296 ((not (eq start-window (posn-window end)))
297 t) ; wait for return to original window
298 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
299 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
300 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
301 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
303 (setq scroll-delta (- row start-row)
304 start-row row)
305 (if col-scrolling-p
306 (setq scroll-col-delta (- col start-col)
307 start-col col))
308 (if (or (/= 0 scroll-delta)
309 (/= 0 scroll-col-delta))
310 (progn
311 (setq have-scrolled t)
312 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
313 ;; If it was a click and not a drag, prepare to pass the event on.
314 ;; Is there a more correct way to reconstruct the event?
315 (if (and (not have-scrolled)
316 (mouse-drag-events-are-point-events-p start-posn end))
317 (push (cons (event-basic-type start-event) (cdr start-event))
318 unread-command-events))
319 ;; Now restore the old window.
320 (select-window old-selected-window)))
323 (provide 'mouse-drag)
325 ;;; mouse-drag.el ends here