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