Sharing init.el between several different Emacsen

Being just a beginning Emacs user, I have to restart Emacs regularly because I messed up my configuration. I also like to use ERC, which would be a pain to restart all the time. Solution: run more then one version of Emacs (something I heard John Wiegley does as well, that’s where I got the idea).

Unless you build your own Emacs with custom settings (which I have no ide how to do), all your Emacsen will use ~/.emacs.d/init.d. How to differentiate between all your different versions of Emacs?

The variable command-line-args contains the filename of your current Emacs. On OSX you can get the application name using:

(setq emacspath (nth 2 (split-string (car command-line-args) "/")))   

By the way, this has nothing to do with argv command line arguments. If you want to run shell scripts using Emacs, for that see Getting Command Line arguments on ErgoEmacs.

The relevant part of my init.d:

;;get OSX application name
(setq emacspath (nth 2 (split-string (car command-line-args) "/")))

(when (string= emacspath "Emacs.app")
  (emacsapp)
  )
(when (string= emacspath "Emacs2.app")
  (emacsapp-irc)
  )
(when (string= emacspath "Aquamacs.app")
  (aquamacs)
  )   

In this way I can use Emacs.app for my regular editing, use Aquamacs for testing (and writing blog-posts), and Emacs2.app for irc.

And in case you’re wondering, why don’t I just run the same version of Emacs from the command-line? Two reasons: Then all the instances have the same icon, which makes switching confusing. And second, copy/paste does not work if I start Emacs like that. Haven’t figured out why, yet.

Prelude init.el & org-babel

Prelude is a very nice Emacs 24 configuration. Private settings are stored under ~/.emacs.d/personal, so it’s easy to keep things clean. The only thing I missed was the possibility to use org-files for my private configuration, something I like in the Emacs Starter Kit. Using org-files for configurations helps to document, and add references to stuff to try out later.

The following change to init.el makes it possible to use org-babel:

;; config changes made through the customize UI will be store here
(setq custom-file (expand-file-name "custom.el" prelude-personal-dir))

;; load the personal settings (this includes `custom-file')
(when (file-exists-p prelude-personal-dir)
  (if (directory-files prelude-personal-dir 't "^[^#].*org$")
      (progn
    (message "Loading personal configuration org-files in %s..." prelude-personal-dir)
    (load-file (expand-file-name "custom.el" prelude-personal-dir))
    (require 'org)
      (mapc 'org-babel-load-file (directory-files prelude-personal-dir 't "^[^#].*org$"))))
    (progn
      (message "Loading personal configuration files in %s..." prelude-personal-dir)
      (mapc 'org-babel-load-file (directory-files prelude-personal-dir 't "^[^#].*el$") ))
    )

If there are org-files in ~/.emacs.d/personal then load org-mode and use org-babel, otherwise just load all the elisp files.

Note: if you use the org-files, then you have to load custom.el by hand, as it’s skipped otherwise.

Evening and day wear for Emacs

During the day, I like a light theme, but in the evening I prefer a dark theme. Off course it is possible to change my theme twice a day, but that gets old quickly, and be honest, who wouldn’t want to spend an hour on elisp to save a couple of minutes of drudgery?

wpid-men-42691_320.jpg

Emacs knows when it gets dark, or for the early risers, when it gets light (see: Sunrise/Sunset – GNU Emacs Manual), so from that it is possible to infer when to switch themes.

As it happens, Joshua Griffith figured that out as well and put theme-changer on github:

Description: “Given a location and day/night color themes, this file provides a `change-theme` function that selects the appropriate theme based on whether it is day or night. It will continue to change themes at sunrise and sunset.”

I like to install my stuff through el-get, but there is no recipe yet, so I createda recipe and installed the package:

(:name theme-changer
       :website "https://github.com/hadronzoo/theme-changer"
       :description "Minor mode for editing WordPress files with Emacs."
       :type github
       :pkgname "hadronzoo/theme-changer"
)

Configuration

Setting up theme changer is very simple, it needs a location, so it knows when the switch themes, and two themes, one for the evening and one for the day.

First, configure your location:

(setq calendar-location-name "The Hague, Netherlands")
(setq calendar-latitude 52.0799)
(setq calendar-longitude 4.3111)

Next we require theme-changer and set the themes we want to use (make sure they are installed already).

(require 'theme-changer)
(change-theme 'solarized-light 'solarized-dark)

And now all that’s left is to wait until sundown…

After using it for a while

How does it actually work? So far not too bad. Themes automatically switch, but if my laptop is closed while Emacs is running switching does not work, and switcher gets confused. So far I play around with color-themes a bit too much, and switching is not too stable. The best solution is restarting Emacs, which takes only second or two, so that’s what I do.

Overal: nice package, will keep.

Shaving time with goto-chg

Through Irreal I found Arne Babenhauserheide’s Emacs configuration. His setup is glean-friendly, using Org-babel. Literate programming is great for config-files. It is probably the best way for people to actually comment their configurations.

shaving and saving time

goto-chg

Arne has lots of little things I want to try. The first one goto-chg is a must-have. From the package:

Goto the point of the most recent edit in the buffer.
When repeated, goto the second most recent edit, etc.
Negative argument, C-u -, for reverse direction.
Works by looking into buffer-undo-list to find points of edit.

I love such packages, they help, but don’t get in the way. If you open a file, you have to start somewhere, so why not at the last place you changed something? Brilliantly simple! It is not a big change, but it will shave a couple of seconds here and there when you revisit a file you edited before, and now you need to make another small change.

As Arne says: “And then you get used to it and it becomes absolutely indispensable.”

Install it (I used el-get) and configure it:

;;(package-require 'goto-chg) ;; you might need this
(global-set-key [(control .)] 'goto-last-change)
; M-. can conflict with etags tag search. But C-. can get overwritten
; by flyspell-auto-correct-word. And goto-last-change needs a really
; fast key.
(global-set-key [(meta .)] 'goto-last-change)
;;(global-set-key [(control ?,)] 'goto-last-change-reverse) ;;I don't use this

That’s it! Simple, but a great time-s(h)aver.