eval-after-load与模式挂钩

Yoo*_*Yoo 67 emacs elisp

设置使用eval-after-load和使用模式挂钩的模式之间有区别吗?

我已经看到一些代码在define-key主模式钩子中使用,而其他一些代码define-keyeval-after-load形式中使用.


更新:

为了更好地理解,下面是使用带有org-mode的eval-after-load和mode钩子的示例.该代码可以运行之前 (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 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)))
Run Code Online (Sandbox Code Playgroud)

san*_*inc 98

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

模式挂钩对于启用了模式的每个缓冲区执行一次,因此它们用于每缓冲区配置.因此,模式挂钩比eval-after-load代码运行得晚; 这使得他们可以根据是否在当前缓冲区中启用其他模式等信息来执行操作.

  • 它只是`eval-after-load`的局部宏包装器,以避免引用传递给`eval-after-load`的表单.即.而不是`(eval-after-load'foo'(progn(foo)(bar)))`我可以写`(后加载'foo(foo)(bar))`. (5认同)
  • @balu:emacs-lisp-mode和lisp-mode与emacs一起转储,并且从不加载。 (2认同)