向下滚动,没有错误“缓冲区的开头”

时间:2014-07-29 00:29:00

标签: emacs elisp

我正在寻找一些帮助,请以编程方式将特定窗口滚动到最开头,而不会产生error 缓冲区的开头(这必然会暂停该功能) 。当前(point)位于可见窗口内,但靠近底部。 scroll-down的行数可能会有所不同,所以我不能确切地确定需要多少行 - 但缓冲区的最开头是我正在寻找的。我不认为(save-excursion (goto-char (point-min)))会做我想做的事。

scroll-down没有针对特定窗口的参数,因此如果此问题的解决方案还可以包含使用特定窗口执行操作的示例(无需切换到该窗口),那将非常感激。换句话说,当这个功能在目标窗口中进行时,我想要在不同的窗口中。

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

2 个答案:

答案 0 :(得分:1)

您可以将scroll-down包裹在condition-case中,以便在发出错误信号时重新获得控制权:

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

我不确定您是否希望您的上一个代码段是您所指的功能的一部分,但您可以将其全部包含在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)))))

编辑:jpkotta在评论中提出了ignore-errors,我同意这看起来更干净:

(ignore-errors
  (scroll-down 20))

答案 1 :(得分:0)

(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.

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


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