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