Is there a difference between setting things for a mode using eval-after-load
and using the mode hook?
I've seen some code where define-key
is used inside a major mode hook, and some other code where define-key
is used in eval-after-load
form.
Update:
For better understanding, here is an example of using eval-after-load and mode hooks with org-mode. The code can run before (load "org")
or (require 'org)
or (package-initialize)
.
;; The following two lines of code set some org-mode options.
;; Usually, these can be outside (eval-after-load ...) and work.
;; In cases that doesn't work, try using setq-default or set-variable
;; and putting them in (eval-after-load ...), if the
;; doc for the variables don't say what to do.
;; Or use Customize interface.
(setq org-hide-leading-stars t)
(setq org-return-follows-link t)
;; "org" because C-h f org-mode RET says that org-mode is defined in org.el
(eval-after-load "org"
'(progn
;; Establishing your own keybindings for org-mode.
;; Variable org-mode-map is available only after org.el or org.elc is loaded.
(define-key org-mode-map (kbd "<C-M-return>") 'org-insert-heading-respect-content)
(define-key org-mode-map (kbd "<M-right>") nil) ; erasing a keybinding.
(define-key org-mode-map (kbd "<M-left>") nil) ; erasing a keybinding.
(defun my-org-mode-hook ()
;; The following two lines of code is run from the mode hook.
;; These are for buffer-specific things.
;; In this setup, you want to enable flyspell-mode
;; and run org-reveal for every org buffer.
(flyspell-mode 1)
(org-reveal))
(add-hook 'org-mode-hook 'my-org-mode-hook)))
Code wrapped in eval-after-load
will be executed only once, so it is typically used to perform one-time setup such as setting default global values and behaviour. An example might be setting up a default keymap for a particular mode. In eval-after-load
code, there's no notion of the "current buffer".
Mode hooks execute once for every buffer in which the mode is enabled, so they're used for per-buffer configuration. Mode hooks are therefore run later than eval-after-load
code; this lets them take actions based upon such information as whether other modes are enabled in the current buffer.