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.4 2005-04-17 21:38:15 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 #+cmu`(system:without-gcing ,@body)
49 #+sbcl`(sb-impl::without-gcing ,@body))
52 (if (and obj (atom obj)) (list obj) obj))
55 (and (symbolp obj) (not (member obj '(t nil)))))
57 (defun all-equal (&rest objects)
62 (equal (first objects) (second objects))
63 (apply #'all-equal (rest objects)))))
65 (defun neq (obj1 obj2)
68 (defmacro return-if (form)
69 (let ((result (make-symbol "RESULT")))
70 `(let ((,result ,form))
74 (defun make-pointer (address)
77 (defun null-pointer-p (pointer)
78 (zerop (sap-int pointer)))
81 (defmacro when-bind ((var expr) &body body)
87 (defmacro assoc-ref (key alist &key (test #'eq))
88 `(cdr (assoc ,key ,alist :test ,test)))
91 (defmacro assoc-lref (key alist &key (test #'eq))
92 `(cadr (assoc ,key ,alist :test ,test)))
95 (defun assoc-rem (key alist &key (test #'eq))
96 (remove-if #'(lambda (element) (funcall test key (car element))) alist))
99 (defun assoc-delete (key alist &key (test #'eq))
100 (delete-if #'(lambda (element) (funcall test key (car element))) alist))
103 (defun funcallable (object)
108 (defun intersection-p (list1 list2 &key (test #'eq))
109 (dolist (obj list1 nil)
110 (when (member obj list2 :test test)
111 (return-from intersection-p t))))
114 (defun split-string (string delimiter)
115 (declare (simple-string string) (character delimiter))
116 (let ((pos (position delimiter string)))
120 (subseq string 0 pos)
121 (split-string (subseq string (1+ pos)) delimiter)))))
123 (defun split-string-if (string predicate)
124 (declare (simple-string string))
125 (let ((pos (position-if predicate string :start 1)))
129 (subseq string 0 pos)
130 (split-string-if (subseq string pos) predicate)))))
132 (defun concatenate-strings (strings &optional delimiter)
133 (if (not (rest strings))
138 (if delimiter (string delimiter) "")
139 (concatenate-strings (rest strings) delimiter))))
141 (defun string-prefix-p (prefix string)
143 (>= (length string) (length prefix))
144 (string= prefix string :end2 (length prefix))))
146 (defun get-all (plist property)
147 (multiple-value-bind (property value tail)
148 (get-properties plist (list property))
150 (cons value (get-all (cddr tail) property)))))
152 (defun plist-remove (plist property)
154 (if (eq (first plist) property)
155 (plist-remove (cddr plist) property)
157 (first plist) (second plist) (plist-remove (cddr plist) property)))))
162 (defun utf-8-encode (code)
163 (labels ((encode-bytes (bit)
167 #x80 (byte 7 6) (ldb (byte bit (- bit 6)) code))
168 (encode-bytes (- bit 6)))))
169 (encode-string (length)
170 (map 'string #'code-char
173 (mask-field (byte 7 (- 7 length)) #xFF)
174 (byte 7 (- 6 length))
175 (ldb (byte (+ (* length 6) 6) (* length 6)) code))
176 (encode-bytes (* length 6))))))
178 ((< code #x80) (string (code-char code)))
179 ((< code #x800) (encode-string 1))
180 ((< code #x10000) (encode-string 2))
181 ((< code #x200000) (encode-string 3))
182 ((< code #x4000000) (encode-string 4))
183 ((< code #x80000000) (encode-string 5))
184 (t (error "Invalid char code ~A" code)))))
187 (defun latin1-to-unicode (string)
189 #'(lambda (str1 str2)
190 (concatenate 'string str1 str2))
191 (map 'list #'(lambda (char) (utf-8-encode (char-code char))) string)))