🕷 zenspider.com

by ryan davis



sitemap
Looking for the Ruby Quickref?

Emacs Automodes

Published 2015-01-07 @ 12:00

Tagged emacs

This is part of the My Emacs Setup series.

I have a system that I’ve already detailed called “autohooks”, but I also hinted about something I use called “automodes”. automodes? What is that and how does it differ from autohooks?

Hooks are intended to be run every time you load a file and go into a mode. When I load a ruby file, my the enh-ruby-mode-hook gets triggered. That’s what my autohook system is for. Automodes tho, is intended to load all the stuff that needs to be loaded before the hook triggers. An easy example is to set up mapping file patterns (eg “*.rb” files) to their modes (enh-ruby-mode) or to set up variables needed for the mode to be set on load (eg enh-ruby-program).

How simple? It’s just 4 lines long:

1
2
3
4
(defun rwd-load-modes ()
  (interactive)
  (dolist (path (directory-files (concat user-init-dir "modes") t ".*el$"))
    (load path)))

This is triggered right after loading emacs via my after-init autohook file:

1
2
3
4
5
6
7
;; after-init.el:
(when (and running-osx (string= "/" default-directory))
  (cd "~"))

(global-whitespace-mode 1)

(rwd-load-modes)

As an example, here is my modes/enh-ruby-mode.el:

1
2
3
4
5
6
7
8
9
10
11
12
13
(autoload 'enh-ruby-mode "enh-ruby-mode" "Major mode for ruby files" t)

(dolist (spec '(("\\.mab$"   . enh-ruby-mode)
                ("\\.rb$"    . enh-ruby-mode)
                ("Rakefile"  . enh-ruby-mode)
                ("Gemfile"   . enh-ruby-mode)
                ("\\.rake$"  . enh-ruby-mode)))
  (add-to-list 'auto-mode-alist spec))

(add-to-list 'interpreter-mode-alist '("ruby" . enh-ruby-mode))

(add-to-list 'load-path (expand-file-name "~/Work/git/enhanced-ruby-mode"))
(setq enh-ruby-program (expand-file-name "/usr/bin/ruby"))

It is a really simple system and really only exists so that I can break up a monolithic setup into many small single-topic files. This has made things a lot more maintainable over time.