annotate lisp/cl-extra.el @ 5162:41262f87eb39

Handle (function ...) specially, cl-prettyprint. 2010-03-21 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (cl-prettyprint): Handle (function ...) specially here, as we do (quote ...).
author Aidan Kehoe <kehoea@parhasard.net>
date Sun, 21 Mar 2010 13:20:35 +0000
parents 868a9ffcc37b
children 2d0937dc83cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 502
diff changeset
1 ;;; cl-extra.el --- Common Lisp extensions for XEmacs Lisp (part two)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
3 ;; Copyright (C) 1993,2000,2003 Free Software Foundation, Inc.
801
2b676dc88c66 [xemacs-hg @ 2002-04-01 03:58:02 by ben]
ben
parents: 776
diff changeset
4 ;; Copyright (C) 2002 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; Version: 2.02
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ;; Keywords: extensions, dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ;; XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 ;; under the terms of the GNU General Public License as published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 ;; any later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 ;; XEmacs is distributed in the hope that it will be useful, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 ;; General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 ;; 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
28 ;;; Synched up with: FSF 21.3.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; These are extensions to Emacs Lisp that provide a degree of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; Common Lisp compatibility, beyond what is already built-in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;; in Emacs Lisp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;; This package was written by Dave Gillespie; it is a complete
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 ;; Bug reports, comments, and suggestions are welcome!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 ;; This file contains portions of the Common Lisp extensions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 ;; package which are autoloaded since they are relatively obscure.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 ;; See cl.el for Change Log.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 ;;; Code:
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
50 ;; XEmacs addition
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 (eval-when-compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 (require 'obsolete))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 (or (memq 'cl-19 features)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 (error "Tried to load `cl-extra' before `cl'!"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 ;;; Type coercion.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 (defun coerce (x type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 "Coerce OBJECT to type TYPE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 TYPE is a Common Lisp type specifier."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 (cond ((eq type 'list) (if (listp x) x (append x nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 ((eq type 'string) (if (stringp x) x (concat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 ((eq type 'array) (if (arrayp x) x (vconcat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
69 ;; XEmacs addition character <-> integer coercions
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
70 ((and (eq type 'character) (char-int-p x)) (int-char x))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
71 ((and (eq type 'integer) (characterp x)) (char-int x))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 ((eq type 'float) (float x))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
73 ;; XEmacs addition: enhanced numeric type coercions
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2153
diff changeset
74 ((and-fboundp 'coerce-number
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2153
diff changeset
75 (memq type '(integer ratio bigfloat))
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2153
diff changeset
76 (coerce-number x type)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
77 ;; XEmacs addition: bit-vector coercion
4995
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
78 ((or (eq type 'bit-vector)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
79 (eq type 'simple-bit-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
80 (if (bit-vector-p x) x (apply 'bit-vector (append x nil))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
81 ;; XEmacs addition: weak-list coercion
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 ((eq type 'weak-list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 (if (weak-list-p x) x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 (let ((wl (make-weak-list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 (set-weak-list-list wl (if (listp x) x (append x nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 wl)))
4995
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
87 ((and
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
88 (consp type)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
89 (or (eq (car type) 'vector)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
90 (eq (car type) 'simple-array)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
91 (eq (car type) 'simple-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
92 (cond
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
93 ((equal (cdr-safe type) '(*))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
94 (coerce x 'vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
95 ((equal (cdr-safe type) '(bit))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
96 (coerce x 'bit-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
97 ((equal (cdr-safe type) '(character))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
98 (coerce x 'string)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 ((typep x type) x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 (t (error "Can't coerce %s to type %s" x type))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102
4906
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
103 ;;;;; Predicates.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
104 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
105 ;;;; I'd actually prefer not to have this inline, the space
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
106 ;;;; vs. amount-it's-called trade-off isn't reasonable, but that would
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
107 ;;;; introduce bytecode problems with the compiler macro in cl-macs.el.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
108 ;;(defsubst cl-string-vector-equalp (cl-string cl-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
109 ;; "Helper function for `equalp', which see."
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
110 ;;; (check-argument-type #'stringp cl-string)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
111 ;;; (check-argument-type #'vector cl-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
112 ;; (let ((cl-i (length cl-string))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
113 ;; cl-char cl-other)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
114 ;; (when (= cl-i (length cl-vector))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
115 ;; (while (and (>= (setq cl-i (1- cl-i)) 0)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
116 ;; (or (eq (setq cl-char (aref cl-string cl-i))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
117 ;; (setq cl-other (aref cl-vector cl-i)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
118 ;; (and (characterp cl-other) ; Note we want to call this
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
119 ;; ; as rarely as possible, it
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
120 ;; ; doesn't have a bytecode.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
121 ;; (eq (downcase cl-char) (downcase cl-other))))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
122 ;; (< cl-i 0))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
123 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
124 ;;;; See comment on cl-string-vector-equalp above.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
125 ;;(defsubst cl-bit-vector-vector-equalp (cl-bit-vector cl-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
126 ;; "Helper function for `equalp', which see."
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
127 ;;; (check-argument-type #'bit-vector-p cl-bit-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
128 ;;; (check-argument-type #'vectorp cl-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
129 ;; (let ((cl-i (length cl-bit-vector))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
130 ;; cl-other)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
131 ;; (when (= cl-i (length cl-vector))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
132 ;; (while (and (>= (setq cl-i (1- cl-i)) 0)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
133 ;; (numberp (setq cl-other (aref cl-vector cl-i)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
134 ;; ;; Differs from clisp here.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
135 ;; (= (aref cl-bit-vector cl-i) cl-other)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
136 ;; (< cl-i 0))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
137 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
138 ;;;; These two helper functions call equalp recursively, the two above have no
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
139 ;;;; need to.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
140 ;;(defsubst cl-vector-array-equalp (cl-vector cl-array)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
141 ;; "Helper function for `equalp', which see."
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
142 ;;; (check-argument-type #'vector cl-vector)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
143 ;;; (check-argument-type #'arrayp cl-array)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
144 ;; (let ((cl-i (length cl-vector)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
145 ;; (when (= cl-i (length cl-array))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
146 ;; (while (and (>= (setq cl-i (1- cl-i)) 0)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
147 ;; (equalp (aref cl-vector cl-i) (aref cl-array cl-i))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
148 ;; (< cl-i 0))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
149 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
150 ;;(defsubst cl-hash-table-contents-equalp (cl-hash-table-1 cl-hash-table-2)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
151 ;; "Helper function for `equalp', which see."
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
152 ;; (symbol-macrolet
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
153 ;; ;; If someone has gone and fished the uninterned symbol out of this
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
154 ;; ;; function's constants vector, and subsequently stored it as a value
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
155 ;; ;; in a hash table, it's their own damn fault when
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
156 ;; ;; `cl-hash-table-contents-equalp' gives the wrong answer.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
157 ;; ((equalp-default '#:equalp-default))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
158 ;; (loop
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
159 ;; for x-key being the hash-key in cl-hash-table-1
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
160 ;; using (hash-value x-value)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
161 ;; with y-value = nil
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
162 ;; always (and (not (eq equalp-default
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
163 ;; (setq y-value (gethash x-key cl-hash-table-2
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
164 ;; equalp-default))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
165 ;; (equalp y-value x-value)))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
166 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
167 ;;(defun equalp (x y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
168 ;; "Return t if two Lisp objects have similar structures and contents.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
169 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
170 ;;This is like `equal', except that it accepts numerically equal
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
171 ;;numbers of different types (float, integer, bignum, bigfloat), and also
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
172 ;;compares strings and characters case-insensitively.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
173 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
174 ;;Arrays (that is, strings, bit-vectors, and vectors) of the same length and
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
175 ;;with contents that are `equalp' are themselves `equalp'.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
176 ;;
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
177 ;;Two hash tables are `equalp' if they have the same test (see
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
178 ;;`hash-table-test'), if they have the same number of entries, and if, for
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
179 ;;each entry in one hash table, its key is equivalent to a key in the other
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
180 ;;hash table using the hash table test, and its value is `equalp' to the other
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
181 ;;hash table's value for that key."
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
182 ;; (cond ((eq x y))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
183 ;; ((stringp x)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
184 ;; (if (stringp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
185 ;; (eq t (compare-strings x nil nil y nil nil t))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
186 ;; (if (vectorp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
187 ;; (cl-string-vector-equalp x y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
188 ;; ;; bit-vectors and strings are only equalp if they're
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
189 ;; ;; zero-length:
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
190 ;; (and (equal "" x) (equal #* y)))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
191 ;; ((numberp x)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
192 ;; (and (numberp y) (= x y)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
193 ;; ((consp x)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
194 ;; (while (and (consp x) (consp y) (equalp (car x) (car y)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
195 ;; (setq x (cdr x) y (cdr y)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
196 ;; (and (not (consp x)) (equalp x y)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
197 ;; (t
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
198 ;; ;; From here on, the type tests don't (yet) have bytecodes.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
199 ;; (let ((x-type (type-of x)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
200 ;; (cond ((eq 'vector x-type)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
201 ;; (if (stringp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
202 ;; (cl-string-vector-equalp y x)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
203 ;; (if (vectorp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
204 ;; (cl-vector-array-equalp x y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
205 ;; (if (bit-vector-p y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
206 ;; (cl-bit-vector-vector-equalp y x)))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
207 ;; ((eq 'character x-type)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
208 ;; (and (characterp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
209 ;; ;; If the characters are actually identical, the
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
210 ;; ;; first eq test will have caught them above; we only
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
211 ;; ;; need to check them case-insensitively here.
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
212 ;; (eq (downcase x) (downcase y))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
213 ;; ((eq 'hash-table x-type)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
214 ;; (and (hash-table-p y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
215 ;; (eq (hash-table-test x) (hash-table-test y))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
216 ;; (= (hash-table-count x) (hash-table-count y))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
217 ;; (cl-hash-table-contents-equalp x y)))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
218 ;; ((eq 'bit-vector x-type)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
219 ;; (if (bit-vector-p y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
220 ;; (equal x y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
221 ;; (if (vectorp y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
222 ;; (cl-bit-vector-vector-equalp x y)
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
223 ;; ;; bit-vectors and strings are only equalp if they're
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
224 ;; ;; zero-length:
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
225 ;; (and (equal "" y) (equal #* x)))))
6ef8256a020a implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents: 4800
diff changeset
226 ;; (t (equal x y)))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227
4997
8800b5350a13 Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4996
diff changeset
228 ;; XEmacs; #'map, #'mapc, #'mapl, #'maplist, #'mapcon, #'some and #'every
8800b5350a13 Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4996
diff changeset
229 ;; are now in C, together with #'map-into, which was never in this file.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 (defun notany (cl-pred cl-seq &rest cl-rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 "Return true if PREDICATE is false of every element of SEQ or SEQs."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 (not (apply 'some cl-pred cl-seq cl-rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 (defun notevery (cl-pred cl-seq &rest cl-rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 "Return true if PREDICATE is false of some element of SEQ or SEQs."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 (not (apply 'every cl-pred cl-seq cl-rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 ;;; Support for `loop'.
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
240 (defalias 'cl-map-keymap 'map-keymap)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 (or cl-base
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
244 (setq cl-base (copy-sequence [0])))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
245 (map-keymap
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 (lambda (cl-key cl-bind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 (aset cl-base (1- (length cl-base)) cl-key)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 (if (keymapp cl-bind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 (cl-map-keymap-recursively
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 cl-func-rec cl-bind
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
252 (vconcat cl-base (list 0)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (funcall cl-func-rec cl-base cl-bind))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 cl-map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 (or cl-what (setq cl-what (current-buffer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 (if (bufferp cl-what)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
260 (with-current-buffer cl-what
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 (setq cl-mark (copy-marker (or cl-start (point-min))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
264 (setq cl-next (if cl-prop (next-single-property-change
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
265 cl-mark cl-prop cl-what)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
266 (next-property-change cl-mark cl-what))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
267 cl-next2 (or cl-next (with-current-buffer cl-what
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
268 (point-max))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 (funcall cl-func (prog1 (marker-position cl-mark)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 (set-marker cl-mark cl-next2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 (or cl-start (setq cl-start 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 (or cl-end (setq cl-end (length cl-what)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 (while (< cl-start cl-end)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
276 (let ((cl-next (or (if cl-prop (next-single-property-change
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
277 cl-start cl-prop cl-what)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
278 (next-property-change cl-start cl-what))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 (funcall cl-func cl-start (min cl-next cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 (setq cl-start cl-next)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 (or cl-buffer (setq cl-buffer (current-buffer)))
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
285 (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
286 (if-fboundp 'overlay-lists
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
288 ;; This is the preferred algorithm, though overlay-lists is
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
289 ;; undocumented.
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
290 (let (cl-ovl)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
291 (with-current-buffer cl-buffer
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
292 (setq cl-ovl (overlay-lists))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
293 (if cl-start (setq cl-start (copy-marker cl-start)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
294 (if cl-end (setq cl-end (copy-marker cl-end))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
295 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
296 (while (and cl-ovl
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
297 (or (not (overlay-start (car cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
298 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
299 (and cl-start (<= (overlay-end (car cl-ovl))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
300 cl-start))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
301 (not (funcall cl-func (car cl-ovl) cl-arg))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
302 (setq cl-ovl (cdr cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
303 (if cl-start (set-marker cl-start nil))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
304 (if cl-end (set-marker cl-end nil)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
305
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
306 ;; This alternate algorithm fails to find zero-length overlays.
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
307 (let ((cl-mark (with-current-buffer cl-buffer
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
308 (copy-marker (or cl-start (point-min)))))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
309 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
310 (copy-marker cl-end))))
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
311 cl-pos cl-ovl)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
312 (while (save-excursion
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
313 (and (setq cl-pos (marker-position cl-mark))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
314 (< cl-pos (or cl-mark2 (point-max)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
315 (progn
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
316 (set-buffer cl-buffer)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
317 (setq cl-ovl (overlays-at cl-pos))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
318 (set-marker cl-mark (next-overlay-change cl-pos)))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
319 (while (and cl-ovl
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
320 (or (/= (overlay-start (car cl-ovl)) cl-pos)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
321 (not (and (funcall cl-func (car cl-ovl) cl-arg)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
322 (set-marker cl-mark nil)))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
323 (setq cl-ovl (cdr cl-ovl))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
324 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 ;;; Support for `setf'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 (defun cl-set-frame-visible-p (frame val)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 (cond ((null val) (make-frame-invisible frame))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 ((eq val 'icon) (iconify-frame frame))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 (t (make-frame-visible frame)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 val)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 ;;; Support for `progv'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 (defvar cl-progv-save)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 (defun cl-progv-before (syms values)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 (while syms
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
337 (push (if (boundp (car syms))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 (cons (car syms) (symbol-value (car syms)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 (car syms)) cl-progv-save)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 (if values
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
341 (set (pop syms) (pop values))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
342 (makunbound (pop syms)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 (defun cl-progv-after ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 (while cl-progv-save
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 (if (consp (car cl-progv-save))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 (makunbound (car cl-progv-save)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
349 (pop cl-progv-save)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 ;;; Numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 (defun gcd (&rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 "Return the greatest common divisor of the arguments."
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
356 (let ((a (abs (or (pop args) 0))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 (while args
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
358 (let ((b (abs (pop args))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 (while (> b 0) (setq b (% a (setq a b))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 a))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 (defun lcm (&rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 "Return the least common multiple of the arguments."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 (if (memq 0 args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 0
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
366 (let ((a (abs (or (pop args) 1))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 (while args
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
368 (let ((b (abs (pop args))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 (setq a (* (/ a (gcd a b)) b))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 a)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 (defun isqrt (a)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 "Return the integer square root of the argument."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 (if (and (integerp a) (> a 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 ((>= a 100) 100) (t 10)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 g2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 (setq g g2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 g)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 (if (eq a 0) 0 (signal 'arith-error nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
384 ;; XEmacs addition
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 (defun cl-expt (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 "Return X raised to the power of Y. Works only for integer arguments."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 (defalias 'expt 'cl-expt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
392 ;; We can't use macrolet in this file; whence the literal macro
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
393 ;; definition-and-call:
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
394 ((macro . (lambda (&rest symbols)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
395 "Make some old CL package truncate and round functions available.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
397 These functions are now implemented in C; their Lisp implementations in this
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
398 XEmacs are trivial, so we provide them and mark them obsolete."
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
399 (let (symbol result)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
400 (while symbols
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
401 (setq symbol (car symbols)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
402 symbols (cdr symbols))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
403 (push `(make-obsolete ',(intern (format "%s*" symbol))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
404 ',symbol "21.5.29")
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
405 result)
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
406 (push
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
407 `(defun ,(intern (format "%s*" symbol)) (number &optional divisor)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
408 ,(format "See `%s'. This returns a list, not multiple values."
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
409 symbol)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
410 (multiple-value-list (,symbol number divisor)))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
411 result))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
412 (cons 'progn result))))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
413 ceiling floor round truncate)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 (defun mod* (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 "The remainder of X divided by Y, with the same sign as Y."
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
417 (nth-value 1 (floor x y)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 (defun rem* (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 "The remainder of X divided by Y, with the same sign as X."
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
421 (nth-value 1 (truncate x y)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 (defun signum (a)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 "Return 1 if A is positive, -1 if negative, 0 if zero."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 ;; Random numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 (defvar *random-state*)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 (defun random* (lim &optional state)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 "Return a random nonnegative number less than LIM, an integer or float.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 Optional second arg STATE is a random-state object."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 (or state (setq state *random-state*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 (let ((vec (aref state 3)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 (if (integerp vec)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 (aset state 3 (setq vec (make-vector 55 nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 (aset vec 0 j)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 (while (> (setq i (% (+ i 21) 55)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 (aset vec i (setq j (prog1 k (setq k (- j k))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 (while (< (setq i (1+ i)) 200) (random* 2 state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 (j (aset state 2 (% (1+ (aref state 2)) 55)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 (if (integerp lim)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 (if (<= lim 512) (% n lim)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 (let ((mask 1023))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 (* (/ n '8388608e0) lim)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 (defun make-random-state (&optional state)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 If STATE is t, return a new state object seeded from the time of day."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 (cond ((null state) (make-random-state *random-state*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 ((vectorp state) (cl-copy-tree state t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 (t (make-random-state (cl-random-time)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 (defun random-state-p (object)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 "Return t if OBJECT is a random-state object."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 (and (vectorp object) (= (length object) 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 (eq (aref object 0) 'cl-random-state-tag)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 ;; Implementation limits.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 (defun cl-finite-do (func a b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 (condition-case nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 (let ((res (funcall func a b))) ; check for IEEE infinity
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 (and (numberp res) (/= res (/ res 2)) res))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 (arith-error nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 (defvar most-positive-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 (defvar most-negative-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 (defvar least-positive-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 (defvar least-negative-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 (defvar least-positive-normalized-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 (defvar least-negative-normalized-float)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 (defvar float-epsilon)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 (defvar float-negative-epsilon)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 (defun cl-float-limits ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 (or most-positive-float (not (numberp '2e1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 (let ((x '2e0) y z)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 ;; Find maximum exponent (first two loops are optimizations)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 (while (cl-finite-do '* x x) (setq x (* x x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 (while (cl-finite-do '+ x x) (setq x (+ x x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 (setq z x y (/ x 2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 ;; Now fill in 1's in the mantissa.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 (setq x (+ x y) y (/ y 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 (setq most-positive-float x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 most-negative-float (- x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 ;; Divide down until mantissa starts rounding.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 (setq x (/ x z) y (/ 16 z) x (* x y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 (arith-error nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 (setq x (/ x 2) y (/ y 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 (setq least-positive-normalized-float y
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 least-negative-normalized-float (- y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 ;; Divide down until value underflows to zero.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 (setq x (/ 1 z) y x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (while (condition-case nil (> (/ x 2) 0) (arith-error nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 (setq least-positive-float x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 least-negative-float (- x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 (setq x '1e0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 (setq float-epsilon (* x 2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 (setq x '1e0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 (setq float-negative-epsilon (* x 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 ;;; Sequence functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 ;XEmacs -- our built-in is more powerful.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 ;(defun subseq (seq start &optional end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 ; "Return the subsequence of SEQ from START to END.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 ;If END is omitted, it defaults to the length of the sequence.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 ;If START or END is negative, it counts from the end."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 ; (if (stringp seq) (substring seq start end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 ; (let (len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 ; (and end (< end 0) (setq end (+ end (setq len (length seq)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 ; (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 ; (cond ((listp seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 ; (if (> start 0) (setq seq (nthcdr start seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 ; (if end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 ; (let ((res nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 ; (while (>= (setq end (1- end)) start)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
536 ; (push (pop seq) res))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 ; (nreverse res))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 ; (copy-sequence seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 ; (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 ; (or end (setq end (or len (length seq))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 ; (let ((res (make-vector (max (- end start) 0) nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 ; (i 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 ; (while (< start end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 ; (aset res i (aref seq start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 ; (setq i (1+ i) start (1+ start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 ; res))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 (defun concatenate (type &rest seqs)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
550 ;; XEmacs change: use case instead of cond for clarity
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 (case type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 (vector (apply 'vconcat seqs))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 (string (apply 'concat seqs))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 (list (apply 'append (append seqs '(nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 (t (error "Not a sequence type name: %s" type))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 ;;; List functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 (defun revappend (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 "Equivalent to (append (reverse X) Y)."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 (nconc (reverse x) y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 (defun nreconc (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 "Equivalent to (nconc (nreverse X) Y)."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 (nconc (nreverse x) y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 (defun list-length (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 "Return the length of a list. Return nil if list is circular."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 (let ((n 0) (fast x) (slow x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 (if fast (if (cdr fast) nil (1+ n)) n)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 (defun tailp (sublist list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 "Return true if SUBLIST is a tail of LIST."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 (while (and (consp list) (not (eq sublist list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 (setq list (cdr list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (if (numberp sublist) (equal sublist list) (eq sublist list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
580 (defalias 'cl-copy-tree 'copy-tree)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 ;;; Property lists.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 ;; XEmacs: our `get' groks DEFAULT.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 (defalias 'get* 'get)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
587 (defalias 'getf 'plist-get)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 (defun cl-set-getf (plist tag val)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 (let ((p plist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 (defun cl-do-remf (plist tag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 (let ((p (cdr plist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
599 ;; XEmacs change: we have a builtin remprop
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
600 (defalias 'cl-remprop 'remprop)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
601
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
602 (defun get-properties (plist indicator-list)
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
603 "Find a property from INDICATOR-LIST in PLIST.
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
604 Return 3 values:
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
605 - the first property found,
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
606 - its value,
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
607 - the tail of PLIST beginning with the found entry."
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
608 (do ((plst plist (cddr plst)))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
609 ((null plst) (values nil nil nil))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
610 (cond ((atom (cdr plst))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
611 (error "Malformed property list: %S." plist))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
612 ((memq (car plst) indicator-list)
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
613 (return (values (car plst) (cadr plst) plst))))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
614
5075
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
615 ;; See also the compiler macro in cl-macs.el.
5056
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
616 (defun constantly (value &rest more-values)
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
617 "Construct a function always returning VALUE, and possibly MORE-VALUES.
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
618
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
619 The constructed function accepts any number of arguments, and ignores them.
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
620
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
621 Members of MORE-VALUES, if provided, will be passed as multiple values; see
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
622 `multiple-value-bind' and `multiple-value-setq'."
5075
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
623 (symbol-macrolet
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
624 ((arglist '(&rest ignore)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
625 (if (or more-values (eval-when-compile (not (cl-compiling-file))))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
626 `(lambda ,arglist (values-list ',(cons value more-values)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
627 (make-byte-code
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
628 arglist
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
629 (eval-when-compile
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
630 (let ((compiled (byte-compile-sexp #'(lambda (&rest ignore)
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
631 (declare (ignore ignore))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
632 'placeholder))))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
633 (assert (and
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
634 (equal [placeholder]
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
635 (compiled-function-constants compiled))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
636 (= 1 (compiled-function-stack-depth compiled)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
637 t
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
638 "Our assumptions about compiled code appear not to hold.")
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
639 (compiled-function-instructions compiled)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
640 (vector value) 1))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
641
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 ;;; Hash tables.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 ;; The `regular' Common Lisp hash-table stuff has been moved into C.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 ;; Only backward compatibility stuff remains here.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 (defun make-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 (make-hash-table :test test :size size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 (defun make-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 (make-hash-table :test test :size size :weakness t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 (defun make-key-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 (make-hash-table :test test :size size :weakness 'key))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 (defun make-value-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 (make-hash-table :test test :size size :weakness 'value))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 (define-obsolete-function-alias 'hashtablep 'hash-table-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 (define-obsolete-function-alias 'hashtable-type 'hash-table-type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 (define-obsolete-function-alias 'hashtable-size 'hash-table-size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 (make-obsolete 'make-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 (make-obsolete 'make-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 (make-obsolete 'make-key-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 (make-obsolete 'make-value-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 (make-obsolete 'hash-table-type 'hash-table-weakness)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 (when (fboundp 'x-keysym-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 ;; Compatibility stuff for old kludgy cl.el hash table implementation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 (defvar cl-builtin-gethash (symbol-function 'gethash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 (defvar cl-builtin-remhash (symbol-function 'remhash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 (defvar cl-builtin-clrhash (symbol-function 'clrhash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 (defvar cl-builtin-maphash (symbol-function 'maphash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 (defalias 'cl-gethash 'gethash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 (defalias 'cl-puthash 'puthash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 (defalias 'cl-remhash 'remhash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 (defalias 'cl-clrhash 'clrhash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 (defalias 'cl-maphash 'maphash)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
682 ;; These three actually didn't exist in Emacs-20.
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
683 (defalias 'cl-make-hash-table 'make-hash-table)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
684 (defalias 'cl-hash-table-p 'hash-table-p)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
685 (defalias 'cl-hash-table-count 'hash-table-count)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 ;;; Some debugging aids.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 (defun cl-prettyprint (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
691 (let ((pt (point)) last just)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 (insert "\n" (prin1-to-string form) "\n")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 (setq last (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 (goto-char (1+ pt))
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
695 (while (re-search-forward "(\\(?:\\(?:function\\|quote\\) \\)" last t)
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
696 (delete-region (match-beginning 0) (match-end 0))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
697 (if (= (length "(function ") (- (match-end 0) (match-beginning 0)))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
698 (insert "#'")
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
699 (insert "'"))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
700 (setq just (point))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 (forward-sexp)
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
702 (delete-char 1)
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
703 (goto-char just))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 (goto-char (1+ pt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 (cl-do-prettyprint)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707 (defun cl-do-prettyprint ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 (skip-chars-forward " ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709 (if (looking-at "(")
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
710 (let ((skip (or (looking-at "((")
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
711 ;; XEmacs: be selective about trailing stuff after prog
1729
175ee2cb4d3a [xemacs-hg @ 2003-09-30 22:09:31 by youngs]
youngs
parents: 801
diff changeset
712 (looking-at "(prog[nv12\\(ress-feedback\\|n-with-message\\)]")
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 (looking-at "(unwind-protect ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 (looking-at "(function (")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 (looking-at "(cl-block-wrapper ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 (set (looking-at "(p?set[qf] ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 (if (or skip let
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 (forward-sexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 (and (>= (current-column) 78) (progn (backward-sexp) t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 (let ((nl t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 (cl-do-prettyprint)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 (or skip (looking-at ")") (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 (or (not two) (looking-at ")") (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 (while (not (looking-at ")"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 (if set (setq nl (not nl)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 (if nl (insert "\n"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 (lisp-indent-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 (forward-char 1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 (forward-sexp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 (defvar cl-macroexpand-cmacs nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 (defvar cl-closure-vars nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 (defun cl-macroexpand-all (form &optional env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 "Expand all macro calls through a Lisp FORM.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 This also does some trivial optimizations to make the form prettier."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 (while (or (not (eq form (setq form (macroexpand form env))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 (and cl-macroexpand-cmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 (not (eq form (setq form (compiler-macroexpand form)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 (cond ((not (consp form)) form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 ((memq (car form) '(let let*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 (if (null (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 (cl-macroexpand-all (cons 'progn (cddr form)) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 (let ((letf nil) (res nil) (lets (cadr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 (while lets
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
751 (push (if (consp (car lets))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 (let ((exp (cl-macroexpand-all (caar lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 (or (symbolp exp) (setq letf t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 (cons exp (cl-macroexpand-body (cdar lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 (let ((exp (cl-macroexpand-all (car lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 (if (symbolp exp) exp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 (setq letf t) (list exp nil)))) res)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 (setq lets (cdr lets)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 ((eq (car form) 'cond)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 (cons (car form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 ((eq (car form) 'condition-case)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 (mapcar (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 (cons (car x) (cl-macroexpand-body (cdr x) env))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 (cdddr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 ((memq (car form) '(quote function))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 (if (eq (car-safe (nth 1 form)) 'lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 (let ((body (cl-macroexpand-body (cddadr form) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 (if (and cl-closure-vars (eq (car form) 'function)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 (cl-expr-contains-any body cl-closure-vars))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 (let* ((new (mapcar 'gensym cl-closure-vars))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 (sub (pairlis cl-closure-vars new)) (decls nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 (while (or (stringp (car body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 (eq (car-safe (car body)) 'interactive))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
780 (push (list 'quote (pop body)) decls))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 (put (car (last cl-closure-vars)) 'used t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782 (append
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 (sublis sub (nreverse decls))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 (list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 (list* 'list '(quote apply)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
787 ;; XEmacs: put a quote before the function
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 (list 'list '(quote quote)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 (list 'function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 (list* 'lambda
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 (append new (cadadr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 (sublis sub body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 (nconc (mapcar (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
795 (list 'list '(quote quote) x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 cl-closure-vars)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 '((quote --cl-rest--)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 (list (car form) (list* 'lambda (cadadr form) body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 (let ((found (assq (cadr form) env)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
800 ;; XEmacs: cadr/caddr operate on nil without errors
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 (if (eq (cadr (caddr found)) 'cl-labels-args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 ((memq (car form) '(defun defmacro))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 ((and (eq (car form) 'progn) (not (cddr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 (cl-macroexpand-all (nth 1 form) env))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 ((eq (car form) 'setq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 (while (and p (symbolp (car p))) (setq p (cddr p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 (defun cl-macroexpand-body (body &optional env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 (defun cl-prettyexpand (form &optional full)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 (message "Expanding...")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 (byte-compile-macro-environment nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 (setq form (cl-macroexpand-all form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 (and (not full) '((block) (eval-when)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 (message "Formatting...")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824 (prog1 (cl-prettyprint form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 (message ""))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 (run-hooks 'cl-extra-load-hook)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
831 ;; XEmacs addition
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 (provide 'cl-extra)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
834 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 ;;; cl-extra.el ends here