emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org.el: Fix newline at eob in org-insert-heading
@ 2019-02-11 13:38 Leo Vivier
  2019-02-11 14:40 ` Leo Vivier
  2019-02-16  8:38 ` Nicolas Goaziou
  0 siblings, 2 replies; 4+ messages in thread
From: Leo Vivier @ 2019-02-11 13:38 UTC (permalink / raw)
  To: emacs-orgmode; +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-<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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] org.el: Fix newline at eob in org-insert-heading
  2019-02-11 13:38 [PATCH] org.el: Fix newline at eob in org-insert-heading Leo Vivier
@ 2019-02-11 14:40 ` Leo Vivier
  2019-02-16  8:38 ` Nicolas Goaziou
  1 sibling, 0 replies; 4+ messages in thread
From: Leo Vivier @ 2019-02-11 14:40 UTC (permalink / raw)
  To: emacs-orgmode; +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-<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
we are at the end of the *widened* buffer prior to inserting the
newline fixes the problem.

The patch I'd originally submitted failed to address narrowed buffer
whose `(max-pos)` was also that of the widened buffer.  Rather than
using `(buffer-narrowed-p)`, I opted for a `(widen)` followed by
`(eobp)`.

TINYCHANGE
---
I was able to replicate the problem with `emacs -q`, so the problem
doesn't seem to come from custom options in my own setup. 

The problematic lines were inserted in the following commit:
b16feed40c7f519ada0cd9315251dcc257be31d2 .  Their goal was to C-<RET>
more predictable, and I don't think I've modified that behaviour in a
any way.

Let me know if you'd rather have me squash the changes.

 lisp/org.el | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 7e74c2199..fef13f818 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -7542,8 +7542,9 @@ unconditionally."
       (unless (and blank? (org-previous-line-empty-p))
 	(org-N-empty-lines-before-current (if blank? 1 0)))
       (insert stars " ")
-      (when (and (eobp)
-                 (not (buffer-narrowed-p)))
+      (when (save-restriction
+              (widen)
+              (eobp))
         (save-excursion (insert "\n")))
       ;; When INVISIBLE-OK is non-nil, ensure newly created headline
       ;; is visible.
@@ -7572,15 +7573,17 @@ unconditionally."
 	       (when blank? (insert "\n"))
 	       (insert "\n" stars " ")
 	       (when (org-string-nw-p split) (insert split))
-	       (when (and (eobp)
-                          (not (buffer-narrowed-p)))
+	       (when (save-restriction
+                       (widen)
+                       (eobp))
                  (save-excursion (insert "\n")))))
 	    (t
 	     (end-of-line)
 	     (when blank? (insert "\n"))
 	     (insert "\n" stars " ")
-	     (when (and (eobp)
-                        (not (buffer-narrowed-p)))
+	     (when (save-restriction
+                       (widen)
+                       (eobp))
                (save-excursion (insert "\n"))))))
      ;; On regular text, turn line into a headline or split, if
      ;; appropriate.
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] org.el: Fix newline at eob in org-insert-heading
  2019-02-11 13:38 [PATCH] org.el: Fix newline at eob in org-insert-heading Leo Vivier
  2019-02-11 14:40 ` Leo Vivier
@ 2019-02-16  8:38 ` Nicolas Goaziou
  2019-02-16  9:31   ` Leo Vivier
  1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2019-02-16  8:38 UTC (permalink / raw)
  To: Leo Vivier; +Cc: emacs-orgmode

Hello,

Leo Vivier <zaephon@gmail.com> writes:

> * 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.

I don't think this would be a sufficient fix, because the buffer can be
narrowed and, yet, showing the end of the file.

Anyway, I don't think this final newline is needed. I removed it. This
should fix your issue. Please let me know if you encounter other
glitches in that area.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] org.el: Fix newline at eob in org-insert-heading
  2019-02-16  8:38 ` Nicolas Goaziou
@ 2019-02-16  9:31   ` Leo Vivier
  0 siblings, 0 replies; 4+ messages in thread
From: Leo Vivier @ 2019-02-16  9:31 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

Hello,

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

>> 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.
>
> I don't think this would be a sufficient fix, because the buffer can be
> narrowed and, yet, showing the end of the file.

Yes, this is also something that I noticed, and I’d sent another patch
that address issue as a reply to the original email.  However, not
knowing whether to submit the patch as is, squash it with the previous
one, or send it as a new thread altogether, I ended doing a clumsy job.

If you check it, you’ll see at the end of the commit message an appended
note on that issue.

> Anyway, I don't think this final newline is needed. I removed it. This
> should fix your issue. Please let me know if you encounter other
> glitches in that area.

Thank you.  I’ll let you know if I encounter anything unexpected.

Best,

P.S.: I’d sent the patches with the wrong email.  This is now resolved.
-- 
Leo Vivier
English Studies & General Linguistics
Master Student, English Department
Université Rennes 2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-02-16  9:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-11 13:38 [PATCH] org.el: Fix newline at eob in org-insert-heading Leo Vivier
2019-02-11 14:40 ` Leo Vivier
2019-02-16  8:38 ` Nicolas Goaziou
2019-02-16  9:31   ` Leo Vivier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).