EmacsWiki: smooth-scroll.el (original) (raw)

Download

(defconst smooth-scroll/version "1.1")

(eval-when-compile (require 'cl) (require 'easy-mmode))

(defgroup smooth-scroll nil "Scroll window smoothly." :group 'editing)

(defcustom smooth-scroll/vscroll-step-size 2 "Amount of lines, which determines quality of smooth vertical scrolling. The small number makes it better, the large number makes it rough.

This value must be a positive number, otherwise `1' will be used.

Default value is `2'." :type 'integer :group 'smooth-scroll)

(defcustom smooth-scroll/hscroll-step-size 2 "Amount of columns, which determines quality of smooth horizontal scrolling. The small number makes it better, the large number makes it rough.

This value must be a positive number, otherwise `1' will be used.

Default value is `2'." :type 'integer :group 'smooth-scroll)

(defun scroll-up-1 (&optional arg) "Scroll text of selected window upward ARG lines. If ARG is omitted or nil, scroll upward by a line. Negative ARG means scroll downward. If ARG is the atom -', scroll downward by a line. When calling from a program, supply as argument a number, nil, or -'.

After scrolling, position of the cursor will be kept when possible." (interactive "P") (let ((amount (case arg ((-) -1) ((nil) 1) (t arg)))) (scroll-up amount)))

(defun scroll-down-1 (&optional arg) "Scroll text of selected window down ARG lines. If ARG is omitted or nil, scroll down by a line. Negative ARG means scroll upward. If ARG is the atom -', scroll upward by a line. When calling from a program, supply as argument a number, nil, or -'.

After scrolling, position of the cursor will be kept when possible." (interactive "P") (let ((amount (case arg ((-) -1) ((nil) 1) (t arg)))) (scroll-down amount)))

(defun scroll-left-1 (&optional arg) "Scroll selected window display ARG columns left. If ARG is omitted or nil, scroll left by a column. Negative ARG means scroll rightward. If ARG is the atom -', scroll right by a column. When calling from a program, supply as argument a number, nil, or -'.

After scrolling, position of the cursor will be kept when possible." (interactive "P") (let ((amount (case arg ((-) -1) ((nil) 1) (t arg)))) (scroll-left amount)))

(defun scroll-right-1 (&optional arg) "Scroll selected window display ARG columns right. If ARG is omitted or nil, scroll right by a column. Negative ARG means scroll leftward. If ARG is the atom -', scroll left by a column. When calling from a program, supply as argument a number, nil, or -'.

After scrolling, position of the cursor will be kept when possible." (interactive "P") (let ((amount (case arg ((-) -1) ((nil) 1) (t arg)))) (scroll-right amount)))

(defvar smooth-scroll/.scrolling-p nil "Private variable used in `smooth-scroll-mode'.")

(defvar smooth-scroll/.debugging-p nil "Private variable used in `smooth-scroll-mode'.")

(defmacro smooth-scroll/.run-without-recursive-call (&rest body) `(when (and (not smooth-scroll/.scrolling-p) (not (smooth-scroll/.drop-events))) (prog2 (setq smooth-scroll/.scrolling-p t) (unwind-protect (progn ,@body) (setq smooth-scroll/.scrolling-p nil)))))

(defun smooth-scroll/.debug-msg (str) (when smooth-scroll/.debugging-p (with-current-buffer (get-buffer-create "Messages") (goto-char (point-max)) (insert str) (goto-char (point-max))

  )))

(defun smooth-scroll/.drop-events ()

(when (input-pending-p) (smooth-scroll/.debug-msg "smooth-scroll/.drop-events: dropped event.\n") (let (ev) (while (and (input-pending-p) (setq ev (read-event))) (smooth-scroll/.debug-msg (format ""%s"\n" ev)))) t))

(defvar smooth-scroll/redisplay-interval (when (not window-system) 0.005) "Private variable used in `smooth-scroll-mode'.")

(defun smooth-scroll/.force-redisplay () (while (and (not executing-kbd-macro) (not (redisplay t))) (smooth-scroll/.debug-msg "Redisplay was not run.")) (when (numberp smooth-scroll/redisplay-interval)

(sleep-for smooth-scroll/redisplay-interval)))

(defun smooth-scroll/scroll-up (&optional arg) "Scroll text of selected window upward ARG lines. If ARG is omitted or nil, scroll upward by a near full screen. A near full screen is next-screen-context-lines' less than a full screen. Negative ARG means scroll downward. If ARG is the atom -', scroll downward by nearly full screen. When calling from a program, supply as argument a number, nil, or `-'." (interactive "P") (smooth-scroll/.vscroll-aux arg t))

(defun smooth-scroll/scroll-down (&optional arg) "Scroll text of selected window down ARG lines. If ARG is omitted or nil, scroll down by a near full screen. A near full screen is next-screen-context-lines' less than a full screen. Negative ARG means scroll upward. If ARG is the atom -', scroll upward by nearly full screen. When calling from a program, supply as argument a number, nil, or `-'." (interactive "P") (smooth-scroll/.vscroll-aux arg nil))

(defun smooth-scroll/scroll-other-window (&optional arg) "Scroll next window upward ARG lines; or near full screen if no ARG. A near full screen is next-screen-context-lines' less than a full screen. The next window is the one below the current one; or the one at the top if the current one is at the bottom. Negative ARG means scroll downward. If ARG is the atom -', scroll downward by nearly full screen. When calling from a program, supply as argument a number, nil, or `-'.

If other-window-scroll-buffer' is non-nil, scroll the window showing that buffer, popping the buffer up if necessary. If in the minibuffer, minibuffer-scroll-window' if non-nil specifies the window to scroll. This takes precedence over `other-window-scroll-buffer'." (interactive "P") (let ((orig-fn (symbol-function 'smooth-scroll/orig-scroll-up))) (unwind-protect (progn (setf (symbol-function 'smooth-scroll/orig-scroll-up) (symbol-function 'smooth-scroll/orig-scroll-other-window)) (smooth-scroll/.vscroll-aux arg t)) (setf (symbol-function 'smooth-scroll/orig-scroll-up) orig-fn))))

(defun smooth-scroll/scroll-other-window-down (&optional arg) "Scroll the "other window" down. For more details, see the documentation for `smooth-scroll/scroll-other-window'." (interactive "P") (let ((orig-fn (symbol-function 'smooth-scroll/orig-scroll-up))) (unwind-protect (progn (setf (symbol-function 'smooth-scroll/orig-scroll-up) (symbol-function 'smooth-scroll/orig-scroll-other-window)) (smooth-scroll/.vscroll-aux arg t t)) (setf (symbol-function 'smooth-scroll/orig-scroll-up) orig-fn))))

(defun smooth-scroll/.vscroll-aux (amount up-p &optional inverse) "Private function used in `smooth-scroll-mode'." (smooth-scroll/.run-without-recursive-call

 (when (listp amount)
   (setq amount (first amount)))

(when (eq amount '-) (setq amount nil) (setq up-p (not up-p))) (when (minusp (or amount 0)) (setq amount (- amount)) (setq up-p (not up-p)))

(or amount (setq amount (- (window-height) 1
(if mode-line-format 1 0) (if header-line-format 1 0) (or next-screen-context-lines 0))))

(while (> amount 0) (let ((delta (min amount (max smooth-scroll/vscroll-step-size 1))))

   (if up-p
     (if (fboundp 'smooth-scroll/orig-scroll-up) 
         (smooth-scroll/orig-scroll-up (if inverse (- delta) delta)))
     (if (fboundp 'smooth-scroll/orig-scroll-down) 
         (smooth-scroll/orig-scroll-down (if inverse (- delta) delta))))
   (smooth-scroll/.force-redisplay)
   (setq amount (- amount delta))))

amount))

(defun smooth-scroll/scroll-left (&optional arg set-minimum) "Scroll selected window display ARG columns left. Default for ARG is window width minus 2. Value is the total amount of leftward horizontal scrolling in effect after the change. If SET-MINIMUM is non-nil, the new scroll amount becomes the lower bound for automatic scrolling, i.e. automatic scrolling will not scroll a window to a column less than the value returned by this function. This happens in an interactive call." (interactive "P")

(smooth-scroll/.hscroll-aux arg t))

(defun smooth-scroll/scroll-right (&optional arg set-minimum) "Scroll selected window display ARG columns right. Default for ARG is window width minus 2. Value is the total amount of leftward horizontal scrolling in effect after the change. If SET-MINIMUM is non-nil, the new scroll amount becomes the lower bound for automatic scrolling, i.e. automatic scrolling will not scroll a window to a column less than the value returned by this function. This happens in an interactive call." (interactive "P")

(smooth-scroll/.hscroll-aux arg nil))

(defun smooth-scroll/.keep-cursor-within-new-hscroll-margin (delta left-p)

(let* ((col-num (- (point) (point-at-bol))) (hscroll (window-hscroll)) (required-margin (+ hscroll-margin delta 1)))

(if left-p 
  
  (let ((left-margin (- col-num hscroll)))
    (when (< left-margin required-margin)
      
      (forward-char (min (- required-margin left-margin)
                         
                         (- (point-at-eol) (point))))))
  
  (let ((right-margin (- (+ (window-width) hscroll) col-num)))
    (when (< right-margin required-margin)
      (backward-char (min (- required-margin right-margin)
                          
                          col-num)))))))

(defun smooth-scroll/.do-primitive-hscroll (delta left-p) (if left-p (if (fboundp 'smooth-scroll/orig-scroll-left) (smooth-scroll/orig-scroll-left delta)) (if (fboundp 'smooth-scroll/orig-scroll-right) (smooth-scroll/orig-scroll-right delta))))

(defvar smooth-scroll/.pre-command-hook nil) (defvar smooth-scroll/.orig-auto-hscroll-mode nil)

(defun smooth-scroll/.restore-auto-hscroll-mode (orig-auto-hscroll-mode) (when (not smooth-scroll/.pre-command-hook) (setq smooth-scroll/.orig-auto-hscroll-mode orig-auto-hscroll-mode) (setq smooth-scroll/.pre-command-hook #'(lambda () (when (or (not (symbolp this-command)) (not (get this-command 'scroll-command-p))) (progn (smooth-scroll/.debug-msg (format "this-command: %s\n" this-command)) (setq auto-hscroll-mode smooth-scroll/.orig-auto-hscroll-mode) (remove-hook 'pre-command-hook smooth-scroll/.pre-command-hook) (setq smooth-scroll/.pre-command-hook nil))))) (add-hook 'pre-command-hook smooth-scroll/.pre-command-hook)))

(defun smooth-scroll/.hscroll-aux (amount left-p) (smooth-scroll/.run-without-recursive-call (if (not truncate-lines) (message "[smooth-scroll] hscroll won't work when the value of truncate-line' is nil'.")

 (when (listp amount)
   (setq amount (first amount)))
 
 
 (when (eq amount '-)
   (setq amount nil)
   (setq left-p (not left-p)))
 (when (minusp (or amount 0))
   (setq amount (- amount))
   (setq left-p (not left-p)))
 
 
 (or amount
     (setq amount (- (window-width)
                     2)))

 (let ((orig-auto-p auto-hscroll-mode))
   
   (setq auto-hscroll-mode nil)
   
   (while (> amount 0)
     (let ((delta (min amount
                       (max smooth-scroll/hscroll-step-size 1))))
       (smooth-scroll/.keep-cursor-within-new-hscroll-margin delta left-p)
       (smooth-scroll/.do-primitive-hscroll delta left-p)
       (smooth-scroll/.force-redisplay)
       (decf amount delta)))
   
   (smooth-scroll/.restore-auto-hscroll-mode orig-auto-p)
   
   amount))))

(easy-mmode-define-minor-mode smooth-scroll-mode "Minor mode for smooth scrolling and in-place scrolling." :global t :init-value nil :lighter " SScr" (if (or (not (boundp 'smooth-scroll-mode)) smooth-scroll-mode) (progn

 (setf (symbol-function 'scroll-up)
         (symbol-function 'smooth-scroll/scroll-up))
 (setf (symbol-function 'scroll-down)
         (symbol-function 'smooth-scroll/scroll-down))
 (setf (symbol-function 'scroll-other-window)
         (symbol-function 'smooth-scroll/scroll-other-window))
 (setf (symbol-function 'scroll-other-window-down)
         (symbol-function 'smooth-scroll/scroll-other-window-down))
 (setf (symbol-function 'scroll-left)
         (symbol-function 'smooth-scroll/scroll-left))
 (setf (symbol-function 'scroll-right)
         (symbol-function 'smooth-scroll/scroll-right))

 
 (setq smooth-scroll/.scrolling-p nil)
 (when smooth-scroll/.pre-command-hook
   (remove-hook 'pre-command-hook smooth-scroll/.pre-command-hook)
   (setq smooth-scroll/.pre-command-hook nil)))

(progn

 (setf (symbol-function 'scroll-up)
         (symbol-function 'smooth-scroll/orig-scroll-up))
 (setf (symbol-function 'scroll-down)
         (symbol-function 'smooth-scroll/orig-scroll-down))
 (setf (symbol-function 'scroll-other-window)
         (symbol-function 'smooth-scroll/orig-scroll-other-window))
 (setf (symbol-function 'scroll-other-window-down)
         (symbol-function 'smooth-scroll/orig-scroll-other-window-down))
 (setf (symbol-function 'scroll-left)
         (symbol-function 'smooth-scroll/orig-scroll-left))
 (setf (symbol-function 'scroll-right)
         (symbol-function 'smooth-scroll/orig-scroll-right)))))

(when (not (featurep 'smooth-scroll))

(when (not (fboundp 'smooth-scroll/orig-scroll-up)) (setf (symbol-function 'smooth-scroll/orig-scroll-up) (symbol-function 'scroll-up))) (when (not (fboundp 'smooth-scroll/orig-scroll-down)) (setf (symbol-function 'smooth-scroll/orig-scroll-down) (symbol-function 'scroll-down))) (when (not (fboundp 'smooth-scroll/orig-scroll-other-window)) (setf (symbol-function 'smooth-scroll/orig-scroll-other-window) (symbol-function 'scroll-other-window))) (require 'simple) (when (not (fboundp 'smooth-scroll/orig-scroll-other-window-down)) (setf (symbol-function 'smooth-scroll/orig-scroll-other-window-down) (symbol-function 'scroll-other-window-down))) (when (not (fboundp 'smooth-scroll/orig-scroll-left)) (setf (symbol-function 'smooth-scroll/orig-scroll-left) (symbol-function 'scroll-left))) (when (not (fboundp 'smooth-scroll/orig-scroll-right)) (setf (symbol-function 'smooth-scroll/orig-scroll-right) (symbol-function 'scroll-right)))

(put 'scroll-up 'scroll-command-p t) (put 'scroll-down 'scroll-command-p t) (put 'scroll-other-window 'scroll-command-p t) (put 'scroll-other-window-down 'scroll-command-p t) (put 'scroll-left 'scroll-command-p t) (put 'scroll-right 'scroll-command-p t) (put 'scroll-up-1 'scroll-command-p t) (put 'scroll-down-1 'scroll-command-p t) (put 'scroll-left-1 'scroll-command-p t) (put 'scroll-right-1 'scroll-command-p t))

(provide 'smooth-scroll)