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