Elisp

Execute In Another Buffer

(with-current-buffer (get-buffer "elisp.org")
  (buffer-file-name))
#+RESULTS:
/Users/chris.clark/Projects/cfclrk.com/notes/org/emacs/elisp.org

Association Lists

(assoc "foo" '(("foo" . "bar") ("baz" . "qux")))
#+RESULTS:
(foo . bar)
'(("cats" . "hats") ("dogs" . "hogs"))
#+RESULTS:
((cats . hats) (dogs . hogs))
(assoc "cats" mylist)
#+RESULTS:
(cats . hats)

Pretty Printing Objects

cl-prettyprint

cl-prettyprint formats nicely, but weirdly, it's not a function that returns a string. Instead it literally inserts text into the buffer at point. That can be ok in the scratch buffer, but otherwise useless IMO.

     (let ((my-string-alist '(("foo" . "bar")
                              ("baz" . "qux"))))
       (cl-prettyprint my-string-alist))
(("foo" . "bar")
 ("baz" . "qux"))

We can wrap cl-prettyprint in a function to yank the string. Taken from this SO answer.

(defun pprint (form &optional output-stream)
  (princ (with-temp-buffer
           (cl-prettyprint form)
           (buffer-string))
         output-stream))
#+RESULTS:
pprint
(let ((my-string-alist '(("foo" . "bar")
                         ("baz" . "qux"))))
  (pprint my-string-alist))
#+RESULTS:

(("foo" . "bar")
 ("baz" . "qux"))

pp

pp is great. But I noticed sometimes it doesn't format as vertically as cl-prettyprint (example?).

(let ((my-string-alist '(("foo" . "bar")
                         ("baz" . "qux"))))
  (pp my-string-alist))
#+RESULTS:
(("foo" . "bar")
 ("baz" . "qux"))