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

时间:2011-12-27 12:49:00

标签: emacs elisp

我想在加载自定义文件后运行一些代码,但我希望以通用方式执行此操作。简单的方法是只有一个我追加的函数列表,然后在列表中执行每个函数,但我想看看我是否可以将它作为一个钩子。类似的东西:

(run-hooks 'bw-after-custom-load-hook)

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

(add-hook 'bw-after-custom-load-hook (lambda () 'something))

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

2 个答案:

答案 0 :(得分:4)

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

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

然后在另一个文件中:

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

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

;; 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)

答案 1 :(得分:0)

你正在寻找after-init-hook吗?

(defun my-functions-for-after-init ()
  (....))

然后,

(add-hook 'after-init-hook 'my-functions-for-after-init)