diff --git a/lisp/org.el b/lisp/org.el index 226365262..126318aa3 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7881,15 +7881,14 @@ unconditionally." (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." - (let ((column (current-column)) - (empty-lines (make-string n ?\n))) + (save-excursion (beginning-of-line) - (let ((p (point))) - (skip-chars-backward " \r\t\n") - (unless (bolp) (forward-line)) - (delete-region (point) p)) - (insert empty-lines) - (move-to-column column))) + (unless (bobp) + (let ((start (save-excursion + (skip-chars-backward " \r\t\n") + (line-end-position)))) + (delete-region start (line-end-position 0)))) + (insert (make-string n ?\n)))) (defun org-get-heading (&optional no-tags no-todo no-priority no-comment) "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 1083d0d21..1ff7c39e6 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -1342,7 +1342,16 @@ (equal "* H1\n* H2\n* \n" (org-test-with-temp-text "* H1\n* H2\n" (org-insert-heading) - (buffer-string))))) + (buffer-string)))) + ;; Preserve visibility at beginning of line. In particular, when + ;; removing spurious blank lines, do not visually merge heading with + ;; the line visible above. + (should-not + (org-test-with-temp-text "* H1\nContents\n\n* H2\n" + (org-overview) + (let ((org-blank-before-new-entry '((heading . nil)))) + (org-insert-heading '(4))) + (invisible-p (line-end-position 0))))) (ert-deftest test-org/insert-todo-heading-respect-content () "Test `org-insert-todo-heading-respect-content' specifications."