以通用方式定义自定义 Emacs 钩子

Bra*_*ght 5 emacs elisp

我想在加载我的自定义文件后运行一些代码,但我想以通用方式执行。简单的方法是只添加一个函数列表,然后在列表中执行每个函数,但我想看看我是否可以将它作为一个钩子来做。就像是:

(run-hooks 'bw-after-custom-load-hook)
Run Code Online (Sandbox Code Playgroud)

每次我想添加它时都这样做:

(add-hook 'bw-after-custom-load-hook (lambda () 'something))
Run Code Online (Sandbox Code Playgroud)

这基本上是钩子的工作方式吗?我能找到的所有文档似乎只是向模式提供的现有钩子添加了东西。

Bra*_*ght 5

我解决了(应该在发布前尝试过):

;; add my custom hook
(defvar bw-after-custom-load-hook nil
  "Hook called after the custom file is loaded")
Run Code Online (Sandbox Code Playgroud)

然后在另一个文件中:

;; but load it after custom has loaded, so it's marked safe
(add-hook 'bw-after-custom-load-hook
      (lambda ()
        (load-theme 'solarized-dark)))
Run Code Online (Sandbox Code Playgroud)

然后我们加载自定义并调用钩子:

;; Load custom file last
(setq custom-file (concat dotfiles-dir "custom.el"))
(load custom-file 'noerror)

;; load my custom hooks
(run-hooks 'bw-after-custom-load-hook)
Run Code Online (Sandbox Code Playgroud)