[plt-scheme] writing scheme application extensions in scheme

From: Peter Santoro (peter at pscomp.com)
Date: Mon Jun 9 20:16:00 EDT 2003

Fellow Schemers,

I would like to be able to easily add new functionality to the scheme application I'm writing.  Ideally these extensions 
would be placed in an extensions directory to be loaded at application initialization.  I'm using mzscheme and I quickly 
put the following code together.  It seems to work ok, but I would appreciate constructive criticism on the design and 
code.  If there's a better way to solve this problem, I'd like to hear about that, too.

Thank you for your assistance,


; sample hook-mgr.scm

(define _hooks (list))
(define _hook-key 0)

(define (get-next-hook-key)
   (begin0 _hook-key (set! _hook-key (+ _hook-key 1))))

(define (get-hook-key ls)
   (car ls))

(define (get-hook-desc key)
   (let ((ls (assq key _hooks)))
     (if ls
       (caadr ls)

(define (get-hook-proc key)
   (let ((ls (assq key _hooks)))
     (if ls
       (cadadr ls)

(define (set-hook! proc menu-text)
   (set! _hooks (append _hooks (list (list (get-next-hook-key) (list menu-text proc))))))

; loop through extension directory, loading each extension (hard-coded now for testing)

(load "hook1.scm")
(load "hook2.scm")

; add extensions to menu (may want to add additional info in hook struct to identify menu)

; sample hook1.scm

(define (test-hook-proc1 . args)
   (display "test hook proc 1") (newline))

(set-hook! test-hook-proc1 "test hook proc one")

; sample hook2.scm

(define (test-hook-proc2 . args)
   (display "test hook proc 2") (newline))

(set-hook! test-hook-proc2 "test hook proc two")

Posted on the users mailing list.