summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuri Linkov <juri@linkov.net>2026-05-27 18:32:40 +0300
committerJuri Linkov <juri@linkov.net>2026-05-27 18:32:40 +0300
commit2e70b88623ed4506c4334f8b9e73651f7d21ade7 (patch)
treef3325e10d5a71fecb9754ed3bada7faeef0e3f3f
parentea54c33950f5a360b08040a1412da36ffb14e704 (diff)
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.
-rw-r--r--lisp/textmodes/fill.el13
-rw-r--r--test/lisp/textmodes/fill-resources/fill-paragraph-handle-comment.erts37
-rw-r--r--test/lisp/textmodes/fill-tests.el4
3 files changed, 54 insertions, 0 deletions
diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el
index c1ccdf2ec5f..270cb388971 100644
--- a/lisp/textmodes/fill.el
+++ b/lisp/textmodes/fill.el
@@ -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
diff --git a/test/lisp/textmodes/fill-resources/fill-paragraph-handle-comment.erts b/test/lisp/textmodes/fill-resources/fill-paragraph-handle-comment.erts
new file mode 100644
index 00000000000..c7c9e96ea50
--- /dev/null
+++ b/test/lisp/textmodes/fill-resources/fill-paragraph-handle-comment.erts
@@ -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
+
+=-=-=
diff --git a/test/lisp/textmodes/fill-tests.el b/test/lisp/textmodes/fill-tests.el
index 2bb9d3ea163..c37a879b4b2 100644
--- a/test/lisp/textmodes/fill-tests.el
+++ b/test/lisp/textmodes/fill-tests.el
@@ -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