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