unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Heerdegen via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 69168@debbugs.gnu.org, Stefan Monnier <monnier@iro.umontreal.ca>,
	iarchivedmywholelife@gmail.com
Subject: bug#69168: [BUG] 30.0.50; pp-emacs-lisp-code produces invalid elisp for backquoted forms
Date: Thu, 29 Feb 2024 03:30:12 +0100	[thread overview]
Message-ID: <87y1b4vw7f.fsf@web.de> (raw)
In-Reply-To: <87le79t1j6.fsf@web.de> (Michael Heerdegen's message of "Sun, 25 Feb 2024 09:02:05 +0100")

[-- Attachment #1: Type: text/plain, Size: 501 bytes --]

Michael Heerdegen <michael_heerdegen@web.de> writes:

> One from me: I'm working on a second patch for pp that will make
> printing of backquote forms using the nicer `X syntax.

Here is the additional patch.  No Wayman, maybe you can try if it gives
good results?

This change seems quite straightforward, and I tried to avoid to make
the code less efficient.  For me, it seems to give good results.
Please (Eli), if names and comments are bad English, please feel free to
suggest a better wording.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-WIP-Improve-pp-emacs-lisp-code-backquote-form-printi.patch --]
[-- Type: text/x-diff, Size: 4082 bytes --]

From a6ad2e056b7ac136ac766b1fcdb5f59f2f505e15 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
Date: Sun, 18 Feb 2024 01:55:54 +0100
Subject: [PATCH] WIP: Improve pp-emacs-lisp-code backquote form printing

* lisp/emacs-lisp/pp.el (pp--quoted-or-unquoted-form-p): New helper
function.
(pp--insert-lisp): Take care of quoted, backquoted and
unquoted expressions; print using an recursive call.
(pp--format-list): Exclude more cases from printing as a function call
by default.  Print lists whose second-last element is an (un)quoting
symbol using dotted list syntax; e.g. (a b . ,c) instead of (a b \, c).
---
 lisp/emacs-lisp/pp.el | 56 ++++++++++++++++++++++++++++++-------------
 1 file changed, 40 insertions(+), 16 deletions(-)

diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el
index 944dd750839..045cc171eaa 100644
--- a/lisp/emacs-lisp/pp.el
+++ b/lisp/emacs-lisp/pp.el
@@ -430,23 +430,33 @@ pp-emacs-lisp-code
           (replace-match ""))
         (insert-into-buffer obuf)))))

+(defvar pp--quoting-syntaxes
+  `((quote    . "'")
+    (function . "#'")
+    (,backquote-backquote-symbol . "`")
+    (,backquote-unquote-symbol   . ",")
+    (,backquote-splice-symbol    . ",@")))
+
+(defun pp--quoted-or-unquoted-form-p (cons)
+  ;; Return non-nil when CONS has one of the forms 'X, `X, ,X or ,@X
+  (let ((head (car cons)))
+    (and (symbolp head)
+         (assq head pp--quoting-syntaxes)
+         (let ((rest (cdr cons)))
+           (and (consp rest) (null (cdr rest)))))))
+
 (defun pp--insert-lisp (sexp)
   (cl-case (type-of sexp)
     (vector (pp--format-vector sexp))
     (cons (cond
            ((consp (cdr sexp))
-            (if (and (length= sexp 2)
-                     (memq (car sexp) '(quote function)))
-                (cond
-                 ((symbolp (cadr sexp))
-                  (let ((print-quoted t))
-                    (prin1 sexp (current-buffer))))
-                 ((consp (cadr sexp))
-                  (insert (if (eq (car sexp) 'quote)
-                              "'" "#'"))
-                  (pp--format-list (cadr sexp)
-                                   (set-marker (make-marker) (1- (point))))))
-              (pp--format-list sexp)))
+            (let ((head (car sexp)))
+              (if-let (((null (cddr sexp)))
+                       (syntax-entry (assq head pp--quoting-syntaxes)))
+                  (progn
+                    (insert (cdr syntax-entry))
+                    (pp--insert-lisp (cadr sexp)))
+                (pp--format-list sexp))))
            (t
             (prin1 sexp (current-buffer)))))
     ;; Print some of the smaller integers as characters, perhaps?
@@ -470,15 +480,29 @@ pp--format-vector
   (insert "]"))

 (defun pp--format-list (sexp &optional start)
-  (if (and (symbolp (car sexp))
-           (not pp--inhibit-function-formatting)
-           (not (keywordp (car sexp))))
+  (if (not (let ((head (car sexp)))
+             (or pp--inhibit-function-formatting
+                 (not (symbolp head))
+                 (keywordp head)
+                 (let ((l sexp))
+                   (catch 'not-funcall
+                     (while l
+                       (when (or
+                              (not (consp l)) ; SEXP is a dotted list
+                              ;; Is SEXP is of a form like (ELT... . ,X) ?
+                              (pp--quoted-or-unquoted-form-p l))
+                         (throw 'not-funcall t))
+                       (setq l (cdr l)))
+                     nil)))))
       (pp--format-function sexp)
     (insert "(")
     (pp--insert start (pop sexp))
     (while sexp
       (if (consp sexp)
-          (pp--insert " " (pop sexp))
+          (if (not (pp--quoted-or-unquoted-form-p sexp))
+              (pp--insert " " (pop sexp))
+            (pp--insert " . " sexp)
+            (setq sexp nil))
         (pp--insert " . " sexp)
         (setq sexp nil)))
     (insert ")")))
--
2.39.2


[-- Attachment #3: Type: text/plain, Size: 11 bytes --]



Michael.

  reply	other threads:[~2024-02-29  2:30 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-15 19:19 bug#69168: [BUG] 30.0.50; pp-emacs-lisp-code produces invalid elisp for backquoted forms No Wayman
2024-02-16  2:11 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25  5:00   ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25  6:39     ` Eli Zaretskii
2024-02-25  8:02       ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-29  2:30         ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-03-09  8:49           ` Eli Zaretskii
2024-03-09 15:34             ` Drew Adams
2024-03-11  4:48             ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25 15:08       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y1b4vw7f.fsf@web.de \
    --to=bug-gnu-emacs@gnu.org \
    --cc=69168@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=iarchivedmywholelife@gmail.com \
    --cc=michael_heerdegen@web.de \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).