Emacsでgtagsと真剣に向き合い楽しくRails開発する2017
これまで
めちゃくちゃgrepしてた。もうやめたい。
やりたいこと。
- タグジャンプがしたい
- できればhelm/anythingで
- タグファイルは自動更新してほしい
実行
gtagsのインストール
brew install global --with-exuberant-ctags --with-pygments
gtags.elはなんかパス通ってた
なければ
www.gnu.org
から落として展開してelみつけてパスの通ったディレクトリに置いて。githubとかで開発されてない。
anythin-gtagsのインストール
M-x auto-install-from-url [RET] https://www.emacswiki.org/emacs/download/anything-gtags.el
設定
(require 'gtags) (require 'anything-gtags) (add-hook 'ruby-mode-hook '(lambda() (gtags-mode 1)))
とりまruby-modeにフック
anythingの定義とかキーバインドとか
F3は定義ジャンプの定番(Eclipse教徒、信者ではない)
(defun anything-gtags-from-here () (interactive) (anything :sources '(anything-c-source-imenu anything-c-source-gtags-select ) :input (thing-at-point 'symbol))) (global-set-key [f3] 'anything-gtags-from-here)
自動更新
;; update GTAGS (defun update-gtags (&optional prefix) (interactive "P") (let ((rootdir (gtags-get-rootpath)) (args (if prefix "-v" "-iv"))) (when rootdir (let* ((default-directory rootdir) (buffer (get-buffer-create "*update GTAGS*"))) (save-excursion (set-buffer buffer) (erase-buffer) (let ((result (shell-command "/usr/local/bin/gtags --gtagslabel=pygments"))) (if (= 0 result) (message "GTAGS successfully updated.") (message "update GTAGS error with exit status %d" result))))))))
参考URLと違って生でshell-command叩いてる。特に深い意味はない。
プロジェクト初期作業
gtagsをプロジェクトルートで一回実行しておく
$ cd /path/to/project/ $ /usr/local/bin/gtags --gtagslabel=pygments
実際はaliasを設定しておけばよく、こんなコマンド覚える必要はない。
結果
ファイルを保存したらgtagsが走り、データベースが再生成される。メソッド呼び出しでF3押せばanythingで検索、ジャンプできる。M-*でジャンプ先から戻れる。
すごーい!
たのしー!