This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

practical Guile module handling


I've added a couple of abstractions over the standard guile module
system for Scwm that are probably general purpose enough to be done in
guile itself.  Some may be changed by the module reworkings, of course,
but the gist of my changes still is useful, IMO.

I keep a *scwm-modules* (private) global variable that gets updated on
each successful module load, and write a `scwm-module-loaded?'
predicate that does the obvious thing (I am aware of the feature stuff,
but not all modules provide a feature that can be tested).  I also use
this infrastructure to implement an `eval-after-load' mechanism (that
evals the proc immediately if the module is already loaded, or once it
is loaded successfully).  Finally, I add a `use-scwm-module' macro that
(in addition to assumming (app scwm) as a prefix to whatever module name
you give [obviously that part is not generally useful to guile]) ensures
that a failed module load doesn't prevent all the other modules from
loading.  E.g., guile's

(use-modules (app scwm base)
             (app scwm flux)
             (app scwm window-locations))

will export none of the modules' interfaces if there is an error, say,
in flux.scm.  use-scwm-modules fixes this so I can write:

(use-scwm-modules (app scwm base)
                  (app scwm flux)
                  (app scwm window-locations))

and base and window-locations will at least still get loaded and
exported properly. [[ Actually, we write:

(use-scwm-modules base flux window-locations)

since (app scwm) is a default prefix ]]

In any case, some of these ideas most certainly seem useful to the
core, and can probably be done a better way when willing to change ice-9 
stuff.  The relevant code from scwm's scheme/minimal.scm is included below.
I'd also love it if anyone notices problems with my approach or ways to
improve it;  we're getting close to a scwm-1.0, so any feedback is
appreciated.

Greg

P.S. Thanks, Jim, for applying my snarf.h patch.


;;;; BEGIN CODE FROM scheme/minimal.scm

;; GJB:FIXME:: this should not be public,
;; but I leave it public for now for easier debugging --07/03/99 gjb
(define-public *scwm-modules* '())

(define-public (scwm-module-loaded? module)
  "Return #t iff MODULE has been loaded."
  (let ((entry (assoc module *scwm-modules*))) 
    (and entry (null? (cdr entry)))))

(define (use-scwm-module-note-success module)
  (let ((entry (assoc module *scwm-modules*)))
    (if (not entry)
	(set! *scwm-modules* (cons (cons module '()) *scwm-modules*))
	(let ((eval-after-load-proc (cdr entry)))
	  (if (not (null? eval-after-load-proc))
	      (let ((answer (eval-after-load-proc)))
		(set-cdr! entry '())
		answer))))))

(define-public (eval-after-load module proc)
  "Run PROC after MODULE is loaded.
Run PROC immediately if MODULE has already been loaded."
  (if (scwm-module-loaded? module)
      (proc)
      (set! *scwm-modules* (cons (cons module proc) *scwm-modules*))))

(define-public (process-use-scwm-module module)
  (if (symbol? module)
      (set! module (append '(app scwm) (list module))))
  (catch #t
	 (lambda ()
	   (process-use-modules (list module))
	   (use-scwm-module-note-success module)
	   module)
	 (lambda (key . args)
	   (display "Error loading module: ")
	   (display module) (newline)
	   (catch #t
		  (lambda () (apply handle-system-error (cons key args)))
		  (lambda (key . args) #t))
	   #f)))

(define-public (process-use-scwm-modules module-list)
  "Returns a list of all the modules loaded in successfully.
Modules that failed to load have #f in their place in the
list instead of the module."
  (map process-use-scwm-module (reverse module-list)))

(defmacro use-scwm-modules modules
  `(process-use-scwm-modules ',modules))

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]