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