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