]> code.delx.au - gnu-emacs/blob - src/fns.c
52570a8af9f27edb2f37caccf6def66ec5d36dd9
[gnu-emacs] / src / fns.c
1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23
24 #include <unistd.h>
25 #include <time.h>
26 #include <setjmp.h>
27
28 /* Note on some machines this defines `vector' as a typedef,
29 so make sure we don't use that name in this file. */
30 #undef vector
31 #define vector *****
32
33 #include "lisp.h"
34 #include "commands.h"
35 #include "character.h"
36 #include "coding.h"
37 #include "buffer.h"
38 #include "keyboard.h"
39 #include "keymap.h"
40 #include "intervals.h"
41 #include "frame.h"
42 #include "window.h"
43 #include "blockinput.h"
44 #ifdef HAVE_MENUS
45 #if defined (HAVE_X_WINDOWS)
46 #include "xterm.h"
47 #endif
48 #endif /* HAVE_MENUS */
49
50 #ifndef NULL
51 #define NULL ((POINTER_TYPE *)0)
52 #endif
53
54 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
55 Lisp_Object Qyes_or_no_p_history;
56 Lisp_Object Qcursor_in_echo_area;
57 Lisp_Object Qwidget_type;
58 Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
59
60 static int internal_equal (Lisp_Object , Lisp_Object, int, int);
61
62 extern long get_random (void);
63 extern void seed_random (long);
64
65 #ifndef HAVE_UNISTD_H
66 extern long time ();
67 #endif
68 \f
69 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
70 doc: /* Return the argument unchanged. */)
71 (Lisp_Object arg)
72 {
73 return arg;
74 }
75
76 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
77 doc: /* Return a pseudo-random number.
78 All integers representable in Lisp are equally likely.
79 On most systems, this is 29 bits' worth.
80 With positive integer LIMIT, return random number in interval [0,LIMIT).
81 With argument t, set the random number seed from the current time and pid.
82 Other values of LIMIT are ignored. */)
83 (Lisp_Object limit)
84 {
85 EMACS_INT val;
86 Lisp_Object lispy_val;
87 unsigned long denominator;
88
89 if (EQ (limit, Qt))
90 seed_random (getpid () + time (NULL));
91 if (NATNUMP (limit) && XFASTINT (limit) != 0)
92 {
93 /* Try to take our random number from the higher bits of VAL,
94 not the lower, since (says Gentzel) the low bits of `random'
95 are less random than the higher ones. We do this by using the
96 quotient rather than the remainder. At the high end of the RNG
97 it's possible to get a quotient larger than n; discarding
98 these values eliminates the bias that would otherwise appear
99 when using a large n. */
100 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit);
101 do
102 val = get_random () / denominator;
103 while (val >= XFASTINT (limit));
104 }
105 else
106 val = get_random ();
107 XSETINT (lispy_val, val);
108 return lispy_val;
109 }
110 \f
111 /* Random data-structure functions */
112
113 DEFUN ("length", Flength, Slength, 1, 1, 0,
114 doc: /* Return the length of vector, list or string SEQUENCE.
115 A byte-code function object is also allowed.
116 If the string contains multibyte characters, this is not necessarily
117 the number of bytes in the string; it is the number of characters.
118 To get the number of bytes, use `string-bytes'. */)
119 (register Lisp_Object sequence)
120 {
121 register Lisp_Object val;
122 register int i;
123
124 if (STRINGP (sequence))
125 XSETFASTINT (val, SCHARS (sequence));
126 else if (VECTORP (sequence))
127 XSETFASTINT (val, ASIZE (sequence));
128 else if (CHAR_TABLE_P (sequence))
129 XSETFASTINT (val, MAX_CHAR);
130 else if (BOOL_VECTOR_P (sequence))
131 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
132 else if (COMPILEDP (sequence))
133 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
134 else if (CONSP (sequence))
135 {
136 i = 0;
137 while (CONSP (sequence))
138 {
139 sequence = XCDR (sequence);
140 ++i;
141
142 if (!CONSP (sequence))
143 break;
144
145 sequence = XCDR (sequence);
146 ++i;
147 QUIT;
148 }
149
150 CHECK_LIST_END (sequence, sequence);
151
152 val = make_number (i);
153 }
154 else if (NILP (sequence))
155 XSETFASTINT (val, 0);
156 else
157 wrong_type_argument (Qsequencep, sequence);
158
159 return val;
160 }
161
162 /* This does not check for quits. That is safe since it must terminate. */
163
164 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
165 doc: /* Return the length of a list, but avoid error or infinite loop.
166 This function never gets an error. If LIST is not really a list,
167 it returns 0. If LIST is circular, it returns a finite value
168 which is at least the number of distinct elements. */)
169 (Lisp_Object list)
170 {
171 Lisp_Object tail, halftail, length;
172 int len = 0;
173
174 /* halftail is used to detect circular lists. */
175 halftail = list;
176 for (tail = list; CONSP (tail); tail = XCDR (tail))
177 {
178 if (EQ (tail, halftail) && len != 0)
179 break;
180 len++;
181 if ((len & 1) == 0)
182 halftail = XCDR (halftail);
183 }
184
185 XSETINT (length, len);
186 return length;
187 }
188
189 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
190 doc: /* Return the number of bytes in STRING.
191 If STRING is multibyte, this may be greater than the length of STRING. */)
192 (Lisp_Object string)
193 {
194 CHECK_STRING (string);
195 return make_number (SBYTES (string));
196 }
197
198 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
199 doc: /* Return t if two strings have identical contents.
200 Case is significant, but text properties are ignored.
201 Symbols are also allowed; their print names are used instead. */)
202 (register Lisp_Object s1, Lisp_Object s2)
203 {
204 if (SYMBOLP (s1))
205 s1 = SYMBOL_NAME (s1);
206 if (SYMBOLP (s2))
207 s2 = SYMBOL_NAME (s2);
208 CHECK_STRING (s1);
209 CHECK_STRING (s2);
210
211 if (SCHARS (s1) != SCHARS (s2)
212 || SBYTES (s1) != SBYTES (s2)
213 || memcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
214 return Qnil;
215 return Qt;
216 }
217
218 DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
219 doc: /* Compare the contents of two strings, converting to multibyte if needed.
220 In string STR1, skip the first START1 characters and stop at END1.
221 In string STR2, skip the first START2 characters and stop at END2.
222 END1 and END2 default to the full lengths of the respective strings.
223
224 Case is significant in this comparison if IGNORE-CASE is nil.
225 Unibyte strings are converted to multibyte for comparison.
226
227 The value is t if the strings (or specified portions) match.
228 If string STR1 is less, the value is a negative number N;
229 - 1 - N is the number of characters that match at the beginning.
230 If string STR1 is greater, the value is a positive number N;
231 N - 1 is the number of characters that match at the beginning. */)
232 (Lisp_Object str1, Lisp_Object start1, Lisp_Object end1, Lisp_Object str2, Lisp_Object start2, Lisp_Object end2, Lisp_Object ignore_case)
233 {
234 register EMACS_INT end1_char, end2_char;
235 register EMACS_INT i1, i1_byte, i2, i2_byte;
236
237 CHECK_STRING (str1);
238 CHECK_STRING (str2);
239 if (NILP (start1))
240 start1 = make_number (0);
241 if (NILP (start2))
242 start2 = make_number (0);
243 CHECK_NATNUM (start1);
244 CHECK_NATNUM (start2);
245 if (! NILP (end1))
246 CHECK_NATNUM (end1);
247 if (! NILP (end2))
248 CHECK_NATNUM (end2);
249
250 i1 = XINT (start1);
251 i2 = XINT (start2);
252
253 i1_byte = string_char_to_byte (str1, i1);
254 i2_byte = string_char_to_byte (str2, i2);
255
256 end1_char = SCHARS (str1);
257 if (! NILP (end1) && end1_char > XINT (end1))
258 end1_char = XINT (end1);
259
260 end2_char = SCHARS (str2);
261 if (! NILP (end2) && end2_char > XINT (end2))
262 end2_char = XINT (end2);
263
264 while (i1 < end1_char && i2 < end2_char)
265 {
266 /* When we find a mismatch, we must compare the
267 characters, not just the bytes. */
268 int c1, c2;
269
270 if (STRING_MULTIBYTE (str1))
271 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
272 else
273 {
274 c1 = SREF (str1, i1++);
275 MAKE_CHAR_MULTIBYTE (c1);
276 }
277
278 if (STRING_MULTIBYTE (str2))
279 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
280 else
281 {
282 c2 = SREF (str2, i2++);
283 MAKE_CHAR_MULTIBYTE (c2);
284 }
285
286 if (c1 == c2)
287 continue;
288
289 if (! NILP (ignore_case))
290 {
291 Lisp_Object tem;
292
293 tem = Fupcase (make_number (c1));
294 c1 = XINT (tem);
295 tem = Fupcase (make_number (c2));
296 c2 = XINT (tem);
297 }
298
299 if (c1 == c2)
300 continue;
301
302 /* Note that I1 has already been incremented
303 past the character that we are comparing;
304 hence we don't add or subtract 1 here. */
305 if (c1 < c2)
306 return make_number (- i1 + XINT (start1));
307 else
308 return make_number (i1 - XINT (start1));
309 }
310
311 if (i1 < end1_char)
312 return make_number (i1 - XINT (start1) + 1);
313 if (i2 < end2_char)
314 return make_number (- i1 + XINT (start1) - 1);
315
316 return Qt;
317 }
318
319 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
320 doc: /* Return t if first arg string is less than second in lexicographic order.
321 Case is significant.
322 Symbols are also allowed; their print names are used instead. */)
323 (register Lisp_Object s1, Lisp_Object s2)
324 {
325 register EMACS_INT end;
326 register EMACS_INT i1, i1_byte, i2, i2_byte;
327
328 if (SYMBOLP (s1))
329 s1 = SYMBOL_NAME (s1);
330 if (SYMBOLP (s2))
331 s2 = SYMBOL_NAME (s2);
332 CHECK_STRING (s1);
333 CHECK_STRING (s2);
334
335 i1 = i1_byte = i2 = i2_byte = 0;
336
337 end = SCHARS (s1);
338 if (end > SCHARS (s2))
339 end = SCHARS (s2);
340
341 while (i1 < end)
342 {
343 /* When we find a mismatch, we must compare the
344 characters, not just the bytes. */
345 int c1, c2;
346
347 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
348 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
349
350 if (c1 != c2)
351 return c1 < c2 ? Qt : Qnil;
352 }
353 return i1 < SCHARS (s2) ? Qt : Qnil;
354 }
355 \f
356 static Lisp_Object concat (int nargs, Lisp_Object *args,
357 enum Lisp_Type target_type, int last_special);
358
359 /* ARGSUSED */
360 Lisp_Object
361 concat2 (Lisp_Object s1, Lisp_Object s2)
362 {
363 Lisp_Object args[2];
364 args[0] = s1;
365 args[1] = s2;
366 return concat (2, args, Lisp_String, 0);
367 }
368
369 /* ARGSUSED */
370 Lisp_Object
371 concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
372 {
373 Lisp_Object args[3];
374 args[0] = s1;
375 args[1] = s2;
376 args[2] = s3;
377 return concat (3, args, Lisp_String, 0);
378 }
379
380 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
381 doc: /* Concatenate all the arguments and make the result a list.
382 The result is a list whose elements are the elements of all the arguments.
383 Each argument may be a list, vector or string.
384 The last argument is not copied, just used as the tail of the new list.
385 usage: (append &rest SEQUENCES) */)
386 (int nargs, Lisp_Object *args)
387 {
388 return concat (nargs, args, Lisp_Cons, 1);
389 }
390
391 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
392 doc: /* Concatenate all the arguments and make the result a string.
393 The result is a string whose elements are the elements of all the arguments.
394 Each argument may be a string or a list or vector of characters (integers).
395 usage: (concat &rest SEQUENCES) */)
396 (int nargs, Lisp_Object *args)
397 {
398 return concat (nargs, args, Lisp_String, 0);
399 }
400
401 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
402 doc: /* Concatenate all the arguments and make the result a vector.
403 The result is a vector whose elements are the elements of all the arguments.
404 Each argument may be a list, vector or string.
405 usage: (vconcat &rest SEQUENCES) */)
406 (int nargs, Lisp_Object *args)
407 {
408 return concat (nargs, args, Lisp_Vectorlike, 0);
409 }
410
411
412 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
413 doc: /* Return a copy of a list, vector, string or char-table.
414 The elements of a list or vector are not copied; they are shared
415 with the original. */)
416 (Lisp_Object arg)
417 {
418 if (NILP (arg)) return arg;
419
420 if (CHAR_TABLE_P (arg))
421 {
422 return copy_char_table (arg);
423 }
424
425 if (BOOL_VECTOR_P (arg))
426 {
427 Lisp_Object val;
428 int size_in_chars
429 = ((XBOOL_VECTOR (arg)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
430 / BOOL_VECTOR_BITS_PER_CHAR);
431
432 val = Fmake_bool_vector (Flength (arg), Qnil);
433 memcpy (XBOOL_VECTOR (val)->data, XBOOL_VECTOR (arg)->data,
434 size_in_chars);
435 return val;
436 }
437
438 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
439 wrong_type_argument (Qsequencep, arg);
440
441 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
442 }
443
444 /* This structure holds information of an argument of `concat' that is
445 a string and has text properties to be copied. */
446 struct textprop_rec
447 {
448 int argnum; /* refer to ARGS (arguments of `concat') */
449 EMACS_INT from; /* refer to ARGS[argnum] (argument string) */
450 EMACS_INT to; /* refer to VAL (the target string) */
451 };
452
453 static Lisp_Object
454 concat (int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special)
455 {
456 Lisp_Object val;
457 register Lisp_Object tail;
458 register Lisp_Object this;
459 EMACS_INT toindex;
460 EMACS_INT toindex_byte = 0;
461 register EMACS_INT result_len;
462 register EMACS_INT result_len_byte;
463 register int argnum;
464 Lisp_Object last_tail;
465 Lisp_Object prev;
466 int some_multibyte;
467 /* When we make a multibyte string, we can't copy text properties
468 while concatinating each string because the length of resulting
469 string can't be decided until we finish the whole concatination.
470 So, we record strings that have text properties to be copied
471 here, and copy the text properties after the concatination. */
472 struct textprop_rec *textprops = NULL;
473 /* Number of elements in textprops. */
474 int num_textprops = 0;
475 USE_SAFE_ALLOCA;
476
477 tail = Qnil;
478
479 /* In append, the last arg isn't treated like the others */
480 if (last_special && nargs > 0)
481 {
482 nargs--;
483 last_tail = args[nargs];
484 }
485 else
486 last_tail = Qnil;
487
488 /* Check each argument. */
489 for (argnum = 0; argnum < nargs; argnum++)
490 {
491 this = args[argnum];
492 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
493 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
494 wrong_type_argument (Qsequencep, this);
495 }
496
497 /* Compute total length in chars of arguments in RESULT_LEN.
498 If desired output is a string, also compute length in bytes
499 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
500 whether the result should be a multibyte string. */
501 result_len_byte = 0;
502 result_len = 0;
503 some_multibyte = 0;
504 for (argnum = 0; argnum < nargs; argnum++)
505 {
506 EMACS_INT len;
507 this = args[argnum];
508 len = XFASTINT (Flength (this));
509 if (target_type == Lisp_String)
510 {
511 /* We must count the number of bytes needed in the string
512 as well as the number of characters. */
513 EMACS_INT i;
514 Lisp_Object ch;
515 EMACS_INT this_len_byte;
516
517 if (VECTORP (this))
518 for (i = 0; i < len; i++)
519 {
520 ch = AREF (this, i);
521 CHECK_CHARACTER (ch);
522 this_len_byte = CHAR_BYTES (XINT (ch));
523 result_len_byte += this_len_byte;
524 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
525 some_multibyte = 1;
526 }
527 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
528 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
529 else if (CONSP (this))
530 for (; CONSP (this); this = XCDR (this))
531 {
532 ch = XCAR (this);
533 CHECK_CHARACTER (ch);
534 this_len_byte = CHAR_BYTES (XINT (ch));
535 result_len_byte += this_len_byte;
536 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
537 some_multibyte = 1;
538 }
539 else if (STRINGP (this))
540 {
541 if (STRING_MULTIBYTE (this))
542 {
543 some_multibyte = 1;
544 result_len_byte += SBYTES (this);
545 }
546 else
547 result_len_byte += count_size_as_multibyte (SDATA (this),
548 SCHARS (this));
549 }
550 }
551
552 result_len += len;
553 if (result_len < 0)
554 error ("String overflow");
555 }
556
557 if (! some_multibyte)
558 result_len_byte = result_len;
559
560 /* Create the output object. */
561 if (target_type == Lisp_Cons)
562 val = Fmake_list (make_number (result_len), Qnil);
563 else if (target_type == Lisp_Vectorlike)
564 val = Fmake_vector (make_number (result_len), Qnil);
565 else if (some_multibyte)
566 val = make_uninit_multibyte_string (result_len, result_len_byte);
567 else
568 val = make_uninit_string (result_len);
569
570 /* In `append', if all but last arg are nil, return last arg. */
571 if (target_type == Lisp_Cons && EQ (val, Qnil))
572 return last_tail;
573
574 /* Copy the contents of the args into the result. */
575 if (CONSP (val))
576 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
577 else
578 toindex = 0, toindex_byte = 0;
579
580 prev = Qnil;
581 if (STRINGP (val))
582 SAFE_ALLOCA (textprops, struct textprop_rec *, sizeof (struct textprop_rec) * nargs);
583
584 for (argnum = 0; argnum < nargs; argnum++)
585 {
586 Lisp_Object thislen;
587 EMACS_INT thisleni = 0;
588 register EMACS_INT thisindex = 0;
589 register EMACS_INT thisindex_byte = 0;
590
591 this = args[argnum];
592 if (!CONSP (this))
593 thislen = Flength (this), thisleni = XINT (thislen);
594
595 /* Between strings of the same kind, copy fast. */
596 if (STRINGP (this) && STRINGP (val)
597 && STRING_MULTIBYTE (this) == some_multibyte)
598 {
599 EMACS_INT thislen_byte = SBYTES (this);
600
601 memcpy (SDATA (val) + toindex_byte, SDATA (this), SBYTES (this));
602 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
603 {
604 textprops[num_textprops].argnum = argnum;
605 textprops[num_textprops].from = 0;
606 textprops[num_textprops++].to = toindex;
607 }
608 toindex_byte += thislen_byte;
609 toindex += thisleni;
610 }
611 /* Copy a single-byte string to a multibyte string. */
612 else if (STRINGP (this) && STRINGP (val))
613 {
614 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
615 {
616 textprops[num_textprops].argnum = argnum;
617 textprops[num_textprops].from = 0;
618 textprops[num_textprops++].to = toindex;
619 }
620 toindex_byte += copy_text (SDATA (this),
621 SDATA (val) + toindex_byte,
622 SCHARS (this), 0, 1);
623 toindex += thisleni;
624 }
625 else
626 /* Copy element by element. */
627 while (1)
628 {
629 register Lisp_Object elt;
630
631 /* Fetch next element of `this' arg into `elt', or break if
632 `this' is exhausted. */
633 if (NILP (this)) break;
634 if (CONSP (this))
635 elt = XCAR (this), this = XCDR (this);
636 else if (thisindex >= thisleni)
637 break;
638 else if (STRINGP (this))
639 {
640 int c;
641 if (STRING_MULTIBYTE (this))
642 {
643 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
644 thisindex,
645 thisindex_byte);
646 XSETFASTINT (elt, c);
647 }
648 else
649 {
650 XSETFASTINT (elt, SREF (this, thisindex)); thisindex++;
651 if (some_multibyte
652 && !ASCII_CHAR_P (XINT (elt))
653 && XINT (elt) < 0400)
654 {
655 c = BYTE8_TO_CHAR (XINT (elt));
656 XSETINT (elt, c);
657 }
658 }
659 }
660 else if (BOOL_VECTOR_P (this))
661 {
662 int byte;
663 byte = XBOOL_VECTOR (this)->data[thisindex / BOOL_VECTOR_BITS_PER_CHAR];
664 if (byte & (1 << (thisindex % BOOL_VECTOR_BITS_PER_CHAR)))
665 elt = Qt;
666 else
667 elt = Qnil;
668 thisindex++;
669 }
670 else
671 {
672 elt = AREF (this, thisindex);
673 thisindex++;
674 }
675
676 /* Store this element into the result. */
677 if (toindex < 0)
678 {
679 XSETCAR (tail, elt);
680 prev = tail;
681 tail = XCDR (tail);
682 }
683 else if (VECTORP (val))
684 {
685 ASET (val, toindex, elt);
686 toindex++;
687 }
688 else
689 {
690 CHECK_NUMBER (elt);
691 if (some_multibyte)
692 toindex_byte += CHAR_STRING (XINT (elt),
693 SDATA (val) + toindex_byte);
694 else
695 SSET (val, toindex_byte++, XINT (elt));
696 toindex++;
697 }
698 }
699 }
700 if (!NILP (prev))
701 XSETCDR (prev, last_tail);
702
703 if (num_textprops > 0)
704 {
705 Lisp_Object props;
706 EMACS_INT last_to_end = -1;
707
708 for (argnum = 0; argnum < num_textprops; argnum++)
709 {
710 this = args[textprops[argnum].argnum];
711 props = text_property_list (this,
712 make_number (0),
713 make_number (SCHARS (this)),
714 Qnil);
715 /* If successive arguments have properites, be sure that the
716 value of `composition' property be the copy. */
717 if (last_to_end == textprops[argnum].to)
718 make_composition_value_copy (props);
719 add_text_properties_from_list (val, props,
720 make_number (textprops[argnum].to));
721 last_to_end = textprops[argnum].to + SCHARS (this);
722 }
723 }
724
725 SAFE_FREE ();
726 return val;
727 }
728 \f
729 static Lisp_Object string_char_byte_cache_string;
730 static EMACS_INT string_char_byte_cache_charpos;
731 static EMACS_INT string_char_byte_cache_bytepos;
732
733 void
734 clear_string_char_byte_cache (void)
735 {
736 string_char_byte_cache_string = Qnil;
737 }
738
739 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
740
741 EMACS_INT
742 string_char_to_byte (Lisp_Object string, EMACS_INT char_index)
743 {
744 EMACS_INT i_byte;
745 EMACS_INT best_below, best_below_byte;
746 EMACS_INT best_above, best_above_byte;
747
748 best_below = best_below_byte = 0;
749 best_above = SCHARS (string);
750 best_above_byte = SBYTES (string);
751 if (best_above == best_above_byte)
752 return char_index;
753
754 if (EQ (string, string_char_byte_cache_string))
755 {
756 if (string_char_byte_cache_charpos < char_index)
757 {
758 best_below = string_char_byte_cache_charpos;
759 best_below_byte = string_char_byte_cache_bytepos;
760 }
761 else
762 {
763 best_above = string_char_byte_cache_charpos;
764 best_above_byte = string_char_byte_cache_bytepos;
765 }
766 }
767
768 if (char_index - best_below < best_above - char_index)
769 {
770 unsigned char *p = SDATA (string) + best_below_byte;
771
772 while (best_below < char_index)
773 {
774 p += BYTES_BY_CHAR_HEAD (*p);
775 best_below++;
776 }
777 i_byte = p - SDATA (string);
778 }
779 else
780 {
781 unsigned char *p = SDATA (string) + best_above_byte;
782
783 while (best_above > char_index)
784 {
785 p--;
786 while (!CHAR_HEAD_P (*p)) p--;
787 best_above--;
788 }
789 i_byte = p - SDATA (string);
790 }
791
792 string_char_byte_cache_bytepos = i_byte;
793 string_char_byte_cache_charpos = char_index;
794 string_char_byte_cache_string = string;
795
796 return i_byte;
797 }
798 \f
799 /* Return the character index corresponding to BYTE_INDEX in STRING. */
800
801 EMACS_INT
802 string_byte_to_char (Lisp_Object string, EMACS_INT byte_index)
803 {
804 EMACS_INT i, i_byte;
805 EMACS_INT best_below, best_below_byte;
806 EMACS_INT best_above, best_above_byte;
807
808 best_below = best_below_byte = 0;
809 best_above = SCHARS (string);
810 best_above_byte = SBYTES (string);
811 if (best_above == best_above_byte)
812 return byte_index;
813
814 if (EQ (string, string_char_byte_cache_string))
815 {
816 if (string_char_byte_cache_bytepos < byte_index)
817 {
818 best_below = string_char_byte_cache_charpos;
819 best_below_byte = string_char_byte_cache_bytepos;
820 }
821 else
822 {
823 best_above = string_char_byte_cache_charpos;
824 best_above_byte = string_char_byte_cache_bytepos;
825 }
826 }
827
828 if (byte_index - best_below_byte < best_above_byte - byte_index)
829 {
830 unsigned char *p = SDATA (string) + best_below_byte;
831 unsigned char *pend = SDATA (string) + byte_index;
832
833 while (p < pend)
834 {
835 p += BYTES_BY_CHAR_HEAD (*p);
836 best_below++;
837 }
838 i = best_below;
839 i_byte = p - SDATA (string);
840 }
841 else
842 {
843 unsigned char *p = SDATA (string) + best_above_byte;
844 unsigned char *pbeg = SDATA (string) + byte_index;
845
846 while (p > pbeg)
847 {
848 p--;
849 while (!CHAR_HEAD_P (*p)) p--;
850 best_above--;
851 }
852 i = best_above;
853 i_byte = p - SDATA (string);
854 }
855
856 string_char_byte_cache_bytepos = i_byte;
857 string_char_byte_cache_charpos = i;
858 string_char_byte_cache_string = string;
859
860 return i;
861 }
862 \f
863 /* Convert STRING to a multibyte string. */
864
865 static Lisp_Object
866 string_make_multibyte (Lisp_Object string)
867 {
868 unsigned char *buf;
869 EMACS_INT nbytes;
870 Lisp_Object ret;
871 USE_SAFE_ALLOCA;
872
873 if (STRING_MULTIBYTE (string))
874 return string;
875
876 nbytes = count_size_as_multibyte (SDATA (string),
877 SCHARS (string));
878 /* If all the chars are ASCII, they won't need any more bytes
879 once converted. In that case, we can return STRING itself. */
880 if (nbytes == SBYTES (string))
881 return string;
882
883 SAFE_ALLOCA (buf, unsigned char *, nbytes);
884 copy_text (SDATA (string), buf, SBYTES (string),
885 0, 1);
886
887 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
888 SAFE_FREE ();
889
890 return ret;
891 }
892
893
894 /* Convert STRING (if unibyte) to a multibyte string without changing
895 the number of characters. Characters 0200 trough 0237 are
896 converted to eight-bit characters. */
897
898 Lisp_Object
899 string_to_multibyte (Lisp_Object string)
900 {
901 unsigned char *buf;
902 EMACS_INT nbytes;
903 Lisp_Object ret;
904 USE_SAFE_ALLOCA;
905
906 if (STRING_MULTIBYTE (string))
907 return string;
908
909 nbytes = parse_str_to_multibyte (SDATA (string), SBYTES (string));
910 /* If all the chars are ASCII, they won't need any more bytes once
911 converted. */
912 if (nbytes == SBYTES (string))
913 return make_multibyte_string (SDATA (string), nbytes, nbytes);
914
915 SAFE_ALLOCA (buf, unsigned char *, nbytes);
916 memcpy (buf, SDATA (string), SBYTES (string));
917 str_to_multibyte (buf, nbytes, SBYTES (string));
918
919 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
920 SAFE_FREE ();
921
922 return ret;
923 }
924
925
926 /* Convert STRING to a single-byte string. */
927
928 Lisp_Object
929 string_make_unibyte (Lisp_Object string)
930 {
931 EMACS_INT nchars;
932 unsigned char *buf;
933 Lisp_Object ret;
934 USE_SAFE_ALLOCA;
935
936 if (! STRING_MULTIBYTE (string))
937 return string;
938
939 nchars = SCHARS (string);
940
941 SAFE_ALLOCA (buf, unsigned char *, nchars);
942 copy_text (SDATA (string), buf, SBYTES (string),
943 1, 0);
944
945 ret = make_unibyte_string (buf, nchars);
946 SAFE_FREE ();
947
948 return ret;
949 }
950
951 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
952 1, 1, 0,
953 doc: /* Return the multibyte equivalent of STRING.
954 If STRING is unibyte and contains non-ASCII characters, the function
955 `unibyte-char-to-multibyte' is used to convert each unibyte character
956 to a multibyte character. In this case, the returned string is a
957 newly created string with no text properties. If STRING is multibyte
958 or entirely ASCII, it is returned unchanged. In particular, when
959 STRING is unibyte and entirely ASCII, the returned string is unibyte.
960 \(When the characters are all ASCII, Emacs primitives will treat the
961 string the same way whether it is unibyte or multibyte.) */)
962 (Lisp_Object string)
963 {
964 CHECK_STRING (string);
965
966 return string_make_multibyte (string);
967 }
968
969 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
970 1, 1, 0,
971 doc: /* Return the unibyte equivalent of STRING.
972 Multibyte character codes are converted to unibyte according to
973 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
974 If the lookup in the translation table fails, this function takes just
975 the low 8 bits of each character. */)
976 (Lisp_Object string)
977 {
978 CHECK_STRING (string);
979
980 return string_make_unibyte (string);
981 }
982
983 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
984 1, 1, 0,
985 doc: /* Return a unibyte string with the same individual bytes as STRING.
986 If STRING is unibyte, the result is STRING itself.
987 Otherwise it is a newly created string, with no text properties.
988 If STRING is multibyte and contains a character of charset
989 `eight-bit', it is converted to the corresponding single byte. */)
990 (Lisp_Object string)
991 {
992 CHECK_STRING (string);
993
994 if (STRING_MULTIBYTE (string))
995 {
996 EMACS_INT bytes = SBYTES (string);
997 unsigned char *str = (unsigned char *) xmalloc (bytes);
998
999 memcpy (str, SDATA (string), bytes);
1000 bytes = str_as_unibyte (str, bytes);
1001 string = make_unibyte_string (str, bytes);
1002 xfree (str);
1003 }
1004 return string;
1005 }
1006
1007 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1008 1, 1, 0,
1009 doc: /* Return a multibyte string with the same individual bytes as STRING.
1010 If STRING is multibyte, the result is STRING itself.
1011 Otherwise it is a newly created string, with no text properties.
1012
1013 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1014 part of a correct utf-8 sequence), it is converted to the corresponding
1015 multibyte character of charset `eight-bit'.
1016 See also `string-to-multibyte'.
1017
1018 Beware, this often doesn't really do what you think it does.
1019 It is similar to (decode-coding-string STRING 'utf-8-emacs).
1020 If you're not sure, whether to use `string-as-multibyte' or
1021 `string-to-multibyte', use `string-to-multibyte'. */)
1022 (Lisp_Object string)
1023 {
1024 CHECK_STRING (string);
1025
1026 if (! STRING_MULTIBYTE (string))
1027 {
1028 Lisp_Object new_string;
1029 EMACS_INT nchars, nbytes;
1030
1031 parse_str_as_multibyte (SDATA (string),
1032 SBYTES (string),
1033 &nchars, &nbytes);
1034 new_string = make_uninit_multibyte_string (nchars, nbytes);
1035 memcpy (SDATA (new_string), SDATA (string), SBYTES (string));
1036 if (nbytes != SBYTES (string))
1037 str_as_multibyte (SDATA (new_string), nbytes,
1038 SBYTES (string), NULL);
1039 string = new_string;
1040 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1041 }
1042 return string;
1043 }
1044
1045 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1046 1, 1, 0,
1047 doc: /* Return a multibyte string with the same individual chars as STRING.
1048 If STRING is multibyte, the result is STRING itself.
1049 Otherwise it is a newly created string, with no text properties.
1050
1051 If STRING is unibyte and contains an 8-bit byte, it is converted to
1052 the corresponding multibyte character of charset `eight-bit'.
1053
1054 This differs from `string-as-multibyte' by converting each byte of a correct
1055 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1056 correct sequence. */)
1057 (Lisp_Object string)
1058 {
1059 CHECK_STRING (string);
1060
1061 return string_to_multibyte (string);
1062 }
1063
1064 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1065 1, 1, 0,
1066 doc: /* Return a unibyte string with the same individual chars as STRING.
1067 If STRING is unibyte, the result is STRING itself.
1068 Otherwise it is a newly created string, with no text properties,
1069 where each `eight-bit' character is converted to the corresponding byte.
1070 If STRING contains a non-ASCII, non-`eight-bit' character,
1071 an error is signaled. */)
1072 (Lisp_Object string)
1073 {
1074 CHECK_STRING (string);
1075
1076 if (STRING_MULTIBYTE (string))
1077 {
1078 EMACS_INT chars = SCHARS (string);
1079 unsigned char *str = (unsigned char *) xmalloc (chars);
1080 EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars, 0);
1081
1082 if (converted < chars)
1083 error ("Can't convert the %dth character to unibyte", converted);
1084 string = make_unibyte_string (str, chars);
1085 xfree (str);
1086 }
1087 return string;
1088 }
1089
1090 \f
1091 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1092 doc: /* Return a copy of ALIST.
1093 This is an alist which represents the same mapping from objects to objects,
1094 but does not share the alist structure with ALIST.
1095 The objects mapped (cars and cdrs of elements of the alist)
1096 are shared, however.
1097 Elements of ALIST that are not conses are also shared. */)
1098 (Lisp_Object alist)
1099 {
1100 register Lisp_Object tem;
1101
1102 CHECK_LIST (alist);
1103 if (NILP (alist))
1104 return alist;
1105 alist = concat (1, &alist, Lisp_Cons, 0);
1106 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1107 {
1108 register Lisp_Object car;
1109 car = XCAR (tem);
1110
1111 if (CONSP (car))
1112 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1113 }
1114 return alist;
1115 }
1116
1117 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1118 doc: /* Return a new string whose contents are a substring of STRING.
1119 The returned string consists of the characters between index FROM
1120 \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1121 zero-indexed: 0 means the first character of STRING. Negative values
1122 are counted from the end of STRING. If TO is nil, the substring runs
1123 to the end of STRING.
1124
1125 The STRING argument may also be a vector. In that case, the return
1126 value is a new vector that contains the elements between index FROM
1127 \(inclusive) and index TO (exclusive) of that vector argument. */)
1128 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
1129 {
1130 Lisp_Object res;
1131 EMACS_INT size;
1132 EMACS_INT size_byte = 0;
1133 EMACS_INT from_char, to_char;
1134 EMACS_INT from_byte = 0, to_byte = 0;
1135
1136 CHECK_VECTOR_OR_STRING (string);
1137 CHECK_NUMBER (from);
1138
1139 if (STRINGP (string))
1140 {
1141 size = SCHARS (string);
1142 size_byte = SBYTES (string);
1143 }
1144 else
1145 size = ASIZE (string);
1146
1147 if (NILP (to))
1148 {
1149 to_char = size;
1150 to_byte = size_byte;
1151 }
1152 else
1153 {
1154 CHECK_NUMBER (to);
1155
1156 to_char = XINT (to);
1157 if (to_char < 0)
1158 to_char += size;
1159
1160 if (STRINGP (string))
1161 to_byte = string_char_to_byte (string, to_char);
1162 }
1163
1164 from_char = XINT (from);
1165 if (from_char < 0)
1166 from_char += size;
1167 if (STRINGP (string))
1168 from_byte = string_char_to_byte (string, from_char);
1169
1170 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1171 args_out_of_range_3 (string, make_number (from_char),
1172 make_number (to_char));
1173
1174 if (STRINGP (string))
1175 {
1176 res = make_specified_string (SDATA (string) + from_byte,
1177 to_char - from_char, to_byte - from_byte,
1178 STRING_MULTIBYTE (string));
1179 copy_text_properties (make_number (from_char), make_number (to_char),
1180 string, make_number (0), res, Qnil);
1181 }
1182 else
1183 res = Fvector (to_char - from_char, &AREF (string, from_char));
1184
1185 return res;
1186 }
1187
1188
1189 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1190 doc: /* Return a substring of STRING, without text properties.
1191 It starts at index FROM and ends before TO.
1192 TO may be nil or omitted; then the substring runs to the end of STRING.
1193 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1194 If FROM or TO is negative, it counts from the end.
1195
1196 With one argument, just copy STRING without its properties. */)
1197 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
1198 {
1199 EMACS_INT size, size_byte;
1200 EMACS_INT from_char, to_char;
1201 EMACS_INT from_byte, to_byte;
1202
1203 CHECK_STRING (string);
1204
1205 size = SCHARS (string);
1206 size_byte = SBYTES (string);
1207
1208 if (NILP (from))
1209 from_char = from_byte = 0;
1210 else
1211 {
1212 CHECK_NUMBER (from);
1213 from_char = XINT (from);
1214 if (from_char < 0)
1215 from_char += size;
1216
1217 from_byte = string_char_to_byte (string, from_char);
1218 }
1219
1220 if (NILP (to))
1221 {
1222 to_char = size;
1223 to_byte = size_byte;
1224 }
1225 else
1226 {
1227 CHECK_NUMBER (to);
1228
1229 to_char = XINT (to);
1230 if (to_char < 0)
1231 to_char += size;
1232
1233 to_byte = string_char_to_byte (string, to_char);
1234 }
1235
1236 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1237 args_out_of_range_3 (string, make_number (from_char),
1238 make_number (to_char));
1239
1240 return make_specified_string (SDATA (string) + from_byte,
1241 to_char - from_char, to_byte - from_byte,
1242 STRING_MULTIBYTE (string));
1243 }
1244
1245 /* Extract a substring of STRING, giving start and end positions
1246 both in characters and in bytes. */
1247
1248 Lisp_Object
1249 substring_both (Lisp_Object string, EMACS_INT from, EMACS_INT from_byte,
1250 EMACS_INT to, EMACS_INT to_byte)
1251 {
1252 Lisp_Object res;
1253 EMACS_INT size;
1254 EMACS_INT size_byte;
1255
1256 CHECK_VECTOR_OR_STRING (string);
1257
1258 if (STRINGP (string))
1259 {
1260 size = SCHARS (string);
1261 size_byte = SBYTES (string);
1262 }
1263 else
1264 size = ASIZE (string);
1265
1266 if (!(0 <= from && from <= to && to <= size))
1267 args_out_of_range_3 (string, make_number (from), make_number (to));
1268
1269 if (STRINGP (string))
1270 {
1271 res = make_specified_string (SDATA (string) + from_byte,
1272 to - from, to_byte - from_byte,
1273 STRING_MULTIBYTE (string));
1274 copy_text_properties (make_number (from), make_number (to),
1275 string, make_number (0), res, Qnil);
1276 }
1277 else
1278 res = Fvector (to - from, &AREF (string, from));
1279
1280 return res;
1281 }
1282 \f
1283 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1284 doc: /* Take cdr N times on LIST, return the result. */)
1285 (Lisp_Object n, Lisp_Object list)
1286 {
1287 register int i, num;
1288 CHECK_NUMBER (n);
1289 num = XINT (n);
1290 for (i = 0; i < num && !NILP (list); i++)
1291 {
1292 QUIT;
1293 CHECK_LIST_CONS (list, list);
1294 list = XCDR (list);
1295 }
1296 return list;
1297 }
1298
1299 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1300 doc: /* Return the Nth element of LIST.
1301 N counts from zero. If LIST is not that long, nil is returned. */)
1302 (Lisp_Object n, Lisp_Object list)
1303 {
1304 return Fcar (Fnthcdr (n, list));
1305 }
1306
1307 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1308 doc: /* Return element of SEQUENCE at index N. */)
1309 (register Lisp_Object sequence, Lisp_Object n)
1310 {
1311 CHECK_NUMBER (n);
1312 if (CONSP (sequence) || NILP (sequence))
1313 return Fcar (Fnthcdr (n, sequence));
1314
1315 /* Faref signals a "not array" error, so check here. */
1316 CHECK_ARRAY (sequence, Qsequencep);
1317 return Faref (sequence, n);
1318 }
1319
1320 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1321 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1322 The value is actually the tail of LIST whose car is ELT. */)
1323 (register Lisp_Object elt, Lisp_Object list)
1324 {
1325 register Lisp_Object tail;
1326 for (tail = list; CONSP (tail); tail = XCDR (tail))
1327 {
1328 register Lisp_Object tem;
1329 CHECK_LIST_CONS (tail, list);
1330 tem = XCAR (tail);
1331 if (! NILP (Fequal (elt, tem)))
1332 return tail;
1333 QUIT;
1334 }
1335 return Qnil;
1336 }
1337
1338 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1339 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1340 The value is actually the tail of LIST whose car is ELT. */)
1341 (register Lisp_Object elt, Lisp_Object list)
1342 {
1343 while (1)
1344 {
1345 if (!CONSP (list) || EQ (XCAR (list), elt))
1346 break;
1347
1348 list = XCDR (list);
1349 if (!CONSP (list) || EQ (XCAR (list), elt))
1350 break;
1351
1352 list = XCDR (list);
1353 if (!CONSP (list) || EQ (XCAR (list), elt))
1354 break;
1355
1356 list = XCDR (list);
1357 QUIT;
1358 }
1359
1360 CHECK_LIST (list);
1361 return list;
1362 }
1363
1364 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1365 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1366 The value is actually the tail of LIST whose car is ELT. */)
1367 (register Lisp_Object elt, Lisp_Object list)
1368 {
1369 register Lisp_Object tail;
1370
1371 if (!FLOATP (elt))
1372 return Fmemq (elt, list);
1373
1374 for (tail = list; CONSP (tail); tail = XCDR (tail))
1375 {
1376 register Lisp_Object tem;
1377 CHECK_LIST_CONS (tail, list);
1378 tem = XCAR (tail);
1379 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1380 return tail;
1381 QUIT;
1382 }
1383 return Qnil;
1384 }
1385
1386 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1387 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1388 The value is actually the first element of LIST whose car is KEY.
1389 Elements of LIST that are not conses are ignored. */)
1390 (Lisp_Object key, Lisp_Object list)
1391 {
1392 while (1)
1393 {
1394 if (!CONSP (list)
1395 || (CONSP (XCAR (list))
1396 && EQ (XCAR (XCAR (list)), key)))
1397 break;
1398
1399 list = XCDR (list);
1400 if (!CONSP (list)
1401 || (CONSP (XCAR (list))
1402 && EQ (XCAR (XCAR (list)), key)))
1403 break;
1404
1405 list = XCDR (list);
1406 if (!CONSP (list)
1407 || (CONSP (XCAR (list))
1408 && EQ (XCAR (XCAR (list)), key)))
1409 break;
1410
1411 list = XCDR (list);
1412 QUIT;
1413 }
1414
1415 return CAR (list);
1416 }
1417
1418 /* Like Fassq but never report an error and do not allow quits.
1419 Use only on lists known never to be circular. */
1420
1421 Lisp_Object
1422 assq_no_quit (Lisp_Object key, Lisp_Object list)
1423 {
1424 while (CONSP (list)
1425 && (!CONSP (XCAR (list))
1426 || !EQ (XCAR (XCAR (list)), key)))
1427 list = XCDR (list);
1428
1429 return CAR_SAFE (list);
1430 }
1431
1432 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1433 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1434 The value is actually the first element of LIST whose car equals KEY. */)
1435 (Lisp_Object key, Lisp_Object list)
1436 {
1437 Lisp_Object car;
1438
1439 while (1)
1440 {
1441 if (!CONSP (list)
1442 || (CONSP (XCAR (list))
1443 && (car = XCAR (XCAR (list)),
1444 EQ (car, key) || !NILP (Fequal (car, key)))))
1445 break;
1446
1447 list = XCDR (list);
1448 if (!CONSP (list)
1449 || (CONSP (XCAR (list))
1450 && (car = XCAR (XCAR (list)),
1451 EQ (car, key) || !NILP (Fequal (car, key)))))
1452 break;
1453
1454 list = XCDR (list);
1455 if (!CONSP (list)
1456 || (CONSP (XCAR (list))
1457 && (car = XCAR (XCAR (list)),
1458 EQ (car, key) || !NILP (Fequal (car, key)))))
1459 break;
1460
1461 list = XCDR (list);
1462 QUIT;
1463 }
1464
1465 return CAR (list);
1466 }
1467
1468 /* Like Fassoc but never report an error and do not allow quits.
1469 Use only on lists known never to be circular. */
1470
1471 Lisp_Object
1472 assoc_no_quit (Lisp_Object key, Lisp_Object list)
1473 {
1474 while (CONSP (list)
1475 && (!CONSP (XCAR (list))
1476 || (!EQ (XCAR (XCAR (list)), key)
1477 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1478 list = XCDR (list);
1479
1480 return CONSP (list) ? XCAR (list) : Qnil;
1481 }
1482
1483 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1484 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1485 The value is actually the first element of LIST whose cdr is KEY. */)
1486 (register Lisp_Object key, Lisp_Object list)
1487 {
1488 while (1)
1489 {
1490 if (!CONSP (list)
1491 || (CONSP (XCAR (list))
1492 && EQ (XCDR (XCAR (list)), key)))
1493 break;
1494
1495 list = XCDR (list);
1496 if (!CONSP (list)
1497 || (CONSP (XCAR (list))
1498 && EQ (XCDR (XCAR (list)), key)))
1499 break;
1500
1501 list = XCDR (list);
1502 if (!CONSP (list)
1503 || (CONSP (XCAR (list))
1504 && EQ (XCDR (XCAR (list)), key)))
1505 break;
1506
1507 list = XCDR (list);
1508 QUIT;
1509 }
1510
1511 return CAR (list);
1512 }
1513
1514 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1515 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1516 The value is actually the first element of LIST whose cdr equals KEY. */)
1517 (Lisp_Object key, Lisp_Object list)
1518 {
1519 Lisp_Object cdr;
1520
1521 while (1)
1522 {
1523 if (!CONSP (list)
1524 || (CONSP (XCAR (list))
1525 && (cdr = XCDR (XCAR (list)),
1526 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1527 break;
1528
1529 list = XCDR (list);
1530 if (!CONSP (list)
1531 || (CONSP (XCAR (list))
1532 && (cdr = XCDR (XCAR (list)),
1533 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1534 break;
1535
1536 list = XCDR (list);
1537 if (!CONSP (list)
1538 || (CONSP (XCAR (list))
1539 && (cdr = XCDR (XCAR (list)),
1540 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1541 break;
1542
1543 list = XCDR (list);
1544 QUIT;
1545 }
1546
1547 return CAR (list);
1548 }
1549 \f
1550 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1551 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1552 The modified LIST is returned. Comparison is done with `eq'.
1553 If the first member of LIST is ELT, there is no way to remove it by side effect;
1554 therefore, write `(setq foo (delq element foo))'
1555 to be sure of changing the value of `foo'. */)
1556 (register Lisp_Object elt, Lisp_Object list)
1557 {
1558 register Lisp_Object tail, prev;
1559 register Lisp_Object tem;
1560
1561 tail = list;
1562 prev = Qnil;
1563 while (!NILP (tail))
1564 {
1565 CHECK_LIST_CONS (tail, list);
1566 tem = XCAR (tail);
1567 if (EQ (elt, tem))
1568 {
1569 if (NILP (prev))
1570 list = XCDR (tail);
1571 else
1572 Fsetcdr (prev, XCDR (tail));
1573 }
1574 else
1575 prev = tail;
1576 tail = XCDR (tail);
1577 QUIT;
1578 }
1579 return list;
1580 }
1581
1582 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1583 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1584 SEQ must be a list, a vector, or a string.
1585 The modified SEQ is returned. Comparison is done with `equal'.
1586 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1587 is not a side effect; it is simply using a different sequence.
1588 Therefore, write `(setq foo (delete element foo))'
1589 to be sure of changing the value of `foo'. */)
1590 (Lisp_Object elt, Lisp_Object seq)
1591 {
1592 if (VECTORP (seq))
1593 {
1594 EMACS_INT i, n;
1595
1596 for (i = n = 0; i < ASIZE (seq); ++i)
1597 if (NILP (Fequal (AREF (seq, i), elt)))
1598 ++n;
1599
1600 if (n != ASIZE (seq))
1601 {
1602 struct Lisp_Vector *p = allocate_vector (n);
1603
1604 for (i = n = 0; i < ASIZE (seq); ++i)
1605 if (NILP (Fequal (AREF (seq, i), elt)))
1606 p->contents[n++] = AREF (seq, i);
1607
1608 XSETVECTOR (seq, p);
1609 }
1610 }
1611 else if (STRINGP (seq))
1612 {
1613 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1614 int c;
1615
1616 for (i = nchars = nbytes = ibyte = 0;
1617 i < SCHARS (seq);
1618 ++i, ibyte += cbytes)
1619 {
1620 if (STRING_MULTIBYTE (seq))
1621 {
1622 c = STRING_CHAR (SDATA (seq) + ibyte);
1623 cbytes = CHAR_BYTES (c);
1624 }
1625 else
1626 {
1627 c = SREF (seq, i);
1628 cbytes = 1;
1629 }
1630
1631 if (!INTEGERP (elt) || c != XINT (elt))
1632 {
1633 ++nchars;
1634 nbytes += cbytes;
1635 }
1636 }
1637
1638 if (nchars != SCHARS (seq))
1639 {
1640 Lisp_Object tem;
1641
1642 tem = make_uninit_multibyte_string (nchars, nbytes);
1643 if (!STRING_MULTIBYTE (seq))
1644 STRING_SET_UNIBYTE (tem);
1645
1646 for (i = nchars = nbytes = ibyte = 0;
1647 i < SCHARS (seq);
1648 ++i, ibyte += cbytes)
1649 {
1650 if (STRING_MULTIBYTE (seq))
1651 {
1652 c = STRING_CHAR (SDATA (seq) + ibyte);
1653 cbytes = CHAR_BYTES (c);
1654 }
1655 else
1656 {
1657 c = SREF (seq, i);
1658 cbytes = 1;
1659 }
1660
1661 if (!INTEGERP (elt) || c != XINT (elt))
1662 {
1663 unsigned char *from = SDATA (seq) + ibyte;
1664 unsigned char *to = SDATA (tem) + nbytes;
1665 EMACS_INT n;
1666
1667 ++nchars;
1668 nbytes += cbytes;
1669
1670 for (n = cbytes; n--; )
1671 *to++ = *from++;
1672 }
1673 }
1674
1675 seq = tem;
1676 }
1677 }
1678 else
1679 {
1680 Lisp_Object tail, prev;
1681
1682 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1683 {
1684 CHECK_LIST_CONS (tail, seq);
1685
1686 if (!NILP (Fequal (elt, XCAR (tail))))
1687 {
1688 if (NILP (prev))
1689 seq = XCDR (tail);
1690 else
1691 Fsetcdr (prev, XCDR (tail));
1692 }
1693 else
1694 prev = tail;
1695 QUIT;
1696 }
1697 }
1698
1699 return seq;
1700 }
1701
1702 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1703 doc: /* Reverse LIST by modifying cdr pointers.
1704 Return the reversed list. */)
1705 (Lisp_Object list)
1706 {
1707 register Lisp_Object prev, tail, next;
1708
1709 if (NILP (list)) return list;
1710 prev = Qnil;
1711 tail = list;
1712 while (!NILP (tail))
1713 {
1714 QUIT;
1715 CHECK_LIST_CONS (tail, list);
1716 next = XCDR (tail);
1717 Fsetcdr (tail, prev);
1718 prev = tail;
1719 tail = next;
1720 }
1721 return prev;
1722 }
1723
1724 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1725 doc: /* Reverse LIST, copying. Return the reversed list.
1726 See also the function `nreverse', which is used more often. */)
1727 (Lisp_Object list)
1728 {
1729 Lisp_Object new;
1730
1731 for (new = Qnil; CONSP (list); list = XCDR (list))
1732 {
1733 QUIT;
1734 new = Fcons (XCAR (list), new);
1735 }
1736 CHECK_LIST_END (list, list);
1737 return new;
1738 }
1739 \f
1740 Lisp_Object merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred);
1741
1742 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1743 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1744 Returns the sorted list. LIST is modified by side effects.
1745 PREDICATE is called with two elements of LIST, and should return non-nil
1746 if the first element should sort before the second. */)
1747 (Lisp_Object list, Lisp_Object predicate)
1748 {
1749 Lisp_Object front, back;
1750 register Lisp_Object len, tem;
1751 struct gcpro gcpro1, gcpro2;
1752 register int length;
1753
1754 front = list;
1755 len = Flength (list);
1756 length = XINT (len);
1757 if (length < 2)
1758 return list;
1759
1760 XSETINT (len, (length / 2) - 1);
1761 tem = Fnthcdr (len, list);
1762 back = Fcdr (tem);
1763 Fsetcdr (tem, Qnil);
1764
1765 GCPRO2 (front, back);
1766 front = Fsort (front, predicate);
1767 back = Fsort (back, predicate);
1768 UNGCPRO;
1769 return merge (front, back, predicate);
1770 }
1771
1772 Lisp_Object
1773 merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred)
1774 {
1775 Lisp_Object value;
1776 register Lisp_Object tail;
1777 Lisp_Object tem;
1778 register Lisp_Object l1, l2;
1779 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1780
1781 l1 = org_l1;
1782 l2 = org_l2;
1783 tail = Qnil;
1784 value = Qnil;
1785
1786 /* It is sufficient to protect org_l1 and org_l2.
1787 When l1 and l2 are updated, we copy the new values
1788 back into the org_ vars. */
1789 GCPRO4 (org_l1, org_l2, pred, value);
1790
1791 while (1)
1792 {
1793 if (NILP (l1))
1794 {
1795 UNGCPRO;
1796 if (NILP (tail))
1797 return l2;
1798 Fsetcdr (tail, l2);
1799 return value;
1800 }
1801 if (NILP (l2))
1802 {
1803 UNGCPRO;
1804 if (NILP (tail))
1805 return l1;
1806 Fsetcdr (tail, l1);
1807 return value;
1808 }
1809 tem = call2 (pred, Fcar (l2), Fcar (l1));
1810 if (NILP (tem))
1811 {
1812 tem = l1;
1813 l1 = Fcdr (l1);
1814 org_l1 = l1;
1815 }
1816 else
1817 {
1818 tem = l2;
1819 l2 = Fcdr (l2);
1820 org_l2 = l2;
1821 }
1822 if (NILP (tail))
1823 value = tem;
1824 else
1825 Fsetcdr (tail, tem);
1826 tail = tem;
1827 }
1828 }
1829
1830 \f
1831 /* This does not check for quits. That is safe since it must terminate. */
1832
1833 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1834 doc: /* Extract a value from a property list.
1835 PLIST is a property list, which is a list of the form
1836 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1837 corresponding to the given PROP, or nil if PROP is not one of the
1838 properties on the list. This function never signals an error. */)
1839 (Lisp_Object plist, Lisp_Object prop)
1840 {
1841 Lisp_Object tail, halftail;
1842
1843 /* halftail is used to detect circular lists. */
1844 tail = halftail = plist;
1845 while (CONSP (tail) && CONSP (XCDR (tail)))
1846 {
1847 if (EQ (prop, XCAR (tail)))
1848 return XCAR (XCDR (tail));
1849
1850 tail = XCDR (XCDR (tail));
1851 halftail = XCDR (halftail);
1852 if (EQ (tail, halftail))
1853 break;
1854
1855 #if 0 /* Unsafe version. */
1856 /* This function can be called asynchronously
1857 (setup_coding_system). Don't QUIT in that case. */
1858 if (!interrupt_input_blocked)
1859 QUIT;
1860 #endif
1861 }
1862
1863 return Qnil;
1864 }
1865
1866 DEFUN ("get", Fget, Sget, 2, 2, 0,
1867 doc: /* Return the value of SYMBOL's PROPNAME property.
1868 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1869 (Lisp_Object symbol, Lisp_Object propname)
1870 {
1871 CHECK_SYMBOL (symbol);
1872 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1873 }
1874
1875 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1876 doc: /* Change value in PLIST of PROP to VAL.
1877 PLIST is a property list, which is a list of the form
1878 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1879 If PROP is already a property on the list, its value is set to VAL,
1880 otherwise the new PROP VAL pair is added. The new plist is returned;
1881 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1882 The PLIST is modified by side effects. */)
1883 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
1884 {
1885 register Lisp_Object tail, prev;
1886 Lisp_Object newcell;
1887 prev = Qnil;
1888 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1889 tail = XCDR (XCDR (tail)))
1890 {
1891 if (EQ (prop, XCAR (tail)))
1892 {
1893 Fsetcar (XCDR (tail), val);
1894 return plist;
1895 }
1896
1897 prev = tail;
1898 QUIT;
1899 }
1900 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
1901 if (NILP (prev))
1902 return newcell;
1903 else
1904 Fsetcdr (XCDR (prev), newcell);
1905 return plist;
1906 }
1907
1908 DEFUN ("put", Fput, Sput, 3, 3, 0,
1909 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
1910 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
1911 (Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
1912 {
1913 CHECK_SYMBOL (symbol);
1914 XSYMBOL (symbol)->plist
1915 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
1916 return value;
1917 }
1918 \f
1919 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
1920 doc: /* Extract a value from a property list, comparing with `equal'.
1921 PLIST is a property list, which is a list of the form
1922 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1923 corresponding to the given PROP, or nil if PROP is not
1924 one of the properties on the list. */)
1925 (Lisp_Object plist, Lisp_Object prop)
1926 {
1927 Lisp_Object tail;
1928
1929 for (tail = plist;
1930 CONSP (tail) && CONSP (XCDR (tail));
1931 tail = XCDR (XCDR (tail)))
1932 {
1933 if (! NILP (Fequal (prop, XCAR (tail))))
1934 return XCAR (XCDR (tail));
1935
1936 QUIT;
1937 }
1938
1939 CHECK_LIST_END (tail, prop);
1940
1941 return Qnil;
1942 }
1943
1944 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
1945 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
1946 PLIST is a property list, which is a list of the form
1947 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
1948 If PROP is already a property on the list, its value is set to VAL,
1949 otherwise the new PROP VAL pair is added. The new plist is returned;
1950 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
1951 The PLIST is modified by side effects. */)
1952 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
1953 {
1954 register Lisp_Object tail, prev;
1955 Lisp_Object newcell;
1956 prev = Qnil;
1957 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1958 tail = XCDR (XCDR (tail)))
1959 {
1960 if (! NILP (Fequal (prop, XCAR (tail))))
1961 {
1962 Fsetcar (XCDR (tail), val);
1963 return plist;
1964 }
1965
1966 prev = tail;
1967 QUIT;
1968 }
1969 newcell = Fcons (prop, Fcons (val, Qnil));
1970 if (NILP (prev))
1971 return newcell;
1972 else
1973 Fsetcdr (XCDR (prev), newcell);
1974 return plist;
1975 }
1976 \f
1977 DEFUN ("eql", Feql, Seql, 2, 2, 0,
1978 doc: /* Return t if the two args are the same Lisp object.
1979 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
1980 (Lisp_Object obj1, Lisp_Object obj2)
1981 {
1982 if (FLOATP (obj1))
1983 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
1984 else
1985 return EQ (obj1, obj2) ? Qt : Qnil;
1986 }
1987
1988 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
1989 doc: /* Return t if two Lisp objects have similar structure and contents.
1990 They must have the same data type.
1991 Conses are compared by comparing the cars and the cdrs.
1992 Vectors and strings are compared element by element.
1993 Numbers are compared by value, but integers cannot equal floats.
1994 (Use `=' if you want integers and floats to be able to be equal.)
1995 Symbols must match exactly. */)
1996 (register Lisp_Object o1, Lisp_Object o2)
1997 {
1998 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
1999 }
2000
2001 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2002 doc: /* Return t if two Lisp objects have similar structure and contents.
2003 This is like `equal' except that it compares the text properties
2004 of strings. (`equal' ignores text properties.) */)
2005 (register Lisp_Object o1, Lisp_Object o2)
2006 {
2007 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
2008 }
2009
2010 /* DEPTH is current depth of recursion. Signal an error if it
2011 gets too deep.
2012 PROPS, if non-nil, means compare string text properties too. */
2013
2014 static int
2015 internal_equal (register Lisp_Object o1, register Lisp_Object o2, int depth, int props)
2016 {
2017 if (depth > 200)
2018 error ("Stack overflow in equal");
2019
2020 tail_recurse:
2021 QUIT;
2022 if (EQ (o1, o2))
2023 return 1;
2024 if (XTYPE (o1) != XTYPE (o2))
2025 return 0;
2026
2027 switch (XTYPE (o1))
2028 {
2029 case Lisp_Float:
2030 {
2031 double d1, d2;
2032
2033 d1 = extract_float (o1);
2034 d2 = extract_float (o2);
2035 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2036 though they are not =. */
2037 return d1 == d2 || (d1 != d1 && d2 != d2);
2038 }
2039
2040 case Lisp_Cons:
2041 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
2042 return 0;
2043 o1 = XCDR (o1);
2044 o2 = XCDR (o2);
2045 goto tail_recurse;
2046
2047 case Lisp_Misc:
2048 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2049 return 0;
2050 if (OVERLAYP (o1))
2051 {
2052 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2053 depth + 1, props)
2054 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2055 depth + 1, props))
2056 return 0;
2057 o1 = XOVERLAY (o1)->plist;
2058 o2 = XOVERLAY (o2)->plist;
2059 goto tail_recurse;
2060 }
2061 if (MARKERP (o1))
2062 {
2063 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2064 && (XMARKER (o1)->buffer == 0
2065 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2066 }
2067 break;
2068
2069 case Lisp_Vectorlike:
2070 {
2071 register int i;
2072 EMACS_INT size = ASIZE (o1);
2073 /* Pseudovectors have the type encoded in the size field, so this test
2074 actually checks that the objects have the same type as well as the
2075 same size. */
2076 if (ASIZE (o2) != size)
2077 return 0;
2078 /* Boolvectors are compared much like strings. */
2079 if (BOOL_VECTOR_P (o1))
2080 {
2081 int size_in_chars
2082 = ((XBOOL_VECTOR (o1)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2083 / BOOL_VECTOR_BITS_PER_CHAR);
2084
2085 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2086 return 0;
2087 if (memcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2088 size_in_chars))
2089 return 0;
2090 return 1;
2091 }
2092 if (WINDOW_CONFIGURATIONP (o1))
2093 return compare_window_configurations (o1, o2, 0);
2094
2095 /* Aside from them, only true vectors, char-tables, compiled
2096 functions, and fonts (font-spec, font-entity, font-ojbect)
2097 are sensible to compare, so eliminate the others now. */
2098 if (size & PSEUDOVECTOR_FLAG)
2099 {
2100 if (!(size & (PVEC_COMPILED
2101 | PVEC_CHAR_TABLE | PVEC_SUB_CHAR_TABLE | PVEC_FONT)))
2102 return 0;
2103 size &= PSEUDOVECTOR_SIZE_MASK;
2104 }
2105 for (i = 0; i < size; i++)
2106 {
2107 Lisp_Object v1, v2;
2108 v1 = AREF (o1, i);
2109 v2 = AREF (o2, i);
2110 if (!internal_equal (v1, v2, depth + 1, props))
2111 return 0;
2112 }
2113 return 1;
2114 }
2115 break;
2116
2117 case Lisp_String:
2118 if (SCHARS (o1) != SCHARS (o2))
2119 return 0;
2120 if (SBYTES (o1) != SBYTES (o2))
2121 return 0;
2122 if (memcmp (SDATA (o1), SDATA (o2), SBYTES (o1)))
2123 return 0;
2124 if (props && !compare_string_intervals (o1, o2))
2125 return 0;
2126 return 1;
2127
2128 default:
2129 break;
2130 }
2131
2132 return 0;
2133 }
2134 \f
2135
2136 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2137 doc: /* Store each element of ARRAY with ITEM.
2138 ARRAY is a vector, string, char-table, or bool-vector. */)
2139 (Lisp_Object array, Lisp_Object item)
2140 {
2141 register EMACS_INT size, index;
2142 int charval;
2143
2144 if (VECTORP (array))
2145 {
2146 register Lisp_Object *p = XVECTOR (array)->contents;
2147 size = ASIZE (array);
2148 for (index = 0; index < size; index++)
2149 p[index] = item;
2150 }
2151 else if (CHAR_TABLE_P (array))
2152 {
2153 int i;
2154
2155 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2156 XCHAR_TABLE (array)->contents[i] = item;
2157 XCHAR_TABLE (array)->defalt = item;
2158 }
2159 else if (STRINGP (array))
2160 {
2161 register unsigned char *p = SDATA (array);
2162 CHECK_NUMBER (item);
2163 charval = XINT (item);
2164 size = SCHARS (array);
2165 if (STRING_MULTIBYTE (array))
2166 {
2167 unsigned char str[MAX_MULTIBYTE_LENGTH];
2168 int len = CHAR_STRING (charval, str);
2169 EMACS_INT size_byte = SBYTES (array);
2170 unsigned char *p1 = p, *endp = p + size_byte;
2171 int i;
2172
2173 if (size != size_byte)
2174 while (p1 < endp)
2175 {
2176 int this_len = BYTES_BY_CHAR_HEAD (*p1);
2177 if (len != this_len)
2178 error ("Attempt to change byte length of a string");
2179 p1 += this_len;
2180 }
2181 for (i = 0; i < size_byte; i++)
2182 *p++ = str[i % len];
2183 }
2184 else
2185 for (index = 0; index < size; index++)
2186 p[index] = charval;
2187 }
2188 else if (BOOL_VECTOR_P (array))
2189 {
2190 register unsigned char *p = XBOOL_VECTOR (array)->data;
2191 int size_in_chars
2192 = ((XBOOL_VECTOR (array)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2193 / BOOL_VECTOR_BITS_PER_CHAR);
2194
2195 charval = (! NILP (item) ? -1 : 0);
2196 for (index = 0; index < size_in_chars - 1; index++)
2197 p[index] = charval;
2198 if (index < size_in_chars)
2199 {
2200 /* Mask out bits beyond the vector size. */
2201 if (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)
2202 charval &= (1 << (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2203 p[index] = charval;
2204 }
2205 }
2206 else
2207 wrong_type_argument (Qarrayp, array);
2208 return array;
2209 }
2210
2211 DEFUN ("clear-string", Fclear_string, Sclear_string,
2212 1, 1, 0,
2213 doc: /* Clear the contents of STRING.
2214 This makes STRING unibyte and may change its length. */)
2215 (Lisp_Object string)
2216 {
2217 EMACS_INT len;
2218 CHECK_STRING (string);
2219 len = SBYTES (string);
2220 memset (SDATA (string), 0, len);
2221 STRING_SET_CHARS (string, len);
2222 STRING_SET_UNIBYTE (string);
2223 return Qnil;
2224 }
2225 \f
2226 /* ARGSUSED */
2227 Lisp_Object
2228 nconc2 (Lisp_Object s1, Lisp_Object s2)
2229 {
2230 Lisp_Object args[2];
2231 args[0] = s1;
2232 args[1] = s2;
2233 return Fnconc (2, args);
2234 }
2235
2236 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2237 doc: /* Concatenate any number of lists by altering them.
2238 Only the last argument is not altered, and need not be a list.
2239 usage: (nconc &rest LISTS) */)
2240 (int nargs, Lisp_Object *args)
2241 {
2242 register int argnum;
2243 register Lisp_Object tail, tem, val;
2244
2245 val = tail = Qnil;
2246
2247 for (argnum = 0; argnum < nargs; argnum++)
2248 {
2249 tem = args[argnum];
2250 if (NILP (tem)) continue;
2251
2252 if (NILP (val))
2253 val = tem;
2254
2255 if (argnum + 1 == nargs) break;
2256
2257 CHECK_LIST_CONS (tem, tem);
2258
2259 while (CONSP (tem))
2260 {
2261 tail = tem;
2262 tem = XCDR (tail);
2263 QUIT;
2264 }
2265
2266 tem = args[argnum + 1];
2267 Fsetcdr (tail, tem);
2268 if (NILP (tem))
2269 args[argnum + 1] = tail;
2270 }
2271
2272 return val;
2273 }
2274 \f
2275 /* This is the guts of all mapping functions.
2276 Apply FN to each element of SEQ, one by one,
2277 storing the results into elements of VALS, a C vector of Lisp_Objects.
2278 LENI is the length of VALS, which should also be the length of SEQ. */
2279
2280 static void
2281 mapcar1 (EMACS_INT leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
2282 {
2283 register Lisp_Object tail;
2284 Lisp_Object dummy;
2285 register EMACS_INT i;
2286 struct gcpro gcpro1, gcpro2, gcpro3;
2287
2288 if (vals)
2289 {
2290 /* Don't let vals contain any garbage when GC happens. */
2291 for (i = 0; i < leni; i++)
2292 vals[i] = Qnil;
2293
2294 GCPRO3 (dummy, fn, seq);
2295 gcpro1.var = vals;
2296 gcpro1.nvars = leni;
2297 }
2298 else
2299 GCPRO2 (fn, seq);
2300 /* We need not explicitly protect `tail' because it is used only on lists, and
2301 1) lists are not relocated and 2) the list is marked via `seq' so will not
2302 be freed */
2303
2304 if (VECTORP (seq))
2305 {
2306 for (i = 0; i < leni; i++)
2307 {
2308 dummy = call1 (fn, AREF (seq, i));
2309 if (vals)
2310 vals[i] = dummy;
2311 }
2312 }
2313 else if (BOOL_VECTOR_P (seq))
2314 {
2315 for (i = 0; i < leni; i++)
2316 {
2317 int byte;
2318 byte = XBOOL_VECTOR (seq)->data[i / BOOL_VECTOR_BITS_PER_CHAR];
2319 dummy = (byte & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR))) ? Qt : Qnil;
2320 dummy = call1 (fn, dummy);
2321 if (vals)
2322 vals[i] = dummy;
2323 }
2324 }
2325 else if (STRINGP (seq))
2326 {
2327 EMACS_INT i_byte;
2328
2329 for (i = 0, i_byte = 0; i < leni;)
2330 {
2331 int c;
2332 EMACS_INT i_before = i;
2333
2334 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2335 XSETFASTINT (dummy, c);
2336 dummy = call1 (fn, dummy);
2337 if (vals)
2338 vals[i_before] = dummy;
2339 }
2340 }
2341 else /* Must be a list, since Flength did not get an error */
2342 {
2343 tail = seq;
2344 for (i = 0; i < leni && CONSP (tail); i++)
2345 {
2346 dummy = call1 (fn, XCAR (tail));
2347 if (vals)
2348 vals[i] = dummy;
2349 tail = XCDR (tail);
2350 }
2351 }
2352
2353 UNGCPRO;
2354 }
2355
2356 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2357 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2358 In between each pair of results, stick in SEPARATOR. Thus, " " as
2359 SEPARATOR results in spaces between the values returned by FUNCTION.
2360 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2361 (Lisp_Object function, Lisp_Object sequence, Lisp_Object separator)
2362 {
2363 Lisp_Object len;
2364 register EMACS_INT leni;
2365 int nargs;
2366 register Lisp_Object *args;
2367 register EMACS_INT i;
2368 struct gcpro gcpro1;
2369 Lisp_Object ret;
2370 USE_SAFE_ALLOCA;
2371
2372 len = Flength (sequence);
2373 if (CHAR_TABLE_P (sequence))
2374 wrong_type_argument (Qlistp, sequence);
2375 leni = XINT (len);
2376 nargs = leni + leni - 1;
2377 if (nargs < 0) return empty_unibyte_string;
2378
2379 SAFE_ALLOCA_LISP (args, nargs);
2380
2381 GCPRO1 (separator);
2382 mapcar1 (leni, args, function, sequence);
2383 UNGCPRO;
2384
2385 for (i = leni - 1; i > 0; i--)
2386 args[i + i] = args[i];
2387
2388 for (i = 1; i < nargs; i += 2)
2389 args[i] = separator;
2390
2391 ret = Fconcat (nargs, args);
2392 SAFE_FREE ();
2393
2394 return ret;
2395 }
2396
2397 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2398 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2399 The result is a list just as long as SEQUENCE.
2400 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2401 (Lisp_Object function, Lisp_Object sequence)
2402 {
2403 register Lisp_Object len;
2404 register EMACS_INT leni;
2405 register Lisp_Object *args;
2406 Lisp_Object ret;
2407 USE_SAFE_ALLOCA;
2408
2409 len = Flength (sequence);
2410 if (CHAR_TABLE_P (sequence))
2411 wrong_type_argument (Qlistp, sequence);
2412 leni = XFASTINT (len);
2413
2414 SAFE_ALLOCA_LISP (args, leni);
2415
2416 mapcar1 (leni, args, function, sequence);
2417
2418 ret = Flist (leni, args);
2419 SAFE_FREE ();
2420
2421 return ret;
2422 }
2423
2424 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2425 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2426 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2427 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2428 (Lisp_Object function, Lisp_Object sequence)
2429 {
2430 register EMACS_INT leni;
2431
2432 leni = XFASTINT (Flength (sequence));
2433 if (CHAR_TABLE_P (sequence))
2434 wrong_type_argument (Qlistp, sequence);
2435 mapcar1 (leni, 0, function, sequence);
2436
2437 return sequence;
2438 }
2439 \f
2440 /* This is how C code calls `yes-or-no-p' and allows the user
2441 to redefined it.
2442
2443 Anything that calls this function must protect from GC! */
2444
2445 Lisp_Object
2446 do_yes_or_no_p (Lisp_Object prompt)
2447 {
2448 return call1 (intern ("yes-or-no-p"), prompt);
2449 }
2450
2451 /* Anything that calls this function must protect from GC! */
2452
2453 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2454 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2455 PROMPT is the string to display to ask the question. It should end in
2456 a space; `yes-or-no-p' adds \"(yes or no) \" to it.
2457
2458 The user must confirm the answer with RET, and can edit it until it
2459 has been confirmed.
2460
2461 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2462 is nil, and `use-dialog-box' is non-nil. */)
2463 (Lisp_Object prompt)
2464 {
2465 register Lisp_Object ans;
2466 Lisp_Object args[2];
2467 struct gcpro gcpro1;
2468
2469 CHECK_STRING (prompt);
2470
2471 #ifdef HAVE_MENUS
2472 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2473 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2474 && use_dialog_box
2475 && have_menus_p ())
2476 {
2477 Lisp_Object pane, menu, obj;
2478 redisplay_preserve_echo_area (4);
2479 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2480 Fcons (Fcons (build_string ("No"), Qnil),
2481 Qnil));
2482 GCPRO1 (pane);
2483 menu = Fcons (prompt, pane);
2484 obj = Fx_popup_dialog (Qt, menu, Qnil);
2485 UNGCPRO;
2486 return obj;
2487 }
2488 #endif /* HAVE_MENUS */
2489
2490 args[0] = prompt;
2491 args[1] = build_string ("(yes or no) ");
2492 prompt = Fconcat (2, args);
2493
2494 GCPRO1 (prompt);
2495
2496 while (1)
2497 {
2498 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2499 Qyes_or_no_p_history, Qnil,
2500 Qnil));
2501 if (SCHARS (ans) == 3 && !strcmp (SDATA (ans), "yes"))
2502 {
2503 UNGCPRO;
2504 return Qt;
2505 }
2506 if (SCHARS (ans) == 2 && !strcmp (SDATA (ans), "no"))
2507 {
2508 UNGCPRO;
2509 return Qnil;
2510 }
2511
2512 Fding (Qnil);
2513 Fdiscard_input ();
2514 message ("Please answer yes or no.");
2515 Fsleep_for (make_number (2), Qnil);
2516 }
2517 }
2518 \f
2519 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2520 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2521
2522 Each of the three load averages is multiplied by 100, then converted
2523 to integer.
2524
2525 When USE-FLOATS is non-nil, floats will be used instead of integers.
2526 These floats are not multiplied by 100.
2527
2528 If the 5-minute or 15-minute load averages are not available, return a
2529 shortened list, containing only those averages which are available.
2530
2531 An error is thrown if the load average can't be obtained. In some
2532 cases making it work would require Emacs being installed setuid or
2533 setgid so that it can read kernel information, and that usually isn't
2534 advisable. */)
2535 (Lisp_Object use_floats)
2536 {
2537 double load_ave[3];
2538 int loads = getloadavg (load_ave, 3);
2539 Lisp_Object ret = Qnil;
2540
2541 if (loads < 0)
2542 error ("load-average not implemented for this operating system");
2543
2544 while (loads-- > 0)
2545 {
2546 Lisp_Object load = (NILP (use_floats) ?
2547 make_number ((int) (100.0 * load_ave[loads]))
2548 : make_float (load_ave[loads]));
2549 ret = Fcons (load, ret);
2550 }
2551
2552 return ret;
2553 }
2554 \f
2555 Lisp_Object Qsubfeatures;
2556
2557 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2558 doc: /* Return t if FEATURE is present in this Emacs.
2559
2560 Use this to conditionalize execution of lisp code based on the
2561 presence or absence of Emacs or environment extensions.
2562 Use `provide' to declare that a feature is available. This function
2563 looks at the value of the variable `features'. The optional argument
2564 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2565 (Lisp_Object feature, Lisp_Object subfeature)
2566 {
2567 register Lisp_Object tem;
2568 CHECK_SYMBOL (feature);
2569 tem = Fmemq (feature, Vfeatures);
2570 if (!NILP (tem) && !NILP (subfeature))
2571 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2572 return (NILP (tem)) ? Qnil : Qt;
2573 }
2574
2575 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2576 doc: /* Announce that FEATURE is a feature of the current Emacs.
2577 The optional argument SUBFEATURES should be a list of symbols listing
2578 particular subfeatures supported in this version of FEATURE. */)
2579 (Lisp_Object feature, Lisp_Object subfeatures)
2580 {
2581 register Lisp_Object tem;
2582 CHECK_SYMBOL (feature);
2583 CHECK_LIST (subfeatures);
2584 if (!NILP (Vautoload_queue))
2585 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2586 Vautoload_queue);
2587 tem = Fmemq (feature, Vfeatures);
2588 if (NILP (tem))
2589 Vfeatures = Fcons (feature, Vfeatures);
2590 if (!NILP (subfeatures))
2591 Fput (feature, Qsubfeatures, subfeatures);
2592 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2593
2594 /* Run any load-hooks for this file. */
2595 tem = Fassq (feature, Vafter_load_alist);
2596 if (CONSP (tem))
2597 Fprogn (XCDR (tem));
2598
2599 return feature;
2600 }
2601 \f
2602 /* `require' and its subroutines. */
2603
2604 /* List of features currently being require'd, innermost first. */
2605
2606 Lisp_Object require_nesting_list;
2607
2608 Lisp_Object
2609 require_unwind (Lisp_Object old_value)
2610 {
2611 return require_nesting_list = old_value;
2612 }
2613
2614 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2615 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2616 If FEATURE is not a member of the list `features', then the feature
2617 is not loaded; so load the file FILENAME.
2618 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2619 and `load' will try to load this name appended with the suffix `.elc' or
2620 `.el', in that order. The name without appended suffix will not be used.
2621 If the optional third argument NOERROR is non-nil,
2622 then return nil if the file is not found instead of signaling an error.
2623 Normally the return value is FEATURE.
2624 The normal messages at start and end of loading FILENAME are suppressed. */)
2625 (Lisp_Object feature, Lisp_Object filename, Lisp_Object noerror)
2626 {
2627 register Lisp_Object tem;
2628 struct gcpro gcpro1, gcpro2;
2629 int from_file = load_in_progress;
2630
2631 CHECK_SYMBOL (feature);
2632
2633 /* Record the presence of `require' in this file
2634 even if the feature specified is already loaded.
2635 But not more than once in any file,
2636 and not when we aren't loading or reading from a file. */
2637 if (!from_file)
2638 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2639 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2640 from_file = 1;
2641
2642 if (from_file)
2643 {
2644 tem = Fcons (Qrequire, feature);
2645 if (NILP (Fmember (tem, Vcurrent_load_list)))
2646 LOADHIST_ATTACH (tem);
2647 }
2648 tem = Fmemq (feature, Vfeatures);
2649
2650 if (NILP (tem))
2651 {
2652 int count = SPECPDL_INDEX ();
2653 int nesting = 0;
2654
2655 /* This is to make sure that loadup.el gives a clear picture
2656 of what files are preloaded and when. */
2657 if (! NILP (Vpurify_flag))
2658 error ("(require %s) while preparing to dump",
2659 SDATA (SYMBOL_NAME (feature)));
2660
2661 /* A certain amount of recursive `require' is legitimate,
2662 but if we require the same feature recursively 3 times,
2663 signal an error. */
2664 tem = require_nesting_list;
2665 while (! NILP (tem))
2666 {
2667 if (! NILP (Fequal (feature, XCAR (tem))))
2668 nesting++;
2669 tem = XCDR (tem);
2670 }
2671 if (nesting > 3)
2672 error ("Recursive `require' for feature `%s'",
2673 SDATA (SYMBOL_NAME (feature)));
2674
2675 /* Update the list for any nested `require's that occur. */
2676 record_unwind_protect (require_unwind, require_nesting_list);
2677 require_nesting_list = Fcons (feature, require_nesting_list);
2678
2679 /* Value saved here is to be restored into Vautoload_queue */
2680 record_unwind_protect (un_autoload, Vautoload_queue);
2681 Vautoload_queue = Qt;
2682
2683 /* Load the file. */
2684 GCPRO2 (feature, filename);
2685 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2686 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2687 UNGCPRO;
2688
2689 /* If load failed entirely, return nil. */
2690 if (NILP (tem))
2691 return unbind_to (count, Qnil);
2692
2693 tem = Fmemq (feature, Vfeatures);
2694 if (NILP (tem))
2695 error ("Required feature `%s' was not provided",
2696 SDATA (SYMBOL_NAME (feature)));
2697
2698 /* Once loading finishes, don't undo it. */
2699 Vautoload_queue = Qt;
2700 feature = unbind_to (count, feature);
2701 }
2702
2703 return feature;
2704 }
2705 \f
2706 /* Primitives for work of the "widget" library.
2707 In an ideal world, this section would not have been necessary.
2708 However, lisp function calls being as slow as they are, it turns
2709 out that some functions in the widget library (wid-edit.el) are the
2710 bottleneck of Widget operation. Here is their translation to C,
2711 for the sole reason of efficiency. */
2712
2713 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
2714 doc: /* Return non-nil if PLIST has the property PROP.
2715 PLIST is a property list, which is a list of the form
2716 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
2717 Unlike `plist-get', this allows you to distinguish between a missing
2718 property and a property with the value nil.
2719 The value is actually the tail of PLIST whose car is PROP. */)
2720 (Lisp_Object plist, Lisp_Object prop)
2721 {
2722 while (CONSP (plist) && !EQ (XCAR (plist), prop))
2723 {
2724 QUIT;
2725 plist = XCDR (plist);
2726 plist = CDR (plist);
2727 }
2728 return plist;
2729 }
2730
2731 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
2732 doc: /* In WIDGET, set PROPERTY to VALUE.
2733 The value can later be retrieved with `widget-get'. */)
2734 (Lisp_Object widget, Lisp_Object property, Lisp_Object value)
2735 {
2736 CHECK_CONS (widget);
2737 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
2738 return value;
2739 }
2740
2741 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
2742 doc: /* In WIDGET, get the value of PROPERTY.
2743 The value could either be specified when the widget was created, or
2744 later with `widget-put'. */)
2745 (Lisp_Object widget, Lisp_Object property)
2746 {
2747 Lisp_Object tmp;
2748
2749 while (1)
2750 {
2751 if (NILP (widget))
2752 return Qnil;
2753 CHECK_CONS (widget);
2754 tmp = Fplist_member (XCDR (widget), property);
2755 if (CONSP (tmp))
2756 {
2757 tmp = XCDR (tmp);
2758 return CAR (tmp);
2759 }
2760 tmp = XCAR (widget);
2761 if (NILP (tmp))
2762 return Qnil;
2763 widget = Fget (tmp, Qwidget_type);
2764 }
2765 }
2766
2767 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
2768 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
2769 ARGS are passed as extra arguments to the function.
2770 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
2771 (int nargs, Lisp_Object *args)
2772 {
2773 /* This function can GC. */
2774 Lisp_Object newargs[3];
2775 struct gcpro gcpro1, gcpro2;
2776 Lisp_Object result;
2777
2778 newargs[0] = Fwidget_get (args[0], args[1]);
2779 newargs[1] = args[0];
2780 newargs[2] = Flist (nargs - 2, args + 2);
2781 GCPRO2 (newargs[0], newargs[2]);
2782 result = Fapply (3, newargs);
2783 UNGCPRO;
2784 return result;
2785 }
2786
2787 #ifdef HAVE_LANGINFO_CODESET
2788 #include <langinfo.h>
2789 #endif
2790
2791 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
2792 doc: /* Access locale data ITEM for the current C locale, if available.
2793 ITEM should be one of the following:
2794
2795 `codeset', returning the character set as a string (locale item CODESET);
2796
2797 `days', returning a 7-element vector of day names (locale items DAY_n);
2798
2799 `months', returning a 12-element vector of month names (locale items MON_n);
2800
2801 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
2802 both measured in milimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
2803
2804 If the system can't provide such information through a call to
2805 `nl_langinfo', or if ITEM isn't from the list above, return nil.
2806
2807 See also Info node `(libc)Locales'.
2808
2809 The data read from the system are decoded using `locale-coding-system'. */)
2810 (Lisp_Object item)
2811 {
2812 char *str = NULL;
2813 #ifdef HAVE_LANGINFO_CODESET
2814 Lisp_Object val;
2815 if (EQ (item, Qcodeset))
2816 {
2817 str = nl_langinfo (CODESET);
2818 return build_string (str);
2819 }
2820 #ifdef DAY_1
2821 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
2822 {
2823 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
2824 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
2825 int i;
2826 struct gcpro gcpro1;
2827 GCPRO1 (v);
2828 synchronize_system_time_locale ();
2829 for (i = 0; i < 7; i++)
2830 {
2831 str = nl_langinfo (days[i]);
2832 val = make_unibyte_string (str, strlen (str));
2833 /* Fixme: Is this coding system necessarily right, even if
2834 it is consistent with CODESET? If not, what to do? */
2835 Faset (v, make_number (i),
2836 code_convert_string_norecord (val, Vlocale_coding_system,
2837 0));
2838 }
2839 UNGCPRO;
2840 return v;
2841 }
2842 #endif /* DAY_1 */
2843 #ifdef MON_1
2844 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
2845 {
2846 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
2847 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
2848 MON_8, MON_9, MON_10, MON_11, MON_12};
2849 int i;
2850 struct gcpro gcpro1;
2851 GCPRO1 (v);
2852 synchronize_system_time_locale ();
2853 for (i = 0; i < 12; i++)
2854 {
2855 str = nl_langinfo (months[i]);
2856 val = make_unibyte_string (str, strlen (str));
2857 Faset (v, make_number (i),
2858 code_convert_string_norecord (val, Vlocale_coding_system, 0));
2859 }
2860 UNGCPRO;
2861 return v;
2862 }
2863 #endif /* MON_1 */
2864 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
2865 but is in the locale files. This could be used by ps-print. */
2866 #ifdef PAPER_WIDTH
2867 else if (EQ (item, Qpaper))
2868 {
2869 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
2870 make_number (nl_langinfo (PAPER_HEIGHT)));
2871 }
2872 #endif /* PAPER_WIDTH */
2873 #endif /* HAVE_LANGINFO_CODESET*/
2874 return Qnil;
2875 }
2876 \f
2877 /* base64 encode/decode functions (RFC 2045).
2878 Based on code from GNU recode. */
2879
2880 #define MIME_LINE_LENGTH 76
2881
2882 #define IS_ASCII(Character) \
2883 ((Character) < 128)
2884 #define IS_BASE64(Character) \
2885 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
2886 #define IS_BASE64_IGNORABLE(Character) \
2887 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
2888 || (Character) == '\f' || (Character) == '\r')
2889
2890 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
2891 character or return retval if there are no characters left to
2892 process. */
2893 #define READ_QUADRUPLET_BYTE(retval) \
2894 do \
2895 { \
2896 if (i == length) \
2897 { \
2898 if (nchars_return) \
2899 *nchars_return = nchars; \
2900 return (retval); \
2901 } \
2902 c = from[i++]; \
2903 } \
2904 while (IS_BASE64_IGNORABLE (c))
2905
2906 /* Table of characters coding the 64 values. */
2907 static const char base64_value_to_char[64] =
2908 {
2909 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
2910 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
2911 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
2912 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
2913 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
2914 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
2915 '8', '9', '+', '/' /* 60-63 */
2916 };
2917
2918 /* Table of base64 values for first 128 characters. */
2919 static const short base64_char_to_value[128] =
2920 {
2921 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
2922 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
2923 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
2924 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
2925 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
2926 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
2927 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
2928 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
2929 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
2930 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
2931 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
2932 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
2933 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
2934 };
2935
2936 /* The following diagram shows the logical steps by which three octets
2937 get transformed into four base64 characters.
2938
2939 .--------. .--------. .--------.
2940 |aaaaaabb| |bbbbcccc| |ccdddddd|
2941 `--------' `--------' `--------'
2942 6 2 4 4 2 6
2943 .--------+--------+--------+--------.
2944 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
2945 `--------+--------+--------+--------'
2946
2947 .--------+--------+--------+--------.
2948 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
2949 `--------+--------+--------+--------'
2950
2951 The octets are divided into 6 bit chunks, which are then encoded into
2952 base64 characters. */
2953
2954
2955 static EMACS_INT base64_encode_1 (const char *, char *, EMACS_INT, int, int);
2956 static EMACS_INT base64_decode_1 (const char *, char *, EMACS_INT, int,
2957 EMACS_INT *);
2958
2959 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
2960 2, 3, "r",
2961 doc: /* Base64-encode the region between BEG and END.
2962 Return the length of the encoded text.
2963 Optional third argument NO-LINE-BREAK means do not break long lines
2964 into shorter lines. */)
2965 (Lisp_Object beg, Lisp_Object end, Lisp_Object no_line_break)
2966 {
2967 char *encoded;
2968 EMACS_INT allength, length;
2969 EMACS_INT ibeg, iend, encoded_length;
2970 EMACS_INT old_pos = PT;
2971 USE_SAFE_ALLOCA;
2972
2973 validate_region (&beg, &end);
2974
2975 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
2976 iend = CHAR_TO_BYTE (XFASTINT (end));
2977 move_gap_both (XFASTINT (beg), ibeg);
2978
2979 /* We need to allocate enough room for encoding the text.
2980 We need 33 1/3% more space, plus a newline every 76
2981 characters, and then we round up. */
2982 length = iend - ibeg;
2983 allength = length + length/3 + 1;
2984 allength += allength / MIME_LINE_LENGTH + 1 + 6;
2985
2986 SAFE_ALLOCA (encoded, char *, allength);
2987 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
2988 NILP (no_line_break),
2989 !NILP (current_buffer->enable_multibyte_characters));
2990 if (encoded_length > allength)
2991 abort ();
2992
2993 if (encoded_length < 0)
2994 {
2995 /* The encoding wasn't possible. */
2996 SAFE_FREE ();
2997 error ("Multibyte character in data for base64 encoding");
2998 }
2999
3000 /* Now we have encoded the region, so we insert the new contents
3001 and delete the old. (Insert first in order to preserve markers.) */
3002 SET_PT_BOTH (XFASTINT (beg), ibeg);
3003 insert (encoded, encoded_length);
3004 SAFE_FREE ();
3005 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3006
3007 /* If point was outside of the region, restore it exactly; else just
3008 move to the beginning of the region. */
3009 if (old_pos >= XFASTINT (end))
3010 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3011 else if (old_pos > XFASTINT (beg))
3012 old_pos = XFASTINT (beg);
3013 SET_PT (old_pos);
3014
3015 /* We return the length of the encoded text. */
3016 return make_number (encoded_length);
3017 }
3018
3019 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3020 1, 2, 0,
3021 doc: /* Base64-encode STRING and return the result.
3022 Optional second argument NO-LINE-BREAK means do not break long lines
3023 into shorter lines. */)
3024 (Lisp_Object string, Lisp_Object no_line_break)
3025 {
3026 EMACS_INT allength, length, encoded_length;
3027 char *encoded;
3028 Lisp_Object encoded_string;
3029 USE_SAFE_ALLOCA;
3030
3031 CHECK_STRING (string);
3032
3033 /* We need to allocate enough room for encoding the text.
3034 We need 33 1/3% more space, plus a newline every 76
3035 characters, and then we round up. */
3036 length = SBYTES (string);
3037 allength = length + length/3 + 1;
3038 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3039
3040 /* We need to allocate enough room for decoding the text. */
3041 SAFE_ALLOCA (encoded, char *, allength);
3042
3043 encoded_length = base64_encode_1 (SDATA (string),
3044 encoded, length, NILP (no_line_break),
3045 STRING_MULTIBYTE (string));
3046 if (encoded_length > allength)
3047 abort ();
3048
3049 if (encoded_length < 0)
3050 {
3051 /* The encoding wasn't possible. */
3052 SAFE_FREE ();
3053 error ("Multibyte character in data for base64 encoding");
3054 }
3055
3056 encoded_string = make_unibyte_string (encoded, encoded_length);
3057 SAFE_FREE ();
3058
3059 return encoded_string;
3060 }
3061
3062 static EMACS_INT
3063 base64_encode_1 (const char *from, char *to, EMACS_INT length,
3064 int line_break, int multibyte)
3065 {
3066 int counter = 0;
3067 EMACS_INT i = 0;
3068 char *e = to;
3069 int c;
3070 unsigned int value;
3071 int bytes;
3072
3073 while (i < length)
3074 {
3075 if (multibyte)
3076 {
3077 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3078 if (CHAR_BYTE8_P (c))
3079 c = CHAR_TO_BYTE8 (c);
3080 else if (c >= 256)
3081 return -1;
3082 i += bytes;
3083 }
3084 else
3085 c = from[i++];
3086
3087 /* Wrap line every 76 characters. */
3088
3089 if (line_break)
3090 {
3091 if (counter < MIME_LINE_LENGTH / 4)
3092 counter++;
3093 else
3094 {
3095 *e++ = '\n';
3096 counter = 1;
3097 }
3098 }
3099
3100 /* Process first byte of a triplet. */
3101
3102 *e++ = base64_value_to_char[0x3f & c >> 2];
3103 value = (0x03 & c) << 4;
3104
3105 /* Process second byte of a triplet. */
3106
3107 if (i == length)
3108 {
3109 *e++ = base64_value_to_char[value];
3110 *e++ = '=';
3111 *e++ = '=';
3112 break;
3113 }
3114
3115 if (multibyte)
3116 {
3117 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3118 if (CHAR_BYTE8_P (c))
3119 c = CHAR_TO_BYTE8 (c);
3120 else if (c >= 256)
3121 return -1;
3122 i += bytes;
3123 }
3124 else
3125 c = from[i++];
3126
3127 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3128 value = (0x0f & c) << 2;
3129
3130 /* Process third byte of a triplet. */
3131
3132 if (i == length)
3133 {
3134 *e++ = base64_value_to_char[value];
3135 *e++ = '=';
3136 break;
3137 }
3138
3139 if (multibyte)
3140 {
3141 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3142 if (CHAR_BYTE8_P (c))
3143 c = CHAR_TO_BYTE8 (c);
3144 else if (c >= 256)
3145 return -1;
3146 i += bytes;
3147 }
3148 else
3149 c = from[i++];
3150
3151 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3152 *e++ = base64_value_to_char[0x3f & c];
3153 }
3154
3155 return e - to;
3156 }
3157
3158
3159 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3160 2, 2, "r",
3161 doc: /* Base64-decode the region between BEG and END.
3162 Return the length of the decoded text.
3163 If the region can't be decoded, signal an error and don't modify the buffer. */)
3164 (Lisp_Object beg, Lisp_Object end)
3165 {
3166 EMACS_INT ibeg, iend, length, allength;
3167 char *decoded;
3168 EMACS_INT old_pos = PT;
3169 EMACS_INT decoded_length;
3170 EMACS_INT inserted_chars;
3171 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3172 USE_SAFE_ALLOCA;
3173
3174 validate_region (&beg, &end);
3175
3176 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3177 iend = CHAR_TO_BYTE (XFASTINT (end));
3178
3179 length = iend - ibeg;
3180
3181 /* We need to allocate enough room for decoding the text. If we are
3182 working on a multibyte buffer, each decoded code may occupy at
3183 most two bytes. */
3184 allength = multibyte ? length * 2 : length;
3185 SAFE_ALLOCA (decoded, char *, allength);
3186
3187 move_gap_both (XFASTINT (beg), ibeg);
3188 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3189 multibyte, &inserted_chars);
3190 if (decoded_length > allength)
3191 abort ();
3192
3193 if (decoded_length < 0)
3194 {
3195 /* The decoding wasn't possible. */
3196 SAFE_FREE ();
3197 error ("Invalid base64 data");
3198 }
3199
3200 /* Now we have decoded the region, so we insert the new contents
3201 and delete the old. (Insert first in order to preserve markers.) */
3202 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3203 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3204 SAFE_FREE ();
3205
3206 /* Delete the original text. */
3207 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3208 iend + decoded_length, 1);
3209
3210 /* If point was outside of the region, restore it exactly; else just
3211 move to the beginning of the region. */
3212 if (old_pos >= XFASTINT (end))
3213 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3214 else if (old_pos > XFASTINT (beg))
3215 old_pos = XFASTINT (beg);
3216 SET_PT (old_pos > ZV ? ZV : old_pos);
3217
3218 return make_number (inserted_chars);
3219 }
3220
3221 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3222 1, 1, 0,
3223 doc: /* Base64-decode STRING and return the result. */)
3224 (Lisp_Object string)
3225 {
3226 char *decoded;
3227 EMACS_INT length, decoded_length;
3228 Lisp_Object decoded_string;
3229 USE_SAFE_ALLOCA;
3230
3231 CHECK_STRING (string);
3232
3233 length = SBYTES (string);
3234 /* We need to allocate enough room for decoding the text. */
3235 SAFE_ALLOCA (decoded, char *, length);
3236
3237 /* The decoded result should be unibyte. */
3238 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3239 0, NULL);
3240 if (decoded_length > length)
3241 abort ();
3242 else if (decoded_length >= 0)
3243 decoded_string = make_unibyte_string (decoded, decoded_length);
3244 else
3245 decoded_string = Qnil;
3246
3247 SAFE_FREE ();
3248 if (!STRINGP (decoded_string))
3249 error ("Invalid base64 data");
3250
3251 return decoded_string;
3252 }
3253
3254 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3255 MULTIBYTE is nonzero, the decoded result should be in multibyte
3256 form. If NCHARS_RETRUN is not NULL, store the number of produced
3257 characters in *NCHARS_RETURN. */
3258
3259 static EMACS_INT
3260 base64_decode_1 (const char *from, char *to, EMACS_INT length,
3261 int multibyte, EMACS_INT *nchars_return)
3262 {
3263 EMACS_INT i = 0; /* Used inside READ_QUADRUPLET_BYTE */
3264 char *e = to;
3265 unsigned char c;
3266 unsigned long value;
3267 EMACS_INT nchars = 0;
3268
3269 while (1)
3270 {
3271 /* Process first byte of a quadruplet. */
3272
3273 READ_QUADRUPLET_BYTE (e-to);
3274
3275 if (!IS_BASE64 (c))
3276 return -1;
3277 value = base64_char_to_value[c] << 18;
3278
3279 /* Process second byte of a quadruplet. */
3280
3281 READ_QUADRUPLET_BYTE (-1);
3282
3283 if (!IS_BASE64 (c))
3284 return -1;
3285 value |= base64_char_to_value[c] << 12;
3286
3287 c = (unsigned char) (value >> 16);
3288 if (multibyte && c >= 128)
3289 e += BYTE8_STRING (c, e);
3290 else
3291 *e++ = c;
3292 nchars++;
3293
3294 /* Process third byte of a quadruplet. */
3295
3296 READ_QUADRUPLET_BYTE (-1);
3297
3298 if (c == '=')
3299 {
3300 READ_QUADRUPLET_BYTE (-1);
3301
3302 if (c != '=')
3303 return -1;
3304 continue;
3305 }
3306
3307 if (!IS_BASE64 (c))
3308 return -1;
3309 value |= base64_char_to_value[c] << 6;
3310
3311 c = (unsigned char) (0xff & value >> 8);
3312 if (multibyte && c >= 128)
3313 e += BYTE8_STRING (c, e);
3314 else
3315 *e++ = c;
3316 nchars++;
3317
3318 /* Process fourth byte of a quadruplet. */
3319
3320 READ_QUADRUPLET_BYTE (-1);
3321
3322 if (c == '=')
3323 continue;
3324
3325 if (!IS_BASE64 (c))
3326 return -1;
3327 value |= base64_char_to_value[c];
3328
3329 c = (unsigned char) (0xff & value);
3330 if (multibyte && c >= 128)
3331 e += BYTE8_STRING (c, e);
3332 else
3333 *e++ = c;
3334 nchars++;
3335 }
3336 }
3337
3338
3339 \f
3340 /***********************************************************************
3341 ***** *****
3342 ***** Hash Tables *****
3343 ***** *****
3344 ***********************************************************************/
3345
3346 /* Implemented by gerd@gnu.org. This hash table implementation was
3347 inspired by CMUCL hash tables. */
3348
3349 /* Ideas:
3350
3351 1. For small tables, association lists are probably faster than
3352 hash tables because they have lower overhead.
3353
3354 For uses of hash tables where the O(1) behavior of table
3355 operations is not a requirement, it might therefore be a good idea
3356 not to hash. Instead, we could just do a linear search in the
3357 key_and_value vector of the hash table. This could be done
3358 if a `:linear-search t' argument is given to make-hash-table. */
3359
3360
3361 /* The list of all weak hash tables. Don't staticpro this one. */
3362
3363 struct Lisp_Hash_Table *weak_hash_tables;
3364
3365 /* Various symbols. */
3366
3367 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3368 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3369 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3370
3371 /* Function prototypes. */
3372
3373 static struct Lisp_Hash_Table *check_hash_table (Lisp_Object);
3374 static int get_key_arg (Lisp_Object, int, Lisp_Object *, char *);
3375 static void maybe_resize_hash_table (struct Lisp_Hash_Table *);
3376 static int cmpfn_eql (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3377 Lisp_Object, unsigned);
3378 static int cmpfn_equal (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3379 Lisp_Object, unsigned);
3380 static int cmpfn_user_defined (struct Lisp_Hash_Table *, Lisp_Object,
3381 unsigned, Lisp_Object, unsigned);
3382 static unsigned hashfn_eq (struct Lisp_Hash_Table *, Lisp_Object);
3383 static unsigned hashfn_eql (struct Lisp_Hash_Table *, Lisp_Object);
3384 static unsigned hashfn_equal (struct Lisp_Hash_Table *, Lisp_Object);
3385 static unsigned hashfn_user_defined (struct Lisp_Hash_Table *,
3386 Lisp_Object);
3387 static unsigned sxhash_string (unsigned char *, int);
3388 static unsigned sxhash_list (Lisp_Object, int);
3389 static unsigned sxhash_vector (Lisp_Object, int);
3390 static unsigned sxhash_bool_vector (Lisp_Object);
3391 static int sweep_weak_table (struct Lisp_Hash_Table *, int);
3392
3393
3394 \f
3395 /***********************************************************************
3396 Utilities
3397 ***********************************************************************/
3398
3399 /* If OBJ is a Lisp hash table, return a pointer to its struct
3400 Lisp_Hash_Table. Otherwise, signal an error. */
3401
3402 static struct Lisp_Hash_Table *
3403 check_hash_table (Lisp_Object obj)
3404 {
3405 CHECK_HASH_TABLE (obj);
3406 return XHASH_TABLE (obj);
3407 }
3408
3409
3410 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3411 number. */
3412
3413 int
3414 next_almost_prime (int n)
3415 {
3416 if (n % 2 == 0)
3417 n += 1;
3418 if (n % 3 == 0)
3419 n += 2;
3420 if (n % 7 == 0)
3421 n += 4;
3422 return n;
3423 }
3424
3425
3426 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3427 which USED[I] is non-zero. If found at index I in ARGS, set
3428 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3429 -1. This function is used to extract a keyword/argument pair from
3430 a DEFUN parameter list. */
3431
3432 static int
3433 get_key_arg (Lisp_Object key, int nargs, Lisp_Object *args, char *used)
3434 {
3435 int i;
3436
3437 for (i = 0; i < nargs - 1; ++i)
3438 if (!used[i] && EQ (args[i], key))
3439 break;
3440
3441 if (i >= nargs - 1)
3442 i = -1;
3443 else
3444 {
3445 used[i++] = 1;
3446 used[i] = 1;
3447 }
3448
3449 return i;
3450 }
3451
3452
3453 /* Return a Lisp vector which has the same contents as VEC but has
3454 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3455 vector that are not copied from VEC are set to INIT. */
3456
3457 Lisp_Object
3458 larger_vector (Lisp_Object vec, int new_size, Lisp_Object init)
3459 {
3460 struct Lisp_Vector *v;
3461 int i, old_size;
3462
3463 xassert (VECTORP (vec));
3464 old_size = ASIZE (vec);
3465 xassert (new_size >= old_size);
3466
3467 v = allocate_vector (new_size);
3468 memcpy (v->contents, XVECTOR (vec)->contents, old_size * sizeof *v->contents);
3469 for (i = old_size; i < new_size; ++i)
3470 v->contents[i] = init;
3471 XSETVECTOR (vec, v);
3472 return vec;
3473 }
3474
3475
3476 /***********************************************************************
3477 Low-level Functions
3478 ***********************************************************************/
3479
3480 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3481 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3482 KEY2 are the same. */
3483
3484 static int
3485 cmpfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3486 {
3487 return (FLOATP (key1)
3488 && FLOATP (key2)
3489 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3490 }
3491
3492
3493 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3494 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3495 KEY2 are the same. */
3496
3497 static int
3498 cmpfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3499 {
3500 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3501 }
3502
3503
3504 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3505 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3506 if KEY1 and KEY2 are the same. */
3507
3508 static int
3509 cmpfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3510 {
3511 if (hash1 == hash2)
3512 {
3513 Lisp_Object args[3];
3514
3515 args[0] = h->user_cmp_function;
3516 args[1] = key1;
3517 args[2] = key2;
3518 return !NILP (Ffuncall (3, args));
3519 }
3520 else
3521 return 0;
3522 }
3523
3524
3525 /* Value is a hash code for KEY for use in hash table H which uses
3526 `eq' to compare keys. The hash code returned is guaranteed to fit
3527 in a Lisp integer. */
3528
3529 static unsigned
3530 hashfn_eq (struct Lisp_Hash_Table *h, Lisp_Object key)
3531 {
3532 unsigned hash = XUINT (key) ^ XTYPE (key);
3533 xassert ((hash & ~INTMASK) == 0);
3534 return hash;
3535 }
3536
3537
3538 /* Value is a hash code for KEY for use in hash table H which uses
3539 `eql' to compare keys. The hash code returned is guaranteed to fit
3540 in a Lisp integer. */
3541
3542 static unsigned
3543 hashfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key)
3544 {
3545 unsigned hash;
3546 if (FLOATP (key))
3547 hash = sxhash (key, 0);
3548 else
3549 hash = XUINT (key) ^ XTYPE (key);
3550 xassert ((hash & ~INTMASK) == 0);
3551 return hash;
3552 }
3553
3554
3555 /* Value is a hash code for KEY for use in hash table H which uses
3556 `equal' to compare keys. The hash code returned is guaranteed to fit
3557 in a Lisp integer. */
3558
3559 static unsigned
3560 hashfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key)
3561 {
3562 unsigned hash = sxhash (key, 0);
3563 xassert ((hash & ~INTMASK) == 0);
3564 return hash;
3565 }
3566
3567
3568 /* Value is a hash code for KEY for use in hash table H which uses as
3569 user-defined function to compare keys. The hash code returned is
3570 guaranteed to fit in a Lisp integer. */
3571
3572 static unsigned
3573 hashfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key)
3574 {
3575 Lisp_Object args[2], hash;
3576
3577 args[0] = h->user_hash_function;
3578 args[1] = key;
3579 hash = Ffuncall (2, args);
3580 if (!INTEGERP (hash))
3581 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
3582 return XUINT (hash);
3583 }
3584
3585
3586 /* Create and initialize a new hash table.
3587
3588 TEST specifies the test the hash table will use to compare keys.
3589 It must be either one of the predefined tests `eq', `eql' or
3590 `equal' or a symbol denoting a user-defined test named TEST with
3591 test and hash functions USER_TEST and USER_HASH.
3592
3593 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3594
3595 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3596 new size when it becomes full is computed by adding REHASH_SIZE to
3597 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3598 table's new size is computed by multiplying its old size with
3599 REHASH_SIZE.
3600
3601 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3602 be resized when the ratio of (number of entries in the table) /
3603 (table size) is >= REHASH_THRESHOLD.
3604
3605 WEAK specifies the weakness of the table. If non-nil, it must be
3606 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3607
3608 Lisp_Object
3609 make_hash_table (Lisp_Object test, Lisp_Object size, Lisp_Object rehash_size,
3610 Lisp_Object rehash_threshold, Lisp_Object weak,
3611 Lisp_Object user_test, Lisp_Object user_hash)
3612 {
3613 struct Lisp_Hash_Table *h;
3614 Lisp_Object table;
3615 int index_size, i, sz;
3616
3617 /* Preconditions. */
3618 xassert (SYMBOLP (test));
3619 xassert (INTEGERP (size) && XINT (size) >= 0);
3620 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3621 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3622 xassert (FLOATP (rehash_threshold)
3623 && XFLOATINT (rehash_threshold) > 0
3624 && XFLOATINT (rehash_threshold) <= 1.0);
3625
3626 if (XFASTINT (size) == 0)
3627 size = make_number (1);
3628
3629 /* Allocate a table and initialize it. */
3630 h = allocate_hash_table ();
3631
3632 /* Initialize hash table slots. */
3633 sz = XFASTINT (size);
3634
3635 h->test = test;
3636 if (EQ (test, Qeql))
3637 {
3638 h->cmpfn = cmpfn_eql;
3639 h->hashfn = hashfn_eql;
3640 }
3641 else if (EQ (test, Qeq))
3642 {
3643 h->cmpfn = NULL;
3644 h->hashfn = hashfn_eq;
3645 }
3646 else if (EQ (test, Qequal))
3647 {
3648 h->cmpfn = cmpfn_equal;
3649 h->hashfn = hashfn_equal;
3650 }
3651 else
3652 {
3653 h->user_cmp_function = user_test;
3654 h->user_hash_function = user_hash;
3655 h->cmpfn = cmpfn_user_defined;
3656 h->hashfn = hashfn_user_defined;
3657 }
3658
3659 h->weak = weak;
3660 h->rehash_threshold = rehash_threshold;
3661 h->rehash_size = rehash_size;
3662 h->count = 0;
3663 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3664 h->hash = Fmake_vector (size, Qnil);
3665 h->next = Fmake_vector (size, Qnil);
3666 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
3667 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
3668 h->index = Fmake_vector (make_number (index_size), Qnil);
3669
3670 /* Set up the free list. */
3671 for (i = 0; i < sz - 1; ++i)
3672 HASH_NEXT (h, i) = make_number (i + 1);
3673 h->next_free = make_number (0);
3674
3675 XSET_HASH_TABLE (table, h);
3676 xassert (HASH_TABLE_P (table));
3677 xassert (XHASH_TABLE (table) == h);
3678
3679 /* Maybe add this hash table to the list of all weak hash tables. */
3680 if (NILP (h->weak))
3681 h->next_weak = NULL;
3682 else
3683 {
3684 h->next_weak = weak_hash_tables;
3685 weak_hash_tables = h;
3686 }
3687
3688 return table;
3689 }
3690
3691
3692 /* Return a copy of hash table H1. Keys and values are not copied,
3693 only the table itself is. */
3694
3695 static Lisp_Object
3696 copy_hash_table (struct Lisp_Hash_Table *h1)
3697 {
3698 Lisp_Object table;
3699 struct Lisp_Hash_Table *h2;
3700 struct Lisp_Vector *next;
3701
3702 h2 = allocate_hash_table ();
3703 next = h2->vec_next;
3704 memcpy (h2, h1, sizeof *h2);
3705 h2->vec_next = next;
3706 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
3707 h2->hash = Fcopy_sequence (h1->hash);
3708 h2->next = Fcopy_sequence (h1->next);
3709 h2->index = Fcopy_sequence (h1->index);
3710 XSET_HASH_TABLE (table, h2);
3711
3712 /* Maybe add this hash table to the list of all weak hash tables. */
3713 if (!NILP (h2->weak))
3714 {
3715 h2->next_weak = weak_hash_tables;
3716 weak_hash_tables = h2;
3717 }
3718
3719 return table;
3720 }
3721
3722
3723 /* Resize hash table H if it's too full. If H cannot be resized
3724 because it's already too large, throw an error. */
3725
3726 static INLINE void
3727 maybe_resize_hash_table (struct Lisp_Hash_Table *h)
3728 {
3729 if (NILP (h->next_free))
3730 {
3731 int old_size = HASH_TABLE_SIZE (h);
3732 int i, new_size, index_size;
3733 EMACS_INT nsize;
3734
3735 if (INTEGERP (h->rehash_size))
3736 new_size = old_size + XFASTINT (h->rehash_size);
3737 else
3738 new_size = old_size * XFLOATINT (h->rehash_size);
3739 new_size = max (old_size + 1, new_size);
3740 index_size = next_almost_prime ((int)
3741 (new_size
3742 / XFLOATINT (h->rehash_threshold)));
3743 /* Assignment to EMACS_INT stops GCC whining about limited range
3744 of data type. */
3745 nsize = max (index_size, 2 * new_size);
3746 if (nsize > MOST_POSITIVE_FIXNUM)
3747 error ("Hash table too large to resize");
3748
3749 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
3750 h->next = larger_vector (h->next, new_size, Qnil);
3751 h->hash = larger_vector (h->hash, new_size, Qnil);
3752 h->index = Fmake_vector (make_number (index_size), Qnil);
3753
3754 /* Update the free list. Do it so that new entries are added at
3755 the end of the free list. This makes some operations like
3756 maphash faster. */
3757 for (i = old_size; i < new_size - 1; ++i)
3758 HASH_NEXT (h, i) = make_number (i + 1);
3759
3760 if (!NILP (h->next_free))
3761 {
3762 Lisp_Object last, next;
3763
3764 last = h->next_free;
3765 while (next = HASH_NEXT (h, XFASTINT (last)),
3766 !NILP (next))
3767 last = next;
3768
3769 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
3770 }
3771 else
3772 XSETFASTINT (h->next_free, old_size);
3773
3774 /* Rehash. */
3775 for (i = 0; i < old_size; ++i)
3776 if (!NILP (HASH_HASH (h, i)))
3777 {
3778 unsigned hash_code = XUINT (HASH_HASH (h, i));
3779 int start_of_bucket = hash_code % ASIZE (h->index);
3780 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3781 HASH_INDEX (h, start_of_bucket) = make_number (i);
3782 }
3783 }
3784 }
3785
3786
3787 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3788 the hash code of KEY. Value is the index of the entry in H
3789 matching KEY, or -1 if not found. */
3790
3791 int
3792 hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, unsigned int *hash)
3793 {
3794 unsigned hash_code;
3795 int start_of_bucket;
3796 Lisp_Object idx;
3797
3798 hash_code = h->hashfn (h, key);
3799 if (hash)
3800 *hash = hash_code;
3801
3802 start_of_bucket = hash_code % ASIZE (h->index);
3803 idx = HASH_INDEX (h, start_of_bucket);
3804
3805 /* We need not gcpro idx since it's either an integer or nil. */
3806 while (!NILP (idx))
3807 {
3808 int i = XFASTINT (idx);
3809 if (EQ (key, HASH_KEY (h, i))
3810 || (h->cmpfn
3811 && h->cmpfn (h, key, hash_code,
3812 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
3813 break;
3814 idx = HASH_NEXT (h, i);
3815 }
3816
3817 return NILP (idx) ? -1 : XFASTINT (idx);
3818 }
3819
3820
3821 /* Put an entry into hash table H that associates KEY with VALUE.
3822 HASH is a previously computed hash code of KEY.
3823 Value is the index of the entry in H matching KEY. */
3824
3825 int
3826 hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value, unsigned int hash)
3827 {
3828 int start_of_bucket, i;
3829
3830 xassert ((hash & ~INTMASK) == 0);
3831
3832 /* Increment count after resizing because resizing may fail. */
3833 maybe_resize_hash_table (h);
3834 h->count++;
3835
3836 /* Store key/value in the key_and_value vector. */
3837 i = XFASTINT (h->next_free);
3838 h->next_free = HASH_NEXT (h, i);
3839 HASH_KEY (h, i) = key;
3840 HASH_VALUE (h, i) = value;
3841
3842 /* Remember its hash code. */
3843 HASH_HASH (h, i) = make_number (hash);
3844
3845 /* Add new entry to its collision chain. */
3846 start_of_bucket = hash % ASIZE (h->index);
3847 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3848 HASH_INDEX (h, start_of_bucket) = make_number (i);
3849 return i;
3850 }
3851
3852
3853 /* Remove the entry matching KEY from hash table H, if there is one. */
3854
3855 static void
3856 hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
3857 {
3858 unsigned hash_code;
3859 int start_of_bucket;
3860 Lisp_Object idx, prev;
3861
3862 hash_code = h->hashfn (h, key);
3863 start_of_bucket = hash_code % ASIZE (h->index);
3864 idx = HASH_INDEX (h, start_of_bucket);
3865 prev = Qnil;
3866
3867 /* We need not gcpro idx, prev since they're either integers or nil. */
3868 while (!NILP (idx))
3869 {
3870 int i = XFASTINT (idx);
3871
3872 if (EQ (key, HASH_KEY (h, i))
3873 || (h->cmpfn
3874 && h->cmpfn (h, key, hash_code,
3875 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
3876 {
3877 /* Take entry out of collision chain. */
3878 if (NILP (prev))
3879 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
3880 else
3881 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
3882
3883 /* Clear slots in key_and_value and add the slots to
3884 the free list. */
3885 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
3886 HASH_NEXT (h, i) = h->next_free;
3887 h->next_free = make_number (i);
3888 h->count--;
3889 xassert (h->count >= 0);
3890 break;
3891 }
3892 else
3893 {
3894 prev = idx;
3895 idx = HASH_NEXT (h, i);
3896 }
3897 }
3898 }
3899
3900
3901 /* Clear hash table H. */
3902
3903 static void
3904 hash_clear (struct Lisp_Hash_Table *h)
3905 {
3906 if (h->count > 0)
3907 {
3908 int i, size = HASH_TABLE_SIZE (h);
3909
3910 for (i = 0; i < size; ++i)
3911 {
3912 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
3913 HASH_KEY (h, i) = Qnil;
3914 HASH_VALUE (h, i) = Qnil;
3915 HASH_HASH (h, i) = Qnil;
3916 }
3917
3918 for (i = 0; i < ASIZE (h->index); ++i)
3919 ASET (h->index, i, Qnil);
3920
3921 h->next_free = make_number (0);
3922 h->count = 0;
3923 }
3924 }
3925
3926
3927 \f
3928 /************************************************************************
3929 Weak Hash Tables
3930 ************************************************************************/
3931
3932 void
3933 init_weak_hash_tables (void)
3934 {
3935 weak_hash_tables = NULL;
3936 }
3937
3938 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
3939 entries from the table that don't survive the current GC.
3940 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
3941 non-zero if anything was marked. */
3942
3943 static int
3944 sweep_weak_table (struct Lisp_Hash_Table *h, int remove_entries_p)
3945 {
3946 int bucket, n, marked;
3947
3948 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
3949 marked = 0;
3950
3951 for (bucket = 0; bucket < n; ++bucket)
3952 {
3953 Lisp_Object idx, next, prev;
3954
3955 /* Follow collision chain, removing entries that
3956 don't survive this garbage collection. */
3957 prev = Qnil;
3958 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
3959 {
3960 int i = XFASTINT (idx);
3961 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
3962 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
3963 int remove_p;
3964
3965 if (EQ (h->weak, Qkey))
3966 remove_p = !key_known_to_survive_p;
3967 else if (EQ (h->weak, Qvalue))
3968 remove_p = !value_known_to_survive_p;
3969 else if (EQ (h->weak, Qkey_or_value))
3970 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
3971 else if (EQ (h->weak, Qkey_and_value))
3972 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
3973 else
3974 abort ();
3975
3976 next = HASH_NEXT (h, i);
3977
3978 if (remove_entries_p)
3979 {
3980 if (remove_p)
3981 {
3982 /* Take out of collision chain. */
3983 if (NILP (prev))
3984 HASH_INDEX (h, bucket) = next;
3985 else
3986 HASH_NEXT (h, XFASTINT (prev)) = next;
3987
3988 /* Add to free list. */
3989 HASH_NEXT (h, i) = h->next_free;
3990 h->next_free = idx;
3991
3992 /* Clear key, value, and hash. */
3993 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
3994 HASH_HASH (h, i) = Qnil;
3995
3996 h->count--;
3997 }
3998 else
3999 {
4000 prev = idx;
4001 }
4002 }
4003 else
4004 {
4005 if (!remove_p)
4006 {
4007 /* Make sure key and value survive. */
4008 if (!key_known_to_survive_p)
4009 {
4010 mark_object (HASH_KEY (h, i));
4011 marked = 1;
4012 }
4013
4014 if (!value_known_to_survive_p)
4015 {
4016 mark_object (HASH_VALUE (h, i));
4017 marked = 1;
4018 }
4019 }
4020 }
4021 }
4022 }
4023
4024 return marked;
4025 }
4026
4027 /* Remove elements from weak hash tables that don't survive the
4028 current garbage collection. Remove weak tables that don't survive
4029 from Vweak_hash_tables. Called from gc_sweep. */
4030
4031 void
4032 sweep_weak_hash_tables (void)
4033 {
4034 struct Lisp_Hash_Table *h, *used, *next;
4035 int marked;
4036
4037 /* Mark all keys and values that are in use. Keep on marking until
4038 there is no more change. This is necessary for cases like
4039 value-weak table A containing an entry X -> Y, where Y is used in a
4040 key-weak table B, Z -> Y. If B comes after A in the list of weak
4041 tables, X -> Y might be removed from A, although when looking at B
4042 one finds that it shouldn't. */
4043 do
4044 {
4045 marked = 0;
4046 for (h = weak_hash_tables; h; h = h->next_weak)
4047 {
4048 if (h->size & ARRAY_MARK_FLAG)
4049 marked |= sweep_weak_table (h, 0);
4050 }
4051 }
4052 while (marked);
4053
4054 /* Remove tables and entries that aren't used. */
4055 for (h = weak_hash_tables, used = NULL; h; h = next)
4056 {
4057 next = h->next_weak;
4058
4059 if (h->size & ARRAY_MARK_FLAG)
4060 {
4061 /* TABLE is marked as used. Sweep its contents. */
4062 if (h->count > 0)
4063 sweep_weak_table (h, 1);
4064
4065 /* Add table to the list of used weak hash tables. */
4066 h->next_weak = used;
4067 used = h;
4068 }
4069 }
4070
4071 weak_hash_tables = used;
4072 }
4073
4074
4075 \f
4076 /***********************************************************************
4077 Hash Code Computation
4078 ***********************************************************************/
4079
4080 /* Maximum depth up to which to dive into Lisp structures. */
4081
4082 #define SXHASH_MAX_DEPTH 3
4083
4084 /* Maximum length up to which to take list and vector elements into
4085 account. */
4086
4087 #define SXHASH_MAX_LEN 7
4088
4089 /* Combine two integers X and Y for hashing. */
4090
4091 #define SXHASH_COMBINE(X, Y) \
4092 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4093 + (unsigned)(Y))
4094
4095
4096 /* Return a hash for string PTR which has length LEN. The hash
4097 code returned is guaranteed to fit in a Lisp integer. */
4098
4099 static unsigned
4100 sxhash_string (unsigned char *ptr, int len)
4101 {
4102 unsigned char *p = ptr;
4103 unsigned char *end = p + len;
4104 unsigned char c;
4105 unsigned hash = 0;
4106
4107 while (p != end)
4108 {
4109 c = *p++;
4110 if (c >= 0140)
4111 c -= 40;
4112 hash = ((hash << 4) + (hash >> 28) + c);
4113 }
4114
4115 return hash & INTMASK;
4116 }
4117
4118
4119 /* Return a hash for list LIST. DEPTH is the current depth in the
4120 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4121
4122 static unsigned
4123 sxhash_list (Lisp_Object list, int depth)
4124 {
4125 unsigned hash = 0;
4126 int i;
4127
4128 if (depth < SXHASH_MAX_DEPTH)
4129 for (i = 0;
4130 CONSP (list) && i < SXHASH_MAX_LEN;
4131 list = XCDR (list), ++i)
4132 {
4133 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4134 hash = SXHASH_COMBINE (hash, hash2);
4135 }
4136
4137 if (!NILP (list))
4138 {
4139 unsigned hash2 = sxhash (list, depth + 1);
4140 hash = SXHASH_COMBINE (hash, hash2);
4141 }
4142
4143 return hash;
4144 }
4145
4146
4147 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4148 the Lisp structure. */
4149
4150 static unsigned
4151 sxhash_vector (Lisp_Object vec, int depth)
4152 {
4153 unsigned hash = ASIZE (vec);
4154 int i, n;
4155
4156 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4157 for (i = 0; i < n; ++i)
4158 {
4159 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
4160 hash = SXHASH_COMBINE (hash, hash2);
4161 }
4162
4163 return hash;
4164 }
4165
4166
4167 /* Return a hash for bool-vector VECTOR. */
4168
4169 static unsigned
4170 sxhash_bool_vector (Lisp_Object vec)
4171 {
4172 unsigned hash = XBOOL_VECTOR (vec)->size;
4173 int i, n;
4174
4175 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4176 for (i = 0; i < n; ++i)
4177 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4178
4179 return hash;
4180 }
4181
4182
4183 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4184 structure. Value is an unsigned integer clipped to INTMASK. */
4185
4186 unsigned
4187 sxhash (Lisp_Object obj, int depth)
4188 {
4189 unsigned hash;
4190
4191 if (depth > SXHASH_MAX_DEPTH)
4192 return 0;
4193
4194 switch (XTYPE (obj))
4195 {
4196 case_Lisp_Int:
4197 hash = XUINT (obj);
4198 break;
4199
4200 case Lisp_Misc:
4201 hash = XUINT (obj);
4202 break;
4203
4204 case Lisp_Symbol:
4205 obj = SYMBOL_NAME (obj);
4206 /* Fall through. */
4207
4208 case Lisp_String:
4209 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4210 break;
4211
4212 /* This can be everything from a vector to an overlay. */
4213 case Lisp_Vectorlike:
4214 if (VECTORP (obj))
4215 /* According to the CL HyperSpec, two arrays are equal only if
4216 they are `eq', except for strings and bit-vectors. In
4217 Emacs, this works differently. We have to compare element
4218 by element. */
4219 hash = sxhash_vector (obj, depth);
4220 else if (BOOL_VECTOR_P (obj))
4221 hash = sxhash_bool_vector (obj);
4222 else
4223 /* Others are `equal' if they are `eq', so let's take their
4224 address as hash. */
4225 hash = XUINT (obj);
4226 break;
4227
4228 case Lisp_Cons:
4229 hash = sxhash_list (obj, depth);
4230 break;
4231
4232 case Lisp_Float:
4233 {
4234 double val = XFLOAT_DATA (obj);
4235 unsigned char *p = (unsigned char *) &val;
4236 unsigned char *e = p + sizeof val;
4237 for (hash = 0; p < e; ++p)
4238 hash = SXHASH_COMBINE (hash, *p);
4239 break;
4240 }
4241
4242 default:
4243 abort ();
4244 }
4245
4246 return hash & INTMASK;
4247 }
4248
4249
4250 \f
4251 /***********************************************************************
4252 Lisp Interface
4253 ***********************************************************************/
4254
4255
4256 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4257 doc: /* Compute a hash code for OBJ and return it as integer. */)
4258 (Lisp_Object obj)
4259 {
4260 unsigned hash = sxhash (obj, 0);
4261 return make_number (hash);
4262 }
4263
4264
4265 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4266 doc: /* Create and return a new hash table.
4267
4268 Arguments are specified as keyword/argument pairs. The following
4269 arguments are defined:
4270
4271 :test TEST -- TEST must be a symbol that specifies how to compare
4272 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4273 `equal'. User-supplied test and hash functions can be specified via
4274 `define-hash-table-test'.
4275
4276 :size SIZE -- A hint as to how many elements will be put in the table.
4277 Default is 65.
4278
4279 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4280 fills up. If REHASH-SIZE is an integer, increase the size by that
4281 amount. If it is a float, it must be > 1.0, and the new size is the
4282 old size multiplied by that factor. Default is 1.5.
4283
4284 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4285 Resize the hash table when the ratio (number of entries / table size)
4286 is greater than or equal to THRESHOLD. Default is 0.8.
4287
4288 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4289 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4290 returned is a weak table. Key/value pairs are removed from a weak
4291 hash table when there are no non-weak references pointing to their
4292 key, value, one of key or value, or both key and value, depending on
4293 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4294 is nil.
4295
4296 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4297 (int nargs, Lisp_Object *args)
4298 {
4299 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4300 Lisp_Object user_test, user_hash;
4301 char *used;
4302 int i;
4303
4304 /* The vector `used' is used to keep track of arguments that
4305 have been consumed. */
4306 used = (char *) alloca (nargs * sizeof *used);
4307 memset (used, 0, nargs * sizeof *used);
4308
4309 /* See if there's a `:test TEST' among the arguments. */
4310 i = get_key_arg (QCtest, nargs, args, used);
4311 test = i < 0 ? Qeql : args[i];
4312 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4313 {
4314 /* See if it is a user-defined test. */
4315 Lisp_Object prop;
4316
4317 prop = Fget (test, Qhash_table_test);
4318 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4319 signal_error ("Invalid hash table test", test);
4320 user_test = XCAR (prop);
4321 user_hash = XCAR (XCDR (prop));
4322 }
4323 else
4324 user_test = user_hash = Qnil;
4325
4326 /* See if there's a `:size SIZE' argument. */
4327 i = get_key_arg (QCsize, nargs, args, used);
4328 size = i < 0 ? Qnil : args[i];
4329 if (NILP (size))
4330 size = make_number (DEFAULT_HASH_SIZE);
4331 else if (!INTEGERP (size) || XINT (size) < 0)
4332 signal_error ("Invalid hash table size", size);
4333
4334 /* Look for `:rehash-size SIZE'. */
4335 i = get_key_arg (QCrehash_size, nargs, args, used);
4336 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4337 if (!NUMBERP (rehash_size)
4338 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4339 || XFLOATINT (rehash_size) <= 1.0)
4340 signal_error ("Invalid hash table rehash size", rehash_size);
4341
4342 /* Look for `:rehash-threshold THRESHOLD'. */
4343 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4344 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4345 if (!FLOATP (rehash_threshold)
4346 || XFLOATINT (rehash_threshold) <= 0.0
4347 || XFLOATINT (rehash_threshold) > 1.0)
4348 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4349
4350 /* Look for `:weakness WEAK'. */
4351 i = get_key_arg (QCweakness, nargs, args, used);
4352 weak = i < 0 ? Qnil : args[i];
4353 if (EQ (weak, Qt))
4354 weak = Qkey_and_value;
4355 if (!NILP (weak)
4356 && !EQ (weak, Qkey)
4357 && !EQ (weak, Qvalue)
4358 && !EQ (weak, Qkey_or_value)
4359 && !EQ (weak, Qkey_and_value))
4360 signal_error ("Invalid hash table weakness", weak);
4361
4362 /* Now, all args should have been used up, or there's a problem. */
4363 for (i = 0; i < nargs; ++i)
4364 if (!used[i])
4365 signal_error ("Invalid argument list", args[i]);
4366
4367 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4368 user_test, user_hash);
4369 }
4370
4371
4372 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4373 doc: /* Return a copy of hash table TABLE. */)
4374 (Lisp_Object table)
4375 {
4376 return copy_hash_table (check_hash_table (table));
4377 }
4378
4379
4380 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4381 doc: /* Return the number of elements in TABLE. */)
4382 (Lisp_Object table)
4383 {
4384 return make_number (check_hash_table (table)->count);
4385 }
4386
4387
4388 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4389 Shash_table_rehash_size, 1, 1, 0,
4390 doc: /* Return the current rehash size of TABLE. */)
4391 (Lisp_Object table)
4392 {
4393 return check_hash_table (table)->rehash_size;
4394 }
4395
4396
4397 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4398 Shash_table_rehash_threshold, 1, 1, 0,
4399 doc: /* Return the current rehash threshold of TABLE. */)
4400 (Lisp_Object table)
4401 {
4402 return check_hash_table (table)->rehash_threshold;
4403 }
4404
4405
4406 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4407 doc: /* Return the size of TABLE.
4408 The size can be used as an argument to `make-hash-table' to create
4409 a hash table than can hold as many elements as TABLE holds
4410 without need for resizing. */)
4411 (Lisp_Object table)
4412 {
4413 struct Lisp_Hash_Table *h = check_hash_table (table);
4414 return make_number (HASH_TABLE_SIZE (h));
4415 }
4416
4417
4418 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4419 doc: /* Return the test TABLE uses. */)
4420 (Lisp_Object table)
4421 {
4422 return check_hash_table (table)->test;
4423 }
4424
4425
4426 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4427 1, 1, 0,
4428 doc: /* Return the weakness of TABLE. */)
4429 (Lisp_Object table)
4430 {
4431 return check_hash_table (table)->weak;
4432 }
4433
4434
4435 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4436 doc: /* Return t if OBJ is a Lisp hash table object. */)
4437 (Lisp_Object obj)
4438 {
4439 return HASH_TABLE_P (obj) ? Qt : Qnil;
4440 }
4441
4442
4443 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4444 doc: /* Clear hash table TABLE and return it. */)
4445 (Lisp_Object table)
4446 {
4447 hash_clear (check_hash_table (table));
4448 /* Be compatible with XEmacs. */
4449 return table;
4450 }
4451
4452
4453 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4454 doc: /* Look up KEY in TABLE and return its associated value.
4455 If KEY is not found, return DFLT which defaults to nil. */)
4456 (Lisp_Object key, Lisp_Object table, Lisp_Object dflt)
4457 {
4458 struct Lisp_Hash_Table *h = check_hash_table (table);
4459 int i = hash_lookup (h, key, NULL);
4460 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4461 }
4462
4463
4464 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4465 doc: /* Associate KEY with VALUE in hash table TABLE.
4466 If KEY is already present in table, replace its current value with
4467 VALUE. */)
4468 (Lisp_Object key, Lisp_Object value, Lisp_Object table)
4469 {
4470 struct Lisp_Hash_Table *h = check_hash_table (table);
4471 int i;
4472 unsigned hash;
4473
4474 i = hash_lookup (h, key, &hash);
4475 if (i >= 0)
4476 HASH_VALUE (h, i) = value;
4477 else
4478 hash_put (h, key, value, hash);
4479
4480 return value;
4481 }
4482
4483
4484 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4485 doc: /* Remove KEY from TABLE. */)
4486 (Lisp_Object key, Lisp_Object table)
4487 {
4488 struct Lisp_Hash_Table *h = check_hash_table (table);
4489 hash_remove_from_table (h, key);
4490 return Qnil;
4491 }
4492
4493
4494 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4495 doc: /* Call FUNCTION for all entries in hash table TABLE.
4496 FUNCTION is called with two arguments, KEY and VALUE. */)
4497 (Lisp_Object function, Lisp_Object table)
4498 {
4499 struct Lisp_Hash_Table *h = check_hash_table (table);
4500 Lisp_Object args[3];
4501 int i;
4502
4503 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4504 if (!NILP (HASH_HASH (h, i)))
4505 {
4506 args[0] = function;
4507 args[1] = HASH_KEY (h, i);
4508 args[2] = HASH_VALUE (h, i);
4509 Ffuncall (3, args);
4510 }
4511
4512 return Qnil;
4513 }
4514
4515
4516 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4517 Sdefine_hash_table_test, 3, 3, 0,
4518 doc: /* Define a new hash table test with name NAME, a symbol.
4519
4520 In hash tables created with NAME specified as test, use TEST to
4521 compare keys, and HASH for computing hash codes of keys.
4522
4523 TEST must be a function taking two arguments and returning non-nil if
4524 both arguments are the same. HASH must be a function taking one
4525 argument and return an integer that is the hash code of the argument.
4526 Hash code computation should use the whole value range of integers,
4527 including negative integers. */)
4528 (Lisp_Object name, Lisp_Object test, Lisp_Object hash)
4529 {
4530 return Fput (name, Qhash_table_test, list2 (test, hash));
4531 }
4532
4533
4534 \f
4535 /************************************************************************
4536 MD5
4537 ************************************************************************/
4538
4539 #include "md5.h"
4540
4541 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4542 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4543
4544 A message digest is a cryptographic checksum of a document, and the
4545 algorithm to calculate it is defined in RFC 1321.
4546
4547 The two optional arguments START and END are character positions
4548 specifying for which part of OBJECT the message digest should be
4549 computed. If nil or omitted, the digest is computed for the whole
4550 OBJECT.
4551
4552 The MD5 message digest is computed from the result of encoding the
4553 text in a coding system, not directly from the internal Emacs form of
4554 the text. The optional fourth argument CODING-SYSTEM specifies which
4555 coding system to encode the text with. It should be the same coding
4556 system that you used or will use when actually writing the text into a
4557 file.
4558
4559 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4560 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4561 system would be chosen by default for writing this text into a file.
4562
4563 If OBJECT is a string, the most preferred coding system (see the
4564 command `prefer-coding-system') is used.
4565
4566 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4567 guesswork fails. Normally, an error is signaled in such case. */)
4568 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4569 {
4570 unsigned char digest[16];
4571 unsigned char value[33];
4572 int i;
4573 EMACS_INT size;
4574 EMACS_INT size_byte = 0;
4575 EMACS_INT start_char = 0, end_char = 0;
4576 EMACS_INT start_byte = 0, end_byte = 0;
4577 register EMACS_INT b, e;
4578 register struct buffer *bp;
4579 EMACS_INT temp;
4580
4581 if (STRINGP (object))
4582 {
4583 if (NILP (coding_system))
4584 {
4585 /* Decide the coding-system to encode the data with. */
4586
4587 if (STRING_MULTIBYTE (object))
4588 /* use default, we can't guess correct value */
4589 coding_system = preferred_coding_system ();
4590 else
4591 coding_system = Qraw_text;
4592 }
4593
4594 if (NILP (Fcoding_system_p (coding_system)))
4595 {
4596 /* Invalid coding system. */
4597
4598 if (!NILP (noerror))
4599 coding_system = Qraw_text;
4600 else
4601 xsignal1 (Qcoding_system_error, coding_system);
4602 }
4603
4604 if (STRING_MULTIBYTE (object))
4605 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4606
4607 size = SCHARS (object);
4608 size_byte = SBYTES (object);
4609
4610 if (!NILP (start))
4611 {
4612 CHECK_NUMBER (start);
4613
4614 start_char = XINT (start);
4615
4616 if (start_char < 0)
4617 start_char += size;
4618
4619 start_byte = string_char_to_byte (object, start_char);
4620 }
4621
4622 if (NILP (end))
4623 {
4624 end_char = size;
4625 end_byte = size_byte;
4626 }
4627 else
4628 {
4629 CHECK_NUMBER (end);
4630
4631 end_char = XINT (end);
4632
4633 if (end_char < 0)
4634 end_char += size;
4635
4636 end_byte = string_char_to_byte (object, end_char);
4637 }
4638
4639 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
4640 args_out_of_range_3 (object, make_number (start_char),
4641 make_number (end_char));
4642 }
4643 else
4644 {
4645 struct buffer *prev = current_buffer;
4646
4647 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4648
4649 CHECK_BUFFER (object);
4650
4651 bp = XBUFFER (object);
4652 if (bp != current_buffer)
4653 set_buffer_internal (bp);
4654
4655 if (NILP (start))
4656 b = BEGV;
4657 else
4658 {
4659 CHECK_NUMBER_COERCE_MARKER (start);
4660 b = XINT (start);
4661 }
4662
4663 if (NILP (end))
4664 e = ZV;
4665 else
4666 {
4667 CHECK_NUMBER_COERCE_MARKER (end);
4668 e = XINT (end);
4669 }
4670
4671 if (b > e)
4672 temp = b, b = e, e = temp;
4673
4674 if (!(BEGV <= b && e <= ZV))
4675 args_out_of_range (start, end);
4676
4677 if (NILP (coding_system))
4678 {
4679 /* Decide the coding-system to encode the data with.
4680 See fileio.c:Fwrite-region */
4681
4682 if (!NILP (Vcoding_system_for_write))
4683 coding_system = Vcoding_system_for_write;
4684 else
4685 {
4686 int force_raw_text = 0;
4687
4688 coding_system = XBUFFER (object)->buffer_file_coding_system;
4689 if (NILP (coding_system)
4690 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
4691 {
4692 coding_system = Qnil;
4693 if (NILP (current_buffer->enable_multibyte_characters))
4694 force_raw_text = 1;
4695 }
4696
4697 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
4698 {
4699 /* Check file-coding-system-alist. */
4700 Lisp_Object args[4], val;
4701
4702 args[0] = Qwrite_region; args[1] = start; args[2] = end;
4703 args[3] = Fbuffer_file_name(object);
4704 val = Ffind_operation_coding_system (4, args);
4705 if (CONSP (val) && !NILP (XCDR (val)))
4706 coding_system = XCDR (val);
4707 }
4708
4709 if (NILP (coding_system)
4710 && !NILP (XBUFFER (object)->buffer_file_coding_system))
4711 {
4712 /* If we still have not decided a coding system, use the
4713 default value of buffer-file-coding-system. */
4714 coding_system = XBUFFER (object)->buffer_file_coding_system;
4715 }
4716
4717 if (!force_raw_text
4718 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
4719 /* Confirm that VAL can surely encode the current region. */
4720 coding_system = call4 (Vselect_safe_coding_system_function,
4721 make_number (b), make_number (e),
4722 coding_system, Qnil);
4723
4724 if (force_raw_text)
4725 coding_system = Qraw_text;
4726 }
4727
4728 if (NILP (Fcoding_system_p (coding_system)))
4729 {
4730 /* Invalid coding system. */
4731
4732 if (!NILP (noerror))
4733 coding_system = Qraw_text;
4734 else
4735 xsignal1 (Qcoding_system_error, coding_system);
4736 }
4737 }
4738
4739 object = make_buffer_string (b, e, 0);
4740 if (prev != current_buffer)
4741 set_buffer_internal (prev);
4742 /* Discard the unwind protect for recovering the current
4743 buffer. */
4744 specpdl_ptr--;
4745
4746 if (STRING_MULTIBYTE (object))
4747 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
4748 }
4749
4750 md5_buffer (SDATA (object) + start_byte,
4751 SBYTES (object) - (size_byte - end_byte),
4752 digest);
4753
4754 for (i = 0; i < 16; i++)
4755 sprintf (&value[2 * i], "%02x", digest[i]);
4756 value[32] = '\0';
4757
4758 return make_string (value, 32);
4759 }
4760
4761 \f
4762 void
4763 syms_of_fns (void)
4764 {
4765 /* Hash table stuff. */
4766 Qhash_table_p = intern_c_string ("hash-table-p");
4767 staticpro (&Qhash_table_p);
4768 Qeq = intern_c_string ("eq");
4769 staticpro (&Qeq);
4770 Qeql = intern_c_string ("eql");
4771 staticpro (&Qeql);
4772 Qequal = intern_c_string ("equal");
4773 staticpro (&Qequal);
4774 QCtest = intern_c_string (":test");
4775 staticpro (&QCtest);
4776 QCsize = intern_c_string (":size");
4777 staticpro (&QCsize);
4778 QCrehash_size = intern_c_string (":rehash-size");
4779 staticpro (&QCrehash_size);
4780 QCrehash_threshold = intern_c_string (":rehash-threshold");
4781 staticpro (&QCrehash_threshold);
4782 QCweakness = intern_c_string (":weakness");
4783 staticpro (&QCweakness);
4784 Qkey = intern_c_string ("key");
4785 staticpro (&Qkey);
4786 Qvalue = intern_c_string ("value");
4787 staticpro (&Qvalue);
4788 Qhash_table_test = intern_c_string ("hash-table-test");
4789 staticpro (&Qhash_table_test);
4790 Qkey_or_value = intern_c_string ("key-or-value");
4791 staticpro (&Qkey_or_value);
4792 Qkey_and_value = intern_c_string ("key-and-value");
4793 staticpro (&Qkey_and_value);
4794
4795 defsubr (&Ssxhash);
4796 defsubr (&Smake_hash_table);
4797 defsubr (&Scopy_hash_table);
4798 defsubr (&Shash_table_count);
4799 defsubr (&Shash_table_rehash_size);
4800 defsubr (&Shash_table_rehash_threshold);
4801 defsubr (&Shash_table_size);
4802 defsubr (&Shash_table_test);
4803 defsubr (&Shash_table_weakness);
4804 defsubr (&Shash_table_p);
4805 defsubr (&Sclrhash);
4806 defsubr (&Sgethash);
4807 defsubr (&Sputhash);
4808 defsubr (&Sremhash);
4809 defsubr (&Smaphash);
4810 defsubr (&Sdefine_hash_table_test);
4811
4812 Qstring_lessp = intern_c_string ("string-lessp");
4813 staticpro (&Qstring_lessp);
4814 Qprovide = intern_c_string ("provide");
4815 staticpro (&Qprovide);
4816 Qrequire = intern_c_string ("require");
4817 staticpro (&Qrequire);
4818 Qyes_or_no_p_history = intern_c_string ("yes-or-no-p-history");
4819 staticpro (&Qyes_or_no_p_history);
4820 Qcursor_in_echo_area = intern_c_string ("cursor-in-echo-area");
4821 staticpro (&Qcursor_in_echo_area);
4822 Qwidget_type = intern_c_string ("widget-type");
4823 staticpro (&Qwidget_type);
4824
4825 staticpro (&string_char_byte_cache_string);
4826 string_char_byte_cache_string = Qnil;
4827
4828 require_nesting_list = Qnil;
4829 staticpro (&require_nesting_list);
4830
4831 Fset (Qyes_or_no_p_history, Qnil);
4832
4833 DEFVAR_LISP ("features", Vfeatures,
4834 doc: /* A list of symbols which are the features of the executing Emacs.
4835 Used by `featurep' and `require', and altered by `provide'. */);
4836 Vfeatures = Fcons (intern_c_string ("emacs"), Qnil);
4837 Qsubfeatures = intern_c_string ("subfeatures");
4838 staticpro (&Qsubfeatures);
4839
4840 #ifdef HAVE_LANGINFO_CODESET
4841 Qcodeset = intern_c_string ("codeset");
4842 staticpro (&Qcodeset);
4843 Qdays = intern_c_string ("days");
4844 staticpro (&Qdays);
4845 Qmonths = intern_c_string ("months");
4846 staticpro (&Qmonths);
4847 Qpaper = intern_c_string ("paper");
4848 staticpro (&Qpaper);
4849 #endif /* HAVE_LANGINFO_CODESET */
4850
4851 DEFVAR_BOOL ("use-dialog-box", use_dialog_box,
4852 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
4853 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
4854 invoked by mouse clicks and mouse menu items.
4855
4856 On some platforms, file selection dialogs are also enabled if this is
4857 non-nil. */);
4858 use_dialog_box = 1;
4859
4860 DEFVAR_BOOL ("use-file-dialog", use_file_dialog,
4861 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
4862 This applies to commands from menus and tool bar buttons even when
4863 they are initiated from the keyboard. If `use-dialog-box' is nil,
4864 that disables the use of a file dialog, regardless of the value of
4865 this variable. */);
4866 use_file_dialog = 1;
4867
4868 defsubr (&Sidentity);
4869 defsubr (&Srandom);
4870 defsubr (&Slength);
4871 defsubr (&Ssafe_length);
4872 defsubr (&Sstring_bytes);
4873 defsubr (&Sstring_equal);
4874 defsubr (&Scompare_strings);
4875 defsubr (&Sstring_lessp);
4876 defsubr (&Sappend);
4877 defsubr (&Sconcat);
4878 defsubr (&Svconcat);
4879 defsubr (&Scopy_sequence);
4880 defsubr (&Sstring_make_multibyte);
4881 defsubr (&Sstring_make_unibyte);
4882 defsubr (&Sstring_as_multibyte);
4883 defsubr (&Sstring_as_unibyte);
4884 defsubr (&Sstring_to_multibyte);
4885 defsubr (&Sstring_to_unibyte);
4886 defsubr (&Scopy_alist);
4887 defsubr (&Ssubstring);
4888 defsubr (&Ssubstring_no_properties);
4889 defsubr (&Snthcdr);
4890 defsubr (&Snth);
4891 defsubr (&Selt);
4892 defsubr (&Smember);
4893 defsubr (&Smemq);
4894 defsubr (&Smemql);
4895 defsubr (&Sassq);
4896 defsubr (&Sassoc);
4897 defsubr (&Srassq);
4898 defsubr (&Srassoc);
4899 defsubr (&Sdelq);
4900 defsubr (&Sdelete);
4901 defsubr (&Snreverse);
4902 defsubr (&Sreverse);
4903 defsubr (&Ssort);
4904 defsubr (&Splist_get);
4905 defsubr (&Sget);
4906 defsubr (&Splist_put);
4907 defsubr (&Sput);
4908 defsubr (&Slax_plist_get);
4909 defsubr (&Slax_plist_put);
4910 defsubr (&Seql);
4911 defsubr (&Sequal);
4912 defsubr (&Sequal_including_properties);
4913 defsubr (&Sfillarray);
4914 defsubr (&Sclear_string);
4915 defsubr (&Snconc);
4916 defsubr (&Smapcar);
4917 defsubr (&Smapc);
4918 defsubr (&Smapconcat);
4919 defsubr (&Syes_or_no_p);
4920 defsubr (&Sload_average);
4921 defsubr (&Sfeaturep);
4922 defsubr (&Srequire);
4923 defsubr (&Sprovide);
4924 defsubr (&Splist_member);
4925 defsubr (&Swidget_put);
4926 defsubr (&Swidget_get);
4927 defsubr (&Swidget_apply);
4928 defsubr (&Sbase64_encode_region);
4929 defsubr (&Sbase64_decode_region);
4930 defsubr (&Sbase64_encode_string);
4931 defsubr (&Sbase64_decode_string);
4932 defsubr (&Smd5);
4933 defsubr (&Slocale_info);
4934 }
4935
4936
4937 void
4938 init_fns (void)
4939 {
4940 }
4941