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