23

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?

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.
    – anthonyf
    Aug 23, 2010 at 22:17

2 Answers 2

27

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

2
  • 3
    Easiest way is to just use (pp object) Mar 13, 2016 at 18:11
  • 7
    My... that library looks familiar. Oh yeah, I wrote it. :) Jan 24, 2017 at 4:28
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))
2
  • 1
    When you are a buffer how can you get cl-prettyprint to replace a defun with its pretty printed version? Can
    – vfclists
    Oct 31, 2020 at 10:45
  • Thanks for this. It's weird to me that cl-prettyprint doesn't return a string in the first place. Jun 1, 2021 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.