Browse Source

Fix `org-N-empty-lines-before-current'

* lisp/org.el (org-N-empty-lines-before-current): Preserve point when
  calling the function from the beginning of a line.

* testing/lisp/test-org.el (test-org/insert-heading): Add tests.
Nicolas Goaziou 8 years ago
parent
commit
1fac906174
2 changed files with 30 additions and 3 deletions
  1. 5 3
      lisp/org.el
  2. 25 0
      testing/lisp/test-org.el

+ 5 - 3
lisp/org.el

@@ -8003,16 +8003,18 @@ unconditionally."
         (org-N-empty-lines-before-current (if blank? 1 0))))))
   (run-hooks 'org-insert-heading-hook))
 
-(defun org-N-empty-lines-before-current (N)
+(defun org-N-empty-lines-before-current (n)
   "Make the number of empty lines before current exactly N.
 So this will delete or add empty lines."
-  (save-excursion
+  (let ((column (current-column))
+	(empty-lines (make-string n ?\n)))
     (beginning-of-line)
     (let ((p (point)))
       (skip-chars-backward " \r\t\n")
       (unless (bolp) (forward-line))
       (delete-region (point) p))
-    (when (> N 0) (insert (make-string N ?\n)))))
+    (insert empty-lines)
+    (move-to-column column)))
 
 (defun org-get-heading (&optional no-tags no-todo)
   "Return the heading of the current entry, without the stars.

+ 25 - 0
testing/lisp/test-org.el

@@ -1267,6 +1267,31 @@
 	  (org-test-with-temp-text "* H1\n- item<point>"
 	    (org-insert-heading nil nil t)
 	    (buffer-string))))
+  ;; Obey `org-blank-before-new-entry'.
+  (should
+   (equal "* H1\n\n* \n"
+	  (org-test-with-temp-text "* H1<point>"
+	    (let ((org-blank-before-new-entry '((heading . t))))
+	      (org-insert-heading))
+	    (buffer-string))))
+  (should
+   (equal "* H1\n* \n"
+	  (org-test-with-temp-text "* H1<point>"
+	    (let ((org-blank-before-new-entry '((heading . nil))))
+	      (org-insert-heading))
+	    (buffer-string))))
+  (should
+   (equal "* H1\n* H2\n* \n"
+	  (org-test-with-temp-text "* H1\n* H2<point>"
+	    (let ((org-blank-before-new-entry '((heading . auto))))
+	      (org-insert-heading))
+	    (buffer-string))))
+  (should
+   (equal "* H1\n\n* H2\n\n* \n"
+	  (org-test-with-temp-text "* H1\n\n* H2<point>"
+	    (let ((org-blank-before-new-entry '((heading . auto))))
+	      (org-insert-heading))
+	    (buffer-string))))
   ;; Corner case: correctly insert a headline after an empty one.
   (should
    (equal "* \n* \n"