aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/module.scm
blob: bb0c5ad58e1a9315241faf9647df41aed7799917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
;;; module.scm  --  Integration of Skribe code as Guile modules.
;;;
;;; Copyright 2005  Ludovic Court�s <ludovic.courtes@laas.fr>
;;;
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program 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 General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;; USA.

(define-module (skribilo module)
  :use-module (skribilo reader)
  :use-module (skribilo evaluator)
  :use-module (skribilo debug)
  :use-module (srfi srfi-1)
  :use-module (ice-9 optargs))

;;; Author:  Ludovic Court�s
;;;
;;; Commentary:
;;;
;;; This (fake) module defines a macro called `define-skribe-module' which
;;; allows to package Skribe code (which uses Skribe built-ins and most
;;; importantly a Skribe syntax) as a Guile module.  This module
;;; automatically exports the macro as a core binding so that future
;;; `use-modules' referring to Skribe modules will work as expected.
;;;
;;; Code:

(define *skribilo-user-imports*
  ;; List of modules that should be imported by any good Skribilo module.
  '((srfi srfi-1)         ;; lists
    (srfi srfi-13)        ;; strings
    ;(srfi srfi-19)        ;; date and time
    (ice-9 optargs)       ;; `define*'
    (ice-9 and-let-star)  ;; `and-let*'
    (ice-9 receive)       ;; `receive'

    (skribilo module)
    (skribilo types)      ;; `<document>', `document?', etc.
    (skribilo config)
    (skribilo vars)
    (skribilo runtime)    ;; `the-options', `the-body'
    (skribilo biblio)
    (skribilo lib)        ;; `define-markup', `unwind-protect', etc.
    (skribilo resolve)
    (skribilo engine)
    (skribilo writer)
    (skribilo output)
    (skribilo evaluator)
    (skribilo color)
    (skribilo debug)
    (skribilo source)     ;; `source-read-lines', `source-fontify', etc.
    (skribilo coloring lisp) ;; `skribe', `scheme', `lisp'
    (skribilo coloring xml)  ;; `xml'
    ))

(define %skribe-core-modules
  '("utils" "api" "bib" "index" "param" "sui"))

(define-macro (define-skribe-module name . options)
  `(begin
     (define-module ,name
       #:reader (make-reader 'skribe)
       #:use-module (skribilo reader)
       ,@options)

     ;; Pull all the bindings that Skribe code may expect, plus those needed
     ;; to actually create and read the module.
     ,(cons 'use-modules
	    (append *skribilo-user-imports*
		    (filter-map (lambda (mod)
				  (let ((m `(skribilo skribe
						      ,(string->symbol
							mod))))
				    (and (not (equal? m name)) m)))
				%skribe-core-modules)))))


;; Make it available to the top-level module.
(module-define! the-root-module
                'define-skribe-module define-skribe-module)




(define *skribilo-user-module* #f)

;;;
;;; MAKE-RUN-TIME-MODULE
;;;
(define-public (make-run-time-module)
  "Return a new module that imports all the necessary bindings required for
execution of Skribilo/Skribe code."
  (let ((the-module (make-module)))
        (for-each (lambda (iface)
                    (module-use! the-module (resolve-module iface)))
                  (append *skribilo-user-imports*
			  (map (lambda (mod)
				 `(skribilo skribe
					    ,(string->symbol mod)))
			       %skribe-core-modules)))
        (set-module-name! the-module '(skribilo-user))
        the-module))

;;;
;;; RUN-TIME-MODULE
;;;
(define-public (run-time-module)
  "Return the default instance of a Skribilo/Skribe run-time module."
  (if (not *skribilo-user-module*)
      (set! *skribilo-user-module* (make-run-time-module)))
  *skribilo-user-module*)


;; FIXME:  This will eventually be replaced by the per-module reader thing in
;;         Guile.
(define-public (load-file-with-read file read module)
  (with-debug 5 'load-file-with-read
     (debug-item "loading " file)

     (with-input-from-file (search-path %load-path file)
       (lambda ()
;      (format #t "load-file-with-read: ~a~%" read)
	 (let loop ((sexp (read))
		    (result #f))
	   (if (eof-object? sexp)
	       result
	       (begin
;              (format #t "preparing to evaluate `~a'~%" sexp)
		 (loop (read)
		       (primitive-eval sexp)))))))))

(define-public (load-skribilo-file file reader-name)
  (load-file-with-read file (make-reader reader-name) (current-module)))

(define*-public (load-skribe-modules #:optional (debug? #f))
  "Load the core Skribe modules, both in the @code{(skribilo skribe)}
hierarchy and in @code{(run-time-module)}."
  (for-each (lambda (mod)
              (format #t "~~ loading skribe module `~a'...~%" mod)
              (load-skribilo-file (string-append "skribilo/skribe/"
                                                 mod ".scm")
                                  'skribe)
              (module-use! (run-time-module)
                           (resolve-module `(skribilo skribe
                                             ,(string->symbol mod)))))
            %skribe-core-modules))


;;; module.scm ends here