Browse Source

Normalize blank lines with <C-RET>

* lisp/org.el (org-insert-heading): Make C-RET more predictable.  In
  particular, it should not eat all the blank lines at the end of the
  tree.

Reported-by: David Masterson <dsmasterson@outlook.com>
<http://lists.gnu.org/r/emacs-orgmode/2018-11/msg00275.html>
Nicolas Goaziou 1 year ago
parent
commit
b16feed40c
1 changed files with 17 additions and 14 deletions
  1. 17 14
      lisp/org.el

+ 17 - 14
lisp/org.el

@@ -7640,22 +7640,25 @@ unconditionally."
 	  (member arg '((4) (16)))
 	  (and (not invisible-ok)
 	       (invisible-p (max (1- (point)) (point-min)))))
-      ;; Position point at the location of insertion.
-      (if (not level)			;before first headline
-	  (org-with-limited-levels (outline-next-heading))
-	;; Make sure we end up on a visible headline if INVISIBLE-OK
-	;; is nil.
-	(org-with-limited-levels (org-back-to-heading invisible-ok))
-	(cond ((equal arg '(16))
-	       (org-up-heading-safe)
-	       (org-end-of-subtree t t))
-	      (t
-	       (org-end-of-subtree t t))))
-      (unless (bolp) (insert "\n"))	;ensure final newline
+      ;; Position point at the location of insertion.  Make sure we
+      ;; end up on a visible headline if INVISIBLE-OK is nil.
+      (org-with-limited-levels
+       (if (not level) (outline-next-heading) ;before first headline
+	 (org-back-to-heading invisible-ok)
+	 (when (equal arg '(16)) (org-up-heading-safe))
+	 (org-end-of-subtree)))
+      (unless (bolp) (insert "\n"))
       (unless (and blank? (org-previous-line-empty-p))
 	(org-N-empty-lines-before-current (if blank? 1 0)))
-      (insert stars " \n")
-      (forward-char -1))
+      (insert stars " ")
+      (when (eobp) (save-excursion (insert "\n")))
+      ;; When INVISIBLE-OK is non-nil, ensure newly created headline
+      ;; is visible.
+      (unless invisible-ok
+	(pcase (get-char-property-and-overlay (point) 'invisible)
+	  (`(outline . ,o)
+	   (move-overlay o (overlay-start o) (line-end-position 0)))
+	  (_ nil))))
      ;; At a headline...
      ((org-at-heading-p)
       (cond ((bolp)