| 1 | (in-package :lisp-on-lines) |
| 2 | |
| 3 | (defvar *description*) |
| 4 | (defvar *display*) |
| 5 | (defvar *object*) |
| 6 | |
| 7 | (deflayer display-layer) |
| 8 | |
| 9 | (define-layered-function display-using-description (description display object &rest args) |
| 10 | (:documentation |
| 11 | "Displays OBJECT via description using/in/with/on display")) |
| 12 | |
| 13 | (defun display (display object &rest args) |
| 14 | (display-using-description (description-of object) display object args)) |
| 15 | |
| 16 | (define-layered-method display-using-description |
| 17 | :around (description display object &rest args) |
| 18 | (declare (ignorable args)) |
| 19 | (let ((*description* description) |
| 20 | (*display* display) |
| 21 | (*object* object)) |
| 22 | |
| 23 | (call-next-method))) |
| 24 | |
| 25 | |
| 26 | |
| 27 | (define-layered-method display-using-description (description display object &rest args) |
| 28 | (error "No DISPLAY-USING-DESCRIPTION methods are specified for: ~% DESCRIPTION: ~A ~% DISPLAY: ~A ~% OBJECT: ~A ~% ARGS: ~S |
| 29 | |
| 30 | OMGWTF! If you didn't do this, it's a bug!" description display object args)) |
| 31 | |
| 32 | |
| 33 | |
| 34 | (defmacro define-display (&body body) |
| 35 | (loop with in-descriptionp = (eq (car body) :in-description) |
| 36 | with description = (if in-descriptionp (cadr body) 't) |
| 37 | for tail on (if in-descriptionp (cddr body) body) |
| 38 | until (listp (car tail)) |
| 39 | collect (car tail) into qualifiers |
| 40 | finally |
| 41 | (when (member :in-description qualifiers) |
| 42 | (error "Incorrect occurrence of :in-description in defdisplay. Must occur before qualifiers.")) |
| 43 | (return |
| 44 | (destructuring-bind (description-spec &optional (display-spec (gensym)) (object-spec (gensym))) |
| 45 | (car tail) |
| 46 | `(define-layered-method |
| 47 | display-using-description |
| 48 | :in-layer ,(if (eq t description) |
| 49 | t |
| 50 | (defining-description description)) |
| 51 | ,@qualifiers |
| 52 | (,(if (listp description-spec) |
| 53 | (list (first description-spec) |
| 54 | (if (eq 'description (second description-spec)) |
| 55 | 'description |
| 56 | (defining-description (second description-spec))))) |
| 57 | ,display-spec |
| 58 | ,object-spec &rest args) |
| 59 | (declare (ignorable args)) |
| 60 | ,@(cdr tail)))))) |
| 61 | |
| 62 | |
| 63 | |
| 64 | |