emacs23.2
emacs23.2のインストールとruby開発環境のセットアップ
| Table of Contents |
emacs23.2のインストール on RHEL5.5 [1]
sudo yum -y install gcc sudo yum -y install anthy-el # sudo yum -y install libXpm libjpeg libgif libungif libtiff wget http://ftp.gnu.org/pub/gnu/emacs/emacs-23.2a.tar.gz tar zxvf emacs-23.2a.tar.gz cd ./emacs-23.2 # ./configure --without-x ./configure --with-xpm=no --with-jpeg=no --with-gif=no --with-tiff=no make sudo make install cd .. echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile . ~/.bash_profile emacs --version
初期設定
;; ;;; M-x shell の設定
;; ;;; Cygwin の bash を使う場合
;(setq explicit-shell-file-name "bash.exe")
;(setq shell-file-name "sh.exe")
;(setq shell-command-switch "-c")
;(add-hook 'shell-mode-hook
; (lambda ()
; (set-buffer-process-coding-system 'japanese-shift-jis-unix
; 'japanese-shift-jis-unix)))
;(add-hook 'shell-mode-hook
; (lambda ()
; (set-buffer-process-coding-system 'japanese-shift-jis-unix
; 'japanese-shift-jis-unix)))
;(autoload 'ansi-color-for-comint-mode-on "ansi-color"
; "Set `ansi-color-for-comint-mode' to t." t)
;(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
;(add-hook 'comint-output-filter-functions
; 'comint-watch-for-password-prompt)
;; ;;; Virtually UN*X!にある tcsh.exe を使う場合
;; (setq explicit-shell-file-name "tcsh.exe")
;; (setq shell-file-name "tcsh.exe")
;; (setq shell-command-switch "-c")
;; ;;; WindowsNT に付属の CMD.EXE を使う場合。
;; (setq explicit-shell-file-name "CMD.EXE")
;; (setq shell-file-name "CMD.EXE")
;; (setq shell-command-switch "\\/c")
;; ;;; eshell
;; Change the default eshell prompt
(setq eshell-prompt-function
(lambda ()
(concat
(eshell/pwd) "\n")))
;; ChangeLog
(setq user-full-name "Naotoshi Seo")
(setq user-mail-address "hoge@hoge.com")
;; Always end a file with a newline
(setq require-final-newline t)
;; Stop at the end of the file, not just add lines
(setq next-line-add-newlines nil)
;; Stop make backup files ~
(setq make-backup-files nil)
;; Do not use tab character for indentation (Use Ctrl-q Tab to type tab)
(setq-default indent-tabs-mode nil)
;; Show a tab character with 4 spaces
(setq default-tab-width 4)
;; ツールバーとメニューバーを消す
(tool-bar-mode -1)
(menu-bar-mode -1)
;; visible bell
(setq visible-bell 't)
;; 起動画面を消す
(setq inhibit-startup-message t)
;; 時計
(display-time)
;; C-hをバックスペース
(global-set-key "\C-h" 'backward-delete-char)
;; Set backspace C-h
;;(define-key function-key-map [backspace] [8])
;; Set C-h backspace
;(put 'backspace 'ascii-character 8)
;(setq keyboard-translate-table
;"\^@\^A\^B\^C\^D\^E\^F\^G\^?\^I\^J\^K\^L\^M\^N\^O\^P\^Q\^R\^S\^T\^U\^V\
;\^W\^X\^Y\^Z\^[\^\\\^]\^^\^_ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJK\
;LMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\^H")
;; Emacs Load Path
(setq load-path (cons "~/.emacs.d" load-path))
(setq load-path (cons "~/.emacs.d/elisp" load-path))
auto-install.el [2]
mkdir -p ~/.emacs.d/elisp wget http://www.emacswiki.org/emacs/download/auto-install.el -O ~/.emacs.d/elisp/auto-install.el echo '(setq load-path (cons "~/.emacs.d/auto-install" load-path))' >> ~/.emacs.d/init.el echo ';; auto-install' >> ~/.emacs.d/init.el echo "(require 'auto-install)" >> ~/.emacs.d/init.el echo '(setq auto-install-directory "~/.emacs.d/auto-install/")' >> ~/.emacs.d/init.el echo '(auto-install-update-emacswiki-package-name t)' >> ~/.emacs.d/init.el
anything.el [3][10]
emacs を起動。auto-install を使って anything を入れる。
M-x auto-install-batch anything
C-c c-c をタイプしてインストールを完了。emacs を閉じる。
.emacs.d/init.el
;; anything (require 'anything) (require 'anything-config) (require 'anything-match-plugin) (setq anything-sources '(anything-c-source-buffers+ anything-c-source-file-name-history anything-c-source-buffer-not-found anything-c-source-imenu anything-c-source-emacs-commands )) ;; 自動でimenuのインデックスを作る (setq imenu-auto-rescan t) ;; M-x anything を C-x b で起動。 (global-set-key (kbd "C-x b") 'anything)
emacs をひらいて M-x anything とするとバッファの一覧がでてくる。文字を入力すると、絞り込みができる。C-n で次の候補、C-p で次の候補に移動できる。RET で確定。
imenu も起動させているので、ソースコードの関数定義を検索対象にしてジャンプもできるようになっているはず。
以降は [3] もう初心者なんて言わせない、Anything で始まる Emacs 道。
を参照。
ruby-mode.elの設定 [4][5][9]
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 cp ruby-1.9.2-p180/misc/*.el ~/.emacs.d/elisp
emacs を起動
M-x auto-install-from-emacswiki RET ruby-block.el RET C-c c-c
.emacs.d/init.el を修正
;; ruby-mode.el, etc
(autoload 'ruby-mode "ruby-mode" "Mode for editing ruby source files" t)
(setq auto-mode-alist (cons '("\\.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.el --- auto insert 'end' and '}'
;(require 'ruby-electric)
;(add-hook 'ruby-mode-hook '(lambda () (ruby-electric-mode t)))
;; ruby-indent
(setq ruby-indent-level 2)
(setq ruby-indent-tabs-mode nil)
;; ruby-block.el --- highlight the end
(require 'ruby-block)
(ruby-block-mode t)
(setq ruby-block-highlight-toggle t)
;; flymake for ruby
(require 'flymake)
;; Invoke ruby with '-c' to get syntax checking
(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
(if (not (null buffer-file-name)) (flymake-mode))
;; エラー行で C-c d するとエラーの内容をミニバッファで表示する
(define-key ruby-mode-map "\C-cd" 'credmp/flymake-display-err-minibuf)))
(defun credmp/flymake-display-err-minibuf ()
"Displays the error/warning for the current line in the minibuffer"
(interactive)
(let* ((line-no (flymake-current-line-no))
(line-err-info-list (nth 0 (flymake-find-err-info flymake-err-info line-no)))
(count (length line-err-info-list))
)
(while (> count 0)
(when line-err-info-list
(let* ((file (flymake-ler-file (nth (1- count) line-err-info-list)))
(full-file (flymake-ler-full-file (nth (1- count) line-err-info-list)))
(text (flymake-ler-text (nth (1- count) line-err-info-list)))
(line (flymake-ler-line (nth (1- count) line-err-info-list))))
(message "[%s] %s" line text)
)
)
(setq count (1- count)))))
auto-complete.el と popup.el [6][7]
wget http://cx4a.org/pub/auto-complete/auto-complete-1.3.1.tar.bz2 tar jxvf auto-complete-1.3.1.tar.bz2 cd auto-complete-1.3.1 make install DIR=$HOME/.emacs.d/elisp
表示される通り、.emacs.d/init.el に以下を追加
(require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "/root/.emacs.d/elisp/ac-dict") (ac-config-default) ;; auto-complete (require 'auto-complete) (global-auto-complete-mode t) ; Use C-n/C-p to select candidates (define-key ac-complete-mode-map "\C-n" 'ac-next) (define-key ac-complete-mode-map "\C-p" 'ac-previous) ; Stop completion by M-/ (define-key ac-complete-mode-map "\M-/" 'ac-stop) ; Completion by tab (define-key ac-complete-mode-map "\t" 'ac-complete) (define-key ac-complete-mode-map "\r" nil)
使い方
RSenseのインストール [8]
wget http://cx4a.org/pub/rsense/rsense-0.3.tar.bz2 tar jxvf rsense-0.3.tar.bz2 mkdir ~/opt mv rsense-0.3 ~/opt/ ln -s ~/opt/rsense-0.3 ~/opt/rsense cd ~/opt/rsense chmod +x bin/rsense ruby etc/config.rb > ~/.rsense cp etc/rsense.el ~/.emacs.d/elisp echo ';; RSense' >> ~/.emacs.d/init.el echo '(setq rsense-home (expand-file-name "~/opt/rsense"))' >> ~/.emacs.d/init.el echo "(require 'rsense)" >> ~/.emacs.d/init.el
emacs を起動して、M-x rsense-version できればおk。
追加設定
;; C-c .で rsense
(add-hook 'ruby-mode-hook
(lambda ()
(local-set-key (kbd "C-c .") 'ac-complete-rsense)))
;; . や::を入力した直後にrsense
(add-hook 'ruby-mode-hook
(lambda ()
(add-to-list 'ac-sources 'ac-source-rsense-method)
(add-to-list 'ac-sources 'ac-source-rsense-constant)))
rubyリファレンスマニュアルをRSenseから使う
wget http://www.ruby-lang.org/ja/man/archive/snapshot/ruby-refm-1.9.1-dynamic-snapshot.tar.bz2 tar xjf ruby-refm-1.9.1-dynamic-snapshot.tar.bz2 mkdir ~/opt mv ruby-refm-1.9.1-dynamic-snapshot ~/opt/rurema echo '(setq rsense-rurema-home "~/opt/rurema")' >> ~/.emacs.d/init.el
使い方
linum.el
行番号の表示。emacs22 以上ならデフォルトで入っている。
(require 'linum) (global-linum-mode t)
http://www.emacswiki.org/cgi-bin/wiki/LineNumbers
minibuf-isearch.el
「C-r」でミニバッファの入力履歴をインクリメンタルサーチできる機能
wget http://www.sodan.org/~knagano/emacs/minibuf-isearch/minibuf-isearch.el -O ~/.emacs.d/elisp/minibuf-isearch.el
.emacs.d/init.el
;; minibuf-isearch (require 'minibuf-isearch)
session.el
session.el を使うと、Emacs 終了時に、ミニバッファ履歴や kill-ring (Windowsでいうところのクリップボードのようなもの) の内容を保存しておき、次回起動時に復元してくれるようになります。
wget 'http://downloads.sourceforge.net/project/emacs-session/session/session-2.3.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Femacs-session%2Ffiles%2F&ts=1329747194&use_mirror=jaist' -O /tmp/session-2.3.tar.gz tar zxvf /tmp/session-2.3.tar.gz mv session/lisp/session.el .emacs.d/elisp
.emacs.d/init.el
;; session.el (require 'session) (add-hook 'after-init-hook 'session-initialize)
http://emacs-session.sourceforge.net/
分割ウィンドウ補助
windows.el
ウィンドウ状態を保存。
wget http://www.gentei.org/~yuuji/software/revive.el -O ~/.emacs.d/elisp/revive.el wget http://www.gentei.org/~yuuji/software/windows.el -O ~/.emacs.d/elisp/windows.el emacs -Q -batch -f batch-byte-compile ~/.emacs.d/elisp/*.el
.emacs.d/init.el
;; キーバインドを変更. ;; デフォルトは C-c C-w ;; 変更しない場合」は,以下の 3 行を削除する (setq win:switch-prefix "\C-z") (define-key global-map win:switch-prefix nil) (define-key global-map "\C-z1" 'win-switch-to-window) (require 'windows) ;; 新規にフレームを作らない (setq win:use-frame nil) (win:startup-with-window) (define-key ctl-x-map "C" 'see-you-again)
いつもは、C-x C-c でウィンドウを閉じる所を、C-x C で閉じると状態を保存してくれる。
そして、M-x resume-windows で状態を復元できる。
http://www.bookshelf.jp/soft/meadow_30.html#SEC406
windmove.el
分割ウィンドウを Ctrl-o ではなく Shift + 矢印キーで移動する。el 自体はデフォルトで入っている(はず)
Putty 上で emacs を起動して使っていると、Shift + 矢印が利かなかったため、Ctrl-Alt-k のような vi ライクなキーバインドに変更している。
;; Move divided window using Met and the arrow keys (require 'windmove) ; to load the package ;(windmove-default-keybindings) ; default keybindings (global-set-key "\C-\M-h" 'windmove-left) ; use different keys (global-set-key "\C-\M-j" 'windmove-down) ; because putty disables (global-set-key "\C-\M-k" 'windmove-up) ; cursor keys (global-set-key "\C-\M-l" 'windmove-right) (setq windmove-wrap-around t) ; wrap around
http://www.emacswiki.org/emacs/WindMove
デプロイ方法
emacs をインストールする以外は、~/.emacs.d と ~/opt 以下をコピーすればよさそう。
参考文献
- [1] CentOSにemacs23.2をインストール http://blog.mizoshiri.com/archives/988

- [2] auto-Install.el - 非平衡日常 http://d.hatena.ne.jp/ground256/20090911/1252672358

- [3] もう初心者なんて言わせない、Anything で始まる Emacs 道。 http://d.hatena.ne.jp/tomoya/20090423/1240456834

- [4] http://butamamire.jp/blog/2008/01/10_ruby-mode.html

- [5] emacsでruby開発環境を整える http://hiroki.jp/2011/03/17/1697/

- [6] Auto Complete Mode http://cx4a.org/software/auto-complete/

- [7] emacsに自動補完機能を付加する方法 https://docs.google.com/View?id=drw35kw_128d46vt2ct&pli=1

- [8] RSense - ユーザーマニュアル http://cx4a.org/software/rsense/manual.ja.html#RSense.E3.81.A8.E3.81.AF.EF.BC.9F

- [9] ruby-mode の flymake の設定をしたhttp://d.hatena.ne.jp/gan2/20080702/1214972962

- [10] Emacsの検索機能を使いこなす(anything) http://dev.ariel-networks.com/articles/emacs/part2/

