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.
This commit is contained in:
Nicolas Goaziou 2017-01-02 23:21:53 +01:00
parent c32db8a8da
commit 1fac906174
2 changed files with 30 additions and 3 deletions

View File

@ -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.

View File

@ -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"