r5152: *** empty log message ***
[hyperobject.git] / views.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          views.lisp
6 ;;;; Purpose:       View methods for Hyperobjects
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: views.lisp,v 1.56 2003/06/17 17:50:45 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
14  
15 (in-package #:hyperobject)
16
17
18 (defclass object-view ()
19   ((object-class-name :initform nil :initarg :object-class-name
20                       :accessor object-class-name
21                  :documentation "Name of class of object to be viewed.")
22    (object-class :initform nil :initarg :object-class
23                  :accessor object-class
24                  :documentation "Class of object to be viewed.")
25    (slots :initform nil :initarg :slots :accessor slots
26           :documentation "List of effective slots for object to be viewed.")
27    (name :initform nil :initarg :name :accessor name
28          :documentation "Name for this view.")
29    (category :initform nil :initarg :category :accessor category
30              :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.")
31    (source-code :initform nil :initarg :source-code :accessor source-code 
32                 :documentation "Source code for generating view.")
33    (country-language :initform :en :initarg :country-language
34                      :documentation "Country's Language for this view.")
35    ;;
36    (file-start-str :type (or string null) :initform nil :initarg :file-start-str
37                    :accessor file-start-str)
38    (file-end-str :type (or string null) :initform nil :initarg :file-end-str
39                  :accessor file-end-str)
40    (list-start-printer :type (or string function null) :initform nil
41                            :initarg :list-start-printer
42                       :accessor list-start-printer)
43    (list-start-indent :initform nil :initarg :list-start-indent
44                       :accessor list-start-indent)
45    (list-end-printer :type (or string function null) :initform nil
46                          :initarg :list-end-printer
47                     :accessor list-end-printer)
48    (list-end-indent :initform nil :initarg :list-end-indent
49                     :accessor list-end-indent)
50    (obj-start-printer :type (or string function null) :initform nil :initarg :obj-start-printer
51                      :accessor obj-start-printer)
52    (obj-start-indent :initform nil :initarg :obj-start-indent
53                      :accessor obj-start-indent)
54    (obj-end-printer :type (or string function null) :initform nil :initarg :obj-end-printer
55                    :accessor obj-end-printer)
56    (obj-end-indent :initform nil :initarg :obj-end-indent
57                    :accessor obj-end-indent)
58    (subobj-start-printer :type (or string function null) :initform nil :initarg :subobj-start-printer
59                      :accessor subobj-start-printer)
60    (subobj-start-indent :initform nil :initarg :subobj-start-indent
61                      :accessor subobj-start-indent)
62    (subobj-end-printer :type (or string function null) :initform nil :initarg :subobj-end-printer
63                    :accessor subobj-end-printer)
64    (subobj-end-indent :initform nil :initarg :subobj-end-indent
65                    :accessor subobj-end-indent)
66    (obj-data-indent :initform nil :initarg :obj-data-indent
67                     :accessor obj-data-indent)
68    (obj-data-printer :type (or function null) :initform nil
69                         :initarg :obj-data-printer
70                         :accessor obj-data-printer)
71    (obj-data-print-code :type (or function null) :initform nil
72                   :initarg :obj-data-print-code
73                   :accessor obj-data-print-code)
74    (obj-data-start-printer :type (or function string null) :initform nil
75                      :initarg :obj-data-start-printer
76                      :accessor obj-data-start-printer)
77    (obj-data-end-printer :type (or string null) :initform nil
78                         :initarg :obj-data-end-printer
79                         :accessor obj-data-end-printer)
80    (indenter :type (or function null) :initform nil
81              :accessor indenter
82              :documentation "Function that performs hierarchical indenting")
83    (link-slots :type list :initform nil
84                :documentation "List of slot names that have hyperlinks"
85                :accessor link-slots)
86    (link-page :type (or string null) :initform nil
87                       :initarg :link-page
88                       :accessor link-page)
89    (link-href-start :type (or string null) :initform nil :initarg :link-href-start
90                     :accessor link-href-start)
91    (link-href-end :type (or string null) :initform nil :initarg :link-href-end
92                   :accessor link-href-end)
93    (link-ampersand :type (or string null) :initform nil :initarg :link-ampersand
94                    :accessor link-ampersand))
95   (:default-initargs :link-page "disp-func1")
96   (:documentation "View class for a hyperobject"))
97
98
99 (defun get-category-view (obj category &optional slots)
100   "Find or make a category view for an object"
101   (let ((obj-class (class-of obj)))
102     (if (null category)
103         (default-view obj-class)
104         (aif (find category (views obj-class) :key #'category)
105              it
106              (let ((view
107                     (make-instance 'object-view
108                                    :object-class-name (class-name obj-class)
109                                    :object-class obj-class
110                                    :category category
111                                    :slots slots)))
112                (push view (views obj-class))
113                view)))))
114                              
115 ;;;; *************************************************************************
116 ;;;;  Metaclass Intialization
117 ;;;; *************************************************************************
118
119 (defun finalize-views (cl)
120   "Finalize all views that are given on a objects initialization"
121   (unless (default-print-slots cl)
122     (setf (default-print-slots cl)
123           (mapcar #'slot-definition-name (class-slots cl))))
124   (let ((views '()))
125     (dolist (view-def (views cl))
126       (push (make-object-view cl view-def) views))
127     (setf (views cl) (nreverse views)))
128   (cond
129     ((default-view cl)
130      (setf (default-view cl) (make-object-view cl (default-view cl))))
131     ((car (views cl))
132      (setf (default-view cl) (make-object-view cl (car (views cl)))))
133     (t
134      (setf (default-view cl) (make-object-view cl :default)))))
135
136 (defun make-object-view (cl view-def)
137   "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
138   (cond
139     ((typep view-def 'object-view)
140      view-def)
141     ((eq view-def :default)
142      (let* ((name (class-name cl))
143             (view (make-instance 'object-view :name "automatic"
144                                  :object-class-name name
145                                  :object-class cl
146                                  :category :compact-text)))
147        view))
148     ((consp view-def)
149      (apply #'make-instance 'object-view view-def))
150     (t
151      (error "Invalid parameter to make-object-view: ~S" view-def))))
152
153 (defmethod initialize-instance :after ((view object-view)
154                                        &rest initargs &key &allow-other-keys)
155   (initialize-view (object-class view) view))
156   
157 (defun initialize-view (obj-cl view)
158   "Calculate all view slots for a hyperobject class"
159   (cond
160     ((category view)
161      (initialize-view-by-category obj-cl view))
162     ((source-code view)
163      (initialize-view-by-source-code obj-cl view))
164     (t
165      (setf (category view) :compact-text)
166      (initialize-view-by-category obj-cl view))))
167
168 (defun initialize-view-by-source-code (obj-cl view)
169   "Initialize a view based upon a source code"
170   (let ((source-code (source-code view)))
171     (error "not implemented")
172     )
173   )
174
175 (defmacro write-simple (v s)
176   `(typecase ,v
177     (string
178      (write-string ,v ,s))
179     #+allegro
180     (fixnum
181      (excl::print-fixnum ,s 10 ,v)) 
182     (symbol
183      (write-string (symbol-name ,v) ,s))
184     (t
185      (write-string (write-to-string ,v) ,s))))
186
187 (defun write-ho-value (obj name type formatter cdata strm)
188   (declare (ignorable type))
189   (let* ((slot-data (slot-value obj name))
190          (fmt-data (if formatter
191                        (funcall formatter slot-data)
192                        slot-data)))
193     (if cdata
194         (write-xml-cdata fmt-data strm)
195         (write-simple fmt-data strm))))
196
197 (defun ppfc-html (title name type formatter cdata print-func)
198   (vector-push-extend '(write-string "<span class=\"" s) print-func)
199   (vector-push-extend `(write-string ,title s) print-func)
200   (vector-push-extend '(write-string "\">" s) print-func)
201   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
202   (vector-push-extend '(write-string "</span>" s) print-func))
203
204 (defun ppfc-xml (tag name type formatter cdata print-func)
205   (vector-push-extend '(write-char #\< s) print-func)
206   (vector-push-extend `(write-string ,tag s) print-func)
207   (vector-push-extend '(write-char #\> s) print-func)
208   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
209   (vector-push-extend '(write-string "</" s) print-func)
210   (vector-push-extend `(write-string ,tag s) print-func)
211   (vector-push-extend '(write-char #\> s) print-func))
212
213 (defun ppfc-html-labels (label name type formatter cdata print-func)
214   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
215   (vector-push-extend `(write-string ,label s) print-func)
216   (vector-push-extend '(write-string "</span> " s) print-func)
217   (ppfc-html label name type formatter cdata print-func))
218
219 (defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
220   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
221   (vector-push-extend `(write-string ,label s) print-func)
222   (vector-push-extend '(write-string "</span> " s) print-func)
223   (ppfc-html tag name type formatter cdata print-func))
224
225 (defun ppfc-xml-labels (label tag name type formatter cdata print-func)
226   (vector-push-extend '(write-string "<label>" s) print-func)
227   (vector-push-extend `(write-string ,label s) print-func)
228   (vector-push-extend '(write-string "</label> " s) print-func)
229   (ppfc-xml tag name type formatter cdata print-func))
230
231 (defun ppfc-html-link (name type formatter cdata nlink print-func)
232   (declare (fixnum nlink))
233   (vector-push-extend '(write-char #\< s) print-func)
234   (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func) 
235   (vector-push-extend '(write-char #\> s) print-func)
236   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
237   (vector-push-extend '(write-string "</" s) print-func)
238   (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func) 
239   (vector-push-extend '(write-char #\> s) print-func))
240
241 (defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
242   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
243   (vector-push-extend `(write-string ,label s) print-func)
244   (vector-push-extend '(write-string "</span> " s) print-func)
245   (ppfc-html-link name type formatter cdata nlink print-func))
246
247 (defun push-print-fun-code (category slot nlink print-func)
248   (let* ((formatter (esd-print-formatter slot))
249          (name (slot-definition-name slot))
250          (user-name (esd-user-name slot))
251          (xml-user-name (escape-xml-string user-name))
252          (xml-tag (escape-xml-string user-name))
253          (type (slot-value slot 'type))
254          (cdata (not (null
255                       (and (in category :xml :xhtml :xml-link :xhtml-link
256                                :xml-labels :ie-xml-labels
257                                :xhtml-link-labels :xml-link-labels :ie-xml-link
258                                :ie-xml-link-labels)
259                            (or formatter
260                                (lisp-type-is-a-string type))))))
261          (hyperlink (esd-hyperlink slot)))
262     
263     (case category
264       (:compact-text
265        (vector-push-extend
266         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
267       (:compact-text-labels
268        (vector-push-extend `(write-string ,user-name s) print-func)
269        (vector-push-extend '(write-char #\space s) print-func)
270        (vector-push-extend
271         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
272       ((or :html :xhtml)
273        (ppfc-html user-name name type formatter cdata print-func))
274       (:xml
275        (ppfc-xml xml-tag name type formatter cdata print-func))
276       (:html-labels
277        (ppfc-html-labels user-name name type formatter cdata print-func))
278       (:xhtml-labels
279        (ppfc-xhtml-labels xml-user-name user-name name type formatter cdata print-func))
280       (:xml-labels
281        (ppfc-xml-labels xml-user-name xml-tag name type formatter cdata print-func))
282       ((or :html-link :xhtml-link)
283        (if hyperlink
284            (ppfc-html-link name type formatter cdata nlink print-func)
285            (ppfc-html user-name name type formatter cdata print-func)))
286       ((or :xml-link :ie-xml-link)
287        (if hyperlink
288            (ppfc-html-link name type formatter cdata nlink print-func)
289            (ppfc-xml xml-tag name type formatter cdata print-func)))
290       (:html-link-labels
291        (if hyperlink
292            (ppfc-html-link-labels user-name name type formatter cdata nlink
293                                   print-func)
294            (ppfc-html-labels user-name name type formatter cdata print-func)))
295       (:xhtml-link-labels
296        (if hyperlink
297            (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
298                                   print-func)
299            (ppfc-xhtml-labels xml-tag user-name name type formatter cdata
300                               print-func)))
301       ((or :xml-link-labels :ie-xml-link-labels)
302        (if hyperlink
303            (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
304                                   print-func)
305            (ppfc-xml-labels xml-tag user-name name type formatter cdata
306                             print-func))))))
307
308
309 (defun view-has-links-p (view)
310   (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
311       :html-link-labels :xhtml-link-labels :xml-link-labels
312       :ie-xml-link-labels))
313
314 (defun initialize-view-by-category (obj-cl view)
315   "Initialize a view based upon a preset category"
316   (unless (in (category view) :compact-text :compact-text-labels
317               :html :html-labels :html-link-labels
318               :xhtml :xhtml-labels :xhtml-link-labels
319               :xhtml-link :html-link
320               :xml :xml-labels :xml-link :ie-xml-link
321               :xml-link-labels :ie-xml-link-labels)
322     (error "Unknown view category ~A" (category view)))
323   
324   (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
325
326   (let ((links '())
327         (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
328
329     (do* ((slots (slots view) (cdr slots))
330           (slot-name (car slots) (car slots))
331           (slot (find-slot-by-name obj-cl slot-name)
332                 (find-slot-by-name obj-cl slot-name)))
333          ((null slots))
334       (unless slot
335         (error "Slot ~A is not found in class ~S" slot-name obj-cl))
336       
337       (push-print-fun-code (category view) slot (length links) print-func)
338       (when (> (length slots) 1)
339         (vector-push-extend '(write-char #\space s) print-func))
340
341       (when (and (view-has-links-p view) (esd-hyperlink slot))
342         (push (slot-definition-name slot) links)))
343
344     (vector-push-extend 'x print-func) ;; return object
345     (setf (obj-data-print-code view) `(lambda (x s links)
346                                        (declare (ignorable s links))
347                                        ,@(map 'list #'identity print-func)))
348     (setf (obj-data-printer view)
349           (compile nil (eval (obj-data-print-code view))))
350     
351     (setf (link-slots view) (nreverse links)))
352
353   (finalize-view-by-category view)
354   view)
355
356 (defun finalize-view-by-category (view)
357   (case (category view)
358     ((or :compact-text :compact-text-labels)
359      (initialize-text-view view))
360     ((or :html :html-labels)
361      (initialize-html-view view))
362     ((or :xhtml :xhtml-labels)
363      (initialize-xhtml-view view))
364     ((or :xml :xml-labels)
365      (initialize-xml-view view))
366     ((or :html-link :html-link-labels)
367      (initialize-html-view view)
368      (setf (link-href-start view) "a href=")
369      (setf (link-href-end view) "a")
370      (setf (link-ampersand view) "&"))
371     ((or :xhtml-link :xhtml-link-labels)
372      (initialize-xhtml-view view)
373      (setf (link-href-start view) "a href=")
374      (setf (link-href-end view) "a")
375      (setf (link-ampersand view) "&amp;"))
376     ((or :xml-link :xml-link-labels)
377      (initialize-xml-view view)
378      (setf (link-href-start view)
379            "xmllink xlink:type=\"simple\" xlink:href=")
380      (setf (link-href-end view) "xmllink")
381      (setf (link-ampersand view) "&amp;"))
382     ((or :ie-xml-link :ie-xml-link-labels)
383      (initialize-xml-view view)
384      (setf (link-href-start view) "html:a href=")
385      (setf (link-href-end view) "html:a")
386      (setf (link-ampersand view) "&amp;"))))
387
388
389 ;;;; *************************************************************************
390 ;;;;  View Data Format Section
391 ;;;; *************************************************************************
392
393 (defun class-name-of (obj)
394   (string-downcase (class-name (class-of obj))))
395
396 (defvar +newline-string+ (format nil "~%"))
397
398 (defun write-user-name-maybe-plural (obj nitems strm)
399   (write-string
400    (if (> nitems 1)
401        (hyperobject-class-user-name-plural obj)
402        (hyperobject-class-user-name obj))
403    strm))
404
405 (defun initialize-text-view (view)
406   (setf (list-start-printer view)
407         (compile nil
408                  (eval '(lambda (obj nitems indent strm)
409                          (write-user-name-maybe-plural obj nitems strm)
410                          (write-char #\: strm)
411                          (write-char #\Newline strm)))))
412   (setf (list-start-indent view) t)
413   (setf (obj-data-indent view) t)
414   (setf (obj-data-end-printer view) +newline-string+)
415   (setf (indenter view) #'indent-spaces))
416
417 (defun html-list-start-func (obj nitems indent strm)
418   (write-string "<div class=\"ho-username\">" strm)
419   (write-user-name-maybe-plural obj nitems strm)
420   (write-string "</div>" strm)
421   (write-char #\newline strm)
422   (write-string "<ul>" strm)
423   (write-char #\newline strm))
424
425 (defun initialize-html-view (view)
426   (initialize-text-view view)
427   (setf (indenter view) #'indent-html-spaces)
428   (setf (file-start-str view) (format nil "<html><body>~%"))
429   (setf (file-end-str view) (format nil "</body><html>~%"))
430   (setf (list-start-indent view) t)
431   (setf (list-start-printer view) #'html-list-start-func)
432   (setf (list-end-printer view) (format nil "</ul>~%"))
433   (setf (list-end-indent view) t)
434   (setf (obj-start-indent view) nil)
435   (setf (obj-start-printer view) "<li>")
436   (setf (obj-end-indent view)  nil)
437   (setf (obj-end-printer view)  (format nil "</li>~%"))
438   (setf (obj-data-end-printer view) nil)
439   (setf (obj-data-indent view) nil))
440
441 (defun xhtml-list-start-func (obj nitems indent strm)
442   (write-string "<div class=\"ho-username\">" strm)
443   (indent-html-spaces indent strm)
444   (write-user-name-maybe-plural obj nitems strm)
445   (write-string "</div>" strm)
446   (write-char #\newline strm))
447
448 (defun initialize-xhtml-view (view)
449   (initialize-text-view view)
450   (setf (indenter view) #'indent-html-spaces)
451   (setf (file-start-str view) (format nil "<html><body>~%"))
452   (setf (file-end-str view) (format nil "</body><html>~%"))
453   (setf (list-start-indent view) nil)
454   (setf (list-start-printer view) #'xhtml-list-start-func)
455   (setf (list-end-printer view) (format nil "~%"))
456   (setf (list-end-indent view) nil)
457   (setf (obj-start-indent view) nil)
458   (setf (obj-start-printer view) nil)
459   (setf (obj-end-printer view) (format nil "</div>~%"))
460   (setf (obj-data-start-printer view) "<div>")
461   (setf (obj-data-end-printer view) nil)
462   (setf (obj-end-indent view)  nil)
463   (setf (obj-data-indent view) t))
464
465 (defun xmlformat-list-end-func (x strm)
466   (write-string "</" strm)
467   (write-string (class-name-of x) strm)
468   (write-string "list" strm)
469   (write-string ">" strm)
470   (write-char #\newline strm))
471
472 (defun xmlformat-list-start-func (x nitems indent strm)
473   (write-char #\< strm)
474   (write-string (class-name-of x) strm)
475   (write-string "list><title>" strm)
476   (write-user-name-maybe-plural x nitems strm)
477   (write-string ":</title>" strm)
478   (write-char #\newline strm))
479
480 (defun initialize-xml-view (view)
481   (initialize-text-view view)
482   (setf (file-start-str view) "") ; (std-xml-header)
483   (setf (list-start-indent view)  t)
484   (setf (list-start-printer view) #'xmlformat-list-start-func)
485   (setf (list-end-indent view) t)
486   (setf (list-end-printer view) #'xmlformat-list-end-func)
487   (setf (obj-start-printer view) (format nil "<~(~a~)>" (object-class-name view)))
488   (setf (obj-start-indent view) t)
489   (setf (subobj-end-printer view) (format nil "</~(~a~)>~%" (object-class-name view)))
490   (setf (subobj-end-indent view) nil)
491   (setf (obj-data-indent view) nil))
492
493
494 ;;; File Start and Ends
495
496 (defun fmt-file-start (view strm)
497   (awhen (file-start-str view)
498          (write-string it strm)))
499
500 (defun fmt-file-end (view strm)
501   (awhen (file-end-str view)
502          (write-string it strm)))
503
504 ;;; List Start and Ends
505
506 (defun fmt-list-start (obj view strm indent num-items)
507   (when (list-start-indent view)
508     (awhen (indenter view)
509            (funcall it indent strm)))
510   (awhen (list-start-printer view)
511          (if (stringp it)
512              (write-string it strm)
513              (funcall it obj num-items indent strm))))
514
515 (defun fmt-list-end (obj view strm indent num-items)
516   (declare (ignore num-items))
517   (when (list-end-indent view)
518     (awhen (indenter view)
519            (funcall it indent strm)))
520   (awhen (list-end-printer view)
521          (if (stringp it)
522              (write-string it strm)
523              (funcall it obj strm))))
524
525 ;;; Object Start and Ends
526
527
528 (defun fmt-obj-start (obj view strm indent)
529   (when (obj-start-indent view)
530     (awhen (indenter view)
531            (funcall it indent strm)))
532   (awhen (obj-start-printer view)
533          (if (stringp it)
534              (write-string it strm)
535              (funcall it obj strm))))
536
537 (defun fmt-obj-end (obj view strm indent)
538   (when (obj-end-indent view)
539     (awhen (indenter view)
540            (funcall it indent strm))) 
541   (awhen (obj-end-printer view)
542          (if (stringp it)
543              (write-string it strm)
544              (funcall it obj strm))))
545
546 (defun fmt-subobj-start (obj view strm indent)
547   (when (subobj-start-indent view)
548     (awhen (indenter view)
549            (funcall it indent strm)))
550   (awhen (subobj-start-printer view)
551          (if (stringp it)
552              (write-string it strm)
553              (funcall it obj strm))))
554
555 (defun fmt-subobj-end (obj view strm indent)
556   (when (subobj-end-indent view)
557     (awhen (indenter view)
558            (funcall it indent strm))) 
559   (awhen (subobj-end-printer view)
560          (if (stringp it)
561              (write-string it strm)
562              (funcall it obj strm))))
563   
564 ;;; Object Data 
565
566
567 (defun make-link-start (view fieldfunc fieldvalue refvars link-printer)
568   (with-output-to-string (s)
569     (write-string (link-href-start view) s)
570     (write-char #\" s)
571     (let ((link-page (link-page view)))
572       (cond
573         ((null link-printer)
574          (write-string (make-url link-page) s)
575          (write-string "?func=" s)
576          (write-simple fieldfunc s)
577          (write-string (link-ampersand view) s)
578          (write-string "key=" s)
579          (write-simple fieldvalue s)
580          (dolist (var refvars)
581            (write-string (link-ampersand view) s)
582            (write-simple (car var) s)
583            (write-char #\= s)
584            (write-simple (cdr var) s)))
585         (link-printer
586          (funcall link-printer link-page fieldfunc fieldvalue refvars s))))
587     (write-char #\" s)))
588   
589 (defun make-link-end (obj view fieldname)
590   (declare (ignore obj fieldname))
591   (link-href-end view))
592
593 (defun fmt-obj-data (obj view strm indent refvars link-printer)
594   (awhen (obj-data-start-printer view)
595          (if (stringp it)
596              (write-string it strm)
597              (funcall it obj strm)))
598   (when (obj-data-indent view)
599     (awhen (indenter view)
600            (funcall it indent strm)))
601   (if (link-slots view)
602       (fmt-obj-data-with-link obj view strm refvars link-printer)
603       (fmt-obj-data-plain obj view strm))
604   (awhen (obj-data-end-printer view)
605          (if (stringp it)
606              (write-string it strm)
607              (funcall it obj strm))))
608
609 (defun fmt-obj-data-plain (obj view strm)
610   (awhen (obj-data-printer view)
611          (funcall it obj strm nil)))
612
613 (defun fmt-obj-data-with-link (obj view strm refvars link-printer)
614   (let ((refvalues '()))
615     (declare (dynamic-extent refvalues))
616     ;; make list of hyperlink link fields for printing to refstr template
617     (dolist (name (link-slots view))
618       (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
619              (push (make-link-start view (lookup it) (slot-value obj name)
620                                     (append (link-parameters it) refvars)
621                                     link-printer)
622                    refvalues)
623              (push (make-link-end obj view name) refvalues)))
624     (funcall (obj-data-printer view) obj strm (nreverse refvalues))))
625
626 (defun obj-data (obj view)
627   "Returns the objects data as a string. Used by common-graphics outline function"
628   (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
629
630 ;;; Display method for objects
631
632
633 (defun load-all-subobjects (objs)
634   "Load all subobjects if they have not already been loaded."
635   (dolist (obj (mklist objs))
636     (dolist (subobj (hyperobject-class-subobjects obj))
637       (awhen (slot-value obj (name-slot subobj))
638              (load-all-subobjects it))))
639   objs)
640
641 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
642                          subobjects refvars link-printer)
643   "Display a single or list of hyperobject-class instances and their subobjects"
644   (let-when (objlist (mklist objs))
645     (let ((nobjs (length objlist))
646           (*print-pretty* nil)
647           (*print-circle* nil)
648           (*print-escape* nil)
649           (*print-readably* nil)
650           (*print-length* nil)
651           (*print-level* nil))
652       (fmt-list-start (car objlist) view strm indent nobjs)
653       (dolist (obj objlist)
654         (unless (and filter (not (funcall filter obj)))
655           (fmt-obj-start obj view strm indent)
656           (fmt-obj-data obj view strm (1+ indent) refvars link-printer)
657           (fmt-obj-end obj view strm indent)
658           (fmt-subobj-start obj view strm indent)
659           (when (and subobjects (hyperobject-class-subobjects obj))
660             (dolist (subobj (hyperobject-class-subobjects obj))
661               (aif (slot-value obj (name-slot subobj))
662                    (view-hyperobject it
663                                      (get-category-view (car (mklist it))
664                                                         category)
665                                      category strm (1+ indent) filter
666                                      subobjects refvars link-printer))))
667           (fmt-subobj-end obj view strm indent)))
668       (fmt-list-end (car objlist) view strm indent nobjs)))
669   objs)
670
671
672 (defun view (objs &key (stream *standard-output*) category view
673              filter subobjects refvars file-wrapper link-printer)
674   "EXPORTED Function: prints hyperobject-class objects. Calls view-hyperobject"
675   (let-when (objlist (mklist objs))
676     (when category
677       (setq view (get-category-view (car objlist) category)))
678     (unless view
679       (setq view (default-view (class-of (car objlist)))))
680     (when file-wrapper
681       (fmt-file-start view stream))
682     (view-hyperobject objlist view category stream 0 filter subobjects refvars
683                       link-printer)
684     (when file-wrapper
685       (fmt-file-end view stream)))
686   objs)
687
688
689 ;;; Misc formatting
690
691 (defun fmt-comma-integer (i)
692   (format nil "~:d" i))