Browse Source

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.
Nicolas Goaziou 11 years ago
parent
commit
136081a0d4
2 changed files with 51 additions and 25 deletions
  1. 30 23
      lisp/org.el
  2. 21 2
      testing/lisp/test-org.el

+ 30 - 23
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))))))
 

+ 21 - 2
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))