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