(font-lock-keywords): Fix doc for multiline matches.
[emacs.git] / src / lread.c
blob4f384c809a8882ed4438ec3e071d4ccc868b7e94
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #define _XOPEN_SOURCE 500 /* for Unix 98 ftello on GNU */
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29 #include <errno.h>
30 #include "lisp.h"
31 #include "intervals.h"
33 #ifndef standalone
34 #include "buffer.h"
35 #include "charset.h"
36 #include <epaths.h>
37 #include "commands.h"
38 #include "keyboard.h"
39 #include "termhooks.h"
40 #endif
42 #ifdef lint
43 #include <sys/inode.h>
44 #endif /* lint */
46 #ifdef MSDOS
47 #if __DJGPP__ < 2
48 #include <unistd.h> /* to get X_OK */
49 #endif
50 #include "msdos.h"
51 #endif
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
57 #ifndef X_OK
58 #define X_OK 01
59 #endif
61 #ifdef LISP_FLOAT_TYPE
62 #include <math.h>
63 #endif /* LISP_FLOAT_TYPE */
65 #ifdef HAVE_SETLOCALE
66 #include <locale.h>
67 #endif /* HAVE_SETLOCALE */
69 #ifndef O_RDONLY
70 #define O_RDONLY 0
71 #endif
73 #ifdef HAVE_FTELLO
74 #define file_offset off_t
75 #define file_tell ftello
76 #else
77 #define file_offset long
78 #define file_tell ftell
79 #endif
81 extern int errno;
83 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
84 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
85 Lisp_Object Qascii_character, Qload, Qload_file_name;
86 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
87 Lisp_Object Qinhibit_file_name_operation;
89 extern Lisp_Object Qevent_symbol_element_mask;
90 extern Lisp_Object Qfile_exists_p;
92 /* non-zero if inside `load' */
93 int load_in_progress;
95 /* Directory in which the sources were found. */
96 Lisp_Object Vsource_directory;
98 /* Search path for files to be loaded. */
99 Lisp_Object Vload_path;
101 /* File name of user's init file. */
102 Lisp_Object Vuser_init_file;
104 /* This is the user-visible association list that maps features to
105 lists of defs in their load files. */
106 Lisp_Object Vload_history;
108 /* This is used to build the load history. */
109 Lisp_Object Vcurrent_load_list;
111 /* List of files that were preloaded. */
112 Lisp_Object Vpreloaded_file_list;
114 /* Name of file actually being read by `load'. */
115 Lisp_Object Vload_file_name;
117 /* Function to use for reading, in `load' and friends. */
118 Lisp_Object Vload_read_function;
120 /* The association list of objects read with the #n=object form.
121 Each member of the list has the form (n . object), and is used to
122 look up the object for the corresponding #n# construct.
123 It must be set to nil before all top-level calls to read0. */
124 Lisp_Object read_objects;
126 /* Nonzero means load should forcibly load all dynamic doc strings. */
127 static int load_force_doc_strings;
129 /* Nonzero means read should convert strings to unibyte. */
130 static int load_convert_to_unibyte;
132 /* Function to use for loading an Emacs lisp source file (not
133 compiled) instead of readevalloop. */
134 Lisp_Object Vload_source_file_function;
136 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
137 Lisp_Object Vbyte_boolean_vars;
139 /* List of descriptors now open for Fload. */
140 static Lisp_Object load_descriptor_list;
142 /* File for get_file_char to read from. Use by load. */
143 static FILE *instream;
145 /* When nonzero, read conses in pure space */
146 static int read_pure;
148 /* For use within read-from-string (this reader is non-reentrant!!) */
149 static int read_from_string_index;
150 static int read_from_string_index_byte;
151 static int read_from_string_limit;
153 /* Number of bytes left to read in the buffer character
154 that `readchar' has already advanced over. */
155 static int readchar_backlog;
157 /* This contains the last string skipped with #@. */
158 static char *saved_doc_string;
159 /* Length of buffer allocated in saved_doc_string. */
160 static int saved_doc_string_size;
161 /* Length of actual data in saved_doc_string. */
162 static int saved_doc_string_length;
163 /* This is the file position that string came from. */
164 static file_offset saved_doc_string_position;
166 /* This contains the previous string skipped with #@.
167 We copy it from saved_doc_string when a new string
168 is put in saved_doc_string. */
169 static char *prev_saved_doc_string;
170 /* Length of buffer allocated in prev_saved_doc_string. */
171 static int prev_saved_doc_string_size;
172 /* Length of actual data in prev_saved_doc_string. */
173 static int prev_saved_doc_string_length;
174 /* This is the file position that string came from. */
175 static file_offset prev_saved_doc_string_position;
177 /* Nonzero means inside a new-style backquote
178 with no surrounding parentheses.
179 Fread initializes this to zero, so we need not specbind it
180 or worry about what happens to it when there is an error. */
181 static int new_backquote_flag;
183 /* Handle unreading and rereading of characters.
184 Write READCHAR to read a character,
185 UNREAD(c) to unread c to be read again.
187 These macros actually read/unread a byte code, multibyte characters
188 are not handled here. The caller should manage them if necessary.
191 #define READCHAR readchar (readcharfun)
192 #define UNREAD(c) unreadchar (readcharfun, c)
194 static int
195 readchar (readcharfun)
196 Lisp_Object readcharfun;
198 Lisp_Object tem;
199 register int c;
201 if (BUFFERP (readcharfun))
203 register struct buffer *inbuffer = XBUFFER (readcharfun);
205 int pt_byte = BUF_PT_BYTE (inbuffer);
206 int orig_pt_byte = pt_byte;
208 if (readchar_backlog > 0)
209 /* We get the address of the byte just passed,
210 which is the last byte of the character.
211 The other bytes in this character are consecutive with it,
212 because the gap can't be in the middle of a character. */
213 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
214 - --readchar_backlog);
216 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
217 return -1;
219 readchar_backlog = -1;
221 if (! NILP (inbuffer->enable_multibyte_characters))
223 /* Fetch the character code from the buffer. */
224 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
225 BUF_INC_POS (inbuffer, pt_byte);
226 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
228 else
230 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
231 pt_byte++;
233 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
235 return c;
237 if (MARKERP (readcharfun))
239 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
241 int bytepos = marker_byte_position (readcharfun);
242 int orig_bytepos = bytepos;
244 if (readchar_backlog > 0)
245 /* We get the address of the byte just passed,
246 which is the last byte of the character.
247 The other bytes in this character are consecutive with it,
248 because the gap can't be in the middle of a character. */
249 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
250 - --readchar_backlog);
252 if (bytepos >= BUF_ZV_BYTE (inbuffer))
253 return -1;
255 readchar_backlog = -1;
257 if (! NILP (inbuffer->enable_multibyte_characters))
259 /* Fetch the character code from the buffer. */
260 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
261 BUF_INC_POS (inbuffer, bytepos);
262 c = STRING_CHAR (p, bytepos - orig_bytepos);
264 else
266 c = BUF_FETCH_BYTE (inbuffer, bytepos);
267 bytepos++;
270 XMARKER (readcharfun)->bytepos = bytepos;
271 XMARKER (readcharfun)->charpos++;
273 return c;
276 if (EQ (readcharfun, Qlambda))
277 return read_bytecode_char (0);
279 if (EQ (readcharfun, Qget_file_char))
281 c = getc (instream);
282 #ifdef EINTR
283 /* Interrupted reads have been observed while reading over the network */
284 while (c == EOF && ferror (instream) && errno == EINTR)
286 clearerr (instream);
287 c = getc (instream);
289 #endif
290 return c;
293 if (STRINGP (readcharfun))
295 if (read_from_string_index >= read_from_string_limit)
296 c = -1;
297 else if (STRING_MULTIBYTE (readcharfun))
298 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
299 read_from_string_index,
300 read_from_string_index_byte);
301 else
302 c = XSTRING (readcharfun)->data[read_from_string_index++];
304 return c;
307 tem = call0 (readcharfun);
309 if (NILP (tem))
310 return -1;
311 return XINT (tem);
314 /* Unread the character C in the way appropriate for the stream READCHARFUN.
315 If the stream is a user function, call it with the char as argument. */
317 static void
318 unreadchar (readcharfun, c)
319 Lisp_Object readcharfun;
320 int c;
322 if (c == -1)
323 /* Don't back up the pointer if we're unreading the end-of-input mark,
324 since readchar didn't advance it when we read it. */
326 else if (BUFFERP (readcharfun))
328 struct buffer *b = XBUFFER (readcharfun);
329 int bytepos = BUF_PT_BYTE (b);
331 if (readchar_backlog >= 0)
332 readchar_backlog++;
333 else
335 BUF_PT (b)--;
336 if (! NILP (b->enable_multibyte_characters))
337 BUF_DEC_POS (b, bytepos);
338 else
339 bytepos--;
341 BUF_PT_BYTE (b) = bytepos;
344 else if (MARKERP (readcharfun))
346 struct buffer *b = XMARKER (readcharfun)->buffer;
347 int bytepos = XMARKER (readcharfun)->bytepos;
349 if (readchar_backlog >= 0)
350 readchar_backlog++;
351 else
353 XMARKER (readcharfun)->charpos--;
354 if (! NILP (b->enable_multibyte_characters))
355 BUF_DEC_POS (b, bytepos);
356 else
357 bytepos--;
359 XMARKER (readcharfun)->bytepos = bytepos;
362 else if (STRINGP (readcharfun))
364 read_from_string_index--;
365 read_from_string_index_byte
366 = string_char_to_byte (readcharfun, read_from_string_index);
368 else if (EQ (readcharfun, Qlambda))
369 read_bytecode_char (1);
370 else if (EQ (readcharfun, Qget_file_char))
371 ungetc (c, instream);
372 else
373 call1 (readcharfun, make_number (c));
376 static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
377 static int read_multibyte ();
378 static Lisp_Object substitute_object_recurse ();
379 static void substitute_object_in_subtree (), substitute_in_interval ();
382 /* Get a character from the tty. */
384 extern Lisp_Object read_char ();
386 /* Read input events until we get one that's acceptable for our purposes.
388 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
389 until we get a character we like, and then stuffed into
390 unread_switch_frame.
392 If ASCII_REQUIRED is non-zero, we check function key events to see
393 if the unmodified version of the symbol has a Qascii_character
394 property, and use that character, if present.
396 If ERROR_NONASCII is non-zero, we signal an error if the input we
397 get isn't an ASCII character with modifiers. If it's zero but
398 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
399 character.
401 If INPUT_METHOD is nonzero, we invoke the current input method
402 if the character warrants that. */
404 Lisp_Object
405 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
406 input_method)
407 int no_switch_frame, ascii_required, error_nonascii, input_method;
409 #ifdef standalone
410 return make_number (getchar ());
411 #else
412 register Lisp_Object val, delayed_switch_frame;
414 delayed_switch_frame = Qnil;
416 /* Read until we get an acceptable event. */
417 retry:
418 val = read_char (0, 0, 0,
419 (input_method ? Qnil : Qt),
422 if (BUFFERP (val))
423 goto retry;
425 /* switch-frame events are put off until after the next ASCII
426 character. This is better than signaling an error just because
427 the last characters were typed to a separate minibuffer frame,
428 for example. Eventually, some code which can deal with
429 switch-frame events will read it and process it. */
430 if (no_switch_frame
431 && EVENT_HAS_PARAMETERS (val)
432 && EQ (EVENT_HEAD (val), Qswitch_frame))
434 delayed_switch_frame = val;
435 goto retry;
438 if (ascii_required)
440 /* Convert certain symbols to their ASCII equivalents. */
441 if (SYMBOLP (val))
443 Lisp_Object tem, tem1;
444 tem = Fget (val, Qevent_symbol_element_mask);
445 if (!NILP (tem))
447 tem1 = Fget (Fcar (tem), Qascii_character);
448 /* Merge this symbol's modifier bits
449 with the ASCII equivalent of its basic code. */
450 if (!NILP (tem1))
451 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
455 /* If we don't have a character now, deal with it appropriately. */
456 if (!INTEGERP (val))
458 if (error_nonascii)
460 Vunread_command_events = Fcons (val, Qnil);
461 error ("Non-character input-event");
463 else
464 goto retry;
468 if (! NILP (delayed_switch_frame))
469 unread_switch_frame = delayed_switch_frame;
471 return val;
472 #endif
475 DEFUN ("read-char", Fread_char, Sread_char, 0, 2, 0,
476 "Read a character from the command input (keyboard or macro).\n\
477 It is returned as a number.\n\
478 If the user generates an event which is not a character (i.e. a mouse\n\
479 click or function key event), `read-char' signals an error. As an\n\
480 exception, switch-frame events are put off until non-ASCII events can\n\
481 be read.\n\
482 If you want to read non-character events, or ignore them, call\n\
483 `read-event' or `read-char-exclusive' instead.\n\
485 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
486 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
487 input method is turned on in the current buffer, that input method\n\
488 is used for reading a character.")
489 (prompt, inherit_input_method)
490 Lisp_Object prompt, inherit_input_method;
492 if (! NILP (prompt))
493 message_with_string ("%s", prompt, 0);
494 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method));
497 DEFUN ("read-event", Fread_event, Sread_event, 0, 2, 0,
498 "Read an event object from the input stream.\n\
499 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
500 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
501 input method is turned on in the current buffer, that input method\n\
502 is used for reading a character.")
503 (prompt, inherit_input_method)
504 Lisp_Object prompt, inherit_input_method;
506 if (! NILP (prompt))
507 message_with_string ("%s", prompt, 0);
508 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method));
511 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 2, 0,
512 "Read a character from the command input (keyboard or macro).\n\
513 It is returned as a number. Non-character events are ignored.\n\
515 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
516 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
517 input method is turned on in the current buffer, that input method\n\
518 is used for reading a character.")
519 (prompt, inherit_input_method)
520 Lisp_Object prompt, inherit_input_method;
522 if (! NILP (prompt))
523 message_with_string ("%s", prompt, 0);
524 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method));
527 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
528 "Don't use this yourself.")
531 register Lisp_Object val;
532 XSETINT (val, getc (instream));
533 return val;
536 static void readevalloop ();
537 static Lisp_Object load_unwind ();
538 static Lisp_Object load_descriptor_unwind ();
540 DEFUN ("load", Fload, Sload, 1, 5, 0,
541 "Execute a file of Lisp code named FILE.\n\
542 First try FILE with `.elc' appended, then try with `.el',\n\
543 then try FILE unmodified.\n\
544 This function searches the directories in `load-path'.\n\
545 If optional second arg NOERROR is non-nil,\n\
546 report no error if FILE doesn't exist.\n\
547 Print messages at start and end of loading unless\n\
548 optional third arg NOMESSAGE is non-nil.\n\
549 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
550 suffixes `.elc' or `.el' to the specified name FILE.\n\
551 If optional fifth arg MUST-SUFFIX is non-nil, insist on\n\
552 the suffix `.elc' or `.el'; don't accept just FILE unless\n\
553 it ends in one of those suffixes or includes a directory name.\n\
554 Return t if file exists.")
555 (file, noerror, nomessage, nosuffix, must_suffix)
556 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
558 register FILE *stream;
559 register int fd = -1;
560 register Lisp_Object lispstream;
561 int count = specpdl_ptr - specpdl;
562 Lisp_Object temp;
563 struct gcpro gcpro1;
564 Lisp_Object found;
565 /* 1 means we printed the ".el is newer" message. */
566 int newer = 0;
567 /* 1 means we are loading a compiled file. */
568 int compiled = 0;
569 Lisp_Object handler;
570 char *fmode = "r";
571 #ifdef DOS_NT
572 fmode = "rt";
573 #endif /* DOS_NT */
575 CHECK_STRING (file, 0);
577 /* If file name is magic, call the handler. */
578 handler = Ffind_file_name_handler (file, Qload);
579 if (!NILP (handler))
580 return call5 (handler, Qload, file, noerror, nomessage, nosuffix);
582 /* Do this after the handler to avoid
583 the need to gcpro noerror, nomessage and nosuffix.
584 (Below here, we care only whether they are nil or not.) */
585 file = Fsubstitute_in_file_name (file);
587 /* Avoid weird lossage with null string as arg,
588 since it would try to load a directory as a Lisp file */
589 if (XSTRING (file)->size > 0)
591 int size = STRING_BYTES (XSTRING (file));
593 GCPRO1 (file);
595 if (! NILP (must_suffix))
597 /* Don't insist on adding a suffix if FILE already ends with one. */
598 if (size > 3
599 && !strcmp (XSTRING (file)->data + size - 3, ".el"))
600 must_suffix = Qnil;
601 else if (size > 4
602 && !strcmp (XSTRING (file)->data + size - 4, ".elc"))
603 must_suffix = Qnil;
604 /* Don't insist on adding a suffix
605 if the argument includes a directory name. */
606 else if (! NILP (Ffile_name_directory (file)))
607 must_suffix = Qnil;
610 fd = openp (Vload_path, file,
611 (!NILP (nosuffix) ? ""
612 : ! NILP (must_suffix) ? ".elc.gz:.elc:.el.gz:.el"
613 : ".elc:.elc.gz:.el.gz:.el:"),
614 &found, 0);
615 UNGCPRO;
618 if (fd < 0)
620 if (NILP (noerror))
621 while (1)
622 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
623 Fcons (file, Qnil)));
624 else
625 return Qnil;
628 if (EQ (Qt, Vuser_init_file))
629 Vuser_init_file = found;
631 /* If FD is 0, that means openp found a magic file. */
632 if (fd == 0)
634 if (NILP (Fequal (found, file)))
635 /* If FOUND is a different file name from FILE,
636 find its handler even if we have already inhibited
637 the `load' operation on FILE. */
638 handler = Ffind_file_name_handler (found, Qt);
639 else
640 handler = Ffind_file_name_handler (found, Qload);
641 if (! NILP (handler))
642 return call5 (handler, Qload, found, noerror, nomessage, Qt);
645 /* Load .elc files directly, but not when they are
646 remote and have no handler! */
647 if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
648 ".elc", 4)
649 && fd != 0)
651 struct stat s1, s2;
652 int result;
654 compiled = 1;
656 #ifdef DOS_NT
657 fmode = "rb";
658 #endif /* DOS_NT */
659 stat ((char *)XSTRING (found)->data, &s1);
660 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
661 result = stat ((char *)XSTRING (found)->data, &s2);
662 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
664 /* Make the progress messages mention that source is newer. */
665 newer = 1;
667 /* If we won't print another message, mention this anyway. */
668 if (! NILP (nomessage))
669 message_with_string ("Source file `%s' newer than byte-compiled file",
670 found, 1);
672 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
674 else
676 /* We are loading a source file (*.el). */
677 if (!NILP (Vload_source_file_function))
679 if (fd != 0)
680 emacs_close (fd);
681 return call4 (Vload_source_file_function, found, file,
682 NILP (noerror) ? Qnil : Qt,
683 NILP (nomessage) ? Qnil : Qt);
687 #ifdef WINDOWSNT
688 emacs_close (fd);
689 stream = fopen ((char *) XSTRING (found)->data, fmode);
690 #else /* not WINDOWSNT */
691 stream = fdopen (fd, fmode);
692 #endif /* not WINDOWSNT */
693 if (stream == 0)
695 emacs_close (fd);
696 error ("Failure to create stdio stream for %s", XSTRING (file)->data);
699 if (! NILP (Vpurify_flag))
700 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
702 if (NILP (nomessage))
704 if (!compiled)
705 message_with_string ("Loading %s (source)...", file, 1);
706 else if (newer)
707 message_with_string ("Loading %s (compiled; note, source file is newer)...",
708 file, 1);
709 else /* The typical case; compiled file newer than source file. */
710 message_with_string ("Loading %s...", file, 1);
713 GCPRO1 (file);
714 lispstream = Fcons (Qnil, Qnil);
715 XSETFASTINT (XCAR (lispstream), (EMACS_UINT)stream >> 16);
716 XSETFASTINT (XCDR (lispstream), (EMACS_UINT)stream & 0xffff);
717 record_unwind_protect (load_unwind, lispstream);
718 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
719 specbind (Qload_file_name, found);
720 specbind (Qinhibit_file_name_operation, Qnil);
721 load_descriptor_list
722 = Fcons (make_number (fileno (stream)), load_descriptor_list);
723 load_in_progress++;
724 readevalloop (Qget_file_char, stream, file, Feval, 0, Qnil, Qnil);
725 unbind_to (count, Qnil);
727 /* Run any load-hooks for this file. */
728 temp = Fassoc (file, Vafter_load_alist);
729 if (!NILP (temp))
730 Fprogn (Fcdr (temp));
731 UNGCPRO;
733 if (saved_doc_string)
734 free (saved_doc_string);
735 saved_doc_string = 0;
736 saved_doc_string_size = 0;
738 if (prev_saved_doc_string)
739 free (prev_saved_doc_string);
740 prev_saved_doc_string = 0;
741 prev_saved_doc_string_size = 0;
743 if (!noninteractive && NILP (nomessage))
745 if (!compiled)
746 message_with_string ("Loading %s (source)...done", file, 1);
747 else if (newer)
748 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
749 file, 1);
750 else /* The typical case; compiled file newer than source file. */
751 message_with_string ("Loading %s...done", file, 1);
753 return Qt;
756 static Lisp_Object
757 load_unwind (stream) /* used as unwind-protect function in load */
758 Lisp_Object stream;
760 fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16
761 | XFASTINT (XCDR (stream))));
762 if (--load_in_progress < 0) load_in_progress = 0;
763 return Qnil;
766 static Lisp_Object
767 load_descriptor_unwind (oldlist)
768 Lisp_Object oldlist;
770 load_descriptor_list = oldlist;
771 return Qnil;
774 /* Close all descriptors in use for Floads.
775 This is used when starting a subprocess. */
777 void
778 close_load_descs ()
780 #ifndef WINDOWSNT
781 Lisp_Object tail;
782 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
783 emacs_close (XFASTINT (XCAR (tail)));
784 #endif
787 static int
788 complete_filename_p (pathname)
789 Lisp_Object pathname;
791 register unsigned char *s = XSTRING (pathname)->data;
792 return (IS_DIRECTORY_SEP (s[0])
793 || (XSTRING (pathname)->size > 2
794 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
795 #ifdef ALTOS
796 || *s == '@'
797 #endif
798 #ifdef VMS
799 || index (s, ':')
800 #endif /* VMS */
804 /* Search for a file whose name is STR, looking in directories
805 in the Lisp list PATH, and trying suffixes from SUFFIX.
806 SUFFIX is a string containing possible suffixes separated by colons.
807 On success, returns a file descriptor. On failure, returns -1.
809 EXEC_ONLY nonzero means don't open the files,
810 just look for one that is executable. In this case,
811 returns 1 on success.
813 If STOREPTR is nonzero, it points to a slot where the name of
814 the file actually found should be stored as a Lisp string.
815 nil is stored there on failure.
817 If the file we find is remote, return 0
818 but store the found remote file name in *STOREPTR.
819 We do not check for remote files if EXEC_ONLY is nonzero. */
822 openp (path, str, suffix, storeptr, exec_only)
823 Lisp_Object path, str;
824 char *suffix;
825 Lisp_Object *storeptr;
826 int exec_only;
828 register int fd;
829 int fn_size = 100;
830 char buf[100];
831 register char *fn = buf;
832 int absolute = 0;
833 int want_size;
834 Lisp_Object filename;
835 struct stat st;
836 struct gcpro gcpro1;
838 GCPRO1 (str);
839 if (storeptr)
840 *storeptr = Qnil;
842 if (complete_filename_p (str))
843 absolute = 1;
845 for (; !NILP (path); path = Fcdr (path))
847 char *nsuffix;
849 filename = Fexpand_file_name (str, Fcar (path));
850 if (!complete_filename_p (filename))
851 /* If there are non-absolute elts in PATH (eg ".") */
852 /* Of course, this could conceivably lose if luser sets
853 default-directory to be something non-absolute... */
855 filename = Fexpand_file_name (filename, current_buffer->directory);
856 if (!complete_filename_p (filename))
857 /* Give up on this path element! */
858 continue;
861 /* Calculate maximum size of any filename made from
862 this path element/specified file name and any possible suffix. */
863 want_size = strlen (suffix) + STRING_BYTES (XSTRING (filename)) + 1;
864 if (fn_size < want_size)
865 fn = (char *) alloca (fn_size = 100 + want_size);
867 nsuffix = suffix;
869 /* Loop over suffixes. */
870 while (1)
872 char *esuffix = (char *) index (nsuffix, ':');
873 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
874 Lisp_Object handler;
876 /* Concatenate path element/specified name with the suffix.
877 If the directory starts with /:, remove that. */
878 if (XSTRING (filename)->size > 2
879 && XSTRING (filename)->data[0] == '/'
880 && XSTRING (filename)->data[1] == ':')
882 strncpy (fn, XSTRING (filename)->data + 2,
883 STRING_BYTES (XSTRING (filename)) - 2);
884 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
886 else
888 strncpy (fn, XSTRING (filename)->data,
889 STRING_BYTES (XSTRING (filename)));
890 fn[STRING_BYTES (XSTRING (filename))] = 0;
893 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
894 strncat (fn, nsuffix, lsuffix);
896 /* Check that the file exists and is not a directory. */
897 if (absolute)
898 handler = Qnil;
899 else
900 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
901 if (! NILP (handler) && ! exec_only)
903 Lisp_Object string;
904 int exists;
906 string = build_string (fn);
907 exists = ! NILP (exec_only ? Ffile_executable_p (string)
908 : Ffile_readable_p (string));
909 if (exists
910 && ! NILP (Ffile_directory_p (build_string (fn))))
911 exists = 0;
913 if (exists)
915 /* We succeeded; return this descriptor and filename. */
916 if (storeptr)
917 *storeptr = build_string (fn);
918 UNGCPRO;
919 return 0;
922 else
924 int exists = (stat (fn, &st) >= 0
925 && (st.st_mode & S_IFMT) != S_IFDIR);
926 if (exists)
928 /* Check that we can access or open it. */
929 if (exec_only)
930 fd = (access (fn, X_OK) == 0) ? 1 : -1;
931 else
932 fd = emacs_open (fn, O_RDONLY, 0);
934 if (fd >= 0)
936 /* We succeeded; return this descriptor and filename. */
937 if (storeptr)
938 *storeptr = build_string (fn);
939 UNGCPRO;
940 return fd;
945 /* Advance to next suffix. */
946 if (esuffix == 0)
947 break;
948 nsuffix += lsuffix + 1;
950 if (absolute)
951 break;
954 UNGCPRO;
955 return -1;
959 /* Merge the list we've accumulated of globals from the current input source
960 into the load_history variable. The details depend on whether
961 the source has an associated file name or not. */
963 static void
964 build_load_history (stream, source)
965 FILE *stream;
966 Lisp_Object source;
968 register Lisp_Object tail, prev, newelt;
969 register Lisp_Object tem, tem2;
970 register int foundit, loading;
972 loading = stream || !NARROWED;
974 tail = Vload_history;
975 prev = Qnil;
976 foundit = 0;
977 while (!NILP (tail))
979 tem = Fcar (tail);
981 /* Find the feature's previous assoc list... */
982 if (!NILP (Fequal (source, Fcar (tem))))
984 foundit = 1;
986 /* If we're loading, remove it. */
987 if (loading)
989 if (NILP (prev))
990 Vload_history = Fcdr (tail);
991 else
992 Fsetcdr (prev, Fcdr (tail));
995 /* Otherwise, cons on new symbols that are not already members. */
996 else
998 tem2 = Vcurrent_load_list;
1000 while (CONSP (tem2))
1002 newelt = Fcar (tem2);
1004 if (NILP (Fmemq (newelt, tem)))
1005 Fsetcar (tail, Fcons (Fcar (tem),
1006 Fcons (newelt, Fcdr (tem))));
1008 tem2 = Fcdr (tem2);
1009 QUIT;
1013 else
1014 prev = tail;
1015 tail = Fcdr (tail);
1016 QUIT;
1019 /* If we're loading, cons the new assoc onto the front of load-history,
1020 the most-recently-loaded position. Also do this if we didn't find
1021 an existing member for the current source. */
1022 if (loading || !foundit)
1023 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1024 Vload_history);
1027 Lisp_Object
1028 unreadpure () /* Used as unwind-protect function in readevalloop */
1030 read_pure = 0;
1031 return Qnil;
1034 static Lisp_Object
1035 readevalloop_1 (old)
1036 Lisp_Object old;
1038 load_convert_to_unibyte = ! NILP (old);
1039 return Qnil;
1042 /* UNIBYTE specifies how to set load_convert_to_unibyte
1043 for this invocation.
1044 READFUN, if non-nil, is used instead of `read'. */
1046 static void
1047 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1048 Lisp_Object readcharfun;
1049 FILE *stream;
1050 Lisp_Object sourcename;
1051 Lisp_Object (*evalfun) ();
1052 int printflag;
1053 Lisp_Object unibyte, readfun;
1055 register int c;
1056 register Lisp_Object val;
1057 int count = specpdl_ptr - specpdl;
1058 struct gcpro gcpro1;
1059 struct buffer *b = 0;
1061 if (BUFFERP (readcharfun))
1062 b = XBUFFER (readcharfun);
1063 else if (MARKERP (readcharfun))
1064 b = XMARKER (readcharfun)->buffer;
1066 specbind (Qstandard_input, readcharfun);
1067 specbind (Qcurrent_load_list, Qnil);
1068 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1069 load_convert_to_unibyte = !NILP (unibyte);
1071 readchar_backlog = -1;
1073 GCPRO1 (sourcename);
1075 LOADHIST_ATTACH (sourcename);
1077 while (1)
1079 if (b != 0 && NILP (b->name))
1080 error ("Reading from killed buffer");
1082 instream = stream;
1083 c = READCHAR;
1084 if (c == ';')
1086 while ((c = READCHAR) != '\n' && c != -1);
1087 continue;
1089 if (c < 0) break;
1091 /* Ignore whitespace here, so we can detect eof. */
1092 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1093 continue;
1095 if (!NILP (Vpurify_flag) && c == '(')
1097 int count1 = specpdl_ptr - specpdl;
1098 record_unwind_protect (unreadpure, Qnil);
1099 val = read_list (-1, readcharfun);
1100 unbind_to (count1, Qnil);
1102 else
1104 UNREAD (c);
1105 read_objects = Qnil;
1106 if (! NILP (readfun))
1107 val = call1 (readfun, readcharfun);
1108 else if (! NILP (Vload_read_function))
1109 val = call1 (Vload_read_function, readcharfun);
1110 else
1111 val = read0 (readcharfun);
1114 val = (*evalfun) (val);
1115 if (printflag)
1117 Vvalues = Fcons (val, Vvalues);
1118 if (EQ (Vstandard_output, Qt))
1119 Fprin1 (val, Qnil);
1120 else
1121 Fprint (val, Qnil);
1125 build_load_history (stream, sourcename);
1126 UNGCPRO;
1128 unbind_to (count, Qnil);
1131 #ifndef standalone
1133 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1134 "Execute the current buffer as Lisp code.\n\
1135 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1136 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1137 PRINTFLAG controls printing of output:\n\
1138 nil means discard it; anything else is stream for print.\n\
1140 If the optional third argument FILENAME is non-nil,\n\
1141 it specifies the file name to use for `load-history'.\n\
1142 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1143 for this invocation.\n\
1145 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1146 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1148 This function preserves the position of point.")
1149 (buffer, printflag, filename, unibyte, do_allow_print)
1150 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1152 int count = specpdl_ptr - specpdl;
1153 Lisp_Object tem, buf;
1155 if (NILP (buffer))
1156 buf = Fcurrent_buffer ();
1157 else
1158 buf = Fget_buffer (buffer);
1159 if (NILP (buf))
1160 error ("No such buffer");
1162 if (NILP (printflag) && NILP (do_allow_print))
1163 tem = Qsymbolp;
1164 else
1165 tem = printflag;
1167 if (NILP (filename))
1168 filename = XBUFFER (buf)->filename;
1170 specbind (Qstandard_output, tem);
1171 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1172 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1173 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1174 unbind_to (count, Qnil);
1176 return Qnil;
1179 #if 0
1180 XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
1181 "Execute the current buffer as Lisp code.\n\
1182 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1183 nil means discard it; anything else is stream for print.\n\
1185 If there is no error, point does not move. If there is an error,\n\
1186 point remains at the end of the last character read from the buffer.")
1187 (printflag)
1188 Lisp_Object printflag;
1190 int count = specpdl_ptr - specpdl;
1191 Lisp_Object tem, cbuf;
1193 cbuf = Fcurrent_buffer ()
1195 if (NILP (printflag))
1196 tem = Qsymbolp;
1197 else
1198 tem = printflag;
1199 specbind (Qstandard_output, tem);
1200 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1201 SET_PT (BEGV);
1202 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1203 !NILP (printflag), Qnil, Qnil);
1204 return unbind_to (count, Qnil);
1206 #endif
1208 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1209 "Execute the region as Lisp code.\n\
1210 When called from programs, expects two arguments,\n\
1211 giving starting and ending indices in the current buffer\n\
1212 of the text to be executed.\n\
1213 Programs can pass third argument PRINTFLAG which controls output:\n\
1214 nil means discard it; anything else is stream for printing it.\n\
1215 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1216 instead of `read' to read each expression. It gets one argument\n\
1217 which is the input stream for reading characters.\n\
1219 This function does not move point.")
1220 (start, end, printflag, read_function)
1221 Lisp_Object start, end, printflag, read_function;
1223 int count = specpdl_ptr - specpdl;
1224 Lisp_Object tem, cbuf;
1226 cbuf = Fcurrent_buffer ();
1228 if (NILP (printflag))
1229 tem = Qsymbolp;
1230 else
1231 tem = printflag;
1232 specbind (Qstandard_output, tem);
1234 if (NILP (printflag))
1235 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1236 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1238 /* This both uses start and checks its type. */
1239 Fgoto_char (start);
1240 Fnarrow_to_region (make_number (BEGV), end);
1241 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1242 !NILP (printflag), Qnil, read_function);
1244 return unbind_to (count, Qnil);
1247 #endif /* standalone */
1249 DEFUN ("read", Fread, Sread, 0, 1, 0,
1250 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1251 If STREAM is nil, use the value of `standard-input' (which see).\n\
1252 STREAM or the value of `standard-input' may be:\n\
1253 a buffer (read from point and advance it)\n\
1254 a marker (read from where it points and advance it)\n\
1255 a function (call it with no arguments for each character,\n\
1256 call it with a char as argument to push a char back)\n\
1257 a string (takes text from string, starting at the beginning)\n\
1258 t (read text line using minibuffer and use it).")
1259 (stream)
1260 Lisp_Object stream;
1262 extern Lisp_Object Fread_minibuffer ();
1264 if (NILP (stream))
1265 stream = Vstandard_input;
1266 if (EQ (stream, Qt))
1267 stream = Qread_char;
1269 readchar_backlog = -1;
1270 new_backquote_flag = 0;
1271 read_objects = Qnil;
1273 #ifndef standalone
1274 if (EQ (stream, Qread_char))
1275 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1276 #endif
1278 if (STRINGP (stream))
1279 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1281 return read0 (stream);
1284 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1285 "Read one Lisp expression which is represented as text by STRING.\n\
1286 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1287 START and END optionally delimit a substring of STRING from which to read;\n\
1288 they default to 0 and (length STRING) respectively.")
1289 (string, start, end)
1290 Lisp_Object string, start, end;
1292 int startval, endval;
1293 Lisp_Object tem;
1295 CHECK_STRING (string,0);
1297 if (NILP (end))
1298 endval = XSTRING (string)->size;
1299 else
1301 CHECK_NUMBER (end, 2);
1302 endval = XINT (end);
1303 if (endval < 0 || endval > XSTRING (string)->size)
1304 args_out_of_range (string, end);
1307 if (NILP (start))
1308 startval = 0;
1309 else
1311 CHECK_NUMBER (start, 1);
1312 startval = XINT (start);
1313 if (startval < 0 || startval > endval)
1314 args_out_of_range (string, start);
1317 read_from_string_index = startval;
1318 read_from_string_index_byte = string_char_to_byte (string, startval);
1319 read_from_string_limit = endval;
1321 new_backquote_flag = 0;
1322 read_objects = Qnil;
1324 tem = read0 (string);
1325 return Fcons (tem, make_number (read_from_string_index));
1328 /* Use this for recursive reads, in contexts where internal tokens
1329 are not allowed. */
1331 static Lisp_Object
1332 read0 (readcharfun)
1333 Lisp_Object readcharfun;
1335 register Lisp_Object val;
1336 int c;
1338 val = read1 (readcharfun, &c, 0);
1339 if (c)
1340 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1341 make_number (c)),
1342 Qnil));
1344 return val;
1347 static int read_buffer_size;
1348 static char *read_buffer;
1350 /* Read multibyte form and return it as a character. C is a first
1351 byte of multibyte form, and rest of them are read from
1352 READCHARFUN. */
1354 static int
1355 read_multibyte (c, readcharfun)
1356 register int c;
1357 Lisp_Object readcharfun;
1359 /* We need the actual character code of this multibyte
1360 characters. */
1361 unsigned char str[MAX_MULTIBYTE_LENGTH];
1362 int len = 0;
1364 str[len++] = c;
1365 while ((c = READCHAR) >= 0xA0
1366 && len < MAX_MULTIBYTE_LENGTH)
1367 str[len++] = c;
1368 UNREAD (c);
1369 return STRING_CHAR (str, len);
1372 /* Read a \-escape sequence, assuming we already read the `\'. */
1374 static int
1375 read_escape (readcharfun, stringp)
1376 Lisp_Object readcharfun;
1377 int stringp;
1379 register int c = READCHAR;
1380 switch (c)
1382 case -1:
1383 error ("End of file");
1385 case 'a':
1386 return '\007';
1387 case 'b':
1388 return '\b';
1389 case 'd':
1390 return 0177;
1391 case 'e':
1392 return 033;
1393 case 'f':
1394 return '\f';
1395 case 'n':
1396 return '\n';
1397 case 'r':
1398 return '\r';
1399 case 't':
1400 return '\t';
1401 case 'v':
1402 return '\v';
1403 case '\n':
1404 return -1;
1405 case ' ':
1406 if (stringp)
1407 return -1;
1408 return ' ';
1410 case 'M':
1411 c = READCHAR;
1412 if (c != '-')
1413 error ("Invalid escape character syntax");
1414 c = READCHAR;
1415 if (c == '\\')
1416 c = read_escape (readcharfun, 0);
1417 return c | meta_modifier;
1419 case 'S':
1420 c = READCHAR;
1421 if (c != '-')
1422 error ("Invalid escape character syntax");
1423 c = READCHAR;
1424 if (c == '\\')
1425 c = read_escape (readcharfun, 0);
1426 return c | shift_modifier;
1428 case 'H':
1429 c = READCHAR;
1430 if (c != '-')
1431 error ("Invalid escape character syntax");
1432 c = READCHAR;
1433 if (c == '\\')
1434 c = read_escape (readcharfun, 0);
1435 return c | hyper_modifier;
1437 case 'A':
1438 c = READCHAR;
1439 if (c != '-')
1440 error ("Invalid escape character syntax");
1441 c = READCHAR;
1442 if (c == '\\')
1443 c = read_escape (readcharfun, 0);
1444 return c | alt_modifier;
1446 case 's':
1447 c = READCHAR;
1448 if (c != '-')
1449 error ("Invalid escape character syntax");
1450 c = READCHAR;
1451 if (c == '\\')
1452 c = read_escape (readcharfun, 0);
1453 return c | super_modifier;
1455 case 'C':
1456 c = READCHAR;
1457 if (c != '-')
1458 error ("Invalid escape character syntax");
1459 case '^':
1460 c = READCHAR;
1461 if (c == '\\')
1462 c = read_escape (readcharfun, 0);
1463 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1464 return 0177 | (c & CHAR_MODIFIER_MASK);
1465 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1466 return c | ctrl_modifier;
1467 /* ASCII control chars are made from letters (both cases),
1468 as well as the non-letters within 0100...0137. */
1469 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1470 return (c & (037 | ~0177));
1471 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1472 return (c & (037 | ~0177));
1473 else
1474 return c | ctrl_modifier;
1476 case '0':
1477 case '1':
1478 case '2':
1479 case '3':
1480 case '4':
1481 case '5':
1482 case '6':
1483 case '7':
1484 /* An octal escape, as in ANSI C. */
1486 register int i = c - '0';
1487 register int count = 0;
1488 while (++count < 3)
1490 if ((c = READCHAR) >= '0' && c <= '7')
1492 i *= 8;
1493 i += c - '0';
1495 else
1497 UNREAD (c);
1498 break;
1501 return i;
1504 case 'x':
1505 /* A hex escape, as in ANSI C. */
1507 int i = 0;
1508 while (1)
1510 c = READCHAR;
1511 if (c >= '0' && c <= '9')
1513 i *= 16;
1514 i += c - '0';
1516 else if ((c >= 'a' && c <= 'f')
1517 || (c >= 'A' && c <= 'F'))
1519 i *= 16;
1520 if (c >= 'a' && c <= 'f')
1521 i += c - 'a' + 10;
1522 else
1523 i += c - 'A' + 10;
1525 else
1527 UNREAD (c);
1528 break;
1531 return i;
1534 default:
1535 if (BASE_LEADING_CODE_P (c))
1536 c = read_multibyte (c, readcharfun);
1537 return c;
1541 /* If the next token is ')' or ']' or '.', we store that character
1542 in *PCH and the return value is not interesting. Else, we store
1543 zero in *PCH and we read and return one lisp object.
1545 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1547 static Lisp_Object
1548 read1 (readcharfun, pch, first_in_list)
1549 register Lisp_Object readcharfun;
1550 int *pch;
1551 int first_in_list;
1553 register int c;
1554 int uninterned_symbol = 0;
1556 *pch = 0;
1558 retry:
1560 c = READCHAR;
1561 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1563 switch (c)
1565 case '(':
1566 return read_list (0, readcharfun);
1568 case '[':
1569 return read_vector (readcharfun, 0);
1571 case ')':
1572 case ']':
1574 *pch = c;
1575 return Qnil;
1578 case '#':
1579 c = READCHAR;
1580 if (c == '^')
1582 c = READCHAR;
1583 if (c == '[')
1585 Lisp_Object tmp;
1586 tmp = read_vector (readcharfun, 0);
1587 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1588 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1589 error ("Invalid size char-table");
1590 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1591 XCHAR_TABLE (tmp)->top = Qt;
1592 return tmp;
1594 else if (c == '^')
1596 c = READCHAR;
1597 if (c == '[')
1599 Lisp_Object tmp;
1600 tmp = read_vector (readcharfun, 0);
1601 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1602 error ("Invalid size char-table");
1603 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1604 XCHAR_TABLE (tmp)->top = Qnil;
1605 return tmp;
1607 Fsignal (Qinvalid_read_syntax,
1608 Fcons (make_string ("#^^", 3), Qnil));
1610 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1612 if (c == '&')
1614 Lisp_Object length;
1615 length = read1 (readcharfun, pch, first_in_list);
1616 c = READCHAR;
1617 if (c == '"')
1619 Lisp_Object tmp, val;
1620 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1621 / BITS_PER_CHAR);
1623 UNREAD (c);
1624 tmp = read1 (readcharfun, pch, first_in_list);
1625 if (size_in_chars != XSTRING (tmp)->size
1626 /* We used to print 1 char too many
1627 when the number of bits was a multiple of 8.
1628 Accept such input in case it came from an old version. */
1629 && ! (XFASTINT (length)
1630 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1631 Fsignal (Qinvalid_read_syntax,
1632 Fcons (make_string ("#&...", 5), Qnil));
1634 val = Fmake_bool_vector (length, Qnil);
1635 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1636 size_in_chars);
1637 /* Clear the extraneous bits in the last byte. */
1638 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1639 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1640 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1641 return val;
1643 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1644 Qnil));
1646 if (c == '[')
1648 /* Accept compiled functions at read-time so that we don't have to
1649 build them using function calls. */
1650 Lisp_Object tmp;
1651 tmp = read_vector (readcharfun, 1);
1652 return Fmake_byte_code (XVECTOR (tmp)->size,
1653 XVECTOR (tmp)->contents);
1655 if (c == '(')
1657 Lisp_Object tmp;
1658 struct gcpro gcpro1;
1659 int ch;
1661 /* Read the string itself. */
1662 tmp = read1 (readcharfun, &ch, 0);
1663 if (ch != 0 || !STRINGP (tmp))
1664 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1665 GCPRO1 (tmp);
1666 /* Read the intervals and their properties. */
1667 while (1)
1669 Lisp_Object beg, end, plist;
1671 beg = read1 (readcharfun, &ch, 0);
1672 if (ch == ')')
1673 break;
1674 if (ch == 0)
1675 end = read1 (readcharfun, &ch, 0);
1676 if (ch == 0)
1677 plist = read1 (readcharfun, &ch, 0);
1678 if (ch)
1679 Fsignal (Qinvalid_read_syntax,
1680 Fcons (build_string ("invalid string property list"),
1681 Qnil));
1682 Fset_text_properties (beg, end, plist, tmp);
1684 UNGCPRO;
1685 return tmp;
1688 /* #@NUMBER is used to skip NUMBER following characters.
1689 That's used in .elc files to skip over doc strings
1690 and function definitions. */
1691 if (c == '@')
1693 int i, nskip = 0;
1695 /* Read a decimal integer. */
1696 while ((c = READCHAR) >= 0
1697 && c >= '0' && c <= '9')
1699 nskip *= 10;
1700 nskip += c - '0';
1702 if (c >= 0)
1703 UNREAD (c);
1705 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1707 /* If we are supposed to force doc strings into core right now,
1708 record the last string that we skipped,
1709 and record where in the file it comes from. */
1711 /* But first exchange saved_doc_string
1712 with prev_saved_doc_string, so we save two strings. */
1714 char *temp = saved_doc_string;
1715 int temp_size = saved_doc_string_size;
1716 file_offset temp_pos = saved_doc_string_position;
1717 int temp_len = saved_doc_string_length;
1719 saved_doc_string = prev_saved_doc_string;
1720 saved_doc_string_size = prev_saved_doc_string_size;
1721 saved_doc_string_position = prev_saved_doc_string_position;
1722 saved_doc_string_length = prev_saved_doc_string_length;
1724 prev_saved_doc_string = temp;
1725 prev_saved_doc_string_size = temp_size;
1726 prev_saved_doc_string_position = temp_pos;
1727 prev_saved_doc_string_length = temp_len;
1730 if (saved_doc_string_size == 0)
1732 saved_doc_string_size = nskip + 100;
1733 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1735 if (nskip > saved_doc_string_size)
1737 saved_doc_string_size = nskip + 100;
1738 saved_doc_string = (char *) xrealloc (saved_doc_string,
1739 saved_doc_string_size);
1742 saved_doc_string_position = file_tell (instream);
1744 /* Copy that many characters into saved_doc_string. */
1745 for (i = 0; i < nskip && c >= 0; i++)
1746 saved_doc_string[i] = c = READCHAR;
1748 saved_doc_string_length = i;
1750 else
1752 /* Skip that many characters. */
1753 for (i = 0; i < nskip && c >= 0; i++)
1754 c = READCHAR;
1757 goto retry;
1759 if (c == '$')
1760 return Vload_file_name;
1761 if (c == '\'')
1762 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1763 /* #:foo is the uninterned symbol named foo. */
1764 if (c == ':')
1766 uninterned_symbol = 1;
1767 c = READCHAR;
1768 goto default_label;
1770 /* Reader forms that can reuse previously read objects. */
1771 if (c >= '0' && c <= '9')
1773 int n = 0;
1774 Lisp_Object tem;
1776 /* Read a non-negative integer. */
1777 while (c >= '0' && c <= '9')
1779 n *= 10;
1780 n += c - '0';
1781 c = READCHAR;
1783 /* #n=object returns object, but associates it with n for #n#. */
1784 if (c == '=')
1786 /* Make a placeholder for #n# to use temporarily */
1787 Lisp_Object placeholder;
1788 Lisp_Object cell;
1790 placeholder = Fcons(Qnil, Qnil);
1791 cell = Fcons (make_number (n), placeholder);
1792 read_objects = Fcons (cell, read_objects);
1794 /* Read the object itself. */
1795 tem = read0 (readcharfun);
1797 /* Now put it everywhere the placeholder was... */
1798 substitute_object_in_subtree (tem, placeholder);
1800 /* ...and #n# will use the real value from now on. */
1801 Fsetcdr (cell, tem);
1803 return tem;
1805 /* #n# returns a previously read object. */
1806 if (c == '#')
1808 tem = Fassq (make_number (n), read_objects);
1809 if (CONSP (tem))
1810 return XCDR (tem);
1811 /* Fall through to error message. */
1813 /* Fall through to error message. */
1816 UNREAD (c);
1817 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1819 case ';':
1820 while ((c = READCHAR) >= 0 && c != '\n');
1821 goto retry;
1823 case '\'':
1825 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1828 case '`':
1829 if (first_in_list)
1830 goto default_label;
1831 else
1833 Lisp_Object value;
1835 new_backquote_flag = 1;
1836 value = read0 (readcharfun);
1837 new_backquote_flag = 0;
1839 return Fcons (Qbackquote, Fcons (value, Qnil));
1842 case ',':
1843 if (new_backquote_flag)
1845 Lisp_Object comma_type = Qnil;
1846 Lisp_Object value;
1847 int ch = READCHAR;
1849 if (ch == '@')
1850 comma_type = Qcomma_at;
1851 else if (ch == '.')
1852 comma_type = Qcomma_dot;
1853 else
1855 if (ch >= 0) UNREAD (ch);
1856 comma_type = Qcomma;
1859 new_backquote_flag = 0;
1860 value = read0 (readcharfun);
1861 new_backquote_flag = 1;
1862 return Fcons (comma_type, Fcons (value, Qnil));
1864 else
1865 goto default_label;
1867 case '?':
1869 c = READCHAR;
1870 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1872 if (c == '\\')
1873 c = read_escape (readcharfun, 0);
1874 else if (BASE_LEADING_CODE_P (c))
1875 c = read_multibyte (c, readcharfun);
1877 return make_number (c);
1880 case '"':
1882 register char *p = read_buffer;
1883 register char *end = read_buffer + read_buffer_size;
1884 register int c;
1885 /* Nonzero if we saw an escape sequence specifying
1886 a multibyte character. */
1887 int force_multibyte = 0;
1888 /* Nonzero if we saw an escape sequence specifying
1889 a single-byte character. */
1890 int force_singlebyte = 0;
1891 int cancel = 0;
1892 int nchars;
1894 while ((c = READCHAR) >= 0
1895 && c != '\"')
1897 if (end - p < MAX_MULTIBYTE_LENGTH)
1899 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1900 p += new - read_buffer;
1901 read_buffer += new - read_buffer;
1902 end = read_buffer + read_buffer_size;
1905 if (c == '\\')
1907 c = read_escape (readcharfun, 1);
1909 /* C is -1 if \ newline has just been seen */
1910 if (c == -1)
1912 if (p == read_buffer)
1913 cancel = 1;
1914 continue;
1917 /* If an escape specifies a non-ASCII single-byte character,
1918 this must be a unibyte string. */
1919 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
1920 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
1921 force_singlebyte = 1;
1924 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1926 /* Any modifiers for a multibyte character are invalid. */
1927 if (c & CHAR_MODIFIER_MASK)
1928 error ("Invalid modifier in string");
1929 p += CHAR_STRING (c, p);
1930 force_multibyte = 1;
1932 else
1934 /* Allow `\C- ' and `\C-?'. */
1935 if (c == (CHAR_CTL | ' '))
1936 c = 0;
1937 else if (c == (CHAR_CTL | '?'))
1938 c = 127;
1940 if (c & CHAR_SHIFT)
1942 /* Shift modifier is valid only with [A-Za-z]. */
1943 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
1944 c &= ~CHAR_SHIFT;
1945 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
1946 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
1949 if (c & CHAR_META)
1950 /* Move the meta bit to the right place for a string. */
1951 c = (c & ~CHAR_META) | 0x80;
1952 if (c & ~0xff)
1953 error ("Invalid modifier in string");
1954 *p++ = c;
1957 if (c < 0)
1958 return Fsignal (Qend_of_file, Qnil);
1960 /* If purifying, and string starts with \ newline,
1961 return zero instead. This is for doc strings
1962 that we are really going to find in etc/DOC.nn.nn */
1963 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
1964 return make_number (0);
1966 if (force_multibyte)
1967 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
1968 else if (force_singlebyte)
1969 nchars = p - read_buffer;
1970 else if (load_convert_to_unibyte)
1972 Lisp_Object string;
1973 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
1974 if (p - read_buffer != nchars)
1976 string = make_multibyte_string (read_buffer, nchars,
1977 p - read_buffer);
1978 return Fstring_make_unibyte (string);
1981 else if (EQ (readcharfun, Qget_file_char)
1982 || EQ (readcharfun, Qlambda))
1983 /* Nowadays, reading directly from a file
1984 is used only for compiled Emacs Lisp files,
1985 and those always use the Emacs internal encoding.
1986 Meanwhile, Qlambda is used for reading dynamic byte code
1987 (compiled with byte-compile-dynamic = t). */
1988 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
1989 else
1990 /* In all other cases, if we read these bytes as
1991 separate characters, treat them as separate characters now. */
1992 nchars = p - read_buffer;
1994 if (read_pure)
1995 return make_pure_string (read_buffer, nchars, p - read_buffer,
1996 (force_multibyte
1997 || (p - read_buffer != nchars)));
1998 return make_specified_string (read_buffer, nchars, p - read_buffer,
1999 (force_multibyte
2000 || (p - read_buffer != nchars)));
2003 case '.':
2005 #ifdef LISP_FLOAT_TYPE
2006 /* If a period is followed by a number, then we should read it
2007 as a floating point number. Otherwise, it denotes a dotted
2008 pair. */
2009 int next_char = READCHAR;
2010 UNREAD (next_char);
2012 if (! (next_char >= '0' && next_char <= '9'))
2013 #endif
2015 *pch = c;
2016 return Qnil;
2019 /* Otherwise, we fall through! Note that the atom-reading loop
2020 below will now loop at least once, assuring that we will not
2021 try to UNREAD two characters in a row. */
2023 default:
2024 default_label:
2025 if (c <= 040) goto retry;
2027 register char *p = read_buffer;
2028 int quoted = 0;
2031 register char *end = read_buffer + read_buffer_size;
2033 while (c > 040
2034 && !(c == '\"' || c == '\'' || c == ';' || c == '?'
2035 || c == '(' || c == ')'
2036 #ifndef LISP_FLOAT_TYPE
2037 /* If we have floating-point support, then we need
2038 to allow <digits><dot><digits>. */
2039 || c =='.'
2040 #endif /* not LISP_FLOAT_TYPE */
2041 || c == '[' || c == ']' || c == '#'
2044 if (end - p < MAX_MULTIBYTE_LENGTH)
2046 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2047 p += new - read_buffer;
2048 read_buffer += new - read_buffer;
2049 end = read_buffer + read_buffer_size;
2051 if (c == '\\')
2053 c = READCHAR;
2054 quoted = 1;
2057 if (! SINGLE_BYTE_CHAR_P (c))
2058 p += CHAR_STRING (c, p);
2059 else
2060 *p++ = c;
2062 c = READCHAR;
2065 if (p == end)
2067 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2068 p += new - read_buffer;
2069 read_buffer += new - read_buffer;
2070 /* end = read_buffer + read_buffer_size; */
2072 *p = 0;
2073 if (c >= 0)
2074 UNREAD (c);
2077 if (!quoted && !uninterned_symbol)
2079 register char *p1;
2080 register Lisp_Object val;
2081 p1 = read_buffer;
2082 if (*p1 == '+' || *p1 == '-') p1++;
2083 /* Is it an integer? */
2084 if (p1 != p)
2086 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2087 #ifdef LISP_FLOAT_TYPE
2088 /* Integers can have trailing decimal points. */
2089 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2090 #endif
2091 if (p1 == p)
2092 /* It is an integer. */
2094 #ifdef LISP_FLOAT_TYPE
2095 if (p1[-1] == '.')
2096 p1[-1] = '\0';
2097 #endif
2098 if (sizeof (int) == sizeof (EMACS_INT))
2099 XSETINT (val, atoi (read_buffer));
2100 else if (sizeof (long) == sizeof (EMACS_INT))
2101 XSETINT (val, atol (read_buffer));
2102 else
2103 abort ();
2104 return val;
2107 #ifdef LISP_FLOAT_TYPE
2108 if (isfloat_string (read_buffer))
2110 /* Compute NaN and infinities using 0.0 in a variable,
2111 to cope with compilers that think they are smarter
2112 than we are. */
2113 double zero = 0.0;
2115 double value;
2117 /* Negate the value ourselves. This treats 0, NaNs,
2118 and infinity properly on IEEE floating point hosts,
2119 and works around a common bug where atof ("-0.0")
2120 drops the sign. */
2121 int negative = read_buffer[0] == '-';
2123 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2124 returns 1, is if the input ends in e+INF or e+NaN. */
2125 switch (p[-1])
2127 case 'F':
2128 value = 1.0 / zero;
2129 break;
2130 case 'N':
2131 value = zero / zero;
2132 break;
2133 default:
2134 value = atof (read_buffer + negative);
2135 break;
2138 return make_float (negative ? - value : value);
2140 #endif
2143 if (uninterned_symbol)
2144 return make_symbol (read_buffer);
2145 else
2146 return intern (read_buffer);
2152 /* List of nodes we've seen during substitute_object_in_subtree. */
2153 static Lisp_Object seen_list;
2155 static void
2156 substitute_object_in_subtree (object, placeholder)
2157 Lisp_Object object;
2158 Lisp_Object placeholder;
2160 Lisp_Object check_object;
2162 /* We haven't seen any objects when we start. */
2163 seen_list = Qnil;
2165 /* Make all the substitutions. */
2166 check_object
2167 = substitute_object_recurse (object, placeholder, object);
2169 /* Clear seen_list because we're done with it. */
2170 seen_list = Qnil;
2172 /* The returned object here is expected to always eq the
2173 original. */
2174 if (!EQ (check_object, object))
2175 error ("Unexpected mutation error in reader");
2178 /* Feval doesn't get called from here, so no gc protection is needed. */
2179 #define SUBSTITUTE(get_val, set_val) \
2181 Lisp_Object old_value = get_val; \
2182 Lisp_Object true_value \
2183 = substitute_object_recurse (object, placeholder,\
2184 old_value); \
2186 if (!EQ (old_value, true_value)) \
2188 set_val; \
2192 static Lisp_Object
2193 substitute_object_recurse (object, placeholder, subtree)
2194 Lisp_Object object;
2195 Lisp_Object placeholder;
2196 Lisp_Object subtree;
2198 /* If we find the placeholder, return the target object. */
2199 if (EQ (placeholder, subtree))
2200 return object;
2202 /* If we've been to this node before, don't explore it again. */
2203 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2204 return subtree;
2206 /* If this node can be the entry point to a cycle, remember that
2207 we've seen it. It can only be such an entry point if it was made
2208 by #n=, which means that we can find it as a value in
2209 read_objects. */
2210 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2211 seen_list = Fcons (subtree, seen_list);
2213 /* Recurse according to subtree's type.
2214 Every branch must return a Lisp_Object. */
2215 switch (XTYPE (subtree))
2217 case Lisp_Vectorlike:
2219 int i;
2220 int length = Flength(subtree);
2221 for (i = 0; i < length; i++)
2223 Lisp_Object idx = make_number (i);
2224 SUBSTITUTE (Faref (subtree, idx),
2225 Faset (subtree, idx, true_value));
2227 return subtree;
2230 case Lisp_Cons:
2232 SUBSTITUTE (Fcar_safe (subtree),
2233 Fsetcar (subtree, true_value));
2234 SUBSTITUTE (Fcdr_safe (subtree),
2235 Fsetcdr (subtree, true_value));
2236 return subtree;
2239 case Lisp_String:
2241 /* Check for text properties in each interval.
2242 substitute_in_interval contains part of the logic. */
2244 INTERVAL root_interval = XSTRING (subtree)->intervals;
2245 Lisp_Object arg = Fcons (object, placeholder);
2247 traverse_intervals (root_interval, 1, 0,
2248 &substitute_in_interval, arg);
2250 return subtree;
2253 /* Other types don't recurse any further. */
2254 default:
2255 return subtree;
2259 /* Helper function for substitute_object_recurse. */
2260 static void
2261 substitute_in_interval (interval, arg)
2262 INTERVAL interval;
2263 Lisp_Object arg;
2265 Lisp_Object object = Fcar (arg);
2266 Lisp_Object placeholder = Fcdr (arg);
2268 SUBSTITUTE(interval->plist, interval->plist = true_value);
2272 #ifdef LISP_FLOAT_TYPE
2274 #define LEAD_INT 1
2275 #define DOT_CHAR 2
2276 #define TRAIL_INT 4
2277 #define E_CHAR 8
2278 #define EXP_INT 16
2281 isfloat_string (cp)
2282 register char *cp;
2284 register int state;
2286 char *start = cp;
2288 state = 0;
2289 if (*cp == '+' || *cp == '-')
2290 cp++;
2292 if (*cp >= '0' && *cp <= '9')
2294 state |= LEAD_INT;
2295 while (*cp >= '0' && *cp <= '9')
2296 cp++;
2298 if (*cp == '.')
2300 state |= DOT_CHAR;
2301 cp++;
2303 if (*cp >= '0' && *cp <= '9')
2305 state |= TRAIL_INT;
2306 while (*cp >= '0' && *cp <= '9')
2307 cp++;
2309 if (*cp == 'e' || *cp == 'E')
2311 state |= E_CHAR;
2312 cp++;
2313 if (*cp == '+' || *cp == '-')
2314 cp++;
2317 if (*cp >= '0' && *cp <= '9')
2319 state |= EXP_INT;
2320 while (*cp >= '0' && *cp <= '9')
2321 cp++;
2323 else if (cp == start)
2325 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2327 state |= EXP_INT;
2328 cp += 3;
2330 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2332 state |= EXP_INT;
2333 cp += 3;
2336 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2337 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2338 || state == (DOT_CHAR|TRAIL_INT)
2339 || state == (LEAD_INT|E_CHAR|EXP_INT)
2340 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2341 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2343 #endif /* LISP_FLOAT_TYPE */
2345 static Lisp_Object
2346 read_vector (readcharfun, bytecodeflag)
2347 Lisp_Object readcharfun;
2348 int bytecodeflag;
2350 register int i;
2351 register int size;
2352 register Lisp_Object *ptr;
2353 register Lisp_Object tem, item, vector;
2354 register struct Lisp_Cons *otem;
2355 Lisp_Object len;
2357 tem = read_list (1, readcharfun);
2358 len = Flength (tem);
2359 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2361 size = XVECTOR (vector)->size;
2362 ptr = XVECTOR (vector)->contents;
2363 for (i = 0; i < size; i++)
2365 item = Fcar (tem);
2366 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2367 bytecode object, the docstring containing the bytecode and
2368 constants values must be treated as unibyte and passed to
2369 Fread, to get the actual bytecode string and constants vector. */
2370 if (bytecodeflag && load_force_doc_strings)
2372 if (i == COMPILED_BYTECODE)
2374 if (!STRINGP (item))
2375 error ("invalid byte code");
2377 /* Delay handling the bytecode slot until we know whether
2378 it is lazily-loaded (we can tell by whether the
2379 constants slot is nil). */
2380 ptr[COMPILED_CONSTANTS] = item;
2381 item = Qnil;
2383 else if (i == COMPILED_CONSTANTS)
2385 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2387 if (NILP (item))
2389 /* Coerce string to unibyte (like string-as-unibyte,
2390 but without generating extra garbage and
2391 guaranteeing no change in the contents). */
2392 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2393 SET_STRING_BYTES (XSTRING (bytestr), -1);
2395 item = Fread (bytestr);
2396 if (!CONSP (item))
2397 error ("invalid byte code");
2399 otem = XCONS (item);
2400 bytestr = XCAR (item);
2401 item = XCDR (item);
2402 free_cons (otem);
2405 /* Now handle the bytecode slot. */
2406 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2409 ptr[i] = read_pure ? Fpurecopy (item) : item;
2410 otem = XCONS (tem);
2411 tem = Fcdr (tem);
2412 free_cons (otem);
2414 return vector;
2417 /* FLAG = 1 means check for ] to terminate rather than ) and .
2418 FLAG = -1 means check for starting with defun
2419 and make structure pure. */
2421 static Lisp_Object
2422 read_list (flag, readcharfun)
2423 int flag;
2424 register Lisp_Object readcharfun;
2426 /* -1 means check next element for defun,
2427 0 means don't check,
2428 1 means already checked and found defun. */
2429 int defunflag = flag < 0 ? -1 : 0;
2430 Lisp_Object val, tail;
2431 register Lisp_Object elt, tem;
2432 struct gcpro gcpro1, gcpro2;
2433 /* 0 is the normal case.
2434 1 means this list is a doc reference; replace it with the number 0.
2435 2 means this list is a doc reference; replace it with the doc string. */
2436 int doc_reference = 0;
2438 /* Initialize this to 1 if we are reading a list. */
2439 int first_in_list = flag <= 0;
2441 val = Qnil;
2442 tail = Qnil;
2444 while (1)
2446 int ch;
2447 GCPRO2 (val, tail);
2448 elt = read1 (readcharfun, &ch, first_in_list);
2449 UNGCPRO;
2451 first_in_list = 0;
2453 /* While building, if the list starts with #$, treat it specially. */
2454 if (EQ (elt, Vload_file_name)
2455 && ! NILP (elt)
2456 && !NILP (Vpurify_flag))
2458 if (NILP (Vdoc_file_name))
2459 /* We have not yet called Snarf-documentation, so assume
2460 this file is described in the DOC-MM.NN file
2461 and Snarf-documentation will fill in the right value later.
2462 For now, replace the whole list with 0. */
2463 doc_reference = 1;
2464 else
2465 /* We have already called Snarf-documentation, so make a relative
2466 file name for this file, so it can be found properly
2467 in the installed Lisp directory.
2468 We don't use Fexpand_file_name because that would make
2469 the directory absolute now. */
2470 elt = concat2 (build_string ("../lisp/"),
2471 Ffile_name_nondirectory (elt));
2473 else if (EQ (elt, Vload_file_name)
2474 && ! NILP (elt)
2475 && load_force_doc_strings)
2476 doc_reference = 2;
2478 if (ch)
2480 if (flag > 0)
2482 if (ch == ']')
2483 return val;
2484 Fsignal (Qinvalid_read_syntax,
2485 Fcons (make_string (") or . in a vector", 18), Qnil));
2487 if (ch == ')')
2488 return val;
2489 if (ch == '.')
2491 GCPRO2 (val, tail);
2492 if (!NILP (tail))
2493 XCDR (tail) = read0 (readcharfun);
2494 else
2495 val = read0 (readcharfun);
2496 read1 (readcharfun, &ch, 0);
2497 UNGCPRO;
2498 if (ch == ')')
2500 if (doc_reference == 1)
2501 return make_number (0);
2502 if (doc_reference == 2)
2504 /* Get a doc string from the file we are loading.
2505 If it's in saved_doc_string, get it from there. */
2506 int pos = XINT (XCDR (val));
2507 /* Position is negative for user variables. */
2508 if (pos < 0) pos = -pos;
2509 if (pos >= saved_doc_string_position
2510 && pos < (saved_doc_string_position
2511 + saved_doc_string_length))
2513 int start = pos - saved_doc_string_position;
2514 int from, to;
2516 /* Process quoting with ^A,
2517 and find the end of the string,
2518 which is marked with ^_ (037). */
2519 for (from = start, to = start;
2520 saved_doc_string[from] != 037;)
2522 int c = saved_doc_string[from++];
2523 if (c == 1)
2525 c = saved_doc_string[from++];
2526 if (c == 1)
2527 saved_doc_string[to++] = c;
2528 else if (c == '0')
2529 saved_doc_string[to++] = 0;
2530 else if (c == '_')
2531 saved_doc_string[to++] = 037;
2533 else
2534 saved_doc_string[to++] = c;
2537 return make_string (saved_doc_string + start,
2538 to - start);
2540 /* Look in prev_saved_doc_string the same way. */
2541 else if (pos >= prev_saved_doc_string_position
2542 && pos < (prev_saved_doc_string_position
2543 + prev_saved_doc_string_length))
2545 int start = pos - prev_saved_doc_string_position;
2546 int from, to;
2548 /* Process quoting with ^A,
2549 and find the end of the string,
2550 which is marked with ^_ (037). */
2551 for (from = start, to = start;
2552 prev_saved_doc_string[from] != 037;)
2554 int c = prev_saved_doc_string[from++];
2555 if (c == 1)
2557 c = prev_saved_doc_string[from++];
2558 if (c == 1)
2559 prev_saved_doc_string[to++] = c;
2560 else if (c == '0')
2561 prev_saved_doc_string[to++] = 0;
2562 else if (c == '_')
2563 prev_saved_doc_string[to++] = 037;
2565 else
2566 prev_saved_doc_string[to++] = c;
2569 return make_string (prev_saved_doc_string + start,
2570 to - start);
2572 else
2573 return get_doc_string (val, 0, 0);
2576 return val;
2578 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2580 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2582 tem = (read_pure && flag <= 0
2583 ? pure_cons (elt, Qnil)
2584 : Fcons (elt, Qnil));
2585 if (!NILP (tail))
2586 XCDR (tail) = tem;
2587 else
2588 val = tem;
2589 tail = tem;
2590 if (defunflag < 0)
2591 defunflag = EQ (elt, Qdefun);
2592 else if (defunflag > 0)
2593 read_pure = 1;
2597 Lisp_Object Vobarray;
2598 Lisp_Object initial_obarray;
2600 /* oblookup stores the bucket number here, for the sake of Funintern. */
2602 int oblookup_last_bucket_number;
2604 static int hash_string ();
2605 Lisp_Object oblookup ();
2607 /* Get an error if OBARRAY is not an obarray.
2608 If it is one, return it. */
2610 Lisp_Object
2611 check_obarray (obarray)
2612 Lisp_Object obarray;
2614 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2616 /* If Vobarray is now invalid, force it to be valid. */
2617 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2619 obarray = wrong_type_argument (Qvectorp, obarray);
2621 return obarray;
2624 /* Intern the C string STR: return a symbol with that name,
2625 interned in the current obarray. */
2627 Lisp_Object
2628 intern (str)
2629 char *str;
2631 Lisp_Object tem;
2632 int len = strlen (str);
2633 Lisp_Object obarray;
2635 obarray = Vobarray;
2636 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2637 obarray = check_obarray (obarray);
2638 tem = oblookup (obarray, str, len, len);
2639 if (SYMBOLP (tem))
2640 return tem;
2641 return Fintern (make_string (str, len), obarray);
2644 /* Create an uninterned symbol with name STR. */
2646 Lisp_Object
2647 make_symbol (str)
2648 char *str;
2650 int len = strlen (str);
2652 return Fmake_symbol ((!NILP (Vpurify_flag)
2653 ? make_pure_string (str, len, len, 0)
2654 : make_string (str, len)));
2657 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2658 "Return the canonical symbol whose name is STRING.\n\
2659 If there is none, one is created by this function and returned.\n\
2660 A second optional argument specifies the obarray to use;\n\
2661 it defaults to the value of `obarray'.")
2662 (string, obarray)
2663 Lisp_Object string, obarray;
2665 register Lisp_Object tem, sym, *ptr;
2667 if (NILP (obarray)) obarray = Vobarray;
2668 obarray = check_obarray (obarray);
2670 CHECK_STRING (string, 0);
2672 tem = oblookup (obarray, XSTRING (string)->data,
2673 XSTRING (string)->size,
2674 STRING_BYTES (XSTRING (string)));
2675 if (!INTEGERP (tem))
2676 return tem;
2678 if (!NILP (Vpurify_flag))
2679 string = Fpurecopy (string);
2680 sym = Fmake_symbol (string);
2681 XSYMBOL (sym)->obarray = obarray;
2683 if ((XSTRING (string)->data[0] == ':')
2684 && EQ (obarray, initial_obarray))
2685 XSYMBOL (sym)->value = sym;
2687 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2688 if (SYMBOLP (*ptr))
2689 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2690 else
2691 XSYMBOL (sym)->next = 0;
2692 *ptr = sym;
2693 return sym;
2696 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2697 "Return the canonical symbol named NAME, or nil if none exists.\n\
2698 NAME may be a string or a symbol. If it is a symbol, that exact\n\
2699 symbol is searched for.\n\
2700 A second optional argument specifies the obarray to use;\n\
2701 it defaults to the value of `obarray'.")
2702 (name, obarray)
2703 Lisp_Object name, obarray;
2705 register Lisp_Object tem;
2706 struct Lisp_String *string;
2708 if (NILP (obarray)) obarray = Vobarray;
2709 obarray = check_obarray (obarray);
2711 if (!SYMBOLP (name))
2713 CHECK_STRING (name, 0);
2714 string = XSTRING (name);
2716 else
2717 string = XSYMBOL (name)->name;
2719 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2720 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2721 return Qnil;
2722 else
2723 return tem;
2726 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2727 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2728 The value is t if a symbol was found and deleted, nil otherwise.\n\
2729 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2730 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2731 OBARRAY defaults to the value of the variable `obarray'.")
2732 (name, obarray)
2733 Lisp_Object name, obarray;
2735 register Lisp_Object string, tem;
2736 int hash;
2738 if (NILP (obarray)) obarray = Vobarray;
2739 obarray = check_obarray (obarray);
2741 if (SYMBOLP (name))
2742 XSETSTRING (string, XSYMBOL (name)->name);
2743 else
2745 CHECK_STRING (name, 0);
2746 string = name;
2749 tem = oblookup (obarray, XSTRING (string)->data,
2750 XSTRING (string)->size,
2751 STRING_BYTES (XSTRING (string)));
2752 if (INTEGERP (tem))
2753 return Qnil;
2754 /* If arg was a symbol, don't delete anything but that symbol itself. */
2755 if (SYMBOLP (name) && !EQ (name, tem))
2756 return Qnil;
2758 XSYMBOL (tem)->obarray = Qnil;
2760 hash = oblookup_last_bucket_number;
2762 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2764 if (XSYMBOL (tem)->next)
2765 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2766 else
2767 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2769 else
2771 Lisp_Object tail, following;
2773 for (tail = XVECTOR (obarray)->contents[hash];
2774 XSYMBOL (tail)->next;
2775 tail = following)
2777 XSETSYMBOL (following, XSYMBOL (tail)->next);
2778 if (EQ (following, tem))
2780 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2781 break;
2786 return Qt;
2789 /* Return the symbol in OBARRAY whose names matches the string
2790 of SIZE characters (SIZE_BYTE bytes) at PTR.
2791 If there is no such symbol in OBARRAY, return nil.
2793 Also store the bucket number in oblookup_last_bucket_number. */
2795 Lisp_Object
2796 oblookup (obarray, ptr, size, size_byte)
2797 Lisp_Object obarray;
2798 register char *ptr;
2799 int size, size_byte;
2801 int hash;
2802 int obsize;
2803 register Lisp_Object tail;
2804 Lisp_Object bucket, tem;
2806 if (!VECTORP (obarray)
2807 || (obsize = XVECTOR (obarray)->size) == 0)
2809 obarray = check_obarray (obarray);
2810 obsize = XVECTOR (obarray)->size;
2812 /* This is sometimes needed in the middle of GC. */
2813 obsize &= ~ARRAY_MARK_FLAG;
2814 /* Combining next two lines breaks VMS C 2.3. */
2815 hash = hash_string (ptr, size_byte);
2816 hash %= obsize;
2817 bucket = XVECTOR (obarray)->contents[hash];
2818 oblookup_last_bucket_number = hash;
2819 if (XFASTINT (bucket) == 0)
2821 else if (!SYMBOLP (bucket))
2822 error ("Bad data in guts of obarray"); /* Like CADR error message */
2823 else
2824 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2826 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
2827 && XSYMBOL (tail)->name->size == size
2828 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
2829 return tail;
2830 else if (XSYMBOL (tail)->next == 0)
2831 break;
2833 XSETINT (tem, hash);
2834 return tem;
2837 static int
2838 hash_string (ptr, len)
2839 unsigned char *ptr;
2840 int len;
2842 register unsigned char *p = ptr;
2843 register unsigned char *end = p + len;
2844 register unsigned char c;
2845 register int hash = 0;
2847 while (p != end)
2849 c = *p++;
2850 if (c >= 0140) c -= 40;
2851 hash = ((hash<<3) + (hash>>28) + c);
2853 return hash & 07777777777;
2856 void
2857 map_obarray (obarray, fn, arg)
2858 Lisp_Object obarray;
2859 void (*fn) P_ ((Lisp_Object, Lisp_Object));
2860 Lisp_Object arg;
2862 register int i;
2863 register Lisp_Object tail;
2864 CHECK_VECTOR (obarray, 1);
2865 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
2867 tail = XVECTOR (obarray)->contents[i];
2868 if (SYMBOLP (tail))
2869 while (1)
2871 (*fn) (tail, arg);
2872 if (XSYMBOL (tail)->next == 0)
2873 break;
2874 XSETSYMBOL (tail, XSYMBOL (tail)->next);
2879 void
2880 mapatoms_1 (sym, function)
2881 Lisp_Object sym, function;
2883 call1 (function, sym);
2886 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
2887 "Call FUNCTION on every symbol in OBARRAY.\n\
2888 OBARRAY defaults to the value of `obarray'.")
2889 (function, obarray)
2890 Lisp_Object function, obarray;
2892 if (NILP (obarray)) obarray = Vobarray;
2893 obarray = check_obarray (obarray);
2895 map_obarray (obarray, mapatoms_1, function);
2896 return Qnil;
2899 #define OBARRAY_SIZE 1511
2901 void
2902 init_obarray ()
2904 Lisp_Object oblength;
2905 int hash;
2906 Lisp_Object *tem;
2908 XSETFASTINT (oblength, OBARRAY_SIZE);
2910 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
2911 Vobarray = Fmake_vector (oblength, make_number (0));
2912 initial_obarray = Vobarray;
2913 staticpro (&initial_obarray);
2914 /* Intern nil in the obarray */
2915 XSYMBOL (Qnil)->obarray = Vobarray;
2916 /* These locals are to kludge around a pyramid compiler bug. */
2917 hash = hash_string ("nil", 3);
2918 /* Separate statement here to avoid VAXC bug. */
2919 hash %= OBARRAY_SIZE;
2920 tem = &XVECTOR (Vobarray)->contents[hash];
2921 *tem = Qnil;
2923 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
2924 XSYMBOL (Qnil)->function = Qunbound;
2925 XSYMBOL (Qunbound)->value = Qunbound;
2926 XSYMBOL (Qunbound)->function = Qunbound;
2928 Qt = intern ("t");
2929 XSYMBOL (Qnil)->value = Qnil;
2930 XSYMBOL (Qnil)->plist = Qnil;
2931 XSYMBOL (Qt)->value = Qt;
2933 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2934 Vpurify_flag = Qt;
2936 Qvariable_documentation = intern ("variable-documentation");
2937 staticpro (&Qvariable_documentation);
2939 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
2940 read_buffer = (char *) malloc (read_buffer_size);
2943 void
2944 defsubr (sname)
2945 struct Lisp_Subr *sname;
2947 Lisp_Object sym;
2948 sym = intern (sname->symbol_name);
2949 XSETSUBR (XSYMBOL (sym)->function, sname);
2952 #ifdef NOTDEF /* use fset in subr.el now */
2953 void
2954 defalias (sname, string)
2955 struct Lisp_Subr *sname;
2956 char *string;
2958 Lisp_Object sym;
2959 sym = intern (string);
2960 XSETSUBR (XSYMBOL (sym)->function, sname);
2962 #endif /* NOTDEF */
2964 /* Define an "integer variable"; a symbol whose value is forwarded
2965 to a C variable of type int. Sample call: */
2966 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
2967 void
2968 defvar_int (namestring, address)
2969 char *namestring;
2970 int *address;
2972 Lisp_Object sym, val;
2973 sym = intern (namestring);
2974 val = allocate_misc ();
2975 XMISCTYPE (val) = Lisp_Misc_Intfwd;
2976 XINTFWD (val)->intvar = address;
2977 XSYMBOL (sym)->value = val;
2980 /* Similar but define a variable whose value is T if address contains 1,
2981 NIL if address contains 0 */
2982 void
2983 defvar_bool (namestring, address)
2984 char *namestring;
2985 int *address;
2987 Lisp_Object sym, val;
2988 sym = intern (namestring);
2989 val = allocate_misc ();
2990 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
2991 XBOOLFWD (val)->boolvar = address;
2992 XSYMBOL (sym)->value = val;
2993 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
2996 /* Similar but define a variable whose value is the Lisp Object stored
2997 at address. Two versions: with and without gc-marking of the C
2998 variable. The nopro version is used when that variable will be
2999 gc-marked for some other reason, since marking the same slot twice
3000 can cause trouble with strings. */
3001 void
3002 defvar_lisp_nopro (namestring, address)
3003 char *namestring;
3004 Lisp_Object *address;
3006 Lisp_Object sym, val;
3007 sym = intern (namestring);
3008 val = allocate_misc ();
3009 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3010 XOBJFWD (val)->objvar = address;
3011 XSYMBOL (sym)->value = val;
3014 void
3015 defvar_lisp (namestring, address)
3016 char *namestring;
3017 Lisp_Object *address;
3019 defvar_lisp_nopro (namestring, address);
3020 staticpro (address);
3023 #ifndef standalone
3025 /* Similar but define a variable whose value is the Lisp Object stored in
3026 the current buffer. address is the address of the slot in the buffer
3027 that is current now. */
3029 void
3030 defvar_per_buffer (namestring, address, type, doc)
3031 char *namestring;
3032 Lisp_Object *address;
3033 Lisp_Object type;
3034 char *doc;
3036 Lisp_Object sym, val;
3037 int offset;
3038 extern struct buffer buffer_local_symbols;
3040 sym = intern (namestring);
3041 val = allocate_misc ();
3042 offset = (char *)address - (char *)current_buffer;
3044 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3045 XBUFFER_OBJFWD (val)->offset = offset;
3046 XSYMBOL (sym)->value = val;
3047 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
3048 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
3049 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
3050 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3051 slot of buffer_local_flags */
3052 abort ();
3055 #endif /* standalone */
3057 /* Similar but define a variable whose value is the Lisp Object stored
3058 at a particular offset in the current kboard object. */
3060 void
3061 defvar_kboard (namestring, offset)
3062 char *namestring;
3063 int offset;
3065 Lisp_Object sym, val;
3066 sym = intern (namestring);
3067 val = allocate_misc ();
3068 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3069 XKBOARD_OBJFWD (val)->offset = offset;
3070 XSYMBOL (sym)->value = val;
3073 /* Record the value of load-path used at the start of dumping
3074 so we can see if the site changed it later during dumping. */
3075 static Lisp_Object dump_path;
3077 void
3078 init_lread ()
3080 char *normal;
3081 int turn_off_warning = 0;
3083 /* Compute the default load-path. */
3084 #ifdef CANNOT_DUMP
3085 normal = PATH_LOADSEARCH;
3086 Vload_path = decode_env_path (0, normal);
3087 #else
3088 if (NILP (Vpurify_flag))
3089 normal = PATH_LOADSEARCH;
3090 else
3091 normal = PATH_DUMPLOADSEARCH;
3093 /* In a dumped Emacs, we normally have to reset the value of
3094 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3095 uses ../lisp, instead of the path of the installed elisp
3096 libraries. However, if it appears that Vload_path was changed
3097 from the default before dumping, don't override that value. */
3098 if (initialized)
3100 if (! NILP (Fequal (dump_path, Vload_path)))
3102 Vload_path = decode_env_path (0, normal);
3103 if (!NILP (Vinstallation_directory))
3105 /* Add to the path the lisp subdir of the
3106 installation dir, if it exists. */
3107 Lisp_Object tem, tem1;
3108 tem = Fexpand_file_name (build_string ("lisp"),
3109 Vinstallation_directory);
3110 tem1 = Ffile_exists_p (tem);
3111 if (!NILP (tem1))
3113 if (NILP (Fmember (tem, Vload_path)))
3115 turn_off_warning = 1;
3116 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3119 else
3120 /* That dir doesn't exist, so add the build-time
3121 Lisp dirs instead. */
3122 Vload_path = nconc2 (Vload_path, dump_path);
3124 /* Add leim under the installation dir, if it exists. */
3125 tem = Fexpand_file_name (build_string ("leim"),
3126 Vinstallation_directory);
3127 tem1 = Ffile_exists_p (tem);
3128 if (!NILP (tem1))
3130 if (NILP (Fmember (tem, Vload_path)))
3131 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3134 /* Add site-list under the installation dir, if it exists. */
3135 tem = Fexpand_file_name (build_string ("site-lisp"),
3136 Vinstallation_directory);
3137 tem1 = Ffile_exists_p (tem);
3138 if (!NILP (tem1))
3140 if (NILP (Fmember (tem, Vload_path)))
3141 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3144 /* If Emacs was not built in the source directory,
3145 and it is run from where it was built, add to load-path
3146 the lisp, leim and site-lisp dirs under that directory. */
3148 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3150 Lisp_Object tem2;
3152 tem = Fexpand_file_name (build_string ("src/Makefile"),
3153 Vinstallation_directory);
3154 tem1 = Ffile_exists_p (tem);
3156 /* Don't be fooled if they moved the entire source tree
3157 AFTER dumping Emacs. If the build directory is indeed
3158 different from the source dir, src/Makefile.in and
3159 src/Makefile will not be found together. */
3160 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3161 Vinstallation_directory);
3162 tem2 = Ffile_exists_p (tem);
3163 if (!NILP (tem1) && NILP (tem2))
3165 tem = Fexpand_file_name (build_string ("lisp"),
3166 Vsource_directory);
3168 if (NILP (Fmember (tem, Vload_path)))
3169 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3171 tem = Fexpand_file_name (build_string ("leim"),
3172 Vsource_directory);
3174 if (NILP (Fmember (tem, Vload_path)))
3175 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3177 tem = Fexpand_file_name (build_string ("site-lisp"),
3178 Vsource_directory);
3180 if (NILP (Fmember (tem, Vload_path)))
3181 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3187 else
3189 /* NORMAL refers to the lisp dir in the source directory. */
3190 /* We used to add ../lisp at the front here, but
3191 that caused trouble because it was copied from dump_path
3192 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3193 It should be unnecessary. */
3194 Vload_path = decode_env_path (0, normal);
3195 dump_path = Vload_path;
3197 #endif
3199 #ifndef WINDOWSNT
3200 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3201 almost never correct, thereby causing a warning to be printed out that
3202 confuses users. Since PATH_LOADSEARCH is always overridden by the
3203 EMACSLOADPATH environment variable below, disable the warning on NT. */
3205 /* Warn if dirs in the *standard* path don't exist. */
3206 if (!turn_off_warning)
3208 Lisp_Object path_tail;
3210 for (path_tail = Vload_path;
3211 !NILP (path_tail);
3212 path_tail = XCDR (path_tail))
3214 Lisp_Object dirfile;
3215 dirfile = Fcar (path_tail);
3216 if (STRINGP (dirfile))
3218 dirfile = Fdirectory_file_name (dirfile);
3219 if (access (XSTRING (dirfile)->data, 0) < 0)
3220 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3221 XCAR (path_tail));
3225 #endif /* WINDOWSNT */
3227 /* If the EMACSLOADPATH environment variable is set, use its value.
3228 This doesn't apply if we're dumping. */
3229 #ifndef CANNOT_DUMP
3230 if (NILP (Vpurify_flag)
3231 && egetenv ("EMACSLOADPATH"))
3232 #endif
3233 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3235 Vvalues = Qnil;
3237 load_in_progress = 0;
3238 Vload_file_name = Qnil;
3240 load_descriptor_list = Qnil;
3242 Vstandard_input = Qt;
3245 /* Print a warning, using format string FORMAT, that directory DIRNAME
3246 does not exist. Print it on stderr and put it in *Message*. */
3248 void
3249 dir_warning (format, dirname)
3250 char *format;
3251 Lisp_Object dirname;
3253 char *buffer
3254 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3256 fprintf (stderr, format, XSTRING (dirname)->data);
3257 sprintf (buffer, format, XSTRING (dirname)->data);
3258 /* Don't log the warning before we've initialized!! */
3259 if (initialized)
3260 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3263 void
3264 syms_of_lread ()
3266 defsubr (&Sread);
3267 defsubr (&Sread_from_string);
3268 defsubr (&Sintern);
3269 defsubr (&Sintern_soft);
3270 defsubr (&Sunintern);
3271 defsubr (&Sload);
3272 defsubr (&Seval_buffer);
3273 defsubr (&Seval_region);
3274 defsubr (&Sread_char);
3275 defsubr (&Sread_char_exclusive);
3276 defsubr (&Sread_event);
3277 defsubr (&Sget_file_char);
3278 defsubr (&Smapatoms);
3280 DEFVAR_LISP ("obarray", &Vobarray,
3281 "Symbol table for use by `intern' and `read'.\n\
3282 It is a vector whose length ought to be prime for best results.\n\
3283 The vector's contents don't make sense if examined from Lisp programs;\n\
3284 to find all the symbols in an obarray, use `mapatoms'.");
3286 DEFVAR_LISP ("values", &Vvalues,
3287 "List of values of all expressions which were read, evaluated and printed.\n\
3288 Order is reverse chronological.");
3290 DEFVAR_LISP ("standard-input", &Vstandard_input,
3291 "Stream for read to get input from.\n\
3292 See documentation of `read' for possible values.");
3293 Vstandard_input = Qt;
3295 DEFVAR_LISP ("load-path", &Vload_path,
3296 "*List of directories to search for files to load.\n\
3297 Each element is a string (directory name) or nil (try default directory).\n\
3298 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3299 otherwise to default specified by file `epaths.h' when Emacs was built.");
3301 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3302 "Non-nil iff inside of `load'.");
3304 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3305 "An alist of expressions to be evalled when particular files are loaded.\n\
3306 Each element looks like (FILENAME FORMS...).\n\
3307 When `load' is run and the file-name argument is FILENAME,\n\
3308 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3309 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3310 with no directory specified, since that is how `load' is normally called.\n\
3311 An error in FORMS does not undo the load,\n\
3312 but does prevent execution of the rest of the FORMS.");
3313 Vafter_load_alist = Qnil;
3315 DEFVAR_LISP ("load-history", &Vload_history,
3316 "Alist mapping source file names to symbols and features.\n\
3317 Each alist element is a list that starts with a file name,\n\
3318 except for one element (optional) that starts with nil and describes\n\
3319 definitions evaluated from buffers not visiting files.\n\
3320 The remaining elements of each list are symbols defined as functions\n\
3321 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
3322 Vload_history = Qnil;
3324 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3325 "Full name of file being loaded by `load'.");
3326 Vload_file_name = Qnil;
3328 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3329 "File name, including directory, of user's initialization file.\n\
3330 If the file loaded had extension `.elc' and there was a corresponding `.el'\n\
3331 file, this variable contains the name of the .el file, suitable for use\n\
3332 by functions like `custom-save-all' which edit the init file.");
3333 Vuser_init_file = Qnil;
3335 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3336 "Used for internal purposes by `load'.");
3337 Vcurrent_load_list = Qnil;
3339 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3340 "Function used by `load' and `eval-region' for reading expressions.\n\
3341 The default is nil, which means use the function `read'.");
3342 Vload_read_function = Qnil;
3344 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3345 "Function called in `load' for loading an Emacs lisp source file.\n\
3346 This function is for doing code conversion before reading the source file.\n\
3347 If nil, loading is done without any code conversion.\n\
3348 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3349 FULLNAME is the full name of FILE.\n\
3350 See `load' for the meaning of the remaining arguments.");
3351 Vload_source_file_function = Qnil;
3353 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3354 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3355 This is useful when the file being loaded is a temporary copy.");
3356 load_force_doc_strings = 0;
3358 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3359 "Non-nil means `load' converts strings to unibyte whenever possible.\n\
3360 This is normally used in `load-with-code-conversion'\n\
3361 for loading non-compiled files.");
3362 load_convert_to_unibyte = 0;
3364 DEFVAR_LISP ("source-directory", &Vsource_directory,
3365 "Directory in which Emacs sources were found when Emacs was built.\n\
3366 You cannot count on them to still be there!");
3367 Vsource_directory
3368 = Fexpand_file_name (build_string ("../"),
3369 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3371 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3372 "List of files that were preloaded (when dumping Emacs).");
3373 Vpreloaded_file_list = Qnil;
3375 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3376 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3377 Vbyte_boolean_vars = Qnil;
3379 /* Vsource_directory was initialized in init_lread. */
3381 load_descriptor_list = Qnil;
3382 staticpro (&load_descriptor_list);
3384 Qcurrent_load_list = intern ("current-load-list");
3385 staticpro (&Qcurrent_load_list);
3387 Qstandard_input = intern ("standard-input");
3388 staticpro (&Qstandard_input);
3390 Qread_char = intern ("read-char");
3391 staticpro (&Qread_char);
3393 Qget_file_char = intern ("get-file-char");
3394 staticpro (&Qget_file_char);
3396 Qbackquote = intern ("`");
3397 staticpro (&Qbackquote);
3398 Qcomma = intern (",");
3399 staticpro (&Qcomma);
3400 Qcomma_at = intern (",@");
3401 staticpro (&Qcomma_at);
3402 Qcomma_dot = intern (",.");
3403 staticpro (&Qcomma_dot);
3405 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3406 staticpro (&Qinhibit_file_name_operation);
3408 Qascii_character = intern ("ascii-character");
3409 staticpro (&Qascii_character);
3411 Qfunction = intern ("function");
3412 staticpro (&Qfunction);
3414 Qload = intern ("load");
3415 staticpro (&Qload);
3417 Qload_file_name = intern ("load-file-name");
3418 staticpro (&Qload_file_name);
3420 staticpro (&dump_path);
3422 staticpro (&read_objects);
3423 read_objects = Qnil;
3424 staticpro (&seen_list);