Go to the first, previous, next, last section, table of contents.

forward-paragraph: a Goldmine of Functions

The forward-paragraph function moves point forward to the end of the paragraph. It is usually bound to M-} and makes use of a number of functions that are important in themselves, including let*, match-beginning, and looking-at.

The function definition for forward-paragraph is considerably longer than the function definition for forward-sentence because it works with a paragraph, each line of which may begin with a fill prefix.

A fill prefix consists of a string of characters that are repeated at the beginning of each line. For example, in Lisp code, it is a convention to start each line of a paragraph-long comment with `;;; '. In Text mode, four blank spaces make up another common fill prefix, creating an indented paragraph. (See section `Fill Prefix' in The GNU Emacs Manual, for more information about fill prefixes.)

The existence of a fill prefix means that in addition to being able to find the end of a paragraph whose lines begin on the left-most column, the forward-paragraph function must be able to find the end of a paragraph when all or many of the lines in the buffer begin with the fill prefix.

Moreover, it is sometimes practical to ignore a fill prefix that exists, especially when blank lines separate paragraphs. This is an added complication.

Rather than print all of the forward-paragraph function, we will only print parts of it. Read without preparation, the function can be daunting!

In outline, the function looks like this:

(defun forward-paragraph (&optional arg)
  (interactive "p")
  (or arg (setq arg 1))
    (while (< arg 0)        ; backward-moving-code
      (setq arg (1+ arg)))
    (while (> arg 0)        ; forward-moving-code
      (setq arg (1- arg)))))

The first parts of the function are routine: the function's argument list consists of one optional argument. Documentation follows.

The lower case `p' in the interactive declaration means that the processed prefix argument, if any, is passed to the function. This will be a number, and is the repeat count of how many paragraphs point will move. The or expression in the next line handles the common case when no argument is passed to the function, which occurs if the function is called from other code rather than interactively. This case was described earlier. (See section forward-sentence.) Now we reach the end of the familiar part of this function.

Go to the first, previous, next, last section, table of contents.