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