123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- (defun mouse-drag-safe-scroll (row-delta &optional col-delta)
- "Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
- Keep the cursor on the screen as needed."
- (let ((scroll-preserve-screen-position nil))
- (if (and row-delta
- (/= 0 row-delta))
- (condition-case nil
- (scroll-down row-delta)
- (beginning-of-buffer (message "Beginning of buffer"))
- (end-of-buffer (message "End of buffer"))))
- (if (and col-delta
- (/= 0 col-delta))
- (progn
- (scroll-right col-delta)
-
-
-
-
-
- (cond
- ((< (current-column) (window-hscroll))
- (move-to-column (window-hscroll)))
- ((> (- (current-column) (window-hscroll) (window-width) -2) 0)
- (move-to-column (+ (window-width) (window-hscroll) -3))))))))
- (defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
- "Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
- (while (sit-for mouse-scroll-delay)
- (mouse-drag-safe-scroll row-delta col-delta)))
- (defun mouse-drag-events-are-point-events-p (start-posn end-posn)
- "Determine if START-POSN and END-POSN are \"close\"."
- (let*
- ((start-col-row (posn-col-row start-posn))
- (end-col-row (posn-col-row end-posn)))
- (and
-
-
-
-
-
- (= (car start-col-row) (car end-col-row))
- (= (cdr start-col-row) (cdr end-col-row)))))
- (defvar mouse-drag-electric-col-scrolling t
- "If non-nil, mouse-drag on a long line enables truncate-lines.")
- (defun mouse-drag-should-do-col-scrolling ()
- "Determine if it's wise to enable col-scrolling for the current window.
- Basically, we check for existing horizontal scrolling."
- (or truncate-lines
- (> (window-hscroll (selected-window)) 0)
- (not (window-full-width-p))
- (and
- mouse-drag-electric-col-scrolling
- (save-excursion
- (let
- ((beg (line-beginning-position))
- (end (progn (end-of-line) (point))))
- (if (> (- end beg) (window-width))
- (setq truncate-lines t)
- nil))))))
- (defvar mouse-throw-with-scroll-bar nil
- "*Set direction of mouse-throwing.
- If nil, the text moves in the direction the mouse moves.
- If t, the scroll bar moves in the direction the mouse moves.")
- (defconst mouse-throw-magnifier-min -6)
- (defconst mouse-throw-magnifier-max 6)
- (defconst mouse-throw-magnifier-base 1.5)
- (defun mouse-drag-scroll-delta (mouse-delta)
-
- (setq mouse-delta
- (max mouse-throw-magnifier-min
- (min mouse-throw-magnifier-max mouse-delta)))
- (* (round (exp (* (log mouse-throw-magnifier-base) (abs mouse-delta))))
- (if (< mouse-delta 0) -1 1)
- (if mouse-throw-with-scroll-bar 1 -1)))
- (defun mouse-drag-throw (start-event)
- "\"Throw\" the page according to a mouse drag.
- A \"throw\" is scrolling the page at a speed relative to the distance
- from the original mouse click to the current mouse location. Try it;
- you'll like it. It's easier to observe than to explain.
- If the mouse is clicked and released in the same place of time we
- assume that the user didn't want to scroll but wanted to whatever
- mouse-2 used to do, so we pass it through.
- Throw scrolling was inspired (but is not identical to) the \"hand\"
- option in MacPaint, or the middle button in Tk text widgets.
- If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
- in the opposite direction. (Different people have different ideas
- about which direction is natural. Perhaps it has to do with which
- hemisphere you're in.)
- To test this function, evaluate:
- (global-set-key [down-mouse-2] 'mouse-drag-throw)"
- (interactive "e")
-
- (let* ((start-posn (event-start start-event))
- (start-window (posn-window start-posn))
- (start-row (cdr (posn-col-row start-posn)))
- (start-col (car (posn-col-row start-posn)))
- (old-selected-window (selected-window))
- event end row scroll-delta
- have-scrolled
- col
- (scroll-col-delta 0)
-
- (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
- (select-window start-window)
- (track-mouse
- (while (progn
- (setq event (read-event)
- end (event-end event)
- row (cdr (posn-col-row end))
- col (car (posn-col-row end)))
- (or (mouse-movement-p event)
- (eq (car-safe event) 'switch-frame)))
- (when (eq start-window (posn-window end))
- (when col-scrolling-p
- (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
- (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
- (if (or (/= 0 scroll-delta)
- (/= 0 scroll-col-delta))
- (progn
- (setq have-scrolled t)
- (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
- (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta)))))
-
-
- (if (and (not have-scrolled)
- (mouse-drag-events-are-point-events-p start-posn end))
- (push (cons (event-basic-type start-event) (cdr start-event))
- unread-command-events))
-
- (select-window old-selected-window)))
- (defun mouse-drag-drag (start-event)
- "\"Drag\" the page according to a mouse drag.
- Drag scrolling moves the page according to the movement of the mouse.
- You \"grab\" the character under the mouse and move it around.
- If the mouse is clicked and released in the same place of time we
- assume that the user didn't want to scroll but wanted to whatever
- mouse-2 used to do, so we pass it through.
- Drag scrolling is identical to the \"hand\" option in MacPaint, or the
- middle button in Tk text widgets.
- To test this function, evaluate:
- (global-set-key [down-mouse-2] 'mouse-drag-drag)"
- (interactive "e")
-
- (let* ((start-posn (event-start start-event))
- (start-window (posn-window start-posn))
- (start-row (cdr (posn-col-row start-posn)))
- (start-col (car (posn-col-row start-posn)))
- (old-selected-window (selected-window))
- event end row scroll-delta
- have-scrolled
- window-last-row
- col window-last-col
- (scroll-col-delta 0)
-
- (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
- (select-window start-window)
- (setq window-last-row (- (window-height) 2)
- window-last-col (- (window-width) 2))
- (track-mouse
- (while (progn
- (setq event (read-event)
- end (event-end event)
- row (cdr (posn-col-row end))
- col (car (posn-col-row end)))
- (or (mouse-movement-p event)
- (eq (car-safe event) 'switch-frame)))
-
-
- (cond
- ((not (eq start-window (posn-window end)))
- t)
- ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
- ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
- ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
- ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
- (t
- (setq scroll-delta (- row start-row)
- start-row row)
- (if col-scrolling-p
- (setq scroll-col-delta (- col start-col)
- start-col col))
- (if (or (/= 0 scroll-delta)
- (/= 0 scroll-col-delta))
- (progn
- (setq have-scrolled t)
- (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
-
-
- (if (and (not have-scrolled)
- (mouse-drag-events-are-point-events-p start-posn end))
- (push (cons (event-basic-type start-event) (cdr start-event))
- unread-command-events))
-
- (select-window old-selected-window)))
- (provide 'mouse-drag)
|