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