Re: Printing QUOTE.
Christophe Rhodes via Sbcl-help <[email protected]> Sun, 30 Nov 2025 10:11:05 +0000
| Newsgroups | gmane.lisp.steel-bank.general |
|---|---|
| Message-ID | <[email protected]> |
The pretty-printer is ... pretty customizeable.
The basic mechanism is *PPRINT-DISPATCH-TABLE*, which you can fairly
arbitrarily customize to do special-case pretty-printing of any object.
In your case, you want to do something special for objects of type CONS
whose car is EQL to QUOTE, so that's going to be
(set-pprint-dispatch '(cons (eql quote)) <something>)
Now, how do you want to print your things? Well, some considerations (I
don't claim to have thought of everything):
* you want to continue printing prettily the rest of the cons, inserting
newlines after QUOTE if necessary;
* you want the cons to have its own nesting and indenting, so you can use the
pretty-printer's PPRINT-LOGICAL-BLOCK for that;
* you must not break printing if you happen to have malformed quote
forms, such as (QUOTE), (QUOTE . FOO) or (QUOTE FOO BAR). (Instead of
careful checking in this function, you could be more restrictive with
your type in the pprint dispatch table, to a type such as
(cons (eql quote) (cons t null))
using the fact that the built-in pretty-printer must already handle
all the malformed cases.
(defun pprint-quote-cons (stream object)
(pprint-logical-block (stream object :prefix "(" :suffix ")")
(write 'quote :stream stream)
(do ((cdr (cdr object) (cdr cdr)))
((atom cdr)
(unless (null cdr)
(write-char #\Space stream)
(pprint-newline :linear)
(write-char #\. stream)
(write-char #\Space stream)
(pprint-newline :linear)
(write cdr :stream stream)))
(write-char #\Space stream)
(pprint-newline :linear)
(write (car cdr) :stream stream))))
In production you may also want to think about how to make sure your
customization of the pprint dispatch table doesn't interfere with anyone
else's (you may need your own table, which you can make with
COPY-PPRINT-DISPATCH).
Christophe
Avishek Gorai via Sbcl-help <[email protected]> writes:
> So, how to print-pretty using "(QUOTE ...)" instead of the single
> quote in SBCL?
>
> On 30-11-2025 12:50 am, [email protected] wrote:
>>> On Sat, Nov 29, 2025 at 6:11 PM Stas Boukarev <[email protected]> wrote:
>>>
>>> In SBCL, (write ''foo :pretty nil)
>>> That doesn't work everywhere.
>>>
>>> On Sat, Nov 29, 2025 at 8:03 PM Avishek Gorai via Sbcl-help
>>> <[email protected]> wrote:
>>> >
>>> > How to print "(QUOTE FOO)" instead of "'FOO"?
>>> >
>> Or,
>>
>> (let ((*print-pretty* nil))
>> (print ''foo))
>> ;;=> (QUOTE FOO)
>> ;;=> 'FOO
>>
>> *print-pretty*
>> ;;=> T
>> (print ''foo)
>> ;;=> 'FOO
>> ;;=> 'FOO
>>
>> See:
>>
>> http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_writecm_p_rintcm_princ.html
>> http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/var_stprint-prettyst.html
>>
>
>
> _______________________________________________
> Sbcl-help mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sbcl-help
_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help