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