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