Use empty commented lines as separators when filling comments
authorNicolas Goaziou <n.goaziou@gmail.com>
Wed, 10 Jul 2013 12:00:30 +0000 (10 14:00 +0200)
committerNicolas Goaziou <n.goaziou@gmail.com>
Wed, 10 Jul 2013 12:13:39 +0000 (10 14:13 +0200)
* 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
testing/lisp/test-org.el

index 5be295d..94a929a 100644 (file)
@@ -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))))))
 
index b7a6009..447c856 100644 (file)
@@ -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"
              (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
            (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))