1

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?

ideasman42
  • 8,786
  • 1
  • 32
  • 114
  • 1
    oops, should have been scroll up. – ideasman42 Jul 19 '19 at 03: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 03:08
  • Right, this is what I see too, no scrolling in this case. - Updated question. – ideasman42 Jul 19 '19 at 03:14
  • Perhaps using a condition-case to "[r]egain control when an error is signaled" might be helpful .... – lawlist Jul 19 '19 at 03:26

2 Answers2

1

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)
    (end-of-buffer
      (forward-line 10000))))
(put 'scroll-up-lots 'scroll-command t)

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

0

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)
      (end-of-buffer
        (let ((lines (- (count-lines (window-start) (point-max)) 1)))
          (when (> lines 0) (scroll-up lines)))))))
ideasman42
  • 8,786
  • 1
  • 32
  • 114