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