forked from slyrus/mcclim-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation-defs.lisp
2356 lines (2096 loc) · 87.7 KB
/
presentation-defs.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 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.
;;; Definitions for the standard presentation types and generic functions
(in-package :clim-internals)
;;; The presentation type for T is the built-in type T. The correspondence is
;;; established by hand in presentations.lisp.
;(define-presentation-type t ())
;;; auto-activate is described in the Franz user guide; it controls whether an
;;; accepting an expression returns immediately after typing the closing
;;; delimiter -- a la Genera et Mac Lisp -- or if an activation gesture is
;;; required.
;;; preserve-whitespace controls whether the accept method uses read or
;;; read-preserving-whitespace. This is used in our redefinitions of read and
;;; read-preserving-whitespace that accept forms.
(define-presentation-type expression ()
:options (auto-activate (preserve-whitespace t) (subform-read nil))
:inherit-from t)
(define-presentation-type form ()
:options (auto-activate (preserve-whitespace t) (subform-read nil))
:inherit-from `((expression) :auto-activate ,auto-activate
:preserve-whitespace ,preserve-whitespace
:subform-read ,subform-read ))
;;; Actual definitions of presentation methods and types. They've
;;; been separated from the macro and presentation class definitions and
;;; helper functions in order to avoid putting all of presentations.lisp
;;; inside a (eval-when (compile) ...).
(define-presentation-generic-function %presentation-typep presentation-typep
(type-key parameters object type))
(define-default-presentation-method presentation-typep (object type)
(declare (ignore object type))
nil)
(defun presentation-typep (object type)
(with-presentation-type-decoded (name parameters)
type
(when (null parameters)
(let ((clos-class (find-class name nil))) ; Don't error out.
(when (and clos-class (typep clos-class 'standard-class))
(return-from presentation-typep (typep object name)))))
(funcall-presentation-generic-function presentation-typep object type)))
;;; Not defined as a generic function, but what the hell.
(defgeneric presentation-type-of (object))
(defmethod presentation-type-of (object)
(declare (ignore object))
'expression)
(defun get-ptype-from-class-of (object)
(let* ((name (class-name (class-of object)))
(ptype-entry (gethash name *presentation-type-table*)))
(unless ptype-entry
(return-from get-ptype-from-class-of nil))
;; Does the type have required parameters? If so, we can't use it...
(let ((parameter-ll (parameters-lambda-list ptype-entry)))
(values name
(if (eq (car parameter-ll) '&whole)
(cddr parameter-ll)
parameter-ll)))))
(defmethod presentation-type-of ((object standard-object))
(multiple-value-bind (name lambda-list)
(get-ptype-from-class-of object)
(cond ((and name
(or (null lambda-list)
(member (first lambda-list) lambda-list-keywords)))
name)
(name
'standard-object)
(t (let* ((class (class-of object))
(class-name (class-name class)))
(or class-name class))))))
(defmethod presentation-type-of ((object structure-object))
(multiple-value-bind (name lambda-list)
(get-ptype-from-class-of object)
(if (and name
(or (null lambda-list)
(member lambda-list lambda-list-keywords)))
name
(call-next-method))))
(define-presentation-generic-function
%map-over-presentation-type-supertypes
map-over-presentation-type-supertypes
(type-key function type))
;;; Define the method for presentation and clos types
(define-default-presentation-method map-over-presentation-type-supertypes
(function type)
(let ((type-name (presentation-type-name type)))
(map-over-ptype-superclasses
#'(lambda (super)
(let ((super-name (type-name super)))
(funcall function
super-name
(funcall (expansion-function super)
(translate-specifier-for-type type-name
super-name
type)))))
type-name)))
(defun map-over-presentation-type-supertypes (function type)
(funcall-presentation-generic-function map-over-presentation-type-supertypes
function
type))
(define-presentation-generic-function
%presentation-subtypep
presentation-subtypep
(type-key type putative-supertype))
;;; The semantics of the presentation method presentation-subtypep are truly
;;; weird; method combination is in effect disabled. So, the methods have to
;;; be eql methods.
(defmacro define-subtypep-method (&rest args)
(let ((gf (gethash 'presentation-subtypep *presentation-gf-table*)))
(multiple-value-bind (qualifiers lambda-list decls body)
(parse-method-body args)
(let ((type-arg (nth (1- (type-arg-position gf)) lambda-list)))
(unless (consp type-arg)
(error "Type argument in presentation method must be specialized"))
(unless (eq (car type-arg) 'type)
(error "Type argument mismatch with presentation generic function
definition"))
(destructuring-bind (type-var type-name) type-arg
(let ((method-ll `((,(type-key-arg gf)
(eql (prototype-or-error ',type-name)))
,@(copy-list lambda-list))))
(setf (nth (type-arg-position gf) method-ll) type-var)
`(defmethod %presentation-subtypep ,@qualifiers ,method-ll
(declare (ignorable ,(type-key-arg gf))
,@(cdr decls))
(block presentation-subtypep
,@body))))))))
;;; PRESENTATION-SUBTYPEP suffers from some of the same problems as
;;; CL:SUBTYPEP, most (but sadly not all) of which were solved in
;;; H. Baker "A Decision Procedure for SUBTYPEP"; additionally, it
;;; suffers from the behaviour being underspecified, as CLIM
;;; documentation did not have the years of polish that CLtS did.
;;;
;;; So you might wonder why, instead of copying or using directly some
;;; decent Public Domain subtype code (such as that found in SBCL,
;;; implementing CL:SUBTYPEP), there's this slightly wonky
;;; implementation here. Well, some of the answer lies in the fact
;;; that the subtype relationships answered by this predicate are not
;;; in fact analogous to CL's type system. The major use of
;;; PRESENTATION-SUBTYPEP seems to be for determining whether a
;;; presentation is applicable as input to a translator (including the
;;; default translator, transforming an object to itself); actually,
;;; the first step is taken by STUPID-SUBTYPEP, but that I believe is
;;; simply intended to be a short-circuiting conservative version of
;;; PRESENTATION-SUBTYPEP.
;;;
;;; Most presentation types in CLIM are hierarchically arranged by
;;; single-inheritance, and SUBTYPEP relations on the hierarchy are
;;; easy to determine: simply walk up the hierarchy until you find the
;;; putative supertype (in which case the answer is T, T unless the
;;; type's parameters are wrong) or you find the universal supertype
;;; (in which case the answer is NIL, T. There are numerous wrinkles,
;;; however...
;;;
;;; (1) the NIL presentation type is the universal subtype, breaking
;;; the single-inheritance of the hierarchy. This isn't too bad,
;;; because it can be special-cased.
;;;
;;; (2) union types can be constructed, destroying the
;;; single-inheritance hierarchy (when used as a subtype).
;;;
;;; (3) union types can give rise to ambiguity. For example, is the
;;; NUMBER presentation type subtypep (OR REAL COMPLEX)? What
;;; about (INTEGER 3 6) subtypep (OR (INTEGER 3 4) (INTEGER 5 6))?
;;; Is (OR A B) subtypep (OR B A)? The answer to this last
;;; question is not obvious, as the two types have different
;;; ACCEPT behaviour if A and B have any Lisp objects in common,
;;; even if the presentation types are hierarchically unrelated...
;;;
;;; (4) intersection types can be constructed, destroying the
;;; single-inheritance hierarchy (when used as a supertype). This
;;; is partially mitigated by the explicit documentation that the
;;; first type in the AND type's parameters is privileged and
;;; treated specially by ACCEPT.
;;;
;;; Given these difficulties, I'm aiming for roughly expected
;;; behaviour from STUPID- and PRESENTATION-SUBTYPEP, rather than
;;; something which has a comprehensive understanding of presentation
;;; types and the Lisp object universe (as this would be unachievable
;;; anyway: the user can write arbitrary PRESENTATION-TYPEP
;;; functions); PRESENTATION-SUBTYPEP should not be thought of as a
;;; predicate over sets of Lisp objects, but simply a formal predicate
;;; over a graph of names. This gives rise to the implementation
;;; below for OR and AND types, and the hierarchical walk for all
;;; other types. CSR, 2007-01-10
(defun presentation-subtypep (type maybe-supertype)
;; special shortcuts: the universal subtype is privileged (and
;; doesn't in fact fit into a hierarchical lattice); the universal
;; supertype is easy to identify.
(when (or (eql type nil) (eql maybe-supertype t))
(return-from presentation-subtypep (values t t)))
(when (eql type maybe-supertype)
(return-from presentation-subtypep (values t t)))
(with-presentation-type-decoded (super-name super-parameters)
maybe-supertype
(with-presentation-type-decoded (type-name type-parameters)
type
(cond
;; DO NOT BE TEMPTED TO REARRANGE THESE CLAUSES
((eq type-name 'or)
(dolist (or-type type-parameters
(return-from presentation-subtypep (values t t)))
(multiple-value-bind (yesp surep)
(presentation-subtypep or-type maybe-supertype)
(unless yesp
(return-from presentation-subtypep (values yesp surep))))))
((eq super-name 'and)
(let ((result t))
(dolist (and-type super-parameters
(return-from presentation-subtypep (values result result)))
(cond
((and (consp and-type) (eq (car and-type) 'satisfies))
(setq result nil))
((and (consp and-type) (eq (car and-type) 'not))
(multiple-value-bind (yp sp)
(presentation-subtypep type (cadr and-type))
(if yp
(return-from presentation-subtypep (values nil t))
(setq result nil))))
(t (multiple-value-bind (yp sp)
(presentation-subtypep type and-type)
(unless yp
(if sp
(return-from presentation-subtypep (values nil t))
(setq result nil)))))))))
((eq super-name 'or)
(assert (not (eq type-name 'or)))
;; FIXME: this would be the right method were it not for the
;; fact that there can be unions 'in disguise' in the
;; subtype; examples:
;; (PRESENTATION-SUBTYPEP 'NUMBER '(OR REAL COMPLEX))
;; (PRESENTATION-SUBTYPEP '(INTEGER 3 6)
;; '(OR (INTEGER 2 5) (INTEGER 4 7)))
;; Sorry about that.
(let ((surep t))
(dolist (or-type super-parameters
(return-from presentation-subtypep (values nil surep)))
(multiple-value-bind (yp sp)
(presentation-subtypep type or-type)
(cond
(yp (return-from presentation-subtypep (values t t)))
((not sp) (setq surep nil)))))))
((eq type-name 'and)
(assert (not (eq super-name 'and)))
(multiple-value-bind (yp sp)
(presentation-subtypep (car type-parameters) maybe-supertype)
(return-from presentation-subtypep (values yp yp))))))
(map-over-presentation-type-supertypes
#'(lambda (name massaged)
(when (eq name super-name)
(return-from presentation-subtypep
(funcall-presentation-generic-function presentation-subtypep
massaged
maybe-supertype))))
type))
(values nil t))
(define-default-presentation-method presentation-subtypep
(type maybe-supertype)
(with-presentation-type-decoded (name params)
type
(declare (ignore name))
(with-presentation-type-decoded (super-name super-params)
maybe-supertype
(declare (ignore super-name))
(if (equal params super-params)
(values t t)
(values nil nil)))))
(define-presentation-generic-function
%presentation-type-specifier-p
presentation-type-specifier-p
(type-class type))
(define-default-presentation-method presentation-type-specifier-p (type)
t)
(defun presentation-type-specifier-p (object)
"Return true if `object' is a valid presentation type specifier,
otherwise return false."
;; Apparently, this funtion has to handle arbitrary objects.
(let ((name (presentation-type-name object)))
(when (and (typep name '(or symbol class))
(get-ptype-metaclass name))
(funcall-presentation-generic-function presentation-type-specifier-p object))))
(defun default-describe-presentation-type (description stream plural-count)
(if (symbolp description)
(setq description (make-default-description (symbol-name description))))
(cond ((eql 1 plural-count)
(format stream "~:[a~;an~] ~A"
(find (char description 0) "aeiouAEIOU")
description))
((numberp plural-count)
(format stream "~D ~A~P" plural-count description plural-count))
(plural-count
(format stream "~As" description))
(t (write-string description stream))))
(define-presentation-generic-function %describe-presentation-type
describe-presentation-type
(type-key parameters options type stream plural-count ))
;;; Support for the default method on describe-presentation-type: if a CLOS
;;; class has been defined as a presentation type, get description out of the
;;; presentation type.
(defmethod description ((class standard-class))
(let* ((name (class-name class))
(ptype-entry (gethash name *presentation-type-table*)))
(if ptype-entry
(description ptype-entry)
(make-default-description name))))
(define-default-presentation-method describe-presentation-type
(type stream plural-count)
(with-presentation-type-decoded (name parameters options)
type
(declare (ignore name parameters))
(let ((description (or (getf options :description)
(description (class-of type-key)))))
(default-describe-presentation-type description
stream
plural-count))))
(defun describe-presentation-type (type
&optional
(stream *standard-output*)
(plural-count 1))
(flet ((describe-it (stream)
(funcall-presentation-generic-function describe-presentation-type
type
stream
plural-count)))
(if stream
(describe-it stream)
(with-output-to-string (s)
(describe-it s)))))
(define-presentation-generic-function %presentation-default-processor
presentation-default-processor
(type-key parameters default type &key default-type))
(define-default-presentation-method presentation-default-processor
(default type &key (default-type nil default-type-p))
(values default (if default-type-p
default-type
type)))
;;; XXX The spec calls out that the presentation generic function has keyword
;;; arguments acceptably and for-context-type, but the examples I've seen don't
;;; mention them at all in the methods defined for present. So, leave them out
;;; of the generic function lambda list...
(define-presentation-generic-function %present present
(type-key parameters options object type stream view
&key &allow-other-keys))
(defun present (object &optional (type (presentation-type-of object))
&key
(stream *standard-output*)
(view (stream-default-view stream))
modifier
acceptably
(for-context-type type)
single-box
(allow-sensitive-inferiors t)
(sensitive t)
(record-type 'standard-presentation))
(let* ((real-type (expand-presentation-type-abbreviation type))
(context-type (if (eq for-context-type type)
real-type
(expand-presentation-type-abbreviation
for-context-type))))
(stream-present stream object real-type
:view view :modifier modifier :acceptably acceptably
:for-context-type context-type :single-box single-box
:allow-sensitive-inferiors allow-sensitive-inferiors
:sensitive sensitive
:record-type record-type)))
(defgeneric stream-present (stream object type
&key view modifier acceptably for-context-type
single-box allow-sensitive-inferiors sensitive
record-type))
(defmethod stream-present ((stream output-recording-stream) object type
&key
(view (stream-default-view stream))
modifier
acceptably
(for-context-type type)
single-box
(allow-sensitive-inferiors t)
(sensitive t)
(record-type 'standard-presentation))
;; *allow-sensitive-inferiors* controls whether or not
;; with-output-as-presentation will emit a presentation
(let ((*allow-sensitive-inferiors* (and *allow-sensitive-inferiors*
sensitive)))
(with-output-as-presentation (stream object type
:view view
:modifier modifier
:single-box single-box
:allow-sensitive-inferiors
allow-sensitive-inferiors
:record-type record-type)
(funcall-presentation-generic-function
present object type stream view
:acceptably acceptably :for-context-type for-context-type))))
;;; Should work well enough on non-CLIM streams...
(defmethod stream-present (stream object type
&key
(view +textual-view+)
modifier
acceptably
(for-context-type type)
single-box
(allow-sensitive-inferiors t)
(sensitive t)
(record-type 'standard-presentation))
(declare (ignore modifier single-box allow-sensitive-inferiors sensitive
record-type))
(funcall-presentation-generic-function
present object type stream view
:acceptably acceptably :for-context-type for-context-type)
nil)
(defun present-to-string (object &optional (type (presentation-type-of object))
&key (view +textual-view+)
acceptably
(for-context-type type)
(string nil stringp)
(index 0 indexp))
(let* ((real-type (expand-presentation-type-abbreviation type))
(context-type (if (eq for-context-type type)
real-type
(expand-presentation-type-abbreviation
for-context-type))))
(when (and stringp indexp)
(setf (fill-pointer string) index))
(flet ((do-present (s)
(stream-present s object real-type
:view view :acceptably acceptably
:for-context-type context-type)))
(declare (dynamic-extent #'do-present))
(let ((result (if stringp
(with-output-to-string (stream string)
(do-present stream))
(with-output-to-string (stream)
(do-present stream)))))
(if stringp
(values string (fill-pointer string))
result)))))
;;; I believe this obsolete... --moore
(defmethod presentation-replace-input
((stream input-editing-stream) object type view
&key (buffer-start nil buffer-start-supplied-p)
(rescan nil rescan-supplied-p)
query-identifier
(for-context-type type))
(declare (ignore query-identifier))
(let ((result (present-to-string object type
:view view :acceptably nil
:for-context-type for-context-type)))
(apply #'replace-input stream result `(,@(and buffer-start-supplied-p
`(:buffer-start
,buffer-start))
,@(and rescan-supplied-p
`(:rescan ,rescan))))))
;;; Presentation histories.
;;; This allocates a hash table even if the frame doesn't support
;;; histories! I'm over it already. -- moore
(defclass presentation-history-mixin ()
((presentation-history :accessor frame-presentation-history
:initform (make-hash-table :test #'eq)))
(:documentation "Mixin class for frames that implements presentation
type histories"))
(define-presentation-generic-function %presentation-type-history
presentation-type-history
(type-key parameters type))
;;; This function should exist for convenience even if not mentioned in the
;;; spec.
(defun presentation-type-history (type)
(funcall-presentation-generic-function presentation-type-history type))
(defclass presentation-history ()
((stack :accessor presentation-history-array
:initform (make-array 1 :fill-pointer 0
:adjustable t)
:documentation "The history, with the newest objects at
the end of the array. Should contain conses with the car being
the object and the cdr being the type.")
(pointer :accessor presentation-history-pointer
:initform nil
:documentation "The index of the \"current\" object,
used when navigating the history. If NIL, means that no
navigation has yet been performed."))
(:documentation "Class for objects that contain the history for
a specific type."))
(define-default-presentation-method presentation-type-history (type)
(if (and *application-frame*
(frame-maintain-presentation-histories *application-frame*))
(with-presentation-type-decoded (name)
type
(let* ((ptype (get-ptype-metaclass name))
(history (history ptype)))
(case history
((t)
(let* ((history-table (frame-presentation-history
*application-frame*))
(history-object (gethash name history-table)))
(unless history-object
(setf history-object
(make-instance 'presentation-history)
(gethash name history-table)
history-object))
history-object))
(nil
nil)
(otherwise
(funcall-presentation-generic-function presentation-type-history
type)))))))
;;; Not in the spec, but I think this is necessary (or at any rate, the easiest
;;; way) to control whether or not to use histories in a given context.
(define-presentation-generic-function %presentation-type-history-for-stream
presentation-type-history-for-stream
(type-key parameters type stream)
(:documentation "Returns a type history or nil for a presentation TYPE and
STREAM. This is used by McCLIM to decide whether or not to use histories in a
given situation. A primary method specialized on just the type should
call-next-method to get the \"real\" answer based on the stream type."))
(define-default-presentation-method presentation-type-history-for-stream
(type stream)
(declare (ignore stream))
nil)
;;; method for clim-stream-pane in panes.lisp
(define-presentation-method presentation-type-history-for-stream
((type t) (stream input-editing-stream))
;; What is the purpose of this? Makes stuff harder to do, so
;; commented out...
;;(if (not (stream-rescanning-p stream))
;; (funcall-presentation-generic-function presentation-type-history type)
;; nil)
(funcall-presentation-generic-function presentation-type-history type))
(defun presentation-history-insert (history object ptype)
"Unconditionally insert `object' as an input of presentation
type `type' at the top of the presentation history `history', as
the most recently added object."
(vector-push-extend (cons object ptype)
(presentation-history-array history)))
(defun presentation-history-top (history ptype)
"Find the topmost (most recently added object) of `history'
that is of the presentation type `ptype' or a subtype. Two values
will be returned, the object and the presentation type of the
object. If no applicable object can be found, these values will
both be NIL."
(loop
with array = (presentation-history-array history)
for index from (1- (fill-pointer array)) downto 0
for (object . object-ptype) = (aref array index)
do
(when (presentation-subtypep object-ptype ptype)
(return (aref array index)))
finally (return (values nil nil))))
(defun presentation-history-reset-pointer (history)
"Set the pointer to point at the object most recently added
object."
(setf (presentation-history-pointer history) nil))
(defun presentation-history-next (history ptype)
"Go to the next input (forward in time) in `history' that is a
presentation-subtype of `ptype', respective to the pointer in
`history'. Returns two values: the found object and its
presentation type, both of which will be NIL if no applicable
object can be found."
(with-accessors ((pointer presentation-history-pointer)
(array presentation-history-array)) history
;; If no navigation has been performed, we have no object to go
;; forwards to.
(if (or (null pointer) (>= (1+ pointer) (length array)))
(values nil nil)
(progn
(incf pointer)
(destructuring-bind (object . object-ptype)
(aref array pointer)
(if object-ptype
(if (presentation-subtypep object-ptype ptype)
(values object object-ptype)
(presentation-history-next history ptype))
(values nil nil)))))))
(defun presentation-history-previous (history ptype)
"Go to the previous input (backward in time) in `history' that
is a presentation-subtype of `ptype', respective to the pointer
in `history'. Returns two values: the found object and its
presentation type, both of which will be NIL if no applicable
object can be found."
(with-accessors ((pointer presentation-history-pointer)
(array presentation-history-array)) history
(if (and (numberp pointer) (zerop pointer))
(values nil nil)
(progn
(cond ((and (numberp pointer) (plusp pointer))
(decf pointer))
((plusp (length array))
(setf pointer (1- (fill-pointer array)))))
(if (and (numberp pointer) (array-in-bounds-p array pointer))
(destructuring-bind (object . object-ptype)
(aref array pointer)
(if object-ptype
(if (presentation-subtypep object-ptype ptype)
(values object object-ptype)
(progn (presentation-history-previous history ptype)))
(values nil nil)))
(values nil nil))))))
(defmacro with-object-on-history ((history object ptype) &body body)
"Evaluate `body' with `object' as `ptype' as the head (most
recently added object) on `history', and remove it again after
`body' has run. If `body' as `ptype' is already the head, the
history will be unchanged."
(with-gensyms (added)
`(let ((,added (presentation-history-add ,history ,object ,ptype)))
(unwind-protect (progn ,@body)
(when ,added
(decf (fill-pointer (presentation-history-array ,history))))))))
(defun presentation-history-add (history object ptype)
"Add OBJECT and PTYPE to the HISTORY unless they are already at the head of
HISTORY"
(multiple-value-bind (top-object top-ptype)
(presentation-history-top history ptype)
(unless (and top-ptype (eql object top-object) (equal ptype top-ptype))
(presentation-history-insert history object ptype))))
(define-presentation-generic-function %accept accept
(type-key parameters options type stream view &key))
(defvar *recursive-accept-p* nil)
(defvar *recursive-accept-1-p* nil)
(defvar *active-history-type* nil)
;;; The spec says "default-type most be a presentation type specifier", but the
;;; examples we have imply that default-type is optional, so we'll be liberal
;;; in what we accept.
(defun accept (type &rest rest-args &key
(stream *standard-input*)
(view nil viewp)
(default nil defaultp)
(default-type nil default-type-p)
provide-default insert-default replace-input
(history nil historyp) ; true default supplied below
active-p ; Don't think this will be used
prompt prompt-mode display-default
query-identifier
activation-gestures additional-activation-gestures
delimiter-gestures additional-delimiter-gestures)
(declare (ignore insert-default replace-input active-p prompt prompt-mode
display-default query-identifier
activation-gestures additional-activation-gestures
delimiter-gestures additional-delimiter-gestures))
(handler-bind ((abort-gesture (lambda (condition)
(signal condition) ;; to give outer handlers a chance to say "I know how to handle this"
(abort condition))))
(let* ((real-type (expand-presentation-type-abbreviation type))
(real-default-type (cond (default-type-p
(expand-presentation-type-abbreviation
default-type))
((or defaultp provide-default)
real-type)
(t nil)))
(real-history-type (cond ((null historyp) real-type)
((null history) nil)
(t (expand-presentation-type-abbreviation
history))))
(*recursive-accept-p* *recursive-accept-1-p*)
(*recursive-accept-1-p* t))
(with-keywords-removed (rest-args (:stream))
(when (or default-type-p defaultp)
(setf rest-args
(list* :default-type real-default-type rest-args)))
(when historyp
(setf rest-args (list* :history real-history-type rest-args)))
(cond ((and viewp (symbolp view))
(setf rest-args
(list* :view (funcall #'make-instance view) rest-args)))
((consp view)
(setf rest-args
(list* :view (apply #'make-instance view) rest-args))))
;; Presentation type history interaction. According to the spec,
;; if provide-default is true, we take the default from the
;; presentation history. In addition, we'll implement the Genera
;; behavior of temporarily putting the default on the history
;; stack so the user can conveniently suck it in.
(labels ((get-history ()
(when real-history-type
(funcall-presentation-generic-function
presentation-type-history-for-stream
real-history-type stream)))
(do-accept (args)
(apply #'stream-accept stream real-type args)))
(let* ((default-from-history (and (not defaultp) provide-default))
(history (get-history))
(results
(multiple-value-list
(if history
(unwind-protect
(let ((*active-history-type* real-history-type))
(cond (defaultp
(with-object-on-history
(history default real-default-type)
(do-accept rest-args)))
(default-from-history
(multiple-value-bind
(history-default history-type)
(presentation-history-top history
real-default-type)
(do-accept (if history-type
(list* :default history-default
:default-type history-type
rest-args)
rest-args))))
(t (do-accept rest-args))))
(unless *recursive-accept-p*
(presentation-history-reset-pointer (get-history))))
(do-accept rest-args))))
(results-history (get-history)))
(when results-history
(presentation-history-add results-history
(car results)
real-type))
(values-list results)))))))
(defmethod stream-accept ((stream standard-extended-input-stream) type
&rest args
&key (view (stream-default-view stream))
&allow-other-keys)
(apply #'prompt-for-accept stream type view args)
(apply #'accept-1 stream type args))
(defmethod stream-accept ((stream #.*string-input-stream-class*) type
&key (view (stream-default-view stream))
(default nil defaultp)
(default-type nil default-type-p)
(activation-gestures nil activationsp)
(additional-activation-gestures
nil additional-activations-p)
(delimiter-gestures nil delimitersp)
(additional-delimiter-gestures
nil additional-delimiters-p)
&allow-other-keys)
(with-activation-gestures ((if additional-activations-p
additional-activation-gestures
activation-gestures)
:override activationsp)
(with-delimiter-gestures ((if additional-delimiters-p
additional-delimiter-gestures
delimiter-gestures)
:override delimitersp)
(multiple-value-bind (object object-type)
(apply-presentation-generic-function
accept
type stream view
`(,@(and defaultp `(:default ,default))
,@(and default-type-p `(:default-type ,default-type))))
(values object (or object-type type))))))
(defun accept-1 (stream type &key
(view (stream-default-view stream))
(default nil defaultp)
(default-type nil default-type-p)
provide-default
insert-default
(replace-input t)
history
active-p
prompt
prompt-mode
display-default
query-identifier
(activation-gestures nil activationsp)
(additional-activation-gestures nil additional-activations-p)
(delimiter-gestures nil delimitersp)
(additional-delimiter-gestures nil additional-delimiters-p))
(declare (ignore provide-default history active-p
prompt prompt-mode
display-default query-identifier))
(when (and defaultp (not default-type-p))
(error ":default specified without :default-type"))
(when (and activationsp additional-activations-p)
(error "only one of :activation-gestures or ~
:additional-activation-gestures may be passed to accept."))
(unless (or activationsp additional-activations-p *activation-gestures*)
(setq activation-gestures *standard-activation-gestures*))
(let ((sensitizer-object nil)
(sensitizer-type nil))
(with-input-editing
(stream
:input-sensitizer #'(lambda (stream cont)
(with-output-as-presentation
(stream sensitizer-object sensitizer-type)
(declare (ignore stream))
(funcall cont))))
(with-input-position (stream) ; support for calls to replace-input
(when (and insert-default
(not (stream-rescanning-p stream)))
;; Insert the default value to the input stream. It should
;; become fully keyboard-editable. We do not want to insert
;; the default if we're rescanning, only during initial
;; setup.
(presentation-replace-input stream default default-type view))
(setf (values sensitizer-object sensitizer-type)
(with-input-context (type)
(object object-type event options)
(with-activation-gestures ((if additional-activations-p
additional-activation-gestures
activation-gestures)
:override activationsp)
(with-delimiter-gestures ((if additional-delimiters-p
additional-delimiter-gestures
delimiter-gestures)
:override delimitersp)
(let ((accept-results nil))
(handle-empty-input (stream)
(setq accept-results
(multiple-value-list
(if defaultp
(funcall-presentation-generic-function
accept type stream view
:default default
:default-type default-type)
(funcall-presentation-generic-function
accept type stream view))))
;; User entered activation or delimiter
;; gesture without any input.
(if defaultp
(progn
(presentation-replace-input
stream default default-type view :rescan nil))
(simple-parse-error
"Empty input for type ~S with no supplied default"
type))
(setq accept-results (list default default-type)))
;; Eat trailing activation gesture
;; XXX what about pointer gestures?
;; XXX and delimiter gestures?
(unless *recursive-accept-p*
(let ((ag (read-char-no-hang stream nil stream t)))
(unless (or (null ag) (eq ag stream))
(unless (activation-gesture-p ag)
(unread-char ag stream)))))
(values (car accept-results) (if (cdr accept-results)
(cadr accept-results)
type)))))
;; A presentation was clicked on, or something
(t
(when (and replace-input
(getf options :echo t)
(not (stream-rescanning-p stream)))
(presentation-replace-input stream object object-type view
:rescan nil))
(values object object-type))))
;; Just to make it clear that we're returning values
(values sensitizer-object sensitizer-type)))))
(defmethod prompt-for-accept ((stream t)
type view
&rest accept-args
&key &allow-other-keys)
(declare (ignore view))
(apply #'prompt-for-accept-1 stream type accept-args))
(defun prompt-for-accept-1 (stream type
&key
(default nil defaultp)
(default-type type)
(insert-default nil)
(prompt t)
(prompt-mode :normal)
(display-default prompt)
&allow-other-keys)
(flet ((display-using-mode (stream prompt default)
(ecase prompt-mode
(:normal
(if *recursive-accept-p*
(input-editor-format stream "(~A~@[[~A]~]) " prompt default)
(input-editor-format stream "~A~@[[~A]~]: " prompt default)))
(:raw
(input-editor-format stream "~A" prompt)))))
(let ((prompt-string (if (eq prompt t)
(format nil "~:[Enter ~;~]~A"
*recursive-accept-p*
(describe-presentation-type type nil nil))
prompt))
;; Don't display the default in the prompt if it is to be
;; inserted into the input stream.
(default-string (and defaultp
(not insert-default)
display-default
(present-to-string default default-type))))
(cond ((null prompt)
nil)
(t
(display-using-mode stream prompt-string default-string))))))
(defmethod prompt-for-accept ((stream #.*string-input-stream-class*)
type view
&rest other-args
&key &allow-other-keys)
(declare (ignore type view other-args))
nil)
;;; For ACCEPT-FROM-STRING, use this barebones input-editing-stream.
(defclass string-input-editing-stream (input-editing-stream fundamental-character-input-stream)
((input-buffer :accessor stream-input-buffer)
(insertion-pointer :accessor stream-insertion-pointer
:initform 0
:documentation "This is not used for anything at any point.")
(scan-pointer :accessor stream-scan-pointer
:initform 0
:documentation "This is not used for anything at any point."))
(:documentation "An implementation of the input-editing stream
protocol retrieving gestures from a provided string."))
(defmethod initialize-instance :after ((stream string-input-editing-stream)
&key (string (error "A string must be provided"))
(start 0) (end (length string))
&allow-other-keys)
(setf (stream-input-buffer stream)
(replace (make-array (- end start) :fill-pointer (- end start))
string :start2 start :end2 end)))
(defmethod stream-element-type ((stream string-input-editing-stream))
'character)
(defmethod close ((stream string-input-editing-stream) &key abort)
(declare (ignore abort)))
(defmethod stream-peek-char ((stream string-input-editing-stream))
(or (stream-read-gesture stream :peek-p t)
:eof))
(defmethod stream-read-char-no-hang ((stream string-input-editing-stream))
(if (> (stream-scan-pointer stream) (length (stream-input-buffer stream)))
:eof
(stream-read-gesture stream)))