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