Use SSDATA when the context wants char *.
[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);
130 else if (COMPILEDP (sequence))
7edbb0da 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)
e03f7933 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
dec58e65 515 if (VECTORP (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
799c08ac 885 ret = make_multibyte_string (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
799c08ac 917 ret = make_multibyte_string (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
799c08ac 943 ret = make_unibyte_string (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
KH
998 bytes = str_as_unibyte (str, bytes);
999 string = make_unibyte_string (str, bytes);
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);
1082 string = make_unibyte_string (str, chars);
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. */
1314 CHECK_ARRAY (sequence, Qsequencep);
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
f6204a24
KH
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 {
8f924df7 2098 if (!(size & (PVEC_COMPILED
f6204a24 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
7650760e 2302 if (VECTORP (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
9aea757b 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'
9aea757b
CY
2460is nil, and `use-dialog-box' is non-nil. */)
2461 (Lisp_Object prompt)
7b863bd5
JB
2462{
2463 register Lisp_Object ans;
9aea757b 2464 Lisp_Object args[2];
7b863bd5 2465 struct gcpro gcpro1;
9aea757b
CY
2466
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
9aea757b
CY
2488 args[0] = prompt;
2489 args[1] = build_string ("(yes or no) ");
2490 prompt = Fconcat (2, args);
2491
7b863bd5 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);
24c129e4 2985 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
2efdd1b9
KH
2986 NILP (no_line_break),
2987 !NILP (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 {
62a6e103 3075 c = STRING_CHAR_AND_LENGTH (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 {
62a6e103 3115 c = STRING_CHAR_AND_LENGTH (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 {
62a6e103 3139 c = STRING_CHAR_AND_LENGTH (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;
caff31d4 3169 int multibyte = !NILP (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);
caff31d4
KH
3186 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3187 multibyte, &inserted_chars);
3188 if (decoded_length > allength)
24c129e4
KH
3189 abort ();
3190
3191 if (decoded_length < 0)
8c217645
KH
3192 {
3193 /* The decoding wasn't possible. */
233f3db6 3194 SAFE_FREE ();
a90e80bf 3195 error ("Invalid base64 data");
8c217645 3196 }
24c129e4
KH
3197
3198 /* Now we have decoded the region, so we insert the new contents
3199 and delete the old. (Insert first in order to preserve markers.) */
59f953a2 3200 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
2efdd1b9 3201 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
233f3db6 3202 SAFE_FREE ();
799c08ac 3203
2efdd1b9
KH
3204 /* Delete the original text. */
3205 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3206 iend + decoded_length, 1);
24c129e4
KH
3207
3208 /* If point was outside of the region, restore it exactly; else just
3209 move to the beginning of the region. */
3210 if (old_pos >= XFASTINT (end))
9b703a38
KH
3211 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3212 else if (old_pos > XFASTINT (beg))
3213 old_pos = XFASTINT (beg);
e52ad9c9 3214 SET_PT (old_pos > ZV ? ZV : old_pos);
24c129e4 3215
9b703a38 3216 return make_number (inserted_chars);
24c129e4
KH
3217}
3218
3219DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3220 1, 1, 0,
e9d8ddc9 3221 doc: /* Base64-decode STRING and return the result. */)
5842a27b 3222 (Lisp_Object string)
24c129e4
KH
3223{
3224 char *decoded;
e6d4aefa 3225 EMACS_INT length, decoded_length;
4b2e75e6 3226 Lisp_Object decoded_string;
799c08ac 3227 USE_SAFE_ALLOCA;
24c129e4 3228
b7826503 3229 CHECK_STRING (string);
24c129e4 3230
d5db4077 3231 length = SBYTES (string);
24c129e4 3232 /* We need to allocate enough room for decoding the text. */
799c08ac 3233 SAFE_ALLOCA (decoded, char *, length);
24c129e4 3234
8ec118cd 3235 /* The decoded result should be unibyte. */
42a5b22f 3236 decoded_length = base64_decode_1 (SSDATA (string), decoded, length,
8ec118cd 3237 0, NULL);
24c129e4
KH
3238 if (decoded_length > length)
3239 abort ();
3d6c79c5 3240 else if (decoded_length >= 0)
2efdd1b9 3241 decoded_string = make_unibyte_string (decoded, decoded_length);
3d6c79c5
GM
3242 else
3243 decoded_string = Qnil;
24c129e4 3244
233f3db6 3245 SAFE_FREE ();
3d6c79c5 3246 if (!STRINGP (decoded_string))
a90e80bf 3247 error ("Invalid base64 data");
4b2e75e6
EZ
3248
3249 return decoded_string;
24c129e4
KH
3250}
3251
caff31d4
KH
3252/* Base64-decode the data at FROM of LENGHT bytes into TO. If
3253 MULTIBYTE is nonzero, the decoded result should be in multibyte
3254 form. If NCHARS_RETRUN is not NULL, store the number of produced
3255 characters in *NCHARS_RETURN. */
3256
e6d4aefa
EZ
3257static EMACS_INT
3258base64_decode_1 (const char *from, char *to, EMACS_INT length,
3259 int multibyte, EMACS_INT *nchars_return)
24c129e4 3260{
e6d4aefa 3261 EMACS_INT i = 0; /* Used inside READ_QUADRUPLET_BYTE */
24c129e4
KH
3262 char *e = to;
3263 unsigned char c;
3264 unsigned long value;
e6d4aefa 3265 EMACS_INT nchars = 0;
24c129e4 3266
9a092df0 3267 while (1)
24c129e4 3268 {
9a092df0 3269 /* Process first byte of a quadruplet. */
24c129e4 3270
9a092df0 3271 READ_QUADRUPLET_BYTE (e-to);
24c129e4
KH
3272
3273 if (!IS_BASE64 (c))
3274 return -1;
3275 value = base64_char_to_value[c] << 18;
3276
3277 /* Process second byte of a quadruplet. */
3278
9a092df0 3279 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3280
3281 if (!IS_BASE64 (c))
3282 return -1;
3283 value |= base64_char_to_value[c] << 12;
3284
caff31d4 3285 c = (unsigned char) (value >> 16);
5a38b8c5
KH
3286 if (multibyte && c >= 128)
3287 e += BYTE8_STRING (c, e);
caff31d4
KH
3288 else
3289 *e++ = c;
3290 nchars++;
24c129e4
KH
3291
3292 /* Process third byte of a quadruplet. */
59f953a2 3293
9a092df0 3294 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3295
3296 if (c == '=')
3297 {
9a092df0 3298 READ_QUADRUPLET_BYTE (-1);
59f953a2 3299
24c129e4
KH
3300 if (c != '=')
3301 return -1;
3302 continue;
3303 }
3304
3305 if (!IS_BASE64 (c))
3306 return -1;
3307 value |= base64_char_to_value[c] << 6;
3308
caff31d4 3309 c = (unsigned char) (0xff & value >> 8);
5a38b8c5
KH
3310 if (multibyte && c >= 128)
3311 e += BYTE8_STRING (c, e);
caff31d4
KH
3312 else
3313 *e++ = c;
3314 nchars++;
24c129e4
KH
3315
3316 /* Process fourth byte of a quadruplet. */
3317
9a092df0 3318 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3319
3320 if (c == '=')
3321 continue;
3322
3323 if (!IS_BASE64 (c))
3324 return -1;
3325 value |= base64_char_to_value[c];
3326
caff31d4 3327 c = (unsigned char) (0xff & value);
5a38b8c5
KH
3328 if (multibyte && c >= 128)
3329 e += BYTE8_STRING (c, e);
caff31d4
KH
3330 else
3331 *e++ = c;
3332 nchars++;
24c129e4 3333 }
24c129e4 3334}
d80c6c11
GM
3335
3336
3337\f
3338/***********************************************************************
3339 ***** *****
3340 ***** Hash Tables *****
3341 ***** *****
3342 ***********************************************************************/
3343
3344/* Implemented by gerd@gnu.org. This hash table implementation was
3345 inspired by CMUCL hash tables. */
3346
3347/* Ideas:
3348
3349 1. For small tables, association lists are probably faster than
3350 hash tables because they have lower overhead.
3351
3352 For uses of hash tables where the O(1) behavior of table
3353 operations is not a requirement, it might therefore be a good idea
3354 not to hash. Instead, we could just do a linear search in the
3355 key_and_value vector of the hash table. This could be done
3356 if a `:linear-search t' argument is given to make-hash-table. */
3357
3358
d80c6c11
GM
3359/* The list of all weak hash tables. Don't staticpro this one. */
3360
6c661ec9 3361struct Lisp_Hash_Table *weak_hash_tables;
d80c6c11
GM
3362
3363/* Various symbols. */
3364
f899c503 3365Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
ee0403b3 3366Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
ec504e6f 3367Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
d80c6c11
GM
3368
3369/* Function prototypes. */
3370
f57e2426
J
3371static struct Lisp_Hash_Table *check_hash_table (Lisp_Object);
3372static int get_key_arg (Lisp_Object, int, Lisp_Object *, char *);
3373static void maybe_resize_hash_table (struct Lisp_Hash_Table *);
3374static int cmpfn_eql (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3375 Lisp_Object, unsigned);
3376static int cmpfn_equal (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3377 Lisp_Object, unsigned);
3378static int cmpfn_user_defined (struct Lisp_Hash_Table *, Lisp_Object,
3379 unsigned, Lisp_Object, unsigned);
3380static unsigned hashfn_eq (struct Lisp_Hash_Table *, Lisp_Object);
3381static unsigned hashfn_eql (struct Lisp_Hash_Table *, Lisp_Object);
3382static unsigned hashfn_equal (struct Lisp_Hash_Table *, Lisp_Object);
3383static unsigned hashfn_user_defined (struct Lisp_Hash_Table *,
3384 Lisp_Object);
3385static unsigned sxhash_string (unsigned char *, int);
3386static unsigned sxhash_list (Lisp_Object, int);
3387static unsigned sxhash_vector (Lisp_Object, int);
3388static unsigned sxhash_bool_vector (Lisp_Object);
3389static int sweep_weak_table (struct Lisp_Hash_Table *, int);
d80c6c11
GM
3390
3391
3392\f
3393/***********************************************************************
3394 Utilities
3395 ***********************************************************************/
3396
3397/* If OBJ is a Lisp hash table, return a pointer to its struct
3398 Lisp_Hash_Table. Otherwise, signal an error. */
3399
3400static struct Lisp_Hash_Table *
971de7fb 3401check_hash_table (Lisp_Object obj)
d80c6c11 3402{
b7826503 3403 CHECK_HASH_TABLE (obj);
d80c6c11
GM
3404 return XHASH_TABLE (obj);
3405}
3406
3407
3408/* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3409 number. */
3410
6e509e80 3411int
971de7fb 3412next_almost_prime (int n)
d80c6c11
GM
3413{
3414 if (n % 2 == 0)
3415 n += 1;
3416 if (n % 3 == 0)
3417 n += 2;
3418 if (n % 7 == 0)
3419 n += 4;
3420 return n;
3421}
3422
3423
3424/* Find KEY in ARGS which has size NARGS. Don't consider indices for
3425 which USED[I] is non-zero. If found at index I in ARGS, set
3426 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3427 -1. This function is used to extract a keyword/argument pair from
3428 a DEFUN parameter list. */
3429
3430static int
971de7fb 3431get_key_arg (Lisp_Object key, int nargs, Lisp_Object *args, char *used)
d80c6c11
GM
3432{
3433 int i;
59f953a2 3434
d80c6c11
GM
3435 for (i = 0; i < nargs - 1; ++i)
3436 if (!used[i] && EQ (args[i], key))
3437 break;
59f953a2 3438
d80c6c11
GM
3439 if (i >= nargs - 1)
3440 i = -1;
3441 else
3442 {
3443 used[i++] = 1;
3444 used[i] = 1;
3445 }
59f953a2 3446
d80c6c11
GM
3447 return i;
3448}
3449
3450
3451/* Return a Lisp vector which has the same contents as VEC but has
3452 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3453 vector that are not copied from VEC are set to INIT. */
3454
fa7dad5b 3455Lisp_Object
971de7fb 3456larger_vector (Lisp_Object vec, int new_size, Lisp_Object init)
d80c6c11
GM
3457{
3458 struct Lisp_Vector *v;
3459 int i, old_size;
3460
3461 xassert (VECTORP (vec));
7edbb0da 3462 old_size = ASIZE (vec);
d80c6c11
GM
3463 xassert (new_size >= old_size);
3464
b3660ef6 3465 v = allocate_vector (new_size);
72af86bd 3466 memcpy (v->contents, XVECTOR (vec)->contents, old_size * sizeof *v->contents);
d80c6c11
GM
3467 for (i = old_size; i < new_size; ++i)
3468 v->contents[i] = init;
3469 XSETVECTOR (vec, v);
3470 return vec;
3471}
3472
3473
3474/***********************************************************************
3475 Low-level Functions
3476 ***********************************************************************/
3477
d80c6c11
GM
3478/* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3479 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3480 KEY2 are the same. */
3481
3482static int
971de7fb 3483cmpfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
d80c6c11 3484{
2e5da676
GM
3485 return (FLOATP (key1)
3486 && FLOATP (key2)
e84b1dea 3487 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
d80c6c11
GM
3488}
3489
3490
3491/* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3492 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3493 KEY2 are the same. */
3494
3495static int
971de7fb 3496cmpfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
d80c6c11 3497{
2e5da676 3498 return hash1 == hash2 && !NILP (Fequal (key1, key2));
d80c6c11
GM
3499}
3500
59f953a2 3501
d80c6c11
GM
3502/* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3503 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3504 if KEY1 and KEY2 are the same. */
3505
3506static int
971de7fb 3507cmpfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
d80c6c11
GM
3508{
3509 if (hash1 == hash2)
3510 {
3511 Lisp_Object args[3];
59f953a2 3512
d80c6c11
GM
3513 args[0] = h->user_cmp_function;
3514 args[1] = key1;
3515 args[2] = key2;
3516 return !NILP (Ffuncall (3, args));
3517 }
3518 else
3519 return 0;
3520}
3521
3522
3523/* Value is a hash code for KEY for use in hash table H which uses
3524 `eq' to compare keys. The hash code returned is guaranteed to fit
3525 in a Lisp integer. */
3526
3527static unsigned
971de7fb 3528hashfn_eq (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11 3529{
8e50cc2d 3530 unsigned hash = XUINT (key) ^ XTYPE (key);
6b61353c 3531 xassert ((hash & ~INTMASK) == 0);
cf681889 3532 return hash;
d80c6c11
GM
3533}
3534
3535
3536/* Value is a hash code for KEY for use in hash table H which uses
3537 `eql' to compare keys. The hash code returned is guaranteed to fit
3538 in a Lisp integer. */
3539
3540static unsigned
971de7fb 3541hashfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11 3542{
cf681889
GM
3543 unsigned hash;
3544 if (FLOATP (key))
3545 hash = sxhash (key, 0);
d80c6c11 3546 else
8e50cc2d 3547 hash = XUINT (key) ^ XTYPE (key);
6b61353c 3548 xassert ((hash & ~INTMASK) == 0);
cf681889 3549 return hash;
d80c6c11
GM
3550}
3551
3552
3553/* Value is a hash code for KEY for use in hash table H which uses
3554 `equal' to compare keys. The hash code returned is guaranteed to fit
3555 in a Lisp integer. */
3556
3557static unsigned
971de7fb 3558hashfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11 3559{
cf681889 3560 unsigned hash = sxhash (key, 0);
6b61353c 3561 xassert ((hash & ~INTMASK) == 0);
cf681889 3562 return hash;
d80c6c11
GM
3563}
3564
3565
3566/* Value is a hash code for KEY for use in hash table H which uses as
3567 user-defined function to compare keys. The hash code returned is
3568 guaranteed to fit in a Lisp integer. */
3569
3570static unsigned
971de7fb 3571hashfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11
GM
3572{
3573 Lisp_Object args[2], hash;
59f953a2 3574
d80c6c11
GM
3575 args[0] = h->user_hash_function;
3576 args[1] = key;
3577 hash = Ffuncall (2, args);
3578 if (!INTEGERP (hash))
692ae65c 3579 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
d80c6c11
GM
3580 return XUINT (hash);
3581}
3582
3583
3584/* Create and initialize a new hash table.
3585
3586 TEST specifies the test the hash table will use to compare keys.
3587 It must be either one of the predefined tests `eq', `eql' or
3588 `equal' or a symbol denoting a user-defined test named TEST with
3589 test and hash functions USER_TEST and USER_HASH.
59f953a2 3590
1fd4c450 3591 Give the table initial capacity SIZE, SIZE >= 0, an integer.
d80c6c11
GM
3592
3593 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3594 new size when it becomes full is computed by adding REHASH_SIZE to
3595 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3596 table's new size is computed by multiplying its old size with
3597 REHASH_SIZE.
3598
3599 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3600 be resized when the ratio of (number of entries in the table) /
3601 (table size) is >= REHASH_THRESHOLD.
3602
3603 WEAK specifies the weakness of the table. If non-nil, it must be
ec504e6f 3604 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
d80c6c11
GM
3605
3606Lisp_Object
d5a3eaaf
AS
3607make_hash_table (Lisp_Object test, Lisp_Object size, Lisp_Object rehash_size,
3608 Lisp_Object rehash_threshold, Lisp_Object weak,
3609 Lisp_Object user_test, Lisp_Object user_hash)
d80c6c11
GM
3610{
3611 struct Lisp_Hash_Table *h;
d80c6c11 3612 Lisp_Object table;
b3660ef6 3613 int index_size, i, sz;
d80c6c11
GM
3614
3615 /* Preconditions. */
3616 xassert (SYMBOLP (test));
1fd4c450 3617 xassert (INTEGERP (size) && XINT (size) >= 0);
d80c6c11
GM
3618 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3619 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3620 xassert (FLOATP (rehash_threshold)
3621 && XFLOATINT (rehash_threshold) > 0
3622 && XFLOATINT (rehash_threshold) <= 1.0);
3623
1fd4c450
GM
3624 if (XFASTINT (size) == 0)
3625 size = make_number (1);
3626
b3660ef6
GM
3627 /* Allocate a table and initialize it. */
3628 h = allocate_hash_table ();
d80c6c11
GM
3629
3630 /* Initialize hash table slots. */
3631 sz = XFASTINT (size);
59f953a2 3632
d80c6c11
GM
3633 h->test = test;
3634 if (EQ (test, Qeql))
3635 {
3636 h->cmpfn = cmpfn_eql;
3637 h->hashfn = hashfn_eql;
3638 }
3639 else if (EQ (test, Qeq))
3640 {
2e5da676 3641 h->cmpfn = NULL;
d80c6c11
GM
3642 h->hashfn = hashfn_eq;
3643 }
3644 else if (EQ (test, Qequal))
3645 {
3646 h->cmpfn = cmpfn_equal;
3647 h->hashfn = hashfn_equal;
3648 }
3649 else
3650 {
3651 h->user_cmp_function = user_test;
3652 h->user_hash_function = user_hash;
3653 h->cmpfn = cmpfn_user_defined;
3654 h->hashfn = hashfn_user_defined;
3655 }
59f953a2 3656
d80c6c11
GM
3657 h->weak = weak;
3658 h->rehash_threshold = rehash_threshold;
3659 h->rehash_size = rehash_size;
878f97ff 3660 h->count = 0;
d80c6c11
GM
3661 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3662 h->hash = Fmake_vector (size, Qnil);
3663 h->next = Fmake_vector (size, Qnil);
0690cb37
DL
3664 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
3665 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
d80c6c11
GM
3666 h->index = Fmake_vector (make_number (index_size), Qnil);
3667
3668 /* Set up the free list. */
3669 for (i = 0; i < sz - 1; ++i)
3670 HASH_NEXT (h, i) = make_number (i + 1);
3671 h->next_free = make_number (0);
3672
3673 XSET_HASH_TABLE (table, h);
3674 xassert (HASH_TABLE_P (table));
3675 xassert (XHASH_TABLE (table) == h);
3676
3677 /* Maybe add this hash table to the list of all weak hash tables. */
3678 if (NILP (h->weak))
6c661ec9 3679 h->next_weak = NULL;
d80c6c11
GM
3680 else
3681 {
6c661ec9
SM
3682 h->next_weak = weak_hash_tables;
3683 weak_hash_tables = h;
d80c6c11
GM
3684 }
3685
3686 return table;
3687}
3688
3689
f899c503
GM
3690/* Return a copy of hash table H1. Keys and values are not copied,
3691 only the table itself is. */
3692
2f7c71a1 3693static Lisp_Object
971de7fb 3694copy_hash_table (struct Lisp_Hash_Table *h1)
f899c503
GM
3695{
3696 Lisp_Object table;
3697 struct Lisp_Hash_Table *h2;
44dc78e0 3698 struct Lisp_Vector *next;
59f953a2 3699
b3660ef6 3700 h2 = allocate_hash_table ();
f899c503 3701 next = h2->vec_next;
72af86bd 3702 memcpy (h2, h1, sizeof *h2);
f899c503
GM
3703 h2->vec_next = next;
3704 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
3705 h2->hash = Fcopy_sequence (h1->hash);
3706 h2->next = Fcopy_sequence (h1->next);
3707 h2->index = Fcopy_sequence (h1->index);
3708 XSET_HASH_TABLE (table, h2);
3709
3710 /* Maybe add this hash table to the list of all weak hash tables. */
3711 if (!NILP (h2->weak))
3712 {
6c661ec9
SM
3713 h2->next_weak = weak_hash_tables;
3714 weak_hash_tables = h2;
f899c503
GM
3715 }
3716
3717 return table;
3718}
3719
3720
d80c6c11
GM
3721/* Resize hash table H if it's too full. If H cannot be resized
3722 because it's already too large, throw an error. */
3723
3724static INLINE void
971de7fb 3725maybe_resize_hash_table (struct Lisp_Hash_Table *h)
d80c6c11
GM
3726{
3727 if (NILP (h->next_free))
3728 {
3729 int old_size = HASH_TABLE_SIZE (h);
3730 int i, new_size, index_size;
9bd1cd35 3731 EMACS_INT nsize;
59f953a2 3732
d80c6c11
GM
3733 if (INTEGERP (h->rehash_size))
3734 new_size = old_size + XFASTINT (h->rehash_size);
3735 else
3736 new_size = old_size * XFLOATINT (h->rehash_size);
0d6ba42e 3737 new_size = max (old_size + 1, new_size);
0690cb37
DL
3738 index_size = next_almost_prime ((int)
3739 (new_size
3740 / XFLOATINT (h->rehash_threshold)));
9bd1cd35
EZ
3741 /* Assignment to EMACS_INT stops GCC whining about limited range
3742 of data type. */
3743 nsize = max (index_size, 2 * new_size);
3744 if (nsize > MOST_POSITIVE_FIXNUM)
d80c6c11
GM
3745 error ("Hash table too large to resize");
3746
3747 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
3748 h->next = larger_vector (h->next, new_size, Qnil);
3749 h->hash = larger_vector (h->hash, new_size, Qnil);
3750 h->index = Fmake_vector (make_number (index_size), Qnil);
3751
3752 /* Update the free list. Do it so that new entries are added at
3753 the end of the free list. This makes some operations like
3754 maphash faster. */
3755 for (i = old_size; i < new_size - 1; ++i)
3756 HASH_NEXT (h, i) = make_number (i + 1);
59f953a2 3757
d80c6c11
GM
3758 if (!NILP (h->next_free))
3759 {
3760 Lisp_Object last, next;
59f953a2 3761
d80c6c11
GM
3762 last = h->next_free;
3763 while (next = HASH_NEXT (h, XFASTINT (last)),
3764 !NILP (next))
3765 last = next;
59f953a2 3766
d80c6c11
GM
3767 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
3768 }
3769 else
3770 XSETFASTINT (h->next_free, old_size);
3771
3772 /* Rehash. */
3773 for (i = 0; i < old_size; ++i)
3774 if (!NILP (HASH_HASH (h, i)))
3775 {
3776 unsigned hash_code = XUINT (HASH_HASH (h, i));
7edbb0da 3777 int start_of_bucket = hash_code % ASIZE (h->index);
d80c6c11
GM
3778 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3779 HASH_INDEX (h, start_of_bucket) = make_number (i);
3780 }
59f953a2 3781 }
d80c6c11
GM
3782}
3783
3784
3785/* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3786 the hash code of KEY. Value is the index of the entry in H
3787 matching KEY, or -1 if not found. */
3788
3789int
971de7fb 3790hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, unsigned int *hash)
d80c6c11
GM
3791{
3792 unsigned hash_code;
3793 int start_of_bucket;
3794 Lisp_Object idx;
3795
3796 hash_code = h->hashfn (h, key);
3797 if (hash)
3798 *hash = hash_code;
59f953a2 3799
7edbb0da 3800 start_of_bucket = hash_code % ASIZE (h->index);
d80c6c11
GM
3801 idx = HASH_INDEX (h, start_of_bucket);
3802
f5c75033 3803 /* We need not gcpro idx since it's either an integer or nil. */
d80c6c11
GM
3804 while (!NILP (idx))
3805 {
3806 int i = XFASTINT (idx);
2e5da676
GM
3807 if (EQ (key, HASH_KEY (h, i))
3808 || (h->cmpfn
3809 && h->cmpfn (h, key, hash_code,
7c752c80 3810 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
d80c6c11
GM
3811 break;
3812 idx = HASH_NEXT (h, i);
3813 }
3814
3815 return NILP (idx) ? -1 : XFASTINT (idx);
3816}
3817
3818
3819/* Put an entry into hash table H that associates KEY with VALUE.
64a5094a
KH
3820 HASH is a previously computed hash code of KEY.
3821 Value is the index of the entry in H matching KEY. */
d80c6c11 3822
64a5094a 3823int
971de7fb 3824hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value, unsigned int hash)
d80c6c11
GM
3825{
3826 int start_of_bucket, i;
3827
6b61353c 3828 xassert ((hash & ~INTMASK) == 0);
d80c6c11
GM
3829
3830 /* Increment count after resizing because resizing may fail. */
3831 maybe_resize_hash_table (h);
878f97ff 3832 h->count++;
59f953a2 3833
d80c6c11
GM
3834 /* Store key/value in the key_and_value vector. */
3835 i = XFASTINT (h->next_free);
3836 h->next_free = HASH_NEXT (h, i);
3837 HASH_KEY (h, i) = key;
3838 HASH_VALUE (h, i) = value;
3839
3840 /* Remember its hash code. */
3841 HASH_HASH (h, i) = make_number (hash);
3842
3843 /* Add new entry to its collision chain. */
7edbb0da 3844 start_of_bucket = hash % ASIZE (h->index);
d80c6c11
GM
3845 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3846 HASH_INDEX (h, start_of_bucket) = make_number (i);
64a5094a 3847 return i;
d80c6c11
GM
3848}
3849
3850
3851/* Remove the entry matching KEY from hash table H, if there is one. */
3852
2749d28e 3853static void
971de7fb 3854hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11
GM
3855{
3856 unsigned hash_code;
3857 int start_of_bucket;
3858 Lisp_Object idx, prev;
3859
3860 hash_code = h->hashfn (h, key);
7edbb0da 3861 start_of_bucket = hash_code % ASIZE (h->index);
d80c6c11
GM
3862 idx = HASH_INDEX (h, start_of_bucket);
3863 prev = Qnil;
3864
f5c75033 3865 /* We need not gcpro idx, prev since they're either integers or nil. */
d80c6c11
GM
3866 while (!NILP (idx))
3867 {
3868 int i = XFASTINT (idx);
3869
2e5da676
GM
3870 if (EQ (key, HASH_KEY (h, i))
3871 || (h->cmpfn
3872 && h->cmpfn (h, key, hash_code,
7c752c80 3873 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
d80c6c11
GM
3874 {
3875 /* Take entry out of collision chain. */
3876 if (NILP (prev))
3877 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
3878 else
3879 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
3880
3881 /* Clear slots in key_and_value and add the slots to
3882 the free list. */
3883 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
3884 HASH_NEXT (h, i) = h->next_free;
3885 h->next_free = make_number (i);
878f97ff
SM
3886 h->count--;
3887 xassert (h->count >= 0);
d80c6c11
GM
3888 break;
3889 }
3890 else
3891 {
3892 prev = idx;
3893 idx = HASH_NEXT (h, i);
3894 }
3895 }
3896}
3897
3898
3899/* Clear hash table H. */
3900
2f7c71a1 3901static void
971de7fb 3902hash_clear (struct Lisp_Hash_Table *h)
d80c6c11 3903{
878f97ff 3904 if (h->count > 0)
d80c6c11
GM
3905 {
3906 int i, size = HASH_TABLE_SIZE (h);
3907
3908 for (i = 0; i < size; ++i)
3909 {
3910 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
3911 HASH_KEY (h, i) = Qnil;
3912 HASH_VALUE (h, i) = Qnil;
3913 HASH_HASH (h, i) = Qnil;
3914 }
3915
7edbb0da 3916 for (i = 0; i < ASIZE (h->index); ++i)
68b587a6 3917 ASET (h->index, i, Qnil);
d80c6c11
GM
3918
3919 h->next_free = make_number (0);
878f97ff 3920 h->count = 0;
d80c6c11
GM
3921 }
3922}
3923
3924
3925\f
3926/************************************************************************
3927 Weak Hash Tables
3928 ************************************************************************/
3929
14067ea7 3930void
971de7fb 3931init_weak_hash_tables (void)
14067ea7
CY
3932{
3933 weak_hash_tables = NULL;
3934}
3935
a0b581cc
GM
3936/* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
3937 entries from the table that don't survive the current GC.
3938 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
3939 non-zero if anything was marked. */
3940
3941static int
971de7fb 3942sweep_weak_table (struct Lisp_Hash_Table *h, int remove_entries_p)
a0b581cc
GM
3943{
3944 int bucket, n, marked;
59f953a2 3945
7edbb0da 3946 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
a0b581cc 3947 marked = 0;
59f953a2 3948
a0b581cc
GM
3949 for (bucket = 0; bucket < n; ++bucket)
3950 {
1e546714 3951 Lisp_Object idx, next, prev;
a0b581cc
GM
3952
3953 /* Follow collision chain, removing entries that
3954 don't survive this garbage collection. */
a0b581cc 3955 prev = Qnil;
8e50cc2d 3956 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
a0b581cc 3957 {
a0b581cc 3958 int i = XFASTINT (idx);
1e546714
GM
3959 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
3960 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
3961 int remove_p;
59f953a2 3962
a0b581cc 3963 if (EQ (h->weak, Qkey))
aee625fa 3964 remove_p = !key_known_to_survive_p;
a0b581cc 3965 else if (EQ (h->weak, Qvalue))
aee625fa 3966 remove_p = !value_known_to_survive_p;
ec504e6f 3967 else if (EQ (h->weak, Qkey_or_value))
728c5d9d 3968 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
ec504e6f 3969 else if (EQ (h->weak, Qkey_and_value))
728c5d9d 3970 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
a0b581cc
GM
3971 else
3972 abort ();
59f953a2 3973
a0b581cc
GM
3974 next = HASH_NEXT (h, i);
3975
3976 if (remove_entries_p)
3977 {
3978 if (remove_p)
3979 {
3980 /* Take out of collision chain. */
8e50cc2d 3981 if (NILP (prev))
1e546714 3982 HASH_INDEX (h, bucket) = next;
a0b581cc
GM
3983 else
3984 HASH_NEXT (h, XFASTINT (prev)) = next;
59f953a2 3985
a0b581cc
GM
3986 /* Add to free list. */
3987 HASH_NEXT (h, i) = h->next_free;
3988 h->next_free = idx;
59f953a2 3989
a0b581cc
GM
3990 /* Clear key, value, and hash. */
3991 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
3992 HASH_HASH (h, i) = Qnil;
59f953a2 3993
878f97ff 3994 h->count--;
a0b581cc 3995 }
d278cde0
KS
3996 else
3997 {
3998 prev = idx;
3999 }
a0b581cc
GM
4000 }
4001 else
4002 {
4003 if (!remove_p)
4004 {
4005 /* Make sure key and value survive. */
aee625fa
GM
4006 if (!key_known_to_survive_p)
4007 {
9568e3d8 4008 mark_object (HASH_KEY (h, i));
aee625fa
GM
4009 marked = 1;
4010 }
4011
4012 if (!value_known_to_survive_p)
4013 {
9568e3d8 4014 mark_object (HASH_VALUE (h, i));
aee625fa
GM
4015 marked = 1;
4016 }
a0b581cc
GM
4017 }
4018 }
a0b581cc
GM
4019 }
4020 }
4021
4022 return marked;
4023}
4024
d80c6c11
GM
4025/* Remove elements from weak hash tables that don't survive the
4026 current garbage collection. Remove weak tables that don't survive
4027 from Vweak_hash_tables. Called from gc_sweep. */
4028
4029void
971de7fb 4030sweep_weak_hash_tables (void)
d80c6c11 4031{
6c661ec9 4032 struct Lisp_Hash_Table *h, *used, *next;
a0b581cc
GM
4033 int marked;
4034
4035 /* Mark all keys and values that are in use. Keep on marking until
4036 there is no more change. This is necessary for cases like
4037 value-weak table A containing an entry X -> Y, where Y is used in a
4038 key-weak table B, Z -> Y. If B comes after A in the list of weak
4039 tables, X -> Y might be removed from A, although when looking at B
4040 one finds that it shouldn't. */
4041 do
4042 {
4043 marked = 0;
6c661ec9 4044 for (h = weak_hash_tables; h; h = h->next_weak)
a0b581cc 4045 {
a0b581cc
GM
4046 if (h->size & ARRAY_MARK_FLAG)
4047 marked |= sweep_weak_table (h, 0);
4048 }
4049 }
4050 while (marked);
d80c6c11 4051
a0b581cc 4052 /* Remove tables and entries that aren't used. */
6c661ec9 4053 for (h = weak_hash_tables, used = NULL; h; h = next)
d80c6c11 4054 {
ac0e96ee 4055 next = h->next_weak;
91f78c99 4056
d80c6c11
GM
4057 if (h->size & ARRAY_MARK_FLAG)
4058 {
ac0e96ee 4059 /* TABLE is marked as used. Sweep its contents. */
878f97ff 4060 if (h->count > 0)
a0b581cc 4061 sweep_weak_table (h, 1);
ac0e96ee
GM
4062
4063 /* Add table to the list of used weak hash tables. */
4064 h->next_weak = used;
6c661ec9 4065 used = h;
d80c6c11
GM
4066 }
4067 }
ac0e96ee 4068
6c661ec9 4069 weak_hash_tables = used;
d80c6c11
GM
4070}
4071
4072
4073\f
4074/***********************************************************************
4075 Hash Code Computation
4076 ***********************************************************************/
4077
4078/* Maximum depth up to which to dive into Lisp structures. */
4079
4080#define SXHASH_MAX_DEPTH 3
4081
4082/* Maximum length up to which to take list and vector elements into
4083 account. */
4084
4085#define SXHASH_MAX_LEN 7
4086
4087/* Combine two integers X and Y for hashing. */
4088
4089#define SXHASH_COMBINE(X, Y) \
ada0fa14 4090 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
d80c6c11
GM
4091 + (unsigned)(Y))
4092
4093
cf681889
GM
4094/* Return a hash for string PTR which has length LEN. The hash
4095 code returned is guaranteed to fit in a Lisp integer. */
d80c6c11
GM
4096
4097static unsigned
971de7fb 4098sxhash_string (unsigned char *ptr, int len)
d80c6c11
GM
4099{
4100 unsigned char *p = ptr;
4101 unsigned char *end = p + len;
4102 unsigned char c;
4103 unsigned hash = 0;
4104
4105 while (p != end)
4106 {
4107 c = *p++;
4108 if (c >= 0140)
4109 c -= 40;
11f56bbc 4110 hash = ((hash << 4) + (hash >> 28) + c);
d80c6c11 4111 }
59f953a2 4112
6b61353c 4113 return hash & INTMASK;
d80c6c11
GM
4114}
4115
4116
4117/* Return a hash for list LIST. DEPTH is the current depth in the
4118 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4119
4120static unsigned
971de7fb 4121sxhash_list (Lisp_Object list, int depth)
d80c6c11
GM
4122{
4123 unsigned hash = 0;
4124 int i;
59f953a2 4125
d80c6c11
GM
4126 if (depth < SXHASH_MAX_DEPTH)
4127 for (i = 0;
4128 CONSP (list) && i < SXHASH_MAX_LEN;
4129 list = XCDR (list), ++i)
4130 {
4131 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4132 hash = SXHASH_COMBINE (hash, hash2);
4133 }
4134
ea284f33
KS
4135 if (!NILP (list))
4136 {
4137 unsigned hash2 = sxhash (list, depth + 1);
4138 hash = SXHASH_COMBINE (hash, hash2);
4139 }
4140
d80c6c11
GM
4141 return hash;
4142}
4143
4144
4145/* Return a hash for vector VECTOR. DEPTH is the current depth in
4146 the Lisp structure. */
4147
4148static unsigned
971de7fb 4149sxhash_vector (Lisp_Object vec, int depth)
d80c6c11 4150{
7edbb0da 4151 unsigned hash = ASIZE (vec);
d80c6c11
GM
4152 int i, n;
4153
7edbb0da 4154 n = min (SXHASH_MAX_LEN, ASIZE (vec));
d80c6c11
GM
4155 for (i = 0; i < n; ++i)
4156 {
7edbb0da 4157 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
d80c6c11
GM
4158 hash = SXHASH_COMBINE (hash, hash2);
4159 }
4160
4161 return hash;
4162}
4163
4164
4165/* Return a hash for bool-vector VECTOR. */
4166
4167static unsigned
971de7fb 4168sxhash_bool_vector (Lisp_Object vec)
d80c6c11
GM
4169{
4170 unsigned hash = XBOOL_VECTOR (vec)->size;
4171 int i, n;
4172
4173 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4174 for (i = 0; i < n; ++i)
4175 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4176
4177 return hash;
4178}
4179
4180
4181/* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
6b61353c 4182 structure. Value is an unsigned integer clipped to INTMASK. */
d80c6c11
GM
4183
4184unsigned
971de7fb 4185sxhash (Lisp_Object obj, int depth)
d80c6c11
GM
4186{
4187 unsigned hash;
4188
4189 if (depth > SXHASH_MAX_DEPTH)
4190 return 0;
59f953a2 4191
d80c6c11
GM
4192 switch (XTYPE (obj))
4193 {
2de9f71c 4194 case_Lisp_Int:
d80c6c11
GM
4195 hash = XUINT (obj);
4196 break;
4197
d80c6c11
GM
4198 case Lisp_Misc:
4199 hash = XUINT (obj);
4200 break;
4201
32bfb2d5
EZ
4202 case Lisp_Symbol:
4203 obj = SYMBOL_NAME (obj);
4204 /* Fall through. */
4205
d80c6c11 4206 case Lisp_String:
d5db4077 4207 hash = sxhash_string (SDATA (obj), SCHARS (obj));
d80c6c11
GM
4208 break;
4209
4210 /* This can be everything from a vector to an overlay. */
4211 case Lisp_Vectorlike:
4212 if (VECTORP (obj))
4213 /* According to the CL HyperSpec, two arrays are equal only if
4214 they are `eq', except for strings and bit-vectors. In
4215 Emacs, this works differently. We have to compare element
4216 by element. */
4217 hash = sxhash_vector (obj, depth);
4218 else if (BOOL_VECTOR_P (obj))
4219 hash = sxhash_bool_vector (obj);
4220 else
4221 /* Others are `equal' if they are `eq', so let's take their
4222 address as hash. */
4223 hash = XUINT (obj);
4224 break;
4225
4226 case Lisp_Cons:
4227 hash = sxhash_list (obj, depth);
4228 break;
4229
4230 case Lisp_Float:
4231 {
f601cdf3
KR
4232 double val = XFLOAT_DATA (obj);
4233 unsigned char *p = (unsigned char *) &val;
4234 unsigned char *e = p + sizeof val;
d80c6c11
GM
4235 for (hash = 0; p < e; ++p)
4236 hash = SXHASH_COMBINE (hash, *p);
4237 break;
4238 }
4239
4240 default:
4241 abort ();
4242 }
4243
6b61353c 4244 return hash & INTMASK;
d80c6c11
GM
4245}
4246
4247
4248\f
4249/***********************************************************************
4250 Lisp Interface
4251 ***********************************************************************/
4252
4253
4254DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
e9d8ddc9 4255 doc: /* Compute a hash code for OBJ and return it as integer. */)
5842a27b 4256 (Lisp_Object obj)
d80c6c11 4257{
3b8c0c70 4258 unsigned hash = sxhash (obj, 0);
d80c6c11
GM
4259 return make_number (hash);
4260}
4261
4262
4263DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
e9d8ddc9 4264 doc: /* Create and return a new hash table.
91f78c99 4265
47cebab1
GM
4266Arguments are specified as keyword/argument pairs. The following
4267arguments are defined:
4268
4269:test TEST -- TEST must be a symbol that specifies how to compare
4270keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4271`equal'. User-supplied test and hash functions can be specified via
4272`define-hash-table-test'.
4273
4274:size SIZE -- A hint as to how many elements will be put in the table.
4275Default is 65.
4276
4277:rehash-size REHASH-SIZE - Indicates how to expand the table when it
79d6f59e
CY
4278fills up. If REHASH-SIZE is an integer, increase the size by that
4279amount. If it is a float, it must be > 1.0, and the new size is the
4280old size multiplied by that factor. Default is 1.5.
47cebab1
GM
4281
4282:rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
b756c005 4283Resize the hash table when the ratio (number of entries / table size)
e1025755 4284is greater than or equal to THRESHOLD. Default is 0.8.
47cebab1
GM
4285
4286:weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4287`key-or-value', or `key-and-value'. If WEAK is not nil, the table
4288returned is a weak table. Key/value pairs are removed from a weak
4289hash table when there are no non-weak references pointing to their
4290key, value, one of key or value, or both key and value, depending on
4291WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4bf8e2a3
MB
4292is nil.
4293
4294usage: (make-hash-table &rest KEYWORD-ARGS) */)
5842a27b 4295 (int nargs, Lisp_Object *args)
d80c6c11
GM
4296{
4297 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4298 Lisp_Object user_test, user_hash;
4299 char *used;
4300 int i;
4301
4302 /* The vector `used' is used to keep track of arguments that
4303 have been consumed. */
4304 used = (char *) alloca (nargs * sizeof *used);
72af86bd 4305 memset (used, 0, nargs * sizeof *used);
d80c6c11
GM
4306
4307 /* See if there's a `:test TEST' among the arguments. */
4308 i = get_key_arg (QCtest, nargs, args, used);
4309 test = i < 0 ? Qeql : args[i];
4310 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4311 {
4312 /* See if it is a user-defined test. */
4313 Lisp_Object prop;
59f953a2 4314
d80c6c11 4315 prop = Fget (test, Qhash_table_test);
c1dd95fc 4316 if (!CONSP (prop) || !CONSP (XCDR (prop)))
692ae65c 4317 signal_error ("Invalid hash table test", test);
c1dd95fc
RS
4318 user_test = XCAR (prop);
4319 user_hash = XCAR (XCDR (prop));
d80c6c11
GM
4320 }
4321 else
4322 user_test = user_hash = Qnil;
4323
4324 /* See if there's a `:size SIZE' argument. */
4325 i = get_key_arg (QCsize, nargs, args, used);
cf42cb72
SM
4326 size = i < 0 ? Qnil : args[i];
4327 if (NILP (size))
4328 size = make_number (DEFAULT_HASH_SIZE);
4329 else if (!INTEGERP (size) || XINT (size) < 0)
692ae65c 4330 signal_error ("Invalid hash table size", size);
d80c6c11
GM
4331
4332 /* Look for `:rehash-size SIZE'. */
4333 i = get_key_arg (QCrehash_size, nargs, args, used);
4334 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4335 if (!NUMBERP (rehash_size)
4336 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4337 || XFLOATINT (rehash_size) <= 1.0)
692ae65c 4338 signal_error ("Invalid hash table rehash size", rehash_size);
59f953a2 4339
d80c6c11
GM
4340 /* Look for `:rehash-threshold THRESHOLD'. */
4341 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4342 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4343 if (!FLOATP (rehash_threshold)
4344 || XFLOATINT (rehash_threshold) <= 0.0
4345 || XFLOATINT (rehash_threshold) > 1.0)
692ae65c 4346 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
59f953a2 4347
ee0403b3
GM
4348 /* Look for `:weakness WEAK'. */
4349 i = get_key_arg (QCweakness, nargs, args, used);
d80c6c11 4350 weak = i < 0 ? Qnil : args[i];
ec504e6f
GM
4351 if (EQ (weak, Qt))
4352 weak = Qkey_and_value;
d80c6c11 4353 if (!NILP (weak)
f899c503 4354 && !EQ (weak, Qkey)
ec504e6f
GM
4355 && !EQ (weak, Qvalue)
4356 && !EQ (weak, Qkey_or_value)
4357 && !EQ (weak, Qkey_and_value))
692ae65c 4358 signal_error ("Invalid hash table weakness", weak);
59f953a2 4359
d80c6c11
GM
4360 /* Now, all args should have been used up, or there's a problem. */
4361 for (i = 0; i < nargs; ++i)
4362 if (!used[i])
692ae65c 4363 signal_error ("Invalid argument list", args[i]);
d80c6c11
GM
4364
4365 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4366 user_test, user_hash);
4367}
4368
4369
f899c503 4370DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
e9d8ddc9 4371 doc: /* Return a copy of hash table TABLE. */)
5842a27b 4372 (Lisp_Object table)
f899c503
GM
4373{
4374 return copy_hash_table (check_hash_table (table));
4375}
4376
4377
d80c6c11 4378DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
e9d8ddc9 4379 doc: /* Return the number of elements in TABLE. */)
5842a27b 4380 (Lisp_Object table)
d80c6c11 4381{
878f97ff 4382 return make_number (check_hash_table (table)->count);
d80c6c11
GM
4383}
4384
59f953a2 4385
d80c6c11
GM
4386DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4387 Shash_table_rehash_size, 1, 1, 0,
e9d8ddc9 4388 doc: /* Return the current rehash size of TABLE. */)
5842a27b 4389 (Lisp_Object table)
d80c6c11
GM
4390{
4391 return check_hash_table (table)->rehash_size;
4392}
59f953a2 4393
d80c6c11
GM
4394
4395DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4396 Shash_table_rehash_threshold, 1, 1, 0,
e9d8ddc9 4397 doc: /* Return the current rehash threshold of TABLE. */)
5842a27b 4398 (Lisp_Object table)
d80c6c11
GM
4399{
4400 return check_hash_table (table)->rehash_threshold;
4401}
59f953a2 4402
d80c6c11
GM
4403
4404DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
e9d8ddc9 4405 doc: /* Return the size of TABLE.
47cebab1 4406The size can be used as an argument to `make-hash-table' to create
b756c005 4407a hash table than can hold as many elements as TABLE holds
e9d8ddc9 4408without need for resizing. */)
5842a27b 4409 (Lisp_Object table)
d80c6c11
GM
4410{
4411 struct Lisp_Hash_Table *h = check_hash_table (table);
4412 return make_number (HASH_TABLE_SIZE (h));
4413}
59f953a2 4414
d80c6c11
GM
4415
4416DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
e9d8ddc9 4417 doc: /* Return the test TABLE uses. */)
5842a27b 4418 (Lisp_Object table)
d80c6c11
GM
4419{
4420 return check_hash_table (table)->test;
4421}
4422
59f953a2 4423
e84b1dea
GM
4424DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4425 1, 1, 0,
e9d8ddc9 4426 doc: /* Return the weakness of TABLE. */)
5842a27b 4427 (Lisp_Object table)
d80c6c11
GM
4428{
4429 return check_hash_table (table)->weak;
4430}
4431
59f953a2 4432
d80c6c11 4433DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
e9d8ddc9 4434 doc: /* Return t if OBJ is a Lisp hash table object. */)
5842a27b 4435 (Lisp_Object obj)
d80c6c11
GM
4436{
4437 return HASH_TABLE_P (obj) ? Qt : Qnil;
4438}
4439
4440
4441DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
ccd8f7fe 4442 doc: /* Clear hash table TABLE and return it. */)
5842a27b 4443 (Lisp_Object table)
d80c6c11
GM
4444{
4445 hash_clear (check_hash_table (table));
ccd8f7fe
TTN
4446 /* Be compatible with XEmacs. */
4447 return table;
d80c6c11
GM
4448}
4449
4450
4451DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
e9d8ddc9
MB
4452 doc: /* Look up KEY in TABLE and return its associated value.
4453If KEY is not found, return DFLT which defaults to nil. */)
5842a27b 4454 (Lisp_Object key, Lisp_Object table, Lisp_Object dflt)
d80c6c11
GM
4455{
4456 struct Lisp_Hash_Table *h = check_hash_table (table);
4457 int i = hash_lookup (h, key, NULL);
4458 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4459}
4460
4461
4462DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
e9d8ddc9 4463 doc: /* Associate KEY with VALUE in hash table TABLE.
47cebab1 4464If KEY is already present in table, replace its current value with
e9d8ddc9 4465VALUE. */)
5842a27b 4466 (Lisp_Object key, Lisp_Object value, Lisp_Object table)
d80c6c11
GM
4467{
4468 struct Lisp_Hash_Table *h = check_hash_table (table);
4469 int i;
4470 unsigned hash;
4471
4472 i = hash_lookup (h, key, &hash);
4473 if (i >= 0)
4474 HASH_VALUE (h, i) = value;
4475 else
4476 hash_put (h, key, value, hash);
59f953a2 4477
d9c4f922 4478 return value;
d80c6c11
GM
4479}
4480
4481
4482DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
e9d8ddc9 4483 doc: /* Remove KEY from TABLE. */)
5842a27b 4484 (Lisp_Object key, Lisp_Object table)
d80c6c11
GM
4485{
4486 struct Lisp_Hash_Table *h = check_hash_table (table);
5a2d7ab6 4487 hash_remove_from_table (h, key);
d80c6c11
GM
4488 return Qnil;
4489}
4490
4491
4492DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
e9d8ddc9 4493 doc: /* Call FUNCTION for all entries in hash table TABLE.
c14ec135 4494FUNCTION is called with two arguments, KEY and VALUE. */)
5842a27b 4495 (Lisp_Object function, Lisp_Object table)
d80c6c11
GM
4496{
4497 struct Lisp_Hash_Table *h = check_hash_table (table);
4498 Lisp_Object args[3];
4499 int i;
4500
4501 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4502 if (!NILP (HASH_HASH (h, i)))
4503 {
4504 args[0] = function;
4505 args[1] = HASH_KEY (h, i);
4506 args[2] = HASH_VALUE (h, i);
4507 Ffuncall (3, args);
4508 }
59f953a2 4509
d80c6c11
GM
4510 return Qnil;
4511}
4512
4513
4514DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4515 Sdefine_hash_table_test, 3, 3, 0,
e9d8ddc9 4516 doc: /* Define a new hash table test with name NAME, a symbol.
91f78c99 4517
47cebab1
GM
4518In hash tables created with NAME specified as test, use TEST to
4519compare keys, and HASH for computing hash codes of keys.
4520
4521TEST must be a function taking two arguments and returning non-nil if
4522both arguments are the same. HASH must be a function taking one
4523argument and return an integer that is the hash code of the argument.
4524Hash code computation should use the whole value range of integers,
e9d8ddc9 4525including negative integers. */)
5842a27b 4526 (Lisp_Object name, Lisp_Object test, Lisp_Object hash)
d80c6c11
GM
4527{
4528 return Fput (name, Qhash_table_test, list2 (test, hash));
4529}
4530
a3b210c4 4531
57916a7a 4532\f
5c302da4
GM
4533/************************************************************************
4534 MD5
4535 ************************************************************************/
4536
57916a7a
GM
4537#include "md5.h"
4538
4539DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
e9d8ddc9 4540 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
91f78c99 4541
47cebab1
GM
4542A message digest is a cryptographic checksum of a document, and the
4543algorithm to calculate it is defined in RFC 1321.
4544
4545The two optional arguments START and END are character positions
4546specifying for which part of OBJECT the message digest should be
4547computed. If nil or omitted, the digest is computed for the whole
4548OBJECT.
4549
4550The MD5 message digest is computed from the result of encoding the
4551text in a coding system, not directly from the internal Emacs form of
4552the text. The optional fourth argument CODING-SYSTEM specifies which
4553coding system to encode the text with. It should be the same coding
4554system that you used or will use when actually writing the text into a
4555file.
4556
4557If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4558OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4559system would be chosen by default for writing this text into a file.
4560
4561If OBJECT is a string, the most preferred coding system (see the
4562command `prefer-coding-system') is used.
4563
4564If NOERROR is non-nil, silently assume the `raw-text' coding if the
e9d8ddc9 4565guesswork fails. Normally, an error is signaled in such case. */)
5842a27b 4566 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
57916a7a
GM
4567{
4568 unsigned char digest[16];
4569 unsigned char value[33];
4570 int i;
e6d4aefa
EZ
4571 EMACS_INT size;
4572 EMACS_INT size_byte = 0;
4573 EMACS_INT start_char = 0, end_char = 0;
4574 EMACS_INT start_byte = 0, end_byte = 0;
4575 register EMACS_INT b, e;
57916a7a 4576 register struct buffer *bp;
e6d4aefa 4577 EMACS_INT temp;
57916a7a 4578
5c302da4 4579 if (STRINGP (object))
57916a7a
GM
4580 {
4581 if (NILP (coding_system))
4582 {
5c302da4 4583 /* Decide the coding-system to encode the data with. */
57916a7a 4584
5c302da4
GM
4585 if (STRING_MULTIBYTE (object))
4586 /* use default, we can't guess correct value */
38583a69 4587 coding_system = preferred_coding_system ();
91f78c99 4588 else
5c302da4 4589 coding_system = Qraw_text;
57916a7a 4590 }
91f78c99 4591
5c302da4 4592 if (NILP (Fcoding_system_p (coding_system)))
57916a7a 4593 {
5c302da4 4594 /* Invalid coding system. */
91f78c99 4595
5c302da4
GM
4596 if (!NILP (noerror))
4597 coding_system = Qraw_text;
4598 else
692ae65c 4599 xsignal1 (Qcoding_system_error, coding_system);
57916a7a
GM
4600 }
4601
5c302da4 4602 if (STRING_MULTIBYTE (object))
38583a69 4603 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
5c302da4 4604
d5db4077
KR
4605 size = SCHARS (object);
4606 size_byte = SBYTES (object);
57916a7a
GM
4607
4608 if (!NILP (start))
4609 {
b7826503 4610 CHECK_NUMBER (start);
57916a7a
GM
4611
4612 start_char = XINT (start);
4613
4614 if (start_char < 0)
4615 start_char += size;
4616
4617 start_byte = string_char_to_byte (object, start_char);
4618 }
4619
4620 if (NILP (end))
4621 {
4622 end_char = size;
4623 end_byte = size_byte;
4624 }
4625 else
4626 {
b7826503 4627 CHECK_NUMBER (end);
91f78c99 4628
57916a7a
GM
4629 end_char = XINT (end);
4630
4631 if (end_char < 0)
4632 end_char += size;
91f78c99 4633
57916a7a
GM
4634 end_byte = string_char_to_byte (object, end_char);
4635 }
91f78c99 4636
57916a7a
GM
4637 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
4638 args_out_of_range_3 (object, make_number (start_char),
4639 make_number (end_char));
4640 }
4641 else
4642 {
6b61353c
KH
4643 struct buffer *prev = current_buffer;
4644
4645 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4646
b7826503 4647 CHECK_BUFFER (object);
57916a7a
GM
4648
4649 bp = XBUFFER (object);
6b61353c
KH
4650 if (bp != current_buffer)
4651 set_buffer_internal (bp);
91f78c99 4652
57916a7a 4653 if (NILP (start))
6b61353c 4654 b = BEGV;
57916a7a
GM
4655 else
4656 {
b7826503 4657 CHECK_NUMBER_COERCE_MARKER (start);
57916a7a
GM
4658 b = XINT (start);
4659 }
4660
4661 if (NILP (end))
6b61353c 4662 e = ZV;
57916a7a
GM
4663 else
4664 {
b7826503 4665 CHECK_NUMBER_COERCE_MARKER (end);
57916a7a
GM
4666 e = XINT (end);
4667 }
91f78c99 4668
57916a7a
GM
4669 if (b > e)
4670 temp = b, b = e, e = temp;
91f78c99 4671
6b61353c 4672 if (!(BEGV <= b && e <= ZV))
57916a7a 4673 args_out_of_range (start, end);
91f78c99 4674
57916a7a
GM
4675 if (NILP (coding_system))
4676 {
91f78c99 4677 /* Decide the coding-system to encode the data with.
5c302da4
GM
4678 See fileio.c:Fwrite-region */
4679
4680 if (!NILP (Vcoding_system_for_write))
4681 coding_system = Vcoding_system_for_write;
4682 else
4683 {
4684 int force_raw_text = 0;
4685
4686 coding_system = XBUFFER (object)->buffer_file_coding_system;
4687 if (NILP (coding_system)
4688 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
4689 {
4690 coding_system = Qnil;
4691 if (NILP (current_buffer->enable_multibyte_characters))
4692 force_raw_text = 1;
4693 }
4694
4695 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
4696 {
4697 /* Check file-coding-system-alist. */
4698 Lisp_Object args[4], val;
91f78c99 4699
5c302da4
GM
4700 args[0] = Qwrite_region; args[1] = start; args[2] = end;
4701 args[3] = Fbuffer_file_name(object);
4702 val = Ffind_operation_coding_system (4, args);
4703 if (CONSP (val) && !NILP (XCDR (val)))
4704 coding_system = XCDR (val);
4705 }
4706
4707 if (NILP (coding_system)
4708 && !NILP (XBUFFER (object)->buffer_file_coding_system))
4709 {
4710 /* If we still have not decided a coding system, use the
4711 default value of buffer-file-coding-system. */
4712 coding_system = XBUFFER (object)->buffer_file_coding_system;
4713 }
4714
4715 if (!force_raw_text
4716 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
4717 /* Confirm that VAL can surely encode the current region. */
1e59646d 4718 coding_system = call4 (Vselect_safe_coding_system_function,
70da6a76 4719 make_number (b), make_number (e),
1e59646d 4720 coding_system, Qnil);
5c302da4
GM
4721
4722 if (force_raw_text)
4723 coding_system = Qraw_text;
4724 }
4725
4726 if (NILP (Fcoding_system_p (coding_system)))
57916a7a 4727 {
5c302da4
GM
4728 /* Invalid coding system. */
4729
4730 if (!NILP (noerror))
4731 coding_system = Qraw_text;
4732 else
692ae65c 4733 xsignal1 (Qcoding_system_error, coding_system);
57916a7a
GM
4734 }
4735 }
4736
4737 object = make_buffer_string (b, e, 0);
6b61353c
KH
4738 if (prev != current_buffer)
4739 set_buffer_internal (prev);
4740 /* Discard the unwind protect for recovering the current
4741 buffer. */
4742 specpdl_ptr--;
57916a7a
GM
4743
4744 if (STRING_MULTIBYTE (object))
8f924df7 4745 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
57916a7a
GM
4746 }
4747
42a5b22f 4748 md5_buffer (SSDATA (object) + start_byte,
91f78c99 4749 SBYTES (object) - (size_byte - end_byte),
57916a7a
GM
4750 digest);
4751
4752 for (i = 0; i < 16; i++)
5c302da4 4753 sprintf (&value[2 * i], "%02x", digest[i]);
57916a7a
GM
4754 value[32] = '\0';
4755
4756 return make_string (value, 32);
4757}
4758
24c129e4 4759\f
dfcf069d 4760void
971de7fb 4761syms_of_fns (void)
7b863bd5 4762{
d80c6c11 4763 /* Hash table stuff. */
d67b4f80 4764 Qhash_table_p = intern_c_string ("hash-table-p");
d80c6c11 4765 staticpro (&Qhash_table_p);
d67b4f80 4766 Qeq = intern_c_string ("eq");
d80c6c11 4767 staticpro (&Qeq);
d67b4f80 4768 Qeql = intern_c_string ("eql");
d80c6c11 4769 staticpro (&Qeql);
d67b4f80 4770 Qequal = intern_c_string ("equal");
d80c6c11 4771 staticpro (&Qequal);
d67b4f80 4772 QCtest = intern_c_string (":test");
d80c6c11 4773 staticpro (&QCtest);
d67b4f80 4774 QCsize = intern_c_string (":size");
d80c6c11 4775 staticpro (&QCsize);
d67b4f80 4776 QCrehash_size = intern_c_string (":rehash-size");
d80c6c11 4777 staticpro (&QCrehash_size);
d67b4f80 4778 QCrehash_threshold = intern_c_string (":rehash-threshold");
d80c6c11 4779 staticpro (&QCrehash_threshold);
d67b4f80 4780 QCweakness = intern_c_string (":weakness");
ee0403b3 4781 staticpro (&QCweakness);
d67b4f80 4782 Qkey = intern_c_string ("key");
f899c503 4783 staticpro (&Qkey);
d67b4f80 4784 Qvalue = intern_c_string ("value");
f899c503 4785 staticpro (&Qvalue);
d67b4f80 4786 Qhash_table_test = intern_c_string ("hash-table-test");
d80c6c11 4787 staticpro (&Qhash_table_test);
d67b4f80 4788 Qkey_or_value = intern_c_string ("key-or-value");
ec504e6f 4789 staticpro (&Qkey_or_value);
d67b4f80 4790 Qkey_and_value = intern_c_string ("key-and-value");
ec504e6f 4791 staticpro (&Qkey_and_value);
d80c6c11
GM
4792
4793 defsubr (&Ssxhash);
4794 defsubr (&Smake_hash_table);
f899c503 4795 defsubr (&Scopy_hash_table);
d80c6c11
GM
4796 defsubr (&Shash_table_count);
4797 defsubr (&Shash_table_rehash_size);
4798 defsubr (&Shash_table_rehash_threshold);
4799 defsubr (&Shash_table_size);
4800 defsubr (&Shash_table_test);
e84b1dea 4801 defsubr (&Shash_table_weakness);
d80c6c11
GM
4802 defsubr (&Shash_table_p);
4803 defsubr (&Sclrhash);
4804 defsubr (&Sgethash);
4805 defsubr (&Sputhash);
4806 defsubr (&Sremhash);
4807 defsubr (&Smaphash);
4808 defsubr (&Sdefine_hash_table_test);
59f953a2 4809
d67b4f80 4810 Qstring_lessp = intern_c_string ("string-lessp");
7b863bd5 4811 staticpro (&Qstring_lessp);
d67b4f80 4812 Qprovide = intern_c_string ("provide");
68732608 4813 staticpro (&Qprovide);
d67b4f80 4814 Qrequire = intern_c_string ("require");
68732608 4815 staticpro (&Qrequire);
d67b4f80 4816 Qyes_or_no_p_history = intern_c_string ("yes-or-no-p-history");
0ce830bc 4817 staticpro (&Qyes_or_no_p_history);
d67b4f80 4818 Qcursor_in_echo_area = intern_c_string ("cursor-in-echo-area");
eb4ffa4e 4819 staticpro (&Qcursor_in_echo_area);
d67b4f80 4820 Qwidget_type = intern_c_string ("widget-type");
b4f334f7 4821 staticpro (&Qwidget_type);
7b863bd5 4822
09ab3c3b
KH
4823 staticpro (&string_char_byte_cache_string);
4824 string_char_byte_cache_string = Qnil;
4825
1f79789d
RS
4826 require_nesting_list = Qnil;
4827 staticpro (&require_nesting_list);
4828
52a9879b
RS
4829 Fset (Qyes_or_no_p_history, Qnil);
4830
29208e82 4831 DEFVAR_LISP ("features", Vfeatures,
4774b68e 4832 doc: /* A list of symbols which are the features of the executing Emacs.
47cebab1 4833Used by `featurep' and `require', and altered by `provide'. */);
d67b4f80
DN
4834 Vfeatures = Fcons (intern_c_string ("emacs"), Qnil);
4835 Qsubfeatures = intern_c_string ("subfeatures");
65550192 4836 staticpro (&Qsubfeatures);
7b863bd5 4837
dec002ca 4838#ifdef HAVE_LANGINFO_CODESET
d67b4f80 4839 Qcodeset = intern_c_string ("codeset");
dec002ca 4840 staticpro (&Qcodeset);
d67b4f80 4841 Qdays = intern_c_string ("days");
dec002ca 4842 staticpro (&Qdays);
d67b4f80 4843 Qmonths = intern_c_string ("months");
dec002ca 4844 staticpro (&Qmonths);
d67b4f80 4845 Qpaper = intern_c_string ("paper");
dec002ca
DL
4846 staticpro (&Qpaper);
4847#endif /* HAVE_LANGINFO_CODESET */
4848
29208e82 4849 DEFVAR_BOOL ("use-dialog-box", use_dialog_box,
e9d8ddc9 4850 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
436fa78b 4851This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
7e861e0d
CY
4852invoked by mouse clicks and mouse menu items.
4853
4854On some platforms, file selection dialogs are also enabled if this is
4855non-nil. */);
bdd8d692
RS
4856 use_dialog_box = 1;
4857
29208e82 4858 DEFVAR_BOOL ("use-file-dialog", use_file_dialog,
6b61353c 4859 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
1f1d0797 4860This applies to commands from menus and tool bar buttons even when
2fd0161b
CY
4861they are initiated from the keyboard. If `use-dialog-box' is nil,
4862that disables the use of a file dialog, regardless of the value of
4863this variable. */);
6b61353c
KH
4864 use_file_dialog = 1;
4865
7b863bd5
JB
4866 defsubr (&Sidentity);
4867 defsubr (&Srandom);
4868 defsubr (&Slength);
5a30fab8 4869 defsubr (&Ssafe_length);
026f59ce 4870 defsubr (&Sstring_bytes);
7b863bd5 4871 defsubr (&Sstring_equal);
0e1e9f8d 4872 defsubr (&Scompare_strings);
7b863bd5
JB
4873 defsubr (&Sstring_lessp);
4874 defsubr (&Sappend);
4875 defsubr (&Sconcat);
4876 defsubr (&Svconcat);
4877 defsubr (&Scopy_sequence);
09ab3c3b
KH
4878 defsubr (&Sstring_make_multibyte);
4879 defsubr (&Sstring_make_unibyte);
6d475204
RS
4880 defsubr (&Sstring_as_multibyte);
4881 defsubr (&Sstring_as_unibyte);
2df18cdb 4882 defsubr (&Sstring_to_multibyte);
b4480f16 4883 defsubr (&Sstring_to_unibyte);
7b863bd5
JB
4884 defsubr (&Scopy_alist);
4885 defsubr (&Ssubstring);
aebf4d42 4886 defsubr (&Ssubstring_no_properties);
7b863bd5
JB
4887 defsubr (&Snthcdr);
4888 defsubr (&Snth);
4889 defsubr (&Selt);
4890 defsubr (&Smember);
4891 defsubr (&Smemq);
008ef0ef 4892 defsubr (&Smemql);
7b863bd5
JB
4893 defsubr (&Sassq);
4894 defsubr (&Sassoc);
4895 defsubr (&Srassq);
0fb5a19c 4896 defsubr (&Srassoc);
7b863bd5 4897 defsubr (&Sdelq);
ca8dd546 4898 defsubr (&Sdelete);
7b863bd5
JB
4899 defsubr (&Snreverse);
4900 defsubr (&Sreverse);
4901 defsubr (&Ssort);
be9d483d 4902 defsubr (&Splist_get);
7b863bd5 4903 defsubr (&Sget);
be9d483d 4904 defsubr (&Splist_put);
7b863bd5 4905 defsubr (&Sput);
aebf4d42
RS
4906 defsubr (&Slax_plist_get);
4907 defsubr (&Slax_plist_put);
95f8c3b9 4908 defsubr (&Seql);
7b863bd5 4909 defsubr (&Sequal);
6b61353c 4910 defsubr (&Sequal_including_properties);
7b863bd5 4911 defsubr (&Sfillarray);
85cad579 4912 defsubr (&Sclear_string);
7b863bd5
JB
4913 defsubr (&Snconc);
4914 defsubr (&Smapcar);
5c6740c9 4915 defsubr (&Smapc);
7b863bd5 4916 defsubr (&Smapconcat);
7b863bd5
JB
4917 defsubr (&Syes_or_no_p);
4918 defsubr (&Sload_average);
4919 defsubr (&Sfeaturep);
4920 defsubr (&Srequire);
4921 defsubr (&Sprovide);
a5254817 4922 defsubr (&Splist_member);
b4f334f7
KH
4923 defsubr (&Swidget_put);
4924 defsubr (&Swidget_get);
4925 defsubr (&Swidget_apply);
24c129e4
KH
4926 defsubr (&Sbase64_encode_region);
4927 defsubr (&Sbase64_decode_region);
4928 defsubr (&Sbase64_encode_string);
4929 defsubr (&Sbase64_decode_string);
57916a7a 4930 defsubr (&Smd5);
d68beb2f 4931 defsubr (&Slocale_info);
7b863bd5 4932}
d80c6c11
GM
4933
4934
4935void
971de7fb 4936init_fns (void)
d80c6c11 4937{
d80c6c11 4938}