428
|
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
|
442
|
39 #include "sysfile.h"
|
428
|
40
|
|
41 #include "buffer.h"
|
|
42 #include "bytecode.h"
|
|
43 #include "device.h"
|
|
44 #include "events.h"
|
|
45 #include "extents.h"
|
|
46 #include "frame.h"
|
|
47 #include "systime.h"
|
|
48 #include "insdel.h"
|
|
49 #include "lstream.h"
|
|
50 #include "opaque.h"
|
|
51
|
|
52 /* NOTE: This symbol is also used in lread.c */
|
|
53 #define FEATUREP_SYNTAX
|
|
54
|
|
55 Lisp_Object Qstring_lessp;
|
|
56 Lisp_Object Qidentity;
|
|
57
|
563
|
58 Lisp_Object Qbase64_conversion_error;
|
|
59
|
428
|
60 static int internal_old_equal (Lisp_Object, Lisp_Object, int);
|
454
|
61 Lisp_Object safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth);
|
428
|
62
|
|
63 static Lisp_Object
|
|
64 mark_bit_vector (Lisp_Object obj)
|
|
65 {
|
|
66 return Qnil;
|
|
67 }
|
|
68
|
|
69 static void
|
|
70 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
71 {
|
|
72 size_t i;
|
440
|
73 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
|
428
|
74 size_t len = bit_vector_length (v);
|
|
75 size_t last = len;
|
|
76
|
|
77 if (INTP (Vprint_length))
|
|
78 last = min (len, XINT (Vprint_length));
|
|
79 write_c_string ("#*", printcharfun);
|
|
80 for (i = 0; i < last; i++)
|
|
81 {
|
|
82 if (bit_vector_bit (v, i))
|
|
83 write_c_string ("1", printcharfun);
|
|
84 else
|
|
85 write_c_string ("0", printcharfun);
|
|
86 }
|
|
87
|
|
88 if (last != len)
|
|
89 write_c_string ("...", printcharfun);
|
|
90 }
|
|
91
|
|
92 static int
|
|
93 bit_vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
94 {
|
440
|
95 Lisp_Bit_Vector *v1 = XBIT_VECTOR (obj1);
|
|
96 Lisp_Bit_Vector *v2 = XBIT_VECTOR (obj2);
|
428
|
97
|
|
98 return ((bit_vector_length (v1) == bit_vector_length (v2)) &&
|
|
99 !memcmp (v1->bits, v2->bits,
|
|
100 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
|
|
101 sizeof (long)));
|
|
102 }
|
|
103
|
|
104 static unsigned long
|
|
105 bit_vector_hash (Lisp_Object obj, int depth)
|
|
106 {
|
440
|
107 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
|
428
|
108 return HASH2 (bit_vector_length (v),
|
|
109 memory_hash (v->bits,
|
|
110 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
|
|
111 sizeof (long)));
|
|
112 }
|
|
113
|
442
|
114 static size_t
|
|
115 size_bit_vector (const void *lheader)
|
|
116 {
|
|
117 Lisp_Bit_Vector *v = (Lisp_Bit_Vector *) lheader;
|
456
|
118 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long, bits,
|
442
|
119 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)));
|
|
120 }
|
|
121
|
428
|
122 static const struct lrecord_description bit_vector_description[] = {
|
440
|
123 { XD_LISP_OBJECT, offsetof (Lisp_Bit_Vector, next) },
|
428
|
124 { XD_END }
|
|
125 };
|
|
126
|
|
127
|
442
|
128 DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION ("bit-vector", bit_vector,
|
|
129 mark_bit_vector, print_bit_vector, 0,
|
|
130 bit_vector_equal, bit_vector_hash,
|
|
131 bit_vector_description, size_bit_vector,
|
|
132 Lisp_Bit_Vector);
|
428
|
133
|
|
134 DEFUN ("identity", Fidentity, 1, 1, 0, /*
|
|
135 Return the argument unchanged.
|
|
136 */
|
|
137 (arg))
|
|
138 {
|
|
139 return arg;
|
|
140 }
|
|
141
|
|
142 extern long get_random (void);
|
|
143 extern void seed_random (long arg);
|
|
144
|
|
145 DEFUN ("random", Frandom, 0, 1, 0, /*
|
|
146 Return a pseudo-random number.
|
|
147 All integers representable in Lisp are equally likely.
|
|
148 On most systems, this is 28 bits' worth.
|
|
149 With positive integer argument N, return random number in interval [0,N).
|
|
150 With argument t, set the random number seed from the current time and pid.
|
|
151 */
|
|
152 (limit))
|
|
153 {
|
|
154 EMACS_INT val;
|
|
155 unsigned long denominator;
|
|
156
|
|
157 if (EQ (limit, Qt))
|
|
158 seed_random (getpid () + time (NULL));
|
|
159 if (NATNUMP (limit) && !ZEROP (limit))
|
|
160 {
|
|
161 /* Try to take our random number from the higher bits of VAL,
|
|
162 not the lower, since (says Gentzel) the low bits of `random'
|
|
163 are less random than the higher ones. We do this by using the
|
|
164 quotient rather than the remainder. At the high end of the RNG
|
|
165 it's possible to get a quotient larger than limit; discarding
|
|
166 these values eliminates the bias that would otherwise appear
|
|
167 when using a large limit. */
|
|
168 denominator = ((unsigned long)1 << VALBITS) / XINT (limit);
|
|
169 do
|
|
170 val = get_random () / denominator;
|
|
171 while (val >= XINT (limit));
|
|
172 }
|
|
173 else
|
|
174 val = get_random ();
|
|
175
|
|
176 return make_int (val);
|
|
177 }
|
|
178
|
|
179 /* Random data-structure functions */
|
|
180
|
|
181 #ifdef LOSING_BYTECODE
|
|
182
|
|
183 /* #### Delete this shit */
|
|
184
|
|
185 /* Charcount is a misnomer here as we might be dealing with the
|
|
186 length of a vector or list, but emphasizes that we're not dealing
|
|
187 with Bytecounts in strings */
|
|
188 static Charcount
|
|
189 length_with_bytecode_hack (Lisp_Object seq)
|
|
190 {
|
|
191 if (!COMPILED_FUNCTIONP (seq))
|
|
192 return XINT (Flength (seq));
|
|
193 else
|
|
194 {
|
440
|
195 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (seq);
|
428
|
196
|
|
197 return (f->flags.interactivep ? COMPILED_INTERACTIVE :
|
|
198 f->flags.domainp ? COMPILED_DOMAIN :
|
|
199 COMPILED_DOC_STRING)
|
|
200 + 1;
|
|
201 }
|
|
202 }
|
|
203
|
|
204 #endif /* LOSING_BYTECODE */
|
|
205
|
|
206 void
|
442
|
207 check_losing_bytecode (const char *function, Lisp_Object seq)
|
428
|
208 {
|
|
209 if (COMPILED_FUNCTIONP (seq))
|
563
|
210 signal_ferror_with_frob
|
|
211 (Qinvalid_argument, seq,
|
428
|
212 "As of 20.3, `%s' no longer works with compiled-function objects",
|
|
213 function);
|
|
214 }
|
|
215
|
|
216 DEFUN ("length", Flength, 1, 1, 0, /*
|
|
217 Return the length of vector, bit vector, list or string SEQUENCE.
|
|
218 */
|
|
219 (sequence))
|
|
220 {
|
|
221 retry:
|
|
222 if (STRINGP (sequence))
|
|
223 return make_int (XSTRING_CHAR_LENGTH (sequence));
|
|
224 else if (CONSP (sequence))
|
|
225 {
|
|
226 size_t len;
|
|
227 GET_EXTERNAL_LIST_LENGTH (sequence, len);
|
|
228 return make_int (len);
|
|
229 }
|
|
230 else if (VECTORP (sequence))
|
|
231 return make_int (XVECTOR_LENGTH (sequence));
|
|
232 else if (NILP (sequence))
|
|
233 return Qzero;
|
|
234 else if (BIT_VECTORP (sequence))
|
|
235 return make_int (bit_vector_length (XBIT_VECTOR (sequence)));
|
|
236 else
|
|
237 {
|
|
238 check_losing_bytecode ("length", sequence);
|
|
239 sequence = wrong_type_argument (Qsequencep, sequence);
|
|
240 goto retry;
|
|
241 }
|
|
242 }
|
|
243
|
|
244 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
|
|
245 Return the length of a list, but avoid error or infinite loop.
|
|
246 This function never gets an error. If LIST is not really a list,
|
|
247 it returns 0. If LIST is circular, it returns a finite value
|
|
248 which is at least the number of distinct elements.
|
|
249 */
|
|
250 (list))
|
|
251 {
|
|
252 Lisp_Object hare, tortoise;
|
|
253 size_t len;
|
|
254
|
|
255 for (hare = tortoise = list, len = 0;
|
|
256 CONSP (hare) && (! EQ (hare, tortoise) || len == 0);
|
|
257 hare = XCDR (hare), len++)
|
|
258 {
|
|
259 if (len & 1)
|
|
260 tortoise = XCDR (tortoise);
|
|
261 }
|
|
262
|
|
263 return make_int (len);
|
|
264 }
|
|
265
|
|
266 /*** string functions. ***/
|
|
267
|
|
268 DEFUN ("string-equal", Fstring_equal, 2, 2, 0, /*
|
|
269 Return 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 19. In FSF Emacs 20
|
|
273 `equal' is the same as in XEmacs, in that respect.)
|
|
274 Symbols are also allowed; their print names are used instead.
|
|
275 */
|
444
|
276 (string1, string2))
|
428
|
277 {
|
|
278 Bytecount len;
|
440
|
279 Lisp_String *p1, *p2;
|
428
|
280
|
444
|
281 if (SYMBOLP (string1))
|
|
282 p1 = XSYMBOL (string1)->name;
|
428
|
283 else
|
|
284 {
|
444
|
285 CHECK_STRING (string1);
|
|
286 p1 = XSTRING (string1);
|
428
|
287 }
|
|
288
|
444
|
289 if (SYMBOLP (string2))
|
|
290 p2 = XSYMBOL (string2)->name;
|
428
|
291 else
|
|
292 {
|
444
|
293 CHECK_STRING (string2);
|
|
294 p2 = XSTRING (string2);
|
428
|
295 }
|
|
296
|
|
297 return (((len = string_length (p1)) == string_length (p2)) &&
|
|
298 !memcmp (string_data (p1), string_data (p2), len)) ? Qt : Qnil;
|
|
299 }
|
|
300
|
|
301
|
|
302 DEFUN ("string-lessp", Fstring_lessp, 2, 2, 0, /*
|
|
303 Return t if first arg string is less than second in lexicographic order.
|
|
304 If I18N2 support (but not Mule support) was compiled in, ordering is
|
|
305 determined by the locale. (Case is significant for the default C locale.)
|
|
306 In all other cases, comparison is simply done on a character-by-
|
|
307 character basis using the numeric value of a character. (Note that
|
|
308 this may not produce particularly meaningful results under Mule if
|
|
309 characters from different charsets are being compared.)
|
|
310
|
|
311 Symbols are also allowed; their print names are used instead.
|
|
312
|
|
313 The reason that the I18N2 locale-specific collation is not used under
|
|
314 Mule is that the locale model of internationalization does not handle
|
|
315 multiple charsets and thus has no hope of working properly under Mule.
|
|
316 What we really should do is create a collation table over all built-in
|
|
317 charsets. This is extremely difficult to do from scratch, however.
|
|
318
|
|
319 Unicode is a good first step towards solving this problem. In fact,
|
|
320 it is quite likely that a collation table exists (or will exist) for
|
|
321 Unicode. When Unicode support is added to XEmacs/Mule, this problem
|
|
322 may be solved.
|
|
323 */
|
444
|
324 (string1, string2))
|
428
|
325 {
|
440
|
326 Lisp_String *p1, *p2;
|
428
|
327 Charcount end, len2;
|
|
328 int i;
|
|
329
|
444
|
330 if (SYMBOLP (string1))
|
|
331 p1 = XSYMBOL (string1)->name;
|
428
|
332 else
|
|
333 {
|
444
|
334 CHECK_STRING (string1);
|
|
335 p1 = XSTRING (string1);
|
428
|
336 }
|
|
337
|
444
|
338 if (SYMBOLP (string2))
|
|
339 p2 = XSYMBOL (string2)->name;
|
428
|
340 else
|
|
341 {
|
444
|
342 CHECK_STRING (string2);
|
|
343 p2 = XSTRING (string2);
|
428
|
344 }
|
|
345
|
|
346 end = string_char_length (p1);
|
|
347 len2 = string_char_length (p2);
|
|
348 if (end > len2)
|
|
349 end = len2;
|
|
350
|
|
351 #if defined (I18N2) && !defined (MULE)
|
|
352 /* There is no hope of this working under Mule. Even if we converted
|
|
353 the data into an external format so that strcoll() processed it
|
|
354 properly, it would still not work because strcoll() does not
|
|
355 handle multiple locales. This is the fundamental flaw in the
|
|
356 locale model. */
|
|
357 {
|
|
358 Bytecount bcend = charcount_to_bytecount (string_data (p1), end);
|
|
359 /* Compare strings using collation order of locale. */
|
|
360 /* Need to be tricky to handle embedded nulls. */
|
|
361
|
|
362 for (i = 0; i < bcend; i += strlen((char *) string_data (p1) + i) + 1)
|
|
363 {
|
|
364 int val = strcoll ((char *) string_data (p1) + i,
|
|
365 (char *) string_data (p2) + i);
|
|
366 if (val < 0)
|
|
367 return Qt;
|
|
368 if (val > 0)
|
|
369 return Qnil;
|
|
370 }
|
|
371 }
|
|
372 #else /* not I18N2, or MULE */
|
|
373 {
|
|
374 Bufbyte *ptr1 = string_data (p1);
|
|
375 Bufbyte *ptr2 = string_data (p2);
|
|
376
|
|
377 /* #### It is not really necessary to do this: We could compare
|
|
378 byte-by-byte and still get a reasonable comparison, since this
|
|
379 would compare characters with a charset in the same way. With
|
|
380 a little rearrangement of the leading bytes, we could make most
|
|
381 inter-charset comparisons work out the same, too; even if some
|
|
382 don't, this is not a big deal because inter-charset comparisons
|
|
383 aren't really well-defined anyway. */
|
|
384 for (i = 0; i < end; i++)
|
|
385 {
|
|
386 if (charptr_emchar (ptr1) != charptr_emchar (ptr2))
|
|
387 return charptr_emchar (ptr1) < charptr_emchar (ptr2) ? Qt : Qnil;
|
|
388 INC_CHARPTR (ptr1);
|
|
389 INC_CHARPTR (ptr2);
|
|
390 }
|
|
391 }
|
|
392 #endif /* not I18N2, or MULE */
|
|
393 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
|
|
394 won't work right in I18N2 case */
|
|
395 return end < len2 ? Qt : Qnil;
|
|
396 }
|
|
397
|
|
398 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
|
|
399 Return STRING's tick counter, incremented for each change to the string.
|
|
400 Each string has a tick counter which is incremented each time the contents
|
|
401 of the string are changed (e.g. with `aset'). It wraps around occasionally.
|
|
402 */
|
|
403 (string))
|
|
404 {
|
440
|
405 Lisp_String *s;
|
428
|
406
|
|
407 CHECK_STRING (string);
|
|
408 s = XSTRING (string);
|
|
409 if (CONSP (s->plist) && INTP (XCAR (s->plist)))
|
|
410 return XCAR (s->plist);
|
|
411 else
|
|
412 return Qzero;
|
|
413 }
|
|
414
|
|
415 void
|
|
416 bump_string_modiff (Lisp_Object str)
|
|
417 {
|
440
|
418 Lisp_String *s = XSTRING (str);
|
428
|
419 Lisp_Object *ptr = &s->plist;
|
|
420
|
|
421 #ifdef I18N3
|
|
422 /* #### remove the `string-translatable' property from the string,
|
|
423 if there is one. */
|
|
424 #endif
|
|
425 /* skip over extent info if it's there */
|
|
426 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
|
|
427 ptr = &XCDR (*ptr);
|
|
428 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
|
|
429 XSETINT (XCAR (*ptr), 1+XINT (XCAR (*ptr)));
|
|
430 else
|
|
431 *ptr = Fcons (make_int (1), *ptr);
|
|
432 }
|
|
433
|
|
434
|
|
435 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
|
|
436 static Lisp_Object concat (int nargs, Lisp_Object *args,
|
|
437 enum concat_target_type target_type,
|
|
438 int last_special);
|
|
439
|
|
440 Lisp_Object
|
444
|
441 concat2 (Lisp_Object string1, Lisp_Object string2)
|
428
|
442 {
|
|
443 Lisp_Object args[2];
|
444
|
444 args[0] = string1;
|
|
445 args[1] = string2;
|
428
|
446 return concat (2, args, c_string, 0);
|
|
447 }
|
|
448
|
|
449 Lisp_Object
|
444
|
450 concat3 (Lisp_Object string1, Lisp_Object string2, Lisp_Object string3)
|
428
|
451 {
|
|
452 Lisp_Object args[3];
|
444
|
453 args[0] = string1;
|
|
454 args[1] = string2;
|
|
455 args[2] = string3;
|
428
|
456 return concat (3, args, c_string, 0);
|
|
457 }
|
|
458
|
|
459 Lisp_Object
|
444
|
460 vconcat2 (Lisp_Object vec1, Lisp_Object vec2)
|
428
|
461 {
|
|
462 Lisp_Object args[2];
|
444
|
463 args[0] = vec1;
|
|
464 args[1] = vec2;
|
428
|
465 return concat (2, args, c_vector, 0);
|
|
466 }
|
|
467
|
|
468 Lisp_Object
|
444
|
469 vconcat3 (Lisp_Object vec1, Lisp_Object vec2, Lisp_Object vec3)
|
428
|
470 {
|
|
471 Lisp_Object args[3];
|
444
|
472 args[0] = vec1;
|
|
473 args[1] = vec2;
|
|
474 args[2] = vec3;
|
428
|
475 return concat (3, args, c_vector, 0);
|
|
476 }
|
|
477
|
|
478 DEFUN ("append", Fappend, 0, MANY, 0, /*
|
|
479 Concatenate all the arguments and make the result a list.
|
|
480 The result is a list whose elements are the elements of all the arguments.
|
|
481 Each argument may be a list, vector, bit vector, or string.
|
|
482 The last argument is not copied, just used as the tail of the new list.
|
|
483 Also see: `nconc'.
|
|
484 */
|
|
485 (int nargs, Lisp_Object *args))
|
|
486 {
|
|
487 return concat (nargs, args, c_cons, 1);
|
|
488 }
|
|
489
|
|
490 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
|
|
491 Concatenate all the arguments and make the result a string.
|
|
492 The result is a string whose elements are the elements of all the arguments.
|
|
493 Each argument may be a string or a list or vector of characters.
|
|
494
|
|
495 As of XEmacs 21.0, this function does NOT accept individual integers
|
|
496 as arguments. Old code that relies on, for example, (concat "foo" 50)
|
|
497 returning "foo50" will fail. To fix such code, either apply
|
|
498 `int-to-string' to the integer argument, or use `format'.
|
|
499 */
|
|
500 (int nargs, Lisp_Object *args))
|
|
501 {
|
|
502 return concat (nargs, args, c_string, 0);
|
|
503 }
|
|
504
|
|
505 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
|
|
506 Concatenate all the arguments and make the result a vector.
|
|
507 The result is a vector whose elements are the elements of all the arguments.
|
|
508 Each argument may be a list, vector, bit vector, or string.
|
|
509 */
|
|
510 (int nargs, Lisp_Object *args))
|
|
511 {
|
|
512 return concat (nargs, args, c_vector, 0);
|
|
513 }
|
|
514
|
|
515 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
|
|
516 Concatenate all the arguments and make the result a bit vector.
|
|
517 The result is a bit vector whose elements are the elements of all the
|
|
518 arguments. Each argument may be a list, vector, bit vector, or string.
|
|
519 */
|
|
520 (int nargs, Lisp_Object *args))
|
|
521 {
|
|
522 return concat (nargs, args, c_bit_vector, 0);
|
|
523 }
|
|
524
|
|
525 /* Copy a (possibly dotted) list. LIST must be a cons.
|
|
526 Can't use concat (1, &alist, c_cons, 0) - doesn't handle dotted lists. */
|
|
527 static Lisp_Object
|
|
528 copy_list (Lisp_Object list)
|
|
529 {
|
|
530 Lisp_Object list_copy = Fcons (XCAR (list), XCDR (list));
|
|
531 Lisp_Object last = list_copy;
|
|
532 Lisp_Object hare, tortoise;
|
|
533 size_t len;
|
|
534
|
|
535 for (tortoise = hare = XCDR (list), len = 1;
|
|
536 CONSP (hare);
|
|
537 hare = XCDR (hare), len++)
|
|
538 {
|
|
539 XCDR (last) = Fcons (XCAR (hare), XCDR (hare));
|
|
540 last = XCDR (last);
|
|
541
|
|
542 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
|
|
543 continue;
|
|
544 if (len & 1)
|
|
545 tortoise = XCDR (tortoise);
|
|
546 if (EQ (tortoise, hare))
|
|
547 signal_circular_list_error (list);
|
|
548 }
|
|
549
|
|
550 return list_copy;
|
|
551 }
|
|
552
|
|
553 DEFUN ("copy-list", Fcopy_list, 1, 1, 0, /*
|
|
554 Return a copy of list LIST, which may be a dotted list.
|
|
555 The elements of LIST are not copied; they are shared
|
|
556 with the original.
|
|
557 */
|
|
558 (list))
|
|
559 {
|
|
560 again:
|
|
561 if (NILP (list)) return list;
|
|
562 if (CONSP (list)) return copy_list (list);
|
|
563
|
|
564 list = wrong_type_argument (Qlistp, list);
|
|
565 goto again;
|
|
566 }
|
|
567
|
|
568 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
|
|
569 Return a copy of list, vector, bit vector or string SEQUENCE.
|
|
570 The elements of a list or vector are not copied; they are shared
|
|
571 with the original. SEQUENCE may be a dotted list.
|
|
572 */
|
|
573 (sequence))
|
|
574 {
|
|
575 again:
|
|
576 if (NILP (sequence)) return sequence;
|
|
577 if (CONSP (sequence)) return copy_list (sequence);
|
|
578 if (STRINGP (sequence)) return concat (1, &sequence, c_string, 0);
|
|
579 if (VECTORP (sequence)) return concat (1, &sequence, c_vector, 0);
|
|
580 if (BIT_VECTORP (sequence)) return concat (1, &sequence, c_bit_vector, 0);
|
|
581
|
|
582 check_losing_bytecode ("copy-sequence", sequence);
|
|
583 sequence = wrong_type_argument (Qsequencep, sequence);
|
|
584 goto again;
|
|
585 }
|
|
586
|
|
587 struct merge_string_extents_struct
|
|
588 {
|
|
589 Lisp_Object string;
|
|
590 Bytecount entry_offset;
|
|
591 Bytecount entry_length;
|
|
592 };
|
|
593
|
|
594 static Lisp_Object
|
|
595 concat (int nargs, Lisp_Object *args,
|
|
596 enum concat_target_type target_type,
|
|
597 int last_special)
|
|
598 {
|
|
599 Lisp_Object val;
|
|
600 Lisp_Object tail = Qnil;
|
|
601 int toindex;
|
|
602 int argnum;
|
|
603 Lisp_Object last_tail;
|
|
604 Lisp_Object prev;
|
|
605 struct merge_string_extents_struct *args_mse = 0;
|
|
606 Bufbyte *string_result = 0;
|
|
607 Bufbyte *string_result_ptr = 0;
|
|
608 struct gcpro gcpro1;
|
|
609
|
|
610 /* The modus operandi in Emacs is "caller gc-protects args".
|
|
611 However, concat is called many times in Emacs on freshly
|
|
612 created stuff. So we help those callers out by protecting
|
|
613 the args ourselves to save them a lot of temporary-variable
|
|
614 grief. */
|
|
615
|
|
616 GCPRO1 (args[0]);
|
|
617 gcpro1.nvars = nargs;
|
|
618
|
|
619 #ifdef I18N3
|
|
620 /* #### if the result is a string and any of the strings have a string
|
|
621 for the `string-translatable' property, then concat should also
|
|
622 concat the args but use the `string-translatable' strings, and store
|
|
623 the result in the returned string's `string-translatable' property. */
|
|
624 #endif
|
|
625 if (target_type == c_string)
|
|
626 args_mse = alloca_array (struct merge_string_extents_struct, nargs);
|
|
627
|
|
628 /* In append, the last arg isn't treated like the others */
|
|
629 if (last_special && nargs > 0)
|
|
630 {
|
|
631 nargs--;
|
|
632 last_tail = args[nargs];
|
|
633 }
|
|
634 else
|
|
635 last_tail = Qnil;
|
|
636
|
|
637 /* Check and coerce the arguments. */
|
|
638 for (argnum = 0; argnum < nargs; argnum++)
|
|
639 {
|
|
640 Lisp_Object seq = args[argnum];
|
|
641 if (LISTP (seq))
|
|
642 ;
|
|
643 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
|
|
644 ;
|
|
645 #ifdef LOSING_BYTECODE
|
|
646 else if (COMPILED_FUNCTIONP (seq))
|
|
647 /* Urk! We allow this, for "compatibility"... */
|
|
648 ;
|
|
649 #endif
|
|
650 #if 0 /* removed for XEmacs 21 */
|
|
651 else if (INTP (seq))
|
|
652 /* This is too revolting to think about but maintains
|
|
653 compatibility with FSF (and lots and lots of old code). */
|
|
654 args[argnum] = Fnumber_to_string (seq);
|
|
655 #endif
|
|
656 else
|
|
657 {
|
|
658 check_losing_bytecode ("concat", seq);
|
|
659 args[argnum] = wrong_type_argument (Qsequencep, seq);
|
|
660 }
|
|
661
|
|
662 if (args_mse)
|
|
663 {
|
|
664 if (STRINGP (seq))
|
|
665 args_mse[argnum].string = seq;
|
|
666 else
|
|
667 args_mse[argnum].string = Qnil;
|
|
668 }
|
|
669 }
|
|
670
|
|
671 {
|
|
672 /* Charcount is a misnomer here as we might be dealing with the
|
|
673 length of a vector or list, but emphasizes that we're not dealing
|
|
674 with Bytecounts in strings */
|
|
675 Charcount total_length;
|
|
676
|
|
677 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
|
|
678 {
|
|
679 #ifdef LOSING_BYTECODE
|
|
680 Charcount thislen = length_with_bytecode_hack (args[argnum]);
|
|
681 #else
|
|
682 Charcount thislen = XINT (Flength (args[argnum]));
|
|
683 #endif
|
|
684 total_length += thislen;
|
|
685 }
|
|
686
|
|
687 switch (target_type)
|
|
688 {
|
|
689 case c_cons:
|
|
690 if (total_length == 0)
|
|
691 /* In append, if all but last arg are nil, return last arg */
|
|
692 RETURN_UNGCPRO (last_tail);
|
|
693 val = Fmake_list (make_int (total_length), Qnil);
|
|
694 break;
|
|
695 case c_vector:
|
|
696 val = make_vector (total_length, Qnil);
|
|
697 break;
|
|
698 case c_bit_vector:
|
|
699 val = make_bit_vector (total_length, Qzero);
|
|
700 break;
|
|
701 case c_string:
|
|
702 /* We don't make the string yet because we don't know the
|
|
703 actual number of bytes. This loop was formerly written
|
|
704 to call Fmake_string() here and then call set_string_char()
|
|
705 for each char. This seems logical enough but is waaaaaaaay
|
|
706 slow -- set_string_char() has to scan the whole string up
|
|
707 to the place where the substitution is called for in order
|
|
708 to find the place to change, and may have to do some
|
|
709 realloc()ing in order to make the char fit properly.
|
|
710 O(N^2) yuckage. */
|
|
711 val = Qnil;
|
|
712 string_result = (Bufbyte *) alloca (total_length * MAX_EMCHAR_LEN);
|
|
713 string_result_ptr = string_result;
|
|
714 break;
|
|
715 default:
|
442
|
716 val = Qnil;
|
428
|
717 abort ();
|
|
718 }
|
|
719 }
|
|
720
|
|
721
|
|
722 if (CONSP (val))
|
|
723 tail = val, toindex = -1; /* -1 in toindex is flag we are
|
|
724 making a list */
|
|
725 else
|
|
726 toindex = 0;
|
|
727
|
|
728 prev = Qnil;
|
|
729
|
|
730 for (argnum = 0; argnum < nargs; argnum++)
|
|
731 {
|
|
732 Charcount thisleni = 0;
|
|
733 Charcount thisindex = 0;
|
|
734 Lisp_Object seq = args[argnum];
|
|
735 Bufbyte *string_source_ptr = 0;
|
|
736 Bufbyte *string_prev_result_ptr = string_result_ptr;
|
|
737
|
|
738 if (!CONSP (seq))
|
|
739 {
|
|
740 #ifdef LOSING_BYTECODE
|
|
741 thisleni = length_with_bytecode_hack (seq);
|
|
742 #else
|
|
743 thisleni = XINT (Flength (seq));
|
|
744 #endif
|
|
745 }
|
|
746 if (STRINGP (seq))
|
|
747 string_source_ptr = XSTRING_DATA (seq);
|
|
748
|
|
749 while (1)
|
|
750 {
|
|
751 Lisp_Object elt;
|
|
752
|
|
753 /* We've come to the end of this arg, so exit. */
|
|
754 if (NILP (seq))
|
|
755 break;
|
|
756
|
|
757 /* Fetch next element of `seq' arg into `elt' */
|
|
758 if (CONSP (seq))
|
|
759 {
|
|
760 elt = XCAR (seq);
|
|
761 seq = XCDR (seq);
|
|
762 }
|
|
763 else
|
|
764 {
|
|
765 if (thisindex >= thisleni)
|
|
766 break;
|
|
767
|
|
768 if (STRINGP (seq))
|
|
769 {
|
|
770 elt = make_char (charptr_emchar (string_source_ptr));
|
|
771 INC_CHARPTR (string_source_ptr);
|
|
772 }
|
|
773 else if (VECTORP (seq))
|
|
774 elt = XVECTOR_DATA (seq)[thisindex];
|
|
775 else if (BIT_VECTORP (seq))
|
|
776 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
|
|
777 thisindex));
|
|
778 else
|
|
779 elt = Felt (seq, make_int (thisindex));
|
|
780 thisindex++;
|
|
781 }
|
|
782
|
|
783 /* Store into result */
|
|
784 if (toindex < 0)
|
|
785 {
|
|
786 /* toindex negative means we are making a list */
|
|
787 XCAR (tail) = elt;
|
|
788 prev = tail;
|
|
789 tail = XCDR (tail);
|
|
790 }
|
|
791 else if (VECTORP (val))
|
|
792 XVECTOR_DATA (val)[toindex++] = elt;
|
|
793 else if (BIT_VECTORP (val))
|
|
794 {
|
|
795 CHECK_BIT (elt);
|
|
796 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
|
|
797 }
|
|
798 else
|
|
799 {
|
|
800 CHECK_CHAR_COERCE_INT (elt);
|
|
801 string_result_ptr += set_charptr_emchar (string_result_ptr,
|
|
802 XCHAR (elt));
|
|
803 }
|
|
804 }
|
|
805 if (args_mse)
|
|
806 {
|
|
807 args_mse[argnum].entry_offset =
|
|
808 string_prev_result_ptr - string_result;
|
|
809 args_mse[argnum].entry_length =
|
|
810 string_result_ptr - string_prev_result_ptr;
|
|
811 }
|
|
812 }
|
|
813
|
|
814 /* Now we finally make the string. */
|
|
815 if (target_type == c_string)
|
|
816 {
|
|
817 val = make_string (string_result, string_result_ptr - string_result);
|
|
818 for (argnum = 0; argnum < nargs; argnum++)
|
|
819 {
|
|
820 if (STRINGP (args_mse[argnum].string))
|
|
821 copy_string_extents (val, args_mse[argnum].string,
|
|
822 args_mse[argnum].entry_offset, 0,
|
|
823 args_mse[argnum].entry_length);
|
|
824 }
|
|
825 }
|
|
826
|
|
827 if (!NILP (prev))
|
|
828 XCDR (prev) = last_tail;
|
|
829
|
|
830 RETURN_UNGCPRO (val);
|
|
831 }
|
|
832
|
|
833 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
|
|
834 Return a copy of ALIST.
|
|
835 This is an alist which represents the same mapping from objects to objects,
|
|
836 but does not share the alist structure with ALIST.
|
|
837 The objects mapped (cars and cdrs of elements of the alist)
|
|
838 are shared, however.
|
|
839 Elements of ALIST that are not conses are also shared.
|
|
840 */
|
|
841 (alist))
|
|
842 {
|
|
843 Lisp_Object tail;
|
|
844
|
|
845 if (NILP (alist))
|
|
846 return alist;
|
|
847 CHECK_CONS (alist);
|
|
848
|
|
849 alist = concat (1, &alist, c_cons, 0);
|
|
850 for (tail = alist; CONSP (tail); tail = XCDR (tail))
|
|
851 {
|
|
852 Lisp_Object car = XCAR (tail);
|
|
853
|
|
854 if (CONSP (car))
|
|
855 XCAR (tail) = Fcons (XCAR (car), XCDR (car));
|
|
856 }
|
|
857 return alist;
|
|
858 }
|
|
859
|
|
860 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
|
|
861 Return a copy of a list and substructures.
|
|
862 The argument is copied, and any lists contained within it are copied
|
|
863 recursively. Circularities and shared substructures are not preserved.
|
|
864 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
|
|
865 are not copied.
|
|
866 */
|
|
867 (arg, vecp))
|
|
868 {
|
454
|
869 return safe_copy_tree (arg, vecp, 0);
|
|
870 }
|
|
871
|
|
872 Lisp_Object
|
|
873 safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth)
|
|
874 {
|
|
875 if (depth > 200)
|
563
|
876 stack_overflow ("Stack overflow in copy-tree", arg);
|
454
|
877
|
428
|
878 if (CONSP (arg))
|
|
879 {
|
|
880 Lisp_Object rest;
|
|
881 rest = arg = Fcopy_sequence (arg);
|
|
882 while (CONSP (rest))
|
|
883 {
|
|
884 Lisp_Object elt = XCAR (rest);
|
|
885 QUIT;
|
|
886 if (CONSP (elt) || VECTORP (elt))
|
454
|
887 XCAR (rest) = safe_copy_tree (elt, vecp, depth + 1);
|
428
|
888 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
|
454
|
889 XCDR (rest) = safe_copy_tree (XCDR (rest), vecp, depth +1);
|
428
|
890 rest = XCDR (rest);
|
|
891 }
|
|
892 }
|
|
893 else if (VECTORP (arg) && ! NILP (vecp))
|
|
894 {
|
|
895 int i = XVECTOR_LENGTH (arg);
|
|
896 int j;
|
|
897 arg = Fcopy_sequence (arg);
|
|
898 for (j = 0; j < i; j++)
|
|
899 {
|
|
900 Lisp_Object elt = XVECTOR_DATA (arg) [j];
|
|
901 QUIT;
|
|
902 if (CONSP (elt) || VECTORP (elt))
|
454
|
903 XVECTOR_DATA (arg) [j] = safe_copy_tree (elt, vecp, depth + 1);
|
428
|
904 }
|
|
905 }
|
|
906 return arg;
|
|
907 }
|
|
908
|
|
909 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
|
444
|
910 Return the substring of STRING starting at START and ending before END.
|
|
911 END may be nil or omitted; then the substring runs to the end of STRING.
|
|
912 If START or END is negative, it counts from the end.
|
|
913 Relevant parts of the string-extent-data are copied to the new string.
|
428
|
914 */
|
444
|
915 (string, start, end))
|
428
|
916 {
|
444
|
917 Charcount ccstart, ccend;
|
|
918 Bytecount bstart, blen;
|
428
|
919 Lisp_Object val;
|
|
920
|
|
921 CHECK_STRING (string);
|
444
|
922 CHECK_INT (start);
|
|
923 get_string_range_char (string, start, end, &ccstart, &ccend,
|
428
|
924 GB_HISTORICAL_STRING_BEHAVIOR);
|
444
|
925 bstart = charcount_to_bytecount (XSTRING_DATA (string), ccstart);
|
|
926 blen = charcount_to_bytecount (XSTRING_DATA (string) + bstart, ccend - ccstart);
|
|
927 val = make_string (XSTRING_DATA (string) + bstart, blen);
|
|
928 /* Copy any applicable extent information into the new string. */
|
|
929 copy_string_extents (val, string, 0, bstart, blen);
|
428
|
930 return val;
|
|
931 }
|
|
932
|
|
933 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
|
442
|
934 Return the subsequence of SEQUENCE starting at START and ending before END.
|
|
935 END may be omitted; then the subsequence runs to the end of SEQUENCE.
|
|
936 If START or END is negative, it counts from the end.
|
|
937 The returned subsequence is always of the same type as SEQUENCE.
|
|
938 If SEQUENCE is a string, relevant parts of the string-extent-data
|
|
939 are copied to the new string.
|
428
|
940 */
|
442
|
941 (sequence, start, end))
|
428
|
942 {
|
442
|
943 EMACS_INT len, s, e;
|
|
944
|
|
945 if (STRINGP (sequence))
|
|
946 return Fsubstring (sequence, start, end);
|
|
947
|
|
948 len = XINT (Flength (sequence));
|
|
949
|
|
950 CHECK_INT (start);
|
|
951 s = XINT (start);
|
|
952 if (s < 0)
|
|
953 s = len + s;
|
|
954
|
|
955 if (NILP (end))
|
|
956 e = len;
|
428
|
957 else
|
|
958 {
|
442
|
959 CHECK_INT (end);
|
|
960 e = XINT (end);
|
|
961 if (e < 0)
|
|
962 e = len + e;
|
428
|
963 }
|
|
964
|
442
|
965 if (!(0 <= s && s <= e && e <= len))
|
|
966 args_out_of_range_3 (sequence, make_int (s), make_int (e));
|
|
967
|
|
968 if (VECTORP (sequence))
|
428
|
969 {
|
442
|
970 Lisp_Object result = make_vector (e - s, Qnil);
|
428
|
971 EMACS_INT i;
|
442
|
972 Lisp_Object *in_elts = XVECTOR_DATA (sequence);
|
428
|
973 Lisp_Object *out_elts = XVECTOR_DATA (result);
|
|
974
|
442
|
975 for (i = s; i < e; i++)
|
|
976 out_elts[i - s] = in_elts[i];
|
428
|
977 return result;
|
|
978 }
|
442
|
979 else if (LISTP (sequence))
|
428
|
980 {
|
|
981 Lisp_Object result = Qnil;
|
|
982 EMACS_INT i;
|
|
983
|
442
|
984 sequence = Fnthcdr (make_int (s), sequence);
|
|
985
|
|
986 for (i = s; i < e; i++)
|
428
|
987 {
|
442
|
988 result = Fcons (Fcar (sequence), result);
|
|
989 sequence = Fcdr (sequence);
|
428
|
990 }
|
|
991
|
|
992 return Fnreverse (result);
|
|
993 }
|
442
|
994 else if (BIT_VECTORP (sequence))
|
|
995 {
|
|
996 Lisp_Object result = make_bit_vector (e - s, Qzero);
|
|
997 EMACS_INT i;
|
|
998
|
|
999 for (i = s; i < e; i++)
|
|
1000 set_bit_vector_bit (XBIT_VECTOR (result), i - s,
|
|
1001 bit_vector_bit (XBIT_VECTOR (sequence), i));
|
|
1002 return result;
|
|
1003 }
|
|
1004 else
|
|
1005 {
|
|
1006 abort (); /* unreachable, since Flength (sequence) did not get
|
|
1007 an error */
|
|
1008 return Qnil;
|
|
1009 }
|
428
|
1010 }
|
|
1011
|
|
1012
|
|
1013 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
|
|
1014 Take cdr N times on LIST, and return the result.
|
|
1015 */
|
|
1016 (n, list))
|
|
1017 {
|
|
1018 REGISTER size_t i;
|
|
1019 REGISTER Lisp_Object tail = list;
|
|
1020 CHECK_NATNUM (n);
|
|
1021 for (i = XINT (n); i; i--)
|
|
1022 {
|
|
1023 if (CONSP (tail))
|
|
1024 tail = XCDR (tail);
|
|
1025 else if (NILP (tail))
|
|
1026 return Qnil;
|
|
1027 else
|
|
1028 {
|
|
1029 tail = wrong_type_argument (Qlistp, tail);
|
|
1030 i++;
|
|
1031 }
|
|
1032 }
|
|
1033 return tail;
|
|
1034 }
|
|
1035
|
|
1036 DEFUN ("nth", Fnth, 2, 2, 0, /*
|
|
1037 Return the Nth element of LIST.
|
|
1038 N counts from zero. If LIST is not that long, nil is returned.
|
|
1039 */
|
|
1040 (n, list))
|
|
1041 {
|
|
1042 return Fcar (Fnthcdr (n, list));
|
|
1043 }
|
|
1044
|
|
1045 DEFUN ("elt", Felt, 2, 2, 0, /*
|
|
1046 Return element of SEQUENCE at index N.
|
|
1047 */
|
|
1048 (sequence, n))
|
|
1049 {
|
|
1050 retry:
|
|
1051 CHECK_INT_COERCE_CHAR (n); /* yuck! */
|
|
1052 if (LISTP (sequence))
|
|
1053 {
|
|
1054 Lisp_Object tem = Fnthcdr (n, sequence);
|
|
1055 /* #### Utterly, completely, fucking disgusting.
|
|
1056 * #### The whole point of "elt" is that it operates on
|
|
1057 * #### sequences, and does error- (bounds-) checking.
|
|
1058 */
|
|
1059 if (CONSP (tem))
|
|
1060 return XCAR (tem);
|
|
1061 else
|
|
1062 #if 1
|
|
1063 /* This is The Way It Has Always Been. */
|
|
1064 return Qnil;
|
|
1065 #else
|
|
1066 /* This is The Way Mly and Cltl2 say It Should Be. */
|
|
1067 args_out_of_range (sequence, n);
|
|
1068 #endif
|
|
1069 }
|
|
1070 else if (STRINGP (sequence) ||
|
|
1071 VECTORP (sequence) ||
|
|
1072 BIT_VECTORP (sequence))
|
|
1073 return Faref (sequence, n);
|
|
1074 #ifdef LOSING_BYTECODE
|
|
1075 else if (COMPILED_FUNCTIONP (sequence))
|
|
1076 {
|
|
1077 EMACS_INT idx = XINT (n);
|
|
1078 if (idx < 0)
|
|
1079 {
|
|
1080 lose:
|
|
1081 args_out_of_range (sequence, n);
|
|
1082 }
|
|
1083 /* Utter perversity */
|
|
1084 {
|
|
1085 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (sequence);
|
|
1086 switch (idx)
|
|
1087 {
|
|
1088 case COMPILED_ARGLIST:
|
|
1089 return compiled_function_arglist (f);
|
|
1090 case COMPILED_INSTRUCTIONS:
|
|
1091 return compiled_function_instructions (f);
|
|
1092 case COMPILED_CONSTANTS:
|
|
1093 return compiled_function_constants (f);
|
|
1094 case COMPILED_STACK_DEPTH:
|
|
1095 return compiled_function_stack_depth (f);
|
|
1096 case COMPILED_DOC_STRING:
|
|
1097 return compiled_function_documentation (f);
|
|
1098 case COMPILED_DOMAIN:
|
|
1099 return compiled_function_domain (f);
|
|
1100 case COMPILED_INTERACTIVE:
|
|
1101 if (f->flags.interactivep)
|
|
1102 return compiled_function_interactive (f);
|
|
1103 /* if we return nil, can't tell interactive with no args
|
|
1104 from noninteractive. */
|
|
1105 goto lose;
|
|
1106 default:
|
|
1107 goto lose;
|
|
1108 }
|
|
1109 }
|
|
1110 }
|
|
1111 #endif /* LOSING_BYTECODE */
|
|
1112 else
|
|
1113 {
|
|
1114 check_losing_bytecode ("elt", sequence);
|
|
1115 sequence = wrong_type_argument (Qsequencep, sequence);
|
|
1116 goto retry;
|
|
1117 }
|
|
1118 }
|
|
1119
|
|
1120 DEFUN ("last", Flast, 1, 2, 0, /*
|
|
1121 Return the tail of list LIST, of length N (default 1).
|
|
1122 LIST may be a dotted list, but not a circular list.
|
|
1123 Optional argument N must be a non-negative integer.
|
|
1124 If N is zero, then the atom that terminates the list is returned.
|
|
1125 If N is greater than the length of LIST, then LIST itself is returned.
|
|
1126 */
|
|
1127 (list, n))
|
|
1128 {
|
|
1129 EMACS_INT int_n, count;
|
|
1130 Lisp_Object retval, tortoise, hare;
|
|
1131
|
|
1132 CHECK_LIST (list);
|
|
1133
|
|
1134 if (NILP (n))
|
|
1135 int_n = 1;
|
|
1136 else
|
|
1137 {
|
|
1138 CHECK_NATNUM (n);
|
|
1139 int_n = XINT (n);
|
|
1140 }
|
|
1141
|
|
1142 for (retval = tortoise = hare = list, count = 0;
|
|
1143 CONSP (hare);
|
|
1144 hare = XCDR (hare),
|
|
1145 (int_n-- <= 0 ? ((void) (retval = XCDR (retval))) : (void)0),
|
|
1146 count++)
|
|
1147 {
|
|
1148 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
|
|
1149
|
|
1150 if (count & 1)
|
|
1151 tortoise = XCDR (tortoise);
|
|
1152 if (EQ (hare, tortoise))
|
|
1153 signal_circular_list_error (list);
|
|
1154 }
|
|
1155
|
|
1156 return retval;
|
|
1157 }
|
|
1158
|
|
1159 DEFUN ("nbutlast", Fnbutlast, 1, 2, 0, /*
|
|
1160 Modify LIST to remove the last N (default 1) elements.
|
|
1161 If LIST has N or fewer elements, nil is returned and LIST is unmodified.
|
|
1162 */
|
|
1163 (list, n))
|
|
1164 {
|
|
1165 EMACS_INT int_n;
|
|
1166
|
|
1167 CHECK_LIST (list);
|
|
1168
|
|
1169 if (NILP (n))
|
|
1170 int_n = 1;
|
|
1171 else
|
|
1172 {
|
|
1173 CHECK_NATNUM (n);
|
|
1174 int_n = XINT (n);
|
|
1175 }
|
|
1176
|
|
1177 {
|
|
1178 Lisp_Object last_cons = list;
|
|
1179
|
|
1180 EXTERNAL_LIST_LOOP_1 (list)
|
|
1181 {
|
|
1182 if (int_n-- < 0)
|
|
1183 last_cons = XCDR (last_cons);
|
|
1184 }
|
|
1185
|
|
1186 if (int_n >= 0)
|
|
1187 return Qnil;
|
|
1188
|
|
1189 XCDR (last_cons) = Qnil;
|
|
1190 return list;
|
|
1191 }
|
|
1192 }
|
|
1193
|
|
1194 DEFUN ("butlast", Fbutlast, 1, 2, 0, /*
|
|
1195 Return a copy of LIST with the last N (default 1) elements removed.
|
|
1196 If LIST has N or fewer elements, nil is returned.
|
|
1197 */
|
|
1198 (list, n))
|
|
1199 {
|
444
|
1200 EMACS_INT int_n;
|
428
|
1201
|
|
1202 CHECK_LIST (list);
|
|
1203
|
|
1204 if (NILP (n))
|
|
1205 int_n = 1;
|
|
1206 else
|
|
1207 {
|
|
1208 CHECK_NATNUM (n);
|
|
1209 int_n = XINT (n);
|
|
1210 }
|
|
1211
|
|
1212 {
|
|
1213 Lisp_Object retval = Qnil;
|
|
1214 Lisp_Object tail = list;
|
|
1215
|
|
1216 EXTERNAL_LIST_LOOP_1 (list)
|
|
1217 {
|
|
1218 if (--int_n < 0)
|
|
1219 {
|
|
1220 retval = Fcons (XCAR (tail), retval);
|
|
1221 tail = XCDR (tail);
|
|
1222 }
|
|
1223 }
|
|
1224
|
|
1225 return Fnreverse (retval);
|
|
1226 }
|
|
1227 }
|
|
1228
|
|
1229 DEFUN ("member", Fmember, 2, 2, 0, /*
|
|
1230 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
|
|
1231 The value is actually the tail of LIST whose car is ELT.
|
|
1232 */
|
|
1233 (elt, list))
|
|
1234 {
|
|
1235 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
|
|
1236 {
|
|
1237 if (internal_equal (elt, list_elt, 0))
|
|
1238 return tail;
|
|
1239 }
|
|
1240 return Qnil;
|
|
1241 }
|
|
1242
|
|
1243 DEFUN ("old-member", Fold_member, 2, 2, 0, /*
|
|
1244 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
|
|
1245 The value is actually the tail of LIST whose car is ELT.
|
|
1246 This function is provided only for byte-code compatibility with v19.
|
|
1247 Do not use it.
|
|
1248 */
|
|
1249 (elt, list))
|
|
1250 {
|
|
1251 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
|
|
1252 {
|
|
1253 if (internal_old_equal (elt, list_elt, 0))
|
|
1254 return tail;
|
|
1255 }
|
|
1256 return Qnil;
|
|
1257 }
|
|
1258
|
|
1259 DEFUN ("memq", Fmemq, 2, 2, 0, /*
|
|
1260 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
|
|
1261 The value is actually the tail of LIST whose car is ELT.
|
|
1262 */
|
|
1263 (elt, list))
|
|
1264 {
|
|
1265 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
|
|
1266 {
|
|
1267 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
|
|
1268 return tail;
|
|
1269 }
|
|
1270 return Qnil;
|
|
1271 }
|
|
1272
|
|
1273 DEFUN ("old-memq", Fold_memq, 2, 2, 0, /*
|
|
1274 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
|
|
1275 The value is actually the tail of LIST whose car is ELT.
|
|
1276 This function is provided only for byte-code compatibility with v19.
|
|
1277 Do not use it.
|
|
1278 */
|
|
1279 (elt, list))
|
|
1280 {
|
|
1281 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
|
|
1282 {
|
|
1283 if (HACKEQ_UNSAFE (elt, list_elt))
|
|
1284 return tail;
|
|
1285 }
|
|
1286 return Qnil;
|
|
1287 }
|
|
1288
|
|
1289 Lisp_Object
|
|
1290 memq_no_quit (Lisp_Object elt, Lisp_Object list)
|
|
1291 {
|
|
1292 LIST_LOOP_3 (list_elt, list, tail)
|
|
1293 {
|
|
1294 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
|
|
1295 return tail;
|
|
1296 }
|
|
1297 return Qnil;
|
|
1298 }
|
|
1299
|
|
1300 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
|
444
|
1301 Return non-nil if KEY is `equal' to the car of an element of ALIST.
|
|
1302 The value is actually the element of ALIST whose car equals KEY.
|
428
|
1303 */
|
444
|
1304 (key, alist))
|
428
|
1305 {
|
|
1306 /* This function can GC. */
|
444
|
1307 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1308 {
|
|
1309 if (internal_equal (key, elt_car, 0))
|
|
1310 return elt;
|
|
1311 }
|
|
1312 return Qnil;
|
|
1313 }
|
|
1314
|
|
1315 DEFUN ("old-assoc", Fold_assoc, 2, 2, 0, /*
|
444
|
1316 Return non-nil if KEY is `old-equal' to the car of an element of ALIST.
|
|
1317 The value is actually the element of ALIST whose car equals KEY.
|
428
|
1318 */
|
444
|
1319 (key, alist))
|
428
|
1320 {
|
|
1321 /* This function can GC. */
|
444
|
1322 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1323 {
|
|
1324 if (internal_old_equal (key, elt_car, 0))
|
|
1325 return elt;
|
|
1326 }
|
|
1327 return Qnil;
|
|
1328 }
|
|
1329
|
|
1330 Lisp_Object
|
444
|
1331 assoc_no_quit (Lisp_Object key, Lisp_Object alist)
|
428
|
1332 {
|
|
1333 int speccount = specpdl_depth ();
|
|
1334 specbind (Qinhibit_quit, Qt);
|
444
|
1335 return unbind_to (speccount, Fassoc (key, alist));
|
428
|
1336 }
|
|
1337
|
|
1338 DEFUN ("assq", Fassq, 2, 2, 0, /*
|
444
|
1339 Return non-nil if KEY is `eq' to the car of an element of ALIST.
|
|
1340 The value is actually the element of ALIST whose car is KEY.
|
|
1341 Elements of ALIST that are not conses are ignored.
|
428
|
1342 */
|
444
|
1343 (key, alist))
|
428
|
1344 {
|
444
|
1345 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1346 {
|
|
1347 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
|
|
1348 return elt;
|
|
1349 }
|
|
1350 return Qnil;
|
|
1351 }
|
|
1352
|
|
1353 DEFUN ("old-assq", Fold_assq, 2, 2, 0, /*
|
444
|
1354 Return non-nil if KEY is `old-eq' to the car of an element of ALIST.
|
|
1355 The value is actually the element of ALIST whose car is KEY.
|
|
1356 Elements of ALIST that are not conses are ignored.
|
428
|
1357 This function is provided only for byte-code compatibility with v19.
|
|
1358 Do not use it.
|
|
1359 */
|
444
|
1360 (key, alist))
|
428
|
1361 {
|
444
|
1362 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1363 {
|
|
1364 if (HACKEQ_UNSAFE (key, elt_car))
|
|
1365 return elt;
|
|
1366 }
|
|
1367 return Qnil;
|
|
1368 }
|
|
1369
|
|
1370 /* Like Fassq but never report an error and do not allow quits.
|
|
1371 Use only on lists known never to be circular. */
|
|
1372
|
|
1373 Lisp_Object
|
444
|
1374 assq_no_quit (Lisp_Object key, Lisp_Object alist)
|
428
|
1375 {
|
|
1376 /* This cannot GC. */
|
444
|
1377 LIST_LOOP_2 (elt, alist)
|
428
|
1378 {
|
|
1379 Lisp_Object elt_car = XCAR (elt);
|
|
1380 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
|
|
1381 return elt;
|
|
1382 }
|
|
1383 return Qnil;
|
|
1384 }
|
|
1385
|
|
1386 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
|
444
|
1387 Return non-nil if VALUE is `equal' to the cdr of an element of ALIST.
|
|
1388 The value is actually the element of ALIST whose cdr equals VALUE.
|
428
|
1389 */
|
444
|
1390 (value, alist))
|
428
|
1391 {
|
444
|
1392 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1393 {
|
444
|
1394 if (internal_equal (value, elt_cdr, 0))
|
428
|
1395 return elt;
|
|
1396 }
|
|
1397 return Qnil;
|
|
1398 }
|
|
1399
|
|
1400 DEFUN ("old-rassoc", Fold_rassoc, 2, 2, 0, /*
|
444
|
1401 Return non-nil if VALUE is `old-equal' to the cdr of an element of ALIST.
|
|
1402 The value is actually the element of ALIST whose cdr equals VALUE.
|
428
|
1403 */
|
444
|
1404 (value, alist))
|
428
|
1405 {
|
444
|
1406 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1407 {
|
444
|
1408 if (internal_old_equal (value, elt_cdr, 0))
|
428
|
1409 return elt;
|
|
1410 }
|
|
1411 return Qnil;
|
|
1412 }
|
|
1413
|
|
1414 DEFUN ("rassq", Frassq, 2, 2, 0, /*
|
444
|
1415 Return non-nil if VALUE is `eq' to the cdr of an element of ALIST.
|
|
1416 The value is actually the element of ALIST whose cdr is VALUE.
|
428
|
1417 */
|
444
|
1418 (value, alist))
|
428
|
1419 {
|
444
|
1420 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1421 {
|
444
|
1422 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
|
428
|
1423 return elt;
|
|
1424 }
|
|
1425 return Qnil;
|
|
1426 }
|
|
1427
|
|
1428 DEFUN ("old-rassq", Fold_rassq, 2, 2, 0, /*
|
444
|
1429 Return non-nil if VALUE is `old-eq' to the cdr of an element of ALIST.
|
|
1430 The value is actually the element of ALIST whose cdr is VALUE.
|
428
|
1431 */
|
444
|
1432 (value, alist))
|
428
|
1433 {
|
444
|
1434 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
|
428
|
1435 {
|
444
|
1436 if (HACKEQ_UNSAFE (value, elt_cdr))
|
428
|
1437 return elt;
|
|
1438 }
|
|
1439 return Qnil;
|
|
1440 }
|
|
1441
|
444
|
1442 /* Like Frassq, but caller must ensure that ALIST is properly
|
428
|
1443 nil-terminated and ebola-free. */
|
|
1444 Lisp_Object
|
444
|
1445 rassq_no_quit (Lisp_Object value, Lisp_Object alist)
|
428
|
1446 {
|
444
|
1447 LIST_LOOP_2 (elt, alist)
|
428
|
1448 {
|
|
1449 Lisp_Object elt_cdr = XCDR (elt);
|
444
|
1450 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
|
428
|
1451 return elt;
|
|
1452 }
|
|
1453 return Qnil;
|
|
1454 }
|
|
1455
|
|
1456
|
|
1457 DEFUN ("delete", Fdelete, 2, 2, 0, /*
|
|
1458 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1459 The modified LIST is returned. Comparison is done with `equal'.
|
|
1460 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1461 effect; therefore, write `(setq foo (delete element foo))' to be sure
|
|
1462 of changing the value of `foo'.
|
|
1463 Also see: `remove'.
|
|
1464 */
|
|
1465 (elt, list))
|
|
1466 {
|
|
1467 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
|
|
1468 (internal_equal (elt, list_elt, 0)));
|
|
1469 return list;
|
|
1470 }
|
|
1471
|
|
1472 DEFUN ("old-delete", Fold_delete, 2, 2, 0, /*
|
|
1473 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1474 The modified LIST is returned. Comparison is done with `old-equal'.
|
|
1475 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1476 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
|
|
1477 of changing the value of `foo'.
|
|
1478 */
|
|
1479 (elt, list))
|
|
1480 {
|
|
1481 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
|
|
1482 (internal_old_equal (elt, list_elt, 0)));
|
|
1483 return list;
|
|
1484 }
|
|
1485
|
|
1486 DEFUN ("delq", Fdelq, 2, 2, 0, /*
|
|
1487 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1488 The modified LIST is returned. Comparison is done with `eq'.
|
|
1489 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1490 effect; therefore, write `(setq foo (delq element foo))' to be sure of
|
|
1491 changing the value of `foo'.
|
|
1492 */
|
|
1493 (elt, list))
|
|
1494 {
|
|
1495 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
|
|
1496 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
|
|
1497 return list;
|
|
1498 }
|
|
1499
|
|
1500 DEFUN ("old-delq", Fold_delq, 2, 2, 0, /*
|
|
1501 Delete by side effect any occurrences of ELT as a member of LIST.
|
|
1502 The modified LIST is returned. Comparison is done with `old-eq'.
|
|
1503 If the first member of LIST is ELT, there is no way to remove it by side
|
|
1504 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
|
|
1505 changing the value of `foo'.
|
|
1506 */
|
|
1507 (elt, list))
|
|
1508 {
|
|
1509 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
|
|
1510 (HACKEQ_UNSAFE (elt, list_elt)));
|
|
1511 return list;
|
|
1512 }
|
|
1513
|
|
1514 /* Like Fdelq, but caller must ensure that LIST is properly
|
|
1515 nil-terminated and ebola-free. */
|
|
1516
|
|
1517 Lisp_Object
|
|
1518 delq_no_quit (Lisp_Object elt, Lisp_Object list)
|
|
1519 {
|
|
1520 LIST_LOOP_DELETE_IF (list_elt, list,
|
|
1521 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
|
|
1522 return list;
|
|
1523 }
|
|
1524
|
|
1525 /* Be VERY careful with this. This is like delq_no_quit() but
|
|
1526 also calls free_cons() on the removed conses. You must be SURE
|
|
1527 that no pointers to the freed conses remain around (e.g.
|
|
1528 someone else is pointing to part of the list). This function
|
|
1529 is useful on internal lists that are used frequently and where
|
|
1530 the actual list doesn't escape beyond known code bounds. */
|
|
1531
|
|
1532 Lisp_Object
|
|
1533 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
|
|
1534 {
|
|
1535 REGISTER Lisp_Object tail = list;
|
|
1536 REGISTER Lisp_Object prev = Qnil;
|
|
1537
|
|
1538 while (!NILP (tail))
|
|
1539 {
|
|
1540 REGISTER Lisp_Object tem = XCAR (tail);
|
|
1541 if (EQ (elt, tem))
|
|
1542 {
|
|
1543 Lisp_Object cons_to_free = tail;
|
|
1544 if (NILP (prev))
|
|
1545 list = XCDR (tail);
|
|
1546 else
|
|
1547 XCDR (prev) = XCDR (tail);
|
|
1548 tail = XCDR (tail);
|
|
1549 free_cons (XCONS (cons_to_free));
|
|
1550 }
|
|
1551 else
|
|
1552 {
|
|
1553 prev = tail;
|
|
1554 tail = XCDR (tail);
|
|
1555 }
|
|
1556 }
|
|
1557 return list;
|
|
1558 }
|
|
1559
|
|
1560 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
|
444
|
1561 Delete by side effect any elements of ALIST whose car is `equal' to KEY.
|
|
1562 The modified ALIST is returned. If the first member of ALIST has a car
|
428
|
1563 that is `equal' to KEY, there is no way to remove it by side effect;
|
|
1564 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
|
|
1565 the value of `foo'.
|
|
1566 */
|
444
|
1567 (key, alist))
|
428
|
1568 {
|
444
|
1569 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1570 (CONSP (elt) &&
|
|
1571 internal_equal (key, XCAR (elt), 0)));
|
444
|
1572 return alist;
|
428
|
1573 }
|
|
1574
|
|
1575 Lisp_Object
|
444
|
1576 remassoc_no_quit (Lisp_Object key, Lisp_Object alist)
|
428
|
1577 {
|
|
1578 int speccount = specpdl_depth ();
|
|
1579 specbind (Qinhibit_quit, Qt);
|
444
|
1580 return unbind_to (speccount, Fremassoc (key, alist));
|
428
|
1581 }
|
|
1582
|
|
1583 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
|
444
|
1584 Delete by side effect any elements of ALIST whose car is `eq' to KEY.
|
|
1585 The modified ALIST is returned. If the first member of ALIST has a car
|
428
|
1586 that is `eq' to KEY, there is no way to remove it by side effect;
|
|
1587 therefore, write `(setq foo (remassq key foo))' to be sure of changing
|
|
1588 the value of `foo'.
|
|
1589 */
|
444
|
1590 (key, alist))
|
428
|
1591 {
|
444
|
1592 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1593 (CONSP (elt) &&
|
|
1594 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
|
444
|
1595 return alist;
|
428
|
1596 }
|
|
1597
|
|
1598 /* no quit, no errors; be careful */
|
|
1599
|
|
1600 Lisp_Object
|
444
|
1601 remassq_no_quit (Lisp_Object key, Lisp_Object alist)
|
428
|
1602 {
|
444
|
1603 LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1604 (CONSP (elt) &&
|
|
1605 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
|
444
|
1606 return alist;
|
428
|
1607 }
|
|
1608
|
|
1609 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
|
444
|
1610 Delete by side effect any elements of ALIST whose cdr is `equal' to VALUE.
|
|
1611 The modified ALIST is returned. If the first member of ALIST has a car
|
428
|
1612 that is `equal' to VALUE, there is no way to remove it by side effect;
|
|
1613 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
|
|
1614 the value of `foo'.
|
|
1615 */
|
444
|
1616 (value, alist))
|
428
|
1617 {
|
444
|
1618 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1619 (CONSP (elt) &&
|
|
1620 internal_equal (value, XCDR (elt), 0)));
|
444
|
1621 return alist;
|
428
|
1622 }
|
|
1623
|
|
1624 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
|
444
|
1625 Delete by side effect any elements of ALIST whose cdr is `eq' to VALUE.
|
|
1626 The modified ALIST is returned. If the first member of ALIST has a car
|
428
|
1627 that is `eq' to VALUE, there is no way to remove it by side effect;
|
|
1628 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
|
|
1629 the value of `foo'.
|
|
1630 */
|
444
|
1631 (value, alist))
|
428
|
1632 {
|
444
|
1633 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1634 (CONSP (elt) &&
|
|
1635 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
|
444
|
1636 return alist;
|
428
|
1637 }
|
|
1638
|
|
1639 /* Like Fremrassq, fast and unsafe; be careful */
|
|
1640 Lisp_Object
|
444
|
1641 remrassq_no_quit (Lisp_Object value, Lisp_Object alist)
|
428
|
1642 {
|
444
|
1643 LIST_LOOP_DELETE_IF (elt, alist,
|
428
|
1644 (CONSP (elt) &&
|
|
1645 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
|
444
|
1646 return alist;
|
428
|
1647 }
|
|
1648
|
|
1649 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
|
|
1650 Reverse LIST by destructively modifying cdr pointers.
|
|
1651 Return the beginning of the reversed list.
|
|
1652 Also see: `reverse'.
|
|
1653 */
|
|
1654 (list))
|
|
1655 {
|
|
1656 struct gcpro gcpro1, gcpro2;
|
|
1657 REGISTER Lisp_Object prev = Qnil;
|
|
1658 REGISTER Lisp_Object tail = list;
|
|
1659
|
|
1660 /* We gcpro our args; see `nconc' */
|
|
1661 GCPRO2 (prev, tail);
|
|
1662 while (!NILP (tail))
|
|
1663 {
|
|
1664 REGISTER Lisp_Object next;
|
|
1665 CONCHECK_CONS (tail);
|
|
1666 next = XCDR (tail);
|
|
1667 XCDR (tail) = prev;
|
|
1668 prev = tail;
|
|
1669 tail = next;
|
|
1670 }
|
|
1671 UNGCPRO;
|
|
1672 return prev;
|
|
1673 }
|
|
1674
|
|
1675 DEFUN ("reverse", Freverse, 1, 1, 0, /*
|
|
1676 Reverse LIST, copying. Return the beginning of the reversed list.
|
|
1677 See also the function `nreverse', which is used more often.
|
|
1678 */
|
|
1679 (list))
|
|
1680 {
|
|
1681 Lisp_Object reversed_list = Qnil;
|
|
1682 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1683 {
|
|
1684 reversed_list = Fcons (elt, reversed_list);
|
|
1685 }
|
|
1686 return reversed_list;
|
|
1687 }
|
|
1688
|
|
1689 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1690 Lisp_Object lisp_arg,
|
|
1691 int (*pred_fn) (Lisp_Object, Lisp_Object,
|
|
1692 Lisp_Object lisp_arg));
|
|
1693
|
|
1694 Lisp_Object
|
|
1695 list_sort (Lisp_Object list,
|
|
1696 Lisp_Object lisp_arg,
|
|
1697 int (*pred_fn) (Lisp_Object, Lisp_Object,
|
|
1698 Lisp_Object lisp_arg))
|
|
1699 {
|
|
1700 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1701 Lisp_Object back, tem;
|
|
1702 Lisp_Object front = list;
|
|
1703 Lisp_Object len = Flength (list);
|
444
|
1704
|
|
1705 if (XINT (len) < 2)
|
428
|
1706 return list;
|
|
1707
|
444
|
1708 len = make_int (XINT (len) / 2 - 1);
|
428
|
1709 tem = Fnthcdr (len, list);
|
|
1710 back = Fcdr (tem);
|
|
1711 Fsetcdr (tem, Qnil);
|
|
1712
|
|
1713 GCPRO3 (front, back, lisp_arg);
|
|
1714 front = list_sort (front, lisp_arg, pred_fn);
|
|
1715 back = list_sort (back, lisp_arg, pred_fn);
|
|
1716 UNGCPRO;
|
|
1717 return list_merge (front, back, lisp_arg, pred_fn);
|
|
1718 }
|
|
1719
|
|
1720
|
|
1721 static int
|
|
1722 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
|
|
1723 Lisp_Object pred)
|
|
1724 {
|
|
1725 Lisp_Object tmp;
|
|
1726
|
|
1727 /* prevents the GC from happening in call2 */
|
|
1728 int speccount = specpdl_depth ();
|
|
1729 /* Emacs' GC doesn't actually relocate pointers, so this probably
|
|
1730 isn't strictly necessary */
|
|
1731 record_unwind_protect (restore_gc_inhibit,
|
|
1732 make_int (gc_currently_forbidden));
|
|
1733 gc_currently_forbidden = 1;
|
|
1734 tmp = call2 (pred, obj1, obj2);
|
|
1735 unbind_to (speccount, Qnil);
|
|
1736
|
|
1737 if (NILP (tmp))
|
|
1738 return -1;
|
|
1739 else
|
|
1740 return 1;
|
|
1741 }
|
|
1742
|
|
1743 DEFUN ("sort", Fsort, 2, 2, 0, /*
|
|
1744 Sort LIST, stably, comparing elements using PREDICATE.
|
|
1745 Returns the sorted list. LIST is modified by side effects.
|
|
1746 PREDICATE is called with two elements of LIST, and should return T
|
|
1747 if the first element is "less" than the second.
|
|
1748 */
|
444
|
1749 (list, predicate))
|
428
|
1750 {
|
444
|
1751 return list_sort (list, predicate, merge_pred_function);
|
428
|
1752 }
|
|
1753
|
|
1754 Lisp_Object
|
|
1755 merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1756 Lisp_Object pred)
|
|
1757 {
|
|
1758 return list_merge (org_l1, org_l2, pred, merge_pred_function);
|
|
1759 }
|
|
1760
|
|
1761
|
|
1762 static Lisp_Object
|
|
1763 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
|
|
1764 Lisp_Object lisp_arg,
|
|
1765 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
|
|
1766 {
|
|
1767 Lisp_Object value;
|
|
1768 Lisp_Object tail;
|
|
1769 Lisp_Object tem;
|
|
1770 Lisp_Object l1, l2;
|
|
1771 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
1772
|
|
1773 l1 = org_l1;
|
|
1774 l2 = org_l2;
|
|
1775 tail = Qnil;
|
|
1776 value = Qnil;
|
|
1777
|
|
1778 /* It is sufficient to protect org_l1 and org_l2.
|
|
1779 When l1 and l2 are updated, we copy the new values
|
|
1780 back into the org_ vars. */
|
|
1781
|
|
1782 GCPRO4 (org_l1, org_l2, lisp_arg, value);
|
|
1783
|
|
1784 while (1)
|
|
1785 {
|
|
1786 if (NILP (l1))
|
|
1787 {
|
|
1788 UNGCPRO;
|
|
1789 if (NILP (tail))
|
|
1790 return l2;
|
|
1791 Fsetcdr (tail, l2);
|
|
1792 return value;
|
|
1793 }
|
|
1794 if (NILP (l2))
|
|
1795 {
|
|
1796 UNGCPRO;
|
|
1797 if (NILP (tail))
|
|
1798 return l1;
|
|
1799 Fsetcdr (tail, l1);
|
|
1800 return value;
|
|
1801 }
|
|
1802
|
|
1803 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
|
|
1804 {
|
|
1805 tem = l1;
|
|
1806 l1 = Fcdr (l1);
|
|
1807 org_l1 = l1;
|
|
1808 }
|
|
1809 else
|
|
1810 {
|
|
1811 tem = l2;
|
|
1812 l2 = Fcdr (l2);
|
|
1813 org_l2 = l2;
|
|
1814 }
|
|
1815 if (NILP (tail))
|
|
1816 value = tem;
|
|
1817 else
|
|
1818 Fsetcdr (tail, tem);
|
|
1819 tail = tem;
|
|
1820 }
|
|
1821 }
|
|
1822
|
|
1823
|
|
1824 /************************************************************************/
|
|
1825 /* property-list functions */
|
|
1826 /************************************************************************/
|
|
1827
|
|
1828 /* For properties of text, we need to do order-insensitive comparison of
|
|
1829 plists. That is, we need to compare two plists such that they are the
|
|
1830 same if they have the same set of keys, and equivalent values.
|
|
1831 So (a 1 b 2) would be equal to (b 2 a 1).
|
|
1832
|
|
1833 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
|
|
1834 LAXP means use `equal' for comparisons.
|
|
1835 */
|
|
1836 int
|
|
1837 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
|
|
1838 int laxp, int depth)
|
|
1839 {
|
438
|
1840 int eqp = (depth == -1); /* -1 as depth means use eq, not equal. */
|
428
|
1841 int la, lb, m, i, fill;
|
|
1842 Lisp_Object *keys, *vals;
|
|
1843 char *flags;
|
|
1844 Lisp_Object rest;
|
|
1845
|
|
1846 if (NILP (a) && NILP (b))
|
|
1847 return 0;
|
|
1848
|
|
1849 Fcheck_valid_plist (a);
|
|
1850 Fcheck_valid_plist (b);
|
|
1851
|
|
1852 la = XINT (Flength (a));
|
|
1853 lb = XINT (Flength (b));
|
|
1854 m = (la > lb ? la : lb);
|
|
1855 fill = 0;
|
|
1856 keys = alloca_array (Lisp_Object, m);
|
|
1857 vals = alloca_array (Lisp_Object, m);
|
|
1858 flags = alloca_array (char, m);
|
|
1859
|
|
1860 /* First extract the pairs from A. */
|
|
1861 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
|
|
1862 {
|
|
1863 Lisp_Object k = XCAR (rest);
|
|
1864 Lisp_Object v = XCAR (XCDR (rest));
|
|
1865 /* Maybe be Ebolified. */
|
|
1866 if (nil_means_not_present && NILP (v)) continue;
|
|
1867 keys [fill] = k;
|
|
1868 vals [fill] = v;
|
|
1869 flags[fill] = 0;
|
|
1870 fill++;
|
|
1871 }
|
|
1872 /* Now iterate over B, and stop if we find something that's not in A,
|
|
1873 or that doesn't match. As we match, mark them. */
|
|
1874 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
|
|
1875 {
|
|
1876 Lisp_Object k = XCAR (rest);
|
|
1877 Lisp_Object v = XCAR (XCDR (rest));
|
|
1878 /* Maybe be Ebolified. */
|
|
1879 if (nil_means_not_present && NILP (v)) continue;
|
|
1880 for (i = 0; i < fill; i++)
|
|
1881 {
|
|
1882 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
|
|
1883 {
|
434
|
1884 if (eqp
|
|
1885 /* We narrowly escaped being Ebolified here. */
|
|
1886 ? !EQ_WITH_EBOLA_NOTICE (v, vals [i])
|
|
1887 : !internal_equal (v, vals [i], depth))
|
428
|
1888 /* a property in B has a different value than in A */
|
|
1889 goto MISMATCH;
|
|
1890 flags [i] = 1;
|
|
1891 break;
|
|
1892 }
|
|
1893 }
|
|
1894 if (i == fill)
|
|
1895 /* there are some properties in B that are not in A */
|
|
1896 goto MISMATCH;
|
|
1897 }
|
|
1898 /* Now check to see that all the properties in A were also in B */
|
|
1899 for (i = 0; i < fill; i++)
|
|
1900 if (flags [i] == 0)
|
|
1901 goto MISMATCH;
|
|
1902
|
|
1903 /* Ok. */
|
|
1904 return 0;
|
|
1905
|
|
1906 MISMATCH:
|
|
1907 return 1;
|
|
1908 }
|
|
1909
|
|
1910 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
|
|
1911 Return non-nil if property lists A and B are `eq'.
|
|
1912 A property list is an alternating list of keywords and values.
|
|
1913 This function does order-insensitive comparisons of the property lists:
|
|
1914 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1915 Comparison between values is done using `eq'. See also `plists-equal'.
|
|
1916 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1917 a nil value is ignored. This feature is a virus that has infected
|
|
1918 old Lisp implementations, but should not be used except for backward
|
|
1919 compatibility.
|
|
1920 */
|
|
1921 (a, b, nil_means_not_present))
|
|
1922 {
|
|
1923 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
|
|
1924 ? Qnil : Qt);
|
|
1925 }
|
|
1926
|
|
1927 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
|
|
1928 Return non-nil if property lists A and B are `equal'.
|
|
1929 A property list is an alternating list of keywords and values. This
|
|
1930 function does order-insensitive comparisons of the property lists: For
|
|
1931 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1932 Comparison between values is done using `equal'. See also `plists-eq'.
|
|
1933 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1934 a nil value is ignored. This feature is a virus that has infected
|
|
1935 old Lisp implementations, but should not be used except for backward
|
|
1936 compatibility.
|
|
1937 */
|
|
1938 (a, b, nil_means_not_present))
|
|
1939 {
|
|
1940 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
|
|
1941 ? Qnil : Qt);
|
|
1942 }
|
|
1943
|
|
1944
|
|
1945 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
|
|
1946 Return non-nil if lax property lists A and B are `eq'.
|
|
1947 A property list is an alternating list of keywords and values.
|
|
1948 This function does order-insensitive comparisons of the property lists:
|
|
1949 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1950 Comparison between values is done using `eq'. See also `plists-equal'.
|
|
1951 A lax property list is like a regular one except that comparisons between
|
|
1952 keywords is done using `equal' instead of `eq'.
|
|
1953 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1954 a nil value is ignored. This feature is a virus that has infected
|
|
1955 old Lisp implementations, but should not be used except for backward
|
|
1956 compatibility.
|
|
1957 */
|
|
1958 (a, b, nil_means_not_present))
|
|
1959 {
|
|
1960 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
|
|
1961 ? Qnil : Qt);
|
|
1962 }
|
|
1963
|
|
1964 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
|
|
1965 Return non-nil if lax property lists A and B are `equal'.
|
|
1966 A property list is an alternating list of keywords and values. This
|
|
1967 function does order-insensitive comparisons of the property lists: For
|
|
1968 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
|
|
1969 Comparison between values is done using `equal'. See also `plists-eq'.
|
|
1970 A lax property list is like a regular one except that comparisons between
|
|
1971 keywords is done using `equal' instead of `eq'.
|
|
1972 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
1973 a nil value is ignored. This feature is a virus that has infected
|
|
1974 old Lisp implementations, but should not be used except for backward
|
|
1975 compatibility.
|
|
1976 */
|
|
1977 (a, b, nil_means_not_present))
|
|
1978 {
|
|
1979 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
|
|
1980 ? Qnil : Qt);
|
|
1981 }
|
|
1982
|
|
1983 /* Return the value associated with key PROPERTY in property list PLIST.
|
|
1984 Return nil if key not found. This function is used for internal
|
|
1985 property lists that cannot be directly manipulated by the user.
|
|
1986 */
|
|
1987
|
|
1988 Lisp_Object
|
|
1989 internal_plist_get (Lisp_Object plist, Lisp_Object property)
|
|
1990 {
|
|
1991 Lisp_Object tail;
|
|
1992
|
|
1993 for (tail = plist; !NILP (tail); tail = XCDR (XCDR (tail)))
|
|
1994 {
|
|
1995 if (EQ (XCAR (tail), property))
|
|
1996 return XCAR (XCDR (tail));
|
|
1997 }
|
|
1998
|
|
1999 return Qunbound;
|
|
2000 }
|
|
2001
|
|
2002 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
|
|
2003 internal_plist_get(). */
|
|
2004
|
|
2005 void
|
|
2006 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
|
|
2007 Lisp_Object value)
|
|
2008 {
|
|
2009 Lisp_Object tail;
|
|
2010
|
|
2011 for (tail = *plist; !NILP (tail); tail = XCDR (XCDR (tail)))
|
|
2012 {
|
|
2013 if (EQ (XCAR (tail), property))
|
|
2014 {
|
|
2015 XCAR (XCDR (tail)) = value;
|
|
2016 return;
|
|
2017 }
|
|
2018 }
|
|
2019
|
|
2020 *plist = Fcons (property, Fcons (value, *plist));
|
|
2021 }
|
|
2022
|
|
2023 int
|
|
2024 internal_remprop (Lisp_Object *plist, Lisp_Object property)
|
|
2025 {
|
|
2026 Lisp_Object tail, prev;
|
|
2027
|
|
2028 for (tail = *plist, prev = Qnil;
|
|
2029 !NILP (tail);
|
|
2030 tail = XCDR (XCDR (tail)))
|
|
2031 {
|
|
2032 if (EQ (XCAR (tail), property))
|
|
2033 {
|
|
2034 if (NILP (prev))
|
|
2035 *plist = XCDR (XCDR (tail));
|
|
2036 else
|
|
2037 XCDR (XCDR (prev)) = XCDR (XCDR (tail));
|
|
2038 return 1;
|
|
2039 }
|
|
2040 else
|
|
2041 prev = tail;
|
|
2042 }
|
|
2043
|
|
2044 return 0;
|
|
2045 }
|
|
2046
|
|
2047 /* Called on a malformed property list. BADPLACE should be some
|
|
2048 place where truncating will form a good list -- i.e. we shouldn't
|
|
2049 result in a list with an odd length. */
|
|
2050
|
|
2051 static Lisp_Object
|
|
2052 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
|
|
2053 {
|
|
2054 if (ERRB_EQ (errb, ERROR_ME))
|
|
2055 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
|
|
2056 else
|
|
2057 {
|
|
2058 if (ERRB_EQ (errb, ERROR_ME_WARN))
|
|
2059 {
|
|
2060 warn_when_safe_lispobj
|
|
2061 (Qlist, Qwarning,
|
|
2062 list2 (build_string
|
|
2063 ("Malformed property list -- list has been truncated"),
|
|
2064 *plist));
|
|
2065 *badplace = Qnil;
|
|
2066 }
|
|
2067 return Qunbound;
|
|
2068 }
|
|
2069 }
|
|
2070
|
|
2071 /* Called on a circular property list. BADPLACE should be some place
|
|
2072 where truncating will result in an even-length list, as above.
|
|
2073 If doesn't particularly matter where we truncate -- anywhere we
|
|
2074 truncate along the entire list will break the circularity, because
|
|
2075 it will create a terminus and the list currently doesn't have one.
|
|
2076 */
|
|
2077
|
|
2078 static Lisp_Object
|
|
2079 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
|
|
2080 {
|
|
2081 if (ERRB_EQ (errb, ERROR_ME))
|
|
2082 return Fsignal (Qcircular_property_list, list1 (*plist));
|
|
2083 else
|
|
2084 {
|
|
2085 if (ERRB_EQ (errb, ERROR_ME_WARN))
|
|
2086 {
|
|
2087 warn_when_safe_lispobj
|
|
2088 (Qlist, Qwarning,
|
|
2089 list2 (build_string
|
|
2090 ("Circular property list -- list has been truncated"),
|
|
2091 *plist));
|
|
2092 *badplace = Qnil;
|
|
2093 }
|
|
2094 return Qunbound;
|
|
2095 }
|
|
2096 }
|
|
2097
|
|
2098 /* Advance the tortoise pointer by two (one iteration of a property-list
|
|
2099 loop) and the hare pointer by four and verify that no malformations
|
|
2100 or circularities exist. If so, return zero and store a value into
|
|
2101 RETVAL that should be returned by the calling function. Otherwise,
|
|
2102 return 1. See external_plist_get().
|
|
2103 */
|
|
2104
|
|
2105 static int
|
|
2106 advance_plist_pointers (Lisp_Object *plist,
|
|
2107 Lisp_Object **tortoise, Lisp_Object **hare,
|
|
2108 Error_behavior errb, Lisp_Object *retval)
|
|
2109 {
|
|
2110 int i;
|
|
2111 Lisp_Object *tortsave = *tortoise;
|
|
2112
|
|
2113 /* Note that our "fixing" may be more brutal than necessary,
|
|
2114 but it's the user's own problem, not ours, if they went in and
|
|
2115 manually fucked up a plist. */
|
|
2116
|
|
2117 for (i = 0; i < 2; i++)
|
|
2118 {
|
|
2119 /* This is a standard iteration of a defensive-loop-checking
|
|
2120 loop. We just do it twice because we want to advance past
|
|
2121 both the property and its value.
|
|
2122
|
|
2123 If the pointer indirection is confusing you, remember that
|
|
2124 one level of indirection on the hare and tortoise pointers
|
|
2125 is only due to pass-by-reference for this function. The other
|
|
2126 level is so that the plist can be fixed in place. */
|
|
2127
|
|
2128 /* When we reach the end of a well-formed plist, **HARE is
|
|
2129 nil. In that case, we don't do anything at all except
|
|
2130 advance TORTOISE by one. Otherwise, we advance HARE
|
|
2131 by two (making sure it's OK to do so), then advance
|
|
2132 TORTOISE by one (it will always be OK to do so because
|
|
2133 the HARE is always ahead of the TORTOISE and will have
|
|
2134 already verified the path), then make sure TORTOISE and
|
|
2135 HARE don't contain the same non-nil object -- if the
|
|
2136 TORTOISE and the HARE ever meet, then obviously we're
|
|
2137 in a circularity, and if we're in a circularity, then
|
|
2138 the TORTOISE and the HARE can't cross paths without
|
|
2139 meeting, since the HARE only gains one step over the
|
|
2140 TORTOISE per iteration. */
|
|
2141
|
|
2142 if (!NILP (**hare))
|
|
2143 {
|
|
2144 Lisp_Object *haresave = *hare;
|
|
2145 if (!CONSP (**hare))
|
|
2146 {
|
|
2147 *retval = bad_bad_bunny (plist, haresave, errb);
|
|
2148 return 0;
|
|
2149 }
|
|
2150 *hare = &XCDR (**hare);
|
|
2151 /* In a non-plist, we'd check here for a nil value for
|
|
2152 **HARE, which is OK (it just means the list has an
|
|
2153 odd number of elements). In a plist, it's not OK
|
|
2154 for the list to have an odd number of elements. */
|
|
2155 if (!CONSP (**hare))
|
|
2156 {
|
|
2157 *retval = bad_bad_bunny (plist, haresave, errb);
|
|
2158 return 0;
|
|
2159 }
|
|
2160 *hare = &XCDR (**hare);
|
|
2161 }
|
|
2162
|
|
2163 *tortoise = &XCDR (**tortoise);
|
|
2164 if (!NILP (**hare) && EQ (**tortoise, **hare))
|
|
2165 {
|
|
2166 *retval = bad_bad_turtle (plist, tortsave, errb);
|
|
2167 return 0;
|
|
2168 }
|
|
2169 }
|
|
2170
|
|
2171 return 1;
|
|
2172 }
|
|
2173
|
|
2174 /* Return the value of PROPERTY from PLIST, or Qunbound if
|
|
2175 property is not on the list.
|
|
2176
|
|
2177 PLIST is a Lisp-accessible property list, meaning that it
|
|
2178 has to be checked for malformations and circularities.
|
|
2179
|
|
2180 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
|
|
2181 function will never signal an error; and if ERRB is ERROR_ME_WARN,
|
|
2182 on finding a malformation or a circularity, it issues a warning and
|
|
2183 attempts to silently fix the problem.
|
|
2184
|
|
2185 A pointer to PLIST is passed in so that PLIST can be successfully
|
|
2186 "fixed" even if the error is at the beginning of the plist. */
|
|
2187
|
|
2188 Lisp_Object
|
|
2189 external_plist_get (Lisp_Object *plist, Lisp_Object property,
|
|
2190 int laxp, Error_behavior errb)
|
|
2191 {
|
|
2192 Lisp_Object *tortoise = plist;
|
|
2193 Lisp_Object *hare = plist;
|
|
2194
|
|
2195 while (!NILP (*tortoise))
|
|
2196 {
|
|
2197 Lisp_Object *tortsave = tortoise;
|
|
2198 Lisp_Object retval;
|
|
2199
|
|
2200 /* We do the standard tortoise/hare march. We isolate the
|
|
2201 grungy stuff to do this in advance_plist_pointers(), though.
|
|
2202 To us, all this function does is advance the tortoise
|
|
2203 pointer by two and the hare pointer by four and make sure
|
|
2204 everything's OK. We first advance the pointers and then
|
|
2205 check if a property matched; this ensures that our
|
|
2206 check for a matching property is safe. */
|
|
2207
|
|
2208 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2209 return retval;
|
|
2210
|
|
2211 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2212 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2213 return XCAR (XCDR (*tortsave));
|
|
2214 }
|
|
2215
|
|
2216 return Qunbound;
|
|
2217 }
|
|
2218
|
|
2219 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
|
|
2220 malformed or circular plist. Analogous to external_plist_get(). */
|
|
2221
|
|
2222 void
|
|
2223 external_plist_put (Lisp_Object *plist, Lisp_Object property,
|
|
2224 Lisp_Object value, int laxp, Error_behavior errb)
|
|
2225 {
|
|
2226 Lisp_Object *tortoise = plist;
|
|
2227 Lisp_Object *hare = plist;
|
|
2228
|
|
2229 while (!NILP (*tortoise))
|
|
2230 {
|
|
2231 Lisp_Object *tortsave = tortoise;
|
|
2232 Lisp_Object retval;
|
|
2233
|
|
2234 /* See above */
|
|
2235 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2236 return;
|
|
2237
|
|
2238 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2239 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2240 {
|
|
2241 XCAR (XCDR (*tortsave)) = value;
|
|
2242 return;
|
|
2243 }
|
|
2244 }
|
|
2245
|
|
2246 *plist = Fcons (property, Fcons (value, *plist));
|
|
2247 }
|
|
2248
|
|
2249 int
|
|
2250 external_remprop (Lisp_Object *plist, Lisp_Object property,
|
|
2251 int laxp, Error_behavior errb)
|
|
2252 {
|
|
2253 Lisp_Object *tortoise = plist;
|
|
2254 Lisp_Object *hare = plist;
|
|
2255
|
|
2256 while (!NILP (*tortoise))
|
|
2257 {
|
|
2258 Lisp_Object *tortsave = tortoise;
|
|
2259 Lisp_Object retval;
|
|
2260
|
|
2261 /* See above */
|
|
2262 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
|
|
2263 return 0;
|
|
2264
|
|
2265 if (!laxp ? EQ (XCAR (*tortsave), property)
|
|
2266 : internal_equal (XCAR (*tortsave), property, 0))
|
|
2267 {
|
|
2268 /* Now you see why it's so convenient to have that level
|
|
2269 of indirection. */
|
|
2270 *tortsave = XCDR (XCDR (*tortsave));
|
|
2271 return 1;
|
|
2272 }
|
|
2273 }
|
|
2274
|
|
2275 return 0;
|
|
2276 }
|
|
2277
|
|
2278 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
|
|
2279 Extract a value from a property list.
|
|
2280 PLIST is a property list, which is a list of the form
|
444
|
2281 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...).
|
|
2282 PROPERTY is usually a symbol.
|
|
2283 This function returns the value corresponding to the PROPERTY,
|
|
2284 or DEFAULT if PROPERTY is not one of the properties on the list.
|
428
|
2285 */
|
444
|
2286 (plist, property, default_))
|
428
|
2287 {
|
444
|
2288 Lisp_Object value = external_plist_get (&plist, property, 0, ERROR_ME);
|
|
2289 return UNBOUNDP (value) ? default_ : value;
|
428
|
2290 }
|
|
2291
|
|
2292 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
|
444
|
2293 Change value in PLIST of PROPERTY to VALUE.
|
|
2294 PLIST is a property list, which is a list of the form
|
|
2295 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
|
|
2296 PROPERTY is usually a symbol and VALUE is any object.
|
|
2297 If PROPERTY is already a property on the list, its value is set to VALUE,
|
|
2298 otherwise the new PROPERTY VALUE pair is added.
|
|
2299 The new plist is returned; use `(setq x (plist-put x property value))'
|
|
2300 to be sure to use the new value. PLIST is modified by side effect.
|
428
|
2301 */
|
444
|
2302 (plist, property, value))
|
428
|
2303 {
|
444
|
2304 external_plist_put (&plist, property, value, 0, ERROR_ME);
|
428
|
2305 return plist;
|
|
2306 }
|
|
2307
|
|
2308 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
|
444
|
2309 Remove from PLIST the property PROPERTY and its value.
|
|
2310 PLIST is a property list, which is a list of the form
|
|
2311 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
|
|
2312 PROPERTY is usually a symbol.
|
|
2313 The new plist is returned; use `(setq x (plist-remprop x property))'
|
|
2314 to be sure to use the new value. PLIST is modified by side effect.
|
428
|
2315 */
|
444
|
2316 (plist, property))
|
428
|
2317 {
|
444
|
2318 external_remprop (&plist, property, 0, ERROR_ME);
|
428
|
2319 return plist;
|
|
2320 }
|
|
2321
|
|
2322 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
|
444
|
2323 Return t if PROPERTY has a value specified in PLIST.
|
428
|
2324 */
|
444
|
2325 (plist, property))
|
428
|
2326 {
|
444
|
2327 Lisp_Object value = Fplist_get (plist, property, Qunbound);
|
|
2328 return UNBOUNDP (value) ? Qnil : Qt;
|
428
|
2329 }
|
|
2330
|
|
2331 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
|
|
2332 Given a plist, signal an error if there is anything wrong with it.
|
|
2333 This means that it's a malformed or circular plist.
|
|
2334 */
|
|
2335 (plist))
|
|
2336 {
|
|
2337 Lisp_Object *tortoise;
|
|
2338 Lisp_Object *hare;
|
|
2339
|
|
2340 start_over:
|
|
2341 tortoise = &plist;
|
|
2342 hare = &plist;
|
|
2343 while (!NILP (*tortoise))
|
|
2344 {
|
|
2345 Lisp_Object retval;
|
|
2346
|
|
2347 /* See above */
|
|
2348 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
|
|
2349 &retval))
|
|
2350 goto start_over;
|
|
2351 }
|
|
2352
|
|
2353 return Qnil;
|
|
2354 }
|
|
2355
|
|
2356 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
|
|
2357 Given a plist, return non-nil if its format is correct.
|
|
2358 If it returns nil, `check-valid-plist' will signal an error when given
|
442
|
2359 the plist; that means it's a malformed or circular plist.
|
428
|
2360 */
|
|
2361 (plist))
|
|
2362 {
|
|
2363 Lisp_Object *tortoise;
|
|
2364 Lisp_Object *hare;
|
|
2365
|
|
2366 tortoise = &plist;
|
|
2367 hare = &plist;
|
|
2368 while (!NILP (*tortoise))
|
|
2369 {
|
|
2370 Lisp_Object retval;
|
|
2371
|
|
2372 /* See above */
|
|
2373 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
|
|
2374 &retval))
|
|
2375 return Qnil;
|
|
2376 }
|
|
2377
|
|
2378 return Qt;
|
|
2379 }
|
|
2380
|
|
2381 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
|
|
2382 Destructively remove any duplicate entries from a plist.
|
|
2383 In such cases, the first entry applies.
|
|
2384
|
|
2385 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
2386 a nil value is removed. This feature is a virus that has infected
|
|
2387 old Lisp implementations, but should not be used except for backward
|
|
2388 compatibility.
|
|
2389
|
|
2390 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
|
|
2391 return value may not be EQ to the passed-in value, so make sure to
|
|
2392 `setq' the value back into where it came from.
|
|
2393 */
|
|
2394 (plist, nil_means_not_present))
|
|
2395 {
|
|
2396 Lisp_Object head = plist;
|
|
2397
|
|
2398 Fcheck_valid_plist (plist);
|
|
2399
|
|
2400 while (!NILP (plist))
|
|
2401 {
|
|
2402 Lisp_Object prop = Fcar (plist);
|
|
2403 Lisp_Object next = Fcdr (plist);
|
|
2404
|
|
2405 CHECK_CONS (next); /* just make doubly sure we catch any errors */
|
|
2406 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
|
|
2407 {
|
|
2408 if (EQ (head, plist))
|
|
2409 head = Fcdr (next);
|
|
2410 plist = Fcdr (next);
|
|
2411 continue;
|
|
2412 }
|
|
2413 /* external_remprop returns 1 if it removed any property.
|
|
2414 We have to loop till it didn't remove anything, in case
|
|
2415 the property occurs many times. */
|
|
2416 while (external_remprop (&XCDR (next), prop, 0, ERROR_ME))
|
|
2417 DO_NOTHING;
|
|
2418 plist = Fcdr (next);
|
|
2419 }
|
|
2420
|
|
2421 return head;
|
|
2422 }
|
|
2423
|
|
2424 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
|
|
2425 Extract a value from a lax property list.
|
444
|
2426 LAX-PLIST is a lax property list, which is a list of the form
|
|
2427 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
|
|
2428 properties is done using `equal' instead of `eq'.
|
|
2429 PROPERTY is usually a symbol.
|
|
2430 This function returns the value corresponding to PROPERTY,
|
|
2431 or DEFAULT if PROPERTY is not one of the properties on the list.
|
428
|
2432 */
|
444
|
2433 (lax_plist, property, default_))
|
428
|
2434 {
|
444
|
2435 Lisp_Object value = external_plist_get (&lax_plist, property, 1, ERROR_ME);
|
|
2436 return UNBOUNDP (value) ? default_ : value;
|
428
|
2437 }
|
|
2438
|
|
2439 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
|
444
|
2440 Change value in LAX-PLIST of PROPERTY to VALUE.
|
|
2441 LAX-PLIST is a lax property list, which is a list of the form
|
|
2442 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
|
|
2443 properties is done using `equal' instead of `eq'.
|
|
2444 PROPERTY is usually a symbol and VALUE is any object.
|
|
2445 If PROPERTY is already a property on the list, its value is set to
|
|
2446 VALUE, otherwise the new PROPERTY VALUE pair is added.
|
|
2447 The new plist is returned; use `(setq x (lax-plist-put x property value))'
|
|
2448 to be sure to use the new value. LAX-PLIST is modified by side effect.
|
428
|
2449 */
|
444
|
2450 (lax_plist, property, value))
|
428
|
2451 {
|
444
|
2452 external_plist_put (&lax_plist, property, value, 1, ERROR_ME);
|
428
|
2453 return lax_plist;
|
|
2454 }
|
|
2455
|
|
2456 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
|
444
|
2457 Remove from LAX-PLIST the property PROPERTY and its value.
|
|
2458 LAX-PLIST is a lax property list, which is a list of the form
|
|
2459 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
|
|
2460 properties is done using `equal' instead of `eq'.
|
|
2461 PROPERTY is usually a symbol.
|
|
2462 The new plist is returned; use `(setq x (lax-plist-remprop x property))'
|
|
2463 to be sure to use the new value. LAX-PLIST is modified by side effect.
|
428
|
2464 */
|
444
|
2465 (lax_plist, property))
|
428
|
2466 {
|
444
|
2467 external_remprop (&lax_plist, property, 1, ERROR_ME);
|
428
|
2468 return lax_plist;
|
|
2469 }
|
|
2470
|
|
2471 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
|
444
|
2472 Return t if PROPERTY has a value specified in LAX-PLIST.
|
|
2473 LAX-PLIST is a lax property list, which is a list of the form
|
|
2474 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
|
|
2475 properties is done using `equal' instead of `eq'.
|
428
|
2476 */
|
444
|
2477 (lax_plist, property))
|
428
|
2478 {
|
444
|
2479 return UNBOUNDP (Flax_plist_get (lax_plist, property, Qunbound)) ? Qnil : Qt;
|
428
|
2480 }
|
|
2481
|
|
2482 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
|
|
2483 Destructively remove any duplicate entries from a lax plist.
|
|
2484 In such cases, the first entry applies.
|
|
2485
|
|
2486 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
|
|
2487 a nil value is removed. This feature is a virus that has infected
|
|
2488 old Lisp implementations, but should not be used except for backward
|
|
2489 compatibility.
|
|
2490
|
|
2491 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
|
|
2492 return value may not be EQ to the passed-in value, so make sure to
|
|
2493 `setq' the value back into where it came from.
|
|
2494 */
|
|
2495 (lax_plist, nil_means_not_present))
|
|
2496 {
|
|
2497 Lisp_Object head = lax_plist;
|
|
2498
|
|
2499 Fcheck_valid_plist (lax_plist);
|
|
2500
|
|
2501 while (!NILP (lax_plist))
|
|
2502 {
|
|
2503 Lisp_Object prop = Fcar (lax_plist);
|
|
2504 Lisp_Object next = Fcdr (lax_plist);
|
|
2505
|
|
2506 CHECK_CONS (next); /* just make doubly sure we catch any errors */
|
|
2507 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
|
|
2508 {
|
|
2509 if (EQ (head, lax_plist))
|
|
2510 head = Fcdr (next);
|
|
2511 lax_plist = Fcdr (next);
|
|
2512 continue;
|
|
2513 }
|
|
2514 /* external_remprop returns 1 if it removed any property.
|
|
2515 We have to loop till it didn't remove anything, in case
|
|
2516 the property occurs many times. */
|
|
2517 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME))
|
|
2518 DO_NOTHING;
|
|
2519 lax_plist = Fcdr (next);
|
|
2520 }
|
|
2521
|
|
2522 return head;
|
|
2523 }
|
|
2524
|
|
2525 /* In C because the frame props stuff uses it */
|
|
2526
|
|
2527 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
|
|
2528 Convert association list ALIST into the equivalent property-list form.
|
|
2529 The plist is returned. This converts from
|
|
2530
|
|
2531 \((a . 1) (b . 2) (c . 3))
|
|
2532
|
|
2533 into
|
|
2534
|
|
2535 \(a 1 b 2 c 3)
|
|
2536
|
|
2537 The original alist is destroyed in the process of constructing the plist.
|
|
2538 See also `alist-to-plist'.
|
|
2539 */
|
|
2540 (alist))
|
|
2541 {
|
|
2542 Lisp_Object head = alist;
|
|
2543 while (!NILP (alist))
|
|
2544 {
|
|
2545 /* remember the alist element. */
|
|
2546 Lisp_Object el = Fcar (alist);
|
|
2547
|
|
2548 Fsetcar (alist, Fcar (el));
|
|
2549 Fsetcar (el, Fcdr (el));
|
|
2550 Fsetcdr (el, Fcdr (alist));
|
|
2551 Fsetcdr (alist, el);
|
|
2552 alist = Fcdr (Fcdr (alist));
|
|
2553 }
|
|
2554
|
|
2555 return head;
|
|
2556 }
|
|
2557
|
|
2558 DEFUN ("get", Fget, 2, 3, 0, /*
|
442
|
2559 Return the value of OBJECT's PROPERTY property.
|
|
2560 This is the last VALUE stored with `(put OBJECT PROPERTY VALUE)'.
|
428
|
2561 If there is no such property, return optional third arg DEFAULT
|
442
|
2562 \(which defaults to `nil'). OBJECT can be a symbol, string, extent,
|
|
2563 face, or glyph. See also `put', `remprop', and `object-plist'.
|
428
|
2564 */
|
442
|
2565 (object, property, default_))
|
428
|
2566 {
|
|
2567 /* Various places in emacs call Fget() and expect it not to quit,
|
|
2568 so don't quit. */
|
442
|
2569 Lisp_Object val;
|
|
2570
|
|
2571 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->getprop)
|
|
2572 val = XRECORD_LHEADER_IMPLEMENTATION (object)->getprop (object, property);
|
428
|
2573 else
|
563
|
2574 invalid_operation ("Object type has no properties", object);
|
442
|
2575
|
|
2576 return UNBOUNDP (val) ? default_ : val;
|
428
|
2577 }
|
|
2578
|
|
2579 DEFUN ("put", Fput, 3, 3, 0, /*
|
442
|
2580 Set OBJECT's PROPERTY to VALUE.
|
|
2581 It can be subsequently retrieved with `(get OBJECT PROPERTY)'.
|
|
2582 OBJECT can be a symbol, face, extent, or string.
|
428
|
2583 For a string, no properties currently have predefined meanings.
|
|
2584 For the predefined properties for extents, see `set-extent-property'.
|
|
2585 For the predefined properties for faces, see `set-face-property'.
|
|
2586 See also `get', `remprop', and `object-plist'.
|
|
2587 */
|
442
|
2588 (object, property, value))
|
428
|
2589 {
|
|
2590 CHECK_LISP_WRITEABLE (object);
|
|
2591
|
442
|
2592 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->putprop)
|
428
|
2593 {
|
442
|
2594 if (! XRECORD_LHEADER_IMPLEMENTATION (object)->putprop
|
|
2595 (object, property, value))
|
563
|
2596 invalid_change ("Can't set property on object", property);
|
428
|
2597 }
|
|
2598 else
|
563
|
2599 invalid_change ("Object type has no settable properties", object);
|
428
|
2600
|
|
2601 return value;
|
|
2602 }
|
|
2603
|
|
2604 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
|
442
|
2605 Remove, from OBJECT's property list, PROPERTY and its corresponding value.
|
|
2606 OBJECT can be a symbol, string, extent, face, or glyph. Return non-nil
|
|
2607 if the property list was actually modified (i.e. if PROPERTY was present
|
|
2608 in the property list). See also `get', `put', and `object-plist'.
|
428
|
2609 */
|
442
|
2610 (object, property))
|
428
|
2611 {
|
442
|
2612 int ret = 0;
|
|
2613
|
428
|
2614 CHECK_LISP_WRITEABLE (object);
|
|
2615
|
442
|
2616 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->remprop)
|
428
|
2617 {
|
442
|
2618 ret = XRECORD_LHEADER_IMPLEMENTATION (object)->remprop (object, property);
|
|
2619 if (ret == -1)
|
563
|
2620 invalid_change ("Can't remove property from object", property);
|
428
|
2621 }
|
|
2622 else
|
563
|
2623 invalid_change ("Object type has no removable properties", object);
|
442
|
2624
|
|
2625 return ret ? Qt : Qnil;
|
428
|
2626 }
|
|
2627
|
|
2628 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
|
442
|
2629 Return a property list of OBJECT's properties.
|
|
2630 For a symbol, this is equivalent to `symbol-plist'.
|
|
2631 OBJECT can be a symbol, string, extent, face, or glyph.
|
|
2632 Do not modify the returned property list directly;
|
|
2633 this may or may not have the desired effects. Use `put' instead.
|
428
|
2634 */
|
|
2635 (object))
|
|
2636 {
|
442
|
2637 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->plist)
|
|
2638 return XRECORD_LHEADER_IMPLEMENTATION (object)->plist (object);
|
428
|
2639 else
|
563
|
2640 invalid_operation ("Object type has no properties", object);
|
428
|
2641
|
|
2642 return Qnil;
|
|
2643 }
|
|
2644
|
|
2645
|
|
2646 int
|
|
2647 internal_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2648 {
|
|
2649 if (depth > 200)
|
563
|
2650 stack_overflow ("Stack overflow in equal", Qunbound);
|
428
|
2651 QUIT;
|
|
2652 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
|
|
2653 return 1;
|
|
2654 /* Note that (equal 20 20.0) should be nil */
|
|
2655 if (XTYPE (obj1) != XTYPE (obj2))
|
|
2656 return 0;
|
|
2657 if (LRECORDP (obj1))
|
|
2658 {
|
442
|
2659 const struct lrecord_implementation
|
428
|
2660 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
|
|
2661 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
|
|
2662
|
|
2663 return (imp1 == imp2) &&
|
|
2664 /* EQ-ness of the objects was noticed above */
|
|
2665 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
|
|
2666 }
|
|
2667
|
|
2668 return 0;
|
|
2669 }
|
|
2670
|
|
2671 /* Note that we may be calling sub-objects that will use
|
|
2672 internal_equal() (instead of internal_old_equal()). Oh well.
|
|
2673 We will get an Ebola note if there's any possibility of confusion,
|
|
2674 but that seems unlikely. */
|
|
2675
|
|
2676 static int
|
|
2677 internal_old_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2678 {
|
|
2679 if (depth > 200)
|
563
|
2680 stack_overflow ("Stack overflow in equal", Qunbound);
|
428
|
2681 QUIT;
|
|
2682 if (HACKEQ_UNSAFE (obj1, obj2))
|
|
2683 return 1;
|
|
2684 /* Note that (equal 20 20.0) should be nil */
|
|
2685 if (XTYPE (obj1) != XTYPE (obj2))
|
|
2686 return 0;
|
|
2687
|
|
2688 return internal_equal (obj1, obj2, depth);
|
|
2689 }
|
|
2690
|
|
2691 DEFUN ("equal", Fequal, 2, 2, 0, /*
|
|
2692 Return t if two Lisp objects have similar structure and contents.
|
|
2693 They must have the same data type.
|
|
2694 Conses are compared by comparing the cars and the cdrs.
|
|
2695 Vectors and strings are compared element by element.
|
|
2696 Numbers are compared by value. Symbols must match exactly.
|
|
2697 */
|
444
|
2698 (object1, object2))
|
428
|
2699 {
|
444
|
2700 return internal_equal (object1, object2, 0) ? Qt : Qnil;
|
428
|
2701 }
|
|
2702
|
|
2703 DEFUN ("old-equal", Fold_equal, 2, 2, 0, /*
|
|
2704 Return t if two Lisp objects have similar structure and contents.
|
|
2705 They must have the same data type.
|
|
2706 \(Note, however, that an exception is made for characters and integers;
|
|
2707 this is known as the "char-int confoundance disease." See `eq' and
|
|
2708 `old-eq'.)
|
|
2709 This function is provided only for byte-code compatibility with v19.
|
|
2710 Do not use it.
|
|
2711 */
|
444
|
2712 (object1, object2))
|
428
|
2713 {
|
444
|
2714 return internal_old_equal (object1, object2, 0) ? Qt : Qnil;
|
428
|
2715 }
|
|
2716
|
|
2717
|
|
2718 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
|
434
|
2719 Destructively modify ARRAY by replacing each element with ITEM.
|
428
|
2720 ARRAY is a vector, bit vector, or string.
|
|
2721 */
|
|
2722 (array, item))
|
|
2723 {
|
|
2724 retry:
|
|
2725 if (STRINGP (array))
|
|
2726 {
|
440
|
2727 Lisp_String *s = XSTRING (array);
|
434
|
2728 Bytecount old_bytecount = string_length (s);
|
|
2729 Bytecount new_bytecount;
|
|
2730 Bytecount item_bytecount;
|
|
2731 Bufbyte item_buf[MAX_EMCHAR_LEN];
|
|
2732 Bufbyte *p;
|
|
2733 Bufbyte *end;
|
|
2734
|
428
|
2735 CHECK_CHAR_COERCE_INT (item);
|
|
2736 CHECK_LISP_WRITEABLE (array);
|
434
|
2737
|
|
2738 item_bytecount = set_charptr_emchar (item_buf, XCHAR (item));
|
|
2739 new_bytecount = item_bytecount * string_char_length (s);
|
|
2740
|
|
2741 resize_string (s, -1, new_bytecount - old_bytecount);
|
|
2742
|
|
2743 for (p = string_data (s), end = p + new_bytecount;
|
|
2744 p < end;
|
|
2745 p += item_bytecount)
|
|
2746 memcpy (p, item_buf, item_bytecount);
|
|
2747 *p = '\0';
|
|
2748
|
428
|
2749 bump_string_modiff (array);
|
|
2750 }
|
|
2751 else if (VECTORP (array))
|
|
2752 {
|
|
2753 Lisp_Object *p = XVECTOR_DATA (array);
|
444
|
2754 size_t len = XVECTOR_LENGTH (array);
|
428
|
2755 CHECK_LISP_WRITEABLE (array);
|
|
2756 while (len--)
|
|
2757 *p++ = item;
|
|
2758 }
|
|
2759 else if (BIT_VECTORP (array))
|
|
2760 {
|
440
|
2761 Lisp_Bit_Vector *v = XBIT_VECTOR (array);
|
444
|
2762 size_t len = bit_vector_length (v);
|
428
|
2763 int bit;
|
|
2764 CHECK_BIT (item);
|
444
|
2765 bit = XINT (item);
|
428
|
2766 CHECK_LISP_WRITEABLE (array);
|
|
2767 while (len--)
|
|
2768 set_bit_vector_bit (v, len, bit);
|
|
2769 }
|
|
2770 else
|
|
2771 {
|
|
2772 array = wrong_type_argument (Qarrayp, array);
|
|
2773 goto retry;
|
|
2774 }
|
|
2775 return array;
|
|
2776 }
|
|
2777
|
|
2778 Lisp_Object
|
|
2779 nconc2 (Lisp_Object arg1, Lisp_Object arg2)
|
|
2780 {
|
|
2781 Lisp_Object args[2];
|
|
2782 struct gcpro gcpro1;
|
|
2783 args[0] = arg1;
|
|
2784 args[1] = arg2;
|
|
2785
|
|
2786 GCPRO1 (args[0]);
|
|
2787 gcpro1.nvars = 2;
|
|
2788
|
|
2789 RETURN_UNGCPRO (bytecode_nconc2 (args));
|
|
2790 }
|
|
2791
|
|
2792 Lisp_Object
|
|
2793 bytecode_nconc2 (Lisp_Object *args)
|
|
2794 {
|
|
2795 retry:
|
|
2796
|
|
2797 if (CONSP (args[0]))
|
|
2798 {
|
|
2799 /* (setcdr (last args[0]) args[1]) */
|
|
2800 Lisp_Object tortoise, hare;
|
444
|
2801 size_t count;
|
428
|
2802
|
|
2803 for (hare = tortoise = args[0], count = 0;
|
|
2804 CONSP (XCDR (hare));
|
|
2805 hare = XCDR (hare), count++)
|
|
2806 {
|
|
2807 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
|
|
2808
|
|
2809 if (count & 1)
|
|
2810 tortoise = XCDR (tortoise);
|
|
2811 if (EQ (hare, tortoise))
|
|
2812 signal_circular_list_error (args[0]);
|
|
2813 }
|
|
2814 XCDR (hare) = args[1];
|
|
2815 return args[0];
|
|
2816 }
|
|
2817 else if (NILP (args[0]))
|
|
2818 {
|
|
2819 return args[1];
|
|
2820 }
|
|
2821 else
|
|
2822 {
|
|
2823 args[0] = wrong_type_argument (args[0], Qlistp);
|
|
2824 goto retry;
|
|
2825 }
|
|
2826 }
|
|
2827
|
|
2828 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
|
|
2829 Concatenate any number of lists by altering them.
|
|
2830 Only the last argument is not altered, and need not be a list.
|
|
2831 Also see: `append'.
|
|
2832 If the first argument is nil, there is no way to modify it by side
|
|
2833 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
|
|
2834 changing the value of `foo'.
|
|
2835 */
|
|
2836 (int nargs, Lisp_Object *args))
|
|
2837 {
|
|
2838 int argnum = 0;
|
|
2839 struct gcpro gcpro1;
|
|
2840
|
|
2841 /* The modus operandi in Emacs is "caller gc-protects args".
|
|
2842 However, nconc (particularly nconc2 ()) is called many times
|
|
2843 in Emacs on freshly created stuff (e.g. you see the idiom
|
|
2844 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
|
|
2845 callers out by protecting the args ourselves to save them
|
|
2846 a lot of temporary-variable grief. */
|
|
2847
|
|
2848 GCPRO1 (args[0]);
|
|
2849 gcpro1.nvars = nargs;
|
|
2850
|
|
2851 while (argnum < nargs)
|
|
2852 {
|
|
2853 Lisp_Object val;
|
|
2854 retry:
|
|
2855 val = args[argnum];
|
|
2856 if (CONSP (val))
|
|
2857 {
|
|
2858 /* `val' is the first cons, which will be our return value. */
|
|
2859 /* `last_cons' will be the cons cell to mutate. */
|
|
2860 Lisp_Object last_cons = val;
|
|
2861 Lisp_Object tortoise = val;
|
|
2862
|
|
2863 for (argnum++; argnum < nargs; argnum++)
|
|
2864 {
|
|
2865 Lisp_Object next = args[argnum];
|
|
2866 retry_next:
|
|
2867 if (CONSP (next) || argnum == nargs -1)
|
|
2868 {
|
|
2869 /* (setcdr (last val) next) */
|
444
|
2870 size_t count;
|
428
|
2871
|
|
2872 for (count = 0;
|
|
2873 CONSP (XCDR (last_cons));
|
|
2874 last_cons = XCDR (last_cons), count++)
|
|
2875 {
|
|
2876 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
|
|
2877
|
|
2878 if (count & 1)
|
|
2879 tortoise = XCDR (tortoise);
|
|
2880 if (EQ (last_cons, tortoise))
|
|
2881 signal_circular_list_error (args[argnum-1]);
|
|
2882 }
|
|
2883 XCDR (last_cons) = next;
|
|
2884 }
|
|
2885 else if (NILP (next))
|
|
2886 {
|
|
2887 continue;
|
|
2888 }
|
|
2889 else
|
|
2890 {
|
|
2891 next = wrong_type_argument (Qlistp, next);
|
|
2892 goto retry_next;
|
|
2893 }
|
|
2894 }
|
|
2895 RETURN_UNGCPRO (val);
|
|
2896 }
|
|
2897 else if (NILP (val))
|
|
2898 argnum++;
|
|
2899 else if (argnum == nargs - 1) /* last arg? */
|
|
2900 RETURN_UNGCPRO (val);
|
|
2901 else
|
|
2902 {
|
|
2903 args[argnum] = wrong_type_argument (Qlistp, val);
|
|
2904 goto retry;
|
|
2905 }
|
|
2906 }
|
|
2907 RETURN_UNGCPRO (Qnil); /* No non-nil args provided. */
|
|
2908 }
|
|
2909
|
|
2910
|
434
|
2911 /* This is the guts of several mapping functions.
|
|
2912 Apply FUNCTION to each element of SEQUENCE, one by one,
|
|
2913 storing the results into elements of VALS, a C vector of Lisp_Objects.
|
|
2914 LENI is the length of VALS, which should also be the length of SEQUENCE.
|
428
|
2915
|
|
2916 If VALS is a null pointer, do not accumulate the results. */
|
|
2917
|
|
2918 static void
|
434
|
2919 mapcar1 (size_t leni, Lisp_Object *vals,
|
|
2920 Lisp_Object function, Lisp_Object sequence)
|
428
|
2921 {
|
|
2922 Lisp_Object result;
|
|
2923 Lisp_Object args[2];
|
|
2924 struct gcpro gcpro1;
|
|
2925
|
|
2926 if (vals)
|
|
2927 {
|
|
2928 GCPRO1 (vals[0]);
|
|
2929 gcpro1.nvars = 0;
|
|
2930 }
|
|
2931
|
434
|
2932 args[0] = function;
|
|
2933
|
|
2934 if (LISTP (sequence))
|
428
|
2935 {
|
434
|
2936 /* A devious `function' could either:
|
|
2937 - insert garbage into the list in front of us, causing XCDR to crash
|
|
2938 - amputate the list behind us using (setcdr), causing the remaining
|
|
2939 elts to lose their GCPRO status.
|
|
2940
|
|
2941 if (vals != 0) we avoid this by copying the elts into the
|
|
2942 `vals' array. By a stroke of luck, `vals' is exactly large
|
|
2943 enough to hold the elts left to be traversed as well as the
|
|
2944 results computed so far.
|
|
2945
|
|
2946 if (vals == 0) we don't have any free space available and
|
|
2947 don't want to eat up any more stack with alloca().
|
442
|
2948 So we use EXTERNAL_LIST_LOOP_3_NO_DECLARE and GCPRO the tail. */
|
434
|
2949
|
|
2950 if (vals)
|
428
|
2951 {
|
434
|
2952 Lisp_Object *val = vals;
|
444
|
2953 size_t i;
|
434
|
2954
|
|
2955 LIST_LOOP_2 (elt, sequence)
|
|
2956 *val++ = elt;
|
|
2957
|
|
2958 gcpro1.nvars = leni;
|
|
2959
|
|
2960 for (i = 0; i < leni; i++)
|
|
2961 {
|
|
2962 args[1] = vals[i];
|
|
2963 vals[i] = Ffuncall (2, args);
|
|
2964 }
|
|
2965 }
|
|
2966 else
|
|
2967 {
|
|
2968 Lisp_Object elt, tail;
|
442
|
2969 EMACS_INT len_unused;
|
434
|
2970 struct gcpro ngcpro1;
|
|
2971
|
|
2972 NGCPRO1 (tail);
|
|
2973
|
|
2974 {
|
442
|
2975 EXTERNAL_LIST_LOOP_4_NO_DECLARE (elt, sequence, tail, len_unused)
|
434
|
2976 {
|
|
2977 args[1] = elt;
|
|
2978 Ffuncall (2, args);
|
|
2979 }
|
|
2980 }
|
|
2981
|
|
2982 NUNGCPRO;
|
428
|
2983 }
|
|
2984 }
|
434
|
2985 else if (VECTORP (sequence))
|
428
|
2986 {
|
434
|
2987 Lisp_Object *objs = XVECTOR_DATA (sequence);
|
444
|
2988 size_t i;
|
428
|
2989 for (i = 0; i < leni; i++)
|
|
2990 {
|
|
2991 args[1] = *objs++;
|
|
2992 result = Ffuncall (2, args);
|
|
2993 if (vals) vals[gcpro1.nvars++] = result;
|
|
2994 }
|
|
2995 }
|
434
|
2996 else if (STRINGP (sequence))
|
428
|
2997 {
|
434
|
2998 /* The string data of `sequence' might be relocated during GC. */
|
|
2999 Bytecount slen = XSTRING_LENGTH (sequence);
|
|
3000 Bufbyte *p = alloca_array (Bufbyte, slen);
|
|
3001 Bufbyte *end = p + slen;
|
|
3002
|
|
3003 memcpy (p, XSTRING_DATA (sequence), slen);
|
|
3004
|
|
3005 while (p < end)
|
428
|
3006 {
|
|
3007 args[1] = make_char (charptr_emchar (p));
|
|
3008 INC_CHARPTR (p);
|
|
3009 result = Ffuncall (2, args);
|
|
3010 if (vals) vals[gcpro1.nvars++] = result;
|
|
3011 }
|
|
3012 }
|
434
|
3013 else if (BIT_VECTORP (sequence))
|
428
|
3014 {
|
440
|
3015 Lisp_Bit_Vector *v = XBIT_VECTOR (sequence);
|
444
|
3016 size_t i;
|
428
|
3017 for (i = 0; i < leni; i++)
|
|
3018 {
|
|
3019 args[1] = make_int (bit_vector_bit (v, i));
|
|
3020 result = Ffuncall (2, args);
|
|
3021 if (vals) vals[gcpro1.nvars++] = result;
|
|
3022 }
|
|
3023 }
|
|
3024 else
|
442
|
3025 abort (); /* unreachable, since Flength (sequence) did not get an error */
|
428
|
3026
|
|
3027 if (vals)
|
|
3028 UNGCPRO;
|
|
3029 }
|
|
3030
|
|
3031 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
|
434
|
3032 Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
|
|
3033 In between each pair of results, insert SEPARATOR. Thus, using " " as
|
|
3034 SEPARATOR results in spaces between the values returned by FUNCTION.
|
|
3035 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
428
|
3036 */
|
434
|
3037 (function, sequence, separator))
|
428
|
3038 {
|
444
|
3039 EMACS_INT len = XINT (Flength (sequence));
|
428
|
3040 Lisp_Object *args;
|
444
|
3041 EMACS_INT i;
|
|
3042 EMACS_INT nargs = len + len - 1;
|
428
|
3043
|
442
|
3044 if (len == 0) return build_string ("");
|
428
|
3045
|
|
3046 args = alloca_array (Lisp_Object, nargs);
|
|
3047
|
434
|
3048 mapcar1 (len, args, function, sequence);
|
428
|
3049
|
|
3050 for (i = len - 1; i >= 0; i--)
|
|
3051 args[i + i] = args[i];
|
|
3052
|
|
3053 for (i = 1; i < nargs; i += 2)
|
434
|
3054 args[i] = separator;
|
428
|
3055
|
|
3056 return Fconcat (nargs, args);
|
|
3057 }
|
|
3058
|
|
3059 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
|
434
|
3060 Apply FUNCTION to each element of SEQUENCE; return a list of the results.
|
|
3061 The result is a list of the same length as SEQUENCE.
|
428
|
3062 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
|
3063 */
|
434
|
3064 (function, sequence))
|
428
|
3065 {
|
434
|
3066 size_t len = XINT (Flength (sequence));
|
428
|
3067 Lisp_Object *args = alloca_array (Lisp_Object, len);
|
|
3068
|
434
|
3069 mapcar1 (len, args, function, sequence);
|
428
|
3070
|
|
3071 return Flist (len, args);
|
|
3072 }
|
|
3073
|
|
3074 DEFUN ("mapvector", Fmapvector, 2, 2, 0, /*
|
434
|
3075 Apply FUNCTION to each element of SEQUENCE; return a vector of the results.
|
428
|
3076 The result is a vector of the same length as SEQUENCE.
|
434
|
3077 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
428
|
3078 */
|
434
|
3079 (function, sequence))
|
428
|
3080 {
|
434
|
3081 size_t len = XINT (Flength (sequence));
|
428
|
3082 Lisp_Object result = make_vector (len, Qnil);
|
|
3083 struct gcpro gcpro1;
|
|
3084
|
|
3085 GCPRO1 (result);
|
434
|
3086 mapcar1 (len, XVECTOR_DATA (result), function, sequence);
|
428
|
3087 UNGCPRO;
|
|
3088
|
|
3089 return result;
|
|
3090 }
|
|
3091
|
|
3092 DEFUN ("mapc-internal", Fmapc_internal, 2, 2, 0, /*
|
|
3093 Apply FUNCTION to each element of SEQUENCE.
|
|
3094 SEQUENCE may be a list, a vector, a bit vector, or a string.
|
|
3095 This function is like `mapcar' but does not accumulate the results,
|
|
3096 which is more efficient if you do not use the results.
|
|
3097
|
|
3098 The difference between this and `mapc' is that `mapc' supports all
|
|
3099 the spiffy Common Lisp arguments. You should normally use `mapc'.
|
|
3100 */
|
434
|
3101 (function, sequence))
|
428
|
3102 {
|
434
|
3103 mapcar1 (XINT (Flength (sequence)), 0, function, sequence);
|
|
3104
|
|
3105 return sequence;
|
428
|
3106 }
|
|
3107
|
|
3108
|
442
|
3109
|
|
3110
|
|
3111 DEFUN ("replace-list", Freplace_list, 2, 2, 0, /*
|
|
3112 Destructively replace the list OLD with NEW.
|
|
3113 This is like (copy-sequence NEW) except that it reuses the
|
|
3114 conses in OLD as much as possible. If OLD and NEW are the same
|
|
3115 length, no consing will take place.
|
|
3116 */
|
|
3117 (old, new))
|
|
3118 {
|
|
3119 Lisp_Object tail, oldtail = old, prevoldtail = Qnil;
|
|
3120
|
|
3121 EXTERNAL_LIST_LOOP (tail, new)
|
|
3122 {
|
|
3123 if (!NILP (oldtail))
|
|
3124 {
|
|
3125 CHECK_CONS (oldtail);
|
|
3126 XCAR (oldtail) = XCAR (tail);
|
|
3127 }
|
|
3128 else if (!NILP (prevoldtail))
|
|
3129 {
|
|
3130 XCDR (prevoldtail) = Fcons (XCAR (tail), Qnil);
|
|
3131 prevoldtail = XCDR (prevoldtail);
|
|
3132 }
|
|
3133 else
|
|
3134 old = oldtail = Fcons (XCAR (tail), Qnil);
|
|
3135
|
|
3136 if (!NILP (oldtail))
|
|
3137 {
|
|
3138 prevoldtail = oldtail;
|
|
3139 oldtail = XCDR (oldtail);
|
|
3140 }
|
|
3141 }
|
|
3142
|
|
3143 if (!NILP (prevoldtail))
|
|
3144 XCDR (prevoldtail) = Qnil;
|
|
3145 else
|
|
3146 old = Qnil;
|
|
3147
|
|
3148 return old;
|
|
3149 }
|
|
3150
|
|
3151
|
428
|
3152 /* #### this function doesn't belong in this file! */
|
|
3153
|
442
|
3154 #ifdef HAVE_GETLOADAVG
|
|
3155 #ifdef HAVE_SYS_LOADAVG_H
|
|
3156 #include <sys/loadavg.h>
|
|
3157 #endif
|
|
3158 #else
|
|
3159 int getloadavg (double loadavg[], int nelem); /* Defined in getloadavg.c */
|
|
3160 #endif
|
|
3161
|
428
|
3162 DEFUN ("load-average", Fload_average, 0, 1, 0, /*
|
|
3163 Return list of 1 minute, 5 minute and 15 minute load averages.
|
|
3164 Each of the three load averages is multiplied by 100,
|
|
3165 then converted to integer.
|
|
3166
|
|
3167 When USE-FLOATS is non-nil, floats will be used instead of integers.
|
|
3168 These floats are not multiplied by 100.
|
|
3169
|
|
3170 If the 5-minute or 15-minute load averages are not available, return a
|
|
3171 shortened list, containing only those averages which are available.
|
|
3172
|
|
3173 On some systems, this won't work due to permissions on /dev/kmem,
|
|
3174 in which case you can't use this.
|
|
3175 */
|
|
3176 (use_floats))
|
|
3177 {
|
|
3178 double load_ave[3];
|
|
3179 int loads = getloadavg (load_ave, countof (load_ave));
|
|
3180 Lisp_Object ret = Qnil;
|
|
3181
|
|
3182 if (loads == -2)
|
563
|
3183 signal_error (Qunimplemented,
|
|
3184 "load-average not implemented for this operating system",
|
|
3185 Qunbound);
|
428
|
3186 else if (loads < 0)
|
563
|
3187 invalid_operation ("Could not get load-average", lisp_strerror (errno));
|
428
|
3188
|
|
3189 while (loads-- > 0)
|
|
3190 {
|
|
3191 Lisp_Object load = (NILP (use_floats) ?
|
|
3192 make_int ((int) (100.0 * load_ave[loads]))
|
|
3193 : make_float (load_ave[loads]));
|
|
3194 ret = Fcons (load, ret);
|
|
3195 }
|
|
3196 return ret;
|
|
3197 }
|
|
3198
|
|
3199
|
|
3200 Lisp_Object Vfeatures;
|
|
3201
|
|
3202 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
|
|
3203 Return non-nil if feature FEXP is present in this Emacs.
|
|
3204 Use this to conditionalize execution of lisp code based on the
|
|
3205 presence or absence of emacs or environment extensions.
|
|
3206 FEXP can be a symbol, a number, or a list.
|
|
3207 If it is a symbol, that symbol is looked up in the `features' variable,
|
|
3208 and non-nil will be returned if found.
|
|
3209 If it is a number, the function will return non-nil if this Emacs
|
|
3210 has an equal or greater version number than FEXP.
|
|
3211 If it is a list whose car is the symbol `and', it will return
|
|
3212 non-nil if all the features in its cdr are non-nil.
|
|
3213 If it is a list whose car is the symbol `or', it will return non-nil
|
|
3214 if any of the features in its cdr are non-nil.
|
|
3215 If it is a list whose car is the symbol `not', it will return
|
|
3216 non-nil if the feature is not present.
|
|
3217
|
|
3218 Examples:
|
|
3219
|
|
3220 (featurep 'xemacs)
|
|
3221 => ; Non-nil on XEmacs.
|
|
3222
|
|
3223 (featurep '(and xemacs gnus))
|
|
3224 => ; Non-nil on XEmacs with Gnus loaded.
|
|
3225
|
|
3226 (featurep '(or tty-frames (and emacs 19.30)))
|
|
3227 => ; Non-nil if this Emacs supports TTY frames.
|
|
3228
|
|
3229 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
|
|
3230 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
|
|
3231
|
442
|
3232 (featurep '(and xemacs 21.02))
|
|
3233 => ; Non-nil on XEmacs 21.2 and later.
|
|
3234
|
428
|
3235 NOTE: The advanced arguments of this function (anything other than a
|
|
3236 symbol) are not yet supported by FSF Emacs. If you feel they are useful
|
|
3237 for supporting multiple Emacs variants, lobby Richard Stallman at
|
442
|
3238 <bug-gnu-emacs@gnu.org>.
|
428
|
3239 */
|
|
3240 (fexp))
|
|
3241 {
|
|
3242 #ifndef FEATUREP_SYNTAX
|
|
3243 CHECK_SYMBOL (fexp);
|
|
3244 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
|
|
3245 #else /* FEATUREP_SYNTAX */
|
|
3246 static double featurep_emacs_version;
|
|
3247
|
|
3248 /* Brute force translation from Erik Naggum's lisp function. */
|
|
3249 if (SYMBOLP (fexp))
|
|
3250 {
|
|
3251 /* Original definition */
|
|
3252 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
|
|
3253 }
|
|
3254 else if (INTP (fexp) || FLOATP (fexp))
|
|
3255 {
|
|
3256 double d = extract_float (fexp);
|
|
3257
|
|
3258 if (featurep_emacs_version == 0.0)
|
|
3259 {
|
|
3260 featurep_emacs_version = XINT (Vemacs_major_version) +
|
|
3261 (XINT (Vemacs_minor_version) / 100.0);
|
|
3262 }
|
|
3263 return featurep_emacs_version >= d ? Qt : Qnil;
|
|
3264 }
|
|
3265 else if (CONSP (fexp))
|
|
3266 {
|
|
3267 Lisp_Object tem = XCAR (fexp);
|
|
3268 if (EQ (tem, Qnot))
|
|
3269 {
|
|
3270 Lisp_Object negate;
|
|
3271
|
|
3272 tem = XCDR (fexp);
|
|
3273 negate = Fcar (tem);
|
|
3274 if (!NILP (tem))
|
|
3275 return NILP (call1 (Qfeaturep, negate)) ? Qt : Qnil;
|
|
3276 else
|
|
3277 return Fsignal (Qinvalid_read_syntax, list1 (tem));
|
|
3278 }
|
|
3279 else if (EQ (tem, Qand))
|
|
3280 {
|
|
3281 tem = XCDR (fexp);
|
|
3282 /* Use Fcar/Fcdr for error-checking. */
|
|
3283 while (!NILP (tem) && !NILP (call1 (Qfeaturep, Fcar (tem))))
|
|
3284 {
|
|
3285 tem = Fcdr (tem);
|
|
3286 }
|
|
3287 return NILP (tem) ? Qt : Qnil;
|
|
3288 }
|
|
3289 else if (EQ (tem, Qor))
|
|
3290 {
|
|
3291 tem = XCDR (fexp);
|
|
3292 /* Use Fcar/Fcdr for error-checking. */
|
|
3293 while (!NILP (tem) && NILP (call1 (Qfeaturep, Fcar (tem))))
|
|
3294 {
|
|
3295 tem = Fcdr (tem);
|
|
3296 }
|
|
3297 return NILP (tem) ? Qnil : Qt;
|
|
3298 }
|
|
3299 else
|
|
3300 {
|
|
3301 return Fsignal (Qinvalid_read_syntax, list1 (XCDR (fexp)));
|
|
3302 }
|
|
3303 }
|
|
3304 else
|
|
3305 {
|
|
3306 return Fsignal (Qinvalid_read_syntax, list1 (fexp));
|
|
3307 }
|
|
3308 }
|
|
3309 #endif /* FEATUREP_SYNTAX */
|
|
3310
|
|
3311 DEFUN ("provide", Fprovide, 1, 1, 0, /*
|
|
3312 Announce that FEATURE is a feature of the current Emacs.
|
|
3313 This function updates the value of the variable `features'.
|
|
3314 */
|
|
3315 (feature))
|
|
3316 {
|
|
3317 Lisp_Object tem;
|
|
3318 CHECK_SYMBOL (feature);
|
|
3319 if (!NILP (Vautoload_queue))
|
|
3320 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
|
|
3321 tem = Fmemq (feature, Vfeatures);
|
|
3322 if (NILP (tem))
|
|
3323 Vfeatures = Fcons (feature, Vfeatures);
|
|
3324 LOADHIST_ATTACH (Fcons (Qprovide, feature));
|
|
3325 return feature;
|
|
3326 }
|
|
3327
|
|
3328 DEFUN ("require", Frequire, 1, 2, 0, /*
|
|
3329 If feature FEATURE is not loaded, load it from FILENAME.
|
|
3330 If FEATURE is not a member of the list `features', then the feature
|
|
3331 is not loaded; so load the file FILENAME.
|
|
3332 If FILENAME is omitted, the printname of FEATURE is used as the file name.
|
|
3333 */
|
444
|
3334 (feature, filename))
|
428
|
3335 {
|
|
3336 Lisp_Object tem;
|
|
3337 CHECK_SYMBOL (feature);
|
|
3338 tem = Fmemq (feature, Vfeatures);
|
|
3339 LOADHIST_ATTACH (Fcons (Qrequire, feature));
|
|
3340 if (!NILP (tem))
|
|
3341 return feature;
|
|
3342 else
|
|
3343 {
|
|
3344 int speccount = specpdl_depth ();
|
|
3345
|
|
3346 /* Value saved here is to be restored into Vautoload_queue */
|
|
3347 record_unwind_protect (un_autoload, Vautoload_queue);
|
|
3348 Vautoload_queue = Qt;
|
|
3349
|
444
|
3350 call4 (Qload, NILP (filename) ? Fsymbol_name (feature) : filename,
|
428
|
3351 Qnil, Qt, Qnil);
|
|
3352
|
|
3353 tem = Fmemq (feature, Vfeatures);
|
|
3354 if (NILP (tem))
|
563
|
3355 invalid_state ("Required feature was not provided", feature);
|
428
|
3356
|
|
3357 /* Once loading finishes, don't undo it. */
|
|
3358 Vautoload_queue = Qt;
|
|
3359 return unbind_to (speccount, feature);
|
|
3360 }
|
|
3361 }
|
|
3362
|
|
3363 /* base64 encode/decode functions.
|
|
3364
|
|
3365 Originally based on code from GNU recode. Ported to FSF Emacs by
|
|
3366 Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
|
|
3367 subsequently heavily hacked by Hrvoje Niksic. */
|
|
3368
|
|
3369 #define MIME_LINE_LENGTH 72
|
|
3370
|
|
3371 #define IS_ASCII(Character) \
|
|
3372 ((Character) < 128)
|
|
3373 #define IS_BASE64(Character) \
|
|
3374 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
|
|
3375
|
|
3376 /* Table of characters coding the 64 values. */
|
|
3377 static char base64_value_to_char[64] =
|
|
3378 {
|
|
3379 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
|
|
3380 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
|
|
3381 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
|
|
3382 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
|
|
3383 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
|
|
3384 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
|
|
3385 '8', '9', '+', '/' /* 60-63 */
|
|
3386 };
|
|
3387
|
|
3388 /* Table of base64 values for first 128 characters. */
|
|
3389 static short base64_char_to_value[128] =
|
|
3390 {
|
|
3391 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
|
|
3392 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
|
|
3393 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
|
|
3394 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
|
|
3395 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
|
|
3396 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
|
|
3397 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
|
|
3398 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
|
|
3399 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
|
|
3400 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
|
|
3401 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
|
|
3402 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
|
|
3403 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
|
|
3404 };
|
|
3405
|
|
3406 /* The following diagram shows the logical steps by which three octets
|
|
3407 get transformed into four base64 characters.
|
|
3408
|
|
3409 .--------. .--------. .--------.
|
|
3410 |aaaaaabb| |bbbbcccc| |ccdddddd|
|
|
3411 `--------' `--------' `--------'
|
|
3412 6 2 4 4 2 6
|
|
3413 .--------+--------+--------+--------.
|
|
3414 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
|
|
3415 `--------+--------+--------+--------'
|
|
3416
|
|
3417 .--------+--------+--------+--------.
|
|
3418 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
|
|
3419 `--------+--------+--------+--------'
|
|
3420
|
|
3421 The octets are divided into 6 bit chunks, which are then encoded into
|
|
3422 base64 characters. */
|
|
3423
|
563
|
3424 DOESNT_RETURN
|
|
3425 base64_conversion_error (const char *reason, Lisp_Object frob)
|
|
3426 {
|
|
3427 signal_error (Qbase64_conversion_error, reason, frob);
|
|
3428 }
|
|
3429
|
|
3430 #define ADVANCE_INPUT(c, stream) \
|
|
3431 ((ec = Lstream_get_emchar (stream)) == -1 ? 0 : \
|
|
3432 ((ec > 255) ? \
|
|
3433 (base64_conversion_error ("Non-ascii character in base64 input", \
|
|
3434 make_char (ec)), 0) \
|
428
|
3435 : (c = (Bufbyte)ec), 1))
|
|
3436
|
|
3437 static Bytind
|
|
3438 base64_encode_1 (Lstream *istream, Bufbyte *to, int line_break)
|
|
3439 {
|
|
3440 EMACS_INT counter = 0;
|
|
3441 Bufbyte *e = to;
|
|
3442 Emchar ec;
|
|
3443 unsigned int value;
|
|
3444
|
|
3445 while (1)
|
|
3446 {
|
|
3447 Bufbyte c;
|
|
3448 if (!ADVANCE_INPUT (c, istream))
|
|
3449 break;
|
|
3450
|
|
3451 /* Wrap line every 76 characters. */
|
|
3452 if (line_break)
|
|
3453 {
|
|
3454 if (counter < MIME_LINE_LENGTH / 4)
|
|
3455 counter++;
|
|
3456 else
|
|
3457 {
|
|
3458 *e++ = '\n';
|
|
3459 counter = 1;
|
|
3460 }
|
|
3461 }
|
|
3462
|
|
3463 /* Process first byte of a triplet. */
|
|
3464 *e++ = base64_value_to_char[0x3f & c >> 2];
|
|
3465 value = (0x03 & c) << 4;
|
|
3466
|
|
3467 /* Process second byte of a triplet. */
|
|
3468 if (!ADVANCE_INPUT (c, istream))
|
|
3469 {
|
|
3470 *e++ = base64_value_to_char[value];
|
|
3471 *e++ = '=';
|
|
3472 *e++ = '=';
|
|
3473 break;
|
|
3474 }
|
|
3475
|
|
3476 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
|
|
3477 value = (0x0f & c) << 2;
|
|
3478
|
|
3479 /* Process third byte of a triplet. */
|
|
3480 if (!ADVANCE_INPUT (c, istream))
|
|
3481 {
|
|
3482 *e++ = base64_value_to_char[value];
|
|
3483 *e++ = '=';
|
|
3484 break;
|
|
3485 }
|
|
3486
|
|
3487 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
|
|
3488 *e++ = base64_value_to_char[0x3f & c];
|
|
3489 }
|
|
3490
|
|
3491 return e - to;
|
|
3492 }
|
|
3493 #undef ADVANCE_INPUT
|
|
3494
|
|
3495 /* Get next character from the stream, except that non-base64
|
|
3496 characters are ignored. This is in accordance with rfc2045. EC
|
|
3497 should be an Emchar, so that it can hold -1 as the value for EOF. */
|
|
3498 #define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
|
|
3499 ec = Lstream_get_emchar (stream); \
|
|
3500 ++streampos; \
|
|
3501 /* IS_BASE64 may not be called with negative arguments so check for \
|
|
3502 EOF first. */ \
|
|
3503 if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
|
|
3504 break; \
|
|
3505 } while (1)
|
|
3506
|
|
3507 #define STORE_BYTE(pos, val, ccnt) do { \
|
|
3508 pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
|
|
3509 ++ccnt; \
|
|
3510 } while (0)
|
|
3511
|
|
3512 static Bytind
|
|
3513 base64_decode_1 (Lstream *istream, Bufbyte *to, Charcount *ccptr)
|
|
3514 {
|
|
3515 Charcount ccnt = 0;
|
|
3516 Bufbyte *e = to;
|
|
3517 EMACS_INT streampos = 0;
|
|
3518
|
|
3519 while (1)
|
|
3520 {
|
|
3521 Emchar ec;
|
|
3522 unsigned long value;
|
|
3523
|
|
3524 /* Process first byte of a quadruplet. */
|
|
3525 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
|
|
3526 if (ec < 0)
|
|
3527 break;
|
|
3528 if (ec == '=')
|
563
|
3529 base64_conversion_error ("Illegal `=' character while decoding base64",
|
|
3530 make_int (streampos));
|
428
|
3531 value = base64_char_to_value[ec] << 18;
|
|
3532
|
|
3533 /* Process second byte of a quadruplet. */
|
|
3534 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
|
|
3535 if (ec < 0)
|
563
|
3536 base64_conversion_error ("Premature EOF while decoding base64",
|
|
3537 Qunbound);
|
428
|
3538 if (ec == '=')
|
563
|
3539 base64_conversion_error ("Illegal `=' character while decoding base64",
|
|
3540 make_int (streampos));
|
428
|
3541 value |= base64_char_to_value[ec] << 12;
|
|
3542 STORE_BYTE (e, value >> 16, ccnt);
|
|
3543
|
|
3544 /* Process third byte of a quadruplet. */
|
|
3545 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
|
|
3546 if (ec < 0)
|
563
|
3547 base64_conversion_error ("Premature EOF while decoding base64",
|
|
3548 Qunbound);
|
428
|
3549
|
|
3550 if (ec == '=')
|
|
3551 {
|
|
3552 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
|
|
3553 if (ec < 0)
|
563
|
3554 base64_conversion_error ("Premature EOF while decoding base64",
|
|
3555 Qunbound);
|
428
|
3556 if (ec != '=')
|
563
|
3557 base64_conversion_error
|
|
3558 ("Padding `=' expected but not found while decoding base64",
|
|
3559 make_int (streampos));
|
428
|
3560 continue;
|
|
3561 }
|
|
3562
|
|
3563 value |= base64_char_to_value[ec] << 6;
|
|
3564 STORE_BYTE (e, 0xff & value >> 8, ccnt);
|
|
3565
|
|
3566 /* Process fourth byte of a quadruplet. */
|
|
3567 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
|
|
3568 if (ec < 0)
|
563
|
3569 base64_conversion_error ("Premature EOF while decoding base64",
|
|
3570 Qunbound);
|
428
|
3571 if (ec == '=')
|
|
3572 continue;
|
|
3573
|
|
3574 value |= base64_char_to_value[ec];
|
|
3575 STORE_BYTE (e, 0xff & value, ccnt);
|
|
3576 }
|
|
3577
|
|
3578 *ccptr = ccnt;
|
|
3579 return e - to;
|
|
3580 }
|
|
3581 #undef ADVANCE_INPUT
|
|
3582 #undef ADVANCE_INPUT_IGNORE_NONBASE64
|
|
3583 #undef STORE_BYTE
|
|
3584
|
|
3585 static Lisp_Object
|
|
3586 free_malloced_ptr (Lisp_Object unwind_obj)
|
|
3587 {
|
|
3588 void *ptr = (void *)get_opaque_ptr (unwind_obj);
|
|
3589 xfree (ptr);
|
|
3590 free_opaque_ptr (unwind_obj);
|
|
3591 return Qnil;
|
|
3592 }
|
|
3593
|
|
3594 /* Don't use alloca for regions larger than this, lest we overflow
|
|
3595 the stack. */
|
|
3596 #define MAX_ALLOCA 65536
|
|
3597
|
|
3598 /* We need to setup proper unwinding, because there is a number of
|
|
3599 ways these functions can blow up, and we don't want to have memory
|
|
3600 leaks in those cases. */
|
|
3601 #define XMALLOC_OR_ALLOCA(ptr, len, type) do { \
|
|
3602 size_t XOA_len = (len); \
|
|
3603 if (XOA_len > MAX_ALLOCA) \
|
|
3604 { \
|
|
3605 ptr = xnew_array (type, XOA_len); \
|
|
3606 record_unwind_protect (free_malloced_ptr, \
|
|
3607 make_opaque_ptr ((void *)ptr)); \
|
|
3608 } \
|
|
3609 else \
|
|
3610 ptr = alloca_array (type, XOA_len); \
|
|
3611 } while (0)
|
|
3612
|
|
3613 #define XMALLOC_UNBIND(ptr, len, speccount) do { \
|
|
3614 if ((len) > MAX_ALLOCA) \
|
|
3615 unbind_to (speccount, Qnil); \
|
|
3616 } while (0)
|
|
3617
|
|
3618 DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
|
444
|
3619 Base64-encode the region between START and END.
|
428
|
3620 Return the length of the encoded text.
|
|
3621 Optional third argument NO-LINE-BREAK means do not break long lines
|
|
3622 into shorter lines.
|
|
3623 */
|
444
|
3624 (start, end, no_line_break))
|
428
|
3625 {
|
|
3626 Bufbyte *encoded;
|
|
3627 Bytind encoded_length;
|
|
3628 Charcount allength, length;
|
|
3629 struct buffer *buf = current_buffer;
|
|
3630 Bufpos begv, zv, old_pt = BUF_PT (buf);
|
|
3631 Lisp_Object input;
|
|
3632 int speccount = specpdl_depth();
|
|
3633
|
444
|
3634 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
|
428
|
3635 barf_if_buffer_read_only (buf, begv, zv);
|
|
3636
|
|
3637 /* We need to allocate enough room for encoding the text.
|
|
3638 We need 33 1/3% more space, plus a newline every 76
|
|
3639 characters, and then we round up. */
|
|
3640 length = zv - begv;
|
|
3641 allength = length + length/3 + 1;
|
|
3642 allength += allength / MIME_LINE_LENGTH + 1 + 6;
|
|
3643
|
|
3644 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
|
|
3645 /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
|
|
3646 base64 characters will be single-byte. */
|
|
3647 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
|
|
3648 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
|
|
3649 NILP (no_line_break));
|
|
3650 if (encoded_length > allength)
|
|
3651 abort ();
|
|
3652 Lstream_delete (XLSTREAM (input));
|
|
3653
|
|
3654 /* Now we have encoded the region, so we insert the new contents
|
|
3655 and delete the old. (Insert first in order to preserve markers.) */
|
|
3656 buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
|
|
3657 XMALLOC_UNBIND (encoded, allength, speccount);
|
|
3658 buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
|
|
3659
|
|
3660 /* Simulate FSF Emacs implementation of this function: if point was
|
|
3661 in the region, place it at the beginning. */
|
|
3662 if (old_pt >= begv && old_pt < zv)
|
|
3663 BUF_SET_PT (buf, begv);
|
|
3664
|
|
3665 /* We return the length of the encoded text. */
|
|
3666 return make_int (encoded_length);
|
|
3667 }
|
|
3668
|
|
3669 DEFUN ("base64-encode-string", Fbase64_encode_string, 1, 2, 0, /*
|
|
3670 Base64 encode STRING and return the result.
|
444
|
3671 Optional argument NO-LINE-BREAK means do not break long lines
|
|
3672 into shorter lines.
|
428
|
3673 */
|
|
3674 (string, no_line_break))
|
|
3675 {
|
|
3676 Charcount allength, length;
|
|
3677 Bytind encoded_length;
|
|
3678 Bufbyte *encoded;
|
|
3679 Lisp_Object input, result;
|
|
3680 int speccount = specpdl_depth();
|
|
3681
|
|
3682 CHECK_STRING (string);
|
|
3683
|
|
3684 length = XSTRING_CHAR_LENGTH (string);
|
|
3685 allength = length + length/3 + 1;
|
|
3686 allength += allength / MIME_LINE_LENGTH + 1 + 6;
|
|
3687
|
|
3688 input = make_lisp_string_input_stream (string, 0, -1);
|
|
3689 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
|
|
3690 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
|
|
3691 NILP (no_line_break));
|
|
3692 if (encoded_length > allength)
|
|
3693 abort ();
|
|
3694 Lstream_delete (XLSTREAM (input));
|
|
3695 result = make_string (encoded, encoded_length);
|
|
3696 XMALLOC_UNBIND (encoded, allength, speccount);
|
|
3697 return result;
|
|
3698 }
|
|
3699
|
|
3700 DEFUN ("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
|
444
|
3701 Base64-decode the region between START and END.
|
428
|
3702 Return the length of the decoded text.
|
|
3703 If the region can't be decoded, return nil and don't modify the buffer.
|
|
3704 Characters out of the base64 alphabet are ignored.
|
|
3705 */
|
444
|
3706 (start, end))
|
428
|
3707 {
|
|
3708 struct buffer *buf = current_buffer;
|
|
3709 Bufpos begv, zv, old_pt = BUF_PT (buf);
|
|
3710 Bufbyte *decoded;
|
|
3711 Bytind decoded_length;
|
|
3712 Charcount length, cc_decoded_length;
|
|
3713 Lisp_Object input;
|
|
3714 int speccount = specpdl_depth();
|
|
3715
|
444
|
3716 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
|
428
|
3717 barf_if_buffer_read_only (buf, begv, zv);
|
|
3718
|
|
3719 length = zv - begv;
|
|
3720
|
|
3721 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
|
|
3722 /* We need to allocate enough room for decoding the text. */
|
|
3723 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
|
|
3724 decoded_length = base64_decode_1 (XLSTREAM (input), decoded, &cc_decoded_length);
|
|
3725 if (decoded_length > length * MAX_EMCHAR_LEN)
|
|
3726 abort ();
|
|
3727 Lstream_delete (XLSTREAM (input));
|
|
3728
|
|
3729 /* Now we have decoded the region, so we insert the new contents
|
|
3730 and delete the old. (Insert first in order to preserve markers.) */
|
|
3731 BUF_SET_PT (buf, begv);
|
|
3732 buffer_insert_raw_string_1 (buf, begv, decoded, decoded_length, 0);
|
|
3733 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
|
|
3734 buffer_delete_range (buf, begv + cc_decoded_length,
|
|
3735 zv + cc_decoded_length, 0);
|
|
3736
|
|
3737 /* Simulate FSF Emacs implementation of this function: if point was
|
|
3738 in the region, place it at the beginning. */
|
|
3739 if (old_pt >= begv && old_pt < zv)
|
|
3740 BUF_SET_PT (buf, begv);
|
|
3741
|
|
3742 return make_int (cc_decoded_length);
|
|
3743 }
|
|
3744
|
|
3745 DEFUN ("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
|
|
3746 Base64-decode STRING and return the result.
|
|
3747 Characters out of the base64 alphabet are ignored.
|
|
3748 */
|
|
3749 (string))
|
|
3750 {
|
|
3751 Bufbyte *decoded;
|
|
3752 Bytind decoded_length;
|
|
3753 Charcount length, cc_decoded_length;
|
|
3754 Lisp_Object input, result;
|
|
3755 int speccount = specpdl_depth();
|
|
3756
|
|
3757 CHECK_STRING (string);
|
|
3758
|
|
3759 length = XSTRING_CHAR_LENGTH (string);
|
|
3760 /* We need to allocate enough room for decoding the text. */
|
|
3761 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
|
|
3762
|
|
3763 input = make_lisp_string_input_stream (string, 0, -1);
|
|
3764 decoded_length = base64_decode_1 (XLSTREAM (input), decoded,
|
|
3765 &cc_decoded_length);
|
|
3766 if (decoded_length > length * MAX_EMCHAR_LEN)
|
|
3767 abort ();
|
|
3768 Lstream_delete (XLSTREAM (input));
|
|
3769
|
|
3770 result = make_string (decoded, decoded_length);
|
|
3771 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
|
|
3772 return result;
|
|
3773 }
|
|
3774
|
|
3775 Lisp_Object Qyes_or_no_p;
|
|
3776
|
|
3777 void
|
|
3778 syms_of_fns (void)
|
|
3779 {
|
442
|
3780 INIT_LRECORD_IMPLEMENTATION (bit_vector);
|
|
3781
|
563
|
3782 DEFSYMBOL (Qstring_lessp);
|
|
3783 DEFSYMBOL (Qidentity);
|
|
3784 DEFSYMBOL (Qyes_or_no_p);
|
|
3785
|
|
3786 DEFERROR_STANDARD (Qbase64_conversion_error, Qconversion_error);
|
428
|
3787
|
|
3788 DEFSUBR (Fidentity);
|
|
3789 DEFSUBR (Frandom);
|
|
3790 DEFSUBR (Flength);
|
|
3791 DEFSUBR (Fsafe_length);
|
|
3792 DEFSUBR (Fstring_equal);
|
|
3793 DEFSUBR (Fstring_lessp);
|
|
3794 DEFSUBR (Fstring_modified_tick);
|
|
3795 DEFSUBR (Fappend);
|
|
3796 DEFSUBR (Fconcat);
|
|
3797 DEFSUBR (Fvconcat);
|
|
3798 DEFSUBR (Fbvconcat);
|
|
3799 DEFSUBR (Fcopy_list);
|
|
3800 DEFSUBR (Fcopy_sequence);
|
|
3801 DEFSUBR (Fcopy_alist);
|
|
3802 DEFSUBR (Fcopy_tree);
|
|
3803 DEFSUBR (Fsubstring);
|
|
3804 DEFSUBR (Fsubseq);
|
|
3805 DEFSUBR (Fnthcdr);
|
|
3806 DEFSUBR (Fnth);
|
|
3807 DEFSUBR (Felt);
|
|
3808 DEFSUBR (Flast);
|
|
3809 DEFSUBR (Fbutlast);
|
|
3810 DEFSUBR (Fnbutlast);
|
|
3811 DEFSUBR (Fmember);
|
|
3812 DEFSUBR (Fold_member);
|
|
3813 DEFSUBR (Fmemq);
|
|
3814 DEFSUBR (Fold_memq);
|
|
3815 DEFSUBR (Fassoc);
|
|
3816 DEFSUBR (Fold_assoc);
|
|
3817 DEFSUBR (Fassq);
|
|
3818 DEFSUBR (Fold_assq);
|
|
3819 DEFSUBR (Frassoc);
|
|
3820 DEFSUBR (Fold_rassoc);
|
|
3821 DEFSUBR (Frassq);
|
|
3822 DEFSUBR (Fold_rassq);
|
|
3823 DEFSUBR (Fdelete);
|
|
3824 DEFSUBR (Fold_delete);
|
|
3825 DEFSUBR (Fdelq);
|
|
3826 DEFSUBR (Fold_delq);
|
|
3827 DEFSUBR (Fremassoc);
|
|
3828 DEFSUBR (Fremassq);
|
|
3829 DEFSUBR (Fremrassoc);
|
|
3830 DEFSUBR (Fremrassq);
|
|
3831 DEFSUBR (Fnreverse);
|
|
3832 DEFSUBR (Freverse);
|
|
3833 DEFSUBR (Fsort);
|
|
3834 DEFSUBR (Fplists_eq);
|
|
3835 DEFSUBR (Fplists_equal);
|
|
3836 DEFSUBR (Flax_plists_eq);
|
|
3837 DEFSUBR (Flax_plists_equal);
|
|
3838 DEFSUBR (Fplist_get);
|
|
3839 DEFSUBR (Fplist_put);
|
|
3840 DEFSUBR (Fplist_remprop);
|
|
3841 DEFSUBR (Fplist_member);
|
|
3842 DEFSUBR (Fcheck_valid_plist);
|
|
3843 DEFSUBR (Fvalid_plist_p);
|
|
3844 DEFSUBR (Fcanonicalize_plist);
|
|
3845 DEFSUBR (Flax_plist_get);
|
|
3846 DEFSUBR (Flax_plist_put);
|
|
3847 DEFSUBR (Flax_plist_remprop);
|
|
3848 DEFSUBR (Flax_plist_member);
|
|
3849 DEFSUBR (Fcanonicalize_lax_plist);
|
|
3850 DEFSUBR (Fdestructive_alist_to_plist);
|
|
3851 DEFSUBR (Fget);
|
|
3852 DEFSUBR (Fput);
|
|
3853 DEFSUBR (Fremprop);
|
|
3854 DEFSUBR (Fobject_plist);
|
|
3855 DEFSUBR (Fequal);
|
|
3856 DEFSUBR (Fold_equal);
|
|
3857 DEFSUBR (Ffillarray);
|
|
3858 DEFSUBR (Fnconc);
|
|
3859 DEFSUBR (Fmapcar);
|
|
3860 DEFSUBR (Fmapvector);
|
|
3861 DEFSUBR (Fmapc_internal);
|
|
3862 DEFSUBR (Fmapconcat);
|
442
|
3863 DEFSUBR (Freplace_list);
|
428
|
3864 DEFSUBR (Fload_average);
|
|
3865 DEFSUBR (Ffeaturep);
|
|
3866 DEFSUBR (Frequire);
|
|
3867 DEFSUBR (Fprovide);
|
|
3868 DEFSUBR (Fbase64_encode_region);
|
|
3869 DEFSUBR (Fbase64_encode_string);
|
|
3870 DEFSUBR (Fbase64_decode_region);
|
|
3871 DEFSUBR (Fbase64_decode_string);
|
|
3872 }
|
|
3873
|
|
3874 void
|
|
3875 init_provide_once (void)
|
|
3876 {
|
|
3877 DEFVAR_LISP ("features", &Vfeatures /*
|
|
3878 A list of symbols which are the features of the executing emacs.
|
|
3879 Used by `featurep' and `require', and altered by `provide'.
|
|
3880 */ );
|
|
3881 Vfeatures = Qnil;
|
|
3882
|
|
3883 Fprovide (intern ("base64"));
|
|
3884 }
|