Function: LaTeX-fill-region-as-paragraph

LaTeX-fill-region-as-paragraph is an interactive and byte-compiled function defined in latex.el.

Signature

(LaTeX-fill-region-as-paragraph FROM TO &optional JUSTIFY-FLAG)

Documentation

Fill region as one paragraph.

Break lines to fit fill-column, but leave all lines ending with
\\ (plus its optional argument) alone. Lines with code
comments and lines ending with \par are included in filling but act as boundaries. Prefix arg means justify too. From program, pass args FROM, TO and JUSTIFY-FLAG.

You can disable filling inside a specific environment by adding it to LaTeX-indent-environment-list, only indentation is performed in that case.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-fill-region-as-paragraph (from to &optional justify-flag)
  "Fill region as one paragraph.
Break lines to fit `fill-column', but leave all lines ending with
\\\\ \(plus its optional argument) alone.  Lines with code
comments and lines ending with `\\par' are included in filling but
act as boundaries.  Prefix arg means justify too.  From program,
pass args FROM, TO and JUSTIFY-FLAG.

You can disable filling inside a specific environment by adding
it to `LaTeX-indent-environment-list', only indentation is
performed in that case."
  (declare (modes LaTeX-mode))
  (interactive "*r\nP")
  (let ((end-marker (copy-marker to)) has-code-comment has-regexp-match)
    (if (or (assoc (LaTeX-current-environment) LaTeX-indent-environment-list)
            (member (TeX-current-macro) LaTeX-fill-excluded-macros)
            ;; This could be generalized, if there are more cases where
            ;; a special string at the start of a region to fill should
            ;; inhibit filling.
            (progn (save-excursion (goto-char from)
                                   (looking-at (concat TeX-comment-start-regexp
                                                       "+[ \t]*"
                                                       "Local Variables:")))))
        ;; Filling disabled, only do indentation.
        (indent-region from to nil)
      ;; XXX: This `save-restriction' is a leftover of older codes and
      ;; can now be removed.
      (save-restriction
        (goto-char from)
        (while (< (point) end-marker)
          ;; Code comments.
          (catch 'found
            (while (setq has-code-comment
                         (TeX-search-forward-comment-start end-marker))
              ;; See if there is at least one non-whitespace
              ;; character before the comment starts.
              (goto-char has-code-comment)
              (skip-chars-backward " \t" (line-beginning-position))
              (if (not (bolp))
                  ;; A real code comment.
                  (throw 'found t)
                ;; Not a code comment.  Continue the loop.
                (forward-line 1)
                (if (> (point) end-marker)
                    (goto-char end-marker)))))

          ;; Go back to the former point for the next regexp search.
          (goto-char from)

          (when (setq has-regexp-match
                      (re-search-forward
                       (concat
                        "\\("
                        ;; Lines ending with `\par'.
                        ;; XXX: Why exclude \n?  vv
                        "\\(?:\\=\\|[^" TeX-esc "\n]\\)\\(?:"
                        (regexp-quote (concat TeX-esc TeX-esc))
                        "\\)*"
                        (regexp-quote TeX-esc) "par[ \t]*"
                        ;; XXX: What's this "whitespaces in braces" ?
                        ;;    vvvvvvvv
                        "\\(?:{[ \t]*}\\)?[ \t]*$"
                        "\\)\\|"
                        ;; Lines ending with `\\'.
                        ;; XXX: This matches a line ending with "\\\ ".
                        ;; Should we avoid such corner case?
                        (regexp-quote (concat TeX-esc TeX-esc))
                        ;; XXX: Why not just "\\s-*\\*?" ?
                        "\\(?:\\s-*\\*\\)?"
                        ;; XXX: Why not "\\s-*\\(?:\\[[^]]*\\]\\)?" ?
                        "\\(?:\\s-*\\[[^]]*\\]\\)?"
                        "\\s-*$")
                       (or has-code-comment end-marker) t))
            ;; The regexp matched before the code comment (if any).
            (setq has-code-comment nil))

          ;; Here no need to go back to the former position because
          ;; "ELSE" part of the following `if' doesn't rely on the
          ;; current point.
          ;; (goto-char from)

          (if (or has-code-comment has-regexp-match)
              (progn
                (goto-char (or has-code-comment has-regexp-match))
                (goto-char (line-end-position))
                (delete-horizontal-space)
                ;; I doubt very much if we want justify -
                ;; this is a line with \\
                ;; if you think otherwise - uncomment the next line
                ;; (and justify-flag (justify-current-line))
                (forward-char)
                ;; keep our position in a buffer
                (save-excursion
                  ;; Code comments and lines ending with `\par' are
                  ;; included in filling.  Lines ending with `\\' are
                  ;; skipped.
                  (if (or has-code-comment
                          (match-beginning 1))
                      (LaTeX-fill-region-as-para-do from (point) justify-flag)
                    (LaTeX-fill-region-as-para-do
                     from (line-beginning-position 0) justify-flag)
                    ;; At least indent the line ending with `\\'.
                    (indent-according-to-mode)))
                (setq from (point)))
            ;; ELSE part follows - loop termination relies on a fact
            ;; that (LaTeX-fill-region-as-para-do) moves point past
            ;; the filled region
            (LaTeX-fill-region-as-para-do from end-marker justify-flag)))))
    (set-marker end-marker nil)))