WFHing with Emacs: Work Mode and Command-Line Options
Like the rest of the world, I’m working from home these days. One of the changes I’ve made has been to set up Emacs to work from home.
I use Emacs extensively both at home and at work. So far, my method for keeping personal stuff and work stuff separate has been to, well, keep separate copies of ~/.emacs.d/
on work and home machines. But now that my home machine is my work machine, I figured I’d combine their configs.
To do this, I just added a -work
command-line option, so that emacs -work
runs in work mode. The command-switch-alist
variable is useful here: it allows you to define a command-line option, and a function to call when it is encountered:
(defun my-work-setup (arg) ;; Do setup for work-mode ) (add-to-list 'command-switch-alist '("work" . my-work-setup))
Of course, I’ve never liked defining functions to be called only once. That’s what lambda expressions are for:
(add-to-list 'command-switch-alist '("work" . (lambda (arg) ;; Do setup for work-mode (setq my-mode 'work) )))
One thing to bear in mind about command-switch-alist
is that it gets called as soon as the command-line option is seen. So let’s say you have a -work
argument and a -logging
option. And the -logging
-related code needs to know whether work mode is turned on. That means you would always have to remember to put the -work
option before the -logging
option, which isn’t very elegant.
A better approach is to use the command-switch-alist
entries to just record that a certain option has been set. The sample code above simply sets my-mode
to 'work
when the -work
option is set. Then do the real startup stuff after the command line has been parsed and you know all of the options that have been passed in.
Unsurprisingly, Emacs has a place to put that: emacs-startup-hook
:
(defvar my-mode 'home "Current mode. Either home or work.") (add-to-list 'command-switch-alist '("work" . (lambda (arg) (setq my-mode 'work)))) (defvar logging-p nil "True if and only if we want extra logging.") (add-to-list 'command-switch-alist '("logging" . (lambda (arg) (setq logging-p t)))) (add-hook 'emacs-startup-hook (lambda nil (if (eq my-mode 'work) (message "Work mode is turned on.")) (if logging-p (message "Extra logging is turned on.")) (if (and (eq my-mode 'work) logging-p) (message "Work mode and logging are both turned on."))))
Check the *Messages* buffer to see the output.