Skip to content

Instantly share code, notes, and snippets.

@abicky
Created April 15, 2012 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abicky/2392967 to your computer and use it in GitHub Desktop.
Save abicky/2392967 to your computer and use it in GitHub Desktop.
init.el for R (ESS)
;;; ~/.emacs.d/init.el
;;;---------------------------------
;;; 基本設定
;;;---------------------------------
;; 自分でダウンロードした *.el, *.elc のディレクトリ
(setq my-site-lisp (expand-file-name "~/.emacs.d/site-lisp"))
;; my-site-lisp を load-path に追加
(add-to-list 'load-path my-site-lisp)
;; 起動メッセージを表示しない
(setq inhibit-startup-message t)
;; キャレットが '(' や ')' の直後にある場合に対応する括弧を強調
(show-paren-mode t)
;; Emacs サーバを起動する
;; R だと edit や fix を実行する時に必要
(when (require 'server nil t)
(if (not (server-running-p))
(server-start)))
;; 日本語を utf-8 に統一
(set-language-environment "Japanese")
(setq default-buffer-file-coding-system 'utf-8)
(set-buffer-file-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-clipboard-coding-system 'utf-8)
(setq locale-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
;; comint-mode で重複するコマンドは履歴に保存しない
(add-hook 'comint-mode-hook
(lambda ()
(setq comint-input-ignoredups t)))
;;;---------------------------------
;;; 必須の便利機能
;;;---------------------------------
;; *.el のインストール用
(when (require 'auto-install nil t)
(setq auto-install-directory (expand-file-name my-site-lisp))
(if (executable-find auto-install-wget-command)
(auto-install-update-emacswiki-package-name t)
(message (format "Error: %s command not found." auto-install-wget-command)))
(auto-install-compatibility-setup))
;; Emacs の作業効率を劇的に改善する
(when (require 'anything-startup nil t)
;; find-tag のショートカットを anything-c-etags-select に割り当てる
(define-key global-map (kbd "M-.") 'anything-c-etags-select))
;; 補完機能の追加
(when (require 'auto-complete-config nil t)
(global-auto-complete-mode t)
(add-to-list 'ac-dictionary-directories (expand-file-name "~/.emacs.d/site-lisp/ac-dict"))
(ac-config-default)
;; 補完候補が表示されている間のみ有効な ac-menu-map を有効にする
;; デフォルトで C-n, C-p が利用可能
;; TAB を2回押して(ac-expand が2回実行されて)補完候補が表示された時に有効にならないので(バグ?)使わない
;;(setq ac-use-menu-map t)
;; 補完候補が表示されている時に M-h を押すと即座にドキュメントが表示されるようにする
;; (define-key ac-completing-map (kbd "M-h") (lambda () (interactive) (ac-quick-help t))) だとダメ
(defun ac-quick-help-force ()
(interactive)
(ac-quick-help t))
(define-key ac-completing-map (kbd "M-h") 'ac-quick-help-force)
(define-key ac-completing-map (kbd "C-n") 'ac-next)
(define-key ac-completing-map (kbd "C-p") 'ac-previous))
;;;---------------------------------
;;; R & ESS の設定
;;;---------------------------------
;; パスの追加
(add-to-list 'load-path "/usr/local/share/emacs/site-lisp/ess")
;; 拡張子が r, R の場合に R-mode を起動
(add-to-list 'auto-mode-alist '("\\.[rR]$" . R-mode))
;; R-mode を起動する時に ess-site をロード
(autoload 'R-mode "ess-site" "Emacs Speaks Statistics mode" t)
;; R を起動する時に ess-site をロード
(autoload 'R "ess-site" "start R" t)
;; R-mode, iESS を起動する際に呼び出す初期化関数
(setq ess-loaded-p nil)
(defun ess-load-hook (&optional from-iess-p)
;; インデントの幅を4にする(デフォルト2)
(setq ess-indent-level 4)
;; インデントを調整
(setq ess-arg-function-offset-new-line (list ess-indent-level))
;; comment-region のコメントアウトに # を使う(デフォルト##)
(make-variable-buffer-local 'comment-add)
(setq comment-add 0)
;; 最初に ESS を呼び出した時の処理
(when (not ess-loaded-p)
;; アンダースコアの入力が " <- " にならないようにする
(ess-toggle-underscore nil)
;; C-c r を押した際に表示される候補数の上限値
;; 表示数が多いと処理が重くなる
(setq anything-R-help-limit 40)
(setq anything-R-local-limit 20)
;; C-c r で R の関数やオブジェクトを検索できるようにする
(when (require 'anything-R nil t)
;; ess-smart-comma が導入されたので repospkg と localpkg はあまり必要なさそう
(setq anything-for-R-list '(anything-c-source-R-help
anything-c-source-R-local))
(define-key ess-mode-map (kbd "C-c r") 'anything-for-R)
(define-key inferior-ess-mode-map (kbd "C-c r") 'anything-for-R))
;; C-c C-g で オブジェクトの内容を確認できるようにする
(require 'ess-R-object-popup nil t)
;; 補完機能を有効にする
(setq ess-use-auto-complete t)
;; anything を使いたいので IDO は邪魔
(setq ess-use-ido nil)
;; キャレットがシンボル上にある場合にもエコーエリアにヘルプを表示する
(setq ess-eldoc-show-on-symbol t)
;; 起動時にワーキングディレクトリを尋ねられないようにする
(setq ess-ask-for-ess-directory nil)
;; # の数によってコメントのインデントの挙動が変わるのを無効にする
(setq ess-fancy-comments nil)
(setq ess-loaded-p t)
(unless from-iess-p
;; ウィンドウが1つの状態で *.R を開いた場合はウィンドウを縦に分割して R を表示する
(when (one-window-p)
(split-window-horizontally)
(let ((buf (current-buffer)))
(ess-switch-to-ESS nil)
(switch-to-buffer-other-window buf)))
;; R を起動する前だと auto-complete-mode が off になるので自前で on にする
;; cf. ess.el の ess-load-extras
(when (and ess-use-auto-complete (require 'auto-complete nil t))
(add-to-list 'ac-modes 'ess-mode)
(mapcar (lambda (el) (add-to-list 'ac-trigger-commands el))
'(ess-smart-comma smart-operator-comma skeleton-pair-insert-maybe))
(setq ac-sources '(ac-source-R ac-source-filename)))))
(if from-iess-p
;; R のプロセスが他になければウィンドウを分割する
(if (> (length ess-process-name-list) 0)
(when (one-window-p)
(split-window-horizontally)
(other-window 1)))
;; *.R と R のプロセスを結びつける
;; これをしておかないと補完などの便利な機能が使えない
(ess-force-buffer-current "Process to load into: ")))
;; R-mode 起動直後の処理
(add-hook 'R-mode-hook 'ess-load-hook)
;; R 起動直前の処理
(add-hook 'ess-pre-run-hook (lambda () (ess-load-hook t)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment