如何添加一个只能在特定模式下运行的钩子? [英] How to add a hook to only run in a particular mode?

查看:157
本文介绍了如何添加一个只能在特定模式下运行的钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下defun

 (defun a-test-save-hook()

(消息香蕉)

通过以下钩子

 (add-hook'after-save-hook'a-test-save-hook)

这样可以预期。我想做的是将钩子限制为特定模式,在这种情况下是组织模式。任何有关我将如何处理的想法?



提前感谢

解决方案

如果您查看 add-hook (或添加钩RET ),你会看到一个可能的解决方案是使钩子局部到你想要的主要模式。这比vderyagin的 answer ,如下所示:

 (add-hook'org-mode-hook 
(lambda()
(add-hook'after-save-hook'a-test-save-hook nil'make-it-local))
/ pre>

'make-it-local 是标志(可以是任何不是 nil ),它告诉 add-hook 仅在当前缓冲区中添加钩子。如上所述,您只能在 org-mode 中添加 a-test-save-hook / p>

如果要在多种模式下使用 a-test-save-hook p>

add-hook 的文档是:

  add-hook是subr.el中编译的Lisp函数。 

(加钩HOOK FUNCTION&可选APPEND LOCAL)

添加到HOOK的函数FUNCTION。
如果已经存在FUNCTION,则不会添加。
在挂钩列表
的开始处添加了FUNCTION(如果需要),除非可选参数APPEND不为零,在这种情况下最后添加
FUNCTION。

可选的第四个参数LOCAL,如果不为零,则说修改
钩子的缓冲区本地值而不是默认值。
如果需要,这使钩子缓冲区本地,并且它使得缓冲区本地值的成员
。它作为一个标志来运行钩子
函数的默认值以及本地值。

HOOK应该是一个符号,而FUNCTION可能是任何有效的函数。如果
HOOK为空,则首先设置为nil。如果HOOK的值是单个
函数,则将其更改为函数列表。


I have the following defun

(defun a-test-save-hook()
  "Test of save hook"
  (message "banana")
  )

that I use via the following hook

(add-hook 'after-save-hook 'a-test-save-hook)

This works as expected. What I would like to do is to limit the hook to particular mode, in this case org-mode. Any ideas as to how I would go about this?

Thanks in advance.

解决方案

If you take a look at the documentation for add-hook (or C-h f add-hook RET), you'll see that one possible solution is to make the hook local to the major modes you want. This is slightly more involved than vderyagin's answer, and looks like this:

(add-hook 'org-mode-hook 
          (lambda () 
             (add-hook 'after-save-hook 'a-test-save-hook nil 'make-it-local)))

The 'make-it-local is the flag (can be anything that isn't nil) that tells add-hook to add the hook only in the current buffer. With the above, you'll only get the a-test-save-hook added in org-mode.

This is nice if you want to use a-test-save-hook in more than one mode.

The documentation for add-hook is:

add-hook is a compiled Lisp function in `subr.el'.

(add-hook HOOK FUNCTION &optional APPEND LOCAL)

Add to the value of HOOK the function FUNCTION.
FUNCTION is not added if already present.
FUNCTION is added (if necessary) at the beginning of the hook list
unless the optional argument APPEND is non-nil, in which case
FUNCTION is added at the end.

The optional fourth argument, LOCAL, if non-nil, says to modify
the hook's buffer-local value rather than its default value.
This makes the hook buffer-local if needed, and it makes t a member
of the buffer-local value.  That acts as a flag to run the hook
functions in the default value as well as in the local value.

HOOK should be a symbol, and FUNCTION may be any valid function.  If
HOOK is void, it is first set to nil.  If HOOK's value is a single
function, it is changed to a list of functions.

这篇关于如何添加一个只能在特定模式下运行的钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆