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