Subject: [PATCH] Remove bogus citation quote

From: Manuel Giraud <[email-masked]>
Date: Sat, 29 Mar 2025 14:18:43 +0100

Tags: patch

Hi,

When in message-mode, hitting 'M-RET' in quoted citation and at the end
of line will add a bogus new quote on the following line like this (* is
the point position):

>>> Test*

Hit 'M-RET' will result in:

>>> Test

*

This patch removes this quote.  I also add some tests to validate
`message-newline-and-reformat'.

In GNU Emacs 31.0.50 (build 3, x86_64-unknown-openbsd7.7, X toolkit) of
 2025-03-29 built on computer
Repository revision: eafafb4981c2616fafeedc981e209cff51b564aa
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101016
System Description: OpenBSD computer 7.7 GENERIC.MP#603 amd64

Configured using:
 'configure CC=egcc CPPFLAGS=-I/usr/local/include
 LDFLAGS=-L/usr/local/lib MAKEINFO=gmakeinfo --prefix=/home/manuel/emacs
 --bindir=/home/manuel/bin --with-x-toolkit=lucid
 --with-toolkit-scroll-bars=no --without-cairo
 --without-compress-install'

>From 506cc507b2541c6f6808affdcaf88d904b5132b3 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Sat, 29 Mar 2025 12:47:22 +0100
Subject: [PATCH] Remove bogus citation quote

* lisp/gnus/message.el (message-newline-and-reformat): Do not
add an extra citation quote when formatting at the end of line.
* test/lisp/gnus/message-tests.el
(message-newline-and-reformat): New formatting test.
---
 lisp/gnus/message.el            | 10 +++++++---
 test/lisp/gnus/message-tests.el | 25 +++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el
index a7f27fe1021..98f34285063 100644
--- a/lisp/gnus/message.el
+++ b/lisp/gnus/message.el
@@ -3603,8 +3603,10 @@ message-newline-and-reformat
   (interactive (list (if current-prefix-arg 'full)) message-mode)
   (unless (message-in-body-p)
     (error "This command only works in the body of the message"))
-  (let (quoted point beg end leading-space bolp fill-paragraph-function)
+  (let (quoted point beg end leading-space bolp eolp fill-paragraph-function)
     (setq point (point))
+    (end-of-line)
+    (setq eolp (= (point) point))
     (beginning-of-line)
     (setq beg (point))
     (setq bolp (= beg point))
@@ -3658,6 +3660,8 @@ message-newline-and-reformat
 	    (setq beg (point))
 	    (goto-char (match-end 0))
 	    (looking-at "[ \t]*")
+            ;; Avoid a potential infinite loop.
+            (goto-char beg)
 	    (if (> (length leading-space) (length (match-string 0)))
 		(setq leading-space (match-string 0)))))
       (while (and (not (eobp))
@@ -3683,9 +3687,9 @@ message-newline-and-reformat
 	;; (newline 2) doesn't mark both newline's as hard, so call
 	;; newline twice. -jas
 	(newline)
-	(newline)
+	(unless eolp (newline))
 	(delete-region (point) (re-search-forward "[ \t]*"))
-	(when (and quoted (not bolp))
+	(when (and quoted (not bolp) (not eolp))
 	  (insert quoted leading-space)))
       (undo-boundary)
       (if quoted
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index 7c290486f26..6e517b9a7d0 100644
--- a/test/lisp/gnus/message-tests.el
+++ b/test/lisp/gnus/message-tests.el
@@ -204,6 +204,31 @@ message-default-buffer-type
       (kill-buffer buf)
       (ert-kill-all-test-buffers))))
 
+(ert-deftest message-newline-and-reformat ()
+  (with-temp-buffer
+    (unwind-protect
+        (let (message-auto-save-directory)
+          (message-mode)
+          (insert "> Being cited is\n"
+                  "> not so bad\n"
+                  ">> Above won't\n"
+                  ">>> merge")
+          (re-search-backward "cited")
+          (forward-word)
+          (message-newline-and-reformat)
+          (re-search-forward message-cite-prefix-regexp)
+          (should (string= " is not so bad"
+                           (buffer-substring-no-properties (point) (pos-eol))))
+          (re-search-forward "Above")
+          (message-newline-and-reformat)
+          (re-search-forward message-cite-prefix-regexp)
+          (should (string= " won't"
+                           (buffer-substring-no-properties (point) (pos-eol))))
+          (re-search-forward "merge")
+          (message-newline-and-reformat)
+          (should-error (re-search-forward message-cite-prefix-regexp)))
+      (set-buffer-modified-p nil))))
+
 (provide 'message-mode-tests)
 
 ;;; message-tests.el ends here
-- 
2.49.0

-- 
Manuel Giraud