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