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