0
|
1 /* Random utility Lisp functions.
|
|
2 Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Mule 2.0, FSF 19.30. */
|
|
23
|
|
24 /* This file has been Mule-ized. */
|
|
25
|
|
26 /* Note: FSF 19.30 has bool vectors. We have bit vectors. */
|
|
27
|
|
28 /* Hacked on for Mule by Ben Wing, December 1994, January 1995. */
|
|
29
|
|
30 #include <config.h>
|
|
31
|
|
32 /* Note on some machines this defines `vector' as a typedef,
|
|
33 so make sure we don't use that name in this file. */
|
|
34 #undef vector
|
|
35 #define vector *****
|
|
36
|
|
37 #include "lisp.h"
|
|
38
|
|
39 #include "buffer.h"
|
|
40 #include "bytecode.h"
|
|
41 #include "commands.h"
|
|
42 #include "device.h"
|
|
43 #include "events.h"
|
|
44 #include "extents.h"
|
|
45 #include "frame.h"
|
|
46 #include "systime.h"
|
|
47
|
|
48 Lisp_Object Qstring_lessp;
|
|
49 Lisp_Object Qidentity;
|
|
50
|
|
51 static Lisp_Object mark_bit_vector (Lisp_Object, void (*) (Lisp_Object));
|
|
52 static void print_bit_vector (Lisp_Object, Lisp_Object, int);
|
|
53 static int bit_vector_equal (Lisp_Object o1, Lisp_Object o2, int depth);
|
|
54 static unsigned long bit_vector_hash (Lisp_Object obj, int depth);
|
|
55 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("bit-vector", bit_vector,
|
|
56 mark_bit_vector, print_bit_vector, 0,
|
|
57 bit_vector_equal, bit_vector_hash,
|
|
58 struct Lisp_Bit_Vector);
|
|
59
|
|
60 static Lisp_Object
|
|
61 mark_bit_vector (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
62 {
|
|
63 return (Qnil);
|
|
64 }
|
|
65
|
|
66 static void
|
|
67 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
68 {
|
|
69 int i;
|
|
70 struct Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
|
|
71 int len = bit_vector_length (v);
|
|
72 int last = len;
|
|
73
|
|
74 if (INTP (Vprint_length))
|
|
75 last = min (len, XINT (Vprint_length));
|
|
76 write_c_string ("#*", printcharfun);
|
|
77 for (i = 0; i < last; i++)
|
|
78 {
|
|
79 if (bit_vector_bit (v, i))
|
|
80 write_c_string ("1", printcharfun);
|
|
81 else
|
|
82 write_c_string ("0", printcharfun);
|
|
83 }
|
|
84
|
|
85 if (last != len)
|
|
86 write_c_string ("...", printcharfun);
|
|
87 }
|
|
88
|
|
89 static int
|
|
90 bit_vector_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
91 {
|
|
92 struct Lisp_Bit_Vector *v1 = XBIT_VECTOR (o1);
|
|
93 struct Lisp_Bit_Vector *v2 = XBIT_VECTOR (o2);
|
|
94
|
|
95 if (bit_vector_length (v1) != bit_vector_length (v2))
|
|
96 return 0;
|
|
97
|
|
98 return !memcmp (v1->bits, v2->bits,
|
|
99 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
|
|
100 sizeof (long));
|
|
101 }
|
|
102
|
|
103 static unsigned long
|
|
104 bit_vector_hash (Lisp_Object obj, int depth)
|
|
105 {
|
|
106 struct Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
|
|
107 return HASH2 (bit_vector_length (v),
|
|
108 memory_hash (v->bits,
|
|
109 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
|
|
110 sizeof (long)));
|
|
111 }
|
|
112
|
20
|
113 DEFUN ("identity", Fidentity, 1, 1, 0, /*
|
0
|
114 Return the argument unchanged.
|
20
|
115 */
|
|
116 (arg))
|
0
|
117 {
|
|
118 return arg;
|
|
119 }
|
|
120
|
|
121 extern long get_random (void);
|
|
122 extern void seed_random (long arg);
|
|
123
|
20
|
124 DEFUN ("random", Frandom, 0, 1, 0, /*
|
0
|
125 Return a pseudo-random number.
|
|
126 All integers representable in Lisp are equally likely.\n\
|
|
127 On most systems, this is 28 bits' worth.\n\
|
|
128 With positive integer argument N, return random number in interval [0,N).\n\
|
|
129 With argument t, set the random number seed from the current time and pid.
|
20
|
130 */
|
|
131 (limit))
|
0
|
132 {
|
|
133 EMACS_INT val;
|
|
134 Lisp_Object lispy_val;
|
|
135 unsigned long denominator;
|
|
136
|
|
137 if (EQ (limit, Qt))
|
|
138 seed_random (getpid () + time (NULL));
|
|
139 if (NATNUMP (limit) && !ZEROP (limit))
|
|
140 {
|
|
141 /* Try to take our random number from the higher bits of VAL,
|
|
142 not the lower, since (says Gentzel) the low bits of `random'
|
|
143 are less random than the higher ones. We do this by using the
|
|
144 quotient rather than the remainder. At the high end of the RNG
|
|
145 it's possible to get a quotient larger than limit; discarding
|
|
146 these values eliminates the bias that would otherwise appear
|
|
147 when using a large limit. */
|
|
148 denominator = ((unsigned long)1 << VALBITS) / XINT (limit);
|
|
149 do
|
|
150 val = get_random () / denominator;
|
|
151 while (val >= XINT (limit));
|
|
152 }
|
|
153 else
|
|
154 val = get_random ();
|
|
155 XSETINT (lispy_val, val);
|
|
156 return lispy_val;
|
|
157 }
|
|
158
|
|
159 /* Random data-structure functions */
|
|
160
|
|
161 #ifdef LOSING_BYTECODE
|
|
162
|
|
163 /* #### Delete this shit */
|
|
164
|
|
165 /* Charcount is a misnomer here as we might be dealing with the
|
|
166 length of a vector or list, but emphasizes that we're not dealing
|
|
167 with Bytecounts in strings */
|
|
168 static Charcount
|
|
169 length_with_bytecode_hack (Lisp_Object seq)
|
|
170 {
|
|
171 if (!COMPILED_FUNCTIONP (seq))
|
|
172 return (XINT (Flength (seq)));
|
|
173 else
|
|
174 {
|
|
175 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (seq);
|
|
176 int intp = b->flags.interactivep;
|
|
177 int domainp = b->flags.domainp;
|
|
178
|
|
179 if (intp)
|
|
180 return (COMPILED_INTERACTIVE + 1);
|
|
181 else if (domainp)
|
|
182 return (COMPILED_DOMAIN + 1);
|
|
183 else
|
|
184 return (COMPILED_DOC_STRING + 1);
|
|
185 }
|
|
186 }
|
|
187
|
|
188 #endif /* LOSING_BYTECODE */
|
|
189
|
|
190 void
|
|
191 check_losing_bytecode (CONST char *function, Lisp_Object seq)
|
|
192 {
|
|
193 if (COMPILED_FUNCTIONP (seq))
|
|
194 error_with_frob
|
|
195 (seq,
|
|
196 "As of 19.14, `%s' no longer works with compiled-function objects",
|
|
197 function);
|
|
198 }
|
|
199
|
20
|
200 DEFUN ("length", Flength, 1, 1, 0, /*
|
0
|
201 Return the length of vector, bit vector, list or string SEQUENCE.
|
20
|
202 */
|
|
203 (obj))
|
0
|
204 {
|
|
205 Lisp_Object tail;
|
|
206 int i;
|
|
207
|
|
208 retry:
|
|
209 if (STRINGP (obj))
|
|
210 return (make_int (string_char_length (XSTRING (obj))));
|
|
211 else if (VECTORP (obj))
|
|
212 return (make_int (vector_length (XVECTOR (obj))));
|
|
213 else if (BIT_VECTORP (obj))
|
|
214 return (make_int (bit_vector_length (XBIT_VECTOR (obj))));
|
|
215 else if (CONSP (obj))
|
|
216 {
|
|
217 for (i = 0, tail = obj; !NILP (tail); i++)
|
|
218 {
|
|
219 QUIT;
|
|
220 tail = Fcdr (tail);
|
|
221 }
|
|
222
|
|
223 return (make_int (i));
|
|
224 }
|
|
225 else if (NILP (obj))
|
|
226 {
|
|
227 return (Qzero);
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 check_losing_bytecode ("length", obj);
|
|
232 obj = wrong_type_argument (Qsequencep, obj);
|
|
233 goto retry;
|
|
234 }
|
|
235 }
|
|
236
|
|
237 /* This does not check for quits. That is safe
|
|
238 since it must terminate. */
|
|
239
|
20
|
240 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
|
0
|
241 Return the length of a list, but avoid error or infinite loop.
|
|
242 This function never gets an error. If LIST is not really a list,
|
|
243 it returns 0. If LIST is circular, it returns a finite value
|
|
244 which is at least the number of distinct elements.
|
20
|
245 */
|
|
246 (list))
|
0
|
247 {
|
|
248 Lisp_Object tail, halftail, length;
|
|
249 int len = 0;
|
|
250
|
|
251 /* halftail is used to detect circular lists. */
|
|
252 halftail = list;
|
|
253 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
254 {
|
|
255 if (EQ (tail, halftail) && len != 0)
|
|
256 break;
|
|
257 len++;
|
|
258 if ((len & 1) == 0)
|
|
259 halftail = XCDR (halftail);
|
|
260 }
|
|
261
|
|
262 XSETINT (length, len);
|
|
263 return length;
|
|
264 }
|
|
265
|
|
266 /*** string functions. ***/
|
|
267
|
20
|
268 DEFUN ("string-equal", Fstring_equal, 2, 2, 0, /*
|
0
|
269 T if two strings have identical contents.
|
|
270 Case is significant. Text properties are ignored.
|
|
271 (Under XEmacs, `equal' also ignores text properties and extents in
|
|
272 strings, but this is not the case under FSF Emacs.)
|
|
273 Symbols are also allowed; their print names are used instead.
|
20
|
274 */
|
|
275 (s1, s2))
|
0
|
276 {
|
|
277 int len;
|
|
278
|
|
279 if (SYMBOLP (s1))
|
|
280 XSETSTRING (s1, XSYMBOL (s1)->name);
|
|
281 if (SYMBOLP (s2))
|
|
282 XSETSTRING (s2, XSYMBOL (s2)->name);
|
|
283 CHECK_STRING (s1);
|
|
284 CHECK_STRING (s2);
|
|
285
|
14
|
286 len = XSTRING_LENGTH (s1);
|
|
287 if (len != XSTRING_LENGTH (s2) ||
|
|
288 memcmp (XSTRING_DATA (s1), XSTRING_DATA (s2), len))
|
0
|
289 return Qnil;
|
|
290 return Qt;
|
|
291 }
|
|
292
|
|
293
|
20
|
294 DEFUN ("string-lessp", Fstring_lessp, 2, 2, 0, /*
|
0
|
295 T if first arg string is less than second in lexicographic order.
|
|
296 If I18N2 support was compiled in, ordering is determined by the locale.
|
|
297 Case is significant for the default C locale.
|
|
298 Symbols are also allowed; their print names are used instead.
|
20
|
299 */
|
|
300 (s1, s2))
|
0
|
301 {
|
|
302 struct Lisp_String *p1, *p2;
|
|
303 Charcount end, len2;
|
|
304
|
|
305 if (SYMBOLP (s1))
|
|
306 XSETSTRING (s1, XSYMBOL (s1)->name);
|
|
307 if (SYMBOLP (s2))
|
|
308 XSETSTRING (s2, XSYMBOL (s2)->name);
|
|
309 CHECK_STRING (s1);
|
|
310 CHECK_STRING (s2);
|
|
311
|
|
312 p1 = XSTRING (s1);
|
|
313 p2 = XSTRING (s2);
|
|
314 end = string_char_length (XSTRING (s1));
|
|
315 len2 = string_char_length (XSTRING (s2));
|
|
316 if (end > len2)
|
|
317 end = len2;
|
|
318
|
|
319 {
|
|
320 int i;
|
|
321
|
|
322 #ifdef I18N2
|
|
323 Bytecount bcend = charcount_to_bytecount (string_data (p1), end);
|
|
324 /* Compare strings using collation order of locale. */
|
|
325 /* Need to be tricky to handle embedded nulls. */
|
|
326
|
|
327 for (i = 0; i < bcend; i += strlen((char *) string_data (p1) + i) + 1)
|
|
328 {
|
|
329 int val = strcoll ((char *) string_data (p1) + i,
|
|
330 (char *) string_data (p2) + i);
|
|
331 if (val < 0)
|
|
332 return Qt;
|
|
333 if (val > 0)
|
|
334 return Qnil;
|
|
335 }
|
|
336 #else /* not I18N2 */
|
|
337 for (i = 0; i < end; i++)
|
|
338 {
|
|
339 if (string_char (p1, i) != string_char (p2, i))
|
|
340 return string_char (p1, i) < string_char (p2, i) ? Qt : Qnil;
|
|
341 }
|
|
342 #endif /* not I18N2 */
|
|
343 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
|
|
344 won't work right in I18N2 case */
|
|
345 return ((end < len2) ? Qt : Qnil);
|
|
346 }
|
|
347 }
|
|
348
|
20
|
349 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
|
0
|
350 Return STRING's tick counter, incremented for each change to the string.
|
|
351 Each string has a tick counter which is incremented each time the contents
|
|
352 of the string are changed (e.g. with `aset'). It wraps around occasionally.
|
20
|
353 */
|
|
354 (string))
|
0
|
355 {
|
|
356 struct Lisp_String *s;
|
|
357
|
|
358 CHECK_STRING (string);
|
|
359 s = XSTRING (string);
|
|
360 if (CONSP (s->plist) && INTP (XCAR (s->plist)))
|
|
361 return XCAR (s->plist);
|
|
362 else
|
|
363 return Qzero;
|
|
364 }
|
|
365
|
|
366 void
|
|
367 bump_string_modiff (Lisp_Object str)
|
|
368 {
|
|
369 struct Lisp_String *s = XSTRING (str);
|
|
370 Lisp_Object *ptr = &s->plist;
|
|
371
|
|
372 #ifdef I18N3
|
|
373 /* #### remove the `string-translatable' property from the string,
|
|
374 if there is one. */
|
|
375 #endif
|
|
376 /* skip over extent info if it's there */
|
|
377 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
|
|
378 ptr = &XCDR (*ptr);
|
|
379 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
|
|
380 XSETINT (XCAR (*ptr), 1+XINT (XCAR (*ptr)));
|
|
381 else
|
|
382 *ptr = Fcons (make_int (1), *ptr);
|
|
383 }
|
|
384
|
|
385
|
|
386 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
|
|
387 static Lisp_Object concat (int nargs, Lisp_Object *args,
|
|
388 enum concat_target_type target_type,
|
|
389 int last_special);
|
|
390
|
|
391 Lisp_Object
|
|
392 concat2 (Lisp_Object s1, Lisp_Object s2)
|
|
393 {
|
|
394 Lisp_Object args[2];
|
|
395 args[0] = s1;
|
|
396 args[1] = s2;
|
|
397 return concat (2, args, c_string, 0);
|
|
398 }
|
|
399
|
|
400 Lisp_Object
|
|
401 concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
|
|
402 {
|
|
403 Lisp_Object args[3];
|
|
404 args[0] = s1;
|
|
405 args[1] = s2;
|
|
406 args[2] = s3;
|
|
407 return concat (3, args, c_string, 0);
|
|
408 }
|
|
409
|
|
410 Lisp_Object
|
|
411 vconcat2 (Lisp_Object s1, Lisp_Object s2)
|
|
412 {
|
|
413 Lisp_Object args[2];
|
|
414 args[0] = s1;
|
|
415 args[1] = s2;
|
|
416 return concat (2, args, c_vector, 0);
|
|
417 }
|
|
418
|
|
419 Lisp_Object
|
|
420 vconcat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
|
|
421 {
|
|
422 Lisp_Object args[3];
|
|
423 args[0] = s1;
|
|
424 args[1] = s2;
|
|
425 args[2] = s3;
|
|
426 return concat (3, args, c_vector, 0);
|
|
427 }
|
|
428
|
20
|
429 DEFUN ("append", Fappend, 0, MANY, 0, /*
|
0
|
430 Concatenate all the arguments and make the result a list.
|
|
431 The result is a list whose elements are the elements of all the arguments.
|
|
432 Each argument may be a list, vector, bit vector, or string.
|
|
433 The last argument is not copied, just used as the tail of the new list.
|
20
|
434 */
|
|
435 (int nargs, Lisp_Object *args))
|
0
|
436 {
|
|
437 return concat (nargs, args, c_cons, 1);
|
|
438 }
|
|
439
|
20
|
440 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
|
0
|
441 Concatenate all the arguments and make the result a string.
|
|
442 The result is a string whose elements are the elements of all the arguments.
|
|
443 Each argument may be a string or a list or vector of characters (integers).
|
|
444
|
|
445 Do not use individual integers as arguments!
|
|
446 The behavior of `concat' in that case will be changed later!
|
|
447 If your program passes an integer as an argument to `concat',
|
|
448 you should change it right away not to do so.
|
20
|
449 */
|
|
450 (int nargs, Lisp_Object *args))
|
0
|
451 {
|
|
452 return concat (nargs, args, c_string, 0);
|
|
453 }
|
|
454
|
20
|
455 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
|
0
|
456 Concatenate all the arguments and make the result a vector.
|
|
457 The result is a vector whose elements are the elements of all the arguments.
|
|
458 Each argument may be a list, vector, bit vector, or string.
|
20
|
459 */
|
|
460 (int nargs, Lisp_Object *args))
|
0
|
461 {
|
|
462 return concat (nargs, args, c_vector, 0);
|
|
463 }
|
|
464
|
20
|
465 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
|
0
|
466 Concatenate all the arguments and make the result a bit vector.
|
|
467 The result is a bit vector whose elements are the elements of all the
|
|
468 arguments. Each argument may be a list, vector, bit vector, or string.
|
20
|
469 */
|
|
470 (int nargs, Lisp_Object *args))
|
0
|
471 {
|
|
472 return concat (nargs, args, c_bit_vector, 0);
|
|
473 }
|
|
474
|
20
|
475 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
|
0
|
476 Return a copy of a list, vector, bit vector or string.
|
|
477 The elements of a list or vector are not copied; they are shared
|
|
478 with the original.
|
20
|
479 */
|
|
480 (arg))
|
0
|
481 {
|
|
482 again:
|
|
483 if (NILP (arg)) return arg;
|
|
484 /* We handle conses separately because concat() is big and hairy and
|
|
485 doesn't handle (copy-sequence '(a b . c)) and it's easier to redo this
|
|
486 than to fix concat() without worrying about breaking other things.
|
|
487 */
|
|
488 if (CONSP (arg))
|
|
489 {
|
|
490 Lisp_Object rest = arg;
|
|
491 Lisp_Object head, tail;
|
|
492 tail = Qnil;
|
|
493 while (CONSP (rest))
|
|
494 {
|
|
495 Lisp_Object new = Fcons (XCAR (rest), XCDR (rest));
|
|
496 if (NILP (tail))
|
|
497 head = tail = new;
|
|
498 else
|
|
499 XCDR (tail) = new, tail = new;
|
|
500 rest = XCDR (rest);
|
|
501 QUIT;
|
|
502 }
|
|
503 if (!NILP (tail))
|
|
504 XCDR (tail) = rest;
|
|
505 return head;
|
|
506 }
|
|
507 else if (STRINGP (arg))
|
|
508 return concat (1, &arg, c_string, 0);
|
|
509 else if (VECTORP (arg))
|
|
510 return concat (1, &arg, c_vector, 0);
|
|
511 else if (BIT_VECTORP (arg))
|
|
512 return concat (1, &arg, c_bit_vector, 0);
|
|
513 else
|
|
514 {
|
|
515 check_losing_bytecode ("copy-sequence", arg);
|
|
516 arg = wrong_type_argument (Qsequencep, arg);
|
|
517 goto again;
|
|
518 }
|
|
519 }
|
|
520
|
|
521 struct merge_string_extents_struct
|
|
522 {
|
|
523 Lisp_Object string;
|
|
524 Bytecount entry_offset;
|
|
525 Bytecount entry_length;
|
|
526 };
|
|
527
|
|
528 static Lisp_Object
|
|
529 concat (int nargs, Lisp_Object *args,
|
|
530 enum concat_target_type target_type,
|
|
531 int last_special)
|
|
532 {
|
|
533 Lisp_Object val;
|
|
534 Lisp_Object tail = Qnil;
|
|
535 int toindex;
|
|
536 int argnum;
|
|
537 Lisp_Object last_tail;
|
|
538 Lisp_Object prev;
|
|
539 struct merge_string_extents_struct *args_mse = 0;
|
|
540 Bufbyte *string_result = 0;
|
|
541 Bufbyte *string_result_ptr = 0;
|
|
542 struct gcpro gcpro1;
|
|
543
|
|
544 /* The modus operandi in Emacs is "caller gc-protects args".
|
|
545 However, concat is called many times in Emacs on freshly
|
|
546 created stuff. So we help those callers out by protecting
|
|
547 the args ourselves to save them a lot of temporary-variable
|
|
548 grief. */
|
|
549
|
|
550 GCPRO1 (args[0]);
|
|
551 gcpro1.nvars = nargs;
|
|
552
|
|
553 #ifdef I18N3
|
|
554 /* #### if the result is a string and any of the strings have a string
|
|
555 for the `string-translatable' property, then concat should also
|
|
556 concat the args but use the `string-translatable' strings, and store
|
|
557 the result in the returned string's `string-translatable' property. */
|
|
558 #endif
|
|
559 if (target_type == c_string)
|
|
560 {
|
|
561 args_mse = ((struct merge_string_extents_struct *)
|
|
562 alloca (nargs *
|
|
563 sizeof (struct merge_string_extents_struct)));
|
|
564 }
|
|
565
|
|
566 /* In append, the last arg isn't treated like the others */
|
|
567 if (last_special && nargs > 0)
|
|
568 {
|
|
569 nargs--;
|
|
570 last_tail = args[nargs];
|
|
571 }
|
|
572 else
|
|
573 last_tail = Qnil;
|
|
574
|
|
575 /* Check and coerce the arguments. */
|
|
576 for (argnum = 0; argnum < nargs; argnum++)
|
|
577 {
|
|
578 Lisp_Object seq = args[argnum];
|
|
579 if (CONSP (seq) || NILP (seq))
|
|
580 ;
|
|
581 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
|
|
582 ;
|
|
583 #ifdef LOSING_BYTECODE
|
|
584 else if (COMPILED_FUNCTIONP (seq))
|
|
585 /* Urk! We allow this, for "compatibility"... */
|
|
586 ;
|
|
587 #endif
|
|
588 else if (INTP (seq))
|
|
589 /* This is too revolting to think about but maintains
|
|
590 compatibility with FSF (and lots and lots of old code). */
|
|
591 args[argnum] = Fnumber_to_string (seq);
|
|
592 else
|
|
593 {
|
|
594 check_losing_bytecode ("concat", seq);
|
|
595 args[argnum] = wrong_type_argument (Qsequencep, seq);
|
|
596 }
|
|
597
|
|
598 if (args_mse)
|
|
599 {
|
|
600 if (STRINGP (seq))
|
|
601 args_mse[argnum].string = seq;
|
|
602 else
|
|
603 args_mse[argnum].string = Qnil;
|
|
604 }
|
|
605 }
|
|
606
|
|
607 {
|
|
608 /* Charcount is a misnomer here as we might be dealing with the
|
|
609 length of a vector or list, but emphasizes that we're not dealing
|
|
610 with Bytecounts in strings */
|
|
611 Charcount total_length;
|
|
612
|
|
613 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
|
|
614 {
|
|
615 #ifdef LOSING_BYTECODE
|
|
616 Charcount thislen = length_with_bytecode_hack (args[argnum]);
|
|
617 #else
|
|
618 Charcount thislen = XINT (Flength (args[argnum]));
|
|
619 #endif
|
|
620 total_length += thislen;
|
|
621 }
|
|
622
|
|
623 switch (target_type)
|
|
624 {
|
|
625 case c_cons:
|
|
626 if (total_length == 0)
|
|
627 /* In append, if all but last arg are nil, return last arg */
|
|
628 RETURN_UNGCPRO (last_tail);
|
|
629 val = Fmake_list (make_int (total_length), Qnil);
|
|
630 break;
|
|
631 case c_vector:
|
|
632 val = make_vector (total_length, Qnil);
|
|
633 break;
|
|
634 case c_bit_vector:
|
|
635 val = make_bit_vector (total_length, Qzero);
|
|
636 break;
|
|
637 case c_string:
|
|
638 /* We don't make the string yet because we don't know the
|
|
639 actual number of bytes. This loop was formerly written
|
|
640 to call Fmake_string() here and then call set_string_char()
|
|
641 for each char. This seems logical enough but is waaaaaaaay
|
|
642 slow -- set_string_char() has to scan the whole string up
|
|
643 to the place where the substitution is called for in order
|
|
644 to find the place to change, and may have to do some
|
|
645 realloc()ing in order to make the char fit properly.
|
|
646 O(N^2) yuckage. */
|
|
647 val = Qnil;
|
|
648 string_result = (Bufbyte *) alloca (total_length * MAX_EMCHAR_LEN);
|
|
649 string_result_ptr = string_result;
|
|
650 break;
|
|
651 default:
|
|
652 abort ();
|
|
653 }
|
|
654 }
|
|
655
|
|
656
|
|
657 if (CONSP (val))
|
|
658 tail = val, toindex = -1; /* -1 in toindex is flag we are
|
|
659 making a list */
|
|
660 else
|
|
661 toindex = 0;
|
|
662
|
|
663 prev = Qnil;
|
|
664
|
|
665 for (argnum = 0; argnum < nargs; argnum++)
|
|
666 {
|
|
667 Charcount thisleni = 0;
|
|
668 Charcount thisindex = 0;
|
|
669 Lisp_Object seq = args[argnum];
|
|
670 Bufbyte *string_source_ptr = 0;
|
|
671 Bufbyte *string_prev_result_ptr = string_result_ptr;
|
|
672
|
|
673 if (!CONSP (seq))
|
|
674 {
|
|
675 #ifdef LOSING_BYTECODE
|
|
676 thisleni = length_with_bytecode_hack (seq);
|
|
677 #else
|
|
678 thisleni = XINT (Flength (seq));
|
|
679 #endif
|
|
680 }
|
|
681 if (STRINGP (seq))
|
14
|
682 string_source_ptr = XSTRING_DATA (seq);
|
0
|
683
|
|
684 while (1)
|
|
685 {
|
|
686 Lisp_Object elt;
|
|
687
|
|
688 /* We've come to the end of this arg, so exit. */
|
|
689 if (NILP (seq))
|
|
690 break;
|
|
691
|
|
692 /* Fetch next element of `seq' arg into `elt' */
|
|
693 if (CONSP (seq))
|
|
694 {
|
|
695 elt = Fcar (seq);
|
|
696 seq = Fcdr (seq);
|
|
697 }
|
|
698 else
|
|
699 {
|
|
700 if (thisindex >= thisleni)
|
|
701 break;
|
|
702
|
|
703 if (STRINGP (seq))
|
|
704 {
|
|
705 elt = make_char (charptr_emchar (string_source_ptr));
|
|
706 INC_CHARPTR (string_source_ptr);
|
|
707 }
|
|
708 else if (VECTORP (seq))
|
|
709 elt = vector_data (XVECTOR (seq))[thisindex];
|
|
710 else if (BIT_VECTORP (seq))
|
|
711 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
|
|
712 thisindex));
|
|
713 else
|
|
714 elt = Felt (seq, make_int (thisindex));
|
|
715 thisindex++;
|
|
716 }
|
|
717
|
|
718 /* Store into result */
|
|
719 if (toindex < 0)
|
|
720 {
|
|
721 /* toindex negative means we are making a list */
|
|
722 XCAR (tail) = elt;
|
|
723 prev = tail;
|
|
724 tail = XCDR (tail);
|
|
725 }
|
|
726 else if (VECTORP (val))
|
|
727 vector_data (XVECTOR (val))[toindex++] = elt;
|
|
728 else if (BIT_VECTORP (val))
|
|
729 {
|
|
730 CHECK_BIT (elt);
|
|
731 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
|
|
732 }
|
|
733 else
|
|
734 {
|
|
735 CHECK_CHAR_COERCE_INT (elt);
|
|
736 string_result_ptr += set_charptr_emchar (string_result_ptr,
|
|
737 XCHAR (elt));
|
|
738 }
|
|
739 }
|
|
740 if (args_mse)
|
|
741 {
|
|
742 args_mse[argnum].entry_offset =
|
|
743 string_prev_result_ptr - string_result;
|
|
744 args_mse[argnum].entry_length =
|
|
745 string_result_ptr - string_prev_result_ptr;
|
|
746 }
|
|
747 }
|
|
748
|
|
749 /* Now we finally make the string. */
|
|
750 if (target_type == c_string)
|
|
751 {
|
|
752 val = make_string (string_result, string_result_ptr - string_result);
|
|
753 for (argnum = 0; argnum < nargs; argnum++)
|
|
754 {
|
|
755 if (STRINGP (args_mse[argnum].string))
|
|
756 copy_string_extents (val, args_mse[argnum].string,
|
|
757 args_mse[argnum].entry_offset, 0,
|
|
758 args_mse[argnum].entry_length);
|
|
759 }
|
|
760 }
|
|
761
|
|
762 if (!NILP (prev))
|
|
763 XCDR (prev) = last_tail;
|
|
764
|
|
765 RETURN_UNGCPRO (val);
|
|
766 }
|
|
767
|
20
|
768 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
|
0
|
769 Return a copy of ALIST.
|
|
770 This is an alist which represents the same mapping from objects to objects,
|
|
771 but does not share the alist structure with ALIST.
|
|
772 The objects mapped (cars and cdrs of elements of the alist)
|
|
773 are shared, however.
|
|
774 Elements of ALIST that are not conses are also shared.
|
20
|
775 */
|
|
776 (alist))
|
0
|
777 {
|
|
778 Lisp_Object tem;
|
|
779
|
|
780 CHECK_LIST (alist);
|
|
781 if (NILP (alist))
|
|
782 return alist;
|
|
783 alist = concat (1, &alist, c_cons, 0);
|
|
784 for (tem = alist; CONSP (tem); tem = XCDR (tem))
|
|
785 {
|
|
786 Lisp_Object car;
|
|
787 car = XCAR (tem);
|
|
788
|
|
789 if (CONSP (car))
|
|
790 XCAR (tem) = Fcons (XCAR (car), XCDR (car));
|
|
791 }
|
|
792 return alist;
|
|
793 }
|
|
794
|
20
|
795 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
|
0
|
796 Return a copy of a list and substructures.
|
|
797 The argument is copied, and any lists contained within it are copied
|
|
798 recursively. Circularities and shared substructures are not preserved.
|
|
799 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
|
|
800 are not copied.
|
20
|
801 */
|
|
802 (arg, vecp))
|
0
|
803 {
|
|
804 if (CONSP (arg))
|
|
805 {
|
|
806 Lisp_Object rest;
|
|
807 rest = arg = Fcopy_sequence (arg);
|
|
808 while (CONSP (rest))
|
|
809 {
|
|
810 Lisp_Object elt = XCAR (rest);
|
|
811 QUIT;
|
|
812 if (CONSP (elt) || VECTORP (elt))
|
|
813 XCAR (rest) = Fcopy_tree (elt, vecp);
|
|
814 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
|
|
815 XCDR (rest) = Fcopy_tree (XCDR (rest), vecp);
|
|
816 rest = XCDR (rest);
|
|
817 }
|
|
818 }
|
|
819 else if (VECTORP (arg) && ! NILP (vecp))
|
|
820 {
|
|
821 int i = vector_length (XVECTOR (arg));
|
|
822 int j;
|
|
823 arg = Fcopy_sequence (arg);
|
|
824 for (j = 0; j < i; j++)
|
|
825 {
|
|
826 Lisp_Object elt = vector_data (XVECTOR (arg)) [j];
|
|
827 QUIT;
|
|
828 if (CONSP (elt) || VECTORP (elt))
|
|
829 vector_data (XVECTOR (arg)) [j] = Fcopy_tree (elt, vecp);
|
|
830 }
|
|
831 }
|
|
832 return arg;
|
|
833 }
|
|
834
|
20
|
835 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
|
0
|
836 Return a substring of STRING, starting at index FROM and ending before TO.
|
|
837 TO may be nil or omitted; then the substring runs to the end of STRING.
|
|
838 If FROM or TO is negative, it counts from the end.
|
|
839 Relevant parts of the string-extent-data are copied in the new string.
|
20
|
840 */
|
|
841 (string, from, to))
|
0
|
842 {
|
|
843 Charcount ccfr, ccto;
|
|
844 Bytecount bfr, bto;
|
|
845 Lisp_Object val;
|
|
846
|
|
847 CHECK_STRING (string);
|
|
848 /* Historically, FROM could not be omitted. Whatever ... */
|
|
849 CHECK_INT (from);
|
|
850 get_string_range_char (string, from, to, &ccfr, &ccto,
|
|
851 GB_HISTORICAL_STRING_BEHAVIOR);
|
14
|
852 bfr = charcount_to_bytecount (XSTRING_DATA (string), ccfr);
|
|
853 bto = charcount_to_bytecount (XSTRING_DATA (string), ccto);
|
|
854 val = make_string (XSTRING_DATA (string) + bfr, bto - bfr);
|
0
|
855 /* Copy any applicable extent information into the new string: */
|
|
856 copy_string_extents (val, string, 0, bfr, bto - bfr);
|
|
857 return (val);
|
|
858 }
|
|
859
|
20
|
860 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
|
0
|
861 Return a subsequence of SEQ, starting at index FROM and ending before TO.
|
|
862 TO may be nil or omitted; then the subsequence runs to the end of SEQ.
|
|
863 If FROM or TO is negative, it counts from the end.
|
|
864 The resulting subsequence is always the same type as the original
|
|
865 sequence.
|
|
866 If SEQ is a string, relevant parts of the string-extent-data are copied
|
|
867 in the new string.
|
20
|
868 */
|
|
869 (seq, from, to))
|
0
|
870 {
|
|
871 int len, f, t;
|
|
872
|
|
873 if (STRINGP (seq))
|
|
874 return Fsubstring (seq, from, to);
|
|
875
|
|
876 if (CONSP (seq) || NILP (seq))
|
|
877 ;
|
|
878 else if (VECTORP (seq) || BIT_VECTORP (seq))
|
|
879 ;
|
|
880 else
|
|
881 {
|
|
882 check_losing_bytecode ("subseq", seq);
|
|
883 seq = wrong_type_argument (Qsequencep, seq);
|
|
884 }
|
|
885
|
|
886 len = XINT (Flength (seq));
|
|
887 CHECK_INT (from);
|
|
888 f = XINT (from);
|
|
889 if (f < 0)
|
|
890 f = len + f;
|
|
891 if (NILP (to))
|
|
892 t = len;
|
|
893 else
|
|
894 {
|
|
895 CHECK_INT (to);
|
|
896 t = XINT (to);
|
|
897 if (t < 0)
|
|
898 t = len + t;
|
|
899 }
|
|
900
|
|
901 if (!(0 <= f && f <= t && t <= len))
|
|
902 args_out_of_range_3 (seq, make_int (f), make_int (t));
|
|
903
|
|
904 if (VECTORP (seq))
|
|
905 {
|
|
906 Lisp_Object result = make_vector (t - f, Qnil);
|
|
907 int i;
|
|
908 Lisp_Object *in_elts = vector_data (XVECTOR (seq));
|
|
909 Lisp_Object *out_elts = vector_data (XVECTOR (result));
|
|
910
|
|
911 for (i = f; i < t; i++)
|
|
912 out_elts[i - f] = in_elts[i];
|
|
913 return result;
|
|
914 }
|
|
915
|
|
916 if (CONSP (seq))
|
|
917 {
|
|
918 Lisp_Object result = Qnil;
|
|
919 int i;
|
|
920
|
|
921 seq = Fnthcdr (make_int (f), seq);
|
|
922
|
|
923 for (i = f; i < t; i++)
|
|
924 {
|
|
925 result = Fcons (Fcar (seq), result);
|
|
926 seq = Fcdr (seq);
|
|
927 }
|
|
928
|
|
929 return Fnreverse (result);
|
|
930 }
|
|
931
|
|
932 /* bit vector */
|
|
933 {
|
|
934 Lisp_Object result = make_bit_vector (t - f, Qzero);
|
|
935 int i;
|
|
936
|
|
937 for (i = f; i < t; i++)
|
|
938 set_bit_vector_bit (XBIT_VECTOR (result), i - f,
|
|
939 bit_vector_bit (XBIT_VECTOR (seq), i));
|
|
940 return result;
|
|
941 }
|
|
942 }
|
|
943
|
|
944
|
20
|
945 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
|
0
|
946 Take cdr N times on LIST, returns the result.
|
20
|
947 */
|
|
948 (n, list))
|
0
|
949 {
|
|
950 REGISTER int i, num;
|
|
951 CHECK_INT (n);
|
|
952 num = XINT (n);
|
|
953 for (i = 0; i < num && !NILP (list); i++)
|
|
954 {
|
|
955 QUIT;
|
|
956 list = Fcdr (list);
|
|
957 }
|
|
958 return list;
|
|
959 }
|
|
960
|
20
|
961 DEFUN ("nth", Fnth, 2, 2, 0, /*
|
0
|
962 Return the Nth element of LIST.
|
|
963 N counts from zero. If LIST is not that long, nil is returned.
|
20
|
964 */
|
|
965 (n, list))
|
0
|
966 {
|
|
967 return Fcar (Fnthcdr (n, list));
|
|
968 }
|
|
969
|
20
|
970 DEFUN ("elt", Felt, 2, 2, 0, /*
|
0
|
971 Return element of SEQUENCE at index N.
|
20
|
972 */
|
|
973 (seq, n))
|
0
|
974 {
|
|
975 retry:
|
|
976 CHECK_INT_COERCE_CHAR (n); /* yuck! */
|
|
977 if (CONSP (seq) || NILP (seq))
|
|
978 {
|
|
979 Lisp_Object tem = Fnthcdr (n, seq);
|
|
980 /* #### Utterly, completely, fucking disgusting.
|
|
981 * #### The whole point of "elt" is that it operates on
|
|
982 * #### sequences, and does error- (bounds-) checking.
|
|
983 */
|
|
984 if (CONSP (tem))
|
|
985 return (XCAR (tem));
|
|
986 else
|
|
987 #if 1
|
|
988 /* This is The Way It Has Always Been. */
|
|
989 return Qnil;
|
|
990 #else
|
|
991 /* This is The Way Mly Says It Should Be. */
|
|
992 args_out_of_range (seq, n);
|
|
993 #endif
|
|
994 }
|
|
995 else if (STRINGP (seq)
|
|
996 || VECTORP (seq)
|
|
997 || BIT_VECTORP (seq))
|
|
998 return (Faref (seq, n));
|
|
999 #ifdef LOSING_BYTECODE
|
|
1000 else if (COMPILED_FUNCTIONP (seq))
|
|
1001 {
|
|
1002 int idx = XINT (n);
|
|
1003 if (idx < 0)
|
|
1004 {
|
|
1005 lose:
|
|
1006 args_out_of_range (seq, n);
|
|
1007 }
|
|
1008 /* Utter perversity */
|
|
1009 {
|
|
1010 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (seq);
|
|
1011 switch (idx)
|
|
1012 {
|
|
1013 case COMPILED_ARGLIST:
|
|
1014 return (b->arglist);
|
|
1015 case COMPILED_BYTECODE:
|
|
1016 return (b->bytecodes);
|
|
1017 case COMPILED_CONSTANTS:
|
|
1018 return (b->constants);
|
|
1019 case COMPILED_STACK_DEPTH:
|
|
1020 return (make_int (b->maxdepth));
|
|
1021 case COMPILED_DOC_STRING:
|
|
1022 return (compiled_function_documentation (b));
|
|
1023 case COMPILED_DOMAIN:
|
|
1024 return (compiled_function_domain (b));
|
|
1025 case COMPILED_INTERACTIVE:
|
|
1026 if (b->flags.interactivep)
|
|
1027 return (compiled_function_interactive (b));
|
|
1028 /* if we return nil, can't tell interactive with no args
|
|
1029 from noninteractive. */
|
|
1030 goto lose;
|
|
1031 default:
|
|
1032 goto lose;
|
|
1033 }
|
|
1034 }
|
|
1035 }
|
|
1036 #endif /* LOSING_BYTECODE */
|
|
1037 else
|
|
1038 {
|
|
1039 check_losing_bytecode ("elt", seq);
|
|
1040 seq = wrong_type_argument (Qsequencep, seq);
|
|
1041 goto retry;
|
|
1042 }
|
|
1043 }
|
|
1044
|
20
|
1045 DEFUN ("member", Fmember, 2, 2, 0, /*
|
0
|
1046 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
|
|
1047 The value is actually the tail of LIST whose car is ELT.
|
20
|
1048 */
|
|
1049 (elt, list))
|
0
|
1050 {
|
|
1051 REGISTER Lisp_Object tail, tem;
|
|
1052 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1053 {
|
|
1054 tem = Fcar (tail);
|
|
1055 if (! NILP (Fequal (elt, tem)))
|
|
1056 return tail;
|
|
1057 QUIT;
|
|
1058 }
|
|
1059 return Qnil;
|
|
1060 }
|
|
1061
|
20
|
1062 DEFUN ("memq", Fmemq, 2, 2, 0, /*
|
0
|
1063 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
|
|
1064 The value is actually the tail of LIST whose car is ELT.
|
20
|
1065 */
|
|
1066 (elt, list))
|
0
|
1067 {
|
|
1068 REGISTER Lisp_Object tail, tem;
|
|
1069 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1070 {
|
|
1071 tem = Fcar (tail);
|
|
1072 if (HACKEQ_UNSAFE (elt, tem)) return tail;
|
|
1073 QUIT;
|
|
1074 }
|
|
1075 return Qnil;
|
|
1076 }
|
|
1077
|
|
1078 Lisp_Object
|
|
1079 memq_no_quit (Lisp_Object elt, Lisp_Object list)
|
|
1080 {
|
|
1081 REGISTER Lisp_Object tail, tem;
|
|
1082 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
1083 {
|
|
1084 tem = XCAR (tail);
|
|
1085 if (HACKEQ_UNSAFE (elt, tem)) return tail;
|
|
1086 }
|
|
1087 return Qnil;
|
|
1088 }
|
|
1089
|
20
|
1090 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
|
0
|
1091 Return non-nil if KEY is `equal' to the car of an element of LIST.
|
|
1092 The value is actually the element of LIST whose car equals KEY.
|
20
|
1093 */
|
|
1094 (key, list))
|
0
|
1095 {
|
|
1096 /* This function can GC. */
|
|
1097 REGISTER Lisp_Object tail, elt, tem;
|
|
1098 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1099 {
|
|
1100 elt = Fcar (tail);
|
|
1101 if (!CONSP (elt)) continue;
|
|
1102 tem = Fequal (Fcar (elt), key);
|
|
1103 if (!NILP (tem)) return elt;
|
|
1104 QUIT;
|
|
1105 }
|
|
1106 return Qnil;
|
|
1107 }
|
|
1108
|
|
1109 Lisp_Object
|
|
1110 assoc_no_quit (Lisp_Object key, Lisp_Object list)
|
|
1111 {
|
|
1112 int speccount = specpdl_depth ();
|
|
1113 specbind (Qinhibit_quit, Qt);
|
|
1114 return (unbind_to (speccount, Fassoc (key, list)));
|
|
1115 }
|
|
1116
|
20
|
1117 DEFUN ("assq", Fassq, 2, 2, 0, /*
|
0
|
1118 Return non-nil if KEY is `eq' to the car of an element of LIST.
|
|
1119 The value is actually the element of LIST whose car is KEY.
|
|
1120 Elements of LIST that are not conses are ignored.
|
20
|
1121 */
|
|
1122 (key, list))
|
0
|
1123 {
|
|
1124 REGISTER Lisp_Object tail, elt, tem;
|
|
1125 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1126 {
|
|
1127 elt = Fcar (tail);
|
|
1128 if (!CONSP (elt)) continue;
|
|
1129 tem = Fcar (elt);
|
|
1130 if (HACKEQ_UNSAFE (key, tem)) return elt;
|
|
1131 QUIT;
|
|
1132 }
|
|
1133 return Qnil;
|
|
1134 }
|
|
1135
|
|
1136 /* Like Fassq but never report an error and do not allow quits.
|
|
1137 Use only on lists known never to be circular. */
|
|
1138
|
|
1139 Lisp_Object
|
|
1140 assq_no_quit (Lisp_Object key, Lisp_Object list)
|
|
1141 {
|
|
1142 /* This cannot GC. */
|
|
1143 REGISTER Lisp_Object tail, elt, tem;
|
|
1144 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
1145 {
|
|
1146 elt = XCAR (tail);
|
|
1147 if (!CONSP (elt)) continue;
|
|
1148 tem = XCAR (elt);
|
|
1149 if (HACKEQ_UNSAFE (key, tem)) return elt;
|
|
1150 }
|
|
1151 return Qnil;
|
|
1152 }
|
|
1153
|
20
|
1154 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
|
0
|
1155 Return non-nil if KEY is `equal' to the cdr of an element of LIST.
|
|
1156 The value is actually the element of LIST whose cdr equals KEY.
|
20
|
1157 */
|
|
1158 (key, list))
|
0
|
1159 {
|
|
1160 REGISTER Lisp_Object tail;
|
|
1161 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1162 {
|
|
1163 REGISTER Lisp_Object elt, tem;
|
|
1164 elt = Fcar (tail);
|
|
1165 if (!CONSP (elt)) continue;
|
|
1166 tem = Fequal (Fcdr (elt), key);
|
|
1167 if (!NILP (tem)) return elt;
|
|
1168 QUIT;
|
|
1169 }
|
|
1170 return Qnil;
|
|
1171 }
|
|
1172
|
20
|
1173 DEFUN ("rassq", Frassq, 2, 2, 0, /*
|
0
|
1174 Return non-nil if KEY is `eq' to the cdr of an element of LIST.
|
|
1175 The value is actually the element of LIST whose cdr is KEY.
|
20
|
1176 */
|
|
1177 (key, list))
|
0
|
1178 {
|
|
1179 REGISTER Lisp_Object tail, elt, tem;
|
|
1180 for (tail = list; !NILP (tail); tail = Fcdr (tail))
|
|
1181 {
|
|
1182 elt = Fcar (tail);
|
|
1183 if (!CONSP (elt)) continue;
|
|
1184 tem = Fcdr (elt);
|
|
1185 if (HACKEQ_UNSAFE (key, tem)) return elt;
|
|
1186 QUIT;
|
|
1187 }
|
|
1188 return Qnil;
|
|
1189 }
|
|
1190
|
|
1191 Lisp_Object
|
|
1192 rassq_no_quit (Lisp_Object key, Lisp_Object list)
|
|
1193 {
|
|
1194 REGISTER Lisp_Object tail, elt, tem;
|
|
1195 for (tail = list; CONSP (tail); tail = XCDR (tail))
|
|
1196 {
|
|
1197 elt = XCAR (tail);
|
|
1198 if (!CONSP (elt)) continue;
|
|
1199 tem = XCDR (elt);
|
|
1200 if (HACKEQ_UNSAFE (key, tem)) return elt;
|
|
1201 }
|
|
1202 return Qnil;
|
|
1203 }
|
|
1204
|
|
1205
|
20
|
1206 DEFUN ("delete", Fdelete, 2, 2, 0, /*
|
0
|
1207 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1208 The modified LIST is returned. Comparison is done with `equal'.
|
|
1209 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1210 effect; therefore, write `(setq foo (delete element foo))' to be sure
|
|
1211 of changing the value of `foo'.
|
20
|
1212 */
|
|
1213 (elt, list))
|
0
|
1214 {
|
|
1215 REGISTER Lisp_Object tail, prev;
|
|
1216
|
|
1217 tail = list;
|
|
1218 prev = Qnil;
|
|
1219 while (!NILP (tail))
|
|
1220 {
|
|
1221 if (!NILP (Fequal (elt, Fcar (tail))))
|
|
1222 {
|
|
1223 if (NILP (prev))
|
|
1224 list = Fcdr (tail);
|
|
1225 else
|
|
1226 Fsetcdr (prev, Fcdr (tail));
|
|
1227 }
|
|
1228 else
|
|
1229 prev = tail;
|
|
1230 tail = Fcdr (tail);
|
|
1231 QUIT;
|
|
1232 }
|
|
1233 return list;
|
|
1234 }
|
|
1235
|
20
|
1236 DEFUN ("delq", Fdelq, 2, 2, 0, /*
|
0
|
1237 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1238 The modified LIST is returned. Comparison is done with `eq'.
|
|
1239 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1240 effect; therefore, write `(setq foo (delq element foo))' to be sure of
|
|
1241 changing the value of `foo'.
|
20
|
1242 */
|
|
1243 (elt, list))
|
0
|
1244 {
|
|
1245 REGISTER Lisp_Object tail, prev;
|
|
1246 REGISTER Lisp_Object tem;
|
|
1247
|
|
1248 tail = list;
|
|
1249 prev = Qnil;
|
|
1250 while (!NILP (tail))
|
|
1251 {
|
|
1252 tem = Fcar (tail);
|
|
1253 if (HACKEQ_UNSAFE (elt, tem))
|
|
1254 {
|
|
1255 if (NILP (prev))
|
|
1256 list = Fcdr (tail);
|
|
1257 else
|
|
1258 Fsetcdr (prev, Fcdr (tail));
|
|
1259 }
|
|
1260 else
|
|
1261 prev = tail;
|
|
1262 tail = Fcdr (tail);
|
|
1263 QUIT;
|
|
1264 }
|
|
1265 return list;
|
|
1266 }
|
|
1267
|
|
1268 /* no quit, no errors; be careful */
|
|
1269
|
|
1270 Lisp_Object
|
|
1271 delq_no_quit (Lisp_Object elt, Lisp_Object list)
|
|
1272 {
|
|
1273 REGISTER Lisp_Object tail, prev;
|
|
1274 REGISTER Lisp_Object tem;
|
|
1275
|
|
1276 tail = list;
|
|
1277 prev = Qnil;
|
|
1278 while (CONSP (tail))
|
|
1279 {
|
|
1280 tem = XCAR (tail);
|
|
1281 if (HACKEQ_UNSAFE (elt, tem))
|
|
1282 {
|
|
1283 if (NILP (prev))
|
|
1284 list = XCDR (tail);
|
|
1285 else
|
|
1286 XCDR (prev) = XCDR (tail);
|
|
1287 }
|
|
1288 else
|
|
1289 prev = tail;
|
|
1290 tail = XCDR (tail);
|
|
1291 }
|
|
1292 return list;
|
|
1293 }
|
|
1294
|
|
1295 /* Be VERY careful with this. This is like delq_no_quit() but
|
|
1296 also calls free_cons() on the removed conses. You must be SURE
|
|
1297 that no pointers to the freed conses remain around (e.g.
|
|
1298 someone else is pointing to part of the list). This function
|
|
1299 is useful on internal lists that are used frequently and where
|
|
1300 the actual list doesn't escape beyond known code bounds. */
|
|
1301
|
|
1302 Lisp_Object
|
|
1303 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
|
|
1304 {
|
|
1305 REGISTER Lisp_Object tail, prev;
|
|
1306 REGISTER Lisp_Object tem;
|
|
1307
|
|
1308 tail = list;
|
|
1309 prev = Qnil;
|
|
1310 while (CONSP (tail))
|
|
1311 {
|
|
1312 Lisp_Object cons_to_free = Qnil;
|
|
1313 tem = XCAR (tail);
|
|
1314 if (HACKEQ_UNSAFE (elt, tem))
|
|
1315 {
|
|
1316 if (NILP (prev))
|
|
1317 list = XCDR (tail);
|
|
1318 else
|
|
1319 XCDR (prev) = XCDR (tail);
|
|
1320 cons_to_free = tail;
|
|
1321 }
|
|
1322 else
|
|
1323 prev = tail;
|
|
1324 tail = XCDR (tail);
|
|
1325 if (!NILP (cons_to_free))
|
|
1326 free_cons (XCONS (cons_to_free));
|
|
1327 }
|
|
1328 return list;
|
|
1329 }
|
|
1330
|
20
|
1331 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
|
0
|
1332 Delete by side effect any elements of LIST whose car is `equal' to KEY.
|
|
1333 The modified LIST is returned. If the first member of LIST has a car
|
|
1334 that is `equal' to KEY, there is no way to remove it by side effect;
|
|
1335 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
|
|
1336 the value of `foo'.
|
20
|
1337 */
|
|
1338 (key, list))
|
0
|
1339 {
|
|
1340 REGISTER Lisp_Object tail, prev;
|
|
1341
|
|
1342 tail = list;
|
|
1343 prev = Qnil;
|
|
1344 while (!NILP (tail))
|
|
1345 {
|
|
1346 Lisp_Object elt = Fcar (tail);
|
|
1347 if (CONSP (elt) && ! NILP (Fequal (key, Fcar (elt))))
|
|
1348 {
|
|
1349 if (NILP (prev))
|
|
1350 list = Fcdr (tail);
|
|
1351 else
|
|
1352 Fsetcdr (prev, Fcdr (tail));
|
|
1353 }
|
|
1354 else
|
|
1355 prev = tail;
|
|
1356 tail = Fcdr (tail);
|
|
1357 QUIT;
|
|
1358 }
|
|
1359 return list;
|
|
1360 }
|
|
1361
|
|
1362 Lisp_Object
|
|
1363 remassoc_no_quit (Lisp_Object key, Lisp_Object list)
|
|
1364 {
|
|
1365 int speccount = specpdl_depth ();
|
|
1366 specbind (Qinhibit_quit, Qt);
|
|
1367 return (unbind_to (speccount, Fremassoc (key, list)));
|
|
1368 }
|
|
1369
|
20
|
1370 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
|
0
|
1371 Delete by side effect any elements of LIST whose car is `eq' to KEY.
|
|
1372 The modified LIST is returned. If the first member of LIST has a car
|
|
1373 that is `eq' to KEY, there is no way to remove it by side effect;
|
|
1374 therefore, write `(setq foo (remassq key foo))' to be sure of changing
|
|
1375 the value of `foo'.
|
20
|
1376 */
|
|
1377 (key, list))
|
0
|
1378 {
|
|
1379 REGISTER Lisp_Object tail, prev;
|
|
1380
|
|
1381 tail = list;
|
|
1382 prev = Qnil;
|
|
1383 while (!NILP (tail))
|
|
1384 {
|
|
1385 Lisp_Object elt = Fcar (tail);
|
|
1386 if (CONSP (elt) && HACKEQ_UNSAFE (key, Fcar (elt)))
|
|
1387 {
|
|
1388 if (NILP (prev))
|
|
1389 list = Fcdr (tail);
|
|
1390 else
|
|
1391 Fsetcdr (prev, Fcdr (tail));
|
|
1392 }
|
|
1393 else
|
|
1394 prev = tail;
|
|
1395 tail = Fcdr (tail);
|
|
1396 QUIT;
|
|
1397 }
|
|
1398 return list;
|
|
1399 }
|
|
1400
|
|
1401 /* no quit, no errors; be careful */
|
|
1402
|
|
1403 Lisp_Object
|
|
1404 remassq_no_quit (Lisp_Object key, Lisp_Object list)
|
|
1405 {
|
|
1406 REGISTER Lisp_Object tail, prev;
|
|
1407 REGISTER Lisp_Object tem;
|
|
1408
|
|
1409 tail = list;
|
|
1410 prev = Qnil;
|
|
1411 while (CONSP (tail))
|
|
1412 {
|
|
1413 tem = XCAR (tail);
|
|
1414 if (CONSP (tem) && HACKEQ_UNSAFE (key, XCAR (tem)))
|
|
1415 {
|
|
1416 if (NILP (prev))
|
|
1417 list = XCDR (tail);
|
|
1418 else
|
|
1419 XCDR (prev) = XCDR (tail);
|
|
1420 }
|
|
1421 else
|
|
1422 prev = tail;
|
|
1423 tail = XCDR (tail);
|
|
1424 }
|
|
1425 return list;
|
|
1426 }
|
|
1427
|
20
|
1428 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
|
0
|
1429 Delete by side effect any elements of LIST whose cdr is `equal' to VALUE.
|
|
1430 The modified LIST is returned. If the first member of LIST has a car
|
|
1431 that is `equal' to VALUE, there is no way to remove it by side effect;
|
|
1432 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
|
|
1433 the value of `foo'.
|
20
|
1434 */
|
|
1435 (value, list))
|
0
|
1436 {
|
|
1437 REGISTER Lisp_Object tail, prev;
|
|
1438
|
|
1439 tail = list;
|
|
1440 prev = Qnil;
|
|
1441 while (!NILP (tail))
|
|
1442 {
|
|
1443 Lisp_Object elt = Fcar (tail);
|
|
1444 if (CONSP (elt) && ! NILP (Fequal (value, Fcdr (elt))))
|
|
1445 {
|
|
1446 if (NILP (prev))
|
|
1447 list = Fcdr (tail);
|
|
1448 else
|
|
1449 Fsetcdr (prev, Fcdr (tail));
|
|
1450 }
|
|
1451 else
|
|
1452 prev = tail;
|
|
1453 tail = Fcdr (tail);
|
|
1454 QUIT;
|
|
1455 }
|
|
1456 return list;
|
|
1457 }
|
|
1458
|
20
|
1459 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
|
0
|
1460 Delete by side effect any elements of LIST whose cdr is `eq' to VALUE.
|
|
1461 The modified LIST is returned. If the first member of LIST has a car
|
|
1462 that is `eq' to VALUE, there is no way to remove it by side effect;
|
|
1463 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
|
|
1464 the value of `foo'.
|
20
|
1465 */
|
|
1466 (value, list))
|
0
|
1467 {
|
|
1468 REGISTER Lisp_Object tail, prev;
|
|
1469
|
|
1470 tail = list;
|
|
1471 prev = Qnil;
|
|
1472 while (!NILP (tail))
|
|
1473 {
|
|
1474 Lisp_Object elt = Fcar (tail);
|
|
1475 if (CONSP (elt) && HACKEQ_UNSAFE (value, Fcdr (elt)))
|
|
1476 {
|
|
1477 if (NILP (prev))
|
|
1478 list = Fcdr (tail);
|
|
1479 else
|
|
1480 Fsetcdr (prev, Fcdr (tail));
|
|
1481 }
|
|
1482 else
|
|
1483 prev = tail;
|
|
1484 tail = Fcdr (tail);
|
|
1485 QUIT;
|
|
1486 }
|
|
1487 return list;
|
|
1488 }
|
|
1489
|
|
1490 /* no quit, no errors; be careful */
|
|
1491
|
|
1492 Lisp_Object
|
|
1493 remrassq_no_quit (Lisp_Object value, Lisp_Object list)
|
|
1494 {
|
|
1495 REGISTER Lisp_Object tail, prev;
|
|
1496 REGISTER Lisp_Object tem;
|
|
1497
|
|
1498 tail = list;
|
|
1499 prev = Qnil;
|
|
1500 while (CONSP (tail))
|
|
1501 {
|
|
1502 tem = XCAR (tail);
|
|
1503 if (CONSP (tem) && HACKEQ_UNSAFE (value, XCDR (tem)))
|
|
1504 {
|
|
1505 if (NILP (prev))
|
|
1506 list = XCDR (tail);
|
|
1507 else
|
|
1508 XCDR (prev) = XCDR (tail);
|
|
1509 }
|
|
1510 else
|
|
1511 prev = tail;
|
|
1512 tail = XCDR (tail);
|
|
1513 }
|
|
1514 return list;
|
|
1515 }
|
|
1516
|
20
|
1517 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
|
0
|
1518 Reverse LIST by modifying cdr pointers.
|
|
1519 Returns the beginning of the reversed list.
|
20
|
1520 */
|
|
1521 (list))
|
0
|
1522 {
|
|
1523 Lisp_Object prev, tail, next;
|
|
1524 struct gcpro gcpro1, gcpro2;
|
|
1525
|
|
1526 /* We gcpro our args; see `nconc' */
|
|
1527 prev = Qnil;
|
|
1528 tail = list;
|
|
1529 GCPRO2 (prev, tail);
|
|
1530 while (!NILP (tail))
|
|
1531 {
|
|
1532 QUIT;
|
|
1533 next = Fcdr (tail);
|
|
1534 Fsetcdr (tail, prev);
|
|
1535 prev = tail;
|
|
1536 tail = next;
|
|
1537 }
|
|
1538 UNGCPRO;
|
|
1539 return prev;
|
|
1540 }
|
|
1541
|
20
|
1542 DEFUN ("reverse", Freverse, 1, 1, 0, /*
|
0
|
1543 Reverse LIST, copying. Returns the beginning of the reversed list.
|
|
1544 See also the function `nreverse', which is used more often.
|
20
|
1545 */
|
|
1546 (list))
|
0
|
1547 {
|
|
1548 Lisp_Object length;
|
|
1549 Lisp_Object *vec;
|
|
1550 Lisp_Object tail;
|
|
1551 REGISTER int i;
|
|
1552
|
|
1553 length = Flength (list);
|
|
1554 vec = (Lisp_Object *) alloca (XINT (length) * sizeof (Lisp_Object));
|
|
1555 for (i = XINT (length) - 1, tail = list; i >= 0; i--, tail = Fcdr (tail))
|
|
1556 vec[i] = Fcar (tail);
|
|
1557
|
|
1558 return Flist (XINT (length), vec);
|
|
1559 }
|
|
1560
|
|
1561 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1562 Lisp_Object lisp_arg,
|
|
1563 int (*pred_fn) (Lisp_Object, Lisp_Object,
|
|
1564 Lisp_Object lisp_arg));
|
|
1565
|
|
1566 Lisp_Object
|
|
1567 list_sort (Lisp_Object list,
|
|
1568 Lisp_Object lisp_arg,
|
|
1569 int (*pred_fn) (Lisp_Object, Lisp_Object,
|
|
1570 Lisp_Object lisp_arg))
|
|
1571 {
|
|
1572 Lisp_Object front, back;
|
|
1573 Lisp_Object len, tem;
|
|
1574 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1575 int length;
|
|
1576
|
|
1577 front = list;
|
|
1578 len = Flength (list);
|
|
1579 length = XINT (len);
|
|
1580 if (length < 2)
|
|
1581 return list;
|
|
1582
|
|
1583 XSETINT (len, (length / 2) - 1);
|
|
1584 tem = Fnthcdr (len, list);
|
|
1585 back = Fcdr (tem);
|
|
1586 Fsetcdr (tem, Qnil);
|
|
1587
|
|
1588 GCPRO3 (front, back, lisp_arg);
|
|
1589 front = list_sort (front, lisp_arg, pred_fn);
|
|
1590 back = list_sort (back, lisp_arg, pred_fn);
|
|
1591 UNGCPRO;
|
|
1592 return list_merge (front, back, lisp_arg, pred_fn);
|
|
1593 }
|
|
1594
|
|
1595
|
|
1596 static int
|
|
1597 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
|
|
1598 Lisp_Object pred)
|
|
1599 {
|
|
1600 Lisp_Object tmp;
|
|
1601
|
|
1602 /* prevents the GC from happening in call2 */
|
|
1603 int speccount = specpdl_depth ();
|
|
1604 /* Emacs' GC doesn't actually relocate pointers, so this probably
|
|
1605 isn't strictly necessary */
|
|
1606 record_unwind_protect (restore_gc_inhibit,
|
|
1607 make_int (gc_currently_forbidden));
|
|
1608 gc_currently_forbidden = 1;
|
|
1609 tmp = call2 (pred, obj1, obj2);
|
|
1610 unbind_to (speccount, Qnil);
|
|
1611
|
|
1612 if (NILP (tmp))
|
|
1613 return -1;
|
|
1614 else
|
|
1615 return 1;
|
|
1616 }
|
|
1617
|
20
|
1618 DEFUN ("sort", Fsort, 2, 2, 0, /*
|
0
|
1619 Sort LIST, stably, comparing elements using PREDICATE.
|
|
1620 Returns the sorted list. LIST is modified by side effects.
|
|
1621 PREDICATE is called with two elements of LIST, and should return T
|
|
1622 if the first element is \"less\" than the second.
|
20
|
1623 */
|
|
1624 (list, pred))
|
0
|
1625 {
|
|
1626 return list_sort (list, pred, merge_pred_function);
|
|
1627 }
|
|
1628
|
|
1629 Lisp_Object
|
|
1630 merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1631 Lisp_Object pred)
|
|
1632 {
|
|
1633 return list_merge (org_l1, org_l2, pred, merge_pred_function);
|
|
1634 }
|
|
1635
|
|
1636
|
|
1637 static Lisp_Object
|
|
1638 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1639 Lisp_Object lisp_arg,
|
|
1640 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
|
|
1641 {
|
|
1642 Lisp_Object value;
|
|
1643 Lisp_Object tail;
|
|
1644 Lisp_Object tem;
|
|
1645 Lisp_Object l1, l2;
|
|
1646 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
1647
|
|
1648 l1 = org_l1;
|
|
1649 l2 = org_l2;
|
|
1650 tail = Qnil;
|
|
1651 value = Qnil;
|
|
1652
|
|
1653 /* It is sufficient to protect org_l1 and org_l2.
|
|
1654 When l1 and l2 are updated, we copy the new values
|
|
1655 back into the org_ vars. */
|
|
1656
|
|
1657 GCPRO4 (org_l1, org_l2, lisp_arg, value);
|
|
1658
|
|
1659 while (1)
|
|
1660 {
|
|
1661 if (NILP (l1))
|
|
1662 {
|
|
1663 UNGCPRO;
|
|
1664 if (NILP (tail))
|
|
1665 return l2;
|
|
1666 Fsetcdr (tail, l2);
|
|
1667 return value;
|
|
1668 }
|
|
1669 if (NILP (l2))
|
|
1670 {
|
|
1671 UNGCPRO;
|
|
1672 if (NILP (tail))
|
|
1673 return l1;
|
|
1674 Fsetcdr (tail, l1);
|
|
1675 return value;
|
|
1676 }
|
|
1677
|
|
1678 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
|
|
1679 {
|
|
1680 tem = l1;
|
|
1681 l1 = Fcdr (l1);
|
|
1682 org_l1 = l1;
|
|
1683 }
|
|
1684 else
|
|
1685 {
|
|
1686 tem = l2;
|
|
1687 l2 = Fcdr (l2);
|
|
1688 org_l2 = l2;
|
|
1689 }
|
|
1690 if (NILP (tail))
|
|
1691 value = tem;
|
|
1692 else
|
|
1693 Fsetcdr (tail, tem);
|
|
1694 tail = tem;
|
|
1695 }
|
|
1696 }
|
|
1697
|
|
1698
|
|
1699 /************************************************************************/
|
|
1700 /* property-list functions */
|
|
1701 /************************************************************************/
|
|
1702
|
|
1703 /* For properties of text, we need to do order-insensitive comparison of
|
|
1704 plists. That is, we need to compare two plists such that they are the
|
|
1705 same if they have the same set of keys, and equivalent values.
|
|
1706 So (a 1 b 2) would be equal to (b 2 a 1).
|
|
1707
|
|
1708 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
|
|
1709 LAXP means use `equal' for comparisons.
|
|
1710 */
|
|
1711 int
|
|
1712 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
|
|
1713 int laxp, int depth)
|
|
1714 {
|
|
1715 int eqp = (depth == -1); /* -1 as depth means us eq, not equal. */
|
|
1716 int la, lb, m, i, fill;
|
|
1717 Lisp_Object *keys, *vals;
|
|
1718 char *flags;
|
|
1719 Lisp_Object rest;
|
|
1720
|
|
1721 if (NILP (a) && NILP (b))
|
|
1722 return 0;
|
|
1723
|
|
1724 Fcheck_valid_plist (a);
|
|
1725 Fcheck_valid_plist (b);
|
|
1726
|
|
1727 la = XINT (Flength (a));
|
|
1728 lb = XINT (Flength (b));
|
|
1729 m = (la > lb ? la : lb);
|
|
1730 fill = 0;
|
|
1731 keys = (Lisp_Object *) alloca (m * sizeof (Lisp_Object));
|
|
1732 vals = (Lisp_Object *) alloca (m * sizeof (Lisp_Object));
|
|
1733 flags = (char *) alloca (m * sizeof (char));
|
|
1734
|
|
1735 /* First extract the pairs from A. */
|
|
1736 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
|
|
1737 {
|
|
1738 Lisp_Object k = XCAR (rest);
|
|
1739 Lisp_Object v = XCAR (XCDR (rest));
|
|
1740 /* Maybe be Ebolified. */
|
|
1741 if (nil_means_not_present && NILP (v)) continue;
|
|
1742 keys [fill] = k;
|
|
1743 vals [fill] = v;
|
|
1744 flags[fill] = 0;
|
|
1745 fill++;
|
|
1746 }
|
|
1747 /* Now iterate over B, and stop if we find something that's not in A,
|
|
1748 or that doesn't match. As we match, mark them. */
|
|
1749 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
|
|
1750 {
|
|
1751 Lisp_Object k = XCAR (rest);
|
|
1752 Lisp_Object v = XCAR (XCDR (rest));
|
|
1753 /* Maybe be Ebolified. */
|
|
1754 if (nil_means_not_present && NILP (v)) continue;
|
|
1755 for (i = 0; i < fill; i++)
|
|
1756 {
|
|
1757 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
|
|
1758 {
|
|
1759 if ((eqp
|
|
1760 /* Ebolified here too, sigh ... */
|
|
1761 ? !HACKEQ_UNSAFE (v, vals [i])
|
|
1762 : !internal_equal (v, vals [i], depth)))
|
|
1763 /* a property in B has a different value than in A */
|
|
1764 goto MISMATCH;
|
|
1765 flags [i] = 1;
|
|
1766 break;
|
|
1767 }
|
|
1768 }
|
|
1769 if (i == fill)
|
|
1770 /* there are some properties in B that are not in A */
|
|
1771 goto MISMATCH;
|
|
1772 }
|
|
1773 /* Now check to see that all the properties in A were also in B */
|
|
1774 for (i = 0; i < fill; i++)
|
|
1775 if (flags [i] == 0)
|
|
1776 goto MISMATCH;
|
|
1777
|
|
1778 /* Ok. */
|
|
1779 return 0;
|
|
1780
|
|
1781 MISMATCH:
|
|
1782 return 1;
|
|
1783 }
|
|
1784
|
20
|
1785 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
|
0
|
1786 Return non-nil if property lists A and B are `eq'.
|
|
1787 A property list is an alternating list of keywords and values.
|
|
1788 This function does order-insensitive comparisons of the property lists:
|
|
1789 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1790 Comparison between values is done using `eq'. See also `plists-equal'.
|
|
1791 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1792 a nil value is ignored. This feature is a virus that has infected
|
16
|
1793 old Lisp implementations, but should not be used except for backward
|
|
1794 compatibility.
|
20
|
1795 */
|
|
1796 (a, b, nil_means_not_present))
|
0
|
1797 {
|
|
1798 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
|
|
1799 ? Qnil : Qt);
|
|
1800 }
|
|
1801
|
20
|
1802 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
|
0
|
1803 Return non-nil if property lists A and B are `equal'.
|
|
1804 A property list is an alternating list of keywords and values. This
|
|
1805 function does order-insensitive comparisons of the property lists: For
|
|
1806 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1807 Comparison between values is done using `equal'. See also `plists-eq'.
|
|
1808 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1809 a nil value is ignored. This feature is a virus that has infected
|
16
|
1810 old Lisp implementations, but should not be used except for backward
|
|
1811 compatibility.
|
20
|
1812 */
|
|
1813 (a, b, nil_means_not_present))
|
0
|
1814 {
|
|
1815 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
|
|
1816 ? Qnil : Qt);
|
|
1817 }
|
|
1818
|
|
1819
|
20
|
1820 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
|
0
|
1821 Return non-nil if lax property lists A and B are `eq'.
|
|
1822 A property list is an alternating list of keywords and values.
|
|
1823 This function does order-insensitive comparisons of the property lists:
|
|
1824 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1825 Comparison between values is done using `eq'. See also `plists-equal'.
|
|
1826 A lax property list is like a regular one except that comparisons between
|
|
1827 keywords is done using `equal' instead of `eq'.
|
|
1828 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1829 a nil value is ignored. This feature is a virus that has infected
|
16
|
1830 old Lisp implementations, but should not be used except for backward
|
|
1831 compatibility.
|
20
|
1832 */
|
|
1833 (a, b, nil_means_not_present))
|
0
|
1834 {
|
|
1835 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
|
|
1836 ? Qnil : Qt);
|
|
1837 }
|
|
1838
|
20
|
1839 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
|
0
|
1840 Return non-nil if lax property lists A and B are `equal'.
|
|
1841 A property list is an alternating list of keywords and values. This
|
|
1842 function does order-insensitive comparisons of the property lists: For
|
|
1843 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1844 Comparison between values is done using `equal'. See also `plists-eq'.
|
|
1845 A lax property list is like a regular one except that comparisons between
|
|
1846 keywords is done using `equal' instead of `eq'.
|
|
1847 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1848 a nil value is ignored. This feature is a virus that has infected
|
16
|
1849 old Lisp implementations, but should not be used except for backward
|
|
1850 compatibility.
|
20
|
1851 */
|
|
1852 (a, b, nil_means_not_present))
|
0
|
1853 {
|
|
1854 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
|
|
1855 ? Qnil : Qt);
|
|
1856 }
|
|
1857
|
|
1858 /* Return the value associated with key PROPERTY in property list PLIST.
|
|
1859 Return nil if key not found. This function is used for internal
|
|
1860 property lists that cannot be directly manipulated by the user.
|
|
1861 */
|
|
1862
|
|
1863 Lisp_Object
|
|
1864 internal_plist_get (Lisp_Object plist, Lisp_Object property)
|
|
1865 {
|
|
1866 Lisp_Object tail = plist;
|
|
1867
|
|
1868 for (; !NILP (tail); tail = XCDR (XCDR (tail)))
|
|
1869 {
|
|
1870 struct Lisp_Cons *c = XCONS (tail);
|
|
1871 if (EQ (c->car, property))
|
|
1872 return XCAR (c->cdr);
|
|
1873 }
|
|
1874
|
|
1875 return Qunbound;
|
|
1876 }
|
|
1877
|
|
1878 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
|
|
1879 internal_plist_get(). */
|
|
1880
|
|
1881 void
|
|
1882 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
|
|
1883 Lisp_Object value)
|
|
1884 {
|
|
1885 Lisp_Object tail = *plist;
|
|
1886
|
|
1887 for (; !NILP (tail); tail = XCDR (XCDR (tail)))
|
|
1888 {
|
|
1889 struct Lisp_Cons *c = XCONS (tail);
|
|
1890 if (EQ (c->car, property))
|
|
1891 {
|
|
1892 XCAR (c->cdr) = value;
|
|
1893 return;
|
|
1894 }
|
|
1895 }
|
|
1896
|
|
1897 *plist = Fcons (property, Fcons (value, *plist));
|
|
1898 }
|
|
1899
|
|
1900 int
|
|
1901 internal_remprop (Lisp_Object *plist, Lisp_Object property)
|
|
1902 {
|
|
1903 Lisp_Object tail = *plist;
|
|
1904
|
|
1905 if (NILP (tail))
|
|
1906 return 0;
|
|
1907
|
|
1908 if (EQ (XCAR (tail), property))
|
|
1909 {
|
|
1910 *plist = XCDR (XCDR (tail));
|
|
1911 return 1;
|
|
1912 }
|
|
1913
|
|
1914 for (tail = XCDR (tail); !NILP (XCDR (tail));
|
|
1915 tail = XCDR (XCDR (tail)))
|
|
1916 {
|
|
1917 struct Lisp_Cons *c = XCONS (tail);
|
|
1918 if (EQ (XCAR (c->cdr), property))
|
|
1919 {
|
|
1920 c->cdr = XCDR (XCDR (c->cdr));
|
|
1921 return 1;
|
|
1922 }
|
|
1923 }
|
|
1924
|
|
1925 return 0;
|
|
1926 }
|
|
1927
|
|
1928 /* Called on a malformed property list. BADPLACE should be some
|
|
1929 place where truncating will form a good list -- i.e. we shouldn't
|
|
1930 result in a list with an odd length. */
|
|
1931
|
|
1932 static Lisp_Object
|
|
1933 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
|
|
1934 {
|
|
1935 if (ERRB_EQ (errb, ERROR_ME))
|
|
1936 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
|
|
1937 else
|
|
1938 {
|
|
1939 if (ERRB_EQ (errb, ERROR_ME_WARN))
|
|
1940 {
|
|
1941 warn_when_safe_lispobj
|
|
1942 (Qlist, Qwarning,
|
|
1943 list2 (build_string
|
|
1944 ("Malformed property list -- list has been truncated"),
|
|
1945 *plist));
|
|
1946 *badplace = Qnil;
|
|
1947 }
|
|
1948 return Qunbound;
|
|
1949 }
|
|
1950 }
|
|
1951
|
|
1952 /* Called on a circular property list. BADPLACE should be some place
|
|
1953 where truncating will result in an even-length list, as above.
|
|
1954 If doesn't particularly matter where we truncate -- anywhere we
|
|
1955 truncate along the entire list will break the circularity, because
|
|
1956 it will create a terminus and the list currently doesn't have one.
|
|
1957 */
|
|
1958
|
|
1959 static Lisp_Object
|
|
1960 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
|
|
1961 {
|
|
1962 if (ERRB_EQ (errb, ERROR_ME))
|
|
1963 /* #### Eek, this will probably result in another error
|
|
1964 when PLIST is printed out */
|
|
1965 return Fsignal (Qcircular_property_list, list1 (*plist));
|
|
1966 else
|
|
1967 {
|
|
1968 if (ERRB_EQ (errb, ERROR_ME_WARN))
|
|
1969 {
|
|
1970 warn_when_safe_lispobj
|
|
1971 (Qlist, Qwarning,
|
|
1972 list2 (build_string
|
|
1973 ("Circular property list -- list has been truncated"),
|
|
1974 *plist));
|
|
1975 *badplace = Qnil;
|
|
1976 }
|
|
1977 return Qunbound;
|
|
1978 }
|
|
1979 }
|
|
1980
|
|
1981 /* Advance the tortoise pointer by two (one iteration of a property-list
|
|
1982 loop) and the hare pointer by four and verify that no malformations
|
|
1983 or circularities exist. If so, return zero and store a value into
|
|
1984 RETVAL that should be returned by the calling function. Otherwise,
|
|
1985 return 1. See external_plist_get().
|
|
1986 */
|
|
1987
|
|
1988 static int
|
|
1989 advance_plist_pointers (Lisp_Object *plist,
|
|
1990 Lisp_Object **tortoise, Lisp_Object **hare,
|
|
1991 Error_behavior errb, Lisp_Object *retval)
|
|
1992 {
|
|
1993 int i;
|
|
1994 Lisp_Object *tortsave = *tortoise;
|
|
1995
|
|
1996 /* Note that our "fixing" may be more brutal than necessary,
|
|
1997 but it's the user's own problem, not ours. if they went in and
|
|
1998 manually fucked up a plist. */
|
|
1999
|
|
2000 for (i = 0; i < 2; i++)
|
|
2001 {
|
|
2002 /* This is a standard iteration of a defensive-loop-checking
|
|
2003 loop. We just do it twice because we want to advance past
|
|
2004 both the property and its value.
|
|
2005
|
|
2006 If the pointer indirection is confusing you, remember that
|
|
2007 one level of indirection on the hare and tortoise pointers
|
|
2008 is only due to pass-by-reference for this function. The other
|
|
2009 level is so that the plist can be fixed in place. */
|
|
2010
|
|
2011 /* When we reach the end of a well-formed plist, **HARE is
|
|
2012 nil. In that case, we don't do anything at all except
|
|
2013 advance TORTOISE by one. Otherwise, we advance HARE
|
|
2014 by two (making sure it's OK to do so), then advance
|
|
2015 TORTOISE by one (it will always be OK to do so because
|
|
2016 the HARE is always ahead of the TORTOISE and will have
|
|
2017 already verified the path), then make sure TORTOISE and
|
|
2018 HARE don't contain the same non-nil object -- if the
|
|
2019 TORTOISE and the HARE ever meet, then obviously we're
|
|
2020 in a circularity, and if we're in a circularity, then
|
|
2021 the TORTOISE and the HARE can't cross paths without
|
|
2022 meeting, since the HARE only gains one step over the
|
|
2023 TORTOISE per iteration. */
|
|
2024
|
|
2025 if (!NILP (**hare))
|
|
2026 {
|
|
2027 Lisp_Object *haresave = *hare;
|
|
2028 if (!CONSP (**hare))
|
|
2029 {
|
|
2030 *retval = bad_bad_bunny (plist, haresave, errb);
|
|
2031 return 0;
|
|
2032 }
|
|
2033 *hare = &XCDR (**hare);
|
|
2034 /* In a non-plist, we'd check here for a nil value for
|
|
2035 **HARE, which is OK (it just means the list has an
|
|
2036 odd number of elements). In a plist, it's not OK
|
|
2037 for the list to have an odd number of elements. */
|
|
2038 if (!CONSP (**hare))
|
|
2039 {
|
|
2040 *retval = bad_bad_bunny (plist, haresave, errb);
|
|
2041 return 0;
|
|
2042 }
|
|
2043 *hare = &XCDR (**hare);
|
|
2044 }
|
|
2045
|
|
2046 *tortoise = &XCDR (**tortoise);
|
|
2047 if (!NILP (**hare) && EQ (**tortoise, **hare))
|
|
2048 {
|
|
2049 *retval = bad_bad_turtle (plist, tortsave, errb);
|
|
2050 return 0;
|
|
2051 }
|
|
2052 }
|
|
2053
|
|
2054 return 1;
|
|
2055 }
|
|
2056
|
|
2057 /* Return the value of PROPERTY from PLIST, or Qunbound if
|
|
2058 property is not on the list.
|
|
2059
|
|
2060 PLIST is a Lisp-accessible property list, meaning that it
|
|
2061 has to be checked for malformations and circularities.
|
|
2062
|
|
2063 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
|
|
2064 function will never signal an error; and if ERRB is ERROR_ME_WARN,
|
|
2065 on finding a malformation or a circularity, it issues a warning and
|
|
2066 attempts to silently fix the problem.
|
|
2067
|
|
2068 A pointer to PLIST is passed in so that PLIST can be successfully
|
|
2069 "fixed" even if the error is at the beginning of the plist. */
|
|
2070
|
|
2071 Lisp_Object
|
|
2072 external_plist_get (Lisp_Object *plist, Lisp_Object property,
|
|
2073 int laxp, Error_behavior errb)
|
|
2074 {
|
|
2075 Lisp_Object *tortoise = plist;
|
|
2076 Lisp_Object *hare = plist;
|
|
2077
|
|
2078 while (!NILP (*tortoise))
|
|
2079 {
|
|
2080 Lisp_Object *tortsave = tortoise;
|
|
2081 Lisp_Object retval;
|
|
2082
|
|
2083 /* We do the standard tortoise/hare march. We isolate the
|
|
2084 grungy stuff to do this in advance_plist_pointers(), though.
|
|
2085 To us, all this function does is advance the tortoise
|
|
2086 pointer by two and the hare pointer by four and make sure
|
|
2087 everything's OK. We first advance the pointers and then
|
|
2088 check if a property matched; this ensures that our
|
|
2089 check for a matching property is safe. */
|
|
2090
|
|
2091 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2092 return retval;
|
|
2093
|
|
2094 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2095 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2096 return XCAR (XCDR (*tortsave));
|
|
2097 }
|
|
2098
|
|
2099 return Qunbound;
|
|
2100 }
|
|
2101
|
|
2102 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
|
|
2103 malformed or circular plist. Analogous to external_plist_get(). */
|
|
2104
|
|
2105 void
|
|
2106 external_plist_put (Lisp_Object *plist, Lisp_Object property,
|
|
2107 Lisp_Object value, int laxp, Error_behavior errb)
|
|
2108 {
|
|
2109 Lisp_Object *tortoise = plist;
|
|
2110 Lisp_Object *hare = plist;
|
|
2111
|
|
2112 while (!NILP (*tortoise))
|
|
2113 {
|
|
2114 Lisp_Object *tortsave = tortoise;
|
|
2115 Lisp_Object retval;
|
|
2116
|
|
2117 /* See above */
|
|
2118 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2119 return;
|
|
2120
|
|
2121 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2122 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2123 {
|
|
2124 XCAR (XCDR (*tortsave)) = value;
|
|
2125 return;
|
|
2126 }
|
|
2127 }
|
|
2128
|
|
2129 *plist = Fcons (property, Fcons (value, *plist));
|
|
2130 }
|
|
2131
|
|
2132 int
|
|
2133 external_remprop (Lisp_Object *plist, Lisp_Object property,
|
|
2134 int laxp, Error_behavior errb)
|
|
2135 {
|
|
2136 Lisp_Object *tortoise = plist;
|
|
2137 Lisp_Object *hare = plist;
|
|
2138
|
|
2139 while (!NILP (*tortoise))
|
|
2140 {
|
|
2141 Lisp_Object *tortsave = tortoise;
|
|
2142 Lisp_Object retval;
|
|
2143
|
|
2144 /* See above */
|
|
2145 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2146 return 0;
|
|
2147
|
|
2148 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2149 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2150 {
|
|
2151 /* Now you see why it's so convenient to have that level
|
|
2152 of indirection. */
|
|
2153 *tortsave = XCDR (XCDR (*tortsave));
|
|
2154 return 1;
|
|
2155 }
|
|
2156 }
|
|
2157
|
|
2158 return 0;
|
|
2159 }
|
|
2160
|
20
|
2161 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
|
0
|
2162 Extract a value from a property list.
|
|
2163 PLIST is a property list, which is a list of the form
|
|
2164 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
|
|
2165 corresponding to the given PROP, or DEFAULT if PROP is not
|
|
2166 one of the properties on the list.
|
20
|
2167 */
|
|
2168 (plist, prop, defalt)) /* Cant spel in C */
|
0
|
2169 {
|
|
2170 Lisp_Object val = external_plist_get (&plist, prop, 0, ERROR_ME);
|
|
2171 if (UNBOUNDP (val))
|
|
2172 return defalt;
|
|
2173 return val;
|
|
2174 }
|
|
2175
|
20
|
2176 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
|
0
|
2177 Change value in PLIST of PROP to VAL.
|
|
2178 PLIST is a property list, which is a list of the form \(PROP1 VALUE1
|
|
2179 PROP2 VALUE2 ...). PROP is usually a symbol and VAL is any object.
|
|
2180 If PROP is already a property on the list, its value is set to VAL,
|
|
2181 otherwise the new PROP VAL pair is added. The new plist is returned;
|
|
2182 use `(setq x (plist-put x prop val))' to be sure to use the new value.
|
|
2183 The PLIST is modified by side effects.
|
20
|
2184 */
|
|
2185 (plist, prop, val))
|
0
|
2186 {
|
|
2187 external_plist_put (&plist, prop, val, 0, ERROR_ME);
|
|
2188 return plist;
|
|
2189 }
|
|
2190
|
20
|
2191 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
|
0
|
2192 Remove from PLIST the property PROP and its value.
|
|
2193 PLIST is a property list, which is a list of the form \(PROP1 VALUE1
|
|
2194 PROP2 VALUE2 ...). PROP is usually a symbol. The new plist is
|
|
2195 returned; use `(setq x (plist-remprop x prop val))' to be sure to use
|
|
2196 the new value. The PLIST is modified by side effects.
|
20
|
2197 */
|
|
2198 (plist, prop))
|
0
|
2199 {
|
|
2200 external_remprop (&plist, prop, 0, ERROR_ME);
|
|
2201 return plist;
|
|
2202 }
|
|
2203
|
20
|
2204 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
|
0
|
2205 Return t if PROP has a value specified in PLIST.
|
20
|
2206 */
|
|
2207 (plist, prop))
|
0
|
2208 {
|
|
2209 return UNBOUNDP (Fplist_get (plist, prop, Qunbound)) ? Qnil : Qt;
|
|
2210 }
|
|
2211
|
20
|
2212 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
|
0
|
2213 Given a plist, signal an error if there is anything wrong with it.
|
|
2214 This means that it's a malformed or circular plist.
|
20
|
2215 */
|
|
2216 (plist))
|
0
|
2217 {
|
|
2218 Lisp_Object *tortoise;
|
|
2219 Lisp_Object *hare;
|
|
2220
|
|
2221 start_over:
|
|
2222 tortoise = &plist;
|
|
2223 hare = &plist;
|
|
2224 while (!NILP (*tortoise))
|
|
2225 {
|
|
2226 Lisp_Object retval;
|
|
2227
|
|
2228 /* See above */
|
|
2229 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
|
|
2230 &retval))
|
|
2231 goto start_over;
|
|
2232 }
|
|
2233
|
|
2234 return Qnil;
|
|
2235 }
|
|
2236
|
20
|
2237 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
|
0
|
2238 Given a plist, return non-nil if its format is correct.
|
|
2239 If it returns nil, `check-valid-plist' will signal an error when given
|
|
2240 the plist; that means it's a malformed or circular plist or has non-symbols
|
|
2241 as keywords.
|
20
|
2242 */
|
|
2243 (plist))
|
0
|
2244 {
|
|
2245 Lisp_Object *tortoise;
|
|
2246 Lisp_Object *hare;
|
|
2247
|
|
2248 tortoise = &plist;
|
|
2249 hare = &plist;
|
|
2250 while (!NILP (*tortoise))
|
|
2251 {
|
|
2252 Lisp_Object retval;
|
|
2253
|
|
2254 /* See above */
|
|
2255 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
|
|
2256 &retval))
|
|
2257 return Qnil;
|
|
2258 }
|
|
2259
|
|
2260 return Qt;
|
|
2261 }
|
|
2262
|
20
|
2263 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
|
0
|
2264 Destructively remove any duplicate entries from a plist.
|
|
2265 In such cases, the first entry applies.
|
|
2266
|
|
2267 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
2268 a nil value is removed. This feature is a virus that has infected
|
16
|
2269 old Lisp implementations, but should not be used except for backward
|
|
2270 compatibility.
|
0
|
2271
|
|
2272 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
|
|
2273 return value may not be EQ to the passed-in value, so make sure to
|
|
2274 `setq' the value back into where it came from.
|
20
|
2275 */
|
|
2276 (plist, nil_means_not_present))
|
0
|
2277 {
|
|
2278 Lisp_Object head = plist;
|
|
2279
|
|
2280 Fcheck_valid_plist (plist);
|
|
2281
|
|
2282 while (!NILP (plist))
|
|
2283 {
|
|
2284 Lisp_Object prop = Fcar (plist);
|
|
2285 Lisp_Object next = Fcdr (plist);
|
|
2286
|
|
2287 CHECK_CONS (next); /* just make doubly sure we catch any errors */
|
|
2288 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
|
|
2289 {
|
|
2290 if (EQ (head, plist))
|
|
2291 head = Fcdr (next);
|
|
2292 plist = Fcdr (next);
|
|
2293 continue;
|
|
2294 }
|
|
2295 /* external_remprop returns 1 if it removed any property.
|
|
2296 We have to loop till it didn't remove anything, in case
|
|
2297 the property occurs many times. */
|
|
2298 while (external_remprop (&XCDR (next), prop, 0, ERROR_ME));
|
|
2299 plist = Fcdr (next);
|
|
2300 }
|
|
2301
|
|
2302 return head;
|
|
2303 }
|
|
2304
|
20
|
2305 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
|
0
|
2306 Extract a value from a lax property list.
|
|
2307
|
|
2308 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
|
|
2309 VALUE1 PROP2 VALUE2...), where comparions between properties is done
|
|
2310 using `equal' instead of `eq'. This function returns the value
|
|
2311 corresponding to the given PROP, or DEFAULT if PROP is not one of the
|
|
2312 properties on the list.
|
20
|
2313 */
|
|
2314 (lax_plist, prop, defalt)) /* Cant spel in C */
|
0
|
2315 {
|
|
2316 Lisp_Object val = external_plist_get (&lax_plist, prop, 1, ERROR_ME);
|
|
2317 if (UNBOUNDP (val))
|
|
2318 return defalt;
|
|
2319 return val;
|
|
2320 }
|
|
2321
|
20
|
2322 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
|
0
|
2323 Change value in LAX-PLIST of PROP to VAL.
|
|
2324 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
|
|
2325 VALUE1 PROP2 VALUE2...), where comparions between properties is done
|
|
2326 using `equal' instead of `eq'. PROP is usually a symbol and VAL is
|
|
2327 any object. If PROP is already a property on the list, its value is
|
|
2328 set to VAL, otherwise the new PROP VAL pair is added. The new plist
|
|
2329 is returned; use `(setq x (lax-plist-put x prop val))' to be sure to
|
|
2330 use the new value. The LAX-PLIST is modified by side effects.
|
20
|
2331 */
|
|
2332 (lax_plist, prop, val))
|
0
|
2333 {
|
|
2334 external_plist_put (&lax_plist, prop, val, 1, ERROR_ME);
|
|
2335 return lax_plist;
|
|
2336 }
|
|
2337
|
20
|
2338 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
|
0
|
2339 Remove from LAX-PLIST the property PROP and its value.
|
|
2340 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
|
|
2341 VALUE1 PROP2 VALUE2...), where comparions between properties is done
|
|
2342 using `equal' instead of `eq'. PROP is usually a symbol. The new
|
|
2343 plist is returned; use `(setq x (lax-plist-remprop x prop val))' to be
|
|
2344 sure to use the new value. The LAX-PLIST is modified by side effects.
|
20
|
2345 */
|
|
2346 (lax_plist, prop))
|
0
|
2347 {
|
|
2348 external_remprop (&lax_plist, prop, 1, ERROR_ME);
|
|
2349 return lax_plist;
|
|
2350 }
|
|
2351
|
20
|
2352 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
|
0
|
2353 Return t if PROP has a value specified in LAX-PLIST.
|
|
2354 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
|
|
2355 VALUE1 PROP2 VALUE2...), where comparions between properties is done
|
|
2356 using `equal' instead of `eq'.
|
20
|
2357 */
|
|
2358 (lax_plist, prop))
|
0
|
2359 {
|
|
2360 return UNBOUNDP (Flax_plist_get (lax_plist, prop, Qunbound)) ? Qnil : Qt;
|
|
2361 }
|
|
2362
|
20
|
2363 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
|
0
|
2364 Destructively remove any duplicate entries from a lax plist.
|
|
2365 In such cases, the first entry applies.
|
|
2366
|
|
2367 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
2368 a nil value is removed. This feature is a virus that has infected
|
16
|
2369 old Lisp implementations, but should not be used except for backward
|
|
2370 compatibility.
|
0
|
2371
|
|
2372 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
|
|
2373 return value may not be EQ to the passed-in value, so make sure to
|
|
2374 `setq' the value back into where it came from.
|
20
|
2375 */
|
|
2376 (lax_plist, nil_means_not_present))
|
0
|
2377 {
|
|
2378 Lisp_Object head = lax_plist;
|
|
2379
|
|
2380 Fcheck_valid_plist (lax_plist);
|
|
2381
|
|
2382 while (!NILP (lax_plist))
|
|
2383 {
|
|
2384 Lisp_Object prop = Fcar (lax_plist);
|
|
2385 Lisp_Object next = Fcdr (lax_plist);
|
|
2386
|
|
2387 CHECK_CONS (next); /* just make doubly sure we catch any errors */
|
|
2388 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
|
|
2389 {
|
|
2390 if (EQ (head, lax_plist))
|
|
2391 head = Fcdr (next);
|
|
2392 lax_plist = Fcdr (next);
|
|
2393 continue;
|
|
2394 }
|
|
2395 /* external_remprop returns 1 if it removed any property.
|
|
2396 We have to loop till it didn't remove anything, in case
|
|
2397 the property occurs many times. */
|
|
2398 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME));
|
|
2399 lax_plist = Fcdr (next);
|
|
2400 }
|
|
2401
|
|
2402 return head;
|
|
2403 }
|
|
2404
|
|
2405 /* In C because the frame props stuff uses it */
|
|
2406
|
20
|
2407 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
|
0
|
2408 Convert association list ALIST into the equivalent property-list form.
|
|
2409 The plist is returned. This converts from
|
|
2410
|
|
2411 \((a . 1) (b . 2) (c . 3))
|
|
2412
|
|
2413 into
|
|
2414
|
|
2415 \(a 1 b 2 c 3)
|
|
2416
|
|
2417 The original alist is destroyed in the process of constructing the plist.
|
|
2418 See also `alist-to-plist'.
|
20
|
2419 */
|
|
2420 (alist))
|
0
|
2421 {
|
|
2422 Lisp_Object head = alist;
|
|
2423 while (!NILP (alist))
|
|
2424 {
|
|
2425 /* remember the alist element. */
|
|
2426 Lisp_Object el = Fcar (alist);
|
|
2427
|
|
2428 Fsetcar (alist, Fcar (el));
|
|
2429 Fsetcar (el, Fcdr (el));
|
|
2430 Fsetcdr (el, Fcdr (alist));
|
|
2431 Fsetcdr (alist, el);
|
|
2432 alist = Fcdr (Fcdr (alist));
|
|
2433 }
|
|
2434
|
|
2435 return head;
|
|
2436 }
|
|
2437
|
|
2438 /* Symbol plists are directly accessible, so we need to protect against
|
|
2439 invalid property list structure */
|
|
2440
|
|
2441 static Lisp_Object
|
|
2442 symbol_getprop (Lisp_Object sym, Lisp_Object propname, Lisp_Object defalt)
|
|
2443 {
|
|
2444 Lisp_Object val = external_plist_get (&XSYMBOL (sym)->plist, propname,
|
|
2445 0, ERROR_ME);
|
|
2446 if (UNBOUNDP (val))
|
|
2447 return defalt;
|
|
2448 return val;
|
|
2449 }
|
|
2450
|
|
2451 static void
|
|
2452 symbol_putprop (Lisp_Object sym, Lisp_Object propname, Lisp_Object value)
|
|
2453 {
|
|
2454 external_plist_put (&XSYMBOL (sym)->plist, propname, value, 0, ERROR_ME);
|
|
2455 }
|
|
2456
|
|
2457 static int
|
|
2458 symbol_remprop (Lisp_Object symbol, Lisp_Object propname)
|
|
2459 {
|
|
2460 return external_remprop (&XSYMBOL (symbol)->plist, propname, 0, ERROR_ME);
|
|
2461 }
|
|
2462
|
|
2463 /* We store the string's extent info as the first element of the string's
|
|
2464 property list; and the string's MODIFF as the first or second element
|
|
2465 of the string's property list (depending on whether the extent info
|
|
2466 is present), but only if the string has been modified. This is ugly
|
|
2467 but it reduces the memory allocated for the string in the vast
|
|
2468 majority of cases, where the string is never modified and has no
|
|
2469 extent info. */
|
|
2470
|
|
2471
|
|
2472 static Lisp_Object *
|
|
2473 string_plist_ptr (struct Lisp_String *s)
|
|
2474 {
|
|
2475 Lisp_Object *ptr = &s->plist;
|
|
2476
|
|
2477 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
|
|
2478 ptr = &XCDR (*ptr);
|
|
2479 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
|
|
2480 ptr = &XCDR (*ptr);
|
|
2481 return ptr;
|
|
2482 }
|
|
2483
|
|
2484 Lisp_Object
|
|
2485 string_getprop (struct Lisp_String *s, Lisp_Object property,
|
|
2486 Lisp_Object defalt)
|
|
2487 {
|
|
2488 Lisp_Object val = external_plist_get (string_plist_ptr (s), property, 0,
|
|
2489 ERROR_ME);
|
|
2490 if (UNBOUNDP (val))
|
|
2491 return defalt;
|
|
2492 return val;
|
|
2493 }
|
|
2494
|
|
2495 void
|
|
2496 string_putprop (struct Lisp_String *s, Lisp_Object property,
|
|
2497 Lisp_Object value)
|
|
2498 {
|
|
2499 external_plist_put (string_plist_ptr (s), property, value, 0, ERROR_ME);
|
|
2500 }
|
|
2501
|
|
2502 static int
|
|
2503 string_remprop (struct Lisp_String *s, Lisp_Object property)
|
|
2504 {
|
|
2505 return external_remprop (string_plist_ptr (s), property, 0, ERROR_ME);
|
|
2506 }
|
|
2507
|
|
2508 static Lisp_Object
|
|
2509 string_plist (struct Lisp_String *s)
|
|
2510 {
|
|
2511 return *string_plist_ptr (s);
|
|
2512 }
|
|
2513
|
20
|
2514 DEFUN ("get", Fget, 2, 3, 0, /*
|
0
|
2515 Return the value of OBJECT's PROPNAME property.
|
|
2516 This is the last VALUE stored with `(put OBJECT PROPNAME VALUE)'.
|
|
2517 If there is no such property, return optional third arg DEFAULT
|
|
2518 (which defaults to `nil'). OBJECT can be a symbol, face, extent,
|
|
2519 or string. See also `put', `remprop', and `object-plist'.
|
20
|
2520 */
|
|
2521 (object, propname, defalt)) /* Cant spel in C */
|
0
|
2522 {
|
|
2523 Lisp_Object val;
|
|
2524
|
|
2525 /* Various places in emacs call Fget() and expect it not to quit,
|
|
2526 so don't quit. */
|
|
2527
|
|
2528 /* It's easiest to treat symbols specially because they may not
|
|
2529 be an lrecord */
|
|
2530 if (SYMBOLP (object))
|
|
2531 val = symbol_getprop (object, propname, defalt);
|
|
2532 else if (STRINGP (object))
|
|
2533 val = string_getprop (XSTRING (object), propname, defalt);
|
|
2534 else if (LRECORDP (object))
|
|
2535 {
|
|
2536 CONST struct lrecord_implementation
|
|
2537 *imp = XRECORD_LHEADER (object)->implementation;
|
|
2538 if (imp->getprop)
|
|
2539 {
|
|
2540 val = (imp->getprop) (object, propname);
|
|
2541 if (UNBOUNDP (val))
|
|
2542 val = defalt;
|
|
2543 }
|
|
2544 else
|
|
2545 goto noprops;
|
|
2546 }
|
|
2547 else
|
|
2548 {
|
|
2549 noprops:
|
|
2550 signal_simple_error ("Object type has no properties", object);
|
|
2551 }
|
|
2552
|
|
2553 return val;
|
|
2554 }
|
|
2555
|
20
|
2556 DEFUN ("put", Fput, 3, 3, 0, /*
|
0
|
2557 Store OBJECT's PROPNAME property with value VALUE.
|
|
2558 It can be retrieved with `(get OBJECT PROPNAME)'. OBJECT can be a
|
|
2559 symbol, face, extent, or string.
|
|
2560
|
|
2561 For a string, no properties currently have predefined meanings.
|
|
2562 For the predefined properties for extents, see `set-extent-property'.
|
|
2563 For the predefined properties for faces, see `set-face-property'.
|
|
2564
|
|
2565 See also `get', `remprop', and `object-plist'.
|
20
|
2566 */
|
|
2567 (object, propname, value))
|
0
|
2568 {
|
|
2569 CHECK_SYMBOL (propname);
|
|
2570 CHECK_IMPURE (object);
|
|
2571
|
|
2572 if (SYMBOLP (object))
|
|
2573 symbol_putprop (object, propname, value);
|
|
2574 else if (STRINGP (object))
|
|
2575 string_putprop (XSTRING (object), propname, value);
|
|
2576 else if (LRECORDP (object))
|
|
2577 {
|
|
2578 CONST struct lrecord_implementation
|
|
2579 *imp = XRECORD_LHEADER (object)->implementation;
|
|
2580 if (imp->putprop)
|
|
2581 {
|
|
2582 if (! (imp->putprop) (object, propname, value))
|
|
2583 signal_simple_error ("Can't set property on object", propname);
|
|
2584 }
|
|
2585 else
|
|
2586 goto noprops;
|
|
2587 }
|
|
2588 else
|
|
2589 {
|
|
2590 noprops:
|
|
2591 signal_simple_error ("Object type has no settable properties", object);
|
|
2592 }
|
|
2593
|
|
2594 return value;
|
|
2595 }
|
|
2596
|
|
2597 void
|
|
2598 pure_put (Lisp_Object sym, Lisp_Object prop, Lisp_Object val)
|
|
2599 {
|
|
2600 Fput (sym, prop, Fpurecopy (val));
|
|
2601 }
|
|
2602
|
20
|
2603 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
|
0
|
2604 Remove from OBJECT's property list the property PROPNAME and its
|
|
2605 value. OBJECT can be a symbol, face, extent, or string. Returns
|
|
2606 non-nil if the property list was actually changed (i.e. if PROPNAME
|
|
2607 was present in the property list). See also `get', `put', and
|
|
2608 `object-plist'.
|
20
|
2609 */
|
|
2610 (object, propname))
|
0
|
2611 {
|
|
2612 int retval = 0;
|
|
2613
|
|
2614 CHECK_SYMBOL (propname);
|
|
2615 CHECK_IMPURE (object);
|
|
2616
|
|
2617 if (SYMBOLP (object))
|
|
2618 retval = symbol_remprop (object, propname);
|
|
2619 else if (STRINGP (object))
|
|
2620 retval = string_remprop (XSTRING (object), propname);
|
|
2621 else if (LRECORDP (object))
|
|
2622 {
|
|
2623 CONST struct lrecord_implementation
|
|
2624 *imp = XRECORD_LHEADER (object)->implementation;
|
|
2625 if (imp->remprop)
|
|
2626 {
|
|
2627 retval = (imp->remprop) (object, propname);
|
|
2628 if (retval == -1)
|
|
2629 signal_simple_error ("Can't remove property from object",
|
|
2630 propname);
|
|
2631 }
|
|
2632 else
|
|
2633 goto noprops;
|
|
2634 }
|
|
2635 else
|
|
2636 {
|
|
2637 noprops:
|
|
2638 signal_simple_error ("Object type has no removable properties", object);
|
|
2639 }
|
|
2640
|
|
2641 return retval ? Qt : Qnil;
|
|
2642 }
|
|
2643
|
20
|
2644 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
|
0
|
2645 Return a property list of OBJECT's props.
|
|
2646 For a symbol this is equivalent to `symbol-plist'.
|
|
2647 Do not modify the property list directly; this may or may not have
|
|
2648 the desired effects. (In particular, for a property with a special
|
|
2649 interpretation, this will probably have no effect at all.)
|
20
|
2650 */
|
|
2651 (object))
|
0
|
2652 {
|
|
2653 if (SYMBOLP (object))
|
|
2654 return Fsymbol_plist (object);
|
|
2655 else if (STRINGP (object))
|
|
2656 return string_plist (XSTRING (object));
|
|
2657 else if (LRECORDP (object))
|
|
2658 {
|
|
2659 CONST struct lrecord_implementation
|
|
2660 *imp = XRECORD_LHEADER (object)->implementation;
|
|
2661 if (imp->plist)
|
|
2662 return (imp->plist) (object);
|
|
2663 else
|
|
2664 signal_simple_error ("Object type has no properties", object);
|
|
2665 }
|
|
2666 else
|
|
2667 signal_simple_error ("Object type has no properties", object);
|
|
2668
|
|
2669 return Qnil;
|
|
2670 }
|
|
2671
|
|
2672
|
|
2673 int
|
|
2674 internal_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
2675 {
|
|
2676 if (depth > 200)
|
|
2677 error ("Stack overflow in equal");
|
|
2678 do_cdr:
|
|
2679 QUIT;
|
|
2680 if (HACKEQ_UNSAFE (o1, o2))
|
|
2681 return (1);
|
|
2682 /* Note that (equal 20 20.0) should be nil */
|
|
2683 else if (XTYPE (o1) != XTYPE (o2))
|
|
2684 return (0);
|
|
2685 else if (CONSP (o1))
|
|
2686 {
|
|
2687 if (!internal_equal (Fcar (o1), Fcar (o2), depth + 1))
|
|
2688 return (0);
|
|
2689 o1 = Fcdr (o1);
|
|
2690 o2 = Fcdr (o2);
|
|
2691 goto do_cdr;
|
|
2692 }
|
|
2693
|
|
2694 #ifndef LRECORD_VECTOR
|
|
2695 else if (VECTORP (o1))
|
|
2696 {
|
|
2697 int indecks;
|
|
2698 int len = vector_length (XVECTOR (o1));
|
|
2699 if (len != vector_length (XVECTOR (o2)))
|
|
2700 return (0);
|
|
2701 for (indecks = 0; indecks < len; indecks++)
|
|
2702 {
|
|
2703 Lisp_Object v1, v2;
|
|
2704 v1 = vector_data (XVECTOR (o1)) [indecks];
|
|
2705 v2 = vector_data (XVECTOR (o2)) [indecks];
|
|
2706 if (!internal_equal (v1, v2, depth + 1))
|
|
2707 return (0);
|
|
2708 }
|
|
2709 return (1);
|
|
2710 }
|
|
2711 #endif /* !LRECORD_VECTOR */
|
|
2712 else if (STRINGP (o1))
|
|
2713 {
|
14
|
2714 Bytecount len = XSTRING_LENGTH (o1);
|
|
2715 if (len != XSTRING_LENGTH (o2))
|
0
|
2716 return (0);
|
14
|
2717 if (memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len))
|
0
|
2718 return (0);
|
|
2719 return (1);
|
|
2720 }
|
|
2721 else if (LRECORDP (o1))
|
|
2722 {
|
|
2723 CONST struct lrecord_implementation
|
|
2724 *imp1 = XRECORD_LHEADER (o1)->implementation,
|
|
2725 *imp2 = XRECORD_LHEADER (o2)->implementation;
|
|
2726 if (imp1 != imp2)
|
|
2727 return (0);
|
|
2728 else if (imp1->equal == 0)
|
|
2729 /* EQ-ness of the objects was noticed above */
|
|
2730 return (0);
|
|
2731 else
|
|
2732 return ((imp1->equal) (o1, o2, depth));
|
|
2733 }
|
|
2734
|
|
2735 return (0);
|
|
2736 }
|
|
2737
|
20
|
2738 DEFUN ("equal", Fequal, 2, 2, 0, /*
|
0
|
2739 T if two Lisp objects have similar structure and contents.
|
|
2740 They must have the same data type.
|
|
2741 Conses are compared by comparing the cars and the cdrs.
|
|
2742 Vectors and strings are compared element by element.
|
|
2743 Numbers are compared by value. Symbols must match exactly.
|
20
|
2744 */
|
|
2745 (o1, o2))
|
0
|
2746 {
|
|
2747 return ((internal_equal (o1, o2, 0)) ? Qt : Qnil);
|
|
2748 }
|
|
2749
|
|
2750
|
20
|
2751 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
|
0
|
2752 Store each element of ARRAY with ITEM.
|
|
2753 ARRAY is a vector, bit vector, or string.
|
20
|
2754 */
|
|
2755 (array, item))
|
0
|
2756 {
|
|
2757 retry:
|
10
|
2758 if (STRINGP (array))
|
|
2759 {
|
|
2760 Charcount size;
|
|
2761 Charcount i;
|
|
2762 Emchar charval;
|
|
2763 struct Lisp_String *s;
|
|
2764 CHECK_CHAR_COERCE_INT (item);
|
|
2765 CHECK_IMPURE (array);
|
|
2766 charval = XCHAR (item);
|
|
2767 s = XSTRING (array);
|
|
2768 size = string_char_length (s);
|
|
2769 for (i = 0; i < size; i++)
|
|
2770 set_string_char (s, i, charval);
|
|
2771 bump_string_modiff (array);
|
|
2772 }
|
|
2773 else if (VECTORP (array))
|
0
|
2774 {
|
|
2775 Lisp_Object *p;
|
|
2776 int size;
|
10
|
2777 int i;
|
0
|
2778 CHECK_IMPURE (array);
|
|
2779 size = vector_length (XVECTOR (array));
|
|
2780 p = vector_data (XVECTOR (array));
|
10
|
2781 for (i = 0; i < size; i++)
|
|
2782 p[i] = item;
|
0
|
2783 }
|
10
|
2784 else if (BIT_VECTORP (array))
|
0
|
2785 {
|
|
2786 struct Lisp_Bit_Vector *v;
|
|
2787 int size;
|
10
|
2788 int i;
|
0
|
2789 CHECK_BIT (item);
|
|
2790 CHECK_IMPURE (array);
|
|
2791 v = XBIT_VECTOR (array);
|
|
2792 size = bit_vector_length (v);
|
10
|
2793 for (i = 0; i < size; i++)
|
|
2794 set_bit_vector_bit (v, i, XINT (item));
|
0
|
2795 }
|
|
2796 else
|
|
2797 {
|
|
2798 array = wrong_type_argument (Qarrayp, array);
|
|
2799 goto retry;
|
|
2800 }
|
|
2801 return array;
|
|
2802 }
|
|
2803
|
|
2804 Lisp_Object
|
|
2805 nconc2 (Lisp_Object s1, Lisp_Object s2)
|
|
2806 {
|
|
2807 Lisp_Object args[2];
|
|
2808 args[0] = s1;
|
|
2809 args[1] = s2;
|
|
2810 return Fnconc (2, args);
|
|
2811 }
|
|
2812
|
20
|
2813 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
|
0
|
2814 Concatenate any number of lists by altering them.
|
|
2815 Only the last argument is not altered, and need not be a list.
|
20
|
2816 */
|
|
2817 (int nargs, Lisp_Object *args))
|
0
|
2818 {
|
|
2819 int argnum;
|
|
2820 Lisp_Object tail, tem, val;
|
|
2821 struct gcpro gcpro1;
|
|
2822
|
|
2823 /* The modus operandi in Emacs is "caller gc-protects args".
|
|
2824 However, nconc (particularly nconc2 ()) is called many times
|
|
2825 in Emacs on freshly created stuff (e.g. you see the idiom
|
|
2826 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
|
|
2827 callers out by protecting the args ourselves to save them
|
|
2828 a lot of temporary-variable grief. */
|
|
2829
|
|
2830 GCPRO1 (args[0]);
|
|
2831 gcpro1.nvars = nargs;
|
|
2832
|
|
2833 val = Qnil;
|
|
2834
|
|
2835 for (argnum = 0; argnum < nargs; argnum++)
|
|
2836 {
|
|
2837 tem = args[argnum];
|
|
2838 if (NILP (tem)) continue;
|
|
2839
|
|
2840 if (NILP (val))
|
|
2841 val = tem;
|
|
2842
|
|
2843 if (argnum + 1 == nargs) break;
|
|
2844
|
|
2845 if (!CONSP (tem))
|
|
2846 tem = wrong_type_argument (Qlistp, tem);
|
|
2847
|
|
2848 while (CONSP (tem))
|
|
2849 {
|
|
2850 tail = tem;
|
|
2851 tem = Fcdr (tail);
|
|
2852 QUIT;
|
|
2853 }
|
|
2854
|
|
2855 tem = args[argnum + 1];
|
|
2856 Fsetcdr (tail, tem);
|
|
2857 if (NILP (tem))
|
|
2858 args[argnum + 1] = tail;
|
|
2859 }
|
|
2860
|
|
2861 RETURN_UNGCPRO (val);
|
|
2862 }
|
|
2863
|
|
2864
|
|
2865 /* This is the guts of all mapping functions.
|
|
2866 Apply fn to each element of seq, one by one,
|
|
2867 storing the results into elements of vals, a C vector of Lisp_Objects.
|
|
2868 leni is the length of vals, which should also be the length of seq.
|
|
2869
|
|
2870 If VALS is a null pointer, do not accumulate the results. */
|
|
2871
|
|
2872 static void
|
|
2873 mapcar1 (int leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
|
|
2874 {
|
|
2875 Lisp_Object tail;
|
|
2876 Lisp_Object dummy = Qnil;
|
|
2877 int i;
|
|
2878 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
2879 Lisp_Object result;
|
|
2880
|
|
2881 GCPRO3 (dummy, fn, seq);
|
|
2882
|
|
2883 if (vals)
|
|
2884 {
|
|
2885 /* Don't let vals contain any garbage when GC happens. */
|
|
2886 for (i = 0; i < leni; i++)
|
|
2887 vals[i] = Qnil;
|
|
2888 gcpro1.var = vals;
|
|
2889 gcpro1.nvars = leni;
|
|
2890 }
|
|
2891
|
|
2892 /* We need not explicitly protect `tail' because it is used only on
|
|
2893 lists, and 1) lists are not relocated and 2) the list is marked
|
|
2894 via `seq' so will not be freed */
|
|
2895
|
|
2896 if (VECTORP (seq))
|
|
2897 {
|
|
2898 for (i = 0; i < leni; i++)
|
|
2899 {
|
|
2900 dummy = vector_data (XVECTOR (seq))[i];
|
|
2901 result = call1 (fn, dummy);
|
|
2902 if (vals)
|
|
2903 vals[i] = result;
|
|
2904 }
|
|
2905 }
|
|
2906 else if (BIT_VECTORP (seq))
|
|
2907 {
|
|
2908 struct Lisp_Bit_Vector *v = XBIT_VECTOR (seq);
|
|
2909 for (i = 0; i < leni; i++)
|
|
2910 {
|
|
2911 XSETINT (dummy, bit_vector_bit (v, i));
|
|
2912 result = call1 (fn, dummy);
|
|
2913 if (vals)
|
|
2914 vals[i] = result;
|
|
2915 }
|
|
2916 }
|
|
2917 else if (STRINGP (seq))
|
|
2918 {
|
|
2919 for (i = 0; i < leni; i++)
|
|
2920 {
|
|
2921 result = call1 (fn, make_char (string_char (XSTRING (seq), i)));
|
|
2922 if (vals)
|
|
2923 vals[i] = result;
|
|
2924 }
|
|
2925 }
|
|
2926 else /* Must be a list, since Flength did not get an error */
|
|
2927 {
|
|
2928 tail = seq;
|
|
2929 for (i = 0; i < leni; i++)
|
|
2930 {
|
|
2931 result = call1 (fn, Fcar (tail));
|
|
2932 if (vals)
|
|
2933 vals[i] = result;
|
|
2934 tail = Fcdr (tail);
|
|
2935 }
|
|
2936 }
|
|
2937
|
|
2938 UNGCPRO;
|
|
2939 }
|
|
2940
|
20
|
2941 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
|
0
|
2942 Apply FN to each element of SEQ, and concat the results as strings.
|
|
2943 In between each pair of results, stick in SEP.
|
|
2944 Thus, \" \" as SEP results in spaces between the values returned by FN.
|
20
|
2945 */
|
|
2946 (fn, seq, sep))
|
0
|
2947 {
|
16
|
2948 int len = XINT (Flength (seq));
|
0
|
2949 int nargs;
|
|
2950 Lisp_Object *args;
|
|
2951 int i;
|
|
2952 struct gcpro gcpro1;
|
|
2953
|
16
|
2954 nargs = len + len - 1;
|
0
|
2955 if (nargs < 0) return build_string ("");
|
|
2956
|
|
2957 args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object));
|
|
2958
|
|
2959 GCPRO1 (sep);
|
16
|
2960 mapcar1 (len, args, fn, seq);
|
0
|
2961 UNGCPRO;
|
|
2962
|
16
|
2963 for (i = len - 1; i >= 0; i--)
|
0
|
2964 args[i + i] = args[i];
|
|
2965
|
|
2966 for (i = 1; i < nargs; i += 2)
|
|
2967 args[i] = sep;
|
|
2968
|
|
2969 return Fconcat (nargs, args);
|
|
2970 }
|
|
2971
|
20
|
2972 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
|
0
|
2973 Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
|
|
2974 The result is a list just as long as SEQUENCE.
|
|
2975 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
20
|
2976 */
|
|
2977 (fn, seq))
|
0
|
2978 {
|
16
|
2979 int len = XINT (Flength (seq));
|
|
2980 Lisp_Object *args = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
|
|
2981
|
|
2982 mapcar1 (len, args, fn, seq);
|
|
2983
|
|
2984 return Flist (len, args);
|
0
|
2985 }
|
|
2986
|
20
|
2987 DEFUN ("mapc-internal", Fmapc_internal, 2, 2, 0, /*
|
0
|
2988 Apply FUNCTION to each element of SEQUENCE.
|
|
2989 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
|
2990 This function is like `mapcar' but does not accumulate the results,
|
|
2991 which is more efficient if you do not use the results.
|
20
|
2992 */
|
|
2993 (fn, seq))
|
0
|
2994 {
|
16
|
2995 mapcar1 (XINT (Flength (seq)), 0, fn, seq);
|
0
|
2996
|
|
2997 return Qnil;
|
|
2998 }
|
|
2999
|
|
3000
|
|
3001 /* #### this function doesn't belong in this file! */
|
|
3002
|
20
|
3003 DEFUN ("load-average", Fload_average, 0, 0, 0, /*
|
0
|
3004 Return list of 1 minute, 5 minute and 15 minute load averages.
|
|
3005 Each of the three load averages is multiplied by 100,
|
|
3006 then converted to integer.
|
|
3007
|
|
3008 If the 5-minute or 15-minute load averages are not available, return a
|
|
3009 shortened list, containing only those averages which are available.
|
|
3010
|
|
3011 On most systems, this won't work unless the emacs executable is installed
|
|
3012 as setgid kmem (assuming that /dev/kmem is in the group kmem).
|
20
|
3013 */
|
|
3014 ())
|
0
|
3015 {
|
|
3016 double load_ave[10]; /* hey, just in case */
|
|
3017 int loads = getloadavg (load_ave, 3);
|
|
3018 Lisp_Object ret;
|
|
3019
|
|
3020 if (loads == -2)
|
|
3021 error ("load-average not implemented for this operating system.");
|
|
3022 else if (loads < 0)
|
|
3023 error ("could not get load-average; check permissions.");
|
|
3024
|
|
3025 ret = Qnil;
|
|
3026 while (loads > 0)
|
|
3027 ret = Fcons (make_int ((int) (load_ave[--loads] * 100.0)), ret);
|
|
3028
|
|
3029 return ret;
|
|
3030 }
|
|
3031
|
|
3032
|
|
3033 Lisp_Object Vfeatures;
|
|
3034
|
20
|
3035 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
|
0
|
3036 Return t if FEATURE is present in this Emacs.
|
2
|
3037 Use this to conditionalize execution of lisp code based on the
|
|
3038 presence or absence of emacs or environment extensions.
|
0
|
3039 Use `provide' to declare that a feature is available.
|
|
3040 This function looks at the value of the variable `features'.
|
20
|
3041 */
|
|
3042 (feature))
|
0
|
3043 {
|
|
3044 CHECK_SYMBOL (feature);
|
16
|
3045 return NILP (Fmemq (feature, Vfeatures)) ? Qnil : Qt;
|
0
|
3046 }
|
|
3047
|
20
|
3048 DEFUN ("provide", Fprovide, 1, 1, 0, /*
|
0
|
3049 Announce that FEATURE is a feature of the current Emacs.
|
2
|
3050 This function updates the value of the variable `features'.
|
20
|
3051 */
|
|
3052 (feature))
|
0
|
3053 {
|
|
3054 Lisp_Object tem;
|
|
3055 CHECK_SYMBOL (feature);
|
|
3056 if (!NILP (Vautoload_queue))
|
|
3057 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
|
|
3058 tem = Fmemq (feature, Vfeatures);
|
|
3059 if (NILP (tem))
|
|
3060 Vfeatures = Fcons (feature, Vfeatures);
|
|
3061 LOADHIST_ATTACH (Fcons (Qprovide, feature));
|
|
3062 return feature;
|
|
3063 }
|
|
3064
|
20
|
3065 DEFUN ("require", Frequire, 1, 2, 0, /*
|
0
|
3066 If feature FEATURE is not loaded, load it from FILENAME.
|
|
3067 If FEATURE is not a member of the list `features', then the feature
|
|
3068 is not loaded; so load the file FILENAME.
|
|
3069 If FILENAME is omitted, the printname of FEATURE is used as the file name.
|
20
|
3070 */
|
|
3071 (feature, file_name))
|
0
|
3072 {
|
|
3073 Lisp_Object tem;
|
|
3074 CHECK_SYMBOL (feature);
|
|
3075 tem = Fmemq (feature, Vfeatures);
|
|
3076 LOADHIST_ATTACH (Fcons (Qrequire, feature));
|
|
3077 if (!NILP (tem))
|
|
3078 return (feature);
|
|
3079 else
|
|
3080 {
|
|
3081 int speccount = specpdl_depth ();
|
|
3082
|
|
3083 /* Value saved here is to be restored into Vautoload_queue */
|
|
3084 record_unwind_protect (un_autoload, Vautoload_queue);
|
|
3085 Vautoload_queue = Qt;
|
|
3086
|
|
3087 call4 (Qload, NILP (file_name) ? Fsymbol_name (feature) : file_name,
|
|
3088 Qnil, Qt, Qnil);
|
|
3089
|
|
3090 tem = Fmemq (feature, Vfeatures);
|
|
3091 if (NILP (tem))
|
|
3092 error ("Required feature %s was not provided",
|
|
3093 string_data (XSYMBOL (feature)->name));
|
|
3094
|
|
3095 /* Once loading finishes, don't undo it. */
|
|
3096 Vautoload_queue = Qt;
|
|
3097 return (unbind_to (speccount, feature));
|
|
3098 }
|
|
3099 }
|
|
3100
|
|
3101
|
|
3102 Lisp_Object Qyes_or_no_p;
|
|
3103
|
|
3104 void
|
|
3105 syms_of_fns (void)
|
|
3106 {
|
|
3107 defsymbol (&Qstring_lessp, "string-lessp");
|
|
3108 defsymbol (&Qidentity, "identity");
|
|
3109 defsymbol (&Qyes_or_no_p, "yes-or-no-p");
|
|
3110
|
20
|
3111 DEFSUBR (Fidentity);
|
|
3112 DEFSUBR (Frandom);
|
|
3113 DEFSUBR (Flength);
|
|
3114 DEFSUBR (Fsafe_length);
|
|
3115 DEFSUBR (Fstring_equal);
|
|
3116 DEFSUBR (Fstring_lessp);
|
|
3117 DEFSUBR (Fstring_modified_tick);
|
|
3118 DEFSUBR (Fappend);
|
|
3119 DEFSUBR (Fconcat);
|
|
3120 DEFSUBR (Fvconcat);
|
|
3121 DEFSUBR (Fbvconcat);
|
|
3122 DEFSUBR (Fcopy_sequence);
|
|
3123 DEFSUBR (Fcopy_alist);
|
|
3124 DEFSUBR (Fcopy_tree);
|
|
3125 DEFSUBR (Fsubstring);
|
|
3126 DEFSUBR (Fsubseq);
|
|
3127 DEFSUBR (Fnthcdr);
|
|
3128 DEFSUBR (Fnth);
|
|
3129 DEFSUBR (Felt);
|
|
3130 DEFSUBR (Fmember);
|
|
3131 DEFSUBR (Fmemq);
|
|
3132 DEFSUBR (Fassoc);
|
|
3133 DEFSUBR (Fassq);
|
|
3134 DEFSUBR (Frassoc);
|
|
3135 DEFSUBR (Frassq);
|
|
3136 DEFSUBR (Fdelete);
|
|
3137 DEFSUBR (Fdelq);
|
|
3138 DEFSUBR (Fremassoc);
|
|
3139 DEFSUBR (Fremassq);
|
|
3140 DEFSUBR (Fremrassoc);
|
|
3141 DEFSUBR (Fremrassq);
|
|
3142 DEFSUBR (Fnreverse);
|
|
3143 DEFSUBR (Freverse);
|
|
3144 DEFSUBR (Fsort);
|
|
3145 DEFSUBR (Fplists_eq);
|
|
3146 DEFSUBR (Fplists_equal);
|
|
3147 DEFSUBR (Flax_plists_eq);
|
|
3148 DEFSUBR (Flax_plists_equal);
|
|
3149 DEFSUBR (Fplist_get);
|
|
3150 DEFSUBR (Fplist_put);
|
|
3151 DEFSUBR (Fplist_remprop);
|
|
3152 DEFSUBR (Fplist_member);
|
|
3153 DEFSUBR (Fcheck_valid_plist);
|
|
3154 DEFSUBR (Fvalid_plist_p);
|
|
3155 DEFSUBR (Fcanonicalize_plist);
|
|
3156 DEFSUBR (Flax_plist_get);
|
|
3157 DEFSUBR (Flax_plist_put);
|
|
3158 DEFSUBR (Flax_plist_remprop);
|
|
3159 DEFSUBR (Flax_plist_member);
|
|
3160 DEFSUBR (Fcanonicalize_lax_plist);
|
|
3161 DEFSUBR (Fdestructive_alist_to_plist);
|
|
3162 DEFSUBR (Fget);
|
|
3163 DEFSUBR (Fput);
|
|
3164 DEFSUBR (Fremprop);
|
|
3165 DEFSUBR (Fobject_plist);
|
|
3166 DEFSUBR (Fequal);
|
|
3167 DEFSUBR (Ffillarray);
|
|
3168 DEFSUBR (Fnconc);
|
|
3169 DEFSUBR (Fmapcar);
|
|
3170 DEFSUBR (Fmapc_internal);
|
|
3171 DEFSUBR (Fmapconcat);
|
|
3172 DEFSUBR (Fload_average);
|
|
3173 DEFSUBR (Ffeaturep);
|
|
3174 DEFSUBR (Frequire);
|
|
3175 DEFSUBR (Fprovide);
|
0
|
3176 }
|
|
3177
|
|
3178 void
|
|
3179 init_provide_once (void)
|
|
3180 {
|
|
3181 DEFVAR_LISP ("features", &Vfeatures /*
|
|
3182 A list of symbols which are the features of the executing emacs.
|
|
3183 Used by `featurep' and `require', and altered by `provide'.
|
|
3184 */ );
|
|
3185 Vfeatures = Qnil;
|
|
3186 }
|