| 1 | ;; -*- Mode: Emacs-Lisp ; Coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | ;; ruby-mode |
|---|
| 4 | ;; http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=Emacs |
|---|
| 5 | (add-to-load-path "~/.emacs.d/elisp/ruby-mode/") |
|---|
| 6 | |
|---|
| 7 | (require 'ruby-mode) |
|---|
| 8 | (autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process") |
|---|
| 9 | (autoload 'inf-ruby-keys "inf-ruby" "Set local key defs for inf-ruby in ruby-mode") |
|---|
| 10 | |
|---|
| 11 | (setq auto-mode-alist |
|---|
| 12 | (append '( |
|---|
| 13 | ("\\.rb$" . ruby-mode) |
|---|
| 14 | ("Rakefile" . ruby-mode) |
|---|
| 15 | ("\\.rake$" . ruby-mode) |
|---|
| 16 | ("\\.rjs" . ruby-mode) |
|---|
| 17 | ) auto-mode-alist)) |
|---|
| 18 | |
|---|
| 19 | (setq interpreter-mode-alist |
|---|
| 20 | (append '( |
|---|
| 21 | ("ruby" . ruby-mode) |
|---|
| 22 | ) |
|---|
| 23 | interpreter-mode-alist)) |
|---|
| 24 | |
|---|
| 25 | ;; 深いインデントを避ける |
|---|
| 26 | (setq ruby-deep-indent-paren-style nil) |
|---|
| 27 | |
|---|
| 28 | ;; よくあるコードを、自動挿入する。 |
|---|
| 29 | (require 'ruby-electric) |
|---|
| 30 | |
|---|
| 31 | ;; flymake-modeで補完する対象を追加 |
|---|
| 32 | (push '(".+\\.rb$" flymake-ruby-init) flymake-allowed-file-name-masks) |
|---|
| 33 | (push '("Rakefile$" flymake-ruby-init) flymake-allowed-file-name-masks) |
|---|
| 34 | (push '(".+\\.rake$" flymake-ruby-init) flymake-allowed-file-name-masks) |
|---|
| 35 | (push '(".+\\.rjs$" flymake-ruby-init) flymake-allowed-file-name-masks) |
|---|
| 36 | |
|---|
| 37 | (add-hook 'ruby-mode-hook |
|---|
| 38 | '(lambda () |
|---|
| 39 | (inf-ruby-keys) |
|---|
| 40 | (ruby-electric-mode t) |
|---|
| 41 | (abbrev-mode nil) |
|---|
| 42 | (flymake-mode t))) |
|---|
| 43 | |
|---|
| 44 | ;; rails |
|---|
| 45 | ;; http://d.hatena.ne.jp/higepon/20061222/1166774270 |
|---|
| 46 | (add-to-load-path "~/.emacs.d/elisp/emacs-rails/") |
|---|
| 47 | |
|---|
| 48 | (defun try-complete-abbrev (old) |
|---|
| 49 | (if (expand-abbrev) t nil)) |
|---|
| 50 | |
|---|
| 51 | (setq hippie-expand-try-functions-list |
|---|
| 52 | '(try-complete-abbrev |
|---|
| 53 | try-complete-file-name |
|---|
| 54 | try-expand-dabbrev)) |
|---|
| 55 | |
|---|
| 56 | (require 'rails) |
|---|
| 57 | |
|---|
| 58 | (setq rails-use-mongrel t) |
|---|
| 59 | (define-key rails-minor-mode-map "\C-c\C-p" 'rails-lib:run-primary-switch) |
|---|
| 60 | (define-key rails-minor-mode-map "\C-c\C-n" 'rails-lib:run-secondary-switch) |
|---|
| 61 | |
|---|
| 62 | ;; emacs -nwだと、キーバインドを変更しないと |
|---|
| 63 | ;; rails-goto-file-on-current-lineが動かない。 |
|---|
| 64 | ;; http://d.hatena.ne.jp/kabus/20070822/1187806296 |
|---|
| 65 | (define-key rails-minor-mode-map "\C-cj" 'rails-goto-file-on-current-line) |
|---|
| 66 | |
|---|
| 67 | ;; emacs-railsで、C-c C-c C-tした時にtagsを作るファイルの置かれたディレクトリ |
|---|
| 68 | ;; http://d.hatena.ne.jp/Rommy/20070906/p1 |
|---|
| 69 | (setq rails-tags-dirs '("app" "lib" "test" "db" "vendor")) |
|---|
| 70 | (setq rails-tags-command "ctags -e --Ruby-kinds=-f -o %s --exclude='*.html' -R %s") |
|---|
| 71 | |
|---|
| 72 | ;; RSpec用スニペット |
|---|
| 73 | ;; http://www.achama.com/archives/2007/08/rspecsnippet.html |
|---|
| 74 | (require 'snippet) |
|---|
| 75 | (add-hook 'rails-minor-mode-hook |
|---|
| 76 | '(lambda() |
|---|
| 77 | (snippet-with-abbrev-table 'local-abbrev-table |
|---|
| 78 | ("it" ."it \"$${spec}\" do\n$>$.\nend$>\n") |
|---|
| 79 | ("sbt"."should be_true") |
|---|
| 80 | ("sbi"."should be_an_instance_of($${klass})") |
|---|
| 81 | ("se"."shoud == ") |
|---|
| 82 | ) |
|---|
| 83 | )) |
|---|
| 84 | |
|---|
| 85 | ;; Emacs内からReFeのドキュメントを読む。M-x refeで実行。 |
|---|
| 86 | ;; http://i.loveruby.net/ja/prog/refe.html |
|---|
| 87 | (require 'refe) |
|---|
| 88 | |
|---|
| 89 | ;; Emacs内でautotest実行。M-x autotestで実行。 |
|---|
| 90 | ;; http://www.emacswiki.org/cgi-bin/emacs/download/autotest.el |
|---|
| 91 | (require 'autotest) |
|---|
| 92 | |
|---|
| 93 | ;; magic comment |
|---|
| 94 | ;; Ruby1.9から、ファイルの文字コードを明記する必要がある |
|---|
| 95 | ;; http://d.hatena.ne.jp/rubikitch/20080307/magiccomment |
|---|
| 96 | (defun ruby-insert-magic-comment-if-needed () |
|---|
| 97 | "バッファのcoding-systemをもとにmagic commentをつける。" |
|---|
| 98 | (when (and (eq major-mode 'ruby-mode) |
|---|
| 99 | (find-multibyte-characters (point-min) (point-max) 1)) |
|---|
| 100 | (save-excursion |
|---|
| 101 | (goto-char 1) |
|---|
| 102 | (when (looking-at "^#!") |
|---|
| 103 | (forward-line 1)) |
|---|
| 104 | (if (re-search-forward "^#.+coding" (point-at-eol) t) |
|---|
| 105 | (delete-region (point-at-bol) (point-at-eol)) |
|---|
| 106 | (open-line 1)) |
|---|
| 107 | (let* ((coding-system (symbol-name buffer-file-coding-system)) |
|---|
| 108 | (encoding (cond ((string-match "japanese-iso-8bit\\|euc-j" coding-system) |
|---|
| 109 | "euc-jp") |
|---|
| 110 | ((string-match "shift.jis\\|sjis\\|cp932" coding-system) |
|---|
| 111 | "shift_jis") |
|---|
| 112 | ((string-match "utf-8" coding-system) |
|---|
| 113 | "utf-8")))) |
|---|
| 114 | (insert (format "# -*- coding: %s -*-" encoding)))))) |
|---|
| 115 | |
|---|
| 116 | (add-hook 'before-save-hook 'ruby-insert-magic-comment-if-needed) |
|---|