gpt4 book ai didi

emacs - GNU Emacs 中的前段

转载 作者:行者123 更新时间:2023-12-05 04:18:30 24 4
gpt4 key购买 nike

我正在阅读 Robert J. Chassell 的“An Introduction to Programming in Emacs Lisp”。我有一个问题。在节点“fwd-para while”(forward-paragraphwhile 循环的解释)中,它说:

Interestingly, the loop count is not decremented until we leave the space between paragraphs, unless we come to the end of buffer or stop seeing the local value of the paragraph separator.

我不明白,谁能帮我解释一下?谢谢。

while 循环看起来像这样:

;; going forwards and not at the end of the buffer
(while (and (> arg 0) (not (eobp)))

;; between paragraphs
;; Move forward over separator lines...
(while (and (not (eobp))
(progn (move-to-left-margin) (not (eobp)))
(looking-at parsep))
(forward-line 1))
;; This decrements the loop
(unless (eobp) (setq arg (1- arg)))
;; ... and one more line.
(forward-line 1)

(if fill-prefix-regexp
;; There is a fill prefix; it overrides parstart;
;; we go forward line by line
(while (and (not (eobp))
(progn (move-to-left-margin) (not (eobp)))
(not (looking-at parsep))
(looking-at fill-prefix-regexp))
(forward-line 1))

;; There is no fill prefix;
;; we go forward character by character
(while (and (re-search-forward sp-parstart nil 1)
(progn (setq start (match-beginning 0))
(goto-char start)
(not (eobp)))
(progn (move-to-left-margin)
(not (looking-at parsep)))
(or (not (looking-at parstart))
(and use-hard-newlines
(not (get-text-property (1- start) 'hard)))))
(forward-char 1))

;; and if there is no fill prefix and if we are not at the end,
;; go to whatever was found in the regular expression search
;; for sp-parstart
(if (< (point) (point-max))
(goto-char start))))

感谢编辑和回答。我还有另外三个关于前段的问题:

  1. (progn (move-to-left-margin) (not (eobp))) 是什么意思?如果 (not (eobp)) 为真,那么 (progn (move-to-left-margin) (not (eobp))) 不应该总是为真吗?

  2. 关于这条线:

    ;; ... and one more line.
    (forward-line 1)

    为什么要多转发一行?

  3. 关于本段:

    This while loop has us searching forward for `sp-parstart', which is the combination of possible whitespace with a the local value of the start of a paragraph or of a paragraph separator.

    为什么 段落开头或段落分隔符的本地值?就我而言,段落分隔符的条件已经在 while 的第一个 while 循环中处理过了。

最佳答案

有趣!如果您查看 forward-paragraph 的文档字符串,您会看到:

Returns the count of paragraphs left to move.

这意味着如果你是从缓冲区末尾开始的一段,并且评估(forward-paragraph 3),它将返回2,这意味着它步进向前移动了一段,但无法执行您要求的其余两个步骤。

为了返回未执行的步数,forward-paragraph 返回(就在您引用的代码块之后)值 arg。因此 arg 必须仅在 forward-paragraph 实际向前移动一个段落时递减。我相信你能弄清楚剩下的。

(如果您想知道返回值在哪里使用,它在 fill-paragraph 中以避免在缓冲区末尾填充不存在的段落。)

预计到达时间:我看到您添加了三个新问题。我不会回答它们,而是会告诉您如何使用 Emacs Lisp debugger 尝试自己回答它们。单步执行 forward-paragraph 的代码,看看它实际做了什么。

  1. 通过键入 C-h C-f forward-paragraph RET 找到 forward-paragraph 的源代码,然后单击 *Help* 中的链接 缓冲区。

  2. 通过运行命令 eval-defun 重新编译 forward-paragraph 进行调试,例如键入 C-M-x

  3. 通过键入 M-x debug-on-entry RET forward-paragraph RET 打开 forward-paragraph 的调试。

  4. 转到合适的缓冲区并运行 forward-paragraph,例如键入 M-}

  5. 现在您处于调试器中,它会向您显示调用堆栈和正在评估的当前表单。您可以键入 d 进入该表单并查看其子表单的评估,或 c 完全评估表单并查看其结果,以及 q 退出调试器。 (有关更多调试器命令,请参阅 documentation。)

  6. 逐步执行 forward-paragraph,直到您了解它在做什么。

关于emacs - GNU Emacs 中的前段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com