Support `:activate ((DESC . SPECIAL-INITARGS))' in attributes
[clinton/lisp-on-lines.git] / src / display.lisp
index 423ee8c..e502b85 100644 (file)
@@ -1,8 +1,8 @@
 (in-package :lisp-on-lines)
 
-(defvar *description*)
+
 (defvar *display*)
-(defvar *object* nil)
+
 
 (define-layered-function display-using-description (description display object &rest args)
   (:documentation
     (setf context (remove-layer (find-description d)
                                context)))
   (dolist (d activate context)
-    (setf context (adjoin-layer (find-description d)
+    (setf context (adjoin-layer (find-description (if (consp d) (car d) d))
                                context))))
-  
-
-
 
+(defun funcall-with-attribute-context (attribute thunk)
+  (funcall-with-layer-context 
+   (modify-layer-context (current-layer-context)
+                        :activate (attribute-active-descriptions attribute)
+                        :deactivate (attribute-inactive-descriptions attribute))
+   (lambda ()
+     (with-special-symbol-access
+       (contextl::funcall-with-special-initargs
+       (mappend (lambda (desc)
+                  (when (consp desc)
+                    (let ((description (find-description (car desc))))
+                      (loop 
+                         :for (key val) :on (cdr desc) :by #'cddr
+                         :collect (list (find key (description-attributes description) 
+                                              :key #'attribute-keyword)
+                                 :value val)))))
+                (attribute-active-descriptions attribute))
+       (lambda ()
+         (without-special-symbol-access
+           (funcall thunk))))))))
+
+(defmacro with-attribute-context ((attribute) &body body)
+  `(funcall-with-attribute-context ,attribute (lambda () ,@body)))
+  
+  
 (defun display (display object &rest args &key deactivate activate &allow-other-keys)
   (funcall-with-layer-context 
    (modify-layer-context (current-layer-context) 
      (apply #'display-using-description (description-of object) display object args))))
 
 (define-layered-method display-using-description 
-  :around (description display object &rest args)
+  :around ((description standard-description-object) display object &rest args)
   (declare (ignorable args))
-  (let ((*description* description)
-       (*display* display)
-       (*object*  object))
-;    (<:as-html " " description "Layer Active?: "  (layer-active-p (defining-description 'maxclaims::link-to-viewer)))
-    (dletf (((described-object description) object))
-      (flet ((do-display ()
-              (contextl::funcall-with-special-initargs  
-               (loop 
-                  :for (key val) :on args :by #'cddr
-                  :collect (list (find key (description-attributes description) 
-                                       :key #'attribute-keyword)
-                                 :value val))
-               (lambda ()
-                 (contextl::funcall-with-special-initargs  
-                  (let ((attribute (ignore-errors (find-attribute description 'active-attributes))))   
-                    (when attribute
-                      (loop for spec in (attribute-value attribute)
-                         if (listp spec)
-                         collect (cons (or 
-                                        (find-attribute description (car spec))
-                                        (error "No attribute matching ~A" (car spec)))
-                                       (cdr spec)))))
-                  (lambda () (call-next-method)))))))
-       (funcall-with-layer-context
-        (modify-layer-context 
-         (if (standard-description-p description)
-             (adjoin-layer description (current-layer-context))
-             (current-layer-context))
-         :activate (description-active-descriptions description)
-         :deactivate (description-inactive-descriptions description))
-        (function do-display))))))
-
+#+nil  (break "Entering DISPLAY for ~A on ~A using ~A" object display description)
+  (let ((*display* display))
+    (apply #'funcall-with-described-object 
+     (lambda ()
+       (call-next-method))
+     object description args)))
 
 
 
@@ -88,7 +84,7 @@ OMGWTF! If you didn't do this, it's a bug!" description display object args))
        (return
          (destructuring-bind (description-spec &optional  (display-spec (gensym)) (object-spec (gensym))) 
              (car tail) 
-               `(define-layered-method
+           `(define-layered-method
                  display-using-description
                   :in-layer ,(if (eq t description) 
                                  t
@@ -98,7 +94,7 @@ OMGWTF! If you didn't do this, it's a bug!" description display object args))
                        (list (first description-spec)
                             (if (eq 'description (second description-spec))
                                     'description
-                                    (defining-description (second description-spec)))))
+                                    (contextl::defining-layer (defining-description (second description-spec))))))
                   ,display-spec
                   ,object-spec &rest args)
                   (declare (ignorable args))