From 1fac906174e9c522c25a66f91ccd09e9e7c91973 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Mon, 2 Jan 2017 23:21:53 +0100 Subject: [PATCH] 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. --- lisp/org.el | 8 +++++--- testing/lisp/test-org.el | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index c0098cf73..aed4d371b 100644 --- a/lisp/org.el +++ b/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. diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index e21502a29..52f37a426 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -1267,6 +1267,31 @@ (org-test-with-temp-text "* H1\n- item" (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" + (let ((org-blank-before-new-entry '((heading . t)))) + (org-insert-heading)) + (buffer-string)))) + (should + (equal "* H1\n* \n" + (org-test-with-temp-text "* H1" + (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" + (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" + (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"