r3022: *** 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.11 2002/10/14 19:26:36 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           (package (symbol-package (class-name cl)))
158           (ref-fields (slot-value cl 'ref-fields)))
159       (declare (ignore classname))
160       (dolist (f (slot-value cl 'fields))
161         (let ((name (car f))
162               (namestr (symbol-name (car f)))
163               (namestr-lower (string-downcase (symbol-name (car f))))
164               (type (cadr f))
165               (formatter (caddr f))
166               (value-fmt "~a")
167               (plain-value-func nil)
168               html-str xml-str html-label-str xml-label-str)
169           
170           (when (or (eql type :integer) (eql type :fixnum))
171             (setq value-fmt "~d"))
172           
173           (when (eql type :commainteger)
174             (setq value-fmt "~:d"))
175           
176           (when (eql type :boolean)
177             (setq value-fmt "~a"))
178             
179           (if first-field
180               (setq first-field nil)
181             (progn
182               (string-append fmtstr-text " ")
183               (string-append fmtstr-html " ")
184               (string-append fmtstr-xml " ")
185               (string-append fmtstr-text-labels " ")
186               (string-append fmtstr-html-labels " ")
187               (string-append fmtstr-xml-labels " ")
188               (string-append fmtstr-html-ref " ")
189               (string-append fmtstr-xml-ref " ")
190               (string-append fmtstr-html-ref-labels " ")
191               (string-append fmtstr-xml-ref-labels " ")))
192           
193           (setq html-str value-fmt)
194           (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
195           (setq html-label-str (concatenate 'string "<i>" namestr-lower "</i> " value-fmt))
196           (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
197           
198           (string-append fmtstr-text value-fmt)
199           (string-append fmtstr-html html-str)
200           (string-append fmtstr-xml xml-str)
201           (string-append fmtstr-text-labels namestr-lower " " value-fmt)
202           (string-append fmtstr-html-labels html-label-str)
203           (string-append fmtstr-xml-labels xml-label-str)
204           
205           (if (find name ref-fields :key #'car)
206             (progn
207               (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
208               (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
209               (string-append fmtstr-html-ref-labels "<i>" namestr-lower "</i> <~~a>" value-fmt "</~~a>")
210               (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
211             (progn
212               (string-append fmtstr-html-ref html-str)
213               (string-append fmtstr-xml-ref xml-str)
214               (string-append fmtstr-html-ref-labels html-label-str)
215               (string-append fmtstr-xml-ref-labels xml-label-str)))
216           
217           (if formatter
218               (setq plain-value-func 
219                 (list `(,formatter (,(intern namestr package) x))))
220             (setq plain-value-func 
221               (list `(,(intern namestr package) x))))
222           (setq value-func (append value-func plain-value-func))
223           
224           (if (eql type :cdata)
225               (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
226             (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
227           ))
228
229       (if value-func
230           (setq value-func `(lambda (x) (values ,@value-func)))
231         (setq value-func `(lambda () (values))))
232       (setq value-func (compile nil (eval value-func)))
233       
234       (if xmlvalue-func
235           (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
236         (setq xmlvalue-func `(lambda () (values))))
237       (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
238
239       (setf (slot-value cl 'fmtstr-text) fmtstr-text)
240       (setf (slot-value cl 'fmtstr-html) fmtstr-html)
241       (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
242       (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
243       (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
244       (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
245       (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
246       (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
247       (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
248       (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
249       (setf (slot-value cl 'value-func) value-func)
250       (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
251     (values))
252
253
254 (defun ml-class-fmtstr-text (obj)
255   (slot-value (ml-class-of obj) 'fmtstr-text))
256
257 (defun ml-class-fmtstr-html (obj)
258   (slot-value (ml-class-of obj) 'fmtstr-html))
259
260 (defun ml-class-fmtstr-xml (obj)
261   (slot-value (ml-class-of obj) 'fmtstr-xml))
262
263 (defun ml-class-fmtstr-text-labels (obj)
264   (slot-value (ml-class-of obj) 'fmtstr-text-labels))
265
266 (defun ml-class-fmtstr-html-labels (obj)
267   (slot-value (ml-class-of obj) 'fmtstr-html-labels))
268
269 (defun ml-class-fmtstr-xml-labels (obj)
270   (slot-value (ml-class-of obj) 'fmtstr-xml-labels))
271
272 (defun ml-class-value-func (obj)
273   (slot-value (ml-class-of obj) 'value-func))
274
275 (defun ml-class-xmlvalue-func (obj)
276   (slot-value (ml-class-of obj) 'xmlvalue-func))
277
278 (eval-when (:compile-toplevel :load-toplevel :execute)
279 (defun ml-class-title (obj)
280   (awhen (slot-value (ml-class-of obj) 'title)
281             (if (consp it)
282                 (car it)
283               it))))
284
285 (defun ml-class-subobjects-lists (obj)
286   (slot-value (ml-class-of obj) 'subobjects-lists))
287
288 (defun ml-class-ref-fields (obj)
289   (slot-value (ml-class-of obj) 'ref-fields))
290
291 (defun ml-class-fields (obj)
292   (slot-value (ml-class-of obj) 'fields))
293
294 (defun ml-class-fmtstr-html-ref (obj)
295   (slot-value (ml-class-of obj) 'fmtstr-html-ref))
296
297 (defun ml-class-fmtstr-xml-ref (obj)
298   (slot-value (ml-class-of obj) 'fmtstr-xml-ref))
299
300 (defun ml-class-fmtstr-html-ref-labels (obj)
301   (slot-value (ml-class-of obj) 'fmtstr-html-ref-labels))
302
303 (defun ml-class-fmtstr-xml-ref-labels (obj)
304   (slot-value (ml-class-of obj) 'fmtstr-xml-ref-labels))
305
306 ;;; Class name functions
307
308 (defgeneric ml-class-stdname (x))
309 (defmethod ml-class-stdname ((name string))
310   (string-downcase (subseq name 1)))
311   
312 (defmethod ml-class-stdname ((cl standard-object))
313   (string-downcase (subseq (class-name (ml-class-of cl)) 1)))
314   
315 ;;;; Generic Print functions
316
317 (defparameter *default-textformat* nil)
318 (defparameter *default-htmlformat* nil)
319 (defparameter *default-htmlrefformat* nil)
320 (defparameter *default-xmlformat* nil)
321 (defparameter *default-xmlrefformat* nil)
322 (defparameter *default-nullformat* nil)
323 (defparameter *default-init-format?* nil)
324
325 (defun make-format-instance (fmt)
326   (unless *default-init-format?*
327     (setq *default-textformat* (make-instance 'textformat))
328     (setq *default-htmlformat* (make-instance 'htmlformat))
329     (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
330     (setq *default-xmlformat* (make-instance 'xmlformat))
331     (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
332     (setq *default-nullformat* (make-instance 'nullformat))
333     (setq *default-init-format?* t))
334   
335   (case fmt
336       (:text *default-textformat*)
337       (:html *default-htmlformat*)
338       (:htmlref *default-htmlrefformat*)
339       (:xml  *default-xmlformat*)
340       (:xmlref *default-xmlrefformat*)
341       (:null *default-nullformat*)
342       (otherwise *default-textformat*)))
343     
344 ;;;; Output format classes for print ml-classes
345
346 (defclass dataformat ()
347   ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
348    (file-end-str :type string :initarg :file-end-str :reader file-end-str)
349    (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
350    (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
351    (list-start-indent :initarg :list-start-indent :reader list-start-indent)
352    (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
353    (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
354    (list-end-indent :initarg :list-end-indent :reader list-end-indent)
355    (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
356    (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
357    (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
358    (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
359    (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
360    (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
361    (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
362    (obj-data-fmtstr :initarg :obj-data-fmtstr :reader  obj-data-fmtstr)
363    (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader  obj-data-fmtstr-labels)
364    (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
365    (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
366    (link-ref :initarg :link-ref :reader link-ref))
367   (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
368                      :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
369                      :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
370                      :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
371                      :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
372                      :obj-data-value-func nil :link-ref nil)
373   (:documentation "Parent for all dataformat objects"))
374
375 (defclass binaryformat (dataformat)
376   ())
377
378 (defclass nullformat (dataformat)
379   ())
380
381 (defun text-list-start-value-func (obj nitems)
382   (values (ml-class-title obj) nitems))
383
384 (defclass textformat (dataformat) 
385   ()    
386   (:default-initargs :list-start-fmtstr "~a~P:~%"
387     :list-start-value-func #'text-list-start-value-func
388     :list-start-indent t
389     :obj-data-indent t
390     :obj-data-fmtstr #'ml-class-fmtstr-text
391     :obj-data-fmtstr-labels #'ml-class-fmtstr-text-labels
392     :obj-data-end-fmtstr "~%"
393     :obj-data-value-func #'ml-class-value-func))
394
395 (defclass htmlformat (textformat) 
396   ()
397   (:default-initargs :file-start-str "<html><body>~%"
398     :file-end-str "</body><html>~%"
399     :list-start-indent t
400     :list-start-fmtstr "<p><b>~a~P:</b></p><ul>~%"
401     :list-start-value-func #'text-list-start-value-func
402     :list-end-fmtstr "</ul>~%"
403     :list-end-indent t
404     :list-end-value-func #'identity
405     :obj-start-indent t
406     :obj-start-fmtstr "<li>"
407     :obj-start-value-func #'identity
408     :obj-end-indent  t
409     :obj-end-fmtstr  "</li>~%"
410     :obj-end-value-func #'identity
411     :obj-data-indent t
412     :obj-data-fmtstr #'ml-class-fmtstr-html-labels
413     :obj-data-fmtstr-labels #'ml-class-fmtstr-html-labels
414     :obj-data-value-func #'ml-class-value-func))
415
416
417 (defun class-name-of (obj)
418   (string-downcase (class-name (ml-class-of obj))))
419
420 (defun xmlformat-list-end-value-func (x)
421   (format nil "~alist" (string-downcase (class-name (ml-class-of x)))))
422
423 (defun xmlformat-list-start-value-func (x nitems) 
424   (values (format nil "~alist" (string-downcase (class-name (ml-class-of x)))) (ml-class-title x) nitems))
425
426 (defclass xmlformat (textformat) 
427   ()
428   (:default-initargs :file-start-str "" ; (std-xml-header)
429     :list-start-indent  t
430     :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
431     :list-start-value-func #'xmlformat-list-start-value-func
432     :list-end-indent  t
433     :list-end-fmtstr "</~a>~%"
434     :list-end-value-func #'xmlformat-list-end-value-func
435     :obj-start-fmtstr "<~a>"
436     :obj-start-value-func #'class-name-of
437     :obj-start-indent t
438     :obj-end-fmtstr "</~a>~%"
439     :obj-end-value-func #'class-name-of
440     :obj-end-indent nil
441     :obj-data-indent nil
442     :obj-data-fmtstr #'ml-class-fmtstr-xml
443     :obj-data-fmtstr-labels #'ml-class-fmtstr-xml-labels
444     :obj-data-value-func #'ml-class-xmlvalue-func))
445
446 (defclass link-ref ()
447   ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
448    (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
449    (page-name :type string :initarg :page-name :accessor page-name)
450    (href-head :type string :initarg :href-head :accessor href-head)
451    (href-end :type string :initarg :href-end :accessor href-end)
452    (ampersand :type string :initarg :ampersand :accessor ampersand))
453   (:default-initargs :fmtstr nil 
454     :fmtstr-labels nil 
455     :page-name "disp-func1" 
456     :href-head nil :href-end nil :ampersand nil)
457   (:documentation "Formatting for a linked reference"))
458
459 (defclass html-link-ref (link-ref)
460   ()
461   (:default-initargs :fmtstr #'ml-class-fmtstr-html-ref  
462     :fmtstr-labels #'ml-class-fmtstr-html-ref-labels
463     :href-head "a href=" 
464     :href-end "a" 
465     :ampersand "&"))
466
467 (defclass xml-link-ref (link-ref)
468   ()
469   (:default-initargs :fmtstr #'ml-class-fmtstr-xml-ref 
470     :fmtstr-labels #'ml-class-fmtstr-xml-ref-labels
471     :href-head "xmllink xlink:type=\"simple\" xlink:href=" 
472     :href-end "xmllink" 
473     :ampersand "&amp;"))
474
475
476 (defclass htmlrefformat (htmlformat)
477   ()
478   (:default-initargs :link-ref (make-instance 'html-link-ref)))
479
480 (defclass xmlrefformat (xmlformat)
481   ()
482   (:default-initargs :link-ref (make-instance 'xml-link-ref)))
483
484
485 ;;; File Start and Ends
486
487 (defgeneric fmt-file-start (fmt s))
488 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
489
490 (defmethod fmt-file-start ((fmt textformat) (s stream))
491   (aif (file-start-str fmt)
492       (format s it)))
493
494 (defgeneric fmt-file-end (fmt s))
495 (defmethod fmt-file-end ((fmt textformat) (s stream))
496   (aif (file-end-str fmt)
497           (format s it)))
498
499 ;;; List Start and Ends
500
501 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
502 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
503   (if (list-start-indent fmt)
504       (indent-spaces indent s))
505   (aif (list-start-fmtstr fmt)
506           (apply #'format s it
507                  (multiple-value-list
508                   (funcall (list-start-value-func fmt) x num-items)))))
509
510 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
511 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
512   (declare (ignore num-items))
513   (if (list-end-indent fmt)
514       (indent-spaces indent s))
515   (aif (list-end-fmtstr fmt)
516           (apply #'format s it
517                  (multiple-value-list
518                   (funcall (list-end-value-func fmt) x)))))
519
520 ;;; Object Start and Ends
521
522 (defgeneric fmt-obj-start (obj fmt s &optional indent))
523 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
524   (if (obj-start-indent fmt)
525       (indent-spaces indent s))
526   (aif (obj-start-fmtstr fmt)
527           (apply #'format s it
528                  (multiple-value-list
529                   (funcall (obj-start-value-func fmt) x)))))
530
531 (defgeneric fmt-obj-end (obj fmt s &optional indent))
532 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
533   (if (obj-end-indent fmt)
534       (indent-spaces indent s))
535   (aif (obj-end-fmtstr fmt)
536           (apply #'format s it
537                  (multiple-value-list
538                   (funcall (obj-end-value-func fmt) x)))))
539   
540 ;;; Object Data 
541
542 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
543 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
544   (declare (ignore obj fieldname))
545   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
546           (href-head ref) (make-url (page-name ref)) fieldfunc 
547           (ampersand ref) fieldvalue
548           (if refvars
549               (let ((varstr ""))
550                 (dolist (var refvars)
551                   (string-append varstr (format nil "~a~a=~a" 
552                                                 (ampersand ref) (car var) (cadr var))))
553                 varstr)
554             "")))
555
556 (defgeneric make-link-end (obj ref fieldname)) 
557 (defmethod make-link-end (obj (ref link-ref) fieldname)
558   (declare (ignore obj fieldname))
559   (format nil "~a" (href-end ref))
560   )
561
562 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
563 (defmethod fmt-obj-data (x (fmt textformat) s
564                          &optional (indent 0) (label nil) (refvars nil))
565   (if (obj-data-indent fmt)
566       (indent-spaces indent s))
567   (if (link-ref fmt)
568       (fmt-obj-data-with-ref x fmt s label refvars)
569     (fmt-obj-data-plain x fmt s label))
570   (aif (obj-data-end-fmtstr fmt)
571        (format s it)))
572
573 (defgeneric fmt-obj-data-plain (obj fmt s label))
574 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
575   (if label
576       (apply #'format s
577              (funcall (obj-data-fmtstr-labels fmt) x)
578              (multiple-value-list 
579               (funcall (funcall (obj-data-value-func fmt) x) x)))
580     (apply #'format s (funcall (obj-data-fmtstr fmt) x)
581            (multiple-value-list
582             (funcall (funcall (obj-data-value-func fmt) x) x)))))
583
584 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
585 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
586   (let ((refstr (make-ref-data-str x fmt label))
587         (refvalues nil)
588         (field-values 
589          (multiple-value-list
590           (funcall (funcall (obj-data-value-func fmt) x) x))))
591     
592     ;; make list of reference link fields for printing to refstr template
593     (dolist (field (ml-class-ref-fields x))
594       (let ((link-start 
595              (make-link-start x (link-ref fmt) (car field) (cadr field)
596                               (nth (position (car field) (ml-class-fields x) :key #'car) field-values)  
597                               (append (caddr field) refvars)))
598             (link-end (make-link-end x (link-ref fmt) (car field))))
599         (push link-start refvalues)
600         (push link-end refvalues)))
601     (setq refvalues (nreverse refvalues))
602     
603     (apply #'format s refstr refvalues)))
604
605 (defgeneric obj-data (obj))
606 (defmethod obj-data (x)
607   "Returns the objects data as a string. Used by common-graphics outline function"
608   (let ((fmt (make-format-instance :text)))
609     (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
610            (multiple-value-list 
611             (funcall (funcall (obj-data-value-func fmt) x) x)))))
612
613 (defgeneric make-ref-data-str (obj fmt &optional label))
614 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
615   "Return fmt string for that contains ~a slots for reference link start and end"
616   (unless (link-ref fmt)
617     (error "fmt does not contain a link-ref"))
618   (let ((refstr 
619          (if label
620              (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
621                     (multiple-value-list
622                       (funcall (funcall (obj-data-value-func fmt) x) x)))
623            (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
624                   (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
625     refstr))
626   
627 ;;; Display method for objects
628
629
630 (defgeneric load-all-subobjects (objs))
631 (defmethod load-all-subobjects (objs)
632   "Load all subobjects if they have not already been loaded."
633   (when objs
634     (let ((objlist (mklist objs)))
635       (dolist (obj objlist)
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)
639               (load-all-subobjects it))))))
640     objs))
641
642 (defgeneric output-ml-class (objs fmt strm &optional label english-only-function indent subobjects refvars))
643 (defmethod output-ml-class (objs (fmt dataformat) (strm stream) 
644                             &optional (label nil) (english-only-function nil)
645                                       (indent 0) (subobjects nil) (refvars nil))
646   "Display a single or list of ml-class instances and their subobjects"
647   (when objs
648     (setq objs (mklist objs))
649     (let ((nobjs (length objs)))
650       (fmt-list-start (car objs) fmt strm indent nobjs)
651       (dolist (obj objs)
652         (unless (and english-only-function (not (funcall english-only-function obj)))
653           (fmt-obj-start obj fmt strm indent)
654           (fmt-obj-data obj fmt strm (1+ indent) label refvars)
655           (if subobjects
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) ;; access set of child objects
659                                     (output-ml-class it fmt strm label 
660                                                      english-only-function
661                                                      (1+ indent) subobjects refvars)))))
662           (fmt-obj-end obj fmt strm indent)))
663       (fmt-list-end (car objs) fmt strm indent nobjs))
664     t))
665
666 (defun display-ml-class (objs &key (os *standard-output*) (format :text)
667                                 (label nil) (english-only-function nil) (subobjects nil)
668                                 (file-wrapper t) (refvars nil))
669   "EXPORTED Function: displays a ml-class. Simplies call to output-ml-class"
670   (let ((fmt (make-format-instance format)))
671     (if file-wrapper
672         (fmt-file-start fmt os))
673     (when objs
674       (output-ml-class objs fmt os label english-only-function 0 subobjects refvars))
675     (if file-wrapper
676         (fmt-file-end fmt os)))
677   objs)