Skip to content

Commit

Permalink
hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vindarel committed Apr 2, 2024
1 parent 92f36db commit fedbd4b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions content/en/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,46 @@ you can add a formatter in the mode definition:
)
```

## Hooks

Hooks allow you to run abritrary code before (or after) a command is called or a
given mode is entered (or exited).

Some examples:

- `after-save-hook`
- `kill-buffer-hook`
- `*find-file-hook*`
- `*exit-editor-hook*`
- `*after-init-hook*`
- [self-insert-hook](https://github.com/lem-project/lem/blob/main/extensions/lisp-mode/self-insert-hook.lisp): run code before or after a character is inserted in the buffer.
- and more: checkout each mode (programming modes, vi mode…) inside Lem with the completion.

The hooks mechanism is defined in `src/common/hooks.lisp`. It is made of the available functions and macros:

- `add-hook place callback`: add a hook
- example: `(add-hook *after-init-hook* #'display-welcome)`

- `remove-hook place callback`: remove a hook

- `run-hooks list`: run all hooks.

Each mode defines a hook variable name. For example:

```lisp
(define-major-mode c-mode language-mode
(:name "C"
:keymap *c-mode-keymap*
:syntax-table *c-syntax-table*
:mode-hook *c-mode-hook* ;; <---------- defines our hook name
:formatter #'lem-c-mode/format:clang-format)
(setf (variable-value 'enable-syntax-highlight) t)
;; more settings here…
)
(add-hook *c-mode-hook* 'guess-offset) ;; <------ adds a hook
```


## Example users' init files

Expand Down

0 comments on commit fedbd4b

Please sign in to comment.