r3049: *** empty log message ***
[kmrcl.git] / ml-class.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          ml-class.lisp
6 ;;;; Purpose:       Markup Language Metaclass
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; This metaclass as functions to classes to allow display
11 ;;;; in Text, HTML, and XML formats. This includes hyperlinking
12 ;;;; capability and sub-objects.
13 ;;;;
14 ;;;; $Id: ml-class.lisp,v 1.13 2002/10/16 05:57:12 kevin Exp $
15 ;;;;
16 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
17 ;;;;
18 ;;;; KMRCL users are granted the rights to distribute and use this software
19 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
20 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
21 ;;;; *************************************************************************
22  
23 (in-package :kmrcl)
24
25 (declaim (optimize (speed 3) (safety 1)))
26
27 (defun ml-class-of (obj)
28   #-(or cmu sbcl) (class-of obj)
29   #+sbcl (sb-pcl:class-of obj)
30   #+cmu (pcl:class-of obj))
31
32 (defun ml-class-name (obj)
33   #-(or cmu sbcl) (class-name obj)
34   #+sbcl (sb-pcl:class-name obj)
35   #+cmu (pcl:class-name obj))
36
37 (defclass ml-class (#-(or cmu sbcl) standard-class
38                       #+cmu pcl::standard-class
39                       #+sbcl sb-pcl::standard-class)
40   ((title :initarg :title :type string :reader ml-std-title
41           :documentation 
42 "Print Title for class")
43    (fields :initarg :fields :reader ml-std-fields
44            :documentation
45 "List of field lists for printing. Format is 
46    ((fieldname type optional-formatter) ... )")
47    (subobjects-lists 
48     :initarg :subobjects-lists :reader ml-std-subobjects-lists
49     :documentation 
50 "List of fields that contain a list of subobjects objects.")
51    (ref-fields 
52     :initarg :ref-fields :type list :reader ml-std-ref-field
53     :documentation 
54     "List of fields that can be referred to by browsers. 
55 Format is ((field-name field-lookup-func other-link-params) ...)")
56    
57    ;;; The remainder of these fields are calculated one time
58    ;;; in finalize-inheritence.
59    (value-func :initform nil :type function :reader ml-std-value-func)
60    (xmlvalue-func :initform nil :type function :reader ml-std-xmlvalue-func)
61    (fmtstr-text :initform nil :type string :reader ml-std-fmtstr-text)
62    (fmtstr-html :initform nil :type string :reader ml-std-fmtstr-html)
63    (fmtstr-xml :initform nil :type string :reader ml-std-fmtstr-xml)
64    (fmtstr-text-labels :initform nil :type string :reader ml-std-fmtstr-text-labels)
65    (fmtstr-html-labels :initform nil :type string :reader ml-std-fmtstr-html-labels)
66    (fmtstr-xml-labels :initform nil :type string :reader ml-std-fmtstr-xml-labels)
67    (fmtstr-html-ref :initform nil :type string :reader ml-std-fmtstr-html-ref)
68    (fmtstr-xml-ref :initform nil :type string :reader ml-std-fmtstr-xml-ref)
69    (fmtstr-html-ref-labels :initform nil :type string :reader ml-std-fmtstr-html-ref-labels)
70    (fmtstr-xml-ref-labels :initform nil :type string :reader ml-std-fmtstr-xml-ref-labels)
71    )
72   (:default-initargs :title nil :fields nil :subobjects-lists nil :ref-fields nil)
73   (:documentation "Metaclass for Markup Language classes."))
74
75 #+cmu
76 (defmethod pcl:finalize-inheritance :after ((cl ml-class))
77   (init-ml-class cl))
78
79
80 #+sbcl
81 (defmethod sb-pcl:finalize-inheritance :after ((cl ml-class))
82   (init-ml-class cl))
83
84
85 #+cmu
86 (defmethod pcl:validate-superclass ((class ml-class) (superclass pcl::standard-class))
87   t)
88
89 #+sbcl
90 (defmethod sb-pcl:validate-superclass ((class ml-class) (superclass sb-pcl::standard-class))
91   t)
92
93 #+allegro
94 (defmethod mop:finalize-inheritance :after ((cl ml-class))
95   (init-ml-class cl))
96
97 #+lispworks
98 (defmethod clos:finalize-inheritance :after ((cl ml-class))
99   (init-ml-class cl))
100
101 ;;#+cmu
102 ;;(defmethod pcl::class-finalized-p ((cl ml-class))
103 ;;  (and (not (null (slot-value cl 'pcl::wrapper)))
104 ;;       (not (null (slot-value cl 'fmtstr-text)))))
105
106 ;;#+sbcl
107 ;;(defmethod sb-pcl::class-finalized-p ((cl ml-class))
108 ;;  (and (not (null (slot-value cl 'sb-pcl::wrapper)))
109 ;;       (not (null (slot-value cl 'fmtstr-text)))))
110
111 #+lispworks
112 (defmethod clos:process-a-class-option ((class ml-class)
113                                         (name (eql :title))
114                                         value)
115   (unless value
116     (error "ml-class title must have a value"))
117   (if (null (cdr value))
118       (list name (car value))
119     (list name `',value)))
120
121 #+lispworks
122 (defmethod clos:process-a-class-option ((class ml-class)
123                                         (name (eql :fields))
124                                         value)
125   (unless value
126     (error "ml-class fields must have a value"))
127   (list name `',value))
128
129 #+lispworks
130 (defmethod clos:process-a-class-option ((class ml-class)
131                                         (name (eql :ref-fields))
132                                         value)
133   (unless value
134     (error "ml-class ref-fields must have a value"))
135   (list name `',value))
136
137 #+lispworks
138 (defmethod clos:process-a-class-option ((class ml-class)
139                                         (name (eql :subobjects-lists))
140                                         value)
141   (unless value
142     (error "ml-class subobjects-lists must have a value"))
143   (list name `',value))
144
145 ;;;; Class initialization function
146       
147 (defun init-ml-class (cl)
148     (let ((fmtstr-text "")
149           (fmtstr-html "")
150           (fmtstr-xml "")
151           (fmtstr-text-labels "")
152           (fmtstr-html-labels "")
153           (fmtstr-xml-labels "")
154           (fmtstr-html-ref "")
155           (fmtstr-xml-ref "")
156           (fmtstr-html-ref-labels "")
157           (fmtstr-xml-ref-labels "")
158           (first-field t)
159           (value-func '())
160           (xmlvalue-func '())
161           (classname (class-name cl))
162           (package (symbol-package (ml-class-name cl)))
163           (ref-fields (slot-value cl 'ref-fields)))
164       (declare (ignore classname))
165       (dolist (f (slot-value cl 'fields))
166         (let ((name (car f))
167               (namestr (symbol-name (car f)))
168               (namestr-lower (string-downcase (symbol-name (car f))))
169               (type (cadr f))
170               (formatter (caddr f))
171               (value-fmt "~a")
172               (plain-value-func nil)
173               html-str xml-str html-label-str xml-label-str)
174           
175           (when (or (eql type :integer) (eql type :fixnum))
176             (setq value-fmt "~d"))
177           
178           (when (eql type :commainteger)
179             (setq value-fmt "~:d"))
180           
181           (when (eql type :boolean)
182             (setq value-fmt "~a"))
183             
184           (if first-field
185               (setq first-field nil)
186             (progn
187               (string-append fmtstr-text " ")
188               (string-append fmtstr-html " ")
189               (string-append fmtstr-xml " ")
190               (string-append fmtstr-text-labels " ")
191               (string-append fmtstr-html-labels " ")
192               (string-append fmtstr-xml-labels " ")
193               (string-append fmtstr-html-ref " ")
194               (string-append fmtstr-xml-ref " ")
195               (string-append fmtstr-html-ref-labels " ")
196               (string-append fmtstr-xml-ref-labels " ")))
197           
198           (setq html-str value-fmt)
199           (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
200           (setq html-label-str (concatenate 'string "<i>" namestr-lower "</i> " value-fmt))
201           (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
202           
203           (string-append fmtstr-text value-fmt)
204           (string-append fmtstr-html html-str)
205           (string-append fmtstr-xml xml-str)
206           (string-append fmtstr-text-labels namestr-lower " " value-fmt)
207           (string-append fmtstr-html-labels html-label-str)
208           (string-append fmtstr-xml-labels xml-label-str)
209           
210           (if (find name ref-fields :key #'car)
211             (progn
212               (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
213               (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
214               (string-append fmtstr-html-ref-labels "<i>" namestr-lower "</i> <~~a>" value-fmt "</~~a>")
215               (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
216             (progn
217               (string-append fmtstr-html-ref html-str)
218               (string-append fmtstr-xml-ref xml-str)
219               (string-append fmtstr-html-ref-labels html-label-str)
220               (string-append fmtstr-xml-ref-labels xml-label-str)))
221           
222           (if formatter
223               (setq plain-value-func 
224                 (list `(,formatter (,(intern namestr package) x))))
225             (setq plain-value-func 
226               (list `(,(intern namestr package) x))))
227           (setq value-func (append value-func plain-value-func))
228           
229           (if (eql type :cdata)
230               (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
231             (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
232           ))
233
234       (if value-func
235           (setq value-func `(lambda (x) (values ,@value-func)))
236         (setq value-func `(lambda () (values))))
237       (setq value-func (compile nil (eval value-func)))
238       
239       (if xmlvalue-func
240           (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
241         (setq xmlvalue-func `(lambda () (values))))
242       (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
243
244       (setf (slot-value cl 'fmtstr-text) fmtstr-text)
245       (setf (slot-value cl 'fmtstr-html) fmtstr-html)
246       (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
247       (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
248       (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
249       (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
250       (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
251       (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
252       (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
253       (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
254       (setf (slot-value cl 'value-func) value-func)
255       (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
256     (values))
257
258
259 (defun ml-class-fmtstr-text (obj)
260   (slot-value (ml-class-of obj) 'fmtstr-text))
261
262 (defun ml-class-fmtstr-html (obj)
263   (slot-value (ml-class-of obj) 'fmtstr-html))
264
265 (defun ml-class-fmtstr-xml (obj)
266   (slot-value (ml-class-of obj) 'fmtstr-xml))
267
268 (defun ml-class-fmtstr-text-labels (obj)
269   (slot-value (ml-class-of obj) 'fmtstr-text-labels))
270
271 (defun ml-class-fmtstr-html-labels (obj)
272   (slot-value (ml-class-of obj) 'fmtstr-html-labels))
273
274 (defun ml-class-fmtstr-xml-labels (obj)
275   (slot-value (ml-class-of obj) 'fmtstr-xml-labels))
276
277 (defun ml-class-value-func (obj)
278   (slot-value (ml-class-of obj) 'value-func))
279
280 (defun ml-class-xmlvalue-func (obj)
281   (slot-value (ml-class-of obj) 'xmlvalue-func))
282
283 (eval-when (:compile-toplevel :load-toplevel :execute)
284 (defun ml-class-title (obj)
285   (awhen (slot-value (ml-class-of obj) 'title)
286             (if (consp it)
287                 (car it)
288               it))))
289
290 (defun ml-class-subobjects-lists (obj)
291   (slot-value (ml-class-of obj) 'subobjects-lists))
292
293 (defun ml-class-ref-fields (obj)
294   (slot-value (ml-class-of obj) 'ref-fields))
295
296 (defun ml-class-fields (obj)
297   (slot-value (ml-class-of obj) 'fields))
298
299 (defun ml-class-fmtstr-html-ref (obj)
300   (slot-value (ml-class-of obj) 'fmtstr-html-ref))
301
302 (defun ml-class-fmtstr-xml-ref (obj)
303   (slot-value (ml-class-of obj) 'fmtstr-xml-ref))
304
305 (defun ml-class-fmtstr-html-ref-labels (obj)
306   (slot-value (ml-class-of obj) 'fmtstr-html-ref-labels))
307
308 (defun ml-class-fmtstr-xml-ref-labels (obj)
309   (slot-value (ml-class-of obj) 'fmtstr-xml-ref-labels))
310
311 ;;; Class name functions
312
313 (defgeneric ml-class-stdname (x))
314 (defmethod ml-class-stdname ((name string))
315   (string-downcase (subseq name 1)))
316   
317 (defmethod ml-class-stdname ((cl standard-object))
318   (string-downcase (subseq (ml-class-name (ml-class-of cl)) 1)))
319   
320 ;;;; Generic Print functions
321
322 (defparameter *default-textformat* nil)
323 (defparameter *default-htmlformat* nil)
324 (defparameter *default-htmlrefformat* nil)
325 (defparameter *default-xmlformat* nil)
326 (defparameter *default-xmlrefformat* nil)
327 (defparameter *default-ie-xmlrefformat* nil)
328 (defparameter *default-nullformat* nil)
329 (defparameter *default-init-format?* nil)
330
331 (defun make-format-instance (fmt)
332   (unless *default-init-format?*
333     (setq *default-textformat* (make-instance 'textformat))
334     (setq *default-htmlformat* (make-instance 'htmlformat))
335     (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
336     (setq *default-xmlformat* (make-instance 'xmlformat))
337     (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
338     (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
339     (setq *default-nullformat* (make-instance 'nullformat))
340     (setq *default-init-format?* t))
341   
342   (case fmt
343       (:text *default-textformat*)
344       (:html *default-htmlformat*)
345       (:htmlref *default-htmlrefformat*)
346       (:xml  *default-xmlformat*)
347       (:xml  *default-xmlformat*)
348       (:xmlref *default-xmlrefformat*)
349       (:ie-xmlref *default-ie-xmlrefformat*)
350       (:null *default-nullformat*)
351       (otherwise *default-textformat*)))
352     
353 ;;;; Output format classes for print ml-classes
354
355 (defclass dataformat ()
356   ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
357    (file-end-str :type string :initarg :file-end-str :reader file-end-str)
358    (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
359    (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
360    (list-start-indent :initarg :list-start-indent :reader list-start-indent)
361    (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
362    (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
363    (list-end-indent :initarg :list-end-indent :reader list-end-indent)
364    (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
365    (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
366    (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
367    (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
368    (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
369    (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
370    (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
371    (obj-data-fmtstr :initarg :obj-data-fmtstr :reader  obj-data-fmtstr)
372    (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader  obj-data-fmtstr-labels)
373    (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
374    (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
375    (link-ref :initarg :link-ref :reader link-ref))
376   (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
377                      :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
378                      :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
379                      :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
380                      :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
381                      :obj-data-value-func nil :link-ref nil)
382   (:documentation "Parent for all dataformat objects"))
383
384 (defclass binaryformat (dataformat)
385   ())
386
387 (defclass nullformat (dataformat)
388   ())
389
390 (defun text-list-start-value-func (obj nitems)
391   (values (ml-class-title obj) nitems))
392
393 (defclass textformat (dataformat) 
394   ()    
395   (:default-initargs :list-start-fmtstr "~a~P:~%"
396     :list-start-value-func #'text-list-start-value-func
397     :list-start-indent t
398     :obj-data-indent t
399     :obj-data-fmtstr #'ml-class-fmtstr-text
400     :obj-data-fmtstr-labels #'ml-class-fmtstr-text-labels
401     :obj-data-end-fmtstr "~%"
402     :obj-data-value-func #'ml-class-value-func))
403
404 (defclass htmlformat (textformat) 
405   ()
406   (:default-initargs :file-start-str "<html><body>~%"
407     :file-end-str "</body><html>~%"
408     :list-start-indent t
409     :list-start-fmtstr "<p><b>~a~P:</b></p><ul>~%"
410     :list-start-value-func #'text-list-start-value-func
411     :list-end-fmtstr "</ul>~%"
412     :list-end-indent t
413     :list-end-value-func #'identity
414     :obj-start-indent t
415     :obj-start-fmtstr "<li>"
416     :obj-start-value-func #'identity
417     :obj-end-indent  t
418     :obj-end-fmtstr  "</li>~%"
419     :obj-end-value-func #'identity
420     :obj-data-indent t
421     :obj-data-fmtstr #'ml-class-fmtstr-html-labels
422     :obj-data-fmtstr-labels #'ml-class-fmtstr-html-labels
423     :obj-data-value-func #'ml-class-value-func))
424
425
426 (defun class-name-of (obj)
427   (string-downcase (ml-class-name (ml-class-of obj))))
428
429 (defun xmlformat-list-end-value-func (x)
430   (format nil "~alist" (string-downcase (ml-class-name (ml-class-of x)))))
431
432 (defun xmlformat-list-start-value-func (x nitems) 
433   (values (format nil "~alist" (string-downcase (ml-class-name (ml-class-of x)))) (ml-class-title x) nitems))
434
435 (defclass xmlformat (textformat) 
436   ()
437   (:default-initargs :file-start-str "" ; (std-xml-header)
438     :list-start-indent  t
439     :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
440     :list-start-value-func #'xmlformat-list-start-value-func
441     :list-end-indent  t
442     :list-end-fmtstr "</~a>~%"
443     :list-end-value-func #'xmlformat-list-end-value-func
444     :obj-start-fmtstr "<~a>"
445     :obj-start-value-func #'class-name-of
446     :obj-start-indent t
447     :obj-end-fmtstr "</~a>~%"
448     :obj-end-value-func #'class-name-of
449     :obj-end-indent nil
450     :obj-data-indent nil
451     :obj-data-fmtstr #'ml-class-fmtstr-xml
452     :obj-data-fmtstr-labels #'ml-class-fmtstr-xml-labels
453     :obj-data-value-func #'ml-class-xmlvalue-func))
454
455 (defclass link-ref ()
456   ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
457    (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
458    (page-name :type string :initarg :page-name :accessor page-name)
459    (href-head :type string :initarg :href-head :accessor href-head)
460    (href-end :type string :initarg :href-end :accessor href-end)
461    (ampersand :type string :initarg :ampersand :accessor ampersand))
462   (:default-initargs :fmtstr nil 
463     :fmtstr-labels nil 
464     :page-name "disp-func1" 
465     :href-head nil :href-end nil :ampersand nil)
466   (:documentation "Formatting for a linked reference"))
467
468 (defclass html-link-ref (link-ref)
469   ()
470   (:default-initargs :fmtstr #'ml-class-fmtstr-html-ref  
471     :fmtstr-labels #'ml-class-fmtstr-html-ref-labels
472     :href-head "a href=" 
473     :href-end "a" 
474     :ampersand "&"))
475
476 (defclass xml-link-ref (link-ref)
477   ()
478   (:default-initargs :fmtstr #'ml-class-fmtstr-xml-ref 
479                      :fmtstr-labels #'ml-class-fmtstr-xml-ref-labels
480                      :href-head "xmllink xlink:type=\"simple\" xlink:href=" 
481                      :href-end "xmllink" 
482                      :ampersand "&amp;")
483   (:documentation "Mozilla's and W3's idea of a link with XML"))
484
485 (defclass ie-xml-link-ref (xml-link-ref)
486   ()
487   (:default-initargs :href-head "html:a href=" 
488                      :href-end "html:a" )
489   (:documentation "Internet Explorer's idea of a link with XML"))
490
491
492 (defclass htmlrefformat (htmlformat)
493   ()
494   (:default-initargs :link-ref (make-instance 'html-link-ref)))
495
496 (defclass xmlrefformat (xmlformat)
497   ()
498   (:default-initargs :link-ref (make-instance 'xml-link-ref)))
499
500 (defclass ie-xmlrefformat (xmlformat)
501   ()
502   (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
503
504
505 ;;; File Start and Ends
506
507 (defgeneric fmt-file-start (fmt s))
508 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
509
510 (defmethod fmt-file-start ((fmt textformat) (s stream))
511   (aif (file-start-str fmt)
512       (format s it)))
513
514 (defgeneric fmt-file-end (fmt s))
515 (defmethod fmt-file-end ((fmt textformat) (s stream))
516   (aif (file-end-str fmt)
517           (format s it)))
518
519 ;;; List Start and Ends
520
521 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
522 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
523   (if (list-start-indent fmt)
524       (indent-spaces indent s))
525   (aif (list-start-fmtstr fmt)
526           (apply #'format s it
527                  (multiple-value-list
528                   (funcall (list-start-value-func fmt) x num-items)))))
529
530 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
531 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
532   (declare (ignore num-items))
533   (if (list-end-indent fmt)
534       (indent-spaces indent s))
535   (aif (list-end-fmtstr fmt)
536           (apply #'format s it
537                  (multiple-value-list
538                   (funcall (list-end-value-func fmt) x)))))
539
540 ;;; Object Start and Ends
541
542 (defgeneric fmt-obj-start (obj fmt s &optional indent))
543 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
544   (if (obj-start-indent fmt)
545       (indent-spaces indent s))
546   (aif (obj-start-fmtstr fmt)
547           (apply #'format s it
548                  (multiple-value-list
549                   (funcall (obj-start-value-func fmt) x)))))
550
551 (defgeneric fmt-obj-end (obj fmt s &optional indent))
552 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
553   (if (obj-end-indent fmt)
554       (indent-spaces indent s))
555   (aif (obj-end-fmtstr fmt)
556           (apply #'format s it
557                  (multiple-value-list
558                   (funcall (obj-end-value-func fmt) x)))))
559   
560 ;;; Object Data 
561
562 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
563 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
564   (declare (ignore obj fieldname))
565   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
566           (href-head ref) (make-url (page-name ref)) fieldfunc 
567           (ampersand ref) fieldvalue
568           (if refvars
569               (let ((varstr ""))
570                 (dolist (var refvars)
571                   (string-append varstr (format nil "~a~a=~a" 
572                                                 (ampersand ref) (car var) (cadr var))))
573                 varstr)
574             "")))
575
576 (defgeneric make-link-end (obj ref fieldname)) 
577 (defmethod make-link-end (obj (ref link-ref) fieldname)
578   (declare (ignore obj fieldname))
579   (format nil "~a" (href-end ref))
580   )
581
582 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
583 (defmethod fmt-obj-data (x (fmt textformat) s
584                          &optional (indent 0) (label nil) (refvars nil))
585   (if (obj-data-indent fmt)
586       (indent-spaces indent s))
587   (if (link-ref fmt)
588       (fmt-obj-data-with-ref x fmt s label refvars)
589     (fmt-obj-data-plain x fmt s label))
590   (aif (obj-data-end-fmtstr fmt)
591        (format s it)))
592
593 (defgeneric fmt-obj-data-plain (obj fmt s label))
594 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
595   (if label
596       (apply #'format s
597              (funcall (obj-data-fmtstr-labels fmt) x)
598              (multiple-value-list 
599               (funcall (funcall (obj-data-value-func fmt) x) x)))
600     (apply #'format s (funcall (obj-data-fmtstr fmt) x)
601            (multiple-value-list
602             (funcall (funcall (obj-data-value-func fmt) x) x)))))
603
604 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
605 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
606   (let ((refstr (make-ref-data-str x fmt label))
607         (refvalues nil)
608         (field-values 
609          (multiple-value-list
610           (funcall (funcall (obj-data-value-func fmt) x) x))))
611     
612     ;; make list of reference link fields for printing to refstr template
613     (dolist (field (ml-class-ref-fields x))
614       (let ((link-start 
615              (make-link-start x (link-ref fmt) (car field) (cadr field)
616                               (nth (position (car field) (ml-class-fields x) :key #'car) field-values)  
617                               (append (caddr field) refvars)))
618             (link-end (make-link-end x (link-ref fmt) (car field))))
619         (push link-start refvalues)
620         (push link-end refvalues)))
621     (setq refvalues (nreverse refvalues))
622     
623     (apply #'format s refstr refvalues)))
624
625 (defgeneric obj-data (obj))
626 (defmethod obj-data (x)
627   "Returns the objects data as a string. Used by common-graphics outline function"
628   (let ((fmt (make-format-instance :text)))
629     (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
630            (multiple-value-list 
631             (funcall (funcall (obj-data-value-func fmt) x) x)))))
632
633 (defgeneric make-ref-data-str (obj fmt &optional label))
634 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
635   "Return fmt string for that contains ~a slots for reference link start and end"
636   (unless (link-ref fmt)
637     (error "fmt does not contain a link-ref"))
638   (let ((refstr 
639          (if label
640              (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
641                     (multiple-value-list
642                       (funcall (funcall (obj-data-value-func fmt) x) x)))
643            (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
644                   (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
645     refstr))
646   
647 ;;; Display method for objects
648
649
650 (defgeneric load-all-subobjects (objs))
651 (defmethod load-all-subobjects (objs)
652   "Load all subobjects if they have not already been loaded."
653   (when objs
654     (let ((objlist (mklist objs)))
655       (dolist (obj objlist)
656         (awhen (ml-class-subobjects-lists obj)  ;; access list of functions
657           (dolist (child-obj it)   ;; for each child function
658             (awhen (funcall (car child-obj) obj)
659               (load-all-subobjects it))))))
660     objs))
661
662 (defgeneric output-ml-class (objs fmt strm &optional label english-only-function indent subobjects refvars))
663 (defmethod output-ml-class (objs (fmt dataformat) (strm stream) 
664                             &optional (label nil) (english-only-function nil)
665                                       (indent 0) (subobjects nil) (refvars nil))
666   "Display a single or list of ml-class instances and their subobjects"
667   (when objs
668     (setq objs (mklist objs))
669     (let ((nobjs (length objs)))
670       (fmt-list-start (car objs) fmt strm indent nobjs)
671       (dolist (obj objs)
672         (unless (and english-only-function (not (funcall english-only-function obj)))
673           (fmt-obj-start obj fmt strm indent)
674           (fmt-obj-data obj fmt strm (1+ indent) label refvars)
675           (if subobjects
676               (awhen (ml-class-subobjects-lists obj)  ;; access list of functions
677                         (dolist (child-obj it)   ;; for each child function
678                           (awhen (funcall (car child-obj) obj) ;; access set of child objects
679                                     (output-ml-class it fmt strm label 
680                                                      english-only-function
681                                                      (1+ indent) subobjects refvars)))))
682           (fmt-obj-end obj fmt strm indent)))
683       (fmt-list-end (car objs) fmt strm indent nobjs))
684     t))
685
686 (defun display-ml-class (objs &key (os *standard-output*) (format :text)
687                                 (label nil) (english-only-function nil) (subobjects nil)
688                                 (file-wrapper t) (refvars nil))
689   "EXPORTED Function: displays a ml-class. Simplies call to output-ml-class"
690   (let ((fmt (make-format-instance format)))
691     (if file-wrapper
692         (fmt-file-start fmt os))
693     (when objs
694       (output-ml-class objs fmt os label english-only-function 0 subobjects refvars))
695     (if file-wrapper
696         (fmt-file-end fmt os)))
697   objs)