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