diff options
| author | Michael Heerdegen | 2024-02-18 01:55:54 +0100 |
|---|---|---|
| committer | Michael Heerdegen | 2024-03-11 05:24:59 +0100 |
| commit | bbc53e0bcf3fe18e7c1cd51fb8719cf62b9f6c71 (patch) | |
| tree | a193144bbc550cdb28f05930755b4bb663832580 | |
| parent | 9a2ce74c3783c4be8ba70642da374d8e77c6f9ac (diff) | |
| download | emacs-bbc53e0bcf3fe18e7c1cd51fb8719cf62b9f6c71.tar.gz emacs-bbc53e0bcf3fe18e7c1cd51fb8719cf62b9f6c71.zip | |
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).
| -rw-r--r-- | lisp/emacs-lisp/pp.el | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el index 944dd750839..569f70ca604 100644 --- a/lisp/emacs-lisp/pp.el +++ b/lisp/emacs-lisp/pp.el | |||
| @@ -430,23 +430,33 @@ the bounds of a region containing Lisp code to pretty-print." | |||
| 430 | (replace-match "")) | 430 | (replace-match "")) |
| 431 | (insert-into-buffer obuf))))) | 431 | (insert-into-buffer obuf))))) |
| 432 | 432 | ||
| 433 | (defvar pp--quoting-syntaxes | ||
| 434 | `((quote . "'") | ||
| 435 | (function . "#'") | ||
| 436 | (,backquote-backquote-symbol . "`") | ||
| 437 | (,backquote-unquote-symbol . ",") | ||
| 438 | (,backquote-splice-symbol . ",@"))) | ||
| 439 | |||
| 440 | (defun pp--quoted-or-unquoted-form-p (cons) | ||
| 441 | ;; Return non-nil when CONS has one of the forms 'X, `X, ,X or ,@X | ||
| 442 | (let ((head (car cons))) | ||
| 443 | (and (symbolp head) | ||
| 444 | (assq head pp--quoting-syntaxes) | ||
| 445 | (let ((rest (cdr cons))) | ||
| 446 | (and (consp rest) (null (cdr rest))))))) | ||
| 447 | |||
| 433 | (defun pp--insert-lisp (sexp) | 448 | (defun pp--insert-lisp (sexp) |
| 434 | (cl-case (type-of sexp) | 449 | (cl-case (type-of sexp) |
| 435 | (vector (pp--format-vector sexp)) | 450 | (vector (pp--format-vector sexp)) |
| 436 | (cons (cond | 451 | (cons (cond |
| 437 | ((consp (cdr sexp)) | 452 | ((consp (cdr sexp)) |
| 438 | (if (and (length= sexp 2) | 453 | (let ((head (car sexp))) |
| 439 | (memq (car sexp) '(quote function))) | 454 | (if-let (((null (cddr sexp))) |
| 440 | (cond | 455 | (syntax-entry (assq head pp--quoting-syntaxes))) |
| 441 | ((symbolp (cadr sexp)) | 456 | (progn |
| 442 | (let ((print-quoted t)) | 457 | (insert (cdr syntax-entry)) |
| 443 | (prin1 sexp (current-buffer)))) | 458 | (pp--insert-lisp (cadr sexp))) |
| 444 | ((consp (cadr sexp)) | 459 | (pp--format-list sexp)))) |
| 445 | (insert (if (eq (car sexp) 'quote) | ||
| 446 | "'" "#'")) | ||
| 447 | (pp--format-list (cadr sexp) | ||
| 448 | (set-marker (make-marker) (1- (point)))))) | ||
| 449 | (pp--format-list sexp))) | ||
| 450 | (t | 460 | (t |
| 451 | (prin1 sexp (current-buffer))))) | 461 | (prin1 sexp (current-buffer))))) |
| 452 | ;; Print some of the smaller integers as characters, perhaps? | 462 | ;; Print some of the smaller integers as characters, perhaps? |
| @@ -470,15 +480,29 @@ the bounds of a region containing Lisp code to pretty-print." | |||
| 470 | (insert "]")) | 480 | (insert "]")) |
| 471 | 481 | ||
| 472 | (defun pp--format-list (sexp &optional start) | 482 | (defun pp--format-list (sexp &optional start) |
| 473 | (if (and (symbolp (car sexp)) | 483 | (if (not (let ((head (car sexp))) |
| 474 | (not pp--inhibit-function-formatting) | 484 | (or pp--inhibit-function-formatting |
| 475 | (not (keywordp (car sexp)))) | 485 | (not (symbolp head)) |
| 486 | (keywordp head) | ||
| 487 | (let ((l sexp)) | ||
| 488 | (catch 'not-funcall | ||
| 489 | (while l | ||
| 490 | (when (or | ||
| 491 | (atom l) ; SEXP is a dotted list | ||
| 492 | ;; Does SEXP have a form like (ELT... . ,X) ? | ||
| 493 | (pp--quoted-or-unquoted-form-p l)) | ||
| 494 | (throw 'not-funcall t)) | ||
| 495 | (setq l (cdr l))) | ||
| 496 | nil))))) | ||
| 476 | (pp--format-function sexp) | 497 | (pp--format-function sexp) |
| 477 | (insert "(") | 498 | (insert "(") |
| 478 | (pp--insert start (pop sexp)) | 499 | (pp--insert start (pop sexp)) |
| 479 | (while sexp | 500 | (while sexp |
| 480 | (if (consp sexp) | 501 | (if (consp sexp) |
| 481 | (pp--insert " " (pop sexp)) | 502 | (if (not (pp--quoted-or-unquoted-form-p sexp)) |
| 503 | (pp--insert " " (pop sexp)) | ||
| 504 | (pp--insert " . " sexp) | ||
| 505 | (setq sexp nil)) | ||
| 482 | (pp--insert " . " sexp) | 506 | (pp--insert " . " sexp) |
| 483 | (setq sexp nil))) | 507 | (setq sexp nil))) |
| 484 | (insert ")"))) | 508 | (insert ")"))) |