Subject: Re: bug#66106: 28.2; Undo on yanked message fills message body with headers

From: Eric Abrahamsen <[email-masked]>
Date: Sat, 23 Sep 2023 15:40:27 -0700

Eric Abrahamsen <[email-masked]> writes:

> Bruno Victal <[email-masked]> writes:
>
>> 1. Using debbugs (elpa), go to issue #66057.
>> 2. Open the 3rd reply. (from Jean Abou Samra)
>> 3. In the Article buffer (the buffer with the message) do `S v' to
>> start a wide reply.
>> 4. Within the message body, do `C-c C-y' to copy the original
>> message/yank.
>>
>> Issue #1: I get a “Jean Abou Samra writes:” line followed by nothing, it
>> didn't paste the contents of the message I'm replying to.
>>
>> 5. Press `Undo'.
>>
>> Issue #2: Instead of reverting to an empty message body, I get the
>> headers of the message I'm replying to in its place.
>>
>> Notes that might be of interest:
>> * I have set `message-generate-hashcash' to `t'.
>>
>> There's also another issue I've encountered when I reattempted to reply
>> but using `S V' (wide reply with yank): The message doesn't seem to be
>> properly quoted. I only see a single level of '>' whereas I'd expect to
>> see part of the body with '>>' corresponding to the quoted parts of the
>> original message that started the discussion.
>
> I don't see exactly what you're seeing -- I tried this out and always
> got the message headers (no message body) with one level of quoting.
> Hitting undo just removed the level of quoting. But the basic problem is there.
>
> It looks like the issue is in `gnus-summary-reply'. The function that
> prepares the original copy of the article for yanking is
> `gnus-copy-article-buffer', which is called once per article being
> replied to (note that "S V" is only wide; "S v" is very wide).
>
> Starting at line 1105 in `gnus-summary-reply', we go to the buffer
> containing the article text to yank, and run:
>
> 	(save-restriction
> 	  (message-narrow-to-head)
> 	  (when very-wide
> 	    (erase-buffer)
> 	    (insert headers))
> 	  (goto-char (point-max)))
>
> Perhaps the intention was that the narrowing would affect the behavior
> of `erase-buffer', so that in effect this is supposed to replace
> whatever headers were there with the contents of the "headers" variable.
> But of course `erase-buffer' doesn't respect buffer narrowing, so
> everything (including the actual text you wanted to reply to) gets
> deleted. If I replace (erase-buffer) with (delete-region (point-min)
> (point-max)), it appears to work correctly.
>
> Did `erase-buffer' used to respect narrowing, when this code was written
> 20 years ago? Anyway, the more I look at it, the more I think that's
> what's supposed to be happening here.

I've experimented with this a bit more, and am pushing the attached
patch. I believe it fixes this problem, but please re-open the bug if
you're still seeing undesirable behavior.

Thanks for the report,
Eric

>From 450ca9951a87a055eb00bd161a372cc1de02736f Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <[email-masked]>
Date: Sat, 23 Sep 2023 15:33:53 -0700
Subject: [PATCH] Use delete-region in Gnus message yanking

See Bug#66106

* lisp/gnus/gnus-msg.el (gnus-summary-reply): Not erase-buffer. The
intention was to narrow the buffer to the headers, and then delete
just those headers. But erase-buffer doesn't respect narrowing, so
use (delete-region (point-min) (point-max)) instead.
---
 lisp/gnus/gnus-msg.el | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lisp/gnus/gnus-msg.el b/lisp/gnus/gnus-msg.el
index 0439bf0d59b..b065ae34851 100644
--- a/lisp/gnus/gnus-msg.el
+++ b/lisp/gnus/gnus-msg.el
@@ -1104,12 +1104,12 @@ gnus-summary-reply
 		(setq headers (concat headers (buffer-string)))))))
 	(set-buffer (gnus-copy-article-buffer))
 	(gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
-	(save-restriction
-	  (message-narrow-to-head)
-	  (when very-wide
-	    (erase-buffer)
-	    (insert headers))
-	  (goto-char (point-max)))
+	(when very-wide
+          (save-restriction
+	    (message-narrow-to-head)
+	    (delete-region (point-min) (point-max))
+	    (insert headers)
+	    (goto-char (point-max))))
 	(mml-quote-region (point) (point-max))
 	(message-reply nil wide)
 	(when yank
-- 
2.42.0