From 3f484a52775c8873a3b1b44e8264e01369850a8a Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 19 Sep 2014 20:55:50 +0200 Subject: [PATCH] Fix headline insertion after an empty headline * lisp/org.el (org-N-empty-lines-before-current): Make sure to delete only empty lines, not trailing whitespaces. * testing/lisp/test-org.el (test-org/insert-heading): Add test. Thanks to Oleh for reporting it. http://permalink.gmane.org/gmane.emacs.orgmode/90876 --- lisp/org.el | 13 ++++++------- testing/lisp/test-org.el | 6 ++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 1a6d02850..c42102cac 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7733,13 +7733,12 @@ command." "Make the number of empty lines before current exactly N. So this will delete or add empty lines." (save-excursion - (goto-char (point-at-bol)) - (if (looking-back "\\s-+" nil 'greedy) - (replace-match "")) - (or (bobp) (insert "\n")) - (while (> N 0) - (insert "\n") - (setq N (1- 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))))) (defun org-get-heading (&optional no-tags no-todo) "Return the heading of the current entry, without the stars. diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 39db5bfcd..acd3d54cf 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -428,6 +428,12 @@ (org-test-with-temp-text "Paragraph" (let ((org-M-RET-may-split-line '((default . nil)))) (org-insert-heading)) + (buffer-string)))) + ;; Corner case: correctly insert a headline after an empty one. + (should + (equal "* \n* " + (org-test-with-temp-text "* " + (org-insert-heading) (buffer-string))))) (ert-deftest test-org/insert-todo-heading-respect-content () -- 2.11.4.GIT