Merge from emacs--devo--0
[emacs/old-mirror.git] / src / lread.c
blob0a1260eda4ed92fa01e6405072bfdf448e6dd18e
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29 #include <errno.h>
30 #include "lisp.h"
31 #include "intervals.h"
32 #include "buffer.h"
33 #include "character.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include <epaths.h>
37 #include "commands.h"
38 #include "keyboard.h"
39 #include "termhooks.h"
40 #include "coding.h"
41 #include "blockinput.h"
43 #ifdef lint
44 #include <sys/inode.h>
45 #endif /* lint */
47 #ifdef MSDOS
48 #if __DJGPP__ < 2
49 #include <unistd.h> /* to get X_OK */
50 #endif
51 #include "msdos.h"
52 #endif
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
58 #ifndef X_OK
59 #define X_OK 01
60 #endif
62 #include <math.h>
64 #ifdef HAVE_SETLOCALE
65 #include <locale.h>
66 #endif /* HAVE_SETLOCALE */
68 #ifdef HAVE_FCNTL_H
69 #include <fcntl.h>
70 #endif
71 #ifndef O_RDONLY
72 #define O_RDONLY 0
73 #endif
75 #ifdef HAVE_FSEEKO
76 #define file_offset off_t
77 #define file_tell ftello
78 #else
79 #define file_offset long
80 #define file_tell ftell
81 #endif
83 #ifndef USE_CRT_DLL
84 extern int errno;
85 #endif
87 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
88 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
89 Lisp_Object Qascii_character, Qload, Qload_file_name;
90 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
91 Lisp_Object Qinhibit_file_name_operation;
92 Lisp_Object Qeval_buffer_list, Veval_buffer_list;
93 Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
95 /* Used instead of Qget_file_char while loading *.elc files compiled
96 by Emacs 21 or older. */
97 static Lisp_Object Qget_emacs_mule_file_char;
99 static Lisp_Object Qload_force_doc_strings;
101 extern Lisp_Object Qevent_symbol_element_mask;
102 extern Lisp_Object Qfile_exists_p;
104 /* non-zero iff inside `load' */
105 int load_in_progress;
107 /* Directory in which the sources were found. */
108 Lisp_Object Vsource_directory;
110 /* Search path and suffixes for files to be loaded. */
111 Lisp_Object Vload_path, Vload_suffixes, Vload_file_rep_suffixes;
113 /* File name of user's init file. */
114 Lisp_Object Vuser_init_file;
116 /* This is the user-visible association list that maps features to
117 lists of defs in their load files. */
118 Lisp_Object Vload_history;
120 /* This is used to build the load history. */
121 Lisp_Object Vcurrent_load_list;
123 /* List of files that were preloaded. */
124 Lisp_Object Vpreloaded_file_list;
126 /* Name of file actually being read by `load'. */
127 Lisp_Object Vload_file_name;
129 /* Function to use for reading, in `load' and friends. */
130 Lisp_Object Vload_read_function;
132 /* The association list of objects read with the #n=object form.
133 Each member of the list has the form (n . object), and is used to
134 look up the object for the corresponding #n# construct.
135 It must be set to nil before all top-level calls to read0. */
136 Lisp_Object read_objects;
138 /* Nonzero means load should forcibly load all dynamic doc strings. */
139 static int load_force_doc_strings;
141 /* Nonzero means read should convert strings to unibyte. */
142 static int load_convert_to_unibyte;
144 /* Nonzero means READCHAR should read bytes one by one (not character)
145 when READCHARFUN is Qget_file_char or Qget_emacs_mule_file_char.
146 This is set to 1 by read1 temporarily while handling #@NUMBER. */
147 static int load_each_byte;
149 /* Function to use for loading an Emacs Lisp source file (not
150 compiled) instead of readevalloop. */
151 Lisp_Object Vload_source_file_function;
153 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
154 Lisp_Object Vbyte_boolean_vars;
156 /* Whether or not to add a `read-positions' property to symbols
157 read. */
158 Lisp_Object Vread_with_symbol_positions;
160 /* List of (SYMBOL . POSITION) accumulated so far. */
161 Lisp_Object Vread_symbol_positions_list;
163 /* List of descriptors now open for Fload. */
164 static Lisp_Object load_descriptor_list;
166 /* File for get_file_char to read from. Use by load. */
167 static FILE *instream;
169 /* When nonzero, read conses in pure space */
170 static int read_pure;
172 /* For use within read-from-string (this reader is non-reentrant!!) */
173 static int read_from_string_index;
174 static int read_from_string_index_byte;
175 static int read_from_string_limit;
177 /* Number of characters read in the current call to Fread or
178 Fread_from_string. */
179 static int readchar_count;
181 /* This contains the last string skipped with #@. */
182 static char *saved_doc_string;
183 /* Length of buffer allocated in saved_doc_string. */
184 static int saved_doc_string_size;
185 /* Length of actual data in saved_doc_string. */
186 static int saved_doc_string_length;
187 /* This is the file position that string came from. */
188 static file_offset saved_doc_string_position;
190 /* This contains the previous string skipped with #@.
191 We copy it from saved_doc_string when a new string
192 is put in saved_doc_string. */
193 static char *prev_saved_doc_string;
194 /* Length of buffer allocated in prev_saved_doc_string. */
195 static int prev_saved_doc_string_size;
196 /* Length of actual data in prev_saved_doc_string. */
197 static int prev_saved_doc_string_length;
198 /* This is the file position that string came from. */
199 static file_offset prev_saved_doc_string_position;
201 /* Nonzero means inside a new-style backquote
202 with no surrounding parentheses.
203 Fread initializes this to zero, so we need not specbind it
204 or worry about what happens to it when there is an error. */
205 static int new_backquote_flag;
207 /* A list of file names for files being loaded in Fload. Used to
208 check for recursive loads. */
210 static Lisp_Object Vloads_in_progress;
212 /* Non-zero means load dangerous compiled Lisp files. */
214 int load_dangerous_libraries;
216 /* A regular expression used to detect files compiled with Emacs. */
218 static Lisp_Object Vbytecomp_version_regexp;
220 static int read_emacs_mule_char P_ ((int, int (*) (int, Lisp_Object),
221 Lisp_Object));
223 static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
224 Lisp_Object (*) (), int,
225 Lisp_Object, Lisp_Object,
226 Lisp_Object, Lisp_Object));
227 static Lisp_Object load_unwind P_ ((Lisp_Object));
228 static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
230 static void invalid_syntax P_ ((const char *, int)) NO_RETURN;
231 static void end_of_file_error P_ (()) NO_RETURN;
234 /* Functions that read one byte from the current source READCHARFUN
235 or unreads one byte. If the integer argument C is -1, it returns
236 one read byte, or -1 when there's no more byte in the source. If C
237 is 0 or positive, it unreads C, and the return value is not
238 interesting. */
240 static int readbyte_for_lambda P_ ((int, Lisp_Object));
241 static int readbyte_from_file P_ ((int, Lisp_Object));
242 static int readbyte_from_string P_ ((int, Lisp_Object));
244 /* Handle unreading and rereading of characters.
245 Write READCHAR to read a character,
246 UNREAD(c) to unread c to be read again.
248 These macros correctly read/unread multibyte characters. */
250 #define READCHAR readchar (readcharfun)
251 #define UNREAD(c) unreadchar (readcharfun, c)
253 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
254 Qlambda, or a cons, we use this to keep an unread character because
255 a file stream can't handle multibyte-char unreading. The value -1
256 means that there's no unread character. */
257 static int unread_char;
259 static int
260 readchar (readcharfun)
261 Lisp_Object readcharfun;
263 Lisp_Object tem;
264 register int c;
265 int (*readbyte) P_ ((int, Lisp_Object));
266 unsigned char buf[MAX_MULTIBYTE_LENGTH];
267 int i, len;
268 int emacs_mule_encoding = 0;
270 readchar_count++;
272 if (BUFFERP (readcharfun))
274 register struct buffer *inbuffer = XBUFFER (readcharfun);
276 int pt_byte = BUF_PT_BYTE (inbuffer);
278 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
279 return -1;
281 if (! NILP (inbuffer->enable_multibyte_characters))
283 /* Fetch the character code from the buffer. */
284 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
285 BUF_INC_POS (inbuffer, pt_byte);
286 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
288 else
290 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
291 if (! ASCII_BYTE_P (c))
292 c = BYTE8_TO_CHAR (c);
293 pt_byte++;
295 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
297 return c;
299 if (MARKERP (readcharfun))
301 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
303 int bytepos = marker_byte_position (readcharfun);
305 if (bytepos >= BUF_ZV_BYTE (inbuffer))
306 return -1;
308 if (! NILP (inbuffer->enable_multibyte_characters))
310 /* Fetch the character code from the buffer. */
311 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
312 BUF_INC_POS (inbuffer, bytepos);
313 c = STRING_CHAR (p, bytepos - orig_bytepos);
315 else
317 c = BUF_FETCH_BYTE (inbuffer, bytepos);
318 if (! ASCII_BYTE_P (c))
319 c = BYTE8_TO_CHAR (c);
320 bytepos++;
323 XMARKER (readcharfun)->bytepos = bytepos;
324 XMARKER (readcharfun)->charpos++;
326 return c;
329 if (EQ (readcharfun, Qlambda))
331 readbyte = readbyte_for_lambda;
332 goto read_multibyte;
335 if (EQ (readcharfun, Qget_file_char))
337 readbyte = readbyte_from_file;
338 goto read_multibyte;
341 if (STRINGP (readcharfun))
343 if (read_from_string_index >= read_from_string_limit)
344 c = -1;
345 else
346 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
347 read_from_string_index,
348 read_from_string_index_byte);
350 return c;
353 if (CONSP (readcharfun))
355 /* This is the case that read_vector is reading from a unibyte
356 string that contains a byte sequence previously skipped
357 because of #@NUMBER. The car part of readcharfun is that
358 string, and the cdr part is a value of readcharfun given to
359 read_vector. */
360 readbyte = readbyte_from_string;
361 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
362 emacs_mule_encoding = 1;
363 goto read_multibyte;
366 if (EQ (readcharfun, Qget_emacs_mule_file_char))
368 readbyte = readbyte_from_file;
369 emacs_mule_encoding = 1;
370 goto read_multibyte;
373 tem = call0 (readcharfun);
375 if (NILP (tem))
376 return -1;
377 return XINT (tem);
379 read_multibyte:
380 if (unread_char >= 0)
382 c = unread_char;
383 unread_char = -1;
384 return c;
386 c = (*readbyte) (-1, readcharfun);
387 if (c < 0 || ASCII_BYTE_P (c) || load_each_byte)
388 return c;
389 if (emacs_mule_encoding)
390 return read_emacs_mule_char (c, readbyte, readcharfun);
391 i = 0;
392 buf[i++] = c;
393 len = BYTES_BY_CHAR_HEAD (c);
394 while (i < len)
396 c = (*readbyte) (-1, readcharfun);
397 if (c < 0 || ! TRAILING_CODE_P (c))
399 while (--i > 1)
400 (*readbyte) (buf[i], readcharfun);
401 return BYTE8_TO_CHAR (buf[0]);
403 buf[i++] = c;
405 return STRING_CHAR (buf, i);
408 /* Unread the character C in the way appropriate for the stream READCHARFUN.
409 If the stream is a user function, call it with the char as argument. */
411 static void
412 unreadchar (readcharfun, c)
413 Lisp_Object readcharfun;
414 int c;
416 readchar_count--;
417 if (c == -1)
418 /* Don't back up the pointer if we're unreading the end-of-input mark,
419 since readchar didn't advance it when we read it. */
421 else if (BUFFERP (readcharfun))
423 struct buffer *b = XBUFFER (readcharfun);
424 int bytepos = BUF_PT_BYTE (b);
426 BUF_PT (b)--;
427 if (! NILP (b->enable_multibyte_characters))
428 BUF_DEC_POS (b, bytepos);
429 else
430 bytepos--;
432 BUF_PT_BYTE (b) = bytepos;
434 else if (MARKERP (readcharfun))
436 struct buffer *b = XMARKER (readcharfun)->buffer;
437 int bytepos = XMARKER (readcharfun)->bytepos;
439 XMARKER (readcharfun)->charpos--;
440 if (! NILP (b->enable_multibyte_characters))
441 BUF_DEC_POS (b, bytepos);
442 else
443 bytepos--;
445 XMARKER (readcharfun)->bytepos = bytepos;
447 else if (STRINGP (readcharfun))
449 read_from_string_index--;
450 read_from_string_index_byte
451 = string_char_to_byte (readcharfun, read_from_string_index);
453 else if (CONSP (readcharfun))
455 unread_char = c;
457 else if (EQ (readcharfun, Qlambda))
459 unread_char = c;
461 else if (EQ (readcharfun, Qget_file_char)
462 || EQ (readcharfun, Qget_emacs_mule_file_char))
464 if (load_each_byte)
466 BLOCK_INPUT;
467 ungetc (c, instream);
468 UNBLOCK_INPUT;
470 else
471 unread_char = c;
473 else
474 call1 (readcharfun, make_number (c));
477 static int
478 readbyte_for_lambda (c, readcharfun)
479 int c;
480 Lisp_Object readcharfun;
482 return read_bytecode_char (c >= 0);
486 static int
487 readbyte_from_file (c, readcharfun)
488 int c;
489 Lisp_Object readcharfun;
491 if (c >= 0)
493 BLOCK_INPUT;
494 ungetc (c, instream);
495 UNBLOCK_INPUT;
496 return 0;
499 BLOCK_INPUT;
500 c = getc (instream);
502 #ifdef EINTR
503 /* Interrupted reads have been observed while reading over the network */
504 while (c == EOF && ferror (instream) && errno == EINTR)
506 UNBLOCK_INPUT;
507 QUIT;
508 BLOCK_INPUT;
509 clearerr (instream);
510 c = getc (instream);
512 #endif
514 UNBLOCK_INPUT;
516 return (c == EOF ? -1 : c);
519 static int
520 readbyte_from_string (c, readcharfun)
521 int c;
522 Lisp_Object readcharfun;
524 Lisp_Object string = XCAR (readcharfun);
526 if (c >= 0)
528 read_from_string_index--;
529 read_from_string_index_byte
530 = string_char_to_byte (string, read_from_string_index);
533 if (read_from_string_index >= read_from_string_limit)
534 c = -1;
535 else
536 FETCH_STRING_CHAR_ADVANCE (c, string,
537 read_from_string_index,
538 read_from_string_index_byte);
539 return c;
543 /* Read one non-ASCII character from INSTREAM. The character is
544 encoded in `emacs-mule' and the first byte is already read in
545 C. */
547 extern char emacs_mule_bytes[256];
549 static int
550 read_emacs_mule_char (c, readbyte, readcharfun)
551 int c;
552 int (*readbyte) P_ ((int, Lisp_Object));
553 Lisp_Object readcharfun;
555 /* Emacs-mule coding uses at most 4-byte for one character. */
556 unsigned char buf[4];
557 int len = emacs_mule_bytes[c];
558 struct charset *charset;
559 int i;
560 unsigned code;
562 if (len == 1)
563 /* C is not a valid leading-code of `emacs-mule'. */
564 return BYTE8_TO_CHAR (c);
566 i = 0;
567 buf[i++] = c;
568 while (i < len)
570 c = (*readbyte) (-1, readcharfun);
571 if (c < 0xA0)
573 while (--i > 1)
574 (*readbyte) (buf[i], readcharfun);
575 return BYTE8_TO_CHAR (buf[0]);
577 buf[i++] = c;
580 if (len == 2)
582 charset = emacs_mule_charset[buf[0]];
583 code = buf[1] & 0x7F;
585 else if (len == 3)
587 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
588 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
590 charset = emacs_mule_charset[buf[1]];
591 code = buf[2] & 0x7F;
593 else
595 charset = emacs_mule_charset[buf[0]];
596 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
599 else
601 charset = emacs_mule_charset[buf[1]];
602 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
604 c = DECODE_CHAR (charset, code);
605 if (c < 0)
606 Fsignal (Qinvalid_read_syntax,
607 Fcons (build_string ("invalid multibyte form"), Qnil));
608 return c;
612 static Lisp_Object read_internal_start P_ ((Lisp_Object, Lisp_Object,
613 Lisp_Object));
614 static Lisp_Object read0 P_ ((Lisp_Object));
615 static Lisp_Object read1 P_ ((Lisp_Object, int *, int));
617 static Lisp_Object read_list P_ ((int, Lisp_Object));
618 static Lisp_Object read_vector P_ ((Lisp_Object, int));
620 static Lisp_Object substitute_object_recurse P_ ((Lisp_Object, Lisp_Object,
621 Lisp_Object));
622 static void substitute_object_in_subtree P_ ((Lisp_Object,
623 Lisp_Object));
624 static void substitute_in_interval P_ ((INTERVAL, Lisp_Object));
627 /* Get a character from the tty. */
629 extern Lisp_Object read_char ();
631 /* Read input events until we get one that's acceptable for our purposes.
633 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
634 until we get a character we like, and then stuffed into
635 unread_switch_frame.
637 If ASCII_REQUIRED is non-zero, we check function key events to see
638 if the unmodified version of the symbol has a Qascii_character
639 property, and use that character, if present.
641 If ERROR_NONASCII is non-zero, we signal an error if the input we
642 get isn't an ASCII character with modifiers. If it's zero but
643 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
644 character.
646 If INPUT_METHOD is nonzero, we invoke the current input method
647 if the character warrants that.
649 If SECONDS is a number, we wait that many seconds for input, and
650 return Qnil if no input arrives within that time. */
652 Lisp_Object
653 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
654 input_method, seconds)
655 int no_switch_frame, ascii_required, error_nonascii, input_method;
656 Lisp_Object seconds;
658 Lisp_Object val, delayed_switch_frame;
659 EMACS_TIME end_time;
661 #ifdef HAVE_WINDOW_SYSTEM
662 if (display_hourglass_p)
663 cancel_hourglass ();
664 #endif
666 delayed_switch_frame = Qnil;
668 /* Compute timeout. */
669 if (NUMBERP (seconds))
671 EMACS_TIME wait_time;
672 int sec, usec;
673 double duration = extract_float (seconds);
675 sec = (int) duration;
676 usec = (duration - sec) * 1000000;
677 EMACS_GET_TIME (end_time);
678 EMACS_SET_SECS_USECS (wait_time, sec, usec);
679 EMACS_ADD_TIME (end_time, end_time, wait_time);
682 /* Read until we get an acceptable event. */
683 retry:
684 val = read_char (0, 0, 0, (input_method ? Qnil : Qt), 0,
685 NUMBERP (seconds) ? &end_time : NULL);
687 if (BUFFERP (val))
688 goto retry;
690 /* switch-frame events are put off until after the next ASCII
691 character. This is better than signaling an error just because
692 the last characters were typed to a separate minibuffer frame,
693 for example. Eventually, some code which can deal with
694 switch-frame events will read it and process it. */
695 if (no_switch_frame
696 && EVENT_HAS_PARAMETERS (val)
697 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
699 delayed_switch_frame = val;
700 goto retry;
703 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
705 /* Convert certain symbols to their ASCII equivalents. */
706 if (SYMBOLP (val))
708 Lisp_Object tem, tem1;
709 tem = Fget (val, Qevent_symbol_element_mask);
710 if (!NILP (tem))
712 tem1 = Fget (Fcar (tem), Qascii_character);
713 /* Merge this symbol's modifier bits
714 with the ASCII equivalent of its basic code. */
715 if (!NILP (tem1))
716 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
720 /* If we don't have a character now, deal with it appropriately. */
721 if (!INTEGERP (val))
723 if (error_nonascii)
725 Vunread_command_events = Fcons (val, Qnil);
726 error ("Non-character input-event");
728 else
729 goto retry;
733 if (! NILP (delayed_switch_frame))
734 unread_switch_frame = delayed_switch_frame;
736 #if 0
738 #ifdef HAVE_WINDOW_SYSTEM
739 if (display_hourglass_p)
740 start_hourglass ();
741 #endif
743 #endif
745 return val;
748 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
749 doc: /* Read a character from the command input (keyboard or macro).
750 It is returned as a number.
751 If the user generates an event which is not a character (i.e. a mouse
752 click or function key event), `read-char' signals an error. As an
753 exception, switch-frame events are put off until non-ASCII events can
754 be read.
755 If you want to read non-character events, or ignore them, call
756 `read-event' or `read-char-exclusive' instead.
758 If the optional argument PROMPT is non-nil, display that as a prompt.
759 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
760 input method is turned on in the current buffer, that input method
761 is used for reading a character.
762 If the optional argument SECONDS is non-nil, it should be a number
763 specifying the maximum number of seconds to wait for input. If no
764 input arrives in that time, return nil. SECONDS may be a
765 floating-point value. */)
766 (prompt, inherit_input_method, seconds)
767 Lisp_Object prompt, inherit_input_method, seconds;
769 if (! NILP (prompt))
770 message_with_string ("%s", prompt, 0);
771 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
774 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
775 doc: /* Read an event object from the input stream.
776 If the optional argument PROMPT is non-nil, display that as a prompt.
777 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
778 input method is turned on in the current buffer, that input method
779 is used for reading a character.
780 If the optional argument SECONDS is non-nil, it should be a number
781 specifying the maximum number of seconds to wait for input. If no
782 input arrives in that time, return nil. SECONDS may be a
783 floating-point value. */)
784 (prompt, inherit_input_method, seconds)
785 Lisp_Object prompt, inherit_input_method, seconds;
787 if (! NILP (prompt))
788 message_with_string ("%s", prompt, 0);
789 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
792 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
793 doc: /* Read a character from the command input (keyboard or macro).
794 It is returned as a number. Non-character events are ignored.
796 If the optional argument PROMPT is non-nil, display that as a prompt.
797 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
798 input method is turned on in the current buffer, that input method
799 is used for reading a character.
800 If the optional argument SECONDS is non-nil, it should be a number
801 specifying the maximum number of seconds to wait for input. If no
802 input arrives in that time, return nil. SECONDS may be a
803 floating-point value. */)
804 (prompt, inherit_input_method, seconds)
805 Lisp_Object prompt, inherit_input_method, seconds;
807 if (! NILP (prompt))
808 message_with_string ("%s", prompt, 0);
809 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
812 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
813 doc: /* Don't use this yourself. */)
816 register Lisp_Object val;
817 BLOCK_INPUT;
818 XSETINT (val, getc (instream));
819 UNBLOCK_INPUT;
820 return val;
825 /* Value is a version number of byte compiled code if the file
826 asswociated with file descriptor FD is a compiled Lisp file that's
827 safe to load. Only files compiled with Emacs are safe to load.
828 Files compiled with XEmacs can lead to a crash in Fbyte_code
829 because of an incompatible change in the byte compiler. */
831 static int
832 safe_to_load_p (fd)
833 int fd;
835 char buf[512];
836 int nbytes, i;
837 int safe_p = 1;
838 int version = 1;
840 /* Read the first few bytes from the file, and look for a line
841 specifying the byte compiler version used. */
842 nbytes = emacs_read (fd, buf, sizeof buf - 1);
843 if (nbytes > 0)
845 buf[nbytes] = '\0';
847 /* Skip to the next newline, skipping over the initial `ELC'
848 with NUL bytes following it, but note the version. */
849 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
850 if (i == 4)
851 version = buf[i];
853 if (i == nbytes
854 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
855 buf + i) < 0)
856 safe_p = 0;
858 if (safe_p)
859 safe_p = version;
861 lseek (fd, 0, SEEK_SET);
862 return safe_p;
866 /* Callback for record_unwind_protect. Restore the old load list OLD,
867 after loading a file successfully. */
869 static Lisp_Object
870 record_load_unwind (old)
871 Lisp_Object old;
873 return Vloads_in_progress = old;
876 /* This handler function is used via internal_condition_case_1. */
878 static Lisp_Object
879 load_error_handler (data)
880 Lisp_Object data;
882 return Qnil;
885 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
886 doc: /* Return the suffixes that `load' should try if a suffix is \
887 required.
888 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
891 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
892 while (CONSP (suffixes))
894 Lisp_Object exts = Vload_file_rep_suffixes;
895 suffix = XCAR (suffixes);
896 suffixes = XCDR (suffixes);
897 while (CONSP (exts))
899 ext = XCAR (exts);
900 exts = XCDR (exts);
901 lst = Fcons (concat2 (suffix, ext), lst);
904 return Fnreverse (lst);
907 DEFUN ("load", Fload, Sload, 1, 5, 0,
908 doc: /* Execute a file of Lisp code named FILE.
909 First try FILE with `.elc' appended, then try with `.el',
910 then try FILE unmodified (the exact suffixes in the exact order are
911 determined by `load-suffixes'). Environment variable references in
912 FILE are replaced with their values by calling `substitute-in-file-name'.
913 This function searches the directories in `load-path'.
915 If optional second arg NOERROR is non-nil,
916 report no error if FILE doesn't exist.
917 Print messages at start and end of loading unless
918 optional third arg NOMESSAGE is non-nil.
919 If optional fourth arg NOSUFFIX is non-nil, don't try adding
920 suffixes `.elc' or `.el' to the specified name FILE.
921 If optional fifth arg MUST-SUFFIX is non-nil, insist on
922 the suffix `.elc' or `.el'; don't accept just FILE unless
923 it ends in one of those suffixes or includes a directory name.
925 If this function fails to find a file, it may look for different
926 representations of that file before trying another file.
927 It does so by adding the non-empty suffixes in `load-file-rep-suffixes'
928 to the file name. Emacs uses this feature mainly to find compressed
929 versions of files when Auto Compression mode is enabled.
931 The exact suffixes that this function tries out, in the exact order,
932 are given by the value of the variable `load-file-rep-suffixes' if
933 NOSUFFIX is non-nil and by the return value of the function
934 `get-load-suffixes' if MUST-SUFFIX is non-nil. If both NOSUFFIX and
935 MUST-SUFFIX are nil, this function first tries out the latter suffixes
936 and then the former.
938 Loading a file records its definitions, and its `provide' and
939 `require' calls, in an element of `load-history' whose
940 car is the file name loaded. See `load-history'.
942 Return t if the file exists and loads successfully. */)
943 (file, noerror, nomessage, nosuffix, must_suffix)
944 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
946 register FILE *stream;
947 register int fd = -1;
948 int count = SPECPDL_INDEX ();
949 Lisp_Object temp;
950 struct gcpro gcpro1, gcpro2, gcpro3;
951 Lisp_Object found, efound, hist_file_name;
952 /* 1 means we printed the ".el is newer" message. */
953 int newer = 0;
954 /* 1 means we are loading a compiled file. */
955 int compiled = 0;
956 Lisp_Object handler;
957 int safe_p = 1;
958 char *fmode = "r";
959 Lisp_Object tmp[2];
960 int version;
962 #ifdef DOS_NT
963 fmode = "rt";
964 #endif /* DOS_NT */
966 CHECK_STRING (file);
968 /* If file name is magic, call the handler. */
969 /* This shouldn't be necessary any more now that `openp' handles it right.
970 handler = Ffind_file_name_handler (file, Qload);
971 if (!NILP (handler))
972 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
974 /* Do this after the handler to avoid
975 the need to gcpro noerror, nomessage and nosuffix.
976 (Below here, we care only whether they are nil or not.)
977 The presence of this call is the result of a historical accident:
978 it used to be in every file-operation and when it got removed
979 everywhere, it accidentally stayed here. Since then, enough people
980 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
981 that it seemed risky to remove. */
982 if (! NILP (noerror))
984 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
985 Qt, load_error_handler);
986 if (NILP (file))
987 return Qnil;
989 else
990 file = Fsubstitute_in_file_name (file);
993 /* Avoid weird lossage with null string as arg,
994 since it would try to load a directory as a Lisp file */
995 if (SCHARS (file) > 0)
997 int size = SBYTES (file);
999 found = Qnil;
1000 GCPRO2 (file, found);
1002 if (! NILP (must_suffix))
1004 /* Don't insist on adding a suffix if FILE already ends with one. */
1005 if (size > 3
1006 && !strcmp (SDATA (file) + size - 3, ".el"))
1007 must_suffix = Qnil;
1008 else if (size > 4
1009 && !strcmp (SDATA (file) + size - 4, ".elc"))
1010 must_suffix = Qnil;
1011 /* Don't insist on adding a suffix
1012 if the argument includes a directory name. */
1013 else if (! NILP (Ffile_name_directory (file)))
1014 must_suffix = Qnil;
1017 fd = openp (Vload_path, file,
1018 (!NILP (nosuffix) ? Qnil
1019 : !NILP (must_suffix) ? Fget_load_suffixes ()
1020 : Fappend (2, (tmp[0] = Fget_load_suffixes (),
1021 tmp[1] = Vload_file_rep_suffixes,
1022 tmp))),
1023 &found, Qnil);
1024 UNGCPRO;
1027 if (fd == -1)
1029 if (NILP (noerror))
1030 xsignal2 (Qfile_error, build_string ("Cannot open load file"), file);
1031 return Qnil;
1034 /* Tell startup.el whether or not we found the user's init file. */
1035 if (EQ (Qt, Vuser_init_file))
1036 Vuser_init_file = found;
1038 /* If FD is -2, that means openp found a magic file. */
1039 if (fd == -2)
1041 if (NILP (Fequal (found, file)))
1042 /* If FOUND is a different file name from FILE,
1043 find its handler even if we have already inhibited
1044 the `load' operation on FILE. */
1045 handler = Ffind_file_name_handler (found, Qt);
1046 else
1047 handler = Ffind_file_name_handler (found, Qload);
1048 if (! NILP (handler))
1049 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1052 /* Check if we're stuck in a recursive load cycle.
1054 2000-09-21: It's not possible to just check for the file loaded
1055 being a member of Vloads_in_progress. This fails because of the
1056 way the byte compiler currently works; `provide's are not
1057 evaluted, see font-lock.el/jit-lock.el as an example. This
1058 leads to a certain amount of ``normal'' recursion.
1060 Also, just loading a file recursively is not always an error in
1061 the general case; the second load may do something different. */
1063 int count = 0;
1064 Lisp_Object tem;
1065 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1066 if (!NILP (Fequal (found, XCAR (tem))))
1067 count++;
1068 if (count > 3)
1070 if (fd >= 0)
1071 emacs_close (fd);
1072 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1074 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1075 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1078 /* Get the name for load-history. */
1079 hist_file_name = (! NILP (Vpurify_flag)
1080 ? Fconcat (2, (tmp[0] = Ffile_name_directory (file),
1081 tmp[1] = Ffile_name_nondirectory (found),
1082 tmp))
1083 : found) ;
1085 version = -1;
1086 if (!bcmp (SDATA (found) + SBYTES (found) - 4,
1087 ".elc", 4)
1088 || (version = safe_to_load_p (fd)) > 0)
1089 /* Load .elc files directly, but not when they are
1090 remote and have no handler! */
1092 if (fd != -2)
1094 struct stat s1, s2;
1095 int result;
1097 GCPRO3 (file, found, hist_file_name);
1099 if (version < 0
1100 && ! (version = safe_to_load_p (fd)))
1102 safe_p = 0;
1103 if (!load_dangerous_libraries)
1105 if (fd >= 0)
1106 emacs_close (fd);
1107 error ("File `%s' was not compiled in Emacs",
1108 SDATA (found));
1110 else if (!NILP (nomessage))
1111 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1114 compiled = 1;
1116 efound = ENCODE_FILE (found);
1118 #ifdef DOS_NT
1119 fmode = "rb";
1120 #endif /* DOS_NT */
1121 stat ((char *)SDATA (efound), &s1);
1122 SSET (efound, SBYTES (efound) - 1, 0);
1123 result = stat ((char *)SDATA (efound), &s2);
1124 SSET (efound, SBYTES (efound) - 1, 'c');
1126 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
1128 /* Make the progress messages mention that source is newer. */
1129 newer = 1;
1131 /* If we won't print another message, mention this anyway. */
1132 if (!NILP (nomessage))
1134 Lisp_Object msg_file;
1135 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1136 message_with_string ("Source file `%s' newer than byte-compiled file",
1137 msg_file, 1);
1140 UNGCPRO;
1143 else
1145 /* We are loading a source file (*.el). */
1146 if (!NILP (Vload_source_file_function))
1148 Lisp_Object val;
1150 if (fd >= 0)
1151 emacs_close (fd);
1152 val = call4 (Vload_source_file_function, found, hist_file_name,
1153 NILP (noerror) ? Qnil : Qt,
1154 NILP (nomessage) ? Qnil : Qt);
1155 return unbind_to (count, val);
1159 GCPRO3 (file, found, hist_file_name);
1161 #ifdef WINDOWSNT
1162 emacs_close (fd);
1163 efound = ENCODE_FILE (found);
1164 stream = fopen ((char *) SDATA (efound), fmode);
1165 #else /* not WINDOWSNT */
1166 stream = fdopen (fd, fmode);
1167 #endif /* not WINDOWSNT */
1168 if (stream == 0)
1170 emacs_close (fd);
1171 error ("Failure to create stdio stream for %s", SDATA (file));
1174 if (! NILP (Vpurify_flag))
1175 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
1177 if (NILP (nomessage))
1179 if (!safe_p)
1180 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1181 file, 1);
1182 else if (!compiled)
1183 message_with_string ("Loading %s (source)...", file, 1);
1184 else if (newer)
1185 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1186 file, 1);
1187 else /* The typical case; compiled file newer than source file. */
1188 message_with_string ("Loading %s...", file, 1);
1191 record_unwind_protect (load_unwind, make_save_value (stream, 0));
1192 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
1193 specbind (Qload_file_name, found);
1194 specbind (Qinhibit_file_name_operation, Qnil);
1195 load_descriptor_list
1196 = Fcons (make_number (fileno (stream)), load_descriptor_list);
1197 load_in_progress++;
1198 if (! version || version >= 22)
1199 readevalloop (Qget_file_char, stream, hist_file_name,
1200 Feval, 0, Qnil, Qnil, Qnil, Qnil);
1201 else
1203 /* We can't handle a file which was compiled with
1204 byte-compile-dynamic by older version of Emacs. */
1205 specbind (Qload_force_doc_strings, Qt);
1206 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name, Feval,
1207 0, Qnil, Qnil, Qnil, Qnil);
1209 unbind_to (count, Qnil);
1211 /* Run any eval-after-load forms for this file */
1212 if (NILP (Vpurify_flag)
1213 && (!NILP (Ffboundp (Qdo_after_load_evaluation))))
1214 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1216 UNGCPRO;
1218 if (saved_doc_string)
1219 free (saved_doc_string);
1220 saved_doc_string = 0;
1221 saved_doc_string_size = 0;
1223 if (prev_saved_doc_string)
1224 xfree (prev_saved_doc_string);
1225 prev_saved_doc_string = 0;
1226 prev_saved_doc_string_size = 0;
1228 if (!noninteractive && NILP (nomessage))
1230 if (!safe_p)
1231 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1232 file, 1);
1233 else if (!compiled)
1234 message_with_string ("Loading %s (source)...done", file, 1);
1235 else if (newer)
1236 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1237 file, 1);
1238 else /* The typical case; compiled file newer than source file. */
1239 message_with_string ("Loading %s...done", file, 1);
1242 if (!NILP (Fequal (build_string ("obsolete"),
1243 Ffile_name_nondirectory
1244 (Fdirectory_file_name (Ffile_name_directory (found))))))
1245 message_with_string ("Package %s is obsolete", file, 1);
1247 return Qt;
1250 static Lisp_Object
1251 load_unwind (arg) /* used as unwind-protect function in load */
1252 Lisp_Object arg;
1254 FILE *stream = (FILE *) XSAVE_VALUE (arg)->pointer;
1255 if (stream != NULL)
1257 BLOCK_INPUT;
1258 fclose (stream);
1259 UNBLOCK_INPUT;
1261 if (--load_in_progress < 0) load_in_progress = 0;
1262 return Qnil;
1265 static Lisp_Object
1266 load_descriptor_unwind (oldlist)
1267 Lisp_Object oldlist;
1269 load_descriptor_list = oldlist;
1270 return Qnil;
1273 /* Close all descriptors in use for Floads.
1274 This is used when starting a subprocess. */
1276 void
1277 close_load_descs ()
1279 #ifndef WINDOWSNT
1280 Lisp_Object tail;
1281 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
1282 emacs_close (XFASTINT (XCAR (tail)));
1283 #endif
1286 static int
1287 complete_filename_p (pathname)
1288 Lisp_Object pathname;
1290 register const unsigned char *s = SDATA (pathname);
1291 return (IS_DIRECTORY_SEP (s[0])
1292 || (SCHARS (pathname) > 2
1293 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
1294 #ifdef ALTOS
1295 || *s == '@'
1296 #endif
1297 #ifdef VMS
1298 || index (s, ':')
1299 #endif /* VMS */
1303 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1304 doc: /* Search for FILENAME through PATH.
1305 Returns the file's name in absolute form, or nil if not found.
1306 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1307 file name when searching.
1308 If non-nil, PREDICATE is used instead of `file-readable-p'.
1309 PREDICATE can also be an integer to pass to the access(2) function,
1310 in which case file-name-handlers are ignored. */)
1311 (filename, path, suffixes, predicate)
1312 Lisp_Object filename, path, suffixes, predicate;
1314 Lisp_Object file;
1315 int fd = openp (path, filename, suffixes, &file, predicate);
1316 if (NILP (predicate) && fd > 0)
1317 close (fd);
1318 return file;
1322 /* Search for a file whose name is STR, looking in directories
1323 in the Lisp list PATH, and trying suffixes from SUFFIX.
1324 On success, returns a file descriptor. On failure, returns -1.
1326 SUFFIXES is a list of strings containing possible suffixes.
1327 The empty suffix is automatically added iff the list is empty.
1329 PREDICATE non-nil means don't open the files,
1330 just look for one that satisfies the predicate. In this case,
1331 returns 1 on success. The predicate can be a lisp function or
1332 an integer to pass to `access' (in which case file-name-handlers
1333 are ignored).
1335 If STOREPTR is nonzero, it points to a slot where the name of
1336 the file actually found should be stored as a Lisp string.
1337 nil is stored there on failure.
1339 If the file we find is remote, return -2
1340 but store the found remote file name in *STOREPTR. */
1343 openp (path, str, suffixes, storeptr, predicate)
1344 Lisp_Object path, str;
1345 Lisp_Object suffixes;
1346 Lisp_Object *storeptr;
1347 Lisp_Object predicate;
1349 register int fd;
1350 int fn_size = 100;
1351 char buf[100];
1352 register char *fn = buf;
1353 int absolute = 0;
1354 int want_size;
1355 Lisp_Object filename;
1356 struct stat st;
1357 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
1358 Lisp_Object string, tail, encoded_fn;
1359 int max_suffix_len = 0;
1361 CHECK_STRING (str);
1363 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1365 CHECK_STRING_CAR (tail);
1366 max_suffix_len = max (max_suffix_len,
1367 SBYTES (XCAR (tail)));
1370 string = filename = encoded_fn = Qnil;
1371 GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
1373 if (storeptr)
1374 *storeptr = Qnil;
1376 if (complete_filename_p (str))
1377 absolute = 1;
1379 for (; CONSP (path); path = XCDR (path))
1381 filename = Fexpand_file_name (str, XCAR (path));
1382 if (!complete_filename_p (filename))
1383 /* If there are non-absolute elts in PATH (eg ".") */
1384 /* Of course, this could conceivably lose if luser sets
1385 default-directory to be something non-absolute... */
1387 filename = Fexpand_file_name (filename, current_buffer->directory);
1388 if (!complete_filename_p (filename))
1389 /* Give up on this path element! */
1390 continue;
1393 /* Calculate maximum size of any filename made from
1394 this path element/specified file name and any possible suffix. */
1395 want_size = max_suffix_len + SBYTES (filename) + 1;
1396 if (fn_size < want_size)
1397 fn = (char *) alloca (fn_size = 100 + want_size);
1399 /* Loop over suffixes. */
1400 for (tail = NILP (suffixes) ? Fcons (empty_unibyte_string, Qnil) : suffixes;
1401 CONSP (tail); tail = XCDR (tail))
1403 int lsuffix = SBYTES (XCAR (tail));
1404 Lisp_Object handler;
1405 int exists;
1407 /* Concatenate path element/specified name with the suffix.
1408 If the directory starts with /:, remove that. */
1409 if (SCHARS (filename) > 2
1410 && SREF (filename, 0) == '/'
1411 && SREF (filename, 1) == ':')
1413 strncpy (fn, SDATA (filename) + 2,
1414 SBYTES (filename) - 2);
1415 fn[SBYTES (filename) - 2] = 0;
1417 else
1419 strncpy (fn, SDATA (filename),
1420 SBYTES (filename));
1421 fn[SBYTES (filename)] = 0;
1424 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
1425 strncat (fn, SDATA (XCAR (tail)), lsuffix);
1427 /* Check that the file exists and is not a directory. */
1428 /* We used to only check for handlers on non-absolute file names:
1429 if (absolute)
1430 handler = Qnil;
1431 else
1432 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1433 It's not clear why that was the case and it breaks things like
1434 (load "/bar.el") where the file is actually "/bar.el.gz". */
1435 string = build_string (fn);
1436 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1437 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1439 if (NILP (predicate))
1440 exists = !NILP (Ffile_readable_p (string));
1441 else
1442 exists = !NILP (call1 (predicate, string));
1443 if (exists && !NILP (Ffile_directory_p (string)))
1444 exists = 0;
1446 if (exists)
1448 /* We succeeded; return this descriptor and filename. */
1449 if (storeptr)
1450 *storeptr = string;
1451 UNGCPRO;
1452 return -2;
1455 else
1457 const char *pfn;
1459 encoded_fn = ENCODE_FILE (string);
1460 pfn = SDATA (encoded_fn);
1461 exists = (stat (pfn, &st) >= 0
1462 && (st.st_mode & S_IFMT) != S_IFDIR);
1463 if (exists)
1465 /* Check that we can access or open it. */
1466 if (NATNUMP (predicate))
1467 fd = (access (pfn, XFASTINT (predicate)) == 0) ? 1 : -1;
1468 else
1469 fd = emacs_open (pfn, O_RDONLY, 0);
1471 if (fd >= 0)
1473 /* We succeeded; return this descriptor and filename. */
1474 if (storeptr)
1475 *storeptr = string;
1476 UNGCPRO;
1477 return fd;
1482 if (absolute)
1483 break;
1486 UNGCPRO;
1487 return -1;
1491 /* Merge the list we've accumulated of globals from the current input source
1492 into the load_history variable. The details depend on whether
1493 the source has an associated file name or not.
1495 FILENAME is the file name that we are loading from.
1496 ENTIRE is 1 if loading that entire file, 0 if evaluating part of it. */
1498 static void
1499 build_load_history (filename, entire)
1500 Lisp_Object filename;
1501 int entire;
1503 register Lisp_Object tail, prev, newelt;
1504 register Lisp_Object tem, tem2;
1505 register int foundit = 0;
1507 tail = Vload_history;
1508 prev = Qnil;
1510 while (CONSP (tail))
1512 tem = XCAR (tail);
1514 /* Find the feature's previous assoc list... */
1515 if (!NILP (Fequal (filename, Fcar (tem))))
1517 foundit = 1;
1519 /* If we're loading the entire file, remove old data. */
1520 if (entire)
1522 if (NILP (prev))
1523 Vload_history = XCDR (tail);
1524 else
1525 Fsetcdr (prev, XCDR (tail));
1528 /* Otherwise, cons on new symbols that are not already members. */
1529 else
1531 tem2 = Vcurrent_load_list;
1533 while (CONSP (tem2))
1535 newelt = XCAR (tem2);
1537 if (NILP (Fmember (newelt, tem)))
1538 Fsetcar (tail, Fcons (XCAR (tem),
1539 Fcons (newelt, XCDR (tem))));
1541 tem2 = XCDR (tem2);
1542 QUIT;
1546 else
1547 prev = tail;
1548 tail = XCDR (tail);
1549 QUIT;
1552 /* If we're loading an entire file, cons the new assoc onto the
1553 front of load-history, the most-recently-loaded position. Also
1554 do this if we didn't find an existing member for the file. */
1555 if (entire || !foundit)
1556 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1557 Vload_history);
1560 Lisp_Object
1561 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1562 Lisp_Object junk;
1564 read_pure = 0;
1565 return Qnil;
1568 static Lisp_Object
1569 readevalloop_1 (old)
1570 Lisp_Object old;
1572 load_convert_to_unibyte = ! NILP (old);
1573 return Qnil;
1576 /* Signal an `end-of-file' error, if possible with file name
1577 information. */
1579 static void
1580 end_of_file_error ()
1582 Lisp_Object data;
1584 if (STRINGP (Vload_file_name))
1585 xsignal1 (Qend_of_file, Vload_file_name);
1587 xsignal0 (Qend_of_file);
1590 /* UNIBYTE specifies how to set load_convert_to_unibyte
1591 for this invocation.
1592 READFUN, if non-nil, is used instead of `read'.
1594 START, END specify region to read in current buffer (from eval-region).
1595 If the input is not from a buffer, they must be nil. */
1597 static void
1598 readevalloop (readcharfun, stream, sourcename, evalfun,
1599 printflag, unibyte, readfun, start, end)
1600 Lisp_Object readcharfun;
1601 FILE *stream;
1602 Lisp_Object sourcename;
1603 Lisp_Object (*evalfun) ();
1604 int printflag;
1605 Lisp_Object unibyte, readfun;
1606 Lisp_Object start, end;
1608 register int c;
1609 register Lisp_Object val;
1610 int count = SPECPDL_INDEX ();
1611 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1612 struct buffer *b = 0;
1613 int continue_reading_p;
1614 /* Nonzero if reading an entire buffer. */
1615 int whole_buffer = 0;
1616 /* 1 on the first time around. */
1617 int first_sexp = 1;
1619 if (MARKERP (readcharfun))
1621 if (NILP (start))
1622 start = readcharfun;
1625 if (BUFFERP (readcharfun))
1626 b = XBUFFER (readcharfun);
1627 else if (MARKERP (readcharfun))
1628 b = XMARKER (readcharfun)->buffer;
1630 /* We assume START is nil when input is not from a buffer. */
1631 if (! NILP (start) && !b)
1632 abort ();
1634 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
1635 specbind (Qcurrent_load_list, Qnil);
1636 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1637 load_convert_to_unibyte = !NILP (unibyte);
1639 GCPRO4 (sourcename, readfun, start, end);
1641 /* Try to ensure sourcename is a truename, except whilst preloading. */
1642 if (NILP (Vpurify_flag)
1643 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1644 && !NILP (Ffboundp (Qfile_truename)))
1645 sourcename = call1 (Qfile_truename, sourcename) ;
1647 LOADHIST_ATTACH (sourcename);
1649 continue_reading_p = 1;
1650 while (continue_reading_p)
1652 int count1 = SPECPDL_INDEX ();
1654 if (b != 0 && NILP (b->name))
1655 error ("Reading from killed buffer");
1657 if (!NILP (start))
1659 /* Switch to the buffer we are reading from. */
1660 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1661 set_buffer_internal (b);
1663 /* Save point in it. */
1664 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1665 /* Save ZV in it. */
1666 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1667 /* Those get unbound after we read one expression. */
1669 /* Set point and ZV around stuff to be read. */
1670 Fgoto_char (start);
1671 if (!NILP (end))
1672 Fnarrow_to_region (make_number (BEGV), end);
1674 /* Just for cleanliness, convert END to a marker
1675 if it is an integer. */
1676 if (INTEGERP (end))
1677 end = Fpoint_max_marker ();
1680 /* On the first cycle, we can easily test here
1681 whether we are reading the whole buffer. */
1682 if (b && first_sexp)
1683 whole_buffer = (PT == BEG && ZV == Z);
1685 instream = stream;
1686 read_next:
1687 c = READCHAR;
1688 if (c == ';')
1690 while ((c = READCHAR) != '\n' && c != -1);
1691 goto read_next;
1693 if (c < 0)
1695 unbind_to (count1, Qnil);
1696 break;
1699 /* Ignore whitespace here, so we can detect eof. */
1700 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1701 goto read_next;
1703 if (!NILP (Vpurify_flag) && c == '(')
1705 record_unwind_protect (unreadpure, Qnil);
1706 val = read_list (-1, readcharfun);
1708 else
1710 UNREAD (c);
1711 read_objects = Qnil;
1712 if (!NILP (readfun))
1714 val = call1 (readfun, readcharfun);
1716 /* If READCHARFUN has set point to ZV, we should
1717 stop reading, even if the form read sets point
1718 to a different value when evaluated. */
1719 if (BUFFERP (readcharfun))
1721 struct buffer *b = XBUFFER (readcharfun);
1722 if (BUF_PT (b) == BUF_ZV (b))
1723 continue_reading_p = 0;
1726 else if (! NILP (Vload_read_function))
1727 val = call1 (Vload_read_function, readcharfun);
1728 else
1729 val = read_internal_start (readcharfun, Qnil, Qnil);
1732 if (!NILP (start) && continue_reading_p)
1733 start = Fpoint_marker ();
1735 /* Restore saved point and BEGV. */
1736 unbind_to (count1, Qnil);
1738 /* Now eval what we just read. */
1739 val = (*evalfun) (val);
1741 if (printflag)
1743 Vvalues = Fcons (val, Vvalues);
1744 if (EQ (Vstandard_output, Qt))
1745 Fprin1 (val, Qnil);
1746 else
1747 Fprint (val, Qnil);
1750 first_sexp = 0;
1753 build_load_history (sourcename,
1754 stream || whole_buffer);
1756 UNGCPRO;
1758 unbind_to (count, Qnil);
1761 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1762 doc: /* Execute the current buffer as Lisp code.
1763 Programs can pass two arguments, BUFFER and PRINTFLAG.
1764 BUFFER is the buffer to evaluate (nil means use current buffer).
1765 PRINTFLAG controls printing of output:
1766 A value of nil means discard it; anything else is stream for print.
1768 If the optional third argument FILENAME is non-nil,
1769 it specifies the file name to use for `load-history'.
1770 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'
1771 for this invocation.
1773 The optional fifth argument DO-ALLOW-PRINT, if non-nil, specifies that
1774 `print' and related functions should work normally even if PRINTFLAG is nil.
1776 This function preserves the position of point. */)
1777 (buffer, printflag, filename, unibyte, do_allow_print)
1778 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1780 int count = SPECPDL_INDEX ();
1781 Lisp_Object tem, buf;
1783 if (NILP (buffer))
1784 buf = Fcurrent_buffer ();
1785 else
1786 buf = Fget_buffer (buffer);
1787 if (NILP (buf))
1788 error ("No such buffer");
1790 if (NILP (printflag) && NILP (do_allow_print))
1791 tem = Qsymbolp;
1792 else
1793 tem = printflag;
1795 if (NILP (filename))
1796 filename = XBUFFER (buf)->filename;
1798 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1799 specbind (Qstandard_output, tem);
1800 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1801 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1802 readevalloop (buf, 0, filename, Feval,
1803 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
1804 unbind_to (count, Qnil);
1806 return Qnil;
1809 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1810 doc: /* Execute the region as Lisp code.
1811 When called from programs, expects two arguments,
1812 giving starting and ending indices in the current buffer
1813 of the text to be executed.
1814 Programs can pass third argument PRINTFLAG which controls output:
1815 A value of nil means discard it; anything else is stream for printing it.
1816 Also the fourth argument READ-FUNCTION, if non-nil, is used
1817 instead of `read' to read each expression. It gets one argument
1818 which is the input stream for reading characters.
1820 This function does not move point. */)
1821 (start, end, printflag, read_function)
1822 Lisp_Object start, end, printflag, read_function;
1824 int count = SPECPDL_INDEX ();
1825 Lisp_Object tem, cbuf;
1827 cbuf = Fcurrent_buffer ();
1829 if (NILP (printflag))
1830 tem = Qsymbolp;
1831 else
1832 tem = printflag;
1833 specbind (Qstandard_output, tem);
1834 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
1836 /* readevalloop calls functions which check the type of start and end. */
1837 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1838 !NILP (printflag), Qnil, read_function,
1839 start, end);
1841 return unbind_to (count, Qnil);
1845 DEFUN ("read", Fread, Sread, 0, 1, 0,
1846 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1847 If STREAM is nil, use the value of `standard-input' (which see).
1848 STREAM or the value of `standard-input' may be:
1849 a buffer (read from point and advance it)
1850 a marker (read from where it points and advance it)
1851 a function (call it with no arguments for each character,
1852 call it with a char as argument to push a char back)
1853 a string (takes text from string, starting at the beginning)
1854 t (read text line using minibuffer and use it, or read from
1855 standard input in batch mode). */)
1856 (stream)
1857 Lisp_Object stream;
1859 if (NILP (stream))
1860 stream = Vstandard_input;
1861 if (EQ (stream, Qt))
1862 stream = Qread_char;
1863 if (EQ (stream, Qread_char))
1864 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1866 return read_internal_start (stream, Qnil, Qnil);
1869 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1870 doc: /* Read one Lisp expression which is represented as text by STRING.
1871 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
1872 START and END optionally delimit a substring of STRING from which to read;
1873 they default to 0 and (length STRING) respectively. */)
1874 (string, start, end)
1875 Lisp_Object string, start, end;
1877 Lisp_Object ret;
1878 CHECK_STRING (string);
1879 /* read_internal_start sets read_from_string_index. */
1880 ret = read_internal_start (string, start, end);
1881 return Fcons (ret, make_number (read_from_string_index));
1884 /* Function to set up the global context we need in toplevel read
1885 calls. */
1886 static Lisp_Object
1887 read_internal_start (stream, start, end)
1888 Lisp_Object stream;
1889 Lisp_Object start; /* Only used when stream is a string. */
1890 Lisp_Object end; /* Only used when stream is a string. */
1892 Lisp_Object retval;
1894 readchar_count = 0;
1895 new_backquote_flag = 0;
1896 read_objects = Qnil;
1897 if (EQ (Vread_with_symbol_positions, Qt)
1898 || EQ (Vread_with_symbol_positions, stream))
1899 Vread_symbol_positions_list = Qnil;
1901 if (STRINGP (stream)
1902 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
1904 int startval, endval;
1905 Lisp_Object string;
1907 if (STRINGP (stream))
1908 string = stream;
1909 else
1910 string = XCAR (stream);
1912 if (NILP (end))
1913 endval = SCHARS (string);
1914 else
1916 CHECK_NUMBER (end);
1917 endval = XINT (end);
1918 if (endval < 0 || endval > SCHARS (string))
1919 args_out_of_range (string, end);
1922 if (NILP (start))
1923 startval = 0;
1924 else
1926 CHECK_NUMBER (start);
1927 startval = XINT (start);
1928 if (startval < 0 || startval > endval)
1929 args_out_of_range (string, start);
1931 read_from_string_index = startval;
1932 read_from_string_index_byte = string_char_to_byte (string, startval);
1933 read_from_string_limit = endval;
1936 retval = read0 (stream);
1937 if (EQ (Vread_with_symbol_positions, Qt)
1938 || EQ (Vread_with_symbol_positions, stream))
1939 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
1940 return retval;
1944 /* Signal Qinvalid_read_syntax error.
1945 S is error string of length N (if > 0) */
1947 static void
1948 invalid_syntax (s, n)
1949 const char *s;
1950 int n;
1952 if (!n)
1953 n = strlen (s);
1954 xsignal1 (Qinvalid_read_syntax, make_string (s, n));
1958 /* Use this for recursive reads, in contexts where internal tokens
1959 are not allowed. */
1961 static Lisp_Object
1962 read0 (readcharfun)
1963 Lisp_Object readcharfun;
1965 register Lisp_Object val;
1966 int c;
1968 val = read1 (readcharfun, &c, 0);
1969 if (!c)
1970 return val;
1972 xsignal1 (Qinvalid_read_syntax,
1973 Fmake_string (make_number (1), make_number (c)));
1976 static int read_buffer_size;
1977 static char *read_buffer;
1979 /* Read a \-escape sequence, assuming we already read the `\'.
1980 If the escape sequence forces unibyte, return eight-bit char. */
1982 static int
1983 read_escape (readcharfun, stringp)
1984 Lisp_Object readcharfun;
1985 int stringp;
1987 register int c = READCHAR;
1988 /* \u allows up to four hex digits, \U up to eight. Default to the
1989 behaviour for \u, and change this value in the case that \U is seen. */
1990 int unicode_hex_count = 4;
1992 switch (c)
1994 case -1:
1995 end_of_file_error ();
1997 case 'a':
1998 return '\007';
1999 case 'b':
2000 return '\b';
2001 case 'd':
2002 return 0177;
2003 case 'e':
2004 return 033;
2005 case 'f':
2006 return '\f';
2007 case 'n':
2008 return '\n';
2009 case 'r':
2010 return '\r';
2011 case 't':
2012 return '\t';
2013 case 'v':
2014 return '\v';
2015 case '\n':
2016 return -1;
2017 case ' ':
2018 if (stringp)
2019 return -1;
2020 return ' ';
2022 case 'M':
2023 c = READCHAR;
2024 if (c != '-')
2025 error ("Invalid escape character syntax");
2026 c = READCHAR;
2027 if (c == '\\')
2028 c = read_escape (readcharfun, 0);
2029 return c | meta_modifier;
2031 case 'S':
2032 c = READCHAR;
2033 if (c != '-')
2034 error ("Invalid escape character syntax");
2035 c = READCHAR;
2036 if (c == '\\')
2037 c = read_escape (readcharfun, 0);
2038 return c | shift_modifier;
2040 case 'H':
2041 c = READCHAR;
2042 if (c != '-')
2043 error ("Invalid escape character syntax");
2044 c = READCHAR;
2045 if (c == '\\')
2046 c = read_escape (readcharfun, 0);
2047 return c | hyper_modifier;
2049 case 'A':
2050 c = READCHAR;
2051 if (c != '-')
2052 error ("Invalid escape character syntax");
2053 c = READCHAR;
2054 if (c == '\\')
2055 c = read_escape (readcharfun, 0);
2056 return c | alt_modifier;
2058 case 's':
2059 c = READCHAR;
2060 if (stringp || c != '-')
2062 UNREAD (c);
2063 return ' ';
2065 c = READCHAR;
2066 if (c == '\\')
2067 c = read_escape (readcharfun, 0);
2068 return c | super_modifier;
2070 case 'C':
2071 c = READCHAR;
2072 if (c != '-')
2073 error ("Invalid escape character syntax");
2074 case '^':
2075 c = READCHAR;
2076 if (c == '\\')
2077 c = read_escape (readcharfun, 0);
2078 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2079 return 0177 | (c & CHAR_MODIFIER_MASK);
2080 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2081 return c | ctrl_modifier;
2082 /* ASCII control chars are made from letters (both cases),
2083 as well as the non-letters within 0100...0137. */
2084 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2085 return (c & (037 | ~0177));
2086 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2087 return (c & (037 | ~0177));
2088 else
2089 return c | ctrl_modifier;
2091 case '0':
2092 case '1':
2093 case '2':
2094 case '3':
2095 case '4':
2096 case '5':
2097 case '6':
2098 case '7':
2099 /* An octal escape, as in ANSI C. */
2101 register int i = c - '0';
2102 register int count = 0;
2103 while (++count < 3)
2105 if ((c = READCHAR) >= '0' && c <= '7')
2107 i *= 8;
2108 i += c - '0';
2110 else
2112 UNREAD (c);
2113 break;
2117 if (i >= 0x80 && i < 0x100)
2118 i = BYTE8_TO_CHAR (i);
2119 return i;
2122 case 'x':
2123 /* A hex escape, as in ANSI C. */
2125 int i = 0;
2126 int count = 0;
2127 while (1)
2129 c = READCHAR;
2130 if (c >= '0' && c <= '9')
2132 i *= 16;
2133 i += c - '0';
2135 else if ((c >= 'a' && c <= 'f')
2136 || (c >= 'A' && c <= 'F'))
2138 i *= 16;
2139 if (c >= 'a' && c <= 'f')
2140 i += c - 'a' + 10;
2141 else
2142 i += c - 'A' + 10;
2144 else
2146 UNREAD (c);
2147 break;
2149 count++;
2152 if (count < 3 && i >= 0x80)
2153 return BYTE8_TO_CHAR (i);
2154 return i;
2157 case 'U':
2158 /* Post-Unicode-2.0: Up to eight hex chars. */
2159 unicode_hex_count = 8;
2160 case 'u':
2162 /* A Unicode escape. We only permit them in strings and characters,
2163 not arbitrarily in the source code, as in some other languages. */
2165 int i = 0;
2166 int count = 0;
2168 while (++count <= unicode_hex_count)
2170 c = READCHAR;
2171 /* isdigit and isalpha may be locale-specific, which we don't
2172 want. */
2173 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2174 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2175 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2176 else
2178 error ("Non-hex digit used for Unicode escape");
2179 break;
2183 return i;
2186 default:
2187 return c;
2191 /* Read an integer in radix RADIX using READCHARFUN to read
2192 characters. RADIX must be in the interval [2..36]; if it isn't, a
2193 read error is signaled . Value is the integer read. Signals an
2194 error if encountering invalid read syntax or if RADIX is out of
2195 range. */
2197 static Lisp_Object
2198 read_integer (readcharfun, radix)
2199 Lisp_Object readcharfun;
2200 int radix;
2202 int ndigits = 0, invalid_p, c, sign = 0;
2203 EMACS_INT number = 0;
2205 if (radix < 2 || radix > 36)
2206 invalid_p = 1;
2207 else
2209 number = ndigits = invalid_p = 0;
2210 sign = 1;
2212 c = READCHAR;
2213 if (c == '-')
2215 c = READCHAR;
2216 sign = -1;
2218 else if (c == '+')
2219 c = READCHAR;
2221 while (c >= 0)
2223 int digit;
2225 if (c >= '0' && c <= '9')
2226 digit = c - '0';
2227 else if (c >= 'a' && c <= 'z')
2228 digit = c - 'a' + 10;
2229 else if (c >= 'A' && c <= 'Z')
2230 digit = c - 'A' + 10;
2231 else
2233 UNREAD (c);
2234 break;
2237 if (digit < 0 || digit >= radix)
2238 invalid_p = 1;
2240 number = radix * number + digit;
2241 ++ndigits;
2242 c = READCHAR;
2246 if (ndigits == 0 || invalid_p)
2248 char buf[50];
2249 sprintf (buf, "integer, radix %d", radix);
2250 invalid_syntax (buf, 0);
2253 return make_number (sign * number);
2257 /* If the next token is ')' or ']' or '.', we store that character
2258 in *PCH and the return value is not interesting. Else, we store
2259 zero in *PCH and we read and return one lisp object.
2261 FIRST_IN_LIST is nonzero if this is the first element of a list. */
2263 static Lisp_Object
2264 read1 (readcharfun, pch, first_in_list)
2265 register Lisp_Object readcharfun;
2266 int *pch;
2267 int first_in_list;
2269 register int c;
2270 int uninterned_symbol = 0;
2272 *pch = 0;
2273 load_each_byte = 0;
2275 retry:
2277 c = READCHAR;
2278 if (c < 0)
2279 end_of_file_error ();
2281 switch (c)
2283 case '(':
2284 return read_list (0, readcharfun);
2286 case '[':
2287 return read_vector (readcharfun, 0);
2289 case ')':
2290 case ']':
2292 *pch = c;
2293 return Qnil;
2296 case '#':
2297 c = READCHAR;
2298 if (c == '^')
2300 c = READCHAR;
2301 if (c == '[')
2303 Lisp_Object tmp;
2304 tmp = read_vector (readcharfun, 0);
2305 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS)
2306 error ("Invalid size char-table");
2307 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
2308 return tmp;
2310 else if (c == '^')
2312 c = READCHAR;
2313 if (c == '[')
2315 Lisp_Object tmp;
2316 int depth, size;
2318 tmp = read_vector (readcharfun, 0);
2319 if (!INTEGERP (AREF (tmp, 0)))
2320 error ("Invalid depth in char-table");
2321 depth = XINT (AREF (tmp, 0));
2322 if (depth < 1 || depth > 3)
2323 error ("Invalid depth in char-table");
2324 size = XVECTOR (tmp)->size - 2;
2325 if (chartab_size [depth] != size)
2326 error ("Invalid size char-table");
2327 XSETSUB_CHAR_TABLE (tmp, XSUB_CHAR_TABLE (tmp));
2328 return tmp;
2330 invalid_syntax ("#^^", 3);
2332 invalid_syntax ("#^", 2);
2334 if (c == '&')
2336 Lisp_Object length;
2337 length = read1 (readcharfun, pch, first_in_list);
2338 c = READCHAR;
2339 if (c == '"')
2341 Lisp_Object tmp, val;
2342 int size_in_chars
2343 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2344 / BOOL_VECTOR_BITS_PER_CHAR);
2346 UNREAD (c);
2347 tmp = read1 (readcharfun, pch, first_in_list);
2348 if (STRING_MULTIBYTE (tmp)
2349 || (size_in_chars != SCHARS (tmp)
2350 /* We used to print 1 char too many
2351 when the number of bits was a multiple of 8.
2352 Accept such input in case it came from an old
2353 version. */
2354 && ! (XFASTINT (length)
2355 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2356 invalid_syntax ("#&...", 5);
2358 val = Fmake_bool_vector (length, Qnil);
2359 bcopy (SDATA (tmp), XBOOL_VECTOR (val)->data,
2360 size_in_chars);
2361 /* Clear the extraneous bits in the last byte. */
2362 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2363 XBOOL_VECTOR (val)->data[size_in_chars - 1]
2364 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2365 return val;
2367 invalid_syntax ("#&...", 5);
2369 if (c == '[')
2371 /* Accept compiled functions at read-time so that we don't have to
2372 build them using function calls. */
2373 Lisp_Object tmp;
2374 tmp = read_vector (readcharfun, 1);
2375 return Fmake_byte_code (XVECTOR (tmp)->size,
2376 XVECTOR (tmp)->contents);
2378 if (c == '(')
2380 Lisp_Object tmp;
2381 struct gcpro gcpro1;
2382 int ch;
2384 /* Read the string itself. */
2385 tmp = read1 (readcharfun, &ch, 0);
2386 if (ch != 0 || !STRINGP (tmp))
2387 invalid_syntax ("#", 1);
2388 GCPRO1 (tmp);
2389 /* Read the intervals and their properties. */
2390 while (1)
2392 Lisp_Object beg, end, plist;
2394 beg = read1 (readcharfun, &ch, 0);
2395 end = plist = Qnil;
2396 if (ch == ')')
2397 break;
2398 if (ch == 0)
2399 end = read1 (readcharfun, &ch, 0);
2400 if (ch == 0)
2401 plist = read1 (readcharfun, &ch, 0);
2402 if (ch)
2403 invalid_syntax ("Invalid string property list", 0);
2404 Fset_text_properties (beg, end, plist, tmp);
2406 UNGCPRO;
2407 return tmp;
2410 /* #@NUMBER is used to skip NUMBER following characters.
2411 That's used in .elc files to skip over doc strings
2412 and function definitions. */
2413 if (c == '@')
2415 int i, nskip = 0;
2417 load_each_byte = 1;
2418 /* Read a decimal integer. */
2419 while ((c = READCHAR) >= 0
2420 && c >= '0' && c <= '9')
2422 nskip *= 10;
2423 nskip += c - '0';
2425 if (c >= 0)
2426 UNREAD (c);
2428 if (load_force_doc_strings
2429 && (EQ (readcharfun, Qget_file_char)
2430 || EQ (readcharfun, Qget_emacs_mule_file_char)))
2432 /* If we are supposed to force doc strings into core right now,
2433 record the last string that we skipped,
2434 and record where in the file it comes from. */
2436 /* But first exchange saved_doc_string
2437 with prev_saved_doc_string, so we save two strings. */
2439 char *temp = saved_doc_string;
2440 int temp_size = saved_doc_string_size;
2441 file_offset temp_pos = saved_doc_string_position;
2442 int temp_len = saved_doc_string_length;
2444 saved_doc_string = prev_saved_doc_string;
2445 saved_doc_string_size = prev_saved_doc_string_size;
2446 saved_doc_string_position = prev_saved_doc_string_position;
2447 saved_doc_string_length = prev_saved_doc_string_length;
2449 prev_saved_doc_string = temp;
2450 prev_saved_doc_string_size = temp_size;
2451 prev_saved_doc_string_position = temp_pos;
2452 prev_saved_doc_string_length = temp_len;
2455 if (saved_doc_string_size == 0)
2457 saved_doc_string_size = nskip + 100;
2458 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
2460 if (nskip > saved_doc_string_size)
2462 saved_doc_string_size = nskip + 100;
2463 saved_doc_string = (char *) xrealloc (saved_doc_string,
2464 saved_doc_string_size);
2467 saved_doc_string_position = file_tell (instream);
2469 /* Copy that many characters into saved_doc_string. */
2470 for (i = 0; i < nskip && c >= 0; i++)
2471 saved_doc_string[i] = c = READCHAR;
2473 saved_doc_string_length = i;
2475 else
2477 /* Skip that many characters. */
2478 for (i = 0; i < nskip && c >= 0; i++)
2479 c = READCHAR;
2482 load_each_byte = 0;
2483 goto retry;
2485 if (c == '!')
2487 /* #! appears at the beginning of an executable file.
2488 Skip the first line. */
2489 while (c != '\n' && c >= 0)
2490 c = READCHAR;
2491 goto retry;
2493 if (c == '$')
2494 return Vload_file_name;
2495 if (c == '\'')
2496 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2497 /* #:foo is the uninterned symbol named foo. */
2498 if (c == ':')
2500 uninterned_symbol = 1;
2501 c = READCHAR;
2502 goto default_label;
2504 /* Reader forms that can reuse previously read objects. */
2505 if (c >= '0' && c <= '9')
2507 int n = 0;
2508 Lisp_Object tem;
2510 /* Read a non-negative integer. */
2511 while (c >= '0' && c <= '9')
2513 n *= 10;
2514 n += c - '0';
2515 c = READCHAR;
2517 /* #n=object returns object, but associates it with n for #n#. */
2518 if (c == '=')
2520 /* Make a placeholder for #n# to use temporarily */
2521 Lisp_Object placeholder;
2522 Lisp_Object cell;
2524 placeholder = Fcons(Qnil, Qnil);
2525 cell = Fcons (make_number (n), placeholder);
2526 read_objects = Fcons (cell, read_objects);
2528 /* Read the object itself. */
2529 tem = read0 (readcharfun);
2531 /* Now put it everywhere the placeholder was... */
2532 substitute_object_in_subtree (tem, placeholder);
2534 /* ...and #n# will use the real value from now on. */
2535 Fsetcdr (cell, tem);
2537 return tem;
2539 /* #n# returns a previously read object. */
2540 if (c == '#')
2542 tem = Fassq (make_number (n), read_objects);
2543 if (CONSP (tem))
2544 return XCDR (tem);
2545 /* Fall through to error message. */
2547 else if (c == 'r' || c == 'R')
2548 return read_integer (readcharfun, n);
2550 /* Fall through to error message. */
2552 else if (c == 'x' || c == 'X')
2553 return read_integer (readcharfun, 16);
2554 else if (c == 'o' || c == 'O')
2555 return read_integer (readcharfun, 8);
2556 else if (c == 'b' || c == 'B')
2557 return read_integer (readcharfun, 2);
2559 UNREAD (c);
2560 invalid_syntax ("#", 1);
2562 case ';':
2563 while ((c = READCHAR) >= 0 && c != '\n');
2564 goto retry;
2566 case '\'':
2568 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2571 case '`':
2572 if (first_in_list)
2573 goto default_label;
2574 else
2576 Lisp_Object value;
2578 new_backquote_flag++;
2579 value = read0 (readcharfun);
2580 new_backquote_flag--;
2582 return Fcons (Qbackquote, Fcons (value, Qnil));
2585 case ',':
2586 if (new_backquote_flag)
2588 Lisp_Object comma_type = Qnil;
2589 Lisp_Object value;
2590 int ch = READCHAR;
2592 if (ch == '@')
2593 comma_type = Qcomma_at;
2594 else if (ch == '.')
2595 comma_type = Qcomma_dot;
2596 else
2598 if (ch >= 0) UNREAD (ch);
2599 comma_type = Qcomma;
2602 new_backquote_flag--;
2603 value = read0 (readcharfun);
2604 new_backquote_flag++;
2605 return Fcons (comma_type, Fcons (value, Qnil));
2607 else
2608 goto default_label;
2610 case '?':
2612 int modifiers;
2613 int next_char;
2614 int ok;
2616 c = READCHAR;
2617 if (c < 0)
2618 end_of_file_error ();
2620 /* Accept `single space' syntax like (list ? x) where the
2621 whitespace character is SPC or TAB.
2622 Other literal whitespace like NL, CR, and FF are not accepted,
2623 as there are well-established escape sequences for these. */
2624 if (c == ' ' || c == '\t')
2625 return make_number (c);
2627 if (c == '\\')
2628 c = read_escape (readcharfun, 0);
2629 modifiers = c & CHAR_MODIFIER_MASK;
2630 c &= ~CHAR_MODIFIER_MASK;
2631 if (CHAR_BYTE8_P (c))
2632 c = CHAR_TO_BYTE8 (c);
2633 c |= modifiers;
2635 next_char = READCHAR;
2636 if (next_char == '.')
2638 /* Only a dotted-pair dot is valid after a char constant. */
2639 int next_next_char = READCHAR;
2640 UNREAD (next_next_char);
2642 ok = (next_next_char <= 040
2643 || (next_next_char < 0200
2644 && (index ("\"';([#?", next_next_char)
2645 || (!first_in_list && next_next_char == '`')
2646 || (new_backquote_flag && next_next_char == ','))));
2648 else
2650 ok = (next_char <= 040
2651 || (next_char < 0200
2652 && (index ("\"';()[]#?", next_char)
2653 || (!first_in_list && next_char == '`')
2654 || (new_backquote_flag && next_char == ','))));
2656 UNREAD (next_char);
2657 if (ok)
2658 return make_number (c);
2660 invalid_syntax ("?", 1);
2663 case '"':
2665 char *p = read_buffer;
2666 char *end = read_buffer + read_buffer_size;
2667 register int c;
2668 /* Nonzero if we saw an escape sequence specifying
2669 a multibyte character. */
2670 int force_multibyte = 0;
2671 /* Nonzero if we saw an escape sequence specifying
2672 a single-byte character. */
2673 int force_singlebyte = 0;
2674 int cancel = 0;
2675 int nchars = 0;
2677 while ((c = READCHAR) >= 0
2678 && c != '\"')
2680 if (end - p < MAX_MULTIBYTE_LENGTH)
2682 int offset = p - read_buffer;
2683 read_buffer = (char *) xrealloc (read_buffer,
2684 read_buffer_size *= 2);
2685 p = read_buffer + offset;
2686 end = read_buffer + read_buffer_size;
2689 if (c == '\\')
2691 int modifiers;
2693 c = read_escape (readcharfun, 1);
2695 /* C is -1 if \ newline has just been seen */
2696 if (c == -1)
2698 if (p == read_buffer)
2699 cancel = 1;
2700 continue;
2703 modifiers = c & CHAR_MODIFIER_MASK;
2704 c = c & ~CHAR_MODIFIER_MASK;
2706 if (CHAR_BYTE8_P (c))
2707 force_singlebyte = 1;
2708 else if (! ASCII_CHAR_P (c))
2709 force_multibyte = 1;
2710 else /* i.e. ASCII_CHAR_P (c) */
2712 /* Allow `\C- ' and `\C-?'. */
2713 if (modifiers == CHAR_CTL)
2715 if (c == ' ')
2716 c = 0, modifiers = 0;
2717 else if (c == '?')
2718 c = 127, modifiers = 0;
2720 if (modifiers & CHAR_SHIFT)
2722 /* Shift modifier is valid only with [A-Za-z]. */
2723 if (c >= 'A' && c <= 'Z')
2724 modifiers &= ~CHAR_SHIFT;
2725 else if (c >= 'a' && c <= 'z')
2726 c -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
2729 if (modifiers & CHAR_META)
2731 /* Move the meta bit to the right place for a
2732 string. */
2733 modifiers &= ~CHAR_META;
2734 c = BYTE8_TO_CHAR (c | 0x80);
2735 force_singlebyte = 1;
2739 /* Any modifiers remaining are invalid. */
2740 if (modifiers)
2741 error ("Invalid modifier in string");
2742 p += CHAR_STRING (c, (unsigned char *) p);
2744 else
2746 p += CHAR_STRING (c, (unsigned char *) p);
2747 if (CHAR_BYTE8_P (c))
2748 force_singlebyte = 1;
2749 else if (! ASCII_CHAR_P (c))
2750 force_multibyte = 1;
2752 nchars++;
2755 if (c < 0)
2756 end_of_file_error ();
2758 /* If purifying, and string starts with \ newline,
2759 return zero instead. This is for doc strings
2760 that we are really going to find in etc/DOC.nn.nn */
2761 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2762 return make_number (0);
2764 if (force_multibyte)
2765 /* READ_BUFFER already contains valid multibyte forms. */
2767 else if (force_singlebyte)
2769 nchars = str_as_unibyte (read_buffer, p - read_buffer);
2770 p = read_buffer + nchars;
2772 else
2773 /* Otherwise, READ_BUFFER contains only ASCII. */
2776 /* We want readchar_count to be the number of characters, not
2777 bytes. Hence we adjust for multibyte characters in the
2778 string. ... But it doesn't seem to be necessary, because
2779 READCHAR *does* read multibyte characters from buffers. */
2780 /* readchar_count -= (p - read_buffer) - nchars; */
2781 if (read_pure)
2782 return make_pure_string (read_buffer, nchars, p - read_buffer,
2783 (force_multibyte
2784 || (p - read_buffer != nchars)));
2785 return make_specified_string (read_buffer, nchars, p - read_buffer,
2786 (force_multibyte
2787 || (p - read_buffer != nchars)));
2790 case '.':
2792 int next_char = READCHAR;
2793 UNREAD (next_char);
2795 if (next_char <= 040
2796 || (next_char < 0200
2797 && (index ("\"';([#?", next_char)
2798 || (!first_in_list && next_char == '`')
2799 || (new_backquote_flag && next_char == ','))))
2801 *pch = c;
2802 return Qnil;
2805 /* Otherwise, we fall through! Note that the atom-reading loop
2806 below will now loop at least once, assuring that we will not
2807 try to UNREAD two characters in a row. */
2809 default:
2810 default_label:
2811 if (c <= 040) goto retry;
2813 char *p = read_buffer;
2814 int quoted = 0;
2817 char *end = read_buffer + read_buffer_size;
2819 while (c > 040
2820 && (c >= 0200
2821 || (!index ("\"';()[]#", c)
2822 && !(!first_in_list && c == '`')
2823 && !(new_backquote_flag && c == ','))))
2825 if (end - p < MAX_MULTIBYTE_LENGTH)
2827 int offset = p - read_buffer;
2828 read_buffer = (char *) xrealloc (read_buffer,
2829 read_buffer_size *= 2);
2830 p = read_buffer + offset;
2831 end = read_buffer + read_buffer_size;
2834 if (c == '\\')
2836 c = READCHAR;
2837 if (c == -1)
2838 end_of_file_error ();
2839 quoted = 1;
2842 p += CHAR_STRING (c, p);
2843 c = READCHAR;
2846 if (p == end)
2848 int offset = p - read_buffer;
2849 read_buffer = (char *) xrealloc (read_buffer,
2850 read_buffer_size *= 2);
2851 p = read_buffer + offset;
2852 end = read_buffer + read_buffer_size;
2854 *p = 0;
2855 if (c >= 0)
2856 UNREAD (c);
2859 if (!quoted && !uninterned_symbol)
2861 register char *p1;
2862 register Lisp_Object val;
2863 p1 = read_buffer;
2864 if (*p1 == '+' || *p1 == '-') p1++;
2865 /* Is it an integer? */
2866 if (p1 != p)
2868 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2869 /* Integers can have trailing decimal points. */
2870 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2871 if (p1 == p)
2872 /* It is an integer. */
2874 if (p1[-1] == '.')
2875 p1[-1] = '\0';
2876 /* Fixme: if we have strtol, use that, and check
2877 for overflow. */
2878 if (sizeof (int) == sizeof (EMACS_INT))
2879 XSETINT (val, atoi (read_buffer));
2880 else if (sizeof (long) == sizeof (EMACS_INT))
2881 XSETINT (val, atol (read_buffer));
2882 else
2883 abort ();
2884 return val;
2887 if (isfloat_string (read_buffer))
2889 /* Compute NaN and infinities using 0.0 in a variable,
2890 to cope with compilers that think they are smarter
2891 than we are. */
2892 double zero = 0.0;
2894 double value;
2896 /* Negate the value ourselves. This treats 0, NaNs,
2897 and infinity properly on IEEE floating point hosts,
2898 and works around a common bug where atof ("-0.0")
2899 drops the sign. */
2900 int negative = read_buffer[0] == '-';
2902 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2903 returns 1, is if the input ends in e+INF or e+NaN. */
2904 switch (p[-1])
2906 case 'F':
2907 value = 1.0 / zero;
2908 break;
2909 case 'N':
2910 value = zero / zero;
2912 /* If that made a "negative" NaN, negate it. */
2915 int i;
2916 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
2918 u_data.d = value;
2919 u_minus_zero.d = - 0.0;
2920 for (i = 0; i < sizeof (double); i++)
2921 if (u_data.c[i] & u_minus_zero.c[i])
2923 value = - value;
2924 break;
2927 /* Now VALUE is a positive NaN. */
2928 break;
2929 default:
2930 value = atof (read_buffer + negative);
2931 break;
2934 return make_float (negative ? - value : value);
2938 Lisp_Object result = uninterned_symbol ? make_symbol (read_buffer)
2939 : intern (read_buffer);
2940 if (EQ (Vread_with_symbol_positions, Qt)
2941 || EQ (Vread_with_symbol_positions, readcharfun))
2942 Vread_symbol_positions_list =
2943 /* Kind of a hack; this will probably fail if characters
2944 in the symbol name were escaped. Not really a big
2945 deal, though. */
2946 Fcons (Fcons (result,
2947 make_number (readchar_count
2948 - XFASTINT (Flength (Fsymbol_name (result))))),
2949 Vread_symbol_positions_list);
2950 return result;
2957 /* List of nodes we've seen during substitute_object_in_subtree. */
2958 static Lisp_Object seen_list;
2960 static void
2961 substitute_object_in_subtree (object, placeholder)
2962 Lisp_Object object;
2963 Lisp_Object placeholder;
2965 Lisp_Object check_object;
2967 /* We haven't seen any objects when we start. */
2968 seen_list = Qnil;
2970 /* Make all the substitutions. */
2971 check_object
2972 = substitute_object_recurse (object, placeholder, object);
2974 /* Clear seen_list because we're done with it. */
2975 seen_list = Qnil;
2977 /* The returned object here is expected to always eq the
2978 original. */
2979 if (!EQ (check_object, object))
2980 error ("Unexpected mutation error in reader");
2983 /* Feval doesn't get called from here, so no gc protection is needed. */
2984 #define SUBSTITUTE(get_val, set_val) \
2986 Lisp_Object old_value = get_val; \
2987 Lisp_Object true_value \
2988 = substitute_object_recurse (object, placeholder,\
2989 old_value); \
2991 if (!EQ (old_value, true_value)) \
2993 set_val; \
2997 static Lisp_Object
2998 substitute_object_recurse (object, placeholder, subtree)
2999 Lisp_Object object;
3000 Lisp_Object placeholder;
3001 Lisp_Object subtree;
3003 /* If we find the placeholder, return the target object. */
3004 if (EQ (placeholder, subtree))
3005 return object;
3007 /* If we've been to this node before, don't explore it again. */
3008 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3009 return subtree;
3011 /* If this node can be the entry point to a cycle, remember that
3012 we've seen it. It can only be such an entry point if it was made
3013 by #n=, which means that we can find it as a value in
3014 read_objects. */
3015 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3016 seen_list = Fcons (subtree, seen_list);
3018 /* Recurse according to subtree's type.
3019 Every branch must return a Lisp_Object. */
3020 switch (XTYPE (subtree))
3022 case Lisp_Vectorlike:
3024 int i;
3025 int length = XINT (Flength(subtree));
3026 for (i = 0; i < length; i++)
3028 Lisp_Object idx = make_number (i);
3029 SUBSTITUTE (Faref (subtree, idx),
3030 Faset (subtree, idx, true_value));
3032 return subtree;
3035 case Lisp_Cons:
3037 SUBSTITUTE (Fcar_safe (subtree),
3038 Fsetcar (subtree, true_value));
3039 SUBSTITUTE (Fcdr_safe (subtree),
3040 Fsetcdr (subtree, true_value));
3041 return subtree;
3044 case Lisp_String:
3046 /* Check for text properties in each interval.
3047 substitute_in_interval contains part of the logic. */
3049 INTERVAL root_interval = STRING_INTERVALS (subtree);
3050 Lisp_Object arg = Fcons (object, placeholder);
3052 traverse_intervals_noorder (root_interval,
3053 &substitute_in_interval, arg);
3055 return subtree;
3058 /* Other types don't recurse any further. */
3059 default:
3060 return subtree;
3064 /* Helper function for substitute_object_recurse. */
3065 static void
3066 substitute_in_interval (interval, arg)
3067 INTERVAL interval;
3068 Lisp_Object arg;
3070 Lisp_Object object = Fcar (arg);
3071 Lisp_Object placeholder = Fcdr (arg);
3073 SUBSTITUTE(interval->plist, interval->plist = true_value);
3077 #define LEAD_INT 1
3078 #define DOT_CHAR 2
3079 #define TRAIL_INT 4
3080 #define E_CHAR 8
3081 #define EXP_INT 16
3084 isfloat_string (cp)
3085 register char *cp;
3087 register int state;
3089 char *start = cp;
3091 state = 0;
3092 if (*cp == '+' || *cp == '-')
3093 cp++;
3095 if (*cp >= '0' && *cp <= '9')
3097 state |= LEAD_INT;
3098 while (*cp >= '0' && *cp <= '9')
3099 cp++;
3101 if (*cp == '.')
3103 state |= DOT_CHAR;
3104 cp++;
3106 if (*cp >= '0' && *cp <= '9')
3108 state |= TRAIL_INT;
3109 while (*cp >= '0' && *cp <= '9')
3110 cp++;
3112 if (*cp == 'e' || *cp == 'E')
3114 state |= E_CHAR;
3115 cp++;
3116 if (*cp == '+' || *cp == '-')
3117 cp++;
3120 if (*cp >= '0' && *cp <= '9')
3122 state |= EXP_INT;
3123 while (*cp >= '0' && *cp <= '9')
3124 cp++;
3126 else if (cp == start)
3128 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3130 state |= EXP_INT;
3131 cp += 3;
3133 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3135 state |= EXP_INT;
3136 cp += 3;
3139 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
3140 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
3141 || state == (DOT_CHAR|TRAIL_INT)
3142 || state == (LEAD_INT|E_CHAR|EXP_INT)
3143 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
3144 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
3148 static Lisp_Object
3149 read_vector (readcharfun, bytecodeflag)
3150 Lisp_Object readcharfun;
3151 int bytecodeflag;
3153 register int i;
3154 register int size;
3155 register Lisp_Object *ptr;
3156 register Lisp_Object tem, item, vector;
3157 register struct Lisp_Cons *otem;
3158 Lisp_Object len;
3160 tem = read_list (1, readcharfun);
3161 len = Flength (tem);
3162 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
3164 size = XVECTOR (vector)->size;
3165 ptr = XVECTOR (vector)->contents;
3166 for (i = 0; i < size; i++)
3168 item = Fcar (tem);
3169 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3170 bytecode object, the docstring containing the bytecode and
3171 constants values must be treated as unibyte and passed to
3172 Fread, to get the actual bytecode string and constants vector. */
3173 if (bytecodeflag && load_force_doc_strings)
3175 if (i == COMPILED_BYTECODE)
3177 if (!STRINGP (item))
3178 error ("Invalid byte code");
3180 /* Delay handling the bytecode slot until we know whether
3181 it is lazily-loaded (we can tell by whether the
3182 constants slot is nil). */
3183 ptr[COMPILED_CONSTANTS] = item;
3184 item = Qnil;
3186 else if (i == COMPILED_CONSTANTS)
3188 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3190 if (NILP (item))
3192 /* Coerce string to unibyte (like string-as-unibyte,
3193 but without generating extra garbage and
3194 guaranteeing no change in the contents). */
3195 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3196 STRING_SET_UNIBYTE (bytestr);
3198 item = Fread (Fcons (bytestr, readcharfun));
3199 if (!CONSP (item))
3200 error ("Invalid byte code");
3202 otem = XCONS (item);
3203 bytestr = XCAR (item);
3204 item = XCDR (item);
3205 free_cons (otem);
3208 /* Now handle the bytecode slot. */
3209 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
3211 else if (i == COMPILED_DOC_STRING
3212 && STRINGP (item)
3213 && ! STRING_MULTIBYTE (item))
3215 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3216 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3217 else
3218 item = Fstring_as_multibyte (item);
3221 ptr[i] = read_pure ? Fpurecopy (item) : item;
3222 otem = XCONS (tem);
3223 tem = Fcdr (tem);
3224 free_cons (otem);
3226 return vector;
3229 /* FLAG = 1 means check for ] to terminate rather than ) and .
3230 FLAG = -1 means check for starting with defun
3231 and make structure pure. */
3233 static Lisp_Object
3234 read_list (flag, readcharfun)
3235 int flag;
3236 register Lisp_Object readcharfun;
3238 /* -1 means check next element for defun,
3239 0 means don't check,
3240 1 means already checked and found defun. */
3241 int defunflag = flag < 0 ? -1 : 0;
3242 Lisp_Object val, tail;
3243 register Lisp_Object elt, tem;
3244 struct gcpro gcpro1, gcpro2;
3245 /* 0 is the normal case.
3246 1 means this list is a doc reference; replace it with the number 0.
3247 2 means this list is a doc reference; replace it with the doc string. */
3248 int doc_reference = 0;
3250 /* Initialize this to 1 if we are reading a list. */
3251 int first_in_list = flag <= 0;
3253 val = Qnil;
3254 tail = Qnil;
3256 while (1)
3258 int ch;
3259 GCPRO2 (val, tail);
3260 elt = read1 (readcharfun, &ch, first_in_list);
3261 UNGCPRO;
3263 first_in_list = 0;
3265 /* While building, if the list starts with #$, treat it specially. */
3266 if (EQ (elt, Vload_file_name)
3267 && ! NILP (elt)
3268 && !NILP (Vpurify_flag))
3270 if (NILP (Vdoc_file_name))
3271 /* We have not yet called Snarf-documentation, so assume
3272 this file is described in the DOC-MM.NN file
3273 and Snarf-documentation will fill in the right value later.
3274 For now, replace the whole list with 0. */
3275 doc_reference = 1;
3276 else
3277 /* We have already called Snarf-documentation, so make a relative
3278 file name for this file, so it can be found properly
3279 in the installed Lisp directory.
3280 We don't use Fexpand_file_name because that would make
3281 the directory absolute now. */
3282 elt = concat2 (build_string ("../lisp/"),
3283 Ffile_name_nondirectory (elt));
3285 else if (EQ (elt, Vload_file_name)
3286 && ! NILP (elt)
3287 && load_force_doc_strings)
3288 doc_reference = 2;
3290 if (ch)
3292 if (flag > 0)
3294 if (ch == ']')
3295 return val;
3296 invalid_syntax (") or . in a vector", 18);
3298 if (ch == ')')
3299 return val;
3300 if (ch == '.')
3302 GCPRO2 (val, tail);
3303 if (!NILP (tail))
3304 XSETCDR (tail, read0 (readcharfun));
3305 else
3306 val = read0 (readcharfun);
3307 read1 (readcharfun, &ch, 0);
3308 UNGCPRO;
3309 if (ch == ')')
3311 if (doc_reference == 1)
3312 return make_number (0);
3313 if (doc_reference == 2)
3315 /* Get a doc string from the file we are loading.
3316 If it's in saved_doc_string, get it from there.
3318 Here, we don't know if the string is a
3319 bytecode string or a doc string. As a
3320 bytecode string must be unibyte, we always
3321 return a unibyte string. If it is actually a
3322 doc string, caller must make it
3323 multibyte. */
3325 int pos = XINT (XCDR (val));
3326 /* Position is negative for user variables. */
3327 if (pos < 0) pos = -pos;
3328 if (pos >= saved_doc_string_position
3329 && pos < (saved_doc_string_position
3330 + saved_doc_string_length))
3332 int start = pos - saved_doc_string_position;
3333 int from, to;
3335 /* Process quoting with ^A,
3336 and find the end of the string,
3337 which is marked with ^_ (037). */
3338 for (from = start, to = start;
3339 saved_doc_string[from] != 037;)
3341 int c = saved_doc_string[from++];
3342 if (c == 1)
3344 c = saved_doc_string[from++];
3345 if (c == 1)
3346 saved_doc_string[to++] = c;
3347 else if (c == '0')
3348 saved_doc_string[to++] = 0;
3349 else if (c == '_')
3350 saved_doc_string[to++] = 037;
3352 else
3353 saved_doc_string[to++] = c;
3356 return make_unibyte_string (saved_doc_string + start,
3357 to - start);
3359 /* Look in prev_saved_doc_string the same way. */
3360 else if (pos >= prev_saved_doc_string_position
3361 && pos < (prev_saved_doc_string_position
3362 + prev_saved_doc_string_length))
3364 int start = pos - prev_saved_doc_string_position;
3365 int from, to;
3367 /* Process quoting with ^A,
3368 and find the end of the string,
3369 which is marked with ^_ (037). */
3370 for (from = start, to = start;
3371 prev_saved_doc_string[from] != 037;)
3373 int c = prev_saved_doc_string[from++];
3374 if (c == 1)
3376 c = prev_saved_doc_string[from++];
3377 if (c == 1)
3378 prev_saved_doc_string[to++] = c;
3379 else if (c == '0')
3380 prev_saved_doc_string[to++] = 0;
3381 else if (c == '_')
3382 prev_saved_doc_string[to++] = 037;
3384 else
3385 prev_saved_doc_string[to++] = c;
3388 return make_unibyte_string (prev_saved_doc_string
3389 + start,
3390 to - start);
3392 else
3393 return get_doc_string (val, 1, 0);
3396 return val;
3398 invalid_syntax (". in wrong context", 18);
3400 invalid_syntax ("] in a list", 11);
3402 tem = (read_pure && flag <= 0
3403 ? pure_cons (elt, Qnil)
3404 : Fcons (elt, Qnil));
3405 if (!NILP (tail))
3406 XSETCDR (tail, tem);
3407 else
3408 val = tem;
3409 tail = tem;
3410 if (defunflag < 0)
3411 defunflag = EQ (elt, Qdefun);
3412 else if (defunflag > 0)
3413 read_pure = 1;
3417 Lisp_Object Vobarray;
3418 Lisp_Object initial_obarray;
3420 /* oblookup stores the bucket number here, for the sake of Funintern. */
3422 int oblookup_last_bucket_number;
3424 static int hash_string ();
3426 /* Get an error if OBARRAY is not an obarray.
3427 If it is one, return it. */
3429 Lisp_Object
3430 check_obarray (obarray)
3431 Lisp_Object obarray;
3433 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3435 /* If Vobarray is now invalid, force it to be valid. */
3436 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3437 wrong_type_argument (Qvectorp, obarray);
3439 return obarray;
3442 /* Intern the C string STR: return a symbol with that name,
3443 interned in the current obarray. */
3445 Lisp_Object
3446 intern (str)
3447 const char *str;
3449 Lisp_Object tem;
3450 int len = strlen (str);
3451 Lisp_Object obarray;
3453 obarray = Vobarray;
3454 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3455 obarray = check_obarray (obarray);
3456 tem = oblookup (obarray, str, len, len);
3457 if (SYMBOLP (tem))
3458 return tem;
3459 return Fintern (make_string (str, len), obarray);
3462 /* Create an uninterned symbol with name STR. */
3464 Lisp_Object
3465 make_symbol (str)
3466 char *str;
3468 int len = strlen (str);
3470 return Fmake_symbol ((!NILP (Vpurify_flag)
3471 ? make_pure_string (str, len, len, 0)
3472 : make_string (str, len)));
3475 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3476 doc: /* Return the canonical symbol whose name is STRING.
3477 If there is none, one is created by this function and returned.
3478 A second optional argument specifies the obarray to use;
3479 it defaults to the value of `obarray'. */)
3480 (string, obarray)
3481 Lisp_Object string, obarray;
3483 register Lisp_Object tem, sym, *ptr;
3485 if (NILP (obarray)) obarray = Vobarray;
3486 obarray = check_obarray (obarray);
3488 CHECK_STRING (string);
3490 tem = oblookup (obarray, SDATA (string),
3491 SCHARS (string),
3492 SBYTES (string));
3493 if (!INTEGERP (tem))
3494 return tem;
3496 if (!NILP (Vpurify_flag))
3497 string = Fpurecopy (string);
3498 sym = Fmake_symbol (string);
3500 if (EQ (obarray, initial_obarray))
3501 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3502 else
3503 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
3505 if ((SREF (string, 0) == ':')
3506 && EQ (obarray, initial_obarray))
3508 XSYMBOL (sym)->constant = 1;
3509 XSYMBOL (sym)->value = sym;
3512 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
3513 if (SYMBOLP (*ptr))
3514 XSYMBOL (sym)->next = XSYMBOL (*ptr);
3515 else
3516 XSYMBOL (sym)->next = 0;
3517 *ptr = sym;
3518 return sym;
3521 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3522 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3523 NAME may be a string or a symbol. If it is a symbol, that exact
3524 symbol is searched for.
3525 A second optional argument specifies the obarray to use;
3526 it defaults to the value of `obarray'. */)
3527 (name, obarray)
3528 Lisp_Object name, obarray;
3530 register Lisp_Object tem, string;
3532 if (NILP (obarray)) obarray = Vobarray;
3533 obarray = check_obarray (obarray);
3535 if (!SYMBOLP (name))
3537 CHECK_STRING (name);
3538 string = name;
3540 else
3541 string = SYMBOL_NAME (name);
3543 tem = oblookup (obarray, SDATA (string), SCHARS (string), SBYTES (string));
3544 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3545 return Qnil;
3546 else
3547 return tem;
3550 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3551 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3552 The value is t if a symbol was found and deleted, nil otherwise.
3553 NAME may be a string or a symbol. If it is a symbol, that symbol
3554 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3555 OBARRAY defaults to the value of the variable `obarray'. */)
3556 (name, obarray)
3557 Lisp_Object name, obarray;
3559 register Lisp_Object string, tem;
3560 int hash;
3562 if (NILP (obarray)) obarray = Vobarray;
3563 obarray = check_obarray (obarray);
3565 if (SYMBOLP (name))
3566 string = SYMBOL_NAME (name);
3567 else
3569 CHECK_STRING (name);
3570 string = name;
3573 tem = oblookup (obarray, SDATA (string),
3574 SCHARS (string),
3575 SBYTES (string));
3576 if (INTEGERP (tem))
3577 return Qnil;
3578 /* If arg was a symbol, don't delete anything but that symbol itself. */
3579 if (SYMBOLP (name) && !EQ (name, tem))
3580 return Qnil;
3582 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3583 XSYMBOL (tem)->constant = 0;
3584 XSYMBOL (tem)->indirect_variable = 0;
3586 hash = oblookup_last_bucket_number;
3588 if (EQ (XVECTOR (obarray)->contents[hash], tem))
3590 if (XSYMBOL (tem)->next)
3591 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3592 else
3593 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3595 else
3597 Lisp_Object tail, following;
3599 for (tail = XVECTOR (obarray)->contents[hash];
3600 XSYMBOL (tail)->next;
3601 tail = following)
3603 XSETSYMBOL (following, XSYMBOL (tail)->next);
3604 if (EQ (following, tem))
3606 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3607 break;
3612 return Qt;
3615 /* Return the symbol in OBARRAY whose names matches the string
3616 of SIZE characters (SIZE_BYTE bytes) at PTR.
3617 If there is no such symbol in OBARRAY, return nil.
3619 Also store the bucket number in oblookup_last_bucket_number. */
3621 Lisp_Object
3622 oblookup (obarray, ptr, size, size_byte)
3623 Lisp_Object obarray;
3624 register const char *ptr;
3625 int size, size_byte;
3627 int hash;
3628 int obsize;
3629 register Lisp_Object tail;
3630 Lisp_Object bucket, tem;
3632 if (!VECTORP (obarray)
3633 || (obsize = XVECTOR (obarray)->size) == 0)
3635 obarray = check_obarray (obarray);
3636 obsize = XVECTOR (obarray)->size;
3638 /* This is sometimes needed in the middle of GC. */
3639 obsize &= ~ARRAY_MARK_FLAG;
3640 /* Combining next two lines breaks VMS C 2.3. */
3641 hash = hash_string (ptr, size_byte);
3642 hash %= obsize;
3643 bucket = XVECTOR (obarray)->contents[hash];
3644 oblookup_last_bucket_number = hash;
3645 if (EQ (bucket, make_number (0)))
3647 else if (!SYMBOLP (bucket))
3648 error ("Bad data in guts of obarray"); /* Like CADR error message */
3649 else
3650 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3652 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3653 && SCHARS (SYMBOL_NAME (tail)) == size
3654 && !bcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
3655 return tail;
3656 else if (XSYMBOL (tail)->next == 0)
3657 break;
3659 XSETINT (tem, hash);
3660 return tem;
3663 static int
3664 hash_string (ptr, len)
3665 const unsigned char *ptr;
3666 int len;
3668 register const unsigned char *p = ptr;
3669 register const unsigned char *end = p + len;
3670 register unsigned char c;
3671 register int hash = 0;
3673 while (p != end)
3675 c = *p++;
3676 if (c >= 0140) c -= 40;
3677 hash = ((hash<<3) + (hash>>28) + c);
3679 return hash & 07777777777;
3682 void
3683 map_obarray (obarray, fn, arg)
3684 Lisp_Object obarray;
3685 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3686 Lisp_Object arg;
3688 register int i;
3689 register Lisp_Object tail;
3690 CHECK_VECTOR (obarray);
3691 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3693 tail = XVECTOR (obarray)->contents[i];
3694 if (SYMBOLP (tail))
3695 while (1)
3697 (*fn) (tail, arg);
3698 if (XSYMBOL (tail)->next == 0)
3699 break;
3700 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3705 void
3706 mapatoms_1 (sym, function)
3707 Lisp_Object sym, function;
3709 call1 (function, sym);
3712 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3713 doc: /* Call FUNCTION on every symbol in OBARRAY.
3714 OBARRAY defaults to the value of `obarray'. */)
3715 (function, obarray)
3716 Lisp_Object function, obarray;
3718 if (NILP (obarray)) obarray = Vobarray;
3719 obarray = check_obarray (obarray);
3721 map_obarray (obarray, mapatoms_1, function);
3722 return Qnil;
3725 #define OBARRAY_SIZE 1511
3727 void
3728 init_obarray ()
3730 Lisp_Object oblength;
3731 int hash;
3732 Lisp_Object *tem;
3734 XSETFASTINT (oblength, OBARRAY_SIZE);
3736 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3737 Vobarray = Fmake_vector (oblength, make_number (0));
3738 initial_obarray = Vobarray;
3739 staticpro (&initial_obarray);
3740 /* Intern nil in the obarray */
3741 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3742 XSYMBOL (Qnil)->constant = 1;
3744 /* These locals are to kludge around a pyramid compiler bug. */
3745 hash = hash_string ("nil", 3);
3746 /* Separate statement here to avoid VAXC bug. */
3747 hash %= OBARRAY_SIZE;
3748 tem = &XVECTOR (Vobarray)->contents[hash];
3749 *tem = Qnil;
3751 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3752 XSYMBOL (Qnil)->function = Qunbound;
3753 XSYMBOL (Qunbound)->value = Qunbound;
3754 XSYMBOL (Qunbound)->function = Qunbound;
3756 Qt = intern ("t");
3757 XSYMBOL (Qnil)->value = Qnil;
3758 XSYMBOL (Qnil)->plist = Qnil;
3759 XSYMBOL (Qt)->value = Qt;
3760 XSYMBOL (Qt)->constant = 1;
3762 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3763 Vpurify_flag = Qt;
3765 Qvariable_documentation = intern ("variable-documentation");
3766 staticpro (&Qvariable_documentation);
3768 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3769 read_buffer = (char *) xmalloc (read_buffer_size);
3772 void
3773 defsubr (sname)
3774 struct Lisp_Subr *sname;
3776 Lisp_Object sym;
3777 sym = intern (sname->symbol_name);
3778 XSETSUBR (XSYMBOL (sym)->function, sname);
3781 #ifdef NOTDEF /* use fset in subr.el now */
3782 void
3783 defalias (sname, string)
3784 struct Lisp_Subr *sname;
3785 char *string;
3787 Lisp_Object sym;
3788 sym = intern (string);
3789 XSETSUBR (XSYMBOL (sym)->function, sname);
3791 #endif /* NOTDEF */
3793 /* Define an "integer variable"; a symbol whose value is forwarded
3794 to a C variable of type int. Sample call: */
3795 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3796 void
3797 defvar_int (namestring, address)
3798 char *namestring;
3799 EMACS_INT *address;
3801 Lisp_Object sym, val;
3802 sym = intern (namestring);
3803 val = allocate_misc ();
3804 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3805 XINTFWD (val)->intvar = address;
3806 SET_SYMBOL_VALUE (sym, val);
3809 /* Similar but define a variable whose value is t if address contains 1,
3810 nil if address contains 0 */
3811 void
3812 defvar_bool (namestring, address)
3813 char *namestring;
3814 int *address;
3816 Lisp_Object sym, val;
3817 sym = intern (namestring);
3818 val = allocate_misc ();
3819 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3820 XBOOLFWD (val)->boolvar = address;
3821 SET_SYMBOL_VALUE (sym, val);
3822 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3825 /* Similar but define a variable whose value is the Lisp Object stored
3826 at address. Two versions: with and without gc-marking of the C
3827 variable. The nopro version is used when that variable will be
3828 gc-marked for some other reason, since marking the same slot twice
3829 can cause trouble with strings. */
3830 void
3831 defvar_lisp_nopro (namestring, address)
3832 char *namestring;
3833 Lisp_Object *address;
3835 Lisp_Object sym, val;
3836 sym = intern (namestring);
3837 val = allocate_misc ();
3838 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3839 XOBJFWD (val)->objvar = address;
3840 SET_SYMBOL_VALUE (sym, val);
3843 void
3844 defvar_lisp (namestring, address)
3845 char *namestring;
3846 Lisp_Object *address;
3848 defvar_lisp_nopro (namestring, address);
3849 staticpro (address);
3852 /* Similar but define a variable whose value is the Lisp Object stored in
3853 the current buffer. address is the address of the slot in the buffer
3854 that is current now. */
3856 void
3857 defvar_per_buffer (namestring, address, type, doc)
3858 char *namestring;
3859 Lisp_Object *address;
3860 Lisp_Object type;
3861 char *doc;
3863 Lisp_Object sym, val;
3864 int offset;
3866 sym = intern (namestring);
3867 val = allocate_misc ();
3868 offset = (char *)address - (char *)current_buffer;
3870 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3871 XBUFFER_OBJFWD (val)->offset = offset;
3872 SET_SYMBOL_VALUE (sym, val);
3873 PER_BUFFER_SYMBOL (offset) = sym;
3874 PER_BUFFER_TYPE (offset) = type;
3876 if (PER_BUFFER_IDX (offset) == 0)
3877 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3878 slot of buffer_local_flags */
3879 abort ();
3883 /* Similar but define a variable whose value is the Lisp Object stored
3884 at a particular offset in the current kboard object. */
3886 void
3887 defvar_kboard (namestring, offset)
3888 char *namestring;
3889 int offset;
3891 Lisp_Object sym, val;
3892 sym = intern (namestring);
3893 val = allocate_misc ();
3894 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3895 XKBOARD_OBJFWD (val)->offset = offset;
3896 SET_SYMBOL_VALUE (sym, val);
3899 /* Record the value of load-path used at the start of dumping
3900 so we can see if the site changed it later during dumping. */
3901 static Lisp_Object dump_path;
3903 void
3904 init_lread ()
3906 char *normal;
3907 int turn_off_warning = 0;
3909 /* Compute the default load-path. */
3910 #ifdef CANNOT_DUMP
3911 normal = PATH_LOADSEARCH;
3912 Vload_path = decode_env_path (0, normal);
3913 #else
3914 if (NILP (Vpurify_flag))
3915 normal = PATH_LOADSEARCH;
3916 else
3917 normal = PATH_DUMPLOADSEARCH;
3919 /* In a dumped Emacs, we normally have to reset the value of
3920 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3921 uses ../lisp, instead of the path of the installed elisp
3922 libraries. However, if it appears that Vload_path was changed
3923 from the default before dumping, don't override that value. */
3924 if (initialized)
3926 if (! NILP (Fequal (dump_path, Vload_path)))
3928 Vload_path = decode_env_path (0, normal);
3929 if (!NILP (Vinstallation_directory))
3931 Lisp_Object tem, tem1, sitelisp;
3933 /* Remove site-lisp dirs from path temporarily and store
3934 them in sitelisp, then conc them on at the end so
3935 they're always first in path. */
3936 sitelisp = Qnil;
3937 while (1)
3939 tem = Fcar (Vload_path);
3940 tem1 = Fstring_match (build_string ("site-lisp"),
3941 tem, Qnil);
3942 if (!NILP (tem1))
3944 Vload_path = Fcdr (Vload_path);
3945 sitelisp = Fcons (tem, sitelisp);
3947 else
3948 break;
3951 /* Add to the path the lisp subdir of the
3952 installation dir, if it exists. */
3953 tem = Fexpand_file_name (build_string ("lisp"),
3954 Vinstallation_directory);
3955 tem1 = Ffile_exists_p (tem);
3956 if (!NILP (tem1))
3958 if (NILP (Fmember (tem, Vload_path)))
3960 turn_off_warning = 1;
3961 Vload_path = Fcons (tem, Vload_path);
3964 else
3965 /* That dir doesn't exist, so add the build-time
3966 Lisp dirs instead. */
3967 Vload_path = nconc2 (Vload_path, dump_path);
3969 /* Add leim under the installation dir, if it exists. */
3970 tem = Fexpand_file_name (build_string ("leim"),
3971 Vinstallation_directory);
3972 tem1 = Ffile_exists_p (tem);
3973 if (!NILP (tem1))
3975 if (NILP (Fmember (tem, Vload_path)))
3976 Vload_path = Fcons (tem, Vload_path);
3979 /* Add site-list under the installation dir, if it exists. */
3980 tem = Fexpand_file_name (build_string ("site-lisp"),
3981 Vinstallation_directory);
3982 tem1 = Ffile_exists_p (tem);
3983 if (!NILP (tem1))
3985 if (NILP (Fmember (tem, Vload_path)))
3986 Vload_path = Fcons (tem, Vload_path);
3989 /* If Emacs was not built in the source directory,
3990 and it is run from where it was built, add to load-path
3991 the lisp, leim and site-lisp dirs under that directory. */
3993 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3995 Lisp_Object tem2;
3997 tem = Fexpand_file_name (build_string ("src/Makefile"),
3998 Vinstallation_directory);
3999 tem1 = Ffile_exists_p (tem);
4001 /* Don't be fooled if they moved the entire source tree
4002 AFTER dumping Emacs. If the build directory is indeed
4003 different from the source dir, src/Makefile.in and
4004 src/Makefile will not be found together. */
4005 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4006 Vinstallation_directory);
4007 tem2 = Ffile_exists_p (tem);
4008 if (!NILP (tem1) && NILP (tem2))
4010 tem = Fexpand_file_name (build_string ("lisp"),
4011 Vsource_directory);
4013 if (NILP (Fmember (tem, Vload_path)))
4014 Vload_path = Fcons (tem, Vload_path);
4016 tem = Fexpand_file_name (build_string ("leim"),
4017 Vsource_directory);
4019 if (NILP (Fmember (tem, Vload_path)))
4020 Vload_path = Fcons (tem, Vload_path);
4022 tem = Fexpand_file_name (build_string ("site-lisp"),
4023 Vsource_directory);
4025 if (NILP (Fmember (tem, Vload_path)))
4026 Vload_path = Fcons (tem, Vload_path);
4029 if (!NILP (sitelisp))
4030 Vload_path = nconc2 (Fnreverse (sitelisp), Vload_path);
4034 else
4036 /* NORMAL refers to the lisp dir in the source directory. */
4037 /* We used to add ../lisp at the front here, but
4038 that caused trouble because it was copied from dump_path
4039 into Vload_path, aboe, when Vinstallation_directory was non-nil.
4040 It should be unnecessary. */
4041 Vload_path = decode_env_path (0, normal);
4042 dump_path = Vload_path;
4044 #endif
4046 #if (!(defined(WINDOWSNT) || (defined(HAVE_CARBON))))
4047 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
4048 almost never correct, thereby causing a warning to be printed out that
4049 confuses users. Since PATH_LOADSEARCH is always overridden by the
4050 EMACSLOADPATH environment variable below, disable the warning on NT.
4051 Also, when using the "self-contained" option for Carbon Emacs for MacOSX,
4052 the "standard" paths may not exist and would be overridden by
4053 EMACSLOADPATH as on NT. Since this depends on how the executable
4054 was build and packaged, turn off the warnings in general */
4056 /* Warn if dirs in the *standard* path don't exist. */
4057 if (!turn_off_warning)
4059 Lisp_Object path_tail;
4061 for (path_tail = Vload_path;
4062 !NILP (path_tail);
4063 path_tail = XCDR (path_tail))
4065 Lisp_Object dirfile;
4066 dirfile = Fcar (path_tail);
4067 if (STRINGP (dirfile))
4069 dirfile = Fdirectory_file_name (dirfile);
4070 if (access (SDATA (dirfile), 0) < 0)
4071 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
4072 XCAR (path_tail));
4076 #endif /* !(WINDOWSNT || HAVE_CARBON) */
4078 /* If the EMACSLOADPATH environment variable is set, use its value.
4079 This doesn't apply if we're dumping. */
4080 #ifndef CANNOT_DUMP
4081 if (NILP (Vpurify_flag)
4082 && egetenv ("EMACSLOADPATH"))
4083 #endif
4084 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
4086 Vvalues = Qnil;
4088 load_in_progress = 0;
4089 Vload_file_name = Qnil;
4091 load_descriptor_list = Qnil;
4093 Vstandard_input = Qt;
4094 Vloads_in_progress = Qnil;
4097 /* Print a warning, using format string FORMAT, that directory DIRNAME
4098 does not exist. Print it on stderr and put it in *Message*. */
4100 void
4101 dir_warning (format, dirname)
4102 char *format;
4103 Lisp_Object dirname;
4105 char *buffer
4106 = (char *) alloca (SCHARS (dirname) + strlen (format) + 5);
4108 fprintf (stderr, format, SDATA (dirname));
4109 sprintf (buffer, format, SDATA (dirname));
4110 /* Don't log the warning before we've initialized!! */
4111 if (initialized)
4112 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
4115 void
4116 syms_of_lread ()
4118 defsubr (&Sread);
4119 defsubr (&Sread_from_string);
4120 defsubr (&Sintern);
4121 defsubr (&Sintern_soft);
4122 defsubr (&Sunintern);
4123 defsubr (&Sget_load_suffixes);
4124 defsubr (&Sload);
4125 defsubr (&Seval_buffer);
4126 defsubr (&Seval_region);
4127 defsubr (&Sread_char);
4128 defsubr (&Sread_char_exclusive);
4129 defsubr (&Sread_event);
4130 defsubr (&Sget_file_char);
4131 defsubr (&Smapatoms);
4132 defsubr (&Slocate_file_internal);
4134 DEFVAR_LISP ("obarray", &Vobarray,
4135 doc: /* Symbol table for use by `intern' and `read'.
4136 It is a vector whose length ought to be prime for best results.
4137 The vector's contents don't make sense if examined from Lisp programs;
4138 to find all the symbols in an obarray, use `mapatoms'. */);
4140 DEFVAR_LISP ("values", &Vvalues,
4141 doc: /* List of values of all expressions which were read, evaluated and printed.
4142 Order is reverse chronological. */);
4144 DEFVAR_LISP ("standard-input", &Vstandard_input,
4145 doc: /* Stream for read to get input from.
4146 See documentation of `read' for possible values. */);
4147 Vstandard_input = Qt;
4149 DEFVAR_LISP ("read-with-symbol-positions", &Vread_with_symbol_positions,
4150 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4152 If this variable is a buffer, then only forms read from that buffer
4153 will be added to `read-symbol-positions-list'.
4154 If this variable is t, then all read forms will be added.
4155 The effect of all other values other than nil are not currently
4156 defined, although they may be in the future.
4158 The positions are relative to the last call to `read' or
4159 `read-from-string'. It is probably a bad idea to set this variable at
4160 the toplevel; bind it instead. */);
4161 Vread_with_symbol_positions = Qnil;
4163 DEFVAR_LISP ("read-symbol-positions-list", &Vread_symbol_positions_list,
4164 doc: /* A list mapping read symbols to their positions.
4165 This variable is modified during calls to `read' or
4166 `read-from-string', but only when `read-with-symbol-positions' is
4167 non-nil.
4169 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4170 CHAR-POSITION is an integer giving the offset of that occurrence of the
4171 symbol from the position where `read' or `read-from-string' started.
4173 Note that a symbol will appear multiple times in this list, if it was
4174 read multiple times. The list is in the same order as the symbols
4175 were read in. */);
4176 Vread_symbol_positions_list = Qnil;
4178 DEFVAR_LISP ("load-path", &Vload_path,
4179 doc: /* *List of directories to search for files to load.
4180 Each element is a string (directory name) or nil (try default directory).
4181 Initialized based on EMACSLOADPATH environment variable, if any,
4182 otherwise to default specified by file `epaths.h' when Emacs was built. */);
4184 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
4185 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4186 This list should not include the empty string.
4187 `load' and related functions try to append these suffixes, in order,
4188 to the specified file name if a Lisp suffix is allowed or required. */);
4189 Vload_suffixes = Fcons (build_string (".elc"),
4190 Fcons (build_string (".el"), Qnil));
4191 DEFVAR_LISP ("load-file-rep-suffixes", &Vload_file_rep_suffixes,
4192 doc: /* List of suffixes that indicate representations of \
4193 the same file.
4194 This list should normally start with the empty string.
4196 Enabling Auto Compression mode appends the suffixes in
4197 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4198 mode removes them again. `load' and related functions use this list to
4199 determine whether they should look for compressed versions of a file
4200 and, if so, which suffixes they should try to append to the file name
4201 in order to do so. However, if you want to customize which suffixes
4202 the loading functions recognize as compression suffixes, you should
4203 customize `jka-compr-load-suffixes' rather than the present variable. */);
4204 Vload_file_rep_suffixes = Fcons (empty_unibyte_string, Qnil);
4206 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
4207 doc: /* Non-nil iff inside of `load'. */);
4209 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
4210 doc: /* An alist of expressions to be evalled when particular files are loaded.
4211 Each element looks like (REGEXP-OR-FEATURE FORMS...).
4213 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4214 a symbol \(a feature name).
4216 When `load' is run and the file-name argument matches an element's
4217 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4218 REGEXP-OR-FEATURE, the FORMS in the element are executed.
4220 An error in FORMS does not undo the load, but does prevent execution of
4221 the rest of the FORMS. */);
4222 Vafter_load_alist = Qnil;
4224 DEFVAR_LISP ("load-history", &Vload_history,
4225 doc: /* Alist mapping file names to symbols and features.
4226 Each alist element is a list that starts with a file name,
4227 except for one element (optional) that starts with nil and describes
4228 definitions evaluated from buffers not visiting files.
4230 The file name is absolute and is the true file name (i.e. it doesn't
4231 contain symbolic links) of the loaded file.
4233 The remaining elements of each list are symbols defined as variables
4234 and cons cells of the form `(provide . FEATURE)', `(require . FEATURE)',
4235 `(defun . FUNCTION)', `(autoload . SYMBOL)', `(defface . SYMBOL)'
4236 and `(t . SYMBOL)'. An element `(t . SYMBOL)' precedes an entry
4237 `(defun . FUNCTION)', and means that SYMBOL was an autoload before
4238 this file redefined it as a function.
4240 During preloading, the file name recorded is relative to the main Lisp
4241 directory. These file names are converted to absolute at startup. */);
4242 Vload_history = Qnil;
4244 DEFVAR_LISP ("load-file-name", &Vload_file_name,
4245 doc: /* Full name of file being loaded by `load'. */);
4246 Vload_file_name = Qnil;
4248 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
4249 doc: /* File name, including directory, of user's initialization file.
4250 If the file loaded had extension `.elc', and the corresponding source file
4251 exists, this variable contains the name of source file, suitable for use
4252 by functions like `custom-save-all' which edit the init file.
4253 While Emacs loads and evaluates the init file, value is the real name
4254 of the file, regardless of whether or not it has the `.elc' extension. */);
4255 Vuser_init_file = Qnil;
4257 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
4258 doc: /* Used for internal purposes by `load'. */);
4259 Vcurrent_load_list = Qnil;
4261 DEFVAR_LISP ("load-read-function", &Vload_read_function,
4262 doc: /* Function used by `load' and `eval-region' for reading expressions.
4263 The default is nil, which means use the function `read'. */);
4264 Vload_read_function = Qnil;
4266 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
4267 doc: /* Function called in `load' for loading an Emacs Lisp source file.
4268 This function is for doing code conversion before reading the source file.
4269 If nil, loading is done without any code conversion.
4270 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
4271 FULLNAME is the full name of FILE.
4272 See `load' for the meaning of the remaining arguments. */);
4273 Vload_source_file_function = Qnil;
4275 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
4276 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4277 This is useful when the file being loaded is a temporary copy. */);
4278 load_force_doc_strings = 0;
4280 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
4281 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4282 This is normally bound by `load' and `eval-buffer' to control `read',
4283 and is not meant for users to change. */);
4284 load_convert_to_unibyte = 0;
4286 DEFVAR_LISP ("source-directory", &Vsource_directory,
4287 doc: /* Directory in which Emacs sources were found when Emacs was built.
4288 You cannot count on them to still be there! */);
4289 Vsource_directory
4290 = Fexpand_file_name (build_string ("../"),
4291 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
4293 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
4294 doc: /* List of files that were preloaded (when dumping Emacs). */);
4295 Vpreloaded_file_list = Qnil;
4297 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
4298 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4299 Vbyte_boolean_vars = Qnil;
4301 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
4302 doc: /* Non-nil means load dangerous compiled Lisp files.
4303 Some versions of XEmacs use different byte codes than Emacs. These
4304 incompatible byte codes can make Emacs crash when it tries to execute
4305 them. */);
4306 load_dangerous_libraries = 0;
4308 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
4309 doc: /* Regular expression matching safe to load compiled Lisp files.
4310 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4311 from the file, and matches them against this regular expression.
4312 When the regular expression matches, the file is considered to be safe
4313 to load. See also `load-dangerous-libraries'. */);
4314 Vbytecomp_version_regexp
4315 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4317 DEFVAR_LISP ("eval-buffer-list", &Veval_buffer_list,
4318 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4319 Veval_buffer_list = Qnil;
4321 /* Vsource_directory was initialized in init_lread. */
4323 load_descriptor_list = Qnil;
4324 staticpro (&load_descriptor_list);
4326 Qcurrent_load_list = intern ("current-load-list");
4327 staticpro (&Qcurrent_load_list);
4329 Qstandard_input = intern ("standard-input");
4330 staticpro (&Qstandard_input);
4332 Qread_char = intern ("read-char");
4333 staticpro (&Qread_char);
4335 Qget_file_char = intern ("get-file-char");
4336 staticpro (&Qget_file_char);
4338 Qget_emacs_mule_file_char = intern ("get-emacs-mule-file-char");
4339 staticpro (&Qget_emacs_mule_file_char);
4341 Qload_force_doc_strings = intern ("load-force-doc-strings");
4342 staticpro (&Qload_force_doc_strings);
4344 Qbackquote = intern ("`");
4345 staticpro (&Qbackquote);
4346 Qcomma = intern (",");
4347 staticpro (&Qcomma);
4348 Qcomma_at = intern (",@");
4349 staticpro (&Qcomma_at);
4350 Qcomma_dot = intern (",.");
4351 staticpro (&Qcomma_dot);
4353 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
4354 staticpro (&Qinhibit_file_name_operation);
4356 Qascii_character = intern ("ascii-character");
4357 staticpro (&Qascii_character);
4359 Qfunction = intern ("function");
4360 staticpro (&Qfunction);
4362 Qload = intern ("load");
4363 staticpro (&Qload);
4365 Qload_file_name = intern ("load-file-name");
4366 staticpro (&Qload_file_name);
4368 Qeval_buffer_list = intern ("eval-buffer-list");
4369 staticpro (&Qeval_buffer_list);
4371 Qfile_truename = intern ("file-truename");
4372 staticpro (&Qfile_truename) ;
4374 Qdo_after_load_evaluation = intern ("do-after-load-evaluation");
4375 staticpro (&Qdo_after_load_evaluation) ;
4377 staticpro (&dump_path);
4379 staticpro (&read_objects);
4380 read_objects = Qnil;
4381 staticpro (&seen_list);
4382 seen_list = Qnil;
4384 Vloads_in_progress = Qnil;
4385 staticpro (&Vloads_in_progress);
4388 /* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d
4389 (do not change this comment) */