# 附录 H: 标准钩子

下面是一些钩子变量的列表，你可以在合适的钩子变量中提供合适的函数，这些函数会被 Emacs何时的时候调用。

大多数这些变量的名称都以 ' -hook'结尾。这些狗子是 *普通的钩子*，可以通过`run-hooks`运行。这种钩子的值是一个函数列表；它们将会被以无参数的形式调用，同时它们的返回值也会被省略。将新函数放入这样的钩子上的推荐方法是调用`add-hook`。

名称以' -functions'结尾的变量， 通常是*异常钩子*（一些旧代码也可能使用已弃用的 '-hook' 后缀）; 它们的值是函数列表，但是这些函数以一种特殊的方式被调用（它们需要传递参数，或者它们的返回值会被使用）。名称以' -function'结尾的变量， 具有单一的函数作为它们的值。

这不是一个详尽的列表，它只涵盖了常用的钩子。例如，每个主要模式都定义了一个名为 'modename -mode-hook'。主模式命令会自动通过 'run-mode-hooks' 运行这个普通的钩子，以完成主模式的初始化。请参阅[模式挂钩](https://www.gnu.org/software/emacs/manual/html_node/elisp/Mode-Hooks.html)。大多数次要模式也有模式挂钩。

Emacs有一个特性是，允许用户指定表达式，在加载文件时求值（请参阅[Hooks for Loading](https://www.gnu.org/software/emacs/manual/html_node/elisp/Hooks-for-Loading.html)）。该特性不完全是一个钩子，但可以完成类似的工作。

* 参见[标记](https://www.gnu.org/software/emacs/manual/html_node/elisp/The-Mark.html)：

`activate-mark-hook`

`deactivate-mark-hook`

* 请参阅[更改挂钩](https://www.gnu.org/software/emacs/manual/html_node/elisp/Change-Hooks.html)：

`after-change-functions`

`before-change-functions`

`first-change-hook`

* 请参阅[模式挂钩](https://www.gnu.org/software/emacs/manual/html_node/elisp/Mode-Hooks.html)：

`after-change-major-mode-hook`

`change-major-mode-after-body-hook`

* 请参阅[初始化文件](https://www.gnu.org/software/emacs/manual/html_node/elisp/Init-File.html)：

`after-init-hook`

`before-init-hook`

`emacs-startup-hook`

`window-setup-hook`

* 请参阅[格式转换](https://www.gnu.org/software/emacs/manual/html_node/elisp/Format-Conversion.html)。

`after-insert-file-functions`

`write-region-annotate-functions`

`write-region-post-annotation-function`

* 请参阅[创建框架](https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Frames.html)：

`after-make-frame-functions`

`before-make-frame-hook`

`server-after-make-frame-hook`

* 请参阅[保存缓冲区](https://www.gnu.org/software/emacs/manual/html_node/elisp/Saving-Buffers.html)：

`after-save-hook`

`before-save-hook`

`write-contents-functionswrite-file-functions`

* 在框架的字体更改后运行挂钩：

`after-setting-font-hook`

* 请参阅[自动保存](https://www.gnu.org/software/emacs/manual/html_node/elisp/Auto_002dSaving.html)：

`auto-save-hook`

* 请参阅[文件局部变量](https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Local-Variables.html)：

`before-hack-local-variables-hook`

`hack-local-variables-hook`

* 请参阅[延迟属性](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lazy-Properties.html)：

`buffer-access-fontify-functions`

* 当缓冲区列表改变时钩子运行（见[缓冲区列表](https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-List.html)）：

`buffer-list-update-hook`

* 调用以退出当前缓冲区的函数：

`buffer-quit-function`

* 请参阅[创建缓冲区本地](https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Buffer_002dLocal.html)：

`change-major-mode-hook`

* 这个异常钩子允许派生模式在不提示用户的情况下为底层命令解释器提供密码：

`comint-password-function`

* 请参阅[命令行参数](https://www.gnu.org/software/emacs/manual/html_node/elisp/Command_002dLine-Arguments.html)：

`command-line-functions`

* 命令循环在`post-command-hook`(qv)之后很快运行：

`delayed-warnings-hook`

* 请参阅[输入焦点](https://www.gnu.org/software/emacs/manual/html_node/elisp/Input-Focus.html)：

`focus-in-hook`

`focus-out-hook`

* 请参阅[删除](https://www.gnu.org/software/emacs/manual/html_node/elisp/Deleting-Frames.html)框架：

`delete-frame-functions`

`after-delete-frame-functions`

* 请参阅[多个终端](https://www.gnu.org/software/emacs/manual/html_node/elisp/Multiple-Terminals.html)：

`delete-terminal-functions`

* 请参阅[选择窗口选项](https://www.gnu.org/software/emacs/manual/html_node/elisp/Choosing-Window-Options.html)：

`pop-up-frame-function`

`split-window-preferred-function`

* 请参阅[回声区域自定义](https://www.gnu.org/software/emacs/manual/html_node/elisp/Echo-Area-Customization.html)：

`echo-area-clear-hook`

* 请参阅[访问功能](https://www.gnu.org/software/emacs/manual/html_node/elisp/Visiting-Functions.html)：

`find-file-hook`

`find-file-not-found-functions`

* 请参阅[要重新字体化的区域](https://www.gnu.org/software/emacs/manual/html_node/elisp/Region-to-Refontify.html)：

`font-lock-extend-after-change-region-function`

* 请参阅[多行字体锁定](https://www.gnu.org/software/emacs/manual/html_node/elisp/Multiline-Font-Lock.html)：

`font-lock-extend-region-functions`

* 请参阅[其他字体锁定变量](https://www.gnu.org/software/emacs/manual/html_node/elisp/Other-Font-Lock-Variables.html)：

`font-lock-fontify-buffer-function`

`font-lock-fontify-region-function`

`font-lock-mark-block-function`

`font-lock-unfontify-buffer-function`

`font-lock-unfontify-region-function`

* 请参阅[自动人脸分配](https://www.gnu.org/software/emacs/manual/html_node/elisp/Auto-Faces.html)：

`fontification-functions`

* 请参阅[退出 Windows](https://www.gnu.org/software/emacs/manual/html_node/elisp/Quitting-Windows.html)：

`frame-auto-hide-function`

`quit-window-hook`

* 请参阅[终止缓冲区](https://www.gnu.org/software/emacs/manual/html_node/elisp/Killing-Buffers.html)：

`kill-buffer-hookkill-buffer-query-functions`

* 请参阅[杀死 Emacs](https://www.gnu.org/software/emacs/manual/html_node/elisp/Killing-Emacs.html)：

`kill-emacs-hookkill-emacs-query-functions`

* 请参阅[菜单栏](https://www.gnu.org/software/emacs/manual/html_node/elisp/Menu-Bar.html)：

`menu-bar-update-hook`

* 请参阅[迷你缓冲区杂项](https://www.gnu.org/software/emacs/manual/html_node/elisp/Minibuffer-Misc.html)：

`minibuffer-setup-hookminibuffer-exit-hook`

* 用鼠标命令切换窗口时钩子运行：

`mouse-leave-buffer-hook`

* 请参阅[鼠标位置](https://www.gnu.org/software/emacs/manual/html_node/elisp/Mouse-Position.html)：

`mouse-position-function`

* 由前缀命令（例如C-u）运行的异常钩子，它应该返回一个描述当前前缀状态的字符串。例如，C-u产生 '铜-' 和 '铜 1 2 3-'。每个钩子函数都在不带参数的情况下被调用，并且应该返回一个描述当前前缀状态的字符串，或者`nil`如果没有前缀状态。请参阅[前缀命令参数](https://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Command-Arguments.html)：

`prefix-command-echo-keystrokes-functions`

* 当前缀命令需要通过将当前前缀命令状态传递给下一个命令来保留前缀时，挂钩运行。例如，C-u当用户键入C-u -或跟随C-u数字时， 需要将状态传递给下一个命令：

`prefix-command-preserve-state-hook`

* 在重新显示之前，在每个窗口中运行 Hook。请参阅[强制重新显示](https://www.gnu.org/software/emacs/manual/html_node/elisp/Forcing-Redisplay.html)：

`pre-redisplay-functions`

* 请参阅[命令概述](https://www.gnu.org/software/emacs/manual/html_node/elisp/Command-Overview.html)：

`post-command-hookpre-command-hook`

* 请参阅[垃圾收集](https://www.gnu.org/software/emacs/manual/html_node/elisp/Garbage-Collection.html)。

`post-gc-hook`

`post-self-insert-hook`

* 请参阅[键盘映射和次要模式](https://www.gnu.org/software/emacs/manual/html_node/elisp/Keymaps-and-Minor-Modes.html)：

`suspend-hooksuspend-resume-hook`

* 请参阅[暂停 Emacs](https://www.gnu.org/software/emacs/manual/html_node/elisp/Suspending-Emacs.html)：

`suspend-tty-functionsresume-tty-functions`

* 请参阅[句法字体锁定](https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntactic-Font-Lock.html)，请参阅[语法属性](https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Properties.html)：

`syntax-begin-function`

`syntax-propertize-extend-region-functions`

`syntax-propertize-function`

`font-lock-syntactic-face-function`

* 请参阅[临时显示器](https://www.gnu.org/software/emacs/manual/html_node/elisp/Temporary-Displays.html)：

`temp-buffer-setup-hook`

`temp-buffer-show-function`

`temp-buffer-show-hook`

* 请参阅[特定于终端的](https://www.gnu.org/software/emacs/manual/html_node/elisp/Terminal_002dSpecific.html)：

`tty-setup-hook`

* 请参阅[窗口挂钩](https://www.gnu.org/software/emacs/manual/html_node/elisp/Window-Hooks.html)：

`window-configuration-change-hook`

`window-scroll-functionswindow-size-change-functions`
