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