| 1 |
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-GD; Base: 10 -*- |
|---|
| 2 |
;;; $Header: /usr/local/cvsrep/gd/util.lisp,v 1.15 2007/02/28 15:47:58 edi Exp $ |
|---|
| 3 |
|
|---|
| 4 |
;;; Copyright (c) 2003-2007, Dr. Edmund Weitz. All rights reserved. |
|---|
| 5 |
|
|---|
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
|---|
| 7 |
;;; modification, are permitted provided that the following conditions |
|---|
| 8 |
;;; are met: |
|---|
| 9 |
|
|---|
| 10 |
;;; * Redistributions of source code must retain the above copyright |
|---|
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
|---|
| 12 |
|
|---|
| 13 |
;;; * Redistributions in binary form must reproduce the above |
|---|
| 14 |
;;; copyright notice, this list of conditions and the following |
|---|
| 15 |
;;; disclaimer in the documentation and/or other materials |
|---|
| 16 |
;;; provided with the distribution. |
|---|
| 17 |
|
|---|
| 18 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
|---|
| 19 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 20 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 21 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|---|
| 22 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 23 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
|---|
| 24 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 25 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 26 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 27 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 28 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
|
|---|
| 30 |
(in-package :cl-gd) |
|---|
| 31 |
|
|---|
| 32 |
#+:lispworks |
|---|
| 33 |
(import 'lw:with-unique-names) |
|---|
| 34 |
|
|---|
| 35 |
#-:lispworks |
|---|
| 36 |
(defmacro with-unique-names ((&rest bindings) &body body) |
|---|
| 37 |
"Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form* |
|---|
| 38 |
|
|---|
| 39 |
Executes a series of forms with each VAR bound to a fresh, |
|---|
| 40 |
uninterned symbol. The uninterned symbol is as if returned by a call |
|---|
| 41 |
to GENSYM with the string denoted by X - or, if X is not supplied, the |
|---|
| 42 |
string denoted by VAR - as argument. |
|---|
| 43 |
|
|---|
| 44 |
The variable bindings created are lexical unless special declarations |
|---|
| 45 |
are specified. The scopes of the name bindings and declarations do not |
|---|
| 46 |
include the Xs. |
|---|
| 47 |
|
|---|
| 48 |
The forms are evaluated in order, and the values of all but the last |
|---|
| 49 |
are discarded \(that is, the body is an implicit PROGN)." |
|---|
| 50 |
;; reference implementation posted to comp.lang.lisp as |
|---|
| 51 |
;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also |
|---|
| 52 |
;; <http://www.cliki.net/Common%20Lisp%20Utilities> |
|---|
| 53 |
`(let ,(mapcar #'(lambda (binding) |
|---|
| 54 |
(check-type binding (or cons symbol)) |
|---|
| 55 |
(if (consp binding) |
|---|
| 56 |
(destructuring-bind (var x) binding |
|---|
| 57 |
(check-type var symbol) |
|---|
| 58 |
`(,var (gensym ,(etypecase x |
|---|
| 59 |
(symbol (symbol-name x)) |
|---|
| 60 |
(character (string x)) |
|---|
| 61 |
(string x))))) |
|---|
| 62 |
`(,binding (gensym ,(symbol-name binding))))) |
|---|
| 63 |
bindings) |
|---|
| 64 |
,@body)) |
|---|
| 65 |
|
|---|
| 66 |
#+:lispworks |
|---|
| 67 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
|---|
| 68 |
(setf (macro-function 'with-rebinding) |
|---|
| 69 |
(macro-function 'lw:rebinding))) |
|---|
| 70 |
|
|---|
| 71 |
#-:lispworks |
|---|
| 72 |
(defmacro with-rebinding (bindings &body body) |
|---|
| 73 |
"REBINDING ( { var | (var prefix) }* ) form* |
|---|
| 74 |
|
|---|
| 75 |
Evaluates a series of forms in the lexical environment that is |
|---|
| 76 |
formed by adding the binding of each VAR to a fresh, uninterned |
|---|
| 77 |
symbol, and the binding of that fresh, uninterned symbol to VAR's |
|---|
| 78 |
original value, i.e., its value in the current lexical environment. |
|---|
| 79 |
|
|---|
| 80 |
The uninterned symbol is created as if by a call to GENSYM with the |
|---|
| 81 |
string denoted by PREFIX - or, if PREFIX is not supplied, the string |
|---|
| 82 |
denoted by VAR - as argument. |
|---|
| 83 |
|
|---|
| 84 |
The forms are evaluated in order, and the values of all but the last |
|---|
| 85 |
are discarded \(that is, the body is an implicit PROGN)." |
|---|
| 86 |
;; reference implementation posted to comp.lang.lisp as |
|---|
| 87 |
;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also |
|---|
| 88 |
;; <http://www.cliki.net/Common%20Lisp%20Utilities> |
|---|
| 89 |
(loop for binding in bindings |
|---|
| 90 |
for var = (if (consp binding) (car binding) binding) |
|---|
| 91 |
for name = (gensym) |
|---|
| 92 |
collect `(,name ,var) into renames |
|---|
| 93 |
collect ``(,,var ,,name) into temps |
|---|
| 94 |
finally (return `(let ,renames |
|---|
| 95 |
(with-unique-names ,bindings |
|---|
| 96 |
`(let (,,@temps) |
|---|
| 97 |
,,@body)))))) |
|---|
| 98 |
|
|---|
| 99 |
(defun sans (plist &rest keys) |
|---|
| 100 |
"Returns PLIST with keyword arguments from KEYS removed." |
|---|
| 101 |
;; stolen from Usenet posting <3247672165664225@naggum.no> by Erik |
|---|
| 102 |
;; Naggum |
|---|
| 103 |
(let ((sans ())) |
|---|
| 104 |
(loop |
|---|
| 105 |
(let ((tail (nth-value 2 (get-properties plist keys)))) |
|---|
| 106 |
;; this is how it ends |
|---|
| 107 |
(unless tail |
|---|
| 108 |
(return (nreconc sans plist))) |
|---|
| 109 |
;; copy all the unmatched keys |
|---|
| 110 |
(loop until (eq plist tail) do |
|---|
| 111 |
(push (pop plist) sans) |
|---|
| 112 |
(push (pop plist) sans)) |
|---|
| 113 |
;; skip the matched key |
|---|
| 114 |
(setq plist (cddr plist)))))) |
|---|
| 115 |
|
|---|
| 116 |
(defun convert-to-char-references (string) |
|---|
| 117 |
"Returns a string where all characters of STRING with CHAR-CODE |
|---|
| 118 |
greater than 127 are converted to XML character entities." |
|---|
| 119 |
(with-output-to-string (s) |
|---|
| 120 |
(with-standard-io-syntax |
|---|
| 121 |
(loop for char across string |
|---|
| 122 |
for char-code = (char-code char) |
|---|
| 123 |
when (<= char-code 127) do |
|---|
| 124 |
(write-char char s) |
|---|
| 125 |
else do |
|---|
| 126 |
(write-char #\& s) |
|---|
| 127 |
(write-char #\# s) |
|---|
| 128 |
(princ char-code s) |
|---|
| 129 |
(write-char #\; s))))) |
|---|
| 130 |
|
|---|
| 131 |
(defmacro with-safe-alloc ((var alloc free) &rest body) |
|---|
| 132 |
`(let (,var) |
|---|
| 133 |
(unwind-protect |
|---|
| 134 |
(progn (setf ,var ,alloc) |
|---|
| 135 |
,@body) |
|---|
| 136 |
(when ,var ,free)))) |
|---|