Fix fill-paragraph combining text with preceding comment

* lisp/textmodes/fill.el (fill-paragraph): Handle the case
when a non-comment line follows a comment line with
non-nil 'fill-paragraph-handle-comment' (bug#80449).
Skip such a comment line before filling a non-comment line.

* test/lisp/textmodes/fill-tests.el
(fill-test-fill-paragraph-handle-comment): Add new test.

* test/lisp/textmodes/fill-resources/fill-paragraph-handle-comment.erts:
New file.
This commit is contained in:
Juri Linkov 2026-05-27 18:32:40 +03:00
parent ea54c33950
commit 2e70b88623
3 changed files with 54 additions and 0 deletions

View file

@ -911,6 +911,7 @@ region, instead of just filling the current paragraph."
(fill-comment-paragraph justify)))
;; 4. If it all fails, default to the good ol' text paragraph filling.
(let ((before (point))
(paragraph-start-orig paragraph-start)
(paragraph-start paragraph-start)
;; Fill prefix used for filling the paragraph.
fill-pfx)
@ -933,6 +934,18 @@ region, instead of just filling the current paragraph."
(setq fill-pfx "")
(let ((end (point))
(beg (progn (fill-forward-paragraph -1) (point))))
;; If the paragraph starts with a comment line preceding point
;; on a non-comment line, skip such comment lines, so they
;; are not filled together (bug#80449).
(when (and fill-paragraph-handle-comment comment-start-skip
(< beg before))
(save-excursion
(goto-char beg)
(when (looking-at paragraph-start-orig)
(goto-char (1+ (match-end 0))))
(when (looking-at comment-start-skip)
(forward-line 1)
(setq beg (point)))))
(goto-char before)
(setq fill-pfx
(if use-hard-newlines

View file

@ -0,0 +1,37 @@
Point-Char: |
Name: fill-paragraph-handle-comment - non-comment line before comment line
Code:
(lambda ()
(setq-local comment-start "# ")
(setq-local fill-paragraph-handle-comment t)
(setq-local fill-column 42)
(fill-paragraph))
=-=
this is not part of the comment this is not part of the comment|
# this is a comment this is a comment this is a comment
=-=
this is not part of the comment this is
not part of the comment
# this is a comment this is a comment this is a comment
=-=-=
Name: fill-paragraph-handle-comment - non-comment line after comment line
=-=
# this is a comment this is a comment this is a comment
this is not part of the comment this is not part of the comment|
=-=
# this is a comment this is a comment this is a comment
this is not part of the comment this is
not part of the comment
=-=-=

View file

@ -163,6 +163,10 @@ eius. Foo")))
(skip-unless (functionp 'markdown-mode))
(ert-test-erts-file (ert-resource-file "fill-paragraph-semlf-markdown-mode.erts")))
(ert-deftest fill-test-fill-paragraph-handle-comment ()
"Test the `fill-paragraph-handle-comment' variable."
(ert-test-erts-file (ert-resource-file "fill-paragraph-handle-comment.erts")))
(provide 'fill-tests)
;;; fill-tests.el ends here