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