Yet another round of fixing the Emacs manual
[emacs.git] / src / lread.c
blob3104c441ecfd480af93c7b5fe2521be91192a745
1 /* Lisp parsing and input streams.
3 Copyright (C) 1985-1989, 1993-1995, 1997-2018 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 /* Tell globals.h to define tables needed by init_obarray. */
22 #define DEFINE_SYMBOLS
24 #include <config.h>
25 #include "sysstdio.h"
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/file.h>
30 #include <errno.h>
31 #include <math.h>
32 #include <stat-time.h>
33 #include "lisp.h"
34 #include "dispextern.h"
35 #include "intervals.h"
36 #include "character.h"
37 #include "buffer.h"
38 #include "charset.h"
39 #include <epaths.h>
40 #include "commands.h"
41 #include "keyboard.h"
42 #include "systime.h"
43 #include "termhooks.h"
44 #include "blockinput.h"
45 #include <c-ctype.h>
47 #ifdef MSDOS
48 #include "msdos.h"
49 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 5
50 # define INFINITY __builtin_inf()
51 # define NAN __builtin_nan("")
52 #endif
53 #endif
55 #ifdef HAVE_NS
56 #include "nsterm.h"
57 #endif
59 #include <unistd.h>
61 #ifdef HAVE_SETLOCALE
62 #include <locale.h>
63 #endif /* HAVE_SETLOCALE */
65 #include <fcntl.h>
67 #ifdef HAVE_FSEEKO
68 #define file_offset off_t
69 #define file_tell ftello
70 #else
71 #define file_offset long
72 #define file_tell ftell
73 #endif
75 /* The objects or placeholders read with the #n=object form.
77 A hash table maps a number to either a placeholder (while the
78 object is still being parsed, in case it's referenced within its
79 own definition) or to the completed object. With small integers
80 for keys, it's effectively little more than a vector, but it'll
81 manage any needed resizing for us.
83 The variable must be reset to an empty hash table before all
84 top-level calls to read0. In between calls, it may be an empty
85 hash table left unused from the previous call (to reduce
86 allocations), or nil. */
87 static Lisp_Object read_objects_map;
89 /* The recursive objects read with the #n=object form.
91 Objects that might have circular references are stored here, so
92 that recursive substitution knows not to keep processing them
93 multiple times.
95 Only objects that are completely processed, including substituting
96 references to themselves (but not necessarily replacing
97 placeholders for other objects still being read), are stored.
99 A hash table is used for efficient lookups of keys. We don't care
100 what the value slots hold. The variable must be set to an empty
101 hash table before all top-level calls to read0. In between calls,
102 it may be an empty hash table left unused from the previous call
103 (to reduce allocations), or nil. */
104 static Lisp_Object read_objects_completed;
106 /* File and lookahead for get-file-char and get-emacs-mule-file-char
107 to read from. Used by Fload. */
108 static struct infile
110 /* The input stream. */
111 FILE *stream;
113 /* Lookahead byte count. */
114 signed char lookahead;
116 /* Lookahead bytes, in reverse order. Keep these here because it is
117 not portable to ungetc more than one byte at a time. */
118 unsigned char buf[MAX_MULTIBYTE_LENGTH - 1];
119 } *infile;
121 /* For use within read-from-string (this reader is non-reentrant!!) */
122 static ptrdiff_t read_from_string_index;
123 static ptrdiff_t read_from_string_index_byte;
124 static ptrdiff_t read_from_string_limit;
126 /* Number of characters read in the current call to Fread or
127 Fread_from_string. */
128 static EMACS_INT readchar_count;
130 /* This contains the last string skipped with #@. */
131 static char *saved_doc_string;
132 /* Length of buffer allocated in saved_doc_string. */
133 static ptrdiff_t saved_doc_string_size;
134 /* Length of actual data in saved_doc_string. */
135 static ptrdiff_t saved_doc_string_length;
136 /* This is the file position that string came from. */
137 static file_offset saved_doc_string_position;
139 /* This contains the previous string skipped with #@.
140 We copy it from saved_doc_string when a new string
141 is put in saved_doc_string. */
142 static char *prev_saved_doc_string;
143 /* Length of buffer allocated in prev_saved_doc_string. */
144 static ptrdiff_t prev_saved_doc_string_size;
145 /* Length of actual data in prev_saved_doc_string. */
146 static ptrdiff_t prev_saved_doc_string_length;
147 /* This is the file position that string came from. */
148 static file_offset prev_saved_doc_string_position;
150 /* True means inside a new-style backquote
151 with no surrounding parentheses.
152 Fread initializes this to false, so we need not specbind it
153 or worry about what happens to it when there is an error. */
154 static bool new_backquote_flag;
156 /* A list of file names for files being loaded in Fload. Used to
157 check for recursive loads. */
159 static Lisp_Object Vloads_in_progress;
161 static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
162 Lisp_Object);
164 static void readevalloop (Lisp_Object, struct infile *, Lisp_Object, bool,
165 Lisp_Object, Lisp_Object,
166 Lisp_Object, Lisp_Object);
168 /* Functions that read one byte from the current source READCHARFUN
169 or unreads one byte. If the integer argument C is -1, it returns
170 one read byte, or -1 when there's no more byte in the source. If C
171 is 0 or positive, it unreads C, and the return value is not
172 interesting. */
174 static int readbyte_for_lambda (int, Lisp_Object);
175 static int readbyte_from_file (int, Lisp_Object);
176 static int readbyte_from_string (int, Lisp_Object);
178 /* Handle unreading and rereading of characters.
179 Write READCHAR to read a character,
180 UNREAD(c) to unread c to be read again.
182 These macros correctly read/unread multibyte characters. */
184 #define READCHAR readchar (readcharfun, NULL)
185 #define UNREAD(c) unreadchar (readcharfun, c)
187 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
188 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
190 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
191 Qlambda, or a cons, we use this to keep an unread character because
192 a file stream can't handle multibyte-char unreading. The value -1
193 means that there's no unread character. */
194 static int unread_char;
196 static int
197 readchar (Lisp_Object readcharfun, bool *multibyte)
199 Lisp_Object tem;
200 register int c;
201 int (*readbyte) (int, Lisp_Object);
202 unsigned char buf[MAX_MULTIBYTE_LENGTH];
203 int i, len;
204 bool emacs_mule_encoding = 0;
206 if (multibyte)
207 *multibyte = 0;
209 readchar_count++;
211 if (BUFFERP (readcharfun))
213 register struct buffer *inbuffer = XBUFFER (readcharfun);
215 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
217 if (! BUFFER_LIVE_P (inbuffer))
218 return -1;
220 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
221 return -1;
223 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
225 /* Fetch the character code from the buffer. */
226 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
227 BUF_INC_POS (inbuffer, pt_byte);
228 c = STRING_CHAR (p);
229 if (multibyte)
230 *multibyte = 1;
232 else
234 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
235 if (! ASCII_CHAR_P (c))
236 c = BYTE8_TO_CHAR (c);
237 pt_byte++;
239 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
241 return c;
243 if (MARKERP (readcharfun))
245 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
247 ptrdiff_t bytepos = marker_byte_position (readcharfun);
249 if (bytepos >= BUF_ZV_BYTE (inbuffer))
250 return -1;
252 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
254 /* Fetch the character code from the buffer. */
255 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
256 BUF_INC_POS (inbuffer, bytepos);
257 c = STRING_CHAR (p);
258 if (multibyte)
259 *multibyte = 1;
261 else
263 c = BUF_FETCH_BYTE (inbuffer, bytepos);
264 if (! ASCII_CHAR_P (c))
265 c = BYTE8_TO_CHAR (c);
266 bytepos++;
269 XMARKER (readcharfun)->bytepos = bytepos;
270 XMARKER (readcharfun)->charpos++;
272 return c;
275 if (EQ (readcharfun, Qlambda))
277 readbyte = readbyte_for_lambda;
278 goto read_multibyte;
281 if (EQ (readcharfun, Qget_file_char))
283 readbyte = readbyte_from_file;
284 goto read_multibyte;
287 if (STRINGP (readcharfun))
289 if (read_from_string_index >= read_from_string_limit)
290 c = -1;
291 else if (STRING_MULTIBYTE (readcharfun))
293 if (multibyte)
294 *multibyte = 1;
295 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
296 read_from_string_index,
297 read_from_string_index_byte);
299 else
301 c = SREF (readcharfun, read_from_string_index_byte);
302 read_from_string_index++;
303 read_from_string_index_byte++;
305 return c;
308 if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
310 /* This is the case that read_vector is reading from a unibyte
311 string that contains a byte sequence previously skipped
312 because of #@NUMBER. The car part of readcharfun is that
313 string, and the cdr part is a value of readcharfun given to
314 read_vector. */
315 readbyte = readbyte_from_string;
316 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
317 emacs_mule_encoding = 1;
318 goto read_multibyte;
321 if (EQ (readcharfun, Qget_emacs_mule_file_char))
323 readbyte = readbyte_from_file;
324 emacs_mule_encoding = 1;
325 goto read_multibyte;
328 tem = call0 (readcharfun);
330 if (NILP (tem))
331 return -1;
332 return XINT (tem);
334 read_multibyte:
335 if (unread_char >= 0)
337 c = unread_char;
338 unread_char = -1;
339 return c;
341 c = (*readbyte) (-1, readcharfun);
342 if (c < 0)
343 return c;
344 if (multibyte)
345 *multibyte = 1;
346 if (ASCII_CHAR_P (c))
347 return c;
348 if (emacs_mule_encoding)
349 return read_emacs_mule_char (c, readbyte, readcharfun);
350 i = 0;
351 buf[i++] = c;
352 len = BYTES_BY_CHAR_HEAD (c);
353 while (i < len)
355 buf[i++] = c = (*readbyte) (-1, readcharfun);
356 if (c < 0 || ! TRAILING_CODE_P (c))
358 for (i -= c < 0; 0 < --i; )
359 (*readbyte) (buf[i], readcharfun);
360 return BYTE8_TO_CHAR (buf[0]);
363 return STRING_CHAR (buf);
366 #define FROM_FILE_P(readcharfun) \
367 (EQ (readcharfun, Qget_file_char) \
368 || EQ (readcharfun, Qget_emacs_mule_file_char))
370 static void
371 skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
373 if (FROM_FILE_P (readcharfun))
375 block_input (); /* FIXME: Not sure if it's needed. */
376 fseek (infile->stream, n - infile->lookahead, SEEK_CUR);
377 unblock_input ();
378 infile->lookahead = 0;
380 else
381 { /* We're not reading directly from a file. In that case, it's difficult
382 to reliably count bytes, since these are usually meant for the file's
383 encoding, whereas we're now typically in the internal encoding.
384 But luckily, skip_dyn_bytes is used to skip over a single
385 dynamic-docstring (or dynamic byte-code) which is always quoted such
386 that \037 is the final char. */
387 int c;
388 do {
389 c = READCHAR;
390 } while (c >= 0 && c != '\037');
394 static void
395 skip_dyn_eof (Lisp_Object readcharfun)
397 if (FROM_FILE_P (readcharfun))
399 block_input (); /* FIXME: Not sure if it's needed. */
400 fseek (infile->stream, 0, SEEK_END);
401 unblock_input ();
402 infile->lookahead = 0;
404 else
405 while (READCHAR >= 0);
408 /* Unread the character C in the way appropriate for the stream READCHARFUN.
409 If the stream is a user function, call it with the char as argument. */
411 static void
412 unreadchar (Lisp_Object readcharfun, int c)
414 readchar_count--;
415 if (c == -1)
416 /* Don't back up the pointer if we're unreading the end-of-input mark,
417 since readchar didn't advance it when we read it. */
419 else if (BUFFERP (readcharfun))
421 struct buffer *b = XBUFFER (readcharfun);
422 ptrdiff_t charpos = BUF_PT (b);
423 ptrdiff_t bytepos = BUF_PT_BYTE (b);
425 if (! NILP (BVAR (b, enable_multibyte_characters)))
426 BUF_DEC_POS (b, bytepos);
427 else
428 bytepos--;
430 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
432 else if (MARKERP (readcharfun))
434 struct buffer *b = XMARKER (readcharfun)->buffer;
435 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
437 XMARKER (readcharfun)->charpos--;
438 if (! NILP (BVAR (b, enable_multibyte_characters)))
439 BUF_DEC_POS (b, bytepos);
440 else
441 bytepos--;
443 XMARKER (readcharfun)->bytepos = bytepos;
445 else if (STRINGP (readcharfun))
447 read_from_string_index--;
448 read_from_string_index_byte
449 = string_char_to_byte (readcharfun, read_from_string_index);
451 else if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
453 unread_char = c;
455 else if (EQ (readcharfun, Qlambda))
457 unread_char = c;
459 else if (FROM_FILE_P (readcharfun))
461 unread_char = c;
463 else
464 call1 (readcharfun, make_number (c));
467 static int
468 readbyte_for_lambda (int c, Lisp_Object readcharfun)
470 return read_bytecode_char (c >= 0);
474 static int
475 readbyte_from_stdio (void)
477 if (infile->lookahead)
478 return infile->buf[--infile->lookahead];
480 int c;
481 FILE *instream = infile->stream;
483 block_input ();
485 /* Interrupted reads have been observed while reading over the network. */
486 while ((c = getc_unlocked (instream)) == EOF && errno == EINTR
487 && ferror_unlocked (instream))
489 unblock_input ();
490 maybe_quit ();
491 block_input ();
492 clearerr_unlocked (instream);
495 unblock_input ();
497 return (c == EOF ? -1 : c);
500 static int
501 readbyte_from_file (int c, Lisp_Object readcharfun)
503 if (c >= 0)
505 eassert (infile->lookahead < sizeof infile->buf);
506 infile->buf[infile->lookahead++] = c;
507 return 0;
510 return readbyte_from_stdio ();
513 static int
514 readbyte_from_string (int c, Lisp_Object readcharfun)
516 Lisp_Object string = XCAR (readcharfun);
518 if (c >= 0)
520 read_from_string_index--;
521 read_from_string_index_byte
522 = string_char_to_byte (string, read_from_string_index);
525 if (read_from_string_index >= read_from_string_limit)
526 c = -1;
527 else
528 FETCH_STRING_CHAR_ADVANCE (c, string,
529 read_from_string_index,
530 read_from_string_index_byte);
531 return c;
535 /* Read one non-ASCII character from INFILE. The character is
536 encoded in `emacs-mule' and the first byte is already read in
537 C. */
539 static int
540 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
542 /* Emacs-mule coding uses at most 4-byte for one character. */
543 unsigned char buf[4];
544 int len = emacs_mule_bytes[c];
545 struct charset *charset;
546 int i;
547 unsigned code;
549 if (len == 1)
550 /* C is not a valid leading-code of `emacs-mule'. */
551 return BYTE8_TO_CHAR (c);
553 i = 0;
554 buf[i++] = c;
555 while (i < len)
557 buf[i++] = c = (*readbyte) (-1, readcharfun);
558 if (c < 0xA0)
560 for (i -= c < 0; 0 < --i; )
561 (*readbyte) (buf[i], readcharfun);
562 return BYTE8_TO_CHAR (buf[0]);
566 if (len == 2)
568 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
569 code = buf[1] & 0x7F;
571 else if (len == 3)
573 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
574 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
576 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
577 code = buf[2] & 0x7F;
579 else
581 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
582 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
585 else
587 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
588 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
590 c = DECODE_CHAR (charset, code);
591 if (c < 0)
592 Fsignal (Qinvalid_read_syntax,
593 list1 (build_string ("invalid multibyte form")));
594 return c;
598 /* An in-progress substitution of OBJECT for PLACEHOLDER. */
599 struct subst
601 Lisp_Object object;
602 Lisp_Object placeholder;
604 /* Hash table of subobjects of OBJECT that might be circular. If
605 Qt, all such objects might be circular. */
606 Lisp_Object completed;
608 /* List of subobjects of OBJECT that have already been visited. */
609 Lisp_Object seen;
612 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
613 Lisp_Object);
614 static Lisp_Object read0 (Lisp_Object);
615 static Lisp_Object read1 (Lisp_Object, int *, bool);
617 static Lisp_Object read_list (bool, Lisp_Object);
618 static Lisp_Object read_vector (Lisp_Object, bool);
620 static Lisp_Object substitute_object_recurse (struct subst *, Lisp_Object);
621 static void substitute_in_interval (INTERVAL, void *);
624 /* Get a character from the tty. */
626 /* Read input events until we get one that's acceptable for our purposes.
628 If NO_SWITCH_FRAME, switch-frame events are stashed
629 until we get a character we like, and then stuffed into
630 unread_switch_frame.
632 If ASCII_REQUIRED, check function key events to see
633 if the unmodified version of the symbol has a Qascii_character
634 property, and use that character, if present.
636 If ERROR_NONASCII, signal an error if the input we
637 get isn't an ASCII character with modifiers. If it's false but
638 ASCII_REQUIRED is true, just re-read until we get an ASCII
639 character.
641 If INPUT_METHOD, invoke the current input method
642 if the character warrants that.
644 If SECONDS is a number, wait that many seconds for input, and
645 return Qnil if no input arrives within that time. */
647 static Lisp_Object
648 read_filtered_event (bool no_switch_frame, bool ascii_required,
649 bool error_nonascii, bool input_method, Lisp_Object seconds)
651 Lisp_Object val, delayed_switch_frame;
652 struct timespec end_time;
654 #ifdef HAVE_WINDOW_SYSTEM
655 if (display_hourglass_p)
656 cancel_hourglass ();
657 #endif
659 delayed_switch_frame = Qnil;
661 /* Compute timeout. */
662 if (NUMBERP (seconds))
664 double duration = XFLOATINT (seconds);
665 struct timespec wait_time = dtotimespec (duration);
666 end_time = timespec_add (current_timespec (), wait_time);
669 /* Read until we get an acceptable event. */
670 retry:
672 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
673 NUMBERP (seconds) ? &end_time : NULL);
674 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
676 if (BUFFERP (val))
677 goto retry;
679 /* `switch-frame' events are put off until after the next ASCII
680 character. This is better than signaling an error just because
681 the last characters were typed to a separate minibuffer frame,
682 for example. Eventually, some code which can deal with
683 switch-frame events will read it and process it. */
684 if (no_switch_frame
685 && EVENT_HAS_PARAMETERS (val)
686 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
688 delayed_switch_frame = val;
689 goto retry;
692 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
694 /* Convert certain symbols to their ASCII equivalents. */
695 if (SYMBOLP (val))
697 Lisp_Object tem, tem1;
698 tem = Fget (val, Qevent_symbol_element_mask);
699 if (!NILP (tem))
701 tem1 = Fget (Fcar (tem), Qascii_character);
702 /* Merge this symbol's modifier bits
703 with the ASCII equivalent of its basic code. */
704 if (!NILP (tem1))
705 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
709 /* If we don't have a character now, deal with it appropriately. */
710 if (!INTEGERP (val))
712 if (error_nonascii)
714 Vunread_command_events = list1 (val);
715 error ("Non-character input-event");
717 else
718 goto retry;
722 if (! NILP (delayed_switch_frame))
723 unread_switch_frame = delayed_switch_frame;
725 #if 0
727 #ifdef HAVE_WINDOW_SYSTEM
728 if (display_hourglass_p)
729 start_hourglass ();
730 #endif
732 #endif
734 return val;
737 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
738 doc: /* Read a character from the command input (keyboard or macro).
739 It is returned as a number.
740 If the character has modifiers, they are resolved and reflected to the
741 character code if possible (e.g. C-SPC -> 0).
743 If the user generates an event which is not a character (i.e. a mouse
744 click or function key event), `read-char' signals an error. As an
745 exception, switch-frame events are put off until non-character events
746 can be read.
747 If you want to read non-character events, or ignore them, call
748 `read-event' or `read-char-exclusive' instead.
750 If the optional argument PROMPT is non-nil, display that as a prompt.
751 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
752 input method is turned on in the current buffer, that input method
753 is used for reading a character.
754 If the optional argument SECONDS is non-nil, it should be a number
755 specifying the maximum number of seconds to wait for input. If no
756 input arrives in that time, return nil. SECONDS may be a
757 floating-point value. */)
758 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
760 Lisp_Object val;
762 if (! NILP (prompt))
763 message_with_string ("%s", prompt, 0);
764 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
766 return (NILP (val) ? Qnil
767 : make_number (char_resolve_modifier_mask (XINT (val))));
770 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
771 doc: /* Read an event object from the input stream.
772 If the optional argument PROMPT is non-nil, display that as a prompt.
773 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
774 input method is turned on in the current buffer, that input method
775 is used for reading a character.
776 If the optional argument SECONDS is non-nil, it should be a number
777 specifying the maximum number of seconds to wait for input. If no
778 input arrives in that time, return nil. SECONDS may be a
779 floating-point value. */)
780 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
782 if (! NILP (prompt))
783 message_with_string ("%s", prompt, 0);
784 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
787 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
788 doc: /* Read a character from the command input (keyboard or macro).
789 It is returned as a number. Non-character events are ignored.
790 If the character has modifiers, they are resolved and reflected to the
791 character code if possible (e.g. C-SPC -> 0).
793 If the optional argument PROMPT is non-nil, display that as a prompt.
794 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
795 input method is turned on in the current buffer, that input method
796 is used for reading a character.
797 If the optional argument SECONDS is non-nil, it should be a number
798 specifying the maximum number of seconds to wait for input. If no
799 input arrives in that time, return nil. SECONDS may be a
800 floating-point value. */)
801 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
803 Lisp_Object val;
805 if (! NILP (prompt))
806 message_with_string ("%s", prompt, 0);
808 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
810 return (NILP (val) ? Qnil
811 : make_number (char_resolve_modifier_mask (XINT (val))));
814 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
815 doc: /* Don't use this yourself. */)
816 (void)
818 if (!infile)
819 error ("get-file-char misused");
820 return make_number (readbyte_from_stdio ());
826 /* Return true if the lisp code read using READCHARFUN defines a non-nil
827 `lexical-binding' file variable. After returning, the stream is
828 positioned following the first line, if it is a comment or #! line,
829 otherwise nothing is read. */
831 static bool
832 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
834 int ch = READCHAR;
836 if (ch == '#')
838 ch = READCHAR;
839 if (ch != '!')
841 UNREAD (ch);
842 UNREAD ('#');
843 return 0;
845 while (ch != '\n' && ch != EOF)
846 ch = READCHAR;
847 if (ch == '\n') ch = READCHAR;
848 /* It is OK to leave the position after a #! line, since
849 that is what read1 does. */
852 if (ch != ';')
853 /* The first line isn't a comment, just give up. */
855 UNREAD (ch);
856 return 0;
858 else
859 /* Look for an appropriate file-variable in the first line. */
861 bool rv = 0;
862 enum {
863 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX
864 } beg_end_state = NOMINAL;
865 bool in_file_vars = 0;
867 #define UPDATE_BEG_END_STATE(ch) \
868 if (beg_end_state == NOMINAL) \
869 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
870 else if (beg_end_state == AFTER_FIRST_DASH) \
871 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
872 else if (beg_end_state == AFTER_ASTERIX) \
874 if (ch == '-') \
875 in_file_vars = !in_file_vars; \
876 beg_end_state = NOMINAL; \
879 /* Skip until we get to the file vars, if any. */
882 ch = READCHAR;
883 UPDATE_BEG_END_STATE (ch);
885 while (!in_file_vars && ch != '\n' && ch != EOF);
887 while (in_file_vars)
889 char var[100], val[100];
890 unsigned i;
892 ch = READCHAR;
894 /* Read a variable name. */
895 while (ch == ' ' || ch == '\t')
896 ch = READCHAR;
898 i = 0;
899 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
901 if (i < sizeof var - 1)
902 var[i++] = ch;
903 UPDATE_BEG_END_STATE (ch);
904 ch = READCHAR;
907 /* Stop scanning if no colon was found before end marker. */
908 if (!in_file_vars || ch == '\n' || ch == EOF)
909 break;
911 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
912 i--;
913 var[i] = '\0';
915 if (ch == ':')
917 /* Read a variable value. */
918 ch = READCHAR;
920 while (ch == ' ' || ch == '\t')
921 ch = READCHAR;
923 i = 0;
924 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
926 if (i < sizeof val - 1)
927 val[i++] = ch;
928 UPDATE_BEG_END_STATE (ch);
929 ch = READCHAR;
931 if (! in_file_vars)
932 /* The value was terminated by an end-marker, which remove. */
933 i -= 3;
934 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
935 i--;
936 val[i] = '\0';
938 if (strcmp (var, "lexical-binding") == 0)
939 /* This is it... */
941 rv = (strcmp (val, "nil") != 0);
942 break;
947 while (ch != '\n' && ch != EOF)
948 ch = READCHAR;
950 return rv;
954 /* Value is a version number of byte compiled code if the file
955 associated with file descriptor FD is a compiled Lisp file that's
956 safe to load. Only files compiled with Emacs are safe to load.
957 Files compiled with XEmacs can lead to a crash in Fbyte_code
958 because of an incompatible change in the byte compiler. */
960 static int
961 safe_to_load_version (int fd)
963 char buf[512];
964 int nbytes, i;
965 int version = 1;
967 /* Read the first few bytes from the file, and look for a line
968 specifying the byte compiler version used. */
969 nbytes = emacs_read_quit (fd, buf, sizeof buf);
970 if (nbytes > 0)
972 /* Skip to the next newline, skipping over the initial `ELC'
973 with NUL bytes following it, but note the version. */
974 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
975 if (i == 4)
976 version = buf[i];
978 if (i >= nbytes
979 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
980 buf + i, nbytes - i) < 0)
981 version = 0;
984 lseek (fd, 0, SEEK_SET);
985 return version;
989 /* Callback for record_unwind_protect. Restore the old load list OLD,
990 after loading a file successfully. */
992 static void
993 record_load_unwind (Lisp_Object old)
995 Vloads_in_progress = old;
998 /* This handler function is used via internal_condition_case_1. */
1000 static Lisp_Object
1001 load_error_handler (Lisp_Object data)
1003 return Qnil;
1006 static void
1007 load_warn_old_style_backquotes (Lisp_Object file)
1009 if (!NILP (Vlread_old_style_backquotes))
1011 AUTO_STRING (format, "Loading `%s': old-style backquotes detected!");
1012 CALLN (Fmessage, format, file);
1016 static void
1017 load_warn_unescaped_character_literals (Lisp_Object file)
1019 if (NILP (Vlread_unescaped_character_literals)) return;
1020 CHECK_CONS (Vlread_unescaped_character_literals);
1021 Lisp_Object format =
1022 build_string ("Loading `%s': unescaped character literals %s detected!");
1023 Lisp_Object separator = build_string (", ");
1024 Lisp_Object inner_format = build_string ("`?%c'");
1025 CALLN (Fmessage,
1026 format, file,
1027 Fmapconcat (list3 (Qlambda, list1 (Qchar),
1028 list3 (Qformat, inner_format, Qchar)),
1029 Fsort (Vlread_unescaped_character_literals, Qlss),
1030 separator));
1033 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
1034 doc: /* Return the suffixes that `load' should try if a suffix is \
1035 required.
1036 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
1037 (void)
1039 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
1040 while (CONSP (suffixes))
1042 Lisp_Object exts = Vload_file_rep_suffixes;
1043 suffix = XCAR (suffixes);
1044 suffixes = XCDR (suffixes);
1045 while (CONSP (exts))
1047 ext = XCAR (exts);
1048 exts = XCDR (exts);
1049 lst = Fcons (concat2 (suffix, ext), lst);
1052 return Fnreverse (lst);
1055 /* Returns true if STRING ends with SUFFIX */
1056 static bool
1057 suffix_p (Lisp_Object string, const char *suffix)
1059 ptrdiff_t suffix_len = strlen (suffix);
1060 ptrdiff_t string_len = SBYTES (string);
1062 return string_len >= suffix_len && !strcmp (SSDATA (string) + string_len - suffix_len, suffix);
1065 static void
1066 close_infile_unwind (void *arg)
1068 FILE *stream = arg;
1069 eassert (infile == NULL || infile->stream == stream);
1070 infile = NULL;
1071 fclose (stream);
1074 DEFUN ("load", Fload, Sload, 1, 5, 0,
1075 doc: /* Execute a file of Lisp code named FILE.
1076 First try FILE with `.elc' appended, then try with `.el', then try
1077 with a system-dependent suffix of dynamic modules (see `load-suffixes'),
1078 then try FILE unmodified (the exact suffixes in the exact order are
1079 determined by `load-suffixes'). Environment variable references in
1080 FILE are replaced with their values by calling `substitute-in-file-name'.
1081 This function searches the directories in `load-path'.
1083 If optional second arg NOERROR is non-nil,
1084 report no error if FILE doesn't exist.
1085 Print messages at start and end of loading unless
1086 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
1087 overrides that).
1088 If optional fourth arg NOSUFFIX is non-nil, don't try adding
1089 suffixes to the specified name FILE.
1090 If optional fifth arg MUST-SUFFIX is non-nil, insist on
1091 the suffix `.elc' or `.el' or the module suffix; don't accept just
1092 FILE unless it ends in one of those suffixes or includes a directory name.
1094 If NOSUFFIX is nil, then if a file could not be found, try looking for
1095 a different representation of the file by adding non-empty suffixes to
1096 its name, before trying another file. Emacs uses this feature to find
1097 compressed versions of files when Auto Compression mode is enabled.
1098 If NOSUFFIX is non-nil, disable this feature.
1100 The suffixes that this function tries out, when NOSUFFIX is nil, are
1101 given by the return value of `get-load-suffixes' and the values listed
1102 in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1103 return value of `get-load-suffixes' is used, i.e. the file name is
1104 required to have a non-empty suffix.
1106 When searching suffixes, this function normally stops at the first
1107 one that exists. If the option `load-prefer-newer' is non-nil,
1108 however, it tries all suffixes, and uses whichever file is the newest.
1110 Loading a file records its definitions, and its `provide' and
1111 `require' calls, in an element of `load-history' whose
1112 car is the file name loaded. See `load-history'.
1114 While the file is in the process of being loaded, the variable
1115 `load-in-progress' is non-nil and the variable `load-file-name'
1116 is bound to the file's name.
1118 Return t if the file exists and loads successfully. */)
1119 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage,
1120 Lisp_Object nosuffix, Lisp_Object must_suffix)
1122 FILE *stream;
1123 int fd;
1124 int fd_index UNINIT;
1125 ptrdiff_t count = SPECPDL_INDEX ();
1126 Lisp_Object found, efound, hist_file_name;
1127 /* True means we printed the ".el is newer" message. */
1128 bool newer = 0;
1129 /* True means we are loading a compiled file. */
1130 bool compiled = 0;
1131 Lisp_Object handler;
1132 bool safe_p = 1;
1133 const char *fmode = "r" FOPEN_TEXT;
1134 int version;
1136 CHECK_STRING (file);
1138 /* If file name is magic, call the handler. */
1139 /* This shouldn't be necessary any more now that `openp' handles it right.
1140 handler = Ffind_file_name_handler (file, Qload);
1141 if (!NILP (handler))
1142 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1144 /* The presence of this call is the result of a historical accident:
1145 it used to be in every file-operation and when it got removed
1146 everywhere, it accidentally stayed here. Since then, enough people
1147 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1148 that it seemed risky to remove. */
1149 if (! NILP (noerror))
1151 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1152 Qt, load_error_handler);
1153 if (NILP (file))
1154 return Qnil;
1156 else
1157 file = Fsubstitute_in_file_name (file);
1159 /* Avoid weird lossage with null string as arg,
1160 since it would try to load a directory as a Lisp file. */
1161 if (SCHARS (file) == 0)
1163 fd = -1;
1164 errno = ENOENT;
1166 else
1168 Lisp_Object suffixes;
1169 found = Qnil;
1171 if (! NILP (must_suffix))
1173 /* Don't insist on adding a suffix if FILE already ends with one. */
1174 if (suffix_p (file, ".el")
1175 || suffix_p (file, ".elc")
1176 #ifdef HAVE_MODULES
1177 || suffix_p (file, MODULES_SUFFIX)
1178 #endif
1180 must_suffix = Qnil;
1181 /* Don't insist on adding a suffix
1182 if the argument includes a directory name. */
1183 else if (! NILP (Ffile_name_directory (file)))
1184 must_suffix = Qnil;
1187 if (!NILP (nosuffix))
1188 suffixes = Qnil;
1189 else
1191 suffixes = Fget_load_suffixes ();
1192 if (NILP (must_suffix))
1193 suffixes = CALLN (Fappend, suffixes, Vload_file_rep_suffixes);
1196 fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
1199 if (fd == -1)
1201 if (NILP (noerror))
1202 report_file_error ("Cannot open load file", file);
1203 return Qnil;
1206 /* Tell startup.el whether or not we found the user's init file. */
1207 if (EQ (Qt, Vuser_init_file))
1208 Vuser_init_file = found;
1210 /* If FD is -2, that means openp found a magic file. */
1211 if (fd == -2)
1213 if (NILP (Fequal (found, file)))
1214 /* If FOUND is a different file name from FILE,
1215 find its handler even if we have already inhibited
1216 the `load' operation on FILE. */
1217 handler = Ffind_file_name_handler (found, Qt);
1218 else
1219 handler = Ffind_file_name_handler (found, Qload);
1220 if (! NILP (handler))
1221 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1222 #ifdef DOS_NT
1223 /* Tramp has to deal with semi-broken packages that prepend
1224 drive letters to remote files. For that reason, Tramp
1225 catches file operations that test for file existence, which
1226 makes openp think X:/foo.elc files are remote. However,
1227 Tramp does not catch `load' operations for such files, so we
1228 end up with a nil as the `load' handler above. If we would
1229 continue with fd = -2, we will behave wrongly, and in
1230 particular try reading a .elc file in the "rt" mode instead
1231 of "rb". See bug #9311 for the results. To work around
1232 this, we try to open the file locally, and go with that if it
1233 succeeds. */
1234 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1235 if (fd == -1)
1236 fd = -2;
1237 #endif
1240 if (0 <= fd)
1242 fd_index = SPECPDL_INDEX ();
1243 record_unwind_protect_int (close_file_unwind, fd);
1246 #ifdef HAVE_MODULES
1247 if (suffix_p (found, MODULES_SUFFIX))
1248 return unbind_to (count, Fmodule_load (found));
1249 #endif
1251 /* Check if we're stuck in a recursive load cycle.
1253 2000-09-21: It's not possible to just check for the file loaded
1254 being a member of Vloads_in_progress. This fails because of the
1255 way the byte compiler currently works; `provide's are not
1256 evaluated, see font-lock.el/jit-lock.el as an example. This
1257 leads to a certain amount of ``normal'' recursion.
1259 Also, just loading a file recursively is not always an error in
1260 the general case; the second load may do something different. */
1262 int load_count = 0;
1263 Lisp_Object tem;
1264 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1265 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1266 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1267 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1268 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1271 /* All loads are by default dynamic, unless the file itself specifies
1272 otherwise using a file-variable in the first line. This is bound here
1273 so that it takes effect whether or not we use
1274 Vload_source_file_function. */
1275 specbind (Qlexical_binding, Qnil);
1277 /* Get the name for load-history. */
1278 hist_file_name = (! NILP (Vpurify_flag)
1279 ? concat2 (Ffile_name_directory (file),
1280 Ffile_name_nondirectory (found))
1281 : found) ;
1283 version = -1;
1285 /* Check for the presence of old-style quotes and warn about them. */
1286 specbind (Qlread_old_style_backquotes, Qnil);
1287 record_unwind_protect (load_warn_old_style_backquotes, file);
1289 /* Check for the presence of unescaped character literals and warn
1290 about them. */
1291 specbind (Qlread_unescaped_character_literals, Qnil);
1292 record_unwind_protect (load_warn_unescaped_character_literals, file);
1294 int is_elc;
1295 if ((is_elc = suffix_p (found, ".elc")) != 0
1296 /* version = 1 means the file is empty, in which case we can
1297 treat it as not byte-compiled. */
1298 || (fd >= 0 && (version = safe_to_load_version (fd)) > 1))
1299 /* Load .elc files directly, but not when they are
1300 remote and have no handler! */
1302 if (fd != -2)
1304 struct stat s1, s2;
1305 int result;
1307 if (version < 0
1308 && ! (version = safe_to_load_version (fd)))
1310 safe_p = 0;
1311 if (!load_dangerous_libraries)
1312 error ("File `%s' was not compiled in Emacs", SDATA (found));
1313 else if (!NILP (nomessage) && !force_load_messages)
1314 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1317 compiled = 1;
1319 efound = ENCODE_FILE (found);
1320 fmode = "r" FOPEN_BINARY;
1322 /* openp already checked for newness, no point doing it again.
1323 FIXME would be nice to get a message when openp
1324 ignores suffix order due to load_prefer_newer. */
1325 if (!load_prefer_newer && is_elc)
1327 result = stat (SSDATA (efound), &s1);
1328 if (result == 0)
1330 SSET (efound, SBYTES (efound) - 1, 0);
1331 result = stat (SSDATA (efound), &s2);
1332 SSET (efound, SBYTES (efound) - 1, 'c');
1335 if (result == 0
1336 && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
1338 /* Make the progress messages mention that source is newer. */
1339 newer = 1;
1341 /* If we won't print another message, mention this anyway. */
1342 if (!NILP (nomessage) && !force_load_messages)
1344 Lisp_Object msg_file;
1345 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1346 message_with_string ("Source file `%s' newer than byte-compiled file",
1347 msg_file, 1);
1350 } /* !load_prefer_newer */
1353 else
1355 /* We are loading a source file (*.el). */
1356 if (!NILP (Vload_source_file_function))
1358 Lisp_Object val;
1360 if (fd >= 0)
1362 emacs_close (fd);
1363 clear_unwind_protect (fd_index);
1365 val = call4 (Vload_source_file_function, found, hist_file_name,
1366 NILP (noerror) ? Qnil : Qt,
1367 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1368 return unbind_to (count, val);
1372 if (fd < 0)
1374 /* We somehow got here with fd == -2, meaning the file is deemed
1375 to be remote. Don't even try to reopen the file locally;
1376 just force a failure. */
1377 stream = NULL;
1378 errno = EINVAL;
1380 else
1382 #ifdef WINDOWSNT
1383 emacs_close (fd);
1384 clear_unwind_protect (fd_index);
1385 efound = ENCODE_FILE (found);
1386 stream = emacs_fopen (SSDATA (efound), fmode);
1387 #else
1388 stream = fdopen (fd, fmode);
1389 #endif
1391 if (! stream)
1392 report_file_error ("Opening stdio stream", file);
1393 set_unwind_protect_ptr (fd_index, close_infile_unwind, stream);
1395 if (! NILP (Vpurify_flag))
1396 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1398 if (NILP (nomessage) || force_load_messages)
1400 if (!safe_p)
1401 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1402 file, 1);
1403 else if (!compiled)
1404 message_with_string ("Loading %s (source)...", file, 1);
1405 else if (newer)
1406 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1407 file, 1);
1408 else /* The typical case; compiled file newer than source file. */
1409 message_with_string ("Loading %s...", file, 1);
1412 specbind (Qload_file_name, found);
1413 specbind (Qinhibit_file_name_operation, Qnil);
1414 specbind (Qload_in_progress, Qt);
1416 struct infile input;
1417 input.stream = stream;
1418 input.lookahead = 0;
1419 infile = &input;
1421 if (lisp_file_lexically_bound_p (Qget_file_char))
1422 Fset (Qlexical_binding, Qt);
1424 if (! version || version >= 22)
1425 readevalloop (Qget_file_char, &input, hist_file_name,
1426 0, Qnil, Qnil, Qnil, Qnil);
1427 else
1429 /* We can't handle a file which was compiled with
1430 byte-compile-dynamic by older version of Emacs. */
1431 specbind (Qload_force_doc_strings, Qt);
1432 readevalloop (Qget_emacs_mule_file_char, &input, hist_file_name,
1433 0, Qnil, Qnil, Qnil, Qnil);
1435 unbind_to (count, Qnil);
1437 /* Run any eval-after-load forms for this file. */
1438 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1439 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1441 xfree (saved_doc_string);
1442 saved_doc_string = 0;
1443 saved_doc_string_size = 0;
1445 xfree (prev_saved_doc_string);
1446 prev_saved_doc_string = 0;
1447 prev_saved_doc_string_size = 0;
1449 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1451 if (!safe_p)
1452 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1453 file, 1);
1454 else if (!compiled)
1455 message_with_string ("Loading %s (source)...done", file, 1);
1456 else if (newer)
1457 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1458 file, 1);
1459 else /* The typical case; compiled file newer than source file. */
1460 message_with_string ("Loading %s...done", file, 1);
1463 return Qt;
1466 static bool
1467 complete_filename_p (Lisp_Object pathname)
1469 const unsigned char *s = SDATA (pathname);
1470 return (IS_DIRECTORY_SEP (s[0])
1471 || (SCHARS (pathname) > 2
1472 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1475 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1476 doc: /* Search for FILENAME through PATH.
1477 Returns the file's name in absolute form, or nil if not found.
1478 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1479 file name when searching.
1480 If non-nil, PREDICATE is used instead of `file-readable-p'.
1481 PREDICATE can also be an integer to pass to the faccessat(2) function,
1482 in which case file-name-handlers are ignored.
1483 This function will normally skip directories, so if you want it to find
1484 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1485 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1487 Lisp_Object file;
1488 int fd = openp (path, filename, suffixes, &file, predicate, false);
1489 if (NILP (predicate) && fd >= 0)
1490 emacs_close (fd);
1491 return file;
1494 /* Search for a file whose name is STR, looking in directories
1495 in the Lisp list PATH, and trying suffixes from SUFFIX.
1496 On success, return a file descriptor (or 1 or -2 as described below).
1497 On failure, return -1 and set errno.
1499 SUFFIXES is a list of strings containing possible suffixes.
1500 The empty suffix is automatically added if the list is empty.
1502 PREDICATE t means the files are binary.
1503 PREDICATE non-nil and non-t means don't open the files,
1504 just look for one that satisfies the predicate. In this case,
1505 return -2 on success. The predicate can be a lisp function or
1506 an integer to pass to `access' (in which case file-name-handlers
1507 are ignored).
1509 If STOREPTR is nonzero, it points to a slot where the name of
1510 the file actually found should be stored as a Lisp string.
1511 nil is stored there on failure.
1513 If the file we find is remote, return -2
1514 but store the found remote file name in *STOREPTR.
1516 If NEWER is true, try all SUFFIXes and return the result for the
1517 newest file that exists. Does not apply to remote files,
1518 or if a non-nil and non-t PREDICATE is specified. */
1521 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
1522 Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
1524 ptrdiff_t fn_size = 100;
1525 char buf[100];
1526 char *fn = buf;
1527 bool absolute;
1528 ptrdiff_t want_length;
1529 Lisp_Object filename;
1530 Lisp_Object string, tail, encoded_fn, save_string;
1531 ptrdiff_t max_suffix_len = 0;
1532 int last_errno = ENOENT;
1533 int save_fd = -1;
1534 USE_SAFE_ALLOCA;
1536 /* The last-modified time of the newest matching file found.
1537 Initialize it to something less than all valid timestamps. */
1538 struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
1540 CHECK_STRING (str);
1542 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1544 CHECK_STRING_CAR (tail);
1545 max_suffix_len = max (max_suffix_len,
1546 SBYTES (XCAR (tail)));
1549 string = filename = encoded_fn = save_string = Qnil;
1551 if (storeptr)
1552 *storeptr = Qnil;
1554 absolute = complete_filename_p (str);
1556 for (; CONSP (path); path = XCDR (path))
1558 ptrdiff_t baselen, prefixlen;
1560 filename = Fexpand_file_name (str, XCAR (path));
1561 if (!complete_filename_p (filename))
1562 /* If there are non-absolute elts in PATH (eg "."). */
1563 /* Of course, this could conceivably lose if luser sets
1564 default-directory to be something non-absolute... */
1566 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1567 if (!complete_filename_p (filename))
1568 /* Give up on this path element! */
1569 continue;
1572 /* Calculate maximum length of any filename made from
1573 this path element/specified file name and any possible suffix. */
1574 want_length = max_suffix_len + SBYTES (filename);
1575 if (fn_size <= want_length)
1577 fn_size = 100 + want_length;
1578 fn = SAFE_ALLOCA (fn_size);
1581 /* Copy FILENAME's data to FN but remove starting /: if any. */
1582 prefixlen = ((SCHARS (filename) > 2
1583 && SREF (filename, 0) == '/'
1584 && SREF (filename, 1) == ':')
1585 ? 2 : 0);
1586 baselen = SBYTES (filename) - prefixlen;
1587 memcpy (fn, SDATA (filename) + prefixlen, baselen);
1589 /* Loop over suffixes. */
1590 for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
1591 CONSP (tail); tail = XCDR (tail))
1593 Lisp_Object suffix = XCAR (tail);
1594 ptrdiff_t fnlen, lsuffix = SBYTES (suffix);
1595 Lisp_Object handler;
1597 /* Make complete filename by appending SUFFIX. */
1598 memcpy (fn + baselen, SDATA (suffix), lsuffix + 1);
1599 fnlen = baselen + lsuffix;
1601 /* Check that the file exists and is not a directory. */
1602 /* We used to only check for handlers on non-absolute file names:
1603 if (absolute)
1604 handler = Qnil;
1605 else
1606 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1607 It's not clear why that was the case and it breaks things like
1608 (load "/bar.el") where the file is actually "/bar.el.gz". */
1609 /* make_string has its own ideas on when to return a unibyte
1610 string and when a multibyte string, but we know better.
1611 We must have a unibyte string when dumping, since
1612 file-name encoding is shaky at best at that time, and in
1613 particular default-file-name-coding-system is reset
1614 several times during loadup. We therefore don't want to
1615 encode the file before passing it to file I/O library
1616 functions. */
1617 if (!STRING_MULTIBYTE (filename) && !STRING_MULTIBYTE (suffix))
1618 string = make_unibyte_string (fn, fnlen);
1619 else
1620 string = make_string (fn, fnlen);
1621 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1622 if ((!NILP (handler) || (!NILP (predicate) && !EQ (predicate, Qt)))
1623 && !NATNUMP (predicate))
1625 bool exists;
1626 if (NILP (predicate) || EQ (predicate, Qt))
1627 exists = !NILP (Ffile_readable_p (string));
1628 else
1630 Lisp_Object tmp = call1 (predicate, string);
1631 if (NILP (tmp))
1632 exists = false;
1633 else if (EQ (tmp, Qdir_ok)
1634 || NILP (Ffile_directory_p (string)))
1635 exists = true;
1636 else
1638 exists = false;
1639 last_errno = EISDIR;
1643 if (exists)
1645 /* We succeeded; return this descriptor and filename. */
1646 if (storeptr)
1647 *storeptr = string;
1648 SAFE_FREE ();
1649 return -2;
1652 else
1654 int fd;
1655 const char *pfn;
1656 struct stat st;
1658 encoded_fn = ENCODE_FILE (string);
1659 pfn = SSDATA (encoded_fn);
1661 /* Check that we can access or open it. */
1662 if (NATNUMP (predicate))
1664 fd = -1;
1665 if (INT_MAX < XFASTINT (predicate))
1666 last_errno = EINVAL;
1667 else if (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
1668 AT_EACCESS)
1669 == 0)
1671 if (file_directory_p (pfn))
1672 last_errno = EISDIR;
1673 else
1674 fd = 1;
1677 else
1679 fd = emacs_open (pfn, O_RDONLY, 0);
1680 if (fd < 0)
1682 if (errno != ENOENT)
1683 last_errno = errno;
1685 else
1687 int err = (fstat (fd, &st) != 0 ? errno
1688 : S_ISDIR (st.st_mode) ? EISDIR : 0);
1689 if (err)
1691 last_errno = err;
1692 emacs_close (fd);
1693 fd = -1;
1698 if (fd >= 0)
1700 if (newer && !NATNUMP (predicate))
1702 struct timespec mtime = get_stat_mtime (&st);
1704 if (timespec_cmp (mtime, save_mtime) <= 0)
1705 emacs_close (fd);
1706 else
1708 if (0 <= save_fd)
1709 emacs_close (save_fd);
1710 save_fd = fd;
1711 save_mtime = mtime;
1712 save_string = string;
1715 else
1717 /* We succeeded; return this descriptor and filename. */
1718 if (storeptr)
1719 *storeptr = string;
1720 SAFE_FREE ();
1721 return fd;
1725 /* No more suffixes. Return the newest. */
1726 if (0 <= save_fd && ! CONSP (XCDR (tail)))
1728 if (storeptr)
1729 *storeptr = save_string;
1730 SAFE_FREE ();
1731 return save_fd;
1735 if (absolute)
1736 break;
1739 SAFE_FREE ();
1740 errno = last_errno;
1741 return -1;
1745 /* Merge the list we've accumulated of globals from the current input source
1746 into the load_history variable. The details depend on whether
1747 the source has an associated file name or not.
1749 FILENAME is the file name that we are loading from.
1751 ENTIRE is true if loading that entire file, false if evaluating
1752 part of it. */
1754 static void
1755 build_load_history (Lisp_Object filename, bool entire)
1757 Lisp_Object tail, prev, newelt;
1758 Lisp_Object tem, tem2;
1759 bool foundit = 0;
1761 tail = Vload_history;
1762 prev = Qnil;
1764 while (CONSP (tail))
1766 tem = XCAR (tail);
1768 /* Find the feature's previous assoc list... */
1769 if (!NILP (Fequal (filename, Fcar (tem))))
1771 foundit = 1;
1773 /* If we're loading the entire file, remove old data. */
1774 if (entire)
1776 if (NILP (prev))
1777 Vload_history = XCDR (tail);
1778 else
1779 Fsetcdr (prev, XCDR (tail));
1782 /* Otherwise, cons on new symbols that are not already members. */
1783 else
1785 tem2 = Vcurrent_load_list;
1787 while (CONSP (tem2))
1789 newelt = XCAR (tem2);
1791 if (NILP (Fmember (newelt, tem)))
1792 Fsetcar (tail, Fcons (XCAR (tem),
1793 Fcons (newelt, XCDR (tem))));
1795 tem2 = XCDR (tem2);
1796 maybe_quit ();
1800 else
1801 prev = tail;
1802 tail = XCDR (tail);
1803 maybe_quit ();
1806 /* If we're loading an entire file, cons the new assoc onto the
1807 front of load-history, the most-recently-loaded position. Also
1808 do this if we didn't find an existing member for the file. */
1809 if (entire || !foundit)
1810 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1811 Vload_history);
1814 static void
1815 readevalloop_1 (int old)
1817 load_convert_to_unibyte = old;
1820 /* Signal an `end-of-file' error, if possible with file name
1821 information. */
1823 static _Noreturn void
1824 end_of_file_error (void)
1826 if (STRINGP (Vload_file_name))
1827 xsignal1 (Qend_of_file, Vload_file_name);
1829 xsignal0 (Qend_of_file);
1832 static Lisp_Object
1833 readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
1835 /* If we macroexpand the toplevel form non-recursively and it ends
1836 up being a `progn' (or if it was a progn to start), treat each
1837 form in the progn as a top-level form. This way, if one form in
1838 the progn defines a macro, that macro is in effect when we expand
1839 the remaining forms. See similar code in bytecomp.el. */
1840 val = call2 (macroexpand, val, Qnil);
1841 if (EQ (CAR_SAFE (val), Qprogn))
1843 Lisp_Object subforms = XCDR (val);
1845 for (val = Qnil; CONSP (subforms); subforms = XCDR (subforms))
1846 val = readevalloop_eager_expand_eval (XCAR (subforms),
1847 macroexpand);
1849 else
1850 val = eval_sub (call2 (macroexpand, val, Qt));
1851 return val;
1854 /* UNIBYTE specifies how to set load_convert_to_unibyte
1855 for this invocation.
1856 READFUN, if non-nil, is used instead of `read'.
1858 START, END specify region to read in current buffer (from eval-region).
1859 If the input is not from a buffer, they must be nil. */
1861 static void
1862 readevalloop (Lisp_Object readcharfun,
1863 struct infile *infile0,
1864 Lisp_Object sourcename,
1865 bool printflag,
1866 Lisp_Object unibyte, Lisp_Object readfun,
1867 Lisp_Object start, Lisp_Object end)
1869 int c;
1870 Lisp_Object val;
1871 ptrdiff_t count = SPECPDL_INDEX ();
1872 struct buffer *b = 0;
1873 bool continue_reading_p;
1874 Lisp_Object lex_bound;
1875 /* True if reading an entire buffer. */
1876 bool whole_buffer = 0;
1877 /* True on the first time around. */
1878 bool first_sexp = 1;
1879 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1881 if (NILP (Ffboundp (macroexpand))
1882 /* Don't macroexpand in .elc files, since it should have been done
1883 already. We actually don't know whether we're in a .elc file or not,
1884 so we use circumstantial evidence: .el files normally go through
1885 Vload_source_file_function -> load-with-code-conversion
1886 -> eval-buffer. */
1887 || EQ (readcharfun, Qget_file_char)
1888 || EQ (readcharfun, Qget_emacs_mule_file_char))
1889 macroexpand = Qnil;
1891 if (MARKERP (readcharfun))
1893 if (NILP (start))
1894 start = readcharfun;
1897 if (BUFFERP (readcharfun))
1898 b = XBUFFER (readcharfun);
1899 else if (MARKERP (readcharfun))
1900 b = XMARKER (readcharfun)->buffer;
1902 /* We assume START is nil when input is not from a buffer. */
1903 if (! NILP (start) && !b)
1904 emacs_abort ();
1906 specbind (Qstandard_input, readcharfun);
1907 specbind (Qcurrent_load_list, Qnil);
1908 record_unwind_protect_int (readevalloop_1, load_convert_to_unibyte);
1909 load_convert_to_unibyte = !NILP (unibyte);
1911 /* If lexical binding is active (either because it was specified in
1912 the file's header, or via a buffer-local variable), create an empty
1913 lexical environment, otherwise, turn off lexical binding. */
1914 lex_bound = find_symbol_value (Qlexical_binding);
1915 specbind (Qinternal_interpreter_environment,
1916 (NILP (lex_bound) || EQ (lex_bound, Qunbound)
1917 ? Qnil : list1 (Qt)));
1919 /* Try to ensure sourcename is a truename, except whilst preloading. */
1920 if (NILP (Vpurify_flag)
1921 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1922 && !NILP (Ffboundp (Qfile_truename)))
1923 sourcename = call1 (Qfile_truename, sourcename) ;
1925 LOADHIST_ATTACH (sourcename);
1927 continue_reading_p = 1;
1928 while (continue_reading_p)
1930 ptrdiff_t count1 = SPECPDL_INDEX ();
1932 if (b != 0 && !BUFFER_LIVE_P (b))
1933 error ("Reading from killed buffer");
1935 if (!NILP (start))
1937 /* Switch to the buffer we are reading from. */
1938 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1939 set_buffer_internal (b);
1941 /* Save point in it. */
1942 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1943 /* Save ZV in it. */
1944 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1945 /* Those get unbound after we read one expression. */
1947 /* Set point and ZV around stuff to be read. */
1948 Fgoto_char (start);
1949 if (!NILP (end))
1950 Fnarrow_to_region (make_number (BEGV), end);
1952 /* Just for cleanliness, convert END to a marker
1953 if it is an integer. */
1954 if (INTEGERP (end))
1955 end = Fpoint_max_marker ();
1958 /* On the first cycle, we can easily test here
1959 whether we are reading the whole buffer. */
1960 if (b && first_sexp)
1961 whole_buffer = (BUF_PT (b) == BUF_BEG (b) && BUF_ZV (b) == BUF_Z (b));
1963 infile = infile0;
1964 read_next:
1965 c = READCHAR;
1966 if (c == ';')
1968 while ((c = READCHAR) != '\n' && c != -1);
1969 goto read_next;
1971 if (c < 0)
1973 unbind_to (count1, Qnil);
1974 break;
1977 /* Ignore whitespace here, so we can detect eof. */
1978 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1979 || c == NO_BREAK_SPACE)
1980 goto read_next;
1982 if (! HASH_TABLE_P (read_objects_map)
1983 || XHASH_TABLE (read_objects_map)->count)
1984 read_objects_map
1985 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE,
1986 DEFAULT_REHASH_SIZE, DEFAULT_REHASH_THRESHOLD,
1987 Qnil, false);
1988 if (! HASH_TABLE_P (read_objects_completed)
1989 || XHASH_TABLE (read_objects_completed)->count)
1990 read_objects_completed
1991 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE,
1992 DEFAULT_REHASH_SIZE, DEFAULT_REHASH_THRESHOLD,
1993 Qnil, false);
1994 if (!NILP (Vpurify_flag) && c == '(')
1996 val = read_list (0, readcharfun);
1998 else
2000 UNREAD (c);
2001 if (!NILP (readfun))
2003 val = call1 (readfun, readcharfun);
2005 /* If READCHARFUN has set point to ZV, we should
2006 stop reading, even if the form read sets point
2007 to a different value when evaluated. */
2008 if (BUFFERP (readcharfun))
2010 struct buffer *buf = XBUFFER (readcharfun);
2011 if (BUF_PT (buf) == BUF_ZV (buf))
2012 continue_reading_p = 0;
2015 else if (! NILP (Vload_read_function))
2016 val = call1 (Vload_read_function, readcharfun);
2017 else
2018 val = read_internal_start (readcharfun, Qnil, Qnil);
2020 /* Empty hashes can be reused; otherwise, reset on next call. */
2021 if (HASH_TABLE_P (read_objects_map)
2022 && XHASH_TABLE (read_objects_map)->count > 0)
2023 read_objects_map = Qnil;
2024 if (HASH_TABLE_P (read_objects_completed)
2025 && XHASH_TABLE (read_objects_completed)->count > 0)
2026 read_objects_completed = Qnil;
2028 if (!NILP (start) && continue_reading_p)
2029 start = Fpoint_marker ();
2031 /* Restore saved point and BEGV. */
2032 unbind_to (count1, Qnil);
2034 /* Now eval what we just read. */
2035 if (!NILP (macroexpand))
2036 val = readevalloop_eager_expand_eval (val, macroexpand);
2037 else
2038 val = eval_sub (val);
2040 if (printflag)
2042 Vvalues = Fcons (val, Vvalues);
2043 if (EQ (Vstandard_output, Qt))
2044 Fprin1 (val, Qnil);
2045 else
2046 Fprint (val, Qnil);
2049 first_sexp = 0;
2052 build_load_history (sourcename,
2053 infile0 || whole_buffer);
2055 unbind_to (count, Qnil);
2058 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
2059 doc: /* Execute the accessible portion of current buffer as Lisp code.
2060 You can use \\[narrow-to-region] to limit the part of buffer to be evaluated.
2061 When called from a Lisp program (i.e., not interactively), this
2062 function accepts up to five optional arguments:
2063 BUFFER is the buffer to evaluate (nil means use current buffer),
2064 or a name of a buffer (a string).
2065 PRINTFLAG controls printing of output by any output functions in the
2066 evaluated code, such as `print', `princ', and `prin1':
2067 a value of nil means discard it; anything else is the stream to print to.
2068 See Info node `(elisp)Output Streams' for details on streams.
2069 FILENAME specifies the file name to use for `load-history'.
2070 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
2071 invocation.
2072 DO-ALLOW-PRINT, if non-nil, specifies that output functions in the
2073 evaluated code should work normally even if PRINTFLAG is nil, in
2074 which case the output is displayed in the echo area.
2076 This function preserves the position of point. */)
2077 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
2079 ptrdiff_t count = SPECPDL_INDEX ();
2080 Lisp_Object tem, buf;
2082 if (NILP (buffer))
2083 buf = Fcurrent_buffer ();
2084 else
2085 buf = Fget_buffer (buffer);
2086 if (NILP (buf))
2087 error ("No such buffer");
2089 if (NILP (printflag) && NILP (do_allow_print))
2090 tem = Qsymbolp;
2091 else
2092 tem = printflag;
2094 if (NILP (filename))
2095 filename = BVAR (XBUFFER (buf), filename);
2097 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
2098 specbind (Qstandard_output, tem);
2099 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2100 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
2101 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
2102 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
2103 readevalloop (buf, 0, filename,
2104 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
2105 unbind_to (count, Qnil);
2107 return Qnil;
2110 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
2111 doc: /* Execute the region as Lisp code.
2112 When called from programs, expects two arguments,
2113 giving starting and ending indices in the current buffer
2114 of the text to be executed.
2115 Programs can pass third argument PRINTFLAG which controls output:
2116 a value of nil means discard it; anything else is stream for printing it.
2117 See Info node `(elisp)Output Streams' for details on streams.
2118 Also the fourth argument READ-FUNCTION, if non-nil, is used
2119 instead of `read' to read each expression. It gets one argument
2120 which is the input stream for reading characters.
2122 This function does not move point. */)
2123 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
2125 /* FIXME: Do the eval-sexp-add-defvars dance! */
2126 ptrdiff_t count = SPECPDL_INDEX ();
2127 Lisp_Object tem, cbuf;
2129 cbuf = Fcurrent_buffer ();
2131 if (NILP (printflag))
2132 tem = Qsymbolp;
2133 else
2134 tem = printflag;
2135 specbind (Qstandard_output, tem);
2136 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
2138 /* `readevalloop' calls functions which check the type of start and end. */
2139 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
2140 !NILP (printflag), Qnil, read_function,
2141 start, end);
2143 return unbind_to (count, Qnil);
2147 DEFUN ("read", Fread, Sread, 0, 1, 0,
2148 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
2149 If STREAM is nil, use the value of `standard-input' (which see).
2150 STREAM or the value of `standard-input' may be:
2151 a buffer (read from point and advance it)
2152 a marker (read from where it points and advance it)
2153 a function (call it with no arguments for each character,
2154 call it with a char as argument to push a char back)
2155 a string (takes text from string, starting at the beginning)
2156 t (read text line using minibuffer and use it, or read from
2157 standard input in batch mode). */)
2158 (Lisp_Object stream)
2160 if (NILP (stream))
2161 stream = Vstandard_input;
2162 if (EQ (stream, Qt))
2163 stream = Qread_char;
2164 if (EQ (stream, Qread_char))
2165 /* FIXME: ?! When is this used !? */
2166 return call1 (intern ("read-minibuffer"),
2167 build_string ("Lisp expression: "));
2169 return read_internal_start (stream, Qnil, Qnil);
2172 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
2173 doc: /* Read one Lisp expression which is represented as text by STRING.
2174 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
2175 FINAL-STRING-INDEX is an integer giving the position of the next
2176 remaining character in STRING. START and END optionally delimit
2177 a substring of STRING from which to read; they default to 0 and
2178 \(length STRING) respectively. Negative values are counted from
2179 the end of STRING. */)
2180 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
2182 Lisp_Object ret;
2183 CHECK_STRING (string);
2184 /* `read_internal_start' sets `read_from_string_index'. */
2185 ret = read_internal_start (string, start, end);
2186 return Fcons (ret, make_number (read_from_string_index));
2189 /* Function to set up the global context we need in toplevel read
2190 calls. START and END only used when STREAM is a string. */
2191 static Lisp_Object
2192 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
2194 Lisp_Object retval;
2196 readchar_count = 0;
2197 new_backquote_flag = 0;
2198 /* We can get called from readevalloop which may have set these
2199 already. */
2200 if (! HASH_TABLE_P (read_objects_map)
2201 || XHASH_TABLE (read_objects_map)->count)
2202 read_objects_map
2203 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE, DEFAULT_REHASH_SIZE,
2204 DEFAULT_REHASH_THRESHOLD, Qnil, false);
2205 if (! HASH_TABLE_P (read_objects_completed)
2206 || XHASH_TABLE (read_objects_completed)->count)
2207 read_objects_completed
2208 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE, DEFAULT_REHASH_SIZE,
2209 DEFAULT_REHASH_THRESHOLD, Qnil, false);
2210 if (EQ (Vread_with_symbol_positions, Qt)
2211 || EQ (Vread_with_symbol_positions, stream))
2212 Vread_symbol_positions_list = Qnil;
2214 if (STRINGP (stream)
2215 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2217 ptrdiff_t startval, endval;
2218 Lisp_Object string;
2220 if (STRINGP (stream))
2221 string = stream;
2222 else
2223 string = XCAR (stream);
2225 validate_subarray (string, start, end, SCHARS (string),
2226 &startval, &endval);
2228 read_from_string_index = startval;
2229 read_from_string_index_byte = string_char_to_byte (string, startval);
2230 read_from_string_limit = endval;
2233 retval = read0 (stream);
2234 if (EQ (Vread_with_symbol_positions, Qt)
2235 || EQ (Vread_with_symbol_positions, stream))
2236 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2237 /* Empty hashes can be reused; otherwise, reset on next call. */
2238 if (HASH_TABLE_P (read_objects_map)
2239 && XHASH_TABLE (read_objects_map)->count > 0)
2240 read_objects_map = Qnil;
2241 if (HASH_TABLE_P (read_objects_completed)
2242 && XHASH_TABLE (read_objects_completed)->count > 0)
2243 read_objects_completed = Qnil;
2244 return retval;
2248 /* Signal Qinvalid_read_syntax error.
2249 S is error string of length N (if > 0) */
2251 static _Noreturn void
2252 invalid_syntax (const char *s)
2254 xsignal1 (Qinvalid_read_syntax, build_string (s));
2258 /* Use this for recursive reads, in contexts where internal tokens
2259 are not allowed. */
2261 static Lisp_Object
2262 read0 (Lisp_Object readcharfun)
2264 register Lisp_Object val;
2265 int c;
2267 val = read1 (readcharfun, &c, 0);
2268 if (!c)
2269 return val;
2271 xsignal1 (Qinvalid_read_syntax,
2272 Fmake_string (make_number (1), make_number (c)));
2275 /* Grow a read buffer BUF that contains OFFSET useful bytes of data,
2276 by at least MAX_MULTIBYTE_LENGTH bytes. Update *BUF_ADDR and
2277 *BUF_SIZE accordingly; 0 <= OFFSET <= *BUF_SIZE. If *BUF_ADDR is
2278 initially null, BUF is on the stack: copy its data to the new heap
2279 buffer. Otherwise, BUF must equal *BUF_ADDR and can simply be
2280 reallocated. Either way, remember the heap allocation (which is at
2281 pdl slot COUNT) so that it can be freed when unwinding the stack.*/
2283 static char *
2284 grow_read_buffer (char *buf, ptrdiff_t offset,
2285 char **buf_addr, ptrdiff_t *buf_size, ptrdiff_t count)
2287 char *p = xpalloc (*buf_addr, buf_size, MAX_MULTIBYTE_LENGTH, -1, 1);
2288 if (!*buf_addr)
2290 memcpy (p, buf, offset);
2291 record_unwind_protect_ptr (xfree, p);
2293 else
2294 set_unwind_protect_ptr (count, xfree, p);
2295 *buf_addr = p;
2296 return p;
2299 /* Return the scalar value that has the Unicode character name NAME.
2300 Raise 'invalid-read-syntax' if there is no such character. */
2301 static int
2302 character_name_to_code (char const *name, ptrdiff_t name_len)
2304 /* For "U+XXXX", pass the leading '+' to string_to_number to reject
2305 monstrosities like "U+-0000". */
2306 Lisp_Object code
2307 = (name[0] == 'U' && name[1] == '+'
2308 ? string_to_number (name + 1, 16, false)
2309 : call2 (Qchar_from_name, make_unibyte_string (name, name_len), Qt));
2311 if (! RANGED_INTEGERP (0, code, MAX_UNICODE_CHAR)
2312 || char_surrogate_p (XINT (code)))
2314 AUTO_STRING (format, "\\N{%s}");
2315 AUTO_STRING_WITH_LEN (namestr, name, name_len);
2316 xsignal1 (Qinvalid_read_syntax, CALLN (Fformat, format, namestr));
2319 return XINT (code);
2322 /* Bound on the length of a Unicode character name. As of
2323 Unicode 9.0.0 the maximum is 83, so this should be safe. */
2324 enum { UNICODE_CHARACTER_NAME_LENGTH_BOUND = 200 };
2326 /* Read a \-escape sequence, assuming we already read the `\'.
2327 If the escape sequence forces unibyte, return eight-bit char. */
2329 static int
2330 read_escape (Lisp_Object readcharfun, bool stringp)
2332 int c = READCHAR;
2333 /* \u allows up to four hex digits, \U up to eight. Default to the
2334 behavior for \u, and change this value in the case that \U is seen. */
2335 int unicode_hex_count = 4;
2337 switch (c)
2339 case -1:
2340 end_of_file_error ();
2342 case 'a':
2343 return '\007';
2344 case 'b':
2345 return '\b';
2346 case 'd':
2347 return 0177;
2348 case 'e':
2349 return 033;
2350 case 'f':
2351 return '\f';
2352 case 'n':
2353 return '\n';
2354 case 'r':
2355 return '\r';
2356 case 't':
2357 return '\t';
2358 case 'v':
2359 return '\v';
2360 case '\n':
2361 return -1;
2362 case ' ':
2363 if (stringp)
2364 return -1;
2365 return ' ';
2367 case 'M':
2368 c = READCHAR;
2369 if (c != '-')
2370 error ("Invalid escape character syntax");
2371 c = READCHAR;
2372 if (c == '\\')
2373 c = read_escape (readcharfun, 0);
2374 return c | meta_modifier;
2376 case 'S':
2377 c = READCHAR;
2378 if (c != '-')
2379 error ("Invalid escape character syntax");
2380 c = READCHAR;
2381 if (c == '\\')
2382 c = read_escape (readcharfun, 0);
2383 return c | shift_modifier;
2385 case 'H':
2386 c = READCHAR;
2387 if (c != '-')
2388 error ("Invalid escape character syntax");
2389 c = READCHAR;
2390 if (c == '\\')
2391 c = read_escape (readcharfun, 0);
2392 return c | hyper_modifier;
2394 case 'A':
2395 c = READCHAR;
2396 if (c != '-')
2397 error ("Invalid escape character syntax");
2398 c = READCHAR;
2399 if (c == '\\')
2400 c = read_escape (readcharfun, 0);
2401 return c | alt_modifier;
2403 case 's':
2404 c = READCHAR;
2405 if (stringp || c != '-')
2407 UNREAD (c);
2408 return ' ';
2410 c = READCHAR;
2411 if (c == '\\')
2412 c = read_escape (readcharfun, 0);
2413 return c | super_modifier;
2415 case 'C':
2416 c = READCHAR;
2417 if (c != '-')
2418 error ("Invalid escape character syntax");
2419 FALLTHROUGH;
2420 case '^':
2421 c = READCHAR;
2422 if (c == '\\')
2423 c = read_escape (readcharfun, 0);
2424 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2425 return 0177 | (c & CHAR_MODIFIER_MASK);
2426 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2427 return c | ctrl_modifier;
2428 /* ASCII control chars are made from letters (both cases),
2429 as well as the non-letters within 0100...0137. */
2430 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2431 return (c & (037 | ~0177));
2432 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2433 return (c & (037 | ~0177));
2434 else
2435 return c | ctrl_modifier;
2437 case '0':
2438 case '1':
2439 case '2':
2440 case '3':
2441 case '4':
2442 case '5':
2443 case '6':
2444 case '7':
2445 /* An octal escape, as in ANSI C. */
2447 register int i = c - '0';
2448 register int count = 0;
2449 while (++count < 3)
2451 if ((c = READCHAR) >= '0' && c <= '7')
2453 i *= 8;
2454 i += c - '0';
2456 else
2458 UNREAD (c);
2459 break;
2463 if (i >= 0x80 && i < 0x100)
2464 i = BYTE8_TO_CHAR (i);
2465 return i;
2468 case 'x':
2469 /* A hex escape, as in ANSI C. */
2471 unsigned int i = 0;
2472 int count = 0;
2473 while (1)
2475 c = READCHAR;
2476 int digit = char_hexdigit (c);
2477 if (digit < 0)
2479 UNREAD (c);
2480 break;
2482 i = (i << 4) + digit;
2483 /* Allow hex escapes as large as ?\xfffffff, because some
2484 packages use them to denote characters with modifiers. */
2485 if ((CHAR_META | (CHAR_META - 1)) < i)
2486 error ("Hex character out of range: \\x%x...", i);
2487 count += count < 3;
2490 if (count < 3 && i >= 0x80)
2491 return BYTE8_TO_CHAR (i);
2492 return i;
2495 case 'U':
2496 /* Post-Unicode-2.0: Up to eight hex chars. */
2497 unicode_hex_count = 8;
2498 FALLTHROUGH;
2499 case 'u':
2501 /* A Unicode escape. We only permit them in strings and characters,
2502 not arbitrarily in the source code, as in some other languages. */
2504 unsigned int i = 0;
2505 int count = 0;
2507 while (++count <= unicode_hex_count)
2509 c = READCHAR;
2510 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2511 want. */
2512 int digit = char_hexdigit (c);
2513 if (digit < 0)
2514 error ("Non-hex digit used for Unicode escape");
2515 i = (i << 4) + digit;
2517 if (i > 0x10FFFF)
2518 error ("Non-Unicode character: 0x%x", i);
2519 return i;
2522 case 'N':
2523 /* Named character. */
2525 c = READCHAR;
2526 if (c != '{')
2527 invalid_syntax ("Expected opening brace after \\N");
2528 char name[UNICODE_CHARACTER_NAME_LENGTH_BOUND + 1];
2529 bool whitespace = false;
2530 ptrdiff_t length = 0;
2531 while (true)
2533 c = READCHAR;
2534 if (c < 0)
2535 end_of_file_error ();
2536 if (c == '}')
2537 break;
2538 if (! (0 < c && c < 0x80))
2540 AUTO_STRING (format,
2541 "Invalid character U+%04X in character name");
2542 xsignal1 (Qinvalid_read_syntax,
2543 CALLN (Fformat, format, make_natnum (c)));
2545 /* Treat multiple adjacent whitespace characters as a
2546 single space character. This makes it easier to use
2547 character names in e.g. multi-line strings. */
2548 if (c_isspace (c))
2550 if (whitespace)
2551 continue;
2552 c = ' ';
2553 whitespace = true;
2555 else
2556 whitespace = false;
2557 name[length++] = c;
2558 if (length >= sizeof name)
2559 invalid_syntax ("Character name too long");
2561 if (length == 0)
2562 invalid_syntax ("Empty character name");
2563 name[length] = '\0';
2565 /* character_name_to_code can invoke read1, recursively.
2566 This is why read1's buffer is not static. */
2567 return character_name_to_code (name, length);
2570 default:
2571 return c;
2575 /* Return the digit that CHARACTER stands for in the given BASE.
2576 Return -1 if CHARACTER is out of range for BASE,
2577 and -2 if CHARACTER is not valid for any supported BASE. */
2578 static int
2579 digit_to_number (int character, int base)
2581 int digit;
2583 if ('0' <= character && character <= '9')
2584 digit = character - '0';
2585 else if ('a' <= character && character <= 'z')
2586 digit = character - 'a' + 10;
2587 else if ('A' <= character && character <= 'Z')
2588 digit = character - 'A' + 10;
2589 else
2590 return -2;
2592 return digit < base ? digit : -1;
2595 /* Read an integer in radix RADIX using READCHARFUN to read
2596 characters. RADIX must be in the interval [2..36]; if it isn't, a
2597 read error is signaled . Value is the integer read. Signals an
2598 error if encountering invalid read syntax or if RADIX is out of
2599 range. */
2601 static Lisp_Object
2602 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2604 /* Room for sign, leading 0, other digits, trailing null byte.
2605 Also, room for invalid syntax diagnostic. */
2606 char buf[max (1 + 1 + UINTMAX_WIDTH + 1,
2607 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2609 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2611 if (radix < 2 || radix > 36)
2612 valid = 0;
2613 else
2615 char *p = buf;
2616 int c, digit;
2618 c = READCHAR;
2619 if (c == '-' || c == '+')
2621 *p++ = c;
2622 c = READCHAR;
2625 if (c == '0')
2627 *p++ = c;
2628 valid = 1;
2630 /* Ignore redundant leading zeros, so the buffer doesn't
2631 fill up with them. */
2633 c = READCHAR;
2634 while (c == '0');
2637 while ((digit = digit_to_number (c, radix)) >= -1)
2639 if (digit == -1)
2640 valid = 0;
2641 if (valid < 0)
2642 valid = 1;
2644 if (p < buf + sizeof buf - 1)
2645 *p++ = c;
2646 else
2647 valid = 0;
2649 c = READCHAR;
2652 UNREAD (c);
2653 *p = '\0';
2656 if (valid != 1)
2658 sprintf (buf, "integer, radix %"pI"d", radix);
2659 invalid_syntax (buf);
2662 return string_to_number (buf, radix, 0);
2666 /* If the next token is ')' or ']' or '.', we store that character
2667 in *PCH and the return value is not interesting. Else, we store
2668 zero in *PCH and we read and return one lisp object.
2670 FIRST_IN_LIST is true if this is the first element of a list. */
2672 static Lisp_Object
2673 read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
2675 int c;
2676 bool uninterned_symbol = false;
2677 bool multibyte;
2678 char stackbuf[MAX_ALLOCA];
2679 current_thread->stack_top = stackbuf;
2681 *pch = 0;
2683 retry:
2685 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2686 if (c < 0)
2687 end_of_file_error ();
2689 switch (c)
2691 case '(':
2692 return read_list (0, readcharfun);
2694 case '[':
2695 return read_vector (readcharfun, 0);
2697 case ')':
2698 case ']':
2700 *pch = c;
2701 return Qnil;
2704 case '#':
2705 c = READCHAR;
2706 if (c == 's')
2708 c = READCHAR;
2709 if (c == '(')
2711 /* Accept extended format for hash tables (extensible to
2712 other types), e.g.
2713 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2714 Lisp_Object tmp = read_list (0, readcharfun);
2715 Lisp_Object head = CAR_SAFE (tmp);
2716 Lisp_Object data = Qnil;
2717 Lisp_Object val = Qnil;
2718 /* The size is 2 * number of allowed keywords to
2719 make-hash-table. */
2720 Lisp_Object params[12];
2721 Lisp_Object ht;
2722 Lisp_Object key = Qnil;
2723 int param_count = 0;
2725 if (!EQ (head, Qhash_table))
2727 ptrdiff_t size = XINT (Flength (tmp));
2728 Lisp_Object record = Fmake_record (CAR_SAFE (tmp),
2729 make_number (size - 1),
2730 Qnil);
2731 for (int i = 1; i < size; i++)
2733 tmp = Fcdr (tmp);
2734 ASET (record, i, Fcar (tmp));
2736 return record;
2739 tmp = CDR_SAFE (tmp);
2741 /* This is repetitive but fast and simple. */
2742 params[param_count] = QCsize;
2743 params[param_count + 1] = Fplist_get (tmp, Qsize);
2744 if (!NILP (params[param_count + 1]))
2745 param_count += 2;
2747 params[param_count] = QCtest;
2748 params[param_count + 1] = Fplist_get (tmp, Qtest);
2749 if (!NILP (params[param_count + 1]))
2750 param_count += 2;
2752 params[param_count] = QCweakness;
2753 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2754 if (!NILP (params[param_count + 1]))
2755 param_count += 2;
2757 params[param_count] = QCrehash_size;
2758 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2759 if (!NILP (params[param_count + 1]))
2760 param_count += 2;
2762 params[param_count] = QCrehash_threshold;
2763 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2764 if (!NILP (params[param_count + 1]))
2765 param_count += 2;
2767 params[param_count] = QCpurecopy;
2768 params[param_count + 1] = Fplist_get (tmp, Qpurecopy);
2769 if (!NILP (params[param_count + 1]))
2770 param_count += 2;
2772 /* This is the hash table data. */
2773 data = Fplist_get (tmp, Qdata);
2775 /* Now use params to make a new hash table and fill it. */
2776 ht = Fmake_hash_table (param_count, params);
2778 while (CONSP (data))
2780 key = XCAR (data);
2781 data = XCDR (data);
2782 if (!CONSP (data))
2783 error ("Odd number of elements in hash table data");
2784 val = XCAR (data);
2785 data = XCDR (data);
2786 Fputhash (key, val, ht);
2789 return ht;
2791 UNREAD (c);
2792 invalid_syntax ("#");
2794 if (c == '^')
2796 c = READCHAR;
2797 if (c == '[')
2799 Lisp_Object tmp;
2800 tmp = read_vector (readcharfun, 0);
2801 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2802 error ("Invalid size char-table");
2803 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2804 return tmp;
2806 else if (c == '^')
2808 c = READCHAR;
2809 if (c == '[')
2811 /* Sub char-table can't be read as a regular
2812 vector because of a two C integer fields. */
2813 Lisp_Object tbl, tmp = read_list (1, readcharfun);
2814 ptrdiff_t size = XINT (Flength (tmp));
2815 int i, depth, min_char;
2816 struct Lisp_Cons *cell;
2818 if (size == 0)
2819 error ("Zero-sized sub char-table");
2821 if (! RANGED_INTEGERP (1, XCAR (tmp), 3))
2822 error ("Invalid depth in sub char-table");
2823 depth = XINT (XCAR (tmp));
2824 if (chartab_size[depth] != size - 2)
2825 error ("Invalid size in sub char-table");
2826 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2827 free_cons (cell);
2829 if (! RANGED_INTEGERP (0, XCAR (tmp), MAX_CHAR))
2830 error ("Invalid minimum character in sub-char-table");
2831 min_char = XINT (XCAR (tmp));
2832 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2833 free_cons (cell);
2835 tbl = make_uninit_sub_char_table (depth, min_char);
2836 for (i = 0; i < size; i++)
2838 XSUB_CHAR_TABLE (tbl)->contents[i] = XCAR (tmp);
2839 cell = XCONS (tmp), tmp = XCDR (tmp);
2840 free_cons (cell);
2842 return tbl;
2844 invalid_syntax ("#^^");
2846 invalid_syntax ("#^");
2848 if (c == '&')
2850 Lisp_Object length;
2851 length = read1 (readcharfun, pch, first_in_list);
2852 c = READCHAR;
2853 if (c == '"')
2855 Lisp_Object tmp, val;
2856 EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
2857 unsigned char *data;
2859 UNREAD (c);
2860 tmp = read1 (readcharfun, pch, first_in_list);
2861 if (STRING_MULTIBYTE (tmp)
2862 || (size_in_chars != SCHARS (tmp)
2863 /* We used to print 1 char too many
2864 when the number of bits was a multiple of 8.
2865 Accept such input in case it came from an old
2866 version. */
2867 && ! (XFASTINT (length)
2868 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2869 invalid_syntax ("#&...");
2871 val = make_uninit_bool_vector (XFASTINT (length));
2872 data = bool_vector_uchar_data (val);
2873 memcpy (data, SDATA (tmp), size_in_chars);
2874 /* Clear the extraneous bits in the last byte. */
2875 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2876 data[size_in_chars - 1]
2877 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2878 return val;
2880 invalid_syntax ("#&...");
2882 if (c == '[')
2884 /* Accept compiled functions at read-time so that we don't have to
2885 build them using function calls. */
2886 Lisp_Object tmp;
2887 struct Lisp_Vector *vec;
2888 tmp = read_vector (readcharfun, 1);
2889 vec = XVECTOR (tmp);
2890 if (vec->header.size == 0)
2891 invalid_syntax ("Empty byte-code object");
2892 make_byte_code (vec);
2893 return tmp;
2895 if (c == '(')
2897 Lisp_Object tmp;
2898 int ch;
2900 /* Read the string itself. */
2901 tmp = read1 (readcharfun, &ch, 0);
2902 if (ch != 0 || !STRINGP (tmp))
2903 invalid_syntax ("#");
2904 /* Read the intervals and their properties. */
2905 while (1)
2907 Lisp_Object beg, end, plist;
2909 beg = read1 (readcharfun, &ch, 0);
2910 end = plist = Qnil;
2911 if (ch == ')')
2912 break;
2913 if (ch == 0)
2914 end = read1 (readcharfun, &ch, 0);
2915 if (ch == 0)
2916 plist = read1 (readcharfun, &ch, 0);
2917 if (ch)
2918 invalid_syntax ("Invalid string property list");
2919 Fset_text_properties (beg, end, plist, tmp);
2922 return tmp;
2925 /* #@NUMBER is used to skip NUMBER following bytes.
2926 That's used in .elc files to skip over doc strings
2927 and function definitions. */
2928 if (c == '@')
2930 enum { extra = 100 };
2931 ptrdiff_t i, nskip = 0, digits = 0;
2933 /* Read a decimal integer. */
2934 while ((c = READCHAR) >= 0
2935 && c >= '0' && c <= '9')
2937 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2938 string_overflow ();
2939 digits++;
2940 nskip *= 10;
2941 nskip += c - '0';
2942 if (digits == 2 && nskip == 0)
2943 { /* We've just seen #@00, which means "skip to end". */
2944 skip_dyn_eof (readcharfun);
2945 return Qnil;
2948 if (nskip > 0)
2949 /* We can't use UNREAD here, because in the code below we side-step
2950 READCHAR. Instead, assume the first char after #@NNN occupies
2951 a single byte, which is the case normally since it's just
2952 a space. */
2953 nskip--;
2954 else
2955 UNREAD (c);
2957 if (load_force_doc_strings
2958 && (FROM_FILE_P (readcharfun)))
2960 /* If we are supposed to force doc strings into core right now,
2961 record the last string that we skipped,
2962 and record where in the file it comes from. */
2964 /* But first exchange saved_doc_string
2965 with prev_saved_doc_string, so we save two strings. */
2967 char *temp = saved_doc_string;
2968 ptrdiff_t temp_size = saved_doc_string_size;
2969 file_offset temp_pos = saved_doc_string_position;
2970 ptrdiff_t temp_len = saved_doc_string_length;
2972 saved_doc_string = prev_saved_doc_string;
2973 saved_doc_string_size = prev_saved_doc_string_size;
2974 saved_doc_string_position = prev_saved_doc_string_position;
2975 saved_doc_string_length = prev_saved_doc_string_length;
2977 prev_saved_doc_string = temp;
2978 prev_saved_doc_string_size = temp_size;
2979 prev_saved_doc_string_position = temp_pos;
2980 prev_saved_doc_string_length = temp_len;
2983 if (saved_doc_string_size == 0)
2985 saved_doc_string = xmalloc (nskip + extra);
2986 saved_doc_string_size = nskip + extra;
2988 if (nskip > saved_doc_string_size)
2990 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
2991 saved_doc_string_size = nskip + extra;
2994 FILE *instream = infile->stream;
2995 saved_doc_string_position = (file_tell (instream)
2996 - infile->lookahead);
2998 /* Copy that many bytes into saved_doc_string. */
2999 i = 0;
3000 for (int n = min (nskip, infile->lookahead); 0 < n; n--)
3001 saved_doc_string[i++]
3002 = c = infile->buf[--infile->lookahead];
3003 block_input ();
3004 for (; i < nskip && 0 <= c; i++)
3005 saved_doc_string[i] = c = getc_unlocked (instream);
3006 unblock_input ();
3008 saved_doc_string_length = i;
3010 else
3011 /* Skip that many bytes. */
3012 skip_dyn_bytes (readcharfun, nskip);
3014 goto retry;
3016 if (c == '!')
3018 /* #! appears at the beginning of an executable file.
3019 Skip the first line. */
3020 while (c != '\n' && c >= 0)
3021 c = READCHAR;
3022 goto retry;
3024 if (c == '$')
3025 return Vload_file_name;
3026 if (c == '\'')
3027 return list2 (Qfunction, read0 (readcharfun));
3028 /* #:foo is the uninterned symbol named foo. */
3029 if (c == ':')
3031 uninterned_symbol = true;
3032 c = READCHAR;
3033 if (!(c > 040
3034 && c != NO_BREAK_SPACE
3035 && (c >= 0200
3036 || strchr ("\"';()[]#`,", c) == NULL)))
3038 /* No symbol character follows, this is the empty
3039 symbol. */
3040 UNREAD (c);
3041 return Fmake_symbol (empty_unibyte_string);
3043 goto read_symbol;
3045 /* ## is the empty symbol. */
3046 if (c == '#')
3047 return Fintern (empty_unibyte_string, Qnil);
3048 /* Reader forms that can reuse previously read objects. */
3049 if (c >= '0' && c <= '9')
3051 EMACS_INT n = 0;
3052 Lisp_Object tem;
3053 bool overflow = false;
3055 /* Read a non-negative integer. */
3056 while (c >= '0' && c <= '9')
3058 overflow |= INT_MULTIPLY_WRAPV (n, 10, &n);
3059 overflow |= INT_ADD_WRAPV (n, c - '0', &n);
3060 c = READCHAR;
3063 if (!overflow && n <= MOST_POSITIVE_FIXNUM)
3065 if (c == 'r' || c == 'R')
3066 return read_integer (readcharfun, n);
3068 if (! NILP (Vread_circle))
3070 /* #n=object returns object, but associates it with
3071 n for #n#. */
3072 if (c == '=')
3074 /* Make a placeholder for #n# to use temporarily. */
3075 /* Note: We used to use AUTO_CONS to allocate
3076 placeholder, but that is a bad idea, since it
3077 will place a stack-allocated cons cell into
3078 the list in read_objects_map, which is a
3079 staticpro'd global variable, and thus each of
3080 its elements is marked during each GC. A
3081 stack-allocated object will become garbled
3082 when its stack slot goes out of scope, and
3083 some other function reuses it for entirely
3084 different purposes, which will cause crashes
3085 in GC. */
3086 Lisp_Object placeholder = Fcons (Qnil, Qnil);
3087 struct Lisp_Hash_Table *h
3088 = XHASH_TABLE (read_objects_map);
3089 EMACS_UINT hash;
3090 Lisp_Object number = make_number (n);
3092 ptrdiff_t i = hash_lookup (h, number, &hash);
3093 if (i >= 0)
3094 /* Not normal, but input could be malformed. */
3095 set_hash_value_slot (h, i, placeholder);
3096 else
3097 hash_put (h, number, placeholder, hash);
3099 /* Read the object itself. */
3100 tem = read0 (readcharfun);
3102 /* If it can be recursive, remember it for
3103 future substitutions. */
3104 if (! SYMBOLP (tem)
3105 && ! NUMBERP (tem)
3106 && ! (STRINGP (tem) && !string_intervals (tem)))
3108 struct Lisp_Hash_Table *h2
3109 = XHASH_TABLE (read_objects_completed);
3110 i = hash_lookup (h2, tem, &hash);
3111 eassert (i < 0);
3112 hash_put (h2, tem, Qnil, hash);
3115 /* Now put it everywhere the placeholder was... */
3116 if (CONSP (tem))
3118 Fsetcar (placeholder, XCAR (tem));
3119 Fsetcdr (placeholder, XCDR (tem));
3120 return placeholder;
3122 else
3124 Flread__substitute_object_in_subtree
3125 (tem, placeholder, read_objects_completed);
3127 /* ...and #n# will use the real value from now on. */
3128 i = hash_lookup (h, number, &hash);
3129 eassert (i >= 0);
3130 set_hash_value_slot (h, i, tem);
3132 return tem;
3136 /* #n# returns a previously read object. */
3137 if (c == '#')
3139 struct Lisp_Hash_Table *h
3140 = XHASH_TABLE (read_objects_map);
3141 ptrdiff_t i = hash_lookup (h, make_number (n), NULL);
3142 if (i >= 0)
3143 return HASH_VALUE (h, i);
3147 /* Fall through to error message. */
3149 else if (c == 'x' || c == 'X')
3150 return read_integer (readcharfun, 16);
3151 else if (c == 'o' || c == 'O')
3152 return read_integer (readcharfun, 8);
3153 else if (c == 'b' || c == 'B')
3154 return read_integer (readcharfun, 2);
3156 UNREAD (c);
3157 invalid_syntax ("#");
3159 case ';':
3160 while ((c = READCHAR) >= 0 && c != '\n');
3161 goto retry;
3163 case '\'':
3164 return list2 (Qquote, read0 (readcharfun));
3166 case '`':
3168 int next_char = READCHAR;
3169 UNREAD (next_char);
3170 /* Transition from old-style to new-style:
3171 If we see "(`" it used to mean old-style, which usually works
3172 fine because ` should almost never appear in such a position
3173 for new-style. But occasionally we need "(`" to mean new
3174 style, so we try to distinguish the two by the fact that we
3175 can either write "( `foo" or "(` foo", where the first
3176 intends to use new-style whereas the second intends to use
3177 old-style. For Emacs-25, we should completely remove this
3178 first_in_list exception (old-style can still be obtained via
3179 "(\`" anyway). */
3180 if (!new_backquote_flag && first_in_list && next_char == ' ')
3182 Vlread_old_style_backquotes = Qt;
3183 goto default_label;
3185 else
3187 Lisp_Object value;
3188 bool saved_new_backquote_flag = new_backquote_flag;
3190 new_backquote_flag = 1;
3191 value = read0 (readcharfun);
3192 new_backquote_flag = saved_new_backquote_flag;
3194 return list2 (Qbackquote, value);
3197 case ',':
3199 int next_char = READCHAR;
3200 UNREAD (next_char);
3201 /* Transition from old-style to new-style:
3202 It used to be impossible to have a new-style , other than within
3203 a new-style `. This is sufficient when ` and , are used in the
3204 normal way, but ` and , can also appear in args to macros that
3205 will not interpret them in the usual way, in which case , may be
3206 used without any ` anywhere near.
3207 So we now use the same heuristic as for backquote: old-style
3208 unquotes are only recognized when first on a list, and when
3209 followed by a space.
3210 Because it's more difficult to peek 2 chars ahead, a new-style
3211 ,@ can still not be used outside of a `, unless it's in the middle
3212 of a list. */
3213 if (new_backquote_flag
3214 || !first_in_list
3215 || (next_char != ' ' && next_char != '@'))
3217 Lisp_Object comma_type = Qnil;
3218 Lisp_Object value;
3219 int ch = READCHAR;
3221 if (ch == '@')
3222 comma_type = Qcomma_at;
3223 else if (ch == '.')
3224 comma_type = Qcomma_dot;
3225 else
3227 if (ch >= 0) UNREAD (ch);
3228 comma_type = Qcomma;
3231 value = read0 (readcharfun);
3232 return list2 (comma_type, value);
3234 else
3236 Vlread_old_style_backquotes = Qt;
3237 goto default_label;
3240 case '?':
3242 int modifiers;
3243 int next_char;
3244 bool ok;
3246 c = READCHAR;
3247 if (c < 0)
3248 end_of_file_error ();
3250 /* Accept `single space' syntax like (list ? x) where the
3251 whitespace character is SPC or TAB.
3252 Other literal whitespace like NL, CR, and FF are not accepted,
3253 as there are well-established escape sequences for these. */
3254 if (c == ' ' || c == '\t')
3255 return make_number (c);
3257 if (c == '(' || c == ')' || c == '[' || c == ']'
3258 || c == '"' || c == ';')
3260 CHECK_LIST (Vlread_unescaped_character_literals);
3261 Lisp_Object char_obj = make_natnum (c);
3262 if (NILP (Fmemq (char_obj, Vlread_unescaped_character_literals)))
3263 Vlread_unescaped_character_literals =
3264 Fcons (char_obj, Vlread_unescaped_character_literals);
3267 if (c == '\\')
3268 c = read_escape (readcharfun, 0);
3269 modifiers = c & CHAR_MODIFIER_MASK;
3270 c &= ~CHAR_MODIFIER_MASK;
3271 if (CHAR_BYTE8_P (c))
3272 c = CHAR_TO_BYTE8 (c);
3273 c |= modifiers;
3275 next_char = READCHAR;
3276 ok = (next_char <= 040
3277 || (next_char < 0200
3278 && strchr ("\"';()[]#?`,.", next_char) != NULL));
3279 UNREAD (next_char);
3280 if (ok)
3281 return make_number (c);
3283 invalid_syntax ("?");
3286 case '"':
3288 ptrdiff_t count = SPECPDL_INDEX ();
3289 char *read_buffer = stackbuf;
3290 ptrdiff_t read_buffer_size = sizeof stackbuf;
3291 char *heapbuf = NULL;
3292 char *p = read_buffer;
3293 char *end = read_buffer + read_buffer_size;
3294 int ch;
3295 /* True if we saw an escape sequence specifying
3296 a multibyte character. */
3297 bool force_multibyte = false;
3298 /* True if we saw an escape sequence specifying
3299 a single-byte character. */
3300 bool force_singlebyte = false;
3301 bool cancel = false;
3302 ptrdiff_t nchars = 0;
3304 while ((ch = READCHAR) >= 0
3305 && ch != '\"')
3307 if (end - p < MAX_MULTIBYTE_LENGTH)
3309 ptrdiff_t offset = p - read_buffer;
3310 read_buffer = grow_read_buffer (read_buffer, offset,
3311 &heapbuf, &read_buffer_size,
3312 count);
3313 p = read_buffer + offset;
3314 end = read_buffer + read_buffer_size;
3317 if (ch == '\\')
3319 int modifiers;
3321 ch = read_escape (readcharfun, 1);
3323 /* CH is -1 if \ newline or \ space has just been seen. */
3324 if (ch == -1)
3326 if (p == read_buffer)
3327 cancel = true;
3328 continue;
3331 modifiers = ch & CHAR_MODIFIER_MASK;
3332 ch = ch & ~CHAR_MODIFIER_MASK;
3334 if (CHAR_BYTE8_P (ch))
3335 force_singlebyte = true;
3336 else if (! ASCII_CHAR_P (ch))
3337 force_multibyte = true;
3338 else /* I.e. ASCII_CHAR_P (ch). */
3340 /* Allow `\C- ' and `\C-?'. */
3341 if (modifiers == CHAR_CTL)
3343 if (ch == ' ')
3344 ch = 0, modifiers = 0;
3345 else if (ch == '?')
3346 ch = 127, modifiers = 0;
3348 if (modifiers & CHAR_SHIFT)
3350 /* Shift modifier is valid only with [A-Za-z]. */
3351 if (ch >= 'A' && ch <= 'Z')
3352 modifiers &= ~CHAR_SHIFT;
3353 else if (ch >= 'a' && ch <= 'z')
3354 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
3357 if (modifiers & CHAR_META)
3359 /* Move the meta bit to the right place for a
3360 string. */
3361 modifiers &= ~CHAR_META;
3362 ch = BYTE8_TO_CHAR (ch | 0x80);
3363 force_singlebyte = true;
3367 /* Any modifiers remaining are invalid. */
3368 if (modifiers)
3369 error ("Invalid modifier in string");
3370 p += CHAR_STRING (ch, (unsigned char *) p);
3372 else
3374 p += CHAR_STRING (ch, (unsigned char *) p);
3375 if (CHAR_BYTE8_P (ch))
3376 force_singlebyte = true;
3377 else if (! ASCII_CHAR_P (ch))
3378 force_multibyte = true;
3380 nchars++;
3383 if (ch < 0)
3384 end_of_file_error ();
3386 /* If purifying, and string starts with \ newline,
3387 return zero instead. This is for doc strings
3388 that we are really going to find in etc/DOC.nn.nn. */
3389 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
3390 return unbind_to (count, make_number (0));
3392 if (! force_multibyte && force_singlebyte)
3394 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3395 forms. Convert it to unibyte. */
3396 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3397 p - read_buffer);
3398 p = read_buffer + nchars;
3401 Lisp_Object result
3402 = make_specified_string (read_buffer, nchars, p - read_buffer,
3403 (force_multibyte
3404 || (p - read_buffer != nchars)));
3405 return unbind_to (count, result);
3408 case '.':
3410 int next_char = READCHAR;
3411 UNREAD (next_char);
3413 if (next_char <= 040
3414 || (next_char < 0200
3415 && strchr ("\"';([#?`,", next_char) != NULL))
3417 *pch = c;
3418 return Qnil;
3421 /* The atom-reading loop below will now loop at least once,
3422 assuring that we will not try to UNREAD two characters in a
3423 row. */
3424 FALLTHROUGH;
3425 default:
3426 default_label:
3427 if (c <= 040) goto retry;
3428 if (c == NO_BREAK_SPACE)
3429 goto retry;
3431 read_symbol:
3433 ptrdiff_t count = SPECPDL_INDEX ();
3434 char *read_buffer = stackbuf;
3435 ptrdiff_t read_buffer_size = sizeof stackbuf;
3436 char *heapbuf = NULL;
3437 char *p = read_buffer;
3438 char *end = read_buffer + read_buffer_size;
3439 bool quoted = false;
3440 EMACS_INT start_position = readchar_count - 1;
3444 if (end - p < MAX_MULTIBYTE_LENGTH + 1)
3446 ptrdiff_t offset = p - read_buffer;
3447 read_buffer = grow_read_buffer (read_buffer, offset,
3448 &heapbuf, &read_buffer_size,
3449 count);
3450 p = read_buffer + offset;
3451 end = read_buffer + read_buffer_size;
3454 if (c == '\\')
3456 c = READCHAR;
3457 if (c == -1)
3458 end_of_file_error ();
3459 quoted = true;
3462 if (multibyte)
3463 p += CHAR_STRING (c, (unsigned char *) p);
3464 else
3465 *p++ = c;
3466 c = READCHAR;
3468 while (c > 040
3469 && c != NO_BREAK_SPACE
3470 && (c >= 0200
3471 || strchr ("\"';()[]#`,", c) == NULL));
3473 *p = 0;
3474 UNREAD (c);
3476 if (!quoted && !uninterned_symbol)
3478 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3479 if (! NILP (result))
3480 return unbind_to (count, result);
3483 Lisp_Object result;
3484 ptrdiff_t nbytes = p - read_buffer;
3485 ptrdiff_t nchars
3486 = (multibyte
3487 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3488 nbytes)
3489 : nbytes);
3491 if (uninterned_symbol)
3493 Lisp_Object name
3494 = ((! NILP (Vpurify_flag)
3495 ? make_pure_string : make_specified_string)
3496 (read_buffer, nchars, nbytes, multibyte));
3497 result = Fmake_symbol (name);
3499 else
3501 /* Don't create the string object for the name unless
3502 we're going to retain it in a new symbol.
3504 Like intern_1 but supports multibyte names. */
3505 Lisp_Object obarray = check_obarray (Vobarray);
3506 Lisp_Object tem = oblookup (obarray, read_buffer,
3507 nchars, nbytes);
3509 if (SYMBOLP (tem))
3510 result = tem;
3511 else
3513 Lisp_Object name
3514 = make_specified_string (read_buffer, nchars, nbytes,
3515 multibyte);
3516 result = intern_driver (name, obarray, tem);
3520 if (EQ (Vread_with_symbol_positions, Qt)
3521 || EQ (Vread_with_symbol_positions, readcharfun))
3522 Vread_symbol_positions_list
3523 = Fcons (Fcons (result, make_number (start_position)),
3524 Vread_symbol_positions_list);
3525 return unbind_to (count, result);
3531 DEFUN ("lread--substitute-object-in-subtree",
3532 Flread__substitute_object_in_subtree,
3533 Slread__substitute_object_in_subtree, 3, 3, 0,
3534 doc: /* In OBJECT, replace every occurrence of PLACEHOLDER with OBJECT.
3535 COMPLETED is a hash table of objects that might be circular, or is t
3536 if any object might be circular. */)
3537 (Lisp_Object object, Lisp_Object placeholder, Lisp_Object completed)
3539 struct subst subst = { object, placeholder, completed, Qnil };
3540 Lisp_Object check_object = substitute_object_recurse (&subst, object);
3542 /* The returned object here is expected to always eq the
3543 original. */
3544 if (!EQ (check_object, object))
3545 error ("Unexpected mutation error in reader");
3546 return Qnil;
3549 static Lisp_Object
3550 substitute_object_recurse (struct subst *subst, Lisp_Object subtree)
3552 /* If we find the placeholder, return the target object. */
3553 if (EQ (subst->placeholder, subtree))
3554 return subst->object;
3556 /* For common object types that can't contain other objects, don't
3557 bother looking them up; we're done. */
3558 if (SYMBOLP (subtree)
3559 || (STRINGP (subtree) && !string_intervals (subtree))
3560 || NUMBERP (subtree))
3561 return subtree;
3563 /* If we've been to this node before, don't explore it again. */
3564 if (!EQ (Qnil, Fmemq (subtree, subst->seen)))
3565 return subtree;
3567 /* If this node can be the entry point to a cycle, remember that
3568 we've seen it. It can only be such an entry point if it was made
3569 by #n=, which means that we can find it as a value in
3570 COMPLETED. */
3571 if (EQ (subst->completed, Qt)
3572 || hash_lookup (XHASH_TABLE (subst->completed), subtree, NULL) >= 0)
3573 subst->seen = Fcons (subtree, subst->seen);
3575 /* Recurse according to subtree's type.
3576 Every branch must return a Lisp_Object. */
3577 switch (XTYPE (subtree))
3579 case Lisp_Vectorlike:
3581 ptrdiff_t i = 0, length = 0;
3582 if (BOOL_VECTOR_P (subtree))
3583 return subtree; /* No sub-objects anyway. */
3584 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3585 || COMPILEDP (subtree) || HASH_TABLE_P (subtree)
3586 || RECORDP (subtree))
3587 length = PVSIZE (subtree);
3588 else if (VECTORP (subtree))
3589 length = ASIZE (subtree);
3590 else
3591 /* An unknown pseudovector may contain non-Lisp fields, so we
3592 can't just blindly traverse all its fields. We used to call
3593 `Flength' which signaled `sequencep', so I just preserved this
3594 behavior. */
3595 wrong_type_argument (Qsequencep, subtree);
3597 if (SUB_CHAR_TABLE_P (subtree))
3598 i = 2;
3599 for ( ; i < length; i++)
3600 ASET (subtree, i,
3601 substitute_object_recurse (subst, AREF (subtree, i)));
3602 return subtree;
3605 case Lisp_Cons:
3606 XSETCAR (subtree, substitute_object_recurse (subst, XCAR (subtree)));
3607 XSETCDR (subtree, substitute_object_recurse (subst, XCDR (subtree)));
3608 return subtree;
3610 case Lisp_String:
3612 /* Check for text properties in each interval.
3613 substitute_in_interval contains part of the logic. */
3615 INTERVAL root_interval = string_intervals (subtree);
3616 traverse_intervals_noorder (root_interval,
3617 substitute_in_interval, subst);
3618 return subtree;
3621 /* Other types don't recurse any further. */
3622 default:
3623 return subtree;
3627 /* Helper function for substitute_object_recurse. */
3628 static void
3629 substitute_in_interval (INTERVAL interval, void *arg)
3631 set_interval_plist (interval,
3632 substitute_object_recurse (arg, interval->plist));
3636 /* Convert STRING to a number, assuming base BASE. Return a fixnum if
3637 STRING has integer syntax and fits in a fixnum, else return the
3638 nearest float if STRING has either floating point or integer syntax
3639 and BASE is 10, else return nil. If IGNORE_TRAILING, consider just
3640 the longest prefix of STRING that has valid floating point syntax.
3641 Signal an overflow if BASE is not 10 and the number has integer
3642 syntax but does not fit. */
3644 Lisp_Object
3645 string_to_number (char const *string, int base, bool ignore_trailing)
3647 char const *cp = string;
3648 bool float_syntax = 0;
3649 double value = 0;
3651 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3652 IEEE floating point hosts, and works around a formerly-common bug where
3653 atof ("-0.0") drops the sign. */
3654 bool negative = *cp == '-';
3656 bool signedp = negative || *cp == '+';
3657 cp += signedp;
3659 enum { INTOVERFLOW = 1, LEAD_INT = 2, DOT_CHAR = 4, TRAIL_INT = 8,
3660 E_EXP = 16 };
3661 int state = 0;
3662 int leading_digit = digit_to_number (*cp, base);
3663 uintmax_t n = leading_digit;
3664 if (leading_digit >= 0)
3666 state |= LEAD_INT;
3667 for (int digit; 0 <= (digit = digit_to_number (*++cp, base)); )
3669 if (INT_MULTIPLY_OVERFLOW (n, base))
3670 state |= INTOVERFLOW;
3671 n *= base;
3672 if (INT_ADD_OVERFLOW (n, digit))
3673 state |= INTOVERFLOW;
3674 n += digit;
3677 if (*cp == '.')
3679 state |= DOT_CHAR;
3680 cp++;
3683 if (base == 10)
3685 if ('0' <= *cp && *cp <= '9')
3687 state |= TRAIL_INT;
3689 cp++;
3690 while ('0' <= *cp && *cp <= '9');
3692 if (*cp == 'e' || *cp == 'E')
3694 char const *ecp = cp;
3695 cp++;
3696 if (*cp == '+' || *cp == '-')
3697 cp++;
3698 if ('0' <= *cp && *cp <= '9')
3700 state |= E_EXP;
3702 cp++;
3703 while ('0' <= *cp && *cp <= '9');
3705 else if (cp[-1] == '+'
3706 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3708 state |= E_EXP;
3709 cp += 3;
3710 value = INFINITY;
3712 else if (cp[-1] == '+'
3713 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3715 state |= E_EXP;
3716 cp += 3;
3717 /* NAN is a "positive" NaN on all known Emacs hosts. */
3718 value = NAN;
3720 else
3721 cp = ecp;
3724 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3725 || (state & ~INTOVERFLOW) == (LEAD_INT|E_EXP));
3728 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3729 any prefix that matches. Otherwise, the entire string must match. */
3730 if (! (ignore_trailing
3731 ? ((state & LEAD_INT) != 0 || float_syntax)
3732 : (!*cp && ((state & ~(INTOVERFLOW | DOT_CHAR)) == LEAD_INT
3733 || float_syntax))))
3734 return Qnil;
3736 /* If the number uses integer and not float syntax, and is in C-language
3737 range, use its value, preferably as a fixnum. */
3738 if (leading_digit >= 0 && ! float_syntax)
3740 if (state & INTOVERFLOW)
3742 /* Unfortunately there's no simple and accurate way to convert
3743 non-base-10 numbers that are out of C-language range. */
3744 if (base != 10)
3745 xsignal1 (Qoverflow_error, build_string (string));
3747 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3749 EMACS_INT signed_n = n;
3750 return make_number (negative ? -signed_n : signed_n);
3752 else
3753 value = n;
3756 /* Either the number uses float syntax, or it does not fit into a fixnum.
3757 Convert it from string to floating point, unless the value is already
3758 known because it is an infinity, a NAN, or its absolute value fits in
3759 uintmax_t. */
3760 if (! value)
3761 value = atof (string + signedp);
3763 return make_float (negative ? -value : value);
3767 static Lisp_Object
3768 read_vector (Lisp_Object readcharfun, bool bytecodeflag)
3770 ptrdiff_t i, size;
3771 Lisp_Object *ptr;
3772 Lisp_Object tem, item, vector;
3773 struct Lisp_Cons *otem;
3774 Lisp_Object len;
3776 tem = read_list (1, readcharfun);
3777 len = Flength (tem);
3778 vector = Fmake_vector (len, Qnil);
3780 size = ASIZE (vector);
3781 ptr = XVECTOR (vector)->contents;
3782 for (i = 0; i < size; i++)
3784 item = Fcar (tem);
3785 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3786 bytecode object, the docstring containing the bytecode and
3787 constants values must be treated as unibyte and passed to
3788 Fread, to get the actual bytecode string and constants vector. */
3789 if (bytecodeflag && load_force_doc_strings)
3791 if (i == COMPILED_BYTECODE)
3793 if (!STRINGP (item))
3794 error ("Invalid byte code");
3796 /* Delay handling the bytecode slot until we know whether
3797 it is lazily-loaded (we can tell by whether the
3798 constants slot is nil). */
3799 ASET (vector, COMPILED_CONSTANTS, item);
3800 item = Qnil;
3802 else if (i == COMPILED_CONSTANTS)
3804 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3806 if (NILP (item))
3808 /* Coerce string to unibyte (like string-as-unibyte,
3809 but without generating extra garbage and
3810 guaranteeing no change in the contents). */
3811 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3812 STRING_SET_UNIBYTE (bytestr);
3814 item = Fread (Fcons (bytestr, readcharfun));
3815 if (!CONSP (item))
3816 error ("Invalid byte code");
3818 otem = XCONS (item);
3819 bytestr = XCAR (item);
3820 item = XCDR (item);
3821 free_cons (otem);
3824 /* Now handle the bytecode slot. */
3825 ASET (vector, COMPILED_BYTECODE, bytestr);
3827 else if (i == COMPILED_DOC_STRING
3828 && STRINGP (item)
3829 && ! STRING_MULTIBYTE (item))
3831 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3832 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3833 else
3834 item = Fstring_as_multibyte (item);
3837 ASET (vector, i, item);
3838 otem = XCONS (tem);
3839 tem = Fcdr (tem);
3840 free_cons (otem);
3842 return vector;
3845 /* FLAG means check for ']' to terminate rather than ')' and '.'. */
3847 static Lisp_Object
3848 read_list (bool flag, Lisp_Object readcharfun)
3850 Lisp_Object val, tail;
3851 Lisp_Object elt, tem;
3852 /* 0 is the normal case.
3853 1 means this list is a doc reference; replace it with the number 0.
3854 2 means this list is a doc reference; replace it with the doc string. */
3855 int doc_reference = 0;
3857 /* Initialize this to 1 if we are reading a list. */
3858 bool first_in_list = flag <= 0;
3860 val = Qnil;
3861 tail = Qnil;
3863 while (1)
3865 int ch;
3866 elt = read1 (readcharfun, &ch, first_in_list);
3868 first_in_list = 0;
3870 /* While building, if the list starts with #$, treat it specially. */
3871 if (EQ (elt, Vload_file_name)
3872 && ! NILP (elt)
3873 && !NILP (Vpurify_flag))
3875 if (NILP (Vdoc_file_name))
3876 /* We have not yet called Snarf-documentation, so assume
3877 this file is described in the DOC file
3878 and Snarf-documentation will fill in the right value later.
3879 For now, replace the whole list with 0. */
3880 doc_reference = 1;
3881 else
3882 /* We have already called Snarf-documentation, so make a relative
3883 file name for this file, so it can be found properly
3884 in the installed Lisp directory.
3885 We don't use Fexpand_file_name because that would make
3886 the directory absolute now. */
3888 AUTO_STRING (dot_dot_lisp, "../lisp/");
3889 elt = concat2 (dot_dot_lisp, Ffile_name_nondirectory (elt));
3892 else if (EQ (elt, Vload_file_name)
3893 && ! NILP (elt)
3894 && load_force_doc_strings)
3895 doc_reference = 2;
3897 if (ch)
3899 if (flag > 0)
3901 if (ch == ']')
3902 return val;
3903 invalid_syntax (") or . in a vector");
3905 if (ch == ')')
3906 return val;
3907 if (ch == '.')
3909 if (!NILP (tail))
3910 XSETCDR (tail, read0 (readcharfun));
3911 else
3912 val = read0 (readcharfun);
3913 read1 (readcharfun, &ch, 0);
3915 if (ch == ')')
3917 if (doc_reference == 1)
3918 return make_number (0);
3919 if (doc_reference == 2 && INTEGERP (XCDR (val)))
3921 char *saved = NULL;
3922 file_offset saved_position;
3923 /* Get a doc string from the file we are loading.
3924 If it's in saved_doc_string, get it from there.
3926 Here, we don't know if the string is a
3927 bytecode string or a doc string. As a
3928 bytecode string must be unibyte, we always
3929 return a unibyte string. If it is actually a
3930 doc string, caller must make it
3931 multibyte. */
3933 /* Position is negative for user variables. */
3934 EMACS_INT pos = eabs (XINT (XCDR (val)));
3935 if (pos >= saved_doc_string_position
3936 && pos < (saved_doc_string_position
3937 + saved_doc_string_length))
3939 saved = saved_doc_string;
3940 saved_position = saved_doc_string_position;
3942 /* Look in prev_saved_doc_string the same way. */
3943 else if (pos >= prev_saved_doc_string_position
3944 && pos < (prev_saved_doc_string_position
3945 + prev_saved_doc_string_length))
3947 saved = prev_saved_doc_string;
3948 saved_position = prev_saved_doc_string_position;
3950 if (saved)
3952 ptrdiff_t start = pos - saved_position;
3953 ptrdiff_t from, to;
3955 /* Process quoting with ^A,
3956 and find the end of the string,
3957 which is marked with ^_ (037). */
3958 for (from = start, to = start;
3959 saved[from] != 037;)
3961 int c = saved[from++];
3962 if (c == 1)
3964 c = saved[from++];
3965 saved[to++] = (c == 1 ? c
3966 : c == '0' ? 0
3967 : c == '_' ? 037
3968 : c);
3970 else
3971 saved[to++] = c;
3974 return make_unibyte_string (saved + start,
3975 to - start);
3977 else
3978 return get_doc_string (val, 1, 0);
3981 return val;
3983 invalid_syntax (". in wrong context");
3985 invalid_syntax ("] in a list");
3987 tem = list1 (elt);
3988 if (!NILP (tail))
3989 XSETCDR (tail, tem);
3990 else
3991 val = tem;
3992 tail = tem;
3996 static Lisp_Object initial_obarray;
3998 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
4000 static size_t oblookup_last_bucket_number;
4002 /* Get an error if OBARRAY is not an obarray.
4003 If it is one, return it. */
4005 Lisp_Object
4006 check_obarray (Lisp_Object obarray)
4008 /* We don't want to signal a wrong-type-argument error when we are
4009 shutting down due to a fatal error, and we don't want to hit
4010 assertions in VECTORP and ASIZE if the fatal error was during GC. */
4011 if (!fatal_error_in_progress
4012 && (!VECTORP (obarray) || ASIZE (obarray) == 0))
4014 /* If Vobarray is now invalid, force it to be valid. */
4015 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
4016 wrong_type_argument (Qvectorp, obarray);
4018 return obarray;
4021 /* Intern symbol SYM in OBARRAY using bucket INDEX. */
4023 static Lisp_Object
4024 intern_sym (Lisp_Object sym, Lisp_Object obarray, Lisp_Object index)
4026 Lisp_Object *ptr;
4028 XSYMBOL (sym)->u.s.interned = (EQ (obarray, initial_obarray)
4029 ? SYMBOL_INTERNED_IN_INITIAL_OBARRAY
4030 : SYMBOL_INTERNED);
4032 if (SREF (SYMBOL_NAME (sym), 0) == ':' && EQ (obarray, initial_obarray))
4034 make_symbol_constant (sym);
4035 XSYMBOL (sym)->u.s.redirect = SYMBOL_PLAINVAL;
4036 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
4039 ptr = aref_addr (obarray, XINT (index));
4040 set_symbol_next (sym, SYMBOLP (*ptr) ? XSYMBOL (*ptr) : NULL);
4041 *ptr = sym;
4042 return sym;
4045 /* Intern a symbol with name STRING in OBARRAY using bucket INDEX. */
4047 Lisp_Object
4048 intern_driver (Lisp_Object string, Lisp_Object obarray, Lisp_Object index)
4050 return intern_sym (Fmake_symbol (string), obarray, index);
4053 /* Intern the C string STR: return a symbol with that name,
4054 interned in the current obarray. */
4056 Lisp_Object
4057 intern_1 (const char *str, ptrdiff_t len)
4059 Lisp_Object obarray = check_obarray (Vobarray);
4060 Lisp_Object tem = oblookup (obarray, str, len, len);
4062 return (SYMBOLP (tem) ? tem
4063 /* The above `oblookup' was done on the basis of nchars==nbytes, so
4064 the string has to be unibyte. */
4065 : intern_driver (make_unibyte_string (str, len),
4066 obarray, tem));
4069 Lisp_Object
4070 intern_c_string_1 (const char *str, ptrdiff_t len)
4072 Lisp_Object obarray = check_obarray (Vobarray);
4073 Lisp_Object tem = oblookup (obarray, str, len, len);
4075 if (!SYMBOLP (tem))
4077 /* Creating a non-pure string from a string literal not implemented yet.
4078 We could just use make_string here and live with the extra copy. */
4079 eassert (!NILP (Vpurify_flag));
4080 tem = intern_driver (make_pure_c_string (str, len), obarray, tem);
4082 return tem;
4085 static void
4086 define_symbol (Lisp_Object sym, char const *str)
4088 ptrdiff_t len = strlen (str);
4089 Lisp_Object string = make_pure_c_string (str, len);
4090 init_symbol (sym, string);
4092 /* Qunbound is uninterned, so that it's not confused with any symbol
4093 'unbound' created by a Lisp program. */
4094 if (! EQ (sym, Qunbound))
4096 Lisp_Object bucket = oblookup (initial_obarray, str, len, len);
4097 eassert (INTEGERP (bucket));
4098 intern_sym (sym, initial_obarray, bucket);
4102 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
4103 doc: /* Return the canonical symbol whose name is STRING.
4104 If there is none, one is created by this function and returned.
4105 A second optional argument specifies the obarray to use;
4106 it defaults to the value of `obarray'. */)
4107 (Lisp_Object string, Lisp_Object obarray)
4109 Lisp_Object tem;
4111 obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
4112 CHECK_STRING (string);
4114 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
4115 if (!SYMBOLP (tem))
4116 tem = intern_driver (NILP (Vpurify_flag) ? string : Fpurecopy (string),
4117 obarray, tem);
4118 return tem;
4121 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
4122 doc: /* Return the canonical symbol named NAME, or nil if none exists.
4123 NAME may be a string or a symbol. If it is a symbol, that exact
4124 symbol is searched for.
4125 A second optional argument specifies the obarray to use;
4126 it defaults to the value of `obarray'. */)
4127 (Lisp_Object name, Lisp_Object obarray)
4129 register Lisp_Object tem, string;
4131 if (NILP (obarray)) obarray = Vobarray;
4132 obarray = check_obarray (obarray);
4134 if (!SYMBOLP (name))
4136 CHECK_STRING (name);
4137 string = name;
4139 else
4140 string = SYMBOL_NAME (name);
4142 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
4143 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
4144 return Qnil;
4145 else
4146 return tem;
4149 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
4150 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
4151 The value is t if a symbol was found and deleted, nil otherwise.
4152 NAME may be a string or a symbol. If it is a symbol, that symbol
4153 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
4154 OBARRAY, if nil, defaults to the value of the variable `obarray'.
4155 usage: (unintern NAME OBARRAY) */)
4156 (Lisp_Object name, Lisp_Object obarray)
4158 register Lisp_Object string, tem;
4159 size_t hash;
4161 if (NILP (obarray)) obarray = Vobarray;
4162 obarray = check_obarray (obarray);
4164 if (SYMBOLP (name))
4165 string = SYMBOL_NAME (name);
4166 else
4168 CHECK_STRING (name);
4169 string = name;
4172 tem = oblookup (obarray, SSDATA (string),
4173 SCHARS (string),
4174 SBYTES (string));
4175 if (INTEGERP (tem))
4176 return Qnil;
4177 /* If arg was a symbol, don't delete anything but that symbol itself. */
4178 if (SYMBOLP (name) && !EQ (name, tem))
4179 return Qnil;
4181 /* There are plenty of other symbols which will screw up the Emacs
4182 session if we unintern them, as well as even more ways to use
4183 `setq' or `fset' or whatnot to make the Emacs session
4184 unusable. Let's not go down this silly road. --Stef */
4185 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
4186 error ("Attempt to unintern t or nil"); */
4188 XSYMBOL (tem)->u.s.interned = SYMBOL_UNINTERNED;
4190 hash = oblookup_last_bucket_number;
4192 if (EQ (AREF (obarray, hash), tem))
4194 if (XSYMBOL (tem)->u.s.next)
4196 Lisp_Object sym;
4197 XSETSYMBOL (sym, XSYMBOL (tem)->u.s.next);
4198 ASET (obarray, hash, sym);
4200 else
4201 ASET (obarray, hash, make_number (0));
4203 else
4205 Lisp_Object tail, following;
4207 for (tail = AREF (obarray, hash);
4208 XSYMBOL (tail)->u.s.next;
4209 tail = following)
4211 XSETSYMBOL (following, XSYMBOL (tail)->u.s.next);
4212 if (EQ (following, tem))
4214 set_symbol_next (tail, XSYMBOL (following)->u.s.next);
4215 break;
4220 return Qt;
4223 /* Return the symbol in OBARRAY whose names matches the string
4224 of SIZE characters (SIZE_BYTE bytes) at PTR.
4225 If there is no such symbol, return the integer bucket number of
4226 where the symbol would be if it were present.
4228 Also store the bucket number in oblookup_last_bucket_number. */
4230 Lisp_Object
4231 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
4233 size_t hash;
4234 size_t obsize;
4235 register Lisp_Object tail;
4236 Lisp_Object bucket, tem;
4238 obarray = check_obarray (obarray);
4239 /* This is sometimes needed in the middle of GC. */
4240 obsize = gc_asize (obarray);
4241 hash = hash_string (ptr, size_byte) % obsize;
4242 bucket = AREF (obarray, hash);
4243 oblookup_last_bucket_number = hash;
4244 if (EQ (bucket, make_number (0)))
4246 else if (!SYMBOLP (bucket))
4247 error ("Bad data in guts of obarray"); /* Like CADR error message. */
4248 else
4249 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->u.s.next))
4251 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
4252 && SCHARS (SYMBOL_NAME (tail)) == size
4253 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
4254 return tail;
4255 else if (XSYMBOL (tail)->u.s.next == 0)
4256 break;
4258 XSETINT (tem, hash);
4259 return tem;
4262 void
4263 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
4265 ptrdiff_t i;
4266 register Lisp_Object tail;
4267 CHECK_VECTOR (obarray);
4268 for (i = ASIZE (obarray) - 1; i >= 0; i--)
4270 tail = AREF (obarray, i);
4271 if (SYMBOLP (tail))
4272 while (1)
4274 (*fn) (tail, arg);
4275 if (XSYMBOL (tail)->u.s.next == 0)
4276 break;
4277 XSETSYMBOL (tail, XSYMBOL (tail)->u.s.next);
4282 static void
4283 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
4285 call1 (function, sym);
4288 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
4289 doc: /* Call FUNCTION on every symbol in OBARRAY.
4290 OBARRAY defaults to the value of `obarray'. */)
4291 (Lisp_Object function, Lisp_Object obarray)
4293 if (NILP (obarray)) obarray = Vobarray;
4294 obarray = check_obarray (obarray);
4296 map_obarray (obarray, mapatoms_1, function);
4297 return Qnil;
4300 #define OBARRAY_SIZE 15121
4302 void
4303 init_obarray (void)
4305 Vobarray = Fmake_vector (make_number (OBARRAY_SIZE), make_number (0));
4306 initial_obarray = Vobarray;
4307 staticpro (&initial_obarray);
4309 for (int i = 0; i < ARRAYELTS (lispsym); i++)
4310 define_symbol (builtin_lisp_symbol (i), defsym_name[i]);
4312 DEFSYM (Qunbound, "unbound");
4314 DEFSYM (Qnil, "nil");
4315 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
4316 make_symbol_constant (Qnil);
4317 XSYMBOL (Qnil)->u.s.declared_special = true;
4319 DEFSYM (Qt, "t");
4320 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
4321 make_symbol_constant (Qt);
4322 XSYMBOL (Qt)->u.s.declared_special = true;
4324 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
4325 Vpurify_flag = Qt;
4327 DEFSYM (Qvariable_documentation, "variable-documentation");
4330 void
4331 defsubr (struct Lisp_Subr *sname)
4333 Lisp_Object sym, tem;
4334 sym = intern_c_string (sname->symbol_name);
4335 XSETPVECTYPE (sname, PVEC_SUBR);
4336 XSETSUBR (tem, sname);
4337 set_symbol_function (sym, tem);
4340 #ifdef NOTDEF /* Use fset in subr.el now! */
4341 void
4342 defalias (struct Lisp_Subr *sname, char *string)
4344 Lisp_Object sym;
4345 sym = intern (string);
4346 XSETSUBR (XSYMBOL (sym)->u.s.function, sname);
4348 #endif /* NOTDEF */
4350 /* Define an "integer variable"; a symbol whose value is forwarded to a
4351 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
4352 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4353 void
4354 defvar_int (struct Lisp_Intfwd *i_fwd,
4355 const char *namestring, EMACS_INT *address)
4357 Lisp_Object sym;
4358 sym = intern_c_string (namestring);
4359 i_fwd->type = Lisp_Fwd_Int;
4360 i_fwd->intvar = address;
4361 XSYMBOL (sym)->u.s.declared_special = true;
4362 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4363 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4366 /* Similar but define a variable whose value is t if address contains 1,
4367 nil if address contains 0. */
4368 void
4369 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4370 const char *namestring, bool *address)
4372 Lisp_Object sym;
4373 sym = intern_c_string (namestring);
4374 b_fwd->type = Lisp_Fwd_Bool;
4375 b_fwd->boolvar = address;
4376 XSYMBOL (sym)->u.s.declared_special = true;
4377 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4378 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4379 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4382 /* Similar but define a variable whose value is the Lisp Object stored
4383 at address. Two versions: with and without gc-marking of the C
4384 variable. The nopro version is used when that variable will be
4385 gc-marked for some other reason, since marking the same slot twice
4386 can cause trouble with strings. */
4387 void
4388 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4389 const char *namestring, Lisp_Object *address)
4391 Lisp_Object sym;
4392 sym = intern_c_string (namestring);
4393 o_fwd->type = Lisp_Fwd_Obj;
4394 o_fwd->objvar = address;
4395 XSYMBOL (sym)->u.s.declared_special = true;
4396 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4397 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4400 void
4401 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4402 const char *namestring, Lisp_Object *address)
4404 defvar_lisp_nopro (o_fwd, namestring, address);
4405 staticpro (address);
4408 /* Similar but define a variable whose value is the Lisp Object stored
4409 at a particular offset in the current kboard object. */
4411 void
4412 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4413 const char *namestring, int offset)
4415 Lisp_Object sym;
4416 sym = intern_c_string (namestring);
4417 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4418 ko_fwd->offset = offset;
4419 XSYMBOL (sym)->u.s.declared_special = true;
4420 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4421 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4424 /* Check that the elements of lpath exist. */
4426 static void
4427 load_path_check (Lisp_Object lpath)
4429 Lisp_Object path_tail;
4431 /* The only elements that might not exist are those from
4432 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4433 it exists. */
4434 for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
4436 Lisp_Object dirfile;
4437 dirfile = Fcar (path_tail);
4438 if (STRINGP (dirfile))
4440 dirfile = Fdirectory_file_name (dirfile);
4441 if (! file_accessible_directory_p (dirfile))
4442 dir_warning ("Lisp directory", XCAR (path_tail));
4447 /* Return the default load-path, to be used if EMACSLOADPATH is unset.
4448 This does not include the standard site-lisp directories
4449 under the installation prefix (i.e., PATH_SITELOADSEARCH),
4450 but it does (unless no_site_lisp is set) include site-lisp
4451 directories in the source/build directories if those exist and we
4452 are running uninstalled.
4454 Uses the following logic:
4455 If CANNOT_DUMP:
4456 If Vinstallation_directory is not nil (ie, running uninstalled),
4457 use PATH_DUMPLOADSEARCH (ie, build path). Else use PATH_LOADSEARCH.
4458 The remainder is what happens when dumping works:
4459 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4460 Otherwise use PATH_LOADSEARCH.
4462 If !initialized, then just return PATH_DUMPLOADSEARCH.
4463 If initialized:
4464 If Vinstallation_directory is not nil (ie, running uninstalled):
4465 If installation-dir/lisp exists and not already a member,
4466 we must be running uninstalled. Reset the load-path
4467 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4468 refers to the eventual installation directories. Since we
4469 are not yet installed, we should not use them, even if they exist.)
4470 If installation-dir/lisp does not exist, just add
4471 PATH_DUMPLOADSEARCH at the end instead.
4472 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4473 and not already a member) at the front.
4474 If installation-dir != source-dir (ie running an uninstalled,
4475 out-of-tree build) AND install-dir/src/Makefile exists BUT
4476 install-dir/src/Makefile.in does NOT exist (this is a sanity
4477 check), then repeat the above steps for source-dir/lisp, site-lisp. */
4479 static Lisp_Object
4480 load_path_default (void)
4482 Lisp_Object lpath = Qnil;
4483 const char *normal;
4485 #ifdef CANNOT_DUMP
4486 #ifdef HAVE_NS
4487 const char *loadpath = ns_load_path ();
4488 #endif
4490 normal = PATH_LOADSEARCH;
4491 if (!NILP (Vinstallation_directory)) normal = PATH_DUMPLOADSEARCH;
4493 #ifdef HAVE_NS
4494 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4495 #else
4496 lpath = decode_env_path (0, normal, 0);
4497 #endif
4499 #else /* !CANNOT_DUMP */
4501 normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
4503 if (initialized)
4505 #ifdef HAVE_NS
4506 const char *loadpath = ns_load_path ();
4507 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4508 #else
4509 lpath = decode_env_path (0, normal, 0);
4510 #endif
4511 if (!NILP (Vinstallation_directory))
4513 Lisp_Object tem, tem1;
4515 /* Add to the path the lisp subdir of the installation
4516 dir, if it is accessible. Note: in out-of-tree builds,
4517 this directory is empty save for Makefile. */
4518 tem = Fexpand_file_name (build_string ("lisp"),
4519 Vinstallation_directory);
4520 tem1 = Ffile_accessible_directory_p (tem);
4521 if (!NILP (tem1))
4523 if (NILP (Fmember (tem, lpath)))
4525 /* We are running uninstalled. The default load-path
4526 points to the eventual installed lisp directories.
4527 We should not use those now, even if they exist,
4528 so start over from a clean slate. */
4529 lpath = list1 (tem);
4532 else
4533 /* That dir doesn't exist, so add the build-time
4534 Lisp dirs instead. */
4536 Lisp_Object dump_path =
4537 decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
4538 lpath = nconc2 (lpath, dump_path);
4541 /* Add site-lisp under the installation dir, if it exists. */
4542 if (!no_site_lisp)
4544 tem = Fexpand_file_name (build_string ("site-lisp"),
4545 Vinstallation_directory);
4546 tem1 = Ffile_accessible_directory_p (tem);
4547 if (!NILP (tem1))
4549 if (NILP (Fmember (tem, lpath)))
4550 lpath = Fcons (tem, lpath);
4554 /* If Emacs was not built in the source directory,
4555 and it is run from where it was built, add to load-path
4556 the lisp and site-lisp dirs under that directory. */
4558 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4560 Lisp_Object tem2;
4562 tem = Fexpand_file_name (build_string ("src/Makefile"),
4563 Vinstallation_directory);
4564 tem1 = Ffile_exists_p (tem);
4566 /* Don't be fooled if they moved the entire source tree
4567 AFTER dumping Emacs. If the build directory is indeed
4568 different from the source dir, src/Makefile.in and
4569 src/Makefile will not be found together. */
4570 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4571 Vinstallation_directory);
4572 tem2 = Ffile_exists_p (tem);
4573 if (!NILP (tem1) && NILP (tem2))
4575 tem = Fexpand_file_name (build_string ("lisp"),
4576 Vsource_directory);
4578 if (NILP (Fmember (tem, lpath)))
4579 lpath = Fcons (tem, lpath);
4581 if (!no_site_lisp)
4583 tem = Fexpand_file_name (build_string ("site-lisp"),
4584 Vsource_directory);
4585 tem1 = Ffile_accessible_directory_p (tem);
4586 if (!NILP (tem1))
4588 if (NILP (Fmember (tem, lpath)))
4589 lpath = Fcons (tem, lpath);
4593 } /* Vinstallation_directory != Vsource_directory */
4595 } /* if Vinstallation_directory */
4597 else /* !initialized */
4599 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4600 source directory. We used to add ../lisp (ie the lisp dir in
4601 the build directory) at the front here, but that should not
4602 be necessary, since in out of tree builds lisp/ is empty, save
4603 for Makefile. */
4604 lpath = decode_env_path (0, normal, 0);
4606 #endif /* !CANNOT_DUMP */
4608 return lpath;
4611 void
4612 init_lread (void)
4614 if (NILP (Vpurify_flag) && !NILP (Ffboundp (Qfile_truename)))
4615 Vsource_directory = call1 (Qfile_truename, Vsource_directory);
4617 /* First, set Vload_path. */
4619 /* Ignore EMACSLOADPATH when dumping. */
4620 #ifdef CANNOT_DUMP
4621 bool use_loadpath = true;
4622 #else
4623 bool use_loadpath = NILP (Vpurify_flag);
4624 #endif
4626 if (use_loadpath && egetenv ("EMACSLOADPATH"))
4628 Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
4630 /* Check (non-nil) user-supplied elements. */
4631 load_path_check (Vload_path);
4633 /* If no nils in the environment variable, use as-is.
4634 Otherwise, replace any nils with the default. */
4635 if (! NILP (Fmemq (Qnil, Vload_path)))
4637 Lisp_Object elem, elpath = Vload_path;
4638 Lisp_Object default_lpath = load_path_default ();
4640 /* Check defaults, before adding site-lisp. */
4641 load_path_check (default_lpath);
4643 /* Add the site-lisp directories to the front of the default. */
4644 if (!no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4646 Lisp_Object sitelisp;
4647 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4648 if (! NILP (sitelisp))
4649 default_lpath = nconc2 (sitelisp, default_lpath);
4652 Vload_path = Qnil;
4654 /* Replace nils from EMACSLOADPATH by default. */
4655 while (CONSP (elpath))
4657 elem = XCAR (elpath);
4658 elpath = XCDR (elpath);
4659 Vload_path = CALLN (Fappend, Vload_path,
4660 NILP (elem) ? default_lpath : list1 (elem));
4662 } /* Fmemq (Qnil, Vload_path) */
4664 else
4666 Vload_path = load_path_default ();
4668 /* Check before adding site-lisp directories.
4669 The install should have created them, but they are not
4670 required, so no need to warn if they are absent.
4671 Or we might be running before installation. */
4672 load_path_check (Vload_path);
4674 /* Add the site-lisp directories at the front. */
4675 if (initialized && !no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4677 Lisp_Object sitelisp;
4678 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4679 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4683 Vvalues = Qnil;
4685 load_in_progress = 0;
4686 Vload_file_name = Qnil;
4687 Vstandard_input = Qt;
4688 Vloads_in_progress = Qnil;
4691 /* Print a warning that directory intended for use USE and with name
4692 DIRNAME cannot be accessed. On entry, errno should correspond to
4693 the access failure. Print the warning on stderr and put it in
4694 *Messages*. */
4696 void
4697 dir_warning (char const *use, Lisp_Object dirname)
4699 static char const format[] = "Warning: %s '%s': %s\n";
4700 char *diagnostic = emacs_strerror (errno);
4701 fprintf (stderr, format, use, SSDATA (ENCODE_SYSTEM (dirname)), diagnostic);
4703 /* Don't log the warning before we've initialized!! */
4704 if (initialized)
4706 ptrdiff_t diaglen = strlen (diagnostic);
4707 AUTO_STRING_WITH_LEN (diag, diagnostic, diaglen);
4708 if (! NILP (Vlocale_coding_system))
4710 Lisp_Object s
4711 = code_convert_string_norecord (diag, Vlocale_coding_system, false);
4712 diagnostic = SSDATA (s);
4713 diaglen = SBYTES (s);
4715 USE_SAFE_ALLOCA;
4716 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4717 + strlen (use) + SBYTES (dirname) + diaglen);
4718 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4719 diagnostic);
4720 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4721 SAFE_FREE ();
4725 void
4726 syms_of_lread (void)
4728 defsubr (&Sread);
4729 defsubr (&Sread_from_string);
4730 defsubr (&Slread__substitute_object_in_subtree);
4731 defsubr (&Sintern);
4732 defsubr (&Sintern_soft);
4733 defsubr (&Sunintern);
4734 defsubr (&Sget_load_suffixes);
4735 defsubr (&Sload);
4736 defsubr (&Seval_buffer);
4737 defsubr (&Seval_region);
4738 defsubr (&Sread_char);
4739 defsubr (&Sread_char_exclusive);
4740 defsubr (&Sread_event);
4741 defsubr (&Sget_file_char);
4742 defsubr (&Smapatoms);
4743 defsubr (&Slocate_file_internal);
4745 DEFVAR_LISP ("obarray", Vobarray,
4746 doc: /* Symbol table for use by `intern' and `read'.
4747 It is a vector whose length ought to be prime for best results.
4748 The vector's contents don't make sense if examined from Lisp programs;
4749 to find all the symbols in an obarray, use `mapatoms'. */);
4751 DEFVAR_LISP ("values", Vvalues,
4752 doc: /* List of values of all expressions which were read, evaluated and printed.
4753 Order is reverse chronological. */);
4754 XSYMBOL (intern ("values"))->u.s.declared_special = false;
4756 DEFVAR_LISP ("standard-input", Vstandard_input,
4757 doc: /* Stream for read to get input from.
4758 See documentation of `read' for possible values. */);
4759 Vstandard_input = Qt;
4761 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4762 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4764 If this variable is a buffer, then only forms read from that buffer
4765 will be added to `read-symbol-positions-list'.
4766 If this variable is t, then all read forms will be added.
4767 The effect of all other values other than nil are not currently
4768 defined, although they may be in the future.
4770 The positions are relative to the last call to `read' or
4771 `read-from-string'. It is probably a bad idea to set this variable at
4772 the toplevel; bind it instead. */);
4773 Vread_with_symbol_positions = Qnil;
4775 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4776 doc: /* A list mapping read symbols to their positions.
4777 This variable is modified during calls to `read' or
4778 `read-from-string', but only when `read-with-symbol-positions' is
4779 non-nil.
4781 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4782 CHAR-POSITION is an integer giving the offset of that occurrence of the
4783 symbol from the position where `read' or `read-from-string' started.
4785 Note that a symbol will appear multiple times in this list, if it was
4786 read multiple times. The list is in the same order as the symbols
4787 were read in. */);
4788 Vread_symbol_positions_list = Qnil;
4790 DEFVAR_LISP ("read-circle", Vread_circle,
4791 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4792 Vread_circle = Qt;
4794 DEFVAR_LISP ("load-path", Vload_path,
4795 doc: /* List of directories to search for files to load.
4796 Each element is a string (directory file name) or nil (meaning
4797 `default-directory').
4798 This list is consulted by the `require' function.
4799 Initialized during startup as described in Info node `(elisp)Library Search'.
4800 Use `directory-file-name' when adding items to this path. However, Lisp
4801 programs that process this list should tolerate directories both with
4802 and without trailing slashes. */);
4804 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4805 doc: /* List of suffixes for Emacs Lisp files and dynamic modules.
4806 This list includes suffixes for both compiled and source Emacs Lisp files.
4807 This list should not include the empty string.
4808 `load' and related functions try to append these suffixes, in order,
4809 to the specified file name if a suffix is allowed or required. */);
4810 #ifdef HAVE_MODULES
4811 Vload_suffixes = list3 (build_pure_c_string (".elc"),
4812 build_pure_c_string (".el"),
4813 build_pure_c_string (MODULES_SUFFIX));
4814 #else
4815 Vload_suffixes = list2 (build_pure_c_string (".elc"),
4816 build_pure_c_string (".el"));
4817 #endif
4818 DEFVAR_LISP ("module-file-suffix", Vmodule_file_suffix,
4819 doc: /* Suffix of loadable module file, or nil if modules are not supported. */);
4820 #ifdef HAVE_MODULES
4821 Vmodule_file_suffix = build_pure_c_string (MODULES_SUFFIX);
4822 #else
4823 Vmodule_file_suffix = Qnil;
4824 #endif
4825 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4826 doc: /* List of suffixes that indicate representations of \
4827 the same file.
4828 This list should normally start with the empty string.
4830 Enabling Auto Compression mode appends the suffixes in
4831 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4832 mode removes them again. `load' and related functions use this list to
4833 determine whether they should look for compressed versions of a file
4834 and, if so, which suffixes they should try to append to the file name
4835 in order to do so. However, if you want to customize which suffixes
4836 the loading functions recognize as compression suffixes, you should
4837 customize `jka-compr-load-suffixes' rather than the present variable. */);
4838 Vload_file_rep_suffixes = list1 (empty_unibyte_string);
4840 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4841 doc: /* Non-nil if inside of `load'. */);
4842 DEFSYM (Qload_in_progress, "load-in-progress");
4844 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4845 doc: /* An alist of functions to be evalled when particular files are loaded.
4846 Each element looks like (REGEXP-OR-FEATURE FUNCS...).
4848 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4849 a symbol (a feature name).
4851 When `load' is run and the file-name argument matches an element's
4852 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4853 REGEXP-OR-FEATURE, the FUNCS in the element are called.
4855 An error in FORMS does not undo the load, but does prevent execution of
4856 the rest of the FORMS. */);
4857 Vafter_load_alist = Qnil;
4859 DEFVAR_LISP ("load-history", Vload_history,
4860 doc: /* Alist mapping loaded file names to symbols and features.
4861 Each alist element should be a list (FILE-NAME ENTRIES...), where
4862 FILE-NAME is the name of a file that has been loaded into Emacs.
4863 The file name is absolute and true (i.e. it doesn't contain symlinks).
4864 As an exception, one of the alist elements may have FILE-NAME nil,
4865 for symbols and features not associated with any file.
4867 The remaining ENTRIES in the alist element describe the functions and
4868 variables defined in that file, the features provided, and the
4869 features required. Each entry has the form `(provide . FEATURE)',
4870 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4871 `(defface . SYMBOL)', `(define-type . SYMBOL)',
4872 `(cl-defmethod METHOD SPECIALIZERS)', or `(t . SYMBOL)'.
4873 Entries like `(t . SYMBOL)' may precede a `(defun . FUNCTION)' entry,
4874 and means that SYMBOL was an autoload before this file redefined it
4875 as a function. In addition, entries may also be single symbols,
4876 which means that symbol was defined by `defvar' or `defconst'.
4878 During preloading, the file name recorded is relative to the main Lisp
4879 directory. These file names are converted to absolute at startup. */);
4880 Vload_history = Qnil;
4882 DEFVAR_LISP ("load-file-name", Vload_file_name,
4883 doc: /* Full name of file being loaded by `load'. */);
4884 Vload_file_name = Qnil;
4886 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4887 doc: /* File name, including directory, of user's initialization file.
4888 If the file loaded had extension `.elc', and the corresponding source file
4889 exists, this variable contains the name of source file, suitable for use
4890 by functions like `custom-save-all' which edit the init file.
4891 While Emacs loads and evaluates the init file, value is the real name
4892 of the file, regardless of whether or not it has the `.elc' extension. */);
4893 Vuser_init_file = Qnil;
4895 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4896 doc: /* Used for internal purposes by `load'. */);
4897 Vcurrent_load_list = Qnil;
4899 DEFVAR_LISP ("load-read-function", Vload_read_function,
4900 doc: /* Function used by `load' and `eval-region' for reading expressions.
4901 Called with a single argument (the stream from which to read).
4902 The default is to use the function `read'. */);
4903 DEFSYM (Qread, "read");
4904 Vload_read_function = Qread;
4906 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4907 doc: /* Function called in `load' to load an Emacs Lisp source file.
4908 The value should be a function for doing code conversion before
4909 reading a source file. It can also be nil, in which case loading is
4910 done without any code conversion.
4912 If the value is a function, it is called with four arguments,
4913 FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4914 the file to load, FILE is the non-absolute name (for messages etc.),
4915 and NOERROR and NOMESSAGE are the corresponding arguments passed to
4916 `load'. The function should return t if the file was loaded. */);
4917 Vload_source_file_function = Qnil;
4919 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4920 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4921 This is useful when the file being loaded is a temporary copy. */);
4922 load_force_doc_strings = 0;
4924 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4925 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4926 This is normally bound by `load' and `eval-buffer' to control `read',
4927 and is not meant for users to change. */);
4928 load_convert_to_unibyte = 0;
4930 DEFVAR_LISP ("source-directory", Vsource_directory,
4931 doc: /* Directory in which Emacs sources were found when Emacs was built.
4932 You cannot count on them to still be there! */);
4933 Vsource_directory
4934 = Fexpand_file_name (build_string ("../"),
4935 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
4937 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
4938 doc: /* List of files that were preloaded (when dumping Emacs). */);
4939 Vpreloaded_file_list = Qnil;
4941 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
4942 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4943 Vbyte_boolean_vars = Qnil;
4945 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
4946 doc: /* Non-nil means load dangerous compiled Lisp files.
4947 Some versions of XEmacs use different byte codes than Emacs. These
4948 incompatible byte codes can make Emacs crash when it tries to execute
4949 them. */);
4950 load_dangerous_libraries = 0;
4952 DEFVAR_BOOL ("force-load-messages", force_load_messages,
4953 doc: /* Non-nil means force printing messages when loading Lisp files.
4954 This overrides the value of the NOMESSAGE argument to `load'. */);
4955 force_load_messages = 0;
4957 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
4958 doc: /* Regular expression matching safe to load compiled Lisp files.
4959 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4960 from the file, and matches them against this regular expression.
4961 When the regular expression matches, the file is considered to be safe
4962 to load. See also `load-dangerous-libraries'. */);
4963 Vbytecomp_version_regexp
4964 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4966 DEFSYM (Qlexical_binding, "lexical-binding");
4967 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
4968 doc: /* Whether to use lexical binding when evaluating code.
4969 Non-nil means that the code in the current buffer should be evaluated
4970 with lexical binding.
4971 This variable is automatically set from the file variables of an
4972 interpreted Lisp file read using `load'. Unlike other file local
4973 variables, this must be set in the first line of a file. */);
4974 Vlexical_binding = Qnil;
4975 Fmake_variable_buffer_local (Qlexical_binding);
4977 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
4978 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4979 Veval_buffer_list = Qnil;
4981 DEFVAR_LISP ("lread--old-style-backquotes", Vlread_old_style_backquotes,
4982 doc: /* Set to non-nil when `read' encounters an old-style backquote.
4983 For internal use only. */);
4984 Vlread_old_style_backquotes = Qnil;
4985 DEFSYM (Qlread_old_style_backquotes, "lread--old-style-backquotes");
4987 DEFVAR_LISP ("lread--unescaped-character-literals",
4988 Vlread_unescaped_character_literals,
4989 doc: /* List of deprecated unescaped character literals encountered by `read'.
4990 For internal use only. */);
4991 Vlread_unescaped_character_literals = Qnil;
4992 DEFSYM (Qlread_unescaped_character_literals,
4993 "lread--unescaped-character-literals");
4995 DEFSYM (Qlss, "<");
4996 DEFSYM (Qchar, "char");
4997 DEFSYM (Qformat, "format");
4999 DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
5000 doc: /* Non-nil means `load' prefers the newest version of a file.
5001 This applies when a filename suffix is not explicitly specified and
5002 `load' is trying various possible suffixes (see `load-suffixes' and
5003 `load-file-rep-suffixes'). Normally, it stops at the first file
5004 that exists unless you explicitly specify one or the other. If this
5005 option is non-nil, it checks all suffixes and uses whichever file is
5006 newest.
5007 Note that if you customize this, obviously it will not affect files
5008 that are loaded before your customizations are read! */);
5009 load_prefer_newer = 0;
5011 /* Vsource_directory was initialized in init_lread. */
5013 DEFSYM (Qcurrent_load_list, "current-load-list");
5014 DEFSYM (Qstandard_input, "standard-input");
5015 DEFSYM (Qread_char, "read-char");
5016 DEFSYM (Qget_file_char, "get-file-char");
5018 /* Used instead of Qget_file_char while loading *.elc files compiled
5019 by Emacs 21 or older. */
5020 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
5022 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
5024 DEFSYM (Qbackquote, "`");
5025 DEFSYM (Qcomma, ",");
5026 DEFSYM (Qcomma_at, ",@");
5027 DEFSYM (Qcomma_dot, ",.");
5029 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
5030 DEFSYM (Qascii_character, "ascii-character");
5031 DEFSYM (Qfunction, "function");
5032 DEFSYM (Qload, "load");
5033 DEFSYM (Qload_file_name, "load-file-name");
5034 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
5035 DEFSYM (Qfile_truename, "file-truename");
5036 DEFSYM (Qdir_ok, "dir-ok");
5037 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
5039 staticpro (&read_objects_map);
5040 read_objects_map = Qnil;
5041 staticpro (&read_objects_completed);
5042 read_objects_completed = Qnil;
5044 Vloads_in_progress = Qnil;
5045 staticpro (&Vloads_in_progress);
5047 DEFSYM (Qhash_table, "hash-table");
5048 DEFSYM (Qdata, "data");
5049 DEFSYM (Qtest, "test");
5050 DEFSYM (Qsize, "size");
5051 DEFSYM (Qpurecopy, "purecopy");
5052 DEFSYM (Qweakness, "weakness");
5053 DEFSYM (Qrehash_size, "rehash-size");
5054 DEFSYM (Qrehash_threshold, "rehash-threshold");
5056 DEFSYM (Qchar_from_name, "char-from-name");