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