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