October 4, 2007

Ruby on Rails with Aquamacs

A friend of mine recently suggested that I look into Aquamacs. According to the website, Aquamacs is an Aqua-native build of the powerful Emacs text editor.

So, I went ahead and downloaded it. First thing I noticed is that it supported stuff for Ruby development right out of the box as the original Emacs. The only problems is, it's got a hard time working with regular indentations, with tabs and so on. I spent a few hours investigating and picking up hints from many places/sources I've put together a LISP script that makes using Aquamacs really nice for Ruby on Rails development.



; stuff for ruby on rails development
(add-to-list 'load-path
"~/Library/Preferences/Aquamacs Emacs/ruby")
(require 'ruby-mode)

; loads ruby mode when a .rb file is opened.
(setq auto-mode-alist
(cons '(".rb$" . ruby-mode) auto-mode-alist))

(setq auto-mode-alist
(cons '(".rhtml$" . html-mode) auto-mode-alist))

; this allows us to have constant indentation as
; we progress in the code from line to line.
(defun create-newline-and-indent()
(local-set-key [return] 'newline-and-indent))

(add-hook 'ruby-mode-hook 'create-newline-and-indent)


; enables ruby electric for easier editing of
; rb and rhtml files
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))

; forces ruby-mode to use tabs for indentation with
; an indent level of 4
(setq indent-tabs-mode 1)
(setq ruby-indent-level 4)

; allows the [tab] key to work with width 4
(define-key ruby-mode-map "\t" 'self-insert-command)
(set (make-local-variable 'tab-width) 4)

(define-key ruby-mode-map "C-m" 'newline-and-indent)

; setting up ruby-electric
(require 'ruby-electric)
(ruby-electric-mode t)
))


The script can just be copied and pasted in your "Preferences.el" file which is used instead of the usual ".emacs.el". The Preferences.el file can be found in "~/Library/Preferences/Aquamacs Emacs".

Hope this saves a few hours for some people out there.

Cheers!

3 comments:

  1. Hi Jean-Paul,

    I'm a ViM fanboy myself. I'm comfortable with it and it speeds up any coding I do. I simply don't use emacs/aquamacs because ViM works well for me.

    BUT

    The other day I had to edit a SQL export file which was 120 MB big. ViM struggled. I then whipped out Aquamacs and Viola! no probs. Its is astounding with how little effort Emacs can work big text files. I'm sure you can open any file size with little or no problems. Respect. I will however continue my ViM usage for a long time to come and are not looking to switch anytime soon.

    ReplyDelete
  2. @stii :-) Yeah I know what you mean. Believe it or not, while I was working on my Preferences.el file for customization, I did it all in VI. I am a big fan of VI myself and find it easier to use than Emacs. But I am looking into Emacs because Aquamacs seems so nice.

    Cheers!

    ReplyDelete
  3. Hi
    Thanks Man
    You saved a lot of time for me
    Thanks!
    AnoopL

    ReplyDelete