3

I've got a file containing an emacs s-expression and I want to pretty print it from the command line: Is there an easy way of doing this? Paranoia suggests I want to do as little parsing of this data as possible

Context (stack exchange loves context...): I'm doing some processing or org file data externally in a python process. I'm pushing out s-expressions and parsing them in python (with sexpdata) because this is easier than munging stuff into json in emacs.

I'm considering coding up my own parser, or using sexpdata to parse it, or trying to use emacsclient -exec or similar.

  • See also: stackoverflow.com/questions/12990052/…, perhaps this is a duplicate... but I feel the command line requirements make this question distinct. – Att Righ Feb 13 '17 at 19:57
  • Deleted my previous comment (including an early stage one liner) because this one liner contained quite a misleading mistake and I couldn't edit it (inadvertently threw away parts of the pretty printed output). Although perhaps I should have summoned at admin instead. Relevant information is in my gist below. – Att Righ Feb 13 '17 at 21:08
2

I think you should be able to do something like this:

emacsclient -e '(pp (with-temp-buffer (insert-file-contents "my-file-containing sexp") (read (current-buffer))))'

The problem is that this will print a string (e.g., "[foo\n bar]"), so to see it on the screen you will need

echo -e $(emacsclient ...)

You can control the behavior of pp with many variables.

  • Awesome. I've used a similar approach but with the emacs --eval and (message to work around emacsclient's limitiations. I specifically want to read from a file on the command line (I generate this output when by emacs library is in debug mode), so I don't need to use emacsclient to get emacs state. I like the echo -e hack... – Att Righ Feb 13 '17 at 20:34
  • Whoops: the first gist I posted had a thinko: See this gist for my solution. Run as sexpretty.sh file.sexp : gist.github.com/anonymous/78faa02b66c7da10613011dda8d75a29 – Att Righ Feb 13 '17 at 21:04
  • Yep just doing that (posted the other link first to avoid confusion / check the link worked). – Att Righ Feb 13 '17 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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