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