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