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