When calling (scroll-up N) with a large value that would exceed the buffer length - nothing happens.

For example:

(global-set-key (kbd "<f4>") (lambda () (interactive) (scroll-up 10000)))

Prints End of buffer but doesn't scroll.

Is there a way to scroll that clamps to buffer bounds instead of failing silently?

  • 1
    oops, should have been scroll up. – ideasman42 Jul 19 '19 at 3:04
  • In my test evaluating (scroll-up LARGE-NUMBER), I see an "End of buffer" in the echo area and *Messages* buffer -- this is a quit/error message ... An example of how to get Emacs to fail silently without the quit/error message may be helpful to other forum participants. – lawlist Jul 19 '19 at 3:08
  • Right, this is what I see too, no scrolling in this case. - Updated question. – ideasman42 Jul 19 '19 at 3:14
  • Perhaps using a condition-case to "[r]egain control when an error is signaled" might be helpful .... – lawlist Jul 19 '19 at 3:26

PageDown runs scroll-up-command which already does something similar if scroll-error-top-bottom is non-nil (it's nil by default). It works by calling scroll-up and catching the error. So either call scroll-up-command with scroll-error-top-bottom set to non-nil or do the same error catching.

(defun scroll-up-lots ()
  (interactive "^P")
  (condition-case nil
      (scroll-up 10000)
      (forward-line 10000))))
(put 'scroll-up-lots 'scroll-command t)

The scroll-command property makes the command behave with scroll-preserve-screen-position.


Adding my own answer thanks to @Gilles's since it adjusts scroll instead moving the cursor with forward lines.

(defun scroll-by-lines (lines)
  (if (< lines 0)
    (condition-case nil
      (scroll-down (- lines))
      (beginning-of-buffer nil))
    (condition-case nil
      (scroll-up lines)
        (let ((lines (- (count-lines (window-start) (point-max)) 1)))
          (when (> lines 0) (scroll-up lines)))))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.