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