annotate lisp/emacs-lisp/cl-extra.el @ 23953:c8cdb4e487cb

(Fsingle_key_description): Handle (control ?x) etc. (Fkey_description): Handle lists like vectors.
author Richard M. Stallman <rms@gnu.org>
date Tue, 29 Dec 1998 22:53:25 +0000
parents a74507d555ba
children a134726a4a15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16057
a74507d555ba Turn on byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents: 15029
diff changeset
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*-byte-compile-dynamic: t;-*-
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Version: 2.02
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Keywords: extensions
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
12244
ac7375e60931 Update GPL to version 2.
Karl Heuer <kwzh@gnu.org>
parents: 7942
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13970
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13970
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13970
diff changeset
24 ;; Boston, MA 02111-1307, USA.
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
7942
bc5dccc5375f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 4517
diff changeset
26 ;;; Commentary:
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; These are extensions to Emacs Lisp that provide a degree of
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; Common Lisp compatibility, beyond what is already built-in
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; in Emacs Lisp.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; This package was written by Dave Gillespie; it is a complete
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; Bug reports, comments, and suggestions are welcome!
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;; This file contains portions of the Common Lisp extensions
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; package which are autoloaded since they are relatively obscure.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; See cl.el for Change Log.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44
7942
bc5dccc5375f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 4517
diff changeset
45 ;;; Code:
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 (or (memq 'cl-19 features)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (error "Tried to load `cl-extra' before `cl'!"))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;;; We define these here so that this file can compile without having
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;;; loaded the cl.el file already.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 (defmacro cl-pop (place)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 (defvar cl-emacs-type)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 ;;; Type coercion.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (defun coerce (x type)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 "Coerce OBJECT to type TYPE.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 TYPE is a Common Lisp type specifier."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (cond ((eq type 'list) (if (listp x) x (append x nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 ((eq type 'string) (if (stringp x) x (concat x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 ((eq type 'array) (if (arrayp x) x (vconcat x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 ((eq type 'float) (float x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 ((typep x type) x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 (t (error "Can't coerce %s to type %s" x type))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 ;;; Predicates.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (defun equalp (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 "T if two Lisp objects have similar structures and contents.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 This is like `equal', except that it accepts numerically equal
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 numbers of different types (float vs. integer), and also compares
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 strings case-insensitively."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 (cond ((eq x y) t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 ((stringp x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 (and (stringp y) (= (length x) (length y))
14794
c3a2cabb73ef (equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents: 14762
diff changeset
87 (or (string-equal x y)
c3a2cabb73ef (equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents: 14762
diff changeset
88 (string-equal (downcase x) (downcase y))))) ; lazy but simple!
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 ((numberp x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 (and (numberp y) (= x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 ((consp x)
14762
624d5547a6d6 (equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
92 (while (and (consp x) (consp y) (equalp (car x) (car y)))
624d5547a6d6 (equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
93 (setq x (cdr x) y (cdr y)))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 (and (not (consp x)) (equalp x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 ((vectorp x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 (and (vectorp y) (= (length x) (length y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 (let ((i (length x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (while (and (>= (setq i (1- i)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 (equalp (aref x i) (aref y i))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (< i 0))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (t (equal x y))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 ;;; Control structures.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (defun cl-mapcar-many (cl-func cl-seqs)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 (if (cdr (cdr cl-seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 (let* ((cl-res nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (cl-n (apply 'min (mapcar 'length cl-seqs)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (cl-i 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 (cl-args (copy-sequence cl-seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 cl-p1 cl-p2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (setq cl-seqs (copy-sequence cl-seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 (while (< cl-i cl-n)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (setq cl-p1 cl-seqs cl-p2 cl-args)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (while cl-p1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (setcar cl-p2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 (if (consp (car cl-p1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (prog1 (car (car cl-p1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (setcar cl-p1 (cdr (car cl-p1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (aref (car cl-p1) cl-i)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (cl-push (apply cl-func cl-args) cl-res)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 (setq cl-i (1+ cl-i)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (nreverse cl-res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (let ((cl-res nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 (cl-x (car cl-seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 (cl-y (nth 1 cl-seqs)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 (let ((cl-n (min (length cl-x) (length cl-y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 (cl-i -1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 (while (< (setq cl-i (1+ cl-i)) cl-n)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 (cl-push (funcall cl-func
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 (if (consp cl-x) (cl-pop cl-x) (aref cl-x cl-i))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 (if (consp cl-y) (cl-pop cl-y) (aref cl-y cl-i)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 cl-res)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 (nreverse cl-res))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (defun map (cl-type cl-func cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 "Map a function across one or more sequences, returning a sequence.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 TYPE is the sequence type to return, FUNC is the function, and SEQS
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 are the argument sequences."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (and cl-type (coerce cl-res cl-type))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (defun maplist (cl-func cl-list &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 "Map FUNC to each sublist of LIST or LISTS.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 Like `mapcar', except applies to lists and their cdr's rather than to
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 the elements themselves."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (if cl-rest
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (let ((cl-res nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (cl-args (cons cl-list (copy-sequence cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 cl-p)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (while (not (memq nil cl-args))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (cl-push (apply cl-func cl-args) cl-res)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (setq cl-p cl-args)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 (while cl-p (setcar cl-p (cdr (cl-pop cl-p)) )))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (nreverse cl-res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (let ((cl-res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 (while cl-list
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (cl-push (funcall cl-func cl-list) cl-res)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (setq cl-list (cdr cl-list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (nreverse cl-res))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (defun mapc (cl-func cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 "Like `mapcar', but does not accumulate values returned by the function."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (if cl-rest
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (apply 'map nil cl-func cl-seq cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (mapcar cl-func cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (defun mapl (cl-func cl-list &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 "Like `maplist', but does not accumulate values returned by the function."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (if cl-rest
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (apply 'maplist cl-func cl-list cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (let ((cl-p cl-list))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 cl-list)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (defun mapcan (cl-func cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 "Like `mapcar', but nconc's together the values returned by the function."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (defun mapcon (cl-func cl-list &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 "Like `maplist', but nconc's together the values returned by the function."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (defun some (cl-pred cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 "Return true if PREDICATE is true of any element of SEQ or SEQs.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 If so, return the true (non-nil) value returned by PREDICATE."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (if (or cl-rest (nlistp cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (catch 'cl-some
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (apply 'map nil
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (function (lambda (&rest cl-x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (let ((cl-res (apply cl-pred cl-x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (if cl-res (throw 'cl-some cl-res)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 cl-seq cl-rest) nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (let ((cl-x nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 cl-x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (defun every (cl-pred cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 "Return true if PREDICATE is true of every element of SEQ or SEQs."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (if (or cl-rest (nlistp cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (catch 'cl-every
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (apply 'map nil
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (function (lambda (&rest cl-x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 cl-seq cl-rest) t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (while (and cl-seq (funcall cl-pred (car cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (setq cl-seq (cdr cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (null cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (defun notany (cl-pred cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 "Return true if PREDICATE is false of every element of SEQ or SEQs."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 (not (apply 'some cl-pred cl-seq cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (defun notevery (cl-pred cl-seq &rest cl-rest)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 "Return true if PREDICATE is false of some element of SEQ or SEQs."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (not (apply 'every cl-pred cl-seq cl-rest)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 ;;; Support for `loop'.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (defun cl-map-keymap (cl-func cl-map)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 (if (eq cl-emacs-type 'lucid) (funcall 'map-keymap cl-func cl-map)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (if (listp cl-map)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (let ((cl-p cl-map))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 (while (consp (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (cond ((consp (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (funcall cl-func (car (car cl-p)) (cdr (car cl-p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 ((vectorp (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (cl-map-keymap cl-func (car cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 ((eq (car cl-p) 'keymap)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (setq cl-p nil)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (let ((cl-i -1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (while (< (setq cl-i (1+ cl-i)) (length cl-map))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (if (aref cl-map cl-i)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (funcall cl-func cl-i (aref cl-map cl-i))))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (or cl-base
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (setq cl-base (copy-sequence (if (eq cl-emacs-type 18) "0" [0]))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 (cl-map-keymap
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (lambda (cl-key cl-bind)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (aset cl-base (1- (length cl-base)) cl-key)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (if (keymapp cl-bind)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 (cl-map-keymap-recursively
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 cl-func-rec cl-bind
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 (funcall (if (eq cl-emacs-type 18) 'concat 'vconcat)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 cl-base (list 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (funcall cl-func-rec cl-base cl-bind))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 cl-map))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (or cl-what (setq cl-what (current-buffer)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (if (bufferp cl-what)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (save-excursion
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (set-buffer cl-what)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (setq cl-mark (copy-marker (or cl-start (point-min))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 (setq cl-next (and (fboundp 'next-property-change)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (if cl-prop (next-single-property-change
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 cl-mark cl-prop cl-what)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (next-property-change cl-mark cl-what)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 cl-next2 (or cl-next (save-excursion
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (set-buffer cl-what) (point-max))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (funcall cl-func (prog1 (marker-position cl-mark)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (set-marker cl-mark cl-next2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (or cl-start (setq cl-start 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 (or cl-end (setq cl-end (length cl-what)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (while (< cl-start cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (let ((cl-next (or (and (fboundp 'next-property-change)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (if cl-prop (next-single-property-change
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 cl-start cl-prop cl-what)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (next-property-change cl-start cl-what)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (funcall cl-func cl-start (min cl-next cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (setq cl-start cl-next)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (or cl-buffer (setq cl-buffer (current-buffer)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 (if (fboundp 'overlay-lists)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 ;; This is the preferred algorithm, though overlay-lists is undocumented.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (let (cl-ovl)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (save-excursion
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 (set-buffer cl-buffer)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 (setq cl-ovl (overlay-lists))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (if cl-start (setq cl-start (copy-marker cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 (if cl-end (setq cl-end (copy-marker cl-end))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 (while (and cl-ovl
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (or (not (overlay-start (car cl-ovl)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (not (funcall cl-func (car cl-ovl) cl-arg))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 (setq cl-ovl (cdr cl-ovl)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (if cl-start (set-marker cl-start nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (if cl-end (set-marker cl-end nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 ;; This alternate algorithm fails to find zero-length overlays.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (let ((cl-mark (save-excursion (set-buffer cl-buffer)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (copy-marker (or cl-start (point-min)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (copy-marker cl-end))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 cl-pos cl-ovl)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (while (save-excursion
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (and (setq cl-pos (marker-position cl-mark))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (< cl-pos (or cl-mark2 (point-max)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (set-buffer cl-buffer)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 (setq cl-ovl (overlays-at cl-pos))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 (set-marker cl-mark (next-overlay-change cl-pos)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (while (and cl-ovl
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (or (/= (overlay-start (car cl-ovl)) cl-pos)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (not (and (funcall cl-func (car cl-ovl) cl-arg)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 (set-marker cl-mark nil)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 (setq cl-ovl (cdr cl-ovl))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 ;;; Support for `setf'.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (defun cl-set-frame-visible-p (frame val)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 (cond ((null val) (make-frame-invisible frame))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 ((eq val 'icon) (iconify-frame frame))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (t (make-frame-visible frame)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 val)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 ;;; Support for `progv'.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (defvar cl-progv-save)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (defun cl-progv-before (syms values)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (while syms
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (cl-push (if (boundp (car syms))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (cons (car syms) (symbol-value (car syms)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (car syms)) cl-progv-save)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (if values
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (set (cl-pop syms) (cl-pop values))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (makunbound (cl-pop syms)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (defun cl-progv-after ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (while cl-progv-save
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (if (consp (car cl-progv-save))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (makunbound (car cl-progv-save)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (cl-pop cl-progv-save)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 ;;; Numbers.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (defun gcd (&rest args)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 "Return the greatest common divisor of the arguments."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (let ((a (abs (or (cl-pop args) 0))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 (while args
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (let ((b (abs (cl-pop args))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (while (> b 0) (setq b (% a (setq a b))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 a))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (defun lcm (&rest args)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 "Return the least common multiple of the arguments."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (if (memq 0 args)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 0
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (let ((a (abs (or (cl-pop args) 1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (while args
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (let ((b (abs (cl-pop args))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (setq a (* (/ a (gcd a b)) b))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 a)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 (defun isqrt (a)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 "Return the integer square root of the argument."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (if (and (integerp a) (> a 0))
15029
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
374 (let ((g (cond ((<= a 100) 10) ((<= a 10000) 100)
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
375 ((<= a 1000000) 1000) (t a)))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 g2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (setq g g2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 g)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 (if (eq a 0) 0 (signal 'arith-error nil))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 (defun cl-expt (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 "Return X raised to the power of Y. Works only for integer arguments."
15029
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
384 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (defalias 'expt 'cl-expt))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (defun floor* (x &optional y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 "Return a list of the floor of X and the fractional part of X.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 With two arguments, return floor and remainder of their quotient."
4517
45e3868140f1 (floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents: 4355
diff changeset
392 (let ((q (floor x y)))
45e3868140f1 (floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents: 4355
diff changeset
393 (list q (- x (if y (* y q) q)))))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (defun ceiling* (x &optional y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 "Return a list of the ceiling of X and the fractional part of X.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 With two arguments, return ceiling and remainder of their quotient."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (let ((res (floor* x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (if (= (car (cdr res)) 0) res
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 (defun truncate* (x &optional y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 "Return a list of the integer part of X and the fractional part of X.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 With two arguments, return truncation and remainder of their quotient."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (if (eq (>= x 0) (or (null y) (>= y 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (floor* x y) (ceiling* x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (defun round* (x &optional y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 "Return a list of X rounded to the nearest integer and the remainder.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 With two arguments, return rounding and remainder of their quotient."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 (if y
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 (if (and (integerp x) (integerp y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 (let* ((hy (/ y 2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (res (floor* (+ x hy) y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (if (and (= (car (cdr res)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (= (+ hy hy) y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 (/= (% (car res) 2) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 (list (1- (car res)) hy)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (list (car res) (- (car (cdr res)) hy))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 (let ((q (round (/ x y))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 (list q (- x (* q y)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 (if (integerp x) (list x 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 (let ((q (round x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 (list q (- x q))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 (defun mod* (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 "The remainder of X divided by Y, with the same sign as Y."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 (nth 1 (floor* x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 (defun rem* (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 "The remainder of X divided by Y, with the same sign as X."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 (nth 1 (truncate* x y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 (defun signum (a)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 "Return 1 if A is positive, -1 if negative, 0 if zero."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 ;; Random numbers.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 (defvar *random-state*)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 (defun random* (lim &optional state)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 "Return a random nonnegative number less than LIM, an integer or float.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 Optional second arg STATE is a random-state object."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (or state (setq state *random-state*))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 (let ((vec (aref state 3)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 (if (integerp vec)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (aset state 3 (setq vec (make-vector 55 nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 (aset vec 0 j)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 (while (> (setq i (% (+ i 21) 55)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 (aset vec i (setq j (prog1 k (setq k (- j k))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 (while (< (setq i (1+ i)) 200) (random* 2 state))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 (j (aset state 2 (% (1+ (aref state 2)) 55)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 (if (integerp lim)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 (if (<= lim 512) (% n lim)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 (let ((mask 1023))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 (* (/ n '8388608e0) lim)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 (defun make-random-state (&optional state)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 If STATE is t, return a new state object seeded from the time of day."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 (cond ((null state) (make-random-state *random-state*))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 ((vectorp state) (cl-copy-tree state t))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 (t (make-random-state (cl-random-time)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 (defun random-state-p (object)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 "Return t if OBJECT is a random-state object."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 (and (vectorp object) (= (length object) 4)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477 (eq (aref object 0) 'cl-random-state-tag)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480 ;; Implementation limits.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482 (defun cl-finite-do (func a b)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483 (condition-case err
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
484 (let ((res (funcall func a b))) ; check for IEEE infinity
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 (and (numberp res) (/= res (/ res 2)) res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 (arith-error nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488 (defvar most-positive-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
489 (defvar most-negative-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
490 (defvar least-positive-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
491 (defvar least-negative-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 (defvar least-positive-normalized-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493 (defvar least-negative-normalized-float)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
494 (defvar float-epsilon)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 (defvar float-negative-epsilon)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 (defun cl-float-limits ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 (or most-positive-float (not (numberp '2e1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 (let ((x '2e0) y z)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 ;; Find maximum exponent (first two loops are optimizations)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 (while (cl-finite-do '* x x) (setq x (* x x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 (while (cl-finite-do '+ x x) (setq x (+ x x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 (setq z x y (/ x 2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 ;; Now fill in 1's in the mantissa.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 (setq x (+ x y) y (/ y 2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508 (setq most-positive-float x
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509 most-negative-float (- x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 ;; Divide down until mantissa starts rounding.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 (setq x (/ x z) y (/ 16 z) x (* x y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513 (arith-error nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
514 (setq x (/ x 2) y (/ y 2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
515 (setq least-positive-normalized-float y
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
516 least-negative-normalized-float (- y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 ;; Divide down until value underflows to zero.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518 (setq x (/ 1 z) y x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519 (while (condition-case err (> (/ x 2) 0) (arith-error nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 (setq x (/ x 2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 (setq least-positive-float x
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522 least-negative-float (- x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 (setq x '1e0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525 (setq float-epsilon (* x 2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526 (setq x '1e0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528 (setq float-negative-epsilon (* x 2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 ;;; Sequence functions.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 (defun subseq (seq start &optional end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 "Return the subsequence of SEQ from START to END.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
536 If END is omitted, it defaults to the length of the sequence.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
537 If START or END is negative, it counts from the end."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538 (if (stringp seq) (substring seq start end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 (let (len)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 (cond ((listp seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 (if (> start 0) (setq seq (nthcdr start seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (if end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (let ((res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (while (>= (setq end (1- end)) start)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (cl-push (cl-pop seq) res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (nreverse res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 (copy-sequence seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 (t
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 (or end (setq end (or len (length seq))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (let ((res (make-vector (max (- end start) 0) nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (i 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (while (< start end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 (aset res i (aref seq start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556 (setq i (1+ i) start (1+ start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 res))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 (defun concatenate (type &rest seqs)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 (cond ((eq type 'vector) (apply 'vconcat seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 ((eq type 'string) (apply 'concat seqs))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 ((eq type 'list) (apply 'append (append seqs '(nil))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 (t (error "Not a sequence type name: %s" type))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 ;;; List functions.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 (defun revappend (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 "Equivalent to (append (reverse X) Y)."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 (nconc (reverse x) y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 (defun nreconc (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574 "Equivalent to (nconc (nreverse X) Y)."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575 (nconc (nreverse x) y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 (defun list-length (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 "Return the length of a list. Return nil if list is circular."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 (let ((n 0) (fast x) (slow x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 (if fast (if (cdr fast) nil (1+ n)) n)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 (defun tailp (sublist list)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 "Return true if SUBLIST is a tail of LIST."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 (while (and (consp list) (not (eq sublist list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587 (setq list (cdr list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 (if (numberp sublist) (equal sublist list) (eq sublist list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 (defun cl-copy-tree (tree &optional vecp)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 "Make a copy of TREE.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592 If TREE is a cons cell, this recursively copies both its car and its cdr.
13970
cdd61cb79be4 (cl-copy-tree): Doc fix.
Karl Heuer <kwzh@gnu.org>
parents: 13337
diff changeset
593 Contrast to copy-sequence, which copies only along the cdrs. With second
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
594 argument VECP, this copies vectors as well as conses."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595 (if (consp tree)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 (let ((p (setq tree (copy-list tree))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 (while (consp p)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 (if (or (consp (car p)) (and vecp (vectorp (car p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 (setcar p (cl-copy-tree (car p) vecp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 (cl-pop p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
602 (if (and vecp (vectorp tree))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603 (let ((i (length (setq tree (copy-sequence tree)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
604 (while (>= (setq i (1- i)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
605 (aset tree i (cl-copy-tree (aref tree i) vecp))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
606 tree)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608 (defalias 'copy-tree 'cl-copy-tree))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
609
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
610
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
611 ;;; Property lists.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
612
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
613 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
614 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
615 (or (get sym tag)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
616 (and def
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
617 (let ((plist (symbol-plist sym)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
618 (while (and plist (not (eq (car plist) tag)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
619 (setq plist (cdr (cdr plist))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
620 (if plist (car (cdr plist)) def)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
621
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
622 (defun getf (plist tag &optional def)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
623 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
624 PROPLIST is a list of the sort returned by `symbol-plist'."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
625 (setplist '--cl-getf-symbol-- plist)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
626 (or (get '--cl-getf-symbol-- tag)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
627 (and def (get* '--cl-getf-symbol-- tag def))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
628
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
629 (defun cl-set-getf (plist tag val)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
630 (let ((p plist))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
631 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
632 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
633
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
634 (defun cl-do-remf (plist tag)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
635 (let ((p (cdr plist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
636 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
637 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
638
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
639 (defun cl-remprop (sym tag)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
640 "Remove from SYMBOL's plist the property PROP and its value."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
641 (let ((plist (symbol-plist sym)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
642 (if (and plist (eq tag (car plist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
643 (progn (setplist sym (cdr (cdr plist))) t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
644 (cl-do-remf plist tag))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
645 (or (and (fboundp 'remprop) (subrp (symbol-function 'remprop)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
646 (defalias 'remprop 'cl-remprop))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
647
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
648
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
649
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
650 ;;; Hash tables.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
651
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
652 (defun make-hash-table (&rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
653 "Make an empty Common Lisp-style hash-table.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
654 If :test is `eq', this can use Lucid Emacs built-in hash-tables.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
655 In non-Lucid Emacs, or with non-`eq' test, this internally uses a-lists.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
656 Keywords supported: :test :size
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
657 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
658 (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
659 (cl-size (or (car (cdr (memq ':size cl-keys))) 20)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
660 (if (and (eq cl-test 'eq) (fboundp 'make-hashtable))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
661 (funcall 'make-hashtable cl-size)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
662 (list 'cl-hash-table-tag cl-test
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
663 (if (> cl-size 1) (make-vector cl-size 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
664 (let ((sym (make-symbol "--hashsym--"))) (set sym nil) sym))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
665 0))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
666
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
667 (defvar cl-lucid-hash-tag
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
668 (if (and (fboundp 'make-hashtable) (vectorp (make-hashtable 1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
669 (aref (make-hashtable 1) 0) (make-symbol "--cl-hash-tag--")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
670
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
671 (defun hash-table-p (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
672 "Return t if OBJECT is a hash table."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
673 (or (eq (car-safe x) 'cl-hash-table-tag)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
674 (and (vectorp x) (= (length x) 4) (eq (aref x 0) cl-lucid-hash-tag))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
675 (and (fboundp 'hashtablep) (funcall 'hashtablep x))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
676
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
677 (defun cl-not-hash-table (x &optional y &rest z)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
678 (signal 'wrong-type-argument (list 'hash-table-p (or y x))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
679
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
680 (defun cl-hash-lookup (key table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
681 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
682 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
683 (if (symbolp array) (setq str nil sym (symbol-value array))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
684 (while (or (consp str) (and (vectorp str) (> (length str) 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
685 (setq str (elt str 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
686 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
687 ((symbolp str) (setq str (symbol-name str)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
688 ((and (numberp str) (> str -8000000) (< str 8000000))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
689 (or (integerp str) (setq str (truncate str)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
690 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
691 "11" "12" "13" "14" "15"] (logand str 15))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
692 (t (setq str "*")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
693 (setq sym (symbol-value (intern-soft str array))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
694 (list (and sym (cond ((or (eq test 'eq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
695 (and (eq test 'eql) (not (numberp key))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
696 (assq key sym))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
697 ((memq test '(eql equal)) (assoc key sym))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
698 (t (assoc* key sym ':test test))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
699 sym str)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
700
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
701 (defvar cl-builtin-gethash
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
702 (if (and (fboundp 'gethash) (subrp (symbol-function 'gethash)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
703 (symbol-function 'gethash) 'cl-not-hash-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
704 (defvar cl-builtin-remhash
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
705 (if (and (fboundp 'remhash) (subrp (symbol-function 'remhash)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
706 (symbol-function 'remhash) 'cl-not-hash-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
707 (defvar cl-builtin-clrhash
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
708 (if (and (fboundp 'clrhash) (subrp (symbol-function 'clrhash)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
709 (symbol-function 'clrhash) 'cl-not-hash-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
710 (defvar cl-builtin-maphash
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
711 (if (and (fboundp 'maphash) (subrp (symbol-function 'maphash)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
712 (symbol-function 'maphash) 'cl-not-hash-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
713
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
714 (defun cl-gethash (key table &optional def)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
715 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
716 (if (consp table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
717 (let ((found (cl-hash-lookup key table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
718 (if (car found) (cdr (car found)) def))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
719 (funcall cl-builtin-gethash key table def)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
720 (defalias 'gethash 'cl-gethash)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
721
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
722 (defun cl-puthash (key val table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
723 (if (consp table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
724 (let ((found (cl-hash-lookup key table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
725 (if (car found) (setcdr (car found) val)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
726 (if (nth 2 found)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
727 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
728 (if (> (nth 3 table) (* (length (nth 2 table)) 3))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
729 (let ((new-table (make-vector (nth 3 table) 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
730 (mapatoms (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
731 (lambda (sym)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
732 (set (intern (symbol-name sym) new-table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
733 (symbol-value sym))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
734 (nth 2 table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
735 (setcar (cdr (cdr table)) new-table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
736 (set (intern (nth 2 found) (nth 2 table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
737 (cons (cons key val) (nth 1 found))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
738 (set (nth 2 table) (cons (cons key val) (nth 1 found))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
739 (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
740 (funcall 'puthash key val table)) val)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
741
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
742 (defun cl-remhash (key table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
743 "Remove KEY from HASH-TABLE."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
744 (if (consp table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
745 (let ((found (cl-hash-lookup key table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
746 (and (car found)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
747 (let ((del (delq (car found) (nth 1 found))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
748 (setcar (cdr (cdr (cdr table))) (1- (nth 3 table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
749 (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
750 (set (nth 2 table) del)) t)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
751 (prog1 (not (eq (funcall cl-builtin-gethash key table '--cl--) '--cl--))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
752 (funcall cl-builtin-remhash key table))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
753 (defalias 'remhash 'cl-remhash)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
754
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
755 (defun cl-clrhash (table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
756 "Clear HASH-TABLE."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
757 (if (consp table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
758 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
759 (or (hash-table-p table) (cl-not-hash-table table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
760 (if (symbolp (nth 2 table)) (set (nth 2 table) nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
761 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
762 (setcar (cdr (cdr (cdr table))) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
763 (funcall cl-builtin-clrhash table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
764 nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
765 (defalias 'clrhash 'cl-clrhash)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
766
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
767 (defun cl-maphash (cl-func cl-table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
768 "Call FUNCTION on keys and values from HASH-TABLE."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
769 (or (hash-table-p cl-table) (cl-not-hash-table cl-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
770 (if (consp cl-table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
771 (mapatoms (function (lambda (cl-x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
772 (setq cl-x (symbol-value cl-x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
773 (while cl-x
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
774 (funcall cl-func (car (car cl-x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
775 (cdr (car cl-x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
776 (setq cl-x (cdr cl-x)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
777 (if (symbolp (nth 2 cl-table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
778 (vector (nth 2 cl-table)) (nth 2 cl-table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
779 (funcall cl-builtin-maphash cl-func cl-table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
780 (defalias 'maphash 'cl-maphash)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
781
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
782 (defun hash-table-count (table)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
783 "Return the number of entries in HASH-TABLE."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
784 (or (hash-table-p table) (cl-not-hash-table table))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
785 (if (consp table) (nth 3 table) (funcall 'hashtable-fullness table)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
786
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
787
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
788 ;;; Some debugging aids.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
789
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
790 (defun cl-prettyprint (form)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
791 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
792 (let ((pt (point)) last)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
793 (insert "\n" (prin1-to-string form) "\n")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
794 (setq last (point))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
795 (goto-char (1+ pt))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
796 (while (search-forward "(quote " last t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
797 (delete-backward-char 7)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
798 (insert "'")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
799 (forward-sexp)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
800 (delete-char 1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
801 (goto-char (1+ pt))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
802 (cl-do-prettyprint)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
803
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
804 (defun cl-do-prettyprint ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
805 (skip-chars-forward " ")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
806 (if (looking-at "(")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
807 (let ((skip (or (looking-at "((") (looking-at "(prog")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
808 (looking-at "(unwind-protect ")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
809 (looking-at "(function (")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
810 (looking-at "(cl-block-wrapper ")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
811 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
812 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
813 (set (looking-at "(p?set[qf] ")))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
814 (if (or skip let
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
815 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
816 (forward-sexp)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
817 (and (>= (current-column) 78) (progn (backward-sexp) t))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
818 (let ((nl t))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
819 (forward-char 1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
820 (cl-do-prettyprint)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
821 (or skip (looking-at ")") (cl-do-prettyprint))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
822 (or (not two) (looking-at ")") (cl-do-prettyprint))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
823 (while (not (looking-at ")"))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
824 (if set (setq nl (not nl)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
825 (if nl (insert "\n"))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
826 (lisp-indent-line)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
827 (cl-do-prettyprint))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828 (forward-char 1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
829 (forward-sexp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
830
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
831 (defvar cl-macroexpand-cmacs nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
832 (defvar cl-closure-vars nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
833
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
834 (defun cl-macroexpand-all (form &optional env)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
835 "Expand all macro calls through a Lisp FORM.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
836 This also does some trivial optimizations to make the form prettier."
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837 (while (or (not (eq form (setq form (macroexpand form env))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
838 (and cl-macroexpand-cmacs
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
839 (not (eq form (setq form (compiler-macroexpand form)))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
840 (cond ((not (consp form)) form)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
841 ((memq (car form) '(let let*))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
842 (if (null (nth 1 form))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
843 (cl-macroexpand-all (cons 'progn (cddr form)) env)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
844 (let ((letf nil) (res nil) (lets (cadr form)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
845 (while lets
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
846 (cl-push (if (consp (car lets))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
847 (let ((exp (cl-macroexpand-all (caar lets) env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
848 (or (symbolp exp) (setq letf t))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
849 (cons exp (cl-macroexpand-body (cdar lets) env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
850 (let ((exp (cl-macroexpand-all (car lets) env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
851 (if (symbolp exp) exp
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
852 (setq letf t) (list exp nil)))) res)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
853 (setq lets (cdr lets)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
854 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
855 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
856 ((eq (car form) 'cond)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
857 (cons (car form)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 (cdr form))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
860 ((eq (car form) 'condition-case)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
861 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
862 (mapcar (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
863 (lambda (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
864 (cons (car x) (cl-macroexpand-body (cdr x) env))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
865 (cdddr form))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
866 ((memq (car form) '(quote function))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
867 (if (eq (car-safe (nth 1 form)) 'lambda)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
868 (let ((body (cl-macroexpand-body (cddadr form) env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
869 (if (and cl-closure-vars (eq (car form) 'function)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870 (cl-expr-contains-any body cl-closure-vars))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
871 (let* ((new (mapcar 'gensym cl-closure-vars))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
872 (sub (pairlis cl-closure-vars new)) (decls nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
873 (while (or (stringp (car body))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
874 (eq (car-safe (car body)) 'interactive))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
875 (cl-push (list 'quote (cl-pop body)) decls))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
876 (put (car (last cl-closure-vars)) 'used t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
877 (append
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
878 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
879 (sublis sub (nreverse decls))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880 (list
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
881 (list* 'list '(quote apply)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
882 (list 'list '(quote quote)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
883 (list 'function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
884 (list* 'lambda
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
885 (append new (cadadr form))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
886 (sublis sub body))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
887 (nconc (mapcar (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
888 (lambda (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
889 (list 'list '(quote quote) x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
890 cl-closure-vars)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
891 '((quote --cl-rest--)))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
892 (list (car form) (list* 'lambda (cadadr form) body))))
15029
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
893 (let ((found (assq (cadr form) env)))
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
894 (if (eq (cadr (caddr found)) 'cl-labels-args)
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
895 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
ba44a899c055 (isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents: 14794
diff changeset
896 form))))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
897 ((memq (car form) '(defun defmacro))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
898 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
899 ((and (eq (car form) 'progn) (not (cddr form)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
900 (cl-macroexpand-all (nth 1 form) env))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
901 ((eq (car form) 'setq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
902 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
903 (while (and p (symbolp (car p))) (setq p (cddr p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
904 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
905 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
906
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
907 (defun cl-macroexpand-body (body &optional env)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
908 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
909
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
910 (defun cl-prettyexpand (form &optional full)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
911 (message "Expanding...")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
912 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
913 (byte-compile-macro-environment nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
914 (setq form (cl-macroexpand-all form
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
915 (and (not full) '((block) (eval-when)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
916 (message "Formatting...")
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
917 (prog1 (cl-prettyprint form)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
918 (message ""))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
919
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
920
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
921
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
922 (run-hooks 'cl-extra-load-hook)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
923
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
924 ;;; cl-extra.el ends here