emacsでruby開発環境を整える

Emacsemacs

emacs の elisp が ruby のソースコードに一通り含まれているらしいので確認する。

$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.bz2
$ tar jxvf ruby-1.9.2-p180.tar.bz2
$ ls -l ruby-1.9.2-p180/misc
total 216
-rw-r--r--  1 hirocaster  staff    369 10  2  2009 README
-rw-r--r--  1 hirocaster  staff  15271 10  2  2009 inf-ruby.el
-rw-r--r--  1 hirocaster  staff   2757 10  2  2009 rdoc-mode.el
-rw-r--r--  1 hirocaster  staff   7094 10  2  2009 ruby-electric.el
-rw-r--r--  1 hirocaster  staff  55876  1 16 21:35 ruby-mode.el
-rw-r--r--  1 hirocaster  staff   2246  5 25  2010 ruby-style.el
-rw-r--r--  1 hirocaster  staff   4481 10  2  2009 rubydb2x.el
-rw-r--r--  1 hirocaster  staff   4984 10  2  2009 rubydb3x.el

emacsのバージョンは 23.2.1 で、ruby-modeは最初から入っていた。

スポンサーリンク

ruby-electric.el

ruby-electric.el は中括弧や対応する括弧を自動補完してくれるようだ。

$ cp ruby-1.9.2-p180/misc/ruby-electric.el ~/.emacs.d/elisp/

その後、emacsを立ち上げて、M-x byte-compile-file で、 ~/.emacs.d/elisp/ruby-electric.el を指定する。

~/.emacs.d/elisp/ruby-electric.elc というファイルが作成されていることを確認

.emacs に以下を追加

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

emacswikiから以下はインストールした。

ruby-block.el

;; ruby-block.el
(require 'ruby-block)
(ruby-block-mode t)
(setq ruby-block-highlight-toggle t)

flymake-ruby

syntax error を検出してくれるflymakeにrubyもつかえるようなので、設定を追加した。

;; flymake-ruby
(defun flymake-ruby-init ()
  (let* ((temp-file   (flymake-init-create-temp-buffer-copy
                       'flymake-create-temp-inplace))
          (local-file  (file-relative-name
                       temp-file
                       (file-name-directory buffer-file-name))))
    (list "ruby" (list "-c" local-file))))
(push '(".+\\.rb$" flymake-ruby-init) flymake-allowed-file-name-masks)
(push '("Rakefile$" flymake-ruby-init) flymake-allowed-file-name-masks)
(push '("^\\(.*\\):\\([0-9]+\\): \\(.*\\)$" 1 2 nil 3) flymake-err-line-patterns)
(add-hook 'ruby-mode-hook
          '(lambda ()
             ;; Don't want flymake mode for ruby regions in rhtml files and also on read only files
             (if (and (not (null buffer-file-name)) (file-writable-p buffer-file-name))
                 (flymake-mode t))
             ))

素直にTextMateやRedcarつかうのが良いのかもしれないが、emacsでやる。

タイトルとURLをコピーしました