使用eval-after-load设置模式与使用模式挂钩是否有区别?
我看到了一些在主要模式钩子中使用define-key的代码,以及在eval-after-load表单中使用define-key的其他一些代码。
更新:
为了更好地理解,下面是一个示例,说明如何在组织模式中使用加载后评估和模式挂钩。代码可以在之前运行 (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)))发布于 2010-04-29 09:30:05
包装在eval-after-load中的代码将只执行一次,因此通常用于执行一次性设置,例如设置默认的全局值和行为。一个示例可能是为特定模式设置默认密钥映射。在eval-after-load代码中,没有“当前缓冲区”的概念。
模式挂钩对每个启用模式的缓冲区执行一次,因此它们用于每个缓冲区配置。因此,模式挂钩比eval-after-load代码运行得晚;这使它们能够根据诸如是否在当前缓冲区中启用其他模式等信息采取行动。
https://stackoverflow.com/questions/2736087
复制相似问题