forked from franzinc/clim2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp-utilities.lisp
1706 lines (1536 loc) · 63 KB
/
lisp-utilities.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; Syntax: ANSI-Common-Lisp; Package: CLIM-UTILS; Base: 10; Lowercase: Yes -*-
;; See the file LICENSE for the full license governing this code.
;;
(in-package :clim-utils)
;;;"Copyright (c) 1990, 1991, 1992 Symbolics, Inc. All rights reserved.
;;; Portions copyright (c) 1988, 1989, 1990 International Lisp Associates."
;;; Define useful tools that don't exist in vanilla CL.
(defvar *keyword-package* (find-package :keyword))
;;; FUNCTIONP doesn't do what we want and there isn't any other CL function
;;; that does.
(defun funcallable-p (thing)
(etypecase thing
(symbol (fboundp thing))
(function t)))
;;; ONCE-ONLY does the same thing as it does in zetalisp. I should have just
;;; lifted it from there but I am honest. Not only that but this one is
;;; written in Common Lisp. I feel a lot like bootstrapping, or maybe more
;;; like rebuilding Rome.
;; Variables can have &ENVIRONMENT ENV in it
(defmacro once-only (variables &body body)
(let ((gensym-var (gensym))
(run-time-vars (gensym))
(run-time-vals (gensym))
(expand-time-val-forms ()))
(multiple-value-bind (variables #+(or Genera Minima) environment-var)
(decode-once-only-arguments variables)
(dolist (var variables)
(push `(if (or (constantp ,var #+(or Genera Minima) ,environment-var)
(symbolp ,var))
,var
(let ((,gensym-var (gensym)))
(push ,gensym-var ,run-time-vars)
(push ,var ,run-time-vals)
,gensym-var))
expand-time-val-forms))
`(let* (,run-time-vars
,run-time-vals
(wrapped-body
(let ,(mapcar #'list variables (reverse expand-time-val-forms))
,@body)))
`(let ,(mapcar #'list (reverse ,run-time-vars)
(reverse ,run-time-vals))
,wrapped-body)))))
(eval-when (compile load eval)
(defun decode-once-only-arguments (variables)
(let ((vars nil)
(env nil)
(vl variables))
(loop
(when (null vl)
(return-from decode-once-only-arguments
(values (nreverse vars) env)))
(let ((var (pop vl)))
(if (eq var '&environment)
(setq env (pop vl))
(push var vars))))))
) ;eval-when
(defmacro dorest ((var list &optional (by 'cdr)) &body body)
`(do ((,var ,list (,by ,var)))
((null ,var) nil)
,@body))
;; Why is there DOLIST in CL but no DOVECTOR or DOSEQ{uence}
;; rfe5546 - evaluate the from-end argument at run-time
;; This is a deviation from the old definition which evaluated from-end
;; (twice) at compile-time.
;; All CLIM uses are compile-time constants.
;; Any non-contant usage would have generaed a compiler error or very
;; incorrect code.
(defmacro dovector ((var vector &key (start 0) end from-end simple-p)
&body body)
(unless (constantp simple-p)
(setq simple-p nil)
(warn "SIMPLE-P should be a constant, ignoring it"))
(when (and simple-p (null end))
(warn "When SIMPLE-P is T, you must supply :END"))
(let ((fvector '#:vector)
(startd '#:start)
(endd '#:end)
(limit '#:limit)
(fromend '#:from-end)
(variable (if (atom var) var (first var)))
(index (if (atom var) '#:index (second var)))
(aref (if simple-p 'svref 'aref)))
`(block nil
(let* ((,fvector ,vector)
(,startd ,start)
(,endd ,(if simple-p
`,end
`(or ,end (length ,fvector))))
(,fromend ,from-end)
(,index (if ,fromend (1- ,endd) ,startd))
(,limit (if ,fromend (1- ,startd) ,endd)))
(declare (type fixnum ,endd ,index ,limit)
;; Turn on the afterburners...
(optimize (speed 3) (safety 0))
,(if simple-p
`(type simple-vector ,fvector)
`(type vector ,fvector)))
(loop
(when (= ,index ,limit) (return))
(let ((,variable (,aref ,fvector ,index)))
,@body)
(if ,fromend (decf ,index) (incf ,index)))))))
;;The old definition
#+ignore
(defmacro dovector ((var vector &key (start 0) end from-end simple-p) &body body)
(unless (constantp simple-p)
(setq simple-p nil)
(warn "SIMPLE-P should be a constant, ignoring it"))
(when (and simple-p (null end))
(warn "When SIMPLE-P is T, you must supply :END"))
(let ((fvector '#:vector)
(startd '#:start)
(endd '#:end)
(limit '#:limit)
(variable (if (atom var) var (first var)))
(index (if (atom var) '#:index (second var)))
(aref (if simple-p 'svref 'aref)))
`(block nil
(let* ((,fvector ,vector)
(,startd ,start)
(,endd ,(if simple-p `,end `(or ,end (length ,fvector))))
(,index (if ,from-end (1- ,endd) ,startd))
(,limit (if ,from-end (1- ,startd) ,endd)))
(declare (type fixnum ,endd ,index ,limit)
;; Turn on the afterburners...
(optimize (speed 3) (safety 0))
,(if simple-p
`(type simple-vector ,fvector)
`(type vector ,fvector)))
(loop
(when (= ,index ,limit) (return))
(let ((,variable (,aref ,fvector ,index)))
,@body)
(,(if from-end 'decf 'incf) ,index))))))
(defmacro doseq ((var sequence) &body body)
(let ((fcn (gensymbol 'doseq)))
`(flet ((,fcn (,var) ,@body))
(etypecase ,sequence
(list (dolist (thing ,sequence) (,fcn thing)))
(vector (dovector (thing ,sequence) (,fcn thing)))))))
;;; Stuff for dealing with CLIM coordinates
(deftype coordinate ()
#+use-float-coordinates 'single-float
#+use-fixnum-coordinates 'fixnum
#-(or use-float-coordinates use-fixnum-coordinates) 't)
;; Convert a number of arbitrary type into a COORDINATE
(defmacro coordinate (x &optional (round-direction 'round))
#-use-fixnum-coordinates (declare (ignore round-direction))
#+use-float-coordinates `(the coordinate (float ,x 0f0))
#+use-fixnum-coordinates (ecase round-direction
(round
#+(or allegro Lucid) `(the fixnum (round ,x))
#-(or allegro Lucid) `(the fixnum (values (floor (+ ,x .5f0)))))
(floor
`(the fixnum (values (floor ,x))))
(ceiling
`(the fixnum (values (ceiling ,x)))))
#-(or use-float-coordinates use-fixnum-coordinates) `,x)
#-(or aclpc acl86win32)
(defconstant +largest-coordinate+
#+use-float-coordinates (float (expt 10 (floor (log most-positive-fixnum 10))) 0f0)
#+use-fixnum-coordinates most-positive-fixnum
#-(or use-float-coordinates use-fixnum-coordinates) most-positive-fixnum)
#+(or aclpc acl86win32)
(defconstant +largest-coordinate+
#+use-float-coordinates (float (* 3 (expt 10 (floor (log most-positive-fixnum 10)))) 0f0)
#+use-fixnum-coordinates (* 3 (expt 10 (floor (log most-positive-fixnum 10))))
#-(or use-float-coordinates use-fixnum-coordinates)
(* 3 (expt 10 (floor (log most-positive-fixnum 10)))))
(defmacro integerize-single-float-coordinate (coord)
`(the fixnum (values (floor (+ (the single-float ,coord) .5f0)))))
(defmacro integerize-double-float-coordinate (coord)
`(the fixnum (values (floor (+ (the double-float ,coord) .5d0)))))
(defmacro integerize-float-coordinate (coord)
`(the fixnum (values (floor (+ (the float ,coord) .5)))))
#+(or Genera Minima)
(defun-inline fix-coordinate (coord)
(if (typep coord 'fixnum)
coord
(the fixnum (values (floor (+ coord .5f0))))))
#+allegro
(defmacro fix-coordinate (coord)
`(the fixnum (if (excl:fixnump ,coord) ,coord (fixit ,coord))))
#+allegro
(defun fixit (coord)
(declare (optimize (speed 3) (safety 0)))
(typecase coord
(fixnum
coord)
(single-float
;; note we use [x+0.5] rather than ROUND because
;; (1+ (round 1.5)) != (round 2.5) (cim 8/6/94)
(values (the fixnum (floor (+ (the single-float coord) .5f0)))))
(double-float
(values (the fixnum (floor (+ (the double-float coord) .5f0)))))
(t
(values (floor (+ coord .5f0))))))
#-(or Genera Minima allegro)
(defun fix-coordinate (coord)
(etypecase coord
(fixnum coord)
(single-float
(integerize-single-float-coordinate coord))
(double-float
(integerize-double-float-coordinate coord))
(float
(integerize-float-coordinate coord))
(ratio
(values (floor (+ coord 1/2))))
;; disallow bignums and other types of numbers
))
(defmacro fix-coordinates (&body coords)
`(progn
,@(mapcar #'(lambda (x) `(setq ,x (fix-coordinate ,x)))
coords)))
;; Assume that the value is a fixnum and that the result is a fixnum.
(defmacro fast-abs (int)
(let ((value '#:value))
`(let ((,value ,int))
(declare (fixnum ,value))
(the fixnum (if (< ,value 0) (the fixnum (- 0 ,value)) ,value)))))
;; COORDINATE-PAIRS is a list of pairs of coordinates.
;; The coordinates must be of type COORDINATE
(defmacro translate-coordinates (x-delta y-delta &body coordinate-pairs)
(once-only (x-delta y-delta)
`(progn
,@(let ((forms nil))
(dorest (pts coordinate-pairs cddr)
(push `(setf ,(first pts)
(the coordinate (+ ,(first pts) ,x-delta))) forms)
(push `(setf ,(second pts)
(the coordinate (+ ,(second pts) ,y-delta))) forms))
(nreverse forms)))))
;; Warning: this macro evaluates its position arguments multiple times
(defmacro convert-to-device-coordinates (transform &body positions)
(assert (evenp (length positions)) (positions)
"There must be an even number of elements in ~S" positions)
(assert (every #'symbolp positions) (positions)
"Each of the positions must be a variable in ~S" positions)
(let ((forms nil))
(loop
(when (null positions)
(return `(progn ,@(nreverse forms))))
(let* ((x (pop positions))
(y (pop positions)))
(push `(progn
(multiple-value-setq (,x ,y)
(transform-position ,transform ,x ,y))
(fix-coordinates ,x ,y))
forms)))))
;; Warning: this macro evaluates its position arguments multiple times
(defmacro convert-to-device-distances (transform &body distances)
(assert (evenp (length distances)) (distances)
"There must be an even number of elements in ~S" distances)
(assert (every #'symbolp distances) (distances)
"Each of the distances must be a variable in ~S" distances)
(let ((forms nil))
(loop
(when (null distances)
(return `(progn ,@(nreverse forms))))
(let* ((x (pop distances))
(y (pop distances)))
(push `(progn
(multiple-value-setq (,x ,y)
(transform-distance ,transform ,x ,y))
(fix-coordinates ,x ,y))
forms)))))
;;; Characters that are ordinary text rather than potential input editor commands.
;;; Note that GRAPHIC-CHAR-P is true of #\Space
(defun ordinary-char-p (char)
(and (eql char (code-char (char-code char))) ; false for #\control-c
(or (graphic-char-p char)
;; For characters, CHAR= and EQL are the same. Not true of EQ!
(eql char #\Newline)
(eql char #\Return)
(eql char #\Tab))))
(defun whitespace-char-p (char)
(and (characterp char)
(or (char-equal char #\Space)
(eql char #\Tab))))
;;; Make sure we don't get screwed by environments like Coral's that
;;; have *print-case* set to :downcase by default.
#+(or (not ansi-90) aclpc)
(defvar *standard-io-environment-val-cache* nil)
#+(or (not ansi-90) aclpc)
(defun standard-io-environment-vars-and-vals ()
(unless *standard-io-environment-val-cache*
(setq *standard-io-environment-val-cache*
(list 10 ;*read-base*
(copy-readtable nil) ;*readtable*
(find-package :user) ;*package*
t ;*print-escape*
nil ;*print-pretty*
nil ;*print-radix*
10 ;*print-base*
nil ;*print-circle*
nil ;*print-level*
nil ;*print-length*
:upcase ;*print-case*
t ;*print-gensym*
t ;*print-array*
nil))) ;*read-suppress*
(values
'(*read-base* *readtable* *package* *print-escape* *print-pretty*
*print-radix* *print-base* *print-circle* *print-level* *print-length*
*print-case* *print-gensym* *print-array* *read-suppress*)
*standard-io-environment-val-cache*))
(defmacro with-standard-io-environment (&body body)
#+(or (not ansi-90) aclpc)
`(multiple-value-bind (vars vals)
(standard-io-environment-vars-and-vals)
(progv vars vals
,@body))
#-(or (not ansi-90) aclpc)
`(with-standard-io-syntax ,@body))
#+(and (or (not ansi-90) aclpc) (not Genera))
(defmacro with-standard-io-syntax (&body body)
`(with-standard-io-environment ,@body))
;; Define this so we don't have to deal with stupid warnings about
;; the iteration variable being used, or not used, or what not
(defmacro repeat (n &body body)
(let ((i '#:i))
`(dotimes (,i ,n)
#-(or Minima Genera allegro) (declare (ignore i))
,@body)))
;;; Have to provide CLIM-LISP:WITH-OPEN-STREAM, because it needs to use a different
;;; version of CLOSE. We need this for printer streams, at least.
;;; This is a little more complicated than it absolutely needs to be. The idea is
;;; that there should be as short a timing window during which the stream to be closed
;;; is open but we don't have our hands on it to pass to CLOSE. We therefore don't
;;; want to bind the user's variable to the stream outside of the unwind-protect, but
;;; rather want to bind it inside. The reason we need the temporary variable is
;;; because the user might declare the stream variable to be of type STREAM, which
;;; would not be true during the brief interval after the binding and before the
;;; setting of that variable. I believe this implementation of WITH-OPEN-STREAM to
;;; have as small a timing window, and to be as semantically correct as possible.
#+(or (not clim-uses-lisp-stream-functions) ;Do this if we provide CLOSE function
Genera ; Sigh. CLOSE also shadowed for Genera.
CCL-2) ; Sigh. CLOSE also shadowed for CCL-2.
(defmacro with-open-stream ((stream-variable construction-form) &body body &environment env)
(let ((aborted-variable (gensymbol 'aborted-p))
(temporary-stream-variable (gensymbol 'stream)))
(multiple-value-bind (documentation declarations actual-body)
(extract-declarations body env)
(declare (ignore documentation))
`(let (,temporary-stream-variable
(,aborted-variable t))
(unwind-protect
(multiple-value-prog1
(progn (setq ,temporary-stream-variable ,construction-form)
(let ((,stream-variable ,temporary-stream-variable))
,@declarations
,@actual-body))
(setf ,aborted-variable nil))
(when ,temporary-stream-variable
(close ,temporary-stream-variable :abort ,aborted-variable)))))))
(defun follow-synonym-stream (stream)
#+Genera (si:follow-syn-stream stream)
#+(and ansi-90 (not Genera)) (typecase stream
(synonym-stream
(symbol-value (synonym-stream-symbol stream)))
(t stream))
#-(or Genera ansi-90) stream)
#-(or Genera ansi-90)
(eval-when (compile)
(warn "You haven't defined ~S for this implementation. A stub has been provided."
'follow-synonym-stream))
;;; Interning. There are three places where the package matters here:
;;; the current package, the package that is current when FORMAT is
;;; called (matters for ~S &c), and the package into which the symbol
;;; is interned. The aim is that the first two should always be the
;;; current packag, but you get to choose where the symbol is interned
;;; for PACKAGE-FINTERN.
;;;
;;; I am not sure if this stuff is really worth the cost, but...
;;; spr24505, carefully extract symbol-names, so
;;; case-sensitity works better.
(defun package-fintern (package format-string &rest format-args)
;; this argument order is unfortunate.
(declare (dynamic-extent format-args))
(intern (let ((pkg *package*))
(with-standard-io-environment
(let ((*package* pkg))
(apply #'lisp:format () format-string
(mapcar #'(lambda (x)
(excl::if* (symbolp x)
then (symbol-name x)
else x))
format-args)))))
package))
(defun fintern (format-string &rest format-args)
(declare (dynamic-extent format-args))
(apply #'package-fintern *package* format-string format-args))
(defvar *gensymbol* 0)
(eval-when (compile load eval)
(defun gensymbol (&rest parts)
(declare (dynamic-extent parts))
(when (null parts) (setf parts '(gensymbol)))
(make-symbol (lisp:format nil "~{~A-~}~D" parts (incf *gensymbol*))))
) ;eval-when
;;; For macro writers; you can have your GENSYMBOLs start at 1. Use
;;; this in the macro, not in its expansion...
(defmacro with-related-gensymbols (&body body)
`(let ((*gensymbol* 0))
,@body))
;; Used in generating internal function and method names.
;; (remove-word-from-string "com-" 'com-show-file) => "SHOW-FILE"
;; Always returns a new string that can be bashed to your heart's content
(defun remove-word-from-string (word string-or-symbol &optional only-from-beginning-p)
(let ((string (etypecase string-or-symbol
(string string-or-symbol)
(symbol (string string-or-symbol)))))
(let ((word-position (search word string :test #'char-equal)))
(cond ((null word-position)
(concatenate 'string string))
((zerop word-position)
(subseq string (length word)))
(only-from-beginning-p
(concatenate 'string string))
(t
(concatenate 'string
(subseq string 0 word-position)
(subseq string (+ word-position (length word)))))))))
;;; Why PUSHNEW doesn't do this is beyond me.
(defmacro push-unique (item reference &rest args &key test test-not key)
(declare (ignore test test-not))
`(let* ((evaled-item ,item)
(evaled-reference ,reference)
(element (find ,(if key `(funcall ,key evaled-item) 'evaled-item)
evaled-reference ,@args)))
(setf ,reference
(if element
(substitute evaled-item element evaled-reference)
(cons evaled-item evaled-reference)))))
(defmacro catch-if (condition tag &body body)
`(catch (if ,condition ,tag '#:tag-for-catch-if)
,@body))
;#+Genera
;(defmacro letf-globally (places-and-vals &body body)
; `(sys:letf* ,places-and-vals ,@body))
;
;#-Genera
;;; can't hack return-from in macro for aclpc +++pr
(defmacro letf-globally (places-and-vals &body body)
;; I don't want to use LETF-globally, mind you, but I can't easily implement
;; LETF{-not-globally} without something like sys:%bind-location.
;; Of course, this one is really LETF*-GLOBALLY, but don't tell anyone.
;; A minor optimization: when you bind something to itself or don't
;; say what to bind it to, it doesn't get SETF'd, since it isn't
;; being changed.
(if (null places-and-vals)
`(progn ,@body)
(let ((let-forms nil)
(set-forms nil)
(unwind-forms nil))
;; remember that we can't use SCL:LOOP
(map nil #'(lambda (place-and-val)
(let* ((place (pop place-and-val))
(val-p (not (null place-and-val)))
(val (and val-p (pop place-and-val)))
(temp-var (gensymbol 'letf-globally-temp)))
(when (and val-p (equal place val)) (setf val-p nil)) ;bind to itself?
(push `(,temp-var ,place) let-forms)
(when val-p (push place set-forms) (push val set-forms))
(push temp-var unwind-forms) (push place unwind-forms)))
places-and-vals)
`(let ,(nreverse let-forms)
(unwind-protect
(progn (setf ,@(nreverse set-forms)) ,@body)
(setf ,@unwind-forms)))))) ;Undo backwards.
(defmacro letf-globally-if (condition places-and-vals &body body)
#+Genera (declare (zwei:indentation 1 4 2 1))
(when (null places-and-vals)
(return-from letf-globally-if `(progn ,@body)))
(let ((let-forms nil)
(set-forms nil)
(unwind-forms nil)
(condition-value (gensymbol 'condition)))
(map nil #'(lambda (place-and-val)
(let* ((place (pop place-and-val))
(val-p (not (null place-and-val)))
(val (and val-p (pop place-and-val)))
(temp-var (gensymbol 'letf-globally-temp)))
(when (and val-p (equal place val)) (setf val-p nil))
(push `(,temp-var (and ,condition-value ,place)) let-forms)
(when val-p (push place set-forms) (push val set-forms))
(push temp-var unwind-forms) (push place unwind-forms)))
places-and-vals)
`(let ((,condition-value ,condition))
(let ,(nreverse let-forms)
(unwind-protect
(progn (when ,condition-value (setf ,@(nreverse set-forms)))
,@body)
(when ,condition-value (setf ,@unwind-forms)))))))
#-(and ansi-90 (not allegro) (not Symbolics))
(eval-when (compile load eval)
(proclaim '(declaration non-dynamic-extent)))
#+aclpc
(eval-when (compile load eval)
(proclaim '(declaration non-dynamic-extent ignorable)))
#+(and ansi-90 (not allegro) (not aclpc) (not Symbolics))
(define-declaration non-dynamic-extent (spec env)
(let ((vars (rest spec))
(result nil))
(dolist (v vars)
(block process-var
(multiple-value-bind (type local info)
(variable-information v env)
(declare (ignore local))
(case type
(:lexical
(when (cdr (assoc 'dynamic-extent info))
(warn "The variable ~S has been declared ~S,~%it cannot be now declared ~S"
v 'dynamic-extent 'non-dynamic-extent)
(return-from process-var))
(when (cdr (assoc 'ignore info))
(warn "The variable ~S has been declared ~S,~%it cannot be now declared ~S"
v 'ignore 'non-dynamic-extent)
(return-from process-var))
(push `(,v dynamic-extent nil) result))
(otherwise
(warn "~S is not a lexical variable, it cannot be declared ~S."
v 'non-dynamic-extent))))))
(values :variable (nreverse result))))
(defun time-elapsed-p (delta-seconds start-time)
(let ((internal-units (* delta-seconds internal-time-units-per-second))
(internal-time (get-internal-real-time)))
;; don't worry about the clock wrapping around
(> (- internal-time start-time) internal-units)))
(defmacro with-timeout-predicate (((predicate-fun old-predicate)
(timeout-var timeout-val)) &body body)
(let ((start-time (gensymbol "START-TIME"))
(delta-time (gensymbol "DELTA-TIME")))
`(let* ((,delta-time ,timeout-val) ;; Eval it only once...
(,start-time (when ,delta-time
(get-internal-time-units-per-second)))
(,timeout-var nil))
(flet ((,predicate-fun ()
(cond ((,old-predicate)
t)
((and ,delta-time
(time-elapsed-p ,delta-time ,start-time))
(setq ,timeout-var t)
t)
(t nil))))
,@body))))
;;; Bindings on the stack. A work in progress.
;;; Which Lisps support this?
;; I suppose this could be done with IMPORT
#+Genera
(progn
(defmacro with-stack-list ((var &rest elements) &body body)
`(scl:with-stack-list (,var ,@elements) ,@body))
(defmacro with-stack-list* ((var &rest elements) &body body)
`(scl:with-stack-list* (,var ,@elements) ,@body))
(defun-inline evacuate-list (list)
(if (and (sys:%pointerp list)
(not (or (sys:%pointer-lessp list sys:%control-stack-low)
(sys:%pointer-lessp (progn #+3600 sys:%control-stack-limit
#+imach (sys:%read-internal-register
sys:%register-control-stack-limit))
list))))
(copy-list list)
list))
) ;#+Genera
#+Cloe-Runtime
(progn
(defmacro with-stack-list ((var &rest elements) &body body)
`(sys::with-stack-list (,var ,@elements) ,@body))
(defmacro with-stack-list* ((var &rest elements) &body body)
`(sys::with-stack-list* (,var ,@elements) ,@body))
#+Cloe-Runtime
(defun-inline evacuate-list (list)
(if (logtest (the fixnum (sys::gcltype list)) sys::lo$k-_astack)
(copy-list list)
list))
) ;#+Cloe-Runtime
#+allegro
(progn
(defmacro with-stack-list ((var &rest elements) &body body)
`(let ((,var (list ,@elements)))
(declare (dynamic-extent ,var))
,@body))
(defmacro with-stack-list* ((var &rest elements) &body body)
`(let ((,var (list* ,@elements)))
(declare (dynamic-extent ,var))
,@body))
(defun-inline evacuate-list (list)
(if (and (consp list)
(excl::stack-allocated-p list))
(copy-list list)
list))
) ;#+Allegro
#+CCL-2
(progn
(defmacro with-stack-list ((var &rest elements) &body body)
`(let ((,var (list ,@elements)))
(declare (dynamic-extent ,var))
,@body))
(defmacro with-stack-list* ((var &rest elements) &body body)
`(let ((,var (list* ,@elements)))
(declare (dynamic-extent ,var))
,@body))
(defun evacuate-list (list)
;;--- Dunno if this is the right function to be calling
;;--- but it seems to give the right answers.
(cond ((and (ccl::stack-area-endptr list)
(listp list))
(copy-list list))
(t list)))
) ;#+CCL-2
#-(or Genera Cloe-Runtime allegro CCL-2)
(progn
(defmacro with-stack-list ((var &rest elements) &body body)
`(let ((,var (list ,@elements)))
,@body))
(defmacro with-stack-list* ((var &rest elements) &body body)
`(let ((,var (list* ,@elements)))
,@body))
;; Since with-stack-list does nothing, this doesn't either.
;; When stack-consing works for non-Genera/Cloe, make this do something.
(defmacro evacuate-list (list)
`,list)
) ;#-(or Genera Cloe-Runtime allegro)
#+Genera
(defmacro with-stack-array ((name size &rest options) &body body)
`(sys:with-stack-array (,name ,size ,@options) ,@body))
#-Genera
(defmacro with-stack-array ((name size &rest options) &body body)
`(let ((,name (make-stack-array ,size ,@options)))
,@body))
#-Genera ;in case anybody wants to implement this...
(defun-inline make-stack-array (size &rest options)
(declare (dynamic-extent options))
(apply #'make-array size options))
#+(or Genera Cloe-Runtime)
(defmacro with-keywords-removed ((new-list list keywords-to-remove) &body body)
(declare (zwei:indentation 0 3 1 1))
`(si::with-rem-keywords (,new-list ,list ,keywords-to-remove)
,@body))
#+(or Genera Cloe-Runtime)
(defun remove-keywords (list keywords-to-remove)
(si::rem-keywords list keywords-to-remove))
#-(or Genera Cloe-Runtime)
(progn
(defmacro with-keywords-removed ((new-list list keywords-to-remove) &body body)
`(let ((,new-list (remove-keywords ,list ,keywords-to-remove)))
,@body))
(defun remove-keywords (list keywords)
(macrolet ((remove-keywords-1 (name-var predicate-form)
`(let ((head nil)
(tail nil))
(do ()
((null list))
(let ((,name-var (pop list))
(value (pop list)))
(unless ,predicate-form
(setq tail (setq head (list ,name-var value)))
(return))))
(do ()
((null list) head)
(let ((,name-var (pop list))
(value (pop list)))
(unless ,predicate-form
(setq tail (setf (cddr tail) (list ,name-var value)))))))))
(cond ((null list) nil)
((null keywords) list)
;; Special case: use EQ instead of MEMBER when only one keyword is supplied.
((null (cdr keywords))
(let ((keyword (car keywords)))
(remove-keywords-1 name (eq name keyword))))
(t
(remove-keywords-1 name (member name keywords))))))
) ;#-(or Genera Cloe-Runtime)
;;; Generate a list of keyword information for a given type of keyword.
;;; This is a list of lists of the form:
;;; (name init-value keyword supplied-p-var &optional accessor-name)
;;; We also put values for the TYPE property of the keywords according
;;; to the incrementor so we can detect these keywords quickly at
;;; runtime. The value on the property is either some fixed value, an
;;; integer which increments once per keyword, or a member of a set. We
;;; currently use this facility for defining drawing-state keywords and
;;; transform keywords. See the files GRAPHICS-TRANSFORM-MIXIN and
;;; DRAWING-STATE-MIXIN for how it's used.
(defmacro define-keywords (name type (incrementor start) accessor-prefix
&body names-and-values &aux (offset 0))
(flet ((counter-incrementor () (incf offset))
(member-incrementor () (pop start))
(value-incrementor () start))
(let ((keyword-stuff (mapcar #'(lambda (n&v)
`(,(first n&v) ,(second n&v)
,(intern (string (first n&v)) *keyword-package*)
,(gensymbol (first n&v) 'p)
,@(when accessor-prefix
`(,(fintern "~A-~A" accessor-prefix (first n&v))))))
names-and-values))
(incrementor (ecase incrementor
(counter #'counter-incrementor)
(member #'member-incrementor)
(value #'value-incrementor))))
`(progn (defparameter ,name ',keyword-stuff)
,@(when type
(mapcar #'(lambda (k)
`(setf (get ',(third k) ',type) ,(funcall incrementor)))
keyword-stuff))))))
(defmacro writing-clauses (&body body)
(let ((clauses-var (gensymbol 'clauses)))
`(let ((,clauses-var nil))
(macrolet ((clause (clause)
`(push ,clause ,',clauses-var)))
,@body)
(nreverse ,clauses-var))))
;;; Arglist tools
;; (flatten-arglist '(foo bar &optional baz (quux)
;; &rest mumble
;; &key frotz (trouble) ((:izzy the-cat))))
;; (FOO BAR &OPTIONAL BAZ QUUX &REST MUMBLE &KEY FROTZ TROUBLE IZZY)
;; make-pass-on-arglist is the only caller
(defun flatten-arglist (arglist)
(let ((new-arglist nil)
(mode :positional))
(dolist (arg-spec arglist)
(cond ((listp arg-spec)
(case mode
(&optional
(push (first arg-spec) new-arglist))
(&key
;; deal with "(... &key ((:keyword var) default))" syntax
(let ((thing (first arg-spec)))
(push (if (listp thing)
(let ((name (first thing)))
(if (eq (symbol-package name) *keyword-package*)
(intern (symbol-name name))
name))
thing)
new-arglist)))))
((member arg-spec '(&key &rest &optional))
(setq mode arg-spec)
(push arg-spec new-arglist))
(t (push arg-spec new-arglist))))
(nreverse new-arglist)))
;; (make-pass-on-arglist '(foo bar &optional baz (quux)
;; &rest mumble
;; &key frotz (trouble) ((:izzy the-cat))))
;; (FOO BAR BAZ QUUX MUMBLE :FROTZ FROTZ :TROUBLE TROUBLE ':IZZY THE-CAT)
;; --- It looks like &REST and &KEY don't get along well here. A big
;; question in such a circumstane is who should be doing the defaulting?
;; I.e., should this do the defaulting, by making the above arglist be
;; :FROTZ FROTZ :TROUBLE TROUBLE ':IZZY THE-CAT MUMBLE
;; for apply, or should it let the eventual caller do the defaulting by
;; punting the defaulting here and just doing
;; MUMBLE
;; for apply? There are also interactions with &allow-other-keys that
;; aren't done here. If this is for pass-on, then &allow-other-keys
;; implies an &rest beforehand, otherwise it is impossible to get all
;; the keys that are being passed on. There's also the question of
;; whether &allow-other-keys here should put :allow-other-keys in the
;; passed on call list.
(defun make-pass-on-arglist (arglist)
(let ((new-arglist nil)
(fa (flatten-arglist arglist))
(mode :positional)
(apply-p nil))
(do ((args fa (cdr args))
(original-args arglist (cdr original-args)))
((null args) nil)
(let ((arg (first args))
(arg-spec (first original-args)))
(cond ((member arg '(&key &optional &rest))
(setq mode arg))
((eq arg '&allow-other-keys)
(unless (eq mode '&key)
(error "~&&ALLOW-OTHER-KEYS must follow &KEY")))
(t (case mode
(&key
(let ((arg-name arg) (arg-var arg))
(cond ((and (listp arg-spec)
(listp (first arg-spec)))
(setq arg-name `',(first (first arg-spec)))
(setq arg-var (second (first arg-spec))))
(t (setq arg-name (intern (symbol-name arg) *keyword-package*))))
(push arg-name new-arglist)
(push arg-var new-arglist)))
(&rest (setq apply-p t)
(push arg new-arglist))
(t (push arg new-arglist)))))))
(values
(nreverse new-arglist)
apply-p)))
(defun ignore-arglist (arglist)
(flet ((lambda-list-element-compare (element and-option)
(and (atom element)
(string= element and-option))))
`(progn ,@(let ((args nil))
(dolist (arg arglist)
;; These various &keys may be in some other package.
(cond ((member arg '(&rest &downward-rest &key &allow-other-keys &optional)
:test #'lambda-list-element-compare)
nil)
(t (push (cond ((atom arg) arg)
((atom (car arg)) (car arg))
(t (cadar arg)))
args)
(when (and (consp arg) (consp (cdr arg)) (consp (cddr arg)))
(push (third arg) args)))))
(nreverse args))
nil)))
(defun canonicalize-and-match-lambda-lists (canonical-order user-specified
&optional allow-user-keys)
(declare (values lambda-list ignores))
(check-type canonical-order list)
(check-type user-specified list)
(let* ((new-lambda-list nil)
(ignores nil)
(rest-pos (or (position '&rest user-specified) (length user-specified)))
(key-pos (or (position '&key user-specified) rest-pos))
(rest-and-key (nthcdr (min rest-pos key-pos) user-specified)))
(when allow-user-keys
(setq user-specified (subseq user-specified 0 (min rest-pos key-pos))))
(flet ((user-var-symbol (entry)
;; FOO | (FOO NIL) | ((:FOO BAR) NIL) | (FOO NIL FOO-P) | ((:FOO BAR) FOO-P)
;;--- We don't support the FOO-P syntax yet.
(cond ((atom entry)
entry)
((atom (setq entry (first entry)))
entry)
(t (second entry))))
(user-var-name (entry)
;; FOO | (FOO NIL) | ((:FOO BAR) NIL) | (FOO NIL FOO-P) | ((:FOO BAR) FOO-P)
;;--- We don't support the FOO-P syntax yet.
(cond ((atom entry)
entry)
((atom (setq entry (first entry)))
entry)
(t (first entry)))))
(declare (dynamic-extent #'user-var-symbol #'user-var-name))
(dolist (canonical-var canonical-order)
(let ((user-entry (first (member canonical-var user-specified
:test #'string-equal
:key #'user-var-name))))
(cond (user-entry
(push (user-var-symbol user-entry) new-lambda-list)
(setq user-specified (remove user-entry user-specified)))
(t (let ((canonical-gensym (get canonical-var 'canonical-gensym)))
(unless canonical-gensym
(setq canonical-gensym (make-symbol (symbol-name canonical-var)))
(setf (get canonical-var 'canonical-gensym) canonical-gensym))
(push canonical-gensym new-lambda-list)
(push canonical-gensym ignores))))))
(when (set-difference user-specified '(&key &allow-other-keys))
(error "The arguments ~S aren't valid for this lambda list."
user-specified))
(values (if allow-user-keys
(append (nreverse new-lambda-list) rest-and-key)
(nreverse new-lambda-list))
(nreverse ignores)))))
#+Genera
(defmacro defun-property ((symbol indicator) lambda-list &body body)
`(zl:::scl:defun (:property ,symbol ,indicator) ,lambda-list ,@body))
#-Genera
(defmacro defun-property ((symbol indicator) lambda-list &body body)
(let ((function-name
(make-symbol (lisp:format nil "~A-~A-~A" symbol indicator 'property))))
`(progn (defun ,function-name ,lambda-list ,@body)
(eval-when (load eval) (setf (get ',symbol ',indicator) #',function-name)))))
(defmacro do-delimited-substrings (((string &key (start 0) end)
(start-index-var end-index-var &optional char-var))
substring-form
&body char-clauses)
(let ((special-characters ())
(next-var (gensymbol 'next))
(string-var (if (symbolp string) string (gensymbol 'string)))
(end-var (gensymbol 'end))
(char-var (or char-var (gensymbol 'the-char))))
(dolist (char-clause char-clauses)
(let ((chars (first char-clause)))
(if (atom chars)
(push chars special-characters)
(setf special-characters (nconc special-characters chars)))))
`(,(if (eq string string-var) 'let 'let*)
((,start-index-var ,start)
,@(unless (eq string string-var)
`((,string-var ,string)))
(,end-var (or ,end (length ,string-var)))
(,end-index-var 0)
(,next-var nil)
,@(when (cdr special-characters)
`((,char-var ,(car special-characters)))))
(declare (type fixnum ,start-index-var ,end-var ,end-index-var)
(type (or fixnum null) ,next-var)
(character ,char-var))
(loop
(setf ,next-var ,(if (null (cdr special-characters))