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