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