(byte-optimize-lapcode): Fix format calls.
[bpt/emacs.git] / src / lread.c
CommitLineData
078e7b4a 1/* Lisp parsing and input streams.
73aa9704 2 Copyright (C) 1985, 1986, 1987, 1988, 1989,
f8c25f1b 3 1993, 1994, 1995 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
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
98280b76 22#include <config.h>
078e7b4a
JB
23#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/file.h>
2c1b5dbe 27#include <errno.h>
078e7b4a
JB
28#include "lisp.h"
29
30#ifndef standalone
31#include "buffer.h"
2a6b3537 32#include <paths.h>
078e7b4a 33#include "commands.h"
e37c0805 34#include "keyboard.h"
7bd279cd 35#include "termhooks.h"
078e7b4a
JB
36#endif
37
38#ifdef lint
39#include <sys/inode.h>
40#endif /* lint */
41
42#ifndef X_OK
43#define X_OK 01
44#endif
45
46#ifdef LISP_FLOAT_TYPE
93b91208
JB
47#ifdef STDC_HEADERS
48#include <stdlib.h>
49#endif
23a71bd6 50
23a71bd6 51#ifdef MSDOS
7df5cd28 52#include "msdos.h"
6ba93f9d 53/* These are redefined (correctly, but differently) in values.h. */
23a71bd6
RS
54#undef INTBITS
55#undef LONGBITS
56#undef SHORTBITS
57#endif
23a71bd6 58
078e7b4a
JB
59#include <math.h>
60#endif /* LISP_FLOAT_TYPE */
61
f7d279f0
RS
62#ifndef O_RDONLY
63#define O_RDONLY 0
64#endif
65
2c1b5dbe
KH
66extern int errno;
67
8a1f1537 68Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
078e7b4a 69Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
20ea2964 70Lisp_Object Qascii_character, Qload, Qload_file_name;
17634846 71Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot;
7bd279cd
RS
72
73extern Lisp_Object Qevent_symbol_element_mask;
078e7b4a
JB
74
75/* non-zero if inside `load' */
76int load_in_progress;
77
78/* Search path for files to be loaded. */
79Lisp_Object Vload_path;
80
ae321d28
RS
81/* This is the user-visible association list that maps features to
82 lists of defs in their load files. */
83Lisp_Object Vload_history;
84
20ea2964 85/* This is used to build the load history. */
ae321d28
RS
86Lisp_Object Vcurrent_load_list;
87
20ea2964
RS
88/* Name of file actually being read by `load'. */
89Lisp_Object Vload_file_name;
90
84a15045
RS
91/* Function to use for reading, in `load' and friends. */
92Lisp_Object Vload_read_function;
93
d2c6be7f
RS
94/* List of descriptors now open for Fload. */
95static Lisp_Object load_descriptor_list;
96
078e7b4a
JB
97/* File for get_file_char to read from. Use by load */
98static FILE *instream;
99
100/* When nonzero, read conses in pure space */
101static int read_pure;
102
103/* For use within read-from-string (this reader is non-reentrant!!) */
104static int read_from_string_index;
105static int read_from_string_limit;
17634846
RS
106
107/* Nonzero means inside a new-style backquote
108 with no surrounding parentheses.
109 Fread initializes this to zero, so we need not specbind it
110 or worry about what happens to it when there is an error. */
111static int new_backquote_flag;
078e7b4a
JB
112\f
113/* Handle unreading and rereading of characters.
114 Write READCHAR to read a character,
115 UNREAD(c) to unread c to be read again. */
116
117#define READCHAR readchar (readcharfun)
118#define UNREAD(c) unreadchar (readcharfun, c)
119
120static int
121readchar (readcharfun)
122 Lisp_Object readcharfun;
123{
124 Lisp_Object tem;
125 register struct buffer *inbuffer;
126 register int c, mpos;
127
cfff016d 128 if (BUFFERP (readcharfun))
078e7b4a
JB
129 {
130 inbuffer = XBUFFER (readcharfun);
131
132 if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer))
133 return -1;
134 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer));
135 SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1);
136
137 return c;
138 }
cfff016d 139 if (MARKERP (readcharfun))
078e7b4a
JB
140 {
141 inbuffer = XMARKER (readcharfun)->buffer;
142
143 mpos = marker_position (readcharfun);
144
145 if (mpos > BUF_ZV (inbuffer) - 1)
146 return -1;
147 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos);
148 if (mpos != BUF_GPT (inbuffer))
149 XMARKER (readcharfun)->bufpos++;
150 else
151 Fset_marker (readcharfun, make_number (mpos + 1),
152 Fmarker_buffer (readcharfun));
153 return c;
154 }
155 if (EQ (readcharfun, Qget_file_char))
2c1b5dbe
KH
156 {
157 c = getc (instream);
158#ifdef EINTR
159 /* Interrupted reads have been observed while reading over the network */
160 while (c == EOF && ferror (instream) && errno == EINTR)
161 {
162 clearerr (instream);
163 c = getc (instream);
164 }
165#endif
166 return c;
167 }
078e7b4a 168
cfff016d 169 if (STRINGP (readcharfun))
078e7b4a
JB
170 {
171 register int c;
172 /* This used to be return of a conditional expression,
173 but that truncated -1 to a char on VMS. */
174 if (read_from_string_index < read_from_string_limit)
175 c = XSTRING (readcharfun)->data[read_from_string_index++];
176 else
177 c = -1;
178 return c;
179 }
180
181 tem = call0 (readcharfun);
182
265a9e55 183 if (NILP (tem))
078e7b4a
JB
184 return -1;
185 return XINT (tem);
186}
187
188/* Unread the character C in the way appropriate for the stream READCHARFUN.
189 If the stream is a user function, call it with the char as argument. */
190
191static void
192unreadchar (readcharfun, c)
193 Lisp_Object readcharfun;
194 int c;
195{
92fddec9
KH
196 if (c == -1)
197 /* Don't back up the pointer if we're unreading the end-of-input mark,
198 since readchar didn't advance it when we read it. */
199 ;
cfff016d 200 else if (BUFFERP (readcharfun))
078e7b4a
JB
201 {
202 if (XBUFFER (readcharfun) == current_buffer)
203 SET_PT (point - 1);
204 else
205 SET_BUF_PT (XBUFFER (readcharfun), BUF_PT (XBUFFER (readcharfun)) - 1);
206 }
cfff016d 207 else if (MARKERP (readcharfun))
078e7b4a 208 XMARKER (readcharfun)->bufpos--;
cfff016d 209 else if (STRINGP (readcharfun))
078e7b4a
JB
210 read_from_string_index--;
211 else if (EQ (readcharfun, Qget_file_char))
212 ungetc (c, instream);
213 else
214 call1 (readcharfun, make_number (c));
215}
216
217static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
218\f
219/* get a character from the tty */
220
3d9b22be
JB
221extern Lisp_Object read_char ();
222
f42be754
JB
223/* Read input events until we get one that's acceptable for our purposes.
224
225 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
226 until we get a character we like, and then stuffed into
227 unread_switch_frame.
228
229 If ASCII_REQUIRED is non-zero, we check function key events to see
230 if the unmodified version of the symbol has a Qascii_character
231 property, and use that character, if present.
232
233 If ERROR_NONASCII is non-zero, we signal an error if the input we
234 get isn't an ASCII character with modifiers. If it's zero but
235 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
236 character. */
237Lisp_Object
238read_filtered_event (no_switch_frame, ascii_required, error_nonascii)
239 int no_switch_frame, ascii_required, error_nonascii;
240{
241#ifdef standalone
242 return make_number (getchar ());
243#else
153a17b7
KH
244 register Lisp_Object val, delayed_switch_frame;
245
246 delayed_switch_frame = Qnil;
f42be754
JB
247
248 /* Read until we get an acceptable event. */
249 retry:
250 val = read_char (0, 0, 0, Qnil, 0);
251
cfff016d 252 if (BUFFERP (val))
6c82d689
KH
253 goto retry;
254
f42be754
JB
255 /* switch-frame events are put off until after the next ASCII
256 character. This is better than signalling an error just because
257 the last characters were typed to a separate minibuffer frame,
258 for example. Eventually, some code which can deal with
259 switch-frame events will read it and process it. */
260 if (no_switch_frame
261 && EVENT_HAS_PARAMETERS (val)
262 && EQ (EVENT_HEAD (val), Qswitch_frame))
263 {
264 delayed_switch_frame = val;
265 goto retry;
266 }
267
268 if (ascii_required)
269 {
270 /* Convert certain symbols to their ASCII equivalents. */
cfff016d 271 if (SYMBOLP (val))
f42be754
JB
272 {
273 Lisp_Object tem, tem1, tem2;
274 tem = Fget (val, Qevent_symbol_element_mask);
275 if (!NILP (tem))
276 {
277 tem1 = Fget (Fcar (tem), Qascii_character);
278 /* Merge this symbol's modifier bits
279 with the ASCII equivalent of its basic code. */
280 if (!NILP (tem1))
baf69866 281 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
f42be754
JB
282 }
283 }
284
285 /* If we don't have a character now, deal with it appropriately. */
cfff016d 286 if (!INTEGERP (val))
f42be754
JB
287 {
288 if (error_nonascii)
289 {
1ec84625 290 Vunread_command_events = Fcons (val, Qnil);
f42be754
JB
291 error ("Non-character input-event");
292 }
293 else
294 goto retry;
295 }
296 }
297
298 if (! NILP (delayed_switch_frame))
299 unread_switch_frame = delayed_switch_frame;
300
301 return val;
302#endif
303}
304
078e7b4a
JB
305DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
306 "Read a character from the command input (keyboard or macro).\n\
e51e47f7
JB
307It is returned as a number.\n\
308If the user generates an event which is not a character (i.e. a mouse\n\
e37c0805
JB
309click or function key event), `read-char' signals an error. As an\n\
310exception, switch-frame events are put off until non-ASCII events can\n\
311be read.\n\
312If you want to read non-character events, or ignore them, call\n\
313`read-event' or `read-char-exclusive' instead.")
078e7b4a
JB
314 ()
315{
f42be754 316 return read_filtered_event (1, 1, 1);
078e7b4a
JB
317}
318
078e7b4a
JB
319DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0,
320 "Read an event object from the input stream.")
321 ()
322{
f42be754 323 return read_filtered_event (0, 0, 0);
078e7b4a
JB
324}
325
326DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0,
327 "Read a character from the command input (keyboard or macro).\n\
328It is returned as a number. Non character events are ignored.")
329 ()
330{
f42be754 331 return read_filtered_event (1, 1, 0);
078e7b4a 332}
078e7b4a
JB
333
334DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
335 "Don't use this yourself.")
336 ()
337{
338 register Lisp_Object val;
1805de4f 339 XSETINT (val, getc (instream));
078e7b4a
JB
340 return val;
341}
342\f
343static void readevalloop ();
344static Lisp_Object load_unwind ();
d2c6be7f 345static Lisp_Object load_descriptor_unwind ();
078e7b4a
JB
346
347DEFUN ("load", Fload, Sload, 1, 4, 0,
348 "Execute a file of Lisp code named FILE.\n\
349First try FILE with `.elc' appended, then try with `.el',\n\
350 then try FILE unmodified.\n\
351This function searches the directories in `load-path'.\n\
352If optional second arg NOERROR is non-nil,\n\
353 report no error if FILE doesn't exist.\n\
354Print messages at start and end of loading unless\n\
355 optional third arg NOMESSAGE is non-nil.\n\
356If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
357 suffixes `.elc' or `.el' to the specified name FILE.\n\
358Return t if file exists.")
359 (str, noerror, nomessage, nosuffix)
360 Lisp_Object str, noerror, nomessage, nosuffix;
361{
362 register FILE *stream;
363 register int fd = -1;
364 register Lisp_Object lispstream;
078e7b4a
JB
365 int count = specpdl_ptr - specpdl;
366 Lisp_Object temp;
367 struct gcpro gcpro1;
368 Lisp_Object found;
51ac6f83
RS
369 /* 1 means inhibit the message at the beginning. */
370 int nomessage1 = 0;
c2225d00 371 Lisp_Object handler;
317073d5 372#ifdef DOS_NT
23a71bd6 373 char *dosmode = "rt";
317073d5 374#endif /* DOS_NT */
078e7b4a
JB
375
376 CHECK_STRING (str, 0);
078e7b4a 377
c2225d00 378 /* If file name is magic, call the handler. */
a617e913 379 handler = Ffind_file_name_handler (str, Qload);
c2225d00 380 if (!NILP (handler))
8a2c760a 381 return call5 (handler, Qload, str, noerror, nomessage, nosuffix);
c2225d00 382
07a0bda3
RS
383 /* Do this after the handler to avoid
384 the need to gcpro noerror, nomessage and nosuffix.
385 (Below here, we care only whether they are nil or not.) */
386 str = Fsubstitute_in_file_name (str);
387
078e7b4a
JB
388 /* Avoid weird lossage with null string as arg,
389 since it would try to load a directory as a Lisp file */
390 if (XSTRING (str)->size > 0)
391 {
5a6e5452 392 GCPRO1 (str);
265a9e55 393 fd = openp (Vload_path, str, !NILP (nosuffix) ? "" : ".elc:.el:",
078e7b4a 394 &found, 0);
5a6e5452 395 UNGCPRO;
078e7b4a
JB
396 }
397
398 if (fd < 0)
399 {
265a9e55 400 if (NILP (noerror))
078e7b4a
JB
401 while (1)
402 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
403 Fcons (str, Qnil)));
404 else
405 return Qnil;
406 }
407
408 if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]),
409 ".elc", 4))
410 {
411 struct stat s1, s2;
412 int result;
413
317073d5 414#ifdef DOS_NT
23a71bd6 415 dosmode = "rb";
317073d5 416#endif /* DOS_NT */
4ff37b08 417 stat ((char *)XSTRING (found)->data, &s1);
078e7b4a 418 XSTRING (found)->data[XSTRING (found)->size - 1] = 0;
4ff37b08 419 result = stat ((char *)XSTRING (found)->data, &s2);
078e7b4a 420 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
51ac6f83
RS
421 {
422 message ("Source file `%s' newer than byte-compiled file",
423 XSTRING (found)->data);
424 /* Don't immediately overwrite this message. */
425 if (!noninteractive)
426 nomessage1 = 1;
427 }
078e7b4a
JB
428 XSTRING (found)->data[XSTRING (found)->size - 1] = 'c';
429 }
430
317073d5 431#ifdef DOS_NT
23a71bd6
RS
432 close (fd);
433 stream = fopen ((char *) XSTRING (found)->data, dosmode);
317073d5 434#else /* not DOS_NT */
078e7b4a 435 stream = fdopen (fd, "r");
317073d5 436#endif /* not DOS_NT */
078e7b4a
JB
437 if (stream == 0)
438 {
439 close (fd);
440 error ("Failure to create stdio stream for %s", XSTRING (str)->data);
441 }
442
51ac6f83 443 if (NILP (nomessage) && !nomessage1)
078e7b4a
JB
444 message ("Loading %s...", XSTRING (str)->data);
445
446 GCPRO1 (str);
838abf56
KH
447 lispstream = Fcons (Qnil, Qnil);
448 XSETFASTINT (XCONS (lispstream)->car, (EMACS_UINT)stream >> 16);
449 XSETFASTINT (XCONS (lispstream)->cdr, (EMACS_UINT)stream & 0xffff);
078e7b4a 450 record_unwind_protect (load_unwind, lispstream);
d2c6be7f 451 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
20ea2964 452 specbind (Qload_file_name, found);
d2c6be7f
RS
453 load_descriptor_list
454 = Fcons (make_number (fileno (stream)), load_descriptor_list);
078e7b4a 455 load_in_progress++;
ae321d28 456 readevalloop (Qget_file_char, stream, str, Feval, 0);
078e7b4a
JB
457 unbind_to (count, Qnil);
458
459 /* Run any load-hooks for this file. */
460 temp = Fassoc (str, Vafter_load_alist);
265a9e55 461 if (!NILP (temp))
078e7b4a
JB
462 Fprogn (Fcdr (temp));
463 UNGCPRO;
464
265a9e55 465 if (!noninteractive && NILP (nomessage))
078e7b4a
JB
466 message ("Loading %s...done", XSTRING (str)->data);
467 return Qt;
468}
469
470static Lisp_Object
471load_unwind (stream) /* used as unwind-protect function in load */
472 Lisp_Object stream;
473{
c8bdaa8c
RS
474 fclose ((FILE *) (XFASTINT (XCONS (stream)->car) << 16
475 | XFASTINT (XCONS (stream)->cdr)));
078e7b4a
JB
476 if (--load_in_progress < 0) load_in_progress = 0;
477 return Qnil;
478}
479
d2c6be7f
RS
480static Lisp_Object
481load_descriptor_unwind (oldlist)
482 Lisp_Object oldlist;
483{
484 load_descriptor_list = oldlist;
838abf56 485 return Qnil;
d2c6be7f
RS
486}
487
488/* Close all descriptors in use for Floads.
489 This is used when starting a subprocess. */
490
491void
492close_load_descs ()
493{
494 Lisp_Object tail;
495 for (tail = load_descriptor_list; !NILP (tail); tail = XCONS (tail)->cdr)
496 close (XFASTINT (XCONS (tail)->car));
497}
078e7b4a
JB
498\f
499static int
500complete_filename_p (pathname)
501 Lisp_Object pathname;
502{
503 register unsigned char *s = XSTRING (pathname)->data;
317073d5
RS
504 return (IS_DIRECTORY_SEP (s[0])
505 || (XSTRING (pathname)->size > 2
506 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
078e7b4a
JB
507#ifdef ALTOS
508 || *s == '@'
509#endif
510#ifdef VMS
511 || index (s, ':')
512#endif /* VMS */
513 );
514}
515
516/* Search for a file whose name is STR, looking in directories
517 in the Lisp list PATH, and trying suffixes from SUFFIX.
518 SUFFIX is a string containing possible suffixes separated by colons.
519 On success, returns a file descriptor. On failure, returns -1.
520
521 EXEC_ONLY nonzero means don't open the files,
522 just look for one that is executable. In this case,
523 returns 1 on success.
524
525 If STOREPTR is nonzero, it points to a slot where the name of
526 the file actually found should be stored as a Lisp string.
527 Nil is stored there on failure. */
528
529int
530openp (path, str, suffix, storeptr, exec_only)
531 Lisp_Object path, str;
532 char *suffix;
533 Lisp_Object *storeptr;
534 int exec_only;
535{
536 register int fd;
537 int fn_size = 100;
538 char buf[100];
539 register char *fn = buf;
540 int absolute = 0;
541 int want_size;
542 register Lisp_Object filename;
543 struct stat st;
5a6e5452 544 struct gcpro gcpro1;
078e7b4a 545
5a6e5452 546 GCPRO1 (str);
078e7b4a
JB
547 if (storeptr)
548 *storeptr = Qnil;
549
550 if (complete_filename_p (str))
551 absolute = 1;
552
265a9e55 553 for (; !NILP (path); path = Fcdr (path))
078e7b4a
JB
554 {
555 char *nsuffix;
556
557 filename = Fexpand_file_name (str, Fcar (path));
558 if (!complete_filename_p (filename))
559 /* If there are non-absolute elts in PATH (eg ".") */
560 /* Of course, this could conceivably lose if luser sets
561 default-directory to be something non-absolute... */
562 {
563 filename = Fexpand_file_name (filename, current_buffer->directory);
564 if (!complete_filename_p (filename))
565 /* Give up on this path element! */
566 continue;
567 }
568
569 /* Calculate maximum size of any filename made from
570 this path element/specified file name and any possible suffix. */
571 want_size = strlen (suffix) + XSTRING (filename)->size + 1;
572 if (fn_size < want_size)
573 fn = (char *) alloca (fn_size = 100 + want_size);
574
575 nsuffix = suffix;
576
577 /* Loop over suffixes. */
578 while (1)
579 {
580 char *esuffix = (char *) index (nsuffix, ':');
581 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
582
583 /* Concatenate path element/specified name with the suffix. */
584 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
585 fn[XSTRING (filename)->size] = 0;
586 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
587 strncat (fn, nsuffix, lsuffix);
588
589 /* Ignore file if it's a directory. */
590 if (stat (fn, &st) >= 0
591 && (st.st_mode & S_IFMT) != S_IFDIR)
592 {
593 /* Check that we can access or open it. */
594 if (exec_only)
595 fd = (access (fn, X_OK) == 0) ? 1 : -1;
596 else
f7d279f0 597 fd = open (fn, O_RDONLY, 0);
078e7b4a
JB
598
599 if (fd >= 0)
600 {
601 /* We succeeded; return this descriptor and filename. */
602 if (storeptr)
603 *storeptr = build_string (fn);
5ef2a3c0
KH
604 UNGCPRO;
605 return fd;
078e7b4a
JB
606 }
607 }
608
609 /* Advance to next suffix. */
610 if (esuffix == 0)
611 break;
612 nsuffix += lsuffix + 1;
613 }
5a6e5452 614 if (absolute)
5ef2a3c0 615 break;
078e7b4a
JB
616 }
617
5ef2a3c0
KH
618 UNGCPRO;
619 return -1;
078e7b4a
JB
620}
621
622\f
ae321d28
RS
623/* Merge the list we've accumulated of globals from the current input source
624 into the load_history variable. The details depend on whether
625 the source has an associated file name or not. */
626
627static void
628build_load_history (stream, source)
629 FILE *stream;
630 Lisp_Object source;
631{
632 register Lisp_Object tail, prev, newelt;
633 register Lisp_Object tem, tem2;
634 register int foundit, loading;
635
8a1f1537
RS
636 /* Don't bother recording anything for preloaded files. */
637 if (!NILP (Vpurify_flag))
638 return;
639
ae321d28
RS
640 loading = stream || !NARROWED;
641
642 tail = Vload_history;
643 prev = Qnil;
644 foundit = 0;
645 while (!NILP (tail))
646 {
647 tem = Fcar (tail);
648
649 /* Find the feature's previous assoc list... */
650 if (!NILP (Fequal (source, Fcar (tem))))
651 {
652 foundit = 1;
653
654 /* If we're loading, remove it. */
655 if (loading)
656 {
657 if (NILP (prev))
658 Vload_history = Fcdr (tail);
659 else
660 Fsetcdr (prev, Fcdr (tail));
661 }
662
663 /* Otherwise, cons on new symbols that are not already members. */
664 else
665 {
666 tem2 = Vcurrent_load_list;
667
668 while (CONSP (tem2))
669 {
670 newelt = Fcar (tem2);
671
672 if (NILP (Fmemq (newelt, tem)))
673 Fsetcar (tail, Fcons (Fcar (tem),
674 Fcons (newelt, Fcdr (tem))));
675
676 tem2 = Fcdr (tem2);
677 QUIT;
678 }
679 }
680 }
681 else
682 prev = tail;
683 tail = Fcdr (tail);
684 QUIT;
685 }
686
8a1f1537
RS
687 /* If we're loading, cons the new assoc onto the front of load-history,
688 the most-recently-loaded position. Also do this if we didn't find
689 an existing member for the current source. */
690 if (loading || !foundit)
691 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
692 Vload_history);
ae321d28
RS
693}
694
078e7b4a
JB
695Lisp_Object
696unreadpure () /* Used as unwind-protect function in readevalloop */
697{
698 read_pure = 0;
699 return Qnil;
700}
701
702static void
ae321d28 703readevalloop (readcharfun, stream, sourcename, evalfun, printflag)
078e7b4a 704 Lisp_Object readcharfun;
ae321d28
RS
705 FILE *stream;
706 Lisp_Object sourcename;
078e7b4a
JB
707 Lisp_Object (*evalfun) ();
708 int printflag;
709{
710 register int c;
711 register Lisp_Object val;
712 int count = specpdl_ptr - specpdl;
8a1f1537 713 struct gcpro gcpro1;
49cf7ff4
RS
714 struct buffer *b = 0;
715
716 if (BUFFERP (readcharfun))
717 b = XBUFFER (readcharfun);
718 else if (MARKERP (readcharfun))
719 b = XMARKER (readcharfun)->buffer;
078e7b4a
JB
720
721 specbind (Qstandard_input, readcharfun);
8a1f1537 722 specbind (Qcurrent_load_list, Qnil);
078e7b4a 723
8a1f1537 724 GCPRO1 (sourcename);
ae321d28 725
ae321d28
RS
726 LOADHIST_ATTACH (sourcename);
727
078e7b4a
JB
728 while (1)
729 {
49cf7ff4
RS
730 if (b != 0 && NILP (b->name))
731 error ("Reading from killed buffer");
732
078e7b4a
JB
733 instream = stream;
734 c = READCHAR;
735 if (c == ';')
736 {
737 while ((c = READCHAR) != '\n' && c != -1);
738 continue;
739 }
740 if (c < 0) break;
6069d957
RS
741
742 /* Ignore whitespace here, so we can detect eof. */
743 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
744 continue;
078e7b4a 745
265a9e55 746 if (!NILP (Vpurify_flag) && c == '(')
078e7b4a 747 {
0e326373 748 int count1 = specpdl_ptr - specpdl;
078e7b4a
JB
749 record_unwind_protect (unreadpure, Qnil);
750 val = read_list (-1, readcharfun);
0e326373 751 unbind_to (count1, Qnil);
078e7b4a
JB
752 }
753 else
754 {
755 UNREAD (c);
84a15045
RS
756 if (NILP (Vload_read_function))
757 val = read0 (readcharfun);
758 else
759 val = call1 (Vload_read_function, readcharfun);
078e7b4a
JB
760 }
761
762 val = (*evalfun) (val);
763 if (printflag)
764 {
765 Vvalues = Fcons (val, Vvalues);
766 if (EQ (Vstandard_output, Qt))
767 Fprin1 (val, Qnil);
768 else
769 Fprint (val, Qnil);
770 }
771 }
772
ae321d28 773 build_load_history (stream, sourcename);
ae321d28
RS
774 UNGCPRO;
775
078e7b4a
JB
776 unbind_to (count, Qnil);
777}
778
779#ifndef standalone
780
e5d77022 781DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 2, "",
94b304d7
RS
782 "Execute the current buffer as Lisp code.\n\
783Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
784BUFFER is the buffer to evaluate (nil means use current buffer).\n\
785PRINTFLAG controls printing of output:\n\
228d4b1c
JA
786nil means discard it; anything else is stream for print.\n\
787\n\
788If there is no error, point does not move. If there is an error,\n\
789point remains at the end of the last character read from the buffer.")
790 (bufname, printflag)
791 Lisp_Object bufname, printflag;
792{
793 int count = specpdl_ptr - specpdl;
794 Lisp_Object tem, buf;
795
dfdb645c 796 if (NILP (bufname))
228d4b1c
JA
797 buf = Fcurrent_buffer ();
798 else
799 buf = Fget_buffer (bufname);
dfdb645c 800 if (NILP (buf))
228d4b1c
JA
801 error ("No such buffer.");
802
dfdb645c 803 if (NILP (printflag))
228d4b1c
JA
804 tem = Qsymbolp;
805 else
806 tem = printflag;
807 specbind (Qstandard_output, tem);
808 record_unwind_protect (save_excursion_restore, save_excursion_save ());
809 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
ae321d28 810 readevalloop (buf, 0, XBUFFER (buf)->filename, Feval, !NILP (printflag));
cb09ab7a 811 unbind_to (count, Qnil);
228d4b1c
JA
812
813 return Qnil;
814}
815
816#if 0
078e7b4a
JB
817DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
818 "Execute the current buffer as Lisp code.\n\
819Programs can pass argument PRINTFLAG which controls printing of output:\n\
820nil means discard it; anything else is stream for print.\n\
821\n\
822If there is no error, point does not move. If there is an error,\n\
823point remains at the end of the last character read from the buffer.")
824 (printflag)
825 Lisp_Object printflag;
826{
827 int count = specpdl_ptr - specpdl;
ae321d28
RS
828 Lisp_Object tem, cbuf;
829
830 cbuf = Fcurrent_buffer ()
078e7b4a 831
265a9e55 832 if (NILP (printflag))
078e7b4a
JB
833 tem = Qsymbolp;
834 else
835 tem = printflag;
836 specbind (Qstandard_output, tem);
837 record_unwind_protect (save_excursion_restore, save_excursion_save ());
838 SET_PT (BEGV);
ae321d28 839 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
078e7b4a
JB
840 return unbind_to (count, Qnil);
841}
228d4b1c 842#endif
078e7b4a
JB
843
844DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
845 "Execute the region as Lisp code.\n\
846When called from programs, expects two arguments,\n\
847giving starting and ending indices in the current buffer\n\
848of the text to be executed.\n\
849Programs can pass third argument PRINTFLAG which controls output:\n\
850nil means discard it; anything else is stream for printing it.\n\
851\n\
852If there is no error, point does not move. If there is an error,\n\
853point remains at the end of the last character read from the buffer.")
854 (b, e, printflag)
855 Lisp_Object b, e, printflag;
856{
857 int count = specpdl_ptr - specpdl;
ae321d28
RS
858 Lisp_Object tem, cbuf;
859
860 cbuf = Fcurrent_buffer ();
078e7b4a 861
265a9e55 862 if (NILP (printflag))
078e7b4a
JB
863 tem = Qsymbolp;
864 else
865 tem = printflag;
866 specbind (Qstandard_output, tem);
867
265a9e55 868 if (NILP (printflag))
078e7b4a
JB
869 record_unwind_protect (save_excursion_restore, save_excursion_save ());
870 record_unwind_protect (save_restriction_restore, save_restriction_save ());
871
872 /* This both uses b and checks its type. */
873 Fgoto_char (b);
874 Fnarrow_to_region (make_number (BEGV), e);
ae321d28 875 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
078e7b4a
JB
876
877 return unbind_to (count, Qnil);
878}
879
880#endif /* standalone */
881\f
882DEFUN ("read", Fread, Sread, 0, 1, 0,
883 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
884If STREAM is nil, use the value of `standard-input' (which see).\n\
885STREAM or the value of `standard-input' may be:\n\
886 a buffer (read from point and advance it)\n\
887 a marker (read from where it points and advance it)\n\
888 a function (call it with no arguments for each character,\n\
889 call it with a char as argument to push a char back)\n\
890 a string (takes text from string, starting at the beginning)\n\
891 t (read text line using minibuffer and use it).")
5be02dff
KH
892 (stream)
893 Lisp_Object stream;
078e7b4a
JB
894{
895 extern Lisp_Object Fread_minibuffer ();
896
5be02dff
KH
897 if (NILP (stream))
898 stream = Vstandard_input;
899 if (EQ (stream, Qt))
900 stream = Qread_char;
078e7b4a 901
17634846
RS
902 new_backquote_flag = 0;
903
078e7b4a 904#ifndef standalone
5be02dff 905 if (EQ (stream, Qread_char))
078e7b4a
JB
906 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
907#endif
908
5be02dff
KH
909 if (STRINGP (stream))
910 return Fcar (Fread_from_string (stream, Qnil, Qnil));
078e7b4a 911
5be02dff 912 return read0 (stream);
078e7b4a
JB
913}
914
915DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
916 "Read one Lisp expression which is represented as text by STRING.\n\
917Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
918START and END optionally delimit a substring of STRING from which to read;\n\
919 they default to 0 and (length STRING) respectively.")
920 (string, start, end)
921 Lisp_Object string, start, end;
922{
923 int startval, endval;
924 Lisp_Object tem;
925
926 CHECK_STRING (string,0);
927
265a9e55 928 if (NILP (end))
078e7b4a
JB
929 endval = XSTRING (string)->size;
930 else
931 { CHECK_NUMBER (end,2);
932 endval = XINT (end);
933 if (endval < 0 || endval > XSTRING (string)->size)
934 args_out_of_range (string, end);
935 }
936
265a9e55 937 if (NILP (start))
078e7b4a
JB
938 startval = 0;
939 else
940 { CHECK_NUMBER (start,1);
941 startval = XINT (start);
942 if (startval < 0 || startval > endval)
943 args_out_of_range (string, start);
944 }
945
946 read_from_string_index = startval;
947 read_from_string_limit = endval;
948
17634846
RS
949 new_backquote_flag = 0;
950
078e7b4a
JB
951 tem = read0 (string);
952 return Fcons (tem, make_number (read_from_string_index));
953}
954\f
6428369f
KH
955/* Use this for recursive reads, in contexts where internal tokens
956 are not allowed. */
078e7b4a
JB
957static Lisp_Object
958read0 (readcharfun)
959 Lisp_Object readcharfun;
960{
961 register Lisp_Object val;
962 char c;
963
17634846 964 val = read1 (readcharfun, &c, 0);
6428369f
KH
965 if (c)
966 Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
078e7b4a
JB
967
968 return val;
969}
970\f
971static int read_buffer_size;
972static char *read_buffer;
973
974static int
975read_escape (readcharfun)
976 Lisp_Object readcharfun;
977{
978 register int c = READCHAR;
979 switch (c)
980 {
981 case 'a':
265a9e55 982 return '\007';
078e7b4a
JB
983 case 'b':
984 return '\b';
f405a585
RS
985 case 'd':
986 return 0177;
078e7b4a
JB
987 case 'e':
988 return 033;
989 case 'f':
990 return '\f';
991 case 'n':
992 return '\n';
993 case 'r':
994 return '\r';
995 case 't':
996 return '\t';
997 case 'v':
998 return '\v';
999 case '\n':
1000 return -1;
1001
1002 case 'M':
1003 c = READCHAR;
1004 if (c != '-')
1005 error ("Invalid escape character syntax");
1006 c = READCHAR;
1007 if (c == '\\')
1008 c = read_escape (readcharfun);
7bd279cd 1009 return c | meta_modifier;
f405a585
RS
1010
1011 case 'S':
1012 c = READCHAR;
1013 if (c != '-')
1014 error ("Invalid escape character syntax");
1015 c = READCHAR;
1016 if (c == '\\')
1017 c = read_escape (readcharfun);
7bd279cd
RS
1018 return c | shift_modifier;
1019
1020 case 'H':
1021 c = READCHAR;
1022 if (c != '-')
1023 error ("Invalid escape character syntax");
1024 c = READCHAR;
1025 if (c == '\\')
1026 c = read_escape (readcharfun);
1027 return c | hyper_modifier;
1028
1029 case 'A':
1030 c = READCHAR;
1031 if (c != '-')
1032 error ("Invalid escape character syntax");
1033 c = READCHAR;
1034 if (c == '\\')
1035 c = read_escape (readcharfun);
1036 return c | alt_modifier;
1037
1038 case 's':
1039 c = READCHAR;
1040 if (c != '-')
1041 error ("Invalid escape character syntax");
1042 c = READCHAR;
1043 if (c == '\\')
1044 c = read_escape (readcharfun);
1045 return c | super_modifier;
078e7b4a
JB
1046
1047 case 'C':
1048 c = READCHAR;
1049 if (c != '-')
1050 error ("Invalid escape character syntax");
1051 case '^':
1052 c = READCHAR;
1053 if (c == '\\')
1054 c = read_escape (readcharfun);
f405a585
RS
1055 if ((c & 0177) == '?')
1056 return 0177 | c;
1057 /* ASCII control chars are made from letters (both cases),
1058 as well as the non-letters within 0100...0137. */
1059 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1060 return (c & (037 | ~0177));
1061 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1062 return (c & (037 | ~0177));
078e7b4a 1063 else
7bd279cd 1064 return c | ctrl_modifier;
078e7b4a
JB
1065
1066 case '0':
1067 case '1':
1068 case '2':
1069 case '3':
1070 case '4':
1071 case '5':
1072 case '6':
1073 case '7':
1074 /* An octal escape, as in ANSI C. */
1075 {
1076 register int i = c - '0';
1077 register int count = 0;
1078 while (++count < 3)
1079 {
1080 if ((c = READCHAR) >= '0' && c <= '7')
1081 {
1082 i *= 8;
1083 i += c - '0';
1084 }
1085 else
1086 {
1087 UNREAD (c);
1088 break;
1089 }
1090 }
1091 return i;
1092 }
1093
1094 case 'x':
1095 /* A hex escape, as in ANSI C. */
1096 {
1097 int i = 0;
1098 while (1)
1099 {
1100 c = READCHAR;
1101 if (c >= '0' && c <= '9')
1102 {
1103 i *= 16;
1104 i += c - '0';
1105 }
1106 else if ((c >= 'a' && c <= 'f')
1107 || (c >= 'A' && c <= 'F'))
1108 {
1109 i *= 16;
1110 if (c >= 'a' && c <= 'f')
1111 i += c - 'a' + 10;
1112 else
1113 i += c - 'A' + 10;
1114 }
1115 else
1116 {
1117 UNREAD (c);
1118 break;
1119 }
1120 }
1121 return i;
1122 }
1123
1124 default:
1125 return c;
1126 }
1127}
1128
6428369f
KH
1129/* If the next token is ')' or ']' or '.', we store that character
1130 in *PCH and the return value is not interesting. Else, we store
17634846
RS
1131 zero in *PCH and we read and return one lisp object.
1132
1133 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1134
078e7b4a 1135static Lisp_Object
17634846 1136read1 (readcharfun, pch, first_in_list)
078e7b4a 1137 register Lisp_Object readcharfun;
6428369f 1138 char *pch;
17634846 1139 int first_in_list;
078e7b4a
JB
1140{
1141 register int c;
6428369f 1142 *pch = 0;
078e7b4a
JB
1143
1144 retry:
1145
1146 c = READCHAR;
1147 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1148
1149 switch (c)
1150 {
1151 case '(':
1152 return read_list (0, readcharfun);
1153
1154 case '[':
1155 return read_vector (readcharfun);
1156
1157 case ')':
1158 case ']':
078e7b4a 1159 {
6428369f
KH
1160 *pch = c;
1161 return Qnil;
078e7b4a
JB
1162 }
1163
1164 case '#':
200f684e
RS
1165 c = READCHAR;
1166 if (c == '[')
1167 {
1168 /* Accept compiled functions at read-time so that we don't have to
1169 build them using function calls. */
748ef62f
RS
1170 Lisp_Object tmp;
1171 tmp = read_vector (readcharfun);
1172 return Fmake_byte_code (XVECTOR (tmp)->size,
1173 XVECTOR (tmp)->contents);
200f684e 1174 }
748ef62f
RS
1175#ifdef USE_TEXT_PROPERTIES
1176 if (c == '(')
1177 {
1178 Lisp_Object tmp;
1179 struct gcpro gcpro1;
6428369f 1180 char ch;
748ef62f
RS
1181
1182 /* Read the string itself. */
17634846 1183 tmp = read1 (readcharfun, &ch, 0);
6428369f 1184 if (ch != 0 || !STRINGP (tmp))
748ef62f
RS
1185 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1186 GCPRO1 (tmp);
1187 /* Read the intervals and their properties. */
1188 while (1)
1189 {
1190 Lisp_Object beg, end, plist;
1191
17634846 1192 beg = read1 (readcharfun, &ch, 0);
6428369f
KH
1193 if (ch == ')')
1194 break;
1195 if (ch == 0)
17634846 1196 end = read1 (readcharfun, &ch, 0);
6428369f 1197 if (ch == 0)
17634846 1198 plist = read1 (readcharfun, &ch, 0);
6428369f 1199 if (ch)
748ef62f 1200 Fsignal (Qinvalid_read_syntax,
6428369f
KH
1201 Fcons (build_string ("invalid string property list"),
1202 Qnil));
748ef62f
RS
1203 Fset_text_properties (beg, end, plist, tmp);
1204 }
1205 UNGCPRO;
1206 return tmp;
1207 }
1208#endif
20ea2964
RS
1209 /* #@NUMBER is used to skip NUMBER following characters.
1210 That's used in .elc files to skip over doc strings
1211 and function definitions. */
1212 if (c == '@')
1213 {
1214 int i, nskip = 0;
1215
1216 /* Read a decimal integer. */
1217 while ((c = READCHAR) >= 0
1218 && c >= '0' && c <= '9')
1219 {
1220 nskip *= 10;
1221 nskip += c - '0';
1222 }
1223 if (c >= 0)
1224 UNREAD (c);
1225
1226 /* Skip that many characters. */
1227 for (i = 0; i < nskip && c >= 0; i++)
1228 c = READCHAR;
1229 goto retry;
1230 }
1231 if (c == '$')
1232 return Vload_file_name;
1233
200f684e 1234 UNREAD (c);
748ef62f 1235 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
078e7b4a
JB
1236
1237 case ';':
1238 while ((c = READCHAR) >= 0 && c != '\n');
1239 goto retry;
1240
1241 case '\'':
1242 {
1243 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1244 }
1245
17634846
RS
1246 case '`':
1247 if (first_in_list)
1248 goto default_label;
1249 else
1250 {
1251 Lisp_Object value;
1252
1253 new_backquote_flag = 1;
1254 value = read0 (readcharfun);
1255 new_backquote_flag = 0;
1256
1257 return Fcons (Qbackquote, Fcons (value, Qnil));
1258 }
1259
1260 case ',':
1261 if (new_backquote_flag)
1262 {
1263 Lisp_Object comma_type = Qnil;
1264 Lisp_Object value;
1265 int ch = READCHAR;
1266
1267 if (ch == '@')
1268 comma_type = Qcomma_at;
1269 else if (ch == '.')
1270 comma_type = Qcomma_dot;
1271 else
1272 {
1273 if (ch >= 0) UNREAD (ch);
1274 comma_type = Qcomma;
1275 }
1276
1277 new_backquote_flag = 0;
1278 value = read0 (readcharfun);
1279 new_backquote_flag = 1;
1280 return Fcons (comma_type, Fcons (value, Qnil));
1281 }
1282 else
1283 goto default_label;
1284
078e7b4a
JB
1285 case '?':
1286 {
1287 register Lisp_Object val;
1288
1289 c = READCHAR;
1290 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1291
1292 if (c == '\\')
1805de4f 1293 XSETINT (val, read_escape (readcharfun));
078e7b4a 1294 else
1805de4f 1295 XSETINT (val, c);
078e7b4a
JB
1296
1297 return val;
1298 }
1299
1300 case '\"':
1301 {
1302 register char *p = read_buffer;
1303 register char *end = read_buffer + read_buffer_size;
1304 register int c;
1305 int cancel = 0;
1306
1307 while ((c = READCHAR) >= 0
1308 && c != '\"')
1309 {
1310 if (p == end)
1311 {
1312 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1313 p += new - read_buffer;
1314 read_buffer += new - read_buffer;
1315 end = read_buffer + read_buffer_size;
1316 }
1317 if (c == '\\')
1318 c = read_escape (readcharfun);
1319 /* c is -1 if \ newline has just been seen */
f405a585 1320 if (c == -1)
078e7b4a
JB
1321 {
1322 if (p == read_buffer)
1323 cancel = 1;
1324 }
1325 else
f943104a 1326 {
988c2f83
RS
1327 /* Allow `\C- ' and `\C-?'. */
1328 if (c == (CHAR_CTL | ' '))
1329 c = 0;
1330 else if (c == (CHAR_CTL | '?'))
1331 c = 127;
1332
f943104a
KH
1333 if (c & CHAR_META)
1334 /* Move the meta bit to the right place for a string. */
1335 c = (c & ~CHAR_META) | 0x80;
1336 if (c & ~0xff)
1337 error ("Invalid modifier in string");
1338 *p++ = c;
1339 }
078e7b4a
JB
1340 }
1341 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1342
1343 /* If purifying, and string starts with \ newline,
1344 return zero instead. This is for doc strings
08564963 1345 that we are really going to find in etc/DOC.nn.nn */
265a9e55 1346 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
078e7b4a
JB
1347 return make_number (0);
1348
1349 if (read_pure)
1350 return make_pure_string (read_buffer, p - read_buffer);
1351 else
1352 return make_string (read_buffer, p - read_buffer);
1353 }
1354
109d300c
JB
1355 case '.':
1356 {
1357#ifdef LISP_FLOAT_TYPE
1358 /* If a period is followed by a number, then we should read it
1359 as a floating point number. Otherwise, it denotes a dotted
1360 pair. */
1361 int next_char = READCHAR;
1362 UNREAD (next_char);
1363
075027b1 1364 if (! (next_char >= '0' && next_char <= '9'))
109d300c
JB
1365#endif
1366 {
6428369f
KH
1367 *pch = c;
1368 return Qnil;
109d300c
JB
1369 }
1370
1371 /* Otherwise, we fall through! Note that the atom-reading loop
1372 below will now loop at least once, assuring that we will not
1373 try to UNREAD two characters in a row. */
1374 }
078e7b4a 1375 default:
17634846 1376 default_label:
078e7b4a
JB
1377 if (c <= 040) goto retry;
1378 {
1379 register char *p = read_buffer;
481c6336 1380 int quoted = 0;
078e7b4a
JB
1381
1382 {
1383 register char *end = read_buffer + read_buffer_size;
1384
1385 while (c > 040 &&
1386 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1387 || c == '(' || c == ')'
109d300c
JB
1388#ifndef LISP_FLOAT_TYPE
1389 /* If we have floating-point support, then we need
1390 to allow <digits><dot><digits>. */
078e7b4a
JB
1391 || c =='.'
1392#endif /* not LISP_FLOAT_TYPE */
1393 || c == '[' || c == ']' || c == '#'
1394 ))
1395 {
1396 if (p == end)
1397 {
1398 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1399 p += new - read_buffer;
1400 read_buffer += new - read_buffer;
1401 end = read_buffer + read_buffer_size;
1402 }
1403 if (c == '\\')
481c6336
RS
1404 {
1405 c = READCHAR;
1406 quoted = 1;
1407 }
078e7b4a
JB
1408 *p++ = c;
1409 c = READCHAR;
1410 }
1411
1412 if (p == end)
1413 {
1414 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1415 p += new - read_buffer;
1416 read_buffer += new - read_buffer;
1417/* end = read_buffer + read_buffer_size; */
1418 }
1419 *p = 0;
1420 if (c >= 0)
1421 UNREAD (c);
1422 }
1423
481c6336
RS
1424 if (!quoted)
1425 {
1426 register char *p1;
1427 register Lisp_Object val;
1428 p1 = read_buffer;
1429 if (*p1 == '+' || *p1 == '-') p1++;
1430 /* Is it an integer? */
1431 if (p1 != p)
1432 {
1433 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
dbc4e1c1 1434#ifdef LISP_FLOAT_TYPE
481c6336
RS
1435 /* Integers can have trailing decimal points. */
1436 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
dbc4e1c1 1437#endif
481c6336
RS
1438 if (p1 == p)
1439 /* It is an integer. */
1440 {
dbc4e1c1 1441#ifdef LISP_FLOAT_TYPE
481c6336
RS
1442 if (p1[-1] == '.')
1443 p1[-1] = '\0';
dbc4e1c1 1444#endif
faca07fb
RS
1445 if (sizeof (int) == sizeof (EMACS_INT))
1446 XSETINT (val, atoi (read_buffer));
1447 else if (sizeof (long) == sizeof (EMACS_INT))
1448 XSETINT (val, atol (read_buffer));
1449 else
1450 abort ();
481c6336
RS
1451 return val;
1452 }
1453 }
078e7b4a 1454#ifdef LISP_FLOAT_TYPE
481c6336
RS
1455 if (isfloat_string (read_buffer))
1456 return make_float (atof (read_buffer));
078e7b4a 1457#endif
481c6336 1458 }
078e7b4a
JB
1459
1460 return intern (read_buffer);
1461 }
1462 }
1463}
1464\f
1465#ifdef LISP_FLOAT_TYPE
1466
078e7b4a
JB
1467#define LEAD_INT 1
1468#define DOT_CHAR 2
1469#define TRAIL_INT 4
1470#define E_CHAR 8
1471#define EXP_INT 16
1472
1473int
1474isfloat_string (cp)
1475 register char *cp;
1476{
1477 register state;
1478
1479 state = 0;
1480 if (*cp == '+' || *cp == '-')
1481 cp++;
1482
075027b1 1483 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
1484 {
1485 state |= LEAD_INT;
075027b1
RS
1486 while (*cp >= '0' && *cp <= '9')
1487 cp++;
078e7b4a
JB
1488 }
1489 if (*cp == '.')
1490 {
1491 state |= DOT_CHAR;
1492 cp++;
1493 }
075027b1 1494 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
1495 {
1496 state |= TRAIL_INT;
075027b1 1497 while (*cp >= '0' && *cp <= '9')
078e7b4a
JB
1498 cp++;
1499 }
1500 if (*cp == 'e')
1501 {
1502 state |= E_CHAR;
1503 cp++;
e73997a1
RS
1504 if (*cp == '+' || *cp == '-')
1505 cp++;
078e7b4a 1506 }
078e7b4a 1507
075027b1 1508 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
1509 {
1510 state |= EXP_INT;
075027b1 1511 while (*cp >= '0' && *cp <= '9')
078e7b4a
JB
1512 cp++;
1513 }
37579d7c 1514 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
078e7b4a 1515 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
151bdc83 1516 || state == (DOT_CHAR|TRAIL_INT)
078e7b4a 1517 || state == (LEAD_INT|E_CHAR|EXP_INT)
151bdc83
JB
1518 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1519 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
078e7b4a
JB
1520}
1521#endif /* LISP_FLOAT_TYPE */
1522\f
1523static Lisp_Object
1524read_vector (readcharfun)
1525 Lisp_Object readcharfun;
1526{
1527 register int i;
1528 register int size;
1529 register Lisp_Object *ptr;
1530 register Lisp_Object tem, vector;
1531 register struct Lisp_Cons *otem;
1532 Lisp_Object len;
1533
1534 tem = read_list (1, readcharfun);
1535 len = Flength (tem);
1536 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1537
1538
1539 size = XVECTOR (vector)->size;
1540 ptr = XVECTOR (vector)->contents;
1541 for (i = 0; i < size; i++)
1542 {
1543 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1544 otem = XCONS (tem);
1545 tem = Fcdr (tem);
1546 free_cons (otem);
1547 }
1548 return vector;
1549}
1550
1551/* flag = 1 means check for ] to terminate rather than ) and .
1552 flag = -1 means check for starting with defun
1553 and make structure pure. */
1554
1555static Lisp_Object
1556read_list (flag, readcharfun)
1557 int flag;
1558 register Lisp_Object readcharfun;
1559{
1560 /* -1 means check next element for defun,
1561 0 means don't check,
1562 1 means already checked and found defun. */
1563 int defunflag = flag < 0 ? -1 : 0;
1564 Lisp_Object val, tail;
1565 register Lisp_Object elt, tem;
1566 struct gcpro gcpro1, gcpro2;
20ea2964 1567 int cancel = 0;
078e7b4a 1568
17634846
RS
1569 /* Initialize this to 1 if we are reading a list. */
1570 int first_in_list = flag <= 0;
1571
078e7b4a
JB
1572 val = Qnil;
1573 tail = Qnil;
1574
1575 while (1)
1576 {
6428369f 1577 char ch;
078e7b4a 1578 GCPRO2 (val, tail);
17634846 1579 elt = read1 (readcharfun, &ch, first_in_list);
078e7b4a 1580 UNGCPRO;
20ea2964 1581
17634846
RS
1582 first_in_list = 0;
1583
20ea2964
RS
1584 /* If purifying, and the list starts with #$,
1585 return 0 instead. This is a doc string reference
1586 and it will be replaced anyway by Snarf-documentation,
1587 so don't waste pure space with it. */
1588 if (EQ (elt, Vload_file_name)
1589 && !NILP (Vpurify_flag) && NILP (Vdoc_file_name))
1590 cancel = 1;
1591
6428369f 1592 if (ch)
078e7b4a
JB
1593 {
1594 if (flag > 0)
1595 {
6428369f 1596 if (ch == ']')
078e7b4a 1597 return val;
6428369f 1598 Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
078e7b4a 1599 }
6428369f 1600 if (ch == ')')
078e7b4a 1601 return val;
6428369f 1602 if (ch == '.')
078e7b4a
JB
1603 {
1604 GCPRO2 (val, tail);
265a9e55 1605 if (!NILP (tail))
078e7b4a
JB
1606 XCONS (tail)->cdr = read0 (readcharfun);
1607 else
1608 val = read0 (readcharfun);
17634846 1609 read1 (readcharfun, &ch, 0);
078e7b4a 1610 UNGCPRO;
6428369f 1611 if (ch == ')')
20ea2964 1612 return (cancel ? make_number (0) : val);
078e7b4a
JB
1613 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1614 }
1615 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1616 }
1617 tem = (read_pure && flag <= 0
1618 ? pure_cons (elt, Qnil)
1619 : Fcons (elt, Qnil));
265a9e55 1620 if (!NILP (tail))
078e7b4a
JB
1621 XCONS (tail)->cdr = tem;
1622 else
1623 val = tem;
1624 tail = tem;
1625 if (defunflag < 0)
1626 defunflag = EQ (elt, Qdefun);
1627 else if (defunflag > 0)
1628 read_pure = 1;
1629 }
1630}
1631\f
1632Lisp_Object Vobarray;
1633Lisp_Object initial_obarray;
1634
d007f5c8
RS
1635/* oblookup stores the bucket number here, for the sake of Funintern. */
1636
1637int oblookup_last_bucket_number;
1638
1639static int hash_string ();
1640Lisp_Object oblookup ();
1641
1642/* Get an error if OBARRAY is not an obarray.
1643 If it is one, return it. */
1644
078e7b4a
JB
1645Lisp_Object
1646check_obarray (obarray)
1647 Lisp_Object obarray;
1648{
cfff016d 1649 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
078e7b4a
JB
1650 {
1651 /* If Vobarray is now invalid, force it to be valid. */
1652 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1653
1654 obarray = wrong_type_argument (Qvectorp, obarray);
1655 }
1656 return obarray;
1657}
1658
d007f5c8
RS
1659/* Intern the C string STR: return a symbol with that name,
1660 interned in the current obarray. */
078e7b4a
JB
1661
1662Lisp_Object
1663intern (str)
1664 char *str;
1665{
1666 Lisp_Object tem;
1667 int len = strlen (str);
153a17b7 1668 Lisp_Object obarray;
078e7b4a 1669
153a17b7 1670 obarray = Vobarray;
cfff016d 1671 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
078e7b4a
JB
1672 obarray = check_obarray (obarray);
1673 tem = oblookup (obarray, str, len);
cfff016d 1674 if (SYMBOLP (tem))
078e7b4a 1675 return tem;
265a9e55 1676 return Fintern ((!NILP (Vpurify_flag)
078e7b4a
JB
1677 ? make_pure_string (str, len)
1678 : make_string (str, len)),
1679 obarray);
1680}
d007f5c8 1681\f
078e7b4a
JB
1682DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1683 "Return the canonical symbol whose name is STRING.\n\
1684If there is none, one is created by this function and returned.\n\
1685A second optional argument specifies the obarray to use;\n\
1686it defaults to the value of `obarray'.")
1687 (str, obarray)
1688 Lisp_Object str, obarray;
1689{
1690 register Lisp_Object tem, sym, *ptr;
1691
265a9e55 1692 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1693 obarray = check_obarray (obarray);
1694
1695 CHECK_STRING (str, 0);
1696
1697 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
cfff016d 1698 if (!INTEGERP (tem))
078e7b4a
JB
1699 return tem;
1700
265a9e55 1701 if (!NILP (Vpurify_flag))
078e7b4a
JB
1702 str = Fpurecopy (str);
1703 sym = Fmake_symbol (str);
1704
1705 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
cfff016d 1706 if (SYMBOLP (*ptr))
078e7b4a
JB
1707 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1708 else
1709 XSYMBOL (sym)->next = 0;
1710 *ptr = sym;
1711 return sym;
1712}
1713
1714DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1715 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1716A second optional argument specifies the obarray to use;\n\
1717it defaults to the value of `obarray'.")
1718 (str, obarray)
1719 Lisp_Object str, obarray;
1720{
1721 register Lisp_Object tem;
1722
265a9e55 1723 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1724 obarray = check_obarray (obarray);
1725
1726 CHECK_STRING (str, 0);
1727
1728 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
cfff016d 1729 if (!INTEGERP (tem))
078e7b4a
JB
1730 return tem;
1731 return Qnil;
1732}
d007f5c8
RS
1733\f
1734DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
1735 "Delete the symbol named NAME, if any, from OBARRAY.\n\
1736The value is t if a symbol was found and deleted, nil otherwise.\n\
1737NAME may be a string or a symbol. If it is a symbol, that symbol\n\
1738is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
1739OBARRAY defaults to the value of the variable `obarray'.")
1740 (name, obarray)
1741 Lisp_Object name, obarray;
1742{
1743 register Lisp_Object string, tem;
1744 int hash;
1745
1746 if (NILP (obarray)) obarray = Vobarray;
1747 obarray = check_obarray (obarray);
1748
1749 if (SYMBOLP (name))
1750 XSETSTRING (string, XSYMBOL (name)->name);
1751 else
1752 {
1753 CHECK_STRING (name, 0);
1754 string = name;
1755 }
1756
1757 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
1758 if (INTEGERP (tem))
1759 return Qnil;
1760 /* If arg was a symbol, don't delete anything but that symbol itself. */
1761 if (SYMBOLP (name) && !EQ (name, tem))
1762 return Qnil;
1763
1764 hash = oblookup_last_bucket_number;
1765
1766 if (EQ (XVECTOR (obarray)->contents[hash], tem))
1767 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
1768 else
1769 {
1770 Lisp_Object tail, following;
1771
1772 for (tail = XVECTOR (obarray)->contents[hash];
1773 XSYMBOL (tail)->next;
1774 tail = following)
1775 {
1776 XSETSYMBOL (following, XSYMBOL (tail)->next);
1777 if (EQ (following, tem))
1778 {
1779 XSYMBOL (tail)->next = XSYMBOL (following)->next;
1780 break;
1781 }
1782 }
1783 }
1784
1785 return Qt;
1786}
1787\f
1788/* Return the symbol in OBARRAY whose names matches the string
1789 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
1790 return nil.
1791
1792 Also store the bucket number in oblookup_last_bucket_number. */
078e7b4a
JB
1793
1794Lisp_Object
a142f21b 1795oblookup (obarray, ptr, size)
078e7b4a
JB
1796 Lisp_Object obarray;
1797 register char *ptr;
1798 register int size;
1799{
7a70b397
RS
1800 int hash;
1801 int obsize;
078e7b4a
JB
1802 register Lisp_Object tail;
1803 Lisp_Object bucket, tem;
1804
cfff016d 1805 if (!VECTORP (obarray)
7c79a684 1806 || (obsize = XVECTOR (obarray)->size) == 0)
078e7b4a
JB
1807 {
1808 obarray = check_obarray (obarray);
1809 obsize = XVECTOR (obarray)->size;
1810 }
1811 /* Combining next two lines breaks VMS C 2.3. */
1812 hash = hash_string (ptr, size);
1813 hash %= obsize;
1814 bucket = XVECTOR (obarray)->contents[hash];
d007f5c8 1815 oblookup_last_bucket_number = hash;
078e7b4a
JB
1816 if (XFASTINT (bucket) == 0)
1817 ;
cfff016d 1818 else if (!SYMBOLP (bucket))
078e7b4a 1819 error ("Bad data in guts of obarray"); /* Like CADR error message */
d007f5c8
RS
1820 else
1821 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
078e7b4a 1822 {
d007f5c8
RS
1823 if (XSYMBOL (tail)->name->size == size
1824 && !bcmp (XSYMBOL (tail)->name->data, ptr, size))
078e7b4a
JB
1825 return tail;
1826 else if (XSYMBOL (tail)->next == 0)
1827 break;
1828 }
1805de4f 1829 XSETINT (tem, hash);
078e7b4a
JB
1830 return tem;
1831}
1832
1833static int
1834hash_string (ptr, len)
1835 unsigned char *ptr;
1836 int len;
1837{
1838 register unsigned char *p = ptr;
1839 register unsigned char *end = p + len;
1840 register unsigned char c;
1841 register int hash = 0;
1842
1843 while (p != end)
1844 {
1845 c = *p++;
1846 if (c >= 0140) c -= 40;
1847 hash = ((hash<<3) + (hash>>28) + c);
1848 }
1849 return hash & 07777777777;
1850}
d007f5c8 1851\f
078e7b4a
JB
1852void
1853map_obarray (obarray, fn, arg)
1854 Lisp_Object obarray;
1855 int (*fn) ();
1856 Lisp_Object arg;
1857{
1858 register int i;
1859 register Lisp_Object tail;
1860 CHECK_VECTOR (obarray, 1);
1861 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
1862 {
1863 tail = XVECTOR (obarray)->contents[i];
1864 if (XFASTINT (tail) != 0)
1865 while (1)
1866 {
1867 (*fn) (tail, arg);
1868 if (XSYMBOL (tail)->next == 0)
1869 break;
1805de4f 1870 XSETSYMBOL (tail, XSYMBOL (tail)->next);
078e7b4a
JB
1871 }
1872 }
1873}
1874
1875mapatoms_1 (sym, function)
1876 Lisp_Object sym, function;
1877{
1878 call1 (function, sym);
1879}
1880
1881DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
1882 "Call FUNCTION on every symbol in OBARRAY.\n\
1883OBARRAY defaults to the value of `obarray'.")
1884 (function, obarray)
1885 Lisp_Object function, obarray;
1886{
1887 Lisp_Object tem;
1888
265a9e55 1889 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1890 obarray = check_obarray (obarray);
1891
1892 map_obarray (obarray, mapatoms_1, function);
1893 return Qnil;
1894}
1895
5e88a39e 1896#define OBARRAY_SIZE 1511
078e7b4a
JB
1897
1898void
1899init_obarray ()
1900{
1901 Lisp_Object oblength;
1902 int hash;
1903 Lisp_Object *tem;
1904
baf69866 1905 XSETFASTINT (oblength, OBARRAY_SIZE);
078e7b4a
JB
1906
1907 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
1908 Vobarray = Fmake_vector (oblength, make_number (0));
1909 initial_obarray = Vobarray;
1910 staticpro (&initial_obarray);
1911 /* Intern nil in the obarray */
1912 /* These locals are to kludge around a pyramid compiler bug. */
1913 hash = hash_string ("nil", 3);
1914 /* Separate statement here to avoid VAXC bug. */
1915 hash %= OBARRAY_SIZE;
1916 tem = &XVECTOR (Vobarray)->contents[hash];
1917 *tem = Qnil;
1918
1919 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
1920 XSYMBOL (Qnil)->function = Qunbound;
1921 XSYMBOL (Qunbound)->value = Qunbound;
1922 XSYMBOL (Qunbound)->function = Qunbound;
1923
1924 Qt = intern ("t");
1925 XSYMBOL (Qnil)->value = Qnil;
1926 XSYMBOL (Qnil)->plist = Qnil;
1927 XSYMBOL (Qt)->value = Qt;
1928
1929 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
1930 Vpurify_flag = Qt;
1931
1932 Qvariable_documentation = intern ("variable-documentation");
1933
1934 read_buffer_size = 100;
1935 read_buffer = (char *) malloc (read_buffer_size);
1936}
1937\f
1938void
1939defsubr (sname)
1940 struct Lisp_Subr *sname;
1941{
1942 Lisp_Object sym;
1943 sym = intern (sname->symbol_name);
1805de4f 1944 XSETSUBR (XSYMBOL (sym)->function, sname);
078e7b4a
JB
1945}
1946
1947#ifdef NOTDEF /* use fset in subr.el now */
1948void
1949defalias (sname, string)
1950 struct Lisp_Subr *sname;
1951 char *string;
1952{
1953 Lisp_Object sym;
1954 sym = intern (string);
1805de4f 1955 XSETSUBR (XSYMBOL (sym)->function, sname);
078e7b4a
JB
1956}
1957#endif /* NOTDEF */
1958
078e7b4a 1959/* Define an "integer variable"; a symbol whose value is forwarded
1a0f90f7 1960 to a C variable of type int. Sample call: */
950c215d 1961 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
078e7b4a 1962void
e9e00ff2 1963defvar_int (namestring, address)
078e7b4a
JB
1964 char *namestring;
1965 int *address;
078e7b4a 1966{
1a0f90f7 1967 Lisp_Object sym, val;
078e7b4a 1968 sym = intern (namestring);
1a0f90f7 1969 val = allocate_misc ();
47e28b2c 1970 XMISCTYPE (val) = Lisp_Misc_Intfwd;
fc1e7df5 1971 XINTFWD (val)->intvar = address;
1a0f90f7 1972 XSYMBOL (sym)->value = val;
078e7b4a
JB
1973}
1974
1975/* Similar but define a variable whose value is T if address contains 1,
1a0f90f7 1976 NIL if address contains 0 */
078e7b4a 1977void
e9e00ff2 1978defvar_bool (namestring, address)
078e7b4a
JB
1979 char *namestring;
1980 int *address;
078e7b4a 1981{
1a0f90f7 1982 Lisp_Object sym, val;
078e7b4a 1983 sym = intern (namestring);
1a0f90f7 1984 val = allocate_misc ();
47e28b2c 1985 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
fc1e7df5 1986 XBOOLFWD (val)->boolvar = address;
1a0f90f7 1987 XSYMBOL (sym)->value = val;
078e7b4a
JB
1988}
1989
1a0f90f7
KH
1990/* Similar but define a variable whose value is the Lisp Object stored
1991 at address. Two versions: with and without gc-marking of the C
1992 variable. The nopro version is used when that variable will be
1993 gc-marked for some other reason, since marking the same slot twice
1994 can cause trouble with strings. */
078e7b4a 1995void
1a0f90f7 1996defvar_lisp_nopro (namestring, address)
078e7b4a
JB
1997 char *namestring;
1998 Lisp_Object *address;
078e7b4a 1999{
1a0f90f7 2000 Lisp_Object sym, val;
078e7b4a 2001 sym = intern (namestring);
1a0f90f7 2002 val = allocate_misc ();
47e28b2c 2003 XMISCTYPE (val) = Lisp_Misc_Objfwd;
fc1e7df5 2004 XOBJFWD (val)->objvar = address;
1a0f90f7 2005 XSYMBOL (sym)->value = val;
078e7b4a
JB
2006}
2007
078e7b4a 2008void
1a0f90f7 2009defvar_lisp (namestring, address)
078e7b4a
JB
2010 char *namestring;
2011 Lisp_Object *address;
078e7b4a 2012{
1a0f90f7
KH
2013 defvar_lisp_nopro (namestring, address);
2014 staticpro (address);
078e7b4a
JB
2015}
2016
2017#ifndef standalone
2018
2019/* Similar but define a variable whose value is the Lisp Object stored in
2836d9a4
KH
2020 the current buffer. address is the address of the slot in the buffer
2021 that is current now. */
078e7b4a
JB
2022
2023void
4360b0c6 2024defvar_per_buffer (namestring, address, type, doc)
078e7b4a
JB
2025 char *namestring;
2026 Lisp_Object *address;
4360b0c6 2027 Lisp_Object type;
078e7b4a
JB
2028 char *doc;
2029{
1a0f90f7 2030 Lisp_Object sym, val;
078e7b4a
JB
2031 int offset;
2032 extern struct buffer buffer_local_symbols;
2033
2034 sym = intern (namestring);
1a0f90f7 2035 val = allocate_misc ();
078e7b4a
JB
2036 offset = (char *)address - (char *)current_buffer;
2037
47e28b2c 2038 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
fc1e7df5 2039 XBUFFER_OBJFWD (val)->offset = offset;
1a0f90f7 2040 XSYMBOL (sym)->value = val;
078e7b4a 2041 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
4360b0c6 2042 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
2836d9a4 2043 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
078e7b4a
JB
2044 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
2045 slot of buffer_local_flags */
2046 abort ();
2047}
2048
2049#endif /* standalone */
950c215d
KH
2050
2051/* Similar but define a variable whose value is the Lisp Object stored
4ac38690 2052 at a particular offset in the current kboard object. */
950c215d
KH
2053
2054void
4ac38690 2055defvar_kboard (namestring, offset)
950c215d
KH
2056 char *namestring;
2057 int offset;
2058{
2059 Lisp_Object sym, val;
2060 sym = intern (namestring);
2061 val = allocate_misc ();
47e28b2c 2062 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
4ac38690 2063 XKBOARD_OBJFWD (val)->offset = offset;
950c215d
KH
2064 XSYMBOL (sym)->value = val;
2065}
078e7b4a 2066\f
279499f0 2067init_lread ()
078e7b4a 2068{
46947372 2069 char *normal;
e73997a1 2070 int turn_off_warning = 0;
078e7b4a 2071
279499f0 2072 /* Compute the default load-path. */
46947372
JB
2073#ifdef CANNOT_DUMP
2074 normal = PATH_LOADSEARCH;
e065a56e 2075 Vload_path = decode_env_path (0, normal);
46947372
JB
2076#else
2077 if (NILP (Vpurify_flag))
2078 normal = PATH_LOADSEARCH;
279499f0 2079 else
46947372 2080 normal = PATH_DUMPLOADSEARCH;
279499f0 2081
46947372
JB
2082 /* In a dumped Emacs, we normally have to reset the value of
2083 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2084 uses ../lisp, instead of the path of the installed elisp
2085 libraries. However, if it appears that Vload_path was changed
2086 from the default before dumping, don't override that value. */
4746118a
JB
2087 if (initialized)
2088 {
2089 Lisp_Object dump_path;
46947372 2090
e065a56e 2091 dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH);
4746118a 2092 if (! NILP (Fequal (dump_path, Vload_path)))
80667d53
RS
2093 {
2094 Vload_path = decode_env_path (0, normal);
74180aa4 2095 if (!NILP (Vinstallation_directory))
80667d53 2096 {
74180aa4 2097 /* Add to the path the lisp subdir of the
3a3056e5
RS
2098 installation dir, if it exists. */
2099 Lisp_Object tem, tem1;
74180aa4
RS
2100 tem = Fexpand_file_name (build_string ("lisp"),
2101 Vinstallation_directory);
3a3056e5
RS
2102 tem1 = Ffile_exists_p (tem);
2103 if (!NILP (tem1))
2104 {
2105 if (NILP (Fmember (tem, Vload_path)))
e73997a1
RS
2106 {
2107 turn_off_warning = 1;
2108 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2109 }
3a3056e5
RS
2110 }
2111 else
2112 /* That dir doesn't exist, so add the build-time
2113 Lisp dirs instead. */
2114 Vload_path = nconc2 (Vload_path, dump_path);
c478f98c
RS
2115
2116 /* Add site-list under the installation dir, if it exists. */
2117 tem = Fexpand_file_name (build_string ("site-lisp"),
2118 Vinstallation_directory);
2119 tem1 = Ffile_exists_p (tem);
2120 if (!NILP (tem1))
2121 {
2122 if (NILP (Fmember (tem, Vload_path)))
2123 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2124 }
80667d53
RS
2125 }
2126 }
4746118a
JB
2127 }
2128 else
e065a56e 2129 Vload_path = decode_env_path (0, normal);
46947372 2130#endif
279499f0 2131
317073d5
RS
2132#ifndef WINDOWSNT
2133 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2134 almost never correct, thereby causing a warning to be printed out that
2135 confuses users. Since PATH_LOADSEARCH is always overriden by the
2136 EMACSLOADPATH environment variable below, disable the warning on NT. */
2137
078e7b4a 2138 /* Warn if dirs in the *standard* path don't exist. */
e73997a1
RS
2139 if (!turn_off_warning)
2140 {
2141 Lisp_Object path_tail;
078e7b4a 2142
e73997a1
RS
2143 for (path_tail = Vload_path;
2144 !NILP (path_tail);
2145 path_tail = XCONS (path_tail)->cdr)
2146 {
2147 Lisp_Object dirfile;
2148 dirfile = Fcar (path_tail);
2149 if (STRINGP (dirfile))
2150 {
2151 dirfile = Fdirectory_file_name (dirfile);
2152 if (access (XSTRING (dirfile)->data, 0) < 0)
2153 fprintf (stderr,
2154 "Warning: Lisp directory `%s' does not exist.\n",
2155 XSTRING (Fcar (path_tail))->data);
2156 }
2157 }
2158 }
317073d5 2159#endif /* WINDOWSNT */
46947372
JB
2160
2161 /* If the EMACSLOADPATH environment variable is set, use its value.
2162 This doesn't apply if we're dumping. */
ffd9c2a1 2163#ifndef CANNOT_DUMP
46947372
JB
2164 if (NILP (Vpurify_flag)
2165 && egetenv ("EMACSLOADPATH"))
ffd9c2a1 2166#endif
279499f0 2167 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
279499f0
JB
2168
2169 Vvalues = Qnil;
2170
078e7b4a 2171 load_in_progress = 0;
d2c6be7f
RS
2172
2173 load_descriptor_list = Qnil;
078e7b4a
JB
2174}
2175
2176void
279499f0 2177syms_of_lread ()
078e7b4a
JB
2178{
2179 defsubr (&Sread);
2180 defsubr (&Sread_from_string);
2181 defsubr (&Sintern);
2182 defsubr (&Sintern_soft);
d007f5c8 2183 defsubr (&Sunintern);
078e7b4a 2184 defsubr (&Sload);
228d4b1c 2185 defsubr (&Seval_buffer);
078e7b4a
JB
2186 defsubr (&Seval_region);
2187 defsubr (&Sread_char);
2188 defsubr (&Sread_char_exclusive);
078e7b4a 2189 defsubr (&Sread_event);
078e7b4a
JB
2190 defsubr (&Sget_file_char);
2191 defsubr (&Smapatoms);
2192
2193 DEFVAR_LISP ("obarray", &Vobarray,
2194 "Symbol table for use by `intern' and `read'.\n\
2195It is a vector whose length ought to be prime for best results.\n\
2196The vector's contents don't make sense if examined from Lisp programs;\n\
2197to find all the symbols in an obarray, use `mapatoms'.");
2198
2199 DEFVAR_LISP ("values", &Vvalues,
2200 "List of values of all expressions which were read, evaluated and printed.\n\
2201Order is reverse chronological.");
2202
2203 DEFVAR_LISP ("standard-input", &Vstandard_input,
2204 "Stream for read to get input from.\n\
2205See documentation of `read' for possible values.");
2206 Vstandard_input = Qt;
2207
2208 DEFVAR_LISP ("load-path", &Vload_path,
2209 "*List of directories to search for files to load.\n\
2210Each element is a string (directory name) or nil (try default directory).\n\
2211Initialized based on EMACSLOADPATH environment variable, if any,\n\
692f86ad 2212otherwise to default specified by file `paths.h' when Emacs was built.");
078e7b4a
JB
2213
2214 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
2215 "Non-nil iff inside of `load'.");
2216
2217 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
2218 "An alist of expressions to be evalled when particular files are loaded.\n\
2219Each element looks like (FILENAME FORMS...).\n\
2220When `load' is run and the file-name argument is FILENAME,\n\
2221the FORMS in the corresponding element are executed at the end of loading.\n\n\
2222FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2223with no directory specified, since that is how `load' is normally called.\n\
2224An error in FORMS does not undo the load,\n\
2225but does prevent execution of the rest of the FORMS.");
2226 Vafter_load_alist = Qnil;
2227
ae321d28
RS
2228 DEFVAR_LISP ("load-history", &Vload_history,
2229 "Alist mapping source file names to symbols and features.\n\
2230Each alist element is a list that starts with a file name,\n\
2231except for one element (optional) that starts with nil and describes\n\
2232definitions evaluated from buffers not visiting files.\n\
2233The remaining elements of each list are symbols defined as functions\n\
2234or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2235 Vload_history = Qnil;
2236
20ea2964
RS
2237 DEFVAR_LISP ("load-file-name", &Vload_file_name,
2238 "Full name of file being loaded by `load'.");
2239 Vload_file_name = Qnil;
2240
8a1f1537
RS
2241 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
2242 "Used for internal purposes by `load'.");
ae321d28
RS
2243 Vcurrent_load_list = Qnil;
2244
84a15045
RS
2245 DEFVAR_LISP ("load-read-function", &Vload_read_function,
2246 "Function used by `load' and `eval-region' for reading expressions.\n\
2247The default is nil, which means use the function `read'.");
2248 Vload_read_function = Qnil;
2249
d2c6be7f
RS
2250 load_descriptor_list = Qnil;
2251 staticpro (&load_descriptor_list);
2252
8a1f1537
RS
2253 Qcurrent_load_list = intern ("current-load-list");
2254 staticpro (&Qcurrent_load_list);
2255
078e7b4a
JB
2256 Qstandard_input = intern ("standard-input");
2257 staticpro (&Qstandard_input);
2258
2259 Qread_char = intern ("read-char");
2260 staticpro (&Qread_char);
2261
2262 Qget_file_char = intern ("get-file-char");
2263 staticpro (&Qget_file_char);
7bd279cd 2264
17634846
RS
2265 Qbackquote = intern ("`");
2266 staticpro (&Qbackquote);
2267 Qcomma = intern (",");
2268 staticpro (&Qcomma);
2269 Qcomma_at = intern (",@");
2270 staticpro (&Qcomma_at);
2271 Qcomma_dot = intern (",.");
2272 staticpro (&Qcomma_dot);
2273
7bd279cd
RS
2274 Qascii_character = intern ("ascii-character");
2275 staticpro (&Qascii_character);
c2225d00
RS
2276
2277 Qload = intern ("load");
2278 staticpro (&Qload);
20ea2964
RS
2279
2280 Qload_file_name = intern ("load-file-name");
2281 staticpro (&Qload_file_name);
078e7b4a 2282}