Fix a typo in the last entry for movemail.c.
[emacs.git] / src / lread.c
blob2e618633ae3fe44ff9fb38cfd55340cc6389e43a
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 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <errno.h>
29 #include "lisp.h"
30 #include "intervals.h"
31 #include "buffer.h"
32 #include "charset.h"
33 #include <epaths.h>
34 #include "commands.h"
35 #include "keyboard.h"
36 #include "termhooks.h"
38 #ifdef lint
39 #include <sys/inode.h>
40 #endif /* lint */
42 #ifdef MSDOS
43 #if __DJGPP__ < 2
44 #include <unistd.h> /* to get X_OK */
45 #endif
46 #include "msdos.h"
47 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
53 #ifndef X_OK
54 #define X_OK 01
55 #endif
57 #include <math.h>
59 #ifdef HAVE_SETLOCALE
60 #include <locale.h>
61 #endif /* HAVE_SETLOCALE */
63 #ifndef O_RDONLY
64 #define O_RDONLY 0
65 #endif
67 #ifdef HAVE_FTELLO
68 #define file_offset off_t
69 #define file_tell ftello
70 #else
71 #define file_offset long
72 #define file_tell ftell
73 #endif
75 #ifndef USE_CRT_DLL
76 extern int errno;
77 #endif
79 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
80 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
81 Lisp_Object Qascii_character, Qload, Qload_file_name;
82 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
83 Lisp_Object Qinhibit_file_name_operation;
85 extern Lisp_Object Qevent_symbol_element_mask;
86 extern Lisp_Object Qfile_exists_p;
88 /* non-zero if inside `load' */
89 int load_in_progress;
91 /* Directory in which the sources were found. */
92 Lisp_Object Vsource_directory;
94 /* Search path for files to be loaded. */
95 Lisp_Object Vload_path;
97 /* File name of user's init file. */
98 Lisp_Object Vuser_init_file;
100 /* This is the user-visible association list that maps features to
101 lists of defs in their load files. */
102 Lisp_Object Vload_history;
104 /* This is used to build the load history. */
105 Lisp_Object Vcurrent_load_list;
107 /* List of files that were preloaded. */
108 Lisp_Object Vpreloaded_file_list;
110 /* Name of file actually being read by `load'. */
111 Lisp_Object Vload_file_name;
113 /* Function to use for reading, in `load' and friends. */
114 Lisp_Object Vload_read_function;
116 /* The association list of objects read with the #n=object form.
117 Each member of the list has the form (n . object), and is used to
118 look up the object for the corresponding #n# construct.
119 It must be set to nil before all top-level calls to read0. */
120 Lisp_Object read_objects;
122 /* Nonzero means load should forcibly load all dynamic doc strings. */
123 static int load_force_doc_strings;
125 /* Nonzero means read should convert strings to unibyte. */
126 static int load_convert_to_unibyte;
128 /* Function to use for loading an Emacs lisp source file (not
129 compiled) instead of readevalloop. */
130 Lisp_Object Vload_source_file_function;
132 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
133 Lisp_Object Vbyte_boolean_vars;
135 /* List of descriptors now open for Fload. */
136 static Lisp_Object load_descriptor_list;
138 /* File for get_file_char to read from. Use by load. */
139 static FILE *instream;
141 /* When nonzero, read conses in pure space */
142 static int read_pure;
144 /* For use within read-from-string (this reader is non-reentrant!!) */
145 static int read_from_string_index;
146 static int read_from_string_index_byte;
147 static int read_from_string_limit;
149 /* Number of bytes left to read in the buffer character
150 that `readchar' has already advanced over. */
151 static int readchar_backlog;
153 /* This contains the last string skipped with #@. */
154 static char *saved_doc_string;
155 /* Length of buffer allocated in saved_doc_string. */
156 static int saved_doc_string_size;
157 /* Length of actual data in saved_doc_string. */
158 static int saved_doc_string_length;
159 /* This is the file position that string came from. */
160 static file_offset saved_doc_string_position;
162 /* This contains the previous string skipped with #@.
163 We copy it from saved_doc_string when a new string
164 is put in saved_doc_string. */
165 static char *prev_saved_doc_string;
166 /* Length of buffer allocated in prev_saved_doc_string. */
167 static int prev_saved_doc_string_size;
168 /* Length of actual data in prev_saved_doc_string. */
169 static int prev_saved_doc_string_length;
170 /* This is the file position that string came from. */
171 static file_offset prev_saved_doc_string_position;
173 /* Nonzero means inside a new-style backquote
174 with no surrounding parentheses.
175 Fread initializes this to zero, so we need not specbind it
176 or worry about what happens to it when there is an error. */
177 static int new_backquote_flag;
179 /* Handle unreading and rereading of characters.
180 Write READCHAR to read a character,
181 UNREAD(c) to unread c to be read again.
183 These macros actually read/unread a byte code, multibyte characters
184 are not handled here. The caller should manage them if necessary.
187 #define READCHAR readchar (readcharfun)
188 #define UNREAD(c) unreadchar (readcharfun, c)
190 static int
191 readchar (readcharfun)
192 Lisp_Object readcharfun;
194 Lisp_Object tem;
195 register int c;
197 if (BUFFERP (readcharfun))
199 register struct buffer *inbuffer = XBUFFER (readcharfun);
201 int pt_byte = BUF_PT_BYTE (inbuffer);
202 int orig_pt_byte = pt_byte;
204 if (readchar_backlog > 0)
205 /* We get the address of the byte just passed,
206 which is the last byte of the character.
207 The other bytes in this character are consecutive with it,
208 because the gap can't be in the middle of a character. */
209 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
210 - --readchar_backlog);
212 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
213 return -1;
215 readchar_backlog = -1;
217 if (! NILP (inbuffer->enable_multibyte_characters))
219 /* Fetch the character code from the buffer. */
220 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
221 BUF_INC_POS (inbuffer, pt_byte);
222 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
224 else
226 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
227 pt_byte++;
229 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
231 return c;
233 if (MARKERP (readcharfun))
235 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
237 int bytepos = marker_byte_position (readcharfun);
238 int orig_bytepos = bytepos;
240 if (readchar_backlog > 0)
241 /* We get the address of the byte just passed,
242 which is the last byte of the character.
243 The other bytes in this character are consecutive with it,
244 because the gap can't be in the middle of a character. */
245 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
246 - --readchar_backlog);
248 if (bytepos >= BUF_ZV_BYTE (inbuffer))
249 return -1;
251 readchar_backlog = -1;
253 if (! NILP (inbuffer->enable_multibyte_characters))
255 /* Fetch the character code from the buffer. */
256 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
257 BUF_INC_POS (inbuffer, bytepos);
258 c = STRING_CHAR (p, bytepos - orig_bytepos);
260 else
262 c = BUF_FETCH_BYTE (inbuffer, bytepos);
263 bytepos++;
266 XMARKER (readcharfun)->bytepos = bytepos;
267 XMARKER (readcharfun)->charpos++;
269 return c;
272 if (EQ (readcharfun, Qlambda))
273 return read_bytecode_char (0);
275 if (EQ (readcharfun, Qget_file_char))
277 c = getc (instream);
278 #ifdef EINTR
279 /* Interrupted reads have been observed while reading over the network */
280 while (c == EOF && ferror (instream) && errno == EINTR)
282 clearerr (instream);
283 c = getc (instream);
285 #endif
286 return c;
289 if (STRINGP (readcharfun))
291 if (read_from_string_index >= read_from_string_limit)
292 c = -1;
293 else
294 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
295 read_from_string_index,
296 read_from_string_index_byte);
298 return c;
301 tem = call0 (readcharfun);
303 if (NILP (tem))
304 return -1;
305 return XINT (tem);
308 /* Unread the character C in the way appropriate for the stream READCHARFUN.
309 If the stream is a user function, call it with the char as argument. */
311 static void
312 unreadchar (readcharfun, c)
313 Lisp_Object readcharfun;
314 int c;
316 if (c == -1)
317 /* Don't back up the pointer if we're unreading the end-of-input mark,
318 since readchar didn't advance it when we read it. */
320 else if (BUFFERP (readcharfun))
322 struct buffer *b = XBUFFER (readcharfun);
323 int bytepos = BUF_PT_BYTE (b);
325 if (readchar_backlog >= 0)
326 readchar_backlog++;
327 else
329 BUF_PT (b)--;
330 if (! NILP (b->enable_multibyte_characters))
331 BUF_DEC_POS (b, bytepos);
332 else
333 bytepos--;
335 BUF_PT_BYTE (b) = bytepos;
338 else if (MARKERP (readcharfun))
340 struct buffer *b = XMARKER (readcharfun)->buffer;
341 int bytepos = XMARKER (readcharfun)->bytepos;
343 if (readchar_backlog >= 0)
344 readchar_backlog++;
345 else
347 XMARKER (readcharfun)->charpos--;
348 if (! NILP (b->enable_multibyte_characters))
349 BUF_DEC_POS (b, bytepos);
350 else
351 bytepos--;
353 XMARKER (readcharfun)->bytepos = bytepos;
356 else if (STRINGP (readcharfun))
358 read_from_string_index--;
359 read_from_string_index_byte
360 = string_char_to_byte (readcharfun, read_from_string_index);
362 else if (EQ (readcharfun, Qlambda))
363 read_bytecode_char (1);
364 else if (EQ (readcharfun, Qget_file_char))
365 ungetc (c, instream);
366 else
367 call1 (readcharfun, make_number (c));
370 static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
371 static int read_multibyte ();
372 static Lisp_Object substitute_object_recurse ();
373 static void substitute_object_in_subtree (), substitute_in_interval ();
376 /* Get a character from the tty. */
378 extern Lisp_Object read_char ();
380 /* Read input events until we get one that's acceptable for our purposes.
382 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
383 until we get a character we like, and then stuffed into
384 unread_switch_frame.
386 If ASCII_REQUIRED is non-zero, we check function key events to see
387 if the unmodified version of the symbol has a Qascii_character
388 property, and use that character, if present.
390 If ERROR_NONASCII is non-zero, we signal an error if the input we
391 get isn't an ASCII character with modifiers. If it's zero but
392 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
393 character.
395 If INPUT_METHOD is nonzero, we invoke the current input method
396 if the character warrants that. */
398 Lisp_Object
399 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
400 input_method)
401 int no_switch_frame, ascii_required, error_nonascii, input_method;
403 register Lisp_Object val, delayed_switch_frame;
405 #ifdef HAVE_WINDOW_SYSTEM
406 if (display_busy_cursor_p)
407 cancel_busy_cursor ();
408 #endif
410 delayed_switch_frame = Qnil;
412 /* Read until we get an acceptable event. */
413 retry:
414 val = read_char (0, 0, 0,
415 (input_method ? Qnil : Qt),
418 if (BUFFERP (val))
419 goto retry;
421 /* switch-frame events are put off until after the next ASCII
422 character. This is better than signaling an error just because
423 the last characters were typed to a separate minibuffer frame,
424 for example. Eventually, some code which can deal with
425 switch-frame events will read it and process it. */
426 if (no_switch_frame
427 && EVENT_HAS_PARAMETERS (val)
428 && EQ (EVENT_HEAD (val), Qswitch_frame))
430 delayed_switch_frame = val;
431 goto retry;
434 if (ascii_required)
436 /* Convert certain symbols to their ASCII equivalents. */
437 if (SYMBOLP (val))
439 Lisp_Object tem, tem1;
440 tem = Fget (val, Qevent_symbol_element_mask);
441 if (!NILP (tem))
443 tem1 = Fget (Fcar (tem), Qascii_character);
444 /* Merge this symbol's modifier bits
445 with the ASCII equivalent of its basic code. */
446 if (!NILP (tem1))
447 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
451 /* If we don't have a character now, deal with it appropriately. */
452 if (!INTEGERP (val))
454 if (error_nonascii)
456 Vunread_command_events = Fcons (val, Qnil);
457 error ("Non-character input-event");
459 else
460 goto retry;
464 if (! NILP (delayed_switch_frame))
465 unread_switch_frame = delayed_switch_frame;
467 #ifdef HAVE_WINDOW_SYSTEM
468 if (display_busy_cursor_p)
469 start_busy_cursor ();
470 #endif
471 return val;
474 DEFUN ("read-char", Fread_char, Sread_char, 0, 2, 0,
475 "Read a character from the command input (keyboard or macro).\n\
476 It is returned as a number.\n\
477 If the user generates an event which is not a character (i.e. a mouse\n\
478 click or function key event), `read-char' signals an error. As an\n\
479 exception, switch-frame events are put off until non-ASCII events can\n\
480 be read.\n\
481 If you want to read non-character events, or ignore them, call\n\
482 `read-event' or `read-char-exclusive' instead.\n\
484 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
485 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
486 input method is turned on in the current buffer, that input method\n\
487 is used for reading a character.")
488 (prompt, inherit_input_method)
489 Lisp_Object prompt, inherit_input_method;
491 if (! NILP (prompt))
492 message_with_string ("%s", prompt, 0);
493 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method));
496 DEFUN ("read-event", Fread_event, Sread_event, 0, 2, 0,
497 "Read an event object from the input stream.\n\
498 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
499 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
500 input method is turned on in the current buffer, that input method\n\
501 is used for reading a character.")
502 (prompt, inherit_input_method)
503 Lisp_Object prompt, inherit_input_method;
505 if (! NILP (prompt))
506 message_with_string ("%s", prompt, 0);
507 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method));
510 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 2, 0,
511 "Read a character from the command input (keyboard or macro).\n\
512 It is returned as a number. Non-character events are ignored.\n\
514 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
515 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
516 input method is turned on in the current buffer, that input method\n\
517 is used for reading a character.")
518 (prompt, inherit_input_method)
519 Lisp_Object prompt, inherit_input_method;
521 if (! NILP (prompt))
522 message_with_string ("%s", prompt, 0);
523 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method));
526 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
527 "Don't use this yourself.")
530 register Lisp_Object val;
531 XSETINT (val, getc (instream));
532 return val;
535 static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
536 Lisp_Object (*) (), int,
537 Lisp_Object, Lisp_Object));
538 static Lisp_Object load_unwind P_ ((Lisp_Object));
539 static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
541 /* Non-zero means load dangerous compiled Lisp files. */
543 int load_dangerous_libraries;
545 /* A regular expression used to detect files compiled with Emacs. */
547 static Lisp_Object Vbytecomp_version_regexp;
550 /* Value is non-zero if the file asswociated with file descriptor FD
551 is a compiled Lisp file that's safe to load. Only files compiled
552 with Emacs are safe to load. Files compiled with XEmacs can lead
553 to a crash in Fbyte_code because of an incompatible change in the
554 byte compiler. */
556 static int
557 safe_to_load_p (fd)
558 int fd;
560 char buf[512];
561 int nbytes, i;
562 int safe_p = 1;
564 /* Read the first few bytes from the file, and look for a line
565 specifying the byte compiler version used. */
566 nbytes = emacs_read (fd, buf, sizeof buf - 1);
567 if (nbytes > 0)
569 buf[nbytes] = '\0';
571 /* Skip to the next newline, skipping over the initial `ELC'
572 with NUL bytes following it. */
573 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
576 if (i < nbytes
577 && fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
578 buf + i) < 0)
579 safe_p = 0;
582 lseek (fd, 0, SEEK_SET);
583 return safe_p;
587 DEFUN ("load", Fload, Sload, 1, 5, 0,
588 "Execute a file of Lisp code named FILE.\n\
589 First try FILE with `.elc' appended, then try with `.el',\n\
590 then try FILE unmodified.\n\
591 This function searches the directories in `load-path'.\n\
592 If optional second arg NOERROR is non-nil,\n\
593 report no error if FILE doesn't exist.\n\
594 Print messages at start and end of loading unless\n\
595 optional third arg NOMESSAGE is non-nil.\n\
596 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
597 suffixes `.elc' or `.el' to the specified name FILE.\n\
598 If optional fifth arg MUST-SUFFIX is non-nil, insist on\n\
599 the suffix `.elc' or `.el'; don't accept just FILE unless\n\
600 it ends in one of those suffixes or includes a directory name.\n\
601 Return t if file exists.")
602 (file, noerror, nomessage, nosuffix, must_suffix)
603 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
605 register FILE *stream;
606 register int fd = -1;
607 register Lisp_Object lispstream;
608 int count = specpdl_ptr - specpdl;
609 Lisp_Object temp;
610 struct gcpro gcpro1;
611 Lisp_Object found;
612 /* 1 means we printed the ".el is newer" message. */
613 int newer = 0;
614 /* 1 means we are loading a compiled file. */
615 int compiled = 0;
616 Lisp_Object handler;
617 int safe_p = 1;
618 char *fmode = "r";
619 #ifdef DOS_NT
620 fmode = "rt";
621 #endif /* DOS_NT */
623 CHECK_STRING (file, 0);
625 /* If file name is magic, call the handler. */
626 handler = Ffind_file_name_handler (file, Qload);
627 if (!NILP (handler))
628 return call5 (handler, Qload, file, noerror, nomessage, nosuffix);
630 /* Do this after the handler to avoid
631 the need to gcpro noerror, nomessage and nosuffix.
632 (Below here, we care only whether they are nil or not.) */
633 file = Fsubstitute_in_file_name (file);
635 /* Avoid weird lossage with null string as arg,
636 since it would try to load a directory as a Lisp file */
637 if (XSTRING (file)->size > 0)
639 int size = STRING_BYTES (XSTRING (file));
641 GCPRO1 (file);
643 if (! NILP (must_suffix))
645 /* Don't insist on adding a suffix if FILE already ends with one. */
646 if (size > 3
647 && !strcmp (XSTRING (file)->data + size - 3, ".el"))
648 must_suffix = Qnil;
649 else if (size > 4
650 && !strcmp (XSTRING (file)->data + size - 4, ".elc"))
651 must_suffix = Qnil;
652 /* Don't insist on adding a suffix
653 if the argument includes a directory name. */
654 else if (! NILP (Ffile_name_directory (file)))
655 must_suffix = Qnil;
658 fd = openp (Vload_path, file,
659 (!NILP (nosuffix) ? ""
660 : ! NILP (must_suffix) ? ".elc.gz:.elc:.el.gz:.el"
661 : ".elc:.elc.gz:.el.gz:.el:"),
662 &found, 0);
663 UNGCPRO;
666 if (fd < 0)
668 if (NILP (noerror))
669 while (1)
670 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
671 Fcons (file, Qnil)));
672 else
673 return Qnil;
676 /* Tell startup.el whether or not we found the user's init file. */
677 if (EQ (Qt, Vuser_init_file))
678 Vuser_init_file = found;
680 /* If FD is 0, that means openp found a magic file. */
681 if (fd == 0)
683 if (NILP (Fequal (found, file)))
684 /* If FOUND is a different file name from FILE,
685 find its handler even if we have already inhibited
686 the `load' operation on FILE. */
687 handler = Ffind_file_name_handler (found, Qt);
688 else
689 handler = Ffind_file_name_handler (found, Qload);
690 if (! NILP (handler))
691 return call5 (handler, Qload, found, noerror, nomessage, Qt);
694 /* Load .elc files directly, but not when they are
695 remote and have no handler! */
696 if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
697 ".elc", 4)
698 && fd != 0)
700 struct stat s1, s2;
701 int result;
703 if (!safe_to_load_p (fd))
705 safe_p = 0;
706 if (!load_dangerous_libraries)
707 error ("File `%s' was not compiled in Emacs",
708 XSTRING (found)->data);
709 else if (!NILP (nomessage))
710 message_with_string ("File `%s' not compiled in Emacs", found, 1);
713 compiled = 1;
715 #ifdef DOS_NT
716 fmode = "rb";
717 #endif /* DOS_NT */
718 stat ((char *)XSTRING (found)->data, &s1);
719 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
720 result = stat ((char *)XSTRING (found)->data, &s2);
721 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
723 /* Make the progress messages mention that source is newer. */
724 newer = 1;
726 /* If we won't print another message, mention this anyway. */
727 if (! NILP (nomessage))
728 message_with_string ("Source file `%s' newer than byte-compiled file",
729 found, 1);
731 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
733 else
735 load_source:
737 /* We are loading a source file (*.el). */
738 if (!NILP (Vload_source_file_function))
740 if (fd != 0)
741 emacs_close (fd);
742 return call4 (Vload_source_file_function, found, file,
743 NILP (noerror) ? Qnil : Qt,
744 NILP (nomessage) ? Qnil : Qt);
748 #ifdef WINDOWSNT
749 emacs_close (fd);
750 stream = fopen ((char *) XSTRING (found)->data, fmode);
751 #else /* not WINDOWSNT */
752 stream = fdopen (fd, fmode);
753 #endif /* not WINDOWSNT */
754 if (stream == 0)
756 emacs_close (fd);
757 error ("Failure to create stdio stream for %s", XSTRING (file)->data);
760 if (! NILP (Vpurify_flag))
761 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
763 if (NILP (nomessage))
765 if (!safe_p)
766 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
767 file, 1);
768 else if (!compiled)
769 message_with_string ("Loading %s (source)...", file, 1);
770 else if (newer)
771 message_with_string ("Loading %s (compiled; note, source file is newer)...",
772 file, 1);
773 else /* The typical case; compiled file newer than source file. */
774 message_with_string ("Loading %s...", file, 1);
777 GCPRO1 (file);
778 lispstream = Fcons (Qnil, Qnil);
779 XSETFASTINT (XCAR (lispstream), (EMACS_UINT)stream >> 16);
780 XSETFASTINT (XCDR (lispstream), (EMACS_UINT)stream & 0xffff);
781 record_unwind_protect (load_unwind, lispstream);
782 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
783 specbind (Qload_file_name, found);
784 specbind (Qinhibit_file_name_operation, Qnil);
785 load_descriptor_list
786 = Fcons (make_number (fileno (stream)), load_descriptor_list);
787 load_in_progress++;
788 readevalloop (Qget_file_char, stream, file, Feval, 0, Qnil, Qnil);
789 unbind_to (count, Qnil);
791 /* Run any load-hooks for this file. */
792 temp = Fassoc (file, Vafter_load_alist);
793 if (!NILP (temp))
794 Fprogn (Fcdr (temp));
795 UNGCPRO;
797 if (saved_doc_string)
798 free (saved_doc_string);
799 saved_doc_string = 0;
800 saved_doc_string_size = 0;
802 if (prev_saved_doc_string)
803 xfree (prev_saved_doc_string);
804 prev_saved_doc_string = 0;
805 prev_saved_doc_string_size = 0;
807 if (!noninteractive && NILP (nomessage))
809 if (!safe_p)
810 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
811 file, 1);
812 else if (!compiled)
813 message_with_string ("Loading %s (source)...done", file, 1);
814 else if (newer)
815 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
816 file, 1);
817 else /* The typical case; compiled file newer than source file. */
818 message_with_string ("Loading %s...done", file, 1);
820 return Qt;
823 static Lisp_Object
824 load_unwind (stream) /* used as unwind-protect function in load */
825 Lisp_Object stream;
827 fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16
828 | XFASTINT (XCDR (stream))));
829 if (--load_in_progress < 0) load_in_progress = 0;
830 return Qnil;
833 static Lisp_Object
834 load_descriptor_unwind (oldlist)
835 Lisp_Object oldlist;
837 load_descriptor_list = oldlist;
838 return Qnil;
841 /* Close all descriptors in use for Floads.
842 This is used when starting a subprocess. */
844 void
845 close_load_descs ()
847 #ifndef WINDOWSNT
848 Lisp_Object tail;
849 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
850 emacs_close (XFASTINT (XCAR (tail)));
851 #endif
854 static int
855 complete_filename_p (pathname)
856 Lisp_Object pathname;
858 register unsigned char *s = XSTRING (pathname)->data;
859 return (IS_DIRECTORY_SEP (s[0])
860 || (XSTRING (pathname)->size > 2
861 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
862 #ifdef ALTOS
863 || *s == '@'
864 #endif
865 #ifdef VMS
866 || index (s, ':')
867 #endif /* VMS */
871 /* Search for a file whose name is STR, looking in directories
872 in the Lisp list PATH, and trying suffixes from SUFFIX.
873 SUFFIX is a string containing possible suffixes separated by colons.
874 On success, returns a file descriptor. On failure, returns -1.
876 EXEC_ONLY nonzero means don't open the files,
877 just look for one that is executable. In this case,
878 returns 1 on success.
880 If STOREPTR is nonzero, it points to a slot where the name of
881 the file actually found should be stored as a Lisp string.
882 nil is stored there on failure.
884 If the file we find is remote, return 0
885 but store the found remote file name in *STOREPTR.
886 We do not check for remote files if EXEC_ONLY is nonzero. */
889 openp (path, str, suffix, storeptr, exec_only)
890 Lisp_Object path, str;
891 char *suffix;
892 Lisp_Object *storeptr;
893 int exec_only;
895 register int fd;
896 int fn_size = 100;
897 char buf[100];
898 register char *fn = buf;
899 int absolute = 0;
900 int want_size;
901 Lisp_Object filename;
902 struct stat st;
903 struct gcpro gcpro1, gcpro2, gcpro3;
904 Lisp_Object string;
906 string = filename = Qnil;
907 GCPRO3 (str, string, filename);
909 if (storeptr)
910 *storeptr = Qnil;
912 if (complete_filename_p (str))
913 absolute = 1;
915 for (; !NILP (path); path = Fcdr (path))
917 char *nsuffix;
919 filename = Fexpand_file_name (str, Fcar (path));
920 if (!complete_filename_p (filename))
921 /* If there are non-absolute elts in PATH (eg ".") */
922 /* Of course, this could conceivably lose if luser sets
923 default-directory to be something non-absolute... */
925 filename = Fexpand_file_name (filename, current_buffer->directory);
926 if (!complete_filename_p (filename))
927 /* Give up on this path element! */
928 continue;
931 /* Calculate maximum size of any filename made from
932 this path element/specified file name and any possible suffix. */
933 want_size = strlen (suffix) + STRING_BYTES (XSTRING (filename)) + 1;
934 if (fn_size < want_size)
935 fn = (char *) alloca (fn_size = 100 + want_size);
937 nsuffix = suffix;
939 /* Loop over suffixes. */
940 while (1)
942 char *esuffix = (char *) index (nsuffix, ':');
943 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
944 Lisp_Object handler;
946 /* Concatenate path element/specified name with the suffix.
947 If the directory starts with /:, remove that. */
948 if (XSTRING (filename)->size > 2
949 && XSTRING (filename)->data[0] == '/'
950 && XSTRING (filename)->data[1] == ':')
952 strncpy (fn, XSTRING (filename)->data + 2,
953 STRING_BYTES (XSTRING (filename)) - 2);
954 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
956 else
958 strncpy (fn, XSTRING (filename)->data,
959 STRING_BYTES (XSTRING (filename)));
960 fn[STRING_BYTES (XSTRING (filename))] = 0;
963 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
964 strncat (fn, nsuffix, lsuffix);
966 /* Check that the file exists and is not a directory. */
967 if (absolute)
968 handler = Qnil;
969 else
970 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
971 if (! NILP (handler) && ! exec_only)
973 int exists;
975 string = build_string (fn);
976 exists = ! NILP (exec_only ? Ffile_executable_p (string)
977 : Ffile_readable_p (string));
978 if (exists
979 && ! NILP (Ffile_directory_p (build_string (fn))))
980 exists = 0;
982 if (exists)
984 /* We succeeded; return this descriptor and filename. */
985 if (storeptr)
986 *storeptr = build_string (fn);
987 UNGCPRO;
988 return 0;
991 else
993 int exists = (stat (fn, &st) >= 0
994 && (st.st_mode & S_IFMT) != S_IFDIR);
995 if (exists)
997 /* Check that we can access or open it. */
998 if (exec_only)
999 fd = (access (fn, X_OK) == 0) ? 1 : -1;
1000 else
1001 fd = emacs_open (fn, O_RDONLY, 0);
1003 if (fd >= 0)
1005 /* We succeeded; return this descriptor and filename. */
1006 if (storeptr)
1007 *storeptr = build_string (fn);
1008 UNGCPRO;
1009 return fd;
1014 /* Advance to next suffix. */
1015 if (esuffix == 0)
1016 break;
1017 nsuffix += lsuffix + 1;
1019 if (absolute)
1020 break;
1023 UNGCPRO;
1024 return -1;
1028 /* Merge the list we've accumulated of globals from the current input source
1029 into the load_history variable. The details depend on whether
1030 the source has an associated file name or not. */
1032 static void
1033 build_load_history (stream, source)
1034 FILE *stream;
1035 Lisp_Object source;
1037 register Lisp_Object tail, prev, newelt;
1038 register Lisp_Object tem, tem2;
1039 register int foundit, loading;
1041 loading = stream || !NARROWED;
1043 tail = Vload_history;
1044 prev = Qnil;
1045 foundit = 0;
1046 while (!NILP (tail))
1048 tem = Fcar (tail);
1050 /* Find the feature's previous assoc list... */
1051 if (!NILP (Fequal (source, Fcar (tem))))
1053 foundit = 1;
1055 /* If we're loading, remove it. */
1056 if (loading)
1058 if (NILP (prev))
1059 Vload_history = Fcdr (tail);
1060 else
1061 Fsetcdr (prev, Fcdr (tail));
1064 /* Otherwise, cons on new symbols that are not already members. */
1065 else
1067 tem2 = Vcurrent_load_list;
1069 while (CONSP (tem2))
1071 newelt = Fcar (tem2);
1073 if (NILP (Fmemq (newelt, tem)))
1074 Fsetcar (tail, Fcons (Fcar (tem),
1075 Fcons (newelt, Fcdr (tem))));
1077 tem2 = Fcdr (tem2);
1078 QUIT;
1082 else
1083 prev = tail;
1084 tail = Fcdr (tail);
1085 QUIT;
1088 /* If we're loading, cons the new assoc onto the front of load-history,
1089 the most-recently-loaded position. Also do this if we didn't find
1090 an existing member for the current source. */
1091 if (loading || !foundit)
1092 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1093 Vload_history);
1096 Lisp_Object
1097 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1098 Lisp_Object junk;
1100 read_pure = 0;
1101 return Qnil;
1104 static Lisp_Object
1105 readevalloop_1 (old)
1106 Lisp_Object old;
1108 load_convert_to_unibyte = ! NILP (old);
1109 return Qnil;
1112 /* Signal an `end-of-file' error, if possible with file name
1113 information. */
1115 static void
1116 end_of_file_error ()
1118 Lisp_Object data;
1120 if (STRINGP (Vload_file_name))
1121 data = Fcons (Vload_file_name, Qnil);
1122 else
1123 data = Qnil;
1125 Fsignal (Qend_of_file, data);
1128 /* UNIBYTE specifies how to set load_convert_to_unibyte
1129 for this invocation.
1130 READFUN, if non-nil, is used instead of `read'. */
1132 static void
1133 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1134 Lisp_Object readcharfun;
1135 FILE *stream;
1136 Lisp_Object sourcename;
1137 Lisp_Object (*evalfun) ();
1138 int printflag;
1139 Lisp_Object unibyte, readfun;
1141 register int c;
1142 register Lisp_Object val;
1143 int count = specpdl_ptr - specpdl;
1144 struct gcpro gcpro1;
1145 struct buffer *b = 0;
1146 int continue_reading_p;
1148 if (BUFFERP (readcharfun))
1149 b = XBUFFER (readcharfun);
1150 else if (MARKERP (readcharfun))
1151 b = XMARKER (readcharfun)->buffer;
1153 specbind (Qstandard_input, readcharfun);
1154 specbind (Qcurrent_load_list, Qnil);
1155 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1156 load_convert_to_unibyte = !NILP (unibyte);
1158 readchar_backlog = -1;
1160 GCPRO1 (sourcename);
1162 LOADHIST_ATTACH (sourcename);
1164 continue_reading_p = 1;
1165 while (continue_reading_p)
1167 if (b != 0 && NILP (b->name))
1168 error ("Reading from killed buffer");
1170 instream = stream;
1171 c = READCHAR;
1172 if (c == ';')
1174 while ((c = READCHAR) != '\n' && c != -1);
1175 continue;
1177 if (c < 0) break;
1179 /* Ignore whitespace here, so we can detect eof. */
1180 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1181 continue;
1183 if (!NILP (Vpurify_flag) && c == '(')
1185 int count1 = specpdl_ptr - specpdl;
1186 record_unwind_protect (unreadpure, Qnil);
1187 val = read_list (-1, readcharfun);
1188 unbind_to (count1, Qnil);
1190 else
1192 UNREAD (c);
1193 read_objects = Qnil;
1194 if (!NILP (readfun))
1196 val = call1 (readfun, readcharfun);
1198 /* If READCHARFUN has set point to ZV, we should
1199 stop reading, even if the form read sets point
1200 to a different value when evaluated. */
1201 if (BUFFERP (readcharfun))
1203 struct buffer *b = XBUFFER (readcharfun);
1204 if (BUF_PT (b) == BUF_ZV (b))
1205 continue_reading_p = 0;
1208 else if (! NILP (Vload_read_function))
1209 val = call1 (Vload_read_function, readcharfun);
1210 else
1211 val = read0 (readcharfun);
1214 val = (*evalfun) (val);
1216 if (printflag)
1218 Vvalues = Fcons (val, Vvalues);
1219 if (EQ (Vstandard_output, Qt))
1220 Fprin1 (val, Qnil);
1221 else
1222 Fprint (val, Qnil);
1226 build_load_history (stream, sourcename);
1227 UNGCPRO;
1229 unbind_to (count, Qnil);
1232 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1233 "Execute the current buffer as Lisp code.\n\
1234 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1235 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1236 PRINTFLAG controls printing of output:\n\
1237 nil means discard it; anything else is stream for print.\n\
1239 If the optional third argument FILENAME is non-nil,\n\
1240 it specifies the file name to use for `load-history'.\n\
1241 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1242 for this invocation.\n\
1244 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1245 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1247 This function preserves the position of point.")
1248 (buffer, printflag, filename, unibyte, do_allow_print)
1249 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1251 int count = specpdl_ptr - specpdl;
1252 Lisp_Object tem, buf;
1254 if (NILP (buffer))
1255 buf = Fcurrent_buffer ();
1256 else
1257 buf = Fget_buffer (buffer);
1258 if (NILP (buf))
1259 error ("No such buffer");
1261 if (NILP (printflag) && NILP (do_allow_print))
1262 tem = Qsymbolp;
1263 else
1264 tem = printflag;
1266 if (NILP (filename))
1267 filename = XBUFFER (buf)->filename;
1269 specbind (Qstandard_output, tem);
1270 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1271 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1272 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1273 unbind_to (count, Qnil);
1275 return Qnil;
1278 #if 0
1279 XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
1280 "Execute the current buffer as Lisp code.\n\
1281 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1282 nil means discard it; anything else is stream for print.\n\
1284 If there is no error, point does not move. If there is an error,\n\
1285 point remains at the end of the last character read from the buffer.")
1286 (printflag)
1287 Lisp_Object printflag;
1289 int count = specpdl_ptr - specpdl;
1290 Lisp_Object tem, cbuf;
1292 cbuf = Fcurrent_buffer ()
1294 if (NILP (printflag))
1295 tem = Qsymbolp;
1296 else
1297 tem = printflag;
1298 specbind (Qstandard_output, tem);
1299 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1300 SET_PT (BEGV);
1301 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1302 !NILP (printflag), Qnil, Qnil);
1303 return unbind_to (count, Qnil);
1305 #endif
1307 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1308 "Execute the region as Lisp code.\n\
1309 When called from programs, expects two arguments,\n\
1310 giving starting and ending indices in the current buffer\n\
1311 of the text to be executed.\n\
1312 Programs can pass third argument PRINTFLAG which controls output:\n\
1313 nil means discard it; anything else is stream for printing it.\n\
1314 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1315 instead of `read' to read each expression. It gets one argument\n\
1316 which is the input stream for reading characters.\n\
1318 This function does not move point.")
1319 (start, end, printflag, read_function)
1320 Lisp_Object start, end, printflag, read_function;
1322 int count = specpdl_ptr - specpdl;
1323 Lisp_Object tem, cbuf;
1325 cbuf = Fcurrent_buffer ();
1327 if (NILP (printflag))
1328 tem = Qsymbolp;
1329 else
1330 tem = printflag;
1331 specbind (Qstandard_output, tem);
1333 if (NILP (printflag))
1334 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1335 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1337 /* This both uses start and checks its type. */
1338 Fgoto_char (start);
1339 Fnarrow_to_region (make_number (BEGV), end);
1340 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1341 !NILP (printflag), Qnil, read_function);
1343 return unbind_to (count, Qnil);
1347 DEFUN ("read", Fread, Sread, 0, 1, 0,
1348 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1349 If STREAM is nil, use the value of `standard-input' (which see).\n\
1350 STREAM or the value of `standard-input' may be:\n\
1351 a buffer (read from point and advance it)\n\
1352 a marker (read from where it points and advance it)\n\
1353 a function (call it with no arguments for each character,\n\
1354 call it with a char as argument to push a char back)\n\
1355 a string (takes text from string, starting at the beginning)\n\
1356 t (read text line using minibuffer and use it, or read from\n\
1357 standard input in batch mode).")
1358 (stream)
1359 Lisp_Object stream;
1361 extern Lisp_Object Fread_minibuffer ();
1363 if (NILP (stream))
1364 stream = Vstandard_input;
1365 if (EQ (stream, Qt))
1366 stream = Qread_char;
1368 readchar_backlog = -1;
1369 new_backquote_flag = 0;
1370 read_objects = Qnil;
1372 if (EQ (stream, Qread_char))
1373 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1375 if (STRINGP (stream))
1376 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1378 return read0 (stream);
1381 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1382 "Read one Lisp expression which is represented as text by STRING.\n\
1383 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1384 START and END optionally delimit a substring of STRING from which to read;\n\
1385 they default to 0 and (length STRING) respectively.")
1386 (string, start, end)
1387 Lisp_Object string, start, end;
1389 int startval, endval;
1390 Lisp_Object tem;
1392 CHECK_STRING (string,0);
1394 if (NILP (end))
1395 endval = XSTRING (string)->size;
1396 else
1398 CHECK_NUMBER (end, 2);
1399 endval = XINT (end);
1400 if (endval < 0 || endval > XSTRING (string)->size)
1401 args_out_of_range (string, end);
1404 if (NILP (start))
1405 startval = 0;
1406 else
1408 CHECK_NUMBER (start, 1);
1409 startval = XINT (start);
1410 if (startval < 0 || startval > endval)
1411 args_out_of_range (string, start);
1414 read_from_string_index = startval;
1415 read_from_string_index_byte = string_char_to_byte (string, startval);
1416 read_from_string_limit = endval;
1418 new_backquote_flag = 0;
1419 read_objects = Qnil;
1421 tem = read0 (string);
1422 return Fcons (tem, make_number (read_from_string_index));
1425 /* Use this for recursive reads, in contexts where internal tokens
1426 are not allowed. */
1428 static Lisp_Object
1429 read0 (readcharfun)
1430 Lisp_Object readcharfun;
1432 register Lisp_Object val;
1433 int c;
1435 val = read1 (readcharfun, &c, 0);
1436 if (c)
1437 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1438 make_number (c)),
1439 Qnil));
1441 return val;
1444 static int read_buffer_size;
1445 static char *read_buffer;
1447 /* Read multibyte form and return it as a character. C is a first
1448 byte of multibyte form, and rest of them are read from
1449 READCHARFUN. */
1451 static int
1452 read_multibyte (c, readcharfun)
1453 register int c;
1454 Lisp_Object readcharfun;
1456 /* We need the actual character code of this multibyte
1457 characters. */
1458 unsigned char str[MAX_MULTIBYTE_LENGTH];
1459 int len = 0;
1461 str[len++] = c;
1462 while ((c = READCHAR) >= 0xA0
1463 && len < MAX_MULTIBYTE_LENGTH)
1464 str[len++] = c;
1465 UNREAD (c);
1466 return STRING_CHAR (str, len);
1469 /* Read a \-escape sequence, assuming we already read the `\'. */
1471 static int
1472 read_escape (readcharfun, stringp)
1473 Lisp_Object readcharfun;
1474 int stringp;
1476 register int c = READCHAR;
1477 switch (c)
1479 case -1:
1480 error ("End of file");
1482 case 'a':
1483 return '\007';
1484 case 'b':
1485 return '\b';
1486 case 'd':
1487 return 0177;
1488 case 'e':
1489 return 033;
1490 case 'f':
1491 return '\f';
1492 case 'n':
1493 return '\n';
1494 case 'r':
1495 return '\r';
1496 case 't':
1497 return '\t';
1498 case 'v':
1499 return '\v';
1500 case '\n':
1501 return -1;
1502 case ' ':
1503 if (stringp)
1504 return -1;
1505 return ' ';
1507 case 'M':
1508 c = READCHAR;
1509 if (c != '-')
1510 error ("Invalid escape character syntax");
1511 c = READCHAR;
1512 if (c == '\\')
1513 c = read_escape (readcharfun, 0);
1514 return c | meta_modifier;
1516 case 'S':
1517 c = READCHAR;
1518 if (c != '-')
1519 error ("Invalid escape character syntax");
1520 c = READCHAR;
1521 if (c == '\\')
1522 c = read_escape (readcharfun, 0);
1523 return c | shift_modifier;
1525 case 'H':
1526 c = READCHAR;
1527 if (c != '-')
1528 error ("Invalid escape character syntax");
1529 c = READCHAR;
1530 if (c == '\\')
1531 c = read_escape (readcharfun, 0);
1532 return c | hyper_modifier;
1534 case 'A':
1535 c = READCHAR;
1536 if (c != '-')
1537 error ("Invalid escape character syntax");
1538 c = READCHAR;
1539 if (c == '\\')
1540 c = read_escape (readcharfun, 0);
1541 return c | alt_modifier;
1543 case 's':
1544 c = READCHAR;
1545 if (c != '-')
1546 error ("Invalid escape character syntax");
1547 c = READCHAR;
1548 if (c == '\\')
1549 c = read_escape (readcharfun, 0);
1550 return c | super_modifier;
1552 case 'C':
1553 c = READCHAR;
1554 if (c != '-')
1555 error ("Invalid escape character syntax");
1556 case '^':
1557 c = READCHAR;
1558 if (c == '\\')
1559 c = read_escape (readcharfun, 0);
1560 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1561 return 0177 | (c & CHAR_MODIFIER_MASK);
1562 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1563 return c | ctrl_modifier;
1564 /* ASCII control chars are made from letters (both cases),
1565 as well as the non-letters within 0100...0137. */
1566 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1567 return (c & (037 | ~0177));
1568 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1569 return (c & (037 | ~0177));
1570 else
1571 return c | ctrl_modifier;
1573 case '0':
1574 case '1':
1575 case '2':
1576 case '3':
1577 case '4':
1578 case '5':
1579 case '6':
1580 case '7':
1581 /* An octal escape, as in ANSI C. */
1583 register int i = c - '0';
1584 register int count = 0;
1585 while (++count < 3)
1587 if ((c = READCHAR) >= '0' && c <= '7')
1589 i *= 8;
1590 i += c - '0';
1592 else
1594 UNREAD (c);
1595 break;
1598 return i;
1601 case 'x':
1602 /* A hex escape, as in ANSI C. */
1604 int i = 0;
1605 while (1)
1607 c = READCHAR;
1608 if (c >= '0' && c <= '9')
1610 i *= 16;
1611 i += c - '0';
1613 else if ((c >= 'a' && c <= 'f')
1614 || (c >= 'A' && c <= 'F'))
1616 i *= 16;
1617 if (c >= 'a' && c <= 'f')
1618 i += c - 'a' + 10;
1619 else
1620 i += c - 'A' + 10;
1622 else
1624 UNREAD (c);
1625 break;
1628 return i;
1631 default:
1632 if (BASE_LEADING_CODE_P (c))
1633 c = read_multibyte (c, readcharfun);
1634 return c;
1639 /* Read an integer in radix RADIX using READCHARFUN to read
1640 characters. RADIX must be in the interval [2..36]; if it isn't, a
1641 read error is signaled . Value is the integer read. Signals an
1642 error if encountering invalid read syntax or if RADIX is out of
1643 range. */
1645 static Lisp_Object
1646 read_integer (readcharfun, radix)
1647 Lisp_Object readcharfun;
1648 int radix;
1650 int number, ndigits, invalid_p, c, sign;
1652 if (radix < 2 || radix > 36)
1653 invalid_p = 1;
1654 else
1656 number = ndigits = invalid_p = 0;
1657 sign = 1;
1659 c = READCHAR;
1660 if (c == '-')
1662 c = READCHAR;
1663 sign = -1;
1665 else if (c == '+')
1666 c = READCHAR;
1668 while (c >= 0)
1670 int digit;
1672 if (c >= '0' && c <= '9')
1673 digit = c - '0';
1674 else if (c >= 'a' && c <= 'z')
1675 digit = c - 'a' + 10;
1676 else if (c >= 'A' && c <= 'Z')
1677 digit = c - 'A' + 10;
1678 else
1680 UNREAD (c);
1681 break;
1684 if (digit < 0 || digit >= radix)
1685 invalid_p = 1;
1687 number = radix * number + digit;
1688 ++ndigits;
1689 c = READCHAR;
1693 if (ndigits == 0 || invalid_p)
1695 char buf[50];
1696 sprintf (buf, "integer, radix %d", radix);
1697 Fsignal (Qinvalid_read_syntax, Fcons (build_string (buf), Qnil));
1700 return make_number (sign * number);
1704 /* If the next token is ')' or ']' or '.', we store that character
1705 in *PCH and the return value is not interesting. Else, we store
1706 zero in *PCH and we read and return one lisp object.
1708 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1710 static Lisp_Object
1711 read1 (readcharfun, pch, first_in_list)
1712 register Lisp_Object readcharfun;
1713 int *pch;
1714 int first_in_list;
1716 register int c;
1717 int uninterned_symbol = 0;
1719 *pch = 0;
1721 retry:
1723 c = READCHAR;
1724 if (c < 0)
1725 end_of_file_error ();
1727 switch (c)
1729 case '(':
1730 return read_list (0, readcharfun);
1732 case '[':
1733 return read_vector (readcharfun, 0);
1735 case ')':
1736 case ']':
1738 *pch = c;
1739 return Qnil;
1742 case '#':
1743 c = READCHAR;
1744 if (c == '^')
1746 c = READCHAR;
1747 if (c == '[')
1749 Lisp_Object tmp;
1750 tmp = read_vector (readcharfun, 0);
1751 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1752 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1753 error ("Invalid size char-table");
1754 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1755 XCHAR_TABLE (tmp)->top = Qt;
1756 return tmp;
1758 else if (c == '^')
1760 c = READCHAR;
1761 if (c == '[')
1763 Lisp_Object tmp;
1764 tmp = read_vector (readcharfun, 0);
1765 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1766 error ("Invalid size char-table");
1767 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1768 XCHAR_TABLE (tmp)->top = Qnil;
1769 return tmp;
1771 Fsignal (Qinvalid_read_syntax,
1772 Fcons (make_string ("#^^", 3), Qnil));
1774 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1776 if (c == '&')
1778 Lisp_Object length;
1779 length = read1 (readcharfun, pch, first_in_list);
1780 c = READCHAR;
1781 if (c == '"')
1783 Lisp_Object tmp, val;
1784 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1785 / BITS_PER_CHAR);
1787 UNREAD (c);
1788 tmp = read1 (readcharfun, pch, first_in_list);
1789 if (size_in_chars != XSTRING (tmp)->size
1790 /* We used to print 1 char too many
1791 when the number of bits was a multiple of 8.
1792 Accept such input in case it came from an old version. */
1793 && ! (XFASTINT (length)
1794 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1795 Fsignal (Qinvalid_read_syntax,
1796 Fcons (make_string ("#&...", 5), Qnil));
1798 val = Fmake_bool_vector (length, Qnil);
1799 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1800 size_in_chars);
1801 /* Clear the extraneous bits in the last byte. */
1802 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1803 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1804 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1805 return val;
1807 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1808 Qnil));
1810 if (c == '[')
1812 /* Accept compiled functions at read-time so that we don't have to
1813 build them using function calls. */
1814 Lisp_Object tmp;
1815 tmp = read_vector (readcharfun, 1);
1816 return Fmake_byte_code (XVECTOR (tmp)->size,
1817 XVECTOR (tmp)->contents);
1819 if (c == '(')
1821 Lisp_Object tmp;
1822 struct gcpro gcpro1;
1823 int ch;
1825 /* Read the string itself. */
1826 tmp = read1 (readcharfun, &ch, 0);
1827 if (ch != 0 || !STRINGP (tmp))
1828 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1829 GCPRO1 (tmp);
1830 /* Read the intervals and their properties. */
1831 while (1)
1833 Lisp_Object beg, end, plist;
1835 beg = read1 (readcharfun, &ch, 0);
1836 if (ch == ')')
1837 break;
1838 if (ch == 0)
1839 end = read1 (readcharfun, &ch, 0);
1840 if (ch == 0)
1841 plist = read1 (readcharfun, &ch, 0);
1842 if (ch)
1843 Fsignal (Qinvalid_read_syntax,
1844 Fcons (build_string ("invalid string property list"),
1845 Qnil));
1846 Fset_text_properties (beg, end, plist, tmp);
1848 UNGCPRO;
1849 return tmp;
1852 /* #@NUMBER is used to skip NUMBER following characters.
1853 That's used in .elc files to skip over doc strings
1854 and function definitions. */
1855 if (c == '@')
1857 int i, nskip = 0;
1859 /* Read a decimal integer. */
1860 while ((c = READCHAR) >= 0
1861 && c >= '0' && c <= '9')
1863 nskip *= 10;
1864 nskip += c - '0';
1866 if (c >= 0)
1867 UNREAD (c);
1869 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1871 /* If we are supposed to force doc strings into core right now,
1872 record the last string that we skipped,
1873 and record where in the file it comes from. */
1875 /* But first exchange saved_doc_string
1876 with prev_saved_doc_string, so we save two strings. */
1878 char *temp = saved_doc_string;
1879 int temp_size = saved_doc_string_size;
1880 file_offset temp_pos = saved_doc_string_position;
1881 int temp_len = saved_doc_string_length;
1883 saved_doc_string = prev_saved_doc_string;
1884 saved_doc_string_size = prev_saved_doc_string_size;
1885 saved_doc_string_position = prev_saved_doc_string_position;
1886 saved_doc_string_length = prev_saved_doc_string_length;
1888 prev_saved_doc_string = temp;
1889 prev_saved_doc_string_size = temp_size;
1890 prev_saved_doc_string_position = temp_pos;
1891 prev_saved_doc_string_length = temp_len;
1894 if (saved_doc_string_size == 0)
1896 saved_doc_string_size = nskip + 100;
1897 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1899 if (nskip > saved_doc_string_size)
1901 saved_doc_string_size = nskip + 100;
1902 saved_doc_string = (char *) xrealloc (saved_doc_string,
1903 saved_doc_string_size);
1906 saved_doc_string_position = file_tell (instream);
1908 /* Copy that many characters into saved_doc_string. */
1909 for (i = 0; i < nskip && c >= 0; i++)
1910 saved_doc_string[i] = c = READCHAR;
1912 saved_doc_string_length = i;
1914 else
1916 /* Skip that many characters. */
1917 for (i = 0; i < nskip && c >= 0; i++)
1918 c = READCHAR;
1921 goto retry;
1923 if (c == '$')
1924 return Vload_file_name;
1925 if (c == '\'')
1926 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1927 /* #:foo is the uninterned symbol named foo. */
1928 if (c == ':')
1930 uninterned_symbol = 1;
1931 c = READCHAR;
1932 goto default_label;
1934 /* Reader forms that can reuse previously read objects. */
1935 if (c >= '0' && c <= '9')
1937 int n = 0;
1938 Lisp_Object tem;
1940 /* Read a non-negative integer. */
1941 while (c >= '0' && c <= '9')
1943 n *= 10;
1944 n += c - '0';
1945 c = READCHAR;
1947 /* #n=object returns object, but associates it with n for #n#. */
1948 if (c == '=')
1950 /* Make a placeholder for #n# to use temporarily */
1951 Lisp_Object placeholder;
1952 Lisp_Object cell;
1954 placeholder = Fcons(Qnil, Qnil);
1955 cell = Fcons (make_number (n), placeholder);
1956 read_objects = Fcons (cell, read_objects);
1958 /* Read the object itself. */
1959 tem = read0 (readcharfun);
1961 /* Now put it everywhere the placeholder was... */
1962 substitute_object_in_subtree (tem, placeholder);
1964 /* ...and #n# will use the real value from now on. */
1965 Fsetcdr (cell, tem);
1967 return tem;
1969 /* #n# returns a previously read object. */
1970 if (c == '#')
1972 tem = Fassq (make_number (n), read_objects);
1973 if (CONSP (tem))
1974 return XCDR (tem);
1975 /* Fall through to error message. */
1977 else if (c == 'r' || c == 'R')
1978 return read_integer (readcharfun, n);
1980 /* Fall through to error message. */
1982 else if (c == 'x' || c == 'X')
1983 return read_integer (readcharfun, 16);
1984 else if (c == 'o' || c == 'O')
1985 return read_integer (readcharfun, 8);
1986 else if (c == 'b' || c == 'B')
1987 return read_integer (readcharfun, 2);
1989 UNREAD (c);
1990 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1992 case ';':
1993 while ((c = READCHAR) >= 0 && c != '\n');
1994 goto retry;
1996 case '\'':
1998 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2001 case '`':
2002 if (first_in_list)
2003 goto default_label;
2004 else
2006 Lisp_Object value;
2008 new_backquote_flag = 1;
2009 value = read0 (readcharfun);
2010 new_backquote_flag = 0;
2012 return Fcons (Qbackquote, Fcons (value, Qnil));
2015 case ',':
2016 if (new_backquote_flag)
2018 Lisp_Object comma_type = Qnil;
2019 Lisp_Object value;
2020 int ch = READCHAR;
2022 if (ch == '@')
2023 comma_type = Qcomma_at;
2024 else if (ch == '.')
2025 comma_type = Qcomma_dot;
2026 else
2028 if (ch >= 0) UNREAD (ch);
2029 comma_type = Qcomma;
2032 new_backquote_flag = 0;
2033 value = read0 (readcharfun);
2034 new_backquote_flag = 1;
2035 return Fcons (comma_type, Fcons (value, Qnil));
2037 else
2038 goto default_label;
2040 case '?':
2042 c = READCHAR;
2043 if (c < 0)
2044 end_of_file_error ();
2046 if (c == '\\')
2047 c = read_escape (readcharfun, 0);
2048 else if (BASE_LEADING_CODE_P (c))
2049 c = read_multibyte (c, readcharfun);
2051 return make_number (c);
2054 case '"':
2056 register char *p = read_buffer;
2057 register char *end = read_buffer + read_buffer_size;
2058 register int c;
2059 /* Nonzero if we saw an escape sequence specifying
2060 a multibyte character. */
2061 int force_multibyte = 0;
2062 /* Nonzero if we saw an escape sequence specifying
2063 a single-byte character. */
2064 int force_singlebyte = 0;
2065 int cancel = 0;
2066 int nchars;
2068 while ((c = READCHAR) >= 0
2069 && c != '\"')
2071 if (end - p < MAX_MULTIBYTE_LENGTH)
2073 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2074 p += new - read_buffer;
2075 read_buffer += new - read_buffer;
2076 end = read_buffer + read_buffer_size;
2079 if (c == '\\')
2081 c = read_escape (readcharfun, 1);
2083 /* C is -1 if \ newline has just been seen */
2084 if (c == -1)
2086 if (p == read_buffer)
2087 cancel = 1;
2088 continue;
2091 /* If an escape specifies a non-ASCII single-byte character,
2092 this must be a unibyte string. */
2093 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
2094 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
2095 force_singlebyte = 1;
2098 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2100 /* Any modifiers for a multibyte character are invalid. */
2101 if (c & CHAR_MODIFIER_MASK)
2102 error ("Invalid modifier in string");
2103 p += CHAR_STRING (c, p);
2104 force_multibyte = 1;
2106 else
2108 /* Allow `\C- ' and `\C-?'. */
2109 if (c == (CHAR_CTL | ' '))
2110 c = 0;
2111 else if (c == (CHAR_CTL | '?'))
2112 c = 127;
2114 if (c & CHAR_SHIFT)
2116 /* Shift modifier is valid only with [A-Za-z]. */
2117 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2118 c &= ~CHAR_SHIFT;
2119 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2120 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
2123 if (c & CHAR_META)
2124 /* Move the meta bit to the right place for a string. */
2125 c = (c & ~CHAR_META) | 0x80;
2126 if (c & ~0xff)
2127 error ("Invalid modifier in string");
2128 *p++ = c;
2131 if (c < 0)
2132 end_of_file_error ();
2134 /* If purifying, and string starts with \ newline,
2135 return zero instead. This is for doc strings
2136 that we are really going to find in etc/DOC.nn.nn */
2137 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2138 return make_number (0);
2140 if (force_multibyte)
2141 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2142 p - read_buffer, &nchars);
2143 else if (force_singlebyte)
2144 nchars = p - read_buffer;
2145 else if (load_convert_to_unibyte)
2147 Lisp_Object string;
2148 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2149 p - read_buffer, &nchars);
2150 if (p - read_buffer != nchars)
2152 string = make_multibyte_string (read_buffer, nchars,
2153 p - read_buffer);
2154 return Fstring_make_unibyte (string);
2157 else if (EQ (readcharfun, Qget_file_char)
2158 || EQ (readcharfun, Qlambda))
2159 /* Nowadays, reading directly from a file is used only for
2160 compiled Emacs Lisp files, and those always use the
2161 Emacs internal encoding. Meanwhile, Qlambda is used
2162 for reading dynamic byte code (compiled with
2163 byte-compile-dynamic = t). */
2164 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2165 p - read_buffer, &nchars);
2166 else
2167 /* In all other cases, if we read these bytes as
2168 separate characters, treat them as separate characters now. */
2169 nchars = p - read_buffer;
2171 if (read_pure)
2172 return make_pure_string (read_buffer, nchars, p - read_buffer,
2173 (force_multibyte
2174 || (p - read_buffer != nchars)));
2175 return make_specified_string (read_buffer, nchars, p - read_buffer,
2176 (force_multibyte
2177 || (p - read_buffer != nchars)));
2180 case '.':
2182 int next_char = READCHAR;
2183 UNREAD (next_char);
2185 if (next_char <= 040
2186 || index ("\"'`,(", next_char))
2188 *pch = c;
2189 return Qnil;
2192 /* Otherwise, we fall through! Note that the atom-reading loop
2193 below will now loop at least once, assuring that we will not
2194 try to UNREAD two characters in a row. */
2196 default:
2197 default_label:
2198 if (c <= 040) goto retry;
2200 char *p = read_buffer;
2201 int quoted = 0;
2204 char *end = read_buffer + read_buffer_size;
2206 while (c > 040
2207 && !(c == '\"' || c == '\'' || c == ';'
2208 || c == '(' || c == ')'
2209 || c == '[' || c == ']' || c == '#'))
2211 if (end - p < MAX_MULTIBYTE_LENGTH)
2213 char *new = (char *) xrealloc (read_buffer,
2214 read_buffer_size *= 2);
2215 p += new - read_buffer;
2216 read_buffer += new - read_buffer;
2217 end = read_buffer + read_buffer_size;
2220 if (c == '\\')
2222 c = READCHAR;
2223 quoted = 1;
2226 if (! SINGLE_BYTE_CHAR_P (c))
2227 p += CHAR_STRING (c, p);
2228 else
2229 *p++ = c;
2231 c = READCHAR;
2234 if (p == end)
2236 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2237 p += new - read_buffer;
2238 read_buffer += new - read_buffer;
2239 /* end = read_buffer + read_buffer_size; */
2241 *p = 0;
2242 if (c >= 0)
2243 UNREAD (c);
2246 if (!quoted && !uninterned_symbol)
2248 register char *p1;
2249 register Lisp_Object val;
2250 p1 = read_buffer;
2251 if (*p1 == '+' || *p1 == '-') p1++;
2252 /* Is it an integer? */
2253 if (p1 != p)
2255 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2256 /* Integers can have trailing decimal points. */
2257 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2258 if (p1 == p)
2259 /* It is an integer. */
2261 if (p1[-1] == '.')
2262 p1[-1] = '\0';
2263 if (sizeof (int) == sizeof (EMACS_INT))
2264 XSETINT (val, atoi (read_buffer));
2265 else if (sizeof (long) == sizeof (EMACS_INT))
2266 XSETINT (val, atol (read_buffer));
2267 else
2268 abort ();
2269 return val;
2272 if (isfloat_string (read_buffer))
2274 /* Compute NaN and infinities using 0.0 in a variable,
2275 to cope with compilers that think they are smarter
2276 than we are. */
2277 double zero = 0.0;
2279 double value;
2281 /* Negate the value ourselves. This treats 0, NaNs,
2282 and infinity properly on IEEE floating point hosts,
2283 and works around a common bug where atof ("-0.0")
2284 drops the sign. */
2285 int negative = read_buffer[0] == '-';
2287 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2288 returns 1, is if the input ends in e+INF or e+NaN. */
2289 switch (p[-1])
2291 case 'F':
2292 value = 1.0 / zero;
2293 break;
2294 case 'N':
2295 value = zero / zero;
2296 break;
2297 default:
2298 value = atof (read_buffer + negative);
2299 break;
2302 return make_float (negative ? - value : value);
2306 if (uninterned_symbol)
2307 return make_symbol (read_buffer);
2308 else
2309 return intern (read_buffer);
2315 /* List of nodes we've seen during substitute_object_in_subtree. */
2316 static Lisp_Object seen_list;
2318 static void
2319 substitute_object_in_subtree (object, placeholder)
2320 Lisp_Object object;
2321 Lisp_Object placeholder;
2323 Lisp_Object check_object;
2325 /* We haven't seen any objects when we start. */
2326 seen_list = Qnil;
2328 /* Make all the substitutions. */
2329 check_object
2330 = substitute_object_recurse (object, placeholder, object);
2332 /* Clear seen_list because we're done with it. */
2333 seen_list = Qnil;
2335 /* The returned object here is expected to always eq the
2336 original. */
2337 if (!EQ (check_object, object))
2338 error ("Unexpected mutation error in reader");
2341 /* Feval doesn't get called from here, so no gc protection is needed. */
2342 #define SUBSTITUTE(get_val, set_val) \
2344 Lisp_Object old_value = get_val; \
2345 Lisp_Object true_value \
2346 = substitute_object_recurse (object, placeholder,\
2347 old_value); \
2349 if (!EQ (old_value, true_value)) \
2351 set_val; \
2355 static Lisp_Object
2356 substitute_object_recurse (object, placeholder, subtree)
2357 Lisp_Object object;
2358 Lisp_Object placeholder;
2359 Lisp_Object subtree;
2361 /* If we find the placeholder, return the target object. */
2362 if (EQ (placeholder, subtree))
2363 return object;
2365 /* If we've been to this node before, don't explore it again. */
2366 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2367 return subtree;
2369 /* If this node can be the entry point to a cycle, remember that
2370 we've seen it. It can only be such an entry point if it was made
2371 by #n=, which means that we can find it as a value in
2372 read_objects. */
2373 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2374 seen_list = Fcons (subtree, seen_list);
2376 /* Recurse according to subtree's type.
2377 Every branch must return a Lisp_Object. */
2378 switch (XTYPE (subtree))
2380 case Lisp_Vectorlike:
2382 int i;
2383 int length = XINT (Flength(subtree));
2384 for (i = 0; i < length; i++)
2386 Lisp_Object idx = make_number (i);
2387 SUBSTITUTE (Faref (subtree, idx),
2388 Faset (subtree, idx, true_value));
2390 return subtree;
2393 case Lisp_Cons:
2395 SUBSTITUTE (Fcar_safe (subtree),
2396 Fsetcar (subtree, true_value));
2397 SUBSTITUTE (Fcdr_safe (subtree),
2398 Fsetcdr (subtree, true_value));
2399 return subtree;
2402 case Lisp_String:
2404 /* Check for text properties in each interval.
2405 substitute_in_interval contains part of the logic. */
2407 INTERVAL root_interval = XSTRING (subtree)->intervals;
2408 Lisp_Object arg = Fcons (object, placeholder);
2410 traverse_intervals (root_interval, 1, 0,
2411 &substitute_in_interval, arg);
2413 return subtree;
2416 /* Other types don't recurse any further. */
2417 default:
2418 return subtree;
2422 /* Helper function for substitute_object_recurse. */
2423 static void
2424 substitute_in_interval (interval, arg)
2425 INTERVAL interval;
2426 Lisp_Object arg;
2428 Lisp_Object object = Fcar (arg);
2429 Lisp_Object placeholder = Fcdr (arg);
2431 SUBSTITUTE(interval->plist, interval->plist = true_value);
2435 #define LEAD_INT 1
2436 #define DOT_CHAR 2
2437 #define TRAIL_INT 4
2438 #define E_CHAR 8
2439 #define EXP_INT 16
2442 isfloat_string (cp)
2443 register char *cp;
2445 register int state;
2447 char *start = cp;
2449 state = 0;
2450 if (*cp == '+' || *cp == '-')
2451 cp++;
2453 if (*cp >= '0' && *cp <= '9')
2455 state |= LEAD_INT;
2456 while (*cp >= '0' && *cp <= '9')
2457 cp++;
2459 if (*cp == '.')
2461 state |= DOT_CHAR;
2462 cp++;
2464 if (*cp >= '0' && *cp <= '9')
2466 state |= TRAIL_INT;
2467 while (*cp >= '0' && *cp <= '9')
2468 cp++;
2470 if (*cp == 'e' || *cp == 'E')
2472 state |= E_CHAR;
2473 cp++;
2474 if (*cp == '+' || *cp == '-')
2475 cp++;
2478 if (*cp >= '0' && *cp <= '9')
2480 state |= EXP_INT;
2481 while (*cp >= '0' && *cp <= '9')
2482 cp++;
2484 else if (cp == start)
2486 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2488 state |= EXP_INT;
2489 cp += 3;
2491 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2493 state |= EXP_INT;
2494 cp += 3;
2497 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2498 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2499 || state == (DOT_CHAR|TRAIL_INT)
2500 || state == (LEAD_INT|E_CHAR|EXP_INT)
2501 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2502 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2506 static Lisp_Object
2507 read_vector (readcharfun, bytecodeflag)
2508 Lisp_Object readcharfun;
2509 int bytecodeflag;
2511 register int i;
2512 register int size;
2513 register Lisp_Object *ptr;
2514 register Lisp_Object tem, item, vector;
2515 register struct Lisp_Cons *otem;
2516 Lisp_Object len;
2518 tem = read_list (1, readcharfun);
2519 len = Flength (tem);
2520 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2522 size = XVECTOR (vector)->size;
2523 ptr = XVECTOR (vector)->contents;
2524 for (i = 0; i < size; i++)
2526 item = Fcar (tem);
2527 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2528 bytecode object, the docstring containing the bytecode and
2529 constants values must be treated as unibyte and passed to
2530 Fread, to get the actual bytecode string and constants vector. */
2531 if (bytecodeflag && load_force_doc_strings)
2533 if (i == COMPILED_BYTECODE)
2535 if (!STRINGP (item))
2536 error ("invalid byte code");
2538 /* Delay handling the bytecode slot until we know whether
2539 it is lazily-loaded (we can tell by whether the
2540 constants slot is nil). */
2541 ptr[COMPILED_CONSTANTS] = item;
2542 item = Qnil;
2544 else if (i == COMPILED_CONSTANTS)
2546 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2548 if (NILP (item))
2550 /* Coerce string to unibyte (like string-as-unibyte,
2551 but without generating extra garbage and
2552 guaranteeing no change in the contents). */
2553 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2554 SET_STRING_BYTES (XSTRING (bytestr), -1);
2556 item = Fread (bytestr);
2557 if (!CONSP (item))
2558 error ("invalid byte code");
2560 otem = XCONS (item);
2561 bytestr = XCAR (item);
2562 item = XCDR (item);
2563 free_cons (otem);
2566 /* Now handle the bytecode slot. */
2567 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2570 ptr[i] = read_pure ? Fpurecopy (item) : item;
2571 otem = XCONS (tem);
2572 tem = Fcdr (tem);
2573 free_cons (otem);
2575 return vector;
2578 /* FLAG = 1 means check for ] to terminate rather than ) and .
2579 FLAG = -1 means check for starting with defun
2580 and make structure pure. */
2582 static Lisp_Object
2583 read_list (flag, readcharfun)
2584 int flag;
2585 register Lisp_Object readcharfun;
2587 /* -1 means check next element for defun,
2588 0 means don't check,
2589 1 means already checked and found defun. */
2590 int defunflag = flag < 0 ? -1 : 0;
2591 Lisp_Object val, tail;
2592 register Lisp_Object elt, tem;
2593 struct gcpro gcpro1, gcpro2;
2594 /* 0 is the normal case.
2595 1 means this list is a doc reference; replace it with the number 0.
2596 2 means this list is a doc reference; replace it with the doc string. */
2597 int doc_reference = 0;
2599 /* Initialize this to 1 if we are reading a list. */
2600 int first_in_list = flag <= 0;
2602 val = Qnil;
2603 tail = Qnil;
2605 while (1)
2607 int ch;
2608 GCPRO2 (val, tail);
2609 elt = read1 (readcharfun, &ch, first_in_list);
2610 UNGCPRO;
2612 first_in_list = 0;
2614 /* While building, if the list starts with #$, treat it specially. */
2615 if (EQ (elt, Vload_file_name)
2616 && ! NILP (elt)
2617 && !NILP (Vpurify_flag))
2619 if (NILP (Vdoc_file_name))
2620 /* We have not yet called Snarf-documentation, so assume
2621 this file is described in the DOC-MM.NN file
2622 and Snarf-documentation will fill in the right value later.
2623 For now, replace the whole list with 0. */
2624 doc_reference = 1;
2625 else
2626 /* We have already called Snarf-documentation, so make a relative
2627 file name for this file, so it can be found properly
2628 in the installed Lisp directory.
2629 We don't use Fexpand_file_name because that would make
2630 the directory absolute now. */
2631 elt = concat2 (build_string ("../lisp/"),
2632 Ffile_name_nondirectory (elt));
2634 else if (EQ (elt, Vload_file_name)
2635 && ! NILP (elt)
2636 && load_force_doc_strings)
2637 doc_reference = 2;
2639 if (ch)
2641 if (flag > 0)
2643 if (ch == ']')
2644 return val;
2645 Fsignal (Qinvalid_read_syntax,
2646 Fcons (make_string (") or . in a vector", 18), Qnil));
2648 if (ch == ')')
2649 return val;
2650 if (ch == '.')
2652 GCPRO2 (val, tail);
2653 if (!NILP (tail))
2654 XCDR (tail) = read0 (readcharfun);
2655 else
2656 val = read0 (readcharfun);
2657 read1 (readcharfun, &ch, 0);
2658 UNGCPRO;
2659 if (ch == ')')
2661 if (doc_reference == 1)
2662 return make_number (0);
2663 if (doc_reference == 2)
2665 /* Get a doc string from the file we are loading.
2666 If it's in saved_doc_string, get it from there. */
2667 int pos = XINT (XCDR (val));
2668 /* Position is negative for user variables. */
2669 if (pos < 0) pos = -pos;
2670 if (pos >= saved_doc_string_position
2671 && pos < (saved_doc_string_position
2672 + saved_doc_string_length))
2674 int start = pos - saved_doc_string_position;
2675 int from, to;
2677 /* Process quoting with ^A,
2678 and find the end of the string,
2679 which is marked with ^_ (037). */
2680 for (from = start, to = start;
2681 saved_doc_string[from] != 037;)
2683 int c = saved_doc_string[from++];
2684 if (c == 1)
2686 c = saved_doc_string[from++];
2687 if (c == 1)
2688 saved_doc_string[to++] = c;
2689 else if (c == '0')
2690 saved_doc_string[to++] = 0;
2691 else if (c == '_')
2692 saved_doc_string[to++] = 037;
2694 else
2695 saved_doc_string[to++] = c;
2698 return make_string (saved_doc_string + start,
2699 to - start);
2701 /* Look in prev_saved_doc_string the same way. */
2702 else if (pos >= prev_saved_doc_string_position
2703 && pos < (prev_saved_doc_string_position
2704 + prev_saved_doc_string_length))
2706 int start = pos - prev_saved_doc_string_position;
2707 int from, to;
2709 /* Process quoting with ^A,
2710 and find the end of the string,
2711 which is marked with ^_ (037). */
2712 for (from = start, to = start;
2713 prev_saved_doc_string[from] != 037;)
2715 int c = prev_saved_doc_string[from++];
2716 if (c == 1)
2718 c = prev_saved_doc_string[from++];
2719 if (c == 1)
2720 prev_saved_doc_string[to++] = c;
2721 else if (c == '0')
2722 prev_saved_doc_string[to++] = 0;
2723 else if (c == '_')
2724 prev_saved_doc_string[to++] = 037;
2726 else
2727 prev_saved_doc_string[to++] = c;
2730 return make_string (prev_saved_doc_string + start,
2731 to - start);
2733 else
2734 return get_doc_string (val, 0, 0);
2737 return val;
2739 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2741 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2743 tem = (read_pure && flag <= 0
2744 ? pure_cons (elt, Qnil)
2745 : Fcons (elt, Qnil));
2746 if (!NILP (tail))
2747 XCDR (tail) = tem;
2748 else
2749 val = tem;
2750 tail = tem;
2751 if (defunflag < 0)
2752 defunflag = EQ (elt, Qdefun);
2753 else if (defunflag > 0)
2754 read_pure = 1;
2758 Lisp_Object Vobarray;
2759 Lisp_Object initial_obarray;
2761 /* oblookup stores the bucket number here, for the sake of Funintern. */
2763 int oblookup_last_bucket_number;
2765 static int hash_string ();
2766 Lisp_Object oblookup ();
2768 /* Get an error if OBARRAY is not an obarray.
2769 If it is one, return it. */
2771 Lisp_Object
2772 check_obarray (obarray)
2773 Lisp_Object obarray;
2775 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2777 /* If Vobarray is now invalid, force it to be valid. */
2778 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2780 obarray = wrong_type_argument (Qvectorp, obarray);
2782 return obarray;
2785 /* Intern the C string STR: return a symbol with that name,
2786 interned in the current obarray. */
2788 Lisp_Object
2789 intern (str)
2790 char *str;
2792 Lisp_Object tem;
2793 int len = strlen (str);
2794 Lisp_Object obarray;
2796 obarray = Vobarray;
2797 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2798 obarray = check_obarray (obarray);
2799 tem = oblookup (obarray, str, len, len);
2800 if (SYMBOLP (tem))
2801 return tem;
2802 return Fintern (make_string (str, len), obarray);
2805 /* Create an uninterned symbol with name STR. */
2807 Lisp_Object
2808 make_symbol (str)
2809 char *str;
2811 int len = strlen (str);
2813 return Fmake_symbol ((!NILP (Vpurify_flag)
2814 ? make_pure_string (str, len, len, 0)
2815 : make_string (str, len)));
2818 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2819 "Return the canonical symbol whose name is STRING.\n\
2820 If there is none, one is created by this function and returned.\n\
2821 A second optional argument specifies the obarray to use;\n\
2822 it defaults to the value of `obarray'.")
2823 (string, obarray)
2824 Lisp_Object string, obarray;
2826 register Lisp_Object tem, sym, *ptr;
2828 if (NILP (obarray)) obarray = Vobarray;
2829 obarray = check_obarray (obarray);
2831 CHECK_STRING (string, 0);
2833 tem = oblookup (obarray, XSTRING (string)->data,
2834 XSTRING (string)->size,
2835 STRING_BYTES (XSTRING (string)));
2836 if (!INTEGERP (tem))
2837 return tem;
2839 if (!NILP (Vpurify_flag))
2840 string = Fpurecopy (string);
2841 sym = Fmake_symbol (string);
2842 XSYMBOL (sym)->obarray = obarray;
2844 if ((XSTRING (string)->data[0] == ':')
2845 && EQ (obarray, initial_obarray))
2846 XSYMBOL (sym)->value = sym;
2848 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2849 if (SYMBOLP (*ptr))
2850 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2851 else
2852 XSYMBOL (sym)->next = 0;
2853 *ptr = sym;
2854 return sym;
2857 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2858 "Return the canonical symbol named NAME, or nil if none exists.\n\
2859 NAME may be a string or a symbol. If it is a symbol, that exact\n\
2860 symbol is searched for.\n\
2861 A second optional argument specifies the obarray to use;\n\
2862 it defaults to the value of `obarray'.")
2863 (name, obarray)
2864 Lisp_Object name, obarray;
2866 register Lisp_Object tem;
2867 struct Lisp_String *string;
2869 if (NILP (obarray)) obarray = Vobarray;
2870 obarray = check_obarray (obarray);
2872 if (!SYMBOLP (name))
2874 CHECK_STRING (name, 0);
2875 string = XSTRING (name);
2877 else
2878 string = XSYMBOL (name)->name;
2880 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2881 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2882 return Qnil;
2883 else
2884 return tem;
2887 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2888 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2889 The value is t if a symbol was found and deleted, nil otherwise.\n\
2890 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2891 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2892 OBARRAY defaults to the value of the variable `obarray'.")
2893 (name, obarray)
2894 Lisp_Object name, obarray;
2896 register Lisp_Object string, tem;
2897 int hash;
2899 if (NILP (obarray)) obarray = Vobarray;
2900 obarray = check_obarray (obarray);
2902 if (SYMBOLP (name))
2903 XSETSTRING (string, XSYMBOL (name)->name);
2904 else
2906 CHECK_STRING (name, 0);
2907 string = name;
2910 tem = oblookup (obarray, XSTRING (string)->data,
2911 XSTRING (string)->size,
2912 STRING_BYTES (XSTRING (string)));
2913 if (INTEGERP (tem))
2914 return Qnil;
2915 /* If arg was a symbol, don't delete anything but that symbol itself. */
2916 if (SYMBOLP (name) && !EQ (name, tem))
2917 return Qnil;
2919 XSYMBOL (tem)->obarray = Qnil;
2921 hash = oblookup_last_bucket_number;
2923 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2925 if (XSYMBOL (tem)->next)
2926 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2927 else
2928 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2930 else
2932 Lisp_Object tail, following;
2934 for (tail = XVECTOR (obarray)->contents[hash];
2935 XSYMBOL (tail)->next;
2936 tail = following)
2938 XSETSYMBOL (following, XSYMBOL (tail)->next);
2939 if (EQ (following, tem))
2941 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2942 break;
2947 return Qt;
2950 /* Return the symbol in OBARRAY whose names matches the string
2951 of SIZE characters (SIZE_BYTE bytes) at PTR.
2952 If there is no such symbol in OBARRAY, return nil.
2954 Also store the bucket number in oblookup_last_bucket_number. */
2956 Lisp_Object
2957 oblookup (obarray, ptr, size, size_byte)
2958 Lisp_Object obarray;
2959 register char *ptr;
2960 int size, size_byte;
2962 int hash;
2963 int obsize;
2964 register Lisp_Object tail;
2965 Lisp_Object bucket, tem;
2967 if (!VECTORP (obarray)
2968 || (obsize = XVECTOR (obarray)->size) == 0)
2970 obarray = check_obarray (obarray);
2971 obsize = XVECTOR (obarray)->size;
2973 /* This is sometimes needed in the middle of GC. */
2974 obsize &= ~ARRAY_MARK_FLAG;
2975 /* Combining next two lines breaks VMS C 2.3. */
2976 hash = hash_string (ptr, size_byte);
2977 hash %= obsize;
2978 bucket = XVECTOR (obarray)->contents[hash];
2979 oblookup_last_bucket_number = hash;
2980 if (XFASTINT (bucket) == 0)
2982 else if (!SYMBOLP (bucket))
2983 error ("Bad data in guts of obarray"); /* Like CADR error message */
2984 else
2985 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2987 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
2988 && XSYMBOL (tail)->name->size == size
2989 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
2990 return tail;
2991 else if (XSYMBOL (tail)->next == 0)
2992 break;
2994 XSETINT (tem, hash);
2995 return tem;
2998 static int
2999 hash_string (ptr, len)
3000 unsigned char *ptr;
3001 int len;
3003 register unsigned char *p = ptr;
3004 register unsigned char *end = p + len;
3005 register unsigned char c;
3006 register int hash = 0;
3008 while (p != end)
3010 c = *p++;
3011 if (c >= 0140) c -= 40;
3012 hash = ((hash<<3) + (hash>>28) + c);
3014 return hash & 07777777777;
3017 void
3018 map_obarray (obarray, fn, arg)
3019 Lisp_Object obarray;
3020 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3021 Lisp_Object arg;
3023 register int i;
3024 register Lisp_Object tail;
3025 CHECK_VECTOR (obarray, 1);
3026 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3028 tail = XVECTOR (obarray)->contents[i];
3029 if (SYMBOLP (tail))
3030 while (1)
3032 (*fn) (tail, arg);
3033 if (XSYMBOL (tail)->next == 0)
3034 break;
3035 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3040 void
3041 mapatoms_1 (sym, function)
3042 Lisp_Object sym, function;
3044 call1 (function, sym);
3047 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3048 "Call FUNCTION on every symbol in OBARRAY.\n\
3049 OBARRAY defaults to the value of `obarray'.")
3050 (function, obarray)
3051 Lisp_Object function, obarray;
3053 if (NILP (obarray)) obarray = Vobarray;
3054 obarray = check_obarray (obarray);
3056 map_obarray (obarray, mapatoms_1, function);
3057 return Qnil;
3060 #define OBARRAY_SIZE 1511
3062 void
3063 init_obarray ()
3065 Lisp_Object oblength;
3066 int hash;
3067 Lisp_Object *tem;
3069 XSETFASTINT (oblength, OBARRAY_SIZE);
3071 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3072 Vobarray = Fmake_vector (oblength, make_number (0));
3073 initial_obarray = Vobarray;
3074 staticpro (&initial_obarray);
3075 /* Intern nil in the obarray */
3076 XSYMBOL (Qnil)->obarray = Vobarray;
3077 /* These locals are to kludge around a pyramid compiler bug. */
3078 hash = hash_string ("nil", 3);
3079 /* Separate statement here to avoid VAXC bug. */
3080 hash %= OBARRAY_SIZE;
3081 tem = &XVECTOR (Vobarray)->contents[hash];
3082 *tem = Qnil;
3084 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3085 XSYMBOL (Qnil)->function = Qunbound;
3086 XSYMBOL (Qunbound)->value = Qunbound;
3087 XSYMBOL (Qunbound)->function = Qunbound;
3089 Qt = intern ("t");
3090 XSYMBOL (Qnil)->value = Qnil;
3091 XSYMBOL (Qnil)->plist = Qnil;
3092 XSYMBOL (Qt)->value = Qt;
3094 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3095 Vpurify_flag = Qt;
3097 Qvariable_documentation = intern ("variable-documentation");
3098 staticpro (&Qvariable_documentation);
3100 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3101 read_buffer = (char *) xmalloc (read_buffer_size);
3104 void
3105 defsubr (sname)
3106 struct Lisp_Subr *sname;
3108 Lisp_Object sym;
3109 sym = intern (sname->symbol_name);
3110 XSETSUBR (XSYMBOL (sym)->function, sname);
3113 #ifdef NOTDEF /* use fset in subr.el now */
3114 void
3115 defalias (sname, string)
3116 struct Lisp_Subr *sname;
3117 char *string;
3119 Lisp_Object sym;
3120 sym = intern (string);
3121 XSETSUBR (XSYMBOL (sym)->function, sname);
3123 #endif /* NOTDEF */
3125 /* Define an "integer variable"; a symbol whose value is forwarded
3126 to a C variable of type int. Sample call: */
3127 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3128 void
3129 defvar_int (namestring, address)
3130 char *namestring;
3131 int *address;
3133 Lisp_Object sym, val;
3134 sym = intern (namestring);
3135 val = allocate_misc ();
3136 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3137 XINTFWD (val)->intvar = address;
3138 XSYMBOL (sym)->value = val;
3141 /* Similar but define a variable whose value is T if address contains 1,
3142 NIL if address contains 0 */
3143 void
3144 defvar_bool (namestring, address)
3145 char *namestring;
3146 int *address;
3148 Lisp_Object sym, val;
3149 sym = intern (namestring);
3150 val = allocate_misc ();
3151 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3152 XBOOLFWD (val)->boolvar = address;
3153 XSYMBOL (sym)->value = val;
3154 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3157 /* Similar but define a variable whose value is the Lisp Object stored
3158 at address. Two versions: with and without gc-marking of the C
3159 variable. The nopro version is used when that variable will be
3160 gc-marked for some other reason, since marking the same slot twice
3161 can cause trouble with strings. */
3162 void
3163 defvar_lisp_nopro (namestring, address)
3164 char *namestring;
3165 Lisp_Object *address;
3167 Lisp_Object sym, val;
3168 sym = intern (namestring);
3169 val = allocate_misc ();
3170 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3171 XOBJFWD (val)->objvar = address;
3172 XSYMBOL (sym)->value = val;
3175 void
3176 defvar_lisp (namestring, address)
3177 char *namestring;
3178 Lisp_Object *address;
3180 defvar_lisp_nopro (namestring, address);
3181 staticpro (address);
3184 /* Similar but define a variable whose value is the Lisp Object stored in
3185 the current buffer. address is the address of the slot in the buffer
3186 that is current now. */
3188 void
3189 defvar_per_buffer (namestring, address, type, doc)
3190 char *namestring;
3191 Lisp_Object *address;
3192 Lisp_Object type;
3193 char *doc;
3195 Lisp_Object sym, val;
3196 int offset;
3197 extern struct buffer buffer_local_symbols;
3199 sym = intern (namestring);
3200 val = allocate_misc ();
3201 offset = (char *)address - (char *)current_buffer;
3203 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3204 XBUFFER_OBJFWD (val)->offset = offset;
3205 XSYMBOL (sym)->value = val;
3206 PER_BUFFER_SYMBOL (offset) = sym;
3207 PER_BUFFER_TYPE (offset) = type;
3209 if (PER_BUFFER_IDX (offset) == 0)
3210 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3211 slot of buffer_local_flags */
3212 abort ();
3216 /* Similar but define a variable whose value is the Lisp Object stored
3217 at a particular offset in the current kboard object. */
3219 void
3220 defvar_kboard (namestring, offset)
3221 char *namestring;
3222 int offset;
3224 Lisp_Object sym, val;
3225 sym = intern (namestring);
3226 val = allocate_misc ();
3227 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3228 XKBOARD_OBJFWD (val)->offset = offset;
3229 XSYMBOL (sym)->value = val;
3232 /* Record the value of load-path used at the start of dumping
3233 so we can see if the site changed it later during dumping. */
3234 static Lisp_Object dump_path;
3236 void
3237 init_lread ()
3239 char *normal;
3240 int turn_off_warning = 0;
3242 /* Compute the default load-path. */
3243 #ifdef CANNOT_DUMP
3244 normal = PATH_LOADSEARCH;
3245 Vload_path = decode_env_path (0, normal);
3246 #else
3247 if (NILP (Vpurify_flag))
3248 normal = PATH_LOADSEARCH;
3249 else
3250 normal = PATH_DUMPLOADSEARCH;
3252 /* In a dumped Emacs, we normally have to reset the value of
3253 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3254 uses ../lisp, instead of the path of the installed elisp
3255 libraries. However, if it appears that Vload_path was changed
3256 from the default before dumping, don't override that value. */
3257 if (initialized)
3259 if (! NILP (Fequal (dump_path, Vload_path)))
3261 Vload_path = decode_env_path (0, normal);
3262 if (!NILP (Vinstallation_directory))
3264 /* Add to the path the lisp subdir of the
3265 installation dir, if it exists. */
3266 Lisp_Object tem, tem1;
3267 tem = Fexpand_file_name (build_string ("lisp"),
3268 Vinstallation_directory);
3269 tem1 = Ffile_exists_p (tem);
3270 if (!NILP (tem1))
3272 if (NILP (Fmember (tem, Vload_path)))
3274 turn_off_warning = 1;
3275 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3278 else
3279 /* That dir doesn't exist, so add the build-time
3280 Lisp dirs instead. */
3281 Vload_path = nconc2 (Vload_path, dump_path);
3283 /* Add leim under the installation dir, if it exists. */
3284 tem = Fexpand_file_name (build_string ("leim"),
3285 Vinstallation_directory);
3286 tem1 = Ffile_exists_p (tem);
3287 if (!NILP (tem1))
3289 if (NILP (Fmember (tem, Vload_path)))
3290 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3293 /* Add site-list under the installation dir, if it exists. */
3294 tem = Fexpand_file_name (build_string ("site-lisp"),
3295 Vinstallation_directory);
3296 tem1 = Ffile_exists_p (tem);
3297 if (!NILP (tem1))
3299 if (NILP (Fmember (tem, Vload_path)))
3300 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3303 /* If Emacs was not built in the source directory,
3304 and it is run from where it was built, add to load-path
3305 the lisp, leim and site-lisp dirs under that directory. */
3307 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3309 Lisp_Object tem2;
3311 tem = Fexpand_file_name (build_string ("src/Makefile"),
3312 Vinstallation_directory);
3313 tem1 = Ffile_exists_p (tem);
3315 /* Don't be fooled if they moved the entire source tree
3316 AFTER dumping Emacs. If the build directory is indeed
3317 different from the source dir, src/Makefile.in and
3318 src/Makefile will not be found together. */
3319 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3320 Vinstallation_directory);
3321 tem2 = Ffile_exists_p (tem);
3322 if (!NILP (tem1) && NILP (tem2))
3324 tem = Fexpand_file_name (build_string ("lisp"),
3325 Vsource_directory);
3327 if (NILP (Fmember (tem, Vload_path)))
3328 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3330 tem = Fexpand_file_name (build_string ("leim"),
3331 Vsource_directory);
3333 if (NILP (Fmember (tem, Vload_path)))
3334 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3336 tem = Fexpand_file_name (build_string ("site-lisp"),
3337 Vsource_directory);
3339 if (NILP (Fmember (tem, Vload_path)))
3340 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3346 else
3348 /* NORMAL refers to the lisp dir in the source directory. */
3349 /* We used to add ../lisp at the front here, but
3350 that caused trouble because it was copied from dump_path
3351 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3352 It should be unnecessary. */
3353 Vload_path = decode_env_path (0, normal);
3354 dump_path = Vload_path;
3356 #endif
3358 #ifndef WINDOWSNT
3359 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3360 almost never correct, thereby causing a warning to be printed out that
3361 confuses users. Since PATH_LOADSEARCH is always overridden by the
3362 EMACSLOADPATH environment variable below, disable the warning on NT. */
3364 /* Warn if dirs in the *standard* path don't exist. */
3365 if (!turn_off_warning)
3367 Lisp_Object path_tail;
3369 for (path_tail = Vload_path;
3370 !NILP (path_tail);
3371 path_tail = XCDR (path_tail))
3373 Lisp_Object dirfile;
3374 dirfile = Fcar (path_tail);
3375 if (STRINGP (dirfile))
3377 dirfile = Fdirectory_file_name (dirfile);
3378 if (access (XSTRING (dirfile)->data, 0) < 0)
3379 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3380 XCAR (path_tail));
3384 #endif /* WINDOWSNT */
3386 /* If the EMACSLOADPATH environment variable is set, use its value.
3387 This doesn't apply if we're dumping. */
3388 #ifndef CANNOT_DUMP
3389 if (NILP (Vpurify_flag)
3390 && egetenv ("EMACSLOADPATH"))
3391 #endif
3392 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3394 Vvalues = Qnil;
3396 load_in_progress = 0;
3397 Vload_file_name = Qnil;
3399 load_descriptor_list = Qnil;
3401 Vstandard_input = Qt;
3404 /* Print a warning, using format string FORMAT, that directory DIRNAME
3405 does not exist. Print it on stderr and put it in *Message*. */
3407 void
3408 dir_warning (format, dirname)
3409 char *format;
3410 Lisp_Object dirname;
3412 char *buffer
3413 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3415 fprintf (stderr, format, XSTRING (dirname)->data);
3416 sprintf (buffer, format, XSTRING (dirname)->data);
3417 /* Don't log the warning before we've initialized!! */
3418 if (initialized)
3419 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3422 void
3423 syms_of_lread ()
3425 defsubr (&Sread);
3426 defsubr (&Sread_from_string);
3427 defsubr (&Sintern);
3428 defsubr (&Sintern_soft);
3429 defsubr (&Sunintern);
3430 defsubr (&Sload);
3431 defsubr (&Seval_buffer);
3432 defsubr (&Seval_region);
3433 defsubr (&Sread_char);
3434 defsubr (&Sread_char_exclusive);
3435 defsubr (&Sread_event);
3436 defsubr (&Sget_file_char);
3437 defsubr (&Smapatoms);
3439 DEFVAR_LISP ("obarray", &Vobarray,
3440 "Symbol table for use by `intern' and `read'.\n\
3441 It is a vector whose length ought to be prime for best results.\n\
3442 The vector's contents don't make sense if examined from Lisp programs;\n\
3443 to find all the symbols in an obarray, use `mapatoms'.");
3445 DEFVAR_LISP ("values", &Vvalues,
3446 "List of values of all expressions which were read, evaluated and printed.\n\
3447 Order is reverse chronological.");
3449 DEFVAR_LISP ("standard-input", &Vstandard_input,
3450 "Stream for read to get input from.\n\
3451 See documentation of `read' for possible values.");
3452 Vstandard_input = Qt;
3454 DEFVAR_LISP ("load-path", &Vload_path,
3455 "*List of directories to search for files to load.\n\
3456 Each element is a string (directory name) or nil (try default directory).\n\
3457 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3458 otherwise to default specified by file `epaths.h' when Emacs was built.");
3460 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3461 "Non-nil iff inside of `load'.");
3463 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3464 "An alist of expressions to be evalled when particular files are loaded.\n\
3465 Each element looks like (FILENAME FORMS...).\n\
3466 When `load' is run and the file-name argument is FILENAME,\n\
3467 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3468 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3469 with no directory specified, since that is how `load' is normally called.\n\
3470 An error in FORMS does not undo the load,\n\
3471 but does prevent execution of the rest of the FORMS.");
3472 Vafter_load_alist = Qnil;
3474 DEFVAR_LISP ("load-history", &Vload_history,
3475 "Alist mapping source file names to symbols and features.\n\
3476 Each alist element is a list that starts with a file name,\n\
3477 except for one element (optional) that starts with nil and describes\n\
3478 definitions evaluated from buffers not visiting files.\n\
3479 The remaining elements of each list are symbols defined as functions\n\
3480 or variables, and cons cells `(provide . FEATURE)', `(require . FEATURE)',\n\
3481 and `(autoload . SYMBOL)'.");
3482 Vload_history = Qnil;
3484 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3485 "Full name of file being loaded by `load'.");
3486 Vload_file_name = Qnil;
3488 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3489 "File name, including directory, of user's initialization file.\n\
3490 If the file loaded had extension `.elc' and there was a corresponding `.el'\n\
3491 file, this variable contains the name of the .el file, suitable for use\n\
3492 by functions like `custom-save-all' which edit the init file.");
3493 Vuser_init_file = Qnil;
3495 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3496 "Used for internal purposes by `load'.");
3497 Vcurrent_load_list = Qnil;
3499 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3500 "Function used by `load' and `eval-region' for reading expressions.\n\
3501 The default is nil, which means use the function `read'.");
3502 Vload_read_function = Qnil;
3504 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3505 "Function called in `load' for loading an Emacs lisp source file.\n\
3506 This function is for doing code conversion before reading the source file.\n\
3507 If nil, loading is done without any code conversion.\n\
3508 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3509 FULLNAME is the full name of FILE.\n\
3510 See `load' for the meaning of the remaining arguments.");
3511 Vload_source_file_function = Qnil;
3513 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3514 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3515 This is useful when the file being loaded is a temporary copy.");
3516 load_force_doc_strings = 0;
3518 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3519 "Non-nil means `read' converts strings to unibyte whenever possible.\n\
3520 This is normally bound by `load' and `eval-buffer' to control `read',\n\
3521 and is not meant for users to change.");
3522 load_convert_to_unibyte = 0;
3524 DEFVAR_LISP ("source-directory", &Vsource_directory,
3525 "Directory in which Emacs sources were found when Emacs was built.\n\
3526 You cannot count on them to still be there!");
3527 Vsource_directory
3528 = Fexpand_file_name (build_string ("../"),
3529 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3531 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3532 "List of files that were preloaded (when dumping Emacs).");
3533 Vpreloaded_file_list = Qnil;
3535 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3536 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3537 Vbyte_boolean_vars = Qnil;
3539 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
3540 "Non-nil means load dangerous compiled Lisp files.\n\
3541 Some versions of XEmacs use different byte codes than Emacs. These\n\
3542 incompatible byte codes can make Emacs crash when it tries to execute\n\
3543 them.");
3544 load_dangerous_libraries = 0;
3546 Vbytecomp_version_regexp = build_string ("^;;;.in Emacs version");
3547 staticpro (&Vbytecomp_version_regexp);
3549 /* Vsource_directory was initialized in init_lread. */
3551 load_descriptor_list = Qnil;
3552 staticpro (&load_descriptor_list);
3554 Qcurrent_load_list = intern ("current-load-list");
3555 staticpro (&Qcurrent_load_list);
3557 Qstandard_input = intern ("standard-input");
3558 staticpro (&Qstandard_input);
3560 Qread_char = intern ("read-char");
3561 staticpro (&Qread_char);
3563 Qget_file_char = intern ("get-file-char");
3564 staticpro (&Qget_file_char);
3566 Qbackquote = intern ("`");
3567 staticpro (&Qbackquote);
3568 Qcomma = intern (",");
3569 staticpro (&Qcomma);
3570 Qcomma_at = intern (",@");
3571 staticpro (&Qcomma_at);
3572 Qcomma_dot = intern (",.");
3573 staticpro (&Qcomma_dot);
3575 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3576 staticpro (&Qinhibit_file_name_operation);
3578 Qascii_character = intern ("ascii-character");
3579 staticpro (&Qascii_character);
3581 Qfunction = intern ("function");
3582 staticpro (&Qfunction);
3584 Qload = intern ("load");
3585 staticpro (&Qload);
3587 Qload_file_name = intern ("load-file-name");
3588 staticpro (&Qload_file_name);
3590 staticpro (&dump_path);
3592 staticpro (&read_objects);
3593 read_objects = Qnil;
3594 staticpro (&seen_list);