lawlist
lawlist

Reputation: 13447

Scroll-down all the way without error "beginning of buffer"

I'm looking for some assistance, please, to programmatically scroll a particular window to the very beginning without incurring an error beginning of buffer (which necessarily halts the function). The current (point) is within the visible window, but near the bottom. The number of lines to scroll-down may vary, so I cannot say for sure exactly how many is needed -- but the very beginning of the buffer is what I'm looking for. I don't think (save-excursion (goto-char (point-min))) will do what I want.

scroll-down does not have an argument for a specific window, so if the solution to this question could also include an example to perform the action with a specific window (without switching to it), that would be greatly appreciated. In other words, I'd like to be in a different window while this function is going on in the target window.

(when
    (not (pos-visible-in-window-p (point-min)
       (get-buffer-window calendar-buffer (selected-frame))))
  (scroll-down 20))

Upvotes: 0

Views: 184

Answers (2)

lawlist
lawlist

Reputation: 13447

(let ((win (get-buffer-window calendar-buffer (selected-frame))))
  (when (not (pos-visible-in-window-p (point-min) win))
    (set-window-start win (point-min))))

`set-window-start` is a built-in function in `C source code'.

(set-window-start WINDOW POS &optional NOFORCE)

Make display in WINDOW start at position POS in WINDOW's buffer.
WINDOW must be a live window and defaults to the selected one.  Return
POS.  Optional third arg NOFORCE non-nil inhibits next redisplay from
overriding motion of point in order to display at this exact start.

See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Window-Start-and-End.html


See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Selecting-Windows.html

Upvotes: 0

Dan
Dan

Reputation: 5369

You can wrap scroll-down in a condition-case to regain control when an error is signaled:

(condition-case nil
    (scroll-down 20)
  (error nil))

I wasn't exactly sure if you wanted your last code snippet was part of the function you were referring to, but you can wrap it all in a save-selected-window:

(let ((cal (get-buffer-window calendar-buffer (selected-frame))))
  (when cal
    (save-selected-window
      (select-window cal)
      (condition-case nil
          (scroll-down 20)
        (error nil)))))

EDIT: jpkotta suggested ignore-errors in the comment, which I agree looks cleaner:

(ignore-errors
  (scroll-down 20))

Upvotes: 1

Related Questions