;;; Copyright (C) 2025 Free Software Foundation, Inc. ;;; ;;; This library is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU Lesser General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this program. If not, see ;;; . ;;; Commentary: ;;; ;;; ;;; Code: (define-module (ice-9 hooks) ;; FIXME: #:export instead of #:replace when deprecated code is ;; removed. #:replace (make-hook hook? hook-empty? add-hook! remove-hook! reset-hook! run-hook hook->list)) (define (make-record-type ' '(procs) (lambda (hook port) (print-hook hook port)))) (define %make-hook (record-constructor )) (define* (make-hook #:optional nargs) "Create a hook containing an ordered list of procedures." (%make-hook '())) (define hook? (record-predicate )) (define hook-procs (record-accessor 'procs)) (define set-hook-procs! (record-modifier 'procs)) (define (hook-empty? hook) "Return @code{#t} if @var{hook} is an empty hook, @code{#f} otherwise." (null? (hook-procs hook))) (define* (add-hook! hook proc #:optional _append? #:key (append? _append?)) "Add the procedure @var{proc} to the hook @var{hook}. The procedure is added to the end if @var{append?} is true, otherwise it is added to the front." (let ((procs (delq! proc (hook-procs hook)))) (set-hook-procs! hook (if append? (append procs (list proc)) (cons proc procs)))) (values)) (define (remove-hook! hook proc) "Remove the procedure @var{proc} from the hook @var{hook}." (set-hook-procs! hook (delq! proc (hook-procs hook))) (values)) (define (reset-hook! hook) "Remove all procedures from the hook @var{hook}." (set-hook-procs! hook '()) (values)) (define (run-hook hook . args) "Apply all procedures from the hook @var{hook} to the arguments @var{args}. The order of the procedure application is first to last. The return value of this procedure is not specified." (for-each (lambda (proc) (apply proc args)) (hook-procs hook)) (values))