merge trunk
[emacs.git] / src / lread.c
bloba2b6d1f26d92f9b6bb47ebdec9eea5f435a0cc99
1 /* Lisp parsing and input streams.
3 Copyright (C) 1985-1989, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/file.h>
26 #include <errno.h>
27 #include <limits.h> /* For CHAR_BIT. */
28 #include <stat-time.h>
29 #include "lisp.h"
30 #include "intervals.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "charset.h"
34 #include "coding.h"
35 #include <epaths.h>
36 #include "commands.h"
37 #include "keyboard.h"
38 #include "frame.h"
39 #include "termhooks.h"
40 #include "coding.h"
41 #include "blockinput.h"
43 #ifdef MSDOS
44 #include "msdos.h"
45 #endif
47 #ifdef HAVE_NS
48 #include "nsterm.h"
49 #endif
51 #include <unistd.h>
53 #ifdef HAVE_SETLOCALE
54 #include <locale.h>
55 #endif /* HAVE_SETLOCALE */
57 #include <fcntl.h>
59 #ifdef HAVE_FSEEKO
60 #define file_offset off_t
61 #define file_tell ftello
62 #else
63 #define file_offset long
64 #define file_tell ftell
65 #endif
67 /* Hash table read constants. */
68 static Lisp_Object Qhash_table, Qdata;
69 static Lisp_Object Qtest, Qsize;
70 static Lisp_Object Qweakness;
71 static Lisp_Object Qrehash_size;
72 static Lisp_Object Qrehash_threshold;
74 static Lisp_Object Qread_char, Qget_file_char, Qcurrent_load_list;
75 Lisp_Object Qstandard_input;
76 Lisp_Object Qvariable_documentation;
77 static Lisp_Object Qascii_character, Qload, Qload_file_name;
78 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
79 static Lisp_Object Qinhibit_file_name_operation;
80 static Lisp_Object Qeval_buffer_list;
81 Lisp_Object Qlexical_binding;
82 static Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
84 /* Used instead of Qget_file_char while loading *.elc files compiled
85 by Emacs 21 or older. */
86 static Lisp_Object Qget_emacs_mule_file_char;
88 static Lisp_Object Qload_force_doc_strings;
90 static Lisp_Object Qload_in_progress;
92 /* The association list of objects read with the #n=object form.
93 Each member of the list has the form (n . object), and is used to
94 look up the object for the corresponding #n# construct.
95 It must be set to nil before all top-level calls to read0. */
96 static Lisp_Object read_objects;
98 /* Nonzero means READCHAR should read bytes one by one (not character)
99 when READCHARFUN is Qget_file_char or Qget_emacs_mule_file_char.
100 This is set to 1 by read1 temporarily while handling #@NUMBER. */
101 static int load_each_byte;
103 /* List of descriptors now open for Fload. */
104 static Lisp_Object load_descriptor_list;
106 /* File for get_file_char to read from. Use by load. */
107 static FILE *instream;
109 /* For use within read-from-string (this reader is non-reentrant!!) */
110 static ptrdiff_t read_from_string_index;
111 static ptrdiff_t read_from_string_index_byte;
112 static ptrdiff_t read_from_string_limit;
114 /* Number of characters read in the current call to Fread or
115 Fread_from_string. */
116 static EMACS_INT readchar_count;
118 /* This contains the last string skipped with #@. */
119 static char *saved_doc_string;
120 /* Length of buffer allocated in saved_doc_string. */
121 static ptrdiff_t saved_doc_string_size;
122 /* Length of actual data in saved_doc_string. */
123 static ptrdiff_t saved_doc_string_length;
124 /* This is the file position that string came from. */
125 static file_offset saved_doc_string_position;
127 /* This contains the previous string skipped with #@.
128 We copy it from saved_doc_string when a new string
129 is put in saved_doc_string. */
130 static char *prev_saved_doc_string;
131 /* Length of buffer allocated in prev_saved_doc_string. */
132 static ptrdiff_t prev_saved_doc_string_size;
133 /* Length of actual data in prev_saved_doc_string. */
134 static ptrdiff_t prev_saved_doc_string_length;
135 /* This is the file position that string came from. */
136 static file_offset prev_saved_doc_string_position;
138 /* Nonzero means inside a new-style backquote
139 with no surrounding parentheses.
140 Fread initializes this to zero, so we need not specbind it
141 or worry about what happens to it when there is an error. */
142 static int new_backquote_flag;
143 static Lisp_Object Qold_style_backquotes;
145 /* A list of file names for files being loaded in Fload. Used to
146 check for recursive loads. */
148 static Lisp_Object Vloads_in_progress;
150 static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
151 Lisp_Object);
153 static void readevalloop (Lisp_Object, FILE*, Lisp_Object, int,
154 Lisp_Object, Lisp_Object,
155 Lisp_Object, Lisp_Object);
156 static Lisp_Object load_unwind (Lisp_Object);
157 static Lisp_Object load_descriptor_unwind (Lisp_Object);
159 /* Functions that read one byte from the current source READCHARFUN
160 or unreads one byte. If the integer argument C is -1, it returns
161 one read byte, or -1 when there's no more byte in the source. If C
162 is 0 or positive, it unreads C, and the return value is not
163 interesting. */
165 static int readbyte_for_lambda (int, Lisp_Object);
166 static int readbyte_from_file (int, Lisp_Object);
167 static int readbyte_from_string (int, Lisp_Object);
169 /* Handle unreading and rereading of characters.
170 Write READCHAR to read a character,
171 UNREAD(c) to unread c to be read again.
173 These macros correctly read/unread multibyte characters. */
175 #define READCHAR readchar (readcharfun, NULL)
176 #define UNREAD(c) unreadchar (readcharfun, c)
178 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
179 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
181 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
182 Qlambda, or a cons, we use this to keep an unread character because
183 a file stream can't handle multibyte-char unreading. The value -1
184 means that there's no unread character. */
185 static int unread_char;
187 static int
188 readchar (Lisp_Object readcharfun, bool *multibyte)
190 Lisp_Object tem;
191 register int c;
192 int (*readbyte) (int, Lisp_Object);
193 unsigned char buf[MAX_MULTIBYTE_LENGTH];
194 int i, len;
195 int emacs_mule_encoding = 0;
197 if (multibyte)
198 *multibyte = 0;
200 readchar_count++;
202 if (BUFFERP (readcharfun))
204 register struct buffer *inbuffer = XBUFFER (readcharfun);
206 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
208 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
209 return -1;
211 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
213 /* Fetch the character code from the buffer. */
214 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
215 BUF_INC_POS (inbuffer, pt_byte);
216 c = STRING_CHAR (p);
217 if (multibyte)
218 *multibyte = 1;
220 else
222 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
223 if (! ASCII_BYTE_P (c))
224 c = BYTE8_TO_CHAR (c);
225 pt_byte++;
227 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
229 return c;
231 if (MARKERP (readcharfun))
233 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
235 ptrdiff_t bytepos = marker_byte_position (readcharfun);
237 if (bytepos >= BUF_ZV_BYTE (inbuffer))
238 return -1;
240 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
242 /* Fetch the character code from the buffer. */
243 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
244 BUF_INC_POS (inbuffer, bytepos);
245 c = STRING_CHAR (p);
246 if (multibyte)
247 *multibyte = 1;
249 else
251 c = BUF_FETCH_BYTE (inbuffer, bytepos);
252 if (! ASCII_BYTE_P (c))
253 c = BYTE8_TO_CHAR (c);
254 bytepos++;
257 XMARKER (readcharfun)->bytepos = bytepos;
258 XMARKER (readcharfun)->charpos++;
260 return c;
263 if (EQ (readcharfun, Qlambda))
265 readbyte = readbyte_for_lambda;
266 goto read_multibyte;
269 if (EQ (readcharfun, Qget_file_char))
271 readbyte = readbyte_from_file;
272 goto read_multibyte;
275 if (STRINGP (readcharfun))
277 if (read_from_string_index >= read_from_string_limit)
278 c = -1;
279 else if (STRING_MULTIBYTE (readcharfun))
281 if (multibyte)
282 *multibyte = 1;
283 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
284 read_from_string_index,
285 read_from_string_index_byte);
287 else
289 c = SREF (readcharfun, read_from_string_index_byte);
290 read_from_string_index++;
291 read_from_string_index_byte++;
293 return c;
296 if (CONSP (readcharfun))
298 /* This is the case that read_vector is reading from a unibyte
299 string that contains a byte sequence previously skipped
300 because of #@NUMBER. The car part of readcharfun is that
301 string, and the cdr part is a value of readcharfun given to
302 read_vector. */
303 readbyte = readbyte_from_string;
304 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
305 emacs_mule_encoding = 1;
306 goto read_multibyte;
309 if (EQ (readcharfun, Qget_emacs_mule_file_char))
311 readbyte = readbyte_from_file;
312 emacs_mule_encoding = 1;
313 goto read_multibyte;
316 tem = call0 (readcharfun);
318 if (NILP (tem))
319 return -1;
320 return XINT (tem);
322 read_multibyte:
323 if (unread_char >= 0)
325 c = unread_char;
326 unread_char = -1;
327 return c;
329 c = (*readbyte) (-1, readcharfun);
330 if (c < 0 || load_each_byte)
331 return c;
332 if (multibyte)
333 *multibyte = 1;
334 if (ASCII_BYTE_P (c))
335 return c;
336 if (emacs_mule_encoding)
337 return read_emacs_mule_char (c, readbyte, readcharfun);
338 i = 0;
339 buf[i++] = c;
340 len = BYTES_BY_CHAR_HEAD (c);
341 while (i < len)
343 c = (*readbyte) (-1, readcharfun);
344 if (c < 0 || ! TRAILING_CODE_P (c))
346 while (--i > 1)
347 (*readbyte) (buf[i], readcharfun);
348 return BYTE8_TO_CHAR (buf[0]);
350 buf[i++] = c;
352 return STRING_CHAR (buf);
355 /* Unread the character C in the way appropriate for the stream READCHARFUN.
356 If the stream is a user function, call it with the char as argument. */
358 static void
359 unreadchar (Lisp_Object readcharfun, int c)
361 readchar_count--;
362 if (c == -1)
363 /* Don't back up the pointer if we're unreading the end-of-input mark,
364 since readchar didn't advance it when we read it. */
366 else if (BUFFERP (readcharfun))
368 struct buffer *b = XBUFFER (readcharfun);
369 ptrdiff_t charpos = BUF_PT (b);
370 ptrdiff_t bytepos = BUF_PT_BYTE (b);
372 if (! NILP (BVAR (b, enable_multibyte_characters)))
373 BUF_DEC_POS (b, bytepos);
374 else
375 bytepos--;
377 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
379 else if (MARKERP (readcharfun))
381 struct buffer *b = XMARKER (readcharfun)->buffer;
382 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
384 XMARKER (readcharfun)->charpos--;
385 if (! NILP (BVAR (b, enable_multibyte_characters)))
386 BUF_DEC_POS (b, bytepos);
387 else
388 bytepos--;
390 XMARKER (readcharfun)->bytepos = bytepos;
392 else if (STRINGP (readcharfun))
394 read_from_string_index--;
395 read_from_string_index_byte
396 = string_char_to_byte (readcharfun, read_from_string_index);
398 else if (CONSP (readcharfun))
400 unread_char = c;
402 else if (EQ (readcharfun, Qlambda))
404 unread_char = c;
406 else if (EQ (readcharfun, Qget_file_char)
407 || EQ (readcharfun, Qget_emacs_mule_file_char))
409 if (load_each_byte)
411 block_input ();
412 ungetc (c, instream);
413 unblock_input ();
415 else
416 unread_char = c;
418 else
419 call1 (readcharfun, make_number (c));
422 static int
423 readbyte_for_lambda (int c, Lisp_Object readcharfun)
425 return read_bytecode_char (c >= 0);
429 static int
430 readbyte_from_file (int c, Lisp_Object readcharfun)
432 if (c >= 0)
434 block_input ();
435 ungetc (c, instream);
436 unblock_input ();
437 return 0;
440 block_input ();
441 c = getc (instream);
443 #ifdef EINTR
444 /* Interrupted reads have been observed while reading over the network. */
445 while (c == EOF && ferror (instream) && errno == EINTR)
447 unblock_input ();
448 QUIT;
449 block_input ();
450 clearerr (instream);
451 c = getc (instream);
453 #endif
455 unblock_input ();
457 return (c == EOF ? -1 : c);
460 static int
461 readbyte_from_string (int c, Lisp_Object readcharfun)
463 Lisp_Object string = XCAR (readcharfun);
465 if (c >= 0)
467 read_from_string_index--;
468 read_from_string_index_byte
469 = string_char_to_byte (string, read_from_string_index);
472 if (read_from_string_index >= read_from_string_limit)
473 c = -1;
474 else
475 FETCH_STRING_CHAR_ADVANCE (c, string,
476 read_from_string_index,
477 read_from_string_index_byte);
478 return c;
482 /* Read one non-ASCII character from INSTREAM. The character is
483 encoded in `emacs-mule' and the first byte is already read in
484 C. */
486 static int
487 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
489 /* Emacs-mule coding uses at most 4-byte for one character. */
490 unsigned char buf[4];
491 int len = emacs_mule_bytes[c];
492 struct charset *charset;
493 int i;
494 unsigned code;
496 if (len == 1)
497 /* C is not a valid leading-code of `emacs-mule'. */
498 return BYTE8_TO_CHAR (c);
500 i = 0;
501 buf[i++] = c;
502 while (i < len)
504 c = (*readbyte) (-1, readcharfun);
505 if (c < 0xA0)
507 while (--i > 1)
508 (*readbyte) (buf[i], readcharfun);
509 return BYTE8_TO_CHAR (buf[0]);
511 buf[i++] = c;
514 if (len == 2)
516 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
517 code = buf[1] & 0x7F;
519 else if (len == 3)
521 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
522 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
524 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
525 code = buf[2] & 0x7F;
527 else
529 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
530 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
533 else
535 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
536 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
538 c = DECODE_CHAR (charset, code);
539 if (c < 0)
540 Fsignal (Qinvalid_read_syntax,
541 Fcons (build_string ("invalid multibyte form"), Qnil));
542 return c;
546 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
547 Lisp_Object);
548 static Lisp_Object read0 (Lisp_Object);
549 static Lisp_Object read1 (Lisp_Object, int *, int);
551 static Lisp_Object read_list (int, Lisp_Object);
552 static Lisp_Object read_vector (Lisp_Object, int);
554 static Lisp_Object substitute_object_recurse (Lisp_Object, Lisp_Object,
555 Lisp_Object);
556 static void substitute_object_in_subtree (Lisp_Object,
557 Lisp_Object);
558 static void substitute_in_interval (INTERVAL, Lisp_Object);
561 /* Get a character from the tty. */
563 /* Read input events until we get one that's acceptable for our purposes.
565 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
566 until we get a character we like, and then stuffed into
567 unread_switch_frame.
569 If ASCII_REQUIRED is non-zero, we check function key events to see
570 if the unmodified version of the symbol has a Qascii_character
571 property, and use that character, if present.
573 If ERROR_NONASCII is non-zero, we signal an error if the input we
574 get isn't an ASCII character with modifiers. If it's zero but
575 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
576 character.
578 If INPUT_METHOD is nonzero, we invoke the current input method
579 if the character warrants that.
581 If SECONDS is a number, we wait that many seconds for input, and
582 return Qnil if no input arrives within that time. */
584 static Lisp_Object
585 read_filtered_event (int no_switch_frame, int ascii_required,
586 int error_nonascii, int input_method, Lisp_Object seconds)
588 Lisp_Object val, delayed_switch_frame;
589 EMACS_TIME end_time;
591 #ifdef HAVE_WINDOW_SYSTEM
592 if (display_hourglass_p)
593 cancel_hourglass ();
594 #endif
596 delayed_switch_frame = Qnil;
598 /* Compute timeout. */
599 if (NUMBERP (seconds))
601 double duration = extract_float (seconds);
602 EMACS_TIME wait_time = EMACS_TIME_FROM_DOUBLE (duration);
603 end_time = add_emacs_time (current_emacs_time (), wait_time);
606 /* Read until we get an acceptable event. */
607 retry:
609 val = read_char (0, 0, 0, (input_method ? Qnil : Qt), 0,
610 NUMBERP (seconds) ? &end_time : NULL);
611 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
613 if (BUFFERP (val))
614 goto retry;
616 /* switch-frame events are put off until after the next ASCII
617 character. This is better than signaling an error just because
618 the last characters were typed to a separate minibuffer frame,
619 for example. Eventually, some code which can deal with
620 switch-frame events will read it and process it. */
621 if (no_switch_frame
622 && EVENT_HAS_PARAMETERS (val)
623 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
625 delayed_switch_frame = val;
626 goto retry;
629 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
631 /* Convert certain symbols to their ASCII equivalents. */
632 if (SYMBOLP (val))
634 Lisp_Object tem, tem1;
635 tem = Fget (val, Qevent_symbol_element_mask);
636 if (!NILP (tem))
638 tem1 = Fget (Fcar (tem), Qascii_character);
639 /* Merge this symbol's modifier bits
640 with the ASCII equivalent of its basic code. */
641 if (!NILP (tem1))
642 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
646 /* If we don't have a character now, deal with it appropriately. */
647 if (!INTEGERP (val))
649 if (error_nonascii)
651 Vunread_command_events = Fcons (val, Qnil);
652 error ("Non-character input-event");
654 else
655 goto retry;
659 if (! NILP (delayed_switch_frame))
660 unread_switch_frame = delayed_switch_frame;
662 #if 0
664 #ifdef HAVE_WINDOW_SYSTEM
665 if (display_hourglass_p)
666 start_hourglass ();
667 #endif
669 #endif
671 return val;
674 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
675 doc: /* Read a character from the command input (keyboard or macro).
676 It is returned as a number.
677 If the character has modifiers, they are resolved and reflected to the
678 character code if possible (e.g. C-SPC -> 0).
680 If the user generates an event which is not a character (i.e. a mouse
681 click or function key event), `read-char' signals an error. As an
682 exception, switch-frame events are put off until non-character events
683 can be read.
684 If you want to read non-character events, or ignore them, call
685 `read-event' or `read-char-exclusive' instead.
687 If the optional argument PROMPT is non-nil, display that as a prompt.
688 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
689 input method is turned on in the current buffer, that input method
690 is used for reading a character.
691 If the optional argument SECONDS is non-nil, it should be a number
692 specifying the maximum number of seconds to wait for input. If no
693 input arrives in that time, return nil. SECONDS may be a
694 floating-point value. */)
695 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
697 Lisp_Object val;
699 if (! NILP (prompt))
700 message_with_string ("%s", prompt, 0);
701 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
703 return (NILP (val) ? Qnil
704 : make_number (char_resolve_modifier_mask (XINT (val))));
707 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
708 doc: /* Read an event object from the input stream.
709 If the optional argument PROMPT is non-nil, display that as a prompt.
710 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
711 input method is turned on in the current buffer, that input method
712 is used for reading a character.
713 If the optional argument SECONDS is non-nil, it should be a number
714 specifying the maximum number of seconds to wait for input. If no
715 input arrives in that time, return nil. SECONDS may be a
716 floating-point value. */)
717 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
719 if (! NILP (prompt))
720 message_with_string ("%s", prompt, 0);
721 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
724 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
725 doc: /* Read a character from the command input (keyboard or macro).
726 It is returned as a number. Non-character events are ignored.
727 If the character has modifiers, they are resolved and reflected to the
728 character code if possible (e.g. C-SPC -> 0).
730 If the optional argument PROMPT is non-nil, display that as a prompt.
731 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
732 input method is turned on in the current buffer, that input method
733 is used for reading a character.
734 If the optional argument SECONDS is non-nil, it should be a number
735 specifying the maximum number of seconds to wait for input. If no
736 input arrives in that time, return nil. SECONDS may be a
737 floating-point value. */)
738 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
740 Lisp_Object val;
742 if (! NILP (prompt))
743 message_with_string ("%s", prompt, 0);
745 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
747 return (NILP (val) ? Qnil
748 : make_number (char_resolve_modifier_mask (XINT (val))));
751 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
752 doc: /* Don't use this yourself. */)
753 (void)
755 register Lisp_Object val;
756 block_input ();
757 XSETINT (val, getc (instream));
758 unblock_input ();
759 return val;
765 /* Return true if the lisp code read using READCHARFUN defines a non-nil
766 `lexical-binding' file variable. After returning, the stream is
767 positioned following the first line, if it is a comment or #! line,
768 otherwise nothing is read. */
770 static int
771 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
773 int ch = READCHAR;
775 if (ch == '#')
777 ch = READCHAR;
778 if (ch != '!')
780 UNREAD (ch);
781 UNREAD ('#');
782 return 0;
784 while (ch != '\n' && ch != EOF)
785 ch = READCHAR;
786 if (ch == '\n') ch = READCHAR;
787 /* It is OK to leave the position after a #! line, since
788 that is what read1 does. */
791 if (ch != ';')
792 /* The first line isn't a comment, just give up. */
794 UNREAD (ch);
795 return 0;
797 else
798 /* Look for an appropriate file-variable in the first line. */
800 int rv = 0;
801 enum {
802 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX,
803 } beg_end_state = NOMINAL;
804 int in_file_vars = 0;
806 #define UPDATE_BEG_END_STATE(ch) \
807 if (beg_end_state == NOMINAL) \
808 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
809 else if (beg_end_state == AFTER_FIRST_DASH) \
810 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
811 else if (beg_end_state == AFTER_ASTERIX) \
813 if (ch == '-') \
814 in_file_vars = !in_file_vars; \
815 beg_end_state = NOMINAL; \
818 /* Skip until we get to the file vars, if any. */
821 ch = READCHAR;
822 UPDATE_BEG_END_STATE (ch);
824 while (!in_file_vars && ch != '\n' && ch != EOF);
826 while (in_file_vars)
828 char var[100], val[100];
829 unsigned i;
831 ch = READCHAR;
833 /* Read a variable name. */
834 while (ch == ' ' || ch == '\t')
835 ch = READCHAR;
837 i = 0;
838 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
840 if (i < sizeof var - 1)
841 var[i++] = ch;
842 UPDATE_BEG_END_STATE (ch);
843 ch = READCHAR;
846 /* Stop scanning if no colon was found before end marker. */
847 if (!in_file_vars || ch == '\n' || ch == EOF)
848 break;
850 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
851 i--;
852 var[i] = '\0';
854 if (ch == ':')
856 /* Read a variable value. */
857 ch = READCHAR;
859 while (ch == ' ' || ch == '\t')
860 ch = READCHAR;
862 i = 0;
863 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
865 if (i < sizeof val - 1)
866 val[i++] = ch;
867 UPDATE_BEG_END_STATE (ch);
868 ch = READCHAR;
870 if (! in_file_vars)
871 /* The value was terminated by an end-marker, which remove. */
872 i -= 3;
873 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
874 i--;
875 val[i] = '\0';
877 if (strcmp (var, "lexical-binding") == 0)
878 /* This is it... */
880 rv = (strcmp (val, "nil") != 0);
881 break;
886 while (ch != '\n' && ch != EOF)
887 ch = READCHAR;
889 return rv;
893 /* Value is a version number of byte compiled code if the file
894 associated with file descriptor FD is a compiled Lisp file that's
895 safe to load. Only files compiled with Emacs are safe to load.
896 Files compiled with XEmacs can lead to a crash in Fbyte_code
897 because of an incompatible change in the byte compiler. */
899 static int
900 safe_to_load_p (int fd)
902 char buf[512];
903 int nbytes, i;
904 int safe_p = 1;
905 int version = 1;
907 /* Read the first few bytes from the file, and look for a line
908 specifying the byte compiler version used. */
909 nbytes = emacs_read (fd, buf, sizeof buf - 1);
910 if (nbytes > 0)
912 buf[nbytes] = '\0';
914 /* Skip to the next newline, skipping over the initial `ELC'
915 with NUL bytes following it, but note the version. */
916 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
917 if (i == 4)
918 version = buf[i];
920 if (i >= nbytes
921 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
922 buf + i, nbytes - i) < 0)
923 safe_p = 0;
925 if (safe_p)
926 safe_p = version;
928 lseek (fd, 0, SEEK_SET);
929 return safe_p;
933 /* Callback for record_unwind_protect. Restore the old load list OLD,
934 after loading a file successfully. */
936 static Lisp_Object
937 record_load_unwind (Lisp_Object old)
939 return Vloads_in_progress = old;
942 /* This handler function is used via internal_condition_case_1. */
944 static Lisp_Object
945 load_error_handler (Lisp_Object data)
947 return Qnil;
950 static Lisp_Object
951 load_warn_old_style_backquotes (Lisp_Object file)
953 if (!NILP (Vold_style_backquotes))
955 Lisp_Object args[2];
956 args[0] = build_string ("Loading `%s': old-style backquotes detected!");
957 args[1] = file;
958 Fmessage (2, args);
960 return Qnil;
963 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
964 doc: /* Return the suffixes that `load' should try if a suffix is \
965 required.
966 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
967 (void)
969 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
970 while (CONSP (suffixes))
972 Lisp_Object exts = Vload_file_rep_suffixes;
973 suffix = XCAR (suffixes);
974 suffixes = XCDR (suffixes);
975 while (CONSP (exts))
977 ext = XCAR (exts);
978 exts = XCDR (exts);
979 lst = Fcons (concat2 (suffix, ext), lst);
982 return Fnreverse (lst);
985 DEFUN ("load", Fload, Sload, 1, 5, 0,
986 doc: /* Execute a file of Lisp code named FILE.
987 First try FILE with `.elc' appended, then try with `.el',
988 then try FILE unmodified (the exact suffixes in the exact order are
989 determined by `load-suffixes'). Environment variable references in
990 FILE are replaced with their values by calling `substitute-in-file-name'.
991 This function searches the directories in `load-path'.
993 If optional second arg NOERROR is non-nil,
994 report no error if FILE doesn't exist.
995 Print messages at start and end of loading unless
996 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
997 overrides that).
998 If optional fourth arg NOSUFFIX is non-nil, don't try adding
999 suffixes `.elc' or `.el' to the specified name FILE.
1000 If optional fifth arg MUST-SUFFIX is non-nil, insist on
1001 the suffix `.elc' or `.el'; don't accept just FILE unless
1002 it ends in one of those suffixes or includes a directory name.
1004 If this function fails to find a file, it may look for different
1005 representations of that file before trying another file.
1006 It does so by adding the non-empty suffixes in `load-file-rep-suffixes'
1007 to the file name. Emacs uses this feature mainly to find compressed
1008 versions of files when Auto Compression mode is enabled.
1010 The exact suffixes that this function tries out, in the exact order,
1011 are given by the value of the variable `load-file-rep-suffixes' if
1012 NOSUFFIX is non-nil and by the return value of the function
1013 `get-load-suffixes' if MUST-SUFFIX is non-nil. If both NOSUFFIX and
1014 MUST-SUFFIX are nil, this function first tries out the latter suffixes
1015 and then the former.
1017 Loading a file records its definitions, and its `provide' and
1018 `require' calls, in an element of `load-history' whose
1019 car is the file name loaded. See `load-history'.
1021 While the file is in the process of being loaded, the variable
1022 `load-in-progress' is non-nil and the variable `load-file-name'
1023 is bound to the file's name.
1025 Return t if the file exists and loads successfully. */)
1026 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage, Lisp_Object nosuffix, Lisp_Object must_suffix)
1028 register FILE *stream;
1029 register int fd = -1;
1030 ptrdiff_t count = SPECPDL_INDEX ();
1031 struct gcpro gcpro1, gcpro2, gcpro3;
1032 Lisp_Object found, efound, hist_file_name;
1033 /* 1 means we printed the ".el is newer" message. */
1034 int newer = 0;
1035 /* 1 means we are loading a compiled file. */
1036 int compiled = 0;
1037 Lisp_Object handler;
1038 int safe_p = 1;
1039 const char *fmode = "r";
1040 Lisp_Object tmp[2];
1041 int version;
1043 #ifdef DOS_NT
1044 fmode = "rt";
1045 #endif /* DOS_NT */
1047 CHECK_STRING (file);
1049 /* If file name is magic, call the handler. */
1050 /* This shouldn't be necessary any more now that `openp' handles it right.
1051 handler = Ffind_file_name_handler (file, Qload);
1052 if (!NILP (handler))
1053 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1055 /* Do this after the handler to avoid
1056 the need to gcpro noerror, nomessage and nosuffix.
1057 (Below here, we care only whether they are nil or not.)
1058 The presence of this call is the result of a historical accident:
1059 it used to be in every file-operation and when it got removed
1060 everywhere, it accidentally stayed here. Since then, enough people
1061 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1062 that it seemed risky to remove. */
1063 if (! NILP (noerror))
1065 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1066 Qt, load_error_handler);
1067 if (NILP (file))
1068 return Qnil;
1070 else
1071 file = Fsubstitute_in_file_name (file);
1074 /* Avoid weird lossage with null string as arg,
1075 since it would try to load a directory as a Lisp file. */
1076 if (SBYTES (file) > 0)
1078 ptrdiff_t size = SBYTES (file);
1080 found = Qnil;
1081 GCPRO2 (file, found);
1083 if (! NILP (must_suffix))
1085 /* Don't insist on adding a suffix if FILE already ends with one. */
1086 if (size > 3
1087 && !strcmp (SSDATA (file) + size - 3, ".el"))
1088 must_suffix = Qnil;
1089 else if (size > 4
1090 && !strcmp (SSDATA (file) + size - 4, ".elc"))
1091 must_suffix = Qnil;
1092 /* Don't insist on adding a suffix
1093 if the argument includes a directory name. */
1094 else if (! NILP (Ffile_name_directory (file)))
1095 must_suffix = Qnil;
1098 fd = openp (Vload_path, file,
1099 (!NILP (nosuffix) ? Qnil
1100 : !NILP (must_suffix) ? Fget_load_suffixes ()
1101 : Fappend (2, (tmp[0] = Fget_load_suffixes (),
1102 tmp[1] = Vload_file_rep_suffixes,
1103 tmp))),
1104 &found, Qnil);
1105 UNGCPRO;
1108 if (fd == -1)
1110 if (NILP (noerror))
1111 xsignal2 (Qfile_error, build_string ("Cannot open load file"), file);
1112 return Qnil;
1115 /* Tell startup.el whether or not we found the user's init file. */
1116 if (EQ (Qt, Vuser_init_file))
1117 Vuser_init_file = found;
1119 /* If FD is -2, that means openp found a magic file. */
1120 if (fd == -2)
1122 if (NILP (Fequal (found, file)))
1123 /* If FOUND is a different file name from FILE,
1124 find its handler even if we have already inhibited
1125 the `load' operation on FILE. */
1126 handler = Ffind_file_name_handler (found, Qt);
1127 else
1128 handler = Ffind_file_name_handler (found, Qload);
1129 if (! NILP (handler))
1130 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1131 #ifdef DOS_NT
1132 /* Tramp has to deal with semi-broken packages that prepend
1133 drive letters to remote files. For that reason, Tramp
1134 catches file operations that test for file existence, which
1135 makes openp think X:/foo.elc files are remote. However,
1136 Tramp does not catch `load' operations for such files, so we
1137 end up with a nil as the `load' handler above. If we would
1138 continue with fd = -2, we will behave wrongly, and in
1139 particular try reading a .elc file in the "rt" mode instead
1140 of "rb". See bug #9311 for the results. To work around
1141 this, we try to open the file locally, and go with that if it
1142 succeeds. */
1143 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1144 if (fd == -1)
1145 fd = -2;
1146 #endif
1149 /* Check if we're stuck in a recursive load cycle.
1151 2000-09-21: It's not possible to just check for the file loaded
1152 being a member of Vloads_in_progress. This fails because of the
1153 way the byte compiler currently works; `provide's are not
1154 evaluated, see font-lock.el/jit-lock.el as an example. This
1155 leads to a certain amount of ``normal'' recursion.
1157 Also, just loading a file recursively is not always an error in
1158 the general case; the second load may do something different. */
1160 int load_count = 0;
1161 Lisp_Object tem;
1162 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1163 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1165 if (fd >= 0)
1166 emacs_close (fd);
1167 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1169 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1170 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1173 /* All loads are by default dynamic, unless the file itself specifies
1174 otherwise using a file-variable in the first line. This is bound here
1175 so that it takes effect whether or not we use
1176 Vload_source_file_function. */
1177 specbind (Qlexical_binding, Qnil);
1179 /* Get the name for load-history. */
1180 hist_file_name = (! NILP (Vpurify_flag)
1181 ? Fconcat (2, (tmp[0] = Ffile_name_directory (file),
1182 tmp[1] = Ffile_name_nondirectory (found),
1183 tmp))
1184 : found) ;
1186 version = -1;
1188 /* Check for the presence of old-style quotes and warn about them. */
1189 specbind (Qold_style_backquotes, Qnil);
1190 record_unwind_protect (load_warn_old_style_backquotes, file);
1192 if (!memcmp (SDATA (found) + SBYTES (found) - 4, ".elc", 4)
1193 || (fd >= 0 && (version = safe_to_load_p (fd)) > 0))
1194 /* Load .elc files directly, but not when they are
1195 remote and have no handler! */
1197 if (fd != -2)
1199 struct stat s1, s2;
1200 int result;
1202 GCPRO3 (file, found, hist_file_name);
1204 if (version < 0
1205 && ! (version = safe_to_load_p (fd)))
1207 safe_p = 0;
1208 if (!load_dangerous_libraries)
1210 if (fd >= 0)
1211 emacs_close (fd);
1212 error ("File `%s' was not compiled in Emacs",
1213 SDATA (found));
1215 else if (!NILP (nomessage) && !force_load_messages)
1216 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1219 compiled = 1;
1221 efound = ENCODE_FILE (found);
1223 #ifdef DOS_NT
1224 fmode = "rb";
1225 #endif /* DOS_NT */
1226 result = stat (SSDATA (efound), &s1);
1227 if (result == 0)
1229 SSET (efound, SBYTES (efound) - 1, 0);
1230 result = stat (SSDATA (efound), &s2);
1231 SSET (efound, SBYTES (efound) - 1, 'c');
1234 if (result == 0
1235 && EMACS_TIME_LT (get_stat_mtime (&s1), get_stat_mtime (&s2)))
1237 /* Make the progress messages mention that source is newer. */
1238 newer = 1;
1240 /* If we won't print another message, mention this anyway. */
1241 if (!NILP (nomessage) && !force_load_messages)
1243 Lisp_Object msg_file;
1244 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1245 message_with_string ("Source file `%s' newer than byte-compiled file",
1246 msg_file, 1);
1249 UNGCPRO;
1252 else
1254 /* We are loading a source file (*.el). */
1255 if (!NILP (Vload_source_file_function))
1257 Lisp_Object val;
1259 if (fd >= 0)
1260 emacs_close (fd);
1261 val = call4 (Vload_source_file_function, found, hist_file_name,
1262 NILP (noerror) ? Qnil : Qt,
1263 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1264 return unbind_to (count, val);
1268 GCPRO3 (file, found, hist_file_name);
1270 #ifdef WINDOWSNT
1271 efound = ENCODE_FILE (found);
1272 /* If we somehow got here with fd == -2, meaning the file is deemed
1273 to be remote, don't even try to reopen the file locally; just
1274 force a failure instead. */
1275 if (fd >= 0)
1277 emacs_close (fd);
1278 stream = fopen (SSDATA (efound), fmode);
1280 else
1281 stream = NULL;
1282 #else /* not WINDOWSNT */
1283 stream = fdopen (fd, fmode);
1284 #endif /* not WINDOWSNT */
1285 if (stream == 0)
1287 emacs_close (fd);
1288 error ("Failure to create stdio stream for %s", SDATA (file));
1291 if (! NILP (Vpurify_flag))
1292 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1294 if (NILP (nomessage) || force_load_messages)
1296 if (!safe_p)
1297 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1298 file, 1);
1299 else if (!compiled)
1300 message_with_string ("Loading %s (source)...", file, 1);
1301 else if (newer)
1302 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1303 file, 1);
1304 else /* The typical case; compiled file newer than source file. */
1305 message_with_string ("Loading %s...", file, 1);
1308 record_unwind_protect (load_unwind, make_save_value (stream, 0));
1309 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
1310 specbind (Qload_file_name, found);
1311 specbind (Qinhibit_file_name_operation, Qnil);
1312 load_descriptor_list
1313 = Fcons (make_number (fileno (stream)), load_descriptor_list);
1314 specbind (Qload_in_progress, Qt);
1316 instream = stream;
1317 if (lisp_file_lexically_bound_p (Qget_file_char))
1318 Fset (Qlexical_binding, Qt);
1320 if (! version || version >= 22)
1321 readevalloop (Qget_file_char, stream, hist_file_name,
1322 0, Qnil, Qnil, Qnil, Qnil);
1323 else
1325 /* We can't handle a file which was compiled with
1326 byte-compile-dynamic by older version of Emacs. */
1327 specbind (Qload_force_doc_strings, Qt);
1328 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name,
1329 0, Qnil, Qnil, Qnil, Qnil);
1331 unbind_to (count, Qnil);
1333 /* Run any eval-after-load forms for this file. */
1334 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1335 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1337 UNGCPRO;
1339 xfree (saved_doc_string);
1340 saved_doc_string = 0;
1341 saved_doc_string_size = 0;
1343 xfree (prev_saved_doc_string);
1344 prev_saved_doc_string = 0;
1345 prev_saved_doc_string_size = 0;
1347 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1349 if (!safe_p)
1350 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1351 file, 1);
1352 else if (!compiled)
1353 message_with_string ("Loading %s (source)...done", file, 1);
1354 else if (newer)
1355 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1356 file, 1);
1357 else /* The typical case; compiled file newer than source file. */
1358 message_with_string ("Loading %s...done", file, 1);
1361 return Qt;
1364 static Lisp_Object
1365 load_unwind (Lisp_Object arg) /* Used as unwind-protect function in load. */
1367 FILE *stream = (FILE *) XSAVE_VALUE (arg)->pointer;
1368 if (stream != NULL)
1370 block_input ();
1371 fclose (stream);
1372 unblock_input ();
1374 return Qnil;
1377 static Lisp_Object
1378 load_descriptor_unwind (Lisp_Object oldlist)
1380 load_descriptor_list = oldlist;
1381 return Qnil;
1384 /* Close all descriptors in use for Floads.
1385 This is used when starting a subprocess. */
1387 void
1388 close_load_descs (void)
1390 #ifndef WINDOWSNT
1391 Lisp_Object tail;
1392 for (tail = load_descriptor_list; CONSP (tail); tail = XCDR (tail))
1393 emacs_close (XFASTINT (XCAR (tail)));
1394 #endif
1397 static int
1398 complete_filename_p (Lisp_Object pathname)
1400 register const unsigned char *s = SDATA (pathname);
1401 return (IS_DIRECTORY_SEP (s[0])
1402 || (SCHARS (pathname) > 2
1403 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1406 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1407 doc: /* Search for FILENAME through PATH.
1408 Returns the file's name in absolute form, or nil if not found.
1409 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1410 file name when searching.
1411 If non-nil, PREDICATE is used instead of `file-readable-p'.
1412 PREDICATE can also be an integer to pass to the access(2) function,
1413 in which case file-name-handlers are ignored.
1414 This function will normally skip directories, so if you want it to find
1415 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1416 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1418 Lisp_Object file;
1419 int fd = openp (path, filename, suffixes, &file, predicate);
1420 if (NILP (predicate) && fd > 0)
1421 close (fd);
1422 return file;
1425 static Lisp_Object Qdir_ok;
1427 /* Search for a file whose name is STR, looking in directories
1428 in the Lisp list PATH, and trying suffixes from SUFFIX.
1429 On success, returns a file descriptor. On failure, returns -1.
1431 SUFFIXES is a list of strings containing possible suffixes.
1432 The empty suffix is automatically added if the list is empty.
1434 PREDICATE non-nil means don't open the files,
1435 just look for one that satisfies the predicate. In this case,
1436 returns 1 on success. The predicate can be a lisp function or
1437 an integer to pass to `access' (in which case file-name-handlers
1438 are ignored).
1440 If STOREPTR is nonzero, it points to a slot where the name of
1441 the file actually found should be stored as a Lisp string.
1442 nil is stored there on failure.
1444 If the file we find is remote, return -2
1445 but store the found remote file name in *STOREPTR. */
1448 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *storeptr, Lisp_Object predicate)
1450 register int fd;
1451 ptrdiff_t fn_size = 100;
1452 char buf[100];
1453 register char *fn = buf;
1454 int absolute = 0;
1455 ptrdiff_t want_length;
1456 Lisp_Object filename;
1457 struct stat st;
1458 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
1459 Lisp_Object string, tail, encoded_fn;
1460 ptrdiff_t max_suffix_len = 0;
1462 CHECK_STRING (str);
1464 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1466 CHECK_STRING_CAR (tail);
1467 max_suffix_len = max (max_suffix_len,
1468 SBYTES (XCAR (tail)));
1471 string = filename = encoded_fn = Qnil;
1472 GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
1474 if (storeptr)
1475 *storeptr = Qnil;
1477 if (complete_filename_p (str))
1478 absolute = 1;
1480 for (; CONSP (path); path = XCDR (path))
1482 filename = Fexpand_file_name (str, XCAR (path));
1483 if (!complete_filename_p (filename))
1484 /* If there are non-absolute elts in PATH (eg "."). */
1485 /* Of course, this could conceivably lose if luser sets
1486 default-directory to be something non-absolute... */
1488 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1489 if (!complete_filename_p (filename))
1490 /* Give up on this path element! */
1491 continue;
1494 /* Calculate maximum length of any filename made from
1495 this path element/specified file name and any possible suffix. */
1496 want_length = max_suffix_len + SBYTES (filename);
1497 if (fn_size <= want_length)
1498 fn = alloca (fn_size = 100 + want_length);
1500 /* Loop over suffixes. */
1501 for (tail = NILP (suffixes) ? Fcons (empty_unibyte_string, Qnil) : suffixes;
1502 CONSP (tail); tail = XCDR (tail))
1504 ptrdiff_t fnlen, lsuffix = SBYTES (XCAR (tail));
1505 Lisp_Object handler;
1506 int exists;
1508 /* Concatenate path element/specified name with the suffix.
1509 If the directory starts with /:, remove that. */
1510 int prefixlen = ((SCHARS (filename) > 2
1511 && SREF (filename, 0) == '/'
1512 && SREF (filename, 1) == ':')
1513 ? 2 : 0);
1514 fnlen = SBYTES (filename) - prefixlen;
1515 memcpy (fn, SDATA (filename) + prefixlen, fnlen);
1516 memcpy (fn + fnlen, SDATA (XCAR (tail)), lsuffix + 1);
1517 fnlen += lsuffix;
1518 /* Check that the file exists and is not a directory. */
1519 /* We used to only check for handlers on non-absolute file names:
1520 if (absolute)
1521 handler = Qnil;
1522 else
1523 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1524 It's not clear why that was the case and it breaks things like
1525 (load "/bar.el") where the file is actually "/bar.el.gz". */
1526 string = make_string (fn, fnlen);
1527 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1528 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1530 if (NILP (predicate))
1531 exists = !NILP (Ffile_readable_p (string));
1532 else
1534 Lisp_Object tmp = call1 (predicate, string);
1535 exists = !NILP (tmp)
1536 && (EQ (tmp, Qdir_ok)
1537 || NILP (Ffile_directory_p (string)));
1540 if (exists)
1542 /* We succeeded; return this descriptor and filename. */
1543 if (storeptr)
1544 *storeptr = string;
1545 UNGCPRO;
1546 return -2;
1549 else
1551 const char *pfn;
1553 encoded_fn = ENCODE_FILE (string);
1554 pfn = SSDATA (encoded_fn);
1555 exists = (stat (pfn, &st) == 0 && ! S_ISDIR (st.st_mode));
1556 if (exists)
1558 /* Check that we can access or open it. */
1559 if (NATNUMP (predicate))
1560 fd = (((XFASTINT (predicate) & ~INT_MAX) == 0
1561 && access (pfn, XFASTINT (predicate)) == 0)
1562 ? 1 : -1);
1563 else
1564 fd = emacs_open (pfn, O_RDONLY, 0);
1566 if (fd >= 0)
1568 /* We succeeded; return this descriptor and filename. */
1569 if (storeptr)
1570 *storeptr = string;
1571 UNGCPRO;
1572 return fd;
1577 if (absolute)
1578 break;
1581 UNGCPRO;
1582 return -1;
1586 /* Merge the list we've accumulated of globals from the current input source
1587 into the load_history variable. The details depend on whether
1588 the source has an associated file name or not.
1590 FILENAME is the file name that we are loading from.
1591 ENTIRE is 1 if loading that entire file, 0 if evaluating part of it. */
1593 static void
1594 build_load_history (Lisp_Object filename, int entire)
1596 register Lisp_Object tail, prev, newelt;
1597 register Lisp_Object tem, tem2;
1598 register int foundit = 0;
1600 tail = Vload_history;
1601 prev = Qnil;
1603 while (CONSP (tail))
1605 tem = XCAR (tail);
1607 /* Find the feature's previous assoc list... */
1608 if (!NILP (Fequal (filename, Fcar (tem))))
1610 foundit = 1;
1612 /* If we're loading the entire file, remove old data. */
1613 if (entire)
1615 if (NILP (prev))
1616 Vload_history = XCDR (tail);
1617 else
1618 Fsetcdr (prev, XCDR (tail));
1621 /* Otherwise, cons on new symbols that are not already members. */
1622 else
1624 tem2 = Vcurrent_load_list;
1626 while (CONSP (tem2))
1628 newelt = XCAR (tem2);
1630 if (NILP (Fmember (newelt, tem)))
1631 Fsetcar (tail, Fcons (XCAR (tem),
1632 Fcons (newelt, XCDR (tem))));
1634 tem2 = XCDR (tem2);
1635 QUIT;
1639 else
1640 prev = tail;
1641 tail = XCDR (tail);
1642 QUIT;
1645 /* If we're loading an entire file, cons the new assoc onto the
1646 front of load-history, the most-recently-loaded position. Also
1647 do this if we didn't find an existing member for the file. */
1648 if (entire || !foundit)
1649 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1650 Vload_history);
1653 static Lisp_Object
1654 readevalloop_1 (Lisp_Object old)
1656 load_convert_to_unibyte = ! NILP (old);
1657 return Qnil;
1660 /* Signal an `end-of-file' error, if possible with file name
1661 information. */
1663 static _Noreturn void
1664 end_of_file_error (void)
1666 if (STRINGP (Vload_file_name))
1667 xsignal1 (Qend_of_file, Vload_file_name);
1669 xsignal0 (Qend_of_file);
1672 /* UNIBYTE specifies how to set load_convert_to_unibyte
1673 for this invocation.
1674 READFUN, if non-nil, is used instead of `read'.
1676 START, END specify region to read in current buffer (from eval-region).
1677 If the input is not from a buffer, they must be nil. */
1679 static void
1680 readevalloop (Lisp_Object readcharfun,
1681 FILE *stream,
1682 Lisp_Object sourcename,
1683 int printflag,
1684 Lisp_Object unibyte, Lisp_Object readfun,
1685 Lisp_Object start, Lisp_Object end)
1687 register int c;
1688 register Lisp_Object val;
1689 ptrdiff_t count = SPECPDL_INDEX ();
1690 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1691 struct buffer *b = 0;
1692 int continue_reading_p;
1693 Lisp_Object lex_bound;
1694 /* Nonzero if reading an entire buffer. */
1695 int whole_buffer = 0;
1696 /* 1 on the first time around. */
1697 int first_sexp = 1;
1698 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1700 if (NILP (Ffboundp (macroexpand))
1701 /* Don't macroexpand in .elc files, since it should have been done
1702 already. We actually don't know whether we're in a .elc file or not,
1703 so we use circumstantial evidence: .el files normally go through
1704 Vload_source_file_function -> load-with-code-conversion
1705 -> eval-buffer. */
1706 || EQ (readcharfun, Qget_file_char)
1707 || EQ (readcharfun, Qget_emacs_mule_file_char))
1708 macroexpand = Qnil;
1710 if (MARKERP (readcharfun))
1712 if (NILP (start))
1713 start = readcharfun;
1716 if (BUFFERP (readcharfun))
1717 b = XBUFFER (readcharfun);
1718 else if (MARKERP (readcharfun))
1719 b = XMARKER (readcharfun)->buffer;
1721 /* We assume START is nil when input is not from a buffer. */
1722 if (! NILP (start) && !b)
1723 emacs_abort ();
1725 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
1726 specbind (Qcurrent_load_list, Qnil);
1727 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1728 load_convert_to_unibyte = !NILP (unibyte);
1730 /* If lexical binding is active (either because it was specified in
1731 the file's header, or via a buffer-local variable), create an empty
1732 lexical environment, otherwise, turn off lexical binding. */
1733 lex_bound = find_symbol_value (Qlexical_binding);
1734 specbind (Qinternal_interpreter_environment,
1735 NILP (lex_bound) || EQ (lex_bound, Qunbound)
1736 ? Qnil : Fcons (Qt, Qnil));
1738 GCPRO4 (sourcename, readfun, start, end);
1740 /* Try to ensure sourcename is a truename, except whilst preloading. */
1741 if (NILP (Vpurify_flag)
1742 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1743 && !NILP (Ffboundp (Qfile_truename)))
1744 sourcename = call1 (Qfile_truename, sourcename) ;
1746 LOADHIST_ATTACH (sourcename);
1748 continue_reading_p = 1;
1749 while (continue_reading_p)
1751 ptrdiff_t count1 = SPECPDL_INDEX ();
1753 if (b != 0 && !BUFFER_LIVE_P (b))
1754 error ("Reading from killed buffer");
1756 if (!NILP (start))
1758 /* Switch to the buffer we are reading from. */
1759 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1760 set_buffer_internal (b);
1762 /* Save point in it. */
1763 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1764 /* Save ZV in it. */
1765 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1766 /* Those get unbound after we read one expression. */
1768 /* Set point and ZV around stuff to be read. */
1769 Fgoto_char (start);
1770 if (!NILP (end))
1771 Fnarrow_to_region (make_number (BEGV), end);
1773 /* Just for cleanliness, convert END to a marker
1774 if it is an integer. */
1775 if (INTEGERP (end))
1776 end = Fpoint_max_marker ();
1779 /* On the first cycle, we can easily test here
1780 whether we are reading the whole buffer. */
1781 if (b && first_sexp)
1782 whole_buffer = (PT == BEG && ZV == Z);
1784 instream = stream;
1785 read_next:
1786 c = READCHAR;
1787 if (c == ';')
1789 while ((c = READCHAR) != '\n' && c != -1);
1790 goto read_next;
1792 if (c < 0)
1794 unbind_to (count1, Qnil);
1795 break;
1798 /* Ignore whitespace here, so we can detect eof. */
1799 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1800 || c == 0xa0) /* NBSP */
1801 goto read_next;
1803 if (!NILP (Vpurify_flag) && c == '(')
1805 val = read_list (0, readcharfun);
1807 else
1809 UNREAD (c);
1810 read_objects = Qnil;
1811 if (!NILP (readfun))
1813 val = call1 (readfun, readcharfun);
1815 /* If READCHARFUN has set point to ZV, we should
1816 stop reading, even if the form read sets point
1817 to a different value when evaluated. */
1818 if (BUFFERP (readcharfun))
1820 struct buffer *buf = XBUFFER (readcharfun);
1821 if (BUF_PT (buf) == BUF_ZV (buf))
1822 continue_reading_p = 0;
1825 else if (! NILP (Vload_read_function))
1826 val = call1 (Vload_read_function, readcharfun);
1827 else
1828 val = read_internal_start (readcharfun, Qnil, Qnil);
1831 if (!NILP (start) && continue_reading_p)
1832 start = Fpoint_marker ();
1834 /* Restore saved point and BEGV. */
1835 unbind_to (count1, Qnil);
1837 /* Now eval what we just read. */
1838 if (!NILP (macroexpand))
1839 val = call1 (macroexpand, val);
1840 val = eval_sub (val);
1842 if (printflag)
1844 Vvalues = Fcons (val, Vvalues);
1845 if (EQ (Vstandard_output, Qt))
1846 Fprin1 (val, Qnil);
1847 else
1848 Fprint (val, Qnil);
1851 first_sexp = 0;
1854 build_load_history (sourcename,
1855 stream || whole_buffer);
1857 UNGCPRO;
1859 unbind_to (count, Qnil);
1862 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1863 doc: /* Execute the current buffer as Lisp code.
1864 When called from a Lisp program (i.e., not interactively), this
1865 function accepts up to five optional arguments:
1866 BUFFER is the buffer to evaluate (nil means use current buffer).
1867 PRINTFLAG controls printing of output:
1868 A value of nil means discard it; anything else is stream for print.
1869 FILENAME specifies the file name to use for `load-history'.
1870 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
1871 invocation.
1872 DO-ALLOW-PRINT, if non-nil, specifies that `print' and related
1873 functions should work normally even if PRINTFLAG is nil.
1875 This function preserves the position of point. */)
1876 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
1878 ptrdiff_t count = SPECPDL_INDEX ();
1879 Lisp_Object tem, buf;
1881 if (NILP (buffer))
1882 buf = Fcurrent_buffer ();
1883 else
1884 buf = Fget_buffer (buffer);
1885 if (NILP (buf))
1886 error ("No such buffer");
1888 if (NILP (printflag) && NILP (do_allow_print))
1889 tem = Qsymbolp;
1890 else
1891 tem = printflag;
1893 if (NILP (filename))
1894 filename = BVAR (XBUFFER (buf), filename);
1896 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1897 specbind (Qstandard_output, tem);
1898 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1899 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1900 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
1901 readevalloop (buf, 0, filename,
1902 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
1903 unbind_to (count, Qnil);
1905 return Qnil;
1908 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1909 doc: /* Execute the region as Lisp code.
1910 When called from programs, expects two arguments,
1911 giving starting and ending indices in the current buffer
1912 of the text to be executed.
1913 Programs can pass third argument PRINTFLAG which controls output:
1914 A value of nil means discard it; anything else is stream for printing it.
1915 Also the fourth argument READ-FUNCTION, if non-nil, is used
1916 instead of `read' to read each expression. It gets one argument
1917 which is the input stream for reading characters.
1919 This function does not move point. */)
1920 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
1922 /* FIXME: Do the eval-sexp-add-defvars dance! */
1923 ptrdiff_t count = SPECPDL_INDEX ();
1924 Lisp_Object tem, cbuf;
1926 cbuf = Fcurrent_buffer ();
1928 if (NILP (printflag))
1929 tem = Qsymbolp;
1930 else
1931 tem = printflag;
1932 specbind (Qstandard_output, tem);
1933 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
1935 /* `readevalloop' calls functions which check the type of start and end. */
1936 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
1937 !NILP (printflag), Qnil, read_function,
1938 start, end);
1940 return unbind_to (count, Qnil);
1944 DEFUN ("read", Fread, Sread, 0, 1, 0,
1945 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1946 If STREAM is nil, use the value of `standard-input' (which see).
1947 STREAM or the value of `standard-input' may be:
1948 a buffer (read from point and advance it)
1949 a marker (read from where it points and advance it)
1950 a function (call it with no arguments for each character,
1951 call it with a char as argument to push a char back)
1952 a string (takes text from string, starting at the beginning)
1953 t (read text line using minibuffer and use it, or read from
1954 standard input in batch mode). */)
1955 (Lisp_Object stream)
1957 if (NILP (stream))
1958 stream = Vstandard_input;
1959 if (EQ (stream, Qt))
1960 stream = Qread_char;
1961 if (EQ (stream, Qread_char))
1962 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1964 return read_internal_start (stream, Qnil, Qnil);
1967 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1968 doc: /* Read one Lisp expression which is represented as text by STRING.
1969 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
1970 FINAL-STRING-INDEX is an integer giving the position of the next
1971 remaining character in STRING.
1972 START and END optionally delimit a substring of STRING from which to read;
1973 they default to 0 and (length STRING) respectively. */)
1974 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
1976 Lisp_Object ret;
1977 CHECK_STRING (string);
1978 /* `read_internal_start' sets `read_from_string_index'. */
1979 ret = read_internal_start (string, start, end);
1980 return Fcons (ret, make_number (read_from_string_index));
1983 /* Function to set up the global context we need in toplevel read
1984 calls. */
1985 static Lisp_Object
1986 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
1987 /* `start', `end' only used when stream is a string. */
1989 Lisp_Object retval;
1991 readchar_count = 0;
1992 new_backquote_flag = 0;
1993 read_objects = Qnil;
1994 if (EQ (Vread_with_symbol_positions, Qt)
1995 || EQ (Vread_with_symbol_positions, stream))
1996 Vread_symbol_positions_list = Qnil;
1998 if (STRINGP (stream)
1999 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2001 ptrdiff_t startval, endval;
2002 Lisp_Object string;
2004 if (STRINGP (stream))
2005 string = stream;
2006 else
2007 string = XCAR (stream);
2009 if (NILP (end))
2010 endval = SCHARS (string);
2011 else
2013 CHECK_NUMBER (end);
2014 if (! (0 <= XINT (end) && XINT (end) <= SCHARS (string)))
2015 args_out_of_range (string, end);
2016 endval = XINT (end);
2019 if (NILP (start))
2020 startval = 0;
2021 else
2023 CHECK_NUMBER (start);
2024 if (! (0 <= XINT (start) && XINT (start) <= endval))
2025 args_out_of_range (string, start);
2026 startval = XINT (start);
2028 read_from_string_index = startval;
2029 read_from_string_index_byte = string_char_to_byte (string, startval);
2030 read_from_string_limit = endval;
2033 retval = read0 (stream);
2034 if (EQ (Vread_with_symbol_positions, Qt)
2035 || EQ (Vread_with_symbol_positions, stream))
2036 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2037 return retval;
2041 /* Signal Qinvalid_read_syntax error.
2042 S is error string of length N (if > 0) */
2044 static _Noreturn void
2045 invalid_syntax (const char *s)
2047 xsignal1 (Qinvalid_read_syntax, build_string (s));
2051 /* Use this for recursive reads, in contexts where internal tokens
2052 are not allowed. */
2054 static Lisp_Object
2055 read0 (Lisp_Object readcharfun)
2057 register Lisp_Object val;
2058 int c;
2060 val = read1 (readcharfun, &c, 0);
2061 if (!c)
2062 return val;
2064 xsignal1 (Qinvalid_read_syntax,
2065 Fmake_string (make_number (1), make_number (c)));
2068 static ptrdiff_t read_buffer_size;
2069 static char *read_buffer;
2071 /* Read a \-escape sequence, assuming we already read the `\'.
2072 If the escape sequence forces unibyte, return eight-bit char. */
2074 static int
2075 read_escape (Lisp_Object readcharfun, int stringp)
2077 register int c = READCHAR;
2078 /* \u allows up to four hex digits, \U up to eight. Default to the
2079 behavior for \u, and change this value in the case that \U is seen. */
2080 int unicode_hex_count = 4;
2082 switch (c)
2084 case -1:
2085 end_of_file_error ();
2087 case 'a':
2088 return '\007';
2089 case 'b':
2090 return '\b';
2091 case 'd':
2092 return 0177;
2093 case 'e':
2094 return 033;
2095 case 'f':
2096 return '\f';
2097 case 'n':
2098 return '\n';
2099 case 'r':
2100 return '\r';
2101 case 't':
2102 return '\t';
2103 case 'v':
2104 return '\v';
2105 case '\n':
2106 return -1;
2107 case ' ':
2108 if (stringp)
2109 return -1;
2110 return ' ';
2112 case 'M':
2113 c = READCHAR;
2114 if (c != '-')
2115 error ("Invalid escape character syntax");
2116 c = READCHAR;
2117 if (c == '\\')
2118 c = read_escape (readcharfun, 0);
2119 return c | meta_modifier;
2121 case 'S':
2122 c = READCHAR;
2123 if (c != '-')
2124 error ("Invalid escape character syntax");
2125 c = READCHAR;
2126 if (c == '\\')
2127 c = read_escape (readcharfun, 0);
2128 return c | shift_modifier;
2130 case 'H':
2131 c = READCHAR;
2132 if (c != '-')
2133 error ("Invalid escape character syntax");
2134 c = READCHAR;
2135 if (c == '\\')
2136 c = read_escape (readcharfun, 0);
2137 return c | hyper_modifier;
2139 case 'A':
2140 c = READCHAR;
2141 if (c != '-')
2142 error ("Invalid escape character syntax");
2143 c = READCHAR;
2144 if (c == '\\')
2145 c = read_escape (readcharfun, 0);
2146 return c | alt_modifier;
2148 case 's':
2149 c = READCHAR;
2150 if (stringp || c != '-')
2152 UNREAD (c);
2153 return ' ';
2155 c = READCHAR;
2156 if (c == '\\')
2157 c = read_escape (readcharfun, 0);
2158 return c | super_modifier;
2160 case 'C':
2161 c = READCHAR;
2162 if (c != '-')
2163 error ("Invalid escape character syntax");
2164 case '^':
2165 c = READCHAR;
2166 if (c == '\\')
2167 c = read_escape (readcharfun, 0);
2168 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2169 return 0177 | (c & CHAR_MODIFIER_MASK);
2170 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2171 return c | ctrl_modifier;
2172 /* ASCII control chars are made from letters (both cases),
2173 as well as the non-letters within 0100...0137. */
2174 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2175 return (c & (037 | ~0177));
2176 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2177 return (c & (037 | ~0177));
2178 else
2179 return c | ctrl_modifier;
2181 case '0':
2182 case '1':
2183 case '2':
2184 case '3':
2185 case '4':
2186 case '5':
2187 case '6':
2188 case '7':
2189 /* An octal escape, as in ANSI C. */
2191 register int i = c - '0';
2192 register int count = 0;
2193 while (++count < 3)
2195 if ((c = READCHAR) >= '0' && c <= '7')
2197 i *= 8;
2198 i += c - '0';
2200 else
2202 UNREAD (c);
2203 break;
2207 if (i >= 0x80 && i < 0x100)
2208 i = BYTE8_TO_CHAR (i);
2209 return i;
2212 case 'x':
2213 /* A hex escape, as in ANSI C. */
2215 unsigned int i = 0;
2216 int count = 0;
2217 while (1)
2219 c = READCHAR;
2220 if (c >= '0' && c <= '9')
2222 i *= 16;
2223 i += c - '0';
2225 else if ((c >= 'a' && c <= 'f')
2226 || (c >= 'A' && c <= 'F'))
2228 i *= 16;
2229 if (c >= 'a' && c <= 'f')
2230 i += c - 'a' + 10;
2231 else
2232 i += c - 'A' + 10;
2234 else
2236 UNREAD (c);
2237 break;
2239 /* Allow hex escapes as large as ?\xfffffff, because some
2240 packages use them to denote characters with modifiers. */
2241 if ((CHAR_META | (CHAR_META - 1)) < i)
2242 error ("Hex character out of range: \\x%x...", i);
2243 count += count < 3;
2246 if (count < 3 && i >= 0x80)
2247 return BYTE8_TO_CHAR (i);
2248 return i;
2251 case 'U':
2252 /* Post-Unicode-2.0: Up to eight hex chars. */
2253 unicode_hex_count = 8;
2254 case 'u':
2256 /* A Unicode escape. We only permit them in strings and characters,
2257 not arbitrarily in the source code, as in some other languages. */
2259 unsigned int i = 0;
2260 int count = 0;
2262 while (++count <= unicode_hex_count)
2264 c = READCHAR;
2265 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2266 want. */
2267 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2268 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2269 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2270 else
2271 error ("Non-hex digit used for Unicode escape");
2273 if (i > 0x10FFFF)
2274 error ("Non-Unicode character: 0x%x", i);
2275 return i;
2278 default:
2279 return c;
2283 /* Return the digit that CHARACTER stands for in the given BASE.
2284 Return -1 if CHARACTER is out of range for BASE,
2285 and -2 if CHARACTER is not valid for any supported BASE. */
2286 static int
2287 digit_to_number (int character, int base)
2289 int digit;
2291 if ('0' <= character && character <= '9')
2292 digit = character - '0';
2293 else if ('a' <= character && character <= 'z')
2294 digit = character - 'a' + 10;
2295 else if ('A' <= character && character <= 'Z')
2296 digit = character - 'A' + 10;
2297 else
2298 return -2;
2300 return digit < base ? digit : -1;
2303 /* Read an integer in radix RADIX using READCHARFUN to read
2304 characters. RADIX must be in the interval [2..36]; if it isn't, a
2305 read error is signaled . Value is the integer read. Signals an
2306 error if encountering invalid read syntax or if RADIX is out of
2307 range. */
2309 static Lisp_Object
2310 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2312 /* Room for sign, leading 0, other digits, trailing null byte.
2313 Also, room for invalid syntax diagnostic. */
2314 char buf[max (1 + 1 + sizeof (uintmax_t) * CHAR_BIT + 1,
2315 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2317 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2319 if (radix < 2 || radix > 36)
2320 valid = 0;
2321 else
2323 char *p = buf;
2324 int c, digit;
2326 c = READCHAR;
2327 if (c == '-' || c == '+')
2329 *p++ = c;
2330 c = READCHAR;
2333 if (c == '0')
2335 *p++ = c;
2336 valid = 1;
2338 /* Ignore redundant leading zeros, so the buffer doesn't
2339 fill up with them. */
2341 c = READCHAR;
2342 while (c == '0');
2345 while (-1 <= (digit = digit_to_number (c, radix)))
2347 if (digit == -1)
2348 valid = 0;
2349 if (valid < 0)
2350 valid = 1;
2352 if (p < buf + sizeof buf - 1)
2353 *p++ = c;
2354 else
2355 valid = 0;
2357 c = READCHAR;
2360 UNREAD (c);
2361 *p = '\0';
2364 if (! valid)
2366 sprintf (buf, "integer, radix %"pI"d", radix);
2367 invalid_syntax (buf);
2370 return string_to_number (buf, radix, 0);
2374 /* If the next token is ')' or ']' or '.', we store that character
2375 in *PCH and the return value is not interesting. Else, we store
2376 zero in *PCH and we read and return one lisp object.
2378 FIRST_IN_LIST is nonzero if this is the first element of a list. */
2380 static Lisp_Object
2381 read1 (register Lisp_Object readcharfun, int *pch, int first_in_list)
2383 int c;
2384 bool uninterned_symbol = 0;
2385 bool multibyte;
2387 *pch = 0;
2388 load_each_byte = 0;
2390 retry:
2392 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2393 if (c < 0)
2394 end_of_file_error ();
2396 switch (c)
2398 case '(':
2399 return read_list (0, readcharfun);
2401 case '[':
2402 return read_vector (readcharfun, 0);
2404 case ')':
2405 case ']':
2407 *pch = c;
2408 return Qnil;
2411 case '#':
2412 c = READCHAR;
2413 if (c == 's')
2415 c = READCHAR;
2416 if (c == '(')
2418 /* Accept extended format for hashtables (extensible to
2419 other types), e.g.
2420 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2421 Lisp_Object tmp = read_list (0, readcharfun);
2422 Lisp_Object head = CAR_SAFE (tmp);
2423 Lisp_Object data = Qnil;
2424 Lisp_Object val = Qnil;
2425 /* The size is 2 * number of allowed keywords to
2426 make-hash-table. */
2427 Lisp_Object params[10];
2428 Lisp_Object ht;
2429 Lisp_Object key = Qnil;
2430 int param_count = 0;
2432 if (!EQ (head, Qhash_table))
2433 error ("Invalid extended read marker at head of #s list "
2434 "(only hash-table allowed)");
2436 tmp = CDR_SAFE (tmp);
2438 /* This is repetitive but fast and simple. */
2439 params[param_count] = QCsize;
2440 params[param_count + 1] = Fplist_get (tmp, Qsize);
2441 if (!NILP (params[param_count + 1]))
2442 param_count += 2;
2444 params[param_count] = QCtest;
2445 params[param_count + 1] = Fplist_get (tmp, Qtest);
2446 if (!NILP (params[param_count + 1]))
2447 param_count += 2;
2449 params[param_count] = QCweakness;
2450 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2451 if (!NILP (params[param_count + 1]))
2452 param_count += 2;
2454 params[param_count] = QCrehash_size;
2455 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2456 if (!NILP (params[param_count + 1]))
2457 param_count += 2;
2459 params[param_count] = QCrehash_threshold;
2460 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2461 if (!NILP (params[param_count + 1]))
2462 param_count += 2;
2464 /* This is the hashtable data. */
2465 data = Fplist_get (tmp, Qdata);
2467 /* Now use params to make a new hashtable and fill it. */
2468 ht = Fmake_hash_table (param_count, params);
2470 while (CONSP (data))
2472 key = XCAR (data);
2473 data = XCDR (data);
2474 if (!CONSP (data))
2475 error ("Odd number of elements in hashtable data");
2476 val = XCAR (data);
2477 data = XCDR (data);
2478 Fputhash (key, val, ht);
2481 return ht;
2483 UNREAD (c);
2484 invalid_syntax ("#");
2486 if (c == '^')
2488 c = READCHAR;
2489 if (c == '[')
2491 Lisp_Object tmp;
2492 tmp = read_vector (readcharfun, 0);
2493 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2494 error ("Invalid size char-table");
2495 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2496 return tmp;
2498 else if (c == '^')
2500 c = READCHAR;
2501 if (c == '[')
2503 Lisp_Object tmp;
2504 int depth;
2505 ptrdiff_t size;
2507 tmp = read_vector (readcharfun, 0);
2508 size = ASIZE (tmp);
2509 if (size == 0)
2510 error ("Invalid size char-table");
2511 if (! RANGED_INTEGERP (1, AREF (tmp, 0), 3))
2512 error ("Invalid depth in char-table");
2513 depth = XINT (AREF (tmp, 0));
2514 if (chartab_size[depth] != size - 2)
2515 error ("Invalid size char-table");
2516 XSETPVECTYPE (XVECTOR (tmp), PVEC_SUB_CHAR_TABLE);
2517 return tmp;
2519 invalid_syntax ("#^^");
2521 invalid_syntax ("#^");
2523 if (c == '&')
2525 Lisp_Object length;
2526 length = read1 (readcharfun, pch, first_in_list);
2527 c = READCHAR;
2528 if (c == '"')
2530 Lisp_Object tmp, val;
2531 EMACS_INT size_in_chars
2532 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2533 / BOOL_VECTOR_BITS_PER_CHAR);
2535 UNREAD (c);
2536 tmp = read1 (readcharfun, pch, first_in_list);
2537 if (STRING_MULTIBYTE (tmp)
2538 || (size_in_chars != SCHARS (tmp)
2539 /* We used to print 1 char too many
2540 when the number of bits was a multiple of 8.
2541 Accept such input in case it came from an old
2542 version. */
2543 && ! (XFASTINT (length)
2544 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2545 invalid_syntax ("#&...");
2547 val = Fmake_bool_vector (length, Qnil);
2548 memcpy (XBOOL_VECTOR (val)->data, SDATA (tmp), size_in_chars);
2549 /* Clear the extraneous bits in the last byte. */
2550 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2551 XBOOL_VECTOR (val)->data[size_in_chars - 1]
2552 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2553 return val;
2555 invalid_syntax ("#&...");
2557 if (c == '[')
2559 /* Accept compiled functions at read-time so that we don't have to
2560 build them using function calls. */
2561 Lisp_Object tmp;
2562 tmp = read_vector (readcharfun, 1);
2563 make_byte_code (XVECTOR (tmp));
2564 return tmp;
2566 if (c == '(')
2568 Lisp_Object tmp;
2569 struct gcpro gcpro1;
2570 int ch;
2572 /* Read the string itself. */
2573 tmp = read1 (readcharfun, &ch, 0);
2574 if (ch != 0 || !STRINGP (tmp))
2575 invalid_syntax ("#");
2576 GCPRO1 (tmp);
2577 /* Read the intervals and their properties. */
2578 while (1)
2580 Lisp_Object beg, end, plist;
2582 beg = read1 (readcharfun, &ch, 0);
2583 end = plist = Qnil;
2584 if (ch == ')')
2585 break;
2586 if (ch == 0)
2587 end = read1 (readcharfun, &ch, 0);
2588 if (ch == 0)
2589 plist = read1 (readcharfun, &ch, 0);
2590 if (ch)
2591 invalid_syntax ("Invalid string property list");
2592 Fset_text_properties (beg, end, plist, tmp);
2594 UNGCPRO;
2595 return tmp;
2598 /* #@NUMBER is used to skip NUMBER following characters.
2599 That's used in .elc files to skip over doc strings
2600 and function definitions. */
2601 if (c == '@')
2603 enum { extra = 100 };
2604 ptrdiff_t i, nskip = 0;
2606 load_each_byte = 1;
2607 /* Read a decimal integer. */
2608 while ((c = READCHAR) >= 0
2609 && c >= '0' && c <= '9')
2611 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2612 string_overflow ();
2613 nskip *= 10;
2614 nskip += c - '0';
2616 UNREAD (c);
2618 if (load_force_doc_strings
2619 && (EQ (readcharfun, Qget_file_char)
2620 || EQ (readcharfun, Qget_emacs_mule_file_char)))
2622 /* If we are supposed to force doc strings into core right now,
2623 record the last string that we skipped,
2624 and record where in the file it comes from. */
2626 /* But first exchange saved_doc_string
2627 with prev_saved_doc_string, so we save two strings. */
2629 char *temp = saved_doc_string;
2630 ptrdiff_t temp_size = saved_doc_string_size;
2631 file_offset temp_pos = saved_doc_string_position;
2632 ptrdiff_t temp_len = saved_doc_string_length;
2634 saved_doc_string = prev_saved_doc_string;
2635 saved_doc_string_size = prev_saved_doc_string_size;
2636 saved_doc_string_position = prev_saved_doc_string_position;
2637 saved_doc_string_length = prev_saved_doc_string_length;
2639 prev_saved_doc_string = temp;
2640 prev_saved_doc_string_size = temp_size;
2641 prev_saved_doc_string_position = temp_pos;
2642 prev_saved_doc_string_length = temp_len;
2645 if (saved_doc_string_size == 0)
2647 saved_doc_string = xmalloc (nskip + extra);
2648 saved_doc_string_size = nskip + extra;
2650 if (nskip > saved_doc_string_size)
2652 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
2653 saved_doc_string_size = nskip + extra;
2656 saved_doc_string_position = file_tell (instream);
2658 /* Copy that many characters into saved_doc_string. */
2659 for (i = 0; i < nskip && c >= 0; i++)
2660 saved_doc_string[i] = c = READCHAR;
2662 saved_doc_string_length = i;
2664 else
2666 /* Skip that many characters. */
2667 for (i = 0; i < nskip && c >= 0; i++)
2668 c = READCHAR;
2671 load_each_byte = 0;
2672 goto retry;
2674 if (c == '!')
2676 /* #! appears at the beginning of an executable file.
2677 Skip the first line. */
2678 while (c != '\n' && c >= 0)
2679 c = READCHAR;
2680 goto retry;
2682 if (c == '$')
2683 return Vload_file_name;
2684 if (c == '\'')
2685 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2686 /* #:foo is the uninterned symbol named foo. */
2687 if (c == ':')
2689 uninterned_symbol = 1;
2690 c = READCHAR;
2691 if (!(c > 040
2692 && c != 0xa0 /* NBSP */
2693 && (c >= 0200
2694 || strchr ("\"';()[]#`,", c) == NULL)))
2696 /* No symbol character follows, this is the empty
2697 symbol. */
2698 UNREAD (c);
2699 return Fmake_symbol (empty_unibyte_string);
2701 goto read_symbol;
2703 /* ## is the empty symbol. */
2704 if (c == '#')
2705 return Fintern (empty_unibyte_string, Qnil);
2706 /* Reader forms that can reuse previously read objects. */
2707 if (c >= '0' && c <= '9')
2709 EMACS_INT n = 0;
2710 Lisp_Object tem;
2712 /* Read a non-negative integer. */
2713 while (c >= '0' && c <= '9')
2715 if (MOST_POSITIVE_FIXNUM / 10 < n
2716 || MOST_POSITIVE_FIXNUM < n * 10 + c - '0')
2717 n = MOST_POSITIVE_FIXNUM + 1;
2718 else
2719 n = n * 10 + c - '0';
2720 c = READCHAR;
2723 if (n <= MOST_POSITIVE_FIXNUM)
2725 if (c == 'r' || c == 'R')
2726 return read_integer (readcharfun, n);
2728 if (! NILP (Vread_circle))
2730 /* #n=object returns object, but associates it with
2731 n for #n#. */
2732 if (c == '=')
2734 /* Make a placeholder for #n# to use temporarily. */
2735 Lisp_Object placeholder;
2736 Lisp_Object cell;
2738 placeholder = Fcons (Qnil, Qnil);
2739 cell = Fcons (make_number (n), placeholder);
2740 read_objects = Fcons (cell, read_objects);
2742 /* Read the object itself. */
2743 tem = read0 (readcharfun);
2745 /* Now put it everywhere the placeholder was... */
2746 substitute_object_in_subtree (tem, placeholder);
2748 /* ...and #n# will use the real value from now on. */
2749 Fsetcdr (cell, tem);
2751 return tem;
2754 /* #n# returns a previously read object. */
2755 if (c == '#')
2757 tem = Fassq (make_number (n), read_objects);
2758 if (CONSP (tem))
2759 return XCDR (tem);
2763 /* Fall through to error message. */
2765 else if (c == 'x' || c == 'X')
2766 return read_integer (readcharfun, 16);
2767 else if (c == 'o' || c == 'O')
2768 return read_integer (readcharfun, 8);
2769 else if (c == 'b' || c == 'B')
2770 return read_integer (readcharfun, 2);
2772 UNREAD (c);
2773 invalid_syntax ("#");
2775 case ';':
2776 while ((c = READCHAR) >= 0 && c != '\n');
2777 goto retry;
2779 case '\'':
2781 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2784 case '`':
2786 int next_char = READCHAR;
2787 UNREAD (next_char);
2788 /* Transition from old-style to new-style:
2789 If we see "(`" it used to mean old-style, which usually works
2790 fine because ` should almost never appear in such a position
2791 for new-style. But occasionally we need "(`" to mean new
2792 style, so we try to distinguish the two by the fact that we
2793 can either write "( `foo" or "(` foo", where the first
2794 intends to use new-style whereas the second intends to use
2795 old-style. For Emacs-25, we should completely remove this
2796 first_in_list exception (old-style can still be obtained via
2797 "(\`" anyway). */
2798 if (!new_backquote_flag && first_in_list && next_char == ' ')
2800 Vold_style_backquotes = Qt;
2801 goto default_label;
2803 else
2805 Lisp_Object value;
2807 new_backquote_flag++;
2808 value = read0 (readcharfun);
2809 new_backquote_flag--;
2811 return Fcons (Qbackquote, Fcons (value, Qnil));
2814 case ',':
2816 int next_char = READCHAR;
2817 UNREAD (next_char);
2818 /* Transition from old-style to new-style:
2819 It used to be impossible to have a new-style , other than within
2820 a new-style `. This is sufficient when ` and , are used in the
2821 normal way, but ` and , can also appear in args to macros that
2822 will not interpret them in the usual way, in which case , may be
2823 used without any ` anywhere near.
2824 So we now use the same heuristic as for backquote: old-style
2825 unquotes are only recognized when first on a list, and when
2826 followed by a space.
2827 Because it's more difficult to peek 2 chars ahead, a new-style
2828 ,@ can still not be used outside of a `, unless it's in the middle
2829 of a list. */
2830 if (new_backquote_flag
2831 || !first_in_list
2832 || (next_char != ' ' && next_char != '@'))
2834 Lisp_Object comma_type = Qnil;
2835 Lisp_Object value;
2836 int ch = READCHAR;
2838 if (ch == '@')
2839 comma_type = Qcomma_at;
2840 else if (ch == '.')
2841 comma_type = Qcomma_dot;
2842 else
2844 if (ch >= 0) UNREAD (ch);
2845 comma_type = Qcomma;
2848 value = read0 (readcharfun);
2849 return Fcons (comma_type, Fcons (value, Qnil));
2851 else
2853 Vold_style_backquotes = Qt;
2854 goto default_label;
2857 case '?':
2859 int modifiers;
2860 int next_char;
2861 int ok;
2863 c = READCHAR;
2864 if (c < 0)
2865 end_of_file_error ();
2867 /* Accept `single space' syntax like (list ? x) where the
2868 whitespace character is SPC or TAB.
2869 Other literal whitespace like NL, CR, and FF are not accepted,
2870 as there are well-established escape sequences for these. */
2871 if (c == ' ' || c == '\t')
2872 return make_number (c);
2874 if (c == '\\')
2875 c = read_escape (readcharfun, 0);
2876 modifiers = c & CHAR_MODIFIER_MASK;
2877 c &= ~CHAR_MODIFIER_MASK;
2878 if (CHAR_BYTE8_P (c))
2879 c = CHAR_TO_BYTE8 (c);
2880 c |= modifiers;
2882 next_char = READCHAR;
2883 ok = (next_char <= 040
2884 || (next_char < 0200
2885 && strchr ("\"';()[]#?`,.", next_char) != NULL));
2886 UNREAD (next_char);
2887 if (ok)
2888 return make_number (c);
2890 invalid_syntax ("?");
2893 case '"':
2895 char *p = read_buffer;
2896 char *end = read_buffer + read_buffer_size;
2897 register int ch;
2898 /* Nonzero if we saw an escape sequence specifying
2899 a multibyte character. */
2900 int force_multibyte = 0;
2901 /* Nonzero if we saw an escape sequence specifying
2902 a single-byte character. */
2903 int force_singlebyte = 0;
2904 int cancel = 0;
2905 ptrdiff_t nchars = 0;
2907 while ((ch = READCHAR) >= 0
2908 && ch != '\"')
2910 if (end - p < MAX_MULTIBYTE_LENGTH)
2912 ptrdiff_t offset = p - read_buffer;
2913 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
2914 memory_full (SIZE_MAX);
2915 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
2916 read_buffer_size *= 2;
2917 p = read_buffer + offset;
2918 end = read_buffer + read_buffer_size;
2921 if (ch == '\\')
2923 int modifiers;
2925 ch = read_escape (readcharfun, 1);
2927 /* CH is -1 if \ newline has just been seen. */
2928 if (ch == -1)
2930 if (p == read_buffer)
2931 cancel = 1;
2932 continue;
2935 modifiers = ch & CHAR_MODIFIER_MASK;
2936 ch = ch & ~CHAR_MODIFIER_MASK;
2938 if (CHAR_BYTE8_P (ch))
2939 force_singlebyte = 1;
2940 else if (! ASCII_CHAR_P (ch))
2941 force_multibyte = 1;
2942 else /* I.e. ASCII_CHAR_P (ch). */
2944 /* Allow `\C- ' and `\C-?'. */
2945 if (modifiers == CHAR_CTL)
2947 if (ch == ' ')
2948 ch = 0, modifiers = 0;
2949 else if (ch == '?')
2950 ch = 127, modifiers = 0;
2952 if (modifiers & CHAR_SHIFT)
2954 /* Shift modifier is valid only with [A-Za-z]. */
2955 if (ch >= 'A' && ch <= 'Z')
2956 modifiers &= ~CHAR_SHIFT;
2957 else if (ch >= 'a' && ch <= 'z')
2958 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
2961 if (modifiers & CHAR_META)
2963 /* Move the meta bit to the right place for a
2964 string. */
2965 modifiers &= ~CHAR_META;
2966 ch = BYTE8_TO_CHAR (ch | 0x80);
2967 force_singlebyte = 1;
2971 /* Any modifiers remaining are invalid. */
2972 if (modifiers)
2973 error ("Invalid modifier in string");
2974 p += CHAR_STRING (ch, (unsigned char *) p);
2976 else
2978 p += CHAR_STRING (ch, (unsigned char *) p);
2979 if (CHAR_BYTE8_P (ch))
2980 force_singlebyte = 1;
2981 else if (! ASCII_CHAR_P (ch))
2982 force_multibyte = 1;
2984 nchars++;
2987 if (ch < 0)
2988 end_of_file_error ();
2990 /* If purifying, and string starts with \ newline,
2991 return zero instead. This is for doc strings
2992 that we are really going to find in etc/DOC.nn.nn. */
2993 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2994 return make_number (0);
2996 if (! force_multibyte && force_singlebyte)
2998 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
2999 forms. Convert it to unibyte. */
3000 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3001 p - read_buffer);
3002 p = read_buffer + nchars;
3005 return make_specified_string (read_buffer, nchars, p - read_buffer,
3006 (force_multibyte
3007 || (p - read_buffer != nchars)));
3010 case '.':
3012 int next_char = READCHAR;
3013 UNREAD (next_char);
3015 if (next_char <= 040
3016 || (next_char < 0200
3017 && strchr ("\"';([#?`,", next_char) != NULL))
3019 *pch = c;
3020 return Qnil;
3023 /* Otherwise, we fall through! Note that the atom-reading loop
3024 below will now loop at least once, assuring that we will not
3025 try to UNREAD two characters in a row. */
3027 default:
3028 default_label:
3029 if (c <= 040) goto retry;
3030 if (c == 0xa0) /* NBSP */
3031 goto retry;
3033 read_symbol:
3035 char *p = read_buffer;
3036 int quoted = 0;
3037 EMACS_INT start_position = readchar_count - 1;
3040 char *end = read_buffer + read_buffer_size;
3044 if (end - p < MAX_MULTIBYTE_LENGTH)
3046 ptrdiff_t offset = p - read_buffer;
3047 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3048 memory_full (SIZE_MAX);
3049 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3050 read_buffer_size *= 2;
3051 p = read_buffer + offset;
3052 end = read_buffer + read_buffer_size;
3055 if (c == '\\')
3057 c = READCHAR;
3058 if (c == -1)
3059 end_of_file_error ();
3060 quoted = 1;
3063 if (multibyte)
3064 p += CHAR_STRING (c, (unsigned char *) p);
3065 else
3066 *p++ = c;
3067 c = READCHAR;
3069 while (c > 040
3070 && c != 0xa0 /* NBSP */
3071 && (c >= 0200
3072 || strchr ("\"';()[]#`,", c) == NULL));
3074 if (p == end)
3076 ptrdiff_t offset = p - read_buffer;
3077 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3078 memory_full (SIZE_MAX);
3079 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3080 read_buffer_size *= 2;
3081 p = read_buffer + offset;
3082 end = read_buffer + read_buffer_size;
3084 *p = 0;
3085 UNREAD (c);
3088 if (!quoted && !uninterned_symbol)
3090 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3091 if (! NILP (result))
3092 return result;
3095 Lisp_Object name, result;
3096 ptrdiff_t nbytes = p - read_buffer;
3097 ptrdiff_t nchars
3098 = (multibyte
3099 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3100 nbytes)
3101 : nbytes);
3103 name = ((uninterned_symbol && ! NILP (Vpurify_flag)
3104 ? make_pure_string : make_specified_string)
3105 (read_buffer, nchars, nbytes, multibyte));
3106 result = (uninterned_symbol ? Fmake_symbol (name)
3107 : Fintern (name, Qnil));
3109 if (EQ (Vread_with_symbol_positions, Qt)
3110 || EQ (Vread_with_symbol_positions, readcharfun))
3111 Vread_symbol_positions_list
3112 = Fcons (Fcons (result, make_number (start_position)),
3113 Vread_symbol_positions_list);
3114 return result;
3121 /* List of nodes we've seen during substitute_object_in_subtree. */
3122 static Lisp_Object seen_list;
3124 static void
3125 substitute_object_in_subtree (Lisp_Object object, Lisp_Object placeholder)
3127 Lisp_Object check_object;
3129 /* We haven't seen any objects when we start. */
3130 seen_list = Qnil;
3132 /* Make all the substitutions. */
3133 check_object
3134 = substitute_object_recurse (object, placeholder, object);
3136 /* Clear seen_list because we're done with it. */
3137 seen_list = Qnil;
3139 /* The returned object here is expected to always eq the
3140 original. */
3141 if (!EQ (check_object, object))
3142 error ("Unexpected mutation error in reader");
3145 /* Feval doesn't get called from here, so no gc protection is needed. */
3146 #define SUBSTITUTE(get_val, set_val) \
3147 do { \
3148 Lisp_Object old_value = get_val; \
3149 Lisp_Object true_value \
3150 = substitute_object_recurse (object, placeholder, \
3151 old_value); \
3153 if (!EQ (old_value, true_value)) \
3155 set_val; \
3157 } while (0)
3159 static Lisp_Object
3160 substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Object subtree)
3162 /* If we find the placeholder, return the target object. */
3163 if (EQ (placeholder, subtree))
3164 return object;
3166 /* If we've been to this node before, don't explore it again. */
3167 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3168 return subtree;
3170 /* If this node can be the entry point to a cycle, remember that
3171 we've seen it. It can only be such an entry point if it was made
3172 by #n=, which means that we can find it as a value in
3173 read_objects. */
3174 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3175 seen_list = Fcons (subtree, seen_list);
3177 /* Recurse according to subtree's type.
3178 Every branch must return a Lisp_Object. */
3179 switch (XTYPE (subtree))
3181 case Lisp_Vectorlike:
3183 ptrdiff_t i, length = 0;
3184 if (BOOL_VECTOR_P (subtree))
3185 return subtree; /* No sub-objects anyway. */
3186 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3187 || COMPILEDP (subtree))
3188 length = ASIZE (subtree) & PSEUDOVECTOR_SIZE_MASK;
3189 else if (VECTORP (subtree))
3190 length = ASIZE (subtree);
3191 else
3192 /* An unknown pseudovector may contain non-Lisp fields, so we
3193 can't just blindly traverse all its fields. We used to call
3194 `Flength' which signaled `sequencep', so I just preserved this
3195 behavior. */
3196 wrong_type_argument (Qsequencep, subtree);
3198 for (i = 0; i < length; i++)
3199 SUBSTITUTE (AREF (subtree, i),
3200 ASET (subtree, i, true_value));
3201 return subtree;
3204 case Lisp_Cons:
3206 SUBSTITUTE (XCAR (subtree),
3207 XSETCAR (subtree, true_value));
3208 SUBSTITUTE (XCDR (subtree),
3209 XSETCDR (subtree, true_value));
3210 return subtree;
3213 case Lisp_String:
3215 /* Check for text properties in each interval.
3216 substitute_in_interval contains part of the logic. */
3218 INTERVAL root_interval = string_intervals (subtree);
3219 Lisp_Object arg = Fcons (object, placeholder);
3221 traverse_intervals_noorder (root_interval,
3222 &substitute_in_interval, arg);
3224 return subtree;
3227 /* Other types don't recurse any further. */
3228 default:
3229 return subtree;
3233 /* Helper function for substitute_object_recurse. */
3234 static void
3235 substitute_in_interval (INTERVAL interval, Lisp_Object arg)
3237 Lisp_Object object = Fcar (arg);
3238 Lisp_Object placeholder = Fcdr (arg);
3240 SUBSTITUTE (interval->plist, set_interval_plist (interval, true_value));
3244 #define LEAD_INT 1
3245 #define DOT_CHAR 2
3246 #define TRAIL_INT 4
3247 #define E_EXP 16
3250 /* Convert STRING to a number, assuming base BASE. Return a fixnum if CP has
3251 integer syntax and fits in a fixnum, else return the nearest float if CP has
3252 either floating point or integer syntax and BASE is 10, else return nil. If
3253 IGNORE_TRAILING is nonzero, consider just the longest prefix of CP that has
3254 valid floating point syntax. Signal an overflow if BASE is not 10 and the
3255 number has integer syntax but does not fit. */
3257 Lisp_Object
3258 string_to_number (char const *string, int base, int ignore_trailing)
3260 int state;
3261 char const *cp = string;
3262 int leading_digit;
3263 int float_syntax = 0;
3264 double value = 0;
3266 /* Compute NaN and infinities using a variable, to cope with compilers that
3267 think they are smarter than we are. */
3268 double zero = 0;
3270 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3271 IEEE floating point hosts, and works around a formerly-common bug where
3272 atof ("-0.0") drops the sign. */
3273 int negative = *cp == '-';
3275 int signedp = negative || *cp == '+';
3276 cp += signedp;
3278 state = 0;
3280 leading_digit = digit_to_number (*cp, base);
3281 if (0 <= leading_digit)
3283 state |= LEAD_INT;
3285 ++cp;
3286 while (0 <= digit_to_number (*cp, base));
3288 if (*cp == '.')
3290 state |= DOT_CHAR;
3291 cp++;
3294 if (base == 10)
3296 if ('0' <= *cp && *cp <= '9')
3298 state |= TRAIL_INT;
3300 cp++;
3301 while ('0' <= *cp && *cp <= '9');
3303 if (*cp == 'e' || *cp == 'E')
3305 char const *ecp = cp;
3306 cp++;
3307 if (*cp == '+' || *cp == '-')
3308 cp++;
3309 if ('0' <= *cp && *cp <= '9')
3311 state |= E_EXP;
3313 cp++;
3314 while ('0' <= *cp && *cp <= '9');
3316 else if (cp[-1] == '+'
3317 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3319 state |= E_EXP;
3320 cp += 3;
3321 value = 1.0 / zero;
3323 else if (cp[-1] == '+'
3324 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3326 state |= E_EXP;
3327 cp += 3;
3328 value = zero / zero;
3330 /* If that made a "negative" NaN, negate it. */
3332 int i;
3333 union { double d; char c[sizeof (double)]; }
3334 u_data, u_minus_zero;
3335 u_data.d = value;
3336 u_minus_zero.d = -0.0;
3337 for (i = 0; i < sizeof (double); i++)
3338 if (u_data.c[i] & u_minus_zero.c[i])
3340 value = -value;
3341 break;
3344 /* Now VALUE is a positive NaN. */
3346 else
3347 cp = ecp;
3350 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3351 || state == (LEAD_INT|E_EXP));
3354 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3355 any prefix that matches. Otherwise, the entire string must match. */
3356 if (! (ignore_trailing
3357 ? ((state & LEAD_INT) != 0 || float_syntax)
3358 : (!*cp && ((state & ~DOT_CHAR) == LEAD_INT || float_syntax))))
3359 return Qnil;
3361 /* If the number uses integer and not float syntax, and is in C-language
3362 range, use its value, preferably as a fixnum. */
3363 if (0 <= leading_digit && ! float_syntax)
3365 uintmax_t n;
3367 /* Fast special case for single-digit integers. This also avoids a
3368 glitch when BASE is 16 and IGNORE_TRAILING is nonzero, because in that
3369 case some versions of strtoumax accept numbers like "0x1" that Emacs
3370 does not allow. */
3371 if (digit_to_number (string[signedp + 1], base) < 0)
3372 return make_number (negative ? -leading_digit : leading_digit);
3374 errno = 0;
3375 n = strtoumax (string + signedp, NULL, base);
3376 if (errno == ERANGE)
3378 /* Unfortunately there's no simple and accurate way to convert
3379 non-base-10 numbers that are out of C-language range. */
3380 if (base != 10)
3381 xsignal1 (Qoverflow_error, build_string (string));
3383 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3385 EMACS_INT signed_n = n;
3386 return make_number (negative ? -signed_n : signed_n);
3388 else
3389 value = n;
3392 /* Either the number uses float syntax, or it does not fit into a fixnum.
3393 Convert it from string to floating point, unless the value is already
3394 known because it is an infinity, a NAN, or its absolute value fits in
3395 uintmax_t. */
3396 if (! value)
3397 value = atof (string + signedp);
3399 return make_float (negative ? -value : value);
3403 static Lisp_Object
3404 read_vector (Lisp_Object readcharfun, int bytecodeflag)
3406 ptrdiff_t i, size;
3407 register Lisp_Object *ptr;
3408 register Lisp_Object tem, item, vector;
3409 register struct Lisp_Cons *otem;
3410 Lisp_Object len;
3412 tem = read_list (1, readcharfun);
3413 len = Flength (tem);
3414 vector = Fmake_vector (len, Qnil);
3416 size = ASIZE (vector);
3417 ptr = XVECTOR (vector)->contents;
3418 for (i = 0; i < size; i++)
3420 item = Fcar (tem);
3421 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3422 bytecode object, the docstring containing the bytecode and
3423 constants values must be treated as unibyte and passed to
3424 Fread, to get the actual bytecode string and constants vector. */
3425 if (bytecodeflag && load_force_doc_strings)
3427 if (i == COMPILED_BYTECODE)
3429 if (!STRINGP (item))
3430 error ("Invalid byte code");
3432 /* Delay handling the bytecode slot until we know whether
3433 it is lazily-loaded (we can tell by whether the
3434 constants slot is nil). */
3435 ASET (vector, COMPILED_CONSTANTS, item);
3436 item = Qnil;
3438 else if (i == COMPILED_CONSTANTS)
3440 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3442 if (NILP (item))
3444 /* Coerce string to unibyte (like string-as-unibyte,
3445 but without generating extra garbage and
3446 guaranteeing no change in the contents). */
3447 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3448 STRING_SET_UNIBYTE (bytestr);
3450 item = Fread (Fcons (bytestr, readcharfun));
3451 if (!CONSP (item))
3452 error ("Invalid byte code");
3454 otem = XCONS (item);
3455 bytestr = XCAR (item);
3456 item = XCDR (item);
3457 free_cons (otem);
3460 /* Now handle the bytecode slot. */
3461 ASET (vector, COMPILED_BYTECODE, bytestr);
3463 else if (i == COMPILED_DOC_STRING
3464 && STRINGP (item)
3465 && ! STRING_MULTIBYTE (item))
3467 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3468 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3469 else
3470 item = Fstring_as_multibyte (item);
3473 ASET (vector, i, item);
3474 otem = XCONS (tem);
3475 tem = Fcdr (tem);
3476 free_cons (otem);
3478 return vector;
3481 /* FLAG = 1 means check for ] to terminate rather than ) and . */
3483 static Lisp_Object
3484 read_list (int flag, register Lisp_Object readcharfun)
3486 Lisp_Object val, tail;
3487 register Lisp_Object elt, tem;
3488 struct gcpro gcpro1, gcpro2;
3489 /* 0 is the normal case.
3490 1 means this list is a doc reference; replace it with the number 0.
3491 2 means this list is a doc reference; replace it with the doc string. */
3492 int doc_reference = 0;
3494 /* Initialize this to 1 if we are reading a list. */
3495 int first_in_list = flag <= 0;
3497 val = Qnil;
3498 tail = Qnil;
3500 while (1)
3502 int ch;
3503 GCPRO2 (val, tail);
3504 elt = read1 (readcharfun, &ch, first_in_list);
3505 UNGCPRO;
3507 first_in_list = 0;
3509 /* While building, if the list starts with #$, treat it specially. */
3510 if (EQ (elt, Vload_file_name)
3511 && ! NILP (elt)
3512 && !NILP (Vpurify_flag))
3514 if (NILP (Vdoc_file_name))
3515 /* We have not yet called Snarf-documentation, so assume
3516 this file is described in the DOC-MM.NN file
3517 and Snarf-documentation will fill in the right value later.
3518 For now, replace the whole list with 0. */
3519 doc_reference = 1;
3520 else
3521 /* We have already called Snarf-documentation, so make a relative
3522 file name for this file, so it can be found properly
3523 in the installed Lisp directory.
3524 We don't use Fexpand_file_name because that would make
3525 the directory absolute now. */
3526 elt = concat2 (build_string ("../lisp/"),
3527 Ffile_name_nondirectory (elt));
3529 else if (EQ (elt, Vload_file_name)
3530 && ! NILP (elt)
3531 && load_force_doc_strings)
3532 doc_reference = 2;
3534 if (ch)
3536 if (flag > 0)
3538 if (ch == ']')
3539 return val;
3540 invalid_syntax (") or . in a vector");
3542 if (ch == ')')
3543 return val;
3544 if (ch == '.')
3546 GCPRO2 (val, tail);
3547 if (!NILP (tail))
3548 XSETCDR (tail, read0 (readcharfun));
3549 else
3550 val = read0 (readcharfun);
3551 read1 (readcharfun, &ch, 0);
3552 UNGCPRO;
3553 if (ch == ')')
3555 if (doc_reference == 1)
3556 return make_number (0);
3557 if (doc_reference == 2)
3559 /* Get a doc string from the file we are loading.
3560 If it's in saved_doc_string, get it from there.
3562 Here, we don't know if the string is a
3563 bytecode string or a doc string. As a
3564 bytecode string must be unibyte, we always
3565 return a unibyte string. If it is actually a
3566 doc string, caller must make it
3567 multibyte. */
3569 EMACS_INT pos = XINT (XCDR (val));
3570 /* Position is negative for user variables. */
3571 if (pos < 0) pos = -pos;
3572 if (pos >= saved_doc_string_position
3573 && pos < (saved_doc_string_position
3574 + saved_doc_string_length))
3576 ptrdiff_t start = pos - saved_doc_string_position;
3577 ptrdiff_t from, to;
3579 /* Process quoting with ^A,
3580 and find the end of the string,
3581 which is marked with ^_ (037). */
3582 for (from = start, to = start;
3583 saved_doc_string[from] != 037;)
3585 int c = saved_doc_string[from++];
3586 if (c == 1)
3588 c = saved_doc_string[from++];
3589 if (c == 1)
3590 saved_doc_string[to++] = c;
3591 else if (c == '0')
3592 saved_doc_string[to++] = 0;
3593 else if (c == '_')
3594 saved_doc_string[to++] = 037;
3596 else
3597 saved_doc_string[to++] = c;
3600 return make_unibyte_string (saved_doc_string + start,
3601 to - start);
3603 /* Look in prev_saved_doc_string the same way. */
3604 else if (pos >= prev_saved_doc_string_position
3605 && pos < (prev_saved_doc_string_position
3606 + prev_saved_doc_string_length))
3608 ptrdiff_t start =
3609 pos - prev_saved_doc_string_position;
3610 ptrdiff_t from, to;
3612 /* Process quoting with ^A,
3613 and find the end of the string,
3614 which is marked with ^_ (037). */
3615 for (from = start, to = start;
3616 prev_saved_doc_string[from] != 037;)
3618 int c = prev_saved_doc_string[from++];
3619 if (c == 1)
3621 c = prev_saved_doc_string[from++];
3622 if (c == 1)
3623 prev_saved_doc_string[to++] = c;
3624 else if (c == '0')
3625 prev_saved_doc_string[to++] = 0;
3626 else if (c == '_')
3627 prev_saved_doc_string[to++] = 037;
3629 else
3630 prev_saved_doc_string[to++] = c;
3633 return make_unibyte_string (prev_saved_doc_string
3634 + start,
3635 to - start);
3637 else
3638 return get_doc_string (val, 1, 0);
3641 return val;
3643 invalid_syntax (". in wrong context");
3645 invalid_syntax ("] in a list");
3647 tem = Fcons (elt, Qnil);
3648 if (!NILP (tail))
3649 XSETCDR (tail, tem);
3650 else
3651 val = tem;
3652 tail = tem;
3656 static Lisp_Object initial_obarray;
3658 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
3660 static size_t oblookup_last_bucket_number;
3662 /* Get an error if OBARRAY is not an obarray.
3663 If it is one, return it. */
3665 Lisp_Object
3666 check_obarray (Lisp_Object obarray)
3668 if (!VECTORP (obarray) || ASIZE (obarray) == 0)
3670 /* If Vobarray is now invalid, force it to be valid. */
3671 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3672 wrong_type_argument (Qvectorp, obarray);
3674 return obarray;
3677 /* Intern the C string STR: return a symbol with that name,
3678 interned in the current obarray. */
3680 Lisp_Object
3681 intern_1 (const char *str, ptrdiff_t len)
3683 Lisp_Object obarray = check_obarray (Vobarray);
3684 Lisp_Object tem = oblookup (obarray, str, len, len);
3686 return SYMBOLP (tem) ? tem : Fintern (make_string (str, len), obarray);
3689 Lisp_Object
3690 intern_c_string_1 (const char *str, ptrdiff_t len)
3692 Lisp_Object obarray = check_obarray (Vobarray);
3693 Lisp_Object tem = oblookup (obarray, str, len, len);
3695 if (SYMBOLP (tem))
3696 return tem;
3698 if (NILP (Vpurify_flag))
3699 /* Creating a non-pure string from a string literal not
3700 implemented yet. We could just use make_string here and live
3701 with the extra copy. */
3702 emacs_abort ();
3704 return Fintern (make_pure_c_string (str, len), obarray);
3707 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3708 doc: /* Return the canonical symbol whose name is STRING.
3709 If there is none, one is created by this function and returned.
3710 A second optional argument specifies the obarray to use;
3711 it defaults to the value of `obarray'. */)
3712 (Lisp_Object string, Lisp_Object obarray)
3714 register Lisp_Object tem, sym, *ptr;
3716 if (NILP (obarray)) obarray = Vobarray;
3717 obarray = check_obarray (obarray);
3719 CHECK_STRING (string);
3721 tem = oblookup (obarray, SSDATA (string),
3722 SCHARS (string),
3723 SBYTES (string));
3724 if (!INTEGERP (tem))
3725 return tem;
3727 if (!NILP (Vpurify_flag))
3728 string = Fpurecopy (string);
3729 sym = Fmake_symbol (string);
3731 if (EQ (obarray, initial_obarray))
3732 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3733 else
3734 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
3736 if ((SREF (string, 0) == ':')
3737 && EQ (obarray, initial_obarray))
3739 XSYMBOL (sym)->constant = 1;
3740 XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
3741 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
3744 ptr = aref_addr (obarray, XINT(tem));
3745 if (SYMBOLP (*ptr))
3746 set_symbol_next (sym, XSYMBOL (*ptr));
3747 else
3748 set_symbol_next (sym, NULL);
3749 *ptr = sym;
3750 return sym;
3753 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3754 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3755 NAME may be a string or a symbol. If it is a symbol, that exact
3756 symbol is searched for.
3757 A second optional argument specifies the obarray to use;
3758 it defaults to the value of `obarray'. */)
3759 (Lisp_Object name, Lisp_Object obarray)
3761 register Lisp_Object tem, string;
3763 if (NILP (obarray)) obarray = Vobarray;
3764 obarray = check_obarray (obarray);
3766 if (!SYMBOLP (name))
3768 CHECK_STRING (name);
3769 string = name;
3771 else
3772 string = SYMBOL_NAME (name);
3774 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3775 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3776 return Qnil;
3777 else
3778 return tem;
3781 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3782 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3783 The value is t if a symbol was found and deleted, nil otherwise.
3784 NAME may be a string or a symbol. If it is a symbol, that symbol
3785 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3786 OBARRAY defaults to the value of the variable `obarray'. */)
3787 (Lisp_Object name, Lisp_Object obarray)
3789 register Lisp_Object string, tem;
3790 size_t hash;
3792 if (NILP (obarray)) obarray = Vobarray;
3793 obarray = check_obarray (obarray);
3795 if (SYMBOLP (name))
3796 string = SYMBOL_NAME (name);
3797 else
3799 CHECK_STRING (name);
3800 string = name;
3803 tem = oblookup (obarray, SSDATA (string),
3804 SCHARS (string),
3805 SBYTES (string));
3806 if (INTEGERP (tem))
3807 return Qnil;
3808 /* If arg was a symbol, don't delete anything but that symbol itself. */
3809 if (SYMBOLP (name) && !EQ (name, tem))
3810 return Qnil;
3812 /* There are plenty of other symbols which will screw up the Emacs
3813 session if we unintern them, as well as even more ways to use
3814 `setq' or `fset' or whatnot to make the Emacs session
3815 unusable. Let's not go down this silly road. --Stef */
3816 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
3817 error ("Attempt to unintern t or nil"); */
3819 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3821 hash = oblookup_last_bucket_number;
3823 if (EQ (AREF (obarray, hash), tem))
3825 if (XSYMBOL (tem)->next)
3827 Lisp_Object sym;
3828 XSETSYMBOL (sym, XSYMBOL (tem)->next);
3829 ASET (obarray, hash, sym);
3831 else
3832 ASET (obarray, hash, make_number (0));
3834 else
3836 Lisp_Object tail, following;
3838 for (tail = AREF (obarray, hash);
3839 XSYMBOL (tail)->next;
3840 tail = following)
3842 XSETSYMBOL (following, XSYMBOL (tail)->next);
3843 if (EQ (following, tem))
3845 set_symbol_next (tail, XSYMBOL (following)->next);
3846 break;
3851 return Qt;
3854 /* Return the symbol in OBARRAY whose names matches the string
3855 of SIZE characters (SIZE_BYTE bytes) at PTR.
3856 If there is no such symbol in OBARRAY, return nil.
3858 Also store the bucket number in oblookup_last_bucket_number. */
3860 Lisp_Object
3861 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
3863 size_t hash;
3864 size_t obsize;
3865 register Lisp_Object tail;
3866 Lisp_Object bucket, tem;
3868 obarray = check_obarray (obarray);
3869 obsize = ASIZE (obarray);
3871 /* This is sometimes needed in the middle of GC. */
3872 obsize &= ~ARRAY_MARK_FLAG;
3873 hash = hash_string (ptr, size_byte) % obsize;
3874 bucket = AREF (obarray, hash);
3875 oblookup_last_bucket_number = hash;
3876 if (EQ (bucket, make_number (0)))
3878 else if (!SYMBOLP (bucket))
3879 error ("Bad data in guts of obarray"); /* Like CADR error message. */
3880 else
3881 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3883 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3884 && SCHARS (SYMBOL_NAME (tail)) == size
3885 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
3886 return tail;
3887 else if (XSYMBOL (tail)->next == 0)
3888 break;
3890 XSETINT (tem, hash);
3891 return tem;
3894 void
3895 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
3897 ptrdiff_t i;
3898 register Lisp_Object tail;
3899 CHECK_VECTOR (obarray);
3900 for (i = ASIZE (obarray) - 1; i >= 0; i--)
3902 tail = AREF (obarray, i);
3903 if (SYMBOLP (tail))
3904 while (1)
3906 (*fn) (tail, arg);
3907 if (XSYMBOL (tail)->next == 0)
3908 break;
3909 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3914 static void
3915 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
3917 call1 (function, sym);
3920 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3921 doc: /* Call FUNCTION on every symbol in OBARRAY.
3922 OBARRAY defaults to the value of `obarray'. */)
3923 (Lisp_Object function, Lisp_Object obarray)
3925 if (NILP (obarray)) obarray = Vobarray;
3926 obarray = check_obarray (obarray);
3928 map_obarray (obarray, mapatoms_1, function);
3929 return Qnil;
3932 #define OBARRAY_SIZE 1511
3934 void
3935 init_obarray (void)
3937 Lisp_Object oblength;
3938 ptrdiff_t size = 100 + MAX_MULTIBYTE_LENGTH;
3940 XSETFASTINT (oblength, OBARRAY_SIZE);
3942 Vobarray = Fmake_vector (oblength, make_number (0));
3943 initial_obarray = Vobarray;
3944 staticpro (&initial_obarray);
3946 Qunbound = Fmake_symbol (build_pure_c_string ("unbound"));
3947 /* Set temporary dummy values to Qnil and Vpurify_flag to satisfy the
3948 NILP (Vpurify_flag) check in intern_c_string. */
3949 Qnil = make_number (-1); Vpurify_flag = make_number (1);
3950 Qnil = intern_c_string ("nil");
3952 /* Fmake_symbol inits fields of new symbols with Qunbound and Qnil,
3953 so those two need to be fixed manually. */
3954 SET_SYMBOL_VAL (XSYMBOL (Qunbound), Qunbound);
3955 set_symbol_function (Qunbound, Qunbound);
3956 set_symbol_plist (Qunbound, Qnil);
3957 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
3958 XSYMBOL (Qnil)->constant = 1;
3959 XSYMBOL (Qnil)->declared_special = 1;
3960 set_symbol_plist (Qnil, Qnil);
3962 Qt = intern_c_string ("t");
3963 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
3964 XSYMBOL (Qnil)->declared_special = 1;
3965 XSYMBOL (Qt)->constant = 1;
3967 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3968 Vpurify_flag = Qt;
3970 DEFSYM (Qvariable_documentation, "variable-documentation");
3972 read_buffer = xmalloc (size);
3973 read_buffer_size = size;
3976 void
3977 defsubr (struct Lisp_Subr *sname)
3979 Lisp_Object sym, tem;
3980 sym = intern_c_string (sname->symbol_name);
3981 XSETTYPED_PVECTYPE (sname, size, PVEC_SUBR);
3982 XSETSUBR (tem, sname);
3983 set_symbol_function (sym, tem);
3986 #ifdef NOTDEF /* Use fset in subr.el now! */
3987 void
3988 defalias (struct Lisp_Subr *sname, char *string)
3990 Lisp_Object sym;
3991 sym = intern (string);
3992 XSETSUBR (XSYMBOL (sym)->function, sname);
3994 #endif /* NOTDEF */
3996 /* Define an "integer variable"; a symbol whose value is forwarded to a
3997 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
3998 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
3999 void
4000 defvar_int (struct Lisp_Intfwd *i_fwd,
4001 const char *namestring, EMACS_INT *address)
4003 Lisp_Object sym;
4004 sym = intern_c_string (namestring);
4005 i_fwd->type = Lisp_Fwd_Int;
4006 i_fwd->intvar = address;
4007 XSYMBOL (sym)->declared_special = 1;
4008 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4009 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4012 /* Similar but define a variable whose value is t if address contains 1,
4013 nil if address contains 0. */
4014 void
4015 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4016 const char *namestring, bool *address)
4018 Lisp_Object sym;
4019 sym = intern_c_string (namestring);
4020 b_fwd->type = Lisp_Fwd_Bool;
4021 b_fwd->boolvar = address;
4022 XSYMBOL (sym)->declared_special = 1;
4023 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4024 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4025 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4028 /* Similar but define a variable whose value is the Lisp Object stored
4029 at address. Two versions: with and without gc-marking of the C
4030 variable. The nopro version is used when that variable will be
4031 gc-marked for some other reason, since marking the same slot twice
4032 can cause trouble with strings. */
4033 void
4034 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4035 const char *namestring, Lisp_Object *address)
4037 Lisp_Object sym;
4038 sym = intern_c_string (namestring);
4039 o_fwd->type = Lisp_Fwd_Obj;
4040 o_fwd->objvar = address;
4041 XSYMBOL (sym)->declared_special = 1;
4042 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4043 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4046 void
4047 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4048 const char *namestring, Lisp_Object *address)
4050 defvar_lisp_nopro (o_fwd, namestring, address);
4051 staticpro (address);
4054 /* Similar but define a variable whose value is the Lisp Object stored
4055 at a particular offset in the current kboard object. */
4057 void
4058 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4059 const char *namestring, int offset)
4061 Lisp_Object sym;
4062 sym = intern_c_string (namestring);
4063 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4064 ko_fwd->offset = offset;
4065 XSYMBOL (sym)->declared_special = 1;
4066 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4067 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4070 /* Check that the elements of Vload_path exist. */
4072 static void
4073 load_path_check (void)
4075 Lisp_Object path_tail;
4077 /* The only elements that might not exist are those from
4078 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4079 it exists. */
4080 for (path_tail = Vload_path; !NILP (path_tail); path_tail = XCDR (path_tail))
4082 Lisp_Object dirfile;
4083 dirfile = Fcar (path_tail);
4084 if (STRINGP (dirfile))
4086 dirfile = Fdirectory_file_name (dirfile);
4087 if (access (SSDATA (dirfile), 0) < 0)
4088 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
4089 XCAR (path_tail));
4094 /* Record the value of load-path used at the start of dumping
4095 so we can see if the site changed it later during dumping. */
4096 static Lisp_Object dump_path;
4098 /* Compute the default Vload_path, with the following logic:
4099 If CANNOT_DUMP:
4100 use EMACSLOADPATH env-var if set; otherwise use PATH_LOADSEARCH,
4101 prepending PATH_SITELOADSEARCH unless --no-site-lisp.
4102 The remainder is what happens when dumping works:
4103 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4104 Otherwise use EMACSLOADPATH if set, else PATH_LOADSEARCH.
4106 If !initialized, then just set both Vload_path and dump_path.
4107 If initialized, then if Vload_path != dump_path, do nothing.
4108 (Presumably the load-path has already been changed by something.
4109 This can only be from a site-load file during dumping,
4110 or because EMACSLOADPATH is set.)
4111 If Vinstallation_directory is not nil (ie, running uninstalled):
4112 If installation-dir/lisp exists and not already a member,
4113 we must be running uninstalled. Reset the load-path
4114 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4115 refers to the eventual installation directories. Since we
4116 are not yet installed, we should not use them, even if they exist.)
4117 If installation-dir/lisp does not exist, just add dump_path at the
4118 end instead.
4119 Add installation-dir/leim (if exists and not already a member) at the front.
4120 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4121 and not already a member) at the front.
4122 If installation-dir != source-dir (ie running an uninstalled,
4123 out-of-tree build) AND install-dir/src/Makefile exists BUT
4124 install-dir/src/Makefile.in does NOT exist (this is a sanity
4125 check), then repeat the above steps for source-dir/lisp,
4126 leim and site-lisp.
4127 Finally, add the site-lisp directories at the front (if !no_site_lisp).
4130 void
4131 init_lread (void)
4133 const char *normal;
4135 #ifdef CANNOT_DUMP
4136 #ifdef HAVE_NS
4137 const char *loadpath = ns_load_path ();
4138 #endif
4140 normal = PATH_LOADSEARCH;
4141 #ifdef HAVE_NS
4142 Vload_path = decode_env_path ("EMACSLOADPATH", loadpath ? loadpath : normal);
4143 #else
4144 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
4145 #endif
4147 load_path_check ();
4149 /* FIXME CANNOT_DUMP platforms should get source-dir/lisp etc added
4150 to their load-path too, AFAICS. I don't think we can tell the
4151 difference between initialized and !initialized in this case,
4152 so we'll have to do it unconditionally when Vinstallation_directory
4153 is non-nil. */
4154 if (!no_site_lisp && !egetenv ("EMACSLOADPATH"))
4156 Lisp_Object sitelisp;
4157 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
4158 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4160 #else /* !CANNOT_DUMP */
4161 if (NILP (Vpurify_flag))
4163 normal = PATH_LOADSEARCH;
4164 /* If the EMACSLOADPATH environment variable is set, use its value.
4165 This doesn't apply if we're dumping. */
4166 if (egetenv ("EMACSLOADPATH"))
4167 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
4169 else
4170 normal = PATH_DUMPLOADSEARCH;
4172 /* In a dumped Emacs, we normally reset the value of Vload_path using
4173 PATH_LOADSEARCH, since the value that was dumped uses lisp/ in
4174 the source directory, instead of the path of the installed elisp
4175 libraries. However, if it appears that Vload_path has already been
4176 changed from the default that was saved before dumping, don't
4177 change it further. Changes can only be due to EMACSLOADPATH, or
4178 site-lisp files that were processed during dumping. */
4179 if (initialized)
4181 if (NILP (Fequal (dump_path, Vload_path)))
4183 /* Do not make any changes, just check the elements exist. */
4184 /* Note: --no-site-lisp is ignored.
4185 I don't know what to do about this. */
4186 load_path_check ();
4188 else
4190 #ifdef HAVE_NS
4191 const char *loadpath = ns_load_path ();
4192 Vload_path = decode_env_path (0, loadpath ? loadpath : normal);
4193 #else
4194 Vload_path = decode_env_path (0, normal);
4195 #endif
4196 if (!NILP (Vinstallation_directory))
4198 Lisp_Object tem, tem1;
4200 /* Add to the path the lisp subdir of the installation
4201 dir, if it exists. Note: in out-of-tree builds,
4202 this directory is empty save for Makefile. */
4203 tem = Fexpand_file_name (build_string ("lisp"),
4204 Vinstallation_directory);
4205 tem1 = Ffile_exists_p (tem);
4206 if (!NILP (tem1))
4208 if (NILP (Fmember (tem, Vload_path)))
4210 /* We are running uninstalled. The default load-path
4211 points to the eventual installed lisp, leim
4212 directories. We should not use those now, even
4213 if they exist, so start over from a clean slate. */
4214 Vload_path = Fcons (tem, Qnil);
4217 else
4218 /* That dir doesn't exist, so add the build-time
4219 Lisp dirs instead. */
4220 Vload_path = nconc2 (Vload_path, dump_path);
4222 /* Add leim under the installation dir, if it exists. */
4223 tem = Fexpand_file_name (build_string ("leim"),
4224 Vinstallation_directory);
4225 tem1 = Ffile_exists_p (tem);
4226 if (!NILP (tem1))
4228 if (NILP (Fmember (tem, Vload_path)))
4229 Vload_path = Fcons (tem, Vload_path);
4232 /* Add site-lisp under the installation dir, if it exists. */
4233 if (!no_site_lisp)
4235 tem = Fexpand_file_name (build_string ("site-lisp"),
4236 Vinstallation_directory);
4237 tem1 = Ffile_exists_p (tem);
4238 if (!NILP (tem1))
4240 if (NILP (Fmember (tem, Vload_path)))
4241 Vload_path = Fcons (tem, Vload_path);
4245 /* If Emacs was not built in the source directory,
4246 and it is run from where it was built, add to load-path
4247 the lisp, leim and site-lisp dirs under that directory. */
4249 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4251 Lisp_Object tem2;
4253 tem = Fexpand_file_name (build_string ("src/Makefile"),
4254 Vinstallation_directory);
4255 tem1 = Ffile_exists_p (tem);
4257 /* Don't be fooled if they moved the entire source tree
4258 AFTER dumping Emacs. If the build directory is indeed
4259 different from the source dir, src/Makefile.in and
4260 src/Makefile will not be found together. */
4261 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4262 Vinstallation_directory);
4263 tem2 = Ffile_exists_p (tem);
4264 if (!NILP (tem1) && NILP (tem2))
4266 tem = Fexpand_file_name (build_string ("lisp"),
4267 Vsource_directory);
4269 if (NILP (Fmember (tem, Vload_path)))
4270 Vload_path = Fcons (tem, Vload_path);
4272 tem = Fexpand_file_name (build_string ("leim"),
4273 Vsource_directory);
4275 if (NILP (Fmember (tem, Vload_path)))
4276 Vload_path = Fcons (tem, Vload_path);
4278 if (!no_site_lisp)
4280 tem = Fexpand_file_name (build_string ("site-lisp"),
4281 Vsource_directory);
4282 tem1 = Ffile_exists_p (tem);
4283 if (!NILP (tem1))
4285 if (NILP (Fmember (tem, Vload_path)))
4286 Vload_path = Fcons (tem, Vload_path);
4290 } /* Vinstallation_directory != Vsource_directory */
4292 } /* if Vinstallation_directory */
4294 /* Check before adding the site-lisp directories.
4295 The install should have created them, but they are not
4296 required, so no need to warn if they are absent.
4297 Or we might be running before installation. */
4298 load_path_check ();
4300 /* Add the site-lisp directories at the front. */
4301 if (!no_site_lisp)
4303 Lisp_Object sitelisp;
4304 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
4305 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4307 } /* if dump_path == Vload_path */
4309 else /* !initialized */
4311 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4312 source directory. We used to add ../lisp (ie the lisp dir in
4313 the build directory) at the front here, but that caused trouble
4314 because it was copied from dump_path into Vload_path, above,
4315 when Vinstallation_directory was non-nil. It should not be
4316 necessary, since in out of tree builds lisp/ is empty, save
4317 for Makefile. */
4318 Vload_path = decode_env_path (0, normal);
4319 dump_path = Vload_path;
4320 /* No point calling load_path_check; load-path only contains essential
4321 elements from the source directory at this point. They cannot
4322 be missing unless something went extremely (and improbably)
4323 wrong, in which case the build will fail in obvious ways. */
4325 #endif /* !CANNOT_DUMP */
4327 Vvalues = Qnil;
4329 load_in_progress = 0;
4330 Vload_file_name = Qnil;
4332 load_descriptor_list = Qnil;
4334 Vstandard_input = Qt;
4335 Vloads_in_progress = Qnil;
4338 /* Print a warning, using format string FORMAT, that directory DIRNAME
4339 does not exist. Print it on stderr and put it in *Messages*. */
4341 void
4342 dir_warning (const char *format, Lisp_Object dirname)
4344 fprintf (stderr, format, SDATA (dirname));
4346 /* Don't log the warning before we've initialized!! */
4347 if (initialized)
4349 USE_SAFE_ALLOCA;
4350 char *buffer = SAFE_ALLOCA (SBYTES (dirname)
4351 + strlen (format) - (sizeof "%s" - 1) + 1);
4352 ptrdiff_t message_len = esprintf (buffer, format, SDATA (dirname));
4353 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4354 SAFE_FREE ();
4358 void
4359 syms_of_lread (void)
4361 defsubr (&Sread);
4362 defsubr (&Sread_from_string);
4363 defsubr (&Sintern);
4364 defsubr (&Sintern_soft);
4365 defsubr (&Sunintern);
4366 defsubr (&Sget_load_suffixes);
4367 defsubr (&Sload);
4368 defsubr (&Seval_buffer);
4369 defsubr (&Seval_region);
4370 defsubr (&Sread_char);
4371 defsubr (&Sread_char_exclusive);
4372 defsubr (&Sread_event);
4373 defsubr (&Sget_file_char);
4374 defsubr (&Smapatoms);
4375 defsubr (&Slocate_file_internal);
4377 DEFVAR_LISP ("obarray", Vobarray,
4378 doc: /* Symbol table for use by `intern' and `read'.
4379 It is a vector whose length ought to be prime for best results.
4380 The vector's contents don't make sense if examined from Lisp programs;
4381 to find all the symbols in an obarray, use `mapatoms'. */);
4383 DEFVAR_LISP ("values", Vvalues,
4384 doc: /* List of values of all expressions which were read, evaluated and printed.
4385 Order is reverse chronological. */);
4386 XSYMBOL (intern ("values"))->declared_special = 0;
4388 DEFVAR_LISP ("standard-input", Vstandard_input,
4389 doc: /* Stream for read to get input from.
4390 See documentation of `read' for possible values. */);
4391 Vstandard_input = Qt;
4393 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4394 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4396 If this variable is a buffer, then only forms read from that buffer
4397 will be added to `read-symbol-positions-list'.
4398 If this variable is t, then all read forms will be added.
4399 The effect of all other values other than nil are not currently
4400 defined, although they may be in the future.
4402 The positions are relative to the last call to `read' or
4403 `read-from-string'. It is probably a bad idea to set this variable at
4404 the toplevel; bind it instead. */);
4405 Vread_with_symbol_positions = Qnil;
4407 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4408 doc: /* A list mapping read symbols to their positions.
4409 This variable is modified during calls to `read' or
4410 `read-from-string', but only when `read-with-symbol-positions' is
4411 non-nil.
4413 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4414 CHAR-POSITION is an integer giving the offset of that occurrence of the
4415 symbol from the position where `read' or `read-from-string' started.
4417 Note that a symbol will appear multiple times in this list, if it was
4418 read multiple times. The list is in the same order as the symbols
4419 were read in. */);
4420 Vread_symbol_positions_list = Qnil;
4422 DEFVAR_LISP ("read-circle", Vread_circle,
4423 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4424 Vread_circle = Qt;
4426 DEFVAR_LISP ("load-path", Vload_path,
4427 doc: /* List of directories to search for files to load.
4428 Each element is a string (directory name) or nil (try default directory).
4429 Initialized based on EMACSLOADPATH environment variable, if any,
4430 otherwise to default specified by file `epaths.h' when Emacs was built. */);
4432 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4433 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4434 This list should not include the empty string.
4435 `load' and related functions try to append these suffixes, in order,
4436 to the specified file name if a Lisp suffix is allowed or required. */);
4437 Vload_suffixes = Fcons (build_pure_c_string (".elc"),
4438 Fcons (build_pure_c_string (".el"), Qnil));
4439 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4440 doc: /* List of suffixes that indicate representations of \
4441 the same file.
4442 This list should normally start with the empty string.
4444 Enabling Auto Compression mode appends the suffixes in
4445 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4446 mode removes them again. `load' and related functions use this list to
4447 determine whether they should look for compressed versions of a file
4448 and, if so, which suffixes they should try to append to the file name
4449 in order to do so. However, if you want to customize which suffixes
4450 the loading functions recognize as compression suffixes, you should
4451 customize `jka-compr-load-suffixes' rather than the present variable. */);
4452 Vload_file_rep_suffixes = Fcons (empty_unibyte_string, Qnil);
4454 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4455 doc: /* Non-nil if inside of `load'. */);
4456 DEFSYM (Qload_in_progress, "load-in-progress");
4458 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4459 doc: /* An alist of expressions to be evalled when particular files are loaded.
4460 Each element looks like (REGEXP-OR-FEATURE FORMS...).
4462 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4463 a symbol \(a feature name).
4465 When `load' is run and the file-name argument matches an element's
4466 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4467 REGEXP-OR-FEATURE, the FORMS in the element are executed.
4469 An error in FORMS does not undo the load, but does prevent execution of
4470 the rest of the FORMS. */);
4471 Vafter_load_alist = Qnil;
4473 DEFVAR_LISP ("load-history", Vload_history,
4474 doc: /* Alist mapping loaded file names to symbols and features.
4475 Each alist element should be a list (FILE-NAME ENTRIES...), where
4476 FILE-NAME is the name of a file that has been loaded into Emacs.
4477 The file name is absolute and true (i.e. it doesn't contain symlinks).
4478 As an exception, one of the alist elements may have FILE-NAME nil,
4479 for symbols and features not associated with any file.
4481 The remaining ENTRIES in the alist element describe the functions and
4482 variables defined in that file, the features provided, and the
4483 features required. Each entry has the form `(provide . FEATURE)',
4484 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4485 `(defface . SYMBOL)', or `(t . SYMBOL)'. Entries like `(t . SYMBOL)'
4486 may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
4487 autoload before this file redefined it as a function. In addition,
4488 entries may also be single symbols, which means that SYMBOL was
4489 defined by `defvar' or `defconst'.
4491 During preloading, the file name recorded is relative to the main Lisp
4492 directory. These file names are converted to absolute at startup. */);
4493 Vload_history = Qnil;
4495 DEFVAR_LISP ("load-file-name", Vload_file_name,
4496 doc: /* Full name of file being loaded by `load'. */);
4497 Vload_file_name = Qnil;
4499 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4500 doc: /* File name, including directory, of user's initialization file.
4501 If the file loaded had extension `.elc', and the corresponding source file
4502 exists, this variable contains the name of source file, suitable for use
4503 by functions like `custom-save-all' which edit the init file.
4504 While Emacs loads and evaluates the init file, value is the real name
4505 of the file, regardless of whether or not it has the `.elc' extension. */);
4506 Vuser_init_file = Qnil;
4508 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4509 doc: /* Used for internal purposes by `load'. */);
4510 Vcurrent_load_list = Qnil;
4512 DEFVAR_LISP ("load-read-function", Vload_read_function,
4513 doc: /* Function used by `load' and `eval-region' for reading expressions.
4514 The default is nil, which means use the function `read'. */);
4515 Vload_read_function = Qnil;
4517 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4518 doc: /* Function called in `load' for loading an Emacs Lisp source file.
4519 This function is for doing code conversion before reading the source file.
4520 If nil, loading is done without any code conversion.
4521 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
4522 FULLNAME is the full name of FILE.
4523 See `load' for the meaning of the remaining arguments. */);
4524 Vload_source_file_function = Qnil;
4526 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4527 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4528 This is useful when the file being loaded is a temporary copy. */);
4529 load_force_doc_strings = 0;
4531 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4532 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4533 This is normally bound by `load' and `eval-buffer' to control `read',
4534 and is not meant for users to change. */);
4535 load_convert_to_unibyte = 0;
4537 DEFVAR_LISP ("source-directory", Vsource_directory,
4538 doc: /* Directory in which Emacs sources were found when Emacs was built.
4539 You cannot count on them to still be there! */);
4540 Vsource_directory
4541 = Fexpand_file_name (build_string ("../"),
4542 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
4544 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
4545 doc: /* List of files that were preloaded (when dumping Emacs). */);
4546 Vpreloaded_file_list = Qnil;
4548 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
4549 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4550 Vbyte_boolean_vars = Qnil;
4552 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
4553 doc: /* Non-nil means load dangerous compiled Lisp files.
4554 Some versions of XEmacs use different byte codes than Emacs. These
4555 incompatible byte codes can make Emacs crash when it tries to execute
4556 them. */);
4557 load_dangerous_libraries = 0;
4559 DEFVAR_BOOL ("force-load-messages", force_load_messages,
4560 doc: /* Non-nil means force printing messages when loading Lisp files.
4561 This overrides the value of the NOMESSAGE argument to `load'. */);
4562 force_load_messages = 0;
4564 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
4565 doc: /* Regular expression matching safe to load compiled Lisp files.
4566 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4567 from the file, and matches them against this regular expression.
4568 When the regular expression matches, the file is considered to be safe
4569 to load. See also `load-dangerous-libraries'. */);
4570 Vbytecomp_version_regexp
4571 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4573 DEFSYM (Qlexical_binding, "lexical-binding");
4574 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
4575 doc: /* Whether to use lexical binding when evaluating code.
4576 Non-nil means that the code in the current buffer should be evaluated
4577 with lexical binding.
4578 This variable is automatically set from the file variables of an
4579 interpreted Lisp file read using `load'. Unlike other file local
4580 variables, this must be set in the first line of a file. */);
4581 Vlexical_binding = Qnil;
4582 Fmake_variable_buffer_local (Qlexical_binding);
4584 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
4585 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4586 Veval_buffer_list = Qnil;
4588 DEFVAR_LISP ("old-style-backquotes", Vold_style_backquotes,
4589 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4590 Vold_style_backquotes = Qnil;
4591 DEFSYM (Qold_style_backquotes, "old-style-backquotes");
4593 /* Vsource_directory was initialized in init_lread. */
4595 load_descriptor_list = Qnil;
4596 staticpro (&load_descriptor_list);
4598 DEFSYM (Qcurrent_load_list, "current-load-list");
4599 DEFSYM (Qstandard_input, "standard-input");
4600 DEFSYM (Qread_char, "read-char");
4601 DEFSYM (Qget_file_char, "get-file-char");
4602 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
4603 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
4605 DEFSYM (Qbackquote, "`");
4606 DEFSYM (Qcomma, ",");
4607 DEFSYM (Qcomma_at, ",@");
4608 DEFSYM (Qcomma_dot, ",.");
4610 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
4611 DEFSYM (Qascii_character, "ascii-character");
4612 DEFSYM (Qfunction, "function");
4613 DEFSYM (Qload, "load");
4614 DEFSYM (Qload_file_name, "load-file-name");
4615 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
4616 DEFSYM (Qfile_truename, "file-truename");
4617 DEFSYM (Qdir_ok, "dir-ok");
4618 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
4620 staticpro (&dump_path);
4622 staticpro (&read_objects);
4623 read_objects = Qnil;
4624 staticpro (&seen_list);
4625 seen_list = Qnil;
4627 Vloads_in_progress = Qnil;
4628 staticpro (&Vloads_in_progress);
4630 DEFSYM (Qhash_table, "hash-table");
4631 DEFSYM (Qdata, "data");
4632 DEFSYM (Qtest, "test");
4633 DEFSYM (Qsize, "size");
4634 DEFSYM (Qweakness, "weakness");
4635 DEFSYM (Qrehash_size, "rehash-size");
4636 DEFSYM (Qrehash_threshold, "rehash-threshold");