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