eval-after-load設定すべきもの, hookで設定すべきもの

eval-after-loadで設定すべきもの

一度だけ設定すればよいものは, eval-after-loadで設定すべきです.
具体的にはキーバインド, face, バッファローカルでない変数の設定,
ファイルのロードなどです. (defcustomizeで定義された変数については
custom-set-variablesを使えば, eval-after-load内に書く必要はありません)


local-set-keyを使いたい場合は hookになります.

with-eval-after-load

Emacs 24.4からは with-eval-after-loadというマクロが加わっています.
これにより, quote+prognを書く必要がなくなります. 少しすっきりします.

Before
(eval-after-load 'comint
  '(progn
     (setq comint-input-history-ignore "^\\s-+#")
     (set-face-attribute 'comint-highlight-input nil
                         :foreground "grey80" :weight 'semi-bold)))
After
(with-eval-after-load 'comint
  (setq comint-input-history-ignore "^\\s-+#")
  (set-face-attribute 'comint-highlight-input nil
                      :foreground "grey80" :weight 'semi-bold))

hookで設定すべきもの

バッファローカルな変数の設定, local-set-keyなどです. hookは
バッファを開くたびに呼ばれます(バッファ切り替え等では呼ばれません).
変数がバッファローカルであるかどうかは describe-variableで確認
できます.


setq-defaultを hookで行っている設定をたまに見かけますが, それは
誤りです. setq-defaultは eval-after-load, toplevelに置くべきです.

具体例

(eval-after-load 'cc-mode
  ;; key bindings
  '(progn
     (define-key c-mode-map (kbd "C-c C-t") 'ff-find-other-file)
     (define-key c-mode-map (kbd "C-c C-s") 'my/unwrap-at-point)

     (require 'ac-c-headers)))

(defun my/c-mode-hook ()
  (c-set-style "k&r")
  (setq indent-tabs-mode t
        c-basic-offset 8)
  (c-toggle-electric-state -1)
  (add-to-list 'ac-sources 'ac-source-c-headers)
  (add-to-list 'ac-sources 'ac-source-c-header-symbols t))

(add-hook 'c-mode-hook 'my/c-mode-hook)

おわりに

何かわからないことがありましたら, twitter, lingr等で聞いてくれたら
答えますので遠慮なくお願いします.