* lisp/org.el (org-insert-heading): Check if narrowed before inserting newline at eob
When narrowed into an org-buffer (e.g. when capturing), adding a new heading with C-<return> or M-<return> on the last line of a buffer (i.e. that not without a newline at the end) would result in the insertion of a newline at the bottom of the narrowed capture buffer. - C-<return>: `org-insert-heading-respect-content' - M-<return>: `org-meta-return' Both functions use `org-insert-heading' in their definitions. The problem is due to `eobp' returning t when point is on the last character of the narrowed buffer (as explained in its docstring). Since those `eobp' predicates in `org-insert-heading' are probably there to ensure a newline at the end of the *file*, checking whether the buffer is *narrowed* (with `buffer-narrowed-p') prior to inserting the newline fixes the problem. TINYCHANGE --- lisp/org.el | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index e2258749b..7e74c2199 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7542,7 +7542,9 @@ unconditionally." (unless (and blank? (org-previous-line-empty-p)) (org-N-empty-lines-before-current (if blank? 1 0))) (insert stars " ") - (when (eobp) (save-excursion (insert "\n"))) + (when (and (eobp) + (not (buffer-narrowed-p))) + (save-excursion (insert "\n"))) ;; When INVISIBLE-OK is non-nil, ensure newly created headline ;; is visible. (unless invisible-ok @@ -7570,12 +7572,16 @@ unconditionally." (when blank? (insert "\n")) (insert "\n" stars " ") (when (org-string-nw-p split) (insert split)) - (when (eobp) (save-excursion (insert "\n"))))) + (when (and (eobp) + (not (buffer-narrowed-p))) + (save-excursion (insert "\n"))))) (t (end-of-line) (when blank? (insert "\n")) (insert "\n" stars " ") - (when (eobp) (save-excursion (insert "\n")))))) + (when (and (eobp) + (not (buffer-narrowed-p))) + (save-excursion (insert "\n")))))) ;; On regular text, turn line into a headline or split, if ;; appropriate. ((bolp) -- 2.20.1