I’m reading “An Introduction to Programming in Emacs Lisp” by Robert J. Chassell. And I have a question. In the node “fwd-para while” (explanation of the while loop of forward-paragraph), it says:
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.
I can’t understand it, can somebody explain it for me? Thanks.
The while loop looks like this:
;; 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))))
Thanks for editing and answering. I have another three questions about forward-paragraph:
-
What is the meaning of
(progn (move-to-left-margin) (not (eobp)))? If(not (eobp))is true, shouldn’t(progn (move-to-left-margin) (not (eobp)))always be true? -
About this line:
;; ... and one more line. (forward-line 1)Why forwarding one more line?
-
About this paragraph:
This
whileloop 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.Why does
the local value of the start of a paragraph or of a paragraph separator? As far as I’m concerned, the condition of the paragraph separator has already been processed in the firstwhileloop of thewhile.
It’s not that interesting! If you look at the docstring for
forward-paragraphyou’ll see that it:That means that if you are one paragraph from the end of the buffer, and evaluate
(forward-paragraph 3), it will return2, meaning that it stepped forward by one paragraph, but the remaining two steps that you requested could not be taken.In order to return the number of steps not taken,
forward-paragraphreturns (just after the block of code you’ve quoted) the valuearg. Soargmust only be decremented whenforward-paragraphactually steps forward by a paragraph. I’m sure you can figure out the rest.(If you’re wondering where the return value is used, it’s in
fill-paragraphto avoid filling a non-existent paragraph at the end of the buffer.)ETA: I see that you’ve added three new questions. Instead of answering them, I’ll tell you how you might try to answer them for yourself by using the Emacs Lisp debugger to step through the code for
forward-paragraphand see what it is actually doing.Find the source code for
forward-paragraphby typing C-h C-f forward-paragraph RET and then clicking on the link in the*Help*buffer.Recompile
forward-paragraphfor debugging by running the commandeval-defun, for example by typing C-M-x.Turn on debugging for
forward-paragraphby typing M-x debug-on-entry RET forward-paragraph RET.Go to a suitable buffer and run
forward-paragraph, for example by typing M-}.Now you’re in the debugger, which shows you the call stack and the current form being evaluated. You can type d to step into that form and see the evaluation of its subforms, or c to completely evaluate the form and see its result, and q to exit the debugger. (See the documentation for more debugger commands.)
Step through the execution of
forward-paragraphuntil you understand what it is doing.