eval-after-load与模式钩子

使用eval-after-load和使用模式钩子设置模式的东西有区别吗?

我已经看到了一些代码,其中在主要模式钩子中使用了define-key ,以及其他代码,其中define-keyeval-after-load表单中使用。


更新:

为了更好的理解,下面是使用org-mode使用eval-after-load和模式钩子的一个例子。 代码可以 (load "org")(require 'org)(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 Ch 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 "<CM-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))) 

eval-after-load包装的代码将只执行一次,因此通常用于执行一次性设置,例如设置默认的全局值和行为。 一个例子可能是设置一个特定模式的默认键盘映射。 在eval-after-load代码中,没有“当前缓冲区”的概念。

模式挂钩对于启用该模式的每个缓冲区执行一次,因此它们用于每个缓冲区的configuration。 因此,模式挂接比eval-after-load代码晚运行。 这使得他们可以根据当前缓冲区中是否启用其他模式等信息来采取行动。