-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.lisp
1526 lines (1355 loc) · 58.1 KB
/
macros.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
;;; :FILE-CREATED <Timestamp: #{2010-12-16T20:16:01-05:00Z}#{10504} - by MON>
;;; :FILE mon-systems/macros.lisp
;;; ==============================
(in-package #:mon)
;; sbcl/src/code/early-extensions.lisp
;; *profile-hash-cache*
;; define-hash-cache
;; define-cached-synonym
;; defun-cached
;;; list-circular-p
;;; ==============================
;;; :NOTE MACROEXPAND-1 and SB!INT:EVAL-IN-LEXENV are the only SBCL
;;; functions that get called with the constructed environment
;;; argument. sb-int:eval-in-lexenv punts to sb-int:simple-eval-in-lexenv unless
;;; sb-ext:*evaluator-mode* is :interpret (the default is :compile)
;;;
;;; (where-is "eval-in-lexenv") ;=> sb-int:eval-in-lexenv
;;; (where-is "*evaluator-mode*") ;=> sb-ext:*evaluator-mode*
;;; (where-is "simple-eval-in-lexenv") ;=> sb-int:simple-eval-in-lexenv
;;; (where-is "eval-in-native-environment") ;=> sb-eval:eval-in-native-environment
;;;
;;; :SEE :FILE sbcl/src/code/eval.lisp
;;; :SEE :FILE sbcl/src/code/full-eval.lisp
;;; ==============================
;;; ==============================
;;; :NOTE In SBCL 1.0.42.25 built from git there is a bug where the macro documentation can't be setf'd.
;;; :SEE :FILE BUG-SBCL-1-42-2010-09-20
;;; :SEE (URL `https://bugs.launchpad.net/sbcl/+bug/643958')
;;; Until bug 643958 is fixed we need the following:
;;; (in-package :SB-PCL)
;;; ==============================
;; Should be fixed now <Timestamp: #{2010-12-16T20:16:05-05:00Z}#{10504} - by MON>
;; (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'function)))
;; ;; (when (and (legal-fun-name-p x) (fboundp x))
;; (when (and (sb-pcl::legal-fun-name-p x) (fboundp x))
;; ;; :WAS (setf (documentation (symbol-function x) t) new-value)))
;; (setf (documentation (or (macro-function x) (symbol-function x)) t)
;; new-value)))
;;; ==============================
;;; :NOTE not a macro but might as well make sure it happens early.
(declaim (inline and-so))
(defun and-so (and-x &rest and-so)
(and and-x `(,and-x ,@and-so)))
;;; :SOURCE s-sql.lisp :NOTE sbcl/src/compiler/disassem.lisp has same as `strip-quote'
(declaim (inline dequote))
(defun dequote (val)
(declare ((or symbol cons) val))
(if (and (consp val) (eq (car val) 'quote))
(cadr val)
val))
;;; :SOURCE fare-utils-20100901-git/base/utils.lisp :WAS `eval-now'
(defmacro eval-when-all (&body body)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@body))
;;: :SOURCE clocc/src/cllib :WAS `el::eval-when-compile'
(defmacro eval-when-compile (&rest body)
`(eval-when (:compile-toplevel :load-toplevel) ,@body))
(defmacro w-debug-declared ()
;; :NOTE (info "(sbcl)Debugger Policy Control") says `compilation-speed` must
;; be 0 in order for source to be steppable, e.g.:
;; `> (max speed space compilation-speed)'
`'(declare (optimize (safety 3) (debug 3) (speed 0) (compilation-speed 0))))
(defmacro w-fast-declared ()
;; :NOTE Don't use (safety 0) on SBCL it can cause segv's!
`'(declare (optimize (speed 3) (safety 1) (compilation-speed 0) (space 0))))
;;; ==============================
;;; :WITH-STYLE-MACROS
;;; ==============================
;;; Now imports `alexandria/with-gensyms' from alexandria/macros.lisp
;; #-:sbcl
;; (defmacro with-gensyms (syms &body body)
;; `(let ,(mapcar #'(lambda (s)
;; `(,s (gensym)))
;; syms)
;; ,@body))
#-:sbcl
(defmacro with-gensyms (syms &body body)
`(alexandria:with-gensyms ,syms ,@body))
#+:sbcl
(defmacro with-gensyms (syms &body body)
`(sb-int:with-unique-names ,syms ,@body))
;;; :SOURCE mcclim/ESA/utils.lisp :WAS `retaining-value'
(defmacro retaining-value ((bound-symbol &optional initial-value) &body body)
(let ((symbol (gensym)))
`(progn (unless (boundp ',symbol)
(setf (symbol-value ',symbol) ,initial-value))
(let ((,bound-symbol (symbol-value ',symbol)))
(unwind-protect (progn ,@body)
(setf (symbol-value ',symbol) ,bound-symbol))))))
;;; :SOURCE GBBopen/source/tools/tools.lisp
(defmacro multiple-value-setf (places form)
(loop
for place in places
for name = (gensym)
collect name into bindings
if (eql 'nil place)
unless (eq place (first places))
collect `(declare (ignore ,name)) into ignores
end
else
collect `(setf ,place ,name) into body
finally (return `(multiple-value-bind ,bindings ,form
,@ignores
,@body
;; Return the primary value (like multiple-value-setq)
,(first bindings)))))
;; :NOTE The `with-gensyms' provided in mon-macros uses `with-unique-names'
;; which differs from the with-gensyms version provided in
;; :FILE CLOCC/cllib/port/ext.lisp
;; :SOURCE CLOCC/cllib/port/ext.lisp
(defmacro %compose (&rest functions)
(labels ((rec (xx yy)
(let ((rr (list (car xx) (if (cdr xx) (rec (cdr xx) yy) yy))))
(if (consp (car xx))
(cons 'funcall (if (eq (caar xx) 'quote)
(cons (cadar xx) (cdr rr)) rr))
rr))))
;; #-:sbcl (with-gensyms ("-COMPOSE-" arg)
(with-gensyms (arg)
`(lambda (,arg) ,(rec functions arg)))))
;;; ==============================
(defmacro if-not (test-form then &optional else)
`(if (not ,test-form)
,then
,else))
;;; ==============================
;;; :ANAPHORIC-STYLE-MACROS
;;; ==============================
;; :SOURCE alexandria/flow-control.lisp :WAS `if-bind'
(defmacro ref-bind (var test &body then/else)
(unless (car then/else)
(simple-error-mon :w-sym 'ref-bind
:w-type 'macro
:w-spec "arg then/else missing THEN clause."))
(destructuring-bind (then &optional else)
then/else
`(let ((,var ,test))
(if ,var ,then ,else))))
;; :WAS `aif'
(defmacro ref-it-if (test then &optional else)
`(let ((it ,test)) (if it ,then ,else)))
;;; ==============================
;;; :BINDING-MACROS
;;; ==============================
(defmacro bound-and-true-p (var)
`(and (boundp (quote ,var)) ,var))
;;
;;; :SOURCE lice/src/global.lisp
;;; :NOTE Probably sourced from CLOCC\cllib/port/ext.lisp
(defmacro defsubst (name lambda-list &body body)
`(progn
(declaim (inline ,name))
(defun ,name ,lambda-list ,@body)))
;;
;; :SOURCE CLOCC\cllib/port/ext.lisp
;; (defmacro defcustom (name type init doc)
;; "Define a typed global variable."
;; `(progn (declaim (type ,type ,name))
;; (defvar ,name (the ,type ,init) ,doc)))
;;; ==============================
;;; :NOTE This looses when TO-SYMBOL is redefined.
;;; (defmacro defalias (from-symbol to-symbol); &optional docstring)
;;; `(declare (function ,to-symbol))
;;; `(progn
;;; (intern (symbol-name ,from-symbol) (sb-ext::sane-package))
;;; (setf (fdefinition ,from-symbol) (fdefinition ,to-symbol))))
;;;
;;;-------------------------------------------
;;; This is how clocc/src/cllib approaches it:
;;; "defalias is a function in Emacs, cannot use defmacro a simple
;;; (setf fdefinition) is no good since Emacs defalias works on macros too"
;;; (defun defalias (symbol def)
;;; (eval `(defmacro ,symbol (&body body) (list* ',def body))))
;;;
;;;-------------------------------------------
;;; This is how Mariano Montone approaches it:
;;; :SOURCE gestalt/deps/util/util.lisp
;;; (eval-when (:compile-toplevel :load-toplevel :execute)
;;; (defmacro defalias (fname &rest aliases)
;;; (let ((setf-args '()))
;;; `(progn
;;; (setf
;;; ,@(loop for alias in aliases
;;; do (setf setf-args (nconc setf-args (list `(symbol-function ',alias) `(function ,fname))))
;;; finally (return setf-args)))
;;; ',aliases))))
;;; ==============================
;;
;; :PASTED (URL `http://paste.lisp.org/+2LDL/1')
;; <Timestamp: #{2011-03-30T16:15:04-04:00Z}#{11133} - by MON>
(defmacro defalias (dest-fun-symbol source-fun-symbol &optional docstring) ;; &environment env)
;; :NOTE the eval-when doesn't appear to be needed.
;;`(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((dest-ftype (gensym))
;; (cl-pkg (find-package "COMMON-LISP")))
(cl-pkg (find-package '#:common-lisp)))
`(let ;;((dest-ftype ,dest-ftype))
((,dest-ftype nil))
(block nil
(if (or
(and
(not (symbolp ,source-fun-symbol))
(setf ,dest-ftype '(not symbol)))
(and (or
(typep ,dest-fun-symbol 'boolean)
(typep ,source-fun-symbol 'boolean))
(setf ,dest-ftype 'boolean))
(and (special-operator-p ,dest-fun-symbol)
(setf ,dest-ftype 'special-operator))
(and (special-operator-p ,source-fun-symbol)
(setf ,dest-ftype 'special-operator))
(and (eq (symbol-package ,dest-fun-symbol) ,cl-pkg)
(setf ,dest-ftype '(symbol-package ,cl-pkg)))
(and (not (fboundp ,source-fun-symbol))
(setf ,dest-ftype '(not fbound))))
(return
(values
(warn "~%~3T:MACRO defalias ~
-- failed to satisfy one or more constraints~%~22T~
declining to set fdefinition of: ~S with: ~S~%"
,dest-fun-symbol ,source-fun-symbol)
,dest-ftype))
(and
(cond
((macro-function ,source-fun-symbol)
(setf (macro-function ,dest-fun-symbol) (macro-function ,source-fun-symbol))
(setf ,dest-ftype 'macro-function))
((functionp (,'function ,(dequote source-fun-symbol)))
(setf (symbol-function ,dest-fun-symbol) (symbol-function ,source-fun-symbol))
(setf ,dest-ftype 'symbol-function))
(t
(return
(values
(warn "~%~3T:MACRO defalias ~
-- failed to satisfy one or more constraints~%~22T~
declining to set fdefinition of: ~S with ~S~%"
,dest-fun-symbol ,source-fun-symbol)
(type-of ,source-fun-symbol)))))
(if (compiler-macro-function ,source-fun-symbol)
(progn
(setf (compiler-macro-function ,dest-fun-symbol) (compiler-macro-function ,source-fun-symbol))
(setf ,dest-ftype (list ,dest-ftype 'compiler-macro-function)))
t)
(if (and ,docstring (stringp ,docstring))
(or (fundoc ,dest-fun-symbol ,docstring) t)
t)
(return
(if (atom ,dest-ftype)
(values ,source-fun-symbol ,dest-ftype)
(values-list (adjoin ,source-fun-symbol ,dest-ftype))))))))))
;;; ==============================
;;; :TYPE-MACROS
;;; ==============================
;;; :TYPE-MACROS
;; :SOURCE sbcl/src/code/early-extensions.lisp :WAS `and/type'
(defmacro type-and (type-x type-y)
`(multiple-value-bind (val1 win1) ,type-x
(if (and (not val1) win1)
(values nil t)
(multiple-value-bind (val2 win2) ,type-y
(if (and val1 val2)
(values t t)
(values nil (and win2 (not val2))))))))
;; :SOURCE sbcl/src/code/early-extensions.lisp :WAS `not/type'
(defmacro type-not (type-checked)
(let ((val (gensym "VAL"))
(win (gensym "WIN")))
`(multiple-value-bind (,val ,win)
,type-checked
(if ,win
(values (not ,val) t)
(values nil nil)))))
;; :SOURCE cxml-20101107-git/test/domtest.lisp
(defmacro string-case (keyform &rest case-clauses)
(let ((key (gensym "KEY")))
;; Don't bother mapping when keyform is T|NIL
(when (typep keyform 'boolean) (return-from string-case nil))
(labels ((chk-keys (keys-seq ky)
(or (or (and (every #'stringp keys-seq)
`(find ,ky ',keys-seq :test 'string=))
(and (some #'booleanp keys-seq)
(let ((cpy (delete-if #'booleanp (copy-seq keys-seq))))
(every #'stringp cpy)
`(find ,ky ',cpy :test 'string=))))
(error 'type-error
:datum (elt keys-seq (position-if-not #'stringp keys-seq))
:expected-type 'string))))
`(let ((,key ,keyform))
(declare (ignorable ,key))
(cond
,@(loop
for (keys . forms) in case-clauses
for test = (etypecase keys
(string `(string= ,key ,keys))
(sequence (chk-keys keys key))
((eql t) t))
collect `(,test ,@forms) into cond-forms
finally (return (delete-if #'booleanp cond-forms :key #'car))))))))
;; :NOTE Paul Khuong's string-case is much faster when we know that
;; CASE-CLAUSES is always a list of pairs with car of each elt `stringp'
(defmacro string-case-fast ((keyform &key (default ''nil)) &body case-clauses)
;; Don't bother mapping when keyform is T|NIL
(when (typep keyform 'boolean) (return-from string-case-fast nil))
`(string-case:string-case (,keyform ,@(and default (list :default default)))
,@case-clauses))
;;; ==============================
;;; :NUMBERS-MACROS
;;; ==============================
;; :COURTESY cllib/math.lisp
(defmacro to-percent (vv)
`(* 100d0 (1- ,vv)))
;; :COURTESY cllib/withtype.lisp :WAS `dfloat'
(defmacro number-to-double-float (num)
`(float ,num 1d0))
(defmacro byte-octets-for-integer (unsigned-integer)
(declare (optimize (speed 3)))
`(etypecase (integer-length ,unsigned-integer)
,@(loop
for cnt upfrom 1 below 17
for x upfrom 0 below 128 by 8
for low = 0 then (+ x 1)
for high = 8 then (+ x 8)
collect (list (list 'integer low high) cnt))))
;; :SOURCE sbcl/src/code/run-program.lisp :WAS `round-bytes-to-words'
;; SB-IMPL::ROUND-BYTES-TO-WORDS
#+:sbcl
(defmacro bytes-round-to-words (n-bytes)
;; N-MACHINE-WORD-BITS the natural width of a machine word (as seen in e.g. register width,
;; address space)
;;
;; N-BYTE-BITS the number of bits per byte, where a byte is the smallest addressable object
;;
;; :WAS
;; (let ((bytes-per-word (/ sb-vm:n-machine-word-bits sb-vm:n-byte-bits)))
;; `(logandc2 (the fixnum (+ (the fixnum ,n-bytes)
;; (1- ,bytes-per-word))) (1- ,bytes-per-word))))
(with-gensyms (bytes-per-word per-word-less-1)
`(let* ((,bytes-per-word (/ sb-vm:n-machine-word-bits sb-vm:n-byte-bits)) ;; => (/ 32 8) on x86-32
(,per-word-less-1 (the fixnum (1- ,bytes-per-word))))
(declare (fixnum ,per-word-less-1))
(logandc2 (the fixnum
(+
;; This declaration is faulty b/c N-BYTES could be a value
;; somewhere greater than (- most-positive-fixnum
;; per-word-less-1) Although the likelihood of this occuring
;; is less than realistic...
(the (integer 0 ,most-positive-fixnum) ,n-bytes)
,per-word-less-1)) ,per-word-less-1))))
;;; ==============================
;;; :STRING-MACROS
;;; ==============================
;; :COURTESY babel-20100901-darcs/src/strings.lisp
(defmacro string-get (string index)
`(char-code (char ,string ,index)))
;; :COURTESY babel-20100901-darcs/src/strings.lisp
(defmacro string-set (code string index)
`(setf (char ,string ,index)
(code-char ,code)))
;;; :SOURCE texinfo-docstrings/colorize.lisp
(defmacro string-append-into (output-sym &rest args)
`(and (or (and (null ,output-sym)
(setq ,output-sym ""))
t)
(setq ,output-sym (concatenate (quote string) ,output-sym ,@args))))
;; :SOURCE Sonya Keene p 183 :WAS `standardize-output-stream-var'
;; :WAS (defmacro standardize-output-stream-var (stream)
;; `(setf ,stream (cond
;; ((eq ,stream t) *terminal-io*)
;; ((null ,stream) *standard-output*)
;; (t ,stream))))
;;
(defmacro output-stream-normalize (stream)
(let ((nrmlz (gensym)))
`(let ((,nrmlz ,stream))
(setf ,nrmlz (cond
((eql ,nrmlz t) *terminal-io*)
((null ,nrmlz) *standard-output*)
(t ,nrmlz))))))
;;; ==============================
;;; :CREATED <Timestamp: #{2010-09-20T17:45:16-04:00Z}#{10381} - by MON>
;;; :COURTESY Geoff Summerhayes comp.lang.lisp :WAS `sift-list'
;;; :DATE Tue, 21 May 2002 18:41:19 GMT
;;; :SUBJECT Re: Stumped (basic LISP question)
;;;
;;; :ELISP-VERSION
;;; (defmacro list-sift (sift-list &rest sift-tests)
;;; (let ((collectors (mon-mapcar #'(lambda (genx) (edebug-gensym "--mon-sift-cllct--")) sift-tests))
;;; (sft-last (edebug-gensym "--mon-sift-last--")))
;;; `(let (,@collectors ,sft-last)
;;; (dolist (sft-itm ,sift-list)
;;; (cond ,@(mon-mapcar #'(lambda (sft-x sft-y)
;;; `((funcall ,sft-x sft-itm) (push sft-itm ,sft-y)))
;;; sift-tests collectors)
;;; (t (push sft-itm ,sft-last))))
;;; (list ,@collectors ,sft-last)))
;;;
;;; Common Lisp version, note the functional `values' in the tail:
(defmacro list-sift (sift-list &rest sift-tests)
(let ((collect-sftd (mapcar #'(lambda (x) (declare (ignore x))
(gensym)) sift-tests))
(sft-last (gensym)))
`(let (,@collect-sftd ,sft-last)
(dolist (sft-itm ,sift-list)
(cond ,@(mapcar #'(lambda (sft-x sft-y)
`((funcall ,sft-x sft-itm) (push sft-itm ,sft-y)))
sift-tests collect-sftd)
(t (push sft-itm ,sft-last))))
(values ,@collect-sftd ,sft-last))))
;; :WAS (define-modify-macro sortf (place &rest args) sort args)
(define-modify-macro sortf (place &rest args) sort
"Modify macro for CL:SORT. Destructively sort SEQUENCE. PREDICATE should return non-NIL if ARG1 is to precede ARG2.")
;; (define-modify-macro stable-sortf (place &rest args) stable-sort args)
(define-modify-macro stable-sortf (place &rest args) stable-sort
"Destructively sort SEQUENCE. PREDICATE should return non-NIL if ARG1 is to precede ARG2.")
;;; ==============================
(defmacro popn (n-elts place)
(multiple-value-bind (vars forms var set access)
(get-setf-expansion place) ; LMH ANSI CL changed name
(with-gensyms (gn glst)
`(let* ((,gn ,n-elts)
,@(mapcar #'list vars forms)
(,glst ,access)
(,(car var) (nthcdr ,gn ,glst)))
(prog1 (subseq ,glst 0 ,gn)
,set)))))
;;; ==============================
;;; :ALIST-MACRROS
;;; ==============================
;;; :SOURCE /mcclimExtensions/conditional-commands/creating-assoc.lisp :WAS `creating-assoc'
(defmacro assoc-create (item alist &environment env)
;; :NOTE `get-setf-expansion' place &optional environment
;; => vars, vals, store-vars, writer-form, reader-form
;; Any compound form is a valid place, since any compound form whose
;; operator F has no setf expander are expanded into a call to `(setf F)'.
(multiple-value-bind (dummies vals new setter getter)
(get-setf-expansion alist env)
(let ((object (gensym "object-"))) ;;(gensym)))
`(let* ((,object ,item)
,@(mapcar #'list dummies vals)
(,(car new) ,getter))
(prog1
(or (assoc ,object ,(car new))
;; :was (first (setq ,(car new) (cons (list ,object) ,(car new)))))
(car (setq ,(car new) (cons (list ,object) ,(car new)))))
,setter)))))
;;
;; Should macroexpand-1 to something like this:
;; (LET* ((#:|object-15058| 'BAZ) (#:NEW15057 LIST))
;; (PROG1
;; (OR (ASSOC #:|object-15058| #:NEW15057)
;; (FIRST (SETQ #:G15057 (CONS (LIST #:|object-15058|) #:NEW15057))))
;; (SETQ LIST #:G15057)))
;;; ==============================
;;; :ITERATOR-MACROS
;;; ==============================
(defmacro for ((var start stop) &body body)
(let ((gstop (gensym "STOP-")))
`(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
;;; ==============================
;;; until/while using `do'
(defmacro until (test &body body)
`(do () (,test) ,@body))
;; Alternative version w/out the while call from krmcl/macros.lisp
;; (defmacro while (test &body body)
;; `(do () ((not ,test)) ,@body))
;;
;; Francogrex's
;; (defmacro while (condition &rest body)
;; (let ((var (gensym)))
;; `(do ((,var nil (progn ,@body)))
;; ((null ,condition) ,var))))
;; (let ((a 0)) (while (> 3 a) (setq a (1+ a))) a)
(defmacro while (test &body body)
`(until (not ,test) ,@body))
;;; ==============================
;; (URL `http://paste.lisp.org/display/125328')
;; hexstream's
(defmacro while.hexstream (test &body body)
(let ((tag (gensym (string '#:while))))
`(block nil
(tagbody ,tag
(unless ,test
(return))
,@body
(go ,tag)))))
;;
(defmacro until.hexstream (test &body body)
`(while (not ,test)
,@body))
;;; ==============================
;; :PASTE-AUTHOR stassats
;; :PASTE-DATE 2011-10-05
;; :SOURCE (URL `http://paste.lisp.org/+2OIJ/2')
(defmacro map-do-list ((var list &optional (result nil result-supplied-p)) &body body)
(multiple-value-bind (body declarations) (alexandria:parse-body body)
`(block nil
(mapc (lambda (,var)
,@declarations
(tagbody
,@body))
,list)
,(if result-supplied-p
`(let (,var)
(declare (ignorable ,var))
,@declarations
,result)
nil))))
;;; dosequence (based on James Anderson's mod of Thomas Burdick's version)
;;; :SOURCE GBBopen/source/tools/tools.lisp
(defmacro dosequence ((var sequence &optional result) &body forms)
(with-gensyms (end-p fun)
`(block nil
(flet ((,fun (,var ,end-p)
(tagbody
(when ,end-p (go ,end-p))
,@forms
(return-from ,fun nil)
,end-p
(return-from ,fun ,result))))
(flet ((fn (element) (,fun element nil)))
(declare (dynamic-extent #'fn))
(map nil #'fn ,sequence))
,@(when result `((,fun nil t)))))))
;;; :SOURCE GBBopen/source/tools/tools.lisp
(defmacro dosublists ((var listform &optional result) &body body)
`(do ((,var ,listform (cdr ,var)))
((endp ,var) ,result)
(declare (list ,var))
,@body))
;;; :SOURCE cl-difflib/difflib.lisp :WAS `enumerate'
;;; Hey, it's just like Python's range function!
(defmacro doenumerated ((index-var elt-var sequence &optional result-form) &body body)
(let ((sequence-var (gensym "SEQUENCE")))
`(let ((,sequence-var ,sequence))
(dotimes (,index-var (length ,sequence-var) ,result-form)
(let ((,elt-var (elt ,sequence-var ,index-var)))
,@body)))))
;;; :SOURCE cl-difflib/difflib.lisp :WAS `do-range'
;; Hey, it's just like Python's range function!
(defmacro dorange ((var start-form end-form &optional result-form) &body body)
(let ((start-var (gensym))
(end-var (gensym)))
`(let ((,start-var ,start-form)
(,end-var ,end-form))
(do ((,var ,start-var (1+ ,var)))
((>= ,var ,end-var) ,result-form)
,@body))))
;;; ==============================
;;; :VECTOR-MACROS
;;; ==============================
;;; ==============================
;; :NOTE This causes the SBCL compiler to warn:
;; SB-KERNEL:%ASET called on constant data.
;; This macro is prob. not what is wanted. So, mabye instead use the defun
;; below. At least that way we can live in a state of semi-ignorant comfort.
(defmacro aset (array index new-element)
(declare ((integer 0 *) index))
`(setf (aref ,array ,index) ,new-element))
;;
;; (defun aset (array index new-element)
;; (declare ((integer 0 *) index))
;; (setf (aref array index) new-element))
;;; ==============================
(defmacro mk-array (type init &optional len)
(if len `(make-array ,len :element-type ,type :initial-element ,init)
`(make-array (length ,init) :element-type ,type
:initial-contents ,init)))
;;; :SOURCE freedius/freedius/lisp/lisp/binary-search.lisp :WAS `remove-element'
;;(eval-when (:compile-toplevel :load-toplevel )
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro vector-insert-element (vector pos element num &optional (grow-factor 2))
`(let ((new-num (1+ ,num))
(max (length ,vector)))
(declare (type fixnum new-num max))
(cond ((= ,num max)
;; grow the vector
(let ((new (make-array (truncate (* max ,grow-factor)) :initial-element nil)))
(declare (type simple-vector new))
;; Blt the new buggers into place leaving a space for
;; the new element
(replace new ,vector :end1 ,pos :end2 ,pos)
(replace new ,vector
:start1 (1+ ,pos)
:end1 new-num
:start2 ,pos
:end2 ,num)
(fill ,vector nil)
(setf (svref new ,pos) ,element)
new))
(t
;; move the buggers down a slot
(replace ,vector ,vector :start1 (1+ ,pos) :start2 ,pos)
(setf (svref ,vector ,pos) ,element)
,vector))))
;;
(defmacro vector-remove-element (vector pos num)
`(progn
(replace ,vector ,vector :start1 ,pos :start2 (1+ ,pos) :end1 (1- ,num) :end2 ,num)
(setf (svref ,vector (1- ,num)) nil)
,vector))
) ; :CLOSE eval-when
;; :COURTESY cllib/withtype.lisp :WAS `map-vec'
(defmacro vector-map (type len &rest args)
`(map-into (make-array ,len :element-type ,type) ,@args))
;;; :SOURCE mcclim/Apps/Scigraph/dwim/macros.lisp
(defmacro condition-case ((&rest varlist) form &rest clauses)
;; #+genera (declare (zwei:indentation 1 4 2 2))
;; #+genera `(scl:condition-case ,varlist ,form ,@clauses)
;; #-genera
`(let ,(cdr varlist)
(handler-case
,form
,@(mapcar #'(lambda (cl)
`(,(first cl)
,(if (first varlist) (list (first varlist)))
,@(cdr cl)))
clauses))))
(defmacro multiple-value-nth-p (expr nth-list &key (test 'equal))
(with-gensyms (m-v-rest)
`(destructuring-bind (&rest ,m-v-rest) (multiple-value-list ,expr)
(if (or (null ,m-v-rest)
(< (length ,m-v-rest) 2))
nil
(,test ',nth-list
(list (car ,m-v-rest) (cadr ,m-v-rest)))))))
(defmacro multiple-value-nil-nil-p (expr)
`(multiple-value-nth-p ,expr (nil nil)))
(defmacro multiple-value-nil-t-p (expr)
`(multiple-value-nth-p ,expr (nil t)))
(defmacro multiple-value-t-nil-p (expr)
`(multiple-value-nth-p ,expr (t nil)))
(defmacro multiple-value-t-t-p (expr)
`(multiple-value-nth-p ,expr (t t)))
;;; ==============================
;;: :ASSERT-MACROS
;;; ==============================
;;; #:assert-nil-nil
;;; #:assert-nil-t
;;; #:assert-t-t
;; :SOURCE sbcl/tests/type.impure.lisp
;; (defmacro assert-nil-nil (expr)
;; `(assert (equal '(nil nil) (multiple-value-list ,expr))))
;; (defmacro assert-nil-t (expr)
;; `(assert (equal '(nil t) (multiple-value-list ,expr))))
;; (defmacro assert-t-t (expr)
;; `(assert (equal '(t t) (multiple-value-list ,expr))))
;;; ==============================
;;; :MACROS-DOCUMENTATION
;;; ==============================
;;; ==============================
;;; :EVAL-WHEN-MACROS
;;; ==============================
(fundoc 'eval-when-all
"Expands to an `eval-when' form with all when specifiers present.~%~@
BODY is wrapped inside a form of the type:~%~@
\(eval-when \(:compile-toplevel :load-toplevel :execute\)\)~%@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'eval-when-compile
"Evaluate BODY when :compile-toplevel :load-toplevel.~%~@
:EXAMPLE~%~%~@
{ ... <EXAMPLE> ... } ~%~@
:EMACS-LISP-COMPAT eval-when-compile is an elisp macro in :FILE lisp/emacs-lisp/byte-run.el.~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
;;; ==============================
;;; :SYMBOL-MACROS
;;; ==============================
(fundoc 'defsubst
"Define NAME as an inline function. The syntax is just like that of `defun'.~%~@
A `defsubst'd function is one which is `declaim'd as:~%
\(declaim \(inline <NAME>\)\)~%~@
:EXAMPLE~%~%~@
{ ... <EXAMPLE> ... } ~%~@
:EMACS-LISP-COMPAT defsubst is a Lisp macro in :FILE lisp/emacs-lisp/byte-run.el.~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'bound-and-true-p
"Return the value of symbol VAR if it is bound, else nil.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... }~%~@
:EMACS-LISP-COMPAT~%~@
:SEE-ALSO `boundp'.~%▶▶▶")
(fundoc 'multiple-value-setf
"Like `multiple-value-setq', but works with places.~%~@
A \"place\" of nil means to ignore the corresponding value from FORM.
Return the primary value of evaluating FORM.~%~@
:EXAMPLE~%~% { ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc '%compose
"Compose FUNCTIONS or macros of 1 argument into a funcallable lambda form.~%~@
:EXAMPLE~%
\(compose abs \(dl-val zz\) 'key\)~%
;=> \(lambda \(yy\) \(abs \(funcall \(dl-val zz\) \(funcall key yy\)\)\)\)~%~@
:NOTE This is macro is different from `alexandria:compose' which returns a closure.~%~@
:SEE-ALSO `compose-fun', `compose-all'.~%▶▶▶")
;;; ==============================
;;; :WITH-SYTLE-MACROS
;;; ==============================
(fundoc 'with-gensyms
"Create gensyms with SYMS around BODY.~%~@
Automates the oft found macro idiom:~%
\(let \(\(foo \(gensym \"foo\"\)\)
\(max-index \(gensym \"max-index-\"\)\)\)
{ ... } \)~%~@
\"Good notation eliminates thought.\" -- Eric Siggia~%~@
:EXAMPLE~%
\(macroexpand
'\(with-gensyms \(a-sym b-sym\) \(list \(identity a-sym\) \(identity b-sym\)\)\)\)~%
\(with-gensyms \(a-sym b-sym\) \(list \(identity a-sym\) \(identity b-sym\)\)\)~%~@
:NOTE When #+:sbcl This is `sb-int:with-unique-names' and returns with slightly
different format than the \"On Lisp\" variant, e.g.~%
\(with-gensyms \(a-sym b-sym\) `\(,a-sym ,b-sym\)\)~%
\(with-gensyms \(asym bsym\) `\(,asym ,bsym\)\)~%~@
:SEE-ALSO `gensym', `gentemp', `sb-int:block-gensym'.~%▶▶▶")
(fundoc 'w-debug-declared
"Inline a declaration to produce debuggable/steppable code.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:NOTE Use with <sharp-sign><period> in forms when this is used.~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
;; (defun foo ()
;; \\#\\.(w-debug-declared)
;; (let ((bar zeb))
;; (or (frob bar)
;; (quux bzr))))
(fundoc 'w-fast-declared
"Inline a declaration to produce fast/safe code.~%~@
When evalutated at top of defining form expands to:~%~@
\(declare \(optimize \(speed 3\) \(safety 1\) \(compilation-speed 0\) \(space 0\)\)\)~%~@
:EXAMPLE~%
\(defun some-fast-fun \(\)
#.\(w-fast-declared\)
{... <DO-FAST-STUFF-HERE> ...} \)~%~@
:NOTE Use with <sharp-sign><period> in forms when this is used.~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'retaining-value
"Evaluate `body' with `bound-symbol' bound to `initial-value' (default NIL).~%~@
The next time `body' is evaluated, `bound-symbol' will be bound to whatever its
value was the last time evaluation of `body' ended.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'and-so
"When AND-X returns non-nil return a list comprised of AND-X and AND-SO.~%~@
In other words:~%~@
\(and AND-X `\(AND-X ,@AND-SO\)\)~%~@
:EXAMPLE~%
\(and-so t \"bubba\" \"more-bubba\"\)~%
\(and-so \(+ 1 3\) \"bubba\" \"more-bubba\"\)~%
\(and-so \(not t\) \"bubba\" \"more-bubba\"\)~%~@
:SEE-ALSO `mon:refbind', `mon:retaining-value'.~%▶▶▶")
;;; ==============================
;;; :TYPE-MACROS
;;; ==============================
(fundoc 'type-and
"Whether both TYPE-X and TYPE-Y are true.~%~@
Return as if by `cl:values'.~%~@
Utility macro for two `values' predicates.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `mon:type-not', `mon:type-any', `mon:type-every', `cl:typep',
`cl:subtypep', `cl:type-of', `cl:typecase', `cl:etypecase'.~%▶▶▶")
(fundoc 'type-not
"Whether TYPE-CHECKED is true. Return as if by `cl:values'.~%~@
Utility macro for type predicates.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `mon:type-and', `mon:type-any', `mon:type-every',
`cl:subtypep', `cl:type-of', `cl:typecase', `cl:etypecase'.~%▶▶▶")
;;; ==============================
;;; :EMACS-COMPAT-MACROS
;;; ==============================
(fundoc 'condition-case
"Regain control when an error is signaled.~%~@
Executes bodyform and returns its value if no error happens.~%~@
Each element of handlers looks like \(CONDITION-NAME BODY...\)
where the BODY is made of Lisp expressions.~%~@
A handler is applicable to an error
if CONDITION-NAME is one of the error's condition names.~%~@
If an error happens, the first applicable handler is run.~%~@
The car of a handler may be a list of condition names
instead of a single condition name. Then it handles all of them.~%~@
When a handler handles an error, control returns to the `condition-case'
and it executes the handler's BODY...
with var bound to \(ERROR-SYMBOL . SIGNAL-DATA\) from the error.~%~@
\(If var is nil, the handler can't access that information.\)
Then value of last BODY form is returned from the `condition-case' expression.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:EMACS-LISP-COMPAT~%~@
:SEE-ALSO `cl:define-condition', `cl:make-condition', `cl:handler-case',
`cl:handler-bind', `cl:restart-case', `cl:restart-bind', `cl:find-restart',
`cl:restart', `cl:signal', `cl:error'.~%▶▶▶")
;;; ==============================
;;; :NUMBERS-MACROS
;;; ==============================
(fundoc 'to-percent
"Return percentage value of VV.~%~@
:EXAMPLE~%
\(to-percent 1.234\)~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'number-to-double-float
"Coerce NUM to double float.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
;;; ==============================
;;; :SEQUENCE-MACROS-DOCUMENTATION
;;; ==============================
(fundoc 'list-sift
"SIFT-LIST with SIFT-TESTS.~%~@
On a Common Lisp return is as if by values.~%~@
:EXAMPLE~%
\(list-sift '\( 1 2 3 4 5 6 7 8 9 10\) #'\(lambda \(x\) \(> x 4\)\)\)
;=> \(10 9 8 7 6 5\) \(4 3 2 1\)~%
\(list-sift '\(1 2 3 -1 -2 -3\) #'oddp #'plusp\)
;=> \(-3 -1 3 1\) \(2\) \(-2\)~%
\(list-sift '\(1 2 3 -1 -2 -3\) #'plusp #'oddp\)
;=> \(3 2 1\) \(-3 -1\) \(-2\)~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'popn
"Pop N-ELTS off PLACE.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... }~%~@
:SEE-ALSO `cl:pop', `cl:push'.~%▶▶▶")
(fundoc 'assoc-create
"Like `assoc' but create requested alist item on-the-fly if not yet existing.~%~@
:EXAMPLE~%
\(let* \(\(list '\(\(foo 1\)\)\)\)
\(list \(assoc 'foo list\)
\(assoc 'baz list\)
\(assoc-create 'baz list\)
\(assoc 'baz list\)
list\)\)~%
;=> \(\(FOO 1\)
; NIL
; \(BAZ\)
; \(BAZ\)
; \(\(BAZ\) \(FOO 1\)\)\)~%
\(macroexpand-1 '\(assoc-create 'baz list\)\)~%~@
:SEE (URL `http://paste.lisp.org/display/13846#2')~%~@
:SEE info node `(ansicl)get-setf-expansion)'~%~@
:SEE-ALSO `cl:assoc', `mon:assq'.~%▶▶▶")
;;; ==============================
;;; :ITERATOR-MACROS
;;; ==============================
(fundoc 'while
"If TEST yields non-nil, eval body... and repeat.~%~@
The order of execution is thus test, body, test, body and so on
until test returns nil.~%~@
:EXAMPLE~%~@
\(let \(\(a 0\)\)
\(while \(> 3 a\) \(setq a \(1+ a\)\)\)
a\)~%~@
:EMACS-LISP-COMPAT~%~@
:SEE-ALSO `mon:until', `mon:for', `mon:dosequence', `collect'.~%▶▶▶")
(fundoc 'for
"A curly-braced `for` style function.~%~@
:EXAMPLE~%
\(for \(i 0 8\) \(princ i\)\)~%~@
:SEE-ALSO `dohash', `collect', `mon:dosublists', `mon:dosequence', `mon:for'.~%▶▶▶")
(fundoc 'until
"Until TEST is non-nil do BODY.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... }~%~@
:NOTE This macro uses `cl:do', the macro uses `cl:loop'.
:SEE-ALSO `mon:while', `mon:for'.~%▶▶▶")
(fundoc 'dosublists
"<DOCSTR>~%~@
Arg MAPL-STYLE ~%~@
Arg DOLIST ~%~@