From mboxrd@z Thu Jan 1 00:00:00 1970 From: Leo Vivier Subject: [PATCH] org.el: Fix newline at eob in org-insert-heading Date: Mon, 11 Feb 2019 14:38:13 +0100 Message-ID: <20190211133813.4348-1-zaephon@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([209.51.188.92]:58513) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtBnD-0007xa-R9 for emacs-orgmode@gnu.org; Mon, 11 Feb 2019 08:38:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtBn7-0006A8-JB for emacs-orgmode@gnu.org; Mon, 11 Feb 2019 08:38:36 -0500 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:36715) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gtBn7-00069A-C2 for emacs-orgmode@gnu.org; Mon, 11 Feb 2019 08:38:33 -0500 Received: by mail-wm1-x341.google.com with SMTP id j125so2326517wmj.1 for ; Mon, 11 Feb 2019 05:38:32 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Cc: Leo Vivier * 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- or M- 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-: `org-insert-heading-respect-content' - M-: `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