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