12. Hooks and events

Hooks

clojure
(add-hook! :after-save-hook (fn [ed] (ed/message ed "saved!") ed))
代码

A hook receives and returns an editor. An error is written to the echo area and does not
interrupt editing. Available hooks:

| Hook | Runs |
|---|---|
| `:after-init-hook` | After startup completes |
| `:find-file-hook` | After a file opens |
| `:after-save-hook` | After saving |
| `:kill-buffer-hook` | Before a buffer is killed (runs in the killed buffer) |
| `:kill-emacs-hook` | On exit |
| `:post-command-hook` | After every input event |
| `:post-self-insert-hook` | After self-inserting a character |
| `:window-change-hook` | After window layout changes |
| `:tick-hook` | Every main-loop tick (argument is milliseconds now) |
| `:change-major-mode-hook` / `:after-change-major-mode-hook` | Before / after changing a major mode |
| `<mode>-mode-hook` (for example `:clojure-mode-hook`, `:prog-mode-hook`) | After entering a major mode |
| `<minor-mode>-hook` (for example `:slime-mode-hook`) | After a minor mode turns on or off |

The third argument to `add-hook!` is depth (`-100..100`; default 0; `true` means 90,
following Emacs APPEND). Buffer-local hooks use `coxn.hooks/add-local-hook`.

### Events (bus)
clojure
(subscribe! :document-opened (fn [event {:keys [buffer path new?]}] ...))
代码

Events are **facts that already happened** (past tense + immutable payload); handlers do
not return an editor and cannot veto the event. Built-ins are `:document-opened`
(`{:buffer :path :new?}`) and `:document-renamed` (`{:buffer :new-buffer :path}`).
`subscribe!` returns an unsubscribe function.

Raw Markdown: coxn manual (Markdown) · Installers: coxn product page