I am not what you might call a “competent” Emacs Lisp programmer. More of a cargo-culter, really. But I’m trying to get better.
I create RubyTapas episodes across two different machines: “hazel”, my main development box, and “caroline”, my Windows video editing box. I needed to be able to recognize my synchronized episode directory on either machine, but the paths are different. So I wrote this:
(setq rubytapas-dir (cond ((equal (system-name) "CAROLINE") (concat (getenv "USERPROFILE") "\\Dropbox")) ((equal (system-name) "hazel") (expand-file-name "~/Dropbox/rubytapas")) (t (display-warning :warning "Did not recognize system") (expand-file-name "~/Dropbox/rubytapas"))))
This uses cond , the general-purpose conditional macro. It works, but it’s clunky. I keep repeating the string comparison code. As a Ruby programmer, I want something closer to Ruby’s pattern-matching case statement.
Today I read up on the pcase macro. It turns out it is both simpler and more powerful than I had realized. I haven’t used any of the power yet. But for this very trivial problem, it was as simple as replacing the equality tests with literal strings.
(setq rubytapas-dir (pcase system-name ("CAROLINE" (concat (getenv "USERPROFILE") "\\Dropbox")) ("hazel" (expand-file-name "~/Dropbox/rubytapas")) (_ (display-warning :warning "Did not recognize system") (expand-file-name "~/Dropbox/rubytapas"))))
Easy, and nicer to look at!