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