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