From 136081a0d4dfe782f66c70daa5d4f35999dad86a Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 10 Jul 2013 14:00:30 +0200 Subject: [PATCH] Use empty commented lines as separators when filling comments * lisp/org.el (org-fill-paragraph): Use empty commented lines as separators when filling comments. This mimics default behaviour from "newcomment.el", which is not used in Org. * testing/lisp/test-org.el: Add tests. --- lisp/org.el | 53 +++++++++++++++++++++++++++--------------------- testing/lisp/test-org.el | 23 +++++++++++++++++++-- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 5be295d72..94a929a28 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -22274,34 +22274,41 @@ a footnote definition, try to fill the first paragraph within." (goto-char (org-element-property :end element)) (re-search-backward "^[ \t]*#\\+end_comment" nil t) (line-beginning-position)))) - (when (and (>= (point) beg) (< (point) end)) + (if (or (< (point) beg) (> (point) end)) t (fill-region-as-paragraph - (save-excursion - (end-of-line) - (re-search-backward "^[ \t]*$" beg 'move) - (line-beginning-position)) - (save-excursion - (beginning-of-line) - (re-search-forward "^[ \t]*$" end 'move) - (line-beginning-position)) - justify))) - t) + (save-excursion (end-of-line) + (re-search-backward "^[ \t]*$" beg 'move) + (line-beginning-position)) + (save-excursion (beginning-of-line) + (re-search-forward "^[ \t]*$" end 'move) + (line-beginning-position)) + justify)))) ;; Fill comments. (comment (let ((begin (org-element-property :post-affiliated element)) - (end (save-excursion - (goto-char (org-element-property :end element)) - (skip-chars-backward " \r\t\n") - (line-end-position)))) - ;; Do not fill comments when at a blank line or at - ;; affiliated keywords. + (end (org-element-property :end element))) (when (and (>= (point) begin) (<= (point) end)) - (let ((fill-prefix (save-excursion - (beginning-of-line) - (looking-at "[ \t]*#") - (concat (match-string 0) " ")))) - (save-excursion - (fill-region-as-paragraph begin end justify)))))) + (let ((begin (save-excursion + (end-of-line) + (if (re-search-backward "^[ \t]*#[ \t]*$" begin t) + (progn (forward-line) (point)) + begin))) + (end (save-excursion + (end-of-line) + (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move) + (1- (line-beginning-position)) + (skip-chars-backward " \r\t\n") + (line-end-position))))) + ;; Do not fill comments when at a blank line or at + ;; affiliated keywords. + (let ((fill-prefix (save-excursion + (beginning-of-line) + (looking-at "[ \t]*#") + (concat (match-string 0) " ")))) + (when (> end begin) + (save-excursion + (fill-region-as-paragraph begin end justify)))))) + t)) ;; Ignore every other element. (otherwise t)))))) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index b7a600952..447c856e7 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -69,7 +69,8 @@ (goto-char (point-max)) (call-interactively 'comment-dwim) (buffer-string))))) - ;; Region selected without comments: comment all non-blank lines. + ;; Region selected without comments: comment all lines if + ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise. (should (equal "# Comment 1\n\n# Comment 2" (org-test-with-temp-text "Comment 1\n\nComment 2" @@ -77,7 +78,18 @@ (transient-mark-mode 1) (push-mark (point) t t) (goto-char (point-max)) - (call-interactively 'comment-dwim) + (let ((comment-empty-lines nil)) + (call-interactively 'comment-dwim)) + (buffer-string))))) + (should + (equal "# Comment 1\n# \n# Comment 2" + (org-test-with-temp-text "Comment 1\n\nComment 2" + (progn + (transient-mark-mode 1) + (push-mark (point) t t) + (goto-char (point-max)) + (let ((comment-empty-lines t)) + (call-interactively 'comment-dwim)) (buffer-string))))) ;; In front of a keyword without region, insert a new comment. (should @@ -206,6 +218,13 @@ (let ((fill-column 20)) (org-fill-paragraph) (buffer-string))))) + ;; Use commented empty lines as separators when filling comments. + (should + (equal "# A B\n#\n# C" + (org-test-with-temp-text "# A\n# B\n#\n# C" + (let ((fill-column 20)) + (org-fill-paragraph) + (buffer-string))))) ;; Do nothing at affiliated keywords. (org-test-with-temp-text "#+NAME: para\nSome\ntext." (let ((fill-column 20)) -- 2.11.4.GIT