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