Making Emacs Do Stuff at Startup

Making Emacs Do Stuff at Startup

Like many users, I start an emacs session in .xinitrc and use it throughout the day. Since I’ve recently started using Emacs Planner, I wanted it to start up automatically in that first Emacs session (but not subsequent ones, if I just want to edit a file).


The solution turned out to be quite simple:

(defun my-startup-stuff nil
  (if (string-equal argi "-do-my-init")
      (progn
        (plan)                  ; Start planner
	)
    nil)                        ; Unrecognized option.
  )
(add-to-list 'command-line-functions 'my-startup-stuff)

command-line-functions is a list of function names. When Emacs sees a command-line argument that it doesn’t recognize, sets argi to the argument and command-line-args-left to the rest of the arguments on the command line), then calls the functions on the list until it finds one that returns a non-nil value.

In this case, I’ve defined the my-startup-stuff function. It recognizes the “-do-my-init” command-line option, and starts up the planner and any other stuff I want to do at the beginning of the day.

Then all that remains is to change my .xinitrc to run “emacs -do-my-init” instead of “emacs“, and I’m done.

Update, Thu Apr 19 13:32:59 EDT 2007: Changed name of the dummy command-line option that makes stuff happen, to avoid confusion.