-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.lisp
100 lines (88 loc) · 4.27 KB
/
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
;;; file: utilities.lisp
;;;
;;; Copyright (c) 2007 Cyrus Harmon ([email protected])
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(in-package :nuclblog)
(defmacro with-unique-names ((&rest bindings) &body body)
"Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
Executes a series of forms with each VAR bound to a fresh,
uninterned symbol. The uninterned symbol is as if returned by a call
to GENSYM with the string denoted by X - or, if X is not supplied, the
string denoted by VAR - as argument.
The variable bindings created are lexical unless special declarations
are specified. The scopes of the name bindings and declarations do not
include the Xs.
The forms are evaluated in order, and the values of all but the last
are discarded \(that is, the body is an implicit PROGN)."
;; reference implementation posted to comp.lang.lisp as
;; <[email protected]> by Vebjorn Ljosa - see also
;; <http://www.cliki.net/Common%20Lisp%20Utilities>
`(let ,(mapcar #'(lambda (binding)
(check-type binding (or cons symbol))
(if (consp binding)
(destructuring-bind (var x) binding
(check-type var symbol)
`(,var (gensym ,(etypecase x
(symbol (symbol-name x))
(character (string x))
(string x)))))
`(,binding (gensym ,(symbol-name binding)))))
bindings)
,@body))
(defmacro with-html-page (&body body)
"Executes BODY inside a cl-who:wtih-html-output-to-string body,
directing the output to *standard-output* and setting :prologue to t."
`(with-html-output-to-string (*standard-output* nil :prologue t)
,@body))
(defmacro with-html (&body body)
"Executes BODY inside a cl-who:with-html-output body."
`(with-html-output (*standard-output*)
,@body))
(defmacro with-xml-output-to-string ((&optional (stream *standard-output*))
&body body)
"Prints the <?xml...?> header to stream and Executes BODY inside of
a cl-who:with-html-output-to-string block. There is some machinery to
turn off downcasing of the tags, but I think this doesn't work as I
intended and should be removed."
(let ((who::*downcase-tokens-p* nil))
`(with-html-output-to-string (,stream)
(princ "<?xml version='1.0'?>" ,stream)
,@body)))
(defmacro with-xml (&body body)
"Executes BODY inside a cl-who:with-html-output body. This is a
synonym for with-html, but it would be nice if this could
automatically deal with turning off the tag downcasing, which
it currently doesn't."
`(with-html-output (*standard-output*)
,@body))
(defun concatenate-url (base &rest strings)
"Concatenates strings. In theory, this could be smarter about
checking validity of URLs, fixing redundant slashes, etc..."
(apply #'concatenate 'string base strings))
(defun make-keyword (string)
(intern (string-upcase string) :keyword))