(sgml-empty-tags): New var.
[emacs.git] / src / fns.c
bloba948ed7b567d877b1edf23990b6efd2d05fd4a7b
1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <time.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 "charset.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 #if defined (HAVE_MENUS) && defined (HAVE_X_WINDOWS)
46 #include "xterm.h"
47 #endif
49 #ifndef NULL
50 #define NULL (void *)0
51 #endif
53 /* Nonzero enables use of dialog boxes for questions
54 asked by mouse commands. */
55 int use_dialog_box;
57 extern int minibuffer_auto_raise;
58 extern Lisp_Object minibuf_window;
60 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
61 Lisp_Object Qyes_or_no_p_history;
62 Lisp_Object Qcursor_in_echo_area;
63 Lisp_Object Qwidget_type;
65 extern Lisp_Object Qinput_method_function;
67 static int internal_equal ();
69 extern long get_random ();
70 extern void seed_random ();
72 #ifndef HAVE_UNISTD_H
73 extern long time ();
74 #endif
76 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
77 doc: /* Return the argument unchanged. */)
78 (arg)
79 Lisp_Object arg;
81 return arg;
84 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
85 doc: /* Return a pseudo-random number.
86 All integers representable in Lisp are equally likely.
87 On most systems, this is 28 bits' worth.
88 With positive integer argument N, return random number in interval [0,N).
89 With argument t, set the random number seed from the current time and pid. */)
90 (n)
91 Lisp_Object n;
93 EMACS_INT val;
94 Lisp_Object lispy_val;
95 unsigned long denominator;
97 if (EQ (n, Qt))
98 seed_random (getpid () + time (NULL));
99 if (NATNUMP (n) && XFASTINT (n) != 0)
101 /* Try to take our random number from the higher bits of VAL,
102 not the lower, since (says Gentzel) the low bits of `random'
103 are less random than the higher ones. We do this by using the
104 quotient rather than the remainder. At the high end of the RNG
105 it's possible to get a quotient larger than n; discarding
106 these values eliminates the bias that would otherwise appear
107 when using a large n. */
108 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (n);
110 val = get_random () / denominator;
111 while (val >= XFASTINT (n));
113 else
114 val = get_random ();
115 XSETINT (lispy_val, val);
116 return lispy_val;
119 /* Random data-structure functions */
121 DEFUN ("length", Flength, Slength, 1, 1, 0,
122 doc: /* Return the length of vector, list or string SEQUENCE.
123 A byte-code function object is also allowed.
124 If the string contains multibyte characters, this is not the necessarily
125 the number of bytes in the string; it is the number of characters.
126 To get the number of bytes, use `string-bytes'. */)
127 (sequence)
128 register Lisp_Object sequence;
130 register Lisp_Object val;
131 register int i;
133 retry:
134 if (STRINGP (sequence))
135 XSETFASTINT (val, XSTRING (sequence)->size);
136 else if (VECTORP (sequence))
137 XSETFASTINT (val, XVECTOR (sequence)->size);
138 else if (CHAR_TABLE_P (sequence))
139 XSETFASTINT (val, MAX_CHAR);
140 else if (BOOL_VECTOR_P (sequence))
141 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
142 else if (COMPILEDP (sequence))
143 XSETFASTINT (val, XVECTOR (sequence)->size & PSEUDOVECTOR_SIZE_MASK);
144 else if (CONSP (sequence))
146 i = 0;
147 while (CONSP (sequence))
149 sequence = XCDR (sequence);
150 ++i;
152 if (!CONSP (sequence))
153 break;
155 sequence = XCDR (sequence);
156 ++i;
157 QUIT;
160 if (!NILP (sequence))
161 wrong_type_argument (Qlistp, sequence);
163 val = make_number (i);
165 else if (NILP (sequence))
166 XSETFASTINT (val, 0);
167 else
169 sequence = wrong_type_argument (Qsequencep, sequence);
170 goto retry;
172 return val;
175 /* This does not check for quits. That is safe
176 since it must terminate. */
178 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
179 doc: /* Return the length of a list, but avoid error or infinite loop.
180 This function never gets an error. If LIST is not really a list,
181 it returns 0. If LIST is circular, it returns a finite value
182 which is at least the number of distinct elements. */)
183 (list)
184 Lisp_Object list;
186 Lisp_Object tail, halftail, length;
187 int len = 0;
189 /* halftail is used to detect circular lists. */
190 halftail = list;
191 for (tail = list; CONSP (tail); tail = XCDR (tail))
193 if (EQ (tail, halftail) && len != 0)
194 break;
195 len++;
196 if ((len & 1) == 0)
197 halftail = XCDR (halftail);
200 XSETINT (length, len);
201 return length;
204 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
205 doc: /* Return the number of bytes in STRING.
206 If STRING is a multibyte string, this is greater than the length of STRING. */)
207 (string)
208 Lisp_Object string;
210 CHECK_STRING (string, 1);
211 return make_number (STRING_BYTES (XSTRING (string)));
214 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
215 doc: /* Return t if two strings have identical contents.
216 Case is significant, but text properties are ignored.
217 Symbols are also allowed; their print names are used instead. */)
218 (s1, s2)
219 register Lisp_Object s1, s2;
221 if (SYMBOLP (s1))
222 XSETSTRING (s1, XSYMBOL (s1)->name);
223 if (SYMBOLP (s2))
224 XSETSTRING (s2, XSYMBOL (s2)->name);
225 CHECK_STRING (s1, 0);
226 CHECK_STRING (s2, 1);
228 if (XSTRING (s1)->size != XSTRING (s2)->size
229 || STRING_BYTES (XSTRING (s1)) != STRING_BYTES (XSTRING (s2))
230 || bcmp (XSTRING (s1)->data, XSTRING (s2)->data, STRING_BYTES (XSTRING (s1))))
231 return Qnil;
232 return Qt;
235 DEFUN ("compare-strings", Fcompare_strings,
236 Scompare_strings, 6, 7, 0,
237 doc: /* Compare the contents of two strings, converting to multibyte if needed.
238 In string STR1, skip the first START1 characters and stop at END1.
239 In string STR2, skip the first START2 characters and stop at END2.
240 END1 and END2 default to the full lengths of the respective strings.
242 Case is significant in this comparison if IGNORE-CASE is nil.
243 Unibyte strings are converted to multibyte for comparison.
245 The value is t if the strings (or specified portions) match.
246 If string STR1 is less, the value is a negative number N;
247 - 1 - N is the number of characters that match at the beginning.
248 If string STR1 is greater, the value is a positive number N;
249 N - 1 is the number of characters that match at the beginning. */)
250 (str1, start1, end1, str2, start2, end2, ignore_case)
251 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
253 register int end1_char, end2_char;
254 register int i1, i1_byte, i2, i2_byte;
256 CHECK_STRING (str1, 0);
257 CHECK_STRING (str2, 1);
258 if (NILP (start1))
259 start1 = make_number (0);
260 if (NILP (start2))
261 start2 = make_number (0);
262 CHECK_NATNUM (start1, 2);
263 CHECK_NATNUM (start2, 3);
264 if (! NILP (end1))
265 CHECK_NATNUM (end1, 4);
266 if (! NILP (end2))
267 CHECK_NATNUM (end2, 4);
269 i1 = XINT (start1);
270 i2 = XINT (start2);
272 i1_byte = string_char_to_byte (str1, i1);
273 i2_byte = string_char_to_byte (str2, i2);
275 end1_char = XSTRING (str1)->size;
276 if (! NILP (end1) && end1_char > XINT (end1))
277 end1_char = XINT (end1);
279 end2_char = XSTRING (str2)->size;
280 if (! NILP (end2) && end2_char > XINT (end2))
281 end2_char = XINT (end2);
283 while (i1 < end1_char && i2 < end2_char)
285 /* When we find a mismatch, we must compare the
286 characters, not just the bytes. */
287 int c1, c2;
289 if (STRING_MULTIBYTE (str1))
290 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
291 else
293 c1 = XSTRING (str1)->data[i1++];
294 c1 = unibyte_char_to_multibyte (c1);
297 if (STRING_MULTIBYTE (str2))
298 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
299 else
301 c2 = XSTRING (str2)->data[i2++];
302 c2 = unibyte_char_to_multibyte (c2);
305 if (c1 == c2)
306 continue;
308 if (! NILP (ignore_case))
310 Lisp_Object tem;
312 tem = Fupcase (make_number (c1));
313 c1 = XINT (tem);
314 tem = Fupcase (make_number (c2));
315 c2 = XINT (tem);
318 if (c1 == c2)
319 continue;
321 /* Note that I1 has already been incremented
322 past the character that we are comparing;
323 hence we don't add or subtract 1 here. */
324 if (c1 < c2)
325 return make_number (- i1 + XINT (start1));
326 else
327 return make_number (i1 - XINT (start1));
330 if (i1 < end1_char)
331 return make_number (i1 - XINT (start1) + 1);
332 if (i2 < end2_char)
333 return make_number (- i1 + XINT (start1) - 1);
335 return Qt;
338 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
339 doc: /* Return t if first arg string is less than second in lexicographic order.
340 Case is significant.
341 Symbols are also allowed; their print names are used instead. */)
342 (s1, s2)
343 register Lisp_Object s1, s2;
345 register int end;
346 register int i1, i1_byte, i2, i2_byte;
348 if (SYMBOLP (s1))
349 XSETSTRING (s1, XSYMBOL (s1)->name);
350 if (SYMBOLP (s2))
351 XSETSTRING (s2, XSYMBOL (s2)->name);
352 CHECK_STRING (s1, 0);
353 CHECK_STRING (s2, 1);
355 i1 = i1_byte = i2 = i2_byte = 0;
357 end = XSTRING (s1)->size;
358 if (end > XSTRING (s2)->size)
359 end = XSTRING (s2)->size;
361 while (i1 < end)
363 /* When we find a mismatch, we must compare the
364 characters, not just the bytes. */
365 int c1, c2;
367 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
368 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
370 if (c1 != c2)
371 return c1 < c2 ? Qt : Qnil;
373 return i1 < XSTRING (s2)->size ? Qt : Qnil;
376 static Lisp_Object concat ();
378 /* ARGSUSED */
379 Lisp_Object
380 concat2 (s1, s2)
381 Lisp_Object s1, s2;
383 #ifdef NO_ARG_ARRAY
384 Lisp_Object args[2];
385 args[0] = s1;
386 args[1] = s2;
387 return concat (2, args, Lisp_String, 0);
388 #else
389 return concat (2, &s1, Lisp_String, 0);
390 #endif /* NO_ARG_ARRAY */
393 /* ARGSUSED */
394 Lisp_Object
395 concat3 (s1, s2, s3)
396 Lisp_Object s1, s2, s3;
398 #ifdef NO_ARG_ARRAY
399 Lisp_Object args[3];
400 args[0] = s1;
401 args[1] = s2;
402 args[2] = s3;
403 return concat (3, args, Lisp_String, 0);
404 #else
405 return concat (3, &s1, Lisp_String, 0);
406 #endif /* NO_ARG_ARRAY */
409 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
410 doc: /* Concatenate all the arguments and make the result a list.
411 The result is a list whose elements are the elements of all the arguments.
412 Each argument may be a list, vector or string.
413 The last argument is not copied, just used as the tail of the new list.
414 usage: (append &rest SEQUENCES) */)
415 (nargs, args)
416 int nargs;
417 Lisp_Object *args;
419 return concat (nargs, args, Lisp_Cons, 1);
422 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
423 doc: /* Concatenate all the arguments and make the result a string.
424 The result is a string whose elements are the elements of all the arguments.
425 Each argument may be a string or a list or vector of characters (integers).
426 usage: (concat &rest SEQUENCES) */)
427 (nargs, args)
428 int nargs;
429 Lisp_Object *args;
431 return concat (nargs, args, Lisp_String, 0);
434 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
435 doc: /* Concatenate all the arguments and make the result a vector.
436 The result is a vector whose elements are the elements of all the arguments.
437 Each argument may be a list, vector or string.
438 usage: (vconcat &rest SEQUENCES) */)
439 (nargs, args)
440 int nargs;
441 Lisp_Object *args;
443 return concat (nargs, args, Lisp_Vectorlike, 0);
446 /* Retrun a copy of a sub char table ARG. The elements except for a
447 nested sub char table are not copied. */
448 static Lisp_Object
449 copy_sub_char_table (arg)
450 Lisp_Object arg;
452 Lisp_Object copy = make_sub_char_table (XCHAR_TABLE (arg)->defalt);
453 int i;
455 /* Copy all the contents. */
456 bcopy (XCHAR_TABLE (arg)->contents, XCHAR_TABLE (copy)->contents,
457 SUB_CHAR_TABLE_ORDINARY_SLOTS * sizeof (Lisp_Object));
458 /* Recursively copy any sub char-tables in the ordinary slots. */
459 for (i = 32; i < SUB_CHAR_TABLE_ORDINARY_SLOTS; i++)
460 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg)->contents[i]))
461 XCHAR_TABLE (copy)->contents[i]
462 = copy_sub_char_table (XCHAR_TABLE (copy)->contents[i]);
464 return copy;
468 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
469 doc: /* Return a copy of a list, vector or string.
470 The elements of a list or vector are not copied; they are shared
471 with the original. */)
472 (arg)
473 Lisp_Object arg;
475 if (NILP (arg)) return arg;
477 if (CHAR_TABLE_P (arg))
479 int i;
480 Lisp_Object copy;
482 copy = Fmake_char_table (XCHAR_TABLE (arg)->purpose, Qnil);
483 /* Copy all the slots, including the extra ones. */
484 bcopy (XVECTOR (arg)->contents, XVECTOR (copy)->contents,
485 ((XCHAR_TABLE (arg)->size & PSEUDOVECTOR_SIZE_MASK)
486 * sizeof (Lisp_Object)));
488 /* Recursively copy any sub char tables in the ordinary slots
489 for multibyte characters. */
490 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS;
491 i < CHAR_TABLE_ORDINARY_SLOTS; i++)
492 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg)->contents[i]))
493 XCHAR_TABLE (copy)->contents[i]
494 = copy_sub_char_table (XCHAR_TABLE (copy)->contents[i]);
496 return copy;
499 if (BOOL_VECTOR_P (arg))
501 Lisp_Object val;
502 int size_in_chars
503 = (XBOOL_VECTOR (arg)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
505 val = Fmake_bool_vector (Flength (arg), Qnil);
506 bcopy (XBOOL_VECTOR (arg)->data, XBOOL_VECTOR (val)->data,
507 size_in_chars);
508 return val;
511 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
512 arg = wrong_type_argument (Qsequencep, arg);
513 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
516 /* In string STR of length LEN, see if bytes before STR[I] combine
517 with bytes after STR[I] to form a single character. If so, return
518 the number of bytes after STR[I] which combine in this way.
519 Otherwize, return 0. */
521 static int
522 count_combining (str, len, i)
523 unsigned char *str;
524 int len, i;
526 int j = i - 1, bytes;
528 if (i == 0 || i == len || CHAR_HEAD_P (str[i]))
529 return 0;
530 while (j >= 0 && !CHAR_HEAD_P (str[j])) j--;
531 if (j < 0 || ! BASE_LEADING_CODE_P (str[j]))
532 return 0;
533 PARSE_MULTIBYTE_SEQ (str + j, len - j, bytes);
534 return (bytes <= i - j ? 0 : bytes - (i - j));
537 /* This structure holds information of an argument of `concat' that is
538 a string and has text properties to be copied. */
539 struct textprop_rec
541 int argnum; /* refer to ARGS (arguments of `concat') */
542 int from; /* refer to ARGS[argnum] (argument string) */
543 int to; /* refer to VAL (the target string) */
546 static Lisp_Object
547 concat (nargs, args, target_type, last_special)
548 int nargs;
549 Lisp_Object *args;
550 enum Lisp_Type target_type;
551 int last_special;
553 Lisp_Object val;
554 register Lisp_Object tail;
555 register Lisp_Object this;
556 int toindex;
557 int toindex_byte = 0;
558 register int result_len;
559 register int result_len_byte;
560 register int argnum;
561 Lisp_Object last_tail;
562 Lisp_Object prev;
563 int some_multibyte;
564 /* When we make a multibyte string, we can't copy text properties
565 while concatinating each string because the length of resulting
566 string can't be decided until we finish the whole concatination.
567 So, we record strings that have text properties to be copied
568 here, and copy the text properties after the concatination. */
569 struct textprop_rec *textprops = NULL;
570 /* Number of elments in textprops. */
571 int num_textprops = 0;
573 tail = Qnil;
575 /* In append, the last arg isn't treated like the others */
576 if (last_special && nargs > 0)
578 nargs--;
579 last_tail = args[nargs];
581 else
582 last_tail = Qnil;
584 /* Canonicalize each argument. */
585 for (argnum = 0; argnum < nargs; argnum++)
587 this = args[argnum];
588 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
589 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
591 args[argnum] = wrong_type_argument (Qsequencep, this);
595 /* Compute total length in chars of arguments in RESULT_LEN.
596 If desired output is a string, also compute length in bytes
597 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
598 whether the result should be a multibyte string. */
599 result_len_byte = 0;
600 result_len = 0;
601 some_multibyte = 0;
602 for (argnum = 0; argnum < nargs; argnum++)
604 int len;
605 this = args[argnum];
606 len = XFASTINT (Flength (this));
607 if (target_type == Lisp_String)
609 /* We must count the number of bytes needed in the string
610 as well as the number of characters. */
611 int i;
612 Lisp_Object ch;
613 int this_len_byte;
615 if (VECTORP (this))
616 for (i = 0; i < len; i++)
618 ch = XVECTOR (this)->contents[i];
619 if (! INTEGERP (ch))
620 wrong_type_argument (Qintegerp, ch);
621 this_len_byte = CHAR_BYTES (XINT (ch));
622 result_len_byte += this_len_byte;
623 if (!SINGLE_BYTE_CHAR_P (XINT (ch)))
624 some_multibyte = 1;
626 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
627 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
628 else if (CONSP (this))
629 for (; CONSP (this); this = XCDR (this))
631 ch = XCAR (this);
632 if (! INTEGERP (ch))
633 wrong_type_argument (Qintegerp, ch);
634 this_len_byte = CHAR_BYTES (XINT (ch));
635 result_len_byte += this_len_byte;
636 if (!SINGLE_BYTE_CHAR_P (XINT (ch)))
637 some_multibyte = 1;
639 else if (STRINGP (this))
641 if (STRING_MULTIBYTE (this))
643 some_multibyte = 1;
644 result_len_byte += STRING_BYTES (XSTRING (this));
646 else
647 result_len_byte += count_size_as_multibyte (XSTRING (this)->data,
648 XSTRING (this)->size);
652 result_len += len;
655 if (! some_multibyte)
656 result_len_byte = result_len;
658 /* Create the output object. */
659 if (target_type == Lisp_Cons)
660 val = Fmake_list (make_number (result_len), Qnil);
661 else if (target_type == Lisp_Vectorlike)
662 val = Fmake_vector (make_number (result_len), Qnil);
663 else if (some_multibyte)
664 val = make_uninit_multibyte_string (result_len, result_len_byte);
665 else
666 val = make_uninit_string (result_len);
668 /* In `append', if all but last arg are nil, return last arg. */
669 if (target_type == Lisp_Cons && EQ (val, Qnil))
670 return last_tail;
672 /* Copy the contents of the args into the result. */
673 if (CONSP (val))
674 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
675 else
676 toindex = 0, toindex_byte = 0;
678 prev = Qnil;
679 if (STRINGP (val))
680 textprops
681 = (struct textprop_rec *) alloca (sizeof (struct textprop_rec) * nargs);
683 for (argnum = 0; argnum < nargs; argnum++)
685 Lisp_Object thislen;
686 int thisleni = 0;
687 register unsigned int thisindex = 0;
688 register unsigned int thisindex_byte = 0;
690 this = args[argnum];
691 if (!CONSP (this))
692 thislen = Flength (this), thisleni = XINT (thislen);
694 /* Between strings of the same kind, copy fast. */
695 if (STRINGP (this) && STRINGP (val)
696 && STRING_MULTIBYTE (this) == some_multibyte)
698 int thislen_byte = STRING_BYTES (XSTRING (this));
699 int combined;
701 bcopy (XSTRING (this)->data, XSTRING (val)->data + toindex_byte,
702 STRING_BYTES (XSTRING (this)));
703 combined = (some_multibyte && toindex_byte > 0
704 ? count_combining (XSTRING (val)->data,
705 toindex_byte + thislen_byte,
706 toindex_byte)
707 : 0);
708 if (! NULL_INTERVAL_P (XSTRING (this)->intervals))
710 textprops[num_textprops].argnum = argnum;
711 /* We ignore text properties on characters being combined. */
712 textprops[num_textprops].from = combined;
713 textprops[num_textprops++].to = toindex;
715 toindex_byte += thislen_byte;
716 toindex += thisleni - combined;
717 XSTRING (val)->size -= combined;
719 /* Copy a single-byte string to a multibyte string. */
720 else if (STRINGP (this) && STRINGP (val))
722 if (! NULL_INTERVAL_P (XSTRING (this)->intervals))
724 textprops[num_textprops].argnum = argnum;
725 textprops[num_textprops].from = 0;
726 textprops[num_textprops++].to = toindex;
728 toindex_byte += copy_text (XSTRING (this)->data,
729 XSTRING (val)->data + toindex_byte,
730 XSTRING (this)->size, 0, 1);
731 toindex += thisleni;
733 else
734 /* Copy element by element. */
735 while (1)
737 register Lisp_Object elt;
739 /* Fetch next element of `this' arg into `elt', or break if
740 `this' is exhausted. */
741 if (NILP (this)) break;
742 if (CONSP (this))
743 elt = XCAR (this), this = XCDR (this);
744 else if (thisindex >= thisleni)
745 break;
746 else if (STRINGP (this))
748 int c;
749 if (STRING_MULTIBYTE (this))
751 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
752 thisindex,
753 thisindex_byte);
754 XSETFASTINT (elt, c);
756 else
758 XSETFASTINT (elt, XSTRING (this)->data[thisindex++]);
759 if (some_multibyte
760 && (XINT (elt) >= 0240
761 || (XINT (elt) >= 0200
762 && ! NILP (Vnonascii_translation_table)))
763 && XINT (elt) < 0400)
765 c = unibyte_char_to_multibyte (XINT (elt));
766 XSETINT (elt, c);
770 else if (BOOL_VECTOR_P (this))
772 int byte;
773 byte = XBOOL_VECTOR (this)->data[thisindex / BITS_PER_CHAR];
774 if (byte & (1 << (thisindex % BITS_PER_CHAR)))
775 elt = Qt;
776 else
777 elt = Qnil;
778 thisindex++;
780 else
781 elt = XVECTOR (this)->contents[thisindex++];
783 /* Store this element into the result. */
784 if (toindex < 0)
786 XSETCAR (tail, elt);
787 prev = tail;
788 tail = XCDR (tail);
790 else if (VECTORP (val))
791 XVECTOR (val)->contents[toindex++] = elt;
792 else
794 CHECK_NUMBER (elt, 0);
795 if (SINGLE_BYTE_CHAR_P (XINT (elt)))
797 if (some_multibyte)
798 toindex_byte
799 += CHAR_STRING (XINT (elt),
800 XSTRING (val)->data + toindex_byte);
801 else
802 XSTRING (val)->data[toindex_byte++] = XINT (elt);
803 if (some_multibyte
804 && toindex_byte > 0
805 && count_combining (XSTRING (val)->data,
806 toindex_byte, toindex_byte - 1))
807 XSTRING (val)->size--;
808 else
809 toindex++;
811 else
812 /* If we have any multibyte characters,
813 we already decided to make a multibyte string. */
815 int c = XINT (elt);
816 /* P exists as a variable
817 to avoid a bug on the Masscomp C compiler. */
818 unsigned char *p = & XSTRING (val)->data[toindex_byte];
820 toindex_byte += CHAR_STRING (c, p);
821 toindex++;
826 if (!NILP (prev))
827 XSETCDR (prev, last_tail);
829 if (num_textprops > 0)
831 Lisp_Object props;
832 int last_to_end = -1;
834 for (argnum = 0; argnum < num_textprops; argnum++)
836 this = args[textprops[argnum].argnum];
837 props = text_property_list (this,
838 make_number (0),
839 make_number (XSTRING (this)->size),
840 Qnil);
841 /* If successive arguments have properites, be sure that the
842 value of `composition' property be the copy. */
843 if (last_to_end == textprops[argnum].to)
844 make_composition_value_copy (props);
845 add_text_properties_from_list (val, props,
846 make_number (textprops[argnum].to));
847 last_to_end = textprops[argnum].to + XSTRING (this)->size;
850 return val;
853 static Lisp_Object string_char_byte_cache_string;
854 static int string_char_byte_cache_charpos;
855 static int string_char_byte_cache_bytepos;
857 void
858 clear_string_char_byte_cache ()
860 string_char_byte_cache_string = Qnil;
863 /* Return the character index corresponding to CHAR_INDEX in STRING. */
866 string_char_to_byte (string, char_index)
867 Lisp_Object string;
868 int char_index;
870 int i, i_byte;
871 int best_below, best_below_byte;
872 int best_above, best_above_byte;
874 if (! STRING_MULTIBYTE (string))
875 return char_index;
877 best_below = best_below_byte = 0;
878 best_above = XSTRING (string)->size;
879 best_above_byte = STRING_BYTES (XSTRING (string));
881 if (EQ (string, string_char_byte_cache_string))
883 if (string_char_byte_cache_charpos < char_index)
885 best_below = string_char_byte_cache_charpos;
886 best_below_byte = string_char_byte_cache_bytepos;
888 else
890 best_above = string_char_byte_cache_charpos;
891 best_above_byte = string_char_byte_cache_bytepos;
895 if (char_index - best_below < best_above - char_index)
897 while (best_below < char_index)
899 int c;
900 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string,
901 best_below, best_below_byte);
903 i = best_below;
904 i_byte = best_below_byte;
906 else
908 while (best_above > char_index)
910 unsigned char *pend = XSTRING (string)->data + best_above_byte;
911 unsigned char *pbeg = pend - best_above_byte;
912 unsigned char *p = pend - 1;
913 int bytes;
915 while (p > pbeg && !CHAR_HEAD_P (*p)) p--;
916 PARSE_MULTIBYTE_SEQ (p, pend - p, bytes);
917 if (bytes == pend - p)
918 best_above_byte -= bytes;
919 else if (bytes > pend - p)
920 best_above_byte -= (pend - p);
921 else
922 best_above_byte--;
923 best_above--;
925 i = best_above;
926 i_byte = best_above_byte;
929 string_char_byte_cache_bytepos = i_byte;
930 string_char_byte_cache_charpos = i;
931 string_char_byte_cache_string = string;
933 return i_byte;
936 /* Return the character index corresponding to BYTE_INDEX in STRING. */
939 string_byte_to_char (string, byte_index)
940 Lisp_Object string;
941 int byte_index;
943 int i, i_byte;
944 int best_below, best_below_byte;
945 int best_above, best_above_byte;
947 if (! STRING_MULTIBYTE (string))
948 return byte_index;
950 best_below = best_below_byte = 0;
951 best_above = XSTRING (string)->size;
952 best_above_byte = STRING_BYTES (XSTRING (string));
954 if (EQ (string, string_char_byte_cache_string))
956 if (string_char_byte_cache_bytepos < byte_index)
958 best_below = string_char_byte_cache_charpos;
959 best_below_byte = string_char_byte_cache_bytepos;
961 else
963 best_above = string_char_byte_cache_charpos;
964 best_above_byte = string_char_byte_cache_bytepos;
968 if (byte_index - best_below_byte < best_above_byte - byte_index)
970 while (best_below_byte < byte_index)
972 int c;
973 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string,
974 best_below, best_below_byte);
976 i = best_below;
977 i_byte = best_below_byte;
979 else
981 while (best_above_byte > byte_index)
983 unsigned char *pend = XSTRING (string)->data + best_above_byte;
984 unsigned char *pbeg = pend - best_above_byte;
985 unsigned char *p = pend - 1;
986 int bytes;
988 while (p > pbeg && !CHAR_HEAD_P (*p)) p--;
989 PARSE_MULTIBYTE_SEQ (p, pend - p, bytes);
990 if (bytes == pend - p)
991 best_above_byte -= bytes;
992 else if (bytes > pend - p)
993 best_above_byte -= (pend - p);
994 else
995 best_above_byte--;
996 best_above--;
998 i = best_above;
999 i_byte = best_above_byte;
1002 string_char_byte_cache_bytepos = i_byte;
1003 string_char_byte_cache_charpos = i;
1004 string_char_byte_cache_string = string;
1006 return i;
1009 /* Convert STRING to a multibyte string.
1010 Single-byte characters 0240 through 0377 are converted
1011 by adding nonascii_insert_offset to each. */
1013 Lisp_Object
1014 string_make_multibyte (string)
1015 Lisp_Object string;
1017 unsigned char *buf;
1018 int nbytes;
1020 if (STRING_MULTIBYTE (string))
1021 return string;
1023 nbytes = count_size_as_multibyte (XSTRING (string)->data,
1024 XSTRING (string)->size);
1025 /* If all the chars are ASCII, they won't need any more bytes
1026 once converted. In that case, we can return STRING itself. */
1027 if (nbytes == STRING_BYTES (XSTRING (string)))
1028 return string;
1030 buf = (unsigned char *) alloca (nbytes);
1031 copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
1032 0, 1);
1034 return make_multibyte_string (buf, XSTRING (string)->size, nbytes);
1037 /* Convert STRING to a single-byte string. */
1039 Lisp_Object
1040 string_make_unibyte (string)
1041 Lisp_Object string;
1043 unsigned char *buf;
1045 if (! STRING_MULTIBYTE (string))
1046 return string;
1048 buf = (unsigned char *) alloca (XSTRING (string)->size);
1050 copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
1051 1, 0);
1053 return make_unibyte_string (buf, XSTRING (string)->size);
1056 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1057 1, 1, 0,
1058 doc: /* Return the multibyte equivalent of STRING.
1059 The function `unibyte-char-to-multibyte' is used to convert
1060 each unibyte character to a multibyte character. */)
1061 (string)
1062 Lisp_Object string;
1064 CHECK_STRING (string, 0);
1066 return string_make_multibyte (string);
1069 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1070 1, 1, 0,
1071 doc: /* Return the unibyte equivalent of STRING.
1072 Multibyte character codes are converted to unibyte
1073 by using just the low 8 bits. */)
1074 (string)
1075 Lisp_Object string;
1077 CHECK_STRING (string, 0);
1079 return string_make_unibyte (string);
1082 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1083 1, 1, 0,
1084 doc: /* Return a unibyte string with the same individual bytes as STRING.
1085 If STRING is unibyte, the result is STRING itself.
1086 Otherwise it is a newly created string, with no text properties.
1087 If STRING is multibyte and contains a character of charset
1088 `eight-bit-control' or `eight-bit-graphic', it is converted to the
1089 corresponding single byte. */)
1090 (string)
1091 Lisp_Object string;
1093 CHECK_STRING (string, 0);
1095 if (STRING_MULTIBYTE (string))
1097 int bytes = STRING_BYTES (XSTRING (string));
1098 unsigned char *str = (unsigned char *) xmalloc (bytes);
1100 bcopy (XSTRING (string)->data, str, bytes);
1101 bytes = str_as_unibyte (str, bytes);
1102 string = make_unibyte_string (str, bytes);
1103 xfree (str);
1105 return string;
1108 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1109 1, 1, 0,
1110 doc: /* Return a multibyte string with the same individual bytes as STRING.
1111 If STRING is multibyte, the result is STRING itself.
1112 Otherwise it is a newly created string, with no text properties.
1113 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1114 part of a multibyte form), it is converted to the corresponding
1115 multibyte character of charset `eight-bit-control' or `eight-bit-graphic'. */)
1116 (string)
1117 Lisp_Object string;
1119 CHECK_STRING (string, 0);
1121 if (! STRING_MULTIBYTE (string))
1123 Lisp_Object new_string;
1124 int nchars, nbytes;
1126 parse_str_as_multibyte (XSTRING (string)->data,
1127 STRING_BYTES (XSTRING (string)),
1128 &nchars, &nbytes);
1129 new_string = make_uninit_multibyte_string (nchars, nbytes);
1130 bcopy (XSTRING (string)->data, XSTRING (new_string)->data,
1131 STRING_BYTES (XSTRING (string)));
1132 if (nbytes != STRING_BYTES (XSTRING (string)))
1133 str_as_multibyte (XSTRING (new_string)->data, nbytes,
1134 STRING_BYTES (XSTRING (string)), NULL);
1135 string = new_string;
1136 XSTRING (string)->intervals = NULL_INTERVAL;
1138 return string;
1141 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1142 doc: /* Return a copy of ALIST.
1143 This is an alist which represents the same mapping from objects to objects,
1144 but does not share the alist structure with ALIST.
1145 The objects mapped (cars and cdrs of elements of the alist)
1146 are shared, however.
1147 Elements of ALIST that are not conses are also shared. */)
1148 (alist)
1149 Lisp_Object alist;
1151 register Lisp_Object tem;
1153 CHECK_LIST (alist, 0);
1154 if (NILP (alist))
1155 return alist;
1156 alist = concat (1, &alist, Lisp_Cons, 0);
1157 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1159 register Lisp_Object car;
1160 car = XCAR (tem);
1162 if (CONSP (car))
1163 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1165 return alist;
1168 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1169 doc: /*
1170 Return a substring of STRING, starting at index FROM and ending before TO.
1171 TO may be nil or omitted; then the substring runs to the end of STRING.
1172 If FROM or TO is negative, it counts from the end.
1174 This function allows vectors as well as strings. */)
1175 (string, from, to)
1176 Lisp_Object string;
1177 register Lisp_Object from, to;
1179 Lisp_Object res;
1180 int size;
1181 int size_byte = 0;
1182 int from_char, to_char;
1183 int from_byte = 0, to_byte = 0;
1185 if (! (STRINGP (string) || VECTORP (string)))
1186 wrong_type_argument (Qarrayp, string);
1188 CHECK_NUMBER (from, 1);
1190 if (STRINGP (string))
1192 size = XSTRING (string)->size;
1193 size_byte = STRING_BYTES (XSTRING (string));
1195 else
1196 size = XVECTOR (string)->size;
1198 if (NILP (to))
1200 to_char = size;
1201 to_byte = size_byte;
1203 else
1205 CHECK_NUMBER (to, 2);
1207 to_char = XINT (to);
1208 if (to_char < 0)
1209 to_char += size;
1211 if (STRINGP (string))
1212 to_byte = string_char_to_byte (string, to_char);
1215 from_char = XINT (from);
1216 if (from_char < 0)
1217 from_char += size;
1218 if (STRINGP (string))
1219 from_byte = string_char_to_byte (string, from_char);
1221 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1222 args_out_of_range_3 (string, make_number (from_char),
1223 make_number (to_char));
1225 if (STRINGP (string))
1227 res = make_specified_string (XSTRING (string)->data + from_byte,
1228 to_char - from_char, to_byte - from_byte,
1229 STRING_MULTIBYTE (string));
1230 copy_text_properties (make_number (from_char), make_number (to_char),
1231 string, make_number (0), res, Qnil);
1233 else
1234 res = Fvector (to_char - from_char,
1235 XVECTOR (string)->contents + from_char);
1237 return res;
1240 /* Extract a substring of STRING, giving start and end positions
1241 both in characters and in bytes. */
1243 Lisp_Object
1244 substring_both (string, from, from_byte, to, to_byte)
1245 Lisp_Object string;
1246 int from, from_byte, to, to_byte;
1248 Lisp_Object res;
1249 int size;
1250 int size_byte;
1252 if (! (STRINGP (string) || VECTORP (string)))
1253 wrong_type_argument (Qarrayp, string);
1255 if (STRINGP (string))
1257 size = XSTRING (string)->size;
1258 size_byte = STRING_BYTES (XSTRING (string));
1260 else
1261 size = XVECTOR (string)->size;
1263 if (!(0 <= from && from <= to && to <= size))
1264 args_out_of_range_3 (string, make_number (from), make_number (to));
1266 if (STRINGP (string))
1268 res = make_specified_string (XSTRING (string)->data + from_byte,
1269 to - from, to_byte - from_byte,
1270 STRING_MULTIBYTE (string));
1271 copy_text_properties (make_number (from), make_number (to),
1272 string, make_number (0), res, Qnil);
1274 else
1275 res = Fvector (to - from,
1276 XVECTOR (string)->contents + from);
1278 return res;
1281 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1282 doc: /* Take cdr N times on LIST, returns the result. */)
1283 (n, list)
1284 Lisp_Object n;
1285 register Lisp_Object list;
1287 register int i, num;
1288 CHECK_NUMBER (n, 0);
1289 num = XINT (n);
1290 for (i = 0; i < num && !NILP (list); i++)
1292 QUIT;
1293 if (! CONSP (list))
1294 wrong_type_argument (Qlistp, list);
1295 list = XCDR (list);
1297 return list;
1300 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1301 doc: /* Return the Nth element of LIST.
1302 N counts from zero. If LIST is not that long, nil is returned. */)
1303 (n, list)
1304 Lisp_Object n, list;
1306 return Fcar (Fnthcdr (n, list));
1309 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1310 doc: /* Return element of SEQUENCE at index N. */)
1311 (sequence, n)
1312 register Lisp_Object sequence, n;
1314 CHECK_NUMBER (n, 0);
1315 while (1)
1317 if (CONSP (sequence) || NILP (sequence))
1318 return Fcar (Fnthcdr (n, sequence));
1319 else if (STRINGP (sequence) || VECTORP (sequence)
1320 || BOOL_VECTOR_P (sequence) || CHAR_TABLE_P (sequence))
1321 return Faref (sequence, n);
1322 else
1323 sequence = wrong_type_argument (Qsequencep, sequence);
1327 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1328 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1329 The value is actually the tail of LIST whose car is ELT. */)
1330 (elt, list)
1331 register Lisp_Object elt;
1332 Lisp_Object list;
1334 register Lisp_Object tail;
1335 for (tail = list; !NILP (tail); tail = XCDR (tail))
1337 register Lisp_Object tem;
1338 if (! CONSP (tail))
1339 wrong_type_argument (Qlistp, list);
1340 tem = XCAR (tail);
1341 if (! NILP (Fequal (elt, tem)))
1342 return tail;
1343 QUIT;
1345 return Qnil;
1348 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1349 doc: /* Return non-nil if ELT is an element of LIST.
1350 Comparison done with EQ. The value is actually the tail of LIST
1351 whose car is ELT. */)
1352 (elt, list)
1353 Lisp_Object elt, list;
1355 while (1)
1357 if (!CONSP (list) || EQ (XCAR (list), elt))
1358 break;
1360 list = XCDR (list);
1361 if (!CONSP (list) || EQ (XCAR (list), elt))
1362 break;
1364 list = XCDR (list);
1365 if (!CONSP (list) || EQ (XCAR (list), elt))
1366 break;
1368 list = XCDR (list);
1369 QUIT;
1372 if (!CONSP (list) && !NILP (list))
1373 list = wrong_type_argument (Qlistp, list);
1375 return list;
1378 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1379 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1380 The value is actually the element of LIST whose car is KEY.
1381 Elements of LIST that are not conses are ignored. */)
1382 (key, list)
1383 Lisp_Object key, list;
1385 Lisp_Object result;
1387 while (1)
1389 if (!CONSP (list)
1390 || (CONSP (XCAR (list))
1391 && EQ (XCAR (XCAR (list)), key)))
1392 break;
1394 list = XCDR (list);
1395 if (!CONSP (list)
1396 || (CONSP (XCAR (list))
1397 && EQ (XCAR (XCAR (list)), key)))
1398 break;
1400 list = XCDR (list);
1401 if (!CONSP (list)
1402 || (CONSP (XCAR (list))
1403 && EQ (XCAR (XCAR (list)), key)))
1404 break;
1406 list = XCDR (list);
1407 QUIT;
1410 if (CONSP (list))
1411 result = XCAR (list);
1412 else if (NILP (list))
1413 result = Qnil;
1414 else
1415 result = wrong_type_argument (Qlistp, list);
1417 return result;
1420 /* Like Fassq but never report an error and do not allow quits.
1421 Use only on lists known never to be circular. */
1423 Lisp_Object
1424 assq_no_quit (key, list)
1425 Lisp_Object key, list;
1427 while (CONSP (list)
1428 && (!CONSP (XCAR (list))
1429 || !EQ (XCAR (XCAR (list)), key)))
1430 list = XCDR (list);
1432 return CONSP (list) ? XCAR (list) : Qnil;
1435 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1436 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1437 The value is actually the element of LIST whose car equals KEY. */)
1438 (key, list)
1439 Lisp_Object key, list;
1441 Lisp_Object result, car;
1443 while (1)
1445 if (!CONSP (list)
1446 || (CONSP (XCAR (list))
1447 && (car = XCAR (XCAR (list)),
1448 EQ (car, key) || !NILP (Fequal (car, key)))))
1449 break;
1451 list = XCDR (list);
1452 if (!CONSP (list)
1453 || (CONSP (XCAR (list))
1454 && (car = XCAR (XCAR (list)),
1455 EQ (car, key) || !NILP (Fequal (car, key)))))
1456 break;
1458 list = XCDR (list);
1459 if (!CONSP (list)
1460 || (CONSP (XCAR (list))
1461 && (car = XCAR (XCAR (list)),
1462 EQ (car, key) || !NILP (Fequal (car, key)))))
1463 break;
1465 list = XCDR (list);
1466 QUIT;
1469 if (CONSP (list))
1470 result = XCAR (list);
1471 else if (NILP (list))
1472 result = Qnil;
1473 else
1474 result = wrong_type_argument (Qlistp, list);
1476 return result;
1479 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1480 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1481 The value is actually the element of LIST whose cdr is KEY. */)
1482 (key, list)
1483 register Lisp_Object key;
1484 Lisp_Object list;
1486 Lisp_Object result;
1488 while (1)
1490 if (!CONSP (list)
1491 || (CONSP (XCAR (list))
1492 && EQ (XCDR (XCAR (list)), key)))
1493 break;
1495 list = XCDR (list);
1496 if (!CONSP (list)
1497 || (CONSP (XCAR (list))
1498 && EQ (XCDR (XCAR (list)), key)))
1499 break;
1501 list = XCDR (list);
1502 if (!CONSP (list)
1503 || (CONSP (XCAR (list))
1504 && EQ (XCDR (XCAR (list)), key)))
1505 break;
1507 list = XCDR (list);
1508 QUIT;
1511 if (NILP (list))
1512 result = Qnil;
1513 else if (CONSP (list))
1514 result = XCAR (list);
1515 else
1516 result = wrong_type_argument (Qlistp, list);
1518 return result;
1521 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1522 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1523 The value is actually the element of LIST whose cdr equals KEY. */)
1524 (key, list)
1525 Lisp_Object key, list;
1527 Lisp_Object result, cdr;
1529 while (1)
1531 if (!CONSP (list)
1532 || (CONSP (XCAR (list))
1533 && (cdr = XCDR (XCAR (list)),
1534 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1535 break;
1537 list = XCDR (list);
1538 if (!CONSP (list)
1539 || (CONSP (XCAR (list))
1540 && (cdr = XCDR (XCAR (list)),
1541 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1542 break;
1544 list = XCDR (list);
1545 if (!CONSP (list)
1546 || (CONSP (XCAR (list))
1547 && (cdr = XCDR (XCAR (list)),
1548 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1549 break;
1551 list = XCDR (list);
1552 QUIT;
1555 if (CONSP (list))
1556 result = XCAR (list);
1557 else if (NILP (list))
1558 result = Qnil;
1559 else
1560 result = wrong_type_argument (Qlistp, list);
1562 return result;
1565 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1566 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1567 The modified LIST is returned. Comparison is done with `eq'.
1568 If the first member of LIST is ELT, there is no way to remove it by side effect;
1569 therefore, write `(setq foo (delq element foo))'
1570 to be sure of changing the value of `foo'. */)
1571 (elt, list)
1572 register Lisp_Object elt;
1573 Lisp_Object list;
1575 register Lisp_Object tail, prev;
1576 register Lisp_Object tem;
1578 tail = list;
1579 prev = Qnil;
1580 while (!NILP (tail))
1582 if (! CONSP (tail))
1583 wrong_type_argument (Qlistp, list);
1584 tem = XCAR (tail);
1585 if (EQ (elt, tem))
1587 if (NILP (prev))
1588 list = XCDR (tail);
1589 else
1590 Fsetcdr (prev, XCDR (tail));
1592 else
1593 prev = tail;
1594 tail = XCDR (tail);
1595 QUIT;
1597 return list;
1600 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1601 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1602 SEQ must be a list, a vector, or a string.
1603 The modified SEQ is returned. Comparison is done with `equal'.
1604 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1605 is not a side effect; it is simply using a different sequence.
1606 Therefore, write `(setq foo (delete element foo))'
1607 to be sure of changing the value of `foo'. */)
1608 (elt, seq)
1609 Lisp_Object elt, seq;
1611 if (VECTORP (seq))
1613 EMACS_INT i, n;
1615 for (i = n = 0; i < ASIZE (seq); ++i)
1616 if (NILP (Fequal (AREF (seq, i), elt)))
1617 ++n;
1619 if (n != ASIZE (seq))
1621 struct Lisp_Vector *p = allocate_vector (n);
1623 for (i = n = 0; i < ASIZE (seq); ++i)
1624 if (NILP (Fequal (AREF (seq, i), elt)))
1625 p->contents[n++] = AREF (seq, i);
1627 XSETVECTOR (seq, p);
1630 else if (STRINGP (seq))
1632 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1633 int c;
1635 for (i = nchars = nbytes = ibyte = 0;
1636 i < XSTRING (seq)->size;
1637 ++i, ibyte += cbytes)
1639 if (STRING_MULTIBYTE (seq))
1641 c = STRING_CHAR (&XSTRING (seq)->data[ibyte],
1642 STRING_BYTES (XSTRING (seq)) - ibyte);
1643 cbytes = CHAR_BYTES (c);
1645 else
1647 c = XSTRING (seq)->data[i];
1648 cbytes = 1;
1651 if (!INTEGERP (elt) || c != XINT (elt))
1653 ++nchars;
1654 nbytes += cbytes;
1658 if (nchars != XSTRING (seq)->size)
1660 Lisp_Object tem;
1662 tem = make_uninit_multibyte_string (nchars, nbytes);
1663 if (!STRING_MULTIBYTE (seq))
1664 SET_STRING_BYTES (XSTRING (tem), -1);
1666 for (i = nchars = nbytes = ibyte = 0;
1667 i < XSTRING (seq)->size;
1668 ++i, ibyte += cbytes)
1670 if (STRING_MULTIBYTE (seq))
1672 c = STRING_CHAR (&XSTRING (seq)->data[ibyte],
1673 STRING_BYTES (XSTRING (seq)) - ibyte);
1674 cbytes = CHAR_BYTES (c);
1676 else
1678 c = XSTRING (seq)->data[i];
1679 cbytes = 1;
1682 if (!INTEGERP (elt) || c != XINT (elt))
1684 unsigned char *from = &XSTRING (seq)->data[ibyte];
1685 unsigned char *to = &XSTRING (tem)->data[nbytes];
1686 EMACS_INT n;
1688 ++nchars;
1689 nbytes += cbytes;
1691 for (n = cbytes; n--; )
1692 *to++ = *from++;
1696 seq = tem;
1699 else
1701 Lisp_Object tail, prev;
1703 for (tail = seq, prev = Qnil; !NILP (tail); tail = XCDR (tail))
1705 if (!CONSP (tail))
1706 wrong_type_argument (Qlistp, seq);
1708 if (!NILP (Fequal (elt, XCAR (tail))))
1710 if (NILP (prev))
1711 seq = XCDR (tail);
1712 else
1713 Fsetcdr (prev, XCDR (tail));
1715 else
1716 prev = tail;
1717 QUIT;
1721 return seq;
1724 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1725 doc: /* Reverse LIST by modifying cdr pointers.
1726 Returns the beginning of the reversed list. */)
1727 (list)
1728 Lisp_Object list;
1730 register Lisp_Object prev, tail, next;
1732 if (NILP (list)) return list;
1733 prev = Qnil;
1734 tail = list;
1735 while (!NILP (tail))
1737 QUIT;
1738 if (! CONSP (tail))
1739 wrong_type_argument (Qlistp, list);
1740 next = XCDR (tail);
1741 Fsetcdr (tail, prev);
1742 prev = tail;
1743 tail = next;
1745 return prev;
1748 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1749 doc: /* Reverse LIST, copying. Returns the beginning of the reversed list.
1750 See also the function `nreverse', which is used more often. */)
1751 (list)
1752 Lisp_Object list;
1754 Lisp_Object new;
1756 for (new = Qnil; CONSP (list); list = XCDR (list))
1757 new = Fcons (XCAR (list), new);
1758 if (!NILP (list))
1759 wrong_type_argument (Qconsp, list);
1760 return new;
1763 Lisp_Object merge ();
1765 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1766 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1767 Returns the sorted list. LIST is modified by side effects.
1768 PREDICATE is called with two elements of LIST, and should return t
1769 if the first element is "less" than the second. */)
1770 (list, predicate)
1771 Lisp_Object list, predicate;
1773 Lisp_Object front, back;
1774 register Lisp_Object len, tem;
1775 struct gcpro gcpro1, gcpro2;
1776 register int length;
1778 front = list;
1779 len = Flength (list);
1780 length = XINT (len);
1781 if (length < 2)
1782 return list;
1784 XSETINT (len, (length / 2) - 1);
1785 tem = Fnthcdr (len, list);
1786 back = Fcdr (tem);
1787 Fsetcdr (tem, Qnil);
1789 GCPRO2 (front, back);
1790 front = Fsort (front, predicate);
1791 back = Fsort (back, predicate);
1792 UNGCPRO;
1793 return merge (front, back, predicate);
1796 Lisp_Object
1797 merge (org_l1, org_l2, pred)
1798 Lisp_Object org_l1, org_l2;
1799 Lisp_Object pred;
1801 Lisp_Object value;
1802 register Lisp_Object tail;
1803 Lisp_Object tem;
1804 register Lisp_Object l1, l2;
1805 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1807 l1 = org_l1;
1808 l2 = org_l2;
1809 tail = Qnil;
1810 value = Qnil;
1812 /* It is sufficient to protect org_l1 and org_l2.
1813 When l1 and l2 are updated, we copy the new values
1814 back into the org_ vars. */
1815 GCPRO4 (org_l1, org_l2, pred, value);
1817 while (1)
1819 if (NILP (l1))
1821 UNGCPRO;
1822 if (NILP (tail))
1823 return l2;
1824 Fsetcdr (tail, l2);
1825 return value;
1827 if (NILP (l2))
1829 UNGCPRO;
1830 if (NILP (tail))
1831 return l1;
1832 Fsetcdr (tail, l1);
1833 return value;
1835 tem = call2 (pred, Fcar (l2), Fcar (l1));
1836 if (NILP (tem))
1838 tem = l1;
1839 l1 = Fcdr (l1);
1840 org_l1 = l1;
1842 else
1844 tem = l2;
1845 l2 = Fcdr (l2);
1846 org_l2 = l2;
1848 if (NILP (tail))
1849 value = tem;
1850 else
1851 Fsetcdr (tail, tem);
1852 tail = tem;
1857 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1858 doc: /* Extract a value from a property list.
1859 PLIST is a property list, which is a list of the form
1860 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1861 corresponding to the given PROP, or nil if PROP is not
1862 one of the properties on the list. */)
1863 (plist, prop)
1864 Lisp_Object plist;
1865 Lisp_Object prop;
1867 Lisp_Object tail;
1869 for (tail = plist;
1870 CONSP (tail) && CONSP (XCDR (tail));
1871 tail = XCDR (XCDR (tail)))
1873 if (EQ (prop, XCAR (tail)))
1874 return XCAR (XCDR (tail));
1876 /* This function can be called asynchronously
1877 (setup_coding_system). Don't QUIT in that case. */
1878 if (!interrupt_input_blocked)
1879 QUIT;
1882 if (!NILP (tail))
1883 wrong_type_argument (Qlistp, prop);
1885 return Qnil;
1888 DEFUN ("get", Fget, Sget, 2, 2, 0,
1889 doc: /* Return the value of SYMBOL's PROPNAME property.
1890 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1891 (symbol, propname)
1892 Lisp_Object symbol, propname;
1894 CHECK_SYMBOL (symbol, 0);
1895 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1898 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1899 doc: /* Change value in PLIST of PROP to VAL.
1900 PLIST is a property list, which is a list of the form
1901 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1902 If PROP is already a property on the list, its value is set to VAL,
1903 otherwise the new PROP VAL pair is added. The new plist is returned;
1904 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1905 The PLIST is modified by side effects. */)
1906 (plist, prop, val)
1907 Lisp_Object plist;
1908 register Lisp_Object prop;
1909 Lisp_Object val;
1911 register Lisp_Object tail, prev;
1912 Lisp_Object newcell;
1913 prev = Qnil;
1914 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1915 tail = XCDR (XCDR (tail)))
1917 if (EQ (prop, XCAR (tail)))
1919 Fsetcar (XCDR (tail), val);
1920 return plist;
1923 prev = tail;
1924 QUIT;
1926 newcell = Fcons (prop, Fcons (val, Qnil));
1927 if (NILP (prev))
1928 return newcell;
1929 else
1930 Fsetcdr (XCDR (prev), newcell);
1931 return plist;
1934 DEFUN ("put", Fput, Sput, 3, 3, 0,
1935 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
1936 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
1937 (symbol, propname, value)
1938 Lisp_Object symbol, propname, value;
1940 CHECK_SYMBOL (symbol, 0);
1941 XSYMBOL (symbol)->plist
1942 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
1943 return value;
1946 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
1947 doc: /* Return t if two Lisp objects have similar structure and contents.
1948 They must have the same data type.
1949 Conses are compared by comparing the cars and the cdrs.
1950 Vectors and strings are compared element by element.
1951 Numbers are compared by value, but integers cannot equal floats.
1952 (Use `=' if you want integers and floats to be able to be equal.)
1953 Symbols must match exactly. */)
1954 (o1, o2)
1955 register Lisp_Object o1, o2;
1957 return internal_equal (o1, o2, 0) ? Qt : Qnil;
1960 static int
1961 internal_equal (o1, o2, depth)
1962 register Lisp_Object o1, o2;
1963 int depth;
1965 if (depth > 200)
1966 error ("Stack overflow in equal");
1968 tail_recurse:
1969 QUIT;
1970 if (EQ (o1, o2))
1971 return 1;
1972 if (XTYPE (o1) != XTYPE (o2))
1973 return 0;
1975 switch (XTYPE (o1))
1977 case Lisp_Float:
1978 return (extract_float (o1) == extract_float (o2));
1980 case Lisp_Cons:
1981 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1))
1982 return 0;
1983 o1 = XCDR (o1);
1984 o2 = XCDR (o2);
1985 goto tail_recurse;
1987 case Lisp_Misc:
1988 if (XMISCTYPE (o1) != XMISCTYPE (o2))
1989 return 0;
1990 if (OVERLAYP (o1))
1992 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
1993 depth + 1)
1994 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
1995 depth + 1))
1996 return 0;
1997 o1 = XOVERLAY (o1)->plist;
1998 o2 = XOVERLAY (o2)->plist;
1999 goto tail_recurse;
2001 if (MARKERP (o1))
2003 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2004 && (XMARKER (o1)->buffer == 0
2005 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2007 break;
2009 case Lisp_Vectorlike:
2011 register int i, size;
2012 size = XVECTOR (o1)->size;
2013 /* Pseudovectors have the type encoded in the size field, so this test
2014 actually checks that the objects have the same type as well as the
2015 same size. */
2016 if (XVECTOR (o2)->size != size)
2017 return 0;
2018 /* Boolvectors are compared much like strings. */
2019 if (BOOL_VECTOR_P (o1))
2021 int size_in_chars
2022 = (XBOOL_VECTOR (o1)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
2024 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2025 return 0;
2026 if (bcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2027 size_in_chars))
2028 return 0;
2029 return 1;
2031 if (WINDOW_CONFIGURATIONP (o1))
2032 return compare_window_configurations (o1, o2, 0);
2034 /* Aside from them, only true vectors, char-tables, and compiled
2035 functions are sensible to compare, so eliminate the others now. */
2036 if (size & PSEUDOVECTOR_FLAG)
2038 if (!(size & (PVEC_COMPILED | PVEC_CHAR_TABLE)))
2039 return 0;
2040 size &= PSEUDOVECTOR_SIZE_MASK;
2042 for (i = 0; i < size; i++)
2044 Lisp_Object v1, v2;
2045 v1 = XVECTOR (o1)->contents [i];
2046 v2 = XVECTOR (o2)->contents [i];
2047 if (!internal_equal (v1, v2, depth + 1))
2048 return 0;
2050 return 1;
2052 break;
2054 case Lisp_String:
2055 if (XSTRING (o1)->size != XSTRING (o2)->size)
2056 return 0;
2057 if (STRING_BYTES (XSTRING (o1)) != STRING_BYTES (XSTRING (o2)))
2058 return 0;
2059 if (bcmp (XSTRING (o1)->data, XSTRING (o2)->data,
2060 STRING_BYTES (XSTRING (o1))))
2061 return 0;
2062 return 1;
2064 case Lisp_Int:
2065 case Lisp_Symbol:
2066 case Lisp_Type_Limit:
2067 break;
2070 return 0;
2073 extern Lisp_Object Fmake_char_internal ();
2075 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2076 doc: /* Store each element of ARRAY with ITEM.
2077 ARRAY is a vector, string, char-table, or bool-vector. */)
2078 (array, item)
2079 Lisp_Object array, item;
2081 register int size, index, charval;
2082 retry:
2083 if (VECTORP (array))
2085 register Lisp_Object *p = XVECTOR (array)->contents;
2086 size = XVECTOR (array)->size;
2087 for (index = 0; index < size; index++)
2088 p[index] = item;
2090 else if (CHAR_TABLE_P (array))
2092 register Lisp_Object *p = XCHAR_TABLE (array)->contents;
2093 size = CHAR_TABLE_ORDINARY_SLOTS;
2094 for (index = 0; index < size; index++)
2095 p[index] = item;
2096 XCHAR_TABLE (array)->defalt = Qnil;
2098 else if (STRINGP (array))
2100 register unsigned char *p = XSTRING (array)->data;
2101 CHECK_NUMBER (item, 1);
2102 charval = XINT (item);
2103 size = XSTRING (array)->size;
2104 if (STRING_MULTIBYTE (array))
2106 unsigned char str[MAX_MULTIBYTE_LENGTH];
2107 int len = CHAR_STRING (charval, str);
2108 int size_byte = STRING_BYTES (XSTRING (array));
2109 unsigned char *p1 = p, *endp = p + size_byte;
2110 int i;
2112 if (size != size_byte)
2113 while (p1 < endp)
2115 int this_len = MULTIBYTE_FORM_LENGTH (p1, endp - p1);
2116 if (len != this_len)
2117 error ("Attempt to change byte length of a string");
2118 p1 += this_len;
2120 for (i = 0; i < size_byte; i++)
2121 *p++ = str[i % len];
2123 else
2124 for (index = 0; index < size; index++)
2125 p[index] = charval;
2127 else if (BOOL_VECTOR_P (array))
2129 register unsigned char *p = XBOOL_VECTOR (array)->data;
2130 int size_in_chars
2131 = (XBOOL_VECTOR (array)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
2133 charval = (! NILP (item) ? -1 : 0);
2134 for (index = 0; index < size_in_chars; index++)
2135 p[index] = charval;
2137 else
2139 array = wrong_type_argument (Qarrayp, array);
2140 goto retry;
2142 return array;
2145 DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
2146 1, 1, 0,
2147 doc: /* Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
2148 (char_table)
2149 Lisp_Object char_table;
2151 CHECK_CHAR_TABLE (char_table, 0);
2153 return XCHAR_TABLE (char_table)->purpose;
2156 DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
2157 1, 1, 0,
2158 doc: /* Return the parent char-table of CHAR-TABLE.
2159 The value is either nil or another char-table.
2160 If CHAR-TABLE holds nil for a given character,
2161 then the actual applicable value is inherited from the parent char-table
2162 \(or from its parents, if necessary). */)
2163 (char_table)
2164 Lisp_Object char_table;
2166 CHECK_CHAR_TABLE (char_table, 0);
2168 return XCHAR_TABLE (char_table)->parent;
2171 DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
2172 2, 2, 0,
2173 doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
2174 PARENT must be either nil or another char-table. */)
2175 (char_table, parent)
2176 Lisp_Object char_table, parent;
2178 Lisp_Object temp;
2180 CHECK_CHAR_TABLE (char_table, 0);
2182 if (!NILP (parent))
2184 CHECK_CHAR_TABLE (parent, 0);
2186 for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
2187 if (EQ (temp, char_table))
2188 error ("Attempt to make a chartable be its own parent");
2191 XCHAR_TABLE (char_table)->parent = parent;
2193 return parent;
2196 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
2197 2, 2, 0,
2198 doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
2199 (char_table, n)
2200 Lisp_Object char_table, n;
2202 CHECK_CHAR_TABLE (char_table, 1);
2203 CHECK_NUMBER (n, 2);
2204 if (XINT (n) < 0
2205 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
2206 args_out_of_range (char_table, n);
2208 return XCHAR_TABLE (char_table)->extras[XINT (n)];
2211 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
2212 Sset_char_table_extra_slot,
2213 3, 3, 0,
2214 doc: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
2215 (char_table, n, value)
2216 Lisp_Object char_table, n, value;
2218 CHECK_CHAR_TABLE (char_table, 1);
2219 CHECK_NUMBER (n, 2);
2220 if (XINT (n) < 0
2221 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
2222 args_out_of_range (char_table, n);
2224 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
2227 DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
2228 2, 2, 0,
2229 doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
2230 RANGE should be nil (for the default value)
2231 a vector which identifies a character set or a row of a character set,
2232 a character set name, or a character code. */)
2233 (char_table, range)
2234 Lisp_Object char_table, range;
2236 CHECK_CHAR_TABLE (char_table, 0);
2238 if (EQ (range, Qnil))
2239 return XCHAR_TABLE (char_table)->defalt;
2240 else if (INTEGERP (range))
2241 return Faref (char_table, range);
2242 else if (SYMBOLP (range))
2244 Lisp_Object charset_info;
2246 charset_info = Fget (range, Qcharset);
2247 CHECK_VECTOR (charset_info, 0);
2249 return Faref (char_table,
2250 make_number (XINT (XVECTOR (charset_info)->contents[0])
2251 + 128));
2253 else if (VECTORP (range))
2255 if (XVECTOR (range)->size == 1)
2256 return Faref (char_table,
2257 make_number (XINT (XVECTOR (range)->contents[0]) + 128));
2258 else
2260 int size = XVECTOR (range)->size;
2261 Lisp_Object *val = XVECTOR (range)->contents;
2262 Lisp_Object ch = Fmake_char_internal (size <= 0 ? Qnil : val[0],
2263 size <= 1 ? Qnil : val[1],
2264 size <= 2 ? Qnil : val[2]);
2265 return Faref (char_table, ch);
2268 else
2269 error ("Invalid RANGE argument to `char-table-range'");
2270 return Qt;
2273 DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
2274 3, 3, 0,
2275 doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
2276 RANGE should be t (for all characters), nil (for the default value)
2277 a vector which identifies a character set or a row of a character set,
2278 a coding system, or a character code. */)
2279 (char_table, range, value)
2280 Lisp_Object char_table, range, value;
2282 int i;
2284 CHECK_CHAR_TABLE (char_table, 0);
2286 if (EQ (range, Qt))
2287 for (i = 0; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2288 XCHAR_TABLE (char_table)->contents[i] = value;
2289 else if (EQ (range, Qnil))
2290 XCHAR_TABLE (char_table)->defalt = value;
2291 else if (SYMBOLP (range))
2293 Lisp_Object charset_info;
2295 charset_info = Fget (range, Qcharset);
2296 CHECK_VECTOR (charset_info, 0);
2298 return Faset (char_table,
2299 make_number (XINT (XVECTOR (charset_info)->contents[0])
2300 + 128),
2301 value);
2303 else if (INTEGERP (range))
2304 Faset (char_table, range, value);
2305 else if (VECTORP (range))
2307 if (XVECTOR (range)->size == 1)
2308 return Faset (char_table,
2309 make_number (XINT (XVECTOR (range)->contents[0]) + 128),
2310 value);
2311 else
2313 int size = XVECTOR (range)->size;
2314 Lisp_Object *val = XVECTOR (range)->contents;
2315 Lisp_Object ch = Fmake_char_internal (size <= 0 ? Qnil : val[0],
2316 size <= 1 ? Qnil : val[1],
2317 size <= 2 ? Qnil : val[2]);
2318 return Faset (char_table, ch, value);
2321 else
2322 error ("Invalid RANGE argument to `set-char-table-range'");
2324 return value;
2327 DEFUN ("set-char-table-default", Fset_char_table_default,
2328 Sset_char_table_default, 3, 3, 0,
2329 doc: /* Set the default value in CHAR-TABLE for a generic character CHAR to VALUE.
2330 The generic character specifies the group of characters.
2331 See also the documentation of make-char. */)
2332 (char_table, ch, value)
2333 Lisp_Object char_table, ch, value;
2335 int c, charset, code1, code2;
2336 Lisp_Object temp;
2338 CHECK_CHAR_TABLE (char_table, 0);
2339 CHECK_NUMBER (ch, 1);
2341 c = XINT (ch);
2342 SPLIT_CHAR (c, charset, code1, code2);
2344 /* Since we may want to set the default value for a character set
2345 not yet defined, we check only if the character set is in the
2346 valid range or not, instead of it is already defined or not. */
2347 if (! CHARSET_VALID_P (charset))
2348 invalid_character (c);
2350 if (charset == CHARSET_ASCII)
2351 return (XCHAR_TABLE (char_table)->defalt = value);
2353 /* Even if C is not a generic char, we had better behave as if a
2354 generic char is specified. */
2355 if (!CHARSET_DEFINED_P (charset) || CHARSET_DIMENSION (charset) == 1)
2356 code1 = 0;
2357 temp = XCHAR_TABLE (char_table)->contents[charset + 128];
2358 if (!code1)
2360 if (SUB_CHAR_TABLE_P (temp))
2361 XCHAR_TABLE (temp)->defalt = value;
2362 else
2363 XCHAR_TABLE (char_table)->contents[charset + 128] = value;
2364 return value;
2366 if (SUB_CHAR_TABLE_P (temp))
2367 char_table = temp;
2368 else
2369 char_table = (XCHAR_TABLE (char_table)->contents[charset + 128]
2370 = make_sub_char_table (temp));
2371 temp = XCHAR_TABLE (char_table)->contents[code1];
2372 if (SUB_CHAR_TABLE_P (temp))
2373 XCHAR_TABLE (temp)->defalt = value;
2374 else
2375 XCHAR_TABLE (char_table)->contents[code1] = value;
2376 return value;
2379 /* Look up the element in TABLE at index CH,
2380 and return it as an integer.
2381 If the element is nil, return CH itself.
2382 (Actually we do that for any non-integer.) */
2385 char_table_translate (table, ch)
2386 Lisp_Object table;
2387 int ch;
2389 Lisp_Object value;
2390 value = Faref (table, make_number (ch));
2391 if (! INTEGERP (value))
2392 return ch;
2393 return XINT (value);
2396 static void
2397 optimize_sub_char_table (table, chars)
2398 Lisp_Object *table;
2399 int chars;
2401 Lisp_Object elt;
2402 int from, to;
2404 if (chars == 94)
2405 from = 33, to = 127;
2406 else
2407 from = 32, to = 128;
2409 if (!SUB_CHAR_TABLE_P (*table))
2410 return;
2411 elt = XCHAR_TABLE (*table)->contents[from++];
2412 for (; from < to; from++)
2413 if (NILP (Fequal (elt, XCHAR_TABLE (*table)->contents[from])))
2414 return;
2415 *table = elt;
2418 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
2419 1, 1, 0, doc: /* Optimize char table TABLE. */)
2420 (table)
2421 Lisp_Object table;
2423 Lisp_Object elt;
2424 int dim;
2425 int i, j;
2427 CHECK_CHAR_TABLE (table, 0);
2429 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2431 elt = XCHAR_TABLE (table)->contents[i];
2432 if (!SUB_CHAR_TABLE_P (elt))
2433 continue;
2434 dim = CHARSET_DIMENSION (i - 128);
2435 if (dim == 2)
2436 for (j = 32; j < SUB_CHAR_TABLE_ORDINARY_SLOTS; j++)
2437 optimize_sub_char_table (XCHAR_TABLE (elt)->contents + j, dim);
2438 optimize_sub_char_table (XCHAR_TABLE (table)->contents + i, dim);
2440 return Qnil;
2444 /* Map C_FUNCTION or FUNCTION over SUBTABLE, calling it for each
2445 character or group of characters that share a value.
2446 DEPTH is the current depth in the originally specified
2447 chartable, and INDICES contains the vector indices
2448 for the levels our callers have descended.
2450 ARG is passed to C_FUNCTION when that is called. */
2452 void
2453 map_char_table (c_function, function, subtable, arg, depth, indices)
2454 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
2455 Lisp_Object function, subtable, arg, *indices;
2456 int depth;
2458 int i, to;
2460 if (depth == 0)
2462 /* At first, handle ASCII and 8-bit European characters. */
2463 for (i = 0; i < CHAR_TABLE_SINGLE_BYTE_SLOTS; i++)
2465 Lisp_Object elt = XCHAR_TABLE (subtable)->contents[i];
2466 if (c_function)
2467 (*c_function) (arg, make_number (i), elt);
2468 else
2469 call2 (function, make_number (i), elt);
2471 #if 0 /* If the char table has entries for higher characters,
2472 we should report them. */
2473 if (NILP (current_buffer->enable_multibyte_characters))
2474 return;
2475 #endif
2476 to = CHAR_TABLE_ORDINARY_SLOTS;
2478 else
2480 int charset = XFASTINT (indices[0]) - 128;
2482 i = 32;
2483 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
2484 if (CHARSET_CHARS (charset) == 94)
2485 i++, to--;
2488 for (; i < to; i++)
2490 Lisp_Object elt;
2491 int charset;
2493 elt = XCHAR_TABLE (subtable)->contents[i];
2494 XSETFASTINT (indices[depth], i);
2495 charset = XFASTINT (indices[0]) - 128;
2496 if (depth == 0
2497 && (!CHARSET_DEFINED_P (charset)
2498 || charset == CHARSET_8_BIT_CONTROL
2499 || charset == CHARSET_8_BIT_GRAPHIC))
2500 continue;
2502 if (SUB_CHAR_TABLE_P (elt))
2504 if (depth >= 3)
2505 error ("Too deep char table");
2506 map_char_table (c_function, function, elt, arg, depth + 1, indices);
2508 else
2510 int c1, c2, c;
2512 if (NILP (elt))
2513 elt = XCHAR_TABLE (subtable)->defalt;
2514 c1 = depth >= 1 ? XFASTINT (indices[1]) : 0;
2515 c2 = depth >= 2 ? XFASTINT (indices[2]) : 0;
2516 c = MAKE_CHAR (charset, c1, c2);
2517 if (c_function)
2518 (*c_function) (arg, make_number (c), elt);
2519 else
2520 call2 (function, make_number (c), elt);
2525 DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
2526 2, 2, 0,
2527 doc: /* Call FUNCTION for each (normal and generic) characters in CHAR-TABLE.
2528 FUNCTION is called with two arguments--a key and a value.
2529 The key is always a possible IDX argument to `aref'. */)
2530 (function, char_table)
2531 Lisp_Object function, char_table;
2533 /* The depth of char table is at most 3. */
2534 Lisp_Object indices[3];
2536 CHECK_CHAR_TABLE (char_table, 1);
2538 map_char_table (NULL, function, char_table, char_table, 0, indices);
2539 return Qnil;
2542 /* Return a value for character C in char-table TABLE. Store the
2543 actual index for that value in *IDX. Ignore the default value of
2544 TABLE. */
2546 Lisp_Object
2547 char_table_ref_and_index (table, c, idx)
2548 Lisp_Object table;
2549 int c, *idx;
2551 int charset, c1, c2;
2552 Lisp_Object elt;
2554 if (SINGLE_BYTE_CHAR_P (c))
2556 *idx = c;
2557 return XCHAR_TABLE (table)->contents[c];
2559 SPLIT_CHAR (c, charset, c1, c2);
2560 elt = XCHAR_TABLE (table)->contents[charset + 128];
2561 *idx = MAKE_CHAR (charset, 0, 0);
2562 if (!SUB_CHAR_TABLE_P (elt))
2563 return elt;
2564 if (c1 < 32 || NILP (XCHAR_TABLE (elt)->contents[c1]))
2565 return XCHAR_TABLE (elt)->defalt;
2566 elt = XCHAR_TABLE (elt)->contents[c1];
2567 *idx = MAKE_CHAR (charset, c1, 0);
2568 if (!SUB_CHAR_TABLE_P (elt))
2569 return elt;
2570 if (c2 < 32 || NILP (XCHAR_TABLE (elt)->contents[c2]))
2571 return XCHAR_TABLE (elt)->defalt;
2572 *idx = c;
2573 return XCHAR_TABLE (elt)->contents[c2];
2577 /* ARGSUSED */
2578 Lisp_Object
2579 nconc2 (s1, s2)
2580 Lisp_Object s1, s2;
2582 #ifdef NO_ARG_ARRAY
2583 Lisp_Object args[2];
2584 args[0] = s1;
2585 args[1] = s2;
2586 return Fnconc (2, args);
2587 #else
2588 return Fnconc (2, &s1);
2589 #endif /* NO_ARG_ARRAY */
2592 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2593 doc: /* Concatenate any number of lists by altering them.
2594 Only the last argument is not altered, and need not be a list.
2595 usage: (nconc &rest LISTS) */)
2596 (nargs, args)
2597 int nargs;
2598 Lisp_Object *args;
2600 register int argnum;
2601 register Lisp_Object tail, tem, val;
2603 val = tail = Qnil;
2605 for (argnum = 0; argnum < nargs; argnum++)
2607 tem = args[argnum];
2608 if (NILP (tem)) continue;
2610 if (NILP (val))
2611 val = tem;
2613 if (argnum + 1 == nargs) break;
2615 if (!CONSP (tem))
2616 tem = wrong_type_argument (Qlistp, tem);
2618 while (CONSP (tem))
2620 tail = tem;
2621 tem = Fcdr (tail);
2622 QUIT;
2625 tem = args[argnum + 1];
2626 Fsetcdr (tail, tem);
2627 if (NILP (tem))
2628 args[argnum + 1] = tail;
2631 return val;
2634 /* This is the guts of all mapping functions.
2635 Apply FN to each element of SEQ, one by one,
2636 storing the results into elements of VALS, a C vector of Lisp_Objects.
2637 LENI is the length of VALS, which should also be the length of SEQ. */
2639 static void
2640 mapcar1 (leni, vals, fn, seq)
2641 int leni;
2642 Lisp_Object *vals;
2643 Lisp_Object fn, seq;
2645 register Lisp_Object tail;
2646 Lisp_Object dummy;
2647 register int i;
2648 struct gcpro gcpro1, gcpro2, gcpro3;
2650 if (vals)
2652 /* Don't let vals contain any garbage when GC happens. */
2653 for (i = 0; i < leni; i++)
2654 vals[i] = Qnil;
2656 GCPRO3 (dummy, fn, seq);
2657 gcpro1.var = vals;
2658 gcpro1.nvars = leni;
2660 else
2661 GCPRO2 (fn, seq);
2662 /* We need not explicitly protect `tail' because it is used only on lists, and
2663 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */
2665 if (VECTORP (seq))
2667 for (i = 0; i < leni; i++)
2669 dummy = XVECTOR (seq)->contents[i];
2670 dummy = call1 (fn, dummy);
2671 if (vals)
2672 vals[i] = dummy;
2675 else if (BOOL_VECTOR_P (seq))
2677 for (i = 0; i < leni; i++)
2679 int byte;
2680 byte = XBOOL_VECTOR (seq)->data[i / BITS_PER_CHAR];
2681 if (byte & (1 << (i % BITS_PER_CHAR)))
2682 dummy = Qt;
2683 else
2684 dummy = Qnil;
2686 dummy = call1 (fn, dummy);
2687 if (vals)
2688 vals[i] = dummy;
2691 else if (STRINGP (seq))
2693 int i_byte;
2695 for (i = 0, i_byte = 0; i < leni;)
2697 int c;
2698 int i_before = i;
2700 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2701 XSETFASTINT (dummy, c);
2702 dummy = call1 (fn, dummy);
2703 if (vals)
2704 vals[i_before] = dummy;
2707 else /* Must be a list, since Flength did not get an error */
2709 tail = seq;
2710 for (i = 0; i < leni; i++)
2712 dummy = call1 (fn, Fcar (tail));
2713 if (vals)
2714 vals[i] = dummy;
2715 tail = XCDR (tail);
2719 UNGCPRO;
2722 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2723 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2724 In between each pair of results, stick in SEPARATOR. Thus, " " as
2725 SEPARATOR results in spaces between the values returned by FUNCTION.
2726 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2727 (function, sequence, separator)
2728 Lisp_Object function, sequence, separator;
2730 Lisp_Object len;
2731 register int leni;
2732 int nargs;
2733 register Lisp_Object *args;
2734 register int i;
2735 struct gcpro gcpro1;
2737 len = Flength (sequence);
2738 leni = XINT (len);
2739 nargs = leni + leni - 1;
2740 if (nargs < 0) return build_string ("");
2742 args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object));
2744 GCPRO1 (separator);
2745 mapcar1 (leni, args, function, sequence);
2746 UNGCPRO;
2748 for (i = leni - 1; i >= 0; i--)
2749 args[i + i] = args[i];
2751 for (i = 1; i < nargs; i += 2)
2752 args[i] = separator;
2754 return Fconcat (nargs, args);
2757 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2758 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2759 The result is a list just as long as SEQUENCE.
2760 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2761 (function, sequence)
2762 Lisp_Object function, sequence;
2764 register Lisp_Object len;
2765 register int leni;
2766 register Lisp_Object *args;
2768 len = Flength (sequence);
2769 leni = XFASTINT (len);
2770 args = (Lisp_Object *) alloca (leni * sizeof (Lisp_Object));
2772 mapcar1 (leni, args, function, sequence);
2774 return Flist (leni, args);
2777 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2778 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2779 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2780 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2781 (function, sequence)
2782 Lisp_Object function, sequence;
2784 register int leni;
2786 leni = XFASTINT (Flength (sequence));
2787 mapcar1 (leni, 0, function, sequence);
2789 return sequence;
2792 /* Anything that calls this function must protect from GC! */
2794 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0,
2795 doc: /* Ask user a "y or n" question. Return t if answer is "y".
2796 Takes one argument, which is the string to display to ask the question.
2797 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2798 No confirmation of the answer is requested; a single character is enough.
2799 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2800 the bindings in `query-replace-map'; see the documentation of that variable
2801 for more information. In this case, the useful bindings are `act', `skip',
2802 `recenter', and `quit'.\)
2804 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2805 is nil and `use-dialog-box' is non-nil. */)
2806 (prompt)
2807 Lisp_Object prompt;
2809 register Lisp_Object obj, key, def, map;
2810 register int answer;
2811 Lisp_Object xprompt;
2812 Lisp_Object args[2];
2813 struct gcpro gcpro1, gcpro2;
2814 int count = specpdl_ptr - specpdl;
2816 specbind (Qcursor_in_echo_area, Qt);
2818 map = Fsymbol_value (intern ("query-replace-map"));
2820 CHECK_STRING (prompt, 0);
2821 xprompt = prompt;
2822 GCPRO2 (prompt, xprompt);
2824 #ifdef HAVE_X_WINDOWS
2825 if (display_hourglass_p)
2826 cancel_hourglass ();
2827 #endif
2829 while (1)
2832 #ifdef HAVE_MENUS
2833 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2834 && use_dialog_box
2835 && have_menus_p ())
2837 Lisp_Object pane, menu;
2838 redisplay_preserve_echo_area (3);
2839 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2840 Fcons (Fcons (build_string ("No"), Qnil),
2841 Qnil));
2842 menu = Fcons (prompt, pane);
2843 obj = Fx_popup_dialog (Qt, menu);
2844 answer = !NILP (obj);
2845 break;
2847 #endif /* HAVE_MENUS */
2848 cursor_in_echo_area = 1;
2849 choose_minibuf_frame ();
2850 message_with_string ("%s(y or n) ", xprompt, 0);
2852 if (minibuffer_auto_raise)
2854 Lisp_Object mini_frame;
2856 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
2858 Fraise_frame (mini_frame);
2861 obj = read_filtered_event (1, 0, 0, 0);
2862 cursor_in_echo_area = 0;
2863 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2864 QUIT;
2866 key = Fmake_vector (make_number (1), obj);
2867 def = Flookup_key (map, key, Qt);
2869 if (EQ (def, intern ("skip")))
2871 answer = 0;
2872 break;
2874 else if (EQ (def, intern ("act")))
2876 answer = 1;
2877 break;
2879 else if (EQ (def, intern ("recenter")))
2881 Frecenter (Qnil);
2882 xprompt = prompt;
2883 continue;
2885 else if (EQ (def, intern ("quit")))
2886 Vquit_flag = Qt;
2887 /* We want to exit this command for exit-prefix,
2888 and this is the only way to do it. */
2889 else if (EQ (def, intern ("exit-prefix")))
2890 Vquit_flag = Qt;
2892 QUIT;
2894 /* If we don't clear this, then the next call to read_char will
2895 return quit_char again, and we'll enter an infinite loop. */
2896 Vquit_flag = Qnil;
2898 Fding (Qnil);
2899 Fdiscard_input ();
2900 if (EQ (xprompt, prompt))
2902 args[0] = build_string ("Please answer y or n. ");
2903 args[1] = prompt;
2904 xprompt = Fconcat (2, args);
2907 UNGCPRO;
2909 if (! noninteractive)
2911 cursor_in_echo_area = -1;
2912 message_with_string (answer ? "%s(y or n) y" : "%s(y or n) n",
2913 xprompt, 0);
2916 unbind_to (count, Qnil);
2917 return answer ? Qt : Qnil;
2920 /* This is how C code calls `yes-or-no-p' and allows the user
2921 to redefined it.
2923 Anything that calls this function must protect from GC! */
2925 Lisp_Object
2926 do_yes_or_no_p (prompt)
2927 Lisp_Object prompt;
2929 return call1 (intern ("yes-or-no-p"), prompt);
2932 /* Anything that calls this function must protect from GC! */
2934 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2935 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2936 Takes one argument, which is the string to display to ask the question.
2937 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
2938 The user must confirm the answer with RET,
2939 and can edit it until it has been confirmed.
2941 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2942 is nil, and `use-dialog-box' is non-nil. */)
2943 (prompt)
2944 Lisp_Object prompt;
2946 register Lisp_Object ans;
2947 Lisp_Object args[2];
2948 struct gcpro gcpro1;
2950 CHECK_STRING (prompt, 0);
2952 #ifdef HAVE_MENUS
2953 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2954 && use_dialog_box
2955 && have_menus_p ())
2957 Lisp_Object pane, menu, obj;
2958 redisplay_preserve_echo_area (4);
2959 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2960 Fcons (Fcons (build_string ("No"), Qnil),
2961 Qnil));
2962 GCPRO1 (pane);
2963 menu = Fcons (prompt, pane);
2964 obj = Fx_popup_dialog (Qt, menu);
2965 UNGCPRO;
2966 return obj;
2968 #endif /* HAVE_MENUS */
2970 args[0] = prompt;
2971 args[1] = build_string ("(yes or no) ");
2972 prompt = Fconcat (2, args);
2974 GCPRO1 (prompt);
2976 while (1)
2978 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2979 Qyes_or_no_p_history, Qnil,
2980 Qnil));
2981 if (XSTRING (ans)->size == 3 && !strcmp (XSTRING (ans)->data, "yes"))
2983 UNGCPRO;
2984 return Qt;
2986 if (XSTRING (ans)->size == 2 && !strcmp (XSTRING (ans)->data, "no"))
2988 UNGCPRO;
2989 return Qnil;
2992 Fding (Qnil);
2993 Fdiscard_input ();
2994 message ("Please answer yes or no.");
2995 Fsleep_for (make_number (2), Qnil);
2999 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
3000 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
3002 Each of the three load averages is multiplied by 100, then converted
3003 to integer.
3005 When USE-FLOATS is non-nil, floats will be used instead of integers.
3006 These floats are not multiplied by 100.
3008 If the 5-minute or 15-minute load averages are not available, return a
3009 shortened list, containing only those averages which are available. */)
3010 (use_floats)
3011 Lisp_Object use_floats;
3013 double load_ave[3];
3014 int loads = getloadavg (load_ave, 3);
3015 Lisp_Object ret = Qnil;
3017 if (loads < 0)
3018 error ("load-average not implemented for this operating system");
3020 while (loads-- > 0)
3022 Lisp_Object load = (NILP (use_floats) ?
3023 make_number ((int) (100.0 * load_ave[loads]))
3024 : make_float (load_ave[loads]));
3025 ret = Fcons (load, ret);
3028 return ret;
3031 Lisp_Object Vfeatures, Qsubfeatures;
3032 extern Lisp_Object Vafter_load_alist;
3034 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
3035 doc: /* Returns t if FEATURE is present in this Emacs.
3037 Use this to conditionalize execution of lisp code based on the
3038 presence or absence of emacs or environment extensions.
3039 Use `provide' to declare that a feature is available. This function
3040 looks at the value of the variable `features'. The optional argument
3041 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
3042 (feature, subfeature)
3043 Lisp_Object feature, subfeature;
3045 register Lisp_Object tem;
3046 CHECK_SYMBOL (feature, 0);
3047 tem = Fmemq (feature, Vfeatures);
3048 if (!NILP (tem) && !NILP (subfeature))
3049 tem = Fmemq (subfeature, Fget (feature, Qsubfeatures));
3050 return (NILP (tem)) ? Qnil : Qt;
3053 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
3054 doc: /* Announce that FEATURE is a feature of the current Emacs.
3055 The optional argument SUBFEATURES should be a list of symbols listing
3056 particular subfeatures supported in this version of FEATURE. */)
3057 (feature, subfeatures)
3058 Lisp_Object feature, subfeatures;
3060 register Lisp_Object tem;
3061 CHECK_SYMBOL (feature, 0);
3062 if (!NILP (Vautoload_queue))
3063 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
3064 tem = Fmemq (feature, Vfeatures);
3065 if (NILP (tem))
3066 Vfeatures = Fcons (feature, Vfeatures);
3067 if (!NILP (subfeatures))
3068 Fput (feature, Qsubfeatures, subfeatures);
3069 LOADHIST_ATTACH (Fcons (Qprovide, feature));
3071 /* Run any load-hooks for this file. */
3072 tem = Fassq (feature, Vafter_load_alist);
3073 if (!NILP (tem))
3074 Fprogn (Fcdr (tem));
3076 return feature;
3079 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
3080 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
3081 If FEATURE is not a member of the list `features', then the feature
3082 is not loaded; so load the file FILENAME.
3083 If FILENAME is omitted, the printname of FEATURE is used as the file name,
3084 and `load' will try to load this name appended with the suffix `.elc',
3085 `.el' or the unmodified name, in that order.
3086 If the optional third argument NOERROR is non-nil,
3087 then return nil if the file is not found instead of signaling an error.
3088 Normally the return value is FEATURE.
3089 The normal messages at start and end of loading FILENAME are suppressed. */)
3090 (feature, filename, noerror)
3091 Lisp_Object feature, filename, noerror;
3093 register Lisp_Object tem;
3094 CHECK_SYMBOL (feature, 0);
3095 tem = Fmemq (feature, Vfeatures);
3097 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3099 if (NILP (tem))
3101 int count = specpdl_ptr - specpdl;
3103 /* Value saved here is to be restored into Vautoload_queue */
3104 record_unwind_protect (un_autoload, Vautoload_queue);
3105 Vautoload_queue = Qt;
3107 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
3108 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
3109 /* If load failed entirely, return nil. */
3110 if (NILP (tem))
3111 return unbind_to (count, Qnil);
3113 tem = Fmemq (feature, Vfeatures);
3114 if (NILP (tem))
3115 error ("Required feature %s was not provided",
3116 XSYMBOL (feature)->name->data);
3118 /* Once loading finishes, don't undo it. */
3119 Vautoload_queue = Qt;
3120 feature = unbind_to (count, feature);
3122 return feature;
3125 /* Primitives for work of the "widget" library.
3126 In an ideal world, this section would not have been necessary.
3127 However, lisp function calls being as slow as they are, it turns
3128 out that some functions in the widget library (wid-edit.el) are the
3129 bottleneck of Widget operation. Here is their translation to C,
3130 for the sole reason of efficiency. */
3132 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
3133 doc: /* Return non-nil if PLIST has the property PROP.
3134 PLIST is a property list, which is a list of the form
3135 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
3136 Unlike `plist-get', this allows you to distinguish between a missing
3137 property and a property with the value nil.
3138 The value is actually the tail of PLIST whose car is PROP. */)
3139 (plist, prop)
3140 Lisp_Object plist, prop;
3142 while (CONSP (plist) && !EQ (XCAR (plist), prop))
3144 QUIT;
3145 plist = XCDR (plist);
3146 plist = CDR (plist);
3148 return plist;
3151 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
3152 doc: /* In WIDGET, set PROPERTY to VALUE.
3153 The value can later be retrieved with `widget-get'. */)
3154 (widget, property, value)
3155 Lisp_Object widget, property, value;
3157 CHECK_CONS (widget, 1);
3158 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
3159 return value;
3162 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
3163 doc: /* In WIDGET, get the value of PROPERTY.
3164 The value could either be specified when the widget was created, or
3165 later with `widget-put'. */)
3166 (widget, property)
3167 Lisp_Object widget, property;
3169 Lisp_Object tmp;
3171 while (1)
3173 if (NILP (widget))
3174 return Qnil;
3175 CHECK_CONS (widget, 1);
3176 tmp = Fplist_member (XCDR (widget), property);
3177 if (CONSP (tmp))
3179 tmp = XCDR (tmp);
3180 return CAR (tmp);
3182 tmp = XCAR (widget);
3183 if (NILP (tmp))
3184 return Qnil;
3185 widget = Fget (tmp, Qwidget_type);
3189 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
3190 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3191 ARGS are passed as extra arguments to the function.
3192 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3193 (nargs, args)
3194 int nargs;
3195 Lisp_Object *args;
3197 /* This function can GC. */
3198 Lisp_Object newargs[3];
3199 struct gcpro gcpro1, gcpro2;
3200 Lisp_Object result;
3202 newargs[0] = Fwidget_get (args[0], args[1]);
3203 newargs[1] = args[0];
3204 newargs[2] = Flist (nargs - 2, args + 2);
3205 GCPRO2 (newargs[0], newargs[2]);
3206 result = Fapply (3, newargs);
3207 UNGCPRO;
3208 return result;
3211 /* base64 encode/decode functions (RFC 2045).
3212 Based on code from GNU recode. */
3214 #define MIME_LINE_LENGTH 76
3216 #define IS_ASCII(Character) \
3217 ((Character) < 128)
3218 #define IS_BASE64(Character) \
3219 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3220 #define IS_BASE64_IGNORABLE(Character) \
3221 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3222 || (Character) == '\f' || (Character) == '\r')
3224 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3225 character or return retval if there are no characters left to
3226 process. */
3227 #define READ_QUADRUPLET_BYTE(retval) \
3228 do \
3230 if (i == length) \
3232 if (nchars_return) \
3233 *nchars_return = nchars; \
3234 return (retval); \
3236 c = from[i++]; \
3238 while (IS_BASE64_IGNORABLE (c))
3240 /* Don't use alloca for regions larger than this, lest we overflow
3241 their stack. */
3242 #define MAX_ALLOCA 16*1024
3244 /* Table of characters coding the 64 values. */
3245 static char base64_value_to_char[64] =
3247 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3248 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3249 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3250 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3251 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3252 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3253 '8', '9', '+', '/' /* 60-63 */
3256 /* Table of base64 values for first 128 characters. */
3257 static short base64_char_to_value[128] =
3259 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3260 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3262 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3263 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3264 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3265 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3266 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3267 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3268 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3269 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3270 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3271 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3274 /* The following diagram shows the logical steps by which three octets
3275 get transformed into four base64 characters.
3277 .--------. .--------. .--------.
3278 |aaaaaabb| |bbbbcccc| |ccdddddd|
3279 `--------' `--------' `--------'
3280 6 2 4 4 2 6
3281 .--------+--------+--------+--------.
3282 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3283 `--------+--------+--------+--------'
3285 .--------+--------+--------+--------.
3286 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3287 `--------+--------+--------+--------'
3289 The octets are divided into 6 bit chunks, which are then encoded into
3290 base64 characters. */
3293 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3294 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3296 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3297 2, 3, "r",
3298 doc: /* Base64-encode the region between BEG and END.
3299 Return the length of the encoded text.
3300 Optional third argument NO-LINE-BREAK means do not break long lines
3301 into shorter lines. */)
3302 (beg, end, no_line_break)
3303 Lisp_Object beg, end, no_line_break;
3305 char *encoded;
3306 int allength, length;
3307 int ibeg, iend, encoded_length;
3308 int old_pos = PT;
3310 validate_region (&beg, &end);
3312 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3313 iend = CHAR_TO_BYTE (XFASTINT (end));
3314 move_gap_both (XFASTINT (beg), ibeg);
3316 /* We need to allocate enough room for encoding the text.
3317 We need 33 1/3% more space, plus a newline every 76
3318 characters, and then we round up. */
3319 length = iend - ibeg;
3320 allength = length + length/3 + 1;
3321 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3323 if (allength <= MAX_ALLOCA)
3324 encoded = (char *) alloca (allength);
3325 else
3326 encoded = (char *) xmalloc (allength);
3327 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3328 NILP (no_line_break),
3329 !NILP (current_buffer->enable_multibyte_characters));
3330 if (encoded_length > allength)
3331 abort ();
3333 if (encoded_length < 0)
3335 /* The encoding wasn't possible. */
3336 if (length > MAX_ALLOCA)
3337 xfree (encoded);
3338 error ("Multibyte character in data for base64 encoding");
3341 /* Now we have encoded the region, so we insert the new contents
3342 and delete the old. (Insert first in order to preserve markers.) */
3343 SET_PT_BOTH (XFASTINT (beg), ibeg);
3344 insert (encoded, encoded_length);
3345 if (allength > MAX_ALLOCA)
3346 xfree (encoded);
3347 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3349 /* If point was outside of the region, restore it exactly; else just
3350 move to the beginning of the region. */
3351 if (old_pos >= XFASTINT (end))
3352 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3353 else if (old_pos > XFASTINT (beg))
3354 old_pos = XFASTINT (beg);
3355 SET_PT (old_pos);
3357 /* We return the length of the encoded text. */
3358 return make_number (encoded_length);
3361 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3362 1, 2, 0,
3363 doc: /* Base64-encode STRING and return the result.
3364 Optional second argument NO-LINE-BREAK means do not break long lines
3365 into shorter lines. */)
3366 (string, no_line_break)
3367 Lisp_Object string, no_line_break;
3369 int allength, length, encoded_length;
3370 char *encoded;
3371 Lisp_Object encoded_string;
3373 CHECK_STRING (string, 1);
3375 /* We need to allocate enough room for encoding the text.
3376 We need 33 1/3% more space, plus a newline every 76
3377 characters, and then we round up. */
3378 length = STRING_BYTES (XSTRING (string));
3379 allength = length + length/3 + 1;
3380 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3382 /* We need to allocate enough room for decoding the text. */
3383 if (allength <= MAX_ALLOCA)
3384 encoded = (char *) alloca (allength);
3385 else
3386 encoded = (char *) xmalloc (allength);
3388 encoded_length = base64_encode_1 (XSTRING (string)->data,
3389 encoded, length, NILP (no_line_break),
3390 STRING_MULTIBYTE (string));
3391 if (encoded_length > allength)
3392 abort ();
3394 if (encoded_length < 0)
3396 /* The encoding wasn't possible. */
3397 if (length > MAX_ALLOCA)
3398 xfree (encoded);
3399 error ("Multibyte character in data for base64 encoding");
3402 encoded_string = make_unibyte_string (encoded, encoded_length);
3403 if (allength > MAX_ALLOCA)
3404 xfree (encoded);
3406 return encoded_string;
3409 static int
3410 base64_encode_1 (from, to, length, line_break, multibyte)
3411 const char *from;
3412 char *to;
3413 int length;
3414 int line_break;
3415 int multibyte;
3417 int counter = 0, i = 0;
3418 char *e = to;
3419 int c;
3420 unsigned int value;
3421 int bytes;
3423 while (i < length)
3425 if (multibyte)
3427 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3428 if (c >= 256)
3429 return -1;
3430 i += bytes;
3432 else
3433 c = from[i++];
3435 /* Wrap line every 76 characters. */
3437 if (line_break)
3439 if (counter < MIME_LINE_LENGTH / 4)
3440 counter++;
3441 else
3443 *e++ = '\n';
3444 counter = 1;
3448 /* Process first byte of a triplet. */
3450 *e++ = base64_value_to_char[0x3f & c >> 2];
3451 value = (0x03 & c) << 4;
3453 /* Process second byte of a triplet. */
3455 if (i == length)
3457 *e++ = base64_value_to_char[value];
3458 *e++ = '=';
3459 *e++ = '=';
3460 break;
3463 if (multibyte)
3465 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3466 if (c >= 256)
3467 return -1;
3468 i += bytes;
3470 else
3471 c = from[i++];
3473 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3474 value = (0x0f & c) << 2;
3476 /* Process third byte of a triplet. */
3478 if (i == length)
3480 *e++ = base64_value_to_char[value];
3481 *e++ = '=';
3482 break;
3485 if (multibyte)
3487 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3488 if (c >= 256)
3489 return -1;
3490 i += bytes;
3492 else
3493 c = from[i++];
3495 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3496 *e++ = base64_value_to_char[0x3f & c];
3499 return e - to;
3503 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3504 2, 2, "r",
3505 doc: /* Base64-decode the region between BEG and END.
3506 Return the length of the decoded text.
3507 If the region can't be decoded, signal an error and don't modify the buffer. */)
3508 (beg, end)
3509 Lisp_Object beg, end;
3511 int ibeg, iend, length, allength;
3512 char *decoded;
3513 int old_pos = PT;
3514 int decoded_length;
3515 int inserted_chars;
3516 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3518 validate_region (&beg, &end);
3520 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3521 iend = CHAR_TO_BYTE (XFASTINT (end));
3523 length = iend - ibeg;
3525 /* We need to allocate enough room for decoding the text. If we are
3526 working on a multibyte buffer, each decoded code may occupy at
3527 most two bytes. */
3528 allength = multibyte ? length * 2 : length;
3529 if (allength <= MAX_ALLOCA)
3530 decoded = (char *) alloca (allength);
3531 else
3532 decoded = (char *) xmalloc (allength);
3534 move_gap_both (XFASTINT (beg), ibeg);
3535 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3536 multibyte, &inserted_chars);
3537 if (decoded_length > allength)
3538 abort ();
3540 if (decoded_length < 0)
3542 /* The decoding wasn't possible. */
3543 if (allength > MAX_ALLOCA)
3544 xfree (decoded);
3545 error ("Invalid base64 data");
3548 /* Now we have decoded the region, so we insert the new contents
3549 and delete the old. (Insert first in order to preserve markers.) */
3550 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3551 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3552 if (allength > MAX_ALLOCA)
3553 xfree (decoded);
3554 /* Delete the original text. */
3555 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3556 iend + decoded_length, 1);
3558 /* If point was outside of the region, restore it exactly; else just
3559 move to the beginning of the region. */
3560 if (old_pos >= XFASTINT (end))
3561 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3562 else if (old_pos > XFASTINT (beg))
3563 old_pos = XFASTINT (beg);
3564 SET_PT (old_pos > ZV ? ZV : old_pos);
3566 return make_number (inserted_chars);
3569 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3570 1, 1, 0,
3571 doc: /* Base64-decode STRING and return the result. */)
3572 (string)
3573 Lisp_Object string;
3575 char *decoded;
3576 int length, decoded_length;
3577 Lisp_Object decoded_string;
3579 CHECK_STRING (string, 1);
3581 length = STRING_BYTES (XSTRING (string));
3582 /* We need to allocate enough room for decoding the text. */
3583 if (length <= MAX_ALLOCA)
3584 decoded = (char *) alloca (length);
3585 else
3586 decoded = (char *) xmalloc (length);
3588 /* The decoded result should be unibyte. */
3589 decoded_length = base64_decode_1 (XSTRING (string)->data, decoded, length,
3590 0, NULL);
3591 if (decoded_length > length)
3592 abort ();
3593 else if (decoded_length >= 0)
3594 decoded_string = make_unibyte_string (decoded, decoded_length);
3595 else
3596 decoded_string = Qnil;
3598 if (length > MAX_ALLOCA)
3599 xfree (decoded);
3600 if (!STRINGP (decoded_string))
3601 error ("Invalid base64 data");
3603 return decoded_string;
3606 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3607 MULTIBYTE is nonzero, the decoded result should be in multibyte
3608 form. If NCHARS_RETRUN is not NULL, store the number of produced
3609 characters in *NCHARS_RETURN. */
3611 static int
3612 base64_decode_1 (from, to, length, multibyte, nchars_return)
3613 const char *from;
3614 char *to;
3615 int length;
3616 int multibyte;
3617 int *nchars_return;
3619 int i = 0;
3620 char *e = to;
3621 unsigned char c;
3622 unsigned long value;
3623 int nchars = 0;
3625 while (1)
3627 /* Process first byte of a quadruplet. */
3629 READ_QUADRUPLET_BYTE (e-to);
3631 if (!IS_BASE64 (c))
3632 return -1;
3633 value = base64_char_to_value[c] << 18;
3635 /* Process second byte of a quadruplet. */
3637 READ_QUADRUPLET_BYTE (-1);
3639 if (!IS_BASE64 (c))
3640 return -1;
3641 value |= base64_char_to_value[c] << 12;
3643 c = (unsigned char) (value >> 16);
3644 if (multibyte)
3645 e += CHAR_STRING (c, e);
3646 else
3647 *e++ = c;
3648 nchars++;
3650 /* Process third byte of a quadruplet. */
3652 READ_QUADRUPLET_BYTE (-1);
3654 if (c == '=')
3656 READ_QUADRUPLET_BYTE (-1);
3658 if (c != '=')
3659 return -1;
3660 continue;
3663 if (!IS_BASE64 (c))
3664 return -1;
3665 value |= base64_char_to_value[c] << 6;
3667 c = (unsigned char) (0xff & value >> 8);
3668 if (multibyte)
3669 e += CHAR_STRING (c, e);
3670 else
3671 *e++ = c;
3672 nchars++;
3674 /* Process fourth byte of a quadruplet. */
3676 READ_QUADRUPLET_BYTE (-1);
3678 if (c == '=')
3679 continue;
3681 if (!IS_BASE64 (c))
3682 return -1;
3683 value |= base64_char_to_value[c];
3685 c = (unsigned char) (0xff & value);
3686 if (multibyte)
3687 e += CHAR_STRING (c, e);
3688 else
3689 *e++ = c;
3690 nchars++;
3696 /***********************************************************************
3697 ***** *****
3698 ***** Hash Tables *****
3699 ***** *****
3700 ***********************************************************************/
3702 /* Implemented by gerd@gnu.org. This hash table implementation was
3703 inspired by CMUCL hash tables. */
3705 /* Ideas:
3707 1. For small tables, association lists are probably faster than
3708 hash tables because they have lower overhead.
3710 For uses of hash tables where the O(1) behavior of table
3711 operations is not a requirement, it might therefore be a good idea
3712 not to hash. Instead, we could just do a linear search in the
3713 key_and_value vector of the hash table. This could be done
3714 if a `:linear-search t' argument is given to make-hash-table. */
3717 /* Value is the key part of entry IDX in hash table H. */
3719 #define HASH_KEY(H, IDX) AREF ((H)->key_and_value, 2 * (IDX))
3721 /* Value is the value part of entry IDX in hash table H. */
3723 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
3725 /* Value is the index of the next entry following the one at IDX
3726 in hash table H. */
3728 #define HASH_NEXT(H, IDX) AREF ((H)->next, (IDX))
3730 /* Value is the hash code computed for entry IDX in hash table H. */
3732 #define HASH_HASH(H, IDX) AREF ((H)->hash, (IDX))
3734 /* Value is the index of the element in hash table H that is the
3735 start of the collision list at index IDX in the index vector of H. */
3737 #define HASH_INDEX(H, IDX) AREF ((H)->index, (IDX))
3739 /* Value is the size of hash table H. */
3741 #define HASH_TABLE_SIZE(H) XVECTOR ((H)->next)->size
3743 /* The list of all weak hash tables. Don't staticpro this one. */
3745 Lisp_Object Vweak_hash_tables;
3747 /* Various symbols. */
3749 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3750 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3751 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3753 /* Function prototypes. */
3755 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3756 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3757 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3758 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3759 Lisp_Object, unsigned));
3760 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3761 Lisp_Object, unsigned));
3762 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3763 unsigned, Lisp_Object, unsigned));
3764 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3765 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3766 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3767 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3768 Lisp_Object));
3769 static unsigned sxhash_string P_ ((unsigned char *, int));
3770 static unsigned sxhash_list P_ ((Lisp_Object, int));
3771 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3772 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3773 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3777 /***********************************************************************
3778 Utilities
3779 ***********************************************************************/
3781 /* If OBJ is a Lisp hash table, return a pointer to its struct
3782 Lisp_Hash_Table. Otherwise, signal an error. */
3784 static struct Lisp_Hash_Table *
3785 check_hash_table (obj)
3786 Lisp_Object obj;
3788 CHECK_HASH_TABLE (obj, 0);
3789 return XHASH_TABLE (obj);
3793 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3794 number. */
3797 next_almost_prime (n)
3798 int n;
3800 if (n % 2 == 0)
3801 n += 1;
3802 if (n % 3 == 0)
3803 n += 2;
3804 if (n % 7 == 0)
3805 n += 4;
3806 return n;
3810 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3811 which USED[I] is non-zero. If found at index I in ARGS, set
3812 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3813 -1. This function is used to extract a keyword/argument pair from
3814 a DEFUN parameter list. */
3816 static int
3817 get_key_arg (key, nargs, args, used)
3818 Lisp_Object key;
3819 int nargs;
3820 Lisp_Object *args;
3821 char *used;
3823 int i;
3825 for (i = 0; i < nargs - 1; ++i)
3826 if (!used[i] && EQ (args[i], key))
3827 break;
3829 if (i >= nargs - 1)
3830 i = -1;
3831 else
3833 used[i++] = 1;
3834 used[i] = 1;
3837 return i;
3841 /* Return a Lisp vector which has the same contents as VEC but has
3842 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3843 vector that are not copied from VEC are set to INIT. */
3845 Lisp_Object
3846 larger_vector (vec, new_size, init)
3847 Lisp_Object vec;
3848 int new_size;
3849 Lisp_Object init;
3851 struct Lisp_Vector *v;
3852 int i, old_size;
3854 xassert (VECTORP (vec));
3855 old_size = XVECTOR (vec)->size;
3856 xassert (new_size >= old_size);
3858 v = allocate_vector (new_size);
3859 bcopy (XVECTOR (vec)->contents, v->contents,
3860 old_size * sizeof *v->contents);
3861 for (i = old_size; i < new_size; ++i)
3862 v->contents[i] = init;
3863 XSETVECTOR (vec, v);
3864 return vec;
3868 /***********************************************************************
3869 Low-level Functions
3870 ***********************************************************************/
3872 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3873 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3874 KEY2 are the same. */
3876 static int
3877 cmpfn_eql (h, key1, hash1, key2, hash2)
3878 struct Lisp_Hash_Table *h;
3879 Lisp_Object key1, key2;
3880 unsigned hash1, hash2;
3882 return (FLOATP (key1)
3883 && FLOATP (key2)
3884 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3888 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3889 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3890 KEY2 are the same. */
3892 static int
3893 cmpfn_equal (h, key1, hash1, key2, hash2)
3894 struct Lisp_Hash_Table *h;
3895 Lisp_Object key1, key2;
3896 unsigned hash1, hash2;
3898 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3902 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3903 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3904 if KEY1 and KEY2 are the same. */
3906 static int
3907 cmpfn_user_defined (h, key1, hash1, key2, hash2)
3908 struct Lisp_Hash_Table *h;
3909 Lisp_Object key1, key2;
3910 unsigned hash1, hash2;
3912 if (hash1 == hash2)
3914 Lisp_Object args[3];
3916 args[0] = h->user_cmp_function;
3917 args[1] = key1;
3918 args[2] = key2;
3919 return !NILP (Ffuncall (3, args));
3921 else
3922 return 0;
3926 /* Value is a hash code for KEY for use in hash table H which uses
3927 `eq' to compare keys. The hash code returned is guaranteed to fit
3928 in a Lisp integer. */
3930 static unsigned
3931 hashfn_eq (h, key)
3932 struct Lisp_Hash_Table *h;
3933 Lisp_Object key;
3935 unsigned hash = XUINT (key) ^ XGCTYPE (key);
3936 xassert ((hash & ~VALMASK) == 0);
3937 return hash;
3941 /* Value is a hash code for KEY for use in hash table H which uses
3942 `eql' to compare keys. The hash code returned is guaranteed to fit
3943 in a Lisp integer. */
3945 static unsigned
3946 hashfn_eql (h, key)
3947 struct Lisp_Hash_Table *h;
3948 Lisp_Object key;
3950 unsigned hash;
3951 if (FLOATP (key))
3952 hash = sxhash (key, 0);
3953 else
3954 hash = XUINT (key) ^ XGCTYPE (key);
3955 xassert ((hash & ~VALMASK) == 0);
3956 return hash;
3960 /* Value is a hash code for KEY for use in hash table H which uses
3961 `equal' to compare keys. The hash code returned is guaranteed to fit
3962 in a Lisp integer. */
3964 static unsigned
3965 hashfn_equal (h, key)
3966 struct Lisp_Hash_Table *h;
3967 Lisp_Object key;
3969 unsigned hash = sxhash (key, 0);
3970 xassert ((hash & ~VALMASK) == 0);
3971 return hash;
3975 /* Value is a hash code for KEY for use in hash table H which uses as
3976 user-defined function to compare keys. The hash code returned is
3977 guaranteed to fit in a Lisp integer. */
3979 static unsigned
3980 hashfn_user_defined (h, key)
3981 struct Lisp_Hash_Table *h;
3982 Lisp_Object key;
3984 Lisp_Object args[2], hash;
3986 args[0] = h->user_hash_function;
3987 args[1] = key;
3988 hash = Ffuncall (2, args);
3989 if (!INTEGERP (hash))
3990 Fsignal (Qerror,
3991 list2 (build_string ("Invalid hash code returned from \
3992 user-supplied hash function"),
3993 hash));
3994 return XUINT (hash);
3998 /* Create and initialize a new hash table.
4000 TEST specifies the test the hash table will use to compare keys.
4001 It must be either one of the predefined tests `eq', `eql' or
4002 `equal' or a symbol denoting a user-defined test named TEST with
4003 test and hash functions USER_TEST and USER_HASH.
4005 Give the table initial capacity SIZE, SIZE >= 0, an integer.
4007 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
4008 new size when it becomes full is computed by adding REHASH_SIZE to
4009 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
4010 table's new size is computed by multiplying its old size with
4011 REHASH_SIZE.
4013 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4014 be resized when the ratio of (number of entries in the table) /
4015 (table size) is >= REHASH_THRESHOLD.
4017 WEAK specifies the weakness of the table. If non-nil, it must be
4018 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
4020 Lisp_Object
4021 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4022 user_test, user_hash)
4023 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4024 Lisp_Object user_test, user_hash;
4026 struct Lisp_Hash_Table *h;
4027 Lisp_Object table;
4028 int index_size, i, sz;
4030 /* Preconditions. */
4031 xassert (SYMBOLP (test));
4032 xassert (INTEGERP (size) && XINT (size) >= 0);
4033 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
4034 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
4035 xassert (FLOATP (rehash_threshold)
4036 && XFLOATINT (rehash_threshold) > 0
4037 && XFLOATINT (rehash_threshold) <= 1.0);
4039 if (XFASTINT (size) == 0)
4040 size = make_number (1);
4042 /* Allocate a table and initialize it. */
4043 h = allocate_hash_table ();
4045 /* Initialize hash table slots. */
4046 sz = XFASTINT (size);
4048 h->test = test;
4049 if (EQ (test, Qeql))
4051 h->cmpfn = cmpfn_eql;
4052 h->hashfn = hashfn_eql;
4054 else if (EQ (test, Qeq))
4056 h->cmpfn = NULL;
4057 h->hashfn = hashfn_eq;
4059 else if (EQ (test, Qequal))
4061 h->cmpfn = cmpfn_equal;
4062 h->hashfn = hashfn_equal;
4064 else
4066 h->user_cmp_function = user_test;
4067 h->user_hash_function = user_hash;
4068 h->cmpfn = cmpfn_user_defined;
4069 h->hashfn = hashfn_user_defined;
4072 h->weak = weak;
4073 h->rehash_threshold = rehash_threshold;
4074 h->rehash_size = rehash_size;
4075 h->count = make_number (0);
4076 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4077 h->hash = Fmake_vector (size, Qnil);
4078 h->next = Fmake_vector (size, Qnil);
4079 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4080 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4081 h->index = Fmake_vector (make_number (index_size), Qnil);
4083 /* Set up the free list. */
4084 for (i = 0; i < sz - 1; ++i)
4085 HASH_NEXT (h, i) = make_number (i + 1);
4086 h->next_free = make_number (0);
4088 XSET_HASH_TABLE (table, h);
4089 xassert (HASH_TABLE_P (table));
4090 xassert (XHASH_TABLE (table) == h);
4092 /* Maybe add this hash table to the list of all weak hash tables. */
4093 if (NILP (h->weak))
4094 h->next_weak = Qnil;
4095 else
4097 h->next_weak = Vweak_hash_tables;
4098 Vweak_hash_tables = table;
4101 return table;
4105 /* Return a copy of hash table H1. Keys and values are not copied,
4106 only the table itself is. */
4108 Lisp_Object
4109 copy_hash_table (h1)
4110 struct Lisp_Hash_Table *h1;
4112 Lisp_Object table;
4113 struct Lisp_Hash_Table *h2;
4114 struct Lisp_Vector *v, *next;
4116 h2 = allocate_hash_table ();
4117 next = h2->vec_next;
4118 bcopy (h1, h2, sizeof *h2);
4119 h2->vec_next = next;
4120 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4121 h2->hash = Fcopy_sequence (h1->hash);
4122 h2->next = Fcopy_sequence (h1->next);
4123 h2->index = Fcopy_sequence (h1->index);
4124 XSET_HASH_TABLE (table, h2);
4126 /* Maybe add this hash table to the list of all weak hash tables. */
4127 if (!NILP (h2->weak))
4129 h2->next_weak = Vweak_hash_tables;
4130 Vweak_hash_tables = table;
4133 return table;
4137 /* Resize hash table H if it's too full. If H cannot be resized
4138 because it's already too large, throw an error. */
4140 static INLINE void
4141 maybe_resize_hash_table (h)
4142 struct Lisp_Hash_Table *h;
4144 if (NILP (h->next_free))
4146 int old_size = HASH_TABLE_SIZE (h);
4147 int i, new_size, index_size;
4149 if (INTEGERP (h->rehash_size))
4150 new_size = old_size + XFASTINT (h->rehash_size);
4151 else
4152 new_size = old_size * XFLOATINT (h->rehash_size);
4153 new_size = max (old_size + 1, new_size);
4154 index_size = next_almost_prime ((int)
4155 (new_size
4156 / XFLOATINT (h->rehash_threshold)));
4157 if (max (index_size, 2 * new_size) & ~VALMASK)
4158 error ("Hash table too large to resize");
4160 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4161 h->next = larger_vector (h->next, new_size, Qnil);
4162 h->hash = larger_vector (h->hash, new_size, Qnil);
4163 h->index = Fmake_vector (make_number (index_size), Qnil);
4165 /* Update the free list. Do it so that new entries are added at
4166 the end of the free list. This makes some operations like
4167 maphash faster. */
4168 for (i = old_size; i < new_size - 1; ++i)
4169 HASH_NEXT (h, i) = make_number (i + 1);
4171 if (!NILP (h->next_free))
4173 Lisp_Object last, next;
4175 last = h->next_free;
4176 while (next = HASH_NEXT (h, XFASTINT (last)),
4177 !NILP (next))
4178 last = next;
4180 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4182 else
4183 XSETFASTINT (h->next_free, old_size);
4185 /* Rehash. */
4186 for (i = 0; i < old_size; ++i)
4187 if (!NILP (HASH_HASH (h, i)))
4189 unsigned hash_code = XUINT (HASH_HASH (h, i));
4190 int start_of_bucket = hash_code % XVECTOR (h->index)->size;
4191 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4192 HASH_INDEX (h, start_of_bucket) = make_number (i);
4198 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4199 the hash code of KEY. Value is the index of the entry in H
4200 matching KEY, or -1 if not found. */
4203 hash_lookup (h, key, hash)
4204 struct Lisp_Hash_Table *h;
4205 Lisp_Object key;
4206 unsigned *hash;
4208 unsigned hash_code;
4209 int start_of_bucket;
4210 Lisp_Object idx;
4212 hash_code = h->hashfn (h, key);
4213 if (hash)
4214 *hash = hash_code;
4216 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4217 idx = HASH_INDEX (h, start_of_bucket);
4219 /* We need not gcpro idx since it's either an integer or nil. */
4220 while (!NILP (idx))
4222 int i = XFASTINT (idx);
4223 if (EQ (key, HASH_KEY (h, i))
4224 || (h->cmpfn
4225 && h->cmpfn (h, key, hash_code,
4226 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4227 break;
4228 idx = HASH_NEXT (h, i);
4231 return NILP (idx) ? -1 : XFASTINT (idx);
4235 /* Put an entry into hash table H that associates KEY with VALUE.
4236 HASH is a previously computed hash code of KEY.
4237 Value is the index of the entry in H matching KEY. */
4240 hash_put (h, key, value, hash)
4241 struct Lisp_Hash_Table *h;
4242 Lisp_Object key, value;
4243 unsigned hash;
4245 int start_of_bucket, i;
4247 xassert ((hash & ~VALMASK) == 0);
4249 /* Increment count after resizing because resizing may fail. */
4250 maybe_resize_hash_table (h);
4251 h->count = make_number (XFASTINT (h->count) + 1);
4253 /* Store key/value in the key_and_value vector. */
4254 i = XFASTINT (h->next_free);
4255 h->next_free = HASH_NEXT (h, i);
4256 HASH_KEY (h, i) = key;
4257 HASH_VALUE (h, i) = value;
4259 /* Remember its hash code. */
4260 HASH_HASH (h, i) = make_number (hash);
4262 /* Add new entry to its collision chain. */
4263 start_of_bucket = hash % XVECTOR (h->index)->size;
4264 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4265 HASH_INDEX (h, start_of_bucket) = make_number (i);
4266 return i;
4270 /* Remove the entry matching KEY from hash table H, if there is one. */
4272 void
4273 hash_remove (h, key)
4274 struct Lisp_Hash_Table *h;
4275 Lisp_Object key;
4277 unsigned hash_code;
4278 int start_of_bucket;
4279 Lisp_Object idx, prev;
4281 hash_code = h->hashfn (h, key);
4282 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4283 idx = HASH_INDEX (h, start_of_bucket);
4284 prev = Qnil;
4286 /* We need not gcpro idx, prev since they're either integers or nil. */
4287 while (!NILP (idx))
4289 int i = XFASTINT (idx);
4291 if (EQ (key, HASH_KEY (h, i))
4292 || (h->cmpfn
4293 && h->cmpfn (h, key, hash_code,
4294 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4296 /* Take entry out of collision chain. */
4297 if (NILP (prev))
4298 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4299 else
4300 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4302 /* Clear slots in key_and_value and add the slots to
4303 the free list. */
4304 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4305 HASH_NEXT (h, i) = h->next_free;
4306 h->next_free = make_number (i);
4307 h->count = make_number (XFASTINT (h->count) - 1);
4308 xassert (XINT (h->count) >= 0);
4309 break;
4311 else
4313 prev = idx;
4314 idx = HASH_NEXT (h, i);
4320 /* Clear hash table H. */
4322 void
4323 hash_clear (h)
4324 struct Lisp_Hash_Table *h;
4326 if (XFASTINT (h->count) > 0)
4328 int i, size = HASH_TABLE_SIZE (h);
4330 for (i = 0; i < size; ++i)
4332 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4333 HASH_KEY (h, i) = Qnil;
4334 HASH_VALUE (h, i) = Qnil;
4335 HASH_HASH (h, i) = Qnil;
4338 for (i = 0; i < XVECTOR (h->index)->size; ++i)
4339 XVECTOR (h->index)->contents[i] = Qnil;
4341 h->next_free = make_number (0);
4342 h->count = make_number (0);
4348 /************************************************************************
4349 Weak Hash Tables
4350 ************************************************************************/
4352 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4353 entries from the table that don't survive the current GC.
4354 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4355 non-zero if anything was marked. */
4357 static int
4358 sweep_weak_table (h, remove_entries_p)
4359 struct Lisp_Hash_Table *h;
4360 int remove_entries_p;
4362 int bucket, n, marked;
4364 n = XVECTOR (h->index)->size & ~ARRAY_MARK_FLAG;
4365 marked = 0;
4367 for (bucket = 0; bucket < n; ++bucket)
4369 Lisp_Object idx, next, prev;
4371 /* Follow collision chain, removing entries that
4372 don't survive this garbage collection. */
4373 prev = Qnil;
4374 for (idx = HASH_INDEX (h, bucket); !GC_NILP (idx); idx = next)
4376 int i = XFASTINT (idx);
4377 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4378 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4379 int remove_p;
4381 if (EQ (h->weak, Qkey))
4382 remove_p = !key_known_to_survive_p;
4383 else if (EQ (h->weak, Qvalue))
4384 remove_p = !value_known_to_survive_p;
4385 else if (EQ (h->weak, Qkey_or_value))
4386 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4387 else if (EQ (h->weak, Qkey_and_value))
4388 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4389 else
4390 abort ();
4392 next = HASH_NEXT (h, i);
4394 if (remove_entries_p)
4396 if (remove_p)
4398 /* Take out of collision chain. */
4399 if (GC_NILP (prev))
4400 HASH_INDEX (h, bucket) = next;
4401 else
4402 HASH_NEXT (h, XFASTINT (prev)) = next;
4404 /* Add to free list. */
4405 HASH_NEXT (h, i) = h->next_free;
4406 h->next_free = idx;
4408 /* Clear key, value, and hash. */
4409 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4410 HASH_HASH (h, i) = Qnil;
4412 h->count = make_number (XFASTINT (h->count) - 1);
4415 else
4417 if (!remove_p)
4419 /* Make sure key and value survive. */
4420 if (!key_known_to_survive_p)
4422 mark_object (&HASH_KEY (h, i));
4423 marked = 1;
4426 if (!value_known_to_survive_p)
4428 mark_object (&HASH_VALUE (h, i));
4429 marked = 1;
4436 return marked;
4439 /* Remove elements from weak hash tables that don't survive the
4440 current garbage collection. Remove weak tables that don't survive
4441 from Vweak_hash_tables. Called from gc_sweep. */
4443 void
4444 sweep_weak_hash_tables ()
4446 Lisp_Object table, used, next;
4447 struct Lisp_Hash_Table *h;
4448 int marked;
4450 /* Mark all keys and values that are in use. Keep on marking until
4451 there is no more change. This is necessary for cases like
4452 value-weak table A containing an entry X -> Y, where Y is used in a
4453 key-weak table B, Z -> Y. If B comes after A in the list of weak
4454 tables, X -> Y might be removed from A, although when looking at B
4455 one finds that it shouldn't. */
4458 marked = 0;
4459 for (table = Vweak_hash_tables; !GC_NILP (table); table = h->next_weak)
4461 h = XHASH_TABLE (table);
4462 if (h->size & ARRAY_MARK_FLAG)
4463 marked |= sweep_weak_table (h, 0);
4466 while (marked);
4468 /* Remove tables and entries that aren't used. */
4469 for (table = Vweak_hash_tables, used = Qnil; !GC_NILP (table); table = next)
4471 h = XHASH_TABLE (table);
4472 next = h->next_weak;
4474 if (h->size & ARRAY_MARK_FLAG)
4476 /* TABLE is marked as used. Sweep its contents. */
4477 if (XFASTINT (h->count) > 0)
4478 sweep_weak_table (h, 1);
4480 /* Add table to the list of used weak hash tables. */
4481 h->next_weak = used;
4482 used = table;
4486 Vweak_hash_tables = used;
4491 /***********************************************************************
4492 Hash Code Computation
4493 ***********************************************************************/
4495 /* Maximum depth up to which to dive into Lisp structures. */
4497 #define SXHASH_MAX_DEPTH 3
4499 /* Maximum length up to which to take list and vector elements into
4500 account. */
4502 #define SXHASH_MAX_LEN 7
4504 /* Combine two integers X and Y for hashing. */
4506 #define SXHASH_COMBINE(X, Y) \
4507 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4508 + (unsigned)(Y))
4511 /* Return a hash for string PTR which has length LEN. The hash
4512 code returned is guaranteed to fit in a Lisp integer. */
4514 static unsigned
4515 sxhash_string (ptr, len)
4516 unsigned char *ptr;
4517 int len;
4519 unsigned char *p = ptr;
4520 unsigned char *end = p + len;
4521 unsigned char c;
4522 unsigned hash = 0;
4524 while (p != end)
4526 c = *p++;
4527 if (c >= 0140)
4528 c -= 40;
4529 hash = ((hash << 3) + (hash >> 28) + c);
4532 return hash & VALMASK;
4536 /* Return a hash for list LIST. DEPTH is the current depth in the
4537 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4539 static unsigned
4540 sxhash_list (list, depth)
4541 Lisp_Object list;
4542 int depth;
4544 unsigned hash = 0;
4545 int i;
4547 if (depth < SXHASH_MAX_DEPTH)
4548 for (i = 0;
4549 CONSP (list) && i < SXHASH_MAX_LEN;
4550 list = XCDR (list), ++i)
4552 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4553 hash = SXHASH_COMBINE (hash, hash2);
4556 return hash;
4560 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4561 the Lisp structure. */
4563 static unsigned
4564 sxhash_vector (vec, depth)
4565 Lisp_Object vec;
4566 int depth;
4568 unsigned hash = XVECTOR (vec)->size;
4569 int i, n;
4571 n = min (SXHASH_MAX_LEN, XVECTOR (vec)->size);
4572 for (i = 0; i < n; ++i)
4574 unsigned hash2 = sxhash (XVECTOR (vec)->contents[i], depth + 1);
4575 hash = SXHASH_COMBINE (hash, hash2);
4578 return hash;
4582 /* Return a hash for bool-vector VECTOR. */
4584 static unsigned
4585 sxhash_bool_vector (vec)
4586 Lisp_Object vec;
4588 unsigned hash = XBOOL_VECTOR (vec)->size;
4589 int i, n;
4591 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4592 for (i = 0; i < n; ++i)
4593 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4595 return hash;
4599 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4600 structure. Value is an unsigned integer clipped to VALMASK. */
4602 unsigned
4603 sxhash (obj, depth)
4604 Lisp_Object obj;
4605 int depth;
4607 unsigned hash;
4609 if (depth > SXHASH_MAX_DEPTH)
4610 return 0;
4612 switch (XTYPE (obj))
4614 case Lisp_Int:
4615 hash = XUINT (obj);
4616 break;
4618 case Lisp_Symbol:
4619 hash = sxhash_string (XSYMBOL (obj)->name->data,
4620 XSYMBOL (obj)->name->size);
4621 break;
4623 case Lisp_Misc:
4624 hash = XUINT (obj);
4625 break;
4627 case Lisp_String:
4628 hash = sxhash_string (XSTRING (obj)->data, XSTRING (obj)->size);
4629 break;
4631 /* This can be everything from a vector to an overlay. */
4632 case Lisp_Vectorlike:
4633 if (VECTORP (obj))
4634 /* According to the CL HyperSpec, two arrays are equal only if
4635 they are `eq', except for strings and bit-vectors. In
4636 Emacs, this works differently. We have to compare element
4637 by element. */
4638 hash = sxhash_vector (obj, depth);
4639 else if (BOOL_VECTOR_P (obj))
4640 hash = sxhash_bool_vector (obj);
4641 else
4642 /* Others are `equal' if they are `eq', so let's take their
4643 address as hash. */
4644 hash = XUINT (obj);
4645 break;
4647 case Lisp_Cons:
4648 hash = sxhash_list (obj, depth);
4649 break;
4651 case Lisp_Float:
4653 unsigned char *p = (unsigned char *) &XFLOAT_DATA (obj);
4654 unsigned char *e = p + sizeof XFLOAT_DATA (obj);
4655 for (hash = 0; p < e; ++p)
4656 hash = SXHASH_COMBINE (hash, *p);
4657 break;
4660 default:
4661 abort ();
4664 return hash & VALMASK;
4669 /***********************************************************************
4670 Lisp Interface
4671 ***********************************************************************/
4674 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4675 doc: /* Compute a hash code for OBJ and return it as integer. */)
4676 (obj)
4677 Lisp_Object obj;
4679 unsigned hash = sxhash (obj, 0);;
4680 return make_number (hash);
4684 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4685 doc: /* Create and return a new hash table.
4687 Arguments are specified as keyword/argument pairs. The following
4688 arguments are defined:
4690 :test TEST -- TEST must be a symbol that specifies how to compare
4691 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4692 `equal'. User-supplied test and hash functions can be specified via
4693 `define-hash-table-test'.
4695 :size SIZE -- A hint as to how many elements will be put in the table.
4696 Default is 65.
4698 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4699 fills up. If REHASH-SIZE is an integer, add that many space. If it
4700 is a float, it must be > 1.0, and the new size is computed by
4701 multiplying the old size with that factor. Default is 1.5.
4703 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4704 Resize the hash table when ratio of the number of entries in the
4705 table. Default is 0.8.
4707 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4708 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4709 returned is a weak table. Key/value pairs are removed from a weak
4710 hash table when there are no non-weak references pointing to their
4711 key, value, one of key or value, or both key and value, depending on
4712 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4713 is nil.
4715 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4716 (nargs, args)
4717 int nargs;
4718 Lisp_Object *args;
4720 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4721 Lisp_Object user_test, user_hash;
4722 char *used;
4723 int i;
4725 /* The vector `used' is used to keep track of arguments that
4726 have been consumed. */
4727 used = (char *) alloca (nargs * sizeof *used);
4728 bzero (used, nargs * sizeof *used);
4730 /* See if there's a `:test TEST' among the arguments. */
4731 i = get_key_arg (QCtest, nargs, args, used);
4732 test = i < 0 ? Qeql : args[i];
4733 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4735 /* See if it is a user-defined test. */
4736 Lisp_Object prop;
4738 prop = Fget (test, Qhash_table_test);
4739 if (!CONSP (prop) || XFASTINT (Flength (prop)) < 2)
4740 Fsignal (Qerror, list2 (build_string ("Invalid hash table test"),
4741 test));
4742 user_test = Fnth (make_number (0), prop);
4743 user_hash = Fnth (make_number (1), prop);
4745 else
4746 user_test = user_hash = Qnil;
4748 /* See if there's a `:size SIZE' argument. */
4749 i = get_key_arg (QCsize, nargs, args, used);
4750 size = i < 0 ? make_number (DEFAULT_HASH_SIZE) : args[i];
4751 if (!INTEGERP (size) || XINT (size) < 0)
4752 Fsignal (Qerror,
4753 list2 (build_string ("Invalid hash table size"),
4754 size));
4756 /* Look for `:rehash-size SIZE'. */
4757 i = get_key_arg (QCrehash_size, nargs, args, used);
4758 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4759 if (!NUMBERP (rehash_size)
4760 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4761 || XFLOATINT (rehash_size) <= 1.0)
4762 Fsignal (Qerror,
4763 list2 (build_string ("Invalid hash table rehash size"),
4764 rehash_size));
4766 /* Look for `:rehash-threshold THRESHOLD'. */
4767 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4768 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4769 if (!FLOATP (rehash_threshold)
4770 || XFLOATINT (rehash_threshold) <= 0.0
4771 || XFLOATINT (rehash_threshold) > 1.0)
4772 Fsignal (Qerror,
4773 list2 (build_string ("Invalid hash table rehash threshold"),
4774 rehash_threshold));
4776 /* Look for `:weakness WEAK'. */
4777 i = get_key_arg (QCweakness, nargs, args, used);
4778 weak = i < 0 ? Qnil : args[i];
4779 if (EQ (weak, Qt))
4780 weak = Qkey_and_value;
4781 if (!NILP (weak)
4782 && !EQ (weak, Qkey)
4783 && !EQ (weak, Qvalue)
4784 && !EQ (weak, Qkey_or_value)
4785 && !EQ (weak, Qkey_and_value))
4786 Fsignal (Qerror, list2 (build_string ("Invalid hash table weakness"),
4787 weak));
4789 /* Now, all args should have been used up, or there's a problem. */
4790 for (i = 0; i < nargs; ++i)
4791 if (!used[i])
4792 Fsignal (Qerror,
4793 list2 (build_string ("Invalid argument list"), args[i]));
4795 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4796 user_test, user_hash);
4800 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4801 doc: /* Return a copy of hash table TABLE. */)
4802 (table)
4803 Lisp_Object table;
4805 return copy_hash_table (check_hash_table (table));
4809 DEFUN ("makehash", Fmakehash, Smakehash, 0, 1, 0,
4810 doc: /* Create a new hash table.
4812 Optional first argument TEST specifies how to compare keys in the
4813 table. Predefined tests are `eq', `eql', and `equal'. Default is
4814 `eql'. New tests can be defined with `define-hash-table-test'. */)
4815 (test)
4816 Lisp_Object test;
4818 Lisp_Object args[2];
4819 args[0] = QCtest;
4820 args[1] = NILP (test) ? Qeql : test;
4821 return Fmake_hash_table (2, args);
4825 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4826 doc: /* Return the number of elements in TABLE. */)
4827 (table)
4828 Lisp_Object table;
4830 return check_hash_table (table)->count;
4834 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4835 Shash_table_rehash_size, 1, 1, 0,
4836 doc: /* Return the current rehash size of TABLE. */)
4837 (table)
4838 Lisp_Object table;
4840 return check_hash_table (table)->rehash_size;
4844 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4845 Shash_table_rehash_threshold, 1, 1, 0,
4846 doc: /* Return the current rehash threshold of TABLE. */)
4847 (table)
4848 Lisp_Object table;
4850 return check_hash_table (table)->rehash_threshold;
4854 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4855 doc: /* Return the size of TABLE.
4856 The size can be used as an argument to `make-hash-table' to create
4857 a hash table than can hold as many elements of TABLE holds
4858 without need for resizing. */)
4859 (table)
4860 Lisp_Object table;
4862 struct Lisp_Hash_Table *h = check_hash_table (table);
4863 return make_number (HASH_TABLE_SIZE (h));
4867 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4868 doc: /* Return the test TABLE uses. */)
4869 (table)
4870 Lisp_Object table;
4872 return check_hash_table (table)->test;
4876 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4877 1, 1, 0,
4878 doc: /* Return the weakness of TABLE. */)
4879 (table)
4880 Lisp_Object table;
4882 return check_hash_table (table)->weak;
4886 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4887 doc: /* Return t if OBJ is a Lisp hash table object. */)
4888 (obj)
4889 Lisp_Object obj;
4891 return HASH_TABLE_P (obj) ? Qt : Qnil;
4895 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4896 doc: /* Clear hash table TABLE. */)
4897 (table)
4898 Lisp_Object table;
4900 hash_clear (check_hash_table (table));
4901 return Qnil;
4905 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4906 doc: /* Look up KEY in TABLE and return its associated value.
4907 If KEY is not found, return DFLT which defaults to nil. */)
4908 (key, table, dflt)
4909 Lisp_Object key, table, dflt;
4911 struct Lisp_Hash_Table *h = check_hash_table (table);
4912 int i = hash_lookup (h, key, NULL);
4913 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4917 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4918 doc: /* Associate KEY with VALUE in hash table TABLE.
4919 If KEY is already present in table, replace its current value with
4920 VALUE. */)
4921 (key, value, table)
4922 Lisp_Object key, value, table;
4924 struct Lisp_Hash_Table *h = check_hash_table (table);
4925 int i;
4926 unsigned hash;
4928 i = hash_lookup (h, key, &hash);
4929 if (i >= 0)
4930 HASH_VALUE (h, i) = value;
4931 else
4932 hash_put (h, key, value, hash);
4934 return value;
4938 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4939 doc: /* Remove KEY from TABLE. */)
4940 (key, table)
4941 Lisp_Object key, table;
4943 struct Lisp_Hash_Table *h = check_hash_table (table);
4944 hash_remove (h, key);
4945 return Qnil;
4949 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4950 doc: /* Call FUNCTION for all entries in hash table TABLE.
4951 FUNCTION is called with 2 arguments KEY and VALUE. */)
4952 (function, table)
4953 Lisp_Object function, table;
4955 struct Lisp_Hash_Table *h = check_hash_table (table);
4956 Lisp_Object args[3];
4957 int i;
4959 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4960 if (!NILP (HASH_HASH (h, i)))
4962 args[0] = function;
4963 args[1] = HASH_KEY (h, i);
4964 args[2] = HASH_VALUE (h, i);
4965 Ffuncall (3, args);
4968 return Qnil;
4972 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4973 Sdefine_hash_table_test, 3, 3, 0,
4974 doc: /* Define a new hash table test with name NAME, a symbol.
4976 In hash tables created with NAME specified as test, use TEST to
4977 compare keys, and HASH for computing hash codes of keys.
4979 TEST must be a function taking two arguments and returning non-nil if
4980 both arguments are the same. HASH must be a function taking one
4981 argument and return an integer that is the hash code of the argument.
4982 Hash code computation should use the whole value range of integers,
4983 including negative integers. */)
4984 (name, test, hash)
4985 Lisp_Object name, test, hash;
4987 return Fput (name, Qhash_table_test, list2 (test, hash));
4992 /************************************************************************
4994 ************************************************************************/
4996 #include "md5.h"
4997 #include "coding.h"
4999 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
5000 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
5002 A message digest is a cryptographic checksum of a document, and the
5003 algorithm to calculate it is defined in RFC 1321.
5005 The two optional arguments START and END are character positions
5006 specifying for which part of OBJECT the message digest should be
5007 computed. If nil or omitted, the digest is computed for the whole
5008 OBJECT.
5010 The MD5 message digest is computed from the result of encoding the
5011 text in a coding system, not directly from the internal Emacs form of
5012 the text. The optional fourth argument CODING-SYSTEM specifies which
5013 coding system to encode the text with. It should be the same coding
5014 system that you used or will use when actually writing the text into a
5015 file.
5017 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
5018 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
5019 system would be chosen by default for writing this text into a file.
5021 If OBJECT is a string, the most preferred coding system (see the
5022 command `prefer-coding-system') is used.
5024 If NOERROR is non-nil, silently assume the `raw-text' coding if the
5025 guesswork fails. Normally, an error is signaled in such case. */)
5026 (object, start, end, coding_system, noerror)
5027 Lisp_Object object, start, end, coding_system, noerror;
5029 unsigned char digest[16];
5030 unsigned char value[33];
5031 int i;
5032 int size;
5033 int size_byte = 0;
5034 int start_char = 0, end_char = 0;
5035 int start_byte = 0, end_byte = 0;
5036 register int b, e;
5037 register struct buffer *bp;
5038 int temp;
5040 if (STRINGP (object))
5042 if (NILP (coding_system))
5044 /* Decide the coding-system to encode the data with. */
5046 if (STRING_MULTIBYTE (object))
5047 /* use default, we can't guess correct value */
5048 coding_system = SYMBOL_VALUE (XCAR (Vcoding_category_list));
5049 else
5050 coding_system = Qraw_text;
5053 if (NILP (Fcoding_system_p (coding_system)))
5055 /* Invalid coding system. */
5057 if (!NILP (noerror))
5058 coding_system = Qraw_text;
5059 else
5060 while (1)
5061 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5064 if (STRING_MULTIBYTE (object))
5065 object = code_convert_string1 (object, coding_system, Qnil, 1);
5067 size = XSTRING (object)->size;
5068 size_byte = STRING_BYTES (XSTRING (object));
5070 if (!NILP (start))
5072 CHECK_NUMBER (start, 1);
5074 start_char = XINT (start);
5076 if (start_char < 0)
5077 start_char += size;
5079 start_byte = string_char_to_byte (object, start_char);
5082 if (NILP (end))
5084 end_char = size;
5085 end_byte = size_byte;
5087 else
5089 CHECK_NUMBER (end, 2);
5091 end_char = XINT (end);
5093 if (end_char < 0)
5094 end_char += size;
5096 end_byte = string_char_to_byte (object, end_char);
5099 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5100 args_out_of_range_3 (object, make_number (start_char),
5101 make_number (end_char));
5103 else
5105 CHECK_BUFFER (object, 0);
5107 bp = XBUFFER (object);
5109 if (NILP (start))
5110 b = BUF_BEGV (bp);
5111 else
5113 CHECK_NUMBER_COERCE_MARKER (start, 0);
5114 b = XINT (start);
5117 if (NILP (end))
5118 e = BUF_ZV (bp);
5119 else
5121 CHECK_NUMBER_COERCE_MARKER (end, 1);
5122 e = XINT (end);
5125 if (b > e)
5126 temp = b, b = e, e = temp;
5128 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
5129 args_out_of_range (start, end);
5131 if (NILP (coding_system))
5133 /* Decide the coding-system to encode the data with.
5134 See fileio.c:Fwrite-region */
5136 if (!NILP (Vcoding_system_for_write))
5137 coding_system = Vcoding_system_for_write;
5138 else
5140 int force_raw_text = 0;
5142 coding_system = XBUFFER (object)->buffer_file_coding_system;
5143 if (NILP (coding_system)
5144 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5146 coding_system = Qnil;
5147 if (NILP (current_buffer->enable_multibyte_characters))
5148 force_raw_text = 1;
5151 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5153 /* Check file-coding-system-alist. */
5154 Lisp_Object args[4], val;
5156 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5157 args[3] = Fbuffer_file_name(object);
5158 val = Ffind_operation_coding_system (4, args);
5159 if (CONSP (val) && !NILP (XCDR (val)))
5160 coding_system = XCDR (val);
5163 if (NILP (coding_system)
5164 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5166 /* If we still have not decided a coding system, use the
5167 default value of buffer-file-coding-system. */
5168 coding_system = XBUFFER (object)->buffer_file_coding_system;
5171 if (!force_raw_text
5172 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5173 /* Confirm that VAL can surely encode the current region. */
5174 coding_system = call3 (Vselect_safe_coding_system_function,
5175 make_number (b), make_number (e),
5176 coding_system);
5178 if (force_raw_text)
5179 coding_system = Qraw_text;
5182 if (NILP (Fcoding_system_p (coding_system)))
5184 /* Invalid coding system. */
5186 if (!NILP (noerror))
5187 coding_system = Qraw_text;
5188 else
5189 while (1)
5190 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5194 object = make_buffer_string (b, e, 0);
5196 if (STRING_MULTIBYTE (object))
5197 object = code_convert_string1 (object, coding_system, Qnil, 1);
5200 md5_buffer (XSTRING (object)->data + start_byte,
5201 STRING_BYTES(XSTRING (object)) - (size_byte - end_byte),
5202 digest);
5204 for (i = 0; i < 16; i++)
5205 sprintf (&value[2 * i], "%02x", digest[i]);
5206 value[32] = '\0';
5208 return make_string (value, 32);
5212 void
5213 syms_of_fns ()
5215 /* Hash table stuff. */
5216 Qhash_table_p = intern ("hash-table-p");
5217 staticpro (&Qhash_table_p);
5218 Qeq = intern ("eq");
5219 staticpro (&Qeq);
5220 Qeql = intern ("eql");
5221 staticpro (&Qeql);
5222 Qequal = intern ("equal");
5223 staticpro (&Qequal);
5224 QCtest = intern (":test");
5225 staticpro (&QCtest);
5226 QCsize = intern (":size");
5227 staticpro (&QCsize);
5228 QCrehash_size = intern (":rehash-size");
5229 staticpro (&QCrehash_size);
5230 QCrehash_threshold = intern (":rehash-threshold");
5231 staticpro (&QCrehash_threshold);
5232 QCweakness = intern (":weakness");
5233 staticpro (&QCweakness);
5234 Qkey = intern ("key");
5235 staticpro (&Qkey);
5236 Qvalue = intern ("value");
5237 staticpro (&Qvalue);
5238 Qhash_table_test = intern ("hash-table-test");
5239 staticpro (&Qhash_table_test);
5240 Qkey_or_value = intern ("key-or-value");
5241 staticpro (&Qkey_or_value);
5242 Qkey_and_value = intern ("key-and-value");
5243 staticpro (&Qkey_and_value);
5245 defsubr (&Ssxhash);
5246 defsubr (&Smake_hash_table);
5247 defsubr (&Scopy_hash_table);
5248 defsubr (&Smakehash);
5249 defsubr (&Shash_table_count);
5250 defsubr (&Shash_table_rehash_size);
5251 defsubr (&Shash_table_rehash_threshold);
5252 defsubr (&Shash_table_size);
5253 defsubr (&Shash_table_test);
5254 defsubr (&Shash_table_weakness);
5255 defsubr (&Shash_table_p);
5256 defsubr (&Sclrhash);
5257 defsubr (&Sgethash);
5258 defsubr (&Sputhash);
5259 defsubr (&Sremhash);
5260 defsubr (&Smaphash);
5261 defsubr (&Sdefine_hash_table_test);
5263 Qstring_lessp = intern ("string-lessp");
5264 staticpro (&Qstring_lessp);
5265 Qprovide = intern ("provide");
5266 staticpro (&Qprovide);
5267 Qrequire = intern ("require");
5268 staticpro (&Qrequire);
5269 Qyes_or_no_p_history = intern ("yes-or-no-p-history");
5270 staticpro (&Qyes_or_no_p_history);
5271 Qcursor_in_echo_area = intern ("cursor-in-echo-area");
5272 staticpro (&Qcursor_in_echo_area);
5273 Qwidget_type = intern ("widget-type");
5274 staticpro (&Qwidget_type);
5276 staticpro (&string_char_byte_cache_string);
5277 string_char_byte_cache_string = Qnil;
5279 Fset (Qyes_or_no_p_history, Qnil);
5281 DEFVAR_LISP ("features", &Vfeatures,
5282 doc: /* A list of symbols which are the features of the executing emacs.
5283 Used by `featurep' and `require', and altered by `provide'. */);
5284 Vfeatures = Qnil;
5285 Qsubfeatures = intern ("subfeatures");
5286 staticpro (&Qsubfeatures);
5288 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5289 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5290 This applies to y-or-n and yes-or-no questions asked by commands
5291 invoked by mouse clicks and mouse menu items. */);
5292 use_dialog_box = 1;
5294 defsubr (&Sidentity);
5295 defsubr (&Srandom);
5296 defsubr (&Slength);
5297 defsubr (&Ssafe_length);
5298 defsubr (&Sstring_bytes);
5299 defsubr (&Sstring_equal);
5300 defsubr (&Scompare_strings);
5301 defsubr (&Sstring_lessp);
5302 defsubr (&Sappend);
5303 defsubr (&Sconcat);
5304 defsubr (&Svconcat);
5305 defsubr (&Scopy_sequence);
5306 defsubr (&Sstring_make_multibyte);
5307 defsubr (&Sstring_make_unibyte);
5308 defsubr (&Sstring_as_multibyte);
5309 defsubr (&Sstring_as_unibyte);
5310 defsubr (&Scopy_alist);
5311 defsubr (&Ssubstring);
5312 defsubr (&Snthcdr);
5313 defsubr (&Snth);
5314 defsubr (&Selt);
5315 defsubr (&Smember);
5316 defsubr (&Smemq);
5317 defsubr (&Sassq);
5318 defsubr (&Sassoc);
5319 defsubr (&Srassq);
5320 defsubr (&Srassoc);
5321 defsubr (&Sdelq);
5322 defsubr (&Sdelete);
5323 defsubr (&Snreverse);
5324 defsubr (&Sreverse);
5325 defsubr (&Ssort);
5326 defsubr (&Splist_get);
5327 defsubr (&Sget);
5328 defsubr (&Splist_put);
5329 defsubr (&Sput);
5330 defsubr (&Sequal);
5331 defsubr (&Sfillarray);
5332 defsubr (&Schar_table_subtype);
5333 defsubr (&Schar_table_parent);
5334 defsubr (&Sset_char_table_parent);
5335 defsubr (&Schar_table_extra_slot);
5336 defsubr (&Sset_char_table_extra_slot);
5337 defsubr (&Schar_table_range);
5338 defsubr (&Sset_char_table_range);
5339 defsubr (&Sset_char_table_default);
5340 defsubr (&Soptimize_char_table);
5341 defsubr (&Smap_char_table);
5342 defsubr (&Snconc);
5343 defsubr (&Smapcar);
5344 defsubr (&Smapc);
5345 defsubr (&Smapconcat);
5346 defsubr (&Sy_or_n_p);
5347 defsubr (&Syes_or_no_p);
5348 defsubr (&Sload_average);
5349 defsubr (&Sfeaturep);
5350 defsubr (&Srequire);
5351 defsubr (&Sprovide);
5352 defsubr (&Splist_member);
5353 defsubr (&Swidget_put);
5354 defsubr (&Swidget_get);
5355 defsubr (&Swidget_apply);
5356 defsubr (&Sbase64_encode_region);
5357 defsubr (&Sbase64_decode_region);
5358 defsubr (&Sbase64_encode_string);
5359 defsubr (&Sbase64_decode_string);
5360 defsubr (&Smd5);
5364 void
5365 init_fns ()
5367 Vweak_hash_tables = Qnil;