1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 /* Note on some machines this defines `vector' as a typedef,
30 so make sure we don't use that name in this file. */
40 #include "intervals.h"
43 #if defined (HAVE_MENUS) && defined (HAVE_X_WINDOWS)
48 #define NULL (void *)0
52 #define min(a, b) ((a) < (b) ? (a) : (b))
53 #define max(a, b) ((a) > (b) ? (a) : (b))
56 /* Nonzero enables use of dialog boxes for questions
57 asked by mouse commands. */
60 extern int minibuffer_auto_raise
;
61 extern Lisp_Object minibuf_window
;
63 Lisp_Object Qstring_lessp
, Qprovide
, Qrequire
;
64 Lisp_Object Qyes_or_no_p_history
;
65 Lisp_Object Qcursor_in_echo_area
;
66 Lisp_Object Qwidget_type
;
68 extern Lisp_Object Qinput_method_function
;
70 static int internal_equal ();
72 extern long get_random ();
73 extern void seed_random ();
79 DEFUN ("identity", Fidentity
, Sidentity
, 1, 1, 0,
80 "Return the argument unchanged.")
87 DEFUN ("random", Frandom
, Srandom
, 0, 1, 0,
88 "Return a pseudo-random number.\n\
89 All integers representable in Lisp are equally likely.\n\
90 On most systems, this is 28 bits' worth.\n\
91 With positive integer argument N, return random number in interval [0,N).\n\
92 With argument t, set the random number seed from the current time and pid.")
97 Lisp_Object lispy_val
;
98 unsigned long denominator
;
101 seed_random (getpid () + time (NULL
));
102 if (NATNUMP (n
) && XFASTINT (n
) != 0)
104 /* Try to take our random number from the higher bits of VAL,
105 not the lower, since (says Gentzel) the low bits of `random'
106 are less random than the higher ones. We do this by using the
107 quotient rather than the remainder. At the high end of the RNG
108 it's possible to get a quotient larger than n; discarding
109 these values eliminates the bias that would otherwise appear
110 when using a large n. */
111 denominator
= ((unsigned long)1 << VALBITS
) / XFASTINT (n
);
113 val
= get_random () / denominator
;
114 while (val
>= XFASTINT (n
));
118 XSETINT (lispy_val
, val
);
122 /* Random data-structure functions */
124 DEFUN ("length", Flength
, Slength
, 1, 1, 0,
125 "Return the length of vector, list or string SEQUENCE.\n\
126 A byte-code function object is also allowed.\n\
127 If the string contains multibyte characters, this is not the necessarily\n\
128 the number of bytes in the string; it is the number of characters.\n\
129 To get the number of bytes, use `string-bytes'")
131 register Lisp_Object sequence
;
133 register Lisp_Object tail
, val
;
137 if (STRINGP (sequence
))
138 XSETFASTINT (val
, XSTRING (sequence
)->size
);
139 else if (VECTORP (sequence
))
140 XSETFASTINT (val
, XVECTOR (sequence
)->size
);
141 else if (CHAR_TABLE_P (sequence
))
142 XSETFASTINT (val
, MAX_CHAR
);
143 else if (BOOL_VECTOR_P (sequence
))
144 XSETFASTINT (val
, XBOOL_VECTOR (sequence
)->size
);
145 else if (COMPILEDP (sequence
))
146 XSETFASTINT (val
, XVECTOR (sequence
)->size
& PSEUDOVECTOR_SIZE_MASK
);
147 else if (CONSP (sequence
))
150 while (CONSP (sequence
))
152 sequence
= XCDR (sequence
);
155 if (!CONSP (sequence
))
158 sequence
= XCDR (sequence
);
163 if (!NILP (sequence
))
164 wrong_type_argument (Qlistp
, sequence
);
166 val
= make_number (i
);
168 else if (NILP (sequence
))
169 XSETFASTINT (val
, 0);
172 sequence
= wrong_type_argument (Qsequencep
, sequence
);
178 /* This does not check for quits. That is safe
179 since it must terminate. */
181 DEFUN ("safe-length", Fsafe_length
, Ssafe_length
, 1, 1, 0,
182 "Return the length of a list, but avoid error or infinite loop.\n\
183 This function never gets an error. If LIST is not really a list,\n\
184 it returns 0. If LIST is circular, it returns a finite value\n\
185 which is at least the number of distinct elements.")
189 Lisp_Object tail
, halftail
, length
;
192 /* halftail is used to detect circular lists. */
194 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
196 if (EQ (tail
, halftail
) && len
!= 0)
200 halftail
= XCDR (halftail
);
203 XSETINT (length
, len
);
207 DEFUN ("string-bytes", Fstring_bytes
, Sstring_bytes
, 1, 1, 0,
208 "Return the number of bytes in STRING.\n\
209 If STRING is a multibyte string, this is greater than the length of STRING.")
213 CHECK_STRING (string
, 1);
214 return make_number (STRING_BYTES (XSTRING (string
)));
217 DEFUN ("string-equal", Fstring_equal
, Sstring_equal
, 2, 2, 0,
218 "Return t if two strings have identical contents.\n\
219 Case is significant, but text properties are ignored.\n\
220 Symbols are also allowed; their print names are used instead.")
222 register Lisp_Object s1
, s2
;
225 XSETSTRING (s1
, XSYMBOL (s1
)->name
);
227 XSETSTRING (s2
, XSYMBOL (s2
)->name
);
228 CHECK_STRING (s1
, 0);
229 CHECK_STRING (s2
, 1);
231 if (XSTRING (s1
)->size
!= XSTRING (s2
)->size
232 || STRING_BYTES (XSTRING (s1
)) != STRING_BYTES (XSTRING (s2
))
233 || bcmp (XSTRING (s1
)->data
, XSTRING (s2
)->data
, STRING_BYTES (XSTRING (s1
))))
238 DEFUN ("compare-strings", Fcompare_strings
,
239 Scompare_strings
, 6, 7, 0,
240 "Compare the contents of two strings, converting to multibyte if needed.\n\
241 In string STR1, skip the first START1 characters and stop at END1.\n\
242 In string STR2, skip the first START2 characters and stop at END2.\n\
243 END1 and END2 default to the full lengths of the respective strings.\n\
245 Case is significant in this comparison if IGNORE-CASE is nil.\n\
246 Unibyte strings are converted to multibyte for comparison.\n\
248 The value is t if the strings (or specified portions) match.\n\
249 If string STR1 is less, the value is a negative number N;\n\
250 - 1 - N is the number of characters that match at the beginning.\n\
251 If string STR1 is greater, the value is a positive number N;\n\
252 N - 1 is the number of characters that match at the beginning.")
253 (str1
, start1
, end1
, str2
, start2
, end2
, ignore_case
)
254 Lisp_Object str1
, start1
, end1
, start2
, str2
, end2
, ignore_case
;
256 register int end1_char
, end2_char
;
257 register int i1
, i1_byte
, i2
, i2_byte
;
259 CHECK_STRING (str1
, 0);
260 CHECK_STRING (str2
, 1);
262 start1
= make_number (0);
264 start2
= make_number (0);
265 CHECK_NATNUM (start1
, 2);
266 CHECK_NATNUM (start2
, 3);
268 CHECK_NATNUM (end1
, 4);
270 CHECK_NATNUM (end2
, 4);
275 i1_byte
= string_char_to_byte (str1
, i1
);
276 i2_byte
= string_char_to_byte (str2
, i2
);
278 end1_char
= XSTRING (str1
)->size
;
279 if (! NILP (end1
) && end1_char
> XINT (end1
))
280 end1_char
= XINT (end1
);
282 end2_char
= XSTRING (str2
)->size
;
283 if (! NILP (end2
) && end2_char
> XINT (end2
))
284 end2_char
= XINT (end2
);
286 while (i1
< end1_char
&& i2
< end2_char
)
288 /* When we find a mismatch, we must compare the
289 characters, not just the bytes. */
292 if (STRING_MULTIBYTE (str1
))
293 FETCH_STRING_CHAR_ADVANCE (c1
, str1
, i1
, i1_byte
);
296 c1
= XSTRING (str1
)->data
[i1
++];
297 c1
= unibyte_char_to_multibyte (c1
);
300 if (STRING_MULTIBYTE (str2
))
301 FETCH_STRING_CHAR_ADVANCE (c2
, str2
, i2
, i2_byte
);
304 c2
= XSTRING (str2
)->data
[i2
++];
305 c2
= unibyte_char_to_multibyte (c2
);
311 if (! NILP (ignore_case
))
315 tem
= Fupcase (make_number (c1
));
317 tem
= Fupcase (make_number (c2
));
324 /* Note that I1 has already been incremented
325 past the character that we are comparing;
326 hence we don't add or subtract 1 here. */
328 return make_number (- i1
);
330 return make_number (i1
);
334 return make_number (i1
- XINT (start1
) + 1);
336 return make_number (- i1
+ XINT (start1
) - 1);
341 DEFUN ("string-lessp", Fstring_lessp
, Sstring_lessp
, 2, 2, 0,
342 "Return t if first arg string is less than second in lexicographic order.\n\
343 Case is significant.\n\
344 Symbols are also allowed; their print names are used instead.")
346 register Lisp_Object s1
, s2
;
349 register int i1
, i1_byte
, i2
, i2_byte
;
352 XSETSTRING (s1
, XSYMBOL (s1
)->name
);
354 XSETSTRING (s2
, XSYMBOL (s2
)->name
);
355 CHECK_STRING (s1
, 0);
356 CHECK_STRING (s2
, 1);
358 i1
= i1_byte
= i2
= i2_byte
= 0;
360 end
= XSTRING (s1
)->size
;
361 if (end
> XSTRING (s2
)->size
)
362 end
= XSTRING (s2
)->size
;
366 /* When we find a mismatch, we must compare the
367 characters, not just the bytes. */
370 if (STRING_MULTIBYTE (s1
))
371 FETCH_STRING_CHAR_ADVANCE (c1
, s1
, i1
, i1_byte
);
373 c1
= XSTRING (s1
)->data
[i1
++];
375 if (STRING_MULTIBYTE (s2
))
376 FETCH_STRING_CHAR_ADVANCE (c2
, s2
, i2
, i2_byte
);
378 c2
= XSTRING (s2
)->data
[i2
++];
381 return c1
< c2
? Qt
: Qnil
;
383 return i1
< XSTRING (s2
)->size
? Qt
: Qnil
;
386 static Lisp_Object
concat ();
397 return concat (2, args
, Lisp_String
, 0);
399 return concat (2, &s1
, Lisp_String
, 0);
400 #endif /* NO_ARG_ARRAY */
406 Lisp_Object s1
, s2
, s3
;
413 return concat (3, args
, Lisp_String
, 0);
415 return concat (3, &s1
, Lisp_String
, 0);
416 #endif /* NO_ARG_ARRAY */
419 DEFUN ("append", Fappend
, Sappend
, 0, MANY
, 0,
420 "Concatenate all the arguments and make the result a list.\n\
421 The result is a list whose elements are the elements of all the arguments.\n\
422 Each argument may be a list, vector or string.\n\
423 The last argument is not copied, just used as the tail of the new list.")
428 return concat (nargs
, args
, Lisp_Cons
, 1);
431 DEFUN ("concat", Fconcat
, Sconcat
, 0, MANY
, 0,
432 "Concatenate all the arguments and make the result a string.\n\
433 The result is a string whose elements are the elements of all the arguments.\n\
434 Each argument may be a string or a list or vector of characters (integers).\n\
436 Do not use individual integers as arguments!\n\
437 The behavior of `concat' in that case will be changed later!\n\
438 If your program passes an integer as an argument to `concat',\n\
439 you should change it right away not to do so.")
444 return concat (nargs
, args
, Lisp_String
, 0);
447 DEFUN ("vconcat", Fvconcat
, Svconcat
, 0, MANY
, 0,
448 "Concatenate all the arguments and make the result a vector.\n\
449 The result is a vector whose elements are the elements of all the arguments.\n\
450 Each argument may be a list, vector or string.")
455 return concat (nargs
, args
, Lisp_Vectorlike
, 0);
458 /* Retrun a copy of a sub char table ARG. The elements except for a
459 nested sub char table are not copied. */
461 copy_sub_char_table (arg
)
464 Lisp_Object copy
= make_sub_char_table (XCHAR_TABLE (arg
)->defalt
);
467 /* Copy all the contents. */
468 bcopy (XCHAR_TABLE (arg
)->contents
, XCHAR_TABLE (copy
)->contents
,
469 SUB_CHAR_TABLE_ORDINARY_SLOTS
* sizeof (Lisp_Object
));
470 /* Recursively copy any sub char-tables in the ordinary slots. */
471 for (i
= 32; i
< SUB_CHAR_TABLE_ORDINARY_SLOTS
; i
++)
472 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg
)->contents
[i
]))
473 XCHAR_TABLE (copy
)->contents
[i
]
474 = copy_sub_char_table (XCHAR_TABLE (copy
)->contents
[i
]);
480 DEFUN ("copy-sequence", Fcopy_sequence
, Scopy_sequence
, 1, 1, 0,
481 "Return a copy of a list, vector or string.\n\
482 The elements of a list or vector are not copied; they are shared\n\
487 if (NILP (arg
)) return arg
;
489 if (CHAR_TABLE_P (arg
))
494 copy
= Fmake_char_table (XCHAR_TABLE (arg
)->purpose
, Qnil
);
495 /* Copy all the slots, including the extra ones. */
496 bcopy (XVECTOR (arg
)->contents
, XVECTOR (copy
)->contents
,
497 ((XCHAR_TABLE (arg
)->size
& PSEUDOVECTOR_SIZE_MASK
)
498 * sizeof (Lisp_Object
)));
500 /* Recursively copy any sub char tables in the ordinary slots
501 for multibyte characters. */
502 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
;
503 i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
504 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg
)->contents
[i
]))
505 XCHAR_TABLE (copy
)->contents
[i
]
506 = copy_sub_char_table (XCHAR_TABLE (copy
)->contents
[i
]);
511 if (BOOL_VECTOR_P (arg
))
515 = (XBOOL_VECTOR (arg
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
517 val
= Fmake_bool_vector (Flength (arg
), Qnil
);
518 bcopy (XBOOL_VECTOR (arg
)->data
, XBOOL_VECTOR (val
)->data
,
523 if (!CONSP (arg
) && !VECTORP (arg
) && !STRINGP (arg
))
524 arg
= wrong_type_argument (Qsequencep
, arg
);
525 return concat (1, &arg
, CONSP (arg
) ? Lisp_Cons
: XTYPE (arg
), 0);
528 /* In string STR of length LEN, see if bytes before STR[I] combine
529 with bytes after STR[I] to form a single character. If so, return
530 the number of bytes after STR[I] which combine in this way.
531 Otherwize, return 0. */
534 count_combining (str
, len
, i
)
538 int j
= i
- 1, bytes
;
540 if (i
== 0 || i
== len
|| CHAR_HEAD_P (str
[i
]))
542 while (j
>= 0 && !CHAR_HEAD_P (str
[j
])) j
--;
543 if (j
< 0 || ! BASE_LEADING_CODE_P (str
[j
]))
545 PARSE_MULTIBYTE_SEQ (str
+ j
, len
- j
, bytes
);
546 return (bytes
<= i
- j
? 0 : bytes
- (i
- j
));
549 /* This structure holds information of an argument of `concat' that is
550 a string and has text properties to be copied. */
553 int argnum
; /* refer to ARGS (arguments of `concat') */
554 int from
; /* refer to ARGS[argnum] (argument string) */
555 int to
; /* refer to VAL (the target string) */
559 concat (nargs
, args
, target_type
, last_special
)
562 enum Lisp_Type target_type
;
566 register Lisp_Object tail
;
567 register Lisp_Object
this;
570 register int result_len
;
571 register int result_len_byte
;
573 Lisp_Object last_tail
;
576 /* When we make a multibyte string, we can't copy text properties
577 while concatinating each string because the length of resulting
578 string can't be decided until we finish the whole concatination.
579 So, we record strings that have text properties to be copied
580 here, and copy the text properties after the concatination. */
581 struct textprop_rec
*textprops
;
582 /* Number of elments in textprops. */
583 int num_textprops
= 0;
585 /* In append, the last arg isn't treated like the others */
586 if (last_special
&& nargs
> 0)
589 last_tail
= args
[nargs
];
594 /* Canonicalize each argument. */
595 for (argnum
= 0; argnum
< nargs
; argnum
++)
598 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
599 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
602 args
[argnum
] = Fnumber_to_string (this);
604 args
[argnum
] = wrong_type_argument (Qsequencep
, this);
608 /* Compute total length in chars of arguments in RESULT_LEN.
609 If desired output is a string, also compute length in bytes
610 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
611 whether the result should be a multibyte string. */
615 for (argnum
= 0; argnum
< nargs
; argnum
++)
619 len
= XFASTINT (Flength (this));
620 if (target_type
== Lisp_String
)
622 /* We must count the number of bytes needed in the string
623 as well as the number of characters. */
629 for (i
= 0; i
< len
; i
++)
631 ch
= XVECTOR (this)->contents
[i
];
633 wrong_type_argument (Qintegerp
, ch
);
634 this_len_byte
= CHAR_BYTES (XINT (ch
));
635 result_len_byte
+= this_len_byte
;
636 if (this_len_byte
> 1)
639 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size
> 0)
640 wrong_type_argument (Qintegerp
, Faref (this, make_number (0)));
641 else if (CONSP (this))
642 for (; CONSP (this); this = XCDR (this))
646 wrong_type_argument (Qintegerp
, ch
);
647 this_len_byte
= CHAR_BYTES (XINT (ch
));
648 result_len_byte
+= this_len_byte
;
649 if (this_len_byte
> 1)
652 else if (STRINGP (this))
654 if (STRING_MULTIBYTE (this))
657 result_len_byte
+= STRING_BYTES (XSTRING (this));
660 result_len_byte
+= count_size_as_multibyte (XSTRING (this)->data
,
661 XSTRING (this)->size
);
668 if (! some_multibyte
)
669 result_len_byte
= result_len
;
671 /* Create the output object. */
672 if (target_type
== Lisp_Cons
)
673 val
= Fmake_list (make_number (result_len
), Qnil
);
674 else if (target_type
== Lisp_Vectorlike
)
675 val
= Fmake_vector (make_number (result_len
), Qnil
);
676 else if (some_multibyte
)
677 val
= make_uninit_multibyte_string (result_len
, result_len_byte
);
679 val
= make_uninit_string (result_len
);
681 /* In `append', if all but last arg are nil, return last arg. */
682 if (target_type
== Lisp_Cons
&& EQ (val
, Qnil
))
685 /* Copy the contents of the args into the result. */
687 tail
= val
, toindex
= -1; /* -1 in toindex is flag we are making a list */
689 toindex
= 0, toindex_byte
= 0;
694 = (struct textprop_rec
*) alloca (sizeof (struct textprop_rec
) * nargs
);
696 for (argnum
= 0; argnum
< nargs
; argnum
++)
700 register unsigned int thisindex
= 0;
701 register unsigned int thisindex_byte
= 0;
705 thislen
= Flength (this), thisleni
= XINT (thislen
);
707 /* Between strings of the same kind, copy fast. */
708 if (STRINGP (this) && STRINGP (val
)
709 && STRING_MULTIBYTE (this) == some_multibyte
)
711 int thislen_byte
= STRING_BYTES (XSTRING (this));
714 bcopy (XSTRING (this)->data
, XSTRING (val
)->data
+ toindex_byte
,
715 STRING_BYTES (XSTRING (this)));
716 combined
= (some_multibyte
&& toindex_byte
> 0
717 ? count_combining (XSTRING (val
)->data
,
718 toindex_byte
+ thislen_byte
,
721 if (! NULL_INTERVAL_P (XSTRING (this)->intervals
))
723 textprops
[num_textprops
].argnum
= argnum
;
724 /* We ignore text properties on characters being combined. */
725 textprops
[num_textprops
].from
= combined
;
726 textprops
[num_textprops
++].to
= toindex
;
728 toindex_byte
+= thislen_byte
;
729 toindex
+= thisleni
- combined
;
730 XSTRING (val
)->size
-= combined
;
732 /* Copy a single-byte string to a multibyte string. */
733 else if (STRINGP (this) && STRINGP (val
))
735 if (! NULL_INTERVAL_P (XSTRING (this)->intervals
))
737 textprops
[num_textprops
].argnum
= argnum
;
738 textprops
[num_textprops
].from
= 0;
739 textprops
[num_textprops
++].to
= toindex
;
741 toindex_byte
+= copy_text (XSTRING (this)->data
,
742 XSTRING (val
)->data
+ toindex_byte
,
743 XSTRING (this)->size
, 0, 1);
747 /* Copy element by element. */
750 register Lisp_Object elt
;
752 /* Fetch next element of `this' arg into `elt', or break if
753 `this' is exhausted. */
754 if (NILP (this)) break;
756 elt
= XCAR (this), this = XCDR (this);
757 else if (thisindex
>= thisleni
)
759 else if (STRINGP (this))
762 if (STRING_MULTIBYTE (this))
764 FETCH_STRING_CHAR_ADVANCE (c
, this,
767 XSETFASTINT (elt
, c
);
771 XSETFASTINT (elt
, XSTRING (this)->data
[thisindex
++]);
773 && (XINT (elt
) >= 0240
774 || (XINT (elt
) >= 0200
775 && ! NILP (Vnonascii_translation_table
)))
776 && XINT (elt
) < 0400)
778 c
= unibyte_char_to_multibyte (XINT (elt
));
783 else if (BOOL_VECTOR_P (this))
786 byte
= XBOOL_VECTOR (this)->data
[thisindex
/ BITS_PER_CHAR
];
787 if (byte
& (1 << (thisindex
% BITS_PER_CHAR
)))
794 elt
= XVECTOR (this)->contents
[thisindex
++];
796 /* Store this element into the result. */
803 else if (VECTORP (val
))
804 XVECTOR (val
)->contents
[toindex
++] = elt
;
807 CHECK_NUMBER (elt
, 0);
808 if (SINGLE_BYTE_CHAR_P (XINT (elt
)))
810 XSTRING (val
)->data
[toindex_byte
++] = XINT (elt
);
813 && count_combining (XSTRING (val
)->data
,
814 toindex_byte
, toindex_byte
- 1))
815 XSTRING (val
)->size
--;
820 /* If we have any multibyte characters,
821 we already decided to make a multibyte string. */
824 /* P exists as a variable
825 to avoid a bug on the Masscomp C compiler. */
826 unsigned char *p
= & XSTRING (val
)->data
[toindex_byte
];
828 toindex_byte
+= CHAR_STRING (c
, p
);
835 XCDR (prev
) = last_tail
;
837 if (num_textprops
> 0)
839 for (argnum
= 0; argnum
< num_textprops
; argnum
++)
841 this = args
[textprops
[argnum
].argnum
];
842 copy_text_properties (make_number (textprops
[argnum
].from
),
843 XSTRING (this)->size
, this,
844 make_number (textprops
[argnum
].to
), val
, Qnil
);
850 static Lisp_Object string_char_byte_cache_string
;
851 static int string_char_byte_cache_charpos
;
852 static int string_char_byte_cache_bytepos
;
855 clear_string_char_byte_cache ()
857 string_char_byte_cache_string
= Qnil
;
860 /* Return the character index corresponding to CHAR_INDEX in STRING. */
863 string_char_to_byte (string
, char_index
)
868 int best_below
, best_below_byte
;
869 int best_above
, best_above_byte
;
871 if (! STRING_MULTIBYTE (string
))
874 best_below
= best_below_byte
= 0;
875 best_above
= XSTRING (string
)->size
;
876 best_above_byte
= STRING_BYTES (XSTRING (string
));
878 if (EQ (string
, string_char_byte_cache_string
))
880 if (string_char_byte_cache_charpos
< char_index
)
882 best_below
= string_char_byte_cache_charpos
;
883 best_below_byte
= string_char_byte_cache_bytepos
;
887 best_above
= string_char_byte_cache_charpos
;
888 best_above_byte
= string_char_byte_cache_bytepos
;
892 if (char_index
- best_below
< best_above
- char_index
)
894 while (best_below
< char_index
)
897 FETCH_STRING_CHAR_ADVANCE (c
, string
, best_below
, best_below_byte
);
900 i_byte
= best_below_byte
;
904 while (best_above
> char_index
)
906 unsigned char *pend
= XSTRING (string
)->data
+ best_above_byte
;
907 unsigned char *pbeg
= pend
- best_above_byte
;
908 unsigned char *p
= pend
- 1;
911 while (p
> pbeg
&& !CHAR_HEAD_P (*p
)) p
--;
912 PARSE_MULTIBYTE_SEQ (p
, pend
- p
, bytes
);
913 if (bytes
== pend
- p
)
914 best_above_byte
-= bytes
;
915 else if (bytes
> pend
- p
)
916 best_above_byte
-= (pend
- p
);
922 i_byte
= best_above_byte
;
925 string_char_byte_cache_bytepos
= i_byte
;
926 string_char_byte_cache_charpos
= i
;
927 string_char_byte_cache_string
= string
;
932 /* Return the character index corresponding to BYTE_INDEX in STRING. */
935 string_byte_to_char (string
, byte_index
)
940 int best_below
, best_below_byte
;
941 int best_above
, best_above_byte
;
943 if (! STRING_MULTIBYTE (string
))
946 best_below
= best_below_byte
= 0;
947 best_above
= XSTRING (string
)->size
;
948 best_above_byte
= STRING_BYTES (XSTRING (string
));
950 if (EQ (string
, string_char_byte_cache_string
))
952 if (string_char_byte_cache_bytepos
< byte_index
)
954 best_below
= string_char_byte_cache_charpos
;
955 best_below_byte
= string_char_byte_cache_bytepos
;
959 best_above
= string_char_byte_cache_charpos
;
960 best_above_byte
= string_char_byte_cache_bytepos
;
964 if (byte_index
- best_below_byte
< best_above_byte
- byte_index
)
966 while (best_below_byte
< byte_index
)
969 FETCH_STRING_CHAR_ADVANCE (c
, string
, best_below
, best_below_byte
);
972 i_byte
= best_below_byte
;
976 while (best_above_byte
> byte_index
)
978 unsigned char *pend
= XSTRING (string
)->data
+ best_above_byte
;
979 unsigned char *pbeg
= pend
- best_above_byte
;
980 unsigned char *p
= pend
- 1;
983 while (p
> pbeg
&& !CHAR_HEAD_P (*p
)) p
--;
984 PARSE_MULTIBYTE_SEQ (p
, pend
- p
, bytes
);
985 if (bytes
== pend
- p
)
986 best_above_byte
-= bytes
;
987 else if (bytes
> pend
- p
)
988 best_above_byte
-= (pend
- p
);
994 i_byte
= best_above_byte
;
997 string_char_byte_cache_bytepos
= i_byte
;
998 string_char_byte_cache_charpos
= i
;
999 string_char_byte_cache_string
= string
;
1004 /* Convert STRING to a multibyte string.
1005 Single-byte characters 0240 through 0377 are converted
1006 by adding nonascii_insert_offset to each. */
1009 string_make_multibyte (string
)
1015 if (STRING_MULTIBYTE (string
))
1018 nbytes
= count_size_as_multibyte (XSTRING (string
)->data
,
1019 XSTRING (string
)->size
);
1020 /* If all the chars are ASCII, they won't need any more bytes
1021 once converted. In that case, we can return STRING itself. */
1022 if (nbytes
== STRING_BYTES (XSTRING (string
)))
1025 buf
= (unsigned char *) alloca (nbytes
);
1026 copy_text (XSTRING (string
)->data
, buf
, STRING_BYTES (XSTRING (string
)),
1029 return make_multibyte_string (buf
, XSTRING (string
)->size
, nbytes
);
1032 /* Convert STRING to a single-byte string. */
1035 string_make_unibyte (string
)
1040 if (! STRING_MULTIBYTE (string
))
1043 buf
= (unsigned char *) alloca (XSTRING (string
)->size
);
1045 copy_text (XSTRING (string
)->data
, buf
, STRING_BYTES (XSTRING (string
)),
1048 return make_unibyte_string (buf
, XSTRING (string
)->size
);
1051 DEFUN ("string-make-multibyte", Fstring_make_multibyte
, Sstring_make_multibyte
,
1053 "Return the multibyte equivalent of STRING.\n\
1054 The function `unibyte-char-to-multibyte' is used to convert\n\
1055 each unibyte character to a multibyte character.")
1059 CHECK_STRING (string
, 0);
1061 return string_make_multibyte (string
);
1064 DEFUN ("string-make-unibyte", Fstring_make_unibyte
, Sstring_make_unibyte
,
1066 "Return the unibyte equivalent of STRING.\n\
1067 Multibyte character codes are converted to unibyte\n\
1068 by using just the low 8 bits.")
1072 CHECK_STRING (string
, 0);
1074 return string_make_unibyte (string
);
1077 DEFUN ("string-as-unibyte", Fstring_as_unibyte
, Sstring_as_unibyte
,
1079 "Return a unibyte string with the same individual bytes as STRING.\n\
1080 If STRING is unibyte, the result is STRING itself.\n\
1081 Otherwise it is a newly created string, with no text properties.")
1085 CHECK_STRING (string
, 0);
1087 if (STRING_MULTIBYTE (string
))
1089 string
= Fcopy_sequence (string
);
1090 XSTRING (string
)->size
= STRING_BYTES (XSTRING (string
));
1091 XSTRING (string
)->intervals
= NULL_INTERVAL
;
1092 SET_STRING_BYTES (XSTRING (string
), -1);
1097 DEFUN ("string-as-multibyte", Fstring_as_multibyte
, Sstring_as_multibyte
,
1099 "Return a multibyte string with the same individual bytes as STRING.\n\
1100 If STRING is multibyte, the result is STRING itself.\n\
1101 Otherwise it is a newly created string, with no text properties.")
1105 CHECK_STRING (string
, 0);
1107 if (! STRING_MULTIBYTE (string
))
1109 int nbytes
= STRING_BYTES (XSTRING (string
));
1110 int newlen
= multibyte_chars_in_text (XSTRING (string
)->data
, nbytes
);
1112 string
= Fcopy_sequence (string
);
1113 XSTRING (string
)->size
= newlen
;
1114 XSTRING (string
)->size_byte
= nbytes
;
1115 XSTRING (string
)->intervals
= NULL_INTERVAL
;
1120 DEFUN ("copy-alist", Fcopy_alist
, Scopy_alist
, 1, 1, 0,
1121 "Return a copy of ALIST.\n\
1122 This is an alist which represents the same mapping from objects to objects,\n\
1123 but does not share the alist structure with ALIST.\n\
1124 The objects mapped (cars and cdrs of elements of the alist)\n\
1125 are shared, however.\n\
1126 Elements of ALIST that are not conses are also shared.")
1130 register Lisp_Object tem
;
1132 CHECK_LIST (alist
, 0);
1135 alist
= concat (1, &alist
, Lisp_Cons
, 0);
1136 for (tem
= alist
; CONSP (tem
); tem
= XCDR (tem
))
1138 register Lisp_Object car
;
1142 XCAR (tem
) = Fcons (XCAR (car
), XCDR (car
));
1147 DEFUN ("substring", Fsubstring
, Ssubstring
, 2, 3, 0,
1148 "Return a substring of STRING, starting at index FROM and ending before TO.\n\
1149 TO may be nil or omitted; then the substring runs to the end of STRING.\n\
1150 If FROM or TO is negative, it counts from the end.\n\
1152 This function allows vectors as well as strings.")
1155 register Lisp_Object from
, to
;
1160 int from_char
, to_char
;
1161 int from_byte
, to_byte
;
1163 if (! (STRINGP (string
) || VECTORP (string
)))
1164 wrong_type_argument (Qarrayp
, string
);
1166 CHECK_NUMBER (from
, 1);
1168 if (STRINGP (string
))
1170 size
= XSTRING (string
)->size
;
1171 size_byte
= STRING_BYTES (XSTRING (string
));
1174 size
= XVECTOR (string
)->size
;
1179 to_byte
= size_byte
;
1183 CHECK_NUMBER (to
, 2);
1185 to_char
= XINT (to
);
1189 if (STRINGP (string
))
1190 to_byte
= string_char_to_byte (string
, to_char
);
1193 from_char
= XINT (from
);
1196 if (STRINGP (string
))
1197 from_byte
= string_char_to_byte (string
, from_char
);
1199 if (!(0 <= from_char
&& from_char
<= to_char
&& to_char
<= size
))
1200 args_out_of_range_3 (string
, make_number (from_char
),
1201 make_number (to_char
));
1203 if (STRINGP (string
))
1205 res
= make_specified_string (XSTRING (string
)->data
+ from_byte
,
1206 to_char
- from_char
, to_byte
- from_byte
,
1207 STRING_MULTIBYTE (string
));
1208 copy_text_properties (make_number (from_char
), make_number (to_char
),
1209 string
, make_number (0), res
, Qnil
);
1212 res
= Fvector (to_char
- from_char
,
1213 XVECTOR (string
)->contents
+ from_char
);
1218 /* Extract a substring of STRING, giving start and end positions
1219 both in characters and in bytes. */
1222 substring_both (string
, from
, from_byte
, to
, to_byte
)
1224 int from
, from_byte
, to
, to_byte
;
1230 if (! (STRINGP (string
) || VECTORP (string
)))
1231 wrong_type_argument (Qarrayp
, string
);
1233 if (STRINGP (string
))
1235 size
= XSTRING (string
)->size
;
1236 size_byte
= STRING_BYTES (XSTRING (string
));
1239 size
= XVECTOR (string
)->size
;
1241 if (!(0 <= from
&& from
<= to
&& to
<= size
))
1242 args_out_of_range_3 (string
, make_number (from
), make_number (to
));
1244 if (STRINGP (string
))
1246 res
= make_specified_string (XSTRING (string
)->data
+ from_byte
,
1247 to
- from
, to_byte
- from_byte
,
1248 STRING_MULTIBYTE (string
));
1249 copy_text_properties (make_number (from
), make_number (to
),
1250 string
, make_number (0), res
, Qnil
);
1253 res
= Fvector (to
- from
,
1254 XVECTOR (string
)->contents
+ from
);
1259 DEFUN ("nthcdr", Fnthcdr
, Snthcdr
, 2, 2, 0,
1260 "Take cdr N times on LIST, returns the result.")
1263 register Lisp_Object list
;
1265 register int i
, num
;
1266 CHECK_NUMBER (n
, 0);
1268 for (i
= 0; i
< num
&& !NILP (list
); i
++)
1272 wrong_type_argument (Qlistp
, list
);
1278 DEFUN ("nth", Fnth
, Snth
, 2, 2, 0,
1279 "Return the Nth element of LIST.\n\
1280 N counts from zero. If LIST is not that long, nil is returned.")
1282 Lisp_Object n
, list
;
1284 return Fcar (Fnthcdr (n
, list
));
1287 DEFUN ("elt", Felt
, Selt
, 2, 2, 0,
1288 "Return element of SEQUENCE at index N.")
1290 register Lisp_Object sequence
, n
;
1292 CHECK_NUMBER (n
, 0);
1295 if (CONSP (sequence
) || NILP (sequence
))
1296 return Fcar (Fnthcdr (n
, sequence
));
1297 else if (STRINGP (sequence
) || VECTORP (sequence
)
1298 || BOOL_VECTOR_P (sequence
) || CHAR_TABLE_P (sequence
))
1299 return Faref (sequence
, n
);
1301 sequence
= wrong_type_argument (Qsequencep
, sequence
);
1305 DEFUN ("member", Fmember
, Smember
, 2, 2, 0,
1306 "Return non-nil if ELT is an element of LIST. Comparison done with `equal'.\n\
1307 The value is actually the tail of LIST whose car is ELT.")
1309 register Lisp_Object elt
;
1312 register Lisp_Object tail
;
1313 for (tail
= list
; !NILP (tail
); tail
= XCDR (tail
))
1315 register Lisp_Object tem
;
1317 wrong_type_argument (Qlistp
, list
);
1319 if (! NILP (Fequal (elt
, tem
)))
1326 DEFUN ("memq", Fmemq
, Smemq
, 2, 2, 0,
1327 "Return non-nil if ELT is an element of LIST.\n\
1328 Comparison done with EQ. The value is actually the tail of LIST\n\
1331 Lisp_Object elt
, list
;
1335 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1339 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1343 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1350 if (!CONSP (list
) && !NILP (list
))
1351 list
= wrong_type_argument (Qlistp
, list
);
1356 DEFUN ("assq", Fassq
, Sassq
, 2, 2, 0,
1357 "Return non-nil if KEY is `eq' to the car of an element of LIST.\n\
1358 The value is actually the element of LIST whose car is KEY.\n\
1359 Elements of LIST that are not conses are ignored.")
1361 Lisp_Object key
, list
;
1368 || (CONSP (XCAR (list
))
1369 && EQ (XCAR (XCAR (list
)), key
)))
1374 || (CONSP (XCAR (list
))
1375 && EQ (XCAR (XCAR (list
)), key
)))
1380 || (CONSP (XCAR (list
))
1381 && EQ (XCAR (XCAR (list
)), key
)))
1389 result
= XCAR (list
);
1390 else if (NILP (list
))
1393 result
= wrong_type_argument (Qlistp
, list
);
1398 /* Like Fassq but never report an error and do not allow quits.
1399 Use only on lists known never to be circular. */
1402 assq_no_quit (key
, list
)
1403 Lisp_Object key
, list
;
1406 && (!CONSP (XCAR (list
))
1407 || !EQ (XCAR (XCAR (list
)), key
)))
1410 return CONSP (list
) ? XCAR (list
) : Qnil
;
1413 DEFUN ("assoc", Fassoc
, Sassoc
, 2, 2, 0,
1414 "Return non-nil if KEY is `equal' to the car of an element of LIST.\n\
1415 The value is actually the element of LIST whose car equals KEY.")
1417 Lisp_Object key
, list
;
1419 Lisp_Object result
, car
;
1424 || (CONSP (XCAR (list
))
1425 && (car
= XCAR (XCAR (list
)),
1426 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1431 || (CONSP (XCAR (list
))
1432 && (car
= XCAR (XCAR (list
)),
1433 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1438 || (CONSP (XCAR (list
))
1439 && (car
= XCAR (XCAR (list
)),
1440 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1448 result
= XCAR (list
);
1449 else if (NILP (list
))
1452 result
= wrong_type_argument (Qlistp
, list
);
1457 DEFUN ("rassq", Frassq
, Srassq
, 2, 2, 0,
1458 "Return non-nil if KEY is `eq' to the cdr of an element of LIST.\n\
1459 The value is actually the element of LIST whose cdr is KEY.")
1461 register Lisp_Object key
;
1469 || (CONSP (XCAR (list
))
1470 && EQ (XCDR (XCAR (list
)), key
)))
1475 || (CONSP (XCAR (list
))
1476 && EQ (XCDR (XCAR (list
)), key
)))
1481 || (CONSP (XCAR (list
))
1482 && EQ (XCDR (XCAR (list
)), key
)))
1491 else if (CONSP (list
))
1492 result
= XCAR (list
);
1494 result
= wrong_type_argument (Qlistp
, list
);
1499 DEFUN ("rassoc", Frassoc
, Srassoc
, 2, 2, 0,
1500 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.\n\
1501 The value is actually the element of LIST whose cdr equals KEY.")
1503 Lisp_Object key
, list
;
1505 Lisp_Object result
, cdr
;
1510 || (CONSP (XCAR (list
))
1511 && (cdr
= XCDR (XCAR (list
)),
1512 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1517 || (CONSP (XCAR (list
))
1518 && (cdr
= XCDR (XCAR (list
)),
1519 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1524 || (CONSP (XCAR (list
))
1525 && (cdr
= XCDR (XCAR (list
)),
1526 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1534 result
= XCAR (list
);
1535 else if (NILP (list
))
1538 result
= wrong_type_argument (Qlistp
, list
);
1543 DEFUN ("delq", Fdelq
, Sdelq
, 2, 2, 0,
1544 "Delete by side effect any occurrences of ELT as a member of LIST.\n\
1545 The modified LIST is returned. Comparison is done with `eq'.\n\
1546 If the first member of LIST is ELT, there is no way to remove it by side effect;\n\
1547 therefore, write `(setq foo (delq element foo))'\n\
1548 to be sure of changing the value of `foo'.")
1550 register Lisp_Object elt
;
1553 register Lisp_Object tail
, prev
;
1554 register Lisp_Object tem
;
1558 while (!NILP (tail
))
1561 wrong_type_argument (Qlistp
, list
);
1568 Fsetcdr (prev
, XCDR (tail
));
1578 DEFUN ("delete", Fdelete
, Sdelete
, 2, 2, 0,
1579 "Delete by side effect any occurrences of ELT as a member of LIST.\n\
1580 The modified LIST is returned. Comparison is done with `equal'.\n\
1581 If the first member of LIST is ELT, deleting it is not a side effect;\n\
1582 it is simply using a different list.\n\
1583 Therefore, write `(setq foo (delete element foo))'\n\
1584 to be sure of changing the value of `foo'.")
1586 register Lisp_Object elt
;
1589 register Lisp_Object tail
, prev
;
1590 register Lisp_Object tem
;
1594 while (!NILP (tail
))
1597 wrong_type_argument (Qlistp
, list
);
1599 if (! NILP (Fequal (elt
, tem
)))
1604 Fsetcdr (prev
, XCDR (tail
));
1614 DEFUN ("nreverse", Fnreverse
, Snreverse
, 1, 1, 0,
1615 "Reverse LIST by modifying cdr pointers.\n\
1616 Returns the beginning of the reversed list.")
1620 register Lisp_Object prev
, tail
, next
;
1622 if (NILP (list
)) return list
;
1625 while (!NILP (tail
))
1629 wrong_type_argument (Qlistp
, list
);
1631 Fsetcdr (tail
, prev
);
1638 DEFUN ("reverse", Freverse
, Sreverse
, 1, 1, 0,
1639 "Reverse LIST, copying. Returns the beginning of the reversed list.\n\
1640 See also the function `nreverse', which is used more often.")
1646 for (new = Qnil
; CONSP (list
); list
= XCDR (list
))
1647 new = Fcons (XCAR (list
), new);
1649 wrong_type_argument (Qconsp
, list
);
1653 Lisp_Object
merge ();
1655 DEFUN ("sort", Fsort
, Ssort
, 2, 2, 0,
1656 "Sort LIST, stably, comparing elements using PREDICATE.\n\
1657 Returns the sorted list. LIST is modified by side effects.\n\
1658 PREDICATE is called with two elements of LIST, and should return T\n\
1659 if the first element is \"less\" than the second.")
1661 Lisp_Object list
, predicate
;
1663 Lisp_Object front
, back
;
1664 register Lisp_Object len
, tem
;
1665 struct gcpro gcpro1
, gcpro2
;
1666 register int length
;
1669 len
= Flength (list
);
1670 length
= XINT (len
);
1674 XSETINT (len
, (length
/ 2) - 1);
1675 tem
= Fnthcdr (len
, list
);
1677 Fsetcdr (tem
, Qnil
);
1679 GCPRO2 (front
, back
);
1680 front
= Fsort (front
, predicate
);
1681 back
= Fsort (back
, predicate
);
1683 return merge (front
, back
, predicate
);
1687 merge (org_l1
, org_l2
, pred
)
1688 Lisp_Object org_l1
, org_l2
;
1692 register Lisp_Object tail
;
1694 register Lisp_Object l1
, l2
;
1695 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1702 /* It is sufficient to protect org_l1 and org_l2.
1703 When l1 and l2 are updated, we copy the new values
1704 back into the org_ vars. */
1705 GCPRO4 (org_l1
, org_l2
, pred
, value
);
1725 tem
= call2 (pred
, Fcar (l2
), Fcar (l1
));
1741 Fsetcdr (tail
, tem
);
1747 DEFUN ("plist-get", Fplist_get
, Splist_get
, 2, 2, 0,
1748 "Extract a value from a property list.\n\
1749 PLIST is a property list, which is a list of the form\n\
1750 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value\n\
1751 corresponding to the given PROP, or nil if PROP is not\n\
1752 one of the properties on the list.")
1755 register Lisp_Object prop
;
1757 register Lisp_Object tail
;
1758 for (tail
= plist
; !NILP (tail
); tail
= Fcdr (XCDR (tail
)))
1760 register Lisp_Object tem
;
1763 return Fcar (XCDR (tail
));
1768 DEFUN ("get", Fget
, Sget
, 2, 2, 0,
1769 "Return the value of SYMBOL's PROPNAME property.\n\
1770 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'.")
1772 Lisp_Object symbol
, propname
;
1774 CHECK_SYMBOL (symbol
, 0);
1775 return Fplist_get (XSYMBOL (symbol
)->plist
, propname
);
1778 DEFUN ("plist-put", Fplist_put
, Splist_put
, 3, 3, 0,
1779 "Change value in PLIST of PROP to VAL.\n\
1780 PLIST is a property list, which is a list of the form\n\
1781 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.\n\
1782 If PROP is already a property on the list, its value is set to VAL,\n\
1783 otherwise the new PROP VAL pair is added. The new plist is returned;\n\
1784 use `(setq x (plist-put x prop val))' to be sure to use the new value.\n\
1785 The PLIST is modified by side effects.")
1788 register Lisp_Object prop
;
1791 register Lisp_Object tail
, prev
;
1792 Lisp_Object newcell
;
1794 for (tail
= plist
; CONSP (tail
) && CONSP (XCDR (tail
));
1795 tail
= XCDR (XCDR (tail
)))
1797 if (EQ (prop
, XCAR (tail
)))
1799 Fsetcar (XCDR (tail
), val
);
1804 newcell
= Fcons (prop
, Fcons (val
, Qnil
));
1808 Fsetcdr (XCDR (prev
), newcell
);
1812 DEFUN ("put", Fput
, Sput
, 3, 3, 0,
1813 "Store SYMBOL's PROPNAME property with value VALUE.\n\
1814 It can be retrieved with `(get SYMBOL PROPNAME)'.")
1815 (symbol
, propname
, value
)
1816 Lisp_Object symbol
, propname
, value
;
1818 CHECK_SYMBOL (symbol
, 0);
1819 XSYMBOL (symbol
)->plist
1820 = Fplist_put (XSYMBOL (symbol
)->plist
, propname
, value
);
1824 DEFUN ("equal", Fequal
, Sequal
, 2, 2, 0,
1825 "Return t if two Lisp objects have similar structure and contents.\n\
1826 They must have the same data type.\n\
1827 Conses are compared by comparing the cars and the cdrs.\n\
1828 Vectors and strings are compared element by element.\n\
1829 Numbers are compared by value, but integers cannot equal floats.\n\
1830 (Use `=' if you want integers and floats to be able to be equal.)\n\
1831 Symbols must match exactly.")
1833 register Lisp_Object o1
, o2
;
1835 return internal_equal (o1
, o2
, 0) ? Qt
: Qnil
;
1839 internal_equal (o1
, o2
, depth
)
1840 register Lisp_Object o1
, o2
;
1844 error ("Stack overflow in equal");
1850 if (XTYPE (o1
) != XTYPE (o2
))
1856 return (extract_float (o1
) == extract_float (o2
));
1859 if (!internal_equal (XCAR (o1
), XCAR (o2
), depth
+ 1))
1866 if (XMISCTYPE (o1
) != XMISCTYPE (o2
))
1870 if (!internal_equal (OVERLAY_START (o1
), OVERLAY_START (o2
),
1872 || !internal_equal (OVERLAY_END (o1
), OVERLAY_END (o2
),
1875 o1
= XOVERLAY (o1
)->plist
;
1876 o2
= XOVERLAY (o2
)->plist
;
1881 return (XMARKER (o1
)->buffer
== XMARKER (o2
)->buffer
1882 && (XMARKER (o1
)->buffer
== 0
1883 || XMARKER (o1
)->bytepos
== XMARKER (o2
)->bytepos
));
1887 case Lisp_Vectorlike
:
1889 register int i
, size
;
1890 size
= XVECTOR (o1
)->size
;
1891 /* Pseudovectors have the type encoded in the size field, so this test
1892 actually checks that the objects have the same type as well as the
1894 if (XVECTOR (o2
)->size
!= size
)
1896 /* Boolvectors are compared much like strings. */
1897 if (BOOL_VECTOR_P (o1
))
1900 = (XBOOL_VECTOR (o1
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
1902 if (XBOOL_VECTOR (o1
)->size
!= XBOOL_VECTOR (o2
)->size
)
1904 if (bcmp (XBOOL_VECTOR (o1
)->data
, XBOOL_VECTOR (o2
)->data
,
1909 if (WINDOW_CONFIGURATIONP (o1
))
1910 return compare_window_configurations (o1
, o2
, 0);
1912 /* Aside from them, only true vectors, char-tables, and compiled
1913 functions are sensible to compare, so eliminate the others now. */
1914 if (size
& PSEUDOVECTOR_FLAG
)
1916 if (!(size
& (PVEC_COMPILED
| PVEC_CHAR_TABLE
)))
1918 size
&= PSEUDOVECTOR_SIZE_MASK
;
1920 for (i
= 0; i
< size
; i
++)
1923 v1
= XVECTOR (o1
)->contents
[i
];
1924 v2
= XVECTOR (o2
)->contents
[i
];
1925 if (!internal_equal (v1
, v2
, depth
+ 1))
1933 if (XSTRING (o1
)->size
!= XSTRING (o2
)->size
)
1935 if (STRING_BYTES (XSTRING (o1
)) != STRING_BYTES (XSTRING (o2
)))
1937 if (bcmp (XSTRING (o1
)->data
, XSTRING (o2
)->data
,
1938 STRING_BYTES (XSTRING (o1
))))
1945 extern Lisp_Object
Fmake_char_internal ();
1947 DEFUN ("fillarray", Ffillarray
, Sfillarray
, 2, 2, 0,
1948 "Store each element of ARRAY with ITEM.\n\
1949 ARRAY is a vector, string, char-table, or bool-vector.")
1951 Lisp_Object array
, item
;
1953 register int size
, index
, charval
;
1955 if (VECTORP (array
))
1957 register Lisp_Object
*p
= XVECTOR (array
)->contents
;
1958 size
= XVECTOR (array
)->size
;
1959 for (index
= 0; index
< size
; index
++)
1962 else if (CHAR_TABLE_P (array
))
1964 register Lisp_Object
*p
= XCHAR_TABLE (array
)->contents
;
1965 size
= CHAR_TABLE_ORDINARY_SLOTS
;
1966 for (index
= 0; index
< size
; index
++)
1968 XCHAR_TABLE (array
)->defalt
= Qnil
;
1970 else if (STRINGP (array
))
1972 register unsigned char *p
= XSTRING (array
)->data
;
1973 CHECK_NUMBER (item
, 1);
1974 charval
= XINT (item
);
1975 size
= XSTRING (array
)->size
;
1976 if (STRING_MULTIBYTE (array
))
1978 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1979 int len
= CHAR_STRING (charval
, str
);
1980 int size_byte
= STRING_BYTES (XSTRING (array
));
1981 unsigned char *p1
= p
, *endp
= p
+ size_byte
;
1984 if (size
!= size_byte
)
1987 int this_len
= MULTIBYTE_FORM_LENGTH (p1
, endp
- p1
);
1988 if (len
!= this_len
)
1989 error ("Attempt to change byte length of a string");
1992 for (i
= 0; i
< size_byte
; i
++)
1993 *p
++ = str
[i
% len
];
1996 for (index
= 0; index
< size
; index
++)
1999 else if (BOOL_VECTOR_P (array
))
2001 register unsigned char *p
= XBOOL_VECTOR (array
)->data
;
2003 = (XBOOL_VECTOR (array
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2005 charval
= (! NILP (item
) ? -1 : 0);
2006 for (index
= 0; index
< size_in_chars
; index
++)
2011 array
= wrong_type_argument (Qarrayp
, array
);
2017 DEFUN ("char-table-subtype", Fchar_table_subtype
, Schar_table_subtype
,
2019 "Return the subtype of char-table CHAR-TABLE. The value is a symbol.")
2021 Lisp_Object char_table
;
2023 CHECK_CHAR_TABLE (char_table
, 0);
2025 return XCHAR_TABLE (char_table
)->purpose
;
2028 DEFUN ("char-table-parent", Fchar_table_parent
, Schar_table_parent
,
2030 "Return the parent char-table of CHAR-TABLE.\n\
2031 The value is either nil or another char-table.\n\
2032 If CHAR-TABLE holds nil for a given character,\n\
2033 then the actual applicable value is inherited from the parent char-table\n\
2034 \(or from its parents, if necessary).")
2036 Lisp_Object char_table
;
2038 CHECK_CHAR_TABLE (char_table
, 0);
2040 return XCHAR_TABLE (char_table
)->parent
;
2043 DEFUN ("set-char-table-parent", Fset_char_table_parent
, Sset_char_table_parent
,
2045 "Set the parent char-table of CHAR-TABLE to PARENT.\n\
2046 PARENT must be either nil or another char-table.")
2047 (char_table
, parent
)
2048 Lisp_Object char_table
, parent
;
2052 CHECK_CHAR_TABLE (char_table
, 0);
2056 CHECK_CHAR_TABLE (parent
, 0);
2058 for (temp
= parent
; !NILP (temp
); temp
= XCHAR_TABLE (temp
)->parent
)
2059 if (EQ (temp
, char_table
))
2060 error ("Attempt to make a chartable be its own parent");
2063 XCHAR_TABLE (char_table
)->parent
= parent
;
2068 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot
, Schar_table_extra_slot
,
2070 "Return the value of CHAR-TABLE's extra-slot number N.")
2072 Lisp_Object char_table
, n
;
2074 CHECK_CHAR_TABLE (char_table
, 1);
2075 CHECK_NUMBER (n
, 2);
2077 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
2078 args_out_of_range (char_table
, n
);
2080 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)];
2083 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot
,
2084 Sset_char_table_extra_slot
,
2086 "Set CHAR-TABLE's extra-slot number N to VALUE.")
2087 (char_table
, n
, value
)
2088 Lisp_Object char_table
, n
, value
;
2090 CHECK_CHAR_TABLE (char_table
, 1);
2091 CHECK_NUMBER (n
, 2);
2093 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
2094 args_out_of_range (char_table
, n
);
2096 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)] = value
;
2099 DEFUN ("char-table-range", Fchar_table_range
, Schar_table_range
,
2101 "Return the value in CHAR-TABLE for a range of characters RANGE.\n\
2102 RANGE should be nil (for the default value)\n\
2103 a vector which identifies a character set or a row of a character set,\n\
2104 a character set name, or a character code.")
2106 Lisp_Object char_table
, range
;
2108 CHECK_CHAR_TABLE (char_table
, 0);
2110 if (EQ (range
, Qnil
))
2111 return XCHAR_TABLE (char_table
)->defalt
;
2112 else if (INTEGERP (range
))
2113 return Faref (char_table
, range
);
2114 else if (SYMBOLP (range
))
2116 Lisp_Object charset_info
;
2118 charset_info
= Fget (range
, Qcharset
);
2119 CHECK_VECTOR (charset_info
, 0);
2121 return Faref (char_table
,
2122 make_number (XINT (XVECTOR (charset_info
)->contents
[0])
2125 else if (VECTORP (range
))
2127 if (XVECTOR (range
)->size
== 1)
2128 return Faref (char_table
,
2129 make_number (XINT (XVECTOR (range
)->contents
[0]) + 128));
2132 int size
= XVECTOR (range
)->size
;
2133 Lisp_Object
*val
= XVECTOR (range
)->contents
;
2134 Lisp_Object ch
= Fmake_char_internal (size
<= 0 ? Qnil
: val
[0],
2135 size
<= 1 ? Qnil
: val
[1],
2136 size
<= 2 ? Qnil
: val
[2]);
2137 return Faref (char_table
, ch
);
2141 error ("Invalid RANGE argument to `char-table-range'");
2144 DEFUN ("set-char-table-range", Fset_char_table_range
, Sset_char_table_range
,
2146 "Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.\n\
2147 RANGE should be t (for all characters), nil (for the default value)\n\
2148 a vector which identifies a character set or a row of a character set,\n\
2149 a coding system, or a character code.")
2150 (char_table
, range
, value
)
2151 Lisp_Object char_table
, range
, value
;
2155 CHECK_CHAR_TABLE (char_table
, 0);
2158 for (i
= 0; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
2159 XCHAR_TABLE (char_table
)->contents
[i
] = value
;
2160 else if (EQ (range
, Qnil
))
2161 XCHAR_TABLE (char_table
)->defalt
= value
;
2162 else if (SYMBOLP (range
))
2164 Lisp_Object charset_info
;
2166 charset_info
= Fget (range
, Qcharset
);
2167 CHECK_VECTOR (charset_info
, 0);
2169 return Faset (char_table
,
2170 make_number (XINT (XVECTOR (charset_info
)->contents
[0])
2174 else if (INTEGERP (range
))
2175 Faset (char_table
, range
, value
);
2176 else if (VECTORP (range
))
2178 if (XVECTOR (range
)->size
== 1)
2179 return Faset (char_table
,
2180 make_number (XINT (XVECTOR (range
)->contents
[0]) + 128),
2184 int size
= XVECTOR (range
)->size
;
2185 Lisp_Object
*val
= XVECTOR (range
)->contents
;
2186 Lisp_Object ch
= Fmake_char_internal (size
<= 0 ? Qnil
: val
[0],
2187 size
<= 1 ? Qnil
: val
[1],
2188 size
<= 2 ? Qnil
: val
[2]);
2189 return Faset (char_table
, ch
, value
);
2193 error ("Invalid RANGE argument to `set-char-table-range'");
2198 DEFUN ("set-char-table-default", Fset_char_table_default
,
2199 Sset_char_table_default
, 3, 3, 0,
2200 "Set the default value in CHAR-TABLE for a generic character CHAR to VALUE.\n\
2201 The generic character specifies the group of characters.\n\
2202 See also the documentation of make-char.")
2203 (char_table
, ch
, value
)
2204 Lisp_Object char_table
, ch
, value
;
2206 int c
, charset
, code1
, code2
;
2209 CHECK_CHAR_TABLE (char_table
, 0);
2210 CHECK_NUMBER (ch
, 1);
2213 SPLIT_CHAR (c
, charset
, code1
, code2
);
2215 /* Since we may want to set the default value for a character set
2216 not yet defined, we check only if the character set is in the
2217 valid range or not, instead of it is already defined or not. */
2218 if (! CHARSET_VALID_P (charset
))
2219 invalid_character (c
);
2221 if (charset
== CHARSET_ASCII
)
2222 return (XCHAR_TABLE (char_table
)->defalt
= value
);
2224 /* Even if C is not a generic char, we had better behave as if a
2225 generic char is specified. */
2226 if (CHARSET_DIMENSION (charset
) == 1)
2228 temp
= XCHAR_TABLE (char_table
)->contents
[charset
+ 128];
2231 if (SUB_CHAR_TABLE_P (temp
))
2232 XCHAR_TABLE (temp
)->defalt
= value
;
2234 XCHAR_TABLE (char_table
)->contents
[charset
+ 128] = value
;
2238 if (! SUB_CHAR_TABLE_P (char_table
))
2239 char_table
= (XCHAR_TABLE (char_table
)->contents
[charset
+ 128]
2240 = make_sub_char_table (temp
));
2241 temp
= XCHAR_TABLE (char_table
)->contents
[code1
];
2242 if (SUB_CHAR_TABLE_P (temp
))
2243 XCHAR_TABLE (temp
)->defalt
= value
;
2245 XCHAR_TABLE (char_table
)->contents
[code1
] = value
;
2249 /* Look up the element in TABLE at index CH,
2250 and return it as an integer.
2251 If the element is nil, return CH itself.
2252 (Actually we do that for any non-integer.) */
2255 char_table_translate (table
, ch
)
2260 value
= Faref (table
, make_number (ch
));
2261 if (! INTEGERP (value
))
2263 return XINT (value
);
2266 /* Map C_FUNCTION or FUNCTION over SUBTABLE, calling it for each
2267 character or group of characters that share a value.
2268 DEPTH is the current depth in the originally specified
2269 chartable, and INDICES contains the vector indices
2270 for the levels our callers have descended.
2272 ARG is passed to C_FUNCTION when that is called. */
2275 map_char_table (c_function
, function
, subtable
, arg
, depth
, indices
)
2276 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
2277 Lisp_Object function
, subtable
, arg
, *indices
;
2284 /* At first, handle ASCII and 8-bit European characters. */
2285 for (i
= 0; i
< CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
++)
2287 Lisp_Object elt
= XCHAR_TABLE (subtable
)->contents
[i
];
2289 (*c_function
) (arg
, make_number (i
), elt
);
2291 call2 (function
, make_number (i
), elt
);
2293 #if 0 /* If the char table has entries for higher characters,
2294 we should report them. */
2295 if (NILP (current_buffer
->enable_multibyte_characters
))
2298 to
= CHAR_TABLE_ORDINARY_SLOTS
;
2303 to
= SUB_CHAR_TABLE_ORDINARY_SLOTS
;
2308 Lisp_Object elt
= XCHAR_TABLE (subtable
)->contents
[i
];
2310 XSETFASTINT (indices
[depth
], i
);
2312 if (SUB_CHAR_TABLE_P (elt
))
2315 error ("Too deep char table");
2316 map_char_table (c_function
, function
, elt
, arg
, depth
+ 1, indices
);
2320 int charset
= XFASTINT (indices
[0]) - 128, c1
, c2
, c
;
2322 if (CHARSET_DEFINED_P (charset
))
2324 c1
= depth
>= 1 ? XFASTINT (indices
[1]) : 0;
2325 c2
= depth
>= 2 ? XFASTINT (indices
[2]) : 0;
2326 c
= MAKE_NON_ASCII_CHAR (charset
, c1
, c2
);
2328 (*c_function
) (arg
, make_number (c
), elt
);
2330 call2 (function
, make_number (c
), elt
);
2336 DEFUN ("map-char-table", Fmap_char_table
, Smap_char_table
,
2338 "Call FUNCTION for each (normal and generic) characters in CHAR-TABLE.\n\
2339 FUNCTION is called with two arguments--a key and a value.\n\
2340 The key is always a possible IDX argument to `aref'.")
2341 (function
, char_table
)
2342 Lisp_Object function
, char_table
;
2344 /* The depth of char table is at most 3. */
2345 Lisp_Object indices
[3];
2347 CHECK_CHAR_TABLE (char_table
, 1);
2349 map_char_table (NULL
, function
, char_table
, char_table
, 0, indices
);
2359 Lisp_Object args
[2];
2362 return Fnconc (2, args
);
2364 return Fnconc (2, &s1
);
2365 #endif /* NO_ARG_ARRAY */
2368 DEFUN ("nconc", Fnconc
, Snconc
, 0, MANY
, 0,
2369 "Concatenate any number of lists by altering them.\n\
2370 Only the last argument is not altered, and need not be a list.")
2375 register int argnum
;
2376 register Lisp_Object tail
, tem
, val
;
2380 for (argnum
= 0; argnum
< nargs
; argnum
++)
2383 if (NILP (tem
)) continue;
2388 if (argnum
+ 1 == nargs
) break;
2391 tem
= wrong_type_argument (Qlistp
, tem
);
2400 tem
= args
[argnum
+ 1];
2401 Fsetcdr (tail
, tem
);
2403 args
[argnum
+ 1] = tail
;
2409 /* This is the guts of all mapping functions.
2410 Apply FN to each element of SEQ, one by one,
2411 storing the results into elements of VALS, a C vector of Lisp_Objects.
2412 LENI is the length of VALS, which should also be the length of SEQ. */
2415 mapcar1 (leni
, vals
, fn
, seq
)
2418 Lisp_Object fn
, seq
;
2420 register Lisp_Object tail
;
2423 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2425 /* Don't let vals contain any garbage when GC happens. */
2426 for (i
= 0; i
< leni
; i
++)
2429 GCPRO3 (dummy
, fn
, seq
);
2431 gcpro1
.nvars
= leni
;
2432 /* We need not explicitly protect `tail' because it is used only on lists, and
2433 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */
2437 for (i
= 0; i
< leni
; i
++)
2439 dummy
= XVECTOR (seq
)->contents
[i
];
2440 vals
[i
] = call1 (fn
, dummy
);
2443 else if (BOOL_VECTOR_P (seq
))
2445 for (i
= 0; i
< leni
; i
++)
2448 byte
= XBOOL_VECTOR (seq
)->data
[i
/ BITS_PER_CHAR
];
2449 if (byte
& (1 << (i
% BITS_PER_CHAR
)))
2454 vals
[i
] = call1 (fn
, dummy
);
2457 else if (STRINGP (seq
) && ! STRING_MULTIBYTE (seq
))
2459 /* Single-byte string. */
2460 for (i
= 0; i
< leni
; i
++)
2462 XSETFASTINT (dummy
, XSTRING (seq
)->data
[i
]);
2463 vals
[i
] = call1 (fn
, dummy
);
2466 else if (STRINGP (seq
))
2468 /* Multi-byte string. */
2471 for (i
= 0, i_byte
= 0; i
< leni
;)
2476 FETCH_STRING_CHAR_ADVANCE (c
, seq
, i
, i_byte
);
2477 XSETFASTINT (dummy
, c
);
2478 vals
[i_before
] = call1 (fn
, dummy
);
2481 else /* Must be a list, since Flength did not get an error */
2484 for (i
= 0; i
< leni
; i
++)
2486 vals
[i
] = call1 (fn
, Fcar (tail
));
2494 DEFUN ("mapconcat", Fmapconcat
, Smapconcat
, 3, 3, 0,
2495 "Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.\n\
2496 In between each pair of results, stick in SEPARATOR. Thus, \" \" as\n\
2497 SEPARATOR results in spaces between the values returned by FUNCTION.\n\
2498 SEQUENCE may be a list, a vector, a bool-vector, or a string.")
2499 (function
, sequence
, separator
)
2500 Lisp_Object function
, sequence
, separator
;
2505 register Lisp_Object
*args
;
2507 struct gcpro gcpro1
;
2509 len
= Flength (sequence
);
2511 nargs
= leni
+ leni
- 1;
2512 if (nargs
< 0) return build_string ("");
2514 args
= (Lisp_Object
*) alloca (nargs
* sizeof (Lisp_Object
));
2517 mapcar1 (leni
, args
, function
, sequence
);
2520 for (i
= leni
- 1; i
>= 0; i
--)
2521 args
[i
+ i
] = args
[i
];
2523 for (i
= 1; i
< nargs
; i
+= 2)
2524 args
[i
] = separator
;
2526 return Fconcat (nargs
, args
);
2529 DEFUN ("mapcar", Fmapcar
, Smapcar
, 2, 2, 0,
2530 "Apply FUNCTION to each element of SEQUENCE, and make a list of the results.\n\
2531 The result is a list just as long as SEQUENCE.\n\
2532 SEQUENCE may be a list, a vector, a bool-vector, or a string.")
2533 (function
, sequence
)
2534 Lisp_Object function
, sequence
;
2536 register Lisp_Object len
;
2538 register Lisp_Object
*args
;
2540 len
= Flength (sequence
);
2541 leni
= XFASTINT (len
);
2542 args
= (Lisp_Object
*) alloca (leni
* sizeof (Lisp_Object
));
2544 mapcar1 (leni
, args
, function
, sequence
);
2546 return Flist (leni
, args
);
2549 /* Anything that calls this function must protect from GC! */
2551 DEFUN ("y-or-n-p", Fy_or_n_p
, Sy_or_n_p
, 1, 1, 0,
2552 "Ask user a \"y or n\" question. Return t if answer is \"y\".\n\
2553 Takes one argument, which is the string to display to ask the question.\n\
2554 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.\n\
2555 No confirmation of the answer is requested; a single character is enough.\n\
2556 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses\n\
2557 the bindings in `query-replace-map'; see the documentation of that variable\n\
2558 for more information. In this case, the useful bindings are `act', `skip',\n\
2559 `recenter', and `quit'.\)\n\
2561 Under a windowing system a dialog box will be used if `last-nonmenu-event'\n\
2566 register Lisp_Object obj
, key
, def
, map
;
2567 register int answer
;
2568 Lisp_Object xprompt
;
2569 Lisp_Object args
[2];
2570 struct gcpro gcpro1
, gcpro2
;
2571 int count
= specpdl_ptr
- specpdl
;
2573 specbind (Qcursor_in_echo_area
, Qt
);
2575 map
= Fsymbol_value (intern ("query-replace-map"));
2577 CHECK_STRING (prompt
, 0);
2579 GCPRO2 (prompt
, xprompt
);
2581 #ifdef HAVE_X_WINDOWS
2582 if (display_busy_cursor_p
)
2583 cancel_busy_cursor ();
2590 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2594 Lisp_Object pane
, menu
;
2595 redisplay_preserve_echo_area ();
2596 pane
= Fcons (Fcons (build_string ("Yes"), Qt
),
2597 Fcons (Fcons (build_string ("No"), Qnil
),
2599 menu
= Fcons (prompt
, pane
);
2600 obj
= Fx_popup_dialog (Qt
, menu
);
2601 answer
= !NILP (obj
);
2604 #endif /* HAVE_MENUS */
2605 cursor_in_echo_area
= 1;
2606 choose_minibuf_frame ();
2607 message_with_string ("%s(y or n) ", xprompt
, 0);
2609 if (minibuffer_auto_raise
)
2611 Lisp_Object mini_frame
;
2613 mini_frame
= WINDOW_FRAME (XWINDOW (minibuf_window
));
2615 Fraise_frame (mini_frame
);
2618 obj
= read_filtered_event (1, 0, 0, 0);
2619 cursor_in_echo_area
= 0;
2620 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2623 key
= Fmake_vector (make_number (1), obj
);
2624 def
= Flookup_key (map
, key
, Qt
);
2626 if (EQ (def
, intern ("skip")))
2631 else if (EQ (def
, intern ("act")))
2636 else if (EQ (def
, intern ("recenter")))
2642 else if (EQ (def
, intern ("quit")))
2644 /* We want to exit this command for exit-prefix,
2645 and this is the only way to do it. */
2646 else if (EQ (def
, intern ("exit-prefix")))
2651 /* If we don't clear this, then the next call to read_char will
2652 return quit_char again, and we'll enter an infinite loop. */
2657 if (EQ (xprompt
, prompt
))
2659 args
[0] = build_string ("Please answer y or n. ");
2661 xprompt
= Fconcat (2, args
);
2666 if (! noninteractive
)
2668 cursor_in_echo_area
= -1;
2669 message_with_string (answer
? "%s(y or n) y" : "%s(y or n) n",
2673 unbind_to (count
, Qnil
);
2674 return answer
? Qt
: Qnil
;
2677 /* This is how C code calls `yes-or-no-p' and allows the user
2680 Anything that calls this function must protect from GC! */
2683 do_yes_or_no_p (prompt
)
2686 return call1 (intern ("yes-or-no-p"), prompt
);
2689 /* Anything that calls this function must protect from GC! */
2691 DEFUN ("yes-or-no-p", Fyes_or_no_p
, Syes_or_no_p
, 1, 1, 0,
2692 "Ask user a yes-or-no question. Return t if answer is yes.\n\
2693 Takes one argument, which is the string to display to ask the question.\n\
2694 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.\n\
2695 The user must confirm the answer with RET,\n\
2696 and can edit it until it has been confirmed.\n\
2698 Under a windowing system a dialog box will be used if `last-nonmenu-event'\n\
2703 register Lisp_Object ans
;
2704 Lisp_Object args
[2];
2705 struct gcpro gcpro1
;
2707 CHECK_STRING (prompt
, 0);
2710 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2714 Lisp_Object pane
, menu
, obj
;
2715 redisplay_preserve_echo_area ();
2716 pane
= Fcons (Fcons (build_string ("Yes"), Qt
),
2717 Fcons (Fcons (build_string ("No"), Qnil
),
2720 menu
= Fcons (prompt
, pane
);
2721 obj
= Fx_popup_dialog (Qt
, menu
);
2725 #endif /* HAVE_MENUS */
2728 args
[1] = build_string ("(yes or no) ");
2729 prompt
= Fconcat (2, args
);
2735 ans
= Fdowncase (Fread_from_minibuffer (prompt
, Qnil
, Qnil
, Qnil
,
2736 Qyes_or_no_p_history
, Qnil
,
2738 if (XSTRING (ans
)->size
== 3 && !strcmp (XSTRING (ans
)->data
, "yes"))
2743 if (XSTRING (ans
)->size
== 2 && !strcmp (XSTRING (ans
)->data
, "no"))
2751 message ("Please answer yes or no.");
2752 Fsleep_for (make_number (2), Qnil
);
2756 DEFUN ("load-average", Fload_average
, Sload_average
, 0, 1, 0,
2757 "Return list of 1 minute, 5 minute and 15 minute load averages.\n\
2758 Each of the three load averages is multiplied by 100,\n\
2759 then converted to integer.\n\
2760 When USE-FLOATS is non-nil, floats will be used instead of integers.\n\
2761 These floats are not multiplied by 100.\n\n\
2762 If the 5-minute or 15-minute load averages are not available, return a\n\
2763 shortened list, containing only those averages which are available.")
2765 Lisp_Object use_floats
;
2768 int loads
= getloadavg (load_ave
, 3);
2769 Lisp_Object ret
= Qnil
;
2772 error ("load-average not implemented for this operating system");
2776 Lisp_Object load
= (NILP (use_floats
) ?
2777 make_number ((int) (100.0 * load_ave
[loads
]))
2778 : make_float (load_ave
[loads
]));
2779 ret
= Fcons (load
, ret
);
2785 Lisp_Object Vfeatures
;
2787 DEFUN ("featurep", Ffeaturep
, Sfeaturep
, 1, 1, 0,
2788 "Returns t if FEATURE is present in this Emacs.\n\
2789 Use this to conditionalize execution of lisp code based on the presence or\n\
2790 absence of emacs or environment extensions.\n\
2791 Use `provide' to declare that a feature is available.\n\
2792 This function looks at the value of the variable `features'.")
2794 Lisp_Object feature
;
2796 register Lisp_Object tem
;
2797 CHECK_SYMBOL (feature
, 0);
2798 tem
= Fmemq (feature
, Vfeatures
);
2799 return (NILP (tem
)) ? Qnil
: Qt
;
2802 DEFUN ("provide", Fprovide
, Sprovide
, 1, 1, 0,
2803 "Announce that FEATURE is a feature of the current Emacs.")
2805 Lisp_Object feature
;
2807 register Lisp_Object tem
;
2808 CHECK_SYMBOL (feature
, 0);
2809 if (!NILP (Vautoload_queue
))
2810 Vautoload_queue
= Fcons (Fcons (Vfeatures
, Qnil
), Vautoload_queue
);
2811 tem
= Fmemq (feature
, Vfeatures
);
2813 Vfeatures
= Fcons (feature
, Vfeatures
);
2814 LOADHIST_ATTACH (Fcons (Qprovide
, feature
));
2818 DEFUN ("require", Frequire
, Srequire
, 1, 3, 0,
2819 "If feature FEATURE is not loaded, load it from FILENAME.\n\
2820 If FEATURE is not a member of the list `features', then the feature\n\
2821 is not loaded; so load the file FILENAME.\n\
2822 If FILENAME is omitted, the printname of FEATURE is used as the file name,\n\
2823 but in this case `load' insists on adding the suffix `.el' or `.elc'.\n\
2824 If the optional third argument NOERROR is non-nil,\n\
2825 then return nil if the file is not found.\n\
2826 Normally the return value is FEATURE.")
2827 (feature
, file_name
, noerror
)
2828 Lisp_Object feature
, file_name
, noerror
;
2830 register Lisp_Object tem
;
2831 CHECK_SYMBOL (feature
, 0);
2832 tem
= Fmemq (feature
, Vfeatures
);
2833 LOADHIST_ATTACH (Fcons (Qrequire
, feature
));
2836 int count
= specpdl_ptr
- specpdl
;
2838 /* Value saved here is to be restored into Vautoload_queue */
2839 record_unwind_protect (un_autoload
, Vautoload_queue
);
2840 Vautoload_queue
= Qt
;
2842 tem
= Fload (NILP (file_name
) ? Fsymbol_name (feature
) : file_name
,
2843 noerror
, Qt
, Qnil
, (NILP (file_name
) ? Qt
: Qnil
));
2844 /* If load failed entirely, return nil. */
2846 return unbind_to (count
, Qnil
);
2848 tem
= Fmemq (feature
, Vfeatures
);
2850 error ("Required feature %s was not provided",
2851 XSYMBOL (feature
)->name
->data
);
2853 /* Once loading finishes, don't undo it. */
2854 Vautoload_queue
= Qt
;
2855 feature
= unbind_to (count
, feature
);
2860 /* Primitives for work of the "widget" library.
2861 In an ideal world, this section would not have been necessary.
2862 However, lisp function calls being as slow as they are, it turns
2863 out that some functions in the widget library (wid-edit.el) are the
2864 bottleneck of Widget operation. Here is their translation to C,
2865 for the sole reason of efficiency. */
2867 DEFUN ("widget-plist-member", Fwidget_plist_member
, Swidget_plist_member
, 2, 2, 0,
2868 "Return non-nil if PLIST has the property PROP.\n\
2869 PLIST is a property list, which is a list of the form\n\
2870 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.\n\
2871 Unlike `plist-get', this allows you to distinguish between a missing\n\
2872 property and a property with the value nil.\n\
2873 The value is actually the tail of PLIST whose car is PROP.")
2875 Lisp_Object plist
, prop
;
2877 while (CONSP (plist
) && !EQ (XCAR (plist
), prop
))
2880 plist
= XCDR (plist
);
2881 plist
= CDR (plist
);
2886 DEFUN ("widget-put", Fwidget_put
, Swidget_put
, 3, 3, 0,
2887 "In WIDGET, set PROPERTY to VALUE.\n\
2888 The value can later be retrieved with `widget-get'.")
2889 (widget
, property
, value
)
2890 Lisp_Object widget
, property
, value
;
2892 CHECK_CONS (widget
, 1);
2893 XCDR (widget
) = Fplist_put (XCDR (widget
), property
, value
);
2897 DEFUN ("widget-get", Fwidget_get
, Swidget_get
, 2, 2, 0,
2898 "In WIDGET, get the value of PROPERTY.\n\
2899 The value could either be specified when the widget was created, or\n\
2900 later with `widget-put'.")
2902 Lisp_Object widget
, property
;
2910 CHECK_CONS (widget
, 1);
2911 tmp
= Fwidget_plist_member (XCDR (widget
), property
);
2917 tmp
= XCAR (widget
);
2920 widget
= Fget (tmp
, Qwidget_type
);
2924 DEFUN ("widget-apply", Fwidget_apply
, Swidget_apply
, 2, MANY
, 0,
2925 "Apply the value of WIDGET's PROPERTY to the widget itself.\n\
2926 ARGS are passed as extra arguments to the function.")
2931 /* This function can GC. */
2932 Lisp_Object newargs
[3];
2933 struct gcpro gcpro1
, gcpro2
;
2936 newargs
[0] = Fwidget_get (args
[0], args
[1]);
2937 newargs
[1] = args
[0];
2938 newargs
[2] = Flist (nargs
- 2, args
+ 2);
2939 GCPRO2 (newargs
[0], newargs
[2]);
2940 result
= Fapply (3, newargs
);
2945 /* base64 encode/decode functions.
2946 Based on code from GNU recode. */
2948 #define MIME_LINE_LENGTH 76
2950 #define IS_ASCII(Character) \
2952 #define IS_BASE64(Character) \
2953 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
2954 #define IS_BASE64_IGNORABLE(Character) \
2955 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
2956 || (Character) == '\f' || (Character) == '\r')
2958 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
2959 character or return retval if there are no characters left to
2961 #define READ_QUADRUPLET_BYTE(retval) \
2968 while (IS_BASE64_IGNORABLE (c))
2970 /* Don't use alloca for regions larger than this, lest we overflow
2972 #define MAX_ALLOCA 16*1024
2974 /* Table of characters coding the 64 values. */
2975 static char base64_value_to_char
[64] =
2977 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
2978 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
2979 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
2980 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
2981 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
2982 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
2983 '8', '9', '+', '/' /* 60-63 */
2986 /* Table of base64 values for first 128 characters. */
2987 static short base64_char_to_value
[128] =
2989 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
2990 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
2991 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
2992 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
2993 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
2994 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
2995 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
2996 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
2997 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
2998 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
2999 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3000 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3001 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3004 /* The following diagram shows the logical steps by which three octets
3005 get transformed into four base64 characters.
3007 .--------. .--------. .--------.
3008 |aaaaaabb| |bbbbcccc| |ccdddddd|
3009 `--------' `--------' `--------'
3011 .--------+--------+--------+--------.
3012 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3013 `--------+--------+--------+--------'
3015 .--------+--------+--------+--------.
3016 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3017 `--------+--------+--------+--------'
3019 The octets are divided into 6 bit chunks, which are then encoded into
3020 base64 characters. */
3023 static int base64_encode_1
P_ ((const char *, char *, int, int));
3024 static int base64_decode_1
P_ ((const char *, char *, int));
3026 DEFUN ("base64-encode-region", Fbase64_encode_region
, Sbase64_encode_region
,
3028 "Base64-encode the region between BEG and END.\n\
3029 Return the length of the encoded text.\n\
3030 Optional third argument NO-LINE-BREAK means do not break long lines\n\
3031 into shorter lines.")
3032 (beg
, end
, no_line_break
)
3033 Lisp_Object beg
, end
, no_line_break
;
3036 int allength
, length
;
3037 int ibeg
, iend
, encoded_length
;
3040 validate_region (&beg
, &end
);
3042 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3043 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3044 move_gap_both (XFASTINT (beg
), ibeg
);
3046 /* We need to allocate enough room for encoding the text.
3047 We need 33 1/3% more space, plus a newline every 76
3048 characters, and then we round up. */
3049 length
= iend
- ibeg
;
3050 allength
= length
+ length
/3 + 1;
3051 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3053 if (allength
<= MAX_ALLOCA
)
3054 encoded
= (char *) alloca (allength
);
3056 encoded
= (char *) xmalloc (allength
);
3057 encoded_length
= base64_encode_1 (BYTE_POS_ADDR (ibeg
), encoded
, length
,
3058 NILP (no_line_break
));
3059 if (encoded_length
> allength
)
3062 /* Now we have encoded the region, so we insert the new contents
3063 and delete the old. (Insert first in order to preserve markers.) */
3064 SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3065 insert (encoded
, encoded_length
);
3066 if (allength
> MAX_ALLOCA
)
3068 del_range_byte (ibeg
+ encoded_length
, iend
+ encoded_length
, 1);
3070 /* If point was outside of the region, restore it exactly; else just
3071 move to the beginning of the region. */
3072 if (old_pos
>= XFASTINT (end
))
3073 old_pos
+= encoded_length
- (XFASTINT (end
) - XFASTINT (beg
));
3074 else if (old_pos
> XFASTINT (beg
))
3075 old_pos
= XFASTINT (beg
);
3078 /* We return the length of the encoded text. */
3079 return make_number (encoded_length
);
3082 DEFUN ("base64-encode-string", Fbase64_encode_string
, Sbase64_encode_string
,
3084 "Base64-encode STRING and return the result.\n\
3085 Optional second argument NO-LINE-BREAK means do not break long lines\n\
3086 into shorter lines.")
3087 (string
, no_line_break
)
3088 Lisp_Object string
, no_line_break
;
3090 int allength
, length
, encoded_length
;
3092 Lisp_Object encoded_string
;
3094 CHECK_STRING (string
, 1);
3096 /* We need to allocate enough room for encoding the text.
3097 We need 33 1/3% more space, plus a newline every 76
3098 characters, and then we round up. */
3099 length
= STRING_BYTES (XSTRING (string
));
3100 allength
= length
+ length
/3 + 1;
3101 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3103 /* We need to allocate enough room for decoding the text. */
3104 if (allength
<= MAX_ALLOCA
)
3105 encoded
= (char *) alloca (allength
);
3107 encoded
= (char *) xmalloc (allength
);
3109 encoded_length
= base64_encode_1 (XSTRING (string
)->data
,
3110 encoded
, length
, NILP (no_line_break
));
3111 if (encoded_length
> allength
)
3114 encoded_string
= make_unibyte_string (encoded
, encoded_length
);
3115 if (allength
> MAX_ALLOCA
)
3118 return encoded_string
;
3122 base64_encode_1 (from
, to
, length
, line_break
)
3128 int counter
= 0, i
= 0;
3137 /* Wrap line every 76 characters. */
3141 if (counter
< MIME_LINE_LENGTH
/ 4)
3150 /* Process first byte of a triplet. */
3152 *e
++ = base64_value_to_char
[0x3f & c
>> 2];
3153 value
= (0x03 & c
) << 4;
3155 /* Process second byte of a triplet. */
3159 *e
++ = base64_value_to_char
[value
];
3167 *e
++ = base64_value_to_char
[value
| (0x0f & c
>> 4)];
3168 value
= (0x0f & c
) << 2;
3170 /* Process third byte of a triplet. */
3174 *e
++ = base64_value_to_char
[value
];
3181 *e
++ = base64_value_to_char
[value
| (0x03 & c
>> 6)];
3182 *e
++ = base64_value_to_char
[0x3f & c
];
3189 DEFUN ("base64-decode-region", Fbase64_decode_region
, Sbase64_decode_region
,
3191 "Base64-decode the region between BEG and END.\n\
3192 Return the length of the decoded text.\n\
3193 If the region can't be decoded, return nil and don't modify the buffer.")
3195 Lisp_Object beg
, end
;
3197 int ibeg
, iend
, length
;
3203 validate_region (&beg
, &end
);
3205 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3206 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3208 length
= iend
- ibeg
;
3209 /* We need to allocate enough room for decoding the text. */
3210 if (length
<= MAX_ALLOCA
)
3211 decoded
= (char *) alloca (length
);
3213 decoded
= (char *) xmalloc (length
);
3215 move_gap_both (XFASTINT (beg
), ibeg
);
3216 decoded_length
= base64_decode_1 (BYTE_POS_ADDR (ibeg
), decoded
, length
);
3217 if (decoded_length
> length
)
3220 if (decoded_length
< 0)
3222 /* The decoding wasn't possible. */
3223 if (length
> MAX_ALLOCA
)
3228 /* Now we have decoded the region, so we insert the new contents
3229 and delete the old. (Insert first in order to preserve markers.) */
3230 /* We insert two spaces, then insert the decoded text in between
3231 them, at last, delete those extra two spaces. This is to avoid
3232 byte combining while inserting. */
3233 TEMP_SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3234 insert_1_both (" ", 2, 2, 0, 1, 0);
3235 TEMP_SET_PT_BOTH (XFASTINT (beg
) + 1, ibeg
+ 1);
3236 insert (decoded
, decoded_length
);
3237 inserted_chars
= PT
- (XFASTINT (beg
) + 1);
3238 if (length
> MAX_ALLOCA
)
3240 /* At first delete the original text. This never cause byte
3242 del_range_both (PT
+ 1, PT_BYTE
+ 1, XFASTINT (end
) + inserted_chars
+ 2,
3243 iend
+ decoded_length
+ 2, 1);
3244 /* Next delete the extra spaces. This will cause byte combining
3246 del_range_both (PT
, PT_BYTE
, PT
+ 1, PT_BYTE
+ 1, 0);
3247 del_range_both (XFASTINT (beg
), ibeg
, XFASTINT (beg
) + 1, ibeg
+ 1, 0);
3248 inserted_chars
= PT
- XFASTINT (beg
);
3250 /* If point was outside of the region, restore it exactly; else just
3251 move to the beginning of the region. */
3252 if (old_pos
>= XFASTINT (end
))
3253 old_pos
+= inserted_chars
- (XFASTINT (end
) - XFASTINT (beg
));
3254 else if (old_pos
> XFASTINT (beg
))
3255 old_pos
= XFASTINT (beg
);
3256 SET_PT (old_pos
> ZV
? ZV
: old_pos
);
3258 return make_number (inserted_chars
);
3261 DEFUN ("base64-decode-string", Fbase64_decode_string
, Sbase64_decode_string
,
3263 "Base64-decode STRING and return the result.")
3268 int length
, decoded_length
;
3269 Lisp_Object decoded_string
;
3271 CHECK_STRING (string
, 1);
3273 length
= STRING_BYTES (XSTRING (string
));
3274 /* We need to allocate enough room for decoding the text. */
3275 if (length
<= MAX_ALLOCA
)
3276 decoded
= (char *) alloca (length
);
3278 decoded
= (char *) xmalloc (length
);
3280 decoded_length
= base64_decode_1 (XSTRING (string
)->data
, decoded
, length
);
3281 if (decoded_length
> length
)
3284 if (decoded_length
< 0)
3285 /* The decoding wasn't possible. */
3286 decoded_string
= Qnil
;
3288 decoded_string
= make_string (decoded
, decoded_length
);
3290 if (length
> MAX_ALLOCA
)
3293 return decoded_string
;
3297 base64_decode_1 (from
, to
, length
)
3305 unsigned long value
;
3309 /* Process first byte of a quadruplet. */
3311 READ_QUADRUPLET_BYTE (e
-to
);
3315 value
= base64_char_to_value
[c
] << 18;
3317 /* Process second byte of a quadruplet. */
3319 READ_QUADRUPLET_BYTE (-1);
3323 value
|= base64_char_to_value
[c
] << 12;
3325 *e
++ = (unsigned char) (value
>> 16);
3327 /* Process third byte of a quadruplet. */
3329 READ_QUADRUPLET_BYTE (-1);
3333 READ_QUADRUPLET_BYTE (-1);
3342 value
|= base64_char_to_value
[c
] << 6;
3344 *e
++ = (unsigned char) (0xff & value
>> 8);
3346 /* Process fourth byte of a quadruplet. */
3348 READ_QUADRUPLET_BYTE (-1);
3355 value
|= base64_char_to_value
[c
];
3357 *e
++ = (unsigned char) (0xff & value
);
3363 /***********************************************************************
3365 ***** Hash Tables *****
3367 ***********************************************************************/
3369 /* Implemented by gerd@gnu.org. This hash table implementation was
3370 inspired by CMUCL hash tables. */
3374 1. For small tables, association lists are probably faster than
3375 hash tables because they have lower overhead.
3377 For uses of hash tables where the O(1) behavior of table
3378 operations is not a requirement, it might therefore be a good idea
3379 not to hash. Instead, we could just do a linear search in the
3380 key_and_value vector of the hash table. This could be done
3381 if a `:linear-search t' argument is given to make-hash-table. */
3384 /* Return the contents of vector V at index IDX. */
3386 #define AREF(V, IDX) XVECTOR (V)->contents[IDX]
3388 /* Value is the key part of entry IDX in hash table H. */
3390 #define HASH_KEY(H, IDX) AREF ((H)->key_and_value, 2 * (IDX))
3392 /* Value is the value part of entry IDX in hash table H. */
3394 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
3396 /* Value is the index of the next entry following the one at IDX
3399 #define HASH_NEXT(H, IDX) AREF ((H)->next, (IDX))
3401 /* Value is the hash code computed for entry IDX in hash table H. */
3403 #define HASH_HASH(H, IDX) AREF ((H)->hash, (IDX))
3405 /* Value is the index of the element in hash table H that is the
3406 start of the collision list at index IDX in the index vector of H. */
3408 #define HASH_INDEX(H, IDX) AREF ((H)->index, (IDX))
3410 /* Value is the size of hash table H. */
3412 #define HASH_TABLE_SIZE(H) XVECTOR ((H)->next)->size
3414 /* The list of all weak hash tables. Don't staticpro this one. */
3416 Lisp_Object Vweak_hash_tables
;
3418 /* Various symbols. */
3420 Lisp_Object Qhash_table_p
, Qeq
, Qeql
, Qequal
, Qkey
, Qvalue
;
3421 Lisp_Object QCtest
, QCsize
, QCrehash_size
, QCrehash_threshold
, QCweakness
;
3422 Lisp_Object Qhash_table_test
;
3424 /* Function prototypes. */
3426 static struct Lisp_Hash_Table
*check_hash_table
P_ ((Lisp_Object
));
3427 static int next_almost_prime
P_ ((int));
3428 static int get_key_arg
P_ ((Lisp_Object
, int, Lisp_Object
*, char *));
3429 static Lisp_Object larger_vector
P_ ((Lisp_Object
, int, Lisp_Object
));
3430 static void maybe_resize_hash_table
P_ ((struct Lisp_Hash_Table
*));
3431 static int cmpfn_eql
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
, unsigned,
3432 Lisp_Object
, unsigned));
3433 static int cmpfn_equal
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
, unsigned,
3434 Lisp_Object
, unsigned));
3435 static int cmpfn_user_defined
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
,
3436 unsigned, Lisp_Object
, unsigned));
3437 static unsigned hashfn_eq
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3438 static unsigned hashfn_eql
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3439 static unsigned hashfn_equal
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3440 static unsigned hashfn_user_defined
P_ ((struct Lisp_Hash_Table
*,
3442 static unsigned sxhash_string
P_ ((unsigned char *, int));
3443 static unsigned sxhash_list
P_ ((Lisp_Object
, int));
3444 static unsigned sxhash_vector
P_ ((Lisp_Object
, int));
3445 static unsigned sxhash_bool_vector
P_ ((Lisp_Object
));
3446 static int sweep_weak_table
P_ ((struct Lisp_Hash_Table
*, int));
3450 /***********************************************************************
3452 ***********************************************************************/
3454 /* If OBJ is a Lisp hash table, return a pointer to its struct
3455 Lisp_Hash_Table. Otherwise, signal an error. */
3457 static struct Lisp_Hash_Table
*
3458 check_hash_table (obj
)
3461 CHECK_HASH_TABLE (obj
, 0);
3462 return XHASH_TABLE (obj
);
3466 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3470 next_almost_prime (n
)
3483 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3484 which USED[I] is non-zero. If found at index I in ARGS, set
3485 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3486 -1. This function is used to extract a keyword/argument pair from
3487 a DEFUN parameter list. */
3490 get_key_arg (key
, nargs
, args
, used
)
3498 for (i
= 0; i
< nargs
- 1; ++i
)
3499 if (!used
[i
] && EQ (args
[i
], key
))
3514 /* Return a Lisp vector which has the same contents as VEC but has
3515 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3516 vector that are not copied from VEC are set to INIT. */
3519 larger_vector (vec
, new_size
, init
)
3524 struct Lisp_Vector
*v
;
3527 xassert (VECTORP (vec
));
3528 old_size
= XVECTOR (vec
)->size
;
3529 xassert (new_size
>= old_size
);
3531 v
= allocate_vectorlike (new_size
);
3533 bcopy (XVECTOR (vec
)->contents
, v
->contents
,
3534 old_size
* sizeof *v
->contents
);
3535 for (i
= old_size
; i
< new_size
; ++i
)
3536 v
->contents
[i
] = init
;
3537 XSETVECTOR (vec
, v
);
3542 /***********************************************************************
3544 ***********************************************************************/
3546 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3547 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3548 KEY2 are the same. */
3551 cmpfn_eql (h
, key1
, hash1
, key2
, hash2
)
3552 struct Lisp_Hash_Table
*h
;
3553 Lisp_Object key1
, key2
;
3554 unsigned hash1
, hash2
;
3556 return (FLOATP (key1
)
3558 && XFLOAT_DATA (key1
) == XFLOAT_DATA (key2
));
3562 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3563 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3564 KEY2 are the same. */
3567 cmpfn_equal (h
, key1
, hash1
, key2
, hash2
)
3568 struct Lisp_Hash_Table
*h
;
3569 Lisp_Object key1
, key2
;
3570 unsigned hash1
, hash2
;
3572 return hash1
== hash2
&& !NILP (Fequal (key1
, key2
));
3576 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3577 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3578 if KEY1 and KEY2 are the same. */
3581 cmpfn_user_defined (h
, key1
, hash1
, key2
, hash2
)
3582 struct Lisp_Hash_Table
*h
;
3583 Lisp_Object key1
, key2
;
3584 unsigned hash1
, hash2
;
3588 Lisp_Object args
[3];
3590 args
[0] = h
->user_cmp_function
;
3593 return !NILP (Ffuncall (3, args
));
3600 /* Value is a hash code for KEY for use in hash table H which uses
3601 `eq' to compare keys. The hash code returned is guaranteed to fit
3602 in a Lisp integer. */
3606 struct Lisp_Hash_Table
*h
;
3609 /* Lisp strings can change their address. Don't try to compute a
3610 hash code for a string from its address. */
3612 return sxhash_string (XSTRING (key
)->data
, XSTRING (key
)->size
);
3614 return XUINT (key
) ^ XGCTYPE (key
);
3618 /* Value is a hash code for KEY for use in hash table H which uses
3619 `eql' to compare keys. The hash code returned is guaranteed to fit
3620 in a Lisp integer. */
3624 struct Lisp_Hash_Table
*h
;
3627 /* Lisp strings can change their address. Don't try to compute a
3628 hash code for a string from its address. */
3630 return sxhash_string (XSTRING (key
)->data
, XSTRING (key
)->size
);
3631 else if (FLOATP (key
))
3632 return sxhash (key
, 0);
3634 return XUINT (key
) ^ XGCTYPE (key
);
3638 /* Value is a hash code for KEY for use in hash table H which uses
3639 `equal' to compare keys. The hash code returned is guaranteed to fit
3640 in a Lisp integer. */
3643 hashfn_equal (h
, key
)
3644 struct Lisp_Hash_Table
*h
;
3647 return sxhash (key
, 0);
3651 /* Value is a hash code for KEY for use in hash table H which uses as
3652 user-defined function to compare keys. The hash code returned is
3653 guaranteed to fit in a Lisp integer. */
3656 hashfn_user_defined (h
, key
)
3657 struct Lisp_Hash_Table
*h
;
3660 Lisp_Object args
[2], hash
;
3662 args
[0] = h
->user_hash_function
;
3664 hash
= Ffuncall (2, args
);
3665 if (!INTEGERP (hash
))
3667 list2 (build_string ("Illegal hash code returned from \
3668 user-supplied hash function"),
3670 return XUINT (hash
);
3674 /* Create and initialize a new hash table.
3676 TEST specifies the test the hash table will use to compare keys.
3677 It must be either one of the predefined tests `eq', `eql' or
3678 `equal' or a symbol denoting a user-defined test named TEST with
3679 test and hash functions USER_TEST and USER_HASH.
3681 Give the table initial capacity SIZE, SIZE > 0, an integer.
3683 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3684 new size when it becomes full is computed by adding REHASH_SIZE to
3685 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3686 table's new size is computed by multiplying its old size with
3689 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3690 be resized when the ratio of (number of entries in the table) /
3691 (table size) is >= REHASH_THRESHOLD.
3693 WEAK specifies the weakness of the table. If non-nil, it must be
3694 one of the symbols `key', `value' or t. */
3697 make_hash_table (test
, size
, rehash_size
, rehash_threshold
, weak
,
3698 user_test
, user_hash
)
3699 Lisp_Object test
, size
, rehash_size
, rehash_threshold
, weak
;
3700 Lisp_Object user_test
, user_hash
;
3702 struct Lisp_Hash_Table
*h
;
3703 struct Lisp_Vector
*v
;
3705 int index_size
, i
, len
, sz
;
3707 /* Preconditions. */
3708 xassert (SYMBOLP (test
));
3709 xassert (INTEGERP (size
) && XINT (size
) > 0);
3710 xassert ((INTEGERP (rehash_size
) && XINT (rehash_size
) > 0)
3711 || (FLOATP (rehash_size
) && XFLOATINT (rehash_size
) > 1.0));
3712 xassert (FLOATP (rehash_threshold
)
3713 && XFLOATINT (rehash_threshold
) > 0
3714 && XFLOATINT (rehash_threshold
) <= 1.0);
3716 /* Allocate a vector, and initialize it. */
3717 len
= VECSIZE (struct Lisp_Hash_Table
);
3718 v
= allocate_vectorlike (len
);
3720 for (i
= 0; i
< len
; ++i
)
3721 v
->contents
[i
] = Qnil
;
3723 /* Initialize hash table slots. */
3724 sz
= XFASTINT (size
);
3725 h
= (struct Lisp_Hash_Table
*) v
;
3728 if (EQ (test
, Qeql
))
3730 h
->cmpfn
= cmpfn_eql
;
3731 h
->hashfn
= hashfn_eql
;
3733 else if (EQ (test
, Qeq
))
3736 h
->hashfn
= hashfn_eq
;
3738 else if (EQ (test
, Qequal
))
3740 h
->cmpfn
= cmpfn_equal
;
3741 h
->hashfn
= hashfn_equal
;
3745 h
->user_cmp_function
= user_test
;
3746 h
->user_hash_function
= user_hash
;
3747 h
->cmpfn
= cmpfn_user_defined
;
3748 h
->hashfn
= hashfn_user_defined
;
3752 h
->rehash_threshold
= rehash_threshold
;
3753 h
->rehash_size
= rehash_size
;
3754 h
->count
= make_number (0);
3755 h
->key_and_value
= Fmake_vector (make_number (2 * sz
), Qnil
);
3756 h
->hash
= Fmake_vector (size
, Qnil
);
3757 h
->next
= Fmake_vector (size
, Qnil
);
3758 index_size
= next_almost_prime (sz
/ XFLOATINT (rehash_threshold
));
3759 h
->index
= Fmake_vector (make_number (index_size
), Qnil
);
3761 /* Set up the free list. */
3762 for (i
= 0; i
< sz
- 1; ++i
)
3763 HASH_NEXT (h
, i
) = make_number (i
+ 1);
3764 h
->next_free
= make_number (0);
3766 XSET_HASH_TABLE (table
, h
);
3767 xassert (HASH_TABLE_P (table
));
3768 xassert (XHASH_TABLE (table
) == h
);
3770 /* Maybe add this hash table to the list of all weak hash tables. */
3772 h
->next_weak
= Qnil
;
3775 h
->next_weak
= Vweak_hash_tables
;
3776 Vweak_hash_tables
= table
;
3783 /* Return a copy of hash table H1. Keys and values are not copied,
3784 only the table itself is. */
3787 copy_hash_table (h1
)
3788 struct Lisp_Hash_Table
*h1
;
3791 struct Lisp_Hash_Table
*h2
;
3792 struct Lisp_Vector
*v
, *next
;
3795 len
= VECSIZE (struct Lisp_Hash_Table
);
3796 v
= allocate_vectorlike (len
);
3797 h2
= (struct Lisp_Hash_Table
*) v
;
3798 next
= h2
->vec_next
;
3799 bcopy (h1
, h2
, sizeof *h2
);
3800 h2
->vec_next
= next
;
3801 h2
->key_and_value
= Fcopy_sequence (h1
->key_and_value
);
3802 h2
->hash
= Fcopy_sequence (h1
->hash
);
3803 h2
->next
= Fcopy_sequence (h1
->next
);
3804 h2
->index
= Fcopy_sequence (h1
->index
);
3805 XSET_HASH_TABLE (table
, h2
);
3807 /* Maybe add this hash table to the list of all weak hash tables. */
3808 if (!NILP (h2
->weak
))
3810 h2
->next_weak
= Vweak_hash_tables
;
3811 Vweak_hash_tables
= table
;
3818 /* Resize hash table H if it's too full. If H cannot be resized
3819 because it's already too large, throw an error. */
3822 maybe_resize_hash_table (h
)
3823 struct Lisp_Hash_Table
*h
;
3825 if (NILP (h
->next_free
))
3827 int old_size
= HASH_TABLE_SIZE (h
);
3828 int i
, new_size
, index_size
;
3830 if (INTEGERP (h
->rehash_size
))
3831 new_size
= old_size
+ XFASTINT (h
->rehash_size
);
3833 new_size
= old_size
* XFLOATINT (h
->rehash_size
);
3834 new_size
= max (old_size
+ 1, new_size
);
3835 index_size
= next_almost_prime (new_size
3836 / XFLOATINT (h
->rehash_threshold
));
3837 if (max (index_size
, 2 * new_size
) & ~VALMASK
)
3838 error ("Hash table too large to resize");
3840 h
->key_and_value
= larger_vector (h
->key_and_value
, 2 * new_size
, Qnil
);
3841 h
->next
= larger_vector (h
->next
, new_size
, Qnil
);
3842 h
->hash
= larger_vector (h
->hash
, new_size
, Qnil
);
3843 h
->index
= Fmake_vector (make_number (index_size
), Qnil
);
3845 /* Update the free list. Do it so that new entries are added at
3846 the end of the free list. This makes some operations like
3848 for (i
= old_size
; i
< new_size
- 1; ++i
)
3849 HASH_NEXT (h
, i
) = make_number (i
+ 1);
3851 if (!NILP (h
->next_free
))
3853 Lisp_Object last
, next
;
3855 last
= h
->next_free
;
3856 while (next
= HASH_NEXT (h
, XFASTINT (last
)),
3860 HASH_NEXT (h
, XFASTINT (last
)) = make_number (old_size
);
3863 XSETFASTINT (h
->next_free
, old_size
);
3866 for (i
= 0; i
< old_size
; ++i
)
3867 if (!NILP (HASH_HASH (h
, i
)))
3869 unsigned hash_code
= XUINT (HASH_HASH (h
, i
));
3870 int start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
3871 HASH_NEXT (h
, i
) = HASH_INDEX (h
, start_of_bucket
);
3872 HASH_INDEX (h
, start_of_bucket
) = make_number (i
);
3878 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3879 the hash code of KEY. Value is the index of the entry in H
3880 matching KEY, or -1 if not found. */
3883 hash_lookup (h
, key
, hash
)
3884 struct Lisp_Hash_Table
*h
;
3889 int start_of_bucket
;
3892 hash_code
= h
->hashfn (h
, key
);
3896 start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
3897 idx
= HASH_INDEX (h
, start_of_bucket
);
3901 int i
= XFASTINT (idx
);
3902 if (EQ (key
, HASH_KEY (h
, i
))
3904 && h
->cmpfn (h
, key
, hash_code
,
3905 HASH_KEY (h
, i
), HASH_HASH (h
, i
))))
3907 idx
= HASH_NEXT (h
, i
);
3910 return NILP (idx
) ? -1 : XFASTINT (idx
);
3914 /* Put an entry into hash table H that associates KEY with VALUE.
3915 HASH is a previously computed hash code of KEY.
3916 Value is the index of the entry in H matching KEY. */
3919 hash_put (h
, key
, value
, hash
)
3920 struct Lisp_Hash_Table
*h
;
3921 Lisp_Object key
, value
;
3924 int start_of_bucket
, i
;
3926 xassert ((hash
& ~VALMASK
) == 0);
3928 /* Increment count after resizing because resizing may fail. */
3929 maybe_resize_hash_table (h
);
3930 h
->count
= make_number (XFASTINT (h
->count
) + 1);
3932 /* Store key/value in the key_and_value vector. */
3933 i
= XFASTINT (h
->next_free
);
3934 h
->next_free
= HASH_NEXT (h
, i
);
3935 HASH_KEY (h
, i
) = key
;
3936 HASH_VALUE (h
, i
) = value
;
3938 /* Remember its hash code. */
3939 HASH_HASH (h
, i
) = make_number (hash
);
3941 /* Add new entry to its collision chain. */
3942 start_of_bucket
= hash
% XVECTOR (h
->index
)->size
;
3943 HASH_NEXT (h
, i
) = HASH_INDEX (h
, start_of_bucket
);
3944 HASH_INDEX (h
, start_of_bucket
) = make_number (i
);
3949 /* Remove the entry matching KEY from hash table H, if there is one. */
3952 hash_remove (h
, key
)
3953 struct Lisp_Hash_Table
*h
;
3957 int start_of_bucket
;
3958 Lisp_Object idx
, prev
;
3960 hash_code
= h
->hashfn (h
, key
);
3961 start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
3962 idx
= HASH_INDEX (h
, start_of_bucket
);
3967 int i
= XFASTINT (idx
);
3969 if (EQ (key
, HASH_KEY (h
, i
))
3971 && h
->cmpfn (h
, key
, hash_code
,
3972 HASH_KEY (h
, i
), HASH_HASH (h
, i
))))
3974 /* Take entry out of collision chain. */
3976 HASH_INDEX (h
, start_of_bucket
) = HASH_NEXT (h
, i
);
3978 HASH_NEXT (h
, XFASTINT (prev
)) = HASH_NEXT (h
, i
);
3980 /* Clear slots in key_and_value and add the slots to
3982 HASH_KEY (h
, i
) = HASH_VALUE (h
, i
) = HASH_HASH (h
, i
) = Qnil
;
3983 HASH_NEXT (h
, i
) = h
->next_free
;
3984 h
->next_free
= make_number (i
);
3985 h
->count
= make_number (XFASTINT (h
->count
) - 1);
3986 xassert (XINT (h
->count
) >= 0);
3992 idx
= HASH_NEXT (h
, i
);
3998 /* Clear hash table H. */
4002 struct Lisp_Hash_Table
*h
;
4004 if (XFASTINT (h
->count
) > 0)
4006 int i
, size
= HASH_TABLE_SIZE (h
);
4008 for (i
= 0; i
< size
; ++i
)
4010 HASH_NEXT (h
, i
) = i
< size
- 1 ? make_number (i
+ 1) : Qnil
;
4011 HASH_KEY (h
, i
) = Qnil
;
4012 HASH_VALUE (h
, i
) = Qnil
;
4013 HASH_HASH (h
, i
) = Qnil
;
4016 for (i
= 0; i
< XVECTOR (h
->index
)->size
; ++i
)
4017 XVECTOR (h
->index
)->contents
[i
] = Qnil
;
4019 h
->next_free
= make_number (0);
4020 h
->count
= make_number (0);
4026 /************************************************************************
4028 ************************************************************************/
4030 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4031 entries from the table that don't survive the current GC.
4032 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4033 non-zero if anything was marked. */
4036 sweep_weak_table (h
, remove_entries_p
)
4037 struct Lisp_Hash_Table
*h
;
4038 int remove_entries_p
;
4040 int bucket
, n
, marked
;
4042 n
= XVECTOR (h
->index
)->size
& ~ARRAY_MARK_FLAG
;
4045 for (bucket
= 0; bucket
< n
; ++bucket
)
4047 Lisp_Object idx
, prev
;
4049 /* Follow collision chain, removing entries that
4050 don't survive this garbage collection. */
4051 idx
= HASH_INDEX (h
, bucket
);
4053 while (!GC_NILP (idx
))
4056 int i
= XFASTINT (idx
);
4059 if (EQ (h
->weak
, Qkey
))
4060 remove_p
= !survives_gc_p (HASH_KEY (h
, i
));
4061 else if (EQ (h
->weak
, Qvalue
))
4062 remove_p
= !survives_gc_p (HASH_VALUE (h
, i
));
4063 else if (EQ (h
->weak
, Qt
))
4064 remove_p
= (!survives_gc_p (HASH_KEY (h
, i
))
4065 || !survives_gc_p (HASH_VALUE (h
, i
)));
4069 next
= HASH_NEXT (h
, i
);
4071 if (remove_entries_p
)
4075 /* Take out of collision chain. */
4077 HASH_INDEX (h
, i
) = next
;
4079 HASH_NEXT (h
, XFASTINT (prev
)) = next
;
4081 /* Add to free list. */
4082 HASH_NEXT (h
, i
) = h
->next_free
;
4085 /* Clear key, value, and hash. */
4086 HASH_KEY (h
, i
) = HASH_VALUE (h
, i
) = Qnil
;
4087 HASH_HASH (h
, i
) = Qnil
;
4089 h
->count
= make_number (XFASTINT (h
->count
) - 1);
4096 /* Make sure key and value survive. */
4097 mark_object (&HASH_KEY (h
, i
));
4098 mark_object (&HASH_VALUE (h
, i
));
4110 /* Remove elements from weak hash tables that don't survive the
4111 current garbage collection. Remove weak tables that don't survive
4112 from Vweak_hash_tables. Called from gc_sweep. */
4115 sweep_weak_hash_tables ()
4118 struct Lisp_Hash_Table
*h
, *prev
;
4121 /* Mark all keys and values that are in use. Keep on marking until
4122 there is no more change. This is necessary for cases like
4123 value-weak table A containing an entry X -> Y, where Y is used in a
4124 key-weak table B, Z -> Y. If B comes after A in the list of weak
4125 tables, X -> Y might be removed from A, although when looking at B
4126 one finds that it shouldn't. */
4130 for (table
= Vweak_hash_tables
; !GC_NILP (table
); table
= h
->next_weak
)
4132 h
= XHASH_TABLE (table
);
4133 if (h
->size
& ARRAY_MARK_FLAG
)
4134 marked
|= sweep_weak_table (h
, 0);
4139 /* Remove tables and entries that aren't used. */
4141 for (table
= Vweak_hash_tables
; !GC_NILP (table
); table
= h
->next_weak
)
4144 h
= XHASH_TABLE (table
);
4146 if (h
->size
& ARRAY_MARK_FLAG
)
4148 if (XFASTINT (h
->count
) > 0)
4149 sweep_weak_table (h
, 1);
4153 /* Table is not marked, and will thus be freed.
4154 Take it out of the list of weak hash tables. */
4156 prev
->next_weak
= h
->next_weak
;
4158 Vweak_hash_tables
= h
->next_weak
;
4165 /***********************************************************************
4166 Hash Code Computation
4167 ***********************************************************************/
4169 /* Maximum depth up to which to dive into Lisp structures. */
4171 #define SXHASH_MAX_DEPTH 3
4173 /* Maximum length up to which to take list and vector elements into
4176 #define SXHASH_MAX_LEN 7
4178 /* Combine two integers X and Y for hashing. */
4180 #define SXHASH_COMBINE(X, Y) \
4181 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4185 /* Return a hash for string PTR which has length LEN. */
4188 sxhash_string (ptr
, len
)
4192 unsigned char *p
= ptr
;
4193 unsigned char *end
= p
+ len
;
4202 hash
= ((hash
<< 3) + (hash
>> 28) + c
);
4205 return hash
& 07777777777;
4209 /* Return a hash for list LIST. DEPTH is the current depth in the
4210 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4213 sxhash_list (list
, depth
)
4220 if (depth
< SXHASH_MAX_DEPTH
)
4222 CONSP (list
) && i
< SXHASH_MAX_LEN
;
4223 list
= XCDR (list
), ++i
)
4225 unsigned hash2
= sxhash (XCAR (list
), depth
+ 1);
4226 hash
= SXHASH_COMBINE (hash
, hash2
);
4233 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4234 the Lisp structure. */
4237 sxhash_vector (vec
, depth
)
4241 unsigned hash
= XVECTOR (vec
)->size
;
4244 n
= min (SXHASH_MAX_LEN
, XVECTOR (vec
)->size
);
4245 for (i
= 0; i
< n
; ++i
)
4247 unsigned hash2
= sxhash (XVECTOR (vec
)->contents
[i
], depth
+ 1);
4248 hash
= SXHASH_COMBINE (hash
, hash2
);
4255 /* Return a hash for bool-vector VECTOR. */
4258 sxhash_bool_vector (vec
)
4261 unsigned hash
= XBOOL_VECTOR (vec
)->size
;
4264 n
= min (SXHASH_MAX_LEN
, XBOOL_VECTOR (vec
)->vector_size
);
4265 for (i
= 0; i
< n
; ++i
)
4266 hash
= SXHASH_COMBINE (hash
, XBOOL_VECTOR (vec
)->data
[i
]);
4272 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4273 structure. Value is an unsigned integer clipped to VALMASK. */
4282 if (depth
> SXHASH_MAX_DEPTH
)
4285 switch (XTYPE (obj
))
4292 hash
= sxhash_string (XSYMBOL (obj
)->name
->data
,
4293 XSYMBOL (obj
)->name
->size
);
4301 hash
= sxhash_string (XSTRING (obj
)->data
, XSTRING (obj
)->size
);
4304 /* This can be everything from a vector to an overlay. */
4305 case Lisp_Vectorlike
:
4307 /* According to the CL HyperSpec, two arrays are equal only if
4308 they are `eq', except for strings and bit-vectors. In
4309 Emacs, this works differently. We have to compare element
4311 hash
= sxhash_vector (obj
, depth
);
4312 else if (BOOL_VECTOR_P (obj
))
4313 hash
= sxhash_bool_vector (obj
);
4315 /* Others are `equal' if they are `eq', so let's take their
4321 hash
= sxhash_list (obj
, depth
);
4326 unsigned char *p
= (unsigned char *) &XFLOAT_DATA (obj
);
4327 unsigned char *e
= p
+ sizeof XFLOAT_DATA (obj
);
4328 for (hash
= 0; p
< e
; ++p
)
4329 hash
= SXHASH_COMBINE (hash
, *p
);
4337 return hash
& VALMASK
;
4342 /***********************************************************************
4344 ***********************************************************************/
4347 DEFUN ("sxhash", Fsxhash
, Ssxhash
, 1, 1, 0,
4348 "Compute a hash code for OBJ and return it as integer.")
4352 unsigned hash
= sxhash (obj
, 0);;
4353 return make_number (hash
);
4357 DEFUN ("make-hash-table", Fmake_hash_table
, Smake_hash_table
, 0, MANY
, 0,
4358 "Create and return a new hash table.\n\
4359 Arguments are specified as keyword/argument pairs. The following\n\
4360 arguments are defined:\n\
4362 :TEST TEST -- TEST must be a symbol that specifies how to compare keys.\n\
4363 Default is `eql'. Predefined are the tests `eq', `eql', and `equal'.\n\
4364 User-supplied test and hash functions can be specified via\n\
4365 `define-hash-table-test'.\n\
4367 :SIZE SIZE -- A hint as to how many elements will be put in the table.\n\
4370 :REHASH-SIZE REHASH-SIZE - Indicates how to expand the table when\n\
4371 it fills up. If REHASH-SIZE is an integer, add that many space.\n\
4372 If it is a float, it must be > 1.0, and the new size is computed by\n\
4373 multiplying the old size with that factor. Default is 1.5.\n\
4375 :REHASH-THRESHOLD THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.\n\
4376 Resize the hash table when ratio of the number of entries in the table.\n\
4379 :WEAKNESS WEAK -- WEAK must be one of nil, t, `key', or `value'.\n\
4380 If WEAK is not nil, the table returned is a weak table. Key/value\n\
4381 pairs are removed from a weak hash table when their key, value or both\n\
4382 (WEAK t) are otherwise unreferenced. Default is nil.")
4387 Lisp_Object test
, size
, rehash_size
, rehash_threshold
, weak
;
4388 Lisp_Object user_test
, user_hash
;
4392 /* The vector `used' is used to keep track of arguments that
4393 have been consumed. */
4394 used
= (char *) alloca (nargs
* sizeof *used
);
4395 bzero (used
, nargs
* sizeof *used
);
4397 /* See if there's a `:test TEST' among the arguments. */
4398 i
= get_key_arg (QCtest
, nargs
, args
, used
);
4399 test
= i
< 0 ? Qeql
: args
[i
];
4400 if (!EQ (test
, Qeq
) && !EQ (test
, Qeql
) && !EQ (test
, Qequal
))
4402 /* See if it is a user-defined test. */
4405 prop
= Fget (test
, Qhash_table_test
);
4406 if (!CONSP (prop
) || XFASTINT (Flength (prop
)) < 2)
4407 Fsignal (Qerror
, list2 (build_string ("Illegal hash table test"),
4409 user_test
= Fnth (make_number (0), prop
);
4410 user_hash
= Fnth (make_number (1), prop
);
4413 user_test
= user_hash
= Qnil
;
4415 /* See if there's a `:size SIZE' argument. */
4416 i
= get_key_arg (QCsize
, nargs
, args
, used
);
4417 size
= i
< 0 ? make_number (DEFAULT_HASH_SIZE
) : args
[i
];
4418 if (!INTEGERP (size
) || XINT (size
) <= 0)
4420 list2 (build_string ("Illegal hash table size"),
4423 /* Look for `:rehash-size SIZE'. */
4424 i
= get_key_arg (QCrehash_size
, nargs
, args
, used
);
4425 rehash_size
= i
< 0 ? make_float (DEFAULT_REHASH_SIZE
) : args
[i
];
4426 if (!NUMBERP (rehash_size
)
4427 || (INTEGERP (rehash_size
) && XINT (rehash_size
) <= 0)
4428 || XFLOATINT (rehash_size
) <= 1.0)
4430 list2 (build_string ("Illegal hash table rehash size"),
4433 /* Look for `:rehash-threshold THRESHOLD'. */
4434 i
= get_key_arg (QCrehash_threshold
, nargs
, args
, used
);
4435 rehash_threshold
= i
< 0 ? make_float (DEFAULT_REHASH_THRESHOLD
) : args
[i
];
4436 if (!FLOATP (rehash_threshold
)
4437 || XFLOATINT (rehash_threshold
) <= 0.0
4438 || XFLOATINT (rehash_threshold
) > 1.0)
4440 list2 (build_string ("Illegal hash table rehash threshold"),
4443 /* Look for `:weakness WEAK'. */
4444 i
= get_key_arg (QCweakness
, nargs
, args
, used
);
4445 weak
= i
< 0 ? Qnil
: args
[i
];
4449 && !EQ (weak
, Qvalue
))
4450 Fsignal (Qerror
, list2 (build_string ("Illegal hash table weakness"),
4453 /* Now, all args should have been used up, or there's a problem. */
4454 for (i
= 0; i
< nargs
; ++i
)
4457 list2 (build_string ("Invalid argument list"), args
[i
]));
4459 return make_hash_table (test
, size
, rehash_size
, rehash_threshold
, weak
,
4460 user_test
, user_hash
);
4464 DEFUN ("copy-hash-table", Fcopy_hash_table
, Scopy_hash_table
, 1, 1, 0,
4465 "Return a copy of hash table TABLE.")
4469 return copy_hash_table (check_hash_table (table
));
4473 DEFUN ("makehash", Fmakehash
, Smakehash
, 0, 1, 0,
4474 "Create a new hash table.\n\
4475 Optional first argument TEST specifies how to compare keys in\n\
4476 the table. Predefined tests are `eq', `eql', and `equal'. Default\n\
4477 is `eql'. New tests can be defined with `define-hash-table-test'.")
4481 Lisp_Object args
[2];
4484 return Fmake_hash_table (2, args
);
4488 DEFUN ("hash-table-count", Fhash_table_count
, Shash_table_count
, 1, 1, 0,
4489 "Return the number of elements in TABLE.")
4493 return check_hash_table (table
)->count
;
4497 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size
,
4498 Shash_table_rehash_size
, 1, 1, 0,
4499 "Return the current rehash size of TABLE.")
4503 return check_hash_table (table
)->rehash_size
;
4507 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold
,
4508 Shash_table_rehash_threshold
, 1, 1, 0,
4509 "Return the current rehash threshold of TABLE.")
4513 return check_hash_table (table
)->rehash_threshold
;
4517 DEFUN ("hash-table-size", Fhash_table_size
, Shash_table_size
, 1, 1, 0,
4518 "Return the size of TABLE.\n\
4519 The size can be used as an argument to `make-hash-table' to create\n\
4520 a hash table than can hold as many elements of TABLE holds\n\
4521 without need for resizing.")
4525 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4526 return make_number (HASH_TABLE_SIZE (h
));
4530 DEFUN ("hash-table-test", Fhash_table_test
, Shash_table_test
, 1, 1, 0,
4531 "Return the test TABLE uses.")
4535 return check_hash_table (table
)->test
;
4539 DEFUN ("hash-table-weakness", Fhash_table_weakness
, Shash_table_weakness
,
4541 "Return the weakness of TABLE.")
4545 return check_hash_table (table
)->weak
;
4549 DEFUN ("hash-table-p", Fhash_table_p
, Shash_table_p
, 1, 1, 0,
4550 "Return t if OBJ is a Lisp hash table object.")
4554 return HASH_TABLE_P (obj
) ? Qt
: Qnil
;
4558 DEFUN ("clrhash", Fclrhash
, Sclrhash
, 1, 1, 0,
4559 "Clear hash table TABLE.")
4563 hash_clear (check_hash_table (table
));
4568 DEFUN ("gethash", Fgethash
, Sgethash
, 2, 3, 0,
4569 "Look up KEY in TABLE and return its associated value.\n\
4570 If KEY is not found, return DFLT which defaults to nil.")
4572 Lisp_Object key
, table
, dflt
;
4574 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4575 int i
= hash_lookup (h
, key
, NULL
);
4576 return i
>= 0 ? HASH_VALUE (h
, i
) : dflt
;
4580 DEFUN ("puthash", Fputhash
, Sputhash
, 3, 3, 0,
4581 "Associate KEY with VALUE is hash table TABLE.\n\
4582 If KEY is already present in table, replace its current value with\n\
4585 Lisp_Object key
, value
, table
;
4587 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4591 i
= hash_lookup (h
, key
, &hash
);
4593 HASH_VALUE (h
, i
) = value
;
4595 hash_put (h
, key
, value
, hash
);
4601 DEFUN ("remhash", Fremhash
, Sremhash
, 2, 2, 0,
4602 "Remove KEY from TABLE.")
4604 Lisp_Object key
, table
;
4606 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4607 hash_remove (h
, key
);
4612 DEFUN ("maphash", Fmaphash
, Smaphash
, 2, 2, 0,
4613 "Call FUNCTION for all entries in hash table TABLE.\n\
4614 FUNCTION is called with 2 arguments KEY and VALUE.")
4616 Lisp_Object function
, table
;
4618 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4619 Lisp_Object args
[3];
4622 for (i
= 0; i
< HASH_TABLE_SIZE (h
); ++i
)
4623 if (!NILP (HASH_HASH (h
, i
)))
4626 args
[1] = HASH_KEY (h
, i
);
4627 args
[2] = HASH_VALUE (h
, i
);
4635 DEFUN ("define-hash-table-test", Fdefine_hash_table_test
,
4636 Sdefine_hash_table_test
, 3, 3, 0,
4637 "Define a new hash table test with name NAME, a symbol.\n\
4638 In hash tables create with NAME specified as test, use TEST to compare\n\
4639 keys, and HASH for computing hash codes of keys.\n\
4641 TEST must be a function taking two arguments and returning non-nil\n\
4642 if both arguments are the same. HASH must be a function taking\n\
4643 one argument and return an integer that is the hash code of the\n\
4644 argument. Hash code computation should use the whole value range of\n\
4645 integers, including negative integers.")
4647 Lisp_Object name
, test
, hash
;
4649 return Fput (name
, Qhash_table_test
, list2 (test
, hash
));
4658 /* Hash table stuff. */
4659 Qhash_table_p
= intern ("hash-table-p");
4660 staticpro (&Qhash_table_p
);
4661 Qeq
= intern ("eq");
4663 Qeql
= intern ("eql");
4665 Qequal
= intern ("equal");
4666 staticpro (&Qequal
);
4667 QCtest
= intern (":test");
4668 staticpro (&QCtest
);
4669 QCsize
= intern (":size");
4670 staticpro (&QCsize
);
4671 QCrehash_size
= intern (":rehash-size");
4672 staticpro (&QCrehash_size
);
4673 QCrehash_threshold
= intern (":rehash-threshold");
4674 staticpro (&QCrehash_threshold
);
4675 QCweakness
= intern (":weakness");
4676 staticpro (&QCweakness
);
4677 Qkey
= intern ("key");
4679 Qvalue
= intern ("value");
4680 staticpro (&Qvalue
);
4681 Qhash_table_test
= intern ("hash-table-test");
4682 staticpro (&Qhash_table_test
);
4685 defsubr (&Smake_hash_table
);
4686 defsubr (&Scopy_hash_table
);
4687 defsubr (&Smakehash
);
4688 defsubr (&Shash_table_count
);
4689 defsubr (&Shash_table_rehash_size
);
4690 defsubr (&Shash_table_rehash_threshold
);
4691 defsubr (&Shash_table_size
);
4692 defsubr (&Shash_table_test
);
4693 defsubr (&Shash_table_weakness
);
4694 defsubr (&Shash_table_p
);
4695 defsubr (&Sclrhash
);
4696 defsubr (&Sgethash
);
4697 defsubr (&Sputhash
);
4698 defsubr (&Sremhash
);
4699 defsubr (&Smaphash
);
4700 defsubr (&Sdefine_hash_table_test
);
4702 Qstring_lessp
= intern ("string-lessp");
4703 staticpro (&Qstring_lessp
);
4704 Qprovide
= intern ("provide");
4705 staticpro (&Qprovide
);
4706 Qrequire
= intern ("require");
4707 staticpro (&Qrequire
);
4708 Qyes_or_no_p_history
= intern ("yes-or-no-p-history");
4709 staticpro (&Qyes_or_no_p_history
);
4710 Qcursor_in_echo_area
= intern ("cursor-in-echo-area");
4711 staticpro (&Qcursor_in_echo_area
);
4712 Qwidget_type
= intern ("widget-type");
4713 staticpro (&Qwidget_type
);
4715 staticpro (&string_char_byte_cache_string
);
4716 string_char_byte_cache_string
= Qnil
;
4718 Fset (Qyes_or_no_p_history
, Qnil
);
4720 DEFVAR_LISP ("features", &Vfeatures
,
4721 "A list of symbols which are the features of the executing emacs.\n\
4722 Used by `featurep' and `require', and altered by `provide'.");
4725 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box
,
4726 "*Non-nil means mouse commands use dialog boxes to ask questions.\n\
4727 This applies to y-or-n and yes-or-no questions asked by commands\n\
4728 invoked by mouse clicks and mouse menu items.");
4731 defsubr (&Sidentity
);
4734 defsubr (&Ssafe_length
);
4735 defsubr (&Sstring_bytes
);
4736 defsubr (&Sstring_equal
);
4737 defsubr (&Scompare_strings
);
4738 defsubr (&Sstring_lessp
);
4741 defsubr (&Svconcat
);
4742 defsubr (&Scopy_sequence
);
4743 defsubr (&Sstring_make_multibyte
);
4744 defsubr (&Sstring_make_unibyte
);
4745 defsubr (&Sstring_as_multibyte
);
4746 defsubr (&Sstring_as_unibyte
);
4747 defsubr (&Scopy_alist
);
4748 defsubr (&Ssubstring
);
4760 defsubr (&Snreverse
);
4761 defsubr (&Sreverse
);
4763 defsubr (&Splist_get
);
4765 defsubr (&Splist_put
);
4768 defsubr (&Sfillarray
);
4769 defsubr (&Schar_table_subtype
);
4770 defsubr (&Schar_table_parent
);
4771 defsubr (&Sset_char_table_parent
);
4772 defsubr (&Schar_table_extra_slot
);
4773 defsubr (&Sset_char_table_extra_slot
);
4774 defsubr (&Schar_table_range
);
4775 defsubr (&Sset_char_table_range
);
4776 defsubr (&Sset_char_table_default
);
4777 defsubr (&Smap_char_table
);
4780 defsubr (&Smapconcat
);
4781 defsubr (&Sy_or_n_p
);
4782 defsubr (&Syes_or_no_p
);
4783 defsubr (&Sload_average
);
4784 defsubr (&Sfeaturep
);
4785 defsubr (&Srequire
);
4786 defsubr (&Sprovide
);
4787 defsubr (&Swidget_plist_member
);
4788 defsubr (&Swidget_put
);
4789 defsubr (&Swidget_get
);
4790 defsubr (&Swidget_apply
);
4791 defsubr (&Sbase64_encode_region
);
4792 defsubr (&Sbase64_decode_region
);
4793 defsubr (&Sbase64_encode_string
);
4794 defsubr (&Sbase64_decode_string
);
4801 Vweak_hash_tables
= Qnil
;