PPRINT in Emacs Lisp?
Asked Answered
O

2

24

Emacs Lisp does not seem to have a PPRINT function. How do you pretty print an S-EXP in elisp the way you can in Common Lisp?

Outrigger answered 23/8, 2010 at 22:12 Comment(1)
I see cl-prettyprint will print to the current buffer. I am looking for something that works with an output stream like Common Lisp's PPRINT does.Outrigger
T
28

Use the pp library which is part of GNU Emacs. For example you can use pp-macroexpand-last-sexp for prettifying an sexp.

Toshiatoshiko answered 24/8, 2010 at 7:20 Comment(2)
Easiest way is to just use (pp object)Elizbeth
My... that library looks familiar. Oh yeah, I wrote it. :)Scrivenor
T
13

Assuming that the result of cl-prettyprint is good enough for you, here's how to get its output in a stream.

(defun pprint (form &optional output-stream)
  (princ (with-temp-buffer
           (cl-prettyprint form)
           (buffer-string))
         output-stream))
Trimolecular answered 23/8, 2010 at 23:0 Comment(2)
When you are a buffer how can you get cl-prettyprint to replace a defun with its pretty printed version? CanAppellant
Thanks for this. It's weird to me that cl-prettyprint doesn't return a string in the first place.Portwine

© 2022 - 2024 — McMap. All rights reserved.