better load-path defaults
[bpt/emacs.git] / src / lread.c
CommitLineData
078e7b4a 1/* Lisp parsing and input streams.
039c6cc3 2
ba318903
PE
3Copyright (C) 1985-1989, 1993-1995, 1997-2014 Free Software Foundation,
4Inc.
078e7b4a
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
078e7b4a 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
078e7b4a
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
078e7b4a
JB
20
21
98280b76 22#include <config.h>
406af475 23#include "sysstdio.h"
078e7b4a
JB
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/file.h>
2c1b5dbe 27#include <errno.h>
9660f5fc 28#include <limits.h> /* For CHAR_BIT. */
39adff0d 29#include <stat-time.h>
078e7b4a 30#include "lisp.h"
95384e1e 31#include "intervals.h"
1571601b 32#include "character.h"
e5560ff7 33#include "buffer.h"
fe0e03f3 34#include "charset.h"
8792be66 35#include "coding.h"
57bda87a 36#include <epaths.h>
078e7b4a 37#include "commands.h"
e37c0805 38#include "keyboard.h"
428a555e 39#include "frame.h"
7bd279cd 40#include "termhooks.h"
66cd0949 41#include "blockinput.h"
fa4d3890 42#include "guile.h"
078e7b4a 43
79051982 44#ifdef MSDOS
79051982
RS
45#include "msdos.h"
46#endif
47
9e059e3f
GM
48#ifdef HAVE_NS
49#include "nsterm.h"
50#endif
51
c1a2f60a 52#include <unistd.h>
078e7b4a 53
c011e9a5
RS
54#ifdef HAVE_SETLOCALE
55#include <locale.h>
56#endif /* HAVE_SETLOCALE */
57
bcdec28b 58#include <fcntl.h>
f7d279f0 59
79cf851c 60#ifdef HAVE_FSEEKO
68c45bf0
PE
61#define file_offset off_t
62#define file_tell ftello
63#else
64#define file_offset long
65#define file_tell ftell
66#endif
67
d23d13c0
BT
68static SCM obarrays;
69
48def666 70/* Hash table read constants. */
955cbe7b
PE
71static Lisp_Object Qhash_table, Qdata;
72static Lisp_Object Qtest, Qsize;
73static Lisp_Object Qweakness;
74static Lisp_Object Qrehash_size;
75static Lisp_Object Qrehash_threshold;
76
77static Lisp_Object Qread_char, Qget_file_char, Qcurrent_load_list;
78Lisp_Object Qstandard_input;
29208e82 79Lisp_Object Qvariable_documentation;
955cbe7b 80static Lisp_Object Qascii_character, Qload, Qload_file_name;
2b6cae0c 81Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
955cbe7b
PE
82static Lisp_Object Qinhibit_file_name_operation;
83static Lisp_Object Qeval_buffer_list;
8be3a09c 84Lisp_Object Qlexical_binding;
955cbe7b 85static Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
7bd279cd 86
8792be66
KH
87/* Used instead of Qget_file_char while loading *.elc files compiled
88 by Emacs 21 or older. */
89static Lisp_Object Qget_emacs_mule_file_char;
90
91static Lisp_Object Qload_force_doc_strings;
92
2baf5e76 93static Lisp_Object Qload_in_progress;
ece435a5 94
4ad679f9
EN
95/* The association list of objects read with the #n=object form.
96 Each member of the list has the form (n . object), and is used to
97 look up the object for the corresponding #n# construct.
98 It must be set to nil before all top-level calls to read0. */
1e3890d1 99static Lisp_Object read_objects;
4ad679f9 100
b2a30870 101/* File for get_file_char to read from. Use by load. */
078e7b4a
JB
102static FILE *instream;
103
b2a30870 104/* For use within read-from-string (this reader is non-reentrant!!) */
d311d28c
PE
105static ptrdiff_t read_from_string_index;
106static ptrdiff_t read_from_string_index_byte;
107static ptrdiff_t read_from_string_limit;
17634846 108
abb13b09 109/* Number of characters read in the current call to Fread or
48def666 110 Fread_from_string. */
da43f021 111static EMACS_INT readchar_count;
6f7f43d5 112
c15cfd1f 113/* This contains the last string skipped with #@. */
b2a30870
RS
114static char *saved_doc_string;
115/* Length of buffer allocated in saved_doc_string. */
bfbbd7e7 116static ptrdiff_t saved_doc_string_size;
b2a30870 117/* Length of actual data in saved_doc_string. */
bfbbd7e7 118static ptrdiff_t saved_doc_string_length;
b2a30870 119/* This is the file position that string came from. */
68c45bf0 120static file_offset saved_doc_string_position;
b2a30870 121
c15cfd1f
RS
122/* This contains the previous string skipped with #@.
123 We copy it from saved_doc_string when a new string
124 is put in saved_doc_string. */
125static char *prev_saved_doc_string;
126/* Length of buffer allocated in prev_saved_doc_string. */
bfbbd7e7 127static ptrdiff_t prev_saved_doc_string_size;
c15cfd1f 128/* Length of actual data in prev_saved_doc_string. */
bfbbd7e7 129static ptrdiff_t prev_saved_doc_string_length;
c15cfd1f 130/* This is the file position that string came from. */
68c45bf0 131static file_offset prev_saved_doc_string_position;
c15cfd1f 132
7cded46f 133/* True means inside a new-style backquote
17634846 134 with no surrounding parentheses.
7cded46f 135 Fread initializes this to false, so we need not specbind it
17634846 136 or worry about what happens to it when there is an error. */
7cded46f 137static bool new_backquote_flag;
29208e82 138static Lisp_Object Qold_style_backquotes;
7ee3bd7b
GM
139
140/* A list of file names for files being loaded in Fload. Used to
141 check for recursive loads. */
142
9942fa0c 143static Lisp_Object Vloads_in_progress;
7ee3bd7b 144
f57e2426
J
145static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
146 Lisp_Object);
8792be66 147
7cded46f 148static void readevalloop (Lisp_Object, FILE *, Lisp_Object, bool,
f57e2426
J
149 Lisp_Object, Lisp_Object,
150 Lisp_Object, Lisp_Object);
078e7b4a 151\f
8792be66
KH
152/* Functions that read one byte from the current source READCHARFUN
153 or unreads one byte. If the integer argument C is -1, it returns
154 one read byte, or -1 when there's no more byte in the source. If C
155 is 0 or positive, it unreads C, and the return value is not
156 interesting. */
157
f57e2426
J
158static int readbyte_for_lambda (int, Lisp_Object);
159static int readbyte_from_file (int, Lisp_Object);
160static int readbyte_from_string (int, Lisp_Object);
8792be66 161
078e7b4a
JB
162/* Handle unreading and rereading of characters.
163 Write READCHAR to read a character,
fe0e03f3
KH
164 UNREAD(c) to unread c to be read again.
165
8792be66 166 These macros correctly read/unread multibyte characters. */
078e7b4a 167
1202434b 168#define READCHAR readchar (readcharfun, NULL)
078e7b4a
JB
169#define UNREAD(c) unreadchar (readcharfun, c)
170
1202434b
KH
171/* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
172#define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
173
8792be66 174/* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
8f924df7
KH
175 Qlambda, or a cons, we use this to keep an unread character because
176 a file stream can't handle multibyte-char unreading. The value -1
48def666 177 means that there's no unread character. */
8792be66
KH
178static int unread_char;
179
078e7b4a 180static int
fce31d69 181readchar (Lisp_Object readcharfun, bool *multibyte)
078e7b4a
JB
182{
183 Lisp_Object tem;
95384e1e 184 register int c;
f57e2426 185 int (*readbyte) (int, Lisp_Object);
8792be66
KH
186 unsigned char buf[MAX_MULTIBYTE_LENGTH];
187 int i, len;
7cded46f 188 bool emacs_mule_encoding = 0;
078e7b4a 189
1202434b
KH
190 if (multibyte)
191 *multibyte = 0;
192
abb13b09 193 readchar_count++;
177c0ea7 194
cfff016d 195 if (BUFFERP (readcharfun))
078e7b4a 196 {
bed23cb2 197 register struct buffer *inbuffer = XBUFFER (readcharfun);
078e7b4a 198
d311d28c 199 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
00a9a935 200
15acfe41
GM
201 if (! BUFFER_LIVE_P (inbuffer))
202 return -1;
203
bed23cb2
RS
204 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
205 return -1;
078e7b4a 206
4b4deea2 207 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
bed23cb2 208 {
00a9a935 209 /* Fetch the character code from the buffer. */
bed23cb2
RS
210 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
211 BUF_INC_POS (inbuffer, pt_byte);
62a6e103 212 c = STRING_CHAR (p);
1202434b
KH
213 if (multibyte)
214 *multibyte = 1;
bed23cb2
RS
215 }
216 else
217 {
218 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
200fc949 219 if (! ASCII_CHAR_P (c))
8792be66 220 c = BYTE8_TO_CHAR (c);
bed23cb2 221 pt_byte++;
6f7f43d5 222 }
bed23cb2 223 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
6f7f43d5 224
bed23cb2 225 return c;
078e7b4a 226 }
cfff016d 227 if (MARKERP (readcharfun))
078e7b4a 228 {
bed23cb2 229 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
078e7b4a 230
d311d28c 231 ptrdiff_t bytepos = marker_byte_position (readcharfun);
00a9a935 232
bed23cb2
RS
233 if (bytepos >= BUF_ZV_BYTE (inbuffer))
234 return -1;
6f7f43d5 235
4b4deea2 236 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
bed23cb2 237 {
00a9a935 238 /* Fetch the character code from the buffer. */
bed23cb2
RS
239 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
240 BUF_INC_POS (inbuffer, bytepos);
62a6e103 241 c = STRING_CHAR (p);
1202434b
KH
242 if (multibyte)
243 *multibyte = 1;
bed23cb2
RS
244 }
245 else
246 {
247 c = BUF_FETCH_BYTE (inbuffer, bytepos);
200fc949 248 if (! ASCII_CHAR_P (c))
8792be66 249 c = BYTE8_TO_CHAR (c);
bed23cb2 250 bytepos++;
6f7f43d5
RS
251 }
252
bed23cb2
RS
253 XMARKER (readcharfun)->bytepos = bytepos;
254 XMARKER (readcharfun)->charpos++;
255
256 return c;
078e7b4a 257 }
ad0153e4
RS
258
259 if (EQ (readcharfun, Qlambda))
8792be66
KH
260 {
261 readbyte = readbyte_for_lambda;
262 goto read_multibyte;
263 }
ad0153e4 264
078e7b4a 265 if (EQ (readcharfun, Qget_file_char))
2c1b5dbe 266 {
8792be66
KH
267 readbyte = readbyte_from_file;
268 goto read_multibyte;
2c1b5dbe 269 }
078e7b4a 270
cfff016d 271 if (STRINGP (readcharfun))
078e7b4a 272 {
bed23cb2 273 if (read_from_string_index >= read_from_string_limit)
078e7b4a 274 c = -1;
1202434b
KH
275 else if (STRING_MULTIBYTE (readcharfun))
276 {
277 if (multibyte)
278 *multibyte = 1;
279 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
280 read_from_string_index,
281 read_from_string_index_byte);
282 }
f0354c03 283 else
1202434b
KH
284 {
285 c = SREF (readcharfun, read_from_string_index_byte);
286 read_from_string_index++;
287 read_from_string_index_byte++;
288 }
078e7b4a
JB
289 return c;
290 }
291
8792be66
KH
292 if (CONSP (readcharfun))
293 {
294 /* This is the case that read_vector is reading from a unibyte
295 string that contains a byte sequence previously skipped
296 because of #@NUMBER. The car part of readcharfun is that
297 string, and the cdr part is a value of readcharfun given to
298 read_vector. */
299 readbyte = readbyte_from_string;
300 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
301 emacs_mule_encoding = 1;
302 goto read_multibyte;
303 }
8f924df7 304
8792be66
KH
305 if (EQ (readcharfun, Qget_emacs_mule_file_char))
306 {
307 readbyte = readbyte_from_file;
308 emacs_mule_encoding = 1;
309 goto read_multibyte;
310 }
078e7b4a
JB
311
312 tem = call0 (readcharfun);
313
265a9e55 314 if (NILP (tem))
078e7b4a
JB
315 return -1;
316 return XINT (tem);
8792be66
KH
317
318 read_multibyte:
319 if (unread_char >= 0)
320 {
321 c = unread_char;
322 unread_char = -1;
323 return c;
324 }
325 c = (*readbyte) (-1, readcharfun);
75a65c7e 326 if (c < 0)
1202434b
KH
327 return c;
328 if (multibyte)
329 *multibyte = 1;
200fc949 330 if (ASCII_CHAR_P (c))
8792be66
KH
331 return c;
332 if (emacs_mule_encoding)
333 return read_emacs_mule_char (c, readbyte, readcharfun);
334 i = 0;
335 buf[i++] = c;
336 len = BYTES_BY_CHAR_HEAD (c);
337 while (i < len)
338 {
339 c = (*readbyte) (-1, readcharfun);
340 if (c < 0 || ! TRAILING_CODE_P (c))
341 {
342 while (--i > 1)
343 (*readbyte) (buf[i], readcharfun);
344 return BYTE8_TO_CHAR (buf[0]);
345 }
346 buf[i++] = c;
347 }
62a6e103 348 return STRING_CHAR (buf);
078e7b4a
JB
349}
350
d2e24f92
SM
351#define FROM_FILE_P(readcharfun) \
352 (EQ (readcharfun, Qget_file_char) \
353 || EQ (readcharfun, Qget_emacs_mule_file_char))
354
75a65c7e
SM
355static void
356skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
357{
d2e24f92 358 if (FROM_FILE_P (readcharfun))
75a65c7e
SM
359 {
360 block_input (); /* FIXME: Not sure if it's needed. */
361 fseek (instream, n, SEEK_CUR);
362 unblock_input ();
363 }
364 else
365 { /* We're not reading directly from a file. In that case, it's difficult
366 to reliably count bytes, since these are usually meant for the file's
367 encoding, whereas we're now typically in the internal encoding.
368 But luckily, skip_dyn_bytes is used to skip over a single
369 dynamic-docstring (or dynamic byte-code) which is always quoted such
370 that \037 is the final char. */
371 int c;
372 do {
373 c = READCHAR;
374 } while (c >= 0 && c != '\037');
375 }
376}
377
759fd763
SM
378static void
379skip_dyn_eof (Lisp_Object readcharfun)
380{
381 if (FROM_FILE_P (readcharfun))
382 {
383 block_input (); /* FIXME: Not sure if it's needed. */
384 fseek (instream, 0, SEEK_END);
385 unblock_input ();
386 }
387 else
388 while (READCHAR >= 0);
389}
390
078e7b4a
JB
391/* Unread the character C in the way appropriate for the stream READCHARFUN.
392 If the stream is a user function, call it with the char as argument. */
393
394static void
971de7fb 395unreadchar (Lisp_Object readcharfun, int c)
078e7b4a 396{
abb13b09 397 readchar_count--;
92fddec9
KH
398 if (c == -1)
399 /* Don't back up the pointer if we're unreading the end-of-input mark,
400 since readchar didn't advance it when we read it. */
401 ;
cfff016d 402 else if (BUFFERP (readcharfun))
d7760ca9 403 {
bed23cb2 404 struct buffer *b = XBUFFER (readcharfun);
d311d28c
PE
405 ptrdiff_t charpos = BUF_PT (b);
406 ptrdiff_t bytepos = BUF_PT_BYTE (b);
d7760ca9 407
4b4deea2 408 if (! NILP (BVAR (b, enable_multibyte_characters)))
8792be66 409 BUF_DEC_POS (b, bytepos);
bed23cb2 410 else
8792be66 411 bytepos--;
d7760ca9 412
cffc6f3b 413 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
d7760ca9 414 }
cfff016d 415 else if (MARKERP (readcharfun))
d7760ca9 416 {
bed23cb2 417 struct buffer *b = XMARKER (readcharfun)->buffer;
d311d28c 418 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
d7760ca9 419
8792be66 420 XMARKER (readcharfun)->charpos--;
4b4deea2 421 if (! NILP (BVAR (b, enable_multibyte_characters)))
8792be66 422 BUF_DEC_POS (b, bytepos);
bed23cb2 423 else
8792be66 424 bytepos--;
d7760ca9 425
8792be66 426 XMARKER (readcharfun)->bytepos = bytepos;
d7760ca9 427 }
cfff016d 428 else if (STRINGP (readcharfun))
bed23cb2
RS
429 {
430 read_from_string_index--;
431 read_from_string_index_byte
432 = string_char_to_byte (readcharfun, read_from_string_index);
433 }
8792be66
KH
434 else if (CONSP (readcharfun))
435 {
436 unread_char = c;
437 }
ad0153e4 438 else if (EQ (readcharfun, Qlambda))
8792be66
KH
439 {
440 unread_char = c;
441 }
d2e24f92 442 else if (FROM_FILE_P (readcharfun))
8792be66 443 {
75a65c7e 444 unread_char = c;
8792be66 445 }
078e7b4a
JB
446 else
447 call1 (readcharfun, make_number (c));
448}
449
8792be66 450static int
971de7fb 451readbyte_for_lambda (int c, Lisp_Object readcharfun)
8792be66
KH
452{
453 return read_bytecode_char (c >= 0);
454}
455
456
457static int
971de7fb 458readbyte_from_file (int c, Lisp_Object readcharfun)
8792be66
KH
459{
460 if (c >= 0)
461 {
4d7e6e51 462 block_input ();
8792be66 463 ungetc (c, instream);
4d7e6e51 464 unblock_input ();
8792be66
KH
465 return 0;
466 }
467
4d7e6e51 468 block_input ();
8792be66 469 c = getc (instream);
d85d3839 470
48def666 471 /* Interrupted reads have been observed while reading over the network. */
d85d3839
MB
472 while (c == EOF && ferror (instream) && errno == EINTR)
473 {
4d7e6e51 474 unblock_input ();
d85d3839 475 QUIT;
4d7e6e51 476 block_input ();
57cb2e6f 477 clearerr (instream);
d85d3839 478 c = getc (instream);
66cd0949 479 }
d85d3839 480
4d7e6e51 481 unblock_input ();
57cb2e6f 482
8792be66
KH
483 return (c == EOF ? -1 : c);
484}
485
486static int
971de7fb 487readbyte_from_string (int c, Lisp_Object readcharfun)
8792be66
KH
488{
489 Lisp_Object string = XCAR (readcharfun);
490
491 if (c >= 0)
492 {
493 read_from_string_index--;
494 read_from_string_index_byte
495 = string_char_to_byte (string, read_from_string_index);
496 }
8f924df7 497
8792be66
KH
498 if (read_from_string_index >= read_from_string_limit)
499 c = -1;
500 else
501 FETCH_STRING_CHAR_ADVANCE (c, string,
502 read_from_string_index,
503 read_from_string_index_byte);
504 return c;
505}
506
507
508/* Read one non-ASCII character from INSTREAM. The character is
509 encoded in `emacs-mule' and the first byte is already read in
510 C. */
511
8792be66 512static int
971de7fb 513read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
8792be66
KH
514{
515 /* Emacs-mule coding uses at most 4-byte for one character. */
516 unsigned char buf[4];
517 int len = emacs_mule_bytes[c];
518 struct charset *charset;
519 int i;
520 unsigned code;
521
522 if (len == 1)
523 /* C is not a valid leading-code of `emacs-mule'. */
524 return BYTE8_TO_CHAR (c);
525
526 i = 0;
527 buf[i++] = c;
528 while (i < len)
529 {
530 c = (*readbyte) (-1, readcharfun);
531 if (c < 0xA0)
532 {
533 while (--i > 1)
534 (*readbyte) (buf[i], readcharfun);
535 return BYTE8_TO_CHAR (buf[0]);
536 }
537 buf[i++] = c;
538 }
539
540 if (len == 2)
541 {
b84ae584 542 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
8792be66
KH
543 code = buf[1] & 0x7F;
544 }
545 else if (len == 3)
546 {
547 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
548 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
549 {
b84ae584 550 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
8792be66
KH
551 code = buf[2] & 0x7F;
552 }
553 else
554 {
b84ae584 555 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
8792be66
KH
556 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
557 }
558 }
559 else
560 {
b84ae584 561 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
8f924df7 562 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
8792be66
KH
563 }
564 c = DECODE_CHAR (charset, code);
565 if (c < 0)
566 Fsignal (Qinvalid_read_syntax,
6c6f1994 567 list1 (build_string ("invalid multibyte form")));
8792be66
KH
568 return c;
569}
570
571
f57e2426
J
572static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
573 Lisp_Object);
574static Lisp_Object read0 (Lisp_Object);
7cded46f 575static Lisp_Object read1 (Lisp_Object, int *, bool);
abb13b09 576
7cded46f
PE
577static Lisp_Object read_list (bool, Lisp_Object);
578static Lisp_Object read_vector (Lisp_Object, bool);
abb13b09 579
f57e2426
J
580static Lisp_Object substitute_object_recurse (Lisp_Object, Lisp_Object,
581 Lisp_Object);
582static void substitute_object_in_subtree (Lisp_Object,
583 Lisp_Object);
584static void substitute_in_interval (INTERVAL, Lisp_Object);
9e062b6c 585
078e7b4a 586\f
bed23cb2 587/* Get a character from the tty. */
078e7b4a 588
f42be754
JB
589/* Read input events until we get one that's acceptable for our purposes.
590
7cded46f 591 If NO_SWITCH_FRAME, switch-frame events are stashed
f42be754
JB
592 until we get a character we like, and then stuffed into
593 unread_switch_frame.
594
7cded46f 595 If ASCII_REQUIRED, check function key events to see
f42be754
JB
596 if the unmodified version of the symbol has a Qascii_character
597 property, and use that character, if present.
598
7cded46f
PE
599 If ERROR_NONASCII, signal an error if the input we
600 get isn't an ASCII character with modifiers. If it's false but
601 ASCII_REQUIRED is true, just re-read until we get an ASCII
bb49a192
RS
602 character.
603
7cded46f 604 If INPUT_METHOD, invoke the current input method
50e48299
CY
605 if the character warrants that.
606
7cded46f 607 If SECONDS is a number, wait that many seconds for input, and
50e48299 608 return Qnil if no input arrives within that time. */
cc39bc38 609
2f7c71a1 610static Lisp_Object
7cded46f
PE
611read_filtered_event (bool no_switch_frame, bool ascii_required,
612 bool error_nonascii, bool input_method, Lisp_Object seconds)
f42be754 613{
4332cf50 614 Lisp_Object val, delayed_switch_frame;
43aac990 615 struct timespec end_time;
153a17b7 616
5dbf89bc 617#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
618 if (display_hourglass_p)
619 cancel_hourglass ();
5dbf89bc 620#endif
177c0ea7 621
153a17b7 622 delayed_switch_frame = Qnil;
f42be754 623
50e48299
CY
624 /* Compute timeout. */
625 if (NUMBERP (seconds))
626 {
51d8b30e 627 double duration = extract_float (seconds);
43aac990
PE
628 struct timespec wait_time = dtotimespec (duration);
629 end_time = timespec_add (current_timespec (), wait_time);
50e48299
CY
630 }
631
99d0d6dc 632 /* Read until we get an acceptable event. */
f42be754 633 retry:
88852d45 634 do
99d0d6dc 635 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
251bc578 636 NUMBERP (seconds) ? &end_time : NULL);
9688ff53 637 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
f42be754 638
cfff016d 639 if (BUFFERP (val))
6c82d689
KH
640 goto retry;
641
99d0d6dc 642 /* `switch-frame' events are put off until after the next ASCII
8e6208c5 643 character. This is better than signaling an error just because
f42be754
JB
644 the last characters were typed to a separate minibuffer frame,
645 for example. Eventually, some code which can deal with
646 switch-frame events will read it and process it. */
647 if (no_switch_frame
648 && EVENT_HAS_PARAMETERS (val)
ca77ee45 649 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
f42be754
JB
650 {
651 delayed_switch_frame = val;
652 goto retry;
653 }
654
50e48299 655 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
f42be754
JB
656 {
657 /* Convert certain symbols to their ASCII equivalents. */
cfff016d 658 if (SYMBOLP (val))
f42be754 659 {
95384e1e 660 Lisp_Object tem, tem1;
f42be754
JB
661 tem = Fget (val, Qevent_symbol_element_mask);
662 if (!NILP (tem))
663 {
664 tem1 = Fget (Fcar (tem), Qascii_character);
665 /* Merge this symbol's modifier bits
666 with the ASCII equivalent of its basic code. */
667 if (!NILP (tem1))
baf69866 668 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
f42be754
JB
669 }
670 }
177c0ea7 671
f42be754 672 /* If we don't have a character now, deal with it appropriately. */
cfff016d 673 if (!INTEGERP (val))
f42be754
JB
674 {
675 if (error_nonascii)
676 {
6c6f1994 677 Vunread_command_events = list1 (val);
f42be754
JB
678 error ("Non-character input-event");
679 }
680 else
681 goto retry;
682 }
683 }
684
685 if (! NILP (delayed_switch_frame))
686 unread_switch_frame = delayed_switch_frame;
687
67868d27
PJ
688#if 0
689
5dbf89bc 690#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
691 if (display_hourglass_p)
692 start_hourglass ();
5dbf89bc 693#endif
67868d27
PJ
694
695#endif
696
f42be754 697 return val;
f42be754
JB
698}
699
a7ca3326 700DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
5de38842
PJ
701 doc: /* Read a character from the command input (keyboard or macro).
702It is returned as a number.
fb26c2af
KH
703If the character has modifiers, they are resolved and reflected to the
704character code if possible (e.g. C-SPC -> 0).
705
5de38842
PJ
706If the user generates an event which is not a character (i.e. a mouse
707click or function key event), `read-char' signals an error. As an
fb26c2af
KH
708exception, switch-frame events are put off until non-character events
709can be read.
5de38842
PJ
710If you want to read non-character events, or ignore them, call
711`read-event' or `read-char-exclusive' instead.
712
713If the optional argument PROMPT is non-nil, display that as a prompt.
714If the optional argument INHERIT-INPUT-METHOD is non-nil and some
715input method is turned on in the current buffer, that input method
50e48299
CY
716is used for reading a character.
717If the optional argument SECONDS is non-nil, it should be a number
718specifying the maximum number of seconds to wait for input. If no
719input arrives in that time, return nil. SECONDS may be a
720floating-point value. */)
5842a27b 721 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
078e7b4a 722{
fb26c2af 723 Lisp_Object val;
fb26c2af 724
bb49a192
RS
725 if (! NILP (prompt))
726 message_with_string ("%s", prompt, 0);
fb26c2af 727 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
66c410ce
CY
728
729 return (NILP (val) ? Qnil
730 : make_number (char_resolve_modifier_mask (XINT (val))));
078e7b4a
JB
731}
732
a7ca3326 733DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
5de38842
PJ
734 doc: /* Read an event object from the input stream.
735If the optional argument PROMPT is non-nil, display that as a prompt.
736If the optional argument INHERIT-INPUT-METHOD is non-nil and some
737input method is turned on in the current buffer, that input method
50e48299
CY
738is used for reading a character.
739If the optional argument SECONDS is non-nil, it should be a number
740specifying the maximum number of seconds to wait for input. If no
741input arrives in that time, return nil. SECONDS may be a
742floating-point value. */)
5842a27b 743 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
078e7b4a 744{
bb49a192
RS
745 if (! NILP (prompt))
746 message_with_string ("%s", prompt, 0);
50e48299 747 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
078e7b4a
JB
748}
749
50e48299 750DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
5de38842
PJ
751 doc: /* Read a character from the command input (keyboard or macro).
752It is returned as a number. Non-character events are ignored.
fb26c2af
KH
753If the character has modifiers, they are resolved and reflected to the
754character code if possible (e.g. C-SPC -> 0).
5de38842
PJ
755
756If the optional argument PROMPT is non-nil, display that as a prompt.
757If the optional argument INHERIT-INPUT-METHOD is non-nil and some
758input method is turned on in the current buffer, that input method
50e48299
CY
759is used for reading a character.
760If the optional argument SECONDS is non-nil, it should be a number
761specifying the maximum number of seconds to wait for input. If no
762input arrives in that time, return nil. SECONDS may be a
763floating-point value. */)
5842a27b 764 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
078e7b4a 765{
fb26c2af 766 Lisp_Object val;
fb26c2af 767
bb49a192
RS
768 if (! NILP (prompt))
769 message_with_string ("%s", prompt, 0);
66c410ce 770
fb26c2af 771 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
66c410ce
CY
772
773 return (NILP (val) ? Qnil
774 : make_number (char_resolve_modifier_mask (XINT (val))));
078e7b4a 775}
078e7b4a
JB
776
777DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
5de38842 778 doc: /* Don't use this yourself. */)
5842a27b 779 (void)
078e7b4a
JB
780{
781 register Lisp_Object val;
4d7e6e51 782 block_input ();
1805de4f 783 XSETINT (val, getc (instream));
4d7e6e51 784 unblock_input ();
078e7b4a
JB
785 return val;
786}
da84f340
GM
787
788
550a625e 789\f
b9598260
SM
790
791/* Return true if the lisp code read using READCHARFUN defines a non-nil
792 `lexical-binding' file variable. After returning, the stream is
757140ff
GM
793 positioned following the first line, if it is a comment or #! line,
794 otherwise nothing is read. */
b9598260 795
7cded46f 796static bool
c566235d 797lisp_file_lexically_bound_p (Lisp_Object readcharfun)
b9598260
SM
798{
799 int ch = READCHAR;
757140ff
GM
800
801 if (ch == '#')
802 {
803 ch = READCHAR;
804 if (ch != '!')
805 {
806 UNREAD (ch);
807 UNREAD ('#');
808 return 0;
809 }
810 while (ch != '\n' && ch != EOF)
811 ch = READCHAR;
812 if (ch == '\n') ch = READCHAR;
d2dd3694
GM
813 /* It is OK to leave the position after a #! line, since
814 that is what read1 does. */
757140ff
GM
815 }
816
b9598260
SM
817 if (ch != ';')
818 /* The first line isn't a comment, just give up. */
819 {
820 UNREAD (ch);
821 return 0;
822 }
823 else
824 /* Look for an appropriate file-variable in the first line. */
825 {
7cded46f 826 bool rv = 0;
b9598260 827 enum {
29abe551 828 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX
b9598260 829 } beg_end_state = NOMINAL;
7cded46f 830 bool in_file_vars = 0;
b9598260 831
7200d79c
SM
832#define UPDATE_BEG_END_STATE(ch) \
833 if (beg_end_state == NOMINAL) \
834 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
835 else if (beg_end_state == AFTER_FIRST_DASH) \
836 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
837 else if (beg_end_state == AFTER_ASTERIX) \
838 { \
839 if (ch == '-') \
840 in_file_vars = !in_file_vars; \
841 beg_end_state = NOMINAL; \
b9598260
SM
842 }
843
844 /* Skip until we get to the file vars, if any. */
845 do
846 {
847 ch = READCHAR;
848 UPDATE_BEG_END_STATE (ch);
849 }
850 while (!in_file_vars && ch != '\n' && ch != EOF);
851
852 while (in_file_vars)
853 {
06a0259a 854 char var[100], val[100];
3c346cc3 855 unsigned i;
b9598260
SM
856
857 ch = READCHAR;
858
859 /* Read a variable name. */
860 while (ch == ' ' || ch == '\t')
861 ch = READCHAR;
862
06a0259a 863 i = 0;
db7a0b4f 864 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
b9598260 865 {
06a0259a
PE
866 if (i < sizeof var - 1)
867 var[i++] = ch;
b9598260
SM
868 UPDATE_BEG_END_STATE (ch);
869 ch = READCHAR;
870 }
7200d79c 871
db7a0b4f 872 /* Stop scanning if no colon was found before end marker. */
ad3a2b41 873 if (!in_file_vars || ch == '\n' || ch == EOF)
db7a0b4f
AS
874 break;
875
06a0259a
PE
876 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
877 i--;
878 var[i] = '\0';
b9598260
SM
879
880 if (ch == ':')
881 {
882 /* Read a variable value. */
883 ch = READCHAR;
884
885 while (ch == ' ' || ch == '\t')
886 ch = READCHAR;
887
06a0259a 888 i = 0;
b9598260
SM
889 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
890 {
06a0259a
PE
891 if (i < sizeof val - 1)
892 val[i++] = ch;
b9598260
SM
893 UPDATE_BEG_END_STATE (ch);
894 ch = READCHAR;
895 }
896 if (! in_file_vars)
ad3a2b41 897 /* The value was terminated by an end-marker, which remove. */
06a0259a
PE
898 i -= 3;
899 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
900 i--;
901 val[i] = '\0';
b9598260
SM
902
903 if (strcmp (var, "lexical-binding") == 0)
904 /* This is it... */
905 {
906 rv = (strcmp (val, "nil") != 0);
907 break;
908 }
909 }
910 }
911
912 while (ch != '\n' && ch != EOF)
913 ch = READCHAR;
914
915 return rv;
916 }
917}
b9598260 918\f
8792be66 919/* Value is a version number of byte compiled code if the file
c73bd236 920 associated with file descriptor FD is a compiled Lisp file that's
8792be66
KH
921 safe to load. Only files compiled with Emacs are safe to load.
922 Files compiled with XEmacs can lead to a crash in Fbyte_code
923 because of an incompatible change in the byte compiler. */
da84f340
GM
924
925static int
7cded46f 926safe_to_load_version (int fd)
da84f340
GM
927{
928 char buf[512];
929 int nbytes, i;
8f924df7 930 int version = 1;
da84f340
GM
931
932 /* Read the first few bytes from the file, and look for a line
933 specifying the byte compiler version used. */
7cded46f 934 nbytes = emacs_read (fd, buf, sizeof buf);
da84f340
GM
935 if (nbytes > 0)
936 {
da84f340 937 /* Skip to the next newline, skipping over the initial `ELC'
1da3b9e4 938 with NUL bytes following it, but note the version. */
da84f340 939 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
1da3b9e4 940 if (i == 4)
8792be66 941 version = buf[i];
da84f340 942
e6eb4e9e 943 if (i >= nbytes
927f12da 944 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
d923b542 945 buf + i, nbytes - i) < 0)
7cded46f 946 version = 0;
da84f340
GM
947 }
948
949 lseek (fd, 0, SEEK_SET);
7cded46f 950 return version;
da84f340
GM
951}
952
953
7ee3bd7b
GM
954/* Callback for record_unwind_protect. Restore the old load list OLD,
955 after loading a file successfully. */
956
27e498e6 957static void
971de7fb 958record_load_unwind (Lisp_Object old)
7ee3bd7b 959{
27e498e6 960 Vloads_in_progress = old;
7ee3bd7b
GM
961}
962
164c8bfe
RS
963/* This handler function is used via internal_condition_case_1. */
964
965static Lisp_Object
971de7fb 966load_error_handler (Lisp_Object data)
164c8bfe
RS
967{
968 return Qnil;
969}
7ee3bd7b 970
27e498e6 971static void
971de7fb 972load_warn_old_style_backquotes (Lisp_Object file)
1d064697
SM
973{
974 if (!NILP (Vold_style_backquotes))
975 {
b636b4c1 976 Lisp_Object args[2];
18a4a65e 977 args[0] = build_string ("Loading `%s': old-style backquotes detected!");
1d064697
SM
978 args[1] = file;
979 Fmessage (2, args);
980 }
1d064697
SM
981}
982
a7ca3326 983DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
971a4293
LT
984 doc: /* Return the suffixes that `load' should try if a suffix is \
985required.
986This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
5842a27b 987 (void)
971a4293
LT
988{
989 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
990 while (CONSP (suffixes))
991 {
992 Lisp_Object exts = Vload_file_rep_suffixes;
993 suffix = XCAR (suffixes);
994 suffixes = XCDR (suffixes);
995 while (CONSP (exts))
996 {
997 ext = XCAR (exts);
998 exts = XCDR (exts);
999 lst = Fcons (concat2 (suffix, ext), lst);
1000 }
1001 }
1002 return Fnreverse (lst);
1003}
1004
a7ca3326 1005DEFUN ("load", Fload, Sload, 1, 5, 0,
5de38842
PJ
1006 doc: /* Execute a file of Lisp code named FILE.
1007First try FILE with `.elc' appended, then try with `.el',
971a4293 1008then try FILE unmodified (the exact suffixes in the exact order are
88852d45 1009determined by `load-suffixes'). Environment variable references in
971a4293 1010FILE are replaced with their values by calling `substitute-in-file-name'.
5de38842 1011This function searches the directories in `load-path'.
971a4293 1012
5de38842 1013If optional second arg NOERROR is non-nil,
971a4293 1014report no error if FILE doesn't exist.
5de38842 1015Print messages at start and end of loading unless
beb0b7f9
EZ
1016optional third arg NOMESSAGE is non-nil (but `force-load-messages'
1017overrides that).
5de38842 1018If optional fourth arg NOSUFFIX is non-nil, don't try adding
971a4293 1019suffixes `.elc' or `.el' to the specified name FILE.
5de38842 1020If optional fifth arg MUST-SUFFIX is non-nil, insist on
971a4293
LT
1021the suffix `.elc' or `.el'; don't accept just FILE unless
1022it ends in one of those suffixes or includes a directory name.
1023
c664f463
CY
1024If NOSUFFIX is nil, then if a file could not be found, try looking for
1025a different representation of the file by adding non-empty suffixes to
1026its name, before trying another file. Emacs uses this feature to find
1027compressed versions of files when Auto Compression mode is enabled.
1028If NOSUFFIX is non-nil, disable this feature.
1029
1030The suffixes that this function tries out, when NOSUFFIX is nil, are
1031given by the return value of `get-load-suffixes' and the values listed
1032in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1033return value of `get-load-suffixes' is used, i.e. the file name is
1034required to have a non-empty suffix.
0fc213e9 1035
aec3bf9f
GM
1036When searching suffixes, this function normally stops at the first
1037one that exists. If the option `load-prefer-newer' is non-nil,
1038however, it tries all suffixes, and uses whichever file is the newest.
1039
0fc213e9
RS
1040Loading a file records its definitions, and its `provide' and
1041`require' calls, in an element of `load-history' whose
1042car is the file name loaded. See `load-history'.
1043
c7926fe2
EZ
1044While the file is in the process of being loaded, the variable
1045`load-in-progress' is non-nil and the variable `load-file-name'
1046is bound to the file's name.
1047
971a4293 1048Return t if the file exists and loads successfully. */)
a0931322
PE
1049 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage,
1050 Lisp_Object nosuffix, Lisp_Object must_suffix)
078e7b4a 1051{
5d1922cd 1052 FILE *stream = NULL;
a0931322 1053 int fd;
2bfa3d3e 1054 dynwind_begin ();
6bb6da3e
AM
1055 struct gcpro gcpro1, gcpro2, gcpro3;
1056 Lisp_Object found, efound, hist_file_name;
7cded46f
PE
1057 /* True means we printed the ".el is newer" message. */
1058 bool newer = 0;
1059 /* True means we are loading a compiled file. */
1060 bool compiled = 0;
c2225d00 1061 Lisp_Object handler;
7cded46f 1062 bool safe_p = 1;
8ea90aa3 1063 const char *fmode = "r";
8792be66
KH
1064 int version;
1065
317073d5 1066#ifdef DOS_NT
229ba775 1067 fmode = "rt";
317073d5 1068#endif /* DOS_NT */
078e7b4a 1069
b7826503 1070 CHECK_STRING (file);
078e7b4a 1071
c2225d00 1072 /* If file name is magic, call the handler. */
e61b9b87
SM
1073 /* This shouldn't be necessary any more now that `openp' handles it right.
1074 handler = Ffind_file_name_handler (file, Qload);
1075 if (!NILP (handler))
1076 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
c2225d00 1077
07a0bda3
RS
1078 /* Do this after the handler to avoid
1079 the need to gcpro noerror, nomessage and nosuffix.
e61b9b87
SM
1080 (Below here, we care only whether they are nil or not.)
1081 The presence of this call is the result of a historical accident:
6bb6da3e 1082 it used to be in every file-operation and when it got removed
e61b9b87
SM
1083 everywhere, it accidentally stayed here. Since then, enough people
1084 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1085 that it seemed risky to remove. */
164c8bfe
RS
1086 if (! NILP (noerror))
1087 {
1088 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1089 Qt, load_error_handler);
2bfa3d3e
BT
1090 if (NILP (file)) {
1091 dynwind_end ();
1092 return Qnil;
1093 }
164c8bfe
RS
1094 }
1095 else
1096 file = Fsubstitute_in_file_name (file);
177c0ea7 1097
078e7b4a 1098 /* Avoid weird lossage with null string as arg,
48def666 1099 since it would try to load a directory as a Lisp file. */
a0931322 1100 if (SCHARS (file) == 0)
078e7b4a 1101 {
a0931322
PE
1102 fd = -1;
1103 errno = ENOENT;
1104 }
1105 else
1106 {
1107 Lisp_Object suffixes;
0fc213e9
RS
1108 found = Qnil;
1109 GCPRO2 (file, found);
211f7dcd
RS
1110
1111 if (! NILP (must_suffix))
1112 {
1113 /* Don't insist on adding a suffix if FILE already ends with one. */
a0931322 1114 ptrdiff_t size = SBYTES (file);
211f7dcd 1115 if (size > 3
42a5b22f 1116 && !strcmp (SSDATA (file) + size - 3, ".el"))
211f7dcd
RS
1117 must_suffix = Qnil;
1118 else if (size > 4
42a5b22f 1119 && !strcmp (SSDATA (file) + size - 4, ".elc"))
211f7dcd
RS
1120 must_suffix = Qnil;
1121 /* Don't insist on adding a suffix
1122 if the argument includes a directory name. */
1123 else if (! NILP (Ffile_name_directory (file)))
1124 must_suffix = Qnil;
1125 }
1126
a0931322
PE
1127 if (!NILP (nosuffix))
1128 suffixes = Qnil;
1129 else
1130 {
1131 suffixes = Fget_load_suffixes ();
1132 if (NILP (must_suffix))
1133 {
1134 Lisp_Object arg[2];
1135 arg[0] = suffixes;
1136 arg[1] = Vload_file_rep_suffixes;
1137 suffixes = Fappend (2, arg);
1138 }
1139 }
1140
1f41ee56 1141 fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
5a6e5452 1142 UNGCPRO;
078e7b4a
JB
1143 }
1144
96dc0f4e 1145 if (fd == -1)
078e7b4a 1146 {
265a9e55 1147 if (NILP (noerror))
a0931322 1148 report_file_error ("Cannot open load file", file);
2bfa3d3e 1149 dynwind_end ();
336d4a9c 1150 return Qnil;
078e7b4a
JB
1151 }
1152
ba0d1195 1153 /* Tell startup.el whether or not we found the user's init file. */
4116ab9f
KH
1154 if (EQ (Qt, Vuser_init_file))
1155 Vuser_init_file = found;
1156
96dc0f4e
MB
1157 /* If FD is -2, that means openp found a magic file. */
1158 if (fd == -2)
74549846 1159 {
5e24a1f7
KH
1160 if (NILP (Fequal (found, file)))
1161 /* If FOUND is a different file name from FILE,
1162 find its handler even if we have already inhibited
1163 the `load' operation on FILE. */
1164 handler = Ffind_file_name_handler (found, Qt);
1165 else
1166 handler = Ffind_file_name_handler (found, Qload);
2bfa3d3e
BT
1167 if (! NILP (handler)) {
1168 dynwind_end ();
1169 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1170 }
35774242
EZ
1171#ifdef DOS_NT
1172 /* Tramp has to deal with semi-broken packages that prepend
1173 drive letters to remote files. For that reason, Tramp
1174 catches file operations that test for file existence, which
1175 makes openp think X:/foo.elc files are remote. However,
1176 Tramp does not catch `load' operations for such files, so we
1177 end up with a nil as the `load' handler above. If we would
1178 continue with fd = -2, we will behave wrongly, and in
1179 particular try reading a .elc file in the "rt" mode instead
1180 of "rb". See bug #9311 for the results. To work around
1181 this, we try to open the file locally, and go with that if it
1182 succeeds. */
1183 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1184 if (fd == -1)
1185 fd = -2;
1186#endif
74549846
RS
1187 }
1188
9f62b5dd 1189 if (fd >= 0)
b2a069c2 1190 {
5d1922cd
BT
1191 record_unwind_protect_ptr (close_file_ptr_unwind, &fd);
1192 record_unwind_protect_ptr (fclose_ptr_unwind, &stream);
a0931322
PE
1193 }
1194
550a625e
GM
1195 /* Check if we're stuck in a recursive load cycle.
1196
1197 2000-09-21: It's not possible to just check for the file loaded
1198 being a member of Vloads_in_progress. This fails because of the
1199 way the byte compiler currently works; `provide's are not
9d438325 1200 evaluated, see font-lock.el/jit-lock.el as an example. This
550a625e
GM
1201 leads to a certain amount of ``normal'' recursion.
1202
1203 Also, just loading a file recursively is not always an error in
1204 the general case; the second load may do something different. */
3ba6fd9a 1205 {
0902fe45 1206 int load_count = 0;
3ba6fd9a
RS
1207 Lisp_Object tem;
1208 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
0902fe45 1209 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
a0931322 1210 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
3ba6fd9a
RS
1211 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1212 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1213 }
7ee3bd7b 1214
b9598260
SM
1215 /* All loads are by default dynamic, unless the file itself specifies
1216 otherwise using a file-variable in the first line. This is bound here
1217 so that it takes effect whether or not we use
1218 Vload_source_file_function. */
1219 specbind (Qlexical_binding, Qnil);
1220
48def666 1221 /* Get the name for load-history. */
6bb6da3e 1222 hist_file_name = (! NILP (Vpurify_flag)
a0931322
PE
1223 ? concat2 (Ffile_name_directory (file),
1224 Ffile_name_nondirectory (found))
6bb6da3e
AM
1225 : found) ;
1226
8792be66 1227 version = -1;
1bb9a689 1228
88852d45 1229 /* Check for the presence of old-style quotes and warn about them. */
1d064697
SM
1230 specbind (Qold_style_backquotes, Qnil);
1231 record_unwind_protect (load_warn_old_style_backquotes, file);
1232
72af86bd 1233 if (!memcmp (SDATA (found) + SBYTES (found) - 4, ".elc", 4)
7cded46f 1234 || (fd >= 0 && (version = safe_to_load_version (fd)) > 0))
96dc0f4e
MB
1235 /* Load .elc files directly, but not when they are
1236 remote and have no handler! */
078e7b4a 1237 {
96dc0f4e 1238 if (fd != -2)
da84f340 1239 {
96dc0f4e
MB
1240 struct stat s1, s2;
1241 int result;
da84f340 1242
6bb6da3e 1243 GCPRO3 (file, found, hist_file_name);
0fc213e9 1244
8792be66 1245 if (version < 0
7cded46f 1246 && ! (version = safe_to_load_version (fd)))
96dc0f4e
MB
1247 {
1248 safe_p = 0;
1249 if (!load_dangerous_libraries)
a0931322 1250 error ("File `%s' was not compiled in Emacs", SDATA (found));
beb0b7f9 1251 else if (!NILP (nomessage) && !force_load_messages)
96dc0f4e
MB
1252 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1253 }
04fc68e7 1254
96dc0f4e
MB
1255 compiled = 1;
1256
eb191db2
EZ
1257 efound = ENCODE_FILE (found);
1258
26fbf20b 1259#ifdef DOS_NT
96dc0f4e 1260 fmode = "rb";
26fbf20b 1261#endif /* DOS_NT */
eb191db2 1262
1f41ee56
GM
1263 /* openp already checked for newness, no point doing it again.
1264 FIXME would be nice to get a message when openp
1265 ignores suffix order due to load_prefer_newer. */
1266 if (!load_prefer_newer)
1267 {
1268 result = stat (SSDATA (efound), &s1);
1269 if (result == 0)
1270 {
1271 SSET (efound, SBYTES (efound) - 1, 0);
1272 result = stat (SSDATA (efound), &s2);
1273 SSET (efound, SBYTES (efound) - 1, 'c');
1274 }
04fc68e7 1275
1f41ee56
GM
1276 if (result == 0
1277 && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
1278 {
1279 /* Make the progress messages mention that source is newer. */
1280 newer = 1;
1281
1282 /* If we won't print another message, mention this anyway. */
1283 if (!NILP (nomessage) && !force_load_messages)
1284 {
1285 Lisp_Object msg_file;
1286 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1287 message_with_string ("Source file `%s' newer than byte-compiled file",
1288 msg_file, 1);
1289 }
1290 }
1291 } /* !load_prefer_newer */
0fc213e9 1292 UNGCPRO;
51ac6f83 1293 }
078e7b4a 1294 }
fe0e03f3
KH
1295 else
1296 {
1297 /* We are loading a source file (*.el). */
1298 if (!NILP (Vload_source_file_function))
1299 {
7ee3bd7b 1300 Lisp_Object val;
96dc0f4e
MB
1301
1302 if (fd >= 0)
a0931322
PE
1303 {
1304 emacs_close (fd);
5d1922cd 1305 fd = -1;
a0931322 1306 }
6bb6da3e 1307 val = call4 (Vload_source_file_function, found, hist_file_name,
7ee3bd7b 1308 NILP (noerror) ? Qnil : Qt,
beb0b7f9 1309 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
2bfa3d3e
BT
1310 dynwind_end ();
1311 return val;
fe0e03f3
KH
1312 }
1313 }
078e7b4a 1314
6bb6da3e 1315 GCPRO3 (file, found, hist_file_name);
0fc213e9 1316
a0931322 1317 if (fd < 0)
35774242 1318 {
a0931322
PE
1319 /* We somehow got here with fd == -2, meaning the file is deemed
1320 to be remote. Don't even try to reopen the file locally;
1321 just force a failure. */
1322 stream = NULL;
1323 errno = EINVAL;
35774242
EZ
1324 }
1325 else
078e7b4a 1326 {
a0931322 1327#ifdef WINDOWSNT
68c45bf0 1328 emacs_close (fd);
5d1922cd 1329 fd = -1;
a0931322
PE
1330 efound = ENCODE_FILE (found);
1331 stream = emacs_fopen (SSDATA (efound), fmode);
1332#else
1333 stream = fdopen (fd, fmode);
1334#endif
078e7b4a 1335 }
a0931322
PE
1336 if (! stream)
1337 report_file_error ("Opening stdio stream", file);
078e7b4a 1338
4b104c41 1339 if (! NILP (Vpurify_flag))
5e617bc2 1340 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
4b104c41 1341
beb0b7f9 1342 if (NILP (nomessage) || force_load_messages)
04fc68e7 1343 {
da84f340
GM
1344 if (!safe_p)
1345 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1346 file, 1);
1347 else if (!compiled)
e28552a4 1348 message_with_string ("Loading %s (source)...", file, 1);
db5cae4b 1349 else if (newer)
e28552a4
RS
1350 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1351 file, 1);
db5cae4b 1352 else /* The typical case; compiled file newer than source file. */
e28552a4 1353 message_with_string ("Loading %s...", file, 1);
04fc68e7 1354 }
078e7b4a 1355
20ea2964 1356 specbind (Qload_file_name, found);
74549846 1357 specbind (Qinhibit_file_name_operation, Qnil);
2baf5e76 1358 specbind (Qload_in_progress, Qt);
b9598260
SM
1359
1360 instream = stream;
1361 if (lisp_file_lexically_bound_p (Qget_file_char))
1362 Fset (Qlexical_binding, Qt);
1363
ab2a47e1 1364 if (! version || version >= 22)
b883cdb2 1365 readevalloop (Qget_file_char, stream, hist_file_name,
defb1411 1366 0, Qnil, Qnil, Qnil, Qnil);
8792be66
KH
1367 else
1368 {
1369 /* We can't handle a file which was compiled with
1370 byte-compile-dynamic by older version of Emacs. */
1371 specbind (Qload_force_doc_strings, Qt);
defb1411 1372 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name,
768d4533 1373 0, Qnil, Qnil, Qnil, Qnil);
8792be66 1374 }
2bfa3d3e 1375 dynwind_end ();
078e7b4a 1376
48def666 1377 /* Run any eval-after-load forms for this file. */
428b13d6 1378 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
6bb6da3e
AM
1379 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1380
078e7b4a
JB
1381 UNGCPRO;
1382
97dead2d 1383 xfree (saved_doc_string);
b2a30870
RS
1384 saved_doc_string = 0;
1385 saved_doc_string_size = 0;
1386
70fdbb46 1387 xfree (prev_saved_doc_string);
c15cfd1f
RS
1388 prev_saved_doc_string = 0;
1389 prev_saved_doc_string_size = 0;
1390
beb0b7f9 1391 if (!noninteractive && (NILP (nomessage) || force_load_messages))
04fc68e7 1392 {
da84f340
GM
1393 if (!safe_p)
1394 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1395 file, 1);
1396 else if (!compiled)
e28552a4 1397 message_with_string ("Loading %s (source)...done", file, 1);
db5cae4b 1398 else if (newer)
e28552a4
RS
1399 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1400 file, 1);
db5cae4b 1401 else /* The typical case; compiled file newer than source file. */
e28552a4 1402 message_with_string ("Loading %s...done", file, 1);
04fc68e7 1403 }
7ee3bd7b 1404
078e7b4a
JB
1405 return Qt;
1406}
078e7b4a 1407\f
7cded46f 1408static bool
971de7fb 1409complete_filename_p (Lisp_Object pathname)
078e7b4a 1410{
7cded46f 1411 const unsigned char *s = SDATA (pathname);
317073d5 1412 return (IS_DIRECTORY_SEP (s[0])
d5db4077 1413 || (SCHARS (pathname) > 2
7c2fb837 1414 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
078e7b4a
JB
1415}
1416
86d00812
SM
1417DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1418 doc: /* Search for FILENAME through PATH.
57a131fb 1419Returns the file's name in absolute form, or nil if not found.
86d00812
SM
1420If SUFFIXES is non-nil, it should be a list of suffixes to append to
1421file name when searching.
1422If non-nil, PREDICATE is used instead of `file-readable-p'.
73dcdb9f 1423PREDICATE can also be an integer to pass to the faccessat(2) function,
aa56f361
SM
1424in which case file-name-handlers are ignored.
1425This function will normally skip directories, so if you want it to find
1426directories, make sure the PREDICATE function returns `dir-ok' for them. */)
5842a27b 1427 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
86d00812
SM
1428{
1429 Lisp_Object file;
518c40a2 1430 int fd = openp (path, filename, suffixes, &file, predicate, false);
a773ed9a 1431 if (NILP (predicate) && fd >= 0)
bacba3c2 1432 emacs_close (fd);
86d00812
SM
1433 return file;
1434}
1435
aa56f361 1436static Lisp_Object Qdir_ok;
86d00812 1437
078e7b4a
JB
1438/* Search for a file whose name is STR, looking in directories
1439 in the Lisp list PATH, and trying suffixes from SUFFIX.
5f86adcd
PE
1440 On success, return a file descriptor (or 1 or -2 as described below).
1441 On failure, return -1 and set errno.
078e7b4a 1442
e61b9b87 1443 SUFFIXES is a list of strings containing possible suffixes.
e0f24100 1444 The empty suffix is automatically added if the list is empty.
e61b9b87 1445
86d00812
SM
1446 PREDICATE non-nil means don't open the files,
1447 just look for one that satisfies the predicate. In this case,
5f86adcd 1448 return 1 on success. The predicate can be a lisp function or
86d00812
SM
1449 an integer to pass to `access' (in which case file-name-handlers
1450 are ignored).
078e7b4a
JB
1451
1452 If STOREPTR is nonzero, it points to a slot where the name of
1453 the file actually found should be stored as a Lisp string.
74549846
RS
1454 nil is stored there on failure.
1455
96dc0f4e 1456 If the file we find is remote, return -2
1f41ee56
GM
1457 but store the found remote file name in *STOREPTR.
1458
1459 If NEWER is true, try all SUFFIXes and return the result for the
150622a1
GM
1460 newest file that exists. Does not apply to remote files,
1461 or if PREDICATE is specified. */
078e7b4a
JB
1462
1463int
5f86adcd 1464openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
518c40a2 1465 Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
078e7b4a 1466{
d311d28c 1467 ptrdiff_t fn_size = 100;
078e7b4a 1468 char buf[100];
7cded46f 1469 char *fn = buf;
518c40a2 1470 bool absolute;
d311d28c 1471 ptrdiff_t want_length;
74549846 1472 Lisp_Object filename;
518c40a2 1473 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6, gcpro7;
1f41ee56 1474 Lisp_Object string, tail, encoded_fn, save_string;
d311d28c 1475 ptrdiff_t max_suffix_len = 0;
5f86adcd 1476 int last_errno = ENOENT;
518c40a2 1477 int save_fd = -1;
e61b9b87 1478
70695f13
PE
1479 /* The last-modified time of the newest matching file found.
1480 Initialize it to something less than all valid timestamps. */
1481 struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
1482
6f8eafd1
SM
1483 CHECK_STRING (str);
1484
e61b9b87
SM
1485 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1486 {
f5df591a 1487 CHECK_STRING_CAR (tail);
e61b9b87 1488 max_suffix_len = max (max_suffix_len,
d5db4077 1489 SBYTES (XCAR (tail)));
e61b9b87 1490 }
078e7b4a 1491
518c40a2
PE
1492 string = filename = encoded_fn = save_string = Qnil;
1493 GCPRO7 (str, string, save_string, filename, path, suffixes, encoded_fn);
eb191db2 1494
078e7b4a
JB
1495 if (storeptr)
1496 *storeptr = Qnil;
1497
518c40a2 1498 absolute = complete_filename_p (str);
078e7b4a 1499
e61b9b87 1500 for (; CONSP (path); path = XCDR (path))
078e7b4a 1501 {
e61b9b87 1502 filename = Fexpand_file_name (str, XCAR (path));
078e7b4a 1503 if (!complete_filename_p (filename))
48def666 1504 /* If there are non-absolute elts in PATH (eg "."). */
078e7b4a 1505 /* Of course, this could conceivably lose if luser sets
48def666 1506 default-directory to be something non-absolute... */
078e7b4a 1507 {
4b4deea2 1508 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
078e7b4a 1509 if (!complete_filename_p (filename))
48def666 1510 /* Give up on this path element! */
078e7b4a
JB
1511 continue;
1512 }
1513
640ee02d 1514 /* Calculate maximum length of any filename made from
078e7b4a 1515 this path element/specified file name and any possible suffix. */
640ee02d
PE
1516 want_length = max_suffix_len + SBYTES (filename);
1517 if (fn_size <= want_length)
38182d90 1518 fn = alloca (fn_size = 100 + want_length);
078e7b4a 1519
078e7b4a 1520 /* Loop over suffixes. */
6c6f1994 1521 for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
e61b9b87 1522 CONSP (tail); tail = XCDR (tail))
078e7b4a 1523 {
d0065ff1
EZ
1524 Lisp_Object suffix = XCAR (tail);
1525 ptrdiff_t fnlen, lsuffix = SBYTES (suffix);
74549846 1526 Lisp_Object handler;
078e7b4a 1527
c49afcd7
RS
1528 /* Concatenate path element/specified name with the suffix.
1529 If the directory starts with /:, remove that. */
e99a530f
PE
1530 int prefixlen = ((SCHARS (filename) > 2
1531 && SREF (filename, 0) == '/'
1532 && SREF (filename, 1) == ':')
1533 ? 2 : 0);
1534 fnlen = SBYTES (filename) - prefixlen;
1535 memcpy (fn, SDATA (filename) + prefixlen, fnlen);
d0065ff1 1536 memcpy (fn + fnlen, SDATA (suffix), lsuffix + 1);
e99a530f 1537 fnlen += lsuffix;
74549846 1538 /* Check that the file exists and is not a directory. */
e61b9b87
SM
1539 /* We used to only check for handlers on non-absolute file names:
1540 if (absolute)
1541 handler = Qnil;
1542 else
1543 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1544 It's not clear why that was the case and it breaks things like
1545 (load "/bar.el") where the file is actually "/bar.el.gz". */
d0065ff1
EZ
1546 /* make_string has its own ideas on when to return a unibyte
1547 string and when a multibyte string, but we know better.
1548 We must have a unibyte string when dumping, since
1549 file-name encoding is shaky at best at that time, and in
1550 particular default-file-name-coding-system is reset
1551 several times during loadup. We therefore don't want to
1552 encode the file before passing it to file I/O library
1553 functions. */
1554 if (!STRING_MULTIBYTE (filename) && !STRING_MULTIBYTE (suffix))
1555 string = make_unibyte_string (fn, fnlen);
1556 else
1557 string = make_string (fn, fnlen);
4773b8ca 1558 handler = Ffind_file_name_handler (string, Qfile_exists_p);
86d00812
SM
1559 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1560 {
73dcdb9f 1561 bool exists;
86d00812
SM
1562 if (NILP (predicate))
1563 exists = !NILP (Ffile_readable_p (string));
1564 else
aa56f361
SM
1565 {
1566 Lisp_Object tmp = call1 (predicate, string);
5f86adcd 1567 if (NILP (tmp))
518c40a2 1568 exists = false;
5f86adcd
PE
1569 else if (EQ (tmp, Qdir_ok)
1570 || NILP (Ffile_directory_p (string)))
518c40a2 1571 exists = true;
5f86adcd
PE
1572 else
1573 {
518c40a2 1574 exists = false;
5f86adcd
PE
1575 last_errno = EISDIR;
1576 }
aa56f361 1577 }
74549846
RS
1578
1579 if (exists)
078e7b4a 1580 {
1f41ee56
GM
1581 /* We succeeded; return this descriptor and filename. */
1582 if (storeptr)
1583 *storeptr = string;
1584 UNGCPRO;
1585 return -2;
74549846
RS
1586 }
1587 }
1588 else
1589 {
73dcdb9f 1590 int fd;
4b2dd274 1591 const char *pfn;
1f41ee56 1592 struct stat st;
eb191db2
EZ
1593
1594 encoded_fn = ENCODE_FILE (string);
42a5b22f 1595 pfn = SSDATA (encoded_fn);
f0a80175 1596
73dcdb9f
PE
1597 /* Check that we can access or open it. */
1598 if (NATNUMP (predicate))
5f86adcd
PE
1599 {
1600 fd = -1;
1601 if (INT_MAX < XFASTINT (predicate))
1602 last_errno = EINVAL;
1603 else if (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
73dcdb9f
PE
1604 AT_EACCESS)
1605 == 0)
5f86adcd
PE
1606 {
1607 if (file_directory_p (pfn))
1608 last_errno = EISDIR;
1609 else
1610 fd = 1;
1611 }
1612 }
73dcdb9f
PE
1613 else
1614 {
73dcdb9f 1615 fd = emacs_open (pfn, O_RDONLY, 0);
5f86adcd 1616 if (fd < 0)
a0931322
PE
1617 {
1618 if (errno != ENOENT)
1619 last_errno = errno;
1620 }
5f86adcd 1621 else
74549846 1622 {
5f86adcd
PE
1623 int err = (fstat (fd, &st) != 0 ? errno
1624 : S_ISDIR (st.st_mode) ? EISDIR : 0);
1625 if (err)
1626 {
1627 last_errno = err;
1628 emacs_close (fd);
1629 fd = -1;
1630 }
74549846 1631 }
078e7b4a 1632 }
73dcdb9f
PE
1633
1634 if (fd >= 0)
1635 {
150622a1 1636 if (newer && !NATNUMP (predicate))
1f41ee56
GM
1637 {
1638 struct timespec mtime = get_stat_mtime (&st);
1639
518c40a2
PE
1640 if (timespec_cmp (mtime, save_mtime) <= 0)
1641 emacs_close (fd);
1642 else
1f41ee56 1643 {
518c40a2
PE
1644 if (0 <= save_fd)
1645 emacs_close (save_fd);
1f41ee56
GM
1646 save_fd = fd;
1647 save_mtime = mtime;
1648 save_string = string;
1649 }
1650 }
1651 else
1652 {
1653 /* We succeeded; return this descriptor and filename. */
1654 if (storeptr)
1655 *storeptr = string;
1656 UNGCPRO;
1657 return fd;
1658 }
73dcdb9f 1659 }
1f41ee56
GM
1660
1661 /* No more suffixes. Return the newest. */
518c40a2 1662 if (0 <= save_fd && ! CONSP (XCDR (tail)))
1f41ee56
GM
1663 {
1664 if (storeptr)
1665 *storeptr = save_string;
1666 UNGCPRO;
1667 return save_fd;
1668 }
078e7b4a 1669 }
078e7b4a 1670 }
5a6e5452 1671 if (absolute)
5ef2a3c0 1672 break;
078e7b4a
JB
1673 }
1674
5ef2a3c0 1675 UNGCPRO;
5f86adcd 1676 errno = last_errno;
5ef2a3c0 1677 return -1;
078e7b4a
JB
1678}
1679
1680\f
ae321d28
RS
1681/* Merge the list we've accumulated of globals from the current input source
1682 into the load_history variable. The details depend on whether
b502a9a1
RS
1683 the source has an associated file name or not.
1684
1685 FILENAME is the file name that we are loading from.
7cded46f
PE
1686
1687 ENTIRE is true if loading that entire file, false if evaluating
1688 part of it. */
ae321d28
RS
1689
1690static void
7cded46f 1691build_load_history (Lisp_Object filename, bool entire)
ae321d28 1692{
7cded46f
PE
1693 Lisp_Object tail, prev, newelt;
1694 Lisp_Object tem, tem2;
1695 bool foundit = 0;
ae321d28
RS
1696
1697 tail = Vload_history;
1698 prev = Qnil;
b502a9a1 1699
86d00812 1700 while (CONSP (tail))
ae321d28 1701 {
86d00812 1702 tem = XCAR (tail);
ae321d28 1703
48def666 1704 /* Find the feature's previous assoc list... */
b502a9a1 1705 if (!NILP (Fequal (filename, Fcar (tem))))
ae321d28
RS
1706 {
1707 foundit = 1;
1708
48def666 1709 /* If we're loading the entire file, remove old data. */
b502a9a1 1710 if (entire)
86d00812 1711 {
ae321d28 1712 if (NILP (prev))
86d00812 1713 Vload_history = XCDR (tail);
ae321d28 1714 else
86d00812 1715 Fsetcdr (prev, XCDR (tail));
ae321d28
RS
1716 }
1717
1718 /* Otherwise, cons on new symbols that are not already members. */
1719 else
1720 {
1721 tem2 = Vcurrent_load_list;
1722
1723 while (CONSP (tem2))
1724 {
86d00812 1725 newelt = XCAR (tem2);
ae321d28 1726
d642e4f9 1727 if (NILP (Fmember (newelt, tem)))
86d00812
SM
1728 Fsetcar (tail, Fcons (XCAR (tem),
1729 Fcons (newelt, XCDR (tem))));
ae321d28 1730
86d00812 1731 tem2 = XCDR (tem2);
ae321d28
RS
1732 QUIT;
1733 }
1734 }
1735 }
1736 else
1737 prev = tail;
86d00812 1738 tail = XCDR (tail);
ae321d28
RS
1739 QUIT;
1740 }
1741
b502a9a1
RS
1742 /* If we're loading an entire file, cons the new assoc onto the
1743 front of load-history, the most-recently-loaded position. Also
1744 do this if we didn't find an existing member for the file. */
1745 if (entire || !foundit)
8a1f1537
RS
1746 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1747 Vload_history);
ae321d28
RS
1748}
1749
27e498e6
PE
1750static void
1751readevalloop_1 (int old)
94e554db 1752{
27e498e6 1753 load_convert_to_unibyte = old;
94e554db
RS
1754}
1755
9c97398c
GM
1756/* Signal an `end-of-file' error, if possible with file name
1757 information. */
1758
845ca893 1759static _Noreturn void
971de7fb 1760end_of_file_error (void)
9c97398c 1761{
9c97398c 1762 if (STRINGP (Vload_file_name))
336d4a9c 1763 xsignal1 (Qend_of_file, Vload_file_name);
9c97398c 1764
336d4a9c 1765 xsignal0 (Qend_of_file);
9c97398c
GM
1766}
1767
12b1389c
DC
1768static Lisp_Object
1769readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
1770{
1771 /* If we macroexpand the toplevel form non-recursively and it ends
1772 up being a `progn' (or if it was a progn to start), treat each
1773 form in the progn as a top-level form. This way, if one form in
1774 the progn defines a macro, that macro is in effect when we expand
1775 the remaining forms. See similar code in bytecomp.el. */
1776 val = call2 (macroexpand, val, Qnil);
1777 if (EQ (CAR_SAFE (val), Qprogn))
1778 {
1779 Lisp_Object subforms = XCDR (val);
1780 val = Qnil;
1781 for (; CONSP (subforms); subforms = XCDR (subforms))
1782 val = readevalloop_eager_expand_eval (XCAR (subforms),
1783 macroexpand);
1784 }
1785 else
1786 val = eval_sub (call2 (macroexpand, val, Qt));
1787
1788 return val;
1789}
1790
94e554db 1791/* UNIBYTE specifies how to set load_convert_to_unibyte
976350af 1792 for this invocation.
4c03c46d 1793 READFUN, if non-nil, is used instead of `read'.
1ed7b9ae
RS
1794
1795 START, END specify region to read in current buffer (from eval-region).
1796 If the input is not from a buffer, they must be nil. */
94e554db 1797
078e7b4a 1798static void
dd4c5104
DN
1799readevalloop (Lisp_Object readcharfun,
1800 FILE *stream,
1801 Lisp_Object sourcename,
7cded46f 1802 bool printflag,
dd4c5104
DN
1803 Lisp_Object unibyte, Lisp_Object readfun,
1804 Lisp_Object start, Lisp_Object end)
078e7b4a
JB
1805{
1806 register int c;
1807 register Lisp_Object val;
2bfa3d3e 1808 dynwind_begin ();
0a37f512 1809 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
49cf7ff4 1810 struct buffer *b = 0;
7cded46f 1811 bool continue_reading_p;
b9598260 1812 Lisp_Object lex_bound;
7cded46f
PE
1813 /* True if reading an entire buffer. */
1814 bool whole_buffer = 0;
1815 /* True on the first time around. */
1816 bool first_sexp = 1;
36cb7c1a 1817 Lisp_Object compile_fn = 0;
d11db2c8
RS
1818
1819 if (MARKERP (readcharfun))
1820 {
1821 if (NILP (start))
8878319c 1822 start = readcharfun;
d11db2c8 1823 }
49cf7ff4
RS
1824
1825 if (BUFFERP (readcharfun))
1826 b = XBUFFER (readcharfun);
1827 else if (MARKERP (readcharfun))
1828 b = XMARKER (readcharfun)->buffer;
078e7b4a 1829
1ed7b9ae
RS
1830 /* We assume START is nil when input is not from a buffer. */
1831 if (! NILP (start) && !b)
1088b922 1832 emacs_abort ();
1ed7b9ae 1833
0a37f512 1834 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
8a1f1537 1835 specbind (Qcurrent_load_list, Qnil);
27e498e6 1836 record_unwind_protect_int (readevalloop_1, load_convert_to_unibyte);
94e554db 1837 load_convert_to_unibyte = !NILP (unibyte);
078e7b4a 1838
b9598260
SM
1839 /* If lexical binding is active (either because it was specified in
1840 the file's header, or via a buffer-local variable), create an empty
1841 lexical environment, otherwise, turn off lexical binding. */
1842 lex_bound = find_symbol_value (Qlexical_binding);
f07a954e 1843 specbind (Qinternal_interpreter_environment,
6c6f1994
PE
1844 (NILP (lex_bound) || EQ (lex_bound, Qunbound)
1845 ? Qnil : list1 (Qt)));
b9598260 1846
0a37f512 1847 GCPRO4 (sourcename, readfun, start, end);
ae321d28 1848
48def666 1849 /* Try to ensure sourcename is a truename, except whilst preloading. */
6bb6da3e 1850 if (NILP (Vpurify_flag)
91fe9496
SM
1851 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1852 && !NILP (Ffboundp (Qfile_truename)))
6bb6da3e
AM
1853 sourcename = call1 (Qfile_truename, sourcename) ;
1854
ae321d28
RS
1855 LOADHIST_ATTACH (sourcename);
1856
f38952fe
GM
1857 continue_reading_p = 1;
1858 while (continue_reading_p)
078e7b4a 1859 {
2bfa3d3e 1860 dynwind_begin ();
4c03c46d 1861
e578f381 1862 if (b != 0 && !BUFFER_LIVE_P (b))
49cf7ff4
RS
1863 error ("Reading from killed buffer");
1864
4c03c46d
KS
1865 if (!NILP (start))
1866 {
721b7d9e 1867 /* Switch to the buffer we are reading from. */
4c03c46d 1868 record_unwind_protect (save_excursion_restore, save_excursion_save ());
721b7d9e
RS
1869 set_buffer_internal (b);
1870
1871 /* Save point in it. */
4c03c46d 1872 record_unwind_protect (save_excursion_restore, save_excursion_save ());
721b7d9e 1873 /* Save ZV in it. */
4c03c46d 1874 record_unwind_protect (save_restriction_restore, save_restriction_save ());
721b7d9e
RS
1875 /* Those get unbound after we read one expression. */
1876
1877 /* Set point and ZV around stuff to be read. */
4c03c46d 1878 Fgoto_char (start);
d11db2c8
RS
1879 if (!NILP (end))
1880 Fnarrow_to_region (make_number (BEGV), end);
1881
1882 /* Just for cleanliness, convert END to a marker
1883 if it is an integer. */
1884 if (INTEGERP (end))
1885 end = Fpoint_max_marker ();
4c03c46d
KS
1886 }
1887
d11db2c8
RS
1888 /* On the first cycle, we can easily test here
1889 whether we are reading the whole buffer. */
1890 if (b && first_sexp)
1891 whole_buffer = (PT == BEG && ZV == Z);
1892
078e7b4a 1893 instream = stream;
4c03c46d 1894 read_next:
078e7b4a
JB
1895 c = READCHAR;
1896 if (c == ';')
1897 {
1898 while ((c = READCHAR) != '\n' && c != -1);
4c03c46d
KS
1899 goto read_next;
1900 }
1901 if (c < 0)
1902 {
2bfa3d3e 1903 dynwind_end ();
4c03c46d 1904 break;
078e7b4a 1905 }
6069d957
RS
1906
1907 /* Ignore whitespace here, so we can detect eof. */
adef3de7 1908 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
ed497dd4 1909 || c == 0xa0) /* NBSP */
4c03c46d 1910 goto read_next;
078e7b4a 1911
265a9e55 1912 if (!NILP (Vpurify_flag) && c == '(')
078e7b4a 1913 {
48def666 1914 val = read_list (0, readcharfun);
078e7b4a
JB
1915 }
1916 else
1917 {
1918 UNREAD (c);
4ad679f9 1919 read_objects = Qnil;
f38952fe
GM
1920 if (!NILP (readfun))
1921 {
1922 val = call1 (readfun, readcharfun);
1923
1924 /* If READCHARFUN has set point to ZV, we should
1925 stop reading, even if the form read sets point
1926 to a different value when evaluated. */
1927 if (BUFFERP (readcharfun))
1928 {
0902fe45
PE
1929 struct buffer *buf = XBUFFER (readcharfun);
1930 if (BUF_PT (buf) == BUF_ZV (buf))
f38952fe
GM
1931 continue_reading_p = 0;
1932 }
1933 }
976350af 1934 else if (! NILP (Vload_read_function))
84a15045 1935 val = call1 (Vload_read_function, readcharfun);
976350af 1936 else
abb13b09 1937 val = read_internal_start (readcharfun, Qnil, Qnil);
078e7b4a
JB
1938 }
1939
4c03c46d
KS
1940 if (!NILP (start) && continue_reading_p)
1941 start = Fpoint_marker ();
d11db2c8
RS
1942
1943 /* Restore saved point and BEGV. */
2bfa3d3e 1944 dynwind_end ();
4c03c46d 1945
36cb7c1a 1946 val = eval_sub (val);
f38952fe 1947
078e7b4a
JB
1948 if (printflag)
1949 {
1950 Vvalues = Fcons (val, Vvalues);
1951 if (EQ (Vstandard_output, Qt))
1952 Fprin1 (val, Qnil);
1953 else
1954 Fprint (val, Qnil);
1955 }
d11db2c8
RS
1956
1957 first_sexp = 0;
078e7b4a
JB
1958 }
1959
8878319c 1960 build_load_history (sourcename,
d11db2c8 1961 stream || whole_buffer);
b502a9a1 1962
ae321d28
RS
1963 UNGCPRO;
1964
2bfa3d3e 1965 dynwind_end ();
078e7b4a
JB
1966}
1967
1076254d 1968DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
5de38842 1969 doc: /* Execute the current buffer as Lisp code.
72097cd9
CY
1970When called from a Lisp program (i.e., not interactively), this
1971function accepts up to five optional arguments:
5de38842
PJ
1972BUFFER is the buffer to evaluate (nil means use current buffer).
1973PRINTFLAG controls printing of output:
72097cd9
CY
1974 A value of nil means discard it; anything else is stream for print.
1975FILENAME specifies the file name to use for `load-history'.
1976UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
1977 invocation.
1978DO-ALLOW-PRINT, if non-nil, specifies that `print' and related
1979 functions should work normally even if PRINTFLAG is nil.
5de38842
PJ
1980
1981This function preserves the position of point. */)
5842a27b 1982 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
228d4b1c 1983{
2bfa3d3e 1984 dynwind_begin ();
228d4b1c
JA
1985 Lisp_Object tem, buf;
1986
9391b698 1987 if (NILP (buffer))
228d4b1c
JA
1988 buf = Fcurrent_buffer ();
1989 else
9391b698 1990 buf = Fget_buffer (buffer);
dfdb645c 1991 if (NILP (buf))
13febd85 1992 error ("No such buffer");
228d4b1c 1993
1076254d 1994 if (NILP (printflag) && NILP (do_allow_print))
228d4b1c
JA
1995 tem = Qsymbolp;
1996 else
1997 tem = printflag;
13febd85
RS
1998
1999 if (NILP (filename))
4b4deea2 2000 filename = BVAR (XBUFFER (buf), filename);
13febd85 2001
3f39f996 2002 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
228d4b1c
JA
2003 specbind (Qstandard_output, tem);
2004 record_unwind_protect (save_excursion_restore, save_excursion_save ());
bf1c0f27 2005 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
7200d79c 2006 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
defb1411 2007 readevalloop (buf, 0, filename,
4c03c46d 2008 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
2bfa3d3e 2009 dynwind_end ();
228d4b1c
JA
2010
2011 return Qnil;
2012}
2013
976350af 2014DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
5de38842
PJ
2015 doc: /* Execute the region as Lisp code.
2016When called from programs, expects two arguments,
2017giving starting and ending indices in the current buffer
2018of the text to be executed.
2019Programs can pass third argument PRINTFLAG which controls output:
51d8b30e 2020A value of nil means discard it; anything else is stream for printing it.
5de38842
PJ
2021Also the fourth argument READ-FUNCTION, if non-nil, is used
2022instead of `read' to read each expression. It gets one argument
2023which is the input stream for reading characters.
2024
2025This function does not move point. */)
5842a27b 2026 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
078e7b4a 2027{
e4769531 2028 /* FIXME: Do the eval-sexp-add-defvars dance! */
2bfa3d3e 2029 dynwind_begin ();
ae321d28
RS
2030 Lisp_Object tem, cbuf;
2031
2032 cbuf = Fcurrent_buffer ();
078e7b4a 2033
265a9e55 2034 if (NILP (printflag))
078e7b4a
JB
2035 tem = Qsymbolp;
2036 else
2037 tem = printflag;
2038 specbind (Qstandard_output, tem);
3f39f996 2039 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
078e7b4a 2040
48def666 2041 /* `readevalloop' calls functions which check the type of start and end. */
f619ad4c 2042 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
4c03c46d
KS
2043 !NILP (printflag), Qnil, read_function,
2044 start, end);
078e7b4a 2045
2bfa3d3e
BT
2046 dynwind_end ();
2047 return Qnil;
078e7b4a
JB
2048}
2049
078e7b4a 2050\f
a7ca3326 2051DEFUN ("read", Fread, Sread, 0, 1, 0,
5de38842
PJ
2052 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
2053If STREAM is nil, use the value of `standard-input' (which see).
2054STREAM or the value of `standard-input' may be:
2055 a buffer (read from point and advance it)
2056 a marker (read from where it points and advance it)
2057 a function (call it with no arguments for each character,
2058 call it with a char as argument to push a char back)
2059 a string (takes text from string, starting at the beginning)
2060 t (read text line using minibuffer and use it, or read from
2061 standard input in batch mode). */)
5842a27b 2062 (Lisp_Object stream)
078e7b4a 2063{
5be02dff
KH
2064 if (NILP (stream))
2065 stream = Vstandard_input;
2066 if (EQ (stream, Qt))
2067 stream = Qread_char;
5be02dff 2068 if (EQ (stream, Qread_char))
b483c570 2069 /* FIXME: ?! When is this used !? */
30c7e542
SM
2070 return call1 (intern ("read-minibuffer"),
2071 build_string ("Lisp expression: "));
078e7b4a 2072
abb13b09 2073 return read_internal_start (stream, Qnil, Qnil);
078e7b4a
JB
2074}
2075
a7ca3326 2076DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
5de38842
PJ
2077 doc: /* Read one Lisp expression which is represented as text by STRING.
2078Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
a4824228 2079FINAL-STRING-INDEX is an integer giving the position of the next
a239d4e9 2080 remaining character in STRING.
5de38842
PJ
2081START and END optionally delimit a substring of STRING from which to read;
2082 they default to 0 and (length STRING) respectively. */)
5842a27b 2083 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
078e7b4a 2084{
2530ceaf 2085 Lisp_Object ret;
b7826503 2086 CHECK_STRING (string);
48def666 2087 /* `read_internal_start' sets `read_from_string_index'. */
2530ceaf 2088 ret = read_internal_start (string, start, end);
5135ab39 2089 return Fcons (ret, make_number (read_from_string_index));
abb13b09 2090}
078e7b4a 2091
abb13b09 2092/* Function to set up the global context we need in toplevel read
48def666 2093 calls. */
abb13b09 2094static Lisp_Object
971de7fb 2095read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
48def666 2096/* `start', `end' only used when stream is a string. */
abb13b09
CW
2097{
2098 Lisp_Object retval;
078e7b4a 2099
abb13b09 2100 readchar_count = 0;
17634846 2101 new_backquote_flag = 0;
4ad679f9 2102 read_objects = Qnil;
abb13b09
CW
2103 if (EQ (Vread_with_symbol_positions, Qt)
2104 || EQ (Vread_with_symbol_positions, stream))
2105 Vread_symbol_positions_list = Qnil;
2106
8f924df7
KH
2107 if (STRINGP (stream)
2108 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
abb13b09 2109 {
d311d28c 2110 ptrdiff_t startval, endval;
8f924df7 2111 Lisp_Object string;
078e7b4a 2112
8f924df7
KH
2113 if (STRINGP (stream))
2114 string = stream;
2115 else
2116 string = XCAR (stream);
078e7b4a 2117
abb13b09 2118 if (NILP (end))
8f924df7 2119 endval = SCHARS (string);
abb13b09
CW
2120 else
2121 {
2122 CHECK_NUMBER (end);
d311d28c 2123 if (! (0 <= XINT (end) && XINT (end) <= SCHARS (string)))
8f924df7 2124 args_out_of_range (string, end);
d311d28c 2125 endval = XINT (end);
abb13b09 2126 }
17634846 2127
abb13b09
CW
2128 if (NILP (start))
2129 startval = 0;
2130 else
2131 {
2132 CHECK_NUMBER (start);
d311d28c 2133 if (! (0 <= XINT (start) && XINT (start) <= endval))
8f924df7 2134 args_out_of_range (string, start);
d311d28c 2135 startval = XINT (start);
abb13b09
CW
2136 }
2137 read_from_string_index = startval;
8f924df7 2138 read_from_string_index_byte = string_char_to_byte (string, startval);
abb13b09
CW
2139 read_from_string_limit = endval;
2140 }
177c0ea7 2141
abb13b09
CW
2142 retval = read0 (stream);
2143 if (EQ (Vread_with_symbol_positions, Qt)
2144 || EQ (Vread_with_symbol_positions, stream))
2145 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2146 return retval;
078e7b4a
JB
2147}
2148\f
336d4a9c
KS
2149
2150/* Signal Qinvalid_read_syntax error.
2151 S is error string of length N (if > 0) */
2152
845ca893 2153static _Noreturn void
e4d29b33 2154invalid_syntax (const char *s)
336d4a9c 2155{
e4d29b33 2156 xsignal1 (Qinvalid_read_syntax, build_string (s));
336d4a9c
KS
2157}
2158
2159
6428369f 2160/* Use this for recursive reads, in contexts where internal tokens
48def666 2161 are not allowed. */
e28552a4 2162
078e7b4a 2163static Lisp_Object
971de7fb 2164read0 (Lisp_Object readcharfun)
078e7b4a
JB
2165{
2166 register Lisp_Object val;
e28552a4 2167 int c;
078e7b4a 2168
17634846 2169 val = read1 (readcharfun, &c, 0);
336d4a9c
KS
2170 if (!c)
2171 return val;
078e7b4a 2172
336d4a9c
KS
2173 xsignal1 (Qinvalid_read_syntax,
2174 Fmake_string (make_number (1), make_number (c)));
078e7b4a
JB
2175}
2176\f
82cb60d3 2177static ptrdiff_t read_buffer_size;
078e7b4a
JB
2178static char *read_buffer;
2179
f6f79b37 2180/* Read a \-escape sequence, assuming we already read the `\'.
8f924df7 2181 If the escape sequence forces unibyte, return eight-bit char. */
6f7f43d5 2182
078e7b4a 2183static int
7cded46f 2184read_escape (Lisp_Object readcharfun, bool stringp)
078e7b4a 2185{
7cded46f 2186 int c = READCHAR;
9735b9ce 2187 /* \u allows up to four hex digits, \U up to eight. Default to the
48def666 2188 behavior for \u, and change this value in the case that \U is seen. */
71b169b8 2189 int unicode_hex_count = 4;
f6f79b37 2190
078e7b4a
JB
2191 switch (c)
2192 {
f3849f25 2193 case -1:
da3b886d 2194 end_of_file_error ();
f3849f25 2195
078e7b4a 2196 case 'a':
265a9e55 2197 return '\007';
078e7b4a
JB
2198 case 'b':
2199 return '\b';
f405a585
RS
2200 case 'd':
2201 return 0177;
078e7b4a
JB
2202 case 'e':
2203 return 033;
2204 case 'f':
2205 return '\f';
2206 case 'n':
2207 return '\n';
2208 case 'r':
2209 return '\r';
2210 case 't':
2211 return '\t';
2212 case 'v':
2213 return '\v';
2214 case '\n':
2215 return -1;
e28552a4 2216 case ' ':
e7fc914b
KH
2217 if (stringp)
2218 return -1;
2219 return ' ';
078e7b4a
JB
2220
2221 case 'M':
2222 c = READCHAR;
2223 if (c != '-')
2224 error ("Invalid escape character syntax");
2225 c = READCHAR;
2226 if (c == '\\')
8792be66 2227 c = read_escape (readcharfun, 0);
7bd279cd 2228 return c | meta_modifier;
f405a585
RS
2229
2230 case 'S':
2231 c = READCHAR;
2232 if (c != '-')
2233 error ("Invalid escape character syntax");
2234 c = READCHAR;
2235 if (c == '\\')
8792be66 2236 c = read_escape (readcharfun, 0);
7bd279cd
RS
2237 return c | shift_modifier;
2238
2239 case 'H':
2240 c = READCHAR;
2241 if (c != '-')
2242 error ("Invalid escape character syntax");
2243 c = READCHAR;
2244 if (c == '\\')
8792be66 2245 c = read_escape (readcharfun, 0);
7bd279cd
RS
2246 return c | hyper_modifier;
2247
2248 case 'A':
2249 c = READCHAR;
2250 if (c != '-')
2251 error ("Invalid escape character syntax");
2252 c = READCHAR;
2253 if (c == '\\')
8792be66 2254 c = read_escape (readcharfun, 0);
7bd279cd
RS
2255 return c | alt_modifier;
2256
2257 case 's':
2258 c = READCHAR;
4d7528d0 2259 if (stringp || c != '-')
010b7eac
RS
2260 {
2261 UNREAD (c);
2262 return ' ';
2263 }
7bd279cd
RS
2264 c = READCHAR;
2265 if (c == '\\')
8792be66 2266 c = read_escape (readcharfun, 0);
7bd279cd 2267 return c | super_modifier;
078e7b4a
JB
2268
2269 case 'C':
2270 c = READCHAR;
2271 if (c != '-')
2272 error ("Invalid escape character syntax");
2273 case '^':
2274 c = READCHAR;
2275 if (c == '\\')
8792be66 2276 c = read_escape (readcharfun, 0);
164d590d
KH
2277 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2278 return 0177 | (c & CHAR_MODIFIER_MASK);
2279 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2280 return c | ctrl_modifier;
f405a585
RS
2281 /* ASCII control chars are made from letters (both cases),
2282 as well as the non-letters within 0100...0137. */
2283 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2284 return (c & (037 | ~0177));
2285 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2286 return (c & (037 | ~0177));
078e7b4a 2287 else
7bd279cd 2288 return c | ctrl_modifier;
078e7b4a
JB
2289
2290 case '0':
2291 case '1':
2292 case '2':
2293 case '3':
2294 case '4':
2295 case '5':
2296 case '6':
2297 case '7':
2298 /* An octal escape, as in ANSI C. */
2299 {
2300 register int i = c - '0';
2301 register int count = 0;
2302 while (++count < 3)
2303 {
2304 if ((c = READCHAR) >= '0' && c <= '7')
2305 {
2306 i *= 8;
2307 i += c - '0';
2308 }
2309 else
2310 {
2311 UNREAD (c);
2312 break;
2313 }
2314 }
177c0ea7 2315
8f924df7 2316 if (i >= 0x80 && i < 0x100)
8792be66 2317 i = BYTE8_TO_CHAR (i);
078e7b4a
JB
2318 return i;
2319 }
2320
2321 case 'x':
2322 /* A hex escape, as in ANSI C. */
2323 {
0324f3af 2324 unsigned int i = 0;
1571601b 2325 int count = 0;
078e7b4a
JB
2326 while (1)
2327 {
2328 c = READCHAR;
2329 if (c >= '0' && c <= '9')
2330 {
2331 i *= 16;
2332 i += c - '0';
2333 }
2334 else if ((c >= 'a' && c <= 'f')
2335 || (c >= 'A' && c <= 'F'))
2336 {
2337 i *= 16;
2338 if (c >= 'a' && c <= 'f')
2339 i += c - 'a' + 10;
2340 else
2341 i += c - 'A' + 10;
2342 }
2343 else
2344 {
2345 UNREAD (c);
2346 break;
2347 }
0324f3af
PE
2348 /* Allow hex escapes as large as ?\xfffffff, because some
2349 packages use them to denote characters with modifiers. */
2350 if ((CHAR_META | (CHAR_META - 1)) < i)
39019e54 2351 error ("Hex character out of range: \\x%x...", i);
82cb60d3 2352 count += count < 3;
078e7b4a 2353 }
f6f79b37 2354
1571601b 2355 if (count < 3 && i >= 0x80)
8792be66 2356 return BYTE8_TO_CHAR (i);
078e7b4a
JB
2357 return i;
2358 }
2359
71b169b8
EZ
2360 case 'U':
2361 /* Post-Unicode-2.0: Up to eight hex chars. */
2362 unicode_hex_count = 8;
2363 case 'u':
2364
9735b9ce 2365 /* A Unicode escape. We only permit them in strings and characters,
71b169b8
EZ
2366 not arbitrarily in the source code, as in some other languages. */
2367 {
a808f22d 2368 unsigned int i = 0;
71b169b8 2369 int count = 0;
71b169b8
EZ
2370
2371 while (++count <= unicode_hex_count)
2372 {
2373 c = READCHAR;
48def666
SM
2374 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2375 want. */
71b169b8
EZ
2376 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2377 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2378 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2379 else
39019e54 2380 error ("Non-hex digit used for Unicode escape");
71b169b8 2381 }
a808f22d
KH
2382 if (i > 0x10FFFF)
2383 error ("Non-Unicode character: 0x%x", i);
7ea7f636 2384 return i;
71b169b8
EZ
2385 }
2386
078e7b4a
JB
2387 default:
2388 return c;
2389 }
2390}
2391
a108c10b
PE
2392/* Return the digit that CHARACTER stands for in the given BASE.
2393 Return -1 if CHARACTER is out of range for BASE,
2394 and -2 if CHARACTER is not valid for any supported BASE. */
b0ab8123 2395static int
a108c10b
PE
2396digit_to_number (int character, int base)
2397{
2398 int digit;
2399
2400 if ('0' <= character && character <= '9')
2401 digit = character - '0';
2402 else if ('a' <= character && character <= 'z')
2403 digit = character - 'a' + 10;
2404 else if ('A' <= character && character <= 'Z')
2405 digit = character - 'A' + 10;
2406 else
2407 return -2;
2408
2409 return digit < base ? digit : -1;
2410}
2411
bf5d1a17
GM
2412/* Read an integer in radix RADIX using READCHARFUN to read
2413 characters. RADIX must be in the interval [2..36]; if it isn't, a
2414 read error is signaled . Value is the integer read. Signals an
2415 error if encountering invalid read syntax or if RADIX is out of
2416 range. */
2417
2418static Lisp_Object
8b9ac8b4 2419read_integer (Lisp_Object readcharfun, EMACS_INT radix)
bf5d1a17 2420{
8b9ac8b4
PE
2421 /* Room for sign, leading 0, other digits, trailing null byte.
2422 Also, room for invalid syntax diagnostic. */
2423 char buf[max (1 + 1 + sizeof (uintmax_t) * CHAR_BIT + 1,
2424 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
a108c10b
PE
2425
2426 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
bf5d1a17
GM
2427
2428 if (radix < 2 || radix > 36)
a108c10b 2429 valid = 0;
bf5d1a17
GM
2430 else
2431 {
a108c10b
PE
2432 char *p = buf;
2433 int c, digit;
bf5d1a17
GM
2434
2435 c = READCHAR;
a108c10b 2436 if (c == '-' || c == '+')
bf5d1a17 2437 {
a108c10b 2438 *p++ = c;
bf5d1a17 2439 c = READCHAR;
bf5d1a17 2440 }
177c0ea7 2441
a108c10b 2442 if (c == '0')
bf5d1a17 2443 {
a108c10b
PE
2444 *p++ = c;
2445 valid = 1;
bf5d1a17 2446
a108c10b
PE
2447 /* Ignore redundant leading zeros, so the buffer doesn't
2448 fill up with them. */
2449 do
2450 c = READCHAR;
2451 while (c == '0');
2452 }
2453
908589fd 2454 while ((digit = digit_to_number (c, radix)) >= -1)
a108c10b
PE
2455 {
2456 if (digit == -1)
2457 valid = 0;
2458 if (valid < 0)
2459 valid = 1;
2460
2461 if (p < buf + sizeof buf - 1)
2462 *p++ = c;
2463 else
2464 valid = 0;
bf5d1a17 2465
bf5d1a17
GM
2466 c = READCHAR;
2467 }
a108c10b 2468
0a6a104b 2469 UNREAD (c);
a108c10b 2470 *p = '\0';
bf5d1a17
GM
2471 }
2472
a108c10b 2473 if (! valid)
bf5d1a17 2474 {
8b9ac8b4 2475 sprintf (buf, "integer, radix %"pI"d", radix);
e4d29b33 2476 invalid_syntax (buf);
bf5d1a17
GM
2477 }
2478
a108c10b 2479 return string_to_number (buf, radix, 0);
bf5d1a17
GM
2480}
2481
2482
6428369f
KH
2483/* If the next token is ')' or ']' or '.', we store that character
2484 in *PCH and the return value is not interesting. Else, we store
17634846
RS
2485 zero in *PCH and we read and return one lisp object.
2486
7cded46f 2487 FIRST_IN_LIST is true if this is the first element of a list. */
17634846 2488
078e7b4a 2489static Lisp_Object
7cded46f 2490read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
078e7b4a 2491{
fce31d69
PE
2492 int c;
2493 bool uninterned_symbol = 0;
2494 bool multibyte;
4ad679f9 2495
6428369f 2496 *pch = 0;
078e7b4a
JB
2497
2498 retry:
2499
1202434b 2500 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
9c97398c
GM
2501 if (c < 0)
2502 end_of_file_error ();
078e7b4a
JB
2503
2504 switch (c)
2505 {
2506 case '(':
2507 return read_list (0, readcharfun);
2508
2509 case '[':
c15cfd1f 2510 return read_vector (readcharfun, 0);
078e7b4a
JB
2511
2512 case ')':
2513 case ']':
078e7b4a 2514 {
6428369f
KH
2515 *pch = c;
2516 return Qnil;
078e7b4a
JB
2517 }
2518
2519 case '#':
200f684e 2520 c = READCHAR;
f19a0f5b
TZ
2521 if (c == 's')
2522 {
2523 c = READCHAR;
2524 if (c == '(')
2525 {
2526 /* Accept extended format for hashtables (extensible to
2527 other types), e.g.
48def666 2528 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
f19a0f5b
TZ
2529 Lisp_Object tmp = read_list (0, readcharfun);
2530 Lisp_Object head = CAR_SAFE (tmp);
2531 Lisp_Object data = Qnil;
2532 Lisp_Object val = Qnil;
2533 /* The size is 2 * number of allowed keywords to
48def666 2534 make-hash-table. */
9735b9ce 2535 Lisp_Object params[10];
f19a0f5b
TZ
2536 Lisp_Object ht;
2537 Lisp_Object key = Qnil;
2538 int param_count = 0;
9735b9ce 2539
f19a0f5b
TZ
2540 if (!EQ (head, Qhash_table))
2541 error ("Invalid extended read marker at head of #s list "
2542 "(only hash-table allowed)");
9735b9ce 2543
f19a0f5b
TZ
2544 tmp = CDR_SAFE (tmp);
2545
48def666 2546 /* This is repetitive but fast and simple. */
f19a0f5b 2547 params[param_count] = QCsize;
48def666 2548 params[param_count + 1] = Fplist_get (tmp, Qsize);
5721b4ed
AS
2549 if (!NILP (params[param_count + 1]))
2550 param_count += 2;
f19a0f5b
TZ
2551
2552 params[param_count] = QCtest;
48def666 2553 params[param_count + 1] = Fplist_get (tmp, Qtest);
5721b4ed
AS
2554 if (!NILP (params[param_count + 1]))
2555 param_count += 2;
f19a0f5b
TZ
2556
2557 params[param_count] = QCweakness;
48def666 2558 params[param_count + 1] = Fplist_get (tmp, Qweakness);
5721b4ed
AS
2559 if (!NILP (params[param_count + 1]))
2560 param_count += 2;
f19a0f5b
TZ
2561
2562 params[param_count] = QCrehash_size;
48def666 2563 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
5721b4ed
AS
2564 if (!NILP (params[param_count + 1]))
2565 param_count += 2;
f19a0f5b
TZ
2566
2567 params[param_count] = QCrehash_threshold;
48def666 2568 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
5721b4ed
AS
2569 if (!NILP (params[param_count + 1]))
2570 param_count += 2;
f19a0f5b 2571
48def666 2572 /* This is the hashtable data. */
f19a0f5b
TZ
2573 data = Fplist_get (tmp, Qdata);
2574
48def666 2575 /* Now use params to make a new hashtable and fill it. */
f19a0f5b 2576 ht = Fmake_hash_table (param_count, params);
9735b9ce 2577
f19a0f5b
TZ
2578 while (CONSP (data))
2579 {
2580 key = XCAR (data);
2581 data = XCDR (data);
2582 if (!CONSP (data))
2583 error ("Odd number of elements in hashtable data");
2584 val = XCAR (data);
2585 data = XCDR (data);
2586 Fputhash (key, val, ht);
2587 }
9735b9ce 2588
f19a0f5b
TZ
2589 return ht;
2590 }
5721b4ed 2591 UNREAD (c);
e4d29b33 2592 invalid_syntax ("#");
f19a0f5b 2593 }
c2390933
RS
2594 if (c == '^')
2595 {
2596 c = READCHAR;
2597 if (c == '[')
2598 {
2599 Lisp_Object tmp;
c15cfd1f 2600 tmp = read_vector (readcharfun, 0);
77b37c05 2601 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
c2390933 2602 error ("Invalid size char-table");
985773c9 2603 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
c2390933
RS
2604 return tmp;
2605 }
3701b5de
KH
2606 else if (c == '^')
2607 {
2608 c = READCHAR;
2609 if (c == '[')
2610 {
2611 Lisp_Object tmp;
d311d28c
PE
2612 int depth;
2613 ptrdiff_t size;
8f924df7 2614
c15cfd1f 2615 tmp = read_vector (readcharfun, 0);
6efdadfd
PE
2616 size = ASIZE (tmp);
2617 if (size == 0)
2618 error ("Invalid size char-table");
d311d28c 2619 if (! RANGED_INTEGERP (1, AREF (tmp, 0), 3))
1571601b
KH
2620 error ("Invalid depth in char-table");
2621 depth = XINT (AREF (tmp, 0));
6efdadfd 2622 if (chartab_size[depth] != size - 2)
3701b5de 2623 error ("Invalid size char-table");
985773c9 2624 XSETPVECTYPE (XVECTOR (tmp), PVEC_SUB_CHAR_TABLE);
3701b5de
KH
2625 return tmp;
2626 }
1761305f 2627 invalid_syntax ("#^" "^");
3701b5de 2628 }
e4d29b33 2629 invalid_syntax ("#^");
c2390933
RS
2630 }
2631 if (c == '&')
2632 {
2633 Lisp_Object length;
2634 length = read1 (readcharfun, pch, first_in_list);
2635 c = READCHAR;
2636 if (c == '"')
2637 {
2638 Lisp_Object tmp, val;
2cf00efc 2639 EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
df5b4930 2640 unsigned char *data;
c2390933
RS
2641
2642 UNREAD (c);
2643 tmp = read1 (readcharfun, pch, first_in_list);
8792be66 2644 if (STRING_MULTIBYTE (tmp)
8f924df7 2645 || (size_in_chars != SCHARS (tmp)
8792be66
KH
2646 /* We used to print 1 char too many
2647 when the number of bits was a multiple of 8.
2648 Accept such input in case it came from an old
2649 version. */
2650 && ! (XFASTINT (length)
327719ee 2651 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
e4d29b33 2652 invalid_syntax ("#&...");
177c0ea7 2653
2cf00efc 2654 val = make_uninit_bool_vector (XFASTINT (length));
df5b4930
PE
2655 data = bool_vector_uchar_data (val);
2656 memcpy (data, SDATA (tmp), size_in_chars);
67d3b149 2657 /* Clear the extraneous bits in the last byte. */
d1ca81d9 2658 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
df5b4930 2659 data[size_in_chars - 1]
d1ca81d9 2660 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
c2390933
RS
2661 return val;
2662 }
e4d29b33 2663 invalid_syntax ("#&...");
c2390933 2664 }
200f684e 2665 if (c == '[')
876c194c
SM
2666 {
2667 /* Accept compiled functions at read-time so that we don't have to
2668 build them using function calls. */
2669 Lisp_Object tmp;
56a0e352 2670 struct Lisp_Vector *vec;
876c194c 2671 tmp = read_vector (readcharfun, 1);
56a0e352
PE
2672 vec = XVECTOR (tmp);
2673 if (vec->header.size == 0)
ba355de0
BR
2674 invalid_syntax ("Empty byte-code object");
2675 make_byte_code (vec);
3017f87f 2676 return tmp;
876c194c 2677 }
748ef62f
RS
2678 if (c == '(')
2679 {
2680 Lisp_Object tmp;
2681 struct gcpro gcpro1;
e28552a4 2682 int ch;
748ef62f
RS
2683
2684 /* Read the string itself. */
17634846 2685 tmp = read1 (readcharfun, &ch, 0);
6428369f 2686 if (ch != 0 || !STRINGP (tmp))
e4d29b33 2687 invalid_syntax ("#");
748ef62f
RS
2688 GCPRO1 (tmp);
2689 /* Read the intervals and their properties. */
2690 while (1)
2691 {
2692 Lisp_Object beg, end, plist;
2693
17634846 2694 beg = read1 (readcharfun, &ch, 0);
7ee3bd7b 2695 end = plist = Qnil;
6428369f
KH
2696 if (ch == ')')
2697 break;
2698 if (ch == 0)
17634846 2699 end = read1 (readcharfun, &ch, 0);
6428369f 2700 if (ch == 0)
17634846 2701 plist = read1 (readcharfun, &ch, 0);
6428369f 2702 if (ch)
e4d29b33 2703 invalid_syntax ("Invalid string property list");
748ef62f
RS
2704 Fset_text_properties (beg, end, plist, tmp);
2705 }
2706 UNGCPRO;
2707 return tmp;
2708 }
177c0ea7 2709
75a65c7e 2710 /* #@NUMBER is used to skip NUMBER following bytes.
20ea2964
RS
2711 That's used in .elc files to skip over doc strings
2712 and function definitions. */
2713 if (c == '@')
2714 {
bfbbd7e7 2715 enum { extra = 100 };
759fd763 2716 ptrdiff_t i, nskip = 0, digits = 0;
20ea2964
RS
2717
2718 /* Read a decimal integer. */
2719 while ((c = READCHAR) >= 0
2720 && c >= '0' && c <= '9')
2721 {
bfbbd7e7
PE
2722 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2723 string_overflow ();
759fd763 2724 digits++;
20ea2964
RS
2725 nskip *= 10;
2726 nskip += c - '0';
759fd763
SM
2727 if (digits == 2 && nskip == 0)
2728 { /* We've just seen #@00, which means "skip to end". */
2729 skip_dyn_eof (readcharfun);
2730 return Qnil;
2731 }
20ea2964 2732 }
75a65c7e
SM
2733 if (nskip > 0)
2734 /* We can't use UNREAD here, because in the code below we side-step
2735 READCHAR. Instead, assume the first char after #@NNN occupies
2736 a single byte, which is the case normally since it's just
2737 a space. */
2738 nskip--;
2739 else
2740 UNREAD (c);
7216e43b 2741
8792be66 2742 if (load_force_doc_strings
d2e24f92 2743 && (FROM_FILE_P (readcharfun)))
b2a30870
RS
2744 {
2745 /* If we are supposed to force doc strings into core right now,
2746 record the last string that we skipped,
2747 and record where in the file it comes from. */
c15cfd1f
RS
2748
2749 /* But first exchange saved_doc_string
2750 with prev_saved_doc_string, so we save two strings. */
2751 {
2752 char *temp = saved_doc_string;
bfbbd7e7 2753 ptrdiff_t temp_size = saved_doc_string_size;
68c45bf0 2754 file_offset temp_pos = saved_doc_string_position;
bfbbd7e7 2755 ptrdiff_t temp_len = saved_doc_string_length;
c15cfd1f
RS
2756
2757 saved_doc_string = prev_saved_doc_string;
2758 saved_doc_string_size = prev_saved_doc_string_size;
2759 saved_doc_string_position = prev_saved_doc_string_position;
2760 saved_doc_string_length = prev_saved_doc_string_length;
2761
2762 prev_saved_doc_string = temp;
2763 prev_saved_doc_string_size = temp_size;
2764 prev_saved_doc_string_position = temp_pos;
2765 prev_saved_doc_string_length = temp_len;
2766 }
2767
b2a30870
RS
2768 if (saved_doc_string_size == 0)
2769 {
c1ade6f7 2770 saved_doc_string = xmalloc_atomic (nskip + extra);
bfbbd7e7 2771 saved_doc_string_size = nskip + extra;
b2a30870
RS
2772 }
2773 if (nskip > saved_doc_string_size)
2774 {
38182d90 2775 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
37d0112b 2776 saved_doc_string_size = nskip + extra;
b2a30870
RS
2777 }
2778
68c45bf0 2779 saved_doc_string_position = file_tell (instream);
b2a30870
RS
2780
2781 /* Copy that many characters into saved_doc_string. */
75a65c7e 2782 block_input ();
b2a30870 2783 for (i = 0; i < nskip && c >= 0; i++)
75a65c7e
SM
2784 saved_doc_string[i] = c = getc (instream);
2785 unblock_input ();
b2a30870
RS
2786
2787 saved_doc_string_length = i;
2788 }
2789 else
75a65c7e
SM
2790 /* Skip that many bytes. */
2791 skip_dyn_bytes (readcharfun, nskip);
d49f0c1a 2792
20ea2964
RS
2793 goto retry;
2794 }
e2518d02
RS
2795 if (c == '!')
2796 {
2797 /* #! appears at the beginning of an executable file.
2798 Skip the first line. */
225c7a07 2799 while (c != '\n' && c >= 0)
e2518d02
RS
2800 c = READCHAR;
2801 goto retry;
2802 }
20ea2964
RS
2803 if (c == '$')
2804 return Vload_file_name;
2b6cae0c 2805 if (c == '\'')
6c6f1994 2806 return list2 (Qfunction, read0 (readcharfun));
4ad679f9
EN
2807 /* #:foo is the uninterned symbol named foo. */
2808 if (c == ':')
2809 {
2810 uninterned_symbol = 1;
2811 c = READCHAR;
52968808 2812 if (!(c > 040
ed497dd4 2813 && c != 0xa0 /* NBSP */
52968808 2814 && (c >= 0200
0a6a104b 2815 || strchr ("\"';()[]#`,", c) == NULL)))
52968808
AS
2816 {
2817 /* No symbol character follows, this is the empty
2818 symbol. */
0a6a104b 2819 UNREAD (c);
4a7edc24 2820 return Fmake_symbol (empty_unibyte_string);
52968808
AS
2821 }
2822 goto read_symbol;
4ad679f9 2823 }
9a79b20c
AS
2824 /* ## is the empty symbol. */
2825 if (c == '#')
4a7edc24 2826 return Fintern (empty_unibyte_string, Qnil);
4ad679f9
EN
2827 /* Reader forms that can reuse previously read objects. */
2828 if (c >= '0' && c <= '9')
2829 {
8b9ac8b4 2830 EMACS_INT n = 0;
4ad679f9 2831 Lisp_Object tem;
2b6cae0c 2832
4ad679f9
EN
2833 /* Read a non-negative integer. */
2834 while (c >= '0' && c <= '9')
2835 {
7216e43b
PE
2836 if (MOST_POSITIVE_FIXNUM / 10 < n
2837 || MOST_POSITIVE_FIXNUM < n * 10 + c - '0')
8b9ac8b4
PE
2838 n = MOST_POSITIVE_FIXNUM + 1;
2839 else
2840 n = n * 10 + c - '0';
4ad679f9
EN
2841 c = READCHAR;
2842 }
8b9ac8b4
PE
2843
2844 if (n <= MOST_POSITIVE_FIXNUM)
4ad679f9 2845 {
8b9ac8b4
PE
2846 if (c == 'r' || c == 'R')
2847 return read_integer (readcharfun, n);
9e062b6c 2848
8b9ac8b4
PE
2849 if (! NILP (Vread_circle))
2850 {
2851 /* #n=object returns object, but associates it with
2852 n for #n#. */
2853 if (c == '=')
2854 {
48def666 2855 /* Make a placeholder for #n# to use temporarily. */
8b9ac8b4
PE
2856 Lisp_Object placeholder;
2857 Lisp_Object cell;
9e062b6c 2858
8b9ac8b4
PE
2859 placeholder = Fcons (Qnil, Qnil);
2860 cell = Fcons (make_number (n), placeholder);
2861 read_objects = Fcons (cell, read_objects);
9e062b6c 2862
48def666 2863 /* Read the object itself. */
8b9ac8b4 2864 tem = read0 (readcharfun);
9e062b6c 2865
48def666 2866 /* Now put it everywhere the placeholder was... */
8b9ac8b4 2867 substitute_object_in_subtree (tem, placeholder);
177c0ea7 2868
8b9ac8b4
PE
2869 /* ...and #n# will use the real value from now on. */
2870 Fsetcdr (cell, tem);
177c0ea7 2871
8b9ac8b4
PE
2872 return tem;
2873 }
2874
2875 /* #n# returns a previously read object. */
2876 if (c == '#')
2877 {
2878 tem = Fassq (make_number (n), read_objects);
2879 if (CONSP (tem))
2880 return XCDR (tem);
2881 }
2882 }
2883 }
4ad679f9
EN
2884 /* Fall through to error message. */
2885 }
bf5d1a17
GM
2886 else if (c == 'x' || c == 'X')
2887 return read_integer (readcharfun, 16);
2888 else if (c == 'o' || c == 'O')
2889 return read_integer (readcharfun, 8);
2890 else if (c == 'b' || c == 'B')
2891 return read_integer (readcharfun, 2);
20ea2964 2892
200f684e 2893 UNREAD (c);
e4d29b33 2894 invalid_syntax ("#");
078e7b4a
JB
2895
2896 case ';':
2897 while ((c = READCHAR) >= 0 && c != '\n');
2898 goto retry;
2899
2900 case '\'':
6c6f1994 2901 return list2 (Qquote, read0 (readcharfun));
078e7b4a 2902
17634846 2903 case '`':
9a39b306
TO
2904 {
2905 int next_char = READCHAR;
2906 UNREAD (next_char);
2907 /* Transition from old-style to new-style:
2908 If we see "(`" it used to mean old-style, which usually works
2909 fine because ` should almost never appear in such a position
2910 for new-style. But occasionally we need "(`" to mean new
2911 style, so we try to distinguish the two by the fact that we
2912 can either write "( `foo" or "(` foo", where the first
2913 intends to use new-style whereas the second intends to use
2914 old-style. For Emacs-25, we should completely remove this
2915 first_in_list exception (old-style can still be obtained via
2916 "(\`" anyway). */
bba3e508 2917 if (!new_backquote_flag && first_in_list && next_char == ' ')
9a39b306
TO
2918 {
2919 Vold_style_backquotes = Qt;
2920 goto default_label;
2921 }
2922 else
2923 {
2924 Lisp_Object value;
7cded46f 2925 bool saved_new_backquote_flag = new_backquote_flag;
17634846 2926
7cded46f 2927 new_backquote_flag = 1;
9a39b306 2928 value = read0 (readcharfun);
7cded46f 2929 new_backquote_flag = saved_new_backquote_flag;
17634846 2930
6c6f1994 2931 return list2 (Qbackquote, value);
9a39b306
TO
2932 }
2933 }
17634846 2934 case ',':
bba3e508
SM
2935 {
2936 int next_char = READCHAR;
2937 UNREAD (next_char);
2938 /* Transition from old-style to new-style:
2939 It used to be impossible to have a new-style , other than within
2940 a new-style `. This is sufficient when ` and , are used in the
2941 normal way, but ` and , can also appear in args to macros that
2942 will not interpret them in the usual way, in which case , may be
2943 used without any ` anywhere near.
2944 So we now use the same heuristic as for backquote: old-style
2945 unquotes are only recognized when first on a list, and when
2946 followed by a space.
333f9019 2947 Because it's more difficult to peek 2 chars ahead, a new-style
bba3e508
SM
2948 ,@ can still not be used outside of a `, unless it's in the middle
2949 of a list. */
2950 if (new_backquote_flag
2951 || !first_in_list
2952 || (next_char != ' ' && next_char != '@'))
2953 {
2954 Lisp_Object comma_type = Qnil;
2955 Lisp_Object value;
2956 int ch = READCHAR;
17634846 2957
bba3e508
SM
2958 if (ch == '@')
2959 comma_type = Qcomma_at;
2960 else if (ch == '.')
2961 comma_type = Qcomma_dot;
2962 else
2963 {
2964 if (ch >= 0) UNREAD (ch);
2965 comma_type = Qcomma;
2966 }
17634846 2967
bba3e508 2968 value = read0 (readcharfun);
6c6f1994 2969 return list2 (comma_type, value);
bba3e508
SM
2970 }
2971 else
2972 {
2973 Vold_style_backquotes = Qt;
2974 goto default_label;
2975 }
2976 }
078e7b4a
JB
2977 case '?':
2978 {
8792be66 2979 int modifiers;
df9c2be7 2980 int next_char;
7cded46f 2981 bool ok;
f6f79b37 2982
078e7b4a 2983 c = READCHAR;
9c97398c
GM
2984 if (c < 0)
2985 end_of_file_error ();
078e7b4a 2986
b9284371
KS
2987 /* Accept `single space' syntax like (list ? x) where the
2988 whitespace character is SPC or TAB.
2989 Other literal whitespace like NL, CR, and FF are not accepted,
2990 as there are well-established escape sequences for these. */
2991 if (c == ' ' || c == '\t')
2992 return make_number (c);
2993
078e7b4a 2994 if (c == '\\')
8792be66
KH
2995 c = read_escape (readcharfun, 0);
2996 modifiers = c & CHAR_MODIFIER_MASK;
2997 c &= ~CHAR_MODIFIER_MASK;
2998 if (CHAR_BYTE8_P (c))
2999 c = CHAR_TO_BYTE8 (c);
3000 c |= modifiers;
078e7b4a 3001
df9c2be7 3002 next_char = READCHAR;
bba3e508
SM
3003 ok = (next_char <= 040
3004 || (next_char < 0200
0a6a104b 3005 && strchr ("\"';()[]#?`,.", next_char) != NULL));
df9c2be7 3006 UNREAD (next_char);
336d4a9c
KS
3007 if (ok)
3008 return make_number (c);
37cd4238 3009
e4d29b33 3010 invalid_syntax ("?");
078e7b4a
JB
3011 }
3012
00a9a935 3013 case '"':
078e7b4a 3014 {
a742d646
GM
3015 char *p = read_buffer;
3016 char *end = read_buffer + read_buffer_size;
7cded46f
PE
3017 int ch;
3018 /* True if we saw an escape sequence specifying
1571601b 3019 a multibyte character. */
7cded46f
PE
3020 bool force_multibyte = 0;
3021 /* True if we saw an escape sequence specifying
e7fc914b 3022 a single-byte character. */
7cded46f
PE
3023 bool force_singlebyte = 0;
3024 bool cancel = 0;
82cb60d3 3025 ptrdiff_t nchars = 0;
078e7b4a 3026
0902fe45
PE
3027 while ((ch = READCHAR) >= 0
3028 && ch != '\"')
078e7b4a 3029 {
449fea39 3030 if (end - p < MAX_MULTIBYTE_LENGTH)
078e7b4a 3031 {
82cb60d3 3032 ptrdiff_t offset = p - read_buffer;
7216e43b 3033 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
20270765 3034 memory_full (SIZE_MAX);
38182d90 3035 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
37d0112b 3036 read_buffer_size *= 2;
5d65df0d 3037 p = read_buffer + offset;
078e7b4a
JB
3038 end = read_buffer + read_buffer_size;
3039 }
bed23cb2 3040
0902fe45 3041 if (ch == '\\')
03e88613 3042 {
1571601b 3043 int modifiers;
f6f79b37 3044
0902fe45 3045 ch = read_escape (readcharfun, 1);
bed23cb2 3046
48def666 3047 /* CH is -1 if \ newline has just been seen. */
0902fe45 3048 if (ch == -1)
03e88613 3049 {
bed23cb2
RS
3050 if (p == read_buffer)
3051 cancel = 1;
03e88613
RS
3052 continue;
3053 }
bed23cb2 3054
0902fe45
PE
3055 modifiers = ch & CHAR_MODIFIER_MASK;
3056 ch = ch & ~CHAR_MODIFIER_MASK;
1571601b 3057
0902fe45 3058 if (CHAR_BYTE8_P (ch))
e7fc914b 3059 force_singlebyte = 1;
0902fe45 3060 else if (! ASCII_CHAR_P (ch))
f6f79b37 3061 force_multibyte = 1;
48def666 3062 else /* I.e. ASCII_CHAR_P (ch). */
1571601b
KH
3063 {
3064 /* Allow `\C- ' and `\C-?'. */
3065 if (modifiers == CHAR_CTL)
3066 {
0902fe45
PE
3067 if (ch == ' ')
3068 ch = 0, modifiers = 0;
3069 else if (ch == '?')
3070 ch = 127, modifiers = 0;
1571601b
KH
3071 }
3072 if (modifiers & CHAR_SHIFT)
3073 {
3074 /* Shift modifier is valid only with [A-Za-z]. */
0902fe45 3075 if (ch >= 'A' && ch <= 'Z')
1571601b 3076 modifiers &= ~CHAR_SHIFT;
0902fe45
PE
3077 else if (ch >= 'a' && ch <= 'z')
3078 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
1571601b
KH
3079 }
3080
3081 if (modifiers & CHAR_META)
3082 {
3083 /* Move the meta bit to the right place for a
3084 string. */
3085 modifiers &= ~CHAR_META;
0902fe45 3086 ch = BYTE8_TO_CHAR (ch | 0x80);
1571601b
KH
3087 force_singlebyte = 1;
3088 }
3089 }
5150eeec 3090
1571601b
KH
3091 /* Any modifiers remaining are invalid. */
3092 if (modifiers)
3093 error ("Invalid modifier in string");
0902fe45 3094 p += CHAR_STRING (ch, (unsigned char *) p);
078e7b4a 3095 }
8792be66 3096 else
5150eeec 3097 {
0902fe45
PE
3098 p += CHAR_STRING (ch, (unsigned char *) p);
3099 if (CHAR_BYTE8_P (ch))
988f7a0c 3100 force_singlebyte = 1;
0902fe45 3101 else if (! ASCII_CHAR_P (ch))
988f7a0c 3102 force_multibyte = 1;
f943104a 3103 }
5150eeec 3104 nchars++;
078e7b4a 3105 }
5150eeec 3106
0902fe45 3107 if (ch < 0)
9c97398c 3108 end_of_file_error ();
078e7b4a 3109
4cfd81f6 3110 if (! force_multibyte && force_singlebyte)
a742d646 3111 {
4cfd81f6
PE
3112 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3113 forms. Convert it to unibyte. */
4a25e32a
PE
3114 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3115 p - read_buffer);
1571601b 3116 p = read_buffer + nchars;
a742d646 3117 }
e7fc914b 3118
491f16a2 3119 return make_specified_string (read_buffer, nchars, p - read_buffer,
1571601b
KH
3120 (force_multibyte
3121 || (p - read_buffer != nchars)));
078e7b4a
JB
3122 }
3123
109d300c
JB
3124 case '.':
3125 {
109d300c
JB
3126 int next_char = READCHAR;
3127 UNREAD (next_char);
3128
035eec48 3129 if (next_char <= 040
e613ea97 3130 || (next_char < 0200
0a6a104b 3131 && strchr ("\"';([#?`,", next_char) != NULL))
109d300c 3132 {
6428369f
KH
3133 *pch = c;
3134 return Qnil;
109d300c
JB
3135 }
3136
3137 /* Otherwise, we fall through! Note that the atom-reading loop
3138 below will now loop at least once, assuring that we will not
3139 try to UNREAD two characters in a row. */
3140 }
078e7b4a 3141 default:
17634846 3142 default_label:
88852d45 3143 if (c <= 040) goto retry;
ed497dd4 3144 if (c == 0xa0) /* NBSP */
adef3de7 3145 goto retry;
52968808
AS
3146
3147 read_symbol:
078e7b4a 3148 {
38404229 3149 char *p = read_buffer;
7cded46f 3150 bool quoted = 0;
46cfea45 3151 EMACS_INT start_position = readchar_count - 1;
078e7b4a
JB
3152
3153 {
38404229 3154 char *end = read_buffer + read_buffer_size;
078e7b4a 3155
ef1b0ba7 3156 do
078e7b4a 3157 {
449fea39 3158 if (end - p < MAX_MULTIBYTE_LENGTH)
078e7b4a 3159 {
82cb60d3 3160 ptrdiff_t offset = p - read_buffer;
7216e43b 3161 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
20270765 3162 memory_full (SIZE_MAX);
38182d90 3163 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
37d0112b 3164 read_buffer_size *= 2;
5d65df0d 3165 p = read_buffer + offset;
078e7b4a
JB
3166 end = read_buffer + read_buffer_size;
3167 }
177c0ea7 3168
078e7b4a 3169 if (c == '\\')
481c6336
RS
3170 {
3171 c = READCHAR;
4ab11c09
GM
3172 if (c == -1)
3173 end_of_file_error ();
481c6336
RS
3174 quoted = 1;
3175 }
6f7f43d5 3176
1202434b 3177 if (multibyte)
4a25e32a 3178 p += CHAR_STRING (c, (unsigned char *) p);
1202434b
KH
3179 else
3180 *p++ = c;
078e7b4a 3181 c = READCHAR;
0a6a104b
AS
3182 }
3183 while (c > 040
ed497dd4 3184 && c != 0xa0 /* NBSP */
0a6a104b
AS
3185 && (c >= 0200
3186 || strchr ("\"';()[]#`,", c) == NULL));
078e7b4a
JB
3187
3188 if (p == end)
3189 {
82cb60d3 3190 ptrdiff_t offset = p - read_buffer;
7216e43b 3191 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
20270765 3192 memory_full (SIZE_MAX);
38182d90 3193 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
37d0112b 3194 read_buffer_size *= 2;
5d65df0d
GM
3195 p = read_buffer + offset;
3196 end = read_buffer + read_buffer_size;
078e7b4a
JB
3197 }
3198 *p = 0;
0a6a104b 3199 UNREAD (c);
078e7b4a
JB
3200 }
3201
4ad679f9 3202 if (!quoted && !uninterned_symbol)
481c6336 3203 {
452f4150
PE
3204 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3205 if (! NILP (result))
8b9587d7 3206 return result;
481c6336 3207 }
abb13b09 3208 {
e93abe3d 3209 Lisp_Object name, result;
d311d28c
PE
3210 ptrdiff_t nbytes = p - read_buffer;
3211 ptrdiff_t nchars
4a25e32a
PE
3212 = (multibyte
3213 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3214 nbytes)
e93abe3d
KH
3215 : nbytes);
3216
61b108cc
SM
3217 name = ((uninterned_symbol && ! NILP (Vpurify_flag)
3218 ? make_pure_string : make_specified_string)
3219 (read_buffer, nchars, nbytes, multibyte));
e93abe3d
KH
3220 result = (uninterned_symbol ? Fmake_symbol (name)
3221 : Fintern (name, Qnil));
1202434b 3222
abb13b09
CW
3223 if (EQ (Vread_with_symbol_positions, Qt)
3224 || EQ (Vread_with_symbol_positions, readcharfun))
61b108cc
SM
3225 Vread_symbol_positions_list
3226 = Fcons (Fcons (result, make_number (start_position)),
3227 Vread_symbol_positions_list);
abb13b09
CW
3228 return result;
3229 }
078e7b4a
JB
3230 }
3231 }
3232}
3233\f
9e062b6c 3234
48def666 3235/* List of nodes we've seen during substitute_object_in_subtree. */
9e062b6c
RS
3236static Lisp_Object seen_list;
3237
3238static void
971de7fb 3239substitute_object_in_subtree (Lisp_Object object, Lisp_Object placeholder)
9e062b6c
RS
3240{
3241 Lisp_Object check_object;
3242
48def666 3243 /* We haven't seen any objects when we start. */
9e062b6c
RS
3244 seen_list = Qnil;
3245
48def666 3246 /* Make all the substitutions. */
9e062b6c
RS
3247 check_object
3248 = substitute_object_recurse (object, placeholder, object);
177c0ea7 3249
48def666 3250 /* Clear seen_list because we're done with it. */
9e062b6c
RS
3251 seen_list = Qnil;
3252
3253 /* The returned object here is expected to always eq the
48def666 3254 original. */
9e062b6c
RS
3255 if (!EQ (check_object, object))
3256 error ("Unexpected mutation error in reader");
3257}
3258
48def666 3259/* Feval doesn't get called from here, so no gc protection is needed. */
7a3d90dc
SM
3260#define SUBSTITUTE(get_val, set_val) \
3261 do { \
3262 Lisp_Object old_value = get_val; \
3263 Lisp_Object true_value \
3264 = substitute_object_recurse (object, placeholder, \
3265 old_value); \
3266 \
3267 if (!EQ (old_value, true_value)) \
3268 { \
3269 set_val; \
3270 } \
3271 } while (0)
9e062b6c
RS
3272
3273static Lisp_Object
971de7fb 3274substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Object subtree)
9e062b6c 3275{
48def666 3276 /* If we find the placeholder, return the target object. */
9e062b6c
RS
3277 if (EQ (placeholder, subtree))
3278 return object;
3279
48def666 3280 /* If we've been to this node before, don't explore it again. */
9e062b6c
RS
3281 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3282 return subtree;
3283
3284 /* If this node can be the entry point to a cycle, remember that
3285 we've seen it. It can only be such an entry point if it was made
3286 by #n=, which means that we can find it as a value in
3287 read_objects. */
3288 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3289 seen_list = Fcons (subtree, seen_list);
177c0ea7 3290
9e062b6c
RS
3291 /* Recurse according to subtree's type.
3292 Every branch must return a Lisp_Object. */
2b8f856a 3293 if (VECTORLIKEP (subtree))
9e062b6c 3294 {
2b8f856a
BT
3295 ptrdiff_t i, length = 0;
3296 if (BOOL_VECTOR_P (subtree))
3297 return subtree; /* No sub-objects anyway. */
3298 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3299 || COMPILEDP (subtree) || HASH_TABLE_P (subtree))
3300 length = ASIZE (subtree) & PSEUDOVECTOR_SIZE_MASK;
3301 else if (VECTORP (subtree))
3302 length = ASIZE (subtree);
3303 else
3304 /* An unknown pseudovector may contain non-Lisp fields, so we
3305 can't just blindly traverse all its fields. We used to call
3306 `Flength' which signaled `sequencep', so I just preserved this
3307 behavior. */
3308 wrong_type_argument (Qsequencep, subtree);
3309
3310 for (i = 0; i < length; i++)
3311 SUBSTITUTE (AREF (subtree, i),
3312 ASET (subtree, i, true_value));
3313 return subtree;
3314 }
3315 else if (CONSP (subtree))
3316 {
3317 SUBSTITUTE (XCAR (subtree),
3318 XSETCAR (subtree, true_value));
3319 SUBSTITUTE (XCDR (subtree),
3320 XSETCDR (subtree, true_value));
3321 return subtree;
3322 }
3323 else if (STRINGP (subtree))
3324 {
3325 /* Check for text properties in each interval.
3326 substitute_in_interval contains part of the logic. */
177c0ea7 3327
2b8f856a
BT
3328 INTERVAL root_interval = string_intervals (subtree);
3329 Lisp_Object arg = Fcons (object, placeholder);
9e062b6c 3330
2b8f856a
BT
3331 traverse_intervals_noorder (root_interval,
3332 &substitute_in_interval, arg);
9e062b6c 3333
9e062b6c
RS
3334 return subtree;
3335 }
2b8f856a
BT
3336 else
3337 /* Other types don't recurse any further. */
3338 return subtree;
9e062b6c
RS
3339}
3340
3341/* Helper function for substitute_object_recurse. */
3342static void
971de7fb 3343substitute_in_interval (INTERVAL interval, Lisp_Object arg)
9e062b6c
RS
3344{
3345 Lisp_Object object = Fcar (arg);
3346 Lisp_Object placeholder = Fcdr (arg);
3347
0c94c8d6 3348 SUBSTITUTE (interval->plist, set_interval_plist (interval, true_value));
9e062b6c
RS
3349}
3350
3351\f
078e7b4a
JB
3352#define LEAD_INT 1
3353#define DOT_CHAR 2
3354#define TRAIL_INT 4
452f4150 3355#define E_EXP 16
078e7b4a 3356
8b9587d7 3357
452f4150
PE
3358/* Convert STRING to a number, assuming base BASE. Return a fixnum if CP has
3359 integer syntax and fits in a fixnum, else return the nearest float if CP has
3360 either floating point or integer syntax and BASE is 10, else return nil. If
7cded46f 3361 IGNORE_TRAILING, consider just the longest prefix of CP that has
452f4150
PE
3362 valid floating point syntax. Signal an overflow if BASE is not 10 and the
3363 number has integer syntax but does not fit. */
8b9587d7
PE
3364
3365Lisp_Object
7cded46f 3366string_to_number (char const *string, int base, bool ignore_trailing)
078e7b4a 3367{
a8fe7202 3368 int state;
452f4150
PE
3369 char const *cp = string;
3370 int leading_digit;
7cded46f 3371 bool float_syntax = 0;
452f4150 3372 double value = 0;
d8578e58 3373
8b9587d7
PE
3374 /* Compute NaN and infinities using a variable, to cope with compilers that
3375 think they are smarter than we are. */
3376 double zero = 0;
3377
3378 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3379 IEEE floating point hosts, and works around a formerly-common bug where
3380 atof ("-0.0") drops the sign. */
7cded46f 3381 bool negative = *cp == '-';
8b9587d7 3382
7cded46f 3383 bool signedp = negative || *cp == '+';
452f4150 3384 cp += signedp;
8b9587d7 3385
078e7b4a 3386 state = 0;
078e7b4a 3387
452f4150 3388 leading_digit = digit_to_number (*cp, base);
908589fd 3389 if (leading_digit >= 0)
078e7b4a
JB
3390 {
3391 state |= LEAD_INT;
452f4150
PE
3392 do
3393 ++cp;
908589fd 3394 while (digit_to_number (*cp, base) >= 0);
078e7b4a
JB
3395 }
3396 if (*cp == '.')
3397 {
3398 state |= DOT_CHAR;
3399 cp++;
3400 }
078e7b4a 3401
452f4150 3402 if (base == 10)
eb659c41 3403 {
452f4150
PE
3404 if ('0' <= *cp && *cp <= '9')
3405 {
3406 state |= TRAIL_INT;
3407 do
3408 cp++;
3409 while ('0' <= *cp && *cp <= '9');
3410 }
3411 if (*cp == 'e' || *cp == 'E')
3412 {
3413 char const *ecp = cp;
3414 cp++;
3415 if (*cp == '+' || *cp == '-')
3416 cp++;
3417 if ('0' <= *cp && *cp <= '9')
3418 {
3419 state |= E_EXP;
3420 do
3421 cp++;
3422 while ('0' <= *cp && *cp <= '9');
3423 }
3424 else if (cp[-1] == '+'
3425 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3426 {
3427 state |= E_EXP;
3428 cp += 3;
3429 value = 1.0 / zero;
3430 }
3431 else if (cp[-1] == '+'
3432 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3433 {
3434 state |= E_EXP;
3435 cp += 3;
3436 value = zero / zero;
3437
3438 /* If that made a "negative" NaN, negate it. */
3439 {
3440 int i;
3441 union { double d; char c[sizeof (double)]; }
3442 u_data, u_minus_zero;
3443 u_data.d = value;
3444 u_minus_zero.d = -0.0;
3445 for (i = 0; i < sizeof (double); i++)
3446 if (u_data.c[i] & u_minus_zero.c[i])
3447 {
3448 value = -value;
3449 break;
3450 }
3451 }
3452 /* Now VALUE is a positive NaN. */
3453 }
3454 else
3455 cp = ecp;
3456 }
3457
3458 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3459 || state == (LEAD_INT|E_EXP));
eb659c41 3460 }
8b9587d7 3461
452f4150
PE
3462 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3463 any prefix that matches. Otherwise, the entire string must match. */
3464 if (! (ignore_trailing
3465 ? ((state & LEAD_INT) != 0 || float_syntax)
3466 : (!*cp && ((state & ~DOT_CHAR) == LEAD_INT || float_syntax))))
3467 return Qnil;
8b9587d7 3468
d78050d6
PE
3469 /* If the number uses integer and not float syntax, and is in C-language
3470 range, use its value, preferably as a fixnum. */
908589fd 3471 if (leading_digit >= 0 && ! float_syntax)
452f4150 3472 {
d78050d6
PE
3473 uintmax_t n;
3474
3475 /* Fast special case for single-digit integers. This also avoids a
7cded46f 3476 glitch when BASE is 16 and IGNORE_TRAILING, because in that
d78050d6
PE
3477 case some versions of strtoumax accept numbers like "0x1" that Emacs
3478 does not allow. */
3479 if (digit_to_number (string[signedp + 1], base) < 0)
3480 return make_number (negative ? -leading_digit : leading_digit);
3481
3482 errno = 0;
3483 n = strtoumax (string + signedp, NULL, base);
3484 if (errno == ERANGE)
452f4150 3485 {
d78050d6
PE
3486 /* Unfortunately there's no simple and accurate way to convert
3487 non-base-10 numbers that are out of C-language range. */
3488 if (base != 10)
f14ed477 3489 xsignal1 (Qoverflow_error, build_string (string));
452f4150 3490 }
d78050d6
PE
3491 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3492 {
3493 EMACS_INT signed_n = n;
3494 return make_number (negative ? -signed_n : signed_n);
3495 }
3496 else
3497 value = n;
452f4150 3498 }
8b9587d7 3499
452f4150
PE
3500 /* Either the number uses float syntax, or it does not fit into a fixnum.
3501 Convert it from string to floating point, unless the value is already
d78050d6
PE
3502 known because it is an infinity, a NAN, or its absolute value fits in
3503 uintmax_t. */
8b9587d7 3504 if (! value)
452f4150
PE
3505 value = atof (string + signedp);
3506
d78050d6 3507 return make_float (negative ? -value : value);
078e7b4a 3508}
cc94f3b2 3509
078e7b4a
JB
3510\f
3511static Lisp_Object
7cded46f 3512read_vector (Lisp_Object readcharfun, bool bytecodeflag)
078e7b4a 3513{
82cb60d3 3514 ptrdiff_t i, size;
7cded46f
PE
3515 Lisp_Object *ptr;
3516 Lisp_Object tem, item, vector;
078e7b4a
JB
3517 Lisp_Object len;
3518
3519 tem = read_list (1, readcharfun);
3520 len = Flength (tem);
48def666 3521 vector = Fmake_vector (len, Qnil);
078e7b4a 3522
77b37c05 3523 size = ASIZE (vector);
91f2d272 3524 ptr = XVECTOR (vector)->contents;
078e7b4a
JB
3525 for (i = 0; i < size; i++)
3526 {
c15cfd1f
RS
3527 item = Fcar (tem);
3528 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3529 bytecode object, the docstring containing the bytecode and
3530 constants values must be treated as unibyte and passed to
3531 Fread, to get the actual bytecode string and constants vector. */
876c194c 3532 if (bytecodeflag && load_force_doc_strings)
c15cfd1f
RS
3533 {
3534 if (i == COMPILED_BYTECODE)
3535 {
3536 if (!STRINGP (item))
6fa2b890 3537 error ("Invalid byte code");
c15cfd1f
RS
3538
3539 /* Delay handling the bytecode slot until we know whether
3540 it is lazily-loaded (we can tell by whether the
3541 constants slot is nil). */
086ca913 3542 ASET (vector, COMPILED_CONSTANTS, item);
c15cfd1f
RS
3543 item = Qnil;
3544 }
3545 else if (i == COMPILED_CONSTANTS)
3546 {
3547 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3548
3549 if (NILP (item))
3550 {
3551 /* Coerce string to unibyte (like string-as-unibyte,
3552 but without generating extra garbage and
3553 guaranteeing no change in the contents). */
bee91904 3554 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
d5db4077 3555 STRING_SET_UNIBYTE (bytestr);
c15cfd1f 3556
8792be66 3557 item = Fread (Fcons (bytestr, readcharfun));
c15cfd1f 3558 if (!CONSP (item))
6fa2b890 3559 error ("Invalid byte code");
c15cfd1f 3560
c1d497be
KR
3561 bytestr = XCAR (item);
3562 item = XCDR (item);
c15cfd1f
RS
3563 }
3564
3565 /* Now handle the bytecode slot. */
086ca913 3566 ASET (vector, COMPILED_BYTECODE, bytestr);
c15cfd1f 3567 }
8792be66
KH
3568 else if (i == COMPILED_DOC_STRING
3569 && STRINGP (item)
3570 && ! STRING_MULTIBYTE (item))
3571 {
3572 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3573 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3574 else
3575 item = Fstring_as_multibyte (item);
3576 }
c15cfd1f 3577 }
086ca913 3578 ASET (vector, i, item);
078e7b4a 3579 tem = Fcdr (tem);
078e7b4a
JB
3580 }
3581 return vector;
3582}
177c0ea7 3583
f224e500 3584/* FLAG means check for ']' to terminate rather than ')' and '.'. */
078e7b4a
JB
3585
3586static Lisp_Object
7cded46f 3587read_list (bool flag, Lisp_Object readcharfun)
078e7b4a 3588{
078e7b4a 3589 Lisp_Object val, tail;
7cded46f 3590 Lisp_Object elt, tem;
078e7b4a 3591 struct gcpro gcpro1, gcpro2;
821d417e 3592 /* 0 is the normal case.
b2a30870 3593 1 means this list is a doc reference; replace it with the number 0.
177c0ea7 3594 2 means this list is a doc reference; replace it with the doc string. */
821d417e 3595 int doc_reference = 0;
078e7b4a 3596
17634846 3597 /* Initialize this to 1 if we are reading a list. */
7cded46f 3598 bool first_in_list = flag <= 0;
17634846 3599
078e7b4a
JB
3600 val = Qnil;
3601 tail = Qnil;
3602
3603 while (1)
3604 {
e28552a4 3605 int ch;
078e7b4a 3606 GCPRO2 (val, tail);
17634846 3607 elt = read1 (readcharfun, &ch, first_in_list);
078e7b4a 3608 UNGCPRO;
20ea2964 3609
17634846
RS
3610 first_in_list = 0;
3611
821d417e 3612 /* While building, if the list starts with #$, treat it specially. */
20ea2964 3613 if (EQ (elt, Vload_file_name)
d49f0c1a 3614 && ! NILP (elt)
821d417e
RS
3615 && !NILP (Vpurify_flag))
3616 {
3617 if (NILP (Vdoc_file_name))
3618 /* We have not yet called Snarf-documentation, so assume
6e911150 3619 this file is described in the DOC file
821d417e
RS
3620 and Snarf-documentation will fill in the right value later.
3621 For now, replace the whole list with 0. */
3622 doc_reference = 1;
3623 else
3624 /* We have already called Snarf-documentation, so make a relative
3625 file name for this file, so it can be found properly
3626 in the installed Lisp directory.
3627 We don't use Fexpand_file_name because that would make
3628 the directory absolute now. */
3629 elt = concat2 (build_string ("../lisp/"),
61b108cc 3630 Ffile_name_nondirectory (elt));
821d417e 3631 }
b2a30870 3632 else if (EQ (elt, Vload_file_name)
d49f0c1a 3633 && ! NILP (elt)
b2a30870
RS
3634 && load_force_doc_strings)
3635 doc_reference = 2;
20ea2964 3636
6428369f 3637 if (ch)
078e7b4a
JB
3638 {
3639 if (flag > 0)
3640 {
6428369f 3641 if (ch == ']')
078e7b4a 3642 return val;
e4d29b33 3643 invalid_syntax (") or . in a vector");
078e7b4a 3644 }
6428369f 3645 if (ch == ')')
078e7b4a 3646 return val;
6428369f 3647 if (ch == '.')
078e7b4a
JB
3648 {
3649 GCPRO2 (val, tail);
265a9e55 3650 if (!NILP (tail))
f5df591a 3651 XSETCDR (tail, read0 (readcharfun));
078e7b4a
JB
3652 else
3653 val = read0 (readcharfun);
17634846 3654 read1 (readcharfun, &ch, 0);
078e7b4a 3655 UNGCPRO;
6428369f 3656 if (ch == ')')
821d417e
RS
3657 {
3658 if (doc_reference == 1)
3659 return make_number (0);
d2e24f92 3660 if (doc_reference == 2 && INTEGERP (XCDR (val)))
b2a30870 3661 {
d2e24f92
SM
3662 char *saved = NULL;
3663 file_offset saved_position;
b2a30870 3664 /* Get a doc string from the file we are loading.
8792be66
KH
3665 If it's in saved_doc_string, get it from there.
3666
3667 Here, we don't know if the string is a
3668 bytecode string or a doc string. As a
3669 bytecode string must be unibyte, we always
3670 return a unibyte string. If it is actually a
3671 doc string, caller must make it
3672 multibyte. */
8f924df7 3673
c15cfd1f 3674 /* Position is negative for user variables. */
71376d4b 3675 EMACS_INT pos = eabs (XINT (XCDR (val)));
b2a30870
RS
3676 if (pos >= saved_doc_string_position
3677 && pos < (saved_doc_string_position
3678 + saved_doc_string_length))
3679 {
d2e24f92
SM
3680 saved = saved_doc_string;
3681 saved_position = saved_doc_string_position;
b2a30870 3682 }
c15cfd1f
RS
3683 /* Look in prev_saved_doc_string the same way. */
3684 else if (pos >= prev_saved_doc_string_position
3685 && pos < (prev_saved_doc_string_position
3686 + prev_saved_doc_string_length))
3687 {
d2e24f92
SM
3688 saved = prev_saved_doc_string;
3689 saved_position = prev_saved_doc_string_position;
3690 }
3691 if (saved)
3692 {
3693 ptrdiff_t start = pos - saved_position;
82cb60d3 3694 ptrdiff_t from, to;
c15cfd1f
RS
3695
3696 /* Process quoting with ^A,
3697 and find the end of the string,
3698 which is marked with ^_ (037). */
3699 for (from = start, to = start;
d2e24f92 3700 saved[from] != 037;)
c15cfd1f 3701 {
d2e24f92 3702 int c = saved[from++];
c15cfd1f
RS
3703 if (c == 1)
3704 {
d2e24f92
SM
3705 c = saved[from++];
3706 saved[to++] = (c == 1 ? c
3707 : c == '0' ? 0
3708 : c == '_' ? 037
3709 : c);
c15cfd1f
RS
3710 }
3711 else
d2e24f92 3712 saved[to++] = c;
c15cfd1f
RS
3713 }
3714
d2e24f92 3715 return make_unibyte_string (saved + start,
8792be66 3716 to - start);
c15cfd1f 3717 }
b2a30870 3718 else
8792be66 3719 return get_doc_string (val, 1, 0);
b2a30870
RS
3720 }
3721
821d417e
RS
3722 return val;
3723 }
e4d29b33 3724 invalid_syntax (". in wrong context");
078e7b4a 3725 }
e4d29b33 3726 invalid_syntax ("] in a list");
078e7b4a 3727 }
6c6f1994 3728 tem = list1 (elt);
265a9e55 3729 if (!NILP (tail))
f5df591a 3730 XSETCDR (tail, tem);
078e7b4a
JB
3731 else
3732 val = tem;
3733 tail = tem;
078e7b4a
JB
3734 }
3735}
3736\f
1e3890d1 3737static Lisp_Object initial_obarray;
078e7b4a 3738
d23d13c0
BT
3739Lisp_Object
3740obhash (Lisp_Object obarray)
3741{
3742 Lisp_Object tem = scm_hashq_get_handle (obarrays, obarray);
3743 if (SCM_UNLIKELY (scm_is_false (tem)))
3744 tem = scm_hashq_create_handle_x (obarrays, obarray,
3745 scm_make_obarray ());
3746 return scm_cdr (tem);
3747}
d007f5c8 3748
d007f5c8
RS
3749/* Get an error if OBARRAY is not an obarray.
3750 If it is one, return it. */
3751
078e7b4a 3752Lisp_Object
971de7fb 3753check_obarray (Lisp_Object obarray)
078e7b4a 3754{
77b37c05 3755 if (!VECTORP (obarray) || ASIZE (obarray) == 0)
078e7b4a
JB
3756 {
3757 /* If Vobarray is now invalid, force it to be valid. */
3758 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
8878319c 3759 wrong_type_argument (Qvectorp, obarray);
078e7b4a
JB
3760 }
3761 return obarray;
3762}
3763
d007f5c8
RS
3764/* Intern the C string STR: return a symbol with that name,
3765 interned in the current obarray. */
078e7b4a
JB
3766
3767Lisp_Object
e8df9267 3768intern_1 (const char *str, ptrdiff_t len)
078e7b4a 3769{
3250348f 3770 return Fintern (make_string (str, len), Qnil);
078e7b4a 3771}
4ad679f9 3772
5e2327cf 3773Lisp_Object
e8df9267 3774intern_c_string_1 (const char *str, ptrdiff_t len)
5e2327cf 3775{
3250348f
BT
3776 return Fintern (make_pure_c_string (str, len), Qnil);
3777}
3778\f
3779DEFUN ("find-symbol", Ffind_symbol, Sfind_symbol, 1, 2, 0,
3780 doc: /* find-symbol */)
3781 (Lisp_Object string, Lisp_Object obarray)
3782{
3783 Lisp_Object tem, sstring, found;
5e2327cf 3784
3250348f
BT
3785 obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
3786 CHECK_STRING (string);
5e2327cf 3787
3250348f
BT
3788 sstring = scm_from_utf8_stringn (SSDATA (string), SBYTES (string));
3789 tem = scm_find_symbol (sstring, obhash (obarray));
d1c3da7b 3790 if (scm_is_true (tem))
3250348f
BT
3791 {
3792 if (EQ (tem, Qnil_))
3793 tem = Qnil;
3794 else if (EQ (tem, Qt_))
3795 tem = Qt;
3796 return scm_values (scm_list_2 (tem, Qt));
3797 }
3798 else
3799 return scm_values (scm_list_2 (Qnil, Qnil));
5e2327cf 3800}
3250348f 3801
a7ca3326 3802DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
5de38842
PJ
3803 doc: /* Return the canonical symbol whose name is STRING.
3804If there is none, one is created by this function and returned.
3805A second optional argument specifies the obarray to use;
3806it defaults to the value of `obarray'. */)
5842a27b 3807 (Lisp_Object string, Lisp_Object obarray)
078e7b4a
JB
3808{
3809 register Lisp_Object tem, sym, *ptr;
3810
265a9e55 3811 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
3812 obarray = check_obarray (obarray);
3813
b7826503 3814 CHECK_STRING (string);
078e7b4a 3815
3250348f
BT
3816 tem = Ffind_symbol (string, obarray);
3817 if (! NILP (scm_c_value_ref (tem, 1)))
3818 return scm_c_value_ref (tem, 0);
d23d13c0
BT
3819
3820 sym = scm_intern (scm_from_utf8_stringn (SSDATA (string),
3821 SBYTES (string)),
3822 obhash (obarray));
44c6c019 3823
d5db4077 3824 if ((SREF (string, 0) == ':')
a458d45d 3825 && EQ (obarray, initial_obarray))
44c6c019 3826 {
844e0de1
BT
3827 SET_SYMBOL_CONSTANT (XSYMBOL (sym), 1);
3828 SET_SYMBOL_REDIRECT (XSYMBOL (sym), SYMBOL_PLAINVAL);
ce5b453a 3829 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
44c6c019 3830 }
a0549832 3831
3250348f 3832 return sym;
078e7b4a
JB
3833}
3834
a7ca3326 3835DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
5de38842
PJ
3836 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3837NAME may be a string or a symbol. If it is a symbol, that exact
3838symbol is searched for.
3839A second optional argument specifies the obarray to use;
3840it defaults to the value of `obarray'. */)
5842a27b 3841 (Lisp_Object name, Lisp_Object obarray)
078e7b4a 3842{
3250348f 3843 register Lisp_Object tem, string, mv, found;
078e7b4a 3844
3250348f
BT
3845 string = SYMBOLP (name) ? SYMBOL_NAME (name) : name;
3846 mv = Ffind_symbol (string, obarray);
3847 tem = scm_c_value_ref (mv, 0);
3848 found = scm_c_value_ref (mv, 1);
078e7b4a 3849
3250348f 3850 if (NILP (found) || (SYMBOLP (name) && !EQ (name, tem)))
b55048d4
GM
3851 return Qnil;
3852 else
078e7b4a 3853 return tem;
078e7b4a 3854}
d007f5c8 3855\f
a7ca3326 3856DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
5de38842
PJ
3857 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3858The value is t if a symbol was found and deleted, nil otherwise.
3859NAME may be a string or a symbol. If it is a symbol, that symbol
3860is deleted, if it belongs to OBARRAY--no other symbol is deleted.
0e46275b
JB
3861OBARRAY, if nil, defaults to the value of the variable `obarray'.
3862usage: (unintern NAME OBARRAY) */)
5842a27b 3863 (Lisp_Object name, Lisp_Object obarray)
d007f5c8 3864{
d23d13c0
BT
3865 Lisp_Object string;
3866 Lisp_Object tem;
d007f5c8 3867
d23d13c0
BT
3868 if (NILP (obarray))
3869 obarray = Vobarray;
d007f5c8
RS
3870 obarray = check_obarray (obarray);
3871
3872 if (SYMBOLP (name))
d007f5c8 3873 {
d23d13c0
BT
3874 if (! EQ (name,
3875 scm_find_symbol (scm_symbol_to_string (name),
3876 obhash (obarray))))
3877 return Qnil;
3878 string = SYMBOL_NAME (name);
b2a30870 3879 }
d007f5c8
RS
3880 else
3881 {
d23d13c0
BT
3882 CHECK_STRING (name);
3883 string = name;
3884
d007f5c8
RS
3885 }
3886
d23d13c0 3887 return (scm_is_true (scm_unintern (name, obhash (obarray))) ? Qt : Qnil);
d007f5c8
RS
3888}
3889\f
fa4d3890
RT
3890struct map_obarray_data
3891{
3892 Lisp_Object obarray;
3893 void (*fn) (Lisp_Object, Lisp_Object);
3894 Lisp_Object arg;
3895};
3896
3897static Lisp_Object
3898map_obarray_inner (void *data, Lisp_Object sym)
3899{
3900 struct map_obarray_data *modata = data;
3901
3902 Lisp_Object tem = Ffind_symbol (SYMBOL_NAME (sym), modata->obarray);
3903 if (scm_is_true (scm_c_value_ref (tem, 1))
3904 && EQ (sym, scm_c_value_ref (tem, 0)))
3905 modata->fn (sym, modata->arg);
3906 return SCM_UNSPECIFIED;
3907}
3908
078e7b4a 3909void
971de7fb 3910map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
078e7b4a 3911{
fa4d3890
RT
3912 struct map_obarray_data data = { .obarray = obarray,
3913 .fn = fn,
3914 .arg = arg };
3915
b7826503 3916 CHECK_VECTOR (obarray);
fa4d3890 3917 scm_obarray_for_each (make_c_closure (map_obarray_inner, &data, 1, 0),
d23d13c0 3918 obhash (obarray));
078e7b4a
JB
3919}
3920
7d383292 3921static void
971de7fb 3922mapatoms_1 (Lisp_Object sym, Lisp_Object function)
078e7b4a
JB
3923{
3924 call1 (function, sym);
3925}
3926
3927DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
5de38842
PJ
3928 doc: /* Call FUNCTION on every symbol in OBARRAY.
3929OBARRAY defaults to the value of `obarray'. */)
5842a27b 3930 (Lisp_Object function, Lisp_Object obarray)
078e7b4a 3931{
265a9e55 3932 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
3933 obarray = check_obarray (obarray);
3934
3935 map_obarray (obarray, mapatoms_1, function);
3936 return Qnil;
3937}
3938
5e88a39e 3939#define OBARRAY_SIZE 1511
078e7b4a
JB
3940
3941void
971de7fb 3942init_obarray (void)
078e7b4a
JB
3943{
3944 Lisp_Object oblength;
37d0112b 3945 ptrdiff_t size = 100 + MAX_MULTIBYTE_LENGTH;
078e7b4a 3946
baf69866 3947 XSETFASTINT (oblength, OBARRAY_SIZE);
078e7b4a 3948
078e7b4a
JB
3949 Vobarray = Fmake_vector (oblength, make_number (0));
3950 initial_obarray = Vobarray;
3951 staticpro (&initial_obarray);
078e7b4a 3952
d23d13c0
BT
3953 obarrays = scm_make_hash_table (SCM_UNDEFINED);
3954 scm_hashq_set_x (obarrays, Vobarray, SCM_UNDEFINED);
3955
73880000
BT
3956 Qnil = SCM_ELISP_NIL;
3957 Qt = SCM_BOOL_T;
3958
3959 Qnil_ = intern_c_string ("nil");
3960 SET_SYMBOL_VAL (XSYMBOL (Qnil_), Qnil);
844e0de1
BT
3961 SET_SYMBOL_CONSTANT (XSYMBOL (Qnil_), 1);
3962 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (Qnil_), 1);
ce5b453a 3963
73880000
BT
3964 Qt_ = intern_c_string ("t");
3965 SET_SYMBOL_VAL (XSYMBOL (Qt_), Qt);
844e0de1
BT
3966 SET_SYMBOL_CONSTANT (XSYMBOL (Qt_), 1);
3967 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (Qt_), 1);
73880000 3968
d1c3da7b 3969 Qunbound = scm_c_public_ref ("language elisp runtime", "unbound");
ce5b453a 3970 SET_SYMBOL_VAL (XSYMBOL (Qunbound), Qunbound);
078e7b4a
JB
3971
3972 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3973 Vpurify_flag = Qt;
3974
cd3520a4 3975 DEFSYM (Qvariable_documentation, "variable-documentation");
078e7b4a 3976
c1ade6f7 3977 read_buffer = xmalloc_atomic (size);
37d0112b 3978 read_buffer_size = size;
078e7b4a
JB
3979}
3980\f
3981void
62947569 3982defsubr (const char *lname, scm_t_subr gsubr_fn, short min_args, short max_args, const char *intspec)
078e7b4a 3983{
62947569
BT
3984 Lisp_Object sym = intern_c_string (lname);
3985 Lisp_Object fn;
3986 switch (max_args)
3987 {
3988 case MANY:
3989 fn = scm_c_make_gsubr (lname, 0, 0, 1, gsubr_fn);
3990 break;
3991 case UNEVALLED:
3992 fn = Fcons (Qspecial_operator,
3993 scm_c_make_gsubr (lname, 0, 0, 1, gsubr_fn));
3994 break;
3995 default:
3996 fn = scm_c_make_gsubr (lname, min_args, max_args - min_args, 0, gsubr_fn);
3997 break;
3998 }
3999 set_symbol_function (sym, fn);
4000 if (intspec)
4001 {
4002 Lisp_Object tem = ((*intspec != '(')
4003 ? build_string (intspec)
4004 : Fcar (Fread_from_string (build_string (intspec),
4005 Qnil, Qnil)));
4006 scm_set_procedure_property_x (fn, Qinteractive_form, tem);
4007 }
078e7b4a
JB
4008}
4009
039c6cc3 4010/* Define an "integer variable"; a symbol whose value is forwarded to a
82cb60d3 4011 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
039c6cc3 4012 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
078e7b4a 4013void
ce5b453a
SM
4014defvar_int (struct Lisp_Intfwd *i_fwd,
4015 const char *namestring, EMACS_INT *address)
078e7b4a 4016{
ce5b453a 4017 Lisp_Object sym;
5e2327cf 4018 sym = intern_c_string (namestring);
ce5b453a
SM
4019 i_fwd->type = Lisp_Fwd_Int;
4020 i_fwd->intvar = address;
844e0de1
BT
4021 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
4022 SET_SYMBOL_REDIRECT (XSYMBOL (sym), SYMBOL_FORWARDED);
ce5b453a 4023 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
078e7b4a
JB
4024}
4025
f0529b5b 4026/* Similar but define a variable whose value is t if address contains 1,
0f011df0 4027 nil if address contains 0. */
078e7b4a 4028void
ce5b453a 4029defvar_bool (struct Lisp_Boolfwd *b_fwd,
f5d9e83a 4030 const char *namestring, bool *address)
078e7b4a 4031{
ce5b453a 4032 Lisp_Object sym;
5e2327cf 4033 sym = intern_c_string (namestring);
ce5b453a
SM
4034 b_fwd->type = Lisp_Fwd_Bool;
4035 b_fwd->boolvar = address;
844e0de1
BT
4036 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
4037 SET_SYMBOL_REDIRECT (XSYMBOL (sym), SYMBOL_FORWARDED);
ce5b453a 4038 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
1ffcc3b1 4039 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
078e7b4a
JB
4040}
4041
1a0f90f7
KH
4042/* Similar but define a variable whose value is the Lisp Object stored
4043 at address. Two versions: with and without gc-marking of the C
4044 variable. The nopro version is used when that variable will be
4045 gc-marked for some other reason, since marking the same slot twice
4046 can cause trouble with strings. */
078e7b4a 4047void
ce5b453a
SM
4048defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4049 const char *namestring, Lisp_Object *address)
078e7b4a 4050{
ce5b453a 4051 Lisp_Object sym;
5e2327cf 4052 sym = intern_c_string (namestring);
ce5b453a
SM
4053 o_fwd->type = Lisp_Fwd_Obj;
4054 o_fwd->objvar = address;
844e0de1
BT
4055 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
4056 SET_SYMBOL_REDIRECT (XSYMBOL (sym), SYMBOL_FORWARDED);
ce5b453a 4057 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
078e7b4a
JB
4058}
4059
078e7b4a 4060void
ce5b453a
SM
4061defvar_lisp (struct Lisp_Objfwd *o_fwd,
4062 const char *namestring, Lisp_Object *address)
078e7b4a 4063{
ce5b453a 4064 defvar_lisp_nopro (o_fwd, namestring, address);
1a0f90f7 4065 staticpro (address);
078e7b4a
JB
4066}
4067
950c215d 4068/* Similar but define a variable whose value is the Lisp Object stored
4ac38690 4069 at a particular offset in the current kboard object. */
950c215d
KH
4070
4071void
ce5b453a
SM
4072defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4073 const char *namestring, int offset)
950c215d 4074{
ce5b453a 4075 Lisp_Object sym;
5e2327cf 4076 sym = intern_c_string (namestring);
ce5b453a
SM
4077 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4078 ko_fwd->offset = offset;
844e0de1
BT
4079 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
4080 SET_SYMBOL_REDIRECT (XSYMBOL (sym), SYMBOL_FORWARDED);
ce5b453a 4081 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
950c215d 4082}
078e7b4a 4083\f
17e0445b 4084/* Check that the elements of lpath exist. */
929e7845 4085
b3350bf9 4086static void
17e0445b 4087load_path_check (Lisp_Object lpath)
929e7845
GM
4088{
4089 Lisp_Object path_tail;
4090
4091 /* The only elements that might not exist are those from
4092 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4093 it exists. */
17e0445b 4094 for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
929e7845
GM
4095 {
4096 Lisp_Object dirfile;
4097 dirfile = Fcar (path_tail);
4098 if (STRINGP (dirfile))
4099 {
4100 dirfile = Fdirectory_file_name (dirfile);
73dcdb9f
PE
4101 if (! file_accessible_directory_p (SSDATA (dirfile)))
4102 dir_warning ("Lisp directory", XCAR (path_tail));
929e7845
GM
4103 }
4104 }
4105}
4106
17e0445b
GM
4107/* Return the default load-path, to be used if EMACSLOADPATH is unset.
4108 This does not include the standard site-lisp directories
4109 under the installation prefix (i.e., PATH_SITELOADSEARCH),
4110 but it does (unless no_site_lisp is set) include site-lisp
4111 directories in the source/build directories if those exist and we
4112 are running uninstalled.
4113
4114 Uses the following logic:
4115 If CANNOT_DUMP: Use PATH_LOADSEARCH.
929e7845
GM
4116 The remainder is what happens when dumping works:
4117 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
17e0445b 4118 Otherwise use PATH_LOADSEARCH.
929e7845 4119
06a4f110
GM
4120 If !initialized, then just return PATH_DUMPLOADSEARCH.
4121 If initialized:
929e7845
GM
4122 If Vinstallation_directory is not nil (ie, running uninstalled):
4123 If installation-dir/lisp exists and not already a member,
4124 we must be running uninstalled. Reset the load-path
4125 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4126 refers to the eventual installation directories. Since we
4127 are not yet installed, we should not use them, even if they exist.)
06a4f110
GM
4128 If installation-dir/lisp does not exist, just add
4129 PATH_DUMPLOADSEARCH at the end instead.
929e7845
GM
4130 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4131 and not already a member) at the front.
4132 If installation-dir != source-dir (ie running an uninstalled,
4133 out-of-tree build) AND install-dir/src/Makefile exists BUT
4134 install-dir/src/Makefile.in does NOT exist (this is a sanity
cb6c95a3 4135 check), then repeat the above steps for source-dir/lisp, site-lisp. */
4aaf6b61
PE
4136
4137static Lisp_Object
dc7909c4 4138load_path_default (void)
078e7b4a 4139{
17e0445b 4140 Lisp_Object lpath = Qnil;
8ea90aa3 4141 const char *normal;
41120e0f 4142 bool initialized_or_cannot_dump;
ca26824c 4143
46947372 4144#ifdef CANNOT_DUMP
41120e0f
RT
4145 initialized_or_cannot_dump = true;
4146 normal = PATH_LOADSEARCH;
9e059e3f 4147#else
41120e0f 4148 initialized_or_cannot_dump = initialized;
17e0445b 4149 normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
41120e0f 4150#endif
279499f0 4151
41120e0f 4152 if (initialized_or_cannot_dump)
4746118a 4153 {
9e059e3f 4154#ifdef HAVE_NS
06a4f110
GM
4155 const char *loadpath = ns_load_path ();
4156 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
9e059e3f 4157#else
06a4f110 4158 lpath = decode_env_path (0, normal, 0);
9e059e3f 4159#endif
06a4f110
GM
4160 if (!NILP (Vinstallation_directory))
4161 {
4162 Lisp_Object tem, tem1;
4163
4164 /* Add to the path the lisp subdir of the installation
4165 dir, if it is accessible. Note: in out-of-tree builds,
4166 this directory is empty save for Makefile. */
4167 tem = Fexpand_file_name (build_string ("lisp"),
4168 Vinstallation_directory);
4169 tem1 = Ffile_accessible_directory_p (tem);
4170 if (!NILP (tem1))
4171 {
4172 if (NILP (Fmember (tem, lpath)))
4173 {
4174 /* We are running uninstalled. The default load-path
4175 points to the eventual installed lisp directories.
4176 We should not use those now, even if they exist,
4177 so start over from a clean slate. */
4178 lpath = list1 (tem);
4179 }
4180 }
4181 else
4182 /* That dir doesn't exist, so add the build-time
4183 Lisp dirs instead. */
17e0445b 4184 {
06a4f110
GM
4185 Lisp_Object dump_path =
4186 decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
4187 lpath = nconc2 (lpath, dump_path);
4188 }
ca26824c 4189
06a4f110
GM
4190 /* Add site-lisp under the installation dir, if it exists. */
4191 if (!no_site_lisp)
4192 {
4193 tem = Fexpand_file_name (build_string ("site-lisp"),
ca26824c 4194 Vinstallation_directory);
73dcdb9f 4195 tem1 = Ffile_accessible_directory_p (tem);
ca26824c
GM
4196 if (!NILP (tem1))
4197 {
17e0445b 4198 if (NILP (Fmember (tem, lpath)))
06a4f110 4199 lpath = Fcons (tem, lpath);
ca26824c 4200 }
06a4f110 4201 }
ca26824c 4202
06a4f110
GM
4203 /* If Emacs was not built in the source directory,
4204 and it is run from where it was built, add to load-path
4205 the lisp and site-lisp dirs under that directory. */
ca26824c 4206
06a4f110
GM
4207 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4208 {
4209 Lisp_Object tem2;
4210
4211 tem = Fexpand_file_name (build_string ("src/Makefile"),
4212 Vinstallation_directory);
4213 tem1 = Ffile_exists_p (tem);
ca26824c 4214
06a4f110
GM
4215 /* Don't be fooled if they moved the entire source tree
4216 AFTER dumping Emacs. If the build directory is indeed
4217 different from the source dir, src/Makefile.in and
4218 src/Makefile will not be found together. */
4219 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4220 Vinstallation_directory);
4221 tem2 = Ffile_exists_p (tem);
4222 if (!NILP (tem1) && NILP (tem2))
ca26824c 4223 {
06a4f110
GM
4224 tem = Fexpand_file_name (build_string ("lisp"),
4225 Vsource_directory);
ca26824c 4226
06a4f110
GM
4227 if (NILP (Fmember (tem, lpath)))
4228 lpath = Fcons (tem, lpath);
ca26824c 4229
06a4f110
GM
4230 if (!no_site_lisp)
4231 {
4232 tem = Fexpand_file_name (build_string ("site-lisp"),
4233 Vsource_directory);
4234 tem1 = Ffile_accessible_directory_p (tem);
4235 if (!NILP (tem1))
ca26824c 4236 {
06a4f110
GM
4237 if (NILP (Fmember (tem, lpath)))
4238 lpath = Fcons (tem, lpath);
ca26824c
GM
4239 }
4240 }
06a4f110
GM
4241 }
4242 } /* Vinstallation_directory != Vsource_directory */
929e7845 4243
06a4f110 4244 } /* if Vinstallation_directory */
4746118a 4245 }
41120e0f 4246 else /* !initialized_or_cannot_dump */
11938f10 4247 {
929e7845
GM
4248 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4249 source directory. We used to add ../lisp (ie the lisp dir in
06a4f110
GM
4250 the build directory) at the front here, but that should not
4251 be necessary, since in out of tree builds lisp/ is empty, save
929e7845 4252 for Makefile. */
17e0445b 4253 lpath = decode_env_path (0, normal, 0);
11938f10 4254 }
17e0445b
GM
4255
4256 return lpath;
4257}
4258
4259void
4260init_lread (void)
4261{
41120e0f
RT
4262 /* Set Vsource_directory before calling load_path_default. */
4263 Vsource_directory
4264 = Fexpand_file_name (build_string ("../"),
4265 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
4266
4267 /* Set Vload_path. */
17e0445b 4268
8fb8c4f3
PE
4269 /* Ignore EMACSLOADPATH when dumping. */
4270#ifdef CANNOT_DUMP
4271 bool use_loadpath = true;
4272#else
f952002e 4273 bool use_loadpath = NILP (Vpurify_flag);
8fb8c4f3
PE
4274#endif
4275
4276 if (use_loadpath && egetenv ("EMACSLOADPATH"))
17e0445b 4277 {
06a4f110 4278 Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
17e0445b
GM
4279
4280 /* Check (non-nil) user-supplied elements. */
06a4f110 4281 load_path_check (Vload_path);
17e0445b 4282
310294a3
GM
4283 /* If no nils in the environment variable, use as-is.
4284 Otherwise, replace any nils with the default. */
06a4f110 4285 if (! NILP (Fmemq (Qnil, Vload_path)))
17e0445b 4286 {
06a4f110
GM
4287 Lisp_Object elem, elpath = Vload_path;
4288 Lisp_Object default_lpath = load_path_default ();
17e0445b
GM
4289
4290 /* Check defaults, before adding site-lisp. */
4291 load_path_check (default_lpath);
4292
4293 /* Add the site-lisp directories to the front of the default. */
4294 if (!no_site_lisp)
4295 {
4296 Lisp_Object sitelisp;
4297 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4298 if (! NILP (sitelisp))
4299 default_lpath = nconc2 (sitelisp, default_lpath);
4300 }
4301
4302 Vload_path = Qnil;
4303
4304 /* Replace nils from EMACSLOADPATH by default. */
310294a3 4305 while (CONSP (elpath))
17e0445b
GM
4306 {
4307 Lisp_Object arg[2];
310294a3
GM
4308 elem = XCAR (elpath);
4309 elpath = XCDR (elpath);
17e0445b
GM
4310 arg[0] = Vload_path;
4311 arg[1] = NILP (elem) ? default_lpath : Fcons (elem, Qnil);
4312 Vload_path = Fappend (2, arg);
4313 }
4314 } /* Fmemq (Qnil, Vload_path) */
4315 }
f952002e 4316 else
17e0445b 4317 {
06a4f110 4318 Vload_path = load_path_default ();
17e0445b
GM
4319
4320 /* Check before adding site-lisp directories.
4321 The install should have created them, but they are not
4322 required, so no need to warn if they are absent.
4323 Or we might be running before installation. */
4324 load_path_check (Vload_path);
4325
06a4f110
GM
4326 /* Add the site-lisp directories at the front. */
4327 if (initialized && !no_site_lisp)
17e0445b
GM
4328 {
4329 Lisp_Object sitelisp;
4330 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4331 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4332 }
f952002e 4333 }
279499f0 4334
279499f0
JB
4335 Vvalues = Qnil;
4336
078e7b4a 4337 load_in_progress = 0;
4e53f562 4338 Vload_file_name = Qnil;
8f6b0411 4339 Vstandard_input = Qt;
f74b0705 4340 Vloads_in_progress = Qnil;
078e7b4a
JB
4341}
4342
73dcdb9f
PE
4343/* Print a warning that directory intended for use USE and with name
4344 DIRNAME cannot be accessed. On entry, errno should correspond to
4345 the access failure. Print the warning on stderr and put it in
4346 *Messages*. */
85496b8c 4347
d5b28a9d 4348void
73dcdb9f 4349dir_warning (char const *use, Lisp_Object dirname)
85496b8c 4350{
73dcdb9f
PE
4351 static char const format[] = "Warning: %s `%s': %s\n";
4352 int access_errno = errno;
4353 fprintf (stderr, format, use, SSDATA (dirname), strerror (access_errno));
0df02bf3 4354
48def666 4355 /* Don't log the warning before we've initialized!! */
9b69357e 4356 if (initialized)
0df02bf3 4357 {
73dcdb9f 4358 char const *diagnostic = emacs_strerror (access_errno);
0df02bf3 4359 USE_SAFE_ALLOCA;
73dcdb9f
PE
4360 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4361 + strlen (use) + SBYTES (dirname)
4362 + strlen (diagnostic));
4363 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4364 diagnostic);
0df02bf3
PE
4365 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4366 SAFE_FREE ();
4367 }
85496b8c
RS
4368}
4369
078e7b4a 4370void
971de7fb 4371syms_of_lread (void)
078e7b4a 4372{
fe6aa7a1 4373#include "lread.x"
078e7b4a 4374
29208e82 4375 DEFVAR_LISP ("obarray", Vobarray,
5de38842
PJ
4376 doc: /* Symbol table for use by `intern' and `read'.
4377It is a vector whose length ought to be prime for best results.
4378The vector's contents don't make sense if examined from Lisp programs;
4379to find all the symbols in an obarray, use `mapatoms'. */);
078e7b4a 4380
29208e82 4381 DEFVAR_LISP ("values", Vvalues,
5de38842 4382 doc: /* List of values of all expressions which were read, evaluated and printed.
513749ee 4383 Order is reverse chronological. */);
844e0de1 4384 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (intern ("values")), 0);
078e7b4a 4385
29208e82 4386 DEFVAR_LISP ("standard-input", Vstandard_input,
5de38842
PJ
4387 doc: /* Stream for read to get input from.
4388See documentation of `read' for possible values. */);
078e7b4a
JB
4389 Vstandard_input = Qt;
4390
29208e82 4391 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
abb13b09
CW
4392 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4393
4394If this variable is a buffer, then only forms read from that buffer
4395will be added to `read-symbol-positions-list'.
4396If this variable is t, then all read forms will be added.
4397The effect of all other values other than nil are not currently
4398defined, although they may be in the future.
4399
4400The positions are relative to the last call to `read' or
4401`read-from-string'. It is probably a bad idea to set this variable at
513749ee 4402the toplevel; bind it instead. */);
abb13b09
CW
4403 Vread_with_symbol_positions = Qnil;
4404
29208e82 4405 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
d6567030 4406 doc: /* A list mapping read symbols to their positions.
abb13b09
CW
4407This variable is modified during calls to `read' or
4408`read-from-string', but only when `read-with-symbol-positions' is
4409non-nil.
4410
4411Each element of the list looks like (SYMBOL . CHAR-POSITION), where
d6567030 4412CHAR-POSITION is an integer giving the offset of that occurrence of the
abb13b09
CW
4413symbol from the position where `read' or `read-from-string' started.
4414
4415Note that a symbol will appear multiple times in this list, if it was
4416read multiple times. The list is in the same order as the symbols
513749ee 4417were read in. */);
177c0ea7 4418 Vread_symbol_positions_list = Qnil;
abb13b09 4419
29208e82 4420 DEFVAR_LISP ("read-circle", Vread_circle,
91f68422
CY
4421 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4422 Vread_circle = Qt;
4423
29208e82 4424 DEFVAR_LISP ("load-path", Vload_path,
fb7ada5f 4425 doc: /* List of directories to search for files to load.
17e0445b
GM
4426Each element is a string (directory name) or nil (meaning `default-directory').
4427Initialized during startup as described in Info node `(elisp)Library Search'. */);
078e7b4a 4428
29208e82 4429 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
971a4293
LT
4430 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4431This list should not include the empty string.
4432`load' and related functions try to append these suffixes, in order,
4433to the specified file name if a Lisp suffix is allowed or required. */);
0b7633df 4434 Vload_suffixes = list1 (build_pure_c_string (".el"));
29208e82 4435 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
971a4293
LT
4436 doc: /* List of suffixes that indicate representations of \
4437the same file.
4438This list should normally start with the empty string.
4439
4440Enabling Auto Compression mode appends the suffixes in
4441`jka-compr-load-suffixes' to this list and disabling Auto Compression
4442mode removes them again. `load' and related functions use this list to
4443determine whether they should look for compressed versions of a file
4444and, if so, which suffixes they should try to append to the file name
4445in order to do so. However, if you want to customize which suffixes
4446the loading functions recognize as compression suffixes, you should
4447customize `jka-compr-load-suffixes' rather than the present variable. */);
6c6f1994 4448 Vload_file_rep_suffixes = list1 (empty_unibyte_string);
e61b9b87 4449
29208e82 4450 DEFVAR_BOOL ("load-in-progress", load_in_progress,
e0f24100 4451 doc: /* Non-nil if inside of `load'. */);
cd3520a4 4452 DEFSYM (Qload_in_progress, "load-in-progress");
078e7b4a 4453
29208e82 4454 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
de0503df
SM
4455 doc: /* An alist of functions to be evalled when particular files are loaded.
4456Each element looks like (REGEXP-OR-FEATURE FUNCS...).
6bb6da3e
AM
4457
4458REGEXP-OR-FEATURE is either a regular expression to match file names, or
4459a symbol \(a feature name).
4460
4461When `load' is run and the file-name argument matches an element's
4462REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
de0503df 4463REGEXP-OR-FEATURE, the FUNCS in the element are called.
6bb6da3e
AM
4464
4465An error in FORMS does not undo the load, but does prevent execution of
4466the rest of the FORMS. */);
078e7b4a
JB
4467 Vafter_load_alist = Qnil;
4468
29208e82 4469 DEFVAR_LISP ("load-history", Vload_history,
4801c5fa
CY
4470 doc: /* Alist mapping loaded file names to symbols and features.
4471Each alist element should be a list (FILE-NAME ENTRIES...), where
4472FILE-NAME is the name of a file that has been loaded into Emacs.
4473The file name is absolute and true (i.e. it doesn't contain symlinks).
4474As an exception, one of the alist elements may have FILE-NAME nil,
4475for symbols and features not associated with any file.
4476
4477The remaining ENTRIES in the alist element describe the functions and
4478variables defined in that file, the features provided, and the
4479features required. Each entry has the form `(provide . FEATURE)',
4480`(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
69bb1ef7
LMI
4481`(defface . SYMBOL)', or `(t . SYMBOL)'. Entries like `(t . SYMBOL)'
4482may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
4483autoload before this file redefined it as a function. In addition,
4484entries may also be single symbols, which means that SYMBOL was
4485defined by `defvar' or `defconst'.
0fc213e9 4486
b502a9a1
RS
4487During preloading, the file name recorded is relative to the main Lisp
4488directory. These file names are converted to absolute at startup. */);
ae321d28
RS
4489 Vload_history = Qnil;
4490
29208e82 4491 DEFVAR_LISP ("load-file-name", Vload_file_name,
5de38842 4492 doc: /* Full name of file being loaded by `load'. */);
20ea2964
RS
4493 Vload_file_name = Qnil;
4494
29208e82 4495 DEFVAR_LISP ("user-init-file", Vuser_init_file,
5de38842 4496 doc: /* File name, including directory, of user's initialization file.
0a25a201
RS
4497If the file loaded had extension `.elc', and the corresponding source file
4498exists, this variable contains the name of source file, suitable for use
099de390
JB
4499by functions like `custom-save-all' which edit the init file.
4500While Emacs loads and evaluates the init file, value is the real name
4501of the file, regardless of whether or not it has the `.elc' extension. */);
4116ab9f
KH
4502 Vuser_init_file = Qnil;
4503
29208e82 4504 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
5de38842 4505 doc: /* Used for internal purposes by `load'. */);
ae321d28
RS
4506 Vcurrent_load_list = Qnil;
4507
29208e82 4508 DEFVAR_LISP ("load-read-function", Vload_read_function,
5de38842
PJ
4509 doc: /* Function used by `load' and `eval-region' for reading expressions.
4510The default is nil, which means use the function `read'. */);
84a15045
RS
4511 Vload_read_function = Qnil;
4512
29208e82 4513 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
76b92fee
CS
4514 doc: /* Function called in `load' to load an Emacs Lisp source file.
4515The value should be a function for doing code conversion before
4516reading a source file. It can also be nil, in which case loading is
4517done without any code conversion.
4518
4519If the value is a function, it is called with four arguments,
4520FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4521the file to load, FILE is the non-absolute name (for messages etc.),
4522and NOERROR and NOMESSAGE are the corresponding arguments passed to
4523`load'. The function should return t if the file was loaded. */);
fe0e03f3
KH
4524 Vload_source_file_function = Qnil;
4525
29208e82 4526 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
5de38842
PJ
4527 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4528This is useful when the file being loaded is a temporary copy. */);
b2a30870
RS
4529 load_force_doc_strings = 0;
4530
29208e82 4531 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
5de38842
PJ
4532 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4533This is normally bound by `load' and `eval-buffer' to control `read',
4534and is not meant for users to change. */);
94e554db
RS
4535 load_convert_to_unibyte = 0;
4536
29208e82 4537 DEFVAR_LISP ("source-directory", Vsource_directory,
5de38842
PJ
4538 doc: /* Directory in which Emacs sources were found when Emacs was built.
4539You cannot count on them to still be there! */);
a90ba1e2 4540
29208e82 4541 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
5de38842 4542 doc: /* List of files that were preloaded (when dumping Emacs). */);
4b104c41
RS
4543 Vpreloaded_file_list = Qnil;
4544
29208e82 4545 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
5de38842 4546 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
1ffcc3b1
DL
4547 Vbyte_boolean_vars = Qnil;
4548
29208e82 4549 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
5de38842
PJ
4550 doc: /* Non-nil means load dangerous compiled Lisp files.
4551Some versions of XEmacs use different byte codes than Emacs. These
4552incompatible byte codes can make Emacs crash when it tries to execute
4553them. */);
da84f340
GM
4554 load_dangerous_libraries = 0;
4555
29208e82 4556 DEFVAR_BOOL ("force-load-messages", force_load_messages,
beb0b7f9
EZ
4557 doc: /* Non-nil means force printing messages when loading Lisp files.
4558This overrides the value of the NOMESSAGE argument to `load'. */);
4559 force_load_messages = 0;
4560
29208e82 4561 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
5de38842
PJ
4562 doc: /* Regular expression matching safe to load compiled Lisp files.
4563When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4564from the file, and matches them against this regular expression.
4565When the regular expression matches, the file is considered to be safe
4566to load. See also `load-dangerous-libraries'. */);
bb970e67 4567 Vbytecomp_version_regexp
2a0213a6 4568 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
da84f340 4569
14ae4239 4570 DEFSYM (Qlexical_binding, "lexical-binding");
8f1d2ef6 4571 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
f41628b2
LMI
4572 doc: /* Whether to use lexical binding when evaluating code.
4573Non-nil means that the code in the current buffer should be evaluated
4574with lexical binding.
1d698799 4575This variable is automatically set from the file variables of an
48da7392
GM
4576interpreted Lisp file read using `load'. Unlike other file local
4577variables, this must be set in the first line of a file. */);
14ae4239 4578 Vlexical_binding = Qnil;
b9598260
SM
4579 Fmake_variable_buffer_local (Qlexical_binding);
4580
29208e82 4581 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
3f39f996
RS
4582 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4583 Veval_buffer_list = Qnil;
4584
29208e82 4585 DEFVAR_LISP ("old-style-backquotes", Vold_style_backquotes,
5aa273b0
SM
4586 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4587 Vold_style_backquotes = Qnil;
cd3520a4 4588 DEFSYM (Qold_style_backquotes, "old-style-backquotes");
5aa273b0 4589
1f41ee56
GM
4590 DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
4591 doc: /* Non-nil means `load' prefers the newest version of a file.
4592This applies when a filename suffix is not explicitly specified and
4593`load' is trying various possible suffixes (see `load-suffixes' and
4594`load-file-rep-suffixes'). Normally, it stops at the first file
2dc383bc
XF
4595that exists unless you explicitly specify one or the other. If this
4596option is non-nil, it checks all suffixes and uses whichever file is
4597newest.
1f41ee56
GM
4598Note that if you customize this, obviously it will not affect files
4599that are loaded before your customizations are read! */);
c16fbf67 4600 load_prefer_newer = 1;
1f41ee56 4601
a90ba1e2
KH
4602 /* Vsource_directory was initialized in init_lread. */
4603
cd3520a4
JB
4604 DEFSYM (Qcurrent_load_list, "current-load-list");
4605 DEFSYM (Qstandard_input, "standard-input");
4606 DEFSYM (Qread_char, "read-char");
4607 DEFSYM (Qget_file_char, "get-file-char");
4608 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
4609 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
4610
4611 DEFSYM (Qbackquote, "`");
4612 DEFSYM (Qcomma, ",");
4613 DEFSYM (Qcomma_at, ",@");
4614 DEFSYM (Qcomma_dot, ",.");
4615
4616 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
4617 DEFSYM (Qascii_character, "ascii-character");
4618 DEFSYM (Qfunction, "function");
4619 DEFSYM (Qload, "load");
4620 DEFSYM (Qload_file_name, "load-file-name");
4621 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
4622 DEFSYM (Qfile_truename, "file-truename");
4623 DEFSYM (Qdir_ok, "dir-ok");
4624 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
6bb6da3e 4625
4ad679f9
EN
4626 staticpro (&read_objects);
4627 read_objects = Qnil;
9e062b6c 4628 staticpro (&seen_list);
f153db13 4629 seen_list = Qnil;
177c0ea7 4630
7ee3bd7b
GM
4631 Vloads_in_progress = Qnil;
4632 staticpro (&Vloads_in_progress);
f19a0f5b 4633
cd3520a4
JB
4634 DEFSYM (Qhash_table, "hash-table");
4635 DEFSYM (Qdata, "data");
4636 DEFSYM (Qtest, "test");
4637 DEFSYM (Qsize, "size");
4638 DEFSYM (Qweakness, "weakness");
4639 DEFSYM (Qrehash_size, "rehash-size");
4640 DEFSYM (Qrehash_threshold, "rehash-threshold");
078e7b4a 4641}