util.lisp - clic - Clic is an command line interactive client for gopher written in Common LISP (HTM) git clone git://bitreich.org/clic/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/clic/ (DIR) Log (DIR) Files (DIR) Refs (DIR) Tags (DIR) README (DIR) LICENSE --- util.lisp (8180B) --- 1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: FLEXI-STREAMS; Base: 10 -*- 2 ;;; $Header: /usr/local/cvsrep/flexi-streams/util.lisp,v 1.24 2008/05/25 21:26:12 edi Exp $ 3 4 ;;; Copyright (c) 2005-2008, 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 :flexi-streams) 31 32 #+:lispworks 33 (eval-when (:compile-toplevel :load-toplevel :execute) 34 (import '(lw:with-unique-names lw:when-let))) 35 36 #-:lispworks 37 (defmacro when-let ((var form) &body body) 38 "Evaluates FORM and binds VAR to the result, then executes BODY 39 if VAR has a true value." 40 `(let ((,var ,form)) 41 (when ,var ,@body))) 42 43 #-:lispworks 44 (defmacro with-unique-names ((&rest bindings) &body body) 45 "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form* 46 47 Executes a series of forms with each VAR bound to a fresh, 48 uninterned symbol. The uninterned symbol is as if returned by a call 49 to GENSYM with the string denoted by X - or, if X is not supplied, the 50 string denoted by VAR - as argument. 51 52 The variable bindings created are lexical unless special declarations 53 are specified. The scopes of the name bindings and declarations do not 54 include the Xs. 55 56 The forms are evaluated in order, and the values of all but the last 57 are discarded \(that is, the body is an implicit PROGN)." 58 ;; reference implementation posted to comp.lang.lisp as 59 ;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also 60 ;; <http://www.cliki.net/Common%20Lisp%20Utilities> 61 `(let ,(mapcar #'(lambda (binding) 62 (check-type binding (or cons symbol)) 63 (if (consp binding) 64 (destructuring-bind (var x) binding 65 (check-type var symbol) 66 `(,var (gensym ,(etypecase x 67 (symbol (symbol-name x)) 68 (character (string x)) 69 (string x))))) 70 `(,binding (gensym ,(symbol-name binding))))) 71 bindings) 72 ,@body)) 73 74 #+:lispworks 75 (eval-when (:compile-toplevel :load-toplevel :execute) 76 (setf (macro-function 'with-rebinding) 77 (macro-function 'lw:rebinding))) 78 79 #-:lispworks 80 (defmacro with-rebinding (bindings &body body) 81 "WITH-REBINDING ( { var | (var prefix) }* ) form* 82 83 Evaluates a series of forms in the lexical environment that is 84 formed by adding the binding of each VAR to a fresh, uninterned 85 symbol, and the binding of that fresh, uninterned symbol to VAR's 86 original value, i.e., its value in the current lexical environment. 87 88 The uninterned symbol is created as if by a call to GENSYM with the 89 string denoted by PREFIX - or, if PREFIX is not supplied, the string 90 denoted by VAR - as argument. 91 92 The forms are evaluated in order, and the values of all but the last 93 are discarded \(that is, the body is an implicit PROGN)." 94 ;; reference implementation posted to comp.lang.lisp as 95 ;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also 96 ;; <http://www.cliki.net/Common%20Lisp%20Utilities> 97 (loop for binding in bindings 98 for var = (if (consp binding) (car binding) binding) 99 for name = (gensym) 100 collect `(,name ,var) into renames 101 collect ``(,,var ,,name) into temps 102 finally (return `(let ,renames 103 (with-unique-names ,bindings 104 `(let (,,@temps) 105 ,,@body)))))) 106 107 (defun normalize-external-format-name (name) 108 "Converts NAME \(a symbol) to a `canonical' name for an 109 external format, e.g. :LATIN1 will be converted to :ISO-8859-1. 110 Also checks if there is an external format with that name and 111 signals an error otherwise." 112 (let ((real-name (cdr (find name flex::+name-map+ 113 :test (lambda (item pair) 114 (or (string-equal item (cdr pair)) 115 (string-equal item (car pair)))))))) 116 (unless real-name 117 (error 'external-format-error 118 :format-control "~S is not known to be a name for an external format." 119 :format-arguments (list name))) 120 real-name)) 121 122 (defun ascii-name-p (name) 123 "Checks whether NAME is the keyword :ASCII." 124 (eq name :us-ascii)) 125 126 (defun koi8-r-name-p (name) 127 "Checks whether NAME is the keyword :KOI8-R." 128 (eq name :koi8-r)) 129 130 (defun code-page-name-p (name) 131 "Checks whether NAME is the keyword :CODE-PAGE." 132 (eq name :code-page)) 133 134 (defun iso-8859-name-p (name) 135 "Checks whether NAME \(a keyword) names one of the known 136 ISO-8859 encodings." 137 (find name +iso-8859-tables+ :key #'car)) 138 139 (defun known-code-page-id-p (id) 140 "Checks whether ID \(a number) denotes one of the known Windows 141 code pages." 142 (and (find id +code-page-tables+ :key #'car) 143 id)) 144 145 #+:lispworks 146 (defun sans (plist &rest keys) 147 "Returns PLIST with keyword arguments from KEYS removed." 148 (sys::remove-properties plist keys)) 149 150 #-:lispworks 151 (defun sans (plist &rest keys) 152 "Returns PLIST with keyword arguments from KEYS removed." 153 ;; stolen from Usenet posting <3247672165664225@naggum.no> by Erik 154 ;; Naggum 155 (let ((sans ())) 156 (loop 157 (let ((tail (nth-value 2 (get-properties plist keys)))) 158 ;; this is how it ends 159 (unless tail 160 (return (nreconc sans plist))) 161 ;; copy all the unmatched keys 162 (loop until (eq plist tail) do 163 (push (pop plist) sans) 164 (push (pop plist) sans)) 165 ;; skip the matched key 166 (setq plist (cddr plist)))))) 167 168 #+:lispworks 169 (defmacro with-accessors (slot-entries instance &body body) 170 "For LispWorks, we prefer SLOT-VALUE over accessors for better 171 performance." 172 ;; note that we assume that the variables have the same names as the 173 ;; slots 174 `(with-slots ,(mapcar #'car slot-entries) 175 ,instance 176 ,@body)) 177 178 (defun make-octet-buffer (&optional (size +buffer-size+)) 179 "Creates and returns a fresh buffer \(a specialized array) of size 180 +BUFFER-SIZE+ to hold octets." 181 (declare #.*standard-optimize-settings*) 182 (make-array size :element-type 'octet)) 183 184 (defun type-equal (type1 type2) 185 "Whether TYPE1 and TYPE2 denote the same type." 186 (declare #.*standard-optimize-settings*) 187 (and (subtypep type1 type2) 188 (subtypep type2 type1))) 189 190 (defun maybe-rewind (stream octets) 191 "Tries to `rewind' the \(binary) stream STREAM by OCTETS octets. 192 Returns T if it succeeds, otherwise NIL." 193 (when-let (position (file-position stream)) 194 (if (file-position stream (- position octets)) t nil))) 195 196 (defmacro logand* (x y) 197 "Solely for optimization purposes. Some Lisps need it, some don't." 198 `(the fixnum (logand ,x ,y))) 199 200 (defmacro logior* (x y) 201 "Solely for optimization purposes. Some Lisps need it, some don't." 202 `(the fixnum (logior ,x ,y))) 203 204 (defmacro ash* (integer count) 205 "Solely for optimization purposes. Some Lisps need it, some don't." 206 `(the fixnum (ash ,integer ,count)))