-
Notifications
You must be signed in to change notification settings - Fork 6
/
presentations.lisp
2097 lines (1909 loc) · 73.3 KB
/
presentations.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
;;; (c) copyright 1998,1999,2000 by Michael McDonald ([email protected])
;;; (c) copyright 2001,2002 by Tim Moore ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;; Implementation of the presentation type system, presentation generic
;;; functions and methods, presentation translators, finding an applicable
;;;presentation.
(in-package :clim-internals)
;;; PRESENTATION class
(defvar *allow-sensitive-inferiors* t)
(defclass presentation-mixin (presentation)
((object :accessor presentation-object :initarg :object)
(type :accessor presentation-type :initarg :type)
(view :accessor presentation-view :initarg :view)
(single-box :accessor presentation-single-box :initarg :single-box
:initform nil)
(modifier :reader presentation-modifier :initarg :modifier :initform nil)
(is-sensitive :reader is-sensitive :initarg :is-sensitive
:initform *allow-sensitive-inferiors*)))
(defclass standard-presentation
(presentation-mixin standard-sequence-output-record)
())
(defvar *print-presentation-verbose* nil)
(defmethod print-object ((self standard-presentation) stream)
(print-unreadable-object (self stream :type t :identity t)
(with-bounding-rectangle* (x1 y1 x2 y2)
self
(format stream "~D:~D,~D:~D ~S" x1 x2 y1 y2 (presentation-type self))
(when *print-presentation-verbose*
(format stream " ~S" (presentation-object self))))))
(defmacro with-output-as-presentation ((stream object type
&rest key-args
&key modifier single-box
(allow-sensitive-inferiors t)
parent
(record-type
''standard-presentation)
&allow-other-keys)
&body body)
(declare (ignore parent single-box modifier))
(setq stream (stream-designator-symbol stream '*standard-output*))
(multiple-value-bind (decls with-body)
(get-body-declarations body)
(with-gensyms (record-arg continuation)
(with-keywords-removed (key-args (:record-type
:allow-sensitive-inferiors))
`(flet ((,continuation ()
,@decls
,@with-body))
(declare (dynamic-extent #',continuation))
(if (and (output-recording-stream-p ,stream)
*allow-sensitive-inferiors*)
(with-new-output-record
(,stream ,record-type ,record-arg
:object ,object
:type (expand-presentation-type-abbreviation
,type)
,@key-args)
(let ((*allow-sensitive-inferiors*
,allow-sensitive-inferiors))
(,continuation)))
(,continuation)))))))
(defgeneric ptype-specializer (type)
(:documentation "The specializer to use for this type in a presentation
method lambda list"))
;;; Metaclass for presentation types. For presentation types not associated
;;; with CLOS classes, objects with this metaclass are used as a proxy for the
;;; type during presentation method dispatch. We don't prevent these
;;; presentation proxies from being subclasses of standard-object,
;;; but in presentation method dispatch we remove methods on standard
;;; object for presentation types that are not CLOS classes. The
;;; presentation type metaclass for T is the builtin object T instead
;;; of a presentation-type-class; in this way we can avoid weirdness
;;; with T being a subtype of standard-object!
;;;
(defclass presentation-type ()
((type-name :accessor type-name :initarg :type-name
:documentation "The name assigned to the presentation
type, as opposed to the name constructed for the class")
(ptype-specializer :accessor ptype-specializer :initarg :ptype-specializer)
(parameters :accessor parameters :initarg :parameters :initform nil)
(parameters-lambda-list :accessor parameters-lambda-list
:initarg :parameters-lambda-list
:initform nil
:documentation "The parameters lambda list, altered for use in destructuring bind")
(options :accessor options :initarg :options :initform nil)
(options-lambda-list :accessor options-lambda-list
:initarg :options-lambda-list
:initform nil
:documentation "The options lambda list,
altered for use in destructuring bind")
(inherit-from :accessor inherit-from
:initarg :inherit-from
:documentation "Inherit-from form with dummys substituted")
(inherit-from-function :accessor inherit-from-function
:initarg :inherit-from-function
:documentation "Function that returns the inherit-from form")
(description :accessor description :initarg :description)
(history :accessor history :initarg :history :initform nil
:documentation "Who knows?")
(parameters-are-types :accessor parameters-are-types
:initarg :parameters-are-types
:initform nil)
(expansion-function :accessor expansion-function
:initarg :expansion-function
:documentation "A function which expands the typespec
fully, including defaulting parameters and options.")))
(defmethod initialize-instance :after ((obj presentation-type) &key)
(unless (slot-boundp obj 'ptype-specializer)
(setf (slot-value obj 'ptype-specializer)
(make-presentation-type-name (slot-value obj 'type-name)))))
(defclass presentation-type-class (presentation-type standard-class)
())
(defmethod clim-mop:validate-superclass ((class presentation-type-class)
(super standard-class))
t)
(defclass clos-presentation-type (presentation-type)
((clos-class :accessor clos-class :initarg :clos-class
:documentation "Holds the class object of the CLOS class of this presentation type")))
(defmethod initialize-instance :after ((obj clos-presentation-type)
&key (ptype-specializer
nil
ptype-specializer-p))
(declare (ignore ptype-specializer))
(unless ptype-specializer-p
(setf (slot-value obj 'ptype-specializer)
(slot-value obj 'type-name))))
(defmethod history ((ptype standard-class))
"Default for CLOS types that are not defined explicitly as
presentation types."
t)
(defvar *builtin-t-class* (find-class t))
;;;Methods for the T presentation type
(defmethod type-name ((class (eql *builtin-t-class*)))
t)
(defmethod ptype-specializer ((class (eql *builtin-t-class*)))
t)
(defmethod parameters ((class (eql *builtin-t-class*)))
nil)
(defmethod parameters-lambda-list ((class (eql *builtin-t-class*)))
nil)
(defmethod options ((class (eql *builtin-t-class*)))
nil)
(defmethod options-lambda-list ((class (eql *builtin-t-class*)))
nil)
(defmethod inherit-from ((class (eql *builtin-t-class*)))
nil)
(defmethod inherit-from-function ((class (eql *builtin-t-class*)))
#'(lambda (type)
(declare (ignore type))
nil))
(defmethod description ((class (eql *builtin-t-class*)))
"The supertype of all presentation types.")
(defmethod history ((class (eql *builtin-t-class*)))
t)
(defmethod parameters-are-types ((class (eql *builtin-t-class*)))
nil)
(defmethod expansion-function ((class (eql *builtin-t-class*)))
#'(lambda (type)
(declare (ignore type))
t))
(defun make-presentation-type-name (name)
(intern (format nil "(presentation-type ~A::~A)"
(package-name (symbol-package name))
(symbol-name name))
:clim-internals))
(defun transform-parameters-lambda-list (ll)
"Change the destructuring lambda list so that any optional or key variable
that has no default is supplied with '*"
(when (atom ll)
(return-from transform-parameters-lambda-list ll))
(let ((state 'required))
(loop for lambda-var in ll
collect
(cond ((member lambda-var lambda-list-keywords :test #'eq)
(setq state lambda-var)
lambda-var)
((eq state '&optional)
(if (atom lambda-var)
`(,lambda-var '*)
(cons (transform-parameters-lambda-list (car lambda-var))
(or (cdr lambda-var) '('*)))))
((eq state '&key)
(cond ((atom lambda-var)
`(,lambda-var '*))
((atom (car lambda-var))
(cons (car lambda-var)
(or (cdr lambda-var) '('*))))
(t (destructuring-bind
((var pattern)
&optional (default nil default-p)
&rest supplied-tail)
lambda-var
`((,var ,(transform-parameters-lambda-list
pattern))
,(if default-p
default
'*)
,@supplied-tail)))))
((member state '(required &rest &body &whole))
(when (eq state '&whole)
(setq state 'required))
(transform-parameters-lambda-list lambda-var))
(t lambda-var)))))
(defun fake-params-args (ll)
(let ((state 'required))
(flet ((do-arg (lambda-var)
(let ((var-name (symbol-name lambda-var)))
(cond ((or (eq state 'required) (eq state '&optional))
(list (gensym var-name)))
((eq state '&key)
`(,(intern var-name :keyword) ,(gensym var-name)))
(t nil)))))
(loop for lambda-var in ll
append (cond ((member lambda-var lambda-list-keywords :test #'eq)
(setq state lambda-var)
nil)
((eq state '&whole)
(setq state 'required)
nil)
((atom lambda-var)
(do-arg lambda-var))
((consp lambda-var)
(let ((var (car lambda-var)))
(do-arg (if (and (eq state '&key) (consp var))
(car var)
var)))))))))
;;; Yet another variation on a theme...
(defun get-all-params (ll)
(unless ll
(return-from get-all-params nil))
(when (atom ll)
(return-from get-all-params (list ll)))
(let ((state 'required))
(loop for arg in ll
append (cond ((member arg lambda-list-keywords :test #'eq)
(setq state arg)
nil)
((eq state 'required)
(get-all-params arg))
((or (eq state '&optional) (eq state '&aux))
(if (atom arg)
(list arg)
(get-all-params (car arg))))
((eq state '&key)
(cond ((atom arg)
(list arg))
((atom (car arg))
(list (car arg)))
(t (get-all-params (cadar arg)))))
((member state '(required &rest &body &whole))
(when (eq state '&whole)
(setq state 'required))
(get-all-params arg))
(t nil)))))
;;; ...And another. Given a lambda list, return a form that replicates the
;;; structure of the argument with variables filled in.
(defun map-over-lambda-list (function ll
&key (pass-lambda-list-keywords nil))
(declare (ignore function pass-lambda-list-keywords))
(unless ll
(return-from map-over-lambda-list nil))
(when (atom ll)
(return-from map-over-lambda-list ll))
(loop for args-tail = ll then (cdr args-tail)))
(defun make-keyword (sym)
(intern (symbol-name sym) :keyword))
(defun cull-keywords (keys prop-list)
(let ((plist (copy-list prop-list)))
(loop for key in keys
do (remf plist key))
plist))
(defun recreate-lambda-list (ll)
"Helper function. Returns a form that, when evaluated inside a
DESTRUCTURING-BIND using ll, recreates the argument list with all defaults
filled in."
(unless ll
(return-from recreate-lambda-list nil))
(when (atom ll)
(return-from recreate-lambda-list ll))
(let ((state 'required)
(rest-var nil)
(has-keys nil)
(keys nil)
(allow-other-keys nil))
(loop for arg in ll
append (cond ((member arg lambda-list-keywords :test #'eq)
(setq state arg)
(when (eq arg '&key)
(setq has-keys t))
(when (eq arg '&allow-other-keys)
(setq allow-other-keys t))
nil)
((eq state '&whole)
nil)
((eq state 'required)
(list (recreate-lambda-list arg)))
((eq state '&optional)
(if (atom arg)
(list arg)
(list (recreate-lambda-list (car arg)))))
((or (eq state '&rest) (eq state '&body))
(setq rest-var arg)
nil)
((eq state '&key)
(let ((key nil)
(var nil))
(cond ((atom arg)
(setq key (make-keyword arg)
var arg))
((atom (car arg))
(setq key (make-keyword (car arg))
var (car arg)))
(t (destructuring-bind
((keyword pattern) &rest tail)
arg
(declare (ignore tail))
(setq key keyword
var (recreate-lambda-list
pattern)))))
(push key keys)
(list key var)))
(t nil))
into result-form
finally (cond ((or (not rest-var)
(and has-keys
(not allow-other-keys)))
(return `(list ,@result-form)))
((not has-keys)
(return `(list* ,@result-form ,rest-var)))
(t (return `(list* ,@result-form
(cull-keywords ',(nreverse keys)
,rest-var))))))))
(defun transform-options-lambda-list (ll)
"Return a legal lambda list given an options specification"
(let ((descriptionp nil))
(loop for spec in ll
collect (if (atom spec)
(progn
(when (eq (make-keyword spec) :description)
(setq descriptionp t))
spec)
(progn
(let ((key (if (atom (car spec))
(make-keyword (car spec))
(caar spec))))
(when (eq key :description)
(setq descriptionp t)))
(ldiff spec (cdddr spec))))
into specs
finally (return `(&key
,@specs
,@(unless descriptionp
`(((:description ,(gensym))))))))))
;;; External function
(declaim (inline presentation-type-name))
(defun presentation-type-name (type)
(cond ((atom type)
type)
((atom (car type))
(car type))
(t (caar type))))
(defun decode-parameters (type)
(cond ((atom type)
nil)
((atom (car type))
(cdr type))
(t (cdar type))))
(defun decode-options (type)
(if (or (atom type) (atom (car type)))
nil
(cdr type)))
(defun make-inherit-from-lambda (params-ll options-ll form)
(let ((type (gensym "TYPE")))
`(lambda (,type)
(destructuring-bind ,params-ll (decode-parameters ,type)
(declare (ignorable ,@(get-all-params params-ll)))
(destructuring-bind ,options-ll (decode-options ,type)
(declare (ignorable ,@(get-all-params options-ll)))
,form)))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro with-presentation-type-decoded ((name
&optional (params nil params-p)
(options nil options-p))
type &body body)
(let ((type-var (gensym "TYPE-VAR")))
`(let* ((,type-var ,type)
(,name (presentation-type-name ,type-var))
,@(and params-p `((,params (decode-parameters ,type-var))))
,@(and options-p `((,options (decode-options ,type-var)))))
,@body)))
)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun make-expansion-lambda (params-ll options-ll)
(let ((params-form (recreate-lambda-list params-ll))
(options-form (recreate-lambda-list options-ll))
(parameters (gensym))
(options (gensym)))
`(lambda (typespec)
(with-presentation-type-decoded (name ,parameters ,options)
typespec
(make-type-spec name
(destructuring-bind ,params-ll
,parameters
,params-form)
(destructuring-bind ,options-ll
,options
,options-form)))))))
(defvar *presentation-type-table* (make-hash-table :test #'eq))
(setf (gethash t *presentation-type-table*) (find-class t))
(defgeneric get-ptype-metaclass (type))
(defmethod get-ptype-metaclass ((type symbol))
(let ((maybe-meta (gethash type *presentation-type-table*)))
(if maybe-meta
(get-ptype-metaclass maybe-meta)
(let ((system-meta (find-class type nil)))
(and (typep system-meta 'standard-class)
system-meta)))))
(defmethod get-ptype-metaclass ((type presentation-type-class))
type)
(defmethod get-ptype-metaclass ((type clos-presentation-type))
(clos-class type))
(defmethod get-ptype-metaclass ((type (eql *builtin-t-class*)))
type)
(defmethod get-ptype-metaclass ((type class))
type)
(defmethod get-ptype-metaclass (type)
(error "~A is not the name of a presentation type" type))
;;; external functions
(defun find-presentation-type-class (name &optional (errorp t) environment)
(declare (ignore environment))
(let ((metaclass (get-ptype-metaclass name)))
(cond (metaclass
metaclass)
(errorp
(error "~S is not the name of a presentation type" name))
(t nil))))
(defun class-presentation-type-name (class &optional environment)
(declare (ignore environment))
(cond ((typep class 'presentation-type)
(type-name class))
(t (class-name class))))
;;; For the presentation class T, the stand in object must not be a
;;; standard-object. So... why not the symbol T?
(defvar *class-t-prototype* t)
(defun prototype-or-error (name)
(let ((ptype-meta (get-ptype-metaclass name)))
(unless ptype-meta
(error "~S is an unknown presentation type" name))
(when (eq ptype-meta *builtin-t-class*)
(return-from prototype-or-error *class-t-prototype*))
(unless (clim-mop:class-finalized-p ptype-meta)
(clim-mop:finalize-inheritance ptype-meta))
(or (clim-mop:class-prototype ptype-meta)
(error "Couldn't find a prototype for ~S" name))))
(defun safe-cpl (class)
(unless (clim-mop:class-finalized-p class)
(clim-mop:finalize-inheritance class))
(clim-mop:class-precedence-list class))
(defun get-ptype (name)
(or (gethash name *presentation-type-table*)
(let ((meta (find-class name nil)))
(and (typep meta 'standard-class)
meta))))
(defgeneric presentation-ptype-supers (type)
(:documentation "Gets a list of the presentation type objects for those
supertypes of TYPE that are presentation types"))
(defmethod presentation-ptype-supers ((type symbol))
(let ((ptype (gethash type *presentation-type-table*)))
(if ptype
(presentation-ptype-supers ptype)
nil)))
(defmethod presentation-ptype-supers ((type presentation-type-class))
(mapcan #'(lambda (class)
(typecase class
(presentation-type
(list class))
(standard-class
(let ((clos-ptype (gethash (class-name class)
*presentation-type-table*)))
(if clos-ptype
(list clos-ptype)
nil)))
(t
nil)))
(clim-mop:class-direct-superclasses type)))
(defmethod presentation-ptype-supers ((type clos-presentation-type))
(presentation-ptype-supers (clos-class type)))
;;; External function
(defun presentation-type-direct-supertypes (type)
(with-presentation-type-decoded (name)
type
(let ((supers (presentation-ptype-supers name)))
(mapcar #'class-presentation-type-name supers))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmethod ptype-specializer ((type symbol))
(let ((ptype (gethash type *presentation-type-table*)))
(cond (ptype
(ptype-specializer ptype))
((find-class type nil)
(ptype-specializer (find-class type)))
;; Assume it's a forward referenced CLOS class.
(t type))))
(defmethod ptype-specializer ((type standard-class))
(class-name type)))
;;; We need to patch defclass in every implementation to record a CLOS
;;; class at compiletime. On the other hand, I think we can assume
;;; that if a CLOS class exists at compile time, it will exist at
;;; load/run time too.
(eval-when (:compile-toplevel :load-toplevel :execute)
#-(or excl cmu sbcl openmcl)
(defun compile-time-clos-p (name)
(let ((meta (find-class name nil)))
(and meta
(typep meta 'standard-class))))
#+(or excl cmu sbcl openmcl)
(defun compile-time-clos-p (name)
(let ((metaclass (find-class name nil)))
(or (and metaclass
(typep metaclass 'standard-class))
(clim-lisp-patch::compile-time-clos-class-p name))))
(defun make-default-description (name)
"Create a description string from the type name"
(let ((downcase-name (string-downcase name)))
(setq downcase-name (nsubstitute-if-not #\Space
#'alphanumericp
downcase-name))
(string-trim " " downcase-name)))
(defun record-presentation-type (name parameters params-ll options options-ll
inherit-from-func description history
parameters-are-types
compile-time-p
supers expansion-func)
(let* ((fake-name (make-presentation-type-name name))
(ptype-class-args (list :type-name name
:parameters parameters
:parameters-lambda-list params-ll
:options options
:options-lambda-list options-ll
:inherit-from-function inherit-from-func
:description description
:history history
:parameters-are-types parameters-are-types
:expansion-function expansion-func))
(ptype-meta
(if compile-time-p
(apply #'make-instance
(if (compile-time-clos-p name)
'clos-presentation-type
'presentation-type)
ptype-class-args)
(let* ((clos-meta (find-class name nil))
(closp (typep clos-meta 'standard-class)))
(if closp
(apply #'make-instance 'clos-presentation-type
:clos-class clos-meta
ptype-class-args)
(let ((directs (mapcan
#'(lambda (super)
(if (eq super t)
nil
(list (or (get-ptype-metaclass
super)
super))))
supers)))
(apply #'clim-mop:ensure-class fake-name
:name fake-name
:metaclass 'presentation-type-class
:direct-superclasses directs
ptype-class-args)))))))
(setf (gethash name *presentation-type-table*) ptype-meta)
ptype-meta))
); eval-when
(defgeneric massage-type-for-super (type-name super-name type-spec)
(:documentation "translate TYPE-SPEC from that of TYPE-NAME to one
suitable for SUPER-NAME"))
;;; The default: there ain't no direct specification
(defmethod massage-type-for-super ((type-name t) (super-name t) type-spec)
(declare (ignore type-spec))
(values nil nil))
;;; Load-time actions for define-presentation-type
(defmacro %define-presentation-type (name parameters params-ll
options options-ll
inherit-from inherit-from-lambda
description history parameters-are-types)
(declare (ignore inherit-from))
(let* ((inherit-from-func (coerce inherit-from-lambda 'function))
(inherit-typespec (funcall inherit-from-func
(cons name (fake-params-args params-ll))))
(superclasses (if inherit-typespec
(with-presentation-type-decoded
(super-name super-params)
inherit-typespec
(if (eq super-name 'and)
(mapcar #'presentation-type-name super-params)
(list super-name)))
nil))
(expansion-lambda (make-expansion-lambda params-ll options-ll)))
`(progn
(record-presentation-type ',name ',parameters ',params-ll ',options
',options-ll #',inherit-from-lambda
',description ',history
',parameters-are-types
nil ',superclasses
#',expansion-lambda)
,@(cond ((eq (presentation-type-name inherit-typespec) 'and)
(loop for super in superclasses
for i from 0
append (unless (or (not (atom super))
(eq super 'satisfies)
(eq super 'not))
`((defmethod massage-type-for-super
((type-name (eql ',name))
(super-name (eql ',super))
type)
(values (nth ,i
(cdr (,inherit-from-lambda
type)))
t))))))
(superclasses
`((defmethod massage-type-for-super
((type-name (eql ',name))
(super-name (eql ',(car superclasses)))
type)
(values (,inherit-from-lambda type) t))))
(t nil)))))
(defmacro define-presentation-type (name parameters
&key options inherit-from
(description
(make-default-description name))
(history t)
parameters-are-types)
(let* ((params-ll (transform-parameters-lambda-list parameters))
(options-ll (transform-options-lambda-list options))
(inherit-from-func (make-inherit-from-lambda params-ll
options-ll
inherit-from)))
`(progn
(eval-when (:compile-toplevel)
(record-presentation-type ',name ',parameters ',params-ll ',options
',options-ll #',inherit-from-func
',description ',history
',parameters-are-types
t nil nil))
(eval-when (:load-toplevel :execute)
(%define-presentation-type ,name ,parameters ,params-ll
,options ,options-ll
,inherit-from ,inherit-from-func
,description ,history
,parameters-are-types)))))
;;; These are used by the presentation method MOP code, but are
;;; actually defined in presentation-defs.lisp after the forms for these
;;; types are executed.
(defvar *ptype-t-class*)
(defvar *ptype-expression-class*)
(defvar *ptype-form-class*)
(defun presentation-type-parameters (type-name &optional env)
(declare (ignore env))
(let ((ptype (gethash type-name *presentation-type-table*)))
(unless ptype
(error "~S is not the name of a presentation type" type-name))
(parameters ptype)))
(defun presentation-type-options (type-name &optional env)
(declare (ignore env))
(let ((ptype (gethash type-name *presentation-type-table*)))
(unless ptype
(error "~S is not the name of a presentation type" type-name))
(options ptype)))
(defmacro with-presentation-type-parameters ((type-name type) &body body)
(let ((ptype (get-ptype type-name)))
(unless (or ptype (compile-time-clos-p type-name))
(warn "~S is not a presentation type name." type-name))
(if (typep ptype 'presentation-type)
(let* ((params-ll (parameters-lambda-list ptype))
(params (gensym "PARAMS"))
(type-var (gensym "TYPE-VAR"))
(ignorable-vars (get-all-params params-ll)))
`(let ((,type-var ,type))
(unless (eq ',type-name (presentation-type-name ,type-var))
(error "Presentation type specifier ~S does not match the name ~S"
,type-var
',type-name))
(let ((,params (decode-parameters ,type-var)))
(declare (ignorable ,params))
(destructuring-bind ,params-ll ,params
(declare (ignorable ,@ignorable-vars))
,@body))))
`(let ()
,@body))))
(defmacro with-presentation-type-options ((type-name type) &body body)
(let ((ptype (get-ptype type-name)))
(unless (or ptype (compile-time-clos-p type-name))
(warn "~S is not a presentation type name." type-name))
(if (typep ptype 'presentation-type)
(let* ((options-ll (options-lambda-list ptype))
(options (gensym "OPTIONS"))
(type-var (gensym "TYPE-VAR"))
(ignorable-vars (get-all-params options-ll)))
`(let ((,type-var ,type))
(unless (eq ',type-name (presentation-type-name ,type-var))
(error "Presentation type specifier ~S does not match the name ~S"
,type-var
',type-name))
(let ((,options (decode-options ,type-var)))
(declare (ignorable ,options))
(destructuring-bind ,options-ll ,options
(declare (ignorable ,@ignorable-vars))
,@body))))
`(let ()
,@body))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *presentation-type-abbreviations* (make-hash-table :test #'eq)))
(defmacro define-presentation-type-abbreviation (name parameters
equivalent-type
&key options)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(setf (gethash ',name *presentation-type-abbreviations*)
#',(make-inherit-from-lambda
(transform-parameters-lambda-list parameters)
(transform-options-lambda-list options)
equivalent-type))))
(defun make-type-spec (name parameters options)
(cond (options
`((,name ,@parameters) ,@options))
(parameters
`(,name ,@parameters))
(t name)))
(defun expand-presentation-type-abbreviation-1 (type &optional env)
(flet ((expand-list (specs)
(loop with expand-any-p = nil
for spec in specs
collect (multiple-value-bind (expansion expanded)
(expand-presentation-type-abbreviation-1 spec env)
(setq expand-any-p (or expand-any-p expanded))
expansion)
into new-params
finally (return (if expand-any-p
(values new-params t)
(values specs nil))))))
(with-presentation-type-decoded (name parms options)
type
(case name
((and or sequence sequence-enumerated)
(multiple-value-bind (expansion expanded)
(expand-list parms)
(if expanded
(values (make-type-spec name expansion options) t)
(values type nil))))
(t (let* ((expander (gethash name *presentation-type-abbreviations*)))
(flet ((copy-description (expanded-typespec)
(with-presentation-type-decoded (expand-name
expand-params
expand-options)
expanded-typespec
(let ((description (getf options :description))
(expand-desc (getf expand-options :description)))
(if (and description
(null expand-desc))
(make-type-spec expand-name
expand-params
`(:description ,description
,@expand-options))
expanded-typespec)))))
(if expander
(values (copy-description (funcall expander type)) t)
(values type nil)))))))))
(defun expand-presentation-type-abbreviation (type &optional env)
(let ((expand-any-p nil))
(loop
(multiple-value-bind (expansion expanded)
(expand-presentation-type-abbreviation-1 type env)
(if expanded
(progn
(setq expand-any-p t)
(setq type expansion))
(return (values type expand-any-p)))))))
(defun make-presentation-type-specifier (name-and-params &rest options)
(with-presentation-type-decoded (name)
name-and-params
(let ((ptype (gethash name *presentation-type-table*)))
(unless ptype
(return-from make-presentation-type-specifier name-and-params))
(with-presentation-type-decoded (name parameters defaults)
(funcall (expansion-function ptype) name-and-params)
(declare (ignore name parameters))
(loop for (key val) on options by #'cddr
for default = (getf defaults key)
unless (equal val default)
nconc (list key val) into needed-options
finally (return (if needed-options
`(,name-and-params ,@needed-options)
name-and-params)))))))
;;; Presentation methods.
;;;
;;; The basic dispatch is performed via CLOS instances that are standins
;;; for the presentation types. There are a couple of complications to
;;; this simple plan. First, methods on the presentation type class
;;;STANDARD-OBJECT -- if there are any -- should not be applicable to
;;;presentation types that are not CLOS classes, even though STANDARD-OBJECT
;;;is in the class precedence list of the standin object. Our own methods on
;;;COMPUTE-APPLICABLE-METHODS-USING-CLASSES and COMPUTE-APPLICABLE-METHODS
;;;remove methods specialized on standard-object.
;;;
;;; The second major complication is the whole raison d'etre of presentation
;;; type methods: type parameters and options are massaged so that
;;; applicable methods written on the supertype of a presentation type get
;;; parameters and options in the expected form. "Real" CLIM apparently
;;; does this massaging in the body of the effective method and passes the
;;; massaged parameters as an argument into the method. We do it with a
;;; function call within the body of the method. This is potentially more
;;; expensive, but caching should help that. Our method has the huge
;;; advantage of working with any method combination.
(defclass presentation-gf-info ()
((generic-function-name :accessor generic-function-name
:initarg :generic-function-name)
(lambda-list :accessor lambda-list :initarg :lambda-list)
(type-key-arg :accessor type-key-arg :initarg :type-key-arg)
(parameters-arg :accessor parameters-arg :initarg :parameters-arg
:initform nil)
(options-arg :accessor options-arg :initarg :options-arg :initform nil)
(type-arg-position :accessor type-arg-position
:initarg :type-arg-position)))
(defvar *presentation-gf-table* (make-hash-table :test #'eq))
(defclass presentation-generic-function (standard-generic-function)
()
(:metaclass clim-mop:funcallable-standard-class))
(defvar *standard-object-class* (find-class 'standard-object))
#-scl
(defmethod clim-mop:compute-applicable-methods-using-classes :around
((gf presentation-generic-function) classes)
(multiple-value-bind (methods success)
(call-next-method)
(let ((ptype-class (car classes)))
(if (or (null success)
(not (typep ptype-class 'presentation-type-class)))
(values methods success)
(values (remove-if #'(lambda (method)
(eq (car (clim-mop:method-specializers
method))
*standard-object-class*))
methods)
t)))))
#+scl
(defmethod clim-mop:compute-applicable-methods-using-classes :around
((gf presentation-generic-function) classes)
(multiple-value-bind (methods success non-class-positions)
(call-next-method)
(let ((ptype-class (car classes)))
(if (or (null success)
(not (typep ptype-class 'presentation-type-class)))
(values methods non-class-positions non-class-positions)
(values (remove-if #'(lambda (method)
(eq (car (clim-mop:method-specializers
method))
*standard-object-class*))
methods)
t
non-class-positions)))))
(defun method-applicable (method arguments)
(loop for arg in arguments
for specializer in (clim-mop:method-specializers method)
always (cond ((typep specializer 'clim-mop:eql-specializer)
(eql arg (clim-mop:eql-specializer-object specializer)))
((typep arg specializer)
t)
((and (not (typep (class-of arg)
'presentation-type-class))
(or (eq specializer *ptype-form-class*)
(eq specializer *ptype-expression-class*)))
t)
(t nil))))
(defmethod compute-applicable-methods :around
((gf presentation-generic-function) arguments)
(let ((methods (call-next-method)))
(if (typep (class-of (car arguments)) 'presentation-type-class)