Function: LaTeX-fill-paragraph
LaTeX-fill-paragraph is an interactive and byte-compiled function
defined in latex.el.
Signature
(LaTeX-fill-paragraph &optional JUSTIFY)
Documentation
Like fill-paragraph, but handle LaTeX comments.
With prefix argument JUSTIFY, justify as well.
If any of the current line is a comment, fill the comment or the paragraph of it that point is in. Code comments, that is, comments with uncommented code preceding them in the same line, will not be filled unless the cursor is placed on the line with the code comment.
If LaTeX syntax is taken into consideration during filling
depends on the value of LaTeX-syntactic-comments.
Key Bindings
Source Code
;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-fill-paragraph (&optional justify)
"Like `fill-paragraph', but handle LaTeX comments.
With prefix argument JUSTIFY, justify as well.
If any of the current line is a comment, fill the comment or the
paragraph of it that point is in. Code comments, that is, comments
with uncommented code preceding them in the same line, will not
be filled unless the cursor is placed on the line with the
code comment.
If LaTeX syntax is taken into consideration during filling
depends on the value of `LaTeX-syntactic-comments'."
(declare (modes LaTeX-mode))
(interactive "*P")
(if (save-excursion
(beginning-of-line)
(looking-at (concat TeX-comment-start-regexp "*[ \t]*$")))
;; Don't do anything if we look at an empty line and let
;; `fill-paragraph' think we successfully filled the paragraph.
t
(let (;; Non-nil if the current line contains a comment.
has-comment
;; Non-nil if the current line contains code and a comment.
has-code-and-comment
code-comment-start
;; If has-comment, the appropriate fill-prefix for the comment.
comment-fill-prefix)
;; Figure out what kind of comment we are looking at.
(cond
;; A line only with potential whitespace followed by a
;; comment on it?
((save-excursion
(beginning-of-line)
(looking-at (concat "^[ \t]*" TeX-comment-start-regexp
"\\(" TeX-comment-start-regexp "\\|[ \t]\\)*")))
(setq has-comment t
comment-fill-prefix (TeX-match-buffer 0)))
;; A line with some code, followed by a comment?
((and (setq code-comment-start (save-excursion
(beginning-of-line)
(TeX-search-forward-comment-start
(line-end-position))))
(> (point) code-comment-start)
(not (TeX-in-commented-line))
(save-excursion
(goto-char code-comment-start)
;; See if there is at least one non-whitespace character
;; before the comment starts.
(re-search-backward "[^ \t\n]" (line-beginning-position) t)))
(setq has-comment t
has-code-and-comment t)))
(cond
;; Code comments.
(has-code-and-comment
(save-excursion
(when (>= (- code-comment-start (line-beginning-position))
fill-column)
;; If start of code comment is beyond fill column, fill it as a
;; regular paragraph before it is filled as a code comment.
(let ((end-marker (save-excursion (end-of-line) (point-marker))))
(LaTeX-fill-region-as-paragraph (line-beginning-position)
(line-beginning-position 2)
justify)
(goto-char end-marker)
(beginning-of-line)
(set-marker end-marker nil)))
(LaTeX-fill-code-comment justify)))
;; Syntax-aware filling:
;; * `LaTeX-syntactic-comments' enabled: Everything.
;; * `LaTeX-syntactic-comments' disabled: Uncommented code and
;; line comments in `docTeX-mode'.
((or (or LaTeX-syntactic-comments
(and (not LaTeX-syntactic-comments)
(not has-comment)))
(and (eq major-mode 'docTeX-mode)
(TeX-in-line-comment)))
(let ((fill-prefix comment-fill-prefix))
(save-excursion
(let* ((end (progn (LaTeX-forward-paragraph)
(or (bolp) (newline 1))
(and (eobp) (not (bolp)) (open-line 1))
(point)))
(start
(progn
(LaTeX-backward-paragraph)
(while (and (looking-at
(concat "$\\|[ \t]+$\\|"
"[ \t]*" TeX-comment-start-regexp
"+[ \t]*$"))
(< (point) end))
(forward-line))
(point))))
(LaTeX-fill-region-as-paragraph start end justify)))))
;; Non-syntax-aware filling.
(t
(save-excursion
(save-restriction
(beginning-of-line)
(narrow-to-region
;; Find the first line we should include in the region to fill.
(save-excursion
(while (and (zerop (forward-line -1))
(looking-at (concat "^[ \t]*"
TeX-comment-start-regexp))))
;; We may have gone too far. Go forward again.
(or (looking-at (concat ".*" TeX-comment-start-regexp))
(forward-line 1))
(point))
;; Find the beginning of the first line past the region to fill.
(save-excursion
(while (progn (forward-line 1)
(looking-at (concat "^[ \t]*"
TeX-comment-start-regexp))))
(point)))
;; The definitions of `paragraph-start' and
;; `paragraph-separate' will still make
;; `forward-paragraph' and `backward-paragraph' stop at
;; the respective (La)TeX commands. If these should be
;; disregarded, the definitions would have to be changed
;; accordingly. (Lines with only `%' characters on them
;; can be paragraph boundaries.)
(let* ((paragraph-start
(concat paragraph-start "\\|"
"\\(" TeX-comment-start-regexp "\\|[ \t]\\)*$"))
(paragraph-separate
(concat paragraph-separate "\\|"
"\\(" TeX-comment-start-regexp "\\|[ \t]\\)*$"))
(fill-prefix comment-fill-prefix)
(end (progn (forward-paragraph)
(or (bolp) (newline 1))
(point)))
(beg (progn (backward-paragraph)
(point))))
(fill-region-as-paragraph
beg end
justify nil
(save-excursion
(goto-char beg)
(if (looking-at fill-prefix)
nil
(re-search-forward comment-start-skip nil t)
(point)))))))))
t)))