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