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