make the minibuffer mutex recursive.
[emacs.git] / src / fns.c
blob5ec367c944fe239dc9b97b1612ad4b59bbda0c99
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 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #include <time.h>
27 #include <setjmp.h>
29 /* Note on some machines this defines `vector' as a typedef,
30 so make sure we don't use that name in this file. */
31 #undef vector
32 #define vector *****
34 #include "lisp.h"
35 #include "commands.h"
36 #include "character.h"
37 #include "coding.h"
38 #include "buffer.h"
39 #include "keyboard.h"
40 #include "keymap.h"
41 #include "intervals.h"
42 #include "frame.h"
43 #include "window.h"
44 #include "blockinput.h"
45 #ifdef HAVE_MENUS
46 #if defined (HAVE_X_WINDOWS)
47 #include "xterm.h"
48 #endif
49 #endif /* HAVE_MENUS */
51 #ifndef NULL
52 #define NULL ((POINTER_TYPE *)0)
53 #endif
55 /* Nonzero enables use of dialog boxes for questions
56 asked by mouse commands. */
57 int use_dialog_box;
59 /* Nonzero enables use of a file dialog for file name
60 questions asked by mouse commands. */
61 int use_file_dialog;
63 extern int minibuffer_auto_raise;
64 extern Lisp_Object minibuf_window;
65 extern Lisp_Object impl_Vlocale_coding_system;
66 extern int load_in_progress;
68 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
69 Lisp_Object Qyes_or_no_p_history;
70 Lisp_Object Qcursor_in_echo_area;
71 Lisp_Object Qwidget_type;
72 Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
74 extern Lisp_Object Qinput_method_function;
76 static int internal_equal P_ ((Lisp_Object , Lisp_Object, int, int));
78 extern long get_random ();
79 extern void seed_random P_ ((long));
81 static Lisp_Object Fyes_or_no1 (Lisp_Object prompt);
83 #ifndef HAVE_UNISTD_H
84 extern long time ();
85 #endif
87 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
88 doc: /* Return the argument unchanged. */)
89 (arg)
90 Lisp_Object arg;
92 return arg;
95 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
96 doc: /* Return a pseudo-random number.
97 All integers representable in Lisp are equally likely.
98 On most systems, this is 29 bits' worth.
99 With positive integer LIMIT, return random number in interval [0,LIMIT).
100 With argument t, set the random number seed from the current time and pid.
101 Other values of LIMIT are ignored. */)
102 (limit)
103 Lisp_Object limit;
105 EMACS_INT val;
106 Lisp_Object lispy_val;
107 unsigned long denominator;
109 if (EQ (limit, Qt))
110 seed_random (getpid () + time (NULL));
111 if (NATNUMP (limit) && XFASTINT (limit) != 0)
113 /* Try to take our random number from the higher bits of VAL,
114 not the lower, since (says Gentzel) the low bits of `random'
115 are less random than the higher ones. We do this by using the
116 quotient rather than the remainder. At the high end of the RNG
117 it's possible to get a quotient larger than n; discarding
118 these values eliminates the bias that would otherwise appear
119 when using a large n. */
120 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit);
122 val = get_random () / denominator;
123 while (val >= XFASTINT (limit));
125 else
126 val = get_random ();
127 XSETINT (lispy_val, val);
128 return lispy_val;
131 /* Random data-structure functions */
133 DEFUN ("length", Flength, Slength, 1, 1, 0,
134 doc: /* Return the length of vector, list or string SEQUENCE.
135 A byte-code function object is also allowed.
136 If the string contains multibyte characters, this is not necessarily
137 the number of bytes in the string; it is the number of characters.
138 To get the number of bytes, use `string-bytes'. */)
139 (sequence)
140 register Lisp_Object sequence;
142 register Lisp_Object val;
143 register int i;
145 if (STRINGP (sequence))
146 XSETFASTINT (val, SCHARS (sequence));
147 else if (VECTORP (sequence))
148 XSETFASTINT (val, ASIZE (sequence));
149 else if (CHAR_TABLE_P (sequence))
150 XSETFASTINT (val, MAX_CHAR);
151 else if (BOOL_VECTOR_P (sequence))
152 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
153 else if (COMPILEDP (sequence))
154 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
155 else if (CONSP (sequence))
157 i = 0;
158 while (CONSP (sequence))
160 sequence = XCDR (sequence);
161 ++i;
163 if (!CONSP (sequence))
164 break;
166 sequence = XCDR (sequence);
167 ++i;
168 QUIT;
171 CHECK_LIST_END (sequence, sequence);
173 val = make_number (i);
175 else if (NILP (sequence))
176 XSETFASTINT (val, 0);
177 else
178 wrong_type_argument (Qsequencep, sequence);
180 return val;
183 /* This does not check for quits. That is safe since it must terminate. */
185 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
186 doc: /* Return the length of a list, but avoid error or infinite loop.
187 This function never gets an error. If LIST is not really a list,
188 it returns 0. If LIST is circular, it returns a finite value
189 which is at least the number of distinct elements. */)
190 (list)
191 Lisp_Object list;
193 Lisp_Object tail, halftail, length;
194 int len = 0;
196 /* halftail is used to detect circular lists. */
197 halftail = list;
198 for (tail = list; CONSP (tail); tail = XCDR (tail))
200 if (EQ (tail, halftail) && len != 0)
201 break;
202 len++;
203 if ((len & 1) == 0)
204 halftail = XCDR (halftail);
207 XSETINT (length, len);
208 return length;
211 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
212 doc: /* Return the number of bytes in STRING.
213 If STRING is multibyte, this may be greater than the length of STRING. */)
214 (string)
215 Lisp_Object string;
217 CHECK_STRING (string);
218 return make_number (SBYTES (string));
221 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
222 doc: /* Return t if two strings have identical contents.
223 Case is significant, but text properties are ignored.
224 Symbols are also allowed; their print names are used instead. */)
225 (s1, s2)
226 register Lisp_Object s1, s2;
228 if (SYMBOLP (s1))
229 s1 = SYMBOL_NAME (s1);
230 if (SYMBOLP (s2))
231 s2 = SYMBOL_NAME (s2);
232 CHECK_STRING (s1);
233 CHECK_STRING (s2);
235 if (SCHARS (s1) != SCHARS (s2)
236 || SBYTES (s1) != SBYTES (s2)
237 || bcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
238 return Qnil;
239 return Qt;
242 DEFUN ("compare-strings", Fcompare_strings,
243 Scompare_strings, 6, 7, 0,
244 doc: /* Compare the contents of two strings, converting to multibyte if needed.
245 In string STR1, skip the first START1 characters and stop at END1.
246 In string STR2, skip the first START2 characters and stop at END2.
247 END1 and END2 default to the full lengths of the respective strings.
249 Case is significant in this comparison if IGNORE-CASE is nil.
250 Unibyte strings are converted to multibyte for comparison.
252 The value is t if the strings (or specified portions) match.
253 If string STR1 is less, the value is a negative number N;
254 - 1 - N is the number of characters that match at the beginning.
255 If string STR1 is greater, the value is a positive number N;
256 N - 1 is the number of characters that match at the beginning. */)
257 (str1, start1, end1, str2, start2, end2, ignore_case)
258 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
260 register int end1_char, end2_char;
261 register int i1, i1_byte, i2, i2_byte;
263 CHECK_STRING (str1);
264 CHECK_STRING (str2);
265 if (NILP (start1))
266 start1 = make_number (0);
267 if (NILP (start2))
268 start2 = make_number (0);
269 CHECK_NATNUM (start1);
270 CHECK_NATNUM (start2);
271 if (! NILP (end1))
272 CHECK_NATNUM (end1);
273 if (! NILP (end2))
274 CHECK_NATNUM (end2);
276 i1 = XINT (start1);
277 i2 = XINT (start2);
279 i1_byte = string_char_to_byte (str1, i1);
280 i2_byte = string_char_to_byte (str2, i2);
282 end1_char = SCHARS (str1);
283 if (! NILP (end1) && end1_char > XINT (end1))
284 end1_char = XINT (end1);
286 end2_char = SCHARS (str2);
287 if (! NILP (end2) && end2_char > XINT (end2))
288 end2_char = XINT (end2);
290 while (i1 < end1_char && i2 < end2_char)
292 /* When we find a mismatch, we must compare the
293 characters, not just the bytes. */
294 int c1, c2;
296 if (STRING_MULTIBYTE (str1))
297 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
298 else
300 c1 = SREF (str1, i1++);
301 MAKE_CHAR_MULTIBYTE (c1);
304 if (STRING_MULTIBYTE (str2))
305 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
306 else
308 c2 = SREF (str2, i2++);
309 MAKE_CHAR_MULTIBYTE (c2);
312 if (c1 == c2)
313 continue;
315 if (! NILP (ignore_case))
317 Lisp_Object tem;
319 tem = Fupcase (make_number (c1));
320 c1 = XINT (tem);
321 tem = Fupcase (make_number (c2));
322 c2 = XINT (tem);
325 if (c1 == c2)
326 continue;
328 /* Note that I1 has already been incremented
329 past the character that we are comparing;
330 hence we don't add or subtract 1 here. */
331 if (c1 < c2)
332 return make_number (- i1 + XINT (start1));
333 else
334 return make_number (i1 - XINT (start1));
337 if (i1 < end1_char)
338 return make_number (i1 - XINT (start1) + 1);
339 if (i2 < end2_char)
340 return make_number (- i1 + XINT (start1) - 1);
342 return Qt;
345 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
346 doc: /* Return t if first arg string is less than second in lexicographic order.
347 Case is significant.
348 Symbols are also allowed; their print names are used instead. */)
349 (s1, s2)
350 register Lisp_Object s1, s2;
352 register int end;
353 register int i1, i1_byte, i2, i2_byte;
355 if (SYMBOLP (s1))
356 s1 = SYMBOL_NAME (s1);
357 if (SYMBOLP (s2))
358 s2 = SYMBOL_NAME (s2);
359 CHECK_STRING (s1);
360 CHECK_STRING (s2);
362 i1 = i1_byte = i2 = i2_byte = 0;
364 end = SCHARS (s1);
365 if (end > SCHARS (s2))
366 end = SCHARS (s2);
368 while (i1 < end)
370 /* When we find a mismatch, we must compare the
371 characters, not just the bytes. */
372 int c1, c2;
374 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
375 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
377 if (c1 != c2)
378 return c1 < c2 ? Qt : Qnil;
380 return i1 < SCHARS (s2) ? Qt : Qnil;
383 #if __GNUC__
384 /* "gcc -O3" enables automatic function inlining, which optimizes out
385 the arguments for the invocations of this function, whereas it
386 expects these values on the stack. */
387 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special)) __attribute__((noinline));
388 #else /* !__GNUC__ */
389 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special));
390 #endif
392 /* ARGSUSED */
393 Lisp_Object
394 concat2 (s1, s2)
395 Lisp_Object s1, s2;
397 #ifdef NO_ARG_ARRAY
398 Lisp_Object args[2];
399 args[0] = s1;
400 args[1] = s2;
401 return concat (2, args, Lisp_String, 0);
402 #else
403 return concat (2, &s1, Lisp_String, 0);
404 #endif /* NO_ARG_ARRAY */
407 /* ARGSUSED */
408 Lisp_Object
409 concat3 (s1, s2, s3)
410 Lisp_Object s1, s2, s3;
412 #ifdef NO_ARG_ARRAY
413 Lisp_Object args[3];
414 args[0] = s1;
415 args[1] = s2;
416 args[2] = s3;
417 return concat (3, args, Lisp_String, 0);
418 #else
419 return concat (3, &s1, Lisp_String, 0);
420 #endif /* NO_ARG_ARRAY */
423 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
424 doc: /* Concatenate all the arguments and make the result a list.
425 The result is a list whose elements are the elements of all the arguments.
426 Each argument may be a list, vector or string.
427 The last argument is not copied, just used as the tail of the new list.
428 usage: (append &rest SEQUENCES) */)
429 (nargs, args)
430 int nargs;
431 Lisp_Object *args;
433 return concat (nargs, args, Lisp_Cons, 1);
436 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
437 doc: /* Concatenate all the arguments and make the result a string.
438 The result is a string whose elements are the elements of all the arguments.
439 Each argument may be a string or a list or vector of characters (integers).
440 usage: (concat &rest SEQUENCES) */)
441 (nargs, args)
442 int nargs;
443 Lisp_Object *args;
445 return concat (nargs, args, Lisp_String, 0);
448 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
449 doc: /* Concatenate all the arguments and make the result a vector.
450 The result is a vector whose elements are the elements of all the arguments.
451 Each argument may be a list, vector or string.
452 usage: (vconcat &rest SEQUENCES) */)
453 (nargs, args)
454 int nargs;
455 Lisp_Object *args;
457 return concat (nargs, args, Lisp_Vectorlike, 0);
461 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
462 doc: /* Return a copy of a list, vector, string or char-table.
463 The elements of a list or vector are not copied; they are shared
464 with the original. */)
465 (arg)
466 Lisp_Object arg;
468 if (NILP (arg)) return arg;
470 if (CHAR_TABLE_P (arg))
472 return copy_char_table (arg);
475 if (BOOL_VECTOR_P (arg))
477 Lisp_Object val;
478 int size_in_chars
479 = ((XBOOL_VECTOR (arg)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
480 / BOOL_VECTOR_BITS_PER_CHAR);
482 val = Fmake_bool_vector (Flength (arg), Qnil);
483 bcopy (XBOOL_VECTOR (arg)->data, XBOOL_VECTOR (val)->data,
484 size_in_chars);
485 return val;
488 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
489 wrong_type_argument (Qsequencep, arg);
491 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
494 /* This structure holds information of an argument of `concat' that is
495 a string and has text properties to be copied. */
496 struct textprop_rec
498 int argnum; /* refer to ARGS (arguments of `concat') */
499 int from; /* refer to ARGS[argnum] (argument string) */
500 int to; /* refer to VAL (the target string) */
503 static Lisp_Object
504 concat (nargs, args, target_type, last_special)
505 int nargs;
506 Lisp_Object *args;
507 enum Lisp_Type target_type;
508 int last_special;
510 Lisp_Object val;
511 register Lisp_Object tail;
512 register Lisp_Object this;
513 int toindex;
514 int toindex_byte = 0;
515 register int result_len;
516 register int result_len_byte;
517 register int argnum;
518 Lisp_Object last_tail;
519 Lisp_Object prev;
520 int some_multibyte;
521 /* When we make a multibyte string, we can't copy text properties
522 while concatinating each string because the length of resulting
523 string can't be decided until we finish the whole concatination.
524 So, we record strings that have text properties to be copied
525 here, and copy the text properties after the concatination. */
526 struct textprop_rec *textprops = NULL;
527 /* Number of elments in textprops. */
528 int num_textprops = 0;
529 USE_SAFE_ALLOCA;
531 tail = Qnil;
533 /* In append, the last arg isn't treated like the others */
534 if (last_special && nargs > 0)
536 nargs--;
537 last_tail = args[nargs];
539 else
540 last_tail = Qnil;
542 /* Check each argument. */
543 for (argnum = 0; argnum < nargs; argnum++)
545 this = args[argnum];
546 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
547 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
548 wrong_type_argument (Qsequencep, this);
551 /* Compute total length in chars of arguments in RESULT_LEN.
552 If desired output is a string, also compute length in bytes
553 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
554 whether the result should be a multibyte string. */
555 result_len_byte = 0;
556 result_len = 0;
557 some_multibyte = 0;
558 for (argnum = 0; argnum < nargs; argnum++)
560 int len;
561 this = args[argnum];
562 len = XFASTINT (Flength (this));
563 if (target_type == Lisp_String)
565 /* We must count the number of bytes needed in the string
566 as well as the number of characters. */
567 int i;
568 Lisp_Object ch;
569 int this_len_byte;
571 if (VECTORP (this))
572 for (i = 0; i < len; i++)
574 ch = AREF (this, i);
575 CHECK_CHARACTER (ch);
576 this_len_byte = CHAR_BYTES (XINT (ch));
577 result_len_byte += this_len_byte;
578 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
579 some_multibyte = 1;
581 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
582 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
583 else if (CONSP (this))
584 for (; CONSP (this); this = XCDR (this))
586 ch = XCAR (this);
587 CHECK_CHARACTER (ch);
588 this_len_byte = CHAR_BYTES (XINT (ch));
589 result_len_byte += this_len_byte;
590 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
591 some_multibyte = 1;
593 else if (STRINGP (this))
595 if (STRING_MULTIBYTE (this))
597 some_multibyte = 1;
598 result_len_byte += SBYTES (this);
600 else
601 result_len_byte += count_size_as_multibyte (SDATA (this),
602 SCHARS (this));
606 result_len += len;
607 if (result_len < 0)
608 error ("String overflow");
611 if (! some_multibyte)
612 result_len_byte = result_len;
614 /* Create the output object. */
615 if (target_type == Lisp_Cons)
616 val = Fmake_list (make_number (result_len), Qnil);
617 else if (target_type == Lisp_Vectorlike)
618 val = Fmake_vector (make_number (result_len), Qnil);
619 else if (some_multibyte)
620 val = make_uninit_multibyte_string (result_len, result_len_byte);
621 else
622 val = make_uninit_string (result_len);
624 /* In `append', if all but last arg are nil, return last arg. */
625 if (target_type == Lisp_Cons && EQ (val, Qnil))
626 return last_tail;
628 /* Copy the contents of the args into the result. */
629 if (CONSP (val))
630 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
631 else
632 toindex = 0, toindex_byte = 0;
634 prev = Qnil;
635 if (STRINGP (val))
636 SAFE_ALLOCA (textprops, struct textprop_rec *, sizeof (struct textprop_rec) * nargs);
638 for (argnum = 0; argnum < nargs; argnum++)
640 Lisp_Object thislen;
641 int thisleni = 0;
642 register unsigned int thisindex = 0;
643 register unsigned int thisindex_byte = 0;
645 this = args[argnum];
646 if (!CONSP (this))
647 thislen = Flength (this), thisleni = XINT (thislen);
649 /* Between strings of the same kind, copy fast. */
650 if (STRINGP (this) && STRINGP (val)
651 && STRING_MULTIBYTE (this) == some_multibyte)
653 int thislen_byte = SBYTES (this);
655 bcopy (SDATA (this), SDATA (val) + toindex_byte,
656 SBYTES (this));
657 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
659 textprops[num_textprops].argnum = argnum;
660 textprops[num_textprops].from = 0;
661 textprops[num_textprops++].to = toindex;
663 toindex_byte += thislen_byte;
664 toindex += thisleni;
666 /* Copy a single-byte string to a multibyte string. */
667 else if (STRINGP (this) && STRINGP (val))
669 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
671 textprops[num_textprops].argnum = argnum;
672 textprops[num_textprops].from = 0;
673 textprops[num_textprops++].to = toindex;
675 toindex_byte += copy_text (SDATA (this),
676 SDATA (val) + toindex_byte,
677 SCHARS (this), 0, 1);
678 toindex += thisleni;
680 else
681 /* Copy element by element. */
682 while (1)
684 register Lisp_Object elt;
686 /* Fetch next element of `this' arg into `elt', or break if
687 `this' is exhausted. */
688 if (NILP (this)) break;
689 if (CONSP (this))
690 elt = XCAR (this), this = XCDR (this);
691 else if (thisindex >= thisleni)
692 break;
693 else if (STRINGP (this))
695 int c;
696 if (STRING_MULTIBYTE (this))
698 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
699 thisindex,
700 thisindex_byte);
701 XSETFASTINT (elt, c);
703 else
705 XSETFASTINT (elt, SREF (this, thisindex)); thisindex++;
706 if (some_multibyte
707 && !ASCII_CHAR_P (XINT (elt))
708 && XINT (elt) < 0400)
710 c = BYTE8_TO_CHAR (XINT (elt));
711 XSETINT (elt, c);
715 else if (BOOL_VECTOR_P (this))
717 int byte;
718 byte = XBOOL_VECTOR (this)->data[thisindex / BOOL_VECTOR_BITS_PER_CHAR];
719 if (byte & (1 << (thisindex % BOOL_VECTOR_BITS_PER_CHAR)))
720 elt = Qt;
721 else
722 elt = Qnil;
723 thisindex++;
725 else
727 elt = AREF (this, thisindex);
728 thisindex++;
731 /* Store this element into the result. */
732 if (toindex < 0)
734 XSETCAR (tail, elt);
735 prev = tail;
736 tail = XCDR (tail);
738 else if (VECTORP (val))
740 ASET (val, toindex, elt);
741 toindex++;
743 else
745 CHECK_NUMBER (elt);
746 if (some_multibyte)
747 toindex_byte += CHAR_STRING (XINT (elt),
748 SDATA (val) + toindex_byte);
749 else
750 SSET (val, toindex_byte++, XINT (elt));
751 toindex++;
755 if (!NILP (prev))
756 XSETCDR (prev, last_tail);
758 if (num_textprops > 0)
760 Lisp_Object props;
761 int last_to_end = -1;
763 for (argnum = 0; argnum < num_textprops; argnum++)
765 this = args[textprops[argnum].argnum];
766 props = text_property_list (this,
767 make_number (0),
768 make_number (SCHARS (this)),
769 Qnil);
770 /* If successive arguments have properites, be sure that the
771 value of `composition' property be the copy. */
772 if (last_to_end == textprops[argnum].to)
773 make_composition_value_copy (props);
774 add_text_properties_from_list (val, props,
775 make_number (textprops[argnum].to));
776 last_to_end = textprops[argnum].to + SCHARS (this);
780 SAFE_FREE ();
781 return val;
784 static Lisp_Object string_char_byte_cache_string;
785 static EMACS_INT string_char_byte_cache_charpos;
786 static EMACS_INT string_char_byte_cache_bytepos;
788 void
789 clear_string_char_byte_cache ()
791 string_char_byte_cache_string = Qnil;
794 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
796 EMACS_INT
797 string_char_to_byte (string, char_index)
798 Lisp_Object string;
799 EMACS_INT char_index;
801 EMACS_INT i_byte;
802 EMACS_INT best_below, best_below_byte;
803 EMACS_INT best_above, best_above_byte;
805 best_below = best_below_byte = 0;
806 best_above = SCHARS (string);
807 best_above_byte = SBYTES (string);
808 if (best_above == best_above_byte)
809 return char_index;
811 if (EQ (string, string_char_byte_cache_string))
813 if (string_char_byte_cache_charpos < char_index)
815 best_below = string_char_byte_cache_charpos;
816 best_below_byte = string_char_byte_cache_bytepos;
818 else
820 best_above = string_char_byte_cache_charpos;
821 best_above_byte = string_char_byte_cache_bytepos;
825 if (char_index - best_below < best_above - char_index)
827 unsigned char *p = SDATA (string) + best_below_byte;
829 while (best_below < char_index)
831 p += BYTES_BY_CHAR_HEAD (*p);
832 best_below++;
834 i_byte = p - SDATA (string);
836 else
838 unsigned char *p = SDATA (string) + best_above_byte;
840 while (best_above > char_index)
842 p--;
843 while (!CHAR_HEAD_P (*p)) p--;
844 best_above--;
846 i_byte = p - SDATA (string);
849 string_char_byte_cache_bytepos = i_byte;
850 string_char_byte_cache_charpos = char_index;
851 string_char_byte_cache_string = string;
853 return i_byte;
856 /* Return the character index corresponding to BYTE_INDEX in STRING. */
858 EMACS_INT
859 string_byte_to_char (string, byte_index)
860 Lisp_Object string;
861 EMACS_INT byte_index;
863 EMACS_INT i, i_byte;
864 EMACS_INT best_below, best_below_byte;
865 EMACS_INT best_above, best_above_byte;
867 best_below = best_below_byte = 0;
868 best_above = SCHARS (string);
869 best_above_byte = SBYTES (string);
870 if (best_above == best_above_byte)
871 return byte_index;
873 if (EQ (string, string_char_byte_cache_string))
875 if (string_char_byte_cache_bytepos < byte_index)
877 best_below = string_char_byte_cache_charpos;
878 best_below_byte = string_char_byte_cache_bytepos;
880 else
882 best_above = string_char_byte_cache_charpos;
883 best_above_byte = string_char_byte_cache_bytepos;
887 if (byte_index - best_below_byte < best_above_byte - byte_index)
889 unsigned char *p = SDATA (string) + best_below_byte;
890 unsigned char *pend = SDATA (string) + byte_index;
892 while (p < pend)
894 p += BYTES_BY_CHAR_HEAD (*p);
895 best_below++;
897 i = best_below;
898 i_byte = p - SDATA (string);
900 else
902 unsigned char *p = SDATA (string) + best_above_byte;
903 unsigned char *pbeg = SDATA (string) + byte_index;
905 while (p > pbeg)
907 p--;
908 while (!CHAR_HEAD_P (*p)) p--;
909 best_above--;
911 i = best_above;
912 i_byte = p - SDATA (string);
915 string_char_byte_cache_bytepos = i_byte;
916 string_char_byte_cache_charpos = i;
917 string_char_byte_cache_string = string;
919 return i;
922 /* Convert STRING to a multibyte string. */
924 Lisp_Object
925 string_make_multibyte (string)
926 Lisp_Object string;
928 unsigned char *buf;
929 EMACS_INT nbytes;
930 Lisp_Object ret;
931 USE_SAFE_ALLOCA;
933 if (STRING_MULTIBYTE (string))
934 return string;
936 nbytes = count_size_as_multibyte (SDATA (string),
937 SCHARS (string));
938 /* If all the chars are ASCII, they won't need any more bytes
939 once converted. In that case, we can return STRING itself. */
940 if (nbytes == SBYTES (string))
941 return string;
943 SAFE_ALLOCA (buf, unsigned char *, nbytes);
944 copy_text (SDATA (string), buf, SBYTES (string),
945 0, 1);
947 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
948 SAFE_FREE ();
950 return ret;
954 /* Convert STRING (if unibyte) to a multibyte string without changing
955 the number of characters. Characters 0200 trough 0237 are
956 converted to eight-bit characters. */
958 Lisp_Object
959 string_to_multibyte (string)
960 Lisp_Object string;
962 unsigned char *buf;
963 EMACS_INT nbytes;
964 Lisp_Object ret;
965 USE_SAFE_ALLOCA;
967 if (STRING_MULTIBYTE (string))
968 return string;
970 nbytes = parse_str_to_multibyte (SDATA (string), SBYTES (string));
971 /* If all the chars are ASCII, they won't need any more bytes once
972 converted. */
973 if (nbytes == SBYTES (string))
974 return make_multibyte_string (SDATA (string), nbytes, nbytes);
976 SAFE_ALLOCA (buf, unsigned char *, nbytes);
977 bcopy (SDATA (string), buf, SBYTES (string));
978 str_to_multibyte (buf, nbytes, SBYTES (string));
980 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
981 SAFE_FREE ();
983 return ret;
987 /* Convert STRING to a single-byte string. */
989 Lisp_Object
990 string_make_unibyte (string)
991 Lisp_Object string;
993 int nchars;
994 unsigned char *buf;
995 Lisp_Object ret;
996 USE_SAFE_ALLOCA;
998 if (! STRING_MULTIBYTE (string))
999 return string;
1001 nchars = SCHARS (string);
1003 SAFE_ALLOCA (buf, unsigned char *, nchars);
1004 copy_text (SDATA (string), buf, SBYTES (string),
1005 1, 0);
1007 ret = make_unibyte_string (buf, nchars);
1008 SAFE_FREE ();
1010 return ret;
1013 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1014 1, 1, 0,
1015 doc: /* Return the multibyte equivalent of STRING.
1016 If STRING is unibyte and contains non-ASCII characters, the function
1017 `unibyte-char-to-multibyte' is used to convert each unibyte character
1018 to a multibyte character. In this case, the returned string is a
1019 newly created string with no text properties. If STRING is multibyte
1020 or entirely ASCII, it is returned unchanged. In particular, when
1021 STRING is unibyte and entirely ASCII, the returned string is unibyte.
1022 \(When the characters are all ASCII, Emacs primitives will treat the
1023 string the same way whether it is unibyte or multibyte.) */)
1024 (string)
1025 Lisp_Object string;
1027 CHECK_STRING (string);
1029 return string_make_multibyte (string);
1032 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1033 1, 1, 0,
1034 doc: /* Return the unibyte equivalent of STRING.
1035 Multibyte character codes are converted to unibyte according to
1036 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1037 If the lookup in the translation table fails, this function takes just
1038 the low 8 bits of each character. */)
1039 (string)
1040 Lisp_Object string;
1042 CHECK_STRING (string);
1044 return string_make_unibyte (string);
1047 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1048 1, 1, 0,
1049 doc: /* Return a unibyte string with the same individual bytes as STRING.
1050 If STRING is unibyte, the result is STRING itself.
1051 Otherwise it is a newly created string, with no text properties.
1052 If STRING is multibyte and contains a character of charset
1053 `eight-bit', it is converted to the corresponding single byte. */)
1054 (string)
1055 Lisp_Object string;
1057 CHECK_STRING (string);
1059 if (STRING_MULTIBYTE (string))
1061 int bytes = SBYTES (string);
1062 unsigned char *str = (unsigned char *) xmalloc (bytes);
1064 bcopy (SDATA (string), str, bytes);
1065 bytes = str_as_unibyte (str, bytes);
1066 string = make_unibyte_string (str, bytes);
1067 xfree (str);
1069 return string;
1072 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1073 1, 1, 0,
1074 doc: /* Return a multibyte string with the same individual bytes as STRING.
1075 If STRING is multibyte, the result is STRING itself.
1076 Otherwise it is a newly created string, with no text properties.
1078 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1079 part of a correct utf-8 sequence), it is converted to the corresponding
1080 multibyte character of charset `eight-bit'.
1081 See also `string-to-multibyte'.
1083 Beware, this often doesn't really do what you think it does.
1084 It is similar to (decode-coding-string STRING 'utf-8-emacs).
1085 If you're not sure, whether to use `string-as-multibyte' or
1086 `string-to-multibyte', use `string-to-multibyte'. */)
1087 (string)
1088 Lisp_Object string;
1090 CHECK_STRING (string);
1092 if (! STRING_MULTIBYTE (string))
1094 Lisp_Object new_string;
1095 int nchars, nbytes;
1097 parse_str_as_multibyte (SDATA (string),
1098 SBYTES (string),
1099 &nchars, &nbytes);
1100 new_string = make_uninit_multibyte_string (nchars, nbytes);
1101 bcopy (SDATA (string), SDATA (new_string),
1102 SBYTES (string));
1103 if (nbytes != SBYTES (string))
1104 str_as_multibyte (SDATA (new_string), nbytes,
1105 SBYTES (string), NULL);
1106 string = new_string;
1107 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1109 return string;
1112 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1113 1, 1, 0,
1114 doc: /* Return a multibyte string with the same individual chars as STRING.
1115 If STRING is multibyte, the result is STRING itself.
1116 Otherwise it is a newly created string, with no text properties.
1118 If STRING is unibyte and contains an 8-bit byte, it is converted to
1119 the corresponding multibyte character of charset `eight-bit'.
1121 This differs from `string-as-multibyte' by converting each byte of a correct
1122 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1123 correct sequence. */)
1124 (string)
1125 Lisp_Object string;
1127 CHECK_STRING (string);
1129 return string_to_multibyte (string);
1132 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1133 1, 1, 0,
1134 doc: /* Return a unibyte string with the same individual chars as STRING.
1135 If STRING is unibyte, the result is STRING itself.
1136 Otherwise it is a newly created string, with no text properties,
1137 where each `eight-bit' character is converted to the corresponding byte.
1138 If STRING contains a non-ASCII, non-`eight-bit' character,
1139 an error is signaled. */)
1140 (string)
1141 Lisp_Object string;
1143 CHECK_STRING (string);
1145 if (STRING_MULTIBYTE (string))
1147 EMACS_INT chars = SCHARS (string);
1148 unsigned char *str = (unsigned char *) xmalloc (chars);
1149 EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars, 0);
1151 if (converted < chars)
1152 error ("Can't convert the %dth character to unibyte", converted);
1153 string = make_unibyte_string (str, chars);
1154 xfree (str);
1156 return string;
1160 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1161 doc: /* Return a copy of ALIST.
1162 This is an alist which represents the same mapping from objects to objects,
1163 but does not share the alist structure with ALIST.
1164 The objects mapped (cars and cdrs of elements of the alist)
1165 are shared, however.
1166 Elements of ALIST that are not conses are also shared. */)
1167 (alist)
1168 Lisp_Object alist;
1170 register Lisp_Object tem;
1172 CHECK_LIST (alist);
1173 if (NILP (alist))
1174 return alist;
1175 alist = concat (1, &alist, Lisp_Cons, 0);
1176 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1178 register Lisp_Object car;
1179 car = XCAR (tem);
1181 if (CONSP (car))
1182 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1184 return alist;
1187 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1188 doc: /* Return a new string whose contents are a substring of STRING.
1189 The returned string consists of the characters between index FROM
1190 \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1191 zero-indexed: 0 means the first character of STRING. Negative values
1192 are counted from the end of STRING. If TO is nil, the substring runs
1193 to the end of STRING.
1195 The STRING argument may also be a vector. In that case, the return
1196 value is a new vector that contains the elements between index FROM
1197 \(inclusive) and index TO (exclusive) of that vector argument. */)
1198 (string, from, to)
1199 Lisp_Object string;
1200 register Lisp_Object from, to;
1202 Lisp_Object res;
1203 int size;
1204 int size_byte = 0;
1205 int from_char, to_char;
1206 int from_byte = 0, to_byte = 0;
1208 CHECK_VECTOR_OR_STRING (string);
1209 CHECK_NUMBER (from);
1211 if (STRINGP (string))
1213 size = SCHARS (string);
1214 size_byte = SBYTES (string);
1216 else
1217 size = ASIZE (string);
1219 if (NILP (to))
1221 to_char = size;
1222 to_byte = size_byte;
1224 else
1226 CHECK_NUMBER (to);
1228 to_char = XINT (to);
1229 if (to_char < 0)
1230 to_char += size;
1232 if (STRINGP (string))
1233 to_byte = string_char_to_byte (string, to_char);
1236 from_char = XINT (from);
1237 if (from_char < 0)
1238 from_char += size;
1239 if (STRINGP (string))
1240 from_byte = string_char_to_byte (string, from_char);
1242 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1243 args_out_of_range_3 (string, make_number (from_char),
1244 make_number (to_char));
1246 if (STRINGP (string))
1248 res = make_specified_string (SDATA (string) + from_byte,
1249 to_char - from_char, to_byte - from_byte,
1250 STRING_MULTIBYTE (string));
1251 copy_text_properties (make_number (from_char), make_number (to_char),
1252 string, make_number (0), res, Qnil);
1254 else
1255 res = Fvector (to_char - from_char, &AREF (string, from_char));
1257 return res;
1261 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1262 doc: /* Return a substring of STRING, without text properties.
1263 It starts at index FROM and ending before TO.
1264 TO may be nil or omitted; then the substring runs to the end of STRING.
1265 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1266 If FROM or TO is negative, it counts from the end.
1268 With one argument, just copy STRING without its properties. */)
1269 (string, from, to)
1270 Lisp_Object string;
1271 register Lisp_Object from, to;
1273 int size, size_byte;
1274 int from_char, to_char;
1275 int from_byte, to_byte;
1277 CHECK_STRING (string);
1279 size = SCHARS (string);
1280 size_byte = SBYTES (string);
1282 if (NILP (from))
1283 from_char = from_byte = 0;
1284 else
1286 CHECK_NUMBER (from);
1287 from_char = XINT (from);
1288 if (from_char < 0)
1289 from_char += size;
1291 from_byte = string_char_to_byte (string, from_char);
1294 if (NILP (to))
1296 to_char = size;
1297 to_byte = size_byte;
1299 else
1301 CHECK_NUMBER (to);
1303 to_char = XINT (to);
1304 if (to_char < 0)
1305 to_char += size;
1307 to_byte = string_char_to_byte (string, to_char);
1310 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1311 args_out_of_range_3 (string, make_number (from_char),
1312 make_number (to_char));
1314 return make_specified_string (SDATA (string) + from_byte,
1315 to_char - from_char, to_byte - from_byte,
1316 STRING_MULTIBYTE (string));
1319 /* Extract a substring of STRING, giving start and end positions
1320 both in characters and in bytes. */
1322 Lisp_Object
1323 substring_both (string, from, from_byte, to, to_byte)
1324 Lisp_Object string;
1325 int from, from_byte, to, to_byte;
1327 Lisp_Object res;
1328 int size;
1329 int size_byte;
1331 CHECK_VECTOR_OR_STRING (string);
1333 if (STRINGP (string))
1335 size = SCHARS (string);
1336 size_byte = SBYTES (string);
1338 else
1339 size = ASIZE (string);
1341 if (!(0 <= from && from <= to && to <= size))
1342 args_out_of_range_3 (string, make_number (from), make_number (to));
1344 if (STRINGP (string))
1346 res = make_specified_string (SDATA (string) + from_byte,
1347 to - from, to_byte - from_byte,
1348 STRING_MULTIBYTE (string));
1349 copy_text_properties (make_number (from), make_number (to),
1350 string, make_number (0), res, Qnil);
1352 else
1353 res = Fvector (to - from, &AREF (string, from));
1355 return res;
1358 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1359 doc: /* Take cdr N times on LIST, returns the result. */)
1360 (n, list)
1361 Lisp_Object n;
1362 register Lisp_Object list;
1364 register int i, num;
1365 CHECK_NUMBER (n);
1366 num = XINT (n);
1367 for (i = 0; i < num && !NILP (list); i++)
1369 QUIT;
1370 CHECK_LIST_CONS (list, list);
1371 list = XCDR (list);
1373 return list;
1376 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1377 doc: /* Return the Nth element of LIST.
1378 N counts from zero. If LIST is not that long, nil is returned. */)
1379 (n, list)
1380 Lisp_Object n, list;
1382 return Fcar (Fnthcdr (n, list));
1385 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1386 doc: /* Return element of SEQUENCE at index N. */)
1387 (sequence, n)
1388 register Lisp_Object sequence, n;
1390 CHECK_NUMBER (n);
1391 if (CONSP (sequence) || NILP (sequence))
1392 return Fcar (Fnthcdr (n, sequence));
1394 /* Faref signals a "not array" error, so check here. */
1395 CHECK_ARRAY (sequence, Qsequencep);
1396 return Faref (sequence, n);
1399 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1400 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1401 The value is actually the tail of LIST whose car is ELT. */)
1402 (elt, list)
1403 register Lisp_Object elt;
1404 Lisp_Object list;
1406 register Lisp_Object tail;
1407 for (tail = list; CONSP (tail); tail = XCDR (tail))
1409 register Lisp_Object tem;
1410 CHECK_LIST_CONS (tail, list);
1411 tem = XCAR (tail);
1412 if (! NILP (Fequal (elt, tem)))
1413 return tail;
1414 QUIT;
1416 return Qnil;
1419 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1420 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1421 The value is actually the tail of LIST whose car is ELT. */)
1422 (elt, list)
1423 register Lisp_Object elt, list;
1425 while (1)
1427 if (!CONSP (list) || EQ (XCAR (list), elt))
1428 break;
1430 list = XCDR (list);
1431 if (!CONSP (list) || EQ (XCAR (list), elt))
1432 break;
1434 list = XCDR (list);
1435 if (!CONSP (list) || EQ (XCAR (list), elt))
1436 break;
1438 list = XCDR (list);
1439 QUIT;
1442 CHECK_LIST (list);
1443 return list;
1446 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1447 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1448 The value is actually the tail of LIST whose car is ELT. */)
1449 (elt, list)
1450 register Lisp_Object elt;
1451 Lisp_Object list;
1453 register Lisp_Object tail;
1455 if (!FLOATP (elt))
1456 return Fmemq (elt, list);
1458 for (tail = list; CONSP (tail); tail = XCDR (tail))
1460 register Lisp_Object tem;
1461 CHECK_LIST_CONS (tail, list);
1462 tem = XCAR (tail);
1463 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1464 return tail;
1465 QUIT;
1467 return Qnil;
1470 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1471 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1472 The value is actually the first element of LIST whose car is KEY.
1473 Elements of LIST that are not conses are ignored. */)
1474 (key, list)
1475 Lisp_Object key, list;
1477 while (1)
1479 if (!CONSP (list)
1480 || (CONSP (XCAR (list))
1481 && EQ (XCAR (XCAR (list)), key)))
1482 break;
1484 list = XCDR (list);
1485 if (!CONSP (list)
1486 || (CONSP (XCAR (list))
1487 && EQ (XCAR (XCAR (list)), key)))
1488 break;
1490 list = XCDR (list);
1491 if (!CONSP (list)
1492 || (CONSP (XCAR (list))
1493 && EQ (XCAR (XCAR (list)), key)))
1494 break;
1496 list = XCDR (list);
1497 QUIT;
1500 return CAR (list);
1503 /* Like Fassq but never report an error and do not allow quits.
1504 Use only on lists known never to be circular. */
1506 Lisp_Object
1507 assq_no_quit (key, list)
1508 Lisp_Object key, list;
1510 while (CONSP (list)
1511 && (!CONSP (XCAR (list))
1512 || !EQ (XCAR (XCAR (list)), key)))
1513 list = XCDR (list);
1515 return CAR_SAFE (list);
1518 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1519 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1520 The value is actually the first element of LIST whose car equals KEY. */)
1521 (key, list)
1522 Lisp_Object key, list;
1524 Lisp_Object car;
1526 while (1)
1528 if (!CONSP (list)
1529 || (CONSP (XCAR (list))
1530 && (car = XCAR (XCAR (list)),
1531 EQ (car, key) || !NILP (Fequal (car, key)))))
1532 break;
1534 list = XCDR (list);
1535 if (!CONSP (list)
1536 || (CONSP (XCAR (list))
1537 && (car = XCAR (XCAR (list)),
1538 EQ (car, key) || !NILP (Fequal (car, key)))))
1539 break;
1541 list = XCDR (list);
1542 if (!CONSP (list)
1543 || (CONSP (XCAR (list))
1544 && (car = XCAR (XCAR (list)),
1545 EQ (car, key) || !NILP (Fequal (car, key)))))
1546 break;
1548 list = XCDR (list);
1549 QUIT;
1552 return CAR (list);
1555 /* Like Fassoc but never report an error and do not allow quits.
1556 Use only on lists known never to be circular. */
1558 Lisp_Object
1559 assoc_no_quit (key, list)
1560 Lisp_Object key, list;
1562 while (CONSP (list)
1563 && (!CONSP (XCAR (list))
1564 || (!EQ (XCAR (XCAR (list)), key)
1565 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1566 list = XCDR (list);
1568 return CONSP (list) ? XCAR (list) : Qnil;
1571 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1572 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1573 The value is actually the first element of LIST whose cdr is KEY. */)
1574 (key, list)
1575 register Lisp_Object key;
1576 Lisp_Object list;
1578 while (1)
1580 if (!CONSP (list)
1581 || (CONSP (XCAR (list))
1582 && EQ (XCDR (XCAR (list)), key)))
1583 break;
1585 list = XCDR (list);
1586 if (!CONSP (list)
1587 || (CONSP (XCAR (list))
1588 && EQ (XCDR (XCAR (list)), key)))
1589 break;
1591 list = XCDR (list);
1592 if (!CONSP (list)
1593 || (CONSP (XCAR (list))
1594 && EQ (XCDR (XCAR (list)), key)))
1595 break;
1597 list = XCDR (list);
1598 QUIT;
1601 return CAR (list);
1604 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1605 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1606 The value is actually the first element of LIST whose cdr equals KEY. */)
1607 (key, list)
1608 Lisp_Object key, list;
1610 Lisp_Object cdr;
1612 while (1)
1614 if (!CONSP (list)
1615 || (CONSP (XCAR (list))
1616 && (cdr = XCDR (XCAR (list)),
1617 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1618 break;
1620 list = XCDR (list);
1621 if (!CONSP (list)
1622 || (CONSP (XCAR (list))
1623 && (cdr = XCDR (XCAR (list)),
1624 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1625 break;
1627 list = XCDR (list);
1628 if (!CONSP (list)
1629 || (CONSP (XCAR (list))
1630 && (cdr = XCDR (XCAR (list)),
1631 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1632 break;
1634 list = XCDR (list);
1635 QUIT;
1638 return CAR (list);
1641 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1642 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1643 The modified LIST is returned. Comparison is done with `eq'.
1644 If the first member of LIST is ELT, there is no way to remove it by side effect;
1645 therefore, write `(setq foo (delq element foo))'
1646 to be sure of changing the value of `foo'. */)
1647 (elt, list)
1648 register Lisp_Object elt;
1649 Lisp_Object list;
1651 register Lisp_Object tail, prev;
1652 register Lisp_Object tem;
1654 tail = list;
1655 prev = Qnil;
1656 while (!NILP (tail))
1658 CHECK_LIST_CONS (tail, list);
1659 tem = XCAR (tail);
1660 if (EQ (elt, tem))
1662 if (NILP (prev))
1663 list = XCDR (tail);
1664 else
1665 Fsetcdr (prev, XCDR (tail));
1667 else
1668 prev = tail;
1669 tail = XCDR (tail);
1670 QUIT;
1672 return list;
1675 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1676 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1677 SEQ must be a list, a vector, or a string.
1678 The modified SEQ is returned. Comparison is done with `equal'.
1679 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1680 is not a side effect; it is simply using a different sequence.
1681 Therefore, write `(setq foo (delete element foo))'
1682 to be sure of changing the value of `foo'. */)
1683 (elt, seq)
1684 Lisp_Object elt, seq;
1686 if (VECTORP (seq))
1688 EMACS_INT i, n;
1690 for (i = n = 0; i < ASIZE (seq); ++i)
1691 if (NILP (Fequal (AREF (seq, i), elt)))
1692 ++n;
1694 if (n != ASIZE (seq))
1696 struct Lisp_Vector *p = allocate_vector (n);
1698 for (i = n = 0; i < ASIZE (seq); ++i)
1699 if (NILP (Fequal (AREF (seq, i), elt)))
1700 p->contents[n++] = AREF (seq, i);
1702 XSETVECTOR (seq, p);
1705 else if (STRINGP (seq))
1707 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1708 int c;
1710 for (i = nchars = nbytes = ibyte = 0;
1711 i < SCHARS (seq);
1712 ++i, ibyte += cbytes)
1714 if (STRING_MULTIBYTE (seq))
1716 c = STRING_CHAR (SDATA (seq) + ibyte);
1717 cbytes = CHAR_BYTES (c);
1719 else
1721 c = SREF (seq, i);
1722 cbytes = 1;
1725 if (!INTEGERP (elt) || c != XINT (elt))
1727 ++nchars;
1728 nbytes += cbytes;
1732 if (nchars != SCHARS (seq))
1734 Lisp_Object tem;
1736 tem = make_uninit_multibyte_string (nchars, nbytes);
1737 if (!STRING_MULTIBYTE (seq))
1738 STRING_SET_UNIBYTE (tem);
1740 for (i = nchars = nbytes = ibyte = 0;
1741 i < SCHARS (seq);
1742 ++i, ibyte += cbytes)
1744 if (STRING_MULTIBYTE (seq))
1746 c = STRING_CHAR (SDATA (seq) + ibyte);
1747 cbytes = CHAR_BYTES (c);
1749 else
1751 c = SREF (seq, i);
1752 cbytes = 1;
1755 if (!INTEGERP (elt) || c != XINT (elt))
1757 unsigned char *from = SDATA (seq) + ibyte;
1758 unsigned char *to = SDATA (tem) + nbytes;
1759 EMACS_INT n;
1761 ++nchars;
1762 nbytes += cbytes;
1764 for (n = cbytes; n--; )
1765 *to++ = *from++;
1769 seq = tem;
1772 else
1774 Lisp_Object tail, prev;
1776 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1778 CHECK_LIST_CONS (tail, seq);
1780 if (!NILP (Fequal (elt, XCAR (tail))))
1782 if (NILP (prev))
1783 seq = XCDR (tail);
1784 else
1785 Fsetcdr (prev, XCDR (tail));
1787 else
1788 prev = tail;
1789 QUIT;
1793 return seq;
1796 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1797 doc: /* Reverse LIST by modifying cdr pointers.
1798 Return the reversed list. */)
1799 (list)
1800 Lisp_Object list;
1802 register Lisp_Object prev, tail, next;
1804 if (NILP (list)) return list;
1805 prev = Qnil;
1806 tail = list;
1807 while (!NILP (tail))
1809 QUIT;
1810 CHECK_LIST_CONS (tail, list);
1811 next = XCDR (tail);
1812 Fsetcdr (tail, prev);
1813 prev = tail;
1814 tail = next;
1816 return prev;
1819 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1820 doc: /* Reverse LIST, copying. Return the reversed list.
1821 See also the function `nreverse', which is used more often. */)
1822 (list)
1823 Lisp_Object list;
1825 Lisp_Object new;
1827 for (new = Qnil; CONSP (list); list = XCDR (list))
1829 QUIT;
1830 new = Fcons (XCAR (list), new);
1832 CHECK_LIST_END (list, list);
1833 return new;
1836 Lisp_Object merge ();
1838 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1839 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1840 Returns the sorted list. LIST is modified by side effects.
1841 PREDICATE is called with two elements of LIST, and should return non-nil
1842 if the first element should sort before the second. */)
1843 (list, predicate)
1844 Lisp_Object list, predicate;
1846 Lisp_Object front, back;
1847 register Lisp_Object len, tem;
1848 struct gcpro gcpro1, gcpro2;
1849 register int length;
1851 front = list;
1852 len = Flength (list);
1853 length = XINT (len);
1854 if (length < 2)
1855 return list;
1857 XSETINT (len, (length / 2) - 1);
1858 tem = Fnthcdr (len, list);
1859 back = Fcdr (tem);
1860 Fsetcdr (tem, Qnil);
1862 GCPRO2 (front, back);
1863 front = Fsort (front, predicate);
1864 back = Fsort (back, predicate);
1865 UNGCPRO;
1866 return merge (front, back, predicate);
1869 Lisp_Object
1870 merge (org_l1, org_l2, pred)
1871 Lisp_Object org_l1, org_l2;
1872 Lisp_Object pred;
1874 Lisp_Object value;
1875 register Lisp_Object tail;
1876 Lisp_Object tem;
1877 register Lisp_Object l1, l2;
1878 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1880 l1 = org_l1;
1881 l2 = org_l2;
1882 tail = Qnil;
1883 value = Qnil;
1885 /* It is sufficient to protect org_l1 and org_l2.
1886 When l1 and l2 are updated, we copy the new values
1887 back into the org_ vars. */
1888 GCPRO4 (org_l1, org_l2, pred, value);
1890 while (1)
1892 if (NILP (l1))
1894 UNGCPRO;
1895 if (NILP (tail))
1896 return l2;
1897 Fsetcdr (tail, l2);
1898 return value;
1900 if (NILP (l2))
1902 UNGCPRO;
1903 if (NILP (tail))
1904 return l1;
1905 Fsetcdr (tail, l1);
1906 return value;
1908 tem = call2 (pred, Fcar (l2), Fcar (l1));
1909 if (NILP (tem))
1911 tem = l1;
1912 l1 = Fcdr (l1);
1913 org_l1 = l1;
1915 else
1917 tem = l2;
1918 l2 = Fcdr (l2);
1919 org_l2 = l2;
1921 if (NILP (tail))
1922 value = tem;
1923 else
1924 Fsetcdr (tail, tem);
1925 tail = tem;
1930 /* This does not check for quits. That is safe since it must terminate. */
1932 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1933 doc: /* Extract a value from a property list.
1934 PLIST is a property list, which is a list of the form
1935 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1936 corresponding to the given PROP, or nil if PROP is not one of the
1937 properties on the list. This function never signals an error. */)
1938 (plist, prop)
1939 Lisp_Object plist;
1940 Lisp_Object prop;
1942 Lisp_Object tail, halftail;
1944 /* halftail is used to detect circular lists. */
1945 tail = halftail = plist;
1946 while (CONSP (tail) && CONSP (XCDR (tail)))
1948 if (EQ (prop, XCAR (tail)))
1949 return XCAR (XCDR (tail));
1951 tail = XCDR (XCDR (tail));
1952 halftail = XCDR (halftail);
1953 if (EQ (tail, halftail))
1954 break;
1956 #if 0 /* Unsafe version. */
1957 /* This function can be called asynchronously
1958 (setup_coding_system). Don't QUIT in that case. */
1959 if (!interrupt_input_blocked)
1960 QUIT;
1961 #endif
1964 return Qnil;
1967 DEFUN ("get", Fget, Sget, 2, 2, 0,
1968 doc: /* Return the value of SYMBOL's PROPNAME property.
1969 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1970 (symbol, propname)
1971 Lisp_Object symbol, propname;
1973 CHECK_SYMBOL (symbol);
1974 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1977 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1978 doc: /* Change value in PLIST of PROP to VAL.
1979 PLIST is a property list, which is a list of the form
1980 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1981 If PROP is already a property on the list, its value is set to VAL,
1982 otherwise the new PROP VAL pair is added. The new plist is returned;
1983 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1984 The PLIST is modified by side effects. */)
1985 (plist, prop, val)
1986 Lisp_Object plist;
1987 register Lisp_Object prop;
1988 Lisp_Object val;
1990 register Lisp_Object tail, prev;
1991 Lisp_Object newcell;
1992 prev = Qnil;
1993 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1994 tail = XCDR (XCDR (tail)))
1996 if (EQ (prop, XCAR (tail)))
1998 Fsetcar (XCDR (tail), val);
1999 return plist;
2002 prev = tail;
2003 QUIT;
2005 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
2006 if (NILP (prev))
2007 return newcell;
2008 else
2009 Fsetcdr (XCDR (prev), newcell);
2010 return plist;
2013 DEFUN ("put", Fput, Sput, 3, 3, 0,
2014 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
2015 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
2016 (symbol, propname, value)
2017 Lisp_Object symbol, propname, value;
2019 CHECK_SYMBOL (symbol);
2020 XSYMBOL (symbol)->plist
2021 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
2022 return value;
2025 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
2026 doc: /* Extract a value from a property list, comparing with `equal'.
2027 PLIST is a property list, which is a list of the form
2028 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2029 corresponding to the given PROP, or nil if PROP is not
2030 one of the properties on the list. */)
2031 (plist, prop)
2032 Lisp_Object plist;
2033 Lisp_Object prop;
2035 Lisp_Object tail;
2037 for (tail = plist;
2038 CONSP (tail) && CONSP (XCDR (tail));
2039 tail = XCDR (XCDR (tail)))
2041 if (! NILP (Fequal (prop, XCAR (tail))))
2042 return XCAR (XCDR (tail));
2044 QUIT;
2047 CHECK_LIST_END (tail, prop);
2049 return Qnil;
2052 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
2053 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2054 PLIST is a property list, which is a list of the form
2055 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2056 If PROP is already a property on the list, its value is set to VAL,
2057 otherwise the new PROP VAL pair is added. The new plist is returned;
2058 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2059 The PLIST is modified by side effects. */)
2060 (plist, prop, val)
2061 Lisp_Object plist;
2062 register Lisp_Object prop;
2063 Lisp_Object val;
2065 register Lisp_Object tail, prev;
2066 Lisp_Object newcell;
2067 prev = Qnil;
2068 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2069 tail = XCDR (XCDR (tail)))
2071 if (! NILP (Fequal (prop, XCAR (tail))))
2073 Fsetcar (XCDR (tail), val);
2074 return plist;
2077 prev = tail;
2078 QUIT;
2080 newcell = Fcons (prop, Fcons (val, Qnil));
2081 if (NILP (prev))
2082 return newcell;
2083 else
2084 Fsetcdr (XCDR (prev), newcell);
2085 return plist;
2088 DEFUN ("eql", Feql, Seql, 2, 2, 0,
2089 doc: /* Return t if the two args are the same Lisp object.
2090 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
2091 (obj1, obj2)
2092 Lisp_Object obj1, obj2;
2094 if (FLOATP (obj1))
2095 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
2096 else
2097 return EQ (obj1, obj2) ? Qt : Qnil;
2100 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
2101 doc: /* Return t if two Lisp objects have similar structure and contents.
2102 They must have the same data type.
2103 Conses are compared by comparing the cars and the cdrs.
2104 Vectors and strings are compared element by element.
2105 Numbers are compared by value, but integers cannot equal floats.
2106 (Use `=' if you want integers and floats to be able to be equal.)
2107 Symbols must match exactly. */)
2108 (o1, o2)
2109 register Lisp_Object o1, o2;
2111 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
2114 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2115 doc: /* Return t if two Lisp objects have similar structure and contents.
2116 This is like `equal' except that it compares the text properties
2117 of strings. (`equal' ignores text properties.) */)
2118 (o1, o2)
2119 register Lisp_Object o1, o2;
2121 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
2124 /* DEPTH is current depth of recursion. Signal an error if it
2125 gets too deep.
2126 PROPS, if non-nil, means compare string text properties too. */
2128 static int
2129 internal_equal (o1, o2, depth, props)
2130 register Lisp_Object o1, o2;
2131 int depth, props;
2133 if (depth > 200)
2134 error ("Stack overflow in equal");
2136 tail_recurse:
2137 QUIT;
2138 if (EQ (o1, o2))
2139 return 1;
2140 if (XTYPE (o1) != XTYPE (o2))
2141 return 0;
2143 switch (XTYPE (o1))
2145 case Lisp_Float:
2147 double d1, d2;
2149 d1 = extract_float (o1);
2150 d2 = extract_float (o2);
2151 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2152 though they are not =. */
2153 return d1 == d2 || (d1 != d1 && d2 != d2);
2156 case Lisp_Cons:
2157 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
2158 return 0;
2159 o1 = XCDR (o1);
2160 o2 = XCDR (o2);
2161 goto tail_recurse;
2163 case Lisp_Misc:
2164 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2165 return 0;
2166 if (OVERLAYP (o1))
2168 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2169 depth + 1, props)
2170 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2171 depth + 1, props))
2172 return 0;
2173 o1 = XOVERLAY (o1)->plist;
2174 o2 = XOVERLAY (o2)->plist;
2175 goto tail_recurse;
2177 if (MARKERP (o1))
2179 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2180 && (XMARKER (o1)->buffer == 0
2181 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2183 break;
2185 case Lisp_Vectorlike:
2187 register int i;
2188 EMACS_INT size = ASIZE (o1);
2189 /* Pseudovectors have the type encoded in the size field, so this test
2190 actually checks that the objects have the same type as well as the
2191 same size. */
2192 if (ASIZE (o2) != size)
2193 return 0;
2194 /* Boolvectors are compared much like strings. */
2195 if (BOOL_VECTOR_P (o1))
2197 int size_in_chars
2198 = ((XBOOL_VECTOR (o1)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2199 / BOOL_VECTOR_BITS_PER_CHAR);
2201 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2202 return 0;
2203 if (bcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2204 size_in_chars))
2205 return 0;
2206 return 1;
2208 if (WINDOW_CONFIGURATIONP (o1))
2209 return compare_window_configurations (o1, o2, 0);
2211 /* Aside from them, only true vectors, char-tables, compiled
2212 functions, and fonts (font-spec, font-entity, font-ojbect)
2213 are sensible to compare, so eliminate the others now. */
2214 if (size & PSEUDOVECTOR_FLAG)
2216 if (!(size & (PVEC_COMPILED
2217 | PVEC_CHAR_TABLE | PVEC_SUB_CHAR_TABLE | PVEC_FONT)))
2218 return 0;
2219 size &= PSEUDOVECTOR_SIZE_MASK;
2221 for (i = 0; i < size; i++)
2223 Lisp_Object v1, v2;
2224 v1 = AREF (o1, i);
2225 v2 = AREF (o2, i);
2226 if (!internal_equal (v1, v2, depth + 1, props))
2227 return 0;
2229 return 1;
2231 break;
2233 case Lisp_String:
2234 if (SCHARS (o1) != SCHARS (o2))
2235 return 0;
2236 if (SBYTES (o1) != SBYTES (o2))
2237 return 0;
2238 if (bcmp (SDATA (o1), SDATA (o2),
2239 SBYTES (o1)))
2240 return 0;
2241 if (props && !compare_string_intervals (o1, o2))
2242 return 0;
2243 return 1;
2245 default:
2246 break;
2249 return 0;
2252 extern Lisp_Object Fmake_char_internal ();
2254 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2255 doc: /* Store each element of ARRAY with ITEM.
2256 ARRAY is a vector, string, char-table, or bool-vector. */)
2257 (array, item)
2258 Lisp_Object array, item;
2260 register int size, index, charval;
2261 if (VECTORP (array))
2263 register Lisp_Object *p = XVECTOR (array)->contents;
2264 size = ASIZE (array);
2265 for (index = 0; index < size; index++)
2266 p[index] = item;
2268 else if (CHAR_TABLE_P (array))
2270 int i;
2272 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2273 XCHAR_TABLE (array)->contents[i] = item;
2274 XCHAR_TABLE (array)->defalt = item;
2276 else if (STRINGP (array))
2278 register unsigned char *p = SDATA (array);
2279 CHECK_NUMBER (item);
2280 charval = XINT (item);
2281 size = SCHARS (array);
2282 if (STRING_MULTIBYTE (array))
2284 unsigned char str[MAX_MULTIBYTE_LENGTH];
2285 int len = CHAR_STRING (charval, str);
2286 int size_byte = SBYTES (array);
2287 unsigned char *p1 = p, *endp = p + size_byte;
2288 int i;
2290 if (size != size_byte)
2291 while (p1 < endp)
2293 int this_len = MULTIBYTE_FORM_LENGTH (p1, endp - p1);
2294 if (len != this_len)
2295 error ("Attempt to change byte length of a string");
2296 p1 += this_len;
2298 for (i = 0; i < size_byte; i++)
2299 *p++ = str[i % len];
2301 else
2302 for (index = 0; index < size; index++)
2303 p[index] = charval;
2305 else if (BOOL_VECTOR_P (array))
2307 register unsigned char *p = XBOOL_VECTOR (array)->data;
2308 int size_in_chars
2309 = ((XBOOL_VECTOR (array)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2310 / BOOL_VECTOR_BITS_PER_CHAR);
2312 charval = (! NILP (item) ? -1 : 0);
2313 for (index = 0; index < size_in_chars - 1; index++)
2314 p[index] = charval;
2315 if (index < size_in_chars)
2317 /* Mask out bits beyond the vector size. */
2318 if (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)
2319 charval &= (1 << (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2320 p[index] = charval;
2323 else
2324 wrong_type_argument (Qarrayp, array);
2325 return array;
2328 DEFUN ("clear-string", Fclear_string, Sclear_string,
2329 1, 1, 0,
2330 doc: /* Clear the contents of STRING.
2331 This makes STRING unibyte and may change its length. */)
2332 (string)
2333 Lisp_Object string;
2335 int len;
2336 CHECK_STRING (string);
2337 len = SBYTES (string);
2338 bzero (SDATA (string), len);
2339 STRING_SET_CHARS (string, len);
2340 STRING_SET_UNIBYTE (string);
2341 return Qnil;
2344 /* ARGSUSED */
2345 Lisp_Object
2346 nconc2 (s1, s2)
2347 Lisp_Object s1, s2;
2349 #ifdef NO_ARG_ARRAY
2350 Lisp_Object args[2];
2351 args[0] = s1;
2352 args[1] = s2;
2353 return Fnconc (2, args);
2354 #else
2355 return Fnconc (2, &s1);
2356 #endif /* NO_ARG_ARRAY */
2359 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2360 doc: /* Concatenate any number of lists by altering them.
2361 Only the last argument is not altered, and need not be a list.
2362 usage: (nconc &rest LISTS) */)
2363 (nargs, args)
2364 int nargs;
2365 Lisp_Object *args;
2367 register int argnum;
2368 register Lisp_Object tail, tem, val;
2370 val = tail = Qnil;
2372 for (argnum = 0; argnum < nargs; argnum++)
2374 tem = args[argnum];
2375 if (NILP (tem)) continue;
2377 if (NILP (val))
2378 val = tem;
2380 if (argnum + 1 == nargs) break;
2382 CHECK_LIST_CONS (tem, tem);
2384 while (CONSP (tem))
2386 tail = tem;
2387 tem = XCDR (tail);
2388 QUIT;
2391 tem = args[argnum + 1];
2392 Fsetcdr (tail, tem);
2393 if (NILP (tem))
2394 args[argnum + 1] = tail;
2397 return val;
2400 /* This is the guts of all mapping functions.
2401 Apply FN to each element of SEQ, one by one,
2402 storing the results into elements of VALS, a C vector of Lisp_Objects.
2403 LENI is the length of VALS, which should also be the length of SEQ. */
2405 static void
2406 mapcar1 (leni, vals, fn, seq)
2407 int leni;
2408 Lisp_Object *vals;
2409 Lisp_Object fn, seq;
2411 register Lisp_Object tail;
2412 Lisp_Object dummy;
2413 register int i;
2414 struct gcpro gcpro1, gcpro2, gcpro3;
2416 if (vals)
2418 /* Don't let vals contain any garbage when GC happens. */
2419 for (i = 0; i < leni; i++)
2420 vals[i] = Qnil;
2422 GCPRO3 (dummy, fn, seq);
2423 gcpro1.var = vals;
2424 gcpro1.nvars = leni;
2426 else
2427 GCPRO2 (fn, seq);
2428 /* We need not explicitly protect `tail' because it is used only on lists, and
2429 1) lists are not relocated and 2) the list is marked via `seq' so will not
2430 be freed */
2432 if (VECTORP (seq))
2434 for (i = 0; i < leni; i++)
2436 dummy = call1 (fn, AREF (seq, i));
2437 if (vals)
2438 vals[i] = dummy;
2441 else if (BOOL_VECTOR_P (seq))
2443 for (i = 0; i < leni; i++)
2445 int byte;
2446 byte = XBOOL_VECTOR (seq)->data[i / BOOL_VECTOR_BITS_PER_CHAR];
2447 dummy = (byte & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR))) ? Qt : Qnil;
2448 dummy = call1 (fn, dummy);
2449 if (vals)
2450 vals[i] = dummy;
2453 else if (STRINGP (seq))
2455 int i_byte;
2457 for (i = 0, i_byte = 0; i < leni;)
2459 int c;
2460 int i_before = i;
2462 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2463 XSETFASTINT (dummy, c);
2464 dummy = call1 (fn, dummy);
2465 if (vals)
2466 vals[i_before] = dummy;
2469 else /* Must be a list, since Flength did not get an error */
2471 tail = seq;
2472 for (i = 0; i < leni && CONSP (tail); i++)
2474 dummy = call1 (fn, XCAR (tail));
2475 if (vals)
2476 vals[i] = dummy;
2477 tail = XCDR (tail);
2481 UNGCPRO;
2484 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2485 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2486 In between each pair of results, stick in SEPARATOR. Thus, " " as
2487 SEPARATOR results in spaces between the values returned by FUNCTION.
2488 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2489 (function, sequence, separator)
2490 Lisp_Object function, sequence, separator;
2492 Lisp_Object len;
2493 register int leni;
2494 int nargs;
2495 register Lisp_Object *args;
2496 register int i;
2497 struct gcpro gcpro1;
2498 Lisp_Object ret;
2499 USE_SAFE_ALLOCA;
2501 len = Flength (sequence);
2502 if (CHAR_TABLE_P (sequence))
2503 wrong_type_argument (Qlistp, sequence);
2504 leni = XINT (len);
2505 nargs = leni + leni - 1;
2506 if (nargs < 0) return empty_unibyte_string;
2508 SAFE_ALLOCA_LISP (args, nargs);
2510 GCPRO1 (separator);
2511 mapcar1 (leni, args, function, sequence);
2512 UNGCPRO;
2514 for (i = leni - 1; i > 0; i--)
2515 args[i + i] = args[i];
2517 for (i = 1; i < nargs; i += 2)
2518 args[i] = separator;
2520 ret = Fconcat (nargs, args);
2521 SAFE_FREE ();
2523 return ret;
2526 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2527 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2528 The result is a list just as long as SEQUENCE.
2529 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2530 (function, sequence)
2531 Lisp_Object function, sequence;
2533 register Lisp_Object len;
2534 register int leni;
2535 register Lisp_Object *args;
2536 Lisp_Object ret;
2537 USE_SAFE_ALLOCA;
2539 len = Flength (sequence);
2540 if (CHAR_TABLE_P (sequence))
2541 wrong_type_argument (Qlistp, sequence);
2542 leni = XFASTINT (len);
2544 SAFE_ALLOCA_LISP (args, leni);
2546 mapcar1 (leni, args, function, sequence);
2548 ret = Flist (leni, args);
2549 SAFE_FREE ();
2551 return ret;
2554 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2555 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2556 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2557 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2558 (function, sequence)
2559 Lisp_Object function, sequence;
2561 register int leni;
2563 leni = XFASTINT (Flength (sequence));
2564 if (CHAR_TABLE_P (sequence))
2565 wrong_type_argument (Qlistp, sequence);
2566 mapcar1 (leni, 0, function, sequence);
2568 return sequence;
2571 /* Anything that calls this function must protect from GC! */
2573 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0,
2574 doc: /* Ask user a "y or n" question. Return t if answer is "y".
2575 Takes one argument, which is the string to display to ask the question.
2576 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2577 No confirmation of the answer is requested; a single character is enough.
2578 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2579 the bindings in `query-replace-map'; see the documentation of that variable
2580 for more information. In this case, the useful bindings are `act', `skip',
2581 `recenter', and `quit'.\)
2583 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2584 is nil and `use-dialog-box' is non-nil. */)
2585 (prompt)
2586 Lisp_Object prompt;
2588 register Lisp_Object obj, key, def, map;
2589 register int answer;
2590 Lisp_Object xprompt;
2591 Lisp_Object args[2];
2592 struct gcpro gcpro1, gcpro2;
2593 int count = SPECPDL_INDEX ();
2595 specbind (Qcursor_in_echo_area, Qt);
2597 map = Fsymbol_value (intern ("query-replace-map"));
2599 CHECK_STRING (prompt);
2600 xprompt = prompt;
2601 GCPRO2 (prompt, xprompt);
2603 #ifdef HAVE_WINDOW_SYSTEM
2604 if (display_hourglass_p)
2605 cancel_hourglass ();
2606 #endif
2608 while (1)
2611 #ifdef HAVE_MENUS
2612 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2613 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2614 && use_dialog_box
2615 && have_menus_p ())
2617 Lisp_Object pane, menu;
2618 redisplay_preserve_echo_area (3);
2619 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2620 Fcons (Fcons (build_string ("No"), Qnil),
2621 Qnil));
2622 menu = Fcons (prompt, pane);
2623 obj = Fx_popup_dialog (Qt, menu, Qnil);
2624 answer = !NILP (obj);
2625 break;
2627 #endif /* HAVE_MENUS */
2628 cursor_in_echo_area = 1;
2629 choose_minibuf_frame ();
2632 Lisp_Object pargs[3];
2634 /* Colorize prompt according to `minibuffer-prompt' face. */
2635 pargs[0] = build_string ("%s(y or n) ");
2636 pargs[1] = intern ("face");
2637 pargs[2] = intern ("minibuffer-prompt");
2638 args[0] = Fpropertize (3, pargs);
2639 args[1] = xprompt;
2640 Fmessage (2, args);
2643 if (minibuffer_auto_raise)
2645 Lisp_Object mini_frame;
2647 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
2649 Fraise_frame (mini_frame);
2652 temporarily_switch_to_single_kboard (SELECTED_FRAME ());
2653 obj = read_filtered_event (1, 0, 0, 0, Qnil);
2654 cursor_in_echo_area = 0;
2655 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2656 QUIT;
2658 key = Fmake_vector (make_number (1), obj);
2659 def = Flookup_key (map, key, Qt);
2661 if (EQ (def, intern ("skip")))
2663 answer = 0;
2664 break;
2666 else if (EQ (def, intern ("act")))
2668 answer = 1;
2669 break;
2671 else if (EQ (def, intern ("recenter")))
2673 Frecenter (Qnil);
2674 xprompt = prompt;
2675 continue;
2677 else if (EQ (def, intern ("quit")))
2678 Vquit_flag = Qt;
2679 /* We want to exit this command for exit-prefix,
2680 and this is the only way to do it. */
2681 else if (EQ (def, intern ("exit-prefix")))
2682 Vquit_flag = Qt;
2684 QUIT;
2686 /* If we don't clear this, then the next call to read_char will
2687 return quit_char again, and we'll enter an infinite loop. */
2688 Vquit_flag = Qnil;
2690 Fding (Qnil);
2691 Fdiscard_input ();
2692 if (EQ (xprompt, prompt))
2694 args[0] = build_string ("Please answer y or n. ");
2695 args[1] = prompt;
2696 xprompt = Fconcat (2, args);
2699 UNGCPRO;
2701 if (! noninteractive)
2703 cursor_in_echo_area = -1;
2704 message_with_string (answer ? "%s(y or n) y" : "%s(y or n) n",
2705 xprompt, 0);
2708 unbind_to (count, Qnil);
2709 return answer ? Qt : Qnil;
2712 /* This is how C code calls `yes-or-no-p' and allows the user
2713 to redefined it.
2715 Anything that calls this function must protect from GC! */
2717 Lisp_Object
2718 do_yes_or_no_p (prompt)
2719 Lisp_Object prompt;
2721 return call1 (intern ("yes-or-no-p"), prompt);
2724 /* Anything that calls this function must protect from GC! */
2726 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2727 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2728 Takes one argument, which is the string to display to ask the question.
2729 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
2730 The user must confirm the answer with RET,
2731 and can edit it until it has been confirmed.
2733 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2734 is nil, and `use-dialog-box' is non-nil. */)
2735 (prompt)
2736 Lisp_Object prompt;
2738 Lisp_Object ret;
2739 int count = SPECPDL_INDEX ();
2741 Fmutex_lock (minibuffer_mutex);
2742 record_unwind_protect (Fmutex_unlock, minibuffer_mutex);
2743 ret = Fyes_or_no1 (prompt);
2744 unbind_to (count, Qnil);
2745 return ret;
2748 Lisp_Object
2749 Fyes_or_no1 (Lisp_Object prompt)
2751 register Lisp_Object ans;
2752 Lisp_Object args[2];
2753 struct gcpro gcpro1;
2755 CHECK_STRING (prompt);
2757 #ifdef HAVE_MENUS
2758 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2759 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2760 && use_dialog_box
2761 && have_menus_p ())
2763 Lisp_Object pane, menu, obj;
2764 redisplay_preserve_echo_area (4);
2765 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2766 Fcons (Fcons (build_string ("No"), Qnil),
2767 Qnil));
2768 GCPRO1 (pane);
2769 menu = Fcons (prompt, pane);
2770 obj = Fx_popup_dialog (Qt, menu, Qnil);
2771 UNGCPRO;
2772 return obj;
2774 #endif /* HAVE_MENUS */
2776 args[0] = prompt;
2777 args[1] = build_string ("(yes or no) ");
2778 prompt = Fconcat (2, args);
2780 GCPRO1 (prompt);
2782 while (1)
2784 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2785 Qyes_or_no_p_history, Qnil,
2786 Qnil));
2787 if (SCHARS (ans) == 3 && !strcmp (SDATA (ans), "yes"))
2789 UNGCPRO;
2790 return Qt;
2792 if (SCHARS (ans) == 2 && !strcmp (SDATA (ans), "no"))
2794 UNGCPRO;
2795 return Qnil;
2798 Fding (Qnil);
2799 Fdiscard_input ();
2800 message ("Please answer yes or no.");
2801 Fsleep_for (make_number (2), Qnil);
2805 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2806 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2808 Each of the three load averages is multiplied by 100, then converted
2809 to integer.
2811 When USE-FLOATS is non-nil, floats will be used instead of integers.
2812 These floats are not multiplied by 100.
2814 If the 5-minute or 15-minute load averages are not available, return a
2815 shortened list, containing only those averages which are available.
2817 An error is thrown if the load average can't be obtained. In some
2818 cases making it work would require Emacs being installed setuid or
2819 setgid so that it can read kernel information, and that usually isn't
2820 advisable. */)
2821 (use_floats)
2822 Lisp_Object use_floats;
2824 double load_ave[3];
2825 int loads = getloadavg (load_ave, 3);
2826 Lisp_Object ret = Qnil;
2828 if (loads < 0)
2829 error ("load-average not implemented for this operating system");
2831 while (loads-- > 0)
2833 Lisp_Object load = (NILP (use_floats) ?
2834 make_number ((int) (100.0 * load_ave[loads]))
2835 : make_float (load_ave[loads]));
2836 ret = Fcons (load, ret);
2839 return ret;
2842 Lisp_Object impl_Vfeatures, Qsubfeatures;
2843 extern Lisp_Object impl_Vafter_load_alist;
2845 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2846 doc: /* Returns t if FEATURE is present in this Emacs.
2848 Use this to conditionalize execution of lisp code based on the
2849 presence or absence of Emacs or environment extensions.
2850 Use `provide' to declare that a feature is available. This function
2851 looks at the value of the variable `features'. The optional argument
2852 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2853 (feature, subfeature)
2854 Lisp_Object feature, subfeature;
2856 register Lisp_Object tem;
2857 CHECK_SYMBOL (feature);
2858 tem = Fmemq (feature, Vfeatures);
2859 if (!NILP (tem) && !NILP (subfeature))
2860 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2861 return (NILP (tem)) ? Qnil : Qt;
2864 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2865 doc: /* Announce that FEATURE is a feature of the current Emacs.
2866 The optional argument SUBFEATURES should be a list of symbols listing
2867 particular subfeatures supported in this version of FEATURE. */)
2868 (feature, subfeatures)
2869 Lisp_Object feature, subfeatures;
2871 register Lisp_Object tem;
2872 CHECK_SYMBOL (feature);
2873 CHECK_LIST (subfeatures);
2874 if (!NILP (Vautoload_queue))
2875 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2876 Vautoload_queue);
2877 tem = Fmemq (feature, Vfeatures);
2878 if (NILP (tem))
2879 Vfeatures = Fcons (feature, Vfeatures);
2880 if (!NILP (subfeatures))
2881 Fput (feature, Qsubfeatures, subfeatures);
2882 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2884 /* Run any load-hooks for this file. */
2885 tem = Fassq (feature, Vafter_load_alist);
2886 if (CONSP (tem))
2887 Fprogn (XCDR (tem));
2889 return feature;
2892 /* `require' and its subroutines. */
2894 /* List of features currently being require'd, innermost first. */
2896 Lisp_Object require_nesting_list;
2898 Lisp_Object
2899 require_unwind (old_value)
2900 Lisp_Object old_value;
2902 return require_nesting_list = old_value;
2905 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2906 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2907 If FEATURE is not a member of the list `features', then the feature
2908 is not loaded; so load the file FILENAME.
2909 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2910 and `load' will try to load this name appended with the suffix `.elc' or
2911 `.el', in that order. The name without appended suffix will not be used.
2912 If the optional third argument NOERROR is non-nil,
2913 then return nil if the file is not found instead of signaling an error.
2914 Normally the return value is FEATURE.
2915 The normal messages at start and end of loading FILENAME are suppressed. */)
2916 (feature, filename, noerror)
2917 Lisp_Object feature, filename, noerror;
2919 register Lisp_Object tem;
2920 struct gcpro gcpro1, gcpro2;
2921 int from_file = load_in_progress;
2923 CHECK_SYMBOL (feature);
2925 /* Record the presence of `require' in this file
2926 even if the feature specified is already loaded.
2927 But not more than once in any file,
2928 and not when we aren't loading or reading from a file. */
2929 if (!from_file)
2930 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2931 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2932 from_file = 1;
2934 if (from_file)
2936 tem = Fcons (Qrequire, feature);
2937 if (NILP (Fmember (tem, Vcurrent_load_list)))
2938 LOADHIST_ATTACH (tem);
2940 tem = Fmemq (feature, Vfeatures);
2942 if (NILP (tem))
2944 int count = SPECPDL_INDEX ();
2945 int nesting = 0;
2947 /* This is to make sure that loadup.el gives a clear picture
2948 of what files are preloaded and when. */
2949 if (! NILP (Vpurify_flag))
2950 error ("(require %s) while preparing to dump",
2951 SDATA (SYMBOL_NAME (feature)));
2953 /* A certain amount of recursive `require' is legitimate,
2954 but if we require the same feature recursively 3 times,
2955 signal an error. */
2956 tem = require_nesting_list;
2957 while (! NILP (tem))
2959 if (! NILP (Fequal (feature, XCAR (tem))))
2960 nesting++;
2961 tem = XCDR (tem);
2963 if (nesting > 3)
2964 error ("Recursive `require' for feature `%s'",
2965 SDATA (SYMBOL_NAME (feature)));
2967 /* Update the list for any nested `require's that occur. */
2968 record_unwind_protect (require_unwind, require_nesting_list);
2969 require_nesting_list = Fcons (feature, require_nesting_list);
2971 /* Value saved here is to be restored into Vautoload_queue */
2972 record_unwind_protect (un_autoload, Vautoload_queue);
2973 Vautoload_queue = Qt;
2975 /* Load the file. */
2976 GCPRO2 (feature, filename);
2977 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2978 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2979 UNGCPRO;
2981 /* If load failed entirely, return nil. */
2982 if (NILP (tem))
2983 return unbind_to (count, Qnil);
2985 tem = Fmemq (feature, Vfeatures);
2986 if (NILP (tem))
2987 error ("Required feature `%s' was not provided",
2988 SDATA (SYMBOL_NAME (feature)));
2990 /* Once loading finishes, don't undo it. */
2991 Vautoload_queue = Qt;
2992 feature = unbind_to (count, feature);
2995 return feature;
2998 /* Primitives for work of the "widget" library.
2999 In an ideal world, this section would not have been necessary.
3000 However, lisp function calls being as slow as they are, it turns
3001 out that some functions in the widget library (wid-edit.el) are the
3002 bottleneck of Widget operation. Here is their translation to C,
3003 for the sole reason of efficiency. */
3005 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
3006 doc: /* Return non-nil if PLIST has the property PROP.
3007 PLIST is a property list, which is a list of the form
3008 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
3009 Unlike `plist-get', this allows you to distinguish between a missing
3010 property and a property with the value nil.
3011 The value is actually the tail of PLIST whose car is PROP. */)
3012 (plist, prop)
3013 Lisp_Object plist, prop;
3015 while (CONSP (plist) && !EQ (XCAR (plist), prop))
3017 QUIT;
3018 plist = XCDR (plist);
3019 plist = CDR (plist);
3021 return plist;
3024 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
3025 doc: /* In WIDGET, set PROPERTY to VALUE.
3026 The value can later be retrieved with `widget-get'. */)
3027 (widget, property, value)
3028 Lisp_Object widget, property, value;
3030 CHECK_CONS (widget);
3031 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
3032 return value;
3035 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
3036 doc: /* In WIDGET, get the value of PROPERTY.
3037 The value could either be specified when the widget was created, or
3038 later with `widget-put'. */)
3039 (widget, property)
3040 Lisp_Object widget, property;
3042 Lisp_Object tmp;
3044 while (1)
3046 if (NILP (widget))
3047 return Qnil;
3048 CHECK_CONS (widget);
3049 tmp = Fplist_member (XCDR (widget), property);
3050 if (CONSP (tmp))
3052 tmp = XCDR (tmp);
3053 return CAR (tmp);
3055 tmp = XCAR (widget);
3056 if (NILP (tmp))
3057 return Qnil;
3058 widget = Fget (tmp, Qwidget_type);
3062 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
3063 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3064 ARGS are passed as extra arguments to the function.
3065 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3066 (nargs, args)
3067 int nargs;
3068 Lisp_Object *args;
3070 /* This function can GC. */
3071 Lisp_Object newargs[3];
3072 struct gcpro gcpro1, gcpro2;
3073 Lisp_Object result;
3075 newargs[0] = Fwidget_get (args[0], args[1]);
3076 newargs[1] = args[0];
3077 newargs[2] = Flist (nargs - 2, args + 2);
3078 GCPRO2 (newargs[0], newargs[2]);
3079 result = Fapply (3, newargs);
3080 UNGCPRO;
3081 return result;
3084 #ifdef HAVE_LANGINFO_CODESET
3085 #include <langinfo.h>
3086 #endif
3088 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
3089 doc: /* Access locale data ITEM for the current C locale, if available.
3090 ITEM should be one of the following:
3092 `codeset', returning the character set as a string (locale item CODESET);
3094 `days', returning a 7-element vector of day names (locale items DAY_n);
3096 `months', returning a 12-element vector of month names (locale items MON_n);
3098 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
3099 both measured in milimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
3101 If the system can't provide such information through a call to
3102 `nl_langinfo', or if ITEM isn't from the list above, return nil.
3104 See also Info node `(libc)Locales'.
3106 The data read from the system are decoded using `locale-coding-system'. */)
3107 (item)
3108 Lisp_Object item;
3110 char *str = NULL;
3111 #ifdef HAVE_LANGINFO_CODESET
3112 Lisp_Object val;
3113 if (EQ (item, Qcodeset))
3115 str = nl_langinfo (CODESET);
3116 return build_string (str);
3118 #ifdef DAY_1
3119 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
3121 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
3122 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
3123 int i;
3124 struct gcpro gcpro1;
3125 GCPRO1 (v);
3126 synchronize_system_time_locale ();
3127 for (i = 0; i < 7; i++)
3129 str = nl_langinfo (days[i]);
3130 val = make_unibyte_string (str, strlen (str));
3131 /* Fixme: Is this coding system necessarily right, even if
3132 it is consistent with CODESET? If not, what to do? */
3133 Faset (v, make_number (i),
3134 code_convert_string_norecord (val, Vlocale_coding_system,
3135 0));
3137 UNGCPRO;
3138 return v;
3140 #endif /* DAY_1 */
3141 #ifdef MON_1
3142 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
3144 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
3145 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
3146 MON_8, MON_9, MON_10, MON_11, MON_12};
3147 int i;
3148 struct gcpro gcpro1;
3149 GCPRO1 (v);
3150 synchronize_system_time_locale ();
3151 for (i = 0; i < 12; i++)
3153 str = nl_langinfo (months[i]);
3154 val = make_unibyte_string (str, strlen (str));
3155 Faset (v, make_number (i),
3156 code_convert_string_norecord (val, Vlocale_coding_system, 0));
3158 UNGCPRO;
3159 return v;
3161 #endif /* MON_1 */
3162 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3163 but is in the locale files. This could be used by ps-print. */
3164 #ifdef PAPER_WIDTH
3165 else if (EQ (item, Qpaper))
3167 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
3168 make_number (nl_langinfo (PAPER_HEIGHT)));
3170 #endif /* PAPER_WIDTH */
3171 #endif /* HAVE_LANGINFO_CODESET*/
3172 return Qnil;
3175 /* base64 encode/decode functions (RFC 2045).
3176 Based on code from GNU recode. */
3178 #define MIME_LINE_LENGTH 76
3180 #define IS_ASCII(Character) \
3181 ((Character) < 128)
3182 #define IS_BASE64(Character) \
3183 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3184 #define IS_BASE64_IGNORABLE(Character) \
3185 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3186 || (Character) == '\f' || (Character) == '\r')
3188 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3189 character or return retval if there are no characters left to
3190 process. */
3191 #define READ_QUADRUPLET_BYTE(retval) \
3192 do \
3194 if (i == length) \
3196 if (nchars_return) \
3197 *nchars_return = nchars; \
3198 return (retval); \
3200 c = from[i++]; \
3202 while (IS_BASE64_IGNORABLE (c))
3204 /* Table of characters coding the 64 values. */
3205 static const char base64_value_to_char[64] =
3207 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3208 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3209 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3210 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3211 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3212 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3213 '8', '9', '+', '/' /* 60-63 */
3216 /* Table of base64 values for first 128 characters. */
3217 static const short base64_char_to_value[128] =
3219 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3220 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3221 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3222 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3223 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3224 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3225 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3226 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3227 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3228 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3229 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3230 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3231 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3234 /* The following diagram shows the logical steps by which three octets
3235 get transformed into four base64 characters.
3237 .--------. .--------. .--------.
3238 |aaaaaabb| |bbbbcccc| |ccdddddd|
3239 `--------' `--------' `--------'
3240 6 2 4 4 2 6
3241 .--------+--------+--------+--------.
3242 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3243 `--------+--------+--------+--------'
3245 .--------+--------+--------+--------.
3246 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3247 `--------+--------+--------+--------'
3249 The octets are divided into 6 bit chunks, which are then encoded into
3250 base64 characters. */
3253 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3254 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3256 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3257 2, 3, "r",
3258 doc: /* Base64-encode the region between BEG and END.
3259 Return the length of the encoded text.
3260 Optional third argument NO-LINE-BREAK means do not break long lines
3261 into shorter lines. */)
3262 (beg, end, no_line_break)
3263 Lisp_Object beg, end, no_line_break;
3265 char *encoded;
3266 int allength, length;
3267 int ibeg, iend, encoded_length;
3268 int old_pos = PT;
3269 USE_SAFE_ALLOCA;
3271 validate_region (&beg, &end);
3273 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3274 iend = CHAR_TO_BYTE (XFASTINT (end));
3275 move_gap_both (XFASTINT (beg), ibeg);
3277 /* We need to allocate enough room for encoding the text.
3278 We need 33 1/3% more space, plus a newline every 76
3279 characters, and then we round up. */
3280 length = iend - ibeg;
3281 allength = length + length/3 + 1;
3282 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3284 SAFE_ALLOCA (encoded, char *, allength);
3285 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3286 NILP (no_line_break),
3287 !NILP (BUF_ENABLE_MULTIBYTE_CHARACTERS (current_buffer)));
3288 if (encoded_length > allength)
3289 abort ();
3291 if (encoded_length < 0)
3293 /* The encoding wasn't possible. */
3294 SAFE_FREE ();
3295 error ("Multibyte character in data for base64 encoding");
3298 /* Now we have encoded the region, so we insert the new contents
3299 and delete the old. (Insert first in order to preserve markers.) */
3300 SET_PT_BOTH (XFASTINT (beg), ibeg);
3301 insert (encoded, encoded_length);
3302 SAFE_FREE ();
3303 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3305 /* If point was outside of the region, restore it exactly; else just
3306 move to the beginning of the region. */
3307 if (old_pos >= XFASTINT (end))
3308 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3309 else if (old_pos > XFASTINT (beg))
3310 old_pos = XFASTINT (beg);
3311 SET_PT (old_pos);
3313 /* We return the length of the encoded text. */
3314 return make_number (encoded_length);
3317 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3318 1, 2, 0,
3319 doc: /* Base64-encode STRING and return the result.
3320 Optional second argument NO-LINE-BREAK means do not break long lines
3321 into shorter lines. */)
3322 (string, no_line_break)
3323 Lisp_Object string, no_line_break;
3325 int allength, length, encoded_length;
3326 char *encoded;
3327 Lisp_Object encoded_string;
3328 USE_SAFE_ALLOCA;
3330 CHECK_STRING (string);
3332 /* We need to allocate enough room for encoding the text.
3333 We need 33 1/3% more space, plus a newline every 76
3334 characters, and then we round up. */
3335 length = SBYTES (string);
3336 allength = length + length/3 + 1;
3337 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3339 /* We need to allocate enough room for decoding the text. */
3340 SAFE_ALLOCA (encoded, char *, allength);
3342 encoded_length = base64_encode_1 (SDATA (string),
3343 encoded, length, NILP (no_line_break),
3344 STRING_MULTIBYTE (string));
3345 if (encoded_length > allength)
3346 abort ();
3348 if (encoded_length < 0)
3350 /* The encoding wasn't possible. */
3351 SAFE_FREE ();
3352 error ("Multibyte character in data for base64 encoding");
3355 encoded_string = make_unibyte_string (encoded, encoded_length);
3356 SAFE_FREE ();
3358 return encoded_string;
3361 static int
3362 base64_encode_1 (from, to, length, line_break, multibyte)
3363 const char *from;
3364 char *to;
3365 int length;
3366 int line_break;
3367 int multibyte;
3369 int counter = 0, i = 0;
3370 char *e = to;
3371 int c;
3372 unsigned int value;
3373 int bytes;
3375 while (i < length)
3377 if (multibyte)
3379 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3380 if (CHAR_BYTE8_P (c))
3381 c = CHAR_TO_BYTE8 (c);
3382 else if (c >= 256)
3383 return -1;
3384 i += bytes;
3386 else
3387 c = from[i++];
3389 /* Wrap line every 76 characters. */
3391 if (line_break)
3393 if (counter < MIME_LINE_LENGTH / 4)
3394 counter++;
3395 else
3397 *e++ = '\n';
3398 counter = 1;
3402 /* Process first byte of a triplet. */
3404 *e++ = base64_value_to_char[0x3f & c >> 2];
3405 value = (0x03 & c) << 4;
3407 /* Process second byte of a triplet. */
3409 if (i == length)
3411 *e++ = base64_value_to_char[value];
3412 *e++ = '=';
3413 *e++ = '=';
3414 break;
3417 if (multibyte)
3419 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3420 if (CHAR_BYTE8_P (c))
3421 c = CHAR_TO_BYTE8 (c);
3422 else if (c >= 256)
3423 return -1;
3424 i += bytes;
3426 else
3427 c = from[i++];
3429 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3430 value = (0x0f & c) << 2;
3432 /* Process third byte of a triplet. */
3434 if (i == length)
3436 *e++ = base64_value_to_char[value];
3437 *e++ = '=';
3438 break;
3441 if (multibyte)
3443 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3444 if (CHAR_BYTE8_P (c))
3445 c = CHAR_TO_BYTE8 (c);
3446 else if (c >= 256)
3447 return -1;
3448 i += bytes;
3450 else
3451 c = from[i++];
3453 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3454 *e++ = base64_value_to_char[0x3f & c];
3457 return e - to;
3461 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3462 2, 2, "r",
3463 doc: /* Base64-decode the region between BEG and END.
3464 Return the length of the decoded text.
3465 If the region can't be decoded, signal an error and don't modify the buffer. */)
3466 (beg, end)
3467 Lisp_Object beg, end;
3469 int ibeg, iend, length, allength;
3470 char *decoded;
3471 int old_pos = PT;
3472 int decoded_length;
3473 int inserted_chars;
3474 int multibyte = !NILP (BUF_ENABLE_MULTIBYTE_CHARACTERS (current_buffer));
3475 USE_SAFE_ALLOCA;
3477 validate_region (&beg, &end);
3479 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3480 iend = CHAR_TO_BYTE (XFASTINT (end));
3482 length = iend - ibeg;
3484 /* We need to allocate enough room for decoding the text. If we are
3485 working on a multibyte buffer, each decoded code may occupy at
3486 most two bytes. */
3487 allength = multibyte ? length * 2 : length;
3488 SAFE_ALLOCA (decoded, char *, allength);
3490 move_gap_both (XFASTINT (beg), ibeg);
3491 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3492 multibyte, &inserted_chars);
3493 if (decoded_length > allength)
3494 abort ();
3496 if (decoded_length < 0)
3498 /* The decoding wasn't possible. */
3499 SAFE_FREE ();
3500 error ("Invalid base64 data");
3503 /* Now we have decoded the region, so we insert the new contents
3504 and delete the old. (Insert first in order to preserve markers.) */
3505 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3506 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3507 SAFE_FREE ();
3509 /* Delete the original text. */
3510 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3511 iend + decoded_length, 1);
3513 /* If point was outside of the region, restore it exactly; else just
3514 move to the beginning of the region. */
3515 if (old_pos >= XFASTINT (end))
3516 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3517 else if (old_pos > XFASTINT (beg))
3518 old_pos = XFASTINT (beg);
3519 SET_PT (old_pos > ZV ? ZV : old_pos);
3521 return make_number (inserted_chars);
3524 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3525 1, 1, 0,
3526 doc: /* Base64-decode STRING and return the result. */)
3527 (string)
3528 Lisp_Object string;
3530 char *decoded;
3531 int length, decoded_length;
3532 Lisp_Object decoded_string;
3533 USE_SAFE_ALLOCA;
3535 CHECK_STRING (string);
3537 length = SBYTES (string);
3538 /* We need to allocate enough room for decoding the text. */
3539 SAFE_ALLOCA (decoded, char *, length);
3541 /* The decoded result should be unibyte. */
3542 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3543 0, NULL);
3544 if (decoded_length > length)
3545 abort ();
3546 else if (decoded_length >= 0)
3547 decoded_string = make_unibyte_string (decoded, decoded_length);
3548 else
3549 decoded_string = Qnil;
3551 SAFE_FREE ();
3552 if (!STRINGP (decoded_string))
3553 error ("Invalid base64 data");
3555 return decoded_string;
3558 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3559 MULTIBYTE is nonzero, the decoded result should be in multibyte
3560 form. If NCHARS_RETRUN is not NULL, store the number of produced
3561 characters in *NCHARS_RETURN. */
3563 static int
3564 base64_decode_1 (from, to, length, multibyte, nchars_return)
3565 const char *from;
3566 char *to;
3567 int length;
3568 int multibyte;
3569 int *nchars_return;
3571 int i = 0;
3572 char *e = to;
3573 unsigned char c;
3574 unsigned long value;
3575 int nchars = 0;
3577 while (1)
3579 /* Process first byte of a quadruplet. */
3581 READ_QUADRUPLET_BYTE (e-to);
3583 if (!IS_BASE64 (c))
3584 return -1;
3585 value = base64_char_to_value[c] << 18;
3587 /* Process second byte of a quadruplet. */
3589 READ_QUADRUPLET_BYTE (-1);
3591 if (!IS_BASE64 (c))
3592 return -1;
3593 value |= base64_char_to_value[c] << 12;
3595 c = (unsigned char) (value >> 16);
3596 if (multibyte && c >= 128)
3597 e += BYTE8_STRING (c, e);
3598 else
3599 *e++ = c;
3600 nchars++;
3602 /* Process third byte of a quadruplet. */
3604 READ_QUADRUPLET_BYTE (-1);
3606 if (c == '=')
3608 READ_QUADRUPLET_BYTE (-1);
3610 if (c != '=')
3611 return -1;
3612 continue;
3615 if (!IS_BASE64 (c))
3616 return -1;
3617 value |= base64_char_to_value[c] << 6;
3619 c = (unsigned char) (0xff & value >> 8);
3620 if (multibyte && c >= 128)
3621 e += BYTE8_STRING (c, e);
3622 else
3623 *e++ = c;
3624 nchars++;
3626 /* Process fourth byte of a quadruplet. */
3628 READ_QUADRUPLET_BYTE (-1);
3630 if (c == '=')
3631 continue;
3633 if (!IS_BASE64 (c))
3634 return -1;
3635 value |= base64_char_to_value[c];
3637 c = (unsigned char) (0xff & value);
3638 if (multibyte && c >= 128)
3639 e += BYTE8_STRING (c, e);
3640 else
3641 *e++ = c;
3642 nchars++;
3648 /***********************************************************************
3649 ***** *****
3650 ***** Hash Tables *****
3651 ***** *****
3652 ***********************************************************************/
3654 /* Implemented by gerd@gnu.org. This hash table implementation was
3655 inspired by CMUCL hash tables. */
3657 /* Ideas:
3659 1. For small tables, association lists are probably faster than
3660 hash tables because they have lower overhead.
3662 For uses of hash tables where the O(1) behavior of table
3663 operations is not a requirement, it might therefore be a good idea
3664 not to hash. Instead, we could just do a linear search in the
3665 key_and_value vector of the hash table. This could be done
3666 if a `:linear-search t' argument is given to make-hash-table. */
3669 /* The list of all weak hash tables. Don't staticpro this one. */
3671 struct Lisp_Hash_Table *weak_hash_tables;
3673 /* Various symbols. */
3675 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3676 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3677 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3679 /* Function prototypes. */
3681 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3682 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3683 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3684 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3685 Lisp_Object, unsigned));
3686 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3687 Lisp_Object, unsigned));
3688 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3689 unsigned, Lisp_Object, unsigned));
3690 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3691 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3692 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3693 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3694 Lisp_Object));
3695 static unsigned sxhash_string P_ ((unsigned char *, int));
3696 static unsigned sxhash_list P_ ((Lisp_Object, int));
3697 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3698 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3699 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3703 /***********************************************************************
3704 Utilities
3705 ***********************************************************************/
3707 /* If OBJ is a Lisp hash table, return a pointer to its struct
3708 Lisp_Hash_Table. Otherwise, signal an error. */
3710 static struct Lisp_Hash_Table *
3711 check_hash_table (obj)
3712 Lisp_Object obj;
3714 CHECK_HASH_TABLE (obj);
3715 return XHASH_TABLE (obj);
3719 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3720 number. */
3723 next_almost_prime (n)
3724 int n;
3726 if (n % 2 == 0)
3727 n += 1;
3728 if (n % 3 == 0)
3729 n += 2;
3730 if (n % 7 == 0)
3731 n += 4;
3732 return n;
3736 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3737 which USED[I] is non-zero. If found at index I in ARGS, set
3738 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3739 -1. This function is used to extract a keyword/argument pair from
3740 a DEFUN parameter list. */
3742 static int
3743 get_key_arg (key, nargs, args, used)
3744 Lisp_Object key;
3745 int nargs;
3746 Lisp_Object *args;
3747 char *used;
3749 int i;
3751 for (i = 0; i < nargs - 1; ++i)
3752 if (!used[i] && EQ (args[i], key))
3753 break;
3755 if (i >= nargs - 1)
3756 i = -1;
3757 else
3759 used[i++] = 1;
3760 used[i] = 1;
3763 return i;
3767 /* Return a Lisp vector which has the same contents as VEC but has
3768 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3769 vector that are not copied from VEC are set to INIT. */
3771 Lisp_Object
3772 larger_vector (vec, new_size, init)
3773 Lisp_Object vec;
3774 int new_size;
3775 Lisp_Object init;
3777 struct Lisp_Vector *v;
3778 int i, old_size;
3780 xassert (VECTORP (vec));
3781 old_size = ASIZE (vec);
3782 xassert (new_size >= old_size);
3784 v = allocate_vector (new_size);
3785 bcopy (XVECTOR (vec)->contents, v->contents,
3786 old_size * sizeof *v->contents);
3787 for (i = old_size; i < new_size; ++i)
3788 v->contents[i] = init;
3789 XSETVECTOR (vec, v);
3790 return vec;
3794 /***********************************************************************
3795 Low-level Functions
3796 ***********************************************************************/
3798 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3799 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3800 KEY2 are the same. */
3802 static int
3803 cmpfn_eql (h, key1, hash1, key2, hash2)
3804 struct Lisp_Hash_Table *h;
3805 Lisp_Object key1, key2;
3806 unsigned hash1, hash2;
3808 return (FLOATP (key1)
3809 && FLOATP (key2)
3810 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3814 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3815 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3816 KEY2 are the same. */
3818 static int
3819 cmpfn_equal (h, key1, hash1, key2, hash2)
3820 struct Lisp_Hash_Table *h;
3821 Lisp_Object key1, key2;
3822 unsigned hash1, hash2;
3824 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3828 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3829 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3830 if KEY1 and KEY2 are the same. */
3832 static int
3833 cmpfn_user_defined (h, key1, hash1, key2, hash2)
3834 struct Lisp_Hash_Table *h;
3835 Lisp_Object key1, key2;
3836 unsigned hash1, hash2;
3838 if (hash1 == hash2)
3840 Lisp_Object args[3];
3842 args[0] = h->user_cmp_function;
3843 args[1] = key1;
3844 args[2] = key2;
3845 return !NILP (Ffuncall (3, args));
3847 else
3848 return 0;
3852 /* Value is a hash code for KEY for use in hash table H which uses
3853 `eq' to compare keys. The hash code returned is guaranteed to fit
3854 in a Lisp integer. */
3856 static unsigned
3857 hashfn_eq (h, key)
3858 struct Lisp_Hash_Table *h;
3859 Lisp_Object key;
3861 unsigned hash = XUINT (key) ^ XTYPE (key);
3862 xassert ((hash & ~INTMASK) == 0);
3863 return hash;
3867 /* Value is a hash code for KEY for use in hash table H which uses
3868 `eql' to compare keys. The hash code returned is guaranteed to fit
3869 in a Lisp integer. */
3871 static unsigned
3872 hashfn_eql (h, key)
3873 struct Lisp_Hash_Table *h;
3874 Lisp_Object key;
3876 unsigned hash;
3877 if (FLOATP (key))
3878 hash = sxhash (key, 0);
3879 else
3880 hash = XUINT (key) ^ XTYPE (key);
3881 xassert ((hash & ~INTMASK) == 0);
3882 return hash;
3886 /* Value is a hash code for KEY for use in hash table H which uses
3887 `equal' to compare keys. The hash code returned is guaranteed to fit
3888 in a Lisp integer. */
3890 static unsigned
3891 hashfn_equal (h, key)
3892 struct Lisp_Hash_Table *h;
3893 Lisp_Object key;
3895 unsigned hash = sxhash (key, 0);
3896 xassert ((hash & ~INTMASK) == 0);
3897 return hash;
3901 /* Value is a hash code for KEY for use in hash table H which uses as
3902 user-defined function to compare keys. The hash code returned is
3903 guaranteed to fit in a Lisp integer. */
3905 static unsigned
3906 hashfn_user_defined (h, key)
3907 struct Lisp_Hash_Table *h;
3908 Lisp_Object key;
3910 Lisp_Object args[2], hash;
3912 args[0] = h->user_hash_function;
3913 args[1] = key;
3914 hash = Ffuncall (2, args);
3915 if (!INTEGERP (hash))
3916 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
3917 return XUINT (hash);
3921 /* Create and initialize a new hash table.
3923 TEST specifies the test the hash table will use to compare keys.
3924 It must be either one of the predefined tests `eq', `eql' or
3925 `equal' or a symbol denoting a user-defined test named TEST with
3926 test and hash functions USER_TEST and USER_HASH.
3928 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3930 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3931 new size when it becomes full is computed by adding REHASH_SIZE to
3932 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3933 table's new size is computed by multiplying its old size with
3934 REHASH_SIZE.
3936 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3937 be resized when the ratio of (number of entries in the table) /
3938 (table size) is >= REHASH_THRESHOLD.
3940 WEAK specifies the weakness of the table. If non-nil, it must be
3941 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3943 Lisp_Object
3944 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
3945 user_test, user_hash)
3946 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
3947 Lisp_Object user_test, user_hash;
3949 struct Lisp_Hash_Table *h;
3950 Lisp_Object table;
3951 int index_size, i, sz;
3953 /* Preconditions. */
3954 xassert (SYMBOLP (test));
3955 xassert (INTEGERP (size) && XINT (size) >= 0);
3956 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3957 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3958 xassert (FLOATP (rehash_threshold)
3959 && XFLOATINT (rehash_threshold) > 0
3960 && XFLOATINT (rehash_threshold) <= 1.0);
3962 if (XFASTINT (size) == 0)
3963 size = make_number (1);
3965 /* Allocate a table and initialize it. */
3966 h = allocate_hash_table ();
3968 /* Initialize hash table slots. */
3969 sz = XFASTINT (size);
3971 h->test = test;
3972 if (EQ (test, Qeql))
3974 h->cmpfn = cmpfn_eql;
3975 h->hashfn = hashfn_eql;
3977 else if (EQ (test, Qeq))
3979 h->cmpfn = NULL;
3980 h->hashfn = hashfn_eq;
3982 else if (EQ (test, Qequal))
3984 h->cmpfn = cmpfn_equal;
3985 h->hashfn = hashfn_equal;
3987 else
3989 h->user_cmp_function = user_test;
3990 h->user_hash_function = user_hash;
3991 h->cmpfn = cmpfn_user_defined;
3992 h->hashfn = hashfn_user_defined;
3995 h->weak = weak;
3996 h->rehash_threshold = rehash_threshold;
3997 h->rehash_size = rehash_size;
3998 h->count = 0;
3999 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4000 h->hash = Fmake_vector (size, Qnil);
4001 h->next = Fmake_vector (size, Qnil);
4002 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4003 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4004 h->index = Fmake_vector (make_number (index_size), Qnil);
4006 /* Set up the free list. */
4007 for (i = 0; i < sz - 1; ++i)
4008 HASH_NEXT (h, i) = make_number (i + 1);
4009 h->next_free = make_number (0);
4011 XSET_HASH_TABLE (table, h);
4012 xassert (HASH_TABLE_P (table));
4013 xassert (XHASH_TABLE (table) == h);
4015 /* Maybe add this hash table to the list of all weak hash tables. */
4016 if (NILP (h->weak))
4017 h->next_weak = NULL;
4018 else
4020 h->next_weak = weak_hash_tables;
4021 weak_hash_tables = h;
4024 return table;
4028 /* Return a copy of hash table H1. Keys and values are not copied,
4029 only the table itself is. */
4031 Lisp_Object
4032 copy_hash_table (h1)
4033 struct Lisp_Hash_Table *h1;
4035 Lisp_Object table;
4036 struct Lisp_Hash_Table *h2;
4037 struct Lisp_Vector *next;
4039 h2 = allocate_hash_table ();
4040 next = h2->vec_next;
4041 bcopy (h1, h2, sizeof *h2);
4042 h2->vec_next = next;
4043 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4044 h2->hash = Fcopy_sequence (h1->hash);
4045 h2->next = Fcopy_sequence (h1->next);
4046 h2->index = Fcopy_sequence (h1->index);
4047 XSET_HASH_TABLE (table, h2);
4049 /* Maybe add this hash table to the list of all weak hash tables. */
4050 if (!NILP (h2->weak))
4052 h2->next_weak = weak_hash_tables;
4053 weak_hash_tables = h2;
4056 return table;
4060 /* Resize hash table H if it's too full. If H cannot be resized
4061 because it's already too large, throw an error. */
4063 static INLINE void
4064 maybe_resize_hash_table (h)
4065 struct Lisp_Hash_Table *h;
4067 if (NILP (h->next_free))
4069 int old_size = HASH_TABLE_SIZE (h);
4070 int i, new_size, index_size;
4071 EMACS_INT nsize;
4073 if (INTEGERP (h->rehash_size))
4074 new_size = old_size + XFASTINT (h->rehash_size);
4075 else
4076 new_size = old_size * XFLOATINT (h->rehash_size);
4077 new_size = max (old_size + 1, new_size);
4078 index_size = next_almost_prime ((int)
4079 (new_size
4080 / XFLOATINT (h->rehash_threshold)));
4081 /* Assignment to EMACS_INT stops GCC whining about limited range
4082 of data type. */
4083 nsize = max (index_size, 2 * new_size);
4084 if (nsize > MOST_POSITIVE_FIXNUM)
4085 error ("Hash table too large to resize");
4087 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4088 h->next = larger_vector (h->next, new_size, Qnil);
4089 h->hash = larger_vector (h->hash, new_size, Qnil);
4090 h->index = Fmake_vector (make_number (index_size), Qnil);
4092 /* Update the free list. Do it so that new entries are added at
4093 the end of the free list. This makes some operations like
4094 maphash faster. */
4095 for (i = old_size; i < new_size - 1; ++i)
4096 HASH_NEXT (h, i) = make_number (i + 1);
4098 if (!NILP (h->next_free))
4100 Lisp_Object last, next;
4102 last = h->next_free;
4103 while (next = HASH_NEXT (h, XFASTINT (last)),
4104 !NILP (next))
4105 last = next;
4107 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4109 else
4110 XSETFASTINT (h->next_free, old_size);
4112 /* Rehash. */
4113 for (i = 0; i < old_size; ++i)
4114 if (!NILP (HASH_HASH (h, i)))
4116 unsigned hash_code = XUINT (HASH_HASH (h, i));
4117 int start_of_bucket = hash_code % ASIZE (h->index);
4118 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4119 HASH_INDEX (h, start_of_bucket) = make_number (i);
4125 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4126 the hash code of KEY. Value is the index of the entry in H
4127 matching KEY, or -1 if not found. */
4130 hash_lookup (h, key, hash)
4131 struct Lisp_Hash_Table *h;
4132 Lisp_Object key;
4133 unsigned *hash;
4135 unsigned hash_code;
4136 int start_of_bucket;
4137 Lisp_Object idx;
4139 hash_code = h->hashfn (h, key);
4140 if (hash)
4141 *hash = hash_code;
4143 start_of_bucket = hash_code % ASIZE (h->index);
4144 idx = HASH_INDEX (h, start_of_bucket);
4146 /* We need not gcpro idx since it's either an integer or nil. */
4147 while (!NILP (idx))
4149 int i = XFASTINT (idx);
4150 if (EQ (key, HASH_KEY (h, i))
4151 || (h->cmpfn
4152 && h->cmpfn (h, key, hash_code,
4153 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4154 break;
4155 idx = HASH_NEXT (h, i);
4158 return NILP (idx) ? -1 : XFASTINT (idx);
4162 /* Put an entry into hash table H that associates KEY with VALUE.
4163 HASH is a previously computed hash code of KEY.
4164 Value is the index of the entry in H matching KEY. */
4167 hash_put (h, key, value, hash)
4168 struct Lisp_Hash_Table *h;
4169 Lisp_Object key, value;
4170 unsigned hash;
4172 int start_of_bucket, i;
4174 xassert ((hash & ~INTMASK) == 0);
4176 /* Increment count after resizing because resizing may fail. */
4177 maybe_resize_hash_table (h);
4178 h->count++;
4180 /* Store key/value in the key_and_value vector. */
4181 i = XFASTINT (h->next_free);
4182 h->next_free = HASH_NEXT (h, i);
4183 HASH_KEY (h, i) = key;
4184 HASH_VALUE (h, i) = value;
4186 /* Remember its hash code. */
4187 HASH_HASH (h, i) = make_number (hash);
4189 /* Add new entry to its collision chain. */
4190 start_of_bucket = hash % ASIZE (h->index);
4191 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4192 HASH_INDEX (h, start_of_bucket) = make_number (i);
4193 return i;
4197 /* Remove the entry matching KEY from hash table H, if there is one. */
4199 static void
4200 hash_remove_from_table (h, key)
4201 struct Lisp_Hash_Table *h;
4202 Lisp_Object key;
4204 unsigned hash_code;
4205 int start_of_bucket;
4206 Lisp_Object idx, prev;
4208 hash_code = h->hashfn (h, key);
4209 start_of_bucket = hash_code % ASIZE (h->index);
4210 idx = HASH_INDEX (h, start_of_bucket);
4211 prev = Qnil;
4213 /* We need not gcpro idx, prev since they're either integers or nil. */
4214 while (!NILP (idx))
4216 int i = XFASTINT (idx);
4218 if (EQ (key, HASH_KEY (h, i))
4219 || (h->cmpfn
4220 && h->cmpfn (h, key, hash_code,
4221 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4223 /* Take entry out of collision chain. */
4224 if (NILP (prev))
4225 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4226 else
4227 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4229 /* Clear slots in key_and_value and add the slots to
4230 the free list. */
4231 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4232 HASH_NEXT (h, i) = h->next_free;
4233 h->next_free = make_number (i);
4234 h->count--;
4235 xassert (h->count >= 0);
4236 break;
4238 else
4240 prev = idx;
4241 idx = HASH_NEXT (h, i);
4247 /* Clear hash table H. */
4249 void
4250 hash_clear (h)
4251 struct Lisp_Hash_Table *h;
4253 if (h->count > 0)
4255 int i, size = HASH_TABLE_SIZE (h);
4257 for (i = 0; i < size; ++i)
4259 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4260 HASH_KEY (h, i) = Qnil;
4261 HASH_VALUE (h, i) = Qnil;
4262 HASH_HASH (h, i) = Qnil;
4265 for (i = 0; i < ASIZE (h->index); ++i)
4266 ASET (h->index, i, Qnil);
4268 h->next_free = make_number (0);
4269 h->count = 0;
4275 /************************************************************************
4276 Weak Hash Tables
4277 ************************************************************************/
4279 void
4280 init_weak_hash_tables ()
4282 weak_hash_tables = NULL;
4285 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4286 entries from the table that don't survive the current GC.
4287 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4288 non-zero if anything was marked. */
4290 static int
4291 sweep_weak_table (h, remove_entries_p)
4292 struct Lisp_Hash_Table *h;
4293 int remove_entries_p;
4295 int bucket, n, marked;
4297 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
4298 marked = 0;
4300 for (bucket = 0; bucket < n; ++bucket)
4302 Lisp_Object idx, next, prev;
4304 /* Follow collision chain, removing entries that
4305 don't survive this garbage collection. */
4306 prev = Qnil;
4307 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
4309 int i = XFASTINT (idx);
4310 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4311 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4312 int remove_p;
4314 if (EQ (h->weak, Qkey))
4315 remove_p = !key_known_to_survive_p;
4316 else if (EQ (h->weak, Qvalue))
4317 remove_p = !value_known_to_survive_p;
4318 else if (EQ (h->weak, Qkey_or_value))
4319 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4320 else if (EQ (h->weak, Qkey_and_value))
4321 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4322 else
4323 abort ();
4325 next = HASH_NEXT (h, i);
4327 if (remove_entries_p)
4329 if (remove_p)
4331 /* Take out of collision chain. */
4332 if (NILP (prev))
4333 HASH_INDEX (h, bucket) = next;
4334 else
4335 HASH_NEXT (h, XFASTINT (prev)) = next;
4337 /* Add to free list. */
4338 HASH_NEXT (h, i) = h->next_free;
4339 h->next_free = idx;
4341 /* Clear key, value, and hash. */
4342 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4343 HASH_HASH (h, i) = Qnil;
4345 h->count--;
4347 else
4349 prev = idx;
4352 else
4354 if (!remove_p)
4356 /* Make sure key and value survive. */
4357 if (!key_known_to_survive_p)
4359 mark_object (HASH_KEY (h, i));
4360 marked = 1;
4363 if (!value_known_to_survive_p)
4365 mark_object (HASH_VALUE (h, i));
4366 marked = 1;
4373 return marked;
4376 /* Remove elements from weak hash tables that don't survive the
4377 current garbage collection. Remove weak tables that don't survive
4378 from Vweak_hash_tables. Called from gc_sweep. */
4380 void
4381 sweep_weak_hash_tables ()
4383 struct Lisp_Hash_Table *h, *used, *next;
4384 int marked;
4386 /* Mark all keys and values that are in use. Keep on marking until
4387 there is no more change. This is necessary for cases like
4388 value-weak table A containing an entry X -> Y, where Y is used in a
4389 key-weak table B, Z -> Y. If B comes after A in the list of weak
4390 tables, X -> Y might be removed from A, although when looking at B
4391 one finds that it shouldn't. */
4394 marked = 0;
4395 for (h = weak_hash_tables; h; h = h->next_weak)
4397 if (h->size & ARRAY_MARK_FLAG)
4398 marked |= sweep_weak_table (h, 0);
4401 while (marked);
4403 /* Remove tables and entries that aren't used. */
4404 for (h = weak_hash_tables, used = NULL; h; h = next)
4406 next = h->next_weak;
4408 if (h->size & ARRAY_MARK_FLAG)
4410 /* TABLE is marked as used. Sweep its contents. */
4411 if (h->count > 0)
4412 sweep_weak_table (h, 1);
4414 /* Add table to the list of used weak hash tables. */
4415 h->next_weak = used;
4416 used = h;
4420 weak_hash_tables = used;
4425 /***********************************************************************
4426 Hash Code Computation
4427 ***********************************************************************/
4429 /* Maximum depth up to which to dive into Lisp structures. */
4431 #define SXHASH_MAX_DEPTH 3
4433 /* Maximum length up to which to take list and vector elements into
4434 account. */
4436 #define SXHASH_MAX_LEN 7
4438 /* Combine two integers X and Y for hashing. */
4440 #define SXHASH_COMBINE(X, Y) \
4441 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4442 + (unsigned)(Y))
4445 /* Return a hash for string PTR which has length LEN. The hash
4446 code returned is guaranteed to fit in a Lisp integer. */
4448 static unsigned
4449 sxhash_string (ptr, len)
4450 unsigned char *ptr;
4451 int len;
4453 unsigned char *p = ptr;
4454 unsigned char *end = p + len;
4455 unsigned char c;
4456 unsigned hash = 0;
4458 while (p != end)
4460 c = *p++;
4461 if (c >= 0140)
4462 c -= 40;
4463 hash = ((hash << 4) + (hash >> 28) + c);
4466 return hash & INTMASK;
4470 /* Return a hash for list LIST. DEPTH is the current depth in the
4471 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4473 static unsigned
4474 sxhash_list (list, depth)
4475 Lisp_Object list;
4476 int depth;
4478 unsigned hash = 0;
4479 int i;
4481 if (depth < SXHASH_MAX_DEPTH)
4482 for (i = 0;
4483 CONSP (list) && i < SXHASH_MAX_LEN;
4484 list = XCDR (list), ++i)
4486 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4487 hash = SXHASH_COMBINE (hash, hash2);
4490 if (!NILP (list))
4492 unsigned hash2 = sxhash (list, depth + 1);
4493 hash = SXHASH_COMBINE (hash, hash2);
4496 return hash;
4500 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4501 the Lisp structure. */
4503 static unsigned
4504 sxhash_vector (vec, depth)
4505 Lisp_Object vec;
4506 int depth;
4508 unsigned hash = ASIZE (vec);
4509 int i, n;
4511 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4512 for (i = 0; i < n; ++i)
4514 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
4515 hash = SXHASH_COMBINE (hash, hash2);
4518 return hash;
4522 /* Return a hash for bool-vector VECTOR. */
4524 static unsigned
4525 sxhash_bool_vector (vec)
4526 Lisp_Object vec;
4528 unsigned hash = XBOOL_VECTOR (vec)->size;
4529 int i, n;
4531 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4532 for (i = 0; i < n; ++i)
4533 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4535 return hash;
4539 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4540 structure. Value is an unsigned integer clipped to INTMASK. */
4542 unsigned
4543 sxhash (obj, depth)
4544 Lisp_Object obj;
4545 int depth;
4547 unsigned hash;
4549 if (depth > SXHASH_MAX_DEPTH)
4550 return 0;
4552 switch (XTYPE (obj))
4554 case_Lisp_Int:
4555 hash = XUINT (obj);
4556 break;
4558 case Lisp_Misc:
4559 hash = XUINT (obj);
4560 break;
4562 case Lisp_Symbol:
4563 obj = SYMBOL_NAME (obj);
4564 /* Fall through. */
4566 case Lisp_String:
4567 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4568 break;
4570 /* This can be everything from a vector to an overlay. */
4571 case Lisp_Vectorlike:
4572 if (VECTORP (obj))
4573 /* According to the CL HyperSpec, two arrays are equal only if
4574 they are `eq', except for strings and bit-vectors. In
4575 Emacs, this works differently. We have to compare element
4576 by element. */
4577 hash = sxhash_vector (obj, depth);
4578 else if (BOOL_VECTOR_P (obj))
4579 hash = sxhash_bool_vector (obj);
4580 else
4581 /* Others are `equal' if they are `eq', so let's take their
4582 address as hash. */
4583 hash = XUINT (obj);
4584 break;
4586 case Lisp_Cons:
4587 hash = sxhash_list (obj, depth);
4588 break;
4590 case Lisp_Float:
4592 double val = XFLOAT_DATA (obj);
4593 unsigned char *p = (unsigned char *) &val;
4594 unsigned char *e = p + sizeof val;
4595 for (hash = 0; p < e; ++p)
4596 hash = SXHASH_COMBINE (hash, *p);
4597 break;
4600 default:
4601 abort ();
4604 return hash & INTMASK;
4609 /***********************************************************************
4610 Lisp Interface
4611 ***********************************************************************/
4614 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4615 doc: /* Compute a hash code for OBJ and return it as integer. */)
4616 (obj)
4617 Lisp_Object obj;
4619 unsigned hash = sxhash (obj, 0);
4620 return make_number (hash);
4624 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4625 doc: /* Create and return a new hash table.
4627 Arguments are specified as keyword/argument pairs. The following
4628 arguments are defined:
4630 :test TEST -- TEST must be a symbol that specifies how to compare
4631 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4632 `equal'. User-supplied test and hash functions can be specified via
4633 `define-hash-table-test'.
4635 :size SIZE -- A hint as to how many elements will be put in the table.
4636 Default is 65.
4638 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4639 fills up. If REHASH-SIZE is an integer, add that many space. If it
4640 is a float, it must be > 1.0, and the new size is computed by
4641 multiplying the old size with that factor. Default is 1.5.
4643 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4644 Resize the hash table when ratio of the number of entries in the
4645 table. Default is 0.8.
4647 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4648 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4649 returned is a weak table. Key/value pairs are removed from a weak
4650 hash table when there are no non-weak references pointing to their
4651 key, value, one of key or value, or both key and value, depending on
4652 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4653 is nil.
4655 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4656 (nargs, args)
4657 int nargs;
4658 Lisp_Object *args;
4660 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4661 Lisp_Object user_test, user_hash;
4662 char *used;
4663 int i;
4665 /* The vector `used' is used to keep track of arguments that
4666 have been consumed. */
4667 used = (char *) alloca (nargs * sizeof *used);
4668 bzero (used, nargs * sizeof *used);
4670 /* See if there's a `:test TEST' among the arguments. */
4671 i = get_key_arg (QCtest, nargs, args, used);
4672 test = i < 0 ? Qeql : args[i];
4673 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4675 /* See if it is a user-defined test. */
4676 Lisp_Object prop;
4678 prop = Fget (test, Qhash_table_test);
4679 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4680 signal_error ("Invalid hash table test", test);
4681 user_test = XCAR (prop);
4682 user_hash = XCAR (XCDR (prop));
4684 else
4685 user_test = user_hash = Qnil;
4687 /* See if there's a `:size SIZE' argument. */
4688 i = get_key_arg (QCsize, nargs, args, used);
4689 size = i < 0 ? Qnil : args[i];
4690 if (NILP (size))
4691 size = make_number (DEFAULT_HASH_SIZE);
4692 else if (!INTEGERP (size) || XINT (size) < 0)
4693 signal_error ("Invalid hash table size", size);
4695 /* Look for `:rehash-size SIZE'. */
4696 i = get_key_arg (QCrehash_size, nargs, args, used);
4697 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4698 if (!NUMBERP (rehash_size)
4699 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4700 || XFLOATINT (rehash_size) <= 1.0)
4701 signal_error ("Invalid hash table rehash size", rehash_size);
4703 /* Look for `:rehash-threshold THRESHOLD'. */
4704 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4705 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4706 if (!FLOATP (rehash_threshold)
4707 || XFLOATINT (rehash_threshold) <= 0.0
4708 || XFLOATINT (rehash_threshold) > 1.0)
4709 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4711 /* Look for `:weakness WEAK'. */
4712 i = get_key_arg (QCweakness, nargs, args, used);
4713 weak = i < 0 ? Qnil : args[i];
4714 if (EQ (weak, Qt))
4715 weak = Qkey_and_value;
4716 if (!NILP (weak)
4717 && !EQ (weak, Qkey)
4718 && !EQ (weak, Qvalue)
4719 && !EQ (weak, Qkey_or_value)
4720 && !EQ (weak, Qkey_and_value))
4721 signal_error ("Invalid hash table weakness", weak);
4723 /* Now, all args should have been used up, or there's a problem. */
4724 for (i = 0; i < nargs; ++i)
4725 if (!used[i])
4726 signal_error ("Invalid argument list", args[i]);
4728 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4729 user_test, user_hash);
4733 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4734 doc: /* Return a copy of hash table TABLE. */)
4735 (table)
4736 Lisp_Object table;
4738 return copy_hash_table (check_hash_table (table));
4742 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4743 doc: /* Return the number of elements in TABLE. */)
4744 (table)
4745 Lisp_Object table;
4747 return make_number (check_hash_table (table)->count);
4751 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4752 Shash_table_rehash_size, 1, 1, 0,
4753 doc: /* Return the current rehash size of TABLE. */)
4754 (table)
4755 Lisp_Object table;
4757 return check_hash_table (table)->rehash_size;
4761 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4762 Shash_table_rehash_threshold, 1, 1, 0,
4763 doc: /* Return the current rehash threshold of TABLE. */)
4764 (table)
4765 Lisp_Object table;
4767 return check_hash_table (table)->rehash_threshold;
4771 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4772 doc: /* Return the size of TABLE.
4773 The size can be used as an argument to `make-hash-table' to create
4774 a hash table than can hold as many elements of TABLE holds
4775 without need for resizing. */)
4776 (table)
4777 Lisp_Object table;
4779 struct Lisp_Hash_Table *h = check_hash_table (table);
4780 return make_number (HASH_TABLE_SIZE (h));
4784 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4785 doc: /* Return the test TABLE uses. */)
4786 (table)
4787 Lisp_Object table;
4789 return check_hash_table (table)->test;
4793 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4794 1, 1, 0,
4795 doc: /* Return the weakness of TABLE. */)
4796 (table)
4797 Lisp_Object table;
4799 return check_hash_table (table)->weak;
4803 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4804 doc: /* Return t if OBJ is a Lisp hash table object. */)
4805 (obj)
4806 Lisp_Object obj;
4808 return HASH_TABLE_P (obj) ? Qt : Qnil;
4812 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4813 doc: /* Clear hash table TABLE and return it. */)
4814 (table)
4815 Lisp_Object table;
4817 hash_clear (check_hash_table (table));
4818 /* Be compatible with XEmacs. */
4819 return table;
4823 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4824 doc: /* Look up KEY in TABLE and return its associated value.
4825 If KEY is not found, return DFLT which defaults to nil. */)
4826 (key, table, dflt)
4827 Lisp_Object key, table, dflt;
4829 struct Lisp_Hash_Table *h = check_hash_table (table);
4830 int i = hash_lookup (h, key, NULL);
4831 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4835 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4836 doc: /* Associate KEY with VALUE in hash table TABLE.
4837 If KEY is already present in table, replace its current value with
4838 VALUE. */)
4839 (key, value, table)
4840 Lisp_Object key, value, table;
4842 struct Lisp_Hash_Table *h = check_hash_table (table);
4843 int i;
4844 unsigned hash;
4846 i = hash_lookup (h, key, &hash);
4847 if (i >= 0)
4848 HASH_VALUE (h, i) = value;
4849 else
4850 hash_put (h, key, value, hash);
4852 return value;
4856 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4857 doc: /* Remove KEY from TABLE. */)
4858 (key, table)
4859 Lisp_Object key, table;
4861 struct Lisp_Hash_Table *h = check_hash_table (table);
4862 hash_remove_from_table (h, key);
4863 return Qnil;
4867 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4868 doc: /* Call FUNCTION for all entries in hash table TABLE.
4869 FUNCTION is called with two arguments, KEY and VALUE. */)
4870 (function, table)
4871 Lisp_Object function, table;
4873 struct Lisp_Hash_Table *h = check_hash_table (table);
4874 Lisp_Object args[3];
4875 int i;
4877 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4878 if (!NILP (HASH_HASH (h, i)))
4880 args[0] = function;
4881 args[1] = HASH_KEY (h, i);
4882 args[2] = HASH_VALUE (h, i);
4883 Ffuncall (3, args);
4886 return Qnil;
4890 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4891 Sdefine_hash_table_test, 3, 3, 0,
4892 doc: /* Define a new hash table test with name NAME, a symbol.
4894 In hash tables created with NAME specified as test, use TEST to
4895 compare keys, and HASH for computing hash codes of keys.
4897 TEST must be a function taking two arguments and returning non-nil if
4898 both arguments are the same. HASH must be a function taking one
4899 argument and return an integer that is the hash code of the argument.
4900 Hash code computation should use the whole value range of integers,
4901 including negative integers. */)
4902 (name, test, hash)
4903 Lisp_Object name, test, hash;
4905 return Fput (name, Qhash_table_test, list2 (test, hash));
4910 /************************************************************************
4912 ************************************************************************/
4914 #include "md5.h"
4916 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4917 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4919 A message digest is a cryptographic checksum of a document, and the
4920 algorithm to calculate it is defined in RFC 1321.
4922 The two optional arguments START and END are character positions
4923 specifying for which part of OBJECT the message digest should be
4924 computed. If nil or omitted, the digest is computed for the whole
4925 OBJECT.
4927 The MD5 message digest is computed from the result of encoding the
4928 text in a coding system, not directly from the internal Emacs form of
4929 the text. The optional fourth argument CODING-SYSTEM specifies which
4930 coding system to encode the text with. It should be the same coding
4931 system that you used or will use when actually writing the text into a
4932 file.
4934 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4935 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4936 system would be chosen by default for writing this text into a file.
4938 If OBJECT is a string, the most preferred coding system (see the
4939 command `prefer-coding-system') is used.
4941 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4942 guesswork fails. Normally, an error is signaled in such case. */)
4943 (object, start, end, coding_system, noerror)
4944 Lisp_Object object, start, end, coding_system, noerror;
4946 unsigned char digest[16];
4947 unsigned char value[33];
4948 int i;
4949 int size;
4950 int size_byte = 0;
4951 int start_char = 0, end_char = 0;
4952 int start_byte = 0, end_byte = 0;
4953 register int b, e;
4954 register struct buffer *bp;
4955 int temp;
4957 if (STRINGP (object))
4959 if (NILP (coding_system))
4961 /* Decide the coding-system to encode the data with. */
4963 if (STRING_MULTIBYTE (object))
4964 /* use default, we can't guess correct value */
4965 coding_system = preferred_coding_system ();
4966 else
4967 coding_system = Qraw_text;
4970 if (NILP (Fcoding_system_p (coding_system)))
4972 /* Invalid coding system. */
4974 if (!NILP (noerror))
4975 coding_system = Qraw_text;
4976 else
4977 xsignal1 (Qcoding_system_error, coding_system);
4980 if (STRING_MULTIBYTE (object))
4981 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4983 size = SCHARS (object);
4984 size_byte = SBYTES (object);
4986 if (!NILP (start))
4988 CHECK_NUMBER (start);
4990 start_char = XINT (start);
4992 if (start_char < 0)
4993 start_char += size;
4995 start_byte = string_char_to_byte (object, start_char);
4998 if (NILP (end))
5000 end_char = size;
5001 end_byte = size_byte;
5003 else
5005 CHECK_NUMBER (end);
5007 end_char = XINT (end);
5009 if (end_char < 0)
5010 end_char += size;
5012 end_byte = string_char_to_byte (object, end_char);
5015 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5016 args_out_of_range_3 (object, make_number (start_char),
5017 make_number (end_char));
5019 else
5021 struct buffer *prev = current_buffer;
5023 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5025 CHECK_BUFFER (object);
5027 bp = XBUFFER (object);
5028 if (bp != current_buffer)
5029 set_buffer_internal (bp);
5031 if (NILP (start))
5032 b = BEGV;
5033 else
5035 CHECK_NUMBER_COERCE_MARKER (start);
5036 b = XINT (start);
5039 if (NILP (end))
5040 e = ZV;
5041 else
5043 CHECK_NUMBER_COERCE_MARKER (end);
5044 e = XINT (end);
5047 if (b > e)
5048 temp = b, b = e, e = temp;
5050 if (!(BEGV <= b && e <= ZV))
5051 args_out_of_range (start, end);
5053 if (NILP (coding_system))
5055 /* Decide the coding-system to encode the data with.
5056 See fileio.c:Fwrite-region */
5058 if (!NILP (Vcoding_system_for_write))
5059 coding_system = Vcoding_system_for_write;
5060 else
5062 int force_raw_text = 0;
5064 coding_system = BUF_BUFFER_FILE_CODING_SYSTEM (XBUFFER (object));
5065 if (NILP (coding_system)
5066 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5068 coding_system = Qnil;
5069 if (NILP (BUF_ENABLE_MULTIBYTE_CHARACTERS (current_buffer)))
5070 force_raw_text = 1;
5073 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5075 /* Check file-coding-system-alist. */
5076 Lisp_Object args[4], val;
5078 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5079 args[3] = Fbuffer_file_name(object);
5080 val = Ffind_operation_coding_system (4, args);
5081 if (CONSP (val) && !NILP (XCDR (val)))
5082 coding_system = XCDR (val);
5085 if (NILP (coding_system)
5086 && !NILP (BUF_BUFFER_FILE_CODING_SYSTEM (XBUFFER (object))))
5088 /* If we still have not decided a coding system, use the
5089 default value of buffer-file-coding-system. */
5090 coding_system = BUF_BUFFER_FILE_CODING_SYSTEM (XBUFFER (object));
5093 if (!force_raw_text
5094 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5095 /* Confirm that VAL can surely encode the current region. */
5096 coding_system = call4 (Vselect_safe_coding_system_function,
5097 make_number (b), make_number (e),
5098 coding_system, Qnil);
5100 if (force_raw_text)
5101 coding_system = Qraw_text;
5104 if (NILP (Fcoding_system_p (coding_system)))
5106 /* Invalid coding system. */
5108 if (!NILP (noerror))
5109 coding_system = Qraw_text;
5110 else
5111 xsignal1 (Qcoding_system_error, coding_system);
5115 object = make_buffer_string (b, e, 0);
5116 if (prev != current_buffer)
5117 set_buffer_internal (prev);
5118 /* Discard the unwind protect for recovering the current
5119 buffer. */
5120 specpdl_ptr--;
5122 if (STRING_MULTIBYTE (object))
5123 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
5126 md5_buffer (SDATA (object) + start_byte,
5127 SBYTES (object) - (size_byte - end_byte),
5128 digest);
5130 for (i = 0; i < 16; i++)
5131 sprintf (&value[2 * i], "%02x", digest[i]);
5132 value[32] = '\0';
5134 return make_string (value, 32);
5138 void
5139 syms_of_fns ()
5141 /* Hash table stuff. */
5142 Qhash_table_p = intern_c_string ("hash-table-p");
5143 staticpro (&Qhash_table_p);
5144 Qeq = intern_c_string ("eq");
5145 staticpro (&Qeq);
5146 Qeql = intern_c_string ("eql");
5147 staticpro (&Qeql);
5148 Qequal = intern_c_string ("equal");
5149 staticpro (&Qequal);
5150 QCtest = intern_c_string (":test");
5151 staticpro (&QCtest);
5152 QCsize = intern_c_string (":size");
5153 staticpro (&QCsize);
5154 QCrehash_size = intern_c_string (":rehash-size");
5155 staticpro (&QCrehash_size);
5156 QCrehash_threshold = intern_c_string (":rehash-threshold");
5157 staticpro (&QCrehash_threshold);
5158 QCweakness = intern_c_string (":weakness");
5159 staticpro (&QCweakness);
5160 Qkey = intern_c_string ("key");
5161 staticpro (&Qkey);
5162 Qvalue = intern_c_string ("value");
5163 staticpro (&Qvalue);
5164 Qhash_table_test = intern_c_string ("hash-table-test");
5165 staticpro (&Qhash_table_test);
5166 Qkey_or_value = intern_c_string ("key-or-value");
5167 staticpro (&Qkey_or_value);
5168 Qkey_and_value = intern_c_string ("key-and-value");
5169 staticpro (&Qkey_and_value);
5171 defsubr (&Ssxhash);
5172 defsubr (&Smake_hash_table);
5173 defsubr (&Scopy_hash_table);
5174 defsubr (&Shash_table_count);
5175 defsubr (&Shash_table_rehash_size);
5176 defsubr (&Shash_table_rehash_threshold);
5177 defsubr (&Shash_table_size);
5178 defsubr (&Shash_table_test);
5179 defsubr (&Shash_table_weakness);
5180 defsubr (&Shash_table_p);
5181 defsubr (&Sclrhash);
5182 defsubr (&Sgethash);
5183 defsubr (&Sputhash);
5184 defsubr (&Sremhash);
5185 defsubr (&Smaphash);
5186 defsubr (&Sdefine_hash_table_test);
5188 Qstring_lessp = intern_c_string ("string-lessp");
5189 staticpro (&Qstring_lessp);
5190 Qprovide = intern_c_string ("provide");
5191 staticpro (&Qprovide);
5192 Qrequire = intern_c_string ("require");
5193 staticpro (&Qrequire);
5194 Qyes_or_no_p_history = intern_c_string ("yes-or-no-p-history");
5195 staticpro (&Qyes_or_no_p_history);
5196 Qcursor_in_echo_area = intern_c_string ("cursor-in-echo-area");
5197 staticpro (&Qcursor_in_echo_area);
5198 Qwidget_type = intern_c_string ("widget-type");
5199 staticpro (&Qwidget_type);
5201 staticpro (&string_char_byte_cache_string);
5202 string_char_byte_cache_string = Qnil;
5204 require_nesting_list = Qnil;
5205 staticpro (&require_nesting_list);
5207 Fset (Qyes_or_no_p_history, Qnil);
5209 DEFVAR_LISP ("features", &Vfeatures,
5210 doc: /* A list of symbols which are the features of the executing Emacs.
5211 Used by `featurep' and `require', and altered by `provide'. */);
5212 Vfeatures = Fcons (intern_c_string ("emacs"), Qnil);
5213 Qsubfeatures = intern_c_string ("subfeatures");
5214 staticpro (&Qsubfeatures);
5216 #ifdef HAVE_LANGINFO_CODESET
5217 Qcodeset = intern_c_string ("codeset");
5218 staticpro (&Qcodeset);
5219 Qdays = intern_c_string ("days");
5220 staticpro (&Qdays);
5221 Qmonths = intern_c_string ("months");
5222 staticpro (&Qmonths);
5223 Qpaper = intern_c_string ("paper");
5224 staticpro (&Qpaper);
5225 #endif /* HAVE_LANGINFO_CODESET */
5227 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5228 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5229 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5230 invoked by mouse clicks and mouse menu items.
5232 On some platforms, file selection dialogs are also enabled if this is
5233 non-nil. */);
5234 use_dialog_box = 1;
5236 DEFVAR_BOOL ("use-file-dialog", &use_file_dialog,
5237 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
5238 This applies to commands from menus and tool bar buttons even when
5239 they are initiated from the keyboard. If `use-dialog-box' is nil,
5240 that disables the use of a file dialog, regardless of the value of
5241 this variable. */);
5242 use_file_dialog = 1;
5244 defsubr (&Sidentity);
5245 defsubr (&Srandom);
5246 defsubr (&Slength);
5247 defsubr (&Ssafe_length);
5248 defsubr (&Sstring_bytes);
5249 defsubr (&Sstring_equal);
5250 defsubr (&Scompare_strings);
5251 defsubr (&Sstring_lessp);
5252 defsubr (&Sappend);
5253 defsubr (&Sconcat);
5254 defsubr (&Svconcat);
5255 defsubr (&Scopy_sequence);
5256 defsubr (&Sstring_make_multibyte);
5257 defsubr (&Sstring_make_unibyte);
5258 defsubr (&Sstring_as_multibyte);
5259 defsubr (&Sstring_as_unibyte);
5260 defsubr (&Sstring_to_multibyte);
5261 defsubr (&Sstring_to_unibyte);
5262 defsubr (&Scopy_alist);
5263 defsubr (&Ssubstring);
5264 defsubr (&Ssubstring_no_properties);
5265 defsubr (&Snthcdr);
5266 defsubr (&Snth);
5267 defsubr (&Selt);
5268 defsubr (&Smember);
5269 defsubr (&Smemq);
5270 defsubr (&Smemql);
5271 defsubr (&Sassq);
5272 defsubr (&Sassoc);
5273 defsubr (&Srassq);
5274 defsubr (&Srassoc);
5275 defsubr (&Sdelq);
5276 defsubr (&Sdelete);
5277 defsubr (&Snreverse);
5278 defsubr (&Sreverse);
5279 defsubr (&Ssort);
5280 defsubr (&Splist_get);
5281 defsubr (&Sget);
5282 defsubr (&Splist_put);
5283 defsubr (&Sput);
5284 defsubr (&Slax_plist_get);
5285 defsubr (&Slax_plist_put);
5286 defsubr (&Seql);
5287 defsubr (&Sequal);
5288 defsubr (&Sequal_including_properties);
5289 defsubr (&Sfillarray);
5290 defsubr (&Sclear_string);
5291 defsubr (&Snconc);
5292 defsubr (&Smapcar);
5293 defsubr (&Smapc);
5294 defsubr (&Smapconcat);
5295 defsubr (&Sy_or_n_p);
5296 defsubr (&Syes_or_no_p);
5297 defsubr (&Sload_average);
5298 defsubr (&Sfeaturep);
5299 defsubr (&Srequire);
5300 defsubr (&Sprovide);
5301 defsubr (&Splist_member);
5302 defsubr (&Swidget_put);
5303 defsubr (&Swidget_get);
5304 defsubr (&Swidget_apply);
5305 defsubr (&Sbase64_encode_region);
5306 defsubr (&Sbase64_decode_region);
5307 defsubr (&Sbase64_encode_string);
5308 defsubr (&Sbase64_decode_string);
5309 defsubr (&Smd5);
5310 defsubr (&Slocale_info);
5314 void
5315 init_fns ()
5319 /* arch-tag: 787f8219-5b74-46bd-8469-7e1cc475fa31
5320 (do not change this comment) */