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