Customizing emacs on osx

by Sebastien Mirolo on Wed, 6 Jul 2011

Time for a little maintenance, I downloaded the latest emacs package available (Emacs-23.3-universal-10.6.6.dmg) and started to re-install the extensions I use everyday, as well as some new ones I would like to try. Since so far I had use CarbonEmacs, there were also a few configuration settings I had to tweak differently. Some useful information to run Emacs.app on OSX can be found here.

Emacs extensions

Color theme

color-theme is emacs-lisp mode which I use it primary to read white text on a black background, aka the "ld-dark" theme.

sudo port install color-theme-mode.el

Once installed, the following lines in my .emacs do the trick.

(add-to-list 'load-path "/opt/local/share/emacs/site-lisp/color-theme-6.6.0")
(require 'color-theme)
(eval-after-load "color-theme"
  '(progn
	(color-theme-initialize)
	(color-theme-ld-dark)))

I often get confused and edit files in the head branch while I am fixing a bug in a feature branch so I defined the following macro in my .emacs to switch the color-theme based on the pathname of the file being edited.

(add-hook 'window-configuration-change-hook
		  '(lambda () (if buffer-file-name 
		       (cond ((string-match "head/" buffer-file-name)
		              (color-theme-subtle-hacker))
			     ((string-match "linux/" buffer-file-name)
		              (color-theme-ld-dark))
			     (t (color-theme-high-contrast))))))

Editing modes

Emacs supports out-of-the-box a wide variety of editing modes, supporting many programming languages. Of course there are still the few langugages you will work with that have not make it in the emacs release yet. For those, you will most likely find a emacsscript (.el) package to download, install inside a directory on the emacs search path and autoload on demand.

Verilog mode (latest)

(autoload 'verilog-mode "verilog-mode" "Verilog Editing Mode" t)
(add-to-list 'auto-mode-alist '("\\.v\\'" . verilog-mode))

Whitespaces

Tabs, spaces and indentation is always the most annoying thing when multiple developpers edit the same file. You might settle for a no-tab-four-spaces indent policy but it does not do you much good until you can "see" tab characters while editing a source file. Hopefully there is whitespace.el to the rescue. I use the following in my .emacs to deal with whitespaces and the different tab-indent policies the projects I work with require.

(require 'whitespace)

(defun gen-tab-stop-list (x) (if (<= x 0) () 
			       (cons x (gen-tab-stop-list (- x tab-width)))))

(defun no-tab-in-file-policy ( nb-indent-spaces )
  (setq indent-tabs-mode nil)
  (setq tab-width nb-indent-spaces)          
  (setq c-basic-offset tab-width)
  (setq tab-stop-list (reverse (gen-tab-stop-list 80)))
  (setq whitespace-style '(face tabs trailing lines-tail))
  (setq whitespace-tab 'whitespace-trailing)
  (setq python-indent nb-indent-spaces))

(defun tab-as-indent-policy ( nb-indent-spaces )
  (setq indent-tabs-mode t)
  (setq tab-width nb-indent-spaces)          
  (setq c-basic-offset tab-width)
  (setq tab-stop-list (reverse (gen-tab-stop-list 80)))
  (setq whitespace-style
      '(face indentation::tab space-after-tab::tab space-before-tab::tab 
             trailing lines-tail))
  (setq whitespace-indentation 'whitespace-trailing)
  (setq whitespace-space-after-tab 'whitespace-trailing)
  (setq whitespace-space-before-tab 'whitespace-trailing)
  ; At first, by reading through the whitespace.el source, I tried
  ; to override the local buffer *whitespace-tab-width* definition but 
  ; I haven't found a reliable hook to insert the (setq ...) in.
  (setq whitespace-space-after-tab-regexp
	'("\t+\\(\\( \\{1\\}\\)+\\)"
	  . "\\(\t+\\) +"))
  (setq python-indent nb-indent-spaces)
  (setq ruby-indent-level nb-indent-spaces))

(defun my-common-hook () (if buffer-file-name 
        (cond ((string-match "/linux" buffer-file-name)
               (tab-as-indent-policy 8))
	          (t
               (no-tab-in-file-policy 4)))))

(defun my-mode-hook () (progn (my-common-hook)
                          (whitespace-mode 1)))

(add-hook 'c++-mode-common-hook 'my-mode-hook)
(add-hook 'python-mode-hook 'my-mode-hook)
(add-hook 'window-configuration-change-hook 'my-common-hook)

Git support

Git is the version control system I mostly use these days. If you want to learn more about use git with emacs, read the following excellent introduction. Then start with the familiar port install command as usual:

port install git-core

Then I added the following lines to my .emacs:

(add-to-list 'load-path "/opt/local/share/doc/git-core/contrib/emacs")
(require 'vc-git)
(when (featurep 'vc-git) (add-to-list 'vc-handled-backends 'git))
(require 'git)
(autoload 'git-blame-mode "git-blame"
            "Minor mode for incremental blame for Git." t)

Spell checker

After writing documents, I like to spell check them through ispell and installed the following extension, accessible through M-x ispell-*.

port install ispell

The lines added to the .emacs:

;; Load the spell-checker (M-x ispell-buffer)
(require 'ispell)
(setq ispell-program-name "/opt/local/bin/ispell")

CEDET/ECB

Emacs Code Browser and Collection of Emacs Development Environment Tools give a little more IDE flair to emacs. Since ECB requires CEDET to work properly, both extensions are usually installed together.

sudo port install ecb

The lines added to the .emacs:

(add-to-list 'load-path "/opt/local/share/emacs/site-lisp")
(add-to-list 'load-path "/opt/local/share/emacs/site-lisp/common")
(require 'cedet)
(require 'ecb-autoloads)

;; Enables the database and idle reparse engines. You can also enable
;; more tools useful for coding, such as summary mode imenu support, 
;; and the semantic navigator using (semantic-load-enable-code-helpers)
;; instead or even more coding tools such as intellisense mode decoration 
;; mode, and stickyfunc mode (plus regular code helpers) using 
;; (semantic-load-enable-gaudy-code-helpers)
(semantic-load-enable-minimum-features)

ECB is now ready for use and can be activated by calling "M-x ecb-activate " or "ecb-minor-mode". There is a lot of features in ECB so reading the documentation "ecb-show-help" might be a good start.

After playing around with CEDET and ECB, I haven't found them so useful in my day to day activities. I kind of de-activated them by now but since I have never been big on Visual Studio, Eclipse, NetBeans et al. it might just be that the whole IDE concept is not my thing.

Other extensions

These are extensions I checked out at some point but have not kept using regularly.

by Sebastien Mirolo on Wed, 6 Jul 2011