30 November 2010

Rails options for Emacs

There are several options when it come to Rails development in Emac, and which one you use probably depends on how much time you want to invest in learning how to use the editor.

Ruby Mode plus rhtml mode and ECB

Working in Rails without some sort of file browser in the editor is not fun, so at the very least you want to setup the Emacs Code Browser. I find that combined with ruby-mode and rhtml-mode is about all I need. If you have been following the tutorial so far, adding rhtml is simple. Download it from github and copy all the '.el' files into your emacs includes directory (C:/emacs-23.2/includes in my setup). Then add the following to you _emacs file:

;;; rhtml mode
(require 'rhtml-mode)
; put rhtml templates into rhtml-mode
(setq auto-mode-alist  (cons '("\\.erb$" . rhtml-mode) auto-mode-alist))
; put any rjs scripts into ruby-mode, as they are basically ruby
(setq auto-mode-alist  (cons '("\\.rjs$" . ruby-mode) auto-mode-alist))

This assumes you have added your includes directory as a load path in emacs by adding (add-to-list 'load-path "C:/emacs-23.2/includes") right at the top of your _emacs file.

Rhtml-mode simply syntax highlights the html in your templates while adding ruby highlighting inside the erb blocks.

More advanced Options

When I first setup emacs and started Rails development, I tried out a few other options. These options provide all sorts of features that allow you to jump straight to a related view from the controller, or straight to a unit test, run the Rails server within emacs and so on. It was all just too much to learn, and I quickly fell back into using my simple setup above. These days, there are two options for turning emacs into a full blown 'Rails IDE'

  • rails-mode
  • Rinari

As I don't use either of these, I am not going to explore installing them. The Emacs Wiki has a good page that links to both the projects. If you have followed this tutorial until now, you will be more than skilled enough to get one of these tools working. There is also my old post where I talk about setting up rails-mode, but not in great detail.

Summary

In 5 posts, this tutorial has demonstrated how to get Emacs installed, configure the daunting but very useful Emacs Code Browser, hack ruby-mode into shape and explore some options for Rails development.

If you have been following along and are using emacs 23.2 or newer, then your _emacs file should looking somthing like that below. Enjoy using emacs and Rails, there is plenty more to learn.

(global-font-lock-mode 1)

(add-to-list 'load-path "C:/emacs-23.2/includes")

; ruby mode is bundled with default in emacs 23.2.  This makes it work 
; quite a lot better though.
(add-hook 'ruby-mode-hook
      (lambda()
        (add-hook 'local-write-file-hooks
              '(lambda()
             (save-excursion
               (untabify (point-min) (point-max))
               (delete-trailing-whitespace)
               )))
        (set (make-local-variable 'indent-tabs-mode) 'nil)
        (set (make-local-variable 'tab-width) 2)
        (imenu-add-to-menubar "IMENU")
        (define-key ruby-mode-map "\C-m" 'newline-and-indent) ;Not sure if this line is 100% right!
;            (require 'ruby-electric)
;            (ruby-electric-mode t)
        ))

;;; rhtml mode
(require 'rhtml-mode)

(setq auto-mode-alist  (cons '("\\.erb$" . rhtml-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '("\\.rjs$" . ruby-mode) auto-mode-alist))


(load-file "C:/emacs-23.2/plugins/cedet-1.0/common/cedet.el")

;; Enable EDE (Project Management) features
(global-ede-mode 1)

;; Enable EDE for a pre-existing C++ project
;; (ede-cpp-root-project "NAME" :file "~/myproject/Makefile")


;; Enabling Semantic (code-parsing, smart completion) features
;; Select one of the following:

;; * This enables the database and idle reparse engines
(semantic-load-enable-minimum-features)

;; * This enables some tools useful for coding, such as summary mode
;;   imenu support, and the semantic navigator
(semantic-load-enable-code-helpers)

;; * This enables even more coding tools such as intellisense mode
;;   decoration mode, and stickyfunc mode (plus regular code helpers)
;; (semantic-load-enable-gaudy-code-helpers)

;; * This enables the use of Exuberent ctags if you have it installed.
;;   If you use C++ templates or boost, you should NOT enable it.
;; (semantic-load-enable-all-exuberent-ctags-support)
;;   Or, use one of these two types of support.
;;   Add support for new languges only via ctags.
;; (semantic-load-enable-primary-exuberent-ctags-support)
;;   Add support for using ctags as a backup parser.
;; (semantic-load-enable-secondary-exuberent-ctags-support)

;; Enable SRecode (Template management) minor-mode.
;; (global-srecode-minor-mode 1)

(add-to-list 'load-path "C:/emacs-23.2/plugins/ecb-2.40")
(load-file "C:/emacs-23.2/plugins/ecb-2.40/ecb.el")

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(ecb-layout-name "left14")
 '(ecb-options-version "2.40")
 '(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))
 '(ecb-source-path (quote ("C:/rails")))
 '(ecb-tip-of-the-day nil))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )
blog comments powered by Disqus