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