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