Emacs で Ruby 開発環境

yuko16582007-12-13

RubyRails の開発環境として Aptana StudioNetBeans を試してみたんですが,やはりキーボードですべての操作を行うことができる Emacs を使うことにしました。僕が設定した内容をご紹介します。

設定

今回,僕が Emacs に設定したのは ruby-mode.el と,マイナーモードの ruby-electric.el(対応する括弧やendを自動補完してくれる), rails.el, ruby-block.el(end に対応する行をハイライトする),それから Ruby のデバッガを起動できる rubydb とソースコードディレクトリやファイル,メソッドを表示してくれる ECB です。
まず,Ruby のソースをダウンロードして展開し,misc ディレクトリに入っているすべての *.el ファイルを load-path の通ったところに置きます。(今回は,~/elisp/ に置きました。)
次に,emacs-railsをダウンロードして展開し,同様にすべての *.el ファイルを load-path の通ったところ (~/elisp/) に置きます。
さらに,ruby-block.elをダウンロードして load-path の通ったところ (~/elisp/) に置きます。
次に,ECB をインストールします。ECB の動作には CEDIT も必要だそうなので,これも同時にインストールします。(~/elisp/ にインストールする場合)

$ cd ~/elisp
$ wget "http://jaist.dl.sourceforge.net/sourceforge/cedet/cedet-1.0pre3.tar.gz"
$ tar xvfz cedet-1.0pre3.tar.gz
$ cd cedet-1.0pre3
$ make
$ cd ..
$ wget "http://jaist.dl.sourceforge.net/sourceforge/ecb/ecb-2.32.tar.gz"
$ tar xvfz ecb-2.32.tar.gz
$ cd ecb-2.32
$ make CEDET=../cedet-1.0pre3/

そして .emacs に以下を追加します。(~/elisp/ のところを適時変えてください。)

;; ~/elisp をライブラリパスに追加
(setq load-path
      (append
       (list
        (expand-file-name "~/elisp/")
        )
       load-path))

;; ruby-mode
(autoload 'ruby-mode "ruby-mode"
  "Mode for editing ruby source files" t)
(setq auto-mode-alist
      (append '(("\\.rb$" . ruby-mode)) auto-mode-alist))
(setq interpreter-mode-alist (append '(("ruby" . ruby-mode))
                                     interpreter-mode-alist))
(autoload 'run-ruby "inf-ruby"
  "Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby"
  "Set local key defs for inf-ruby in ruby-mode")
(add-hook 'ruby-mode-hook
          '(lambda () (inf-ruby-keys)))

;; ruby-electric
(require 'ruby-electric)
(add-hook 'ruby-mode-hook '(lambda () (ruby-electric-mode t)))

;; rubydb
(autoload 'rubydb "rubydb3x"
  "run rubydb on program file in buffer *gud-file*.
the directory containing file becomes the initial working directory
and source-file directory for your debugger." t)

;; rails
(defun try-complete-abbrev (old)
  (if (expand-abbrev) t nil))
(setq hippie-expand-try-functions-list
      '(try-complete-abbrev
        try-complete-file-name
        try-expand-dabbrev))
(setq rails-use-mongrel t)
(require 'cl)
(require 'rails)

;; ruby-block
(require 'ruby-block)
(ruby-block-mode t)
;; ミニバッファに表示し, かつ, オーバレイする.
(setq ruby-block-highlight-toggle t)

;; ECB
(setq load-path (cons (expand-file-name "~/elisp/ecb-2.32") load-path))
(load-file "~/elisp/cedet-1.0pre3/common/cedet.el")
(setq semantic-load-turn-useful-things-on t)
(require 'ecb)
(setq ecb-tip-of-the-day nil)
(setq ecb-windows-width 0.25)
(defun ecb-toggle ()
  (interactive)
  (if ecb-minor-mode
      (ecb-deactivate)
    (ecb-activate)))
(global-set-key [f2] 'ecb-toggle)

使い方

僕が使っている機能の使い方を以下にまとめてみます。

機能キーバインド
ruby-mode.el
Ruby のソースファイルに色が付く
インデントして改行C-j
改行C-m
end を挿入C-cC-e
ruby-electric.el
「{}」などの括弧の補完
「class」や「def」,「if」などの後の「end」を補完
rubydb
デバッガを起動M-x rubydb
ブレークポイントを設定C-x space
rails.el
対応する controller/view の切り替えC-c
行き先を選べるファイルの切り替えC-c
関連するファイルを開くC-RET
ruby-block.el
end に対応する行をハイライトする
ECB
ECB を起動[F2]
Edit-window 1 バッファに移動C-c . g 1
ディレクトリバッファに移動C-c . g d
ソースファイルバッファに移動C-c . g m
履歴バッファに移動C-c . g h
ディレクトリバッファでのツリーの開閉[Tab]
選択[RET]

これで AptanaNetBeans と同等かそれ以上の開発環境が Emacs で実現できたと思います。ECB はすごいですね。Emacs でこんなこともできたんですね。rails.el の使い方に関してはまだ全然分かっていないので,これから少しずつ勉強します。rails.el は snippet でいろんな補完ができるみたいなんですけどうまくいきません。ruby-electric と相性が悪いんかな?