Emacs: Debugging elisp

jplindstrom on 2008-01-29T23:54:02

In elisp, Data::Dumper is spelled prin1-to-string. Nicely pretty-printed output of deep data structures. Invaluable when you're, like me, lost in the details of Emacs' (and your own for that matter) data structures.

(message "thing: <%s>" (prin1-to-string thing))


Note 1: (message) takes a string like (format) does, which means that if your string value contains e.g. %s... well that's bad. So you should always do (message "%s" my-string).

This is documented but easily overlooked because (message) is one of the first basic tricks you acquire when you start coding elisp, usually by looking at existing code. And then you think you know how it works. At least, that's how I did it.

I used to write (message (format "blah" stuff)) for a while before I finally read the docs for it.

Note 2: The < > in the example is an old habit of mine: always always always put visible delimiters around variable output in log messages so that you immediately see when the value contains padding whitespace.

Yeah, trouble shooting weird errors is easier if you see what you're looking at.


or M-x...

educated_foo on 2008-01-30T07:57:32

M-x debug-on-entry RET function-name RET also works. The Emacs lisp debugger is probably the best debugger I've ever used.