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