Subject: bug#72434: [PATCH] Print list-ending comma/backquote forms specially

From: Thuna <[email-masked]>
Date: Sat, 03 Aug 2024 03:31:28 +0200

(This patch follows from 72334, but that patch is not necessary for this
one to work.)

Currently something like the following (unevaluated) form:
  `(foo bar . ,baz)
prints as
  `(foo bar \, baz)
which, while technically accurate, is not helpful.  This comes up quite
often in pcase forms where a pattern like `(,head . ,_) is extremely
common.

This patch fixes that, such that if the tail of a list is a proper list
of two elements whose car is a backquote, comma, or comma-at (so ,foo
,@foo and `foo), and print-quoted is non-nil, the printed text will be
in the first form above.

Better ways to implement this are likely possible; this was simply the
easiest and quickest one.  Feel free to change the patch as needed.

>From 4cea5d48a772870df40f7d33fc0a87cdcfc9bbc1 Mon Sep 17 00:00:00 2001
From: Thuna <[email-masked]>
Date: Sat, 3 Aug 2024 03:17:49 +0200
Subject: [PATCH] Print list terminating backquote and comma forms specially

* src/print.c (print_object): When `print-quoted' is non-nil print a
list-terminating (\` obj) as though it terminates the list with a " . "
---
 src/print.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/print.c b/src/print.c
index 256f19aeb90..d654ab76799 100644
--- a/src/print.c
+++ b/src/print.c
@@ -2656,7 +2656,13 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
 		--print_depth;
 		goto next_obj;
 	      }
-	    else if (CONSP (next))
+	    else if (CONSP (next)
+		     && !(print_quoted
+			  && (EQ (Qbackquote, XCAR (next))
+			      || EQ (Qcomma, XCAR (next))
+			      || EQ (Qcomma_at, XCAR (next)))
+			  && CONSP (XCDR (next))
+			  && NILP (XCDR (XCDR (next)))))
 	      {
 		if (!NILP (Vprint_circle))
 		  {
-- 
2.44.2