Remove tool_bar_redisplayed_once and associated code.
[emacs.git] / src / fns.c
blobaa917ac6ec9c88b4be83ec5e66adf134e5024f03
1 /* Random utility Lisp functions.
3 Copyright (C) 1985-1987, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <unistd.h>
24 #include <time.h>
26 #include <intprops.h>
27 #include <vla.h>
29 #include "lisp.h"
30 #include "commands.h"
31 #include "character.h"
32 #include "coding.h"
33 #include "buffer.h"
34 #include "keyboard.h"
35 #include "keymap.h"
36 #include "intervals.h"
37 #include "frame.h"
38 #include "window.h"
39 #include "blockinput.h"
40 #if defined (HAVE_X_WINDOWS)
41 #include "xterm.h"
42 #endif
44 static void sort_vector_copy (Lisp_Object, ptrdiff_t,
45 Lisp_Object [restrict], Lisp_Object [restrict]);
46 static bool internal_equal (Lisp_Object, Lisp_Object, int, bool, Lisp_Object);
48 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
49 doc: /* Return the argument unchanged. */
50 attributes: const)
51 (Lisp_Object arg)
53 return arg;
56 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
57 doc: /* Return a pseudo-random number.
58 All integers representable in Lisp, i.e. between `most-negative-fixnum'
59 and `most-positive-fixnum', inclusive, are equally likely.
61 With positive integer LIMIT, return random number in interval [0,LIMIT).
62 With argument t, set the random number seed from the current time and pid.
63 With a string argument, set the seed based on the string's contents.
64 Other values of LIMIT are ignored.
66 See Info node `(elisp)Random Numbers' for more details. */)
67 (Lisp_Object limit)
69 EMACS_INT val;
71 if (EQ (limit, Qt))
72 init_random ();
73 else if (STRINGP (limit))
74 seed_random (SSDATA (limit), SBYTES (limit));
76 val = get_random ();
77 if (INTEGERP (limit) && 0 < XINT (limit))
78 while (true)
80 /* Return the remainder, except reject the rare case where
81 get_random returns a number so close to INTMASK that the
82 remainder isn't random. */
83 EMACS_INT remainder = val % XINT (limit);
84 if (val - remainder <= INTMASK - XINT (limit) + 1)
85 return make_number (remainder);
86 val = get_random ();
88 return make_number (val);
91 /* Heuristic on how many iterations of a tight loop can be safely done
92 before it's time to do a QUIT. This must be a power of 2. */
93 enum { QUIT_COUNT_HEURISTIC = 1 << 16 };
95 /* Random data-structure functions. */
97 static void
98 CHECK_LIST_END (Lisp_Object x, Lisp_Object y)
100 CHECK_TYPE (NILP (x), Qlistp, y);
103 DEFUN ("length", Flength, Slength, 1, 1, 0,
104 doc: /* Return the length of vector, list or string SEQUENCE.
105 A byte-code function object is also allowed.
106 If the string contains multibyte characters, this is not necessarily
107 the number of bytes in the string; it is the number of characters.
108 To get the number of bytes, use `string-bytes'. */)
109 (register Lisp_Object sequence)
111 register Lisp_Object val;
113 if (STRINGP (sequence))
114 XSETFASTINT (val, SCHARS (sequence));
115 else if (VECTORP (sequence))
116 XSETFASTINT (val, ASIZE (sequence));
117 else if (CHAR_TABLE_P (sequence))
118 XSETFASTINT (val, MAX_CHAR);
119 else if (BOOL_VECTOR_P (sequence))
120 XSETFASTINT (val, bool_vector_size (sequence));
121 else if (COMPILEDP (sequence))
122 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
123 else if (CONSP (sequence))
125 EMACS_INT i = 0;
129 ++i;
130 if ((i & (QUIT_COUNT_HEURISTIC - 1)) == 0)
132 if (MOST_POSITIVE_FIXNUM < i)
133 error ("List too long");
134 QUIT;
136 sequence = XCDR (sequence);
138 while (CONSP (sequence));
140 CHECK_LIST_END (sequence, sequence);
142 val = make_number (i);
144 else if (NILP (sequence))
145 XSETFASTINT (val, 0);
146 else
147 wrong_type_argument (Qsequencep, sequence);
149 return val;
152 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
153 doc: /* Return the length of a list, but avoid error or infinite loop.
154 This function never gets an error. If LIST is not really a list,
155 it returns 0. If LIST is circular, it returns a finite value
156 which is at least the number of distinct elements. */)
157 (Lisp_Object list)
159 Lisp_Object tail, halftail;
160 double hilen = 0;
161 uintmax_t lolen = 1;
163 if (! CONSP (list))
164 return make_number (0);
166 /* halftail is used to detect circular lists. */
167 for (tail = halftail = list; ; )
169 tail = XCDR (tail);
170 if (! CONSP (tail))
171 break;
172 if (EQ (tail, halftail))
173 break;
174 lolen++;
175 if ((lolen & 1) == 0)
177 halftail = XCDR (halftail);
178 if ((lolen & (QUIT_COUNT_HEURISTIC - 1)) == 0)
180 QUIT;
181 if (lolen == 0)
182 hilen += UINTMAX_MAX + 1.0;
187 /* If the length does not fit into a fixnum, return a float.
188 On all known practical machines this returns an upper bound on
189 the true length. */
190 return hilen ? make_float (hilen + lolen) : make_fixnum_or_float (lolen);
193 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
194 doc: /* Return the number of bytes in STRING.
195 If STRING is multibyte, this may be greater than the length of STRING. */)
196 (Lisp_Object string)
198 CHECK_STRING (string);
199 return make_number (SBYTES (string));
202 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
203 doc: /* Return t if two strings have identical contents.
204 Case is significant, but text properties are ignored.
205 Symbols are also allowed; their print names are used instead. */)
206 (register Lisp_Object s1, Lisp_Object s2)
208 if (SYMBOLP (s1))
209 s1 = SYMBOL_NAME (s1);
210 if (SYMBOLP (s2))
211 s2 = SYMBOL_NAME (s2);
212 CHECK_STRING (s1);
213 CHECK_STRING (s2);
215 if (SCHARS (s1) != SCHARS (s2)
216 || SBYTES (s1) != SBYTES (s2)
217 || memcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
218 return Qnil;
219 return Qt;
222 DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
223 doc: /* Compare the contents of two strings, converting to multibyte if needed.
224 The arguments START1, END1, START2, and END2, if non-nil, are
225 positions specifying which parts of STR1 or STR2 to compare. In
226 string STR1, compare the part between START1 (inclusive) and END1
227 \(exclusive). If START1 is nil, it defaults to 0, the beginning of
228 the string; if END1 is nil, it defaults to the length of the string.
229 Likewise, in string STR2, compare the part between START2 and END2.
230 Like in `substring', negative values are counted from the end.
232 The strings are compared by the numeric values of their characters.
233 For instance, STR1 is "less than" STR2 if its first differing
234 character has a smaller numeric value. If IGNORE-CASE is non-nil,
235 characters are converted to lower-case before comparing them. Unibyte
236 strings are converted to multibyte for comparison.
238 The value is t if the strings (or specified portions) match.
239 If string STR1 is less, the value is a negative number N;
240 - 1 - N is the number of characters that match at the beginning.
241 If string STR1 is greater, the value is a positive number N;
242 N - 1 is the number of characters that match at the beginning. */)
243 (Lisp_Object str1, Lisp_Object start1, Lisp_Object end1, Lisp_Object str2,
244 Lisp_Object start2, Lisp_Object end2, Lisp_Object ignore_case)
246 ptrdiff_t from1, to1, from2, to2, i1, i1_byte, i2, i2_byte;
248 CHECK_STRING (str1);
249 CHECK_STRING (str2);
251 /* For backward compatibility, silently bring too-large positive end
252 values into range. */
253 if (INTEGERP (end1) && SCHARS (str1) < XINT (end1))
254 end1 = make_number (SCHARS (str1));
255 if (INTEGERP (end2) && SCHARS (str2) < XINT (end2))
256 end2 = make_number (SCHARS (str2));
258 validate_subarray (str1, start1, end1, SCHARS (str1), &from1, &to1);
259 validate_subarray (str2, start2, end2, SCHARS (str2), &from2, &to2);
261 i1 = from1;
262 i2 = from2;
264 i1_byte = string_char_to_byte (str1, i1);
265 i2_byte = string_char_to_byte (str2, i2);
267 while (i1 < to1 && i2 < to2)
269 /* When we find a mismatch, we must compare the
270 characters, not just the bytes. */
271 int c1, c2;
273 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c1, str1, i1, i1_byte);
274 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c2, str2, i2, i2_byte);
276 if (c1 == c2)
277 continue;
279 if (! NILP (ignore_case))
281 c1 = XINT (Fupcase (make_number (c1)));
282 c2 = XINT (Fupcase (make_number (c2)));
285 if (c1 == c2)
286 continue;
288 /* Note that I1 has already been incremented
289 past the character that we are comparing;
290 hence we don't add or subtract 1 here. */
291 if (c1 < c2)
292 return make_number (- i1 + from1);
293 else
294 return make_number (i1 - from1);
297 if (i1 < to1)
298 return make_number (i1 - from1 + 1);
299 if (i2 < to2)
300 return make_number (- i1 + from1 - 1);
302 return Qt;
305 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
306 doc: /* Return non-nil if STRING1 is less than STRING2 in lexicographic order.
307 Case is significant.
308 Symbols are also allowed; their print names are used instead. */)
309 (register Lisp_Object string1, Lisp_Object string2)
311 register ptrdiff_t end;
312 register ptrdiff_t i1, i1_byte, i2, i2_byte;
314 if (SYMBOLP (string1))
315 string1 = SYMBOL_NAME (string1);
316 if (SYMBOLP (string2))
317 string2 = SYMBOL_NAME (string2);
318 CHECK_STRING (string1);
319 CHECK_STRING (string2);
321 i1 = i1_byte = i2 = i2_byte = 0;
323 end = SCHARS (string1);
324 if (end > SCHARS (string2))
325 end = SCHARS (string2);
327 while (i1 < end)
329 /* When we find a mismatch, we must compare the
330 characters, not just the bytes. */
331 int c1, c2;
333 FETCH_STRING_CHAR_ADVANCE (c1, string1, i1, i1_byte);
334 FETCH_STRING_CHAR_ADVANCE (c2, string2, i2, i2_byte);
336 if (c1 != c2)
337 return c1 < c2 ? Qt : Qnil;
339 return i1 < SCHARS (string2) ? Qt : Qnil;
342 DEFUN ("string-collate-lessp", Fstring_collate_lessp, Sstring_collate_lessp, 2, 4, 0,
343 doc: /* Return t if first arg string is less than second in collation order.
344 Symbols are also allowed; their print names are used instead.
346 This function obeys the conventions for collation order in your
347 locale settings. For example, punctuation and whitespace characters
348 might be considered less significant for sorting:
350 \(sort '\("11" "12" "1 1" "1 2" "1.1" "1.2") \\='string-collate-lessp)
351 => \("11" "1 1" "1.1" "12" "1 2" "1.2")
353 The optional argument LOCALE, a string, overrides the setting of your
354 current locale identifier for collation. The value is system
355 dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
356 while it would be, e.g., \"enu_USA.1252\" on MS-Windows systems.
358 If IGNORE-CASE is non-nil, characters are converted to lower-case
359 before comparing them.
361 To emulate Unicode-compliant collation on MS-Windows systems,
362 bind `w32-collate-ignore-punctuation' to a non-nil value, since
363 the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
365 If your system does not support a locale environment, this function
366 behaves like `string-lessp'. */)
367 (Lisp_Object s1, Lisp_Object s2, Lisp_Object locale, Lisp_Object ignore_case)
369 #if defined __STDC_ISO_10646__ || defined WINDOWSNT
370 /* Check parameters. */
371 if (SYMBOLP (s1))
372 s1 = SYMBOL_NAME (s1);
373 if (SYMBOLP (s2))
374 s2 = SYMBOL_NAME (s2);
375 CHECK_STRING (s1);
376 CHECK_STRING (s2);
377 if (!NILP (locale))
378 CHECK_STRING (locale);
380 return (str_collate (s1, s2, locale, ignore_case) < 0) ? Qt : Qnil;
382 #else /* !__STDC_ISO_10646__, !WINDOWSNT */
383 return Fstring_lessp (s1, s2);
384 #endif /* !__STDC_ISO_10646__, !WINDOWSNT */
387 DEFUN ("string-collate-equalp", Fstring_collate_equalp, Sstring_collate_equalp, 2, 4, 0,
388 doc: /* Return t if two strings have identical contents.
389 Symbols are also allowed; their print names are used instead.
391 This function obeys the conventions for collation order in your locale
392 settings. For example, characters with different coding points but
393 the same meaning might be considered as equal, like different grave
394 accent Unicode characters:
396 \(string-collate-equalp \(string ?\\uFF40) \(string ?\\u1FEF))
397 => t
399 The optional argument LOCALE, a string, overrides the setting of your
400 current locale identifier for collation. The value is system
401 dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
402 while it would be \"enu_USA.1252\" on MS Windows systems.
404 If IGNORE-CASE is non-nil, characters are converted to lower-case
405 before comparing them.
407 To emulate Unicode-compliant collation on MS-Windows systems,
408 bind `w32-collate-ignore-punctuation' to a non-nil value, since
409 the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
411 If your system does not support a locale environment, this function
412 behaves like `string-equal'.
414 Do NOT use this function to compare file names for equality, only
415 for sorting them. */)
416 (Lisp_Object s1, Lisp_Object s2, Lisp_Object locale, Lisp_Object ignore_case)
418 #if defined __STDC_ISO_10646__ || defined WINDOWSNT
419 /* Check parameters. */
420 if (SYMBOLP (s1))
421 s1 = SYMBOL_NAME (s1);
422 if (SYMBOLP (s2))
423 s2 = SYMBOL_NAME (s2);
424 CHECK_STRING (s1);
425 CHECK_STRING (s2);
426 if (!NILP (locale))
427 CHECK_STRING (locale);
429 return (str_collate (s1, s2, locale, ignore_case) == 0) ? Qt : Qnil;
431 #else /* !__STDC_ISO_10646__, !WINDOWSNT */
432 return Fstring_equal (s1, s2);
433 #endif /* !__STDC_ISO_10646__, !WINDOWSNT */
436 static Lisp_Object concat (ptrdiff_t nargs, Lisp_Object *args,
437 enum Lisp_Type target_type, bool last_special);
439 /* ARGSUSED */
440 Lisp_Object
441 concat2 (Lisp_Object s1, Lisp_Object s2)
443 return concat (2, ((Lisp_Object []) {s1, s2}), Lisp_String, 0);
446 /* ARGSUSED */
447 Lisp_Object
448 concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
450 return concat (3, ((Lisp_Object []) {s1, s2, s3}), Lisp_String, 0);
453 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
454 doc: /* Concatenate all the arguments and make the result a list.
455 The result is a list whose elements are the elements of all the arguments.
456 Each argument may be a list, vector or string.
457 The last argument is not copied, just used as the tail of the new list.
458 usage: (append &rest SEQUENCES) */)
459 (ptrdiff_t nargs, Lisp_Object *args)
461 return concat (nargs, args, Lisp_Cons, 1);
464 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
465 doc: /* Concatenate all the arguments and make the result a string.
466 The result is a string whose elements are the elements of all the arguments.
467 Each argument may be a string or a list or vector of characters (integers).
468 usage: (concat &rest SEQUENCES) */)
469 (ptrdiff_t nargs, Lisp_Object *args)
471 return concat (nargs, args, Lisp_String, 0);
474 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
475 doc: /* Concatenate all the arguments and make the result a vector.
476 The result is a vector whose elements are the elements of all the arguments.
477 Each argument may be a list, vector or string.
478 usage: (vconcat &rest SEQUENCES) */)
479 (ptrdiff_t nargs, Lisp_Object *args)
481 return concat (nargs, args, Lisp_Vectorlike, 0);
485 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
486 doc: /* Return a copy of a list, vector, string or char-table.
487 The elements of a list or vector are not copied; they are shared
488 with the original. */)
489 (Lisp_Object arg)
491 if (NILP (arg)) return arg;
493 if (CHAR_TABLE_P (arg))
495 return copy_char_table (arg);
498 if (BOOL_VECTOR_P (arg))
500 EMACS_INT nbits = bool_vector_size (arg);
501 ptrdiff_t nbytes = bool_vector_bytes (nbits);
502 Lisp_Object val = make_uninit_bool_vector (nbits);
503 memcpy (bool_vector_data (val), bool_vector_data (arg), nbytes);
504 return val;
507 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
508 wrong_type_argument (Qsequencep, arg);
510 return concat (1, &arg, XTYPE (arg), 0);
513 /* This structure holds information of an argument of `concat' that is
514 a string and has text properties to be copied. */
515 struct textprop_rec
517 ptrdiff_t argnum; /* refer to ARGS (arguments of `concat') */
518 ptrdiff_t from; /* refer to ARGS[argnum] (argument string) */
519 ptrdiff_t to; /* refer to VAL (the target string) */
522 static Lisp_Object
523 concat (ptrdiff_t nargs, Lisp_Object *args,
524 enum Lisp_Type target_type, bool last_special)
526 Lisp_Object val;
527 Lisp_Object tail;
528 Lisp_Object this;
529 ptrdiff_t toindex;
530 ptrdiff_t toindex_byte = 0;
531 EMACS_INT result_len;
532 EMACS_INT result_len_byte;
533 ptrdiff_t argnum;
534 Lisp_Object last_tail;
535 Lisp_Object prev;
536 bool some_multibyte;
537 /* When we make a multibyte string, we can't copy text properties
538 while concatenating each string because the length of resulting
539 string can't be decided until we finish the whole concatenation.
540 So, we record strings that have text properties to be copied
541 here, and copy the text properties after the concatenation. */
542 struct textprop_rec *textprops = NULL;
543 /* Number of elements in textprops. */
544 ptrdiff_t num_textprops = 0;
545 USE_SAFE_ALLOCA;
547 tail = Qnil;
549 /* In append, the last arg isn't treated like the others */
550 if (last_special && nargs > 0)
552 nargs--;
553 last_tail = args[nargs];
555 else
556 last_tail = Qnil;
558 /* Check each argument. */
559 for (argnum = 0; argnum < nargs; argnum++)
561 this = args[argnum];
562 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
563 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
564 wrong_type_argument (Qsequencep, this);
567 /* Compute total length in chars of arguments in RESULT_LEN.
568 If desired output is a string, also compute length in bytes
569 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
570 whether the result should be a multibyte string. */
571 result_len_byte = 0;
572 result_len = 0;
573 some_multibyte = 0;
574 for (argnum = 0; argnum < nargs; argnum++)
576 EMACS_INT len;
577 this = args[argnum];
578 len = XFASTINT (Flength (this));
579 if (target_type == Lisp_String)
581 /* We must count the number of bytes needed in the string
582 as well as the number of characters. */
583 ptrdiff_t i;
584 Lisp_Object ch;
585 int c;
586 ptrdiff_t this_len_byte;
588 if (VECTORP (this) || COMPILEDP (this))
589 for (i = 0; i < len; i++)
591 ch = AREF (this, i);
592 CHECK_CHARACTER (ch);
593 c = XFASTINT (ch);
594 this_len_byte = CHAR_BYTES (c);
595 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
596 string_overflow ();
597 result_len_byte += this_len_byte;
598 if (! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
599 some_multibyte = 1;
601 else if (BOOL_VECTOR_P (this) && bool_vector_size (this) > 0)
602 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
603 else if (CONSP (this))
604 for (; CONSP (this); this = XCDR (this))
606 ch = XCAR (this);
607 CHECK_CHARACTER (ch);
608 c = XFASTINT (ch);
609 this_len_byte = CHAR_BYTES (c);
610 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
611 string_overflow ();
612 result_len_byte += this_len_byte;
613 if (! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
614 some_multibyte = 1;
616 else if (STRINGP (this))
618 if (STRING_MULTIBYTE (this))
620 some_multibyte = 1;
621 this_len_byte = SBYTES (this);
623 else
624 this_len_byte = count_size_as_multibyte (SDATA (this),
625 SCHARS (this));
626 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
627 string_overflow ();
628 result_len_byte += this_len_byte;
632 result_len += len;
633 if (MOST_POSITIVE_FIXNUM < result_len)
634 memory_full (SIZE_MAX);
637 if (! some_multibyte)
638 result_len_byte = result_len;
640 /* Create the output object. */
641 if (target_type == Lisp_Cons)
642 val = Fmake_list (make_number (result_len), Qnil);
643 else if (target_type == Lisp_Vectorlike)
644 val = Fmake_vector (make_number (result_len), Qnil);
645 else if (some_multibyte)
646 val = make_uninit_multibyte_string (result_len, result_len_byte);
647 else
648 val = make_uninit_string (result_len);
650 /* In `append', if all but last arg are nil, return last arg. */
651 if (target_type == Lisp_Cons && EQ (val, Qnil))
652 return last_tail;
654 /* Copy the contents of the args into the result. */
655 if (CONSP (val))
656 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
657 else
658 toindex = 0, toindex_byte = 0;
660 prev = Qnil;
661 if (STRINGP (val))
662 SAFE_NALLOCA (textprops, 1, nargs);
664 for (argnum = 0; argnum < nargs; argnum++)
666 Lisp_Object thislen;
667 ptrdiff_t thisleni = 0;
668 register ptrdiff_t thisindex = 0;
669 register ptrdiff_t thisindex_byte = 0;
671 this = args[argnum];
672 if (!CONSP (this))
673 thislen = Flength (this), thisleni = XINT (thislen);
675 /* Between strings of the same kind, copy fast. */
676 if (STRINGP (this) && STRINGP (val)
677 && STRING_MULTIBYTE (this) == some_multibyte)
679 ptrdiff_t thislen_byte = SBYTES (this);
681 memcpy (SDATA (val) + toindex_byte, SDATA (this), SBYTES (this));
682 if (string_intervals (this))
684 textprops[num_textprops].argnum = argnum;
685 textprops[num_textprops].from = 0;
686 textprops[num_textprops++].to = toindex;
688 toindex_byte += thislen_byte;
689 toindex += thisleni;
691 /* Copy a single-byte string to a multibyte string. */
692 else if (STRINGP (this) && STRINGP (val))
694 if (string_intervals (this))
696 textprops[num_textprops].argnum = argnum;
697 textprops[num_textprops].from = 0;
698 textprops[num_textprops++].to = toindex;
700 toindex_byte += copy_text (SDATA (this),
701 SDATA (val) + toindex_byte,
702 SCHARS (this), 0, 1);
703 toindex += thisleni;
705 else
706 /* Copy element by element. */
707 while (1)
709 register Lisp_Object elt;
711 /* Fetch next element of `this' arg into `elt', or break if
712 `this' is exhausted. */
713 if (NILP (this)) break;
714 if (CONSP (this))
715 elt = XCAR (this), this = XCDR (this);
716 else if (thisindex >= thisleni)
717 break;
718 else if (STRINGP (this))
720 int c;
721 if (STRING_MULTIBYTE (this))
722 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
723 thisindex,
724 thisindex_byte);
725 else
727 c = SREF (this, thisindex); thisindex++;
728 if (some_multibyte && !ASCII_CHAR_P (c))
729 c = BYTE8_TO_CHAR (c);
731 XSETFASTINT (elt, c);
733 else if (BOOL_VECTOR_P (this))
735 elt = bool_vector_ref (this, thisindex);
736 thisindex++;
738 else
740 elt = AREF (this, thisindex);
741 thisindex++;
744 /* Store this element into the result. */
745 if (toindex < 0)
747 XSETCAR (tail, elt);
748 prev = tail;
749 tail = XCDR (tail);
751 else if (VECTORP (val))
753 ASET (val, toindex, elt);
754 toindex++;
756 else
758 int c;
759 CHECK_CHARACTER (elt);
760 c = XFASTINT (elt);
761 if (some_multibyte)
762 toindex_byte += CHAR_STRING (c, SDATA (val) + toindex_byte);
763 else
764 SSET (val, toindex_byte++, c);
765 toindex++;
769 if (!NILP (prev))
770 XSETCDR (prev, last_tail);
772 if (num_textprops > 0)
774 Lisp_Object props;
775 ptrdiff_t last_to_end = -1;
777 for (argnum = 0; argnum < num_textprops; argnum++)
779 this = args[textprops[argnum].argnum];
780 props = text_property_list (this,
781 make_number (0),
782 make_number (SCHARS (this)),
783 Qnil);
784 /* If successive arguments have properties, be sure that the
785 value of `composition' property be the copy. */
786 if (last_to_end == textprops[argnum].to)
787 make_composition_value_copy (props);
788 add_text_properties_from_list (val, props,
789 make_number (textprops[argnum].to));
790 last_to_end = textprops[argnum].to + SCHARS (this);
794 SAFE_FREE ();
795 return val;
798 static Lisp_Object string_char_byte_cache_string;
799 static ptrdiff_t string_char_byte_cache_charpos;
800 static ptrdiff_t string_char_byte_cache_bytepos;
802 void
803 clear_string_char_byte_cache (void)
805 string_char_byte_cache_string = Qnil;
808 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
810 ptrdiff_t
811 string_char_to_byte (Lisp_Object string, ptrdiff_t char_index)
813 ptrdiff_t i_byte;
814 ptrdiff_t best_below, best_below_byte;
815 ptrdiff_t best_above, best_above_byte;
817 best_below = best_below_byte = 0;
818 best_above = SCHARS (string);
819 best_above_byte = SBYTES (string);
820 if (best_above == best_above_byte)
821 return char_index;
823 if (EQ (string, string_char_byte_cache_string))
825 if (string_char_byte_cache_charpos < char_index)
827 best_below = string_char_byte_cache_charpos;
828 best_below_byte = string_char_byte_cache_bytepos;
830 else
832 best_above = string_char_byte_cache_charpos;
833 best_above_byte = string_char_byte_cache_bytepos;
837 if (char_index - best_below < best_above - char_index)
839 unsigned char *p = SDATA (string) + best_below_byte;
841 while (best_below < char_index)
843 p += BYTES_BY_CHAR_HEAD (*p);
844 best_below++;
846 i_byte = p - SDATA (string);
848 else
850 unsigned char *p = SDATA (string) + best_above_byte;
852 while (best_above > char_index)
854 p--;
855 while (!CHAR_HEAD_P (*p)) p--;
856 best_above--;
858 i_byte = p - SDATA (string);
861 string_char_byte_cache_bytepos = i_byte;
862 string_char_byte_cache_charpos = char_index;
863 string_char_byte_cache_string = string;
865 return i_byte;
868 /* Return the character index corresponding to BYTE_INDEX in STRING. */
870 ptrdiff_t
871 string_byte_to_char (Lisp_Object string, ptrdiff_t byte_index)
873 ptrdiff_t i, i_byte;
874 ptrdiff_t best_below, best_below_byte;
875 ptrdiff_t best_above, best_above_byte;
877 best_below = best_below_byte = 0;
878 best_above = SCHARS (string);
879 best_above_byte = SBYTES (string);
880 if (best_above == best_above_byte)
881 return byte_index;
883 if (EQ (string, string_char_byte_cache_string))
885 if (string_char_byte_cache_bytepos < byte_index)
887 best_below = string_char_byte_cache_charpos;
888 best_below_byte = string_char_byte_cache_bytepos;
890 else
892 best_above = string_char_byte_cache_charpos;
893 best_above_byte = string_char_byte_cache_bytepos;
897 if (byte_index - best_below_byte < best_above_byte - byte_index)
899 unsigned char *p = SDATA (string) + best_below_byte;
900 unsigned char *pend = SDATA (string) + byte_index;
902 while (p < pend)
904 p += BYTES_BY_CHAR_HEAD (*p);
905 best_below++;
907 i = best_below;
908 i_byte = p - SDATA (string);
910 else
912 unsigned char *p = SDATA (string) + best_above_byte;
913 unsigned char *pbeg = SDATA (string) + byte_index;
915 while (p > pbeg)
917 p--;
918 while (!CHAR_HEAD_P (*p)) p--;
919 best_above--;
921 i = best_above;
922 i_byte = p - SDATA (string);
925 string_char_byte_cache_bytepos = i_byte;
926 string_char_byte_cache_charpos = i;
927 string_char_byte_cache_string = string;
929 return i;
932 /* Convert STRING to a multibyte string. */
934 static Lisp_Object
935 string_make_multibyte (Lisp_Object string)
937 unsigned char *buf;
938 ptrdiff_t nbytes;
939 Lisp_Object ret;
940 USE_SAFE_ALLOCA;
942 if (STRING_MULTIBYTE (string))
943 return string;
945 nbytes = count_size_as_multibyte (SDATA (string),
946 SCHARS (string));
947 /* If all the chars are ASCII, they won't need any more bytes
948 once converted. In that case, we can return STRING itself. */
949 if (nbytes == SBYTES (string))
950 return string;
952 buf = SAFE_ALLOCA (nbytes);
953 copy_text (SDATA (string), buf, SBYTES (string),
954 0, 1);
956 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
957 SAFE_FREE ();
959 return ret;
963 /* Convert STRING (if unibyte) to a multibyte string without changing
964 the number of characters. Characters 0200 trough 0237 are
965 converted to eight-bit characters. */
967 Lisp_Object
968 string_to_multibyte (Lisp_Object string)
970 unsigned char *buf;
971 ptrdiff_t nbytes;
972 Lisp_Object ret;
973 USE_SAFE_ALLOCA;
975 if (STRING_MULTIBYTE (string))
976 return string;
978 nbytes = count_size_as_multibyte (SDATA (string), SBYTES (string));
979 /* If all the chars are ASCII, they won't need any more bytes once
980 converted. */
981 if (nbytes == SBYTES (string))
982 return make_multibyte_string (SSDATA (string), nbytes, nbytes);
984 buf = SAFE_ALLOCA (nbytes);
985 memcpy (buf, SDATA (string), SBYTES (string));
986 str_to_multibyte (buf, nbytes, SBYTES (string));
988 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
989 SAFE_FREE ();
991 return ret;
995 /* Convert STRING to a single-byte string. */
997 Lisp_Object
998 string_make_unibyte (Lisp_Object string)
1000 ptrdiff_t nchars;
1001 unsigned char *buf;
1002 Lisp_Object ret;
1003 USE_SAFE_ALLOCA;
1005 if (! STRING_MULTIBYTE (string))
1006 return string;
1008 nchars = SCHARS (string);
1010 buf = SAFE_ALLOCA (nchars);
1011 copy_text (SDATA (string), buf, SBYTES (string),
1012 1, 0);
1014 ret = make_unibyte_string ((char *) buf, nchars);
1015 SAFE_FREE ();
1017 return ret;
1020 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1021 1, 1, 0,
1022 doc: /* Return the multibyte equivalent of STRING.
1023 If STRING is unibyte and contains non-ASCII characters, the function
1024 `unibyte-char-to-multibyte' is used to convert each unibyte character
1025 to a multibyte character. In this case, the returned string is a
1026 newly created string with no text properties. If STRING is multibyte
1027 or entirely ASCII, it is returned unchanged. In particular, when
1028 STRING is unibyte and entirely ASCII, the returned string is unibyte.
1029 \(When the characters are all ASCII, Emacs primitives will treat the
1030 string the same way whether it is unibyte or multibyte.) */)
1031 (Lisp_Object string)
1033 CHECK_STRING (string);
1035 return string_make_multibyte (string);
1038 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1039 1, 1, 0,
1040 doc: /* Return the unibyte equivalent of STRING.
1041 Multibyte character codes are converted to unibyte according to
1042 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1043 If the lookup in the translation table fails, this function takes just
1044 the low 8 bits of each character. */)
1045 (Lisp_Object string)
1047 CHECK_STRING (string);
1049 return string_make_unibyte (string);
1052 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1053 1, 1, 0,
1054 doc: /* Return a unibyte string with the same individual bytes as STRING.
1055 If STRING is unibyte, the result is STRING itself.
1056 Otherwise it is a newly created string, with no text properties.
1057 If STRING is multibyte and contains a character of charset
1058 `eight-bit', it is converted to the corresponding single byte. */)
1059 (Lisp_Object string)
1061 CHECK_STRING (string);
1063 if (STRING_MULTIBYTE (string))
1065 unsigned char *str = (unsigned char *) xlispstrdup (string);
1066 ptrdiff_t bytes = str_as_unibyte (str, SBYTES (string));
1068 string = make_unibyte_string ((char *) str, bytes);
1069 xfree (str);
1071 return string;
1074 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1075 1, 1, 0,
1076 doc: /* Return a multibyte string with the same individual bytes as STRING.
1077 If STRING is multibyte, the result is STRING itself.
1078 Otherwise it is a newly created string, with no text properties.
1080 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1081 part of a correct utf-8 sequence), it is converted to the corresponding
1082 multibyte character of charset `eight-bit'.
1083 See also `string-to-multibyte'.
1085 Beware, this often doesn't really do what you think it does.
1086 It is similar to (decode-coding-string STRING \\='utf-8-emacs).
1087 If you're not sure, whether to use `string-as-multibyte' or
1088 `string-to-multibyte', use `string-to-multibyte'. */)
1089 (Lisp_Object string)
1091 CHECK_STRING (string);
1093 if (! STRING_MULTIBYTE (string))
1095 Lisp_Object new_string;
1096 ptrdiff_t nchars, nbytes;
1098 parse_str_as_multibyte (SDATA (string),
1099 SBYTES (string),
1100 &nchars, &nbytes);
1101 new_string = make_uninit_multibyte_string (nchars, nbytes);
1102 memcpy (SDATA (new_string), SDATA (string), SBYTES (string));
1103 if (nbytes != SBYTES (string))
1104 str_as_multibyte (SDATA (new_string), nbytes,
1105 SBYTES (string), NULL);
1106 string = new_string;
1107 set_string_intervals (string, NULL);
1109 return string;
1112 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1113 1, 1, 0,
1114 doc: /* Return a multibyte string with the same individual chars as STRING.
1115 If STRING is multibyte, the result is STRING itself.
1116 Otherwise it is a newly created string, with no text properties.
1118 If STRING is unibyte and contains an 8-bit byte, it is converted to
1119 the corresponding multibyte character of charset `eight-bit'.
1121 This differs from `string-as-multibyte' by converting each byte of a correct
1122 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1123 correct sequence. */)
1124 (Lisp_Object string)
1126 CHECK_STRING (string);
1128 return string_to_multibyte (string);
1131 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1132 1, 1, 0,
1133 doc: /* Return a unibyte string with the same individual chars as STRING.
1134 If STRING is unibyte, the result is STRING itself.
1135 Otherwise it is a newly created string, with no text properties,
1136 where each `eight-bit' character is converted to the corresponding byte.
1137 If STRING contains a non-ASCII, non-`eight-bit' character,
1138 an error is signaled. */)
1139 (Lisp_Object string)
1141 CHECK_STRING (string);
1143 if (STRING_MULTIBYTE (string))
1145 ptrdiff_t chars = SCHARS (string);
1146 unsigned char *str = xmalloc (chars);
1147 ptrdiff_t converted = str_to_unibyte (SDATA (string), str, chars);
1149 if (converted < chars)
1150 error ("Can't convert the %"pD"dth character to unibyte", converted);
1151 string = make_unibyte_string ((char *) str, chars);
1152 xfree (str);
1154 return string;
1158 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1159 doc: /* Return a copy of ALIST.
1160 This is an alist which represents the same mapping from objects to objects,
1161 but does not share the alist structure with ALIST.
1162 The objects mapped (cars and cdrs of elements of the alist)
1163 are shared, however.
1164 Elements of ALIST that are not conses are also shared. */)
1165 (Lisp_Object alist)
1167 register Lisp_Object tem;
1169 CHECK_LIST (alist);
1170 if (NILP (alist))
1171 return alist;
1172 alist = concat (1, &alist, Lisp_Cons, 0);
1173 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1175 register Lisp_Object car;
1176 car = XCAR (tem);
1178 if (CONSP (car))
1179 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1181 return alist;
1184 /* Check that ARRAY can have a valid subarray [FROM..TO),
1185 given that its size is SIZE.
1186 If FROM is nil, use 0; if TO is nil, use SIZE.
1187 Count negative values backwards from the end.
1188 Set *IFROM and *ITO to the two indexes used. */
1190 void
1191 validate_subarray (Lisp_Object array, Lisp_Object from, Lisp_Object to,
1192 ptrdiff_t size, ptrdiff_t *ifrom, ptrdiff_t *ito)
1194 EMACS_INT f, t;
1196 if (INTEGERP (from))
1198 f = XINT (from);
1199 if (f < 0)
1200 f += size;
1202 else if (NILP (from))
1203 f = 0;
1204 else
1205 wrong_type_argument (Qintegerp, from);
1207 if (INTEGERP (to))
1209 t = XINT (to);
1210 if (t < 0)
1211 t += size;
1213 else if (NILP (to))
1214 t = size;
1215 else
1216 wrong_type_argument (Qintegerp, to);
1218 if (! (0 <= f && f <= t && t <= size))
1219 args_out_of_range_3 (array, from, to);
1221 *ifrom = f;
1222 *ito = t;
1225 DEFUN ("substring", Fsubstring, Ssubstring, 1, 3, 0,
1226 doc: /* Return a new string whose contents are a substring of STRING.
1227 The returned string consists of the characters between index FROM
1228 \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1229 zero-indexed: 0 means the first character of STRING. Negative values
1230 are counted from the end of STRING. If TO is nil, the substring runs
1231 to the end of STRING.
1233 The STRING argument may also be a vector. In that case, the return
1234 value is a new vector that contains the elements between index FROM
1235 \(inclusive) and index TO (exclusive) of that vector argument.
1237 With one argument, just copy STRING (with properties, if any). */)
1238 (Lisp_Object string, Lisp_Object from, Lisp_Object to)
1240 Lisp_Object res;
1241 ptrdiff_t size, ifrom, ito;
1243 size = CHECK_VECTOR_OR_STRING (string);
1244 validate_subarray (string, from, to, size, &ifrom, &ito);
1246 if (STRINGP (string))
1248 ptrdiff_t from_byte
1249 = !ifrom ? 0 : string_char_to_byte (string, ifrom);
1250 ptrdiff_t to_byte
1251 = ito == size ? SBYTES (string) : string_char_to_byte (string, ito);
1252 res = make_specified_string (SSDATA (string) + from_byte,
1253 ito - ifrom, to_byte - from_byte,
1254 STRING_MULTIBYTE (string));
1255 copy_text_properties (make_number (ifrom), make_number (ito),
1256 string, make_number (0), res, Qnil);
1258 else
1259 res = Fvector (ito - ifrom, aref_addr (string, ifrom));
1261 return res;
1265 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1266 doc: /* Return a substring of STRING, without text properties.
1267 It starts at index FROM and ends before TO.
1268 TO may be nil or omitted; then the substring runs to the end of STRING.
1269 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1270 If FROM or TO is negative, it counts from the end.
1272 With one argument, just copy STRING without its properties. */)
1273 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
1275 ptrdiff_t from_char, to_char, from_byte, to_byte, size;
1277 CHECK_STRING (string);
1279 size = SCHARS (string);
1280 validate_subarray (string, from, to, size, &from_char, &to_char);
1282 from_byte = !from_char ? 0 : string_char_to_byte (string, from_char);
1283 to_byte =
1284 to_char == size ? SBYTES (string) : string_char_to_byte (string, to_char);
1285 return make_specified_string (SSDATA (string) + from_byte,
1286 to_char - from_char, to_byte - from_byte,
1287 STRING_MULTIBYTE (string));
1290 /* Extract a substring of STRING, giving start and end positions
1291 both in characters and in bytes. */
1293 Lisp_Object
1294 substring_both (Lisp_Object string, ptrdiff_t from, ptrdiff_t from_byte,
1295 ptrdiff_t to, ptrdiff_t to_byte)
1297 Lisp_Object res;
1298 ptrdiff_t size = CHECK_VECTOR_OR_STRING (string);
1300 if (!(0 <= from && from <= to && to <= size))
1301 args_out_of_range_3 (string, make_number (from), make_number (to));
1303 if (STRINGP (string))
1305 res = make_specified_string (SSDATA (string) + from_byte,
1306 to - from, to_byte - from_byte,
1307 STRING_MULTIBYTE (string));
1308 copy_text_properties (make_number (from), make_number (to),
1309 string, make_number (0), res, Qnil);
1311 else
1312 res = Fvector (to - from, aref_addr (string, from));
1314 return res;
1317 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1318 doc: /* Take cdr N times on LIST, return the result. */)
1319 (Lisp_Object n, Lisp_Object list)
1321 EMACS_INT i, num;
1322 CHECK_NUMBER (n);
1323 num = XINT (n);
1324 for (i = 0; i < num && !NILP (list); i++)
1326 QUIT;
1327 CHECK_LIST_CONS (list, list);
1328 list = XCDR (list);
1330 return list;
1333 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1334 doc: /* Return the Nth element of LIST.
1335 N counts from zero. If LIST is not that long, nil is returned. */)
1336 (Lisp_Object n, Lisp_Object list)
1338 return Fcar (Fnthcdr (n, list));
1341 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1342 doc: /* Return element of SEQUENCE at index N. */)
1343 (register Lisp_Object sequence, Lisp_Object n)
1345 CHECK_NUMBER (n);
1346 if (CONSP (sequence) || NILP (sequence))
1347 return Fcar (Fnthcdr (n, sequence));
1349 /* Faref signals a "not array" error, so check here. */
1350 CHECK_ARRAY (sequence, Qsequencep);
1351 return Faref (sequence, n);
1354 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1355 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1356 The value is actually the tail of LIST whose car is ELT. */)
1357 (register Lisp_Object elt, Lisp_Object list)
1359 register Lisp_Object tail;
1360 for (tail = list; CONSP (tail); tail = XCDR (tail))
1362 register Lisp_Object tem;
1363 CHECK_LIST_CONS (tail, list);
1364 tem = XCAR (tail);
1365 if (! NILP (Fequal (elt, tem)))
1366 return tail;
1367 QUIT;
1369 return Qnil;
1372 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1373 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1374 The value is actually the tail of LIST whose car is ELT. */)
1375 (register Lisp_Object elt, Lisp_Object list)
1377 while (1)
1379 if (!CONSP (list) || EQ (XCAR (list), elt))
1380 break;
1382 list = XCDR (list);
1383 if (!CONSP (list) || EQ (XCAR (list), elt))
1384 break;
1386 list = XCDR (list);
1387 if (!CONSP (list) || EQ (XCAR (list), elt))
1388 break;
1390 list = XCDR (list);
1391 QUIT;
1394 CHECK_LIST (list);
1395 return list;
1398 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1399 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1400 The value is actually the tail of LIST whose car is ELT. */)
1401 (register Lisp_Object elt, Lisp_Object list)
1403 register Lisp_Object tail;
1405 if (!FLOATP (elt))
1406 return Fmemq (elt, list);
1408 for (tail = list; CONSP (tail); tail = XCDR (tail))
1410 register Lisp_Object tem;
1411 CHECK_LIST_CONS (tail, list);
1412 tem = XCAR (tail);
1413 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0, Qnil))
1414 return tail;
1415 QUIT;
1417 return Qnil;
1420 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1421 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1422 The value is actually the first element of LIST whose car is KEY.
1423 Elements of LIST that are not conses are ignored. */)
1424 (Lisp_Object key, Lisp_Object list)
1426 while (1)
1428 if (!CONSP (list)
1429 || (CONSP (XCAR (list))
1430 && EQ (XCAR (XCAR (list)), key)))
1431 break;
1433 list = XCDR (list);
1434 if (!CONSP (list)
1435 || (CONSP (XCAR (list))
1436 && EQ (XCAR (XCAR (list)), key)))
1437 break;
1439 list = XCDR (list);
1440 if (!CONSP (list)
1441 || (CONSP (XCAR (list))
1442 && EQ (XCAR (XCAR (list)), key)))
1443 break;
1445 list = XCDR (list);
1446 QUIT;
1449 return CAR (list);
1452 /* Like Fassq but never report an error and do not allow quits.
1453 Use only on lists known never to be circular. */
1455 Lisp_Object
1456 assq_no_quit (Lisp_Object key, Lisp_Object list)
1458 while (CONSP (list)
1459 && (!CONSP (XCAR (list))
1460 || !EQ (XCAR (XCAR (list)), key)))
1461 list = XCDR (list);
1463 return CAR_SAFE (list);
1466 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1467 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1468 The value is actually the first element of LIST whose car equals KEY. */)
1469 (Lisp_Object key, Lisp_Object list)
1471 Lisp_Object car;
1473 while (1)
1475 if (!CONSP (list)
1476 || (CONSP (XCAR (list))
1477 && (car = XCAR (XCAR (list)),
1478 EQ (car, key) || !NILP (Fequal (car, key)))))
1479 break;
1481 list = XCDR (list);
1482 if (!CONSP (list)
1483 || (CONSP (XCAR (list))
1484 && (car = XCAR (XCAR (list)),
1485 EQ (car, key) || !NILP (Fequal (car, key)))))
1486 break;
1488 list = XCDR (list);
1489 if (!CONSP (list)
1490 || (CONSP (XCAR (list))
1491 && (car = XCAR (XCAR (list)),
1492 EQ (car, key) || !NILP (Fequal (car, key)))))
1493 break;
1495 list = XCDR (list);
1496 QUIT;
1499 return CAR (list);
1502 /* Like Fassoc but never report an error and do not allow quits.
1503 Use only on lists known never to be circular. */
1505 Lisp_Object
1506 assoc_no_quit (Lisp_Object key, Lisp_Object list)
1508 while (CONSP (list)
1509 && (!CONSP (XCAR (list))
1510 || (!EQ (XCAR (XCAR (list)), key)
1511 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1512 list = XCDR (list);
1514 return CONSP (list) ? XCAR (list) : Qnil;
1517 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1518 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1519 The value is actually the first element of LIST whose cdr is KEY. */)
1520 (register Lisp_Object key, Lisp_Object list)
1522 while (1)
1524 if (!CONSP (list)
1525 || (CONSP (XCAR (list))
1526 && EQ (XCDR (XCAR (list)), key)))
1527 break;
1529 list = XCDR (list);
1530 if (!CONSP (list)
1531 || (CONSP (XCAR (list))
1532 && EQ (XCDR (XCAR (list)), key)))
1533 break;
1535 list = XCDR (list);
1536 if (!CONSP (list)
1537 || (CONSP (XCAR (list))
1538 && EQ (XCDR (XCAR (list)), key)))
1539 break;
1541 list = XCDR (list);
1542 QUIT;
1545 return CAR (list);
1548 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1549 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1550 The value is actually the first element of LIST whose cdr equals KEY. */)
1551 (Lisp_Object key, Lisp_Object list)
1553 Lisp_Object cdr;
1555 while (1)
1557 if (!CONSP (list)
1558 || (CONSP (XCAR (list))
1559 && (cdr = XCDR (XCAR (list)),
1560 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1561 break;
1563 list = XCDR (list);
1564 if (!CONSP (list)
1565 || (CONSP (XCAR (list))
1566 && (cdr = XCDR (XCAR (list)),
1567 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1568 break;
1570 list = XCDR (list);
1571 if (!CONSP (list)
1572 || (CONSP (XCAR (list))
1573 && (cdr = XCDR (XCAR (list)),
1574 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1575 break;
1577 list = XCDR (list);
1578 QUIT;
1581 return CAR (list);
1584 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1585 doc: /* Delete members of LIST which are `eq' to ELT, and return the result.
1586 More precisely, this function skips any members `eq' to ELT at the
1587 front of LIST, then removes members `eq' to ELT from the remaining
1588 sublist by modifying its list structure, then returns the resulting
1589 list.
1591 Write `(setq foo (delq element foo))' to be sure of correctly changing
1592 the value of a list `foo'. */)
1593 (register Lisp_Object elt, Lisp_Object list)
1595 Lisp_Object tail, tortoise, prev = Qnil;
1596 bool skip;
1598 FOR_EACH_TAIL (tail, list, tortoise, skip)
1600 Lisp_Object tem = XCAR (tail);
1601 if (EQ (elt, tem))
1603 if (NILP (prev))
1604 list = XCDR (tail);
1605 else
1606 Fsetcdr (prev, XCDR (tail));
1608 else
1609 prev = tail;
1611 return list;
1614 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1615 doc: /* Delete members of SEQ which are `equal' to ELT, and return the result.
1616 SEQ must be a sequence (i.e. a list, a vector, or a string).
1617 The return value is a sequence of the same type.
1619 If SEQ is a list, this behaves like `delq', except that it compares
1620 with `equal' instead of `eq'. In particular, it may remove elements
1621 by altering the list structure.
1623 If SEQ is not a list, deletion is never performed destructively;
1624 instead this function creates and returns a new vector or string.
1626 Write `(setq foo (delete element foo))' to be sure of correctly
1627 changing the value of a sequence `foo'. */)
1628 (Lisp_Object elt, Lisp_Object seq)
1630 if (VECTORP (seq))
1632 ptrdiff_t i, n;
1634 for (i = n = 0; i < ASIZE (seq); ++i)
1635 if (NILP (Fequal (AREF (seq, i), elt)))
1636 ++n;
1638 if (n != ASIZE (seq))
1640 struct Lisp_Vector *p = allocate_vector (n);
1642 for (i = n = 0; i < ASIZE (seq); ++i)
1643 if (NILP (Fequal (AREF (seq, i), elt)))
1644 p->contents[n++] = AREF (seq, i);
1646 XSETVECTOR (seq, p);
1649 else if (STRINGP (seq))
1651 ptrdiff_t i, ibyte, nchars, nbytes, cbytes;
1652 int c;
1654 for (i = nchars = nbytes = ibyte = 0;
1655 i < SCHARS (seq);
1656 ++i, ibyte += cbytes)
1658 if (STRING_MULTIBYTE (seq))
1660 c = STRING_CHAR (SDATA (seq) + ibyte);
1661 cbytes = CHAR_BYTES (c);
1663 else
1665 c = SREF (seq, i);
1666 cbytes = 1;
1669 if (!INTEGERP (elt) || c != XINT (elt))
1671 ++nchars;
1672 nbytes += cbytes;
1676 if (nchars != SCHARS (seq))
1678 Lisp_Object tem;
1680 tem = make_uninit_multibyte_string (nchars, nbytes);
1681 if (!STRING_MULTIBYTE (seq))
1682 STRING_SET_UNIBYTE (tem);
1684 for (i = nchars = nbytes = ibyte = 0;
1685 i < SCHARS (seq);
1686 ++i, ibyte += cbytes)
1688 if (STRING_MULTIBYTE (seq))
1690 c = STRING_CHAR (SDATA (seq) + ibyte);
1691 cbytes = CHAR_BYTES (c);
1693 else
1695 c = SREF (seq, i);
1696 cbytes = 1;
1699 if (!INTEGERP (elt) || c != XINT (elt))
1701 unsigned char *from = SDATA (seq) + ibyte;
1702 unsigned char *to = SDATA (tem) + nbytes;
1703 ptrdiff_t n;
1705 ++nchars;
1706 nbytes += cbytes;
1708 for (n = cbytes; n--; )
1709 *to++ = *from++;
1713 seq = tem;
1716 else
1718 Lisp_Object tail, prev;
1720 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1722 CHECK_LIST_CONS (tail, seq);
1724 if (!NILP (Fequal (elt, XCAR (tail))))
1726 if (NILP (prev))
1727 seq = XCDR (tail);
1728 else
1729 Fsetcdr (prev, XCDR (tail));
1731 else
1732 prev = tail;
1733 QUIT;
1737 return seq;
1740 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1741 doc: /* Reverse order of items in a list, vector or string SEQ.
1742 If SEQ is a list, it should be nil-terminated.
1743 This function may destructively modify SEQ to produce the value. */)
1744 (Lisp_Object seq)
1746 if (NILP (seq))
1747 return seq;
1748 else if (STRINGP (seq))
1749 return Freverse (seq);
1750 else if (CONSP (seq))
1752 Lisp_Object prev, tail, next;
1754 for (prev = Qnil, tail = seq; !NILP (tail); tail = next)
1756 QUIT;
1757 CHECK_LIST_CONS (tail, tail);
1758 next = XCDR (tail);
1759 Fsetcdr (tail, prev);
1760 prev = tail;
1762 seq = prev;
1764 else if (VECTORP (seq))
1766 ptrdiff_t i, size = ASIZE (seq);
1768 for (i = 0; i < size / 2; i++)
1770 Lisp_Object tem = AREF (seq, i);
1771 ASET (seq, i, AREF (seq, size - i - 1));
1772 ASET (seq, size - i - 1, tem);
1775 else if (BOOL_VECTOR_P (seq))
1777 ptrdiff_t i, size = bool_vector_size (seq);
1779 for (i = 0; i < size / 2; i++)
1781 bool tem = bool_vector_bitref (seq, i);
1782 bool_vector_set (seq, i, bool_vector_bitref (seq, size - i - 1));
1783 bool_vector_set (seq, size - i - 1, tem);
1786 else
1787 wrong_type_argument (Qarrayp, seq);
1788 return seq;
1791 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1792 doc: /* Return the reversed copy of list, vector, or string SEQ.
1793 See also the function `nreverse', which is used more often. */)
1794 (Lisp_Object seq)
1796 Lisp_Object new;
1798 if (NILP (seq))
1799 return Qnil;
1800 else if (CONSP (seq))
1802 for (new = Qnil; CONSP (seq); seq = XCDR (seq))
1804 QUIT;
1805 new = Fcons (XCAR (seq), new);
1807 CHECK_LIST_END (seq, seq);
1809 else if (VECTORP (seq))
1811 ptrdiff_t i, size = ASIZE (seq);
1813 new = make_uninit_vector (size);
1814 for (i = 0; i < size; i++)
1815 ASET (new, i, AREF (seq, size - i - 1));
1817 else if (BOOL_VECTOR_P (seq))
1819 ptrdiff_t i;
1820 EMACS_INT nbits = bool_vector_size (seq);
1822 new = make_uninit_bool_vector (nbits);
1823 for (i = 0; i < nbits; i++)
1824 bool_vector_set (new, i, bool_vector_bitref (seq, nbits - i - 1));
1826 else if (STRINGP (seq))
1828 ptrdiff_t size = SCHARS (seq), bytes = SBYTES (seq);
1830 if (size == bytes)
1832 ptrdiff_t i;
1834 new = make_uninit_string (size);
1835 for (i = 0; i < size; i++)
1836 SSET (new, i, SREF (seq, size - i - 1));
1838 else
1840 unsigned char *p, *q;
1842 new = make_uninit_multibyte_string (size, bytes);
1843 p = SDATA (seq), q = SDATA (new) + bytes;
1844 while (q > SDATA (new))
1846 int ch, len;
1848 ch = STRING_CHAR_AND_LENGTH (p, len);
1849 p += len, q -= len;
1850 CHAR_STRING (ch, q);
1854 else
1855 wrong_type_argument (Qsequencep, seq);
1856 return new;
1859 /* Sort LIST using PREDICATE, preserving original order of elements
1860 considered as equal. */
1862 static Lisp_Object
1863 sort_list (Lisp_Object list, Lisp_Object predicate)
1865 Lisp_Object front, back;
1866 Lisp_Object len, tem;
1867 EMACS_INT length;
1869 front = list;
1870 len = Flength (list);
1871 length = XINT (len);
1872 if (length < 2)
1873 return list;
1875 XSETINT (len, (length / 2) - 1);
1876 tem = Fnthcdr (len, list);
1877 back = Fcdr (tem);
1878 Fsetcdr (tem, Qnil);
1880 front = Fsort (front, predicate);
1881 back = Fsort (back, predicate);
1882 return merge (front, back, predicate);
1885 /* Using PRED to compare, return whether A and B are in order.
1886 Compare stably when A appeared before B in the input. */
1887 static bool
1888 inorder (Lisp_Object pred, Lisp_Object a, Lisp_Object b)
1890 return NILP (call2 (pred, b, a));
1893 /* Using PRED to compare, merge from ALEN-length A and BLEN-length B
1894 into DEST. Argument arrays must be nonempty and must not overlap,
1895 except that B might be the last part of DEST. */
1896 static void
1897 merge_vectors (Lisp_Object pred,
1898 ptrdiff_t alen, Lisp_Object const a[restrict VLA_ELEMS (alen)],
1899 ptrdiff_t blen, Lisp_Object const b[VLA_ELEMS (blen)],
1900 Lisp_Object dest[VLA_ELEMS (alen + blen)])
1902 eassume (0 < alen && 0 < blen);
1903 Lisp_Object const *alim = a + alen;
1904 Lisp_Object const *blim = b + blen;
1906 while (true)
1908 if (inorder (pred, a[0], b[0]))
1910 *dest++ = *a++;
1911 if (a == alim)
1913 if (dest != b)
1914 memcpy (dest, b, (blim - b) * sizeof *dest);
1915 return;
1918 else
1920 *dest++ = *b++;
1921 if (b == blim)
1923 memcpy (dest, a, (alim - a) * sizeof *dest);
1924 return;
1930 /* Using PRED to compare, sort LEN-length VEC in place, using TMP for
1931 temporary storage. LEN must be at least 2. */
1932 static void
1933 sort_vector_inplace (Lisp_Object pred, ptrdiff_t len,
1934 Lisp_Object vec[restrict VLA_ELEMS (len)],
1935 Lisp_Object tmp[restrict VLA_ELEMS (len >> 1)])
1937 eassume (2 <= len);
1938 ptrdiff_t halflen = len >> 1;
1939 sort_vector_copy (pred, halflen, vec, tmp);
1940 if (1 < len - halflen)
1941 sort_vector_inplace (pred, len - halflen, vec + halflen, vec);
1942 merge_vectors (pred, halflen, tmp, len - halflen, vec + halflen, vec);
1945 /* Using PRED to compare, sort from LEN-length SRC into DST.
1946 Len must be positive. */
1947 static void
1948 sort_vector_copy (Lisp_Object pred, ptrdiff_t len,
1949 Lisp_Object src[restrict VLA_ELEMS (len)],
1950 Lisp_Object dest[restrict VLA_ELEMS (len)])
1952 eassume (0 < len);
1953 ptrdiff_t halflen = len >> 1;
1954 if (halflen < 1)
1955 dest[0] = src[0];
1956 else
1958 if (1 < halflen)
1959 sort_vector_inplace (pred, halflen, src, dest);
1960 if (1 < len - halflen)
1961 sort_vector_inplace (pred, len - halflen, src + halflen, dest);
1962 merge_vectors (pred, halflen, src, len - halflen, src + halflen, dest);
1966 /* Sort VECTOR in place using PREDICATE, preserving original order of
1967 elements considered as equal. */
1969 static void
1970 sort_vector (Lisp_Object vector, Lisp_Object predicate)
1972 ptrdiff_t len = ASIZE (vector);
1973 if (len < 2)
1974 return;
1975 ptrdiff_t halflen = len >> 1;
1976 Lisp_Object *tmp;
1977 USE_SAFE_ALLOCA;
1978 SAFE_ALLOCA_LISP (tmp, halflen);
1979 for (ptrdiff_t i = 0; i < halflen; i++)
1980 tmp[i] = make_number (0);
1981 sort_vector_inplace (predicate, len, XVECTOR (vector)->contents, tmp);
1982 SAFE_FREE ();
1985 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1986 doc: /* Sort SEQ, stably, comparing elements using PREDICATE.
1987 Returns the sorted sequence. SEQ should be a list or vector. SEQ is
1988 modified by side effects. PREDICATE is called with two elements of
1989 SEQ, and should return non-nil if the first element should sort before
1990 the second. */)
1991 (Lisp_Object seq, Lisp_Object predicate)
1993 if (CONSP (seq))
1994 seq = sort_list (seq, predicate);
1995 else if (VECTORP (seq))
1996 sort_vector (seq, predicate);
1997 else if (!NILP (seq))
1998 wrong_type_argument (Qsequencep, seq);
1999 return seq;
2002 Lisp_Object
2003 merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred)
2005 Lisp_Object l1 = org_l1;
2006 Lisp_Object l2 = org_l2;
2007 Lisp_Object tail = Qnil;
2008 Lisp_Object value = Qnil;
2010 while (1)
2012 if (NILP (l1))
2014 if (NILP (tail))
2015 return l2;
2016 Fsetcdr (tail, l2);
2017 return value;
2019 if (NILP (l2))
2021 if (NILP (tail))
2022 return l1;
2023 Fsetcdr (tail, l1);
2024 return value;
2027 Lisp_Object tem;
2028 if (inorder (pred, Fcar (l1), Fcar (l2)))
2030 tem = l1;
2031 l1 = Fcdr (l1);
2032 org_l1 = l1;
2034 else
2036 tem = l2;
2037 l2 = Fcdr (l2);
2038 org_l2 = l2;
2040 if (NILP (tail))
2041 value = tem;
2042 else
2043 Fsetcdr (tail, tem);
2044 tail = tem;
2049 /* This does not check for quits. That is safe since it must terminate. */
2051 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
2052 doc: /* Extract a value from a property list.
2053 PLIST is a property list, which is a list of the form
2054 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2055 corresponding to the given PROP, or nil if PROP is not one of the
2056 properties on the list. This function never signals an error. */)
2057 (Lisp_Object plist, Lisp_Object prop)
2059 Lisp_Object tail, halftail;
2061 /* halftail is used to detect circular lists. */
2062 tail = halftail = plist;
2063 while (CONSP (tail) && CONSP (XCDR (tail)))
2065 if (EQ (prop, XCAR (tail)))
2066 return XCAR (XCDR (tail));
2068 tail = XCDR (XCDR (tail));
2069 halftail = XCDR (halftail);
2070 if (EQ (tail, halftail))
2071 break;
2074 return Qnil;
2077 DEFUN ("get", Fget, Sget, 2, 2, 0,
2078 doc: /* Return the value of SYMBOL's PROPNAME property.
2079 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
2080 (Lisp_Object symbol, Lisp_Object propname)
2082 CHECK_SYMBOL (symbol);
2083 return Fplist_get (XSYMBOL (symbol)->plist, propname);
2086 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
2087 doc: /* Change value in PLIST of PROP to VAL.
2088 PLIST is a property list, which is a list of the form
2089 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
2090 If PROP is already a property on the list, its value is set to VAL,
2091 otherwise the new PROP VAL pair is added. The new plist is returned;
2092 use `(setq x (plist-put x prop val))' to be sure to use the new value.
2093 The PLIST is modified by side effects. */)
2094 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
2096 register Lisp_Object tail, prev;
2097 Lisp_Object newcell;
2098 prev = Qnil;
2099 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2100 tail = XCDR (XCDR (tail)))
2102 if (EQ (prop, XCAR (tail)))
2104 Fsetcar (XCDR (tail), val);
2105 return plist;
2108 prev = tail;
2109 QUIT;
2111 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
2112 if (NILP (prev))
2113 return newcell;
2114 else
2115 Fsetcdr (XCDR (prev), newcell);
2116 return plist;
2119 DEFUN ("put", Fput, Sput, 3, 3, 0,
2120 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
2121 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
2122 (Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
2124 CHECK_SYMBOL (symbol);
2125 set_symbol_plist
2126 (symbol, Fplist_put (XSYMBOL (symbol)->plist, propname, value));
2127 return value;
2130 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
2131 doc: /* Extract a value from a property list, comparing with `equal'.
2132 PLIST is a property list, which is a list of the form
2133 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2134 corresponding to the given PROP, or nil if PROP is not
2135 one of the properties on the list. */)
2136 (Lisp_Object plist, Lisp_Object prop)
2138 Lisp_Object tail;
2140 for (tail = plist;
2141 CONSP (tail) && CONSP (XCDR (tail));
2142 tail = XCDR (XCDR (tail)))
2144 if (! NILP (Fequal (prop, XCAR (tail))))
2145 return XCAR (XCDR (tail));
2147 QUIT;
2150 CHECK_LIST_END (tail, prop);
2152 return Qnil;
2155 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
2156 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2157 PLIST is a property list, which is a list of the form
2158 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2159 If PROP is already a property on the list, its value is set to VAL,
2160 otherwise the new PROP VAL pair is added. The new plist is returned;
2161 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2162 The PLIST is modified by side effects. */)
2163 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
2165 register Lisp_Object tail, prev;
2166 Lisp_Object newcell;
2167 prev = Qnil;
2168 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2169 tail = XCDR (XCDR (tail)))
2171 if (! NILP (Fequal (prop, XCAR (tail))))
2173 Fsetcar (XCDR (tail), val);
2174 return plist;
2177 prev = tail;
2178 QUIT;
2180 newcell = list2 (prop, val);
2181 if (NILP (prev))
2182 return newcell;
2183 else
2184 Fsetcdr (XCDR (prev), newcell);
2185 return plist;
2188 DEFUN ("eql", Feql, Seql, 2, 2, 0,
2189 doc: /* Return t if the two args are the same Lisp object.
2190 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
2191 (Lisp_Object obj1, Lisp_Object obj2)
2193 if (FLOATP (obj1))
2194 return internal_equal (obj1, obj2, 0, 0, Qnil) ? Qt : Qnil;
2195 else
2196 return EQ (obj1, obj2) ? Qt : Qnil;
2199 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
2200 doc: /* Return t if two Lisp objects have similar structure and contents.
2201 They must have the same data type.
2202 Conses are compared by comparing the cars and the cdrs.
2203 Vectors and strings are compared element by element.
2204 Numbers are compared by value, but integers cannot equal floats.
2205 (Use `=' if you want integers and floats to be able to be equal.)
2206 Symbols must match exactly. */)
2207 (register Lisp_Object o1, Lisp_Object o2)
2209 return internal_equal (o1, o2, 0, 0, Qnil) ? Qt : Qnil;
2212 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2213 doc: /* Return t if two Lisp objects have similar structure and contents.
2214 This is like `equal' except that it compares the text properties
2215 of strings. (`equal' ignores text properties.) */)
2216 (register Lisp_Object o1, Lisp_Object o2)
2218 return internal_equal (o1, o2, 0, 1, Qnil) ? Qt : Qnil;
2221 /* DEPTH is current depth of recursion. Signal an error if it
2222 gets too deep.
2223 PROPS means compare string text properties too. */
2225 static bool
2226 internal_equal (Lisp_Object o1, Lisp_Object o2, int depth, bool props,
2227 Lisp_Object ht)
2229 if (depth > 10)
2231 if (depth > 200)
2232 error ("Stack overflow in equal");
2233 if (NILP (ht))
2234 ht = CALLN (Fmake_hash_table, QCtest, Qeq);
2235 switch (XTYPE (o1))
2237 case Lisp_Cons: case Lisp_Misc: case Lisp_Vectorlike:
2239 struct Lisp_Hash_Table *h = XHASH_TABLE (ht);
2240 EMACS_UINT hash;
2241 ptrdiff_t i = hash_lookup (h, o1, &hash);
2242 if (i >= 0)
2243 { /* `o1' was seen already. */
2244 Lisp_Object o2s = HASH_VALUE (h, i);
2245 if (!NILP (Fmemq (o2, o2s)))
2246 return 1;
2247 else
2248 set_hash_value_slot (h, i, Fcons (o2, o2s));
2250 else
2251 hash_put (h, o1, Fcons (o2, Qnil), hash);
2253 default: ;
2257 tail_recurse:
2258 QUIT;
2259 if (EQ (o1, o2))
2260 return 1;
2261 if (XTYPE (o1) != XTYPE (o2))
2262 return 0;
2264 switch (XTYPE (o1))
2266 case Lisp_Float:
2268 double d1, d2;
2270 d1 = extract_float (o1);
2271 d2 = extract_float (o2);
2272 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2273 though they are not =. */
2274 return d1 == d2 || (d1 != d1 && d2 != d2);
2277 case Lisp_Cons:
2278 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props, ht))
2279 return 0;
2280 o1 = XCDR (o1);
2281 o2 = XCDR (o2);
2282 /* FIXME: This inf-loops in a circular list! */
2283 goto tail_recurse;
2285 case Lisp_Misc:
2286 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2287 return 0;
2288 if (OVERLAYP (o1))
2290 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2291 depth + 1, props, ht)
2292 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2293 depth + 1, props, ht))
2294 return 0;
2295 o1 = XOVERLAY (o1)->plist;
2296 o2 = XOVERLAY (o2)->plist;
2297 goto tail_recurse;
2299 if (MARKERP (o1))
2301 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2302 && (XMARKER (o1)->buffer == 0
2303 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2305 break;
2307 case Lisp_Vectorlike:
2309 register int i;
2310 ptrdiff_t size = ASIZE (o1);
2311 /* Pseudovectors have the type encoded in the size field, so this test
2312 actually checks that the objects have the same type as well as the
2313 same size. */
2314 if (ASIZE (o2) != size)
2315 return 0;
2316 /* Boolvectors are compared much like strings. */
2317 if (BOOL_VECTOR_P (o1))
2319 EMACS_INT size = bool_vector_size (o1);
2320 if (size != bool_vector_size (o2))
2321 return 0;
2322 if (memcmp (bool_vector_data (o1), bool_vector_data (o2),
2323 bool_vector_bytes (size)))
2324 return 0;
2325 return 1;
2327 if (WINDOW_CONFIGURATIONP (o1))
2328 return compare_window_configurations (o1, o2, 0);
2330 /* Aside from them, only true vectors, char-tables, compiled
2331 functions, and fonts (font-spec, font-entity, font-object)
2332 are sensible to compare, so eliminate the others now. */
2333 if (size & PSEUDOVECTOR_FLAG)
2335 if (((size & PVEC_TYPE_MASK) >> PSEUDOVECTOR_AREA_BITS)
2336 < PVEC_COMPILED)
2337 return 0;
2338 size &= PSEUDOVECTOR_SIZE_MASK;
2340 for (i = 0; i < size; i++)
2342 Lisp_Object v1, v2;
2343 v1 = AREF (o1, i);
2344 v2 = AREF (o2, i);
2345 if (!internal_equal (v1, v2, depth + 1, props, ht))
2346 return 0;
2348 return 1;
2350 break;
2352 case Lisp_String:
2353 if (SCHARS (o1) != SCHARS (o2))
2354 return 0;
2355 if (SBYTES (o1) != SBYTES (o2))
2356 return 0;
2357 if (memcmp (SDATA (o1), SDATA (o2), SBYTES (o1)))
2358 return 0;
2359 if (props && !compare_string_intervals (o1, o2))
2360 return 0;
2361 return 1;
2363 default:
2364 break;
2367 return 0;
2371 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2372 doc: /* Store each element of ARRAY with ITEM.
2373 ARRAY is a vector, string, char-table, or bool-vector. */)
2374 (Lisp_Object array, Lisp_Object item)
2376 register ptrdiff_t size, idx;
2378 if (VECTORP (array))
2379 for (idx = 0, size = ASIZE (array); idx < size; idx++)
2380 ASET (array, idx, item);
2381 else if (CHAR_TABLE_P (array))
2383 int i;
2385 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2386 set_char_table_contents (array, i, item);
2387 set_char_table_defalt (array, item);
2389 else if (STRINGP (array))
2391 register unsigned char *p = SDATA (array);
2392 int charval;
2393 CHECK_CHARACTER (item);
2394 charval = XFASTINT (item);
2395 size = SCHARS (array);
2396 if (STRING_MULTIBYTE (array))
2398 unsigned char str[MAX_MULTIBYTE_LENGTH];
2399 int len = CHAR_STRING (charval, str);
2400 ptrdiff_t size_byte = SBYTES (array);
2402 if (INT_MULTIPLY_OVERFLOW (SCHARS (array), len)
2403 || SCHARS (array) * len != size_byte)
2404 error ("Attempt to change byte length of a string");
2405 for (idx = 0; idx < size_byte; idx++)
2406 *p++ = str[idx % len];
2408 else
2409 for (idx = 0; idx < size; idx++)
2410 p[idx] = charval;
2412 else if (BOOL_VECTOR_P (array))
2413 return bool_vector_fill (array, item);
2414 else
2415 wrong_type_argument (Qarrayp, array);
2416 return array;
2419 DEFUN ("clear-string", Fclear_string, Sclear_string,
2420 1, 1, 0,
2421 doc: /* Clear the contents of STRING.
2422 This makes STRING unibyte and may change its length. */)
2423 (Lisp_Object string)
2425 ptrdiff_t len;
2426 CHECK_STRING (string);
2427 len = SBYTES (string);
2428 memset (SDATA (string), 0, len);
2429 STRING_SET_CHARS (string, len);
2430 STRING_SET_UNIBYTE (string);
2431 return Qnil;
2434 /* ARGSUSED */
2435 Lisp_Object
2436 nconc2 (Lisp_Object s1, Lisp_Object s2)
2438 return CALLN (Fnconc, s1, s2);
2441 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2442 doc: /* Concatenate any number of lists by altering them.
2443 Only the last argument is not altered, and need not be a list.
2444 usage: (nconc &rest LISTS) */)
2445 (ptrdiff_t nargs, Lisp_Object *args)
2447 ptrdiff_t argnum;
2448 register Lisp_Object tail, tem, val;
2450 val = tail = Qnil;
2452 for (argnum = 0; argnum < nargs; argnum++)
2454 tem = args[argnum];
2455 if (NILP (tem)) continue;
2457 if (NILP (val))
2458 val = tem;
2460 if (argnum + 1 == nargs) break;
2462 CHECK_LIST_CONS (tem, tem);
2464 while (CONSP (tem))
2466 tail = tem;
2467 tem = XCDR (tail);
2468 QUIT;
2471 tem = args[argnum + 1];
2472 Fsetcdr (tail, tem);
2473 if (NILP (tem))
2474 args[argnum + 1] = tail;
2477 return val;
2480 /* This is the guts of all mapping functions.
2481 Apply FN to each element of SEQ, one by one,
2482 storing the results into elements of VALS, a C vector of Lisp_Objects.
2483 LENI is the length of VALS, which should also be the length of SEQ. */
2485 static void
2486 mapcar1 (EMACS_INT leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
2488 Lisp_Object tail, dummy;
2489 EMACS_INT i;
2491 if (VECTORP (seq) || COMPILEDP (seq))
2493 for (i = 0; i < leni; i++)
2495 dummy = call1 (fn, AREF (seq, i));
2496 if (vals)
2497 vals[i] = dummy;
2500 else if (BOOL_VECTOR_P (seq))
2502 for (i = 0; i < leni; i++)
2504 dummy = call1 (fn, bool_vector_ref (seq, i));
2505 if (vals)
2506 vals[i] = dummy;
2509 else if (STRINGP (seq))
2511 ptrdiff_t i_byte;
2513 for (i = 0, i_byte = 0; i < leni;)
2515 int c;
2516 ptrdiff_t i_before = i;
2518 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2519 XSETFASTINT (dummy, c);
2520 dummy = call1 (fn, dummy);
2521 if (vals)
2522 vals[i_before] = dummy;
2525 else /* Must be a list, since Flength did not get an error */
2527 tail = seq;
2528 for (i = 0; i < leni && CONSP (tail); i++)
2530 dummy = call1 (fn, XCAR (tail));
2531 if (vals)
2532 vals[i] = dummy;
2533 tail = XCDR (tail);
2538 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2539 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2540 In between each pair of results, stick in SEPARATOR. Thus, " " as
2541 SEPARATOR results in spaces between the values returned by FUNCTION.
2542 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2543 (Lisp_Object function, Lisp_Object sequence, Lisp_Object separator)
2545 Lisp_Object len;
2546 EMACS_INT leni;
2547 EMACS_INT nargs;
2548 ptrdiff_t i;
2549 Lisp_Object *args;
2550 Lisp_Object ret;
2551 USE_SAFE_ALLOCA;
2553 len = Flength (sequence);
2554 if (CHAR_TABLE_P (sequence))
2555 wrong_type_argument (Qlistp, sequence);
2556 leni = XINT (len);
2557 nargs = leni + leni - 1;
2558 if (nargs < 0) return empty_unibyte_string;
2560 SAFE_ALLOCA_LISP (args, nargs);
2562 mapcar1 (leni, args, function, sequence);
2564 for (i = leni - 1; i > 0; i--)
2565 args[i + i] = args[i];
2567 for (i = 1; i < nargs; i += 2)
2568 args[i] = separator;
2570 ret = Fconcat (nargs, args);
2571 SAFE_FREE ();
2573 return ret;
2576 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2577 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2578 The result is a list just as long as SEQUENCE.
2579 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2580 (Lisp_Object function, Lisp_Object sequence)
2582 register Lisp_Object len;
2583 register EMACS_INT leni;
2584 register Lisp_Object *args;
2585 Lisp_Object ret;
2586 USE_SAFE_ALLOCA;
2588 len = Flength (sequence);
2589 if (CHAR_TABLE_P (sequence))
2590 wrong_type_argument (Qlistp, sequence);
2591 leni = XFASTINT (len);
2593 SAFE_ALLOCA_LISP (args, leni);
2595 mapcar1 (leni, args, function, sequence);
2597 ret = Flist (leni, args);
2598 SAFE_FREE ();
2600 return ret;
2603 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2604 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2605 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2606 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2607 (Lisp_Object function, Lisp_Object sequence)
2609 register EMACS_INT leni;
2611 leni = XFASTINT (Flength (sequence));
2612 if (CHAR_TABLE_P (sequence))
2613 wrong_type_argument (Qlistp, sequence);
2614 mapcar1 (leni, 0, function, sequence);
2616 return sequence;
2619 /* This is how C code calls `yes-or-no-p' and allows the user
2620 to redefine it. */
2622 Lisp_Object
2623 do_yes_or_no_p (Lisp_Object prompt)
2625 return call1 (intern ("yes-or-no-p"), prompt);
2628 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2629 doc: /* Ask user a yes-or-no question.
2630 Return t if answer is yes, and nil if the answer is no.
2631 PROMPT is the string to display to ask the question. It should end in
2632 a space; `yes-or-no-p' adds \"(yes or no) \" to it.
2634 The user must confirm the answer with RET, and can edit it until it
2635 has been confirmed.
2637 If dialog boxes are supported, a dialog box will be used
2638 if `last-nonmenu-event' is nil, and `use-dialog-box' is non-nil. */)
2639 (Lisp_Object prompt)
2641 Lisp_Object ans;
2643 CHECK_STRING (prompt);
2645 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2646 && use_dialog_box && ! NILP (last_input_event))
2648 Lisp_Object pane, menu, obj;
2649 redisplay_preserve_echo_area (4);
2650 pane = list2 (Fcons (build_string ("Yes"), Qt),
2651 Fcons (build_string ("No"), Qnil));
2652 menu = Fcons (prompt, pane);
2653 obj = Fx_popup_dialog (Qt, menu, Qnil);
2654 return obj;
2657 AUTO_STRING (yes_or_no, "(yes or no) ");
2658 prompt = CALLN (Fconcat, prompt, yes_or_no);
2660 while (1)
2662 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2663 Qyes_or_no_p_history, Qnil,
2664 Qnil));
2665 if (SCHARS (ans) == 3 && !strcmp (SSDATA (ans), "yes"))
2666 return Qt;
2667 if (SCHARS (ans) == 2 && !strcmp (SSDATA (ans), "no"))
2668 return Qnil;
2670 Fding (Qnil);
2671 Fdiscard_input ();
2672 message1 ("Please answer yes or no.");
2673 Fsleep_for (make_number (2), Qnil);
2677 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2678 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2680 Each of the three load averages is multiplied by 100, then converted
2681 to integer.
2683 When USE-FLOATS is non-nil, floats will be used instead of integers.
2684 These floats are not multiplied by 100.
2686 If the 5-minute or 15-minute load averages are not available, return a
2687 shortened list, containing only those averages which are available.
2689 An error is thrown if the load average can't be obtained. In some
2690 cases making it work would require Emacs being installed setuid or
2691 setgid so that it can read kernel information, and that usually isn't
2692 advisable. */)
2693 (Lisp_Object use_floats)
2695 double load_ave[3];
2696 int loads = getloadavg (load_ave, 3);
2697 Lisp_Object ret = Qnil;
2699 if (loads < 0)
2700 error ("load-average not implemented for this operating system");
2702 while (loads-- > 0)
2704 Lisp_Object load = (NILP (use_floats)
2705 ? make_number (100.0 * load_ave[loads])
2706 : make_float (load_ave[loads]));
2707 ret = Fcons (load, ret);
2710 return ret;
2713 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2714 doc: /* Return t if FEATURE is present in this Emacs.
2716 Use this to conditionalize execution of lisp code based on the
2717 presence or absence of Emacs or environment extensions.
2718 Use `provide' to declare that a feature is available. This function
2719 looks at the value of the variable `features'. The optional argument
2720 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2721 (Lisp_Object feature, Lisp_Object subfeature)
2723 register Lisp_Object tem;
2724 CHECK_SYMBOL (feature);
2725 tem = Fmemq (feature, Vfeatures);
2726 if (!NILP (tem) && !NILP (subfeature))
2727 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2728 return (NILP (tem)) ? Qnil : Qt;
2731 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2732 doc: /* Announce that FEATURE is a feature of the current Emacs.
2733 The optional argument SUBFEATURES should be a list of symbols listing
2734 particular subfeatures supported in this version of FEATURE. */)
2735 (Lisp_Object feature, Lisp_Object subfeatures)
2737 register Lisp_Object tem;
2738 CHECK_SYMBOL (feature);
2739 CHECK_LIST (subfeatures);
2740 if (!NILP (Vautoload_queue))
2741 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2742 Vautoload_queue);
2743 tem = Fmemq (feature, Vfeatures);
2744 if (NILP (tem))
2745 Vfeatures = Fcons (feature, Vfeatures);
2746 if (!NILP (subfeatures))
2747 Fput (feature, Qsubfeatures, subfeatures);
2748 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2750 /* Run any load-hooks for this file. */
2751 tem = Fassq (feature, Vafter_load_alist);
2752 if (CONSP (tem))
2753 Fmapc (Qfuncall, XCDR (tem));
2755 return feature;
2758 /* `require' and its subroutines. */
2760 /* List of features currently being require'd, innermost first. */
2762 static Lisp_Object require_nesting_list;
2764 static void
2765 require_unwind (Lisp_Object old_value)
2767 require_nesting_list = old_value;
2770 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2771 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2772 If FEATURE is not a member of the list `features', then the feature
2773 is not loaded; so load the file FILENAME.
2774 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2775 and `load' will try to load this name appended with the suffix `.elc' or
2776 `.el', in that order. The name without appended suffix will not be used.
2777 See `get-load-suffixes' for the complete list of suffixes.
2778 If the optional third argument NOERROR is non-nil,
2779 then return nil if the file is not found instead of signaling an error.
2780 Normally the return value is FEATURE.
2781 The normal messages at start and end of loading FILENAME are suppressed. */)
2782 (Lisp_Object feature, Lisp_Object filename, Lisp_Object noerror)
2784 Lisp_Object tem;
2785 bool from_file = load_in_progress;
2787 CHECK_SYMBOL (feature);
2789 /* Record the presence of `require' in this file
2790 even if the feature specified is already loaded.
2791 But not more than once in any file,
2792 and not when we aren't loading or reading from a file. */
2793 if (!from_file)
2794 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2795 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2796 from_file = 1;
2798 if (from_file)
2800 tem = Fcons (Qrequire, feature);
2801 if (NILP (Fmember (tem, Vcurrent_load_list)))
2802 LOADHIST_ATTACH (tem);
2804 tem = Fmemq (feature, Vfeatures);
2806 if (NILP (tem))
2808 ptrdiff_t count = SPECPDL_INDEX ();
2809 int nesting = 0;
2811 /* This is to make sure that loadup.el gives a clear picture
2812 of what files are preloaded and when. */
2813 if (! NILP (Vpurify_flag))
2814 error ("(require %s) while preparing to dump",
2815 SDATA (SYMBOL_NAME (feature)));
2817 /* A certain amount of recursive `require' is legitimate,
2818 but if we require the same feature recursively 3 times,
2819 signal an error. */
2820 tem = require_nesting_list;
2821 while (! NILP (tem))
2823 if (! NILP (Fequal (feature, XCAR (tem))))
2824 nesting++;
2825 tem = XCDR (tem);
2827 if (nesting > 3)
2828 error ("Recursive `require' for feature `%s'",
2829 SDATA (SYMBOL_NAME (feature)));
2831 /* Update the list for any nested `require's that occur. */
2832 record_unwind_protect (require_unwind, require_nesting_list);
2833 require_nesting_list = Fcons (feature, require_nesting_list);
2835 /* Value saved here is to be restored into Vautoload_queue */
2836 record_unwind_protect (un_autoload, Vautoload_queue);
2837 Vautoload_queue = Qt;
2839 /* Load the file. */
2840 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2841 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2843 /* If load failed entirely, return nil. */
2844 if (NILP (tem))
2845 return unbind_to (count, Qnil);
2847 tem = Fmemq (feature, Vfeatures);
2848 if (NILP (tem))
2849 error ("Required feature `%s' was not provided",
2850 SDATA (SYMBOL_NAME (feature)));
2852 /* Once loading finishes, don't undo it. */
2853 Vautoload_queue = Qt;
2854 feature = unbind_to (count, feature);
2857 return feature;
2860 /* Primitives for work of the "widget" library.
2861 In an ideal world, this section would not have been necessary.
2862 However, lisp function calls being as slow as they are, it turns
2863 out that some functions in the widget library (wid-edit.el) are the
2864 bottleneck of Widget operation. Here is their translation to C,
2865 for the sole reason of efficiency. */
2867 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
2868 doc: /* Return non-nil if PLIST has the property PROP.
2869 PLIST is a property list, which is a list of the form
2870 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
2871 Unlike `plist-get', this allows you to distinguish between a missing
2872 property and a property with the value nil.
2873 The value is actually the tail of PLIST whose car is PROP. */)
2874 (Lisp_Object plist, Lisp_Object prop)
2876 while (CONSP (plist) && !EQ (XCAR (plist), prop))
2878 QUIT;
2879 plist = XCDR (plist);
2880 plist = CDR (plist);
2882 return plist;
2885 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
2886 doc: /* In WIDGET, set PROPERTY to VALUE.
2887 The value can later be retrieved with `widget-get'. */)
2888 (Lisp_Object widget, Lisp_Object property, Lisp_Object value)
2890 CHECK_CONS (widget);
2891 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
2892 return value;
2895 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
2896 doc: /* In WIDGET, get the value of PROPERTY.
2897 The value could either be specified when the widget was created, or
2898 later with `widget-put'. */)
2899 (Lisp_Object widget, Lisp_Object property)
2901 Lisp_Object tmp;
2903 while (1)
2905 if (NILP (widget))
2906 return Qnil;
2907 CHECK_CONS (widget);
2908 tmp = Fplist_member (XCDR (widget), property);
2909 if (CONSP (tmp))
2911 tmp = XCDR (tmp);
2912 return CAR (tmp);
2914 tmp = XCAR (widget);
2915 if (NILP (tmp))
2916 return Qnil;
2917 widget = Fget (tmp, Qwidget_type);
2921 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
2922 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
2923 ARGS are passed as extra arguments to the function.
2924 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
2925 (ptrdiff_t nargs, Lisp_Object *args)
2927 Lisp_Object widget = args[0];
2928 Lisp_Object property = args[1];
2929 Lisp_Object propval = Fwidget_get (widget, property);
2930 Lisp_Object trailing_args = Flist (nargs - 2, args + 2);
2931 Lisp_Object result = CALLN (Fapply, propval, widget, trailing_args);
2932 return result;
2935 #ifdef HAVE_LANGINFO_CODESET
2936 #include <langinfo.h>
2937 #endif
2939 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
2940 doc: /* Access locale data ITEM for the current C locale, if available.
2941 ITEM should be one of the following:
2943 `codeset', returning the character set as a string (locale item CODESET);
2945 `days', returning a 7-element vector of day names (locale items DAY_n);
2947 `months', returning a 12-element vector of month names (locale items MON_n);
2949 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
2950 both measured in millimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
2952 If the system can't provide such information through a call to
2953 `nl_langinfo', or if ITEM isn't from the list above, return nil.
2955 See also Info node `(libc)Locales'.
2957 The data read from the system are decoded using `locale-coding-system'. */)
2958 (Lisp_Object item)
2960 char *str = NULL;
2961 #ifdef HAVE_LANGINFO_CODESET
2962 Lisp_Object val;
2963 if (EQ (item, Qcodeset))
2965 str = nl_langinfo (CODESET);
2966 return build_string (str);
2968 #ifdef DAY_1
2969 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
2971 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
2972 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
2973 int i;
2974 synchronize_system_time_locale ();
2975 for (i = 0; i < 7; i++)
2977 str = nl_langinfo (days[i]);
2978 val = build_unibyte_string (str);
2979 /* Fixme: Is this coding system necessarily right, even if
2980 it is consistent with CODESET? If not, what to do? */
2981 ASET (v, i, code_convert_string_norecord (val, Vlocale_coding_system,
2982 0));
2984 return v;
2986 #endif /* DAY_1 */
2987 #ifdef MON_1
2988 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
2990 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
2991 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
2992 MON_8, MON_9, MON_10, MON_11, MON_12};
2993 int i;
2994 synchronize_system_time_locale ();
2995 for (i = 0; i < 12; i++)
2997 str = nl_langinfo (months[i]);
2998 val = build_unibyte_string (str);
2999 ASET (v, i, code_convert_string_norecord (val, Vlocale_coding_system,
3000 0));
3002 return v;
3004 #endif /* MON_1 */
3005 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3006 but is in the locale files. This could be used by ps-print. */
3007 #ifdef PAPER_WIDTH
3008 else if (EQ (item, Qpaper))
3009 return list2i (nl_langinfo (PAPER_WIDTH), nl_langinfo (PAPER_HEIGHT));
3010 #endif /* PAPER_WIDTH */
3011 #endif /* HAVE_LANGINFO_CODESET*/
3012 return Qnil;
3015 /* base64 encode/decode functions (RFC 2045).
3016 Based on code from GNU recode. */
3018 #define MIME_LINE_LENGTH 76
3020 #define IS_ASCII(Character) \
3021 ((Character) < 128)
3022 #define IS_BASE64(Character) \
3023 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3024 #define IS_BASE64_IGNORABLE(Character) \
3025 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3026 || (Character) == '\f' || (Character) == '\r')
3028 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3029 character or return retval if there are no characters left to
3030 process. */
3031 #define READ_QUADRUPLET_BYTE(retval) \
3032 do \
3034 if (i == length) \
3036 if (nchars_return) \
3037 *nchars_return = nchars; \
3038 return (retval); \
3040 c = from[i++]; \
3042 while (IS_BASE64_IGNORABLE (c))
3044 /* Table of characters coding the 64 values. */
3045 static const char base64_value_to_char[64] =
3047 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3048 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3049 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3050 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3051 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3052 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3053 '8', '9', '+', '/' /* 60-63 */
3056 /* Table of base64 values for first 128 characters. */
3057 static const short base64_char_to_value[128] =
3059 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3060 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3061 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3062 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3063 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3064 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3065 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3066 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3067 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3068 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3069 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3070 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3071 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3074 /* The following diagram shows the logical steps by which three octets
3075 get transformed into four base64 characters.
3077 .--------. .--------. .--------.
3078 |aaaaaabb| |bbbbcccc| |ccdddddd|
3079 `--------' `--------' `--------'
3080 6 2 4 4 2 6
3081 .--------+--------+--------+--------.
3082 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3083 `--------+--------+--------+--------'
3085 .--------+--------+--------+--------.
3086 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3087 `--------+--------+--------+--------'
3089 The octets are divided into 6 bit chunks, which are then encoded into
3090 base64 characters. */
3093 static ptrdiff_t base64_encode_1 (const char *, char *, ptrdiff_t, bool, bool);
3094 static ptrdiff_t base64_decode_1 (const char *, char *, ptrdiff_t, bool,
3095 ptrdiff_t *);
3097 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3098 2, 3, "r",
3099 doc: /* Base64-encode the region between BEG and END.
3100 Return the length of the encoded text.
3101 Optional third argument NO-LINE-BREAK means do not break long lines
3102 into shorter lines. */)
3103 (Lisp_Object beg, Lisp_Object end, Lisp_Object no_line_break)
3105 char *encoded;
3106 ptrdiff_t allength, length;
3107 ptrdiff_t ibeg, iend, encoded_length;
3108 ptrdiff_t old_pos = PT;
3109 USE_SAFE_ALLOCA;
3111 validate_region (&beg, &end);
3113 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3114 iend = CHAR_TO_BYTE (XFASTINT (end));
3115 move_gap_both (XFASTINT (beg), ibeg);
3117 /* We need to allocate enough room for encoding the text.
3118 We need 33 1/3% more space, plus a newline every 76
3119 characters, and then we round up. */
3120 length = iend - ibeg;
3121 allength = length + length/3 + 1;
3122 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3124 encoded = SAFE_ALLOCA (allength);
3125 encoded_length = base64_encode_1 ((char *) BYTE_POS_ADDR (ibeg),
3126 encoded, length, NILP (no_line_break),
3127 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
3128 if (encoded_length > allength)
3129 emacs_abort ();
3131 if (encoded_length < 0)
3133 /* The encoding wasn't possible. */
3134 SAFE_FREE ();
3135 error ("Multibyte character in data for base64 encoding");
3138 /* Now we have encoded the region, so we insert the new contents
3139 and delete the old. (Insert first in order to preserve markers.) */
3140 SET_PT_BOTH (XFASTINT (beg), ibeg);
3141 insert (encoded, encoded_length);
3142 SAFE_FREE ();
3143 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3145 /* If point was outside of the region, restore it exactly; else just
3146 move to the beginning of the region. */
3147 if (old_pos >= XFASTINT (end))
3148 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3149 else if (old_pos > XFASTINT (beg))
3150 old_pos = XFASTINT (beg);
3151 SET_PT (old_pos);
3153 /* We return the length of the encoded text. */
3154 return make_number (encoded_length);
3157 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3158 1, 2, 0,
3159 doc: /* Base64-encode STRING and return the result.
3160 Optional second argument NO-LINE-BREAK means do not break long lines
3161 into shorter lines. */)
3162 (Lisp_Object string, Lisp_Object no_line_break)
3164 ptrdiff_t allength, length, encoded_length;
3165 char *encoded;
3166 Lisp_Object encoded_string;
3167 USE_SAFE_ALLOCA;
3169 CHECK_STRING (string);
3171 /* We need to allocate enough room for encoding the text.
3172 We need 33 1/3% more space, plus a newline every 76
3173 characters, and then we round up. */
3174 length = SBYTES (string);
3175 allength = length + length/3 + 1;
3176 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3178 /* We need to allocate enough room for decoding the text. */
3179 encoded = SAFE_ALLOCA (allength);
3181 encoded_length = base64_encode_1 (SSDATA (string),
3182 encoded, length, NILP (no_line_break),
3183 STRING_MULTIBYTE (string));
3184 if (encoded_length > allength)
3185 emacs_abort ();
3187 if (encoded_length < 0)
3189 /* The encoding wasn't possible. */
3190 error ("Multibyte character in data for base64 encoding");
3193 encoded_string = make_unibyte_string (encoded, encoded_length);
3194 SAFE_FREE ();
3196 return encoded_string;
3199 static ptrdiff_t
3200 base64_encode_1 (const char *from, char *to, ptrdiff_t length,
3201 bool line_break, bool multibyte)
3203 int counter = 0;
3204 ptrdiff_t i = 0;
3205 char *e = to;
3206 int c;
3207 unsigned int value;
3208 int bytes;
3210 while (i < length)
3212 if (multibyte)
3214 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3215 if (CHAR_BYTE8_P (c))
3216 c = CHAR_TO_BYTE8 (c);
3217 else if (c >= 256)
3218 return -1;
3219 i += bytes;
3221 else
3222 c = from[i++];
3224 /* Wrap line every 76 characters. */
3226 if (line_break)
3228 if (counter < MIME_LINE_LENGTH / 4)
3229 counter++;
3230 else
3232 *e++ = '\n';
3233 counter = 1;
3237 /* Process first byte of a triplet. */
3239 *e++ = base64_value_to_char[0x3f & c >> 2];
3240 value = (0x03 & c) << 4;
3242 /* Process second byte of a triplet. */
3244 if (i == length)
3246 *e++ = base64_value_to_char[value];
3247 *e++ = '=';
3248 *e++ = '=';
3249 break;
3252 if (multibyte)
3254 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3255 if (CHAR_BYTE8_P (c))
3256 c = CHAR_TO_BYTE8 (c);
3257 else if (c >= 256)
3258 return -1;
3259 i += bytes;
3261 else
3262 c = from[i++];
3264 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3265 value = (0x0f & c) << 2;
3267 /* Process third byte of a triplet. */
3269 if (i == length)
3271 *e++ = base64_value_to_char[value];
3272 *e++ = '=';
3273 break;
3276 if (multibyte)
3278 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3279 if (CHAR_BYTE8_P (c))
3280 c = CHAR_TO_BYTE8 (c);
3281 else if (c >= 256)
3282 return -1;
3283 i += bytes;
3285 else
3286 c = from[i++];
3288 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3289 *e++ = base64_value_to_char[0x3f & c];
3292 return e - to;
3296 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3297 2, 2, "r",
3298 doc: /* Base64-decode the region between BEG and END.
3299 Return the length of the decoded text.
3300 If the region can't be decoded, signal an error and don't modify the buffer. */)
3301 (Lisp_Object beg, Lisp_Object end)
3303 ptrdiff_t ibeg, iend, length, allength;
3304 char *decoded;
3305 ptrdiff_t old_pos = PT;
3306 ptrdiff_t decoded_length;
3307 ptrdiff_t inserted_chars;
3308 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
3309 USE_SAFE_ALLOCA;
3311 validate_region (&beg, &end);
3313 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3314 iend = CHAR_TO_BYTE (XFASTINT (end));
3316 length = iend - ibeg;
3318 /* We need to allocate enough room for decoding the text. If we are
3319 working on a multibyte buffer, each decoded code may occupy at
3320 most two bytes. */
3321 allength = multibyte ? length * 2 : length;
3322 decoded = SAFE_ALLOCA (allength);
3324 move_gap_both (XFASTINT (beg), ibeg);
3325 decoded_length = base64_decode_1 ((char *) BYTE_POS_ADDR (ibeg),
3326 decoded, length,
3327 multibyte, &inserted_chars);
3328 if (decoded_length > allength)
3329 emacs_abort ();
3331 if (decoded_length < 0)
3333 /* The decoding wasn't possible. */
3334 error ("Invalid base64 data");
3337 /* Now we have decoded the region, so we insert the new contents
3338 and delete the old. (Insert first in order to preserve markers.) */
3339 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3340 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3341 SAFE_FREE ();
3343 /* Delete the original text. */
3344 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3345 iend + decoded_length, 1);
3347 /* If point was outside of the region, restore it exactly; else just
3348 move to the beginning of the region. */
3349 if (old_pos >= XFASTINT (end))
3350 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3351 else if (old_pos > XFASTINT (beg))
3352 old_pos = XFASTINT (beg);
3353 SET_PT (old_pos > ZV ? ZV : old_pos);
3355 return make_number (inserted_chars);
3358 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3359 1, 1, 0,
3360 doc: /* Base64-decode STRING and return the result. */)
3361 (Lisp_Object string)
3363 char *decoded;
3364 ptrdiff_t length, decoded_length;
3365 Lisp_Object decoded_string;
3366 USE_SAFE_ALLOCA;
3368 CHECK_STRING (string);
3370 length = SBYTES (string);
3371 /* We need to allocate enough room for decoding the text. */
3372 decoded = SAFE_ALLOCA (length);
3374 /* The decoded result should be unibyte. */
3375 decoded_length = base64_decode_1 (SSDATA (string), decoded, length,
3376 0, NULL);
3377 if (decoded_length > length)
3378 emacs_abort ();
3379 else if (decoded_length >= 0)
3380 decoded_string = make_unibyte_string (decoded, decoded_length);
3381 else
3382 decoded_string = Qnil;
3384 SAFE_FREE ();
3385 if (!STRINGP (decoded_string))
3386 error ("Invalid base64 data");
3388 return decoded_string;
3391 /* Base64-decode the data at FROM of LENGTH bytes into TO. If
3392 MULTIBYTE, the decoded result should be in multibyte
3393 form. If NCHARS_RETURN is not NULL, store the number of produced
3394 characters in *NCHARS_RETURN. */
3396 static ptrdiff_t
3397 base64_decode_1 (const char *from, char *to, ptrdiff_t length,
3398 bool multibyte, ptrdiff_t *nchars_return)
3400 ptrdiff_t i = 0; /* Used inside READ_QUADRUPLET_BYTE */
3401 char *e = to;
3402 unsigned char c;
3403 unsigned long value;
3404 ptrdiff_t nchars = 0;
3406 while (1)
3408 /* Process first byte of a quadruplet. */
3410 READ_QUADRUPLET_BYTE (e-to);
3412 if (!IS_BASE64 (c))
3413 return -1;
3414 value = base64_char_to_value[c] << 18;
3416 /* Process second byte of a quadruplet. */
3418 READ_QUADRUPLET_BYTE (-1);
3420 if (!IS_BASE64 (c))
3421 return -1;
3422 value |= base64_char_to_value[c] << 12;
3424 c = (unsigned char) (value >> 16);
3425 if (multibyte && c >= 128)
3426 e += BYTE8_STRING (c, e);
3427 else
3428 *e++ = c;
3429 nchars++;
3431 /* Process third byte of a quadruplet. */
3433 READ_QUADRUPLET_BYTE (-1);
3435 if (c == '=')
3437 READ_QUADRUPLET_BYTE (-1);
3439 if (c != '=')
3440 return -1;
3441 continue;
3444 if (!IS_BASE64 (c))
3445 return -1;
3446 value |= base64_char_to_value[c] << 6;
3448 c = (unsigned char) (0xff & value >> 8);
3449 if (multibyte && c >= 128)
3450 e += BYTE8_STRING (c, e);
3451 else
3452 *e++ = c;
3453 nchars++;
3455 /* Process fourth byte of a quadruplet. */
3457 READ_QUADRUPLET_BYTE (-1);
3459 if (c == '=')
3460 continue;
3462 if (!IS_BASE64 (c))
3463 return -1;
3464 value |= base64_char_to_value[c];
3466 c = (unsigned char) (0xff & value);
3467 if (multibyte && c >= 128)
3468 e += BYTE8_STRING (c, e);
3469 else
3470 *e++ = c;
3471 nchars++;
3477 /***********************************************************************
3478 ***** *****
3479 ***** Hash Tables *****
3480 ***** *****
3481 ***********************************************************************/
3483 /* Implemented by gerd@gnu.org. This hash table implementation was
3484 inspired by CMUCL hash tables. */
3486 /* Ideas:
3488 1. For small tables, association lists are probably faster than
3489 hash tables because they have lower overhead.
3491 For uses of hash tables where the O(1) behavior of table
3492 operations is not a requirement, it might therefore be a good idea
3493 not to hash. Instead, we could just do a linear search in the
3494 key_and_value vector of the hash table. This could be done
3495 if a `:linear-search t' argument is given to make-hash-table. */
3498 /* The list of all weak hash tables. Don't staticpro this one. */
3500 static struct Lisp_Hash_Table *weak_hash_tables;
3503 /***********************************************************************
3504 Utilities
3505 ***********************************************************************/
3507 static void
3508 CHECK_HASH_TABLE (Lisp_Object x)
3510 CHECK_TYPE (HASH_TABLE_P (x), Qhash_table_p, x);
3513 static void
3514 set_hash_key_and_value (struct Lisp_Hash_Table *h, Lisp_Object key_and_value)
3516 h->key_and_value = key_and_value;
3518 static void
3519 set_hash_next (struct Lisp_Hash_Table *h, Lisp_Object next)
3521 h->next = next;
3523 static void
3524 set_hash_next_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3526 gc_aset (h->next, idx, val);
3528 static void
3529 set_hash_hash (struct Lisp_Hash_Table *h, Lisp_Object hash)
3531 h->hash = hash;
3533 static void
3534 set_hash_hash_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3536 gc_aset (h->hash, idx, val);
3538 static void
3539 set_hash_index (struct Lisp_Hash_Table *h, Lisp_Object index)
3541 h->index = index;
3543 static void
3544 set_hash_index_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3546 gc_aset (h->index, idx, val);
3549 /* If OBJ is a Lisp hash table, return a pointer to its struct
3550 Lisp_Hash_Table. Otherwise, signal an error. */
3552 static struct Lisp_Hash_Table *
3553 check_hash_table (Lisp_Object obj)
3555 CHECK_HASH_TABLE (obj);
3556 return XHASH_TABLE (obj);
3560 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3561 number. A number is "almost" a prime number if it is not divisible
3562 by any integer in the range 2 .. (NEXT_ALMOST_PRIME_LIMIT - 1). */
3564 EMACS_INT
3565 next_almost_prime (EMACS_INT n)
3567 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
3568 for (n |= 1; ; n += 2)
3569 if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
3570 return n;
3574 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3575 which USED[I] is non-zero. If found at index I in ARGS, set
3576 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3577 0. This function is used to extract a keyword/argument pair from
3578 a DEFUN parameter list. */
3580 static ptrdiff_t
3581 get_key_arg (Lisp_Object key, ptrdiff_t nargs, Lisp_Object *args, char *used)
3583 ptrdiff_t i;
3585 for (i = 1; i < nargs; i++)
3586 if (!used[i - 1] && EQ (args[i - 1], key))
3588 used[i - 1] = 1;
3589 used[i] = 1;
3590 return i;
3593 return 0;
3597 /* Return a Lisp vector which has the same contents as VEC but has
3598 at least INCR_MIN more entries, where INCR_MIN is positive.
3599 If NITEMS_MAX is not -1, do not grow the vector to be any larger
3600 than NITEMS_MAX. Entries in the resulting
3601 vector that are not copied from VEC are set to nil. */
3603 Lisp_Object
3604 larger_vector (Lisp_Object vec, ptrdiff_t incr_min, ptrdiff_t nitems_max)
3606 struct Lisp_Vector *v;
3607 ptrdiff_t incr, incr_max, old_size, new_size;
3608 ptrdiff_t C_language_max = min (PTRDIFF_MAX, SIZE_MAX) / sizeof *v->contents;
3609 ptrdiff_t n_max = (0 <= nitems_max && nitems_max < C_language_max
3610 ? nitems_max : C_language_max);
3611 eassert (VECTORP (vec));
3612 eassert (0 < incr_min && -1 <= nitems_max);
3613 old_size = ASIZE (vec);
3614 incr_max = n_max - old_size;
3615 incr = max (incr_min, min (old_size >> 1, incr_max));
3616 if (incr_max < incr)
3617 memory_full (SIZE_MAX);
3618 new_size = old_size + incr;
3619 v = allocate_vector (new_size);
3620 memcpy (v->contents, XVECTOR (vec)->contents, old_size * sizeof *v->contents);
3621 memclear (v->contents + old_size, incr * word_size);
3622 XSETVECTOR (vec, v);
3623 return vec;
3627 /***********************************************************************
3628 Low-level Functions
3629 ***********************************************************************/
3631 static struct hash_table_test hashtest_eq;
3632 struct hash_table_test hashtest_eql, hashtest_equal;
3634 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3635 HASH2 in hash table H using `eql'. Value is true if KEY1 and
3636 KEY2 are the same. */
3638 static bool
3639 cmpfn_eql (struct hash_table_test *ht,
3640 Lisp_Object key1,
3641 Lisp_Object key2)
3643 return (FLOATP (key1)
3644 && FLOATP (key2)
3645 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3649 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3650 HASH2 in hash table H using `equal'. Value is true if KEY1 and
3651 KEY2 are the same. */
3653 static bool
3654 cmpfn_equal (struct hash_table_test *ht,
3655 Lisp_Object key1,
3656 Lisp_Object key2)
3658 return !NILP (Fequal (key1, key2));
3662 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3663 HASH2 in hash table H using H->user_cmp_function. Value is true
3664 if KEY1 and KEY2 are the same. */
3666 static bool
3667 cmpfn_user_defined (struct hash_table_test *ht,
3668 Lisp_Object key1,
3669 Lisp_Object key2)
3671 return !NILP (call2 (ht->user_cmp_function, key1, key2));
3675 /* Value is a hash code for KEY for use in hash table H which uses
3676 `eq' to compare keys. The hash code returned is guaranteed to fit
3677 in a Lisp integer. */
3679 static EMACS_UINT
3680 hashfn_eq (struct hash_table_test *ht, Lisp_Object key)
3682 EMACS_UINT hash = XHASH (key) ^ XTYPE (key);
3683 return hash;
3686 /* Value is a hash code for KEY for use in hash table H which uses
3687 `eql' to compare keys. The hash code returned is guaranteed to fit
3688 in a Lisp integer. */
3690 static EMACS_UINT
3691 hashfn_eql (struct hash_table_test *ht, Lisp_Object key)
3693 EMACS_UINT hash;
3694 if (FLOATP (key))
3695 hash = sxhash (key, 0);
3696 else
3697 hash = XHASH (key) ^ XTYPE (key);
3698 return hash;
3701 /* Value is a hash code for KEY for use in hash table H which uses
3702 `equal' to compare keys. The hash code returned is guaranteed to fit
3703 in a Lisp integer. */
3705 static EMACS_UINT
3706 hashfn_equal (struct hash_table_test *ht, Lisp_Object key)
3708 EMACS_UINT hash = sxhash (key, 0);
3709 return hash;
3712 /* Value is a hash code for KEY for use in hash table H which uses as
3713 user-defined function to compare keys. The hash code returned is
3714 guaranteed to fit in a Lisp integer. */
3716 static EMACS_UINT
3717 hashfn_user_defined (struct hash_table_test *ht, Lisp_Object key)
3719 Lisp_Object hash = call1 (ht->user_hash_function, key);
3720 return hashfn_eq (ht, hash);
3723 /* Allocate basically initialized hash table. */
3725 static struct Lisp_Hash_Table *
3726 allocate_hash_table (void)
3728 return ALLOCATE_PSEUDOVECTOR (struct Lisp_Hash_Table,
3729 count, PVEC_HASH_TABLE);
3732 /* An upper bound on the size of a hash table index. It must fit in
3733 ptrdiff_t and be a valid Emacs fixnum. */
3734 #define INDEX_SIZE_BOUND \
3735 ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX / word_size))
3737 /* Create and initialize a new hash table.
3739 TEST specifies the test the hash table will use to compare keys.
3740 It must be either one of the predefined tests `eq', `eql' or
3741 `equal' or a symbol denoting a user-defined test named TEST with
3742 test and hash functions USER_TEST and USER_HASH.
3744 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3746 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3747 new size when it becomes full is computed by adding REHASH_SIZE to
3748 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3749 table's new size is computed by multiplying its old size with
3750 REHASH_SIZE.
3752 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3753 be resized when the ratio of (number of entries in the table) /
3754 (table size) is >= REHASH_THRESHOLD.
3756 WEAK specifies the weakness of the table. If non-nil, it must be
3757 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3759 Lisp_Object
3760 make_hash_table (struct hash_table_test test,
3761 Lisp_Object size, Lisp_Object rehash_size,
3762 Lisp_Object rehash_threshold, Lisp_Object weak)
3764 struct Lisp_Hash_Table *h;
3765 Lisp_Object table;
3766 EMACS_INT index_size, sz;
3767 ptrdiff_t i;
3768 double index_float;
3770 /* Preconditions. */
3771 eassert (SYMBOLP (test.name));
3772 eassert (INTEGERP (size) && XINT (size) >= 0);
3773 eassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3774 || (FLOATP (rehash_size) && 1 < XFLOAT_DATA (rehash_size)));
3775 eassert (FLOATP (rehash_threshold)
3776 && 0 < XFLOAT_DATA (rehash_threshold)
3777 && XFLOAT_DATA (rehash_threshold) <= 1.0);
3779 if (XFASTINT (size) == 0)
3780 size = make_number (1);
3782 sz = XFASTINT (size);
3783 index_float = sz / XFLOAT_DATA (rehash_threshold);
3784 index_size = (index_float < INDEX_SIZE_BOUND + 1
3785 ? next_almost_prime (index_float)
3786 : INDEX_SIZE_BOUND + 1);
3787 if (INDEX_SIZE_BOUND < max (index_size, 2 * sz))
3788 error ("Hash table too large");
3790 /* Allocate a table and initialize it. */
3791 h = allocate_hash_table ();
3793 /* Initialize hash table slots. */
3794 h->test = test;
3795 h->weak = weak;
3796 h->rehash_threshold = rehash_threshold;
3797 h->rehash_size = rehash_size;
3798 h->count = 0;
3799 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3800 h->hash = Fmake_vector (size, Qnil);
3801 h->next = Fmake_vector (size, Qnil);
3802 h->index = Fmake_vector (make_number (index_size), Qnil);
3804 /* Set up the free list. */
3805 for (i = 0; i < sz - 1; ++i)
3806 set_hash_next_slot (h, i, make_number (i + 1));
3807 h->next_free = make_number (0);
3809 XSET_HASH_TABLE (table, h);
3810 eassert (HASH_TABLE_P (table));
3811 eassert (XHASH_TABLE (table) == h);
3813 /* Maybe add this hash table to the list of all weak hash tables. */
3814 if (NILP (h->weak))
3815 h->next_weak = NULL;
3816 else
3818 h->next_weak = weak_hash_tables;
3819 weak_hash_tables = h;
3822 return table;
3826 /* Return a copy of hash table H1. Keys and values are not copied,
3827 only the table itself is. */
3829 static Lisp_Object
3830 copy_hash_table (struct Lisp_Hash_Table *h1)
3832 Lisp_Object table;
3833 struct Lisp_Hash_Table *h2;
3835 h2 = allocate_hash_table ();
3836 *h2 = *h1;
3837 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
3838 h2->hash = Fcopy_sequence (h1->hash);
3839 h2->next = Fcopy_sequence (h1->next);
3840 h2->index = Fcopy_sequence (h1->index);
3841 XSET_HASH_TABLE (table, h2);
3843 /* Maybe add this hash table to the list of all weak hash tables. */
3844 if (!NILP (h2->weak))
3846 h2->next_weak = weak_hash_tables;
3847 weak_hash_tables = h2;
3850 return table;
3854 /* Resize hash table H if it's too full. If H cannot be resized
3855 because it's already too large, throw an error. */
3857 static void
3858 maybe_resize_hash_table (struct Lisp_Hash_Table *h)
3860 if (NILP (h->next_free))
3862 ptrdiff_t old_size = HASH_TABLE_SIZE (h);
3863 EMACS_INT new_size, index_size, nsize;
3864 ptrdiff_t i;
3865 double index_float;
3867 if (INTEGERP (h->rehash_size))
3868 new_size = old_size + XFASTINT (h->rehash_size);
3869 else
3871 double float_new_size = old_size * XFLOAT_DATA (h->rehash_size);
3872 if (float_new_size < INDEX_SIZE_BOUND + 1)
3874 new_size = float_new_size;
3875 if (new_size <= old_size)
3876 new_size = old_size + 1;
3878 else
3879 new_size = INDEX_SIZE_BOUND + 1;
3881 index_float = new_size / XFLOAT_DATA (h->rehash_threshold);
3882 index_size = (index_float < INDEX_SIZE_BOUND + 1
3883 ? next_almost_prime (index_float)
3884 : INDEX_SIZE_BOUND + 1);
3885 nsize = max (index_size, 2 * new_size);
3886 if (INDEX_SIZE_BOUND < nsize)
3887 error ("Hash table too large to resize");
3889 #ifdef ENABLE_CHECKING
3890 if (HASH_TABLE_P (Vpurify_flag)
3891 && XHASH_TABLE (Vpurify_flag) == h)
3892 message ("Growing hash table to: %"pI"d", new_size);
3893 #endif
3895 set_hash_key_and_value (h, larger_vector (h->key_and_value,
3896 2 * (new_size - old_size), -1));
3897 set_hash_next (h, larger_vector (h->next, new_size - old_size, -1));
3898 set_hash_hash (h, larger_vector (h->hash, new_size - old_size, -1));
3899 set_hash_index (h, Fmake_vector (make_number (index_size), Qnil));
3901 /* Update the free list. Do it so that new entries are added at
3902 the end of the free list. This makes some operations like
3903 maphash faster. */
3904 for (i = old_size; i < new_size - 1; ++i)
3905 set_hash_next_slot (h, i, make_number (i + 1));
3907 if (!NILP (h->next_free))
3909 Lisp_Object last, next;
3911 last = h->next_free;
3912 while (next = HASH_NEXT (h, XFASTINT (last)),
3913 !NILP (next))
3914 last = next;
3916 set_hash_next_slot (h, XFASTINT (last), make_number (old_size));
3918 else
3919 XSETFASTINT (h->next_free, old_size);
3921 /* Rehash. */
3922 for (i = 0; i < old_size; ++i)
3923 if (!NILP (HASH_HASH (h, i)))
3925 EMACS_UINT hash_code = XUINT (HASH_HASH (h, i));
3926 ptrdiff_t start_of_bucket = hash_code % ASIZE (h->index);
3927 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
3928 set_hash_index_slot (h, start_of_bucket, make_number (i));
3934 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3935 the hash code of KEY. Value is the index of the entry in H
3936 matching KEY, or -1 if not found. */
3938 ptrdiff_t
3939 hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, EMACS_UINT *hash)
3941 EMACS_UINT hash_code;
3942 ptrdiff_t start_of_bucket;
3943 Lisp_Object idx;
3945 hash_code = h->test.hashfn (&h->test, key);
3946 eassert ((hash_code & ~INTMASK) == 0);
3947 if (hash)
3948 *hash = hash_code;
3950 start_of_bucket = hash_code % ASIZE (h->index);
3951 idx = HASH_INDEX (h, start_of_bucket);
3953 while (!NILP (idx))
3955 ptrdiff_t i = XFASTINT (idx);
3956 if (EQ (key, HASH_KEY (h, i))
3957 || (h->test.cmpfn
3958 && hash_code == XUINT (HASH_HASH (h, i))
3959 && h->test.cmpfn (&h->test, key, HASH_KEY (h, i))))
3960 break;
3961 idx = HASH_NEXT (h, i);
3964 return NILP (idx) ? -1 : XFASTINT (idx);
3968 /* Put an entry into hash table H that associates KEY with VALUE.
3969 HASH is a previously computed hash code of KEY.
3970 Value is the index of the entry in H matching KEY. */
3972 ptrdiff_t
3973 hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
3974 EMACS_UINT hash)
3976 ptrdiff_t start_of_bucket, i;
3978 eassert ((hash & ~INTMASK) == 0);
3980 /* Increment count after resizing because resizing may fail. */
3981 maybe_resize_hash_table (h);
3982 h->count++;
3984 /* Store key/value in the key_and_value vector. */
3985 i = XFASTINT (h->next_free);
3986 h->next_free = HASH_NEXT (h, i);
3987 set_hash_key_slot (h, i, key);
3988 set_hash_value_slot (h, i, value);
3990 /* Remember its hash code. */
3991 set_hash_hash_slot (h, i, make_number (hash));
3993 /* Add new entry to its collision chain. */
3994 start_of_bucket = hash % ASIZE (h->index);
3995 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
3996 set_hash_index_slot (h, start_of_bucket, make_number (i));
3997 return i;
4001 /* Remove the entry matching KEY from hash table H, if there is one. */
4003 static void
4004 hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
4006 EMACS_UINT hash_code;
4007 ptrdiff_t start_of_bucket;
4008 Lisp_Object idx, prev;
4010 hash_code = h->test.hashfn (&h->test, key);
4011 eassert ((hash_code & ~INTMASK) == 0);
4012 start_of_bucket = hash_code % ASIZE (h->index);
4013 idx = HASH_INDEX (h, start_of_bucket);
4014 prev = Qnil;
4016 while (!NILP (idx))
4018 ptrdiff_t i = XFASTINT (idx);
4020 if (EQ (key, HASH_KEY (h, i))
4021 || (h->test.cmpfn
4022 && hash_code == XUINT (HASH_HASH (h, i))
4023 && h->test.cmpfn (&h->test, key, HASH_KEY (h, i))))
4025 /* Take entry out of collision chain. */
4026 if (NILP (prev))
4027 set_hash_index_slot (h, start_of_bucket, HASH_NEXT (h, i));
4028 else
4029 set_hash_next_slot (h, XFASTINT (prev), HASH_NEXT (h, i));
4031 /* Clear slots in key_and_value and add the slots to
4032 the free list. */
4033 set_hash_key_slot (h, i, Qnil);
4034 set_hash_value_slot (h, i, Qnil);
4035 set_hash_hash_slot (h, i, Qnil);
4036 set_hash_next_slot (h, i, h->next_free);
4037 h->next_free = make_number (i);
4038 h->count--;
4039 eassert (h->count >= 0);
4040 break;
4042 else
4044 prev = idx;
4045 idx = HASH_NEXT (h, i);
4051 /* Clear hash table H. */
4053 static void
4054 hash_clear (struct Lisp_Hash_Table *h)
4056 if (h->count > 0)
4058 ptrdiff_t i, size = HASH_TABLE_SIZE (h);
4060 for (i = 0; i < size; ++i)
4062 set_hash_next_slot (h, i, i < size - 1 ? make_number (i + 1) : Qnil);
4063 set_hash_key_slot (h, i, Qnil);
4064 set_hash_value_slot (h, i, Qnil);
4065 set_hash_hash_slot (h, i, Qnil);
4068 for (i = 0; i < ASIZE (h->index); ++i)
4069 ASET (h->index, i, Qnil);
4071 h->next_free = make_number (0);
4072 h->count = 0;
4078 /************************************************************************
4079 Weak Hash Tables
4080 ************************************************************************/
4082 /* Sweep weak hash table H. REMOVE_ENTRIES_P means remove
4083 entries from the table that don't survive the current GC.
4084 !REMOVE_ENTRIES_P means mark entries that are in use. Value is
4085 true if anything was marked. */
4087 static bool
4088 sweep_weak_table (struct Lisp_Hash_Table *h, bool remove_entries_p)
4090 ptrdiff_t bucket, n;
4091 bool marked;
4093 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
4094 marked = 0;
4096 for (bucket = 0; bucket < n; ++bucket)
4098 Lisp_Object idx, next, prev;
4100 /* Follow collision chain, removing entries that
4101 don't survive this garbage collection. */
4102 prev = Qnil;
4103 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
4105 ptrdiff_t i = XFASTINT (idx);
4106 bool key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4107 bool value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4108 bool remove_p;
4110 if (EQ (h->weak, Qkey))
4111 remove_p = !key_known_to_survive_p;
4112 else if (EQ (h->weak, Qvalue))
4113 remove_p = !value_known_to_survive_p;
4114 else if (EQ (h->weak, Qkey_or_value))
4115 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4116 else if (EQ (h->weak, Qkey_and_value))
4117 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4118 else
4119 emacs_abort ();
4121 next = HASH_NEXT (h, i);
4123 if (remove_entries_p)
4125 if (remove_p)
4127 /* Take out of collision chain. */
4128 if (NILP (prev))
4129 set_hash_index_slot (h, bucket, next);
4130 else
4131 set_hash_next_slot (h, XFASTINT (prev), next);
4133 /* Add to free list. */
4134 set_hash_next_slot (h, i, h->next_free);
4135 h->next_free = idx;
4137 /* Clear key, value, and hash. */
4138 set_hash_key_slot (h, i, Qnil);
4139 set_hash_value_slot (h, i, Qnil);
4140 set_hash_hash_slot (h, i, Qnil);
4142 h->count--;
4144 else
4146 prev = idx;
4149 else
4151 if (!remove_p)
4153 /* Make sure key and value survive. */
4154 if (!key_known_to_survive_p)
4156 mark_object (HASH_KEY (h, i));
4157 marked = 1;
4160 if (!value_known_to_survive_p)
4162 mark_object (HASH_VALUE (h, i));
4163 marked = 1;
4170 return marked;
4173 /* Remove elements from weak hash tables that don't survive the
4174 current garbage collection. Remove weak tables that don't survive
4175 from Vweak_hash_tables. Called from gc_sweep. */
4177 NO_INLINE /* For better stack traces */
4178 void
4179 sweep_weak_hash_tables (void)
4181 struct Lisp_Hash_Table *h, *used, *next;
4182 bool marked;
4184 /* Mark all keys and values that are in use. Keep on marking until
4185 there is no more change. This is necessary for cases like
4186 value-weak table A containing an entry X -> Y, where Y is used in a
4187 key-weak table B, Z -> Y. If B comes after A in the list of weak
4188 tables, X -> Y might be removed from A, although when looking at B
4189 one finds that it shouldn't. */
4192 marked = 0;
4193 for (h = weak_hash_tables; h; h = h->next_weak)
4195 if (h->header.size & ARRAY_MARK_FLAG)
4196 marked |= sweep_weak_table (h, 0);
4199 while (marked);
4201 /* Remove tables and entries that aren't used. */
4202 for (h = weak_hash_tables, used = NULL; h; h = next)
4204 next = h->next_weak;
4206 if (h->header.size & ARRAY_MARK_FLAG)
4208 /* TABLE is marked as used. Sweep its contents. */
4209 if (h->count > 0)
4210 sweep_weak_table (h, 1);
4212 /* Add table to the list of used weak hash tables. */
4213 h->next_weak = used;
4214 used = h;
4218 weak_hash_tables = used;
4223 /***********************************************************************
4224 Hash Code Computation
4225 ***********************************************************************/
4227 /* Maximum depth up to which to dive into Lisp structures. */
4229 #define SXHASH_MAX_DEPTH 3
4231 /* Maximum length up to which to take list and vector elements into
4232 account. */
4234 #define SXHASH_MAX_LEN 7
4236 /* Return a hash for string PTR which has length LEN. The hash value
4237 can be any EMACS_UINT value. */
4239 EMACS_UINT
4240 hash_string (char const *ptr, ptrdiff_t len)
4242 char const *p = ptr;
4243 char const *end = p + len;
4244 unsigned char c;
4245 EMACS_UINT hash = 0;
4247 while (p != end)
4249 c = *p++;
4250 hash = sxhash_combine (hash, c);
4253 return hash;
4256 /* Return a hash for string PTR which has length LEN. The hash
4257 code returned is guaranteed to fit in a Lisp integer. */
4259 static EMACS_UINT
4260 sxhash_string (char const *ptr, ptrdiff_t len)
4262 EMACS_UINT hash = hash_string (ptr, len);
4263 return SXHASH_REDUCE (hash);
4266 /* Return a hash for the floating point value VAL. */
4268 static EMACS_UINT
4269 sxhash_float (double val)
4271 EMACS_UINT hash = 0;
4272 enum {
4273 WORDS_PER_DOUBLE = (sizeof val / sizeof hash
4274 + (sizeof val % sizeof hash != 0))
4276 union {
4277 double val;
4278 EMACS_UINT word[WORDS_PER_DOUBLE];
4279 } u;
4280 int i;
4281 u.val = val;
4282 memset (&u.val + 1, 0, sizeof u - sizeof u.val);
4283 for (i = 0; i < WORDS_PER_DOUBLE; i++)
4284 hash = sxhash_combine (hash, u.word[i]);
4285 return SXHASH_REDUCE (hash);
4288 /* Return a hash for list LIST. DEPTH is the current depth in the
4289 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4291 static EMACS_UINT
4292 sxhash_list (Lisp_Object list, int depth)
4294 EMACS_UINT hash = 0;
4295 int i;
4297 if (depth < SXHASH_MAX_DEPTH)
4298 for (i = 0;
4299 CONSP (list) && i < SXHASH_MAX_LEN;
4300 list = XCDR (list), ++i)
4302 EMACS_UINT hash2 = sxhash (XCAR (list), depth + 1);
4303 hash = sxhash_combine (hash, hash2);
4306 if (!NILP (list))
4308 EMACS_UINT hash2 = sxhash (list, depth + 1);
4309 hash = sxhash_combine (hash, hash2);
4312 return SXHASH_REDUCE (hash);
4316 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4317 the Lisp structure. */
4319 static EMACS_UINT
4320 sxhash_vector (Lisp_Object vec, int depth)
4322 EMACS_UINT hash = ASIZE (vec);
4323 int i, n;
4325 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4326 for (i = 0; i < n; ++i)
4328 EMACS_UINT hash2 = sxhash (AREF (vec, i), depth + 1);
4329 hash = sxhash_combine (hash, hash2);
4332 return SXHASH_REDUCE (hash);
4335 /* Return a hash for bool-vector VECTOR. */
4337 static EMACS_UINT
4338 sxhash_bool_vector (Lisp_Object vec)
4340 EMACS_INT size = bool_vector_size (vec);
4341 EMACS_UINT hash = size;
4342 int i, n;
4344 n = min (SXHASH_MAX_LEN, bool_vector_words (size));
4345 for (i = 0; i < n; ++i)
4346 hash = sxhash_combine (hash, bool_vector_data (vec)[i]);
4348 return SXHASH_REDUCE (hash);
4352 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4353 structure. Value is an unsigned integer clipped to INTMASK. */
4355 EMACS_UINT
4356 sxhash (Lisp_Object obj, int depth)
4358 EMACS_UINT hash;
4360 if (depth > SXHASH_MAX_DEPTH)
4361 return 0;
4363 switch (XTYPE (obj))
4365 case_Lisp_Int:
4366 hash = XUINT (obj);
4367 break;
4369 case Lisp_Misc:
4370 case Lisp_Symbol:
4371 hash = XHASH (obj);
4372 break;
4374 case Lisp_String:
4375 hash = sxhash_string (SSDATA (obj), SBYTES (obj));
4376 break;
4378 /* This can be everything from a vector to an overlay. */
4379 case Lisp_Vectorlike:
4380 if (VECTORP (obj))
4381 /* According to the CL HyperSpec, two arrays are equal only if
4382 they are `eq', except for strings and bit-vectors. In
4383 Emacs, this works differently. We have to compare element
4384 by element. */
4385 hash = sxhash_vector (obj, depth);
4386 else if (BOOL_VECTOR_P (obj))
4387 hash = sxhash_bool_vector (obj);
4388 else
4389 /* Others are `equal' if they are `eq', so let's take their
4390 address as hash. */
4391 hash = XHASH (obj);
4392 break;
4394 case Lisp_Cons:
4395 hash = sxhash_list (obj, depth);
4396 break;
4398 case Lisp_Float:
4399 hash = sxhash_float (XFLOAT_DATA (obj));
4400 break;
4402 default:
4403 emacs_abort ();
4406 return hash;
4411 /***********************************************************************
4412 Lisp Interface
4413 ***********************************************************************/
4416 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4417 doc: /* Compute a hash code for OBJ and return it as integer. */)
4418 (Lisp_Object obj)
4420 EMACS_UINT hash = sxhash (obj, 0);
4421 return make_number (hash);
4425 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4426 doc: /* Create and return a new hash table.
4428 Arguments are specified as keyword/argument pairs. The following
4429 arguments are defined:
4431 :test TEST -- TEST must be a symbol that specifies how to compare
4432 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4433 `equal'. User-supplied test and hash functions can be specified via
4434 `define-hash-table-test'.
4436 :size SIZE -- A hint as to how many elements will be put in the table.
4437 Default is 65.
4439 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4440 fills up. If REHASH-SIZE is an integer, increase the size by that
4441 amount. If it is a float, it must be > 1.0, and the new size is the
4442 old size multiplied by that factor. Default is 1.5.
4444 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4445 Resize the hash table when the ratio (number of entries / table size)
4446 is greater than or equal to THRESHOLD. Default is 0.8.
4448 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4449 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4450 returned is a weak table. Key/value pairs are removed from a weak
4451 hash table when there are no non-weak references pointing to their
4452 key, value, one of key or value, or both key and value, depending on
4453 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4454 is nil.
4456 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4457 (ptrdiff_t nargs, Lisp_Object *args)
4459 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4460 struct hash_table_test testdesc;
4461 ptrdiff_t i;
4462 USE_SAFE_ALLOCA;
4464 /* The vector `used' is used to keep track of arguments that
4465 have been consumed. */
4466 char *used = SAFE_ALLOCA (nargs * sizeof *used);
4467 memset (used, 0, nargs * sizeof *used);
4469 /* See if there's a `:test TEST' among the arguments. */
4470 i = get_key_arg (QCtest, nargs, args, used);
4471 test = i ? args[i] : Qeql;
4472 if (EQ (test, Qeq))
4473 testdesc = hashtest_eq;
4474 else if (EQ (test, Qeql))
4475 testdesc = hashtest_eql;
4476 else if (EQ (test, Qequal))
4477 testdesc = hashtest_equal;
4478 else
4480 /* See if it is a user-defined test. */
4481 Lisp_Object prop;
4483 prop = Fget (test, Qhash_table_test);
4484 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4485 signal_error ("Invalid hash table test", test);
4486 testdesc.name = test;
4487 testdesc.user_cmp_function = XCAR (prop);
4488 testdesc.user_hash_function = XCAR (XCDR (prop));
4489 testdesc.hashfn = hashfn_user_defined;
4490 testdesc.cmpfn = cmpfn_user_defined;
4493 /* See if there's a `:size SIZE' argument. */
4494 i = get_key_arg (QCsize, nargs, args, used);
4495 size = i ? args[i] : Qnil;
4496 if (NILP (size))
4497 size = make_number (DEFAULT_HASH_SIZE);
4498 else if (!INTEGERP (size) || XINT (size) < 0)
4499 signal_error ("Invalid hash table size", size);
4501 /* Look for `:rehash-size SIZE'. */
4502 i = get_key_arg (QCrehash_size, nargs, args, used);
4503 rehash_size = i ? args[i] : make_float (DEFAULT_REHASH_SIZE);
4504 if (! ((INTEGERP (rehash_size) && 0 < XINT (rehash_size))
4505 || (FLOATP (rehash_size) && 1 < XFLOAT_DATA (rehash_size))))
4506 signal_error ("Invalid hash table rehash size", rehash_size);
4508 /* Look for `:rehash-threshold THRESHOLD'. */
4509 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4510 rehash_threshold = i ? args[i] : make_float (DEFAULT_REHASH_THRESHOLD);
4511 if (! (FLOATP (rehash_threshold)
4512 && 0 < XFLOAT_DATA (rehash_threshold)
4513 && XFLOAT_DATA (rehash_threshold) <= 1))
4514 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4516 /* Look for `:weakness WEAK'. */
4517 i = get_key_arg (QCweakness, nargs, args, used);
4518 weak = i ? args[i] : Qnil;
4519 if (EQ (weak, Qt))
4520 weak = Qkey_and_value;
4521 if (!NILP (weak)
4522 && !EQ (weak, Qkey)
4523 && !EQ (weak, Qvalue)
4524 && !EQ (weak, Qkey_or_value)
4525 && !EQ (weak, Qkey_and_value))
4526 signal_error ("Invalid hash table weakness", weak);
4528 /* Now, all args should have been used up, or there's a problem. */
4529 for (i = 0; i < nargs; ++i)
4530 if (!used[i])
4531 signal_error ("Invalid argument list", args[i]);
4533 SAFE_FREE ();
4534 return make_hash_table (testdesc, size, rehash_size, rehash_threshold, weak);
4538 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4539 doc: /* Return a copy of hash table TABLE. */)
4540 (Lisp_Object table)
4542 return copy_hash_table (check_hash_table (table));
4546 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4547 doc: /* Return the number of elements in TABLE. */)
4548 (Lisp_Object table)
4550 return make_number (check_hash_table (table)->count);
4554 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4555 Shash_table_rehash_size, 1, 1, 0,
4556 doc: /* Return the current rehash size of TABLE. */)
4557 (Lisp_Object table)
4559 return check_hash_table (table)->rehash_size;
4563 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4564 Shash_table_rehash_threshold, 1, 1, 0,
4565 doc: /* Return the current rehash threshold of TABLE. */)
4566 (Lisp_Object table)
4568 return check_hash_table (table)->rehash_threshold;
4572 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4573 doc: /* Return the size of TABLE.
4574 The size can be used as an argument to `make-hash-table' to create
4575 a hash table than can hold as many elements as TABLE holds
4576 without need for resizing. */)
4577 (Lisp_Object table)
4579 struct Lisp_Hash_Table *h = check_hash_table (table);
4580 return make_number (HASH_TABLE_SIZE (h));
4584 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4585 doc: /* Return the test TABLE uses. */)
4586 (Lisp_Object table)
4588 return check_hash_table (table)->test.name;
4592 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4593 1, 1, 0,
4594 doc: /* Return the weakness of TABLE. */)
4595 (Lisp_Object table)
4597 return check_hash_table (table)->weak;
4601 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4602 doc: /* Return t if OBJ is a Lisp hash table object. */)
4603 (Lisp_Object obj)
4605 return HASH_TABLE_P (obj) ? Qt : Qnil;
4609 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4610 doc: /* Clear hash table TABLE and return it. */)
4611 (Lisp_Object table)
4613 hash_clear (check_hash_table (table));
4614 /* Be compatible with XEmacs. */
4615 return table;
4619 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4620 doc: /* Look up KEY in TABLE and return its associated value.
4621 If KEY is not found, return DFLT which defaults to nil. */)
4622 (Lisp_Object key, Lisp_Object table, Lisp_Object dflt)
4624 struct Lisp_Hash_Table *h = check_hash_table (table);
4625 ptrdiff_t i = hash_lookup (h, key, NULL);
4626 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4630 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4631 doc: /* Associate KEY with VALUE in hash table TABLE.
4632 If KEY is already present in table, replace its current value with
4633 VALUE. In any case, return VALUE. */)
4634 (Lisp_Object key, Lisp_Object value, Lisp_Object table)
4636 struct Lisp_Hash_Table *h = check_hash_table (table);
4637 ptrdiff_t i;
4638 EMACS_UINT hash;
4640 i = hash_lookup (h, key, &hash);
4641 if (i >= 0)
4642 set_hash_value_slot (h, i, value);
4643 else
4644 hash_put (h, key, value, hash);
4646 return value;
4650 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4651 doc: /* Remove KEY from TABLE. */)
4652 (Lisp_Object key, Lisp_Object table)
4654 struct Lisp_Hash_Table *h = check_hash_table (table);
4655 hash_remove_from_table (h, key);
4656 return Qnil;
4660 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4661 doc: /* Call FUNCTION for all entries in hash table TABLE.
4662 FUNCTION is called with two arguments, KEY and VALUE.
4663 `maphash' always returns nil. */)
4664 (Lisp_Object function, Lisp_Object table)
4666 struct Lisp_Hash_Table *h = check_hash_table (table);
4668 for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
4669 if (!NILP (HASH_HASH (h, i)))
4670 call2 (function, HASH_KEY (h, i), HASH_VALUE (h, i));
4672 return Qnil;
4676 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4677 Sdefine_hash_table_test, 3, 3, 0,
4678 doc: /* Define a new hash table test with name NAME, a symbol.
4680 In hash tables created with NAME specified as test, use TEST to
4681 compare keys, and HASH for computing hash codes of keys.
4683 TEST must be a function taking two arguments and returning non-nil if
4684 both arguments are the same. HASH must be a function taking one
4685 argument and returning an object that is the hash code of the argument.
4686 It should be the case that if (eq (funcall HASH x1) (funcall HASH x2))
4687 returns nil, then (funcall TEST x1 x2) also returns nil. */)
4688 (Lisp_Object name, Lisp_Object test, Lisp_Object hash)
4690 return Fput (name, Qhash_table_test, list2 (test, hash));
4695 /************************************************************************
4696 MD5, SHA-1, and SHA-2
4697 ************************************************************************/
4699 #include "md5.h"
4700 #include "sha1.h"
4701 #include "sha256.h"
4702 #include "sha512.h"
4704 /* ALGORITHM is a symbol: md5, sha1, sha224 and so on. */
4706 static Lisp_Object
4707 secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start,
4708 Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror,
4709 Lisp_Object binary)
4711 int i;
4712 ptrdiff_t size, start_char = 0, start_byte, end_char = 0, end_byte;
4713 register EMACS_INT b, e;
4714 register struct buffer *bp;
4715 EMACS_INT temp;
4716 int digest_size;
4717 void *(*hash_func) (const char *, size_t, void *);
4718 Lisp_Object digest;
4720 CHECK_SYMBOL (algorithm);
4722 if (STRINGP (object))
4724 if (NILP (coding_system))
4726 /* Decide the coding-system to encode the data with. */
4728 if (STRING_MULTIBYTE (object))
4729 /* use default, we can't guess correct value */
4730 coding_system = preferred_coding_system ();
4731 else
4732 coding_system = Qraw_text;
4735 if (NILP (Fcoding_system_p (coding_system)))
4737 /* Invalid coding system. */
4739 if (!NILP (noerror))
4740 coding_system = Qraw_text;
4741 else
4742 xsignal1 (Qcoding_system_error, coding_system);
4745 if (STRING_MULTIBYTE (object))
4746 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4748 size = SCHARS (object);
4749 validate_subarray (object, start, end, size, &start_char, &end_char);
4751 start_byte = !start_char ? 0 : string_char_to_byte (object, start_char);
4752 end_byte = (end_char == size
4753 ? SBYTES (object)
4754 : string_char_to_byte (object, end_char));
4756 else
4758 struct buffer *prev = current_buffer;
4760 record_unwind_current_buffer ();
4762 CHECK_BUFFER (object);
4764 bp = XBUFFER (object);
4765 set_buffer_internal (bp);
4767 if (NILP (start))
4768 b = BEGV;
4769 else
4771 CHECK_NUMBER_COERCE_MARKER (start);
4772 b = XINT (start);
4775 if (NILP (end))
4776 e = ZV;
4777 else
4779 CHECK_NUMBER_COERCE_MARKER (end);
4780 e = XINT (end);
4783 if (b > e)
4784 temp = b, b = e, e = temp;
4786 if (!(BEGV <= b && e <= ZV))
4787 args_out_of_range (start, end);
4789 if (NILP (coding_system))
4791 /* Decide the coding-system to encode the data with.
4792 See fileio.c:Fwrite-region */
4794 if (!NILP (Vcoding_system_for_write))
4795 coding_system = Vcoding_system_for_write;
4796 else
4798 bool force_raw_text = 0;
4800 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
4801 if (NILP (coding_system)
4802 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
4804 coding_system = Qnil;
4805 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
4806 force_raw_text = 1;
4809 if (NILP (coding_system) && !NILP (Fbuffer_file_name (object)))
4811 /* Check file-coding-system-alist. */
4812 Lisp_Object val = CALLN (Ffind_operation_coding_system,
4813 Qwrite_region, start, end,
4814 Fbuffer_file_name (object));
4815 if (CONSP (val) && !NILP (XCDR (val)))
4816 coding_system = XCDR (val);
4819 if (NILP (coding_system)
4820 && !NILP (BVAR (XBUFFER (object), buffer_file_coding_system)))
4822 /* If we still have not decided a coding system, use the
4823 default value of buffer-file-coding-system. */
4824 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
4827 if (!force_raw_text
4828 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
4829 /* Confirm that VAL can surely encode the current region. */
4830 coding_system = call4 (Vselect_safe_coding_system_function,
4831 make_number (b), make_number (e),
4832 coding_system, Qnil);
4834 if (force_raw_text)
4835 coding_system = Qraw_text;
4838 if (NILP (Fcoding_system_p (coding_system)))
4840 /* Invalid coding system. */
4842 if (!NILP (noerror))
4843 coding_system = Qraw_text;
4844 else
4845 xsignal1 (Qcoding_system_error, coding_system);
4849 object = make_buffer_string (b, e, 0);
4850 set_buffer_internal (prev);
4851 /* Discard the unwind protect for recovering the current
4852 buffer. */
4853 specpdl_ptr--;
4855 if (STRING_MULTIBYTE (object))
4856 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
4857 start_byte = 0;
4858 end_byte = SBYTES (object);
4861 if (EQ (algorithm, Qmd5))
4863 digest_size = MD5_DIGEST_SIZE;
4864 hash_func = md5_buffer;
4866 else if (EQ (algorithm, Qsha1))
4868 digest_size = SHA1_DIGEST_SIZE;
4869 hash_func = sha1_buffer;
4871 else if (EQ (algorithm, Qsha224))
4873 digest_size = SHA224_DIGEST_SIZE;
4874 hash_func = sha224_buffer;
4876 else if (EQ (algorithm, Qsha256))
4878 digest_size = SHA256_DIGEST_SIZE;
4879 hash_func = sha256_buffer;
4881 else if (EQ (algorithm, Qsha384))
4883 digest_size = SHA384_DIGEST_SIZE;
4884 hash_func = sha384_buffer;
4886 else if (EQ (algorithm, Qsha512))
4888 digest_size = SHA512_DIGEST_SIZE;
4889 hash_func = sha512_buffer;
4891 else
4892 error ("Invalid algorithm arg: %s", SDATA (Fsymbol_name (algorithm)));
4894 /* allocate 2 x digest_size so that it can be re-used to hold the
4895 hexified value */
4896 digest = make_uninit_string (digest_size * 2);
4898 hash_func (SSDATA (object) + start_byte,
4899 end_byte - start_byte,
4900 SSDATA (digest));
4902 if (NILP (binary))
4904 unsigned char *p = SDATA (digest);
4905 for (i = digest_size - 1; i >= 0; i--)
4907 static char const hexdigit[16] = "0123456789abcdef";
4908 int p_i = p[i];
4909 p[2 * i] = hexdigit[p_i >> 4];
4910 p[2 * i + 1] = hexdigit[p_i & 0xf];
4912 return digest;
4914 else
4915 return make_unibyte_string (SSDATA (digest), digest_size);
4918 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4919 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4921 A message digest is a cryptographic checksum of a document, and the
4922 algorithm to calculate it is defined in RFC 1321.
4924 The two optional arguments START and END are character positions
4925 specifying for which part of OBJECT the message digest should be
4926 computed. If nil or omitted, the digest is computed for the whole
4927 OBJECT.
4929 The MD5 message digest is computed from the result of encoding the
4930 text in a coding system, not directly from the internal Emacs form of
4931 the text. The optional fourth argument CODING-SYSTEM specifies which
4932 coding system to encode the text with. It should be the same coding
4933 system that you used or will use when actually writing the text into a
4934 file.
4936 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4937 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4938 system would be chosen by default for writing this text into a file.
4940 If OBJECT is a string, the most preferred coding system (see the
4941 command `prefer-coding-system') is used.
4943 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4944 guesswork fails. Normally, an error is signaled in such case. */)
4945 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4947 return secure_hash (Qmd5, object, start, end, coding_system, noerror, Qnil);
4950 DEFUN ("secure-hash", Fsecure_hash, Ssecure_hash, 2, 5, 0,
4951 doc: /* Return the secure hash of OBJECT, a buffer or string.
4952 ALGORITHM is a symbol specifying the hash to use:
4953 md5, sha1, sha224, sha256, sha384 or sha512.
4955 The two optional arguments START and END are positions specifying for
4956 which part of OBJECT to compute the hash. If nil or omitted, uses the
4957 whole OBJECT.
4959 If BINARY is non-nil, returns a string in binary form. */)
4960 (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
4962 return secure_hash (algorithm, object, start, end, Qnil, Qnil, binary);
4965 void
4966 syms_of_fns (void)
4968 DEFSYM (Qmd5, "md5");
4969 DEFSYM (Qsha1, "sha1");
4970 DEFSYM (Qsha224, "sha224");
4971 DEFSYM (Qsha256, "sha256");
4972 DEFSYM (Qsha384, "sha384");
4973 DEFSYM (Qsha512, "sha512");
4975 /* Hash table stuff. */
4976 DEFSYM (Qhash_table_p, "hash-table-p");
4977 DEFSYM (Qeq, "eq");
4978 DEFSYM (Qeql, "eql");
4979 DEFSYM (Qequal, "equal");
4980 DEFSYM (QCtest, ":test");
4981 DEFSYM (QCsize, ":size");
4982 DEFSYM (QCrehash_size, ":rehash-size");
4983 DEFSYM (QCrehash_threshold, ":rehash-threshold");
4984 DEFSYM (QCweakness, ":weakness");
4985 DEFSYM (Qkey, "key");
4986 DEFSYM (Qvalue, "value");
4987 DEFSYM (Qhash_table_test, "hash-table-test");
4988 DEFSYM (Qkey_or_value, "key-or-value");
4989 DEFSYM (Qkey_and_value, "key-and-value");
4991 defsubr (&Ssxhash);
4992 defsubr (&Smake_hash_table);
4993 defsubr (&Scopy_hash_table);
4994 defsubr (&Shash_table_count);
4995 defsubr (&Shash_table_rehash_size);
4996 defsubr (&Shash_table_rehash_threshold);
4997 defsubr (&Shash_table_size);
4998 defsubr (&Shash_table_test);
4999 defsubr (&Shash_table_weakness);
5000 defsubr (&Shash_table_p);
5001 defsubr (&Sclrhash);
5002 defsubr (&Sgethash);
5003 defsubr (&Sputhash);
5004 defsubr (&Sremhash);
5005 defsubr (&Smaphash);
5006 defsubr (&Sdefine_hash_table_test);
5008 DEFSYM (Qstring_lessp, "string-lessp");
5009 DEFSYM (Qprovide, "provide");
5010 DEFSYM (Qrequire, "require");
5011 DEFSYM (Qyes_or_no_p_history, "yes-or-no-p-history");
5012 DEFSYM (Qcursor_in_echo_area, "cursor-in-echo-area");
5013 DEFSYM (Qwidget_type, "widget-type");
5015 staticpro (&string_char_byte_cache_string);
5016 string_char_byte_cache_string = Qnil;
5018 require_nesting_list = Qnil;
5019 staticpro (&require_nesting_list);
5021 Fset (Qyes_or_no_p_history, Qnil);
5023 DEFVAR_LISP ("features", Vfeatures,
5024 doc: /* A list of symbols which are the features of the executing Emacs.
5025 Used by `featurep' and `require', and altered by `provide'. */);
5026 Vfeatures = list1 (Qemacs);
5027 DEFSYM (Qsubfeatures, "subfeatures");
5028 DEFSYM (Qfuncall, "funcall");
5030 #ifdef HAVE_LANGINFO_CODESET
5031 DEFSYM (Qcodeset, "codeset");
5032 DEFSYM (Qdays, "days");
5033 DEFSYM (Qmonths, "months");
5034 DEFSYM (Qpaper, "paper");
5035 #endif /* HAVE_LANGINFO_CODESET */
5037 DEFVAR_BOOL ("use-dialog-box", use_dialog_box,
5038 doc: /* Non-nil means mouse commands use dialog boxes to ask questions.
5039 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5040 invoked by mouse clicks and mouse menu items.
5042 On some platforms, file selection dialogs are also enabled if this is
5043 non-nil. */);
5044 use_dialog_box = 1;
5046 DEFVAR_BOOL ("use-file-dialog", use_file_dialog,
5047 doc: /* Non-nil means mouse commands use a file dialog to ask for files.
5048 This applies to commands from menus and tool bar buttons even when
5049 they are initiated from the keyboard. If `use-dialog-box' is nil,
5050 that disables the use of a file dialog, regardless of the value of
5051 this variable. */);
5052 use_file_dialog = 1;
5054 defsubr (&Sidentity);
5055 defsubr (&Srandom);
5056 defsubr (&Slength);
5057 defsubr (&Ssafe_length);
5058 defsubr (&Sstring_bytes);
5059 defsubr (&Sstring_equal);
5060 defsubr (&Scompare_strings);
5061 defsubr (&Sstring_lessp);
5062 defsubr (&Sstring_collate_lessp);
5063 defsubr (&Sstring_collate_equalp);
5064 defsubr (&Sappend);
5065 defsubr (&Sconcat);
5066 defsubr (&Svconcat);
5067 defsubr (&Scopy_sequence);
5068 defsubr (&Sstring_make_multibyte);
5069 defsubr (&Sstring_make_unibyte);
5070 defsubr (&Sstring_as_multibyte);
5071 defsubr (&Sstring_as_unibyte);
5072 defsubr (&Sstring_to_multibyte);
5073 defsubr (&Sstring_to_unibyte);
5074 defsubr (&Scopy_alist);
5075 defsubr (&Ssubstring);
5076 defsubr (&Ssubstring_no_properties);
5077 defsubr (&Snthcdr);
5078 defsubr (&Snth);
5079 defsubr (&Selt);
5080 defsubr (&Smember);
5081 defsubr (&Smemq);
5082 defsubr (&Smemql);
5083 defsubr (&Sassq);
5084 defsubr (&Sassoc);
5085 defsubr (&Srassq);
5086 defsubr (&Srassoc);
5087 defsubr (&Sdelq);
5088 defsubr (&Sdelete);
5089 defsubr (&Snreverse);
5090 defsubr (&Sreverse);
5091 defsubr (&Ssort);
5092 defsubr (&Splist_get);
5093 defsubr (&Sget);
5094 defsubr (&Splist_put);
5095 defsubr (&Sput);
5096 defsubr (&Slax_plist_get);
5097 defsubr (&Slax_plist_put);
5098 defsubr (&Seql);
5099 defsubr (&Sequal);
5100 defsubr (&Sequal_including_properties);
5101 defsubr (&Sfillarray);
5102 defsubr (&Sclear_string);
5103 defsubr (&Snconc);
5104 defsubr (&Smapcar);
5105 defsubr (&Smapc);
5106 defsubr (&Smapconcat);
5107 defsubr (&Syes_or_no_p);
5108 defsubr (&Sload_average);
5109 defsubr (&Sfeaturep);
5110 defsubr (&Srequire);
5111 defsubr (&Sprovide);
5112 defsubr (&Splist_member);
5113 defsubr (&Swidget_put);
5114 defsubr (&Swidget_get);
5115 defsubr (&Swidget_apply);
5116 defsubr (&Sbase64_encode_region);
5117 defsubr (&Sbase64_decode_region);
5118 defsubr (&Sbase64_encode_string);
5119 defsubr (&Sbase64_decode_string);
5120 defsubr (&Smd5);
5121 defsubr (&Ssecure_hash);
5122 defsubr (&Slocale_info);
5124 hashtest_eq.name = Qeq;
5125 hashtest_eq.user_hash_function = Qnil;
5126 hashtest_eq.user_cmp_function = Qnil;
5127 hashtest_eq.cmpfn = 0;
5128 hashtest_eq.hashfn = hashfn_eq;
5130 hashtest_eql.name = Qeql;
5131 hashtest_eql.user_hash_function = Qnil;
5132 hashtest_eql.user_cmp_function = Qnil;
5133 hashtest_eql.cmpfn = cmpfn_eql;
5134 hashtest_eql.hashfn = hashfn_eql;
5136 hashtest_equal.name = Qequal;
5137 hashtest_equal.user_hash_function = Qnil;
5138 hashtest_equal.user_cmp_function = Qnil;
5139 hashtest_equal.cmpfn = cmpfn_equal;
5140 hashtest_equal.hashfn = hashfn_equal;