sigildocs

(sigil hooks)

(sigil hooks) - Emacs-Style Hook System

Hooks are lists of functions called when specific events occur. Multiple handlers can be registered for each hook and are called in order of registration.

Basic Usage

(import (sigil hooks))

(define my-hook (make-hook))

(add-hook! my-hook (lambda () (display "First\n")))
(add-hook! my-hook (lambda () (display "Second\n")))

(run-hook my-hook)
; prints: First
;         Second

Passing Arguments

(define on-file-save (make-hook))

(add-hook! on-file-save
  (lambda (filename)
    (display "Saved: ")
    (display filename)))

(run-hook-with-args on-file-save "document.txt")

Exports

make-hookprocedure

Create a new empty hook.

(define my-hook (make-hook))
hook?procedure

Check if a value is a hook.

(hook? my-hook)  ; => #t
(hook? '())      ; => #f
hook-empty?procedure

Check if a hook has no handlers.

(hook-empty? (make-hook))  ; => #t
add-hook!procedure

Add a function to a hook.

Functions are called in the order they are added.

(add-hook! my-hook (lambda () (display "Called!")))
remove-hook!procedure

Remove a function from a hook.

The function is matched by identity (eq?).

(define my-fn (lambda () ...))
(add-hook! my-hook my-fn)
(remove-hook! my-hook my-fn)
clear-hook!procedure

Remove all handlers from a hook.

(clear-hook! my-hook)
run-hookprocedure

Run a hook with no arguments.

Calls each handler function in order.

(run-hook my-hook)

Run a hook with arguments.

Each handler receives the same arguments.

(run-hook-with-args on-save-hook filename buffer)