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