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