| 1 | (in-package :lisp-on-lines) |
| 2 | |
| 3 | |
| 4 | (defvar *display*) |
| 5 | |
| 6 | |
| 7 | (define-layered-function display-using-description (description display object &rest args) |
| 8 | (:documentation |
| 9 | "Displays OBJECT via description using/in/with/on display")) |
| 10 | |
| 11 | |
| 12 | |
| 13 | (defun modify-layer-context (context &key activate deactivate) |
| 14 | (dolist (d deactivate) |
| 15 | (setf context (remove-layer (find-description d) |
| 16 | context))) |
| 17 | (dolist (d activate context) |
| 18 | (setf context (adjoin-layer (find-description (if (consp d) (car d) d)) |
| 19 | context)))) |
| 20 | |
| 21 | (defun funcall-with-attribute-context (attribute thunk) |
| 22 | (funcall-with-layer-context |
| 23 | (modify-layer-context (current-layer-context) |
| 24 | :activate (attribute-active-descriptions attribute) |
| 25 | :deactivate (attribute-inactive-descriptions attribute)) |
| 26 | (lambda () |
| 27 | (with-special-symbol-access |
| 28 | (contextl::funcall-with-special-initargs |
| 29 | (mappend (lambda (desc) |
| 30 | (when (consp desc) |
| 31 | (let ((description (find-description (car desc)))) |
| 32 | (loop |
| 33 | :for (key val) :on (cdr desc) :by #'cddr |
| 34 | :collect (list (find key (description-attributes description) |
| 35 | :key #'attribute-keyword) |
| 36 | :value val))))) |
| 37 | (attribute-active-descriptions attribute)) |
| 38 | (lambda () |
| 39 | (without-special-symbol-access |
| 40 | (funcall thunk)))))))) |
| 41 | |
| 42 | (defmacro with-attribute-context ((attribute) &body body) |
| 43 | `(funcall-with-attribute-context ,attribute (lambda () ,@body))) |
| 44 | |
| 45 | |
| 46 | (defun display (display object &rest args &key deactivate activate &allow-other-keys) |
| 47 | (funcall-with-layer-context |
| 48 | (modify-layer-context (current-layer-context) |
| 49 | :activate activate |
| 50 | :deactivate deactivate) |
| 51 | (lambda () |
| 52 | (apply #'display-using-description (description-of object) display object args)))) |
| 53 | |
| 54 | (define-layered-method display-using-description |
| 55 | :around ((description standard-description-object) display object &rest args) |
| 56 | (declare (ignorable args)) |
| 57 | #+nil (break "Entering DISPLAY for ~A on ~A using ~A" object display description) |
| 58 | (let ((*display* display)) |
| 59 | (apply #'funcall-with-described-object |
| 60 | (lambda () |
| 61 | (call-next-method)) |
| 62 | object description args))) |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | (defun display/d (&rest args) |
| 68 | (apply #'display-using-description args)) |
| 69 | |
| 70 | (define-layered-method display-using-description (description display object &rest args) |
| 71 | (error "No DISPLAY-USING-DESCRIPTION methods are specified for: ~% DESCRIPTION: ~A ~% DISPLAY: ~A ~% OBJECT: ~A ~% ARGS: ~S |
| 72 | |
| 73 | OMGWTF! If you didn't do this, it's a bug!" description display object args)) |
| 74 | |
| 75 | (defmacro define-display (&body body) |
| 76 | (loop with in-descriptionp = (eq (car body) :in-description) |
| 77 | with description = (if in-descriptionp (cadr body) 't) |
| 78 | for tail on (if in-descriptionp (cddr body) body) |
| 79 | until (listp (car tail)) |
| 80 | collect (car tail) into qualifiers |
| 81 | finally |
| 82 | (when (member :in-description qualifiers) |
| 83 | (error "Incorrect occurrence of :in-description in defdisplay. Must occur before qualifiers.")) |
| 84 | (return |
| 85 | (destructuring-bind (description-spec &optional (display-spec (gensym)) (object-spec (gensym))) |
| 86 | (car tail) |
| 87 | `(define-layered-method |
| 88 | display-using-description |
| 89 | :in-layer ,(if (eq t description) |
| 90 | t |
| 91 | (defining-description description)) |
| 92 | ,@qualifiers |
| 93 | (,(if (listp description-spec) |
| 94 | (list (first description-spec) |
| 95 | (if (eq 'description (second description-spec)) |
| 96 | 'description |
| 97 | (contextl::defining-layer (defining-description (second description-spec)))))) |
| 98 | ,display-spec |
| 99 | ,object-spec &rest args) |
| 100 | (declare (ignorable args)) |
| 101 | ,@(cdr tail)))))) |
| 102 | |
| 103 | |
| 104 | |
| 105 | |