1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.no>
4 ;; This library is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU Lesser General Public
6 ;; License as published by the Free Software Foundation; either
7 ;; version 2 of the License, or (at your option) any later version.
9 ;; This library is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;; Lesser General Public License for more details.
14 ;; You should have received a copy of the GNU Lesser General Public
15 ;; License along with this library; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ;; $Id: utils.lisp,v 1.3 2005-02-03 23:09:05 espen Exp $
23 (defun type-expand-1 (form)
24 (let ((def (cond ((symbolp form)
25 #+cmu(kernel::info type expander form)
26 #+sbcl(sb-impl::info :type :expander form))
27 ((and (consp form) (symbolp (car form)))
28 #+cmu(kernel::info type expander (car form))
29 #+sbcl(sb-impl::info :type :expander (car form)))
32 (values (funcall def (if (consp form) form (list form))) t)
36 (defun type-expand-to (type form)
37 (labels ((expand (form0)
38 (if (eq (first (mklist form0)) type)
40 (multiple-value-bind (expanded-form expanded-p)
43 (expand expanded-form)
44 (error "~A can not be expanded to ~A" form type))))))
47 (defmacro with-gc-disabled (&body body)
48 (let ((gc-inhibit (make-symbol "GC-INHIBIT")))
50 (let ((,gc-inhibit #+cmu lisp::*gc-inhibit*
51 #+sbcl sb-impl::*gc-inhibit*))
59 (if (and obj (atom obj)) (list obj) obj))
62 (and (symbolp obj) (not (member obj '(t nil)))))
64 (defun all-equal (&rest objects)
69 (equal (first objects) (second objects))
70 (apply #'all-equal (rest objects)))))
72 (defun neq (obj1 obj2)
75 (defmacro return-if (form)
76 (let ((result (make-symbol "RESULT")))
77 `(let ((,result ,form))
81 (defun make-pointer (address)
84 (defun null-pointer-p (pointer)
85 (zerop (sap-int pointer)))
88 (defmacro when-bind ((var expr) &body body)
94 (defmacro assoc-ref (key alist &key (test #'eq))
95 `(cdr (assoc ,key ,alist :test ,test)))
98 (defmacro assoc-lref (key alist &key (test #'eq))
99 `(cadr (assoc ,key ,alist :test ,test)))
102 (defun assoc-rem (key alist &key (test #'eq))
103 (remove-if #'(lambda (element) (funcall test key (car element))) alist))
106 (defun assoc-delete (key alist &key (test #'eq))
107 (delete-if #'(lambda (element) (funcall test key (car element))) alist))
110 (defun funcallable (object)
115 (defun intersection-p (list1 list2 &key (test #'eq))
116 (dolist (obj list1 nil)
117 (when (member obj list2 :test test)
118 (return-from intersection-p t))))
121 (defun split-string (string delimiter)
122 (declare (simple-string string) (character delimiter))
123 (let ((pos (position delimiter string)))
127 (subseq string 0 pos)
128 (split-string (subseq string (1+ pos)) delimiter)))))
130 (defun split-string-if (string predicate)
131 (declare (simple-string string))
132 (let ((pos (position-if predicate string :start 1)))
136 (subseq string 0 pos)
137 (split-string-if (subseq string pos) predicate)))))
139 (defun concatenate-strings (strings &optional delimiter)
140 (if (not (rest strings))
145 (if delimiter (string delimiter) "")
146 (concatenate-strings (rest strings) delimiter))))
148 (defun string-prefix-p (prefix string)
150 (>= (length string) (length prefix))
151 (string= prefix string :end2 (length prefix))))
153 (defun get-all (plist property)
154 (multiple-value-bind (property value tail)
155 (get-properties plist (list property))
157 (cons value (get-all (cddr tail) property)))))
159 (defun plist-remove (plist property)
161 (if (eq (first plist) property)
162 (plist-remove (cddr plist) property)
164 (first plist) (second plist) (plist-remove (cddr plist) property)))))
169 (defun utf-8-encode (code)
170 (labels ((encode-bytes (bit)
174 #x80 (byte 7 6) (ldb (byte bit (- bit 6)) code))
175 (encode-bytes (- bit 6)))))
176 (encode-string (length)
177 (map 'string #'code-char
180 (mask-field (byte 7 (- 7 length)) #xFF)
181 (byte 7 (- 6 length))
182 (ldb (byte (+ (* length 6) 6) (* length 6)) code))
183 (encode-bytes (* length 6))))))
185 ((< code #x80) (string (code-char code)))
186 ((< code #x800) (encode-string 1))
187 ((< code #x10000) (encode-string 2))
188 ((< code #x200000) (encode-string 3))
189 ((< code #x4000000) (encode-string 4))
190 ((< code #x80000000) (encode-string 5))
191 (t (error "Invalid char code ~A" code)))))
194 (defun latin1-to-unicode (string)
196 #'(lambda (str1 str2)
197 (concatenate 'string str1 str2))
198 (map 'list #'(lambda (char) (utf-8-encode (char-code char))) string)))