; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / src / lread.c
blob4229ff568bee5416c11cd0ed2a7d20db50bc66cf
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 with no surrounding
151 parentheses. Fread initializes this to the value of
152 `force_new_style_backquotes', so we need not specbind it or worry
153 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 static void build_load_history (Lisp_Object, bool);
170 /* Functions that read one byte from the current source READCHARFUN
171 or unreads one byte. If the integer argument C is -1, it returns
172 one read byte, or -1 when there's no more byte in the source. If C
173 is 0 or positive, it unreads C, and the return value is not
174 interesting. */
176 static int readbyte_for_lambda (int, Lisp_Object);
177 static int readbyte_from_file (int, Lisp_Object);
178 static int readbyte_from_string (int, Lisp_Object);
180 /* Handle unreading and rereading of characters.
181 Write READCHAR to read a character,
182 UNREAD(c) to unread c to be read again.
184 These macros correctly read/unread multibyte characters. */
186 #define READCHAR readchar (readcharfun, NULL)
187 #define UNREAD(c) unreadchar (readcharfun, c)
189 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
190 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
192 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
193 Qlambda, or a cons, we use this to keep an unread character because
194 a file stream can't handle multibyte-char unreading. The value -1
195 means that there's no unread character. */
196 static int unread_char;
198 static int
199 readchar (Lisp_Object readcharfun, bool *multibyte)
201 Lisp_Object tem;
202 register int c;
203 int (*readbyte) (int, Lisp_Object);
204 unsigned char buf[MAX_MULTIBYTE_LENGTH];
205 int i, len;
206 bool emacs_mule_encoding = 0;
208 if (multibyte)
209 *multibyte = 0;
211 readchar_count++;
213 if (BUFFERP (readcharfun))
215 register struct buffer *inbuffer = XBUFFER (readcharfun);
217 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
219 if (! BUFFER_LIVE_P (inbuffer))
220 return -1;
222 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
223 return -1;
225 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
227 /* Fetch the character code from the buffer. */
228 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
229 BUF_INC_POS (inbuffer, pt_byte);
230 c = STRING_CHAR (p);
231 if (multibyte)
232 *multibyte = 1;
234 else
236 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
237 if (! ASCII_CHAR_P (c))
238 c = BYTE8_TO_CHAR (c);
239 pt_byte++;
241 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
243 return c;
245 if (MARKERP (readcharfun))
247 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
249 ptrdiff_t bytepos = marker_byte_position (readcharfun);
251 if (bytepos >= BUF_ZV_BYTE (inbuffer))
252 return -1;
254 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
256 /* Fetch the character code from the buffer. */
257 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
258 BUF_INC_POS (inbuffer, bytepos);
259 c = STRING_CHAR (p);
260 if (multibyte)
261 *multibyte = 1;
263 else
265 c = BUF_FETCH_BYTE (inbuffer, bytepos);
266 if (! ASCII_CHAR_P (c))
267 c = BYTE8_TO_CHAR (c);
268 bytepos++;
271 XMARKER (readcharfun)->bytepos = bytepos;
272 XMARKER (readcharfun)->charpos++;
274 return c;
277 if (EQ (readcharfun, Qlambda))
279 readbyte = readbyte_for_lambda;
280 goto read_multibyte;
283 if (EQ (readcharfun, Qget_file_char))
285 readbyte = readbyte_from_file;
286 goto read_multibyte;
289 if (STRINGP (readcharfun))
291 if (read_from_string_index >= read_from_string_limit)
292 c = -1;
293 else if (STRING_MULTIBYTE (readcharfun))
295 if (multibyte)
296 *multibyte = 1;
297 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
298 read_from_string_index,
299 read_from_string_index_byte);
301 else
303 c = SREF (readcharfun, read_from_string_index_byte);
304 read_from_string_index++;
305 read_from_string_index_byte++;
307 return c;
310 if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
312 /* This is the case that read_vector is reading from a unibyte
313 string that contains a byte sequence previously skipped
314 because of #@NUMBER. The car part of readcharfun is that
315 string, and the cdr part is a value of readcharfun given to
316 read_vector. */
317 readbyte = readbyte_from_string;
318 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
319 emacs_mule_encoding = 1;
320 goto read_multibyte;
323 if (EQ (readcharfun, Qget_emacs_mule_file_char))
325 readbyte = readbyte_from_file;
326 emacs_mule_encoding = 1;
327 goto read_multibyte;
330 tem = call0 (readcharfun);
332 if (NILP (tem))
333 return -1;
334 return XINT (tem);
336 read_multibyte:
337 if (unread_char >= 0)
339 c = unread_char;
340 unread_char = -1;
341 return c;
343 c = (*readbyte) (-1, readcharfun);
344 if (c < 0)
345 return c;
346 if (multibyte)
347 *multibyte = 1;
348 if (ASCII_CHAR_P (c))
349 return c;
350 if (emacs_mule_encoding)
351 return read_emacs_mule_char (c, readbyte, readcharfun);
352 i = 0;
353 buf[i++] = c;
354 len = BYTES_BY_CHAR_HEAD (c);
355 while (i < len)
357 buf[i++] = c = (*readbyte) (-1, readcharfun);
358 if (c < 0 || ! TRAILING_CODE_P (c))
360 for (i -= c < 0; 0 < --i; )
361 (*readbyte) (buf[i], readcharfun);
362 return BYTE8_TO_CHAR (buf[0]);
365 return STRING_CHAR (buf);
368 #define FROM_FILE_P(readcharfun) \
369 (EQ (readcharfun, Qget_file_char) \
370 || EQ (readcharfun, Qget_emacs_mule_file_char))
372 static void
373 skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
375 if (FROM_FILE_P (readcharfun))
377 block_input (); /* FIXME: Not sure if it's needed. */
378 fseek (infile->stream, n - infile->lookahead, SEEK_CUR);
379 unblock_input ();
380 infile->lookahead = 0;
382 else
383 { /* We're not reading directly from a file. In that case, it's difficult
384 to reliably count bytes, since these are usually meant for the file's
385 encoding, whereas we're now typically in the internal encoding.
386 But luckily, skip_dyn_bytes is used to skip over a single
387 dynamic-docstring (or dynamic byte-code) which is always quoted such
388 that \037 is the final char. */
389 int c;
390 do {
391 c = READCHAR;
392 } while (c >= 0 && c != '\037');
396 static void
397 skip_dyn_eof (Lisp_Object readcharfun)
399 if (FROM_FILE_P (readcharfun))
401 block_input (); /* FIXME: Not sure if it's needed. */
402 fseek (infile->stream, 0, SEEK_END);
403 unblock_input ();
404 infile->lookahead = 0;
406 else
407 while (READCHAR >= 0);
410 /* Unread the character C in the way appropriate for the stream READCHARFUN.
411 If the stream is a user function, call it with the char as argument. */
413 static void
414 unreadchar (Lisp_Object readcharfun, int c)
416 readchar_count--;
417 if (c == -1)
418 /* Don't back up the pointer if we're unreading the end-of-input mark,
419 since readchar didn't advance it when we read it. */
421 else if (BUFFERP (readcharfun))
423 struct buffer *b = XBUFFER (readcharfun);
424 ptrdiff_t charpos = BUF_PT (b);
425 ptrdiff_t bytepos = BUF_PT_BYTE (b);
427 if (! NILP (BVAR (b, enable_multibyte_characters)))
428 BUF_DEC_POS (b, bytepos);
429 else
430 bytepos--;
432 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
434 else if (MARKERP (readcharfun))
436 struct buffer *b = XMARKER (readcharfun)->buffer;
437 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
439 XMARKER (readcharfun)->charpos--;
440 if (! NILP (BVAR (b, enable_multibyte_characters)))
441 BUF_DEC_POS (b, bytepos);
442 else
443 bytepos--;
445 XMARKER (readcharfun)->bytepos = bytepos;
447 else if (STRINGP (readcharfun))
449 read_from_string_index--;
450 read_from_string_index_byte
451 = string_char_to_byte (readcharfun, read_from_string_index);
453 else if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
455 unread_char = c;
457 else if (EQ (readcharfun, Qlambda))
459 unread_char = c;
461 else if (FROM_FILE_P (readcharfun))
463 unread_char = c;
465 else
466 call1 (readcharfun, make_number (c));
469 static int
470 readbyte_for_lambda (int c, Lisp_Object readcharfun)
472 return read_bytecode_char (c >= 0);
476 static int
477 readbyte_from_stdio (void)
479 if (infile->lookahead)
480 return infile->buf[--infile->lookahead];
482 int c;
483 FILE *instream = infile->stream;
485 block_input ();
487 /* Interrupted reads have been observed while reading over the network. */
488 while ((c = getc_unlocked (instream)) == EOF && errno == EINTR
489 && ferror_unlocked (instream))
491 unblock_input ();
492 maybe_quit ();
493 block_input ();
494 clearerr_unlocked (instream);
497 unblock_input ();
499 return (c == EOF ? -1 : c);
502 static int
503 readbyte_from_file (int c, Lisp_Object readcharfun)
505 if (c >= 0)
507 eassert (infile->lookahead < sizeof infile->buf);
508 infile->buf[infile->lookahead++] = c;
509 return 0;
512 return readbyte_from_stdio ();
515 static int
516 readbyte_from_string (int c, Lisp_Object readcharfun)
518 Lisp_Object string = XCAR (readcharfun);
520 if (c >= 0)
522 read_from_string_index--;
523 read_from_string_index_byte
524 = string_char_to_byte (string, read_from_string_index);
527 if (read_from_string_index >= read_from_string_limit)
528 c = -1;
529 else
530 FETCH_STRING_CHAR_ADVANCE (c, string,
531 read_from_string_index,
532 read_from_string_index_byte);
533 return c;
537 /* Read one non-ASCII character from INFILE. The character is
538 encoded in `emacs-mule' and the first byte is already read in
539 C. */
541 static int
542 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
544 /* Emacs-mule coding uses at most 4-byte for one character. */
545 unsigned char buf[4];
546 int len = emacs_mule_bytes[c];
547 struct charset *charset;
548 int i;
549 unsigned code;
551 if (len == 1)
552 /* C is not a valid leading-code of `emacs-mule'. */
553 return BYTE8_TO_CHAR (c);
555 i = 0;
556 buf[i++] = c;
557 while (i < len)
559 buf[i++] = c = (*readbyte) (-1, readcharfun);
560 if (c < 0xA0)
562 for (i -= c < 0; 0 < --i; )
563 (*readbyte) (buf[i], readcharfun);
564 return BYTE8_TO_CHAR (buf[0]);
568 if (len == 2)
570 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
571 code = buf[1] & 0x7F;
573 else if (len == 3)
575 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
576 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
578 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
579 code = buf[2] & 0x7F;
581 else
583 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
584 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
587 else
589 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
590 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
592 c = DECODE_CHAR (charset, code);
593 if (c < 0)
594 Fsignal (Qinvalid_read_syntax,
595 list1 (build_string ("invalid multibyte form")));
596 return c;
600 /* An in-progress substitution of OBJECT for PLACEHOLDER. */
601 struct subst
603 Lisp_Object object;
604 Lisp_Object placeholder;
606 /* Hash table of subobjects of OBJECT that might be circular. If
607 Qt, all such objects might be circular. */
608 Lisp_Object completed;
610 /* List of subobjects of OBJECT that have already been visited. */
611 Lisp_Object seen;
614 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
615 Lisp_Object);
616 static Lisp_Object read0 (Lisp_Object);
617 static Lisp_Object read1 (Lisp_Object, int *, bool);
619 static Lisp_Object read_list (bool, Lisp_Object);
620 static Lisp_Object read_vector (Lisp_Object, bool);
622 static Lisp_Object substitute_object_recurse (struct subst *, Lisp_Object);
623 static void substitute_in_interval (INTERVAL, void *);
626 /* Get a character from the tty. */
628 /* Read input events until we get one that's acceptable for our purposes.
630 If NO_SWITCH_FRAME, switch-frame events are stashed
631 until we get a character we like, and then stuffed into
632 unread_switch_frame.
634 If ASCII_REQUIRED, check function key events to see
635 if the unmodified version of the symbol has a Qascii_character
636 property, and use that character, if present.
638 If ERROR_NONASCII, signal an error if the input we
639 get isn't an ASCII character with modifiers. If it's false but
640 ASCII_REQUIRED is true, just re-read until we get an ASCII
641 character.
643 If INPUT_METHOD, invoke the current input method
644 if the character warrants that.
646 If SECONDS is a number, wait that many seconds for input, and
647 return Qnil if no input arrives within that time. */
649 static Lisp_Object
650 read_filtered_event (bool no_switch_frame, bool ascii_required,
651 bool error_nonascii, bool input_method, Lisp_Object seconds)
653 Lisp_Object val, delayed_switch_frame;
654 struct timespec end_time;
656 #ifdef HAVE_WINDOW_SYSTEM
657 if (display_hourglass_p)
658 cancel_hourglass ();
659 #endif
661 delayed_switch_frame = Qnil;
663 /* Compute timeout. */
664 if (NUMBERP (seconds))
666 double duration = XFLOATINT (seconds);
667 struct timespec wait_time = dtotimespec (duration);
668 end_time = timespec_add (current_timespec (), wait_time);
671 /* Read until we get an acceptable event. */
672 retry:
674 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
675 NUMBERP (seconds) ? &end_time : NULL);
676 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
678 if (BUFFERP (val))
679 goto retry;
681 /* `switch-frame' events are put off until after the next ASCII
682 character. This is better than signaling an error just because
683 the last characters were typed to a separate minibuffer frame,
684 for example. Eventually, some code which can deal with
685 switch-frame events will read it and process it. */
686 if (no_switch_frame
687 && EVENT_HAS_PARAMETERS (val)
688 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
690 delayed_switch_frame = val;
691 goto retry;
694 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
696 /* Convert certain symbols to their ASCII equivalents. */
697 if (SYMBOLP (val))
699 Lisp_Object tem, tem1;
700 tem = Fget (val, Qevent_symbol_element_mask);
701 if (!NILP (tem))
703 tem1 = Fget (Fcar (tem), Qascii_character);
704 /* Merge this symbol's modifier bits
705 with the ASCII equivalent of its basic code. */
706 if (!NILP (tem1))
707 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
711 /* If we don't have a character now, deal with it appropriately. */
712 if (!INTEGERP (val))
714 if (error_nonascii)
716 Vunread_command_events = list1 (val);
717 error ("Non-character input-event");
719 else
720 goto retry;
724 if (! NILP (delayed_switch_frame))
725 unread_switch_frame = delayed_switch_frame;
727 #if 0
729 #ifdef HAVE_WINDOW_SYSTEM
730 if (display_hourglass_p)
731 start_hourglass ();
732 #endif
734 #endif
736 return val;
739 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
740 doc: /* Read a character from the command input (keyboard or macro).
741 It is returned as a number.
742 If the character has modifiers, they are resolved and reflected to the
743 character code if possible (e.g. C-SPC -> 0).
745 If the user generates an event which is not a character (i.e. a mouse
746 click or function key event), `read-char' signals an error. As an
747 exception, switch-frame events are put off until non-character events
748 can be read.
749 If you want to read non-character events, or ignore them, call
750 `read-event' or `read-char-exclusive' instead.
752 If the optional argument PROMPT is non-nil, display that as a prompt.
753 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
754 input method is turned on in the current buffer, that input method
755 is used for reading a character.
756 If the optional argument SECONDS is non-nil, it should be a number
757 specifying the maximum number of seconds to wait for input. If no
758 input arrives in that time, return nil. SECONDS may be a
759 floating-point value. */)
760 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
762 Lisp_Object val;
764 if (! NILP (prompt))
765 message_with_string ("%s", prompt, 0);
766 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
768 return (NILP (val) ? Qnil
769 : make_number (char_resolve_modifier_mask (XINT (val))));
772 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
773 doc: /* Read an event object from the input stream.
774 If the optional argument PROMPT is non-nil, display that as a prompt.
775 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
776 input method is turned on in the current buffer, that input method
777 is used for reading a character.
778 If the optional argument SECONDS is non-nil, it should be a number
779 specifying the maximum number of seconds to wait for input. If no
780 input arrives in that time, return nil. SECONDS may be a
781 floating-point value. */)
782 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
784 if (! NILP (prompt))
785 message_with_string ("%s", prompt, 0);
786 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
789 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
790 doc: /* Read a character from the command input (keyboard or macro).
791 It is returned as a number. Non-character events are ignored.
792 If the character has modifiers, they are resolved and reflected to the
793 character code if possible (e.g. C-SPC -> 0).
795 If the optional argument PROMPT is non-nil, display that as a prompt.
796 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
797 input method is turned on in the current buffer, that input method
798 is used for reading a character.
799 If the optional argument SECONDS is non-nil, it should be a number
800 specifying the maximum number of seconds to wait for input. If no
801 input arrives in that time, return nil. SECONDS may be a
802 floating-point value. */)
803 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
805 Lisp_Object val;
807 if (! NILP (prompt))
808 message_with_string ("%s", prompt, 0);
810 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
812 return (NILP (val) ? Qnil
813 : make_number (char_resolve_modifier_mask (XINT (val))));
816 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
817 doc: /* Don't use this yourself. */)
818 (void)
820 if (!infile)
821 error ("get-file-char misused");
822 return make_number (readbyte_from_stdio ());
828 /* Return true if the lisp code read using READCHARFUN defines a non-nil
829 `lexical-binding' file variable. After returning, the stream is
830 positioned following the first line, if it is a comment or #! line,
831 otherwise nothing is read. */
833 static bool
834 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
836 int ch = READCHAR;
838 if (ch == '#')
840 ch = READCHAR;
841 if (ch != '!')
843 UNREAD (ch);
844 UNREAD ('#');
845 return 0;
847 while (ch != '\n' && ch != EOF)
848 ch = READCHAR;
849 if (ch == '\n') ch = READCHAR;
850 /* It is OK to leave the position after a #! line, since
851 that is what read1 does. */
854 if (ch != ';')
855 /* The first line isn't a comment, just give up. */
857 UNREAD (ch);
858 return 0;
860 else
861 /* Look for an appropriate file-variable in the first line. */
863 bool rv = 0;
864 enum {
865 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX
866 } beg_end_state = NOMINAL;
867 bool in_file_vars = 0;
869 #define UPDATE_BEG_END_STATE(ch) \
870 if (beg_end_state == NOMINAL) \
871 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
872 else if (beg_end_state == AFTER_FIRST_DASH) \
873 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
874 else if (beg_end_state == AFTER_ASTERIX) \
876 if (ch == '-') \
877 in_file_vars = !in_file_vars; \
878 beg_end_state = NOMINAL; \
881 /* Skip until we get to the file vars, if any. */
884 ch = READCHAR;
885 UPDATE_BEG_END_STATE (ch);
887 while (!in_file_vars && ch != '\n' && ch != EOF);
889 while (in_file_vars)
891 char var[100], val[100];
892 unsigned i;
894 ch = READCHAR;
896 /* Read a variable name. */
897 while (ch == ' ' || ch == '\t')
898 ch = READCHAR;
900 i = 0;
901 beg_end_state = NOMINAL;
902 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
904 if (i < sizeof var - 1)
905 var[i++] = ch;
906 UPDATE_BEG_END_STATE (ch);
907 ch = READCHAR;
910 /* Stop scanning if no colon was found before end marker. */
911 if (!in_file_vars || ch == '\n' || ch == EOF)
912 break;
914 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
915 i--;
916 var[i] = '\0';
918 if (ch == ':')
920 /* Read a variable value. */
921 ch = READCHAR;
923 while (ch == ' ' || ch == '\t')
924 ch = READCHAR;
926 i = 0;
927 beg_end_state = NOMINAL;
928 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
930 if (i < sizeof val - 1)
931 val[i++] = ch;
932 UPDATE_BEG_END_STATE (ch);
933 ch = READCHAR;
935 if (! in_file_vars)
936 /* The value was terminated by an end-marker, which remove. */
937 i -= 3;
938 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
939 i--;
940 val[i] = '\0';
942 if (strcmp (var, "lexical-binding") == 0)
943 /* This is it... */
945 rv = (strcmp (val, "nil") != 0);
946 break;
951 while (ch != '\n' && ch != EOF)
952 ch = READCHAR;
954 return rv;
958 /* Value is a version number of byte compiled code if the file
959 associated with file descriptor FD is a compiled Lisp file that's
960 safe to load. Only files compiled with Emacs are safe to load.
961 Files compiled with XEmacs can lead to a crash in Fbyte_code
962 because of an incompatible change in the byte compiler. */
964 static int
965 safe_to_load_version (int fd)
967 char buf[512];
968 int nbytes, i;
969 int version = 1;
971 /* Read the first few bytes from the file, and look for a line
972 specifying the byte compiler version used. */
973 nbytes = emacs_read_quit (fd, buf, sizeof buf);
974 if (nbytes > 0)
976 /* Skip to the next newline, skipping over the initial `ELC'
977 with NUL bytes following it, but note the version. */
978 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
979 if (i == 4)
980 version = buf[i];
982 if (i >= nbytes
983 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
984 buf + i, nbytes - i) < 0)
985 version = 0;
988 lseek (fd, 0, SEEK_SET);
989 return version;
993 /* Callback for record_unwind_protect. Restore the old load list OLD,
994 after loading a file successfully. */
996 static void
997 record_load_unwind (Lisp_Object old)
999 Vloads_in_progress = old;
1002 /* This handler function is used via internal_condition_case_1. */
1004 static Lisp_Object
1005 load_error_handler (Lisp_Object data)
1007 return Qnil;
1010 static _Noreturn void
1011 load_error_old_style_backquotes (void)
1013 if (NILP (Vload_file_name))
1014 xsignal1 (Qerror, build_string ("Old-style backquotes detected!"));
1015 else
1017 AUTO_STRING (format, "Loading `%s': old-style backquotes detected!");
1018 xsignal1 (Qerror, CALLN (Fformat_message, format, Vload_file_name));
1022 static void
1023 load_warn_unescaped_character_literals (Lisp_Object file)
1025 if (NILP (Vlread_unescaped_character_literals)) return;
1026 CHECK_CONS (Vlread_unescaped_character_literals);
1027 Lisp_Object format =
1028 build_string ("Loading `%s': unescaped character literals %s detected!");
1029 Lisp_Object separator = build_string (", ");
1030 Lisp_Object inner_format = build_string ("`?%c'");
1031 CALLN (Fmessage,
1032 format, file,
1033 Fmapconcat (list3 (Qlambda, list1 (Qchar),
1034 list3 (Qformat, inner_format, Qchar)),
1035 Fsort (Vlread_unescaped_character_literals, Qlss),
1036 separator));
1039 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
1040 doc: /* Return the suffixes that `load' should try if a suffix is \
1041 required.
1042 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
1043 (void)
1045 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
1046 while (CONSP (suffixes))
1048 Lisp_Object exts = Vload_file_rep_suffixes;
1049 suffix = XCAR (suffixes);
1050 suffixes = XCDR (suffixes);
1051 while (CONSP (exts))
1053 ext = XCAR (exts);
1054 exts = XCDR (exts);
1055 lst = Fcons (concat2 (suffix, ext), lst);
1058 return Fnreverse (lst);
1061 /* Returns true if STRING ends with SUFFIX */
1062 static bool
1063 suffix_p (Lisp_Object string, const char *suffix)
1065 ptrdiff_t suffix_len = strlen (suffix);
1066 ptrdiff_t string_len = SBYTES (string);
1068 return string_len >= suffix_len && !strcmp (SSDATA (string) + string_len - suffix_len, suffix);
1071 static void
1072 close_infile_unwind (void *arg)
1074 FILE *stream = arg;
1075 eassert (infile == NULL || infile->stream == stream);
1076 infile = NULL;
1077 fclose (stream);
1080 DEFUN ("load", Fload, Sload, 1, 5, 0,
1081 doc: /* Execute a file of Lisp code named FILE.
1082 First try FILE with `.elc' appended, then try with `.el', then try
1083 with a system-dependent suffix of dynamic modules (see `load-suffixes'),
1084 then try FILE unmodified (the exact suffixes in the exact order are
1085 determined by `load-suffixes'). Environment variable references in
1086 FILE are replaced with their values by calling `substitute-in-file-name'.
1087 This function searches the directories in `load-path'.
1089 If optional second arg NOERROR is non-nil,
1090 report no error if FILE doesn't exist.
1091 Print messages at start and end of loading unless
1092 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
1093 overrides that).
1094 If optional fourth arg NOSUFFIX is non-nil, don't try adding
1095 suffixes to the specified name FILE.
1096 If optional fifth arg MUST-SUFFIX is non-nil, insist on
1097 the suffix `.elc' or `.el' or the module suffix; don't accept just
1098 FILE unless it ends in one of those suffixes or includes a directory name.
1100 If NOSUFFIX is nil, then if a file could not be found, try looking for
1101 a different representation of the file by adding non-empty suffixes to
1102 its name, before trying another file. Emacs uses this feature to find
1103 compressed versions of files when Auto Compression mode is enabled.
1104 If NOSUFFIX is non-nil, disable this feature.
1106 The suffixes that this function tries out, when NOSUFFIX is nil, are
1107 given by the return value of `get-load-suffixes' and the values listed
1108 in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1109 return value of `get-load-suffixes' is used, i.e. the file name is
1110 required to have a non-empty suffix.
1112 When searching suffixes, this function normally stops at the first
1113 one that exists. If the option `load-prefer-newer' is non-nil,
1114 however, it tries all suffixes, and uses whichever file is the newest.
1116 Loading a file records its definitions, and its `provide' and
1117 `require' calls, in an element of `load-history' whose
1118 car is the file name loaded. See `load-history'.
1120 While the file is in the process of being loaded, the variable
1121 `load-in-progress' is non-nil and the variable `load-file-name'
1122 is bound to the file's name.
1124 Return t if the file exists and loads successfully. */)
1125 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage,
1126 Lisp_Object nosuffix, Lisp_Object must_suffix)
1128 FILE *stream UNINIT;
1129 int fd;
1130 int fd_index UNINIT;
1131 ptrdiff_t count = SPECPDL_INDEX ();
1132 Lisp_Object found, efound, hist_file_name;
1133 /* True means we printed the ".el is newer" message. */
1134 bool newer = 0;
1135 /* True means we are loading a compiled file. */
1136 bool compiled = 0;
1137 Lisp_Object handler;
1138 bool safe_p = 1;
1139 const char *fmode = "r" FOPEN_TEXT;
1140 int version;
1142 CHECK_STRING (file);
1144 /* If file name is magic, call the handler. */
1145 /* This shouldn't be necessary any more now that `openp' handles it right.
1146 handler = Ffind_file_name_handler (file, Qload);
1147 if (!NILP (handler))
1148 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1150 /* The presence of this call is the result of a historical accident:
1151 it used to be in every file-operation and when it got removed
1152 everywhere, it accidentally stayed here. Since then, enough people
1153 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1154 that it seemed risky to remove. */
1155 if (! NILP (noerror))
1157 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1158 Qt, load_error_handler);
1159 if (NILP (file))
1160 return Qnil;
1162 else
1163 file = Fsubstitute_in_file_name (file);
1165 /* Avoid weird lossage with null string as arg,
1166 since it would try to load a directory as a Lisp file. */
1167 if (SCHARS (file) == 0)
1169 fd = -1;
1170 errno = ENOENT;
1172 else
1174 Lisp_Object suffixes;
1175 found = Qnil;
1177 if (! NILP (must_suffix))
1179 /* Don't insist on adding a suffix if FILE already ends with one. */
1180 if (suffix_p (file, ".el")
1181 || suffix_p (file, ".elc")
1182 #ifdef HAVE_MODULES
1183 || suffix_p (file, MODULES_SUFFIX)
1184 #endif
1186 must_suffix = Qnil;
1187 /* Don't insist on adding a suffix
1188 if the argument includes a directory name. */
1189 else if (! NILP (Ffile_name_directory (file)))
1190 must_suffix = Qnil;
1193 if (!NILP (nosuffix))
1194 suffixes = Qnil;
1195 else
1197 suffixes = Fget_load_suffixes ();
1198 if (NILP (must_suffix))
1199 suffixes = CALLN (Fappend, suffixes, Vload_file_rep_suffixes);
1202 fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
1205 if (fd == -1)
1207 if (NILP (noerror))
1208 report_file_error ("Cannot open load file", file);
1209 return Qnil;
1212 /* Tell startup.el whether or not we found the user's init file. */
1213 if (EQ (Qt, Vuser_init_file))
1214 Vuser_init_file = found;
1216 /* If FD is -2, that means openp found a magic file. */
1217 if (fd == -2)
1219 if (NILP (Fequal (found, file)))
1220 /* If FOUND is a different file name from FILE,
1221 find its handler even if we have already inhibited
1222 the `load' operation on FILE. */
1223 handler = Ffind_file_name_handler (found, Qt);
1224 else
1225 handler = Ffind_file_name_handler (found, Qload);
1226 if (! NILP (handler))
1227 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1228 #ifdef DOS_NT
1229 /* Tramp has to deal with semi-broken packages that prepend
1230 drive letters to remote files. For that reason, Tramp
1231 catches file operations that test for file existence, which
1232 makes openp think X:/foo.elc files are remote. However,
1233 Tramp does not catch `load' operations for such files, so we
1234 end up with a nil as the `load' handler above. If we would
1235 continue with fd = -2, we will behave wrongly, and in
1236 particular try reading a .elc file in the "rt" mode instead
1237 of "rb". See bug #9311 for the results. To work around
1238 this, we try to open the file locally, and go with that if it
1239 succeeds. */
1240 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1241 if (fd == -1)
1242 fd = -2;
1243 #endif
1246 if (0 <= fd)
1248 fd_index = SPECPDL_INDEX ();
1249 record_unwind_protect_int (close_file_unwind, fd);
1252 #ifdef HAVE_MODULES
1253 bool is_module = suffix_p (found, MODULES_SUFFIX);
1254 #else
1255 bool is_module = false;
1256 #endif
1258 /* Check if we're stuck in a recursive load cycle.
1260 2000-09-21: It's not possible to just check for the file loaded
1261 being a member of Vloads_in_progress. This fails because of the
1262 way the byte compiler currently works; `provide's are not
1263 evaluated, see font-lock.el/jit-lock.el as an example. This
1264 leads to a certain amount of ``normal'' recursion.
1266 Also, just loading a file recursively is not always an error in
1267 the general case; the second load may do something different. */
1269 int load_count = 0;
1270 Lisp_Object tem;
1271 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1272 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1273 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1274 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1275 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1278 /* All loads are by default dynamic, unless the file itself specifies
1279 otherwise using a file-variable in the first line. This is bound here
1280 so that it takes effect whether or not we use
1281 Vload_source_file_function. */
1282 specbind (Qlexical_binding, Qnil);
1284 /* Get the name for load-history. */
1285 hist_file_name = (! NILP (Vpurify_flag)
1286 ? concat2 (Ffile_name_directory (file),
1287 Ffile_name_nondirectory (found))
1288 : found) ;
1290 version = -1;
1292 /* Check for the presence of unescaped character literals and warn
1293 about them. */
1294 specbind (Qlread_unescaped_character_literals, Qnil);
1295 record_unwind_protect (load_warn_unescaped_character_literals, file);
1297 int is_elc;
1298 if ((is_elc = suffix_p (found, ".elc")) != 0
1299 /* version = 1 means the file is empty, in which case we can
1300 treat it as not byte-compiled. */
1301 || (fd >= 0 && (version = safe_to_load_version (fd)) > 1))
1302 /* Load .elc files directly, but not when they are
1303 remote and have no handler! */
1305 if (fd != -2)
1307 struct stat s1, s2;
1308 int result;
1310 if (version < 0
1311 && ! (version = safe_to_load_version (fd)))
1313 safe_p = 0;
1314 if (!load_dangerous_libraries)
1315 error ("File `%s' was not compiled in Emacs", SDATA (found));
1316 else if (!NILP (nomessage) && !force_load_messages)
1317 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1320 compiled = 1;
1322 efound = ENCODE_FILE (found);
1323 fmode = "r" FOPEN_BINARY;
1325 /* openp already checked for newness, no point doing it again.
1326 FIXME would be nice to get a message when openp
1327 ignores suffix order due to load_prefer_newer. */
1328 if (!load_prefer_newer && is_elc)
1330 result = stat (SSDATA (efound), &s1);
1331 if (result == 0)
1333 SSET (efound, SBYTES (efound) - 1, 0);
1334 result = stat (SSDATA (efound), &s2);
1335 SSET (efound, SBYTES (efound) - 1, 'c');
1338 if (result == 0
1339 && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
1341 /* Make the progress messages mention that source is newer. */
1342 newer = 1;
1344 /* If we won't print another message, mention this anyway. */
1345 if (!NILP (nomessage) && !force_load_messages)
1347 Lisp_Object msg_file;
1348 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1349 message_with_string ("Source file `%s' newer than byte-compiled file",
1350 msg_file, 1);
1353 } /* !load_prefer_newer */
1356 else if (!is_module)
1358 /* We are loading a source file (*.el). */
1359 if (!NILP (Vload_source_file_function))
1361 Lisp_Object val;
1363 if (fd >= 0)
1365 emacs_close (fd);
1366 clear_unwind_protect (fd_index);
1368 val = call4 (Vload_source_file_function, found, hist_file_name,
1369 NILP (noerror) ? Qnil : Qt,
1370 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1371 return unbind_to (count, val);
1375 if (fd < 0)
1377 /* We somehow got here with fd == -2, meaning the file is deemed
1378 to be remote. Don't even try to reopen the file locally;
1379 just force a failure. */
1380 stream = NULL;
1381 errno = EINVAL;
1383 else if (!is_module)
1385 #ifdef WINDOWSNT
1386 emacs_close (fd);
1387 clear_unwind_protect (fd_index);
1388 efound = ENCODE_FILE (found);
1389 stream = emacs_fopen (SSDATA (efound), fmode);
1390 #else
1391 stream = fdopen (fd, fmode);
1392 #endif
1395 if (is_module)
1397 /* `module-load' uses the file name, so we can close the stream
1398 now. */
1399 if (fd >= 0)
1401 emacs_close (fd);
1402 clear_unwind_protect (fd_index);
1405 else
1407 if (! stream)
1408 report_file_error ("Opening stdio stream", file);
1409 set_unwind_protect_ptr (fd_index, close_infile_unwind, stream);
1412 if (! NILP (Vpurify_flag))
1413 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1415 if (NILP (nomessage) || force_load_messages)
1417 if (!safe_p)
1418 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1419 file, 1);
1420 else if (is_module)
1421 message_with_string ("Loading %s (module)...", file, 1);
1422 else if (!compiled)
1423 message_with_string ("Loading %s (source)...", file, 1);
1424 else if (newer)
1425 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1426 file, 1);
1427 else /* The typical case; compiled file newer than source file. */
1428 message_with_string ("Loading %s...", file, 1);
1431 specbind (Qload_file_name, found);
1432 specbind (Qinhibit_file_name_operation, Qnil);
1433 specbind (Qload_in_progress, Qt);
1435 if (is_module)
1437 #ifdef HAVE_MODULES
1438 specbind (Qcurrent_load_list, Qnil);
1439 LOADHIST_ATTACH (found);
1440 Fmodule_load (found);
1441 build_load_history (found, true);
1442 #else
1443 /* This cannot happen. */
1444 emacs_abort ();
1445 #endif
1447 else
1449 struct infile input;
1450 input.stream = stream;
1451 input.lookahead = 0;
1452 infile = &input;
1454 if (lisp_file_lexically_bound_p (Qget_file_char))
1455 Fset (Qlexical_binding, Qt);
1457 if (! version || version >= 22)
1458 readevalloop (Qget_file_char, &input, hist_file_name,
1459 0, Qnil, Qnil, Qnil, Qnil);
1460 else
1462 /* We can't handle a file which was compiled with
1463 byte-compile-dynamic by older version of Emacs. */
1464 specbind (Qload_force_doc_strings, Qt);
1465 readevalloop (Qget_emacs_mule_file_char, &input, hist_file_name,
1466 0, Qnil, Qnil, Qnil, Qnil);
1469 unbind_to (count, Qnil);
1471 /* Run any eval-after-load forms for this file. */
1472 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1473 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1475 xfree (saved_doc_string);
1476 saved_doc_string = 0;
1477 saved_doc_string_size = 0;
1479 xfree (prev_saved_doc_string);
1480 prev_saved_doc_string = 0;
1481 prev_saved_doc_string_size = 0;
1483 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1485 if (!safe_p)
1486 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1487 file, 1);
1488 else if (is_module)
1489 message_with_string ("Loading %s (module)...done", file, 1);
1490 else if (!compiled)
1491 message_with_string ("Loading %s (source)...done", file, 1);
1492 else if (newer)
1493 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1494 file, 1);
1495 else /* The typical case; compiled file newer than source file. */
1496 message_with_string ("Loading %s...done", file, 1);
1499 return Qt;
1502 static bool
1503 complete_filename_p (Lisp_Object pathname)
1505 const unsigned char *s = SDATA (pathname);
1506 return (IS_DIRECTORY_SEP (s[0])
1507 || (SCHARS (pathname) > 2
1508 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1511 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1512 doc: /* Search for FILENAME through PATH.
1513 Returns the file's name in absolute form, or nil if not found.
1514 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1515 file name when searching.
1516 If non-nil, PREDICATE is used instead of `file-readable-p'.
1517 PREDICATE can also be an integer to pass to the faccessat(2) function,
1518 in which case file-name-handlers are ignored.
1519 This function will normally skip directories, so if you want it to find
1520 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1521 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1523 Lisp_Object file;
1524 int fd = openp (path, filename, suffixes, &file, predicate, false);
1525 if (NILP (predicate) && fd >= 0)
1526 emacs_close (fd);
1527 return file;
1530 /* Search for a file whose name is STR, looking in directories
1531 in the Lisp list PATH, and trying suffixes from SUFFIX.
1532 On success, return a file descriptor (or 1 or -2 as described below).
1533 On failure, return -1 and set errno.
1535 SUFFIXES is a list of strings containing possible suffixes.
1536 The empty suffix is automatically added if the list is empty.
1538 PREDICATE t means the files are binary.
1539 PREDICATE non-nil and non-t means don't open the files,
1540 just look for one that satisfies the predicate. In this case,
1541 return -2 on success. The predicate can be a lisp function or
1542 an integer to pass to `access' (in which case file-name-handlers
1543 are ignored).
1545 If STOREPTR is nonzero, it points to a slot where the name of
1546 the file actually found should be stored as a Lisp string.
1547 nil is stored there on failure.
1549 If the file we find is remote, return -2
1550 but store the found remote file name in *STOREPTR.
1552 If NEWER is true, try all SUFFIXes and return the result for the
1553 newest file that exists. Does not apply to remote files,
1554 or if a non-nil and non-t PREDICATE is specified. */
1557 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
1558 Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
1560 ptrdiff_t fn_size = 100;
1561 char buf[100];
1562 char *fn = buf;
1563 bool absolute;
1564 ptrdiff_t want_length;
1565 Lisp_Object filename;
1566 Lisp_Object string, tail, encoded_fn, save_string;
1567 ptrdiff_t max_suffix_len = 0;
1568 int last_errno = ENOENT;
1569 int save_fd = -1;
1570 USE_SAFE_ALLOCA;
1572 /* The last-modified time of the newest matching file found.
1573 Initialize it to something less than all valid timestamps. */
1574 struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
1576 CHECK_STRING (str);
1578 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1580 CHECK_STRING_CAR (tail);
1581 max_suffix_len = max (max_suffix_len,
1582 SBYTES (XCAR (tail)));
1585 string = filename = encoded_fn = save_string = Qnil;
1587 if (storeptr)
1588 *storeptr = Qnil;
1590 absolute = complete_filename_p (str);
1592 /* Go through all entries in the path and see whether we find the
1593 executable. */
1594 do {
1595 ptrdiff_t baselen, prefixlen;
1597 if (NILP (path))
1598 filename = str;
1599 else
1600 filename = Fexpand_file_name (str, XCAR (path));
1601 if (!complete_filename_p (filename))
1602 /* If there are non-absolute elts in PATH (eg "."). */
1603 /* Of course, this could conceivably lose if luser sets
1604 default-directory to be something non-absolute... */
1606 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1607 if (!complete_filename_p (filename))
1608 /* Give up on this path element! */
1609 continue;
1612 /* Calculate maximum length of any filename made from
1613 this path element/specified file name and any possible suffix. */
1614 want_length = max_suffix_len + SBYTES (filename);
1615 if (fn_size <= want_length)
1617 fn_size = 100 + want_length;
1618 fn = SAFE_ALLOCA (fn_size);
1621 /* Copy FILENAME's data to FN but remove starting /: if any. */
1622 prefixlen = ((SCHARS (filename) > 2
1623 && SREF (filename, 0) == '/'
1624 && SREF (filename, 1) == ':')
1625 ? 2 : 0);
1626 baselen = SBYTES (filename) - prefixlen;
1627 memcpy (fn, SDATA (filename) + prefixlen, baselen);
1629 /* Loop over suffixes. */
1630 for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
1631 CONSP (tail); tail = XCDR (tail))
1633 Lisp_Object suffix = XCAR (tail);
1634 ptrdiff_t fnlen, lsuffix = SBYTES (suffix);
1635 Lisp_Object handler;
1637 /* Make complete filename by appending SUFFIX. */
1638 memcpy (fn + baselen, SDATA (suffix), lsuffix + 1);
1639 fnlen = baselen + lsuffix;
1641 /* Check that the file exists and is not a directory. */
1642 /* We used to only check for handlers on non-absolute file names:
1643 if (absolute)
1644 handler = Qnil;
1645 else
1646 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1647 It's not clear why that was the case and it breaks things like
1648 (load "/bar.el") where the file is actually "/bar.el.gz". */
1649 /* make_string has its own ideas on when to return a unibyte
1650 string and when a multibyte string, but we know better.
1651 We must have a unibyte string when dumping, since
1652 file-name encoding is shaky at best at that time, and in
1653 particular default-file-name-coding-system is reset
1654 several times during loadup. We therefore don't want to
1655 encode the file before passing it to file I/O library
1656 functions. */
1657 if (!STRING_MULTIBYTE (filename) && !STRING_MULTIBYTE (suffix))
1658 string = make_unibyte_string (fn, fnlen);
1659 else
1660 string = make_string (fn, fnlen);
1661 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1662 if ((!NILP (handler) || (!NILP (predicate) && !EQ (predicate, Qt)))
1663 && !NATNUMP (predicate))
1665 bool exists;
1666 if (NILP (predicate) || EQ (predicate, Qt))
1667 exists = !NILP (Ffile_readable_p (string));
1668 else
1670 Lisp_Object tmp = call1 (predicate, string);
1671 if (NILP (tmp))
1672 exists = false;
1673 else if (EQ (tmp, Qdir_ok)
1674 || NILP (Ffile_directory_p (string)))
1675 exists = true;
1676 else
1678 exists = false;
1679 last_errno = EISDIR;
1683 if (exists)
1685 /* We succeeded; return this descriptor and filename. */
1686 if (storeptr)
1687 *storeptr = string;
1688 SAFE_FREE ();
1689 return -2;
1692 else
1694 int fd;
1695 const char *pfn;
1696 struct stat st;
1698 encoded_fn = ENCODE_FILE (string);
1699 pfn = SSDATA (encoded_fn);
1701 /* Check that we can access or open it. */
1702 if (NATNUMP (predicate))
1704 fd = -1;
1705 if (INT_MAX < XFASTINT (predicate))
1706 last_errno = EINVAL;
1707 else if (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
1708 AT_EACCESS)
1709 == 0)
1711 if (file_directory_p (encoded_fn))
1712 last_errno = EISDIR;
1713 else
1714 fd = 1;
1717 else
1719 fd = emacs_open (pfn, O_RDONLY, 0);
1720 if (fd < 0)
1722 if (errno != ENOENT)
1723 last_errno = errno;
1725 else
1727 int err = (fstat (fd, &st) != 0 ? errno
1728 : S_ISDIR (st.st_mode) ? EISDIR : 0);
1729 if (err)
1731 last_errno = err;
1732 emacs_close (fd);
1733 fd = -1;
1738 if (fd >= 0)
1740 if (newer && !NATNUMP (predicate))
1742 struct timespec mtime = get_stat_mtime (&st);
1744 if (timespec_cmp (mtime, save_mtime) <= 0)
1745 emacs_close (fd);
1746 else
1748 if (0 <= save_fd)
1749 emacs_close (save_fd);
1750 save_fd = fd;
1751 save_mtime = mtime;
1752 save_string = string;
1755 else
1757 /* We succeeded; return this descriptor and filename. */
1758 if (storeptr)
1759 *storeptr = string;
1760 SAFE_FREE ();
1761 return fd;
1765 /* No more suffixes. Return the newest. */
1766 if (0 <= save_fd && ! CONSP (XCDR (tail)))
1768 if (storeptr)
1769 *storeptr = save_string;
1770 SAFE_FREE ();
1771 return save_fd;
1775 if (absolute || NILP (path))
1776 break;
1777 path = XCDR (path);
1778 } while (CONSP (path));
1780 SAFE_FREE ();
1781 errno = last_errno;
1782 return -1;
1786 /* Merge the list we've accumulated of globals from the current input source
1787 into the load_history variable. The details depend on whether
1788 the source has an associated file name or not.
1790 FILENAME is the file name that we are loading from.
1792 ENTIRE is true if loading that entire file, false if evaluating
1793 part of it. */
1795 static void
1796 build_load_history (Lisp_Object filename, bool entire)
1798 Lisp_Object tail, prev, newelt;
1799 Lisp_Object tem, tem2;
1800 bool foundit = 0;
1802 tail = Vload_history;
1803 prev = Qnil;
1805 while (CONSP (tail))
1807 tem = XCAR (tail);
1809 /* Find the feature's previous assoc list... */
1810 if (!NILP (Fequal (filename, Fcar (tem))))
1812 foundit = 1;
1814 /* If we're loading the entire file, remove old data. */
1815 if (entire)
1817 if (NILP (prev))
1818 Vload_history = XCDR (tail);
1819 else
1820 Fsetcdr (prev, XCDR (tail));
1823 /* Otherwise, cons on new symbols that are not already members. */
1824 else
1826 tem2 = Vcurrent_load_list;
1828 while (CONSP (tem2))
1830 newelt = XCAR (tem2);
1832 if (NILP (Fmember (newelt, tem)))
1833 Fsetcar (tail, Fcons (XCAR (tem),
1834 Fcons (newelt, XCDR (tem))));
1836 tem2 = XCDR (tem2);
1837 maybe_quit ();
1841 else
1842 prev = tail;
1843 tail = XCDR (tail);
1844 maybe_quit ();
1847 /* If we're loading an entire file, cons the new assoc onto the
1848 front of load-history, the most-recently-loaded position. Also
1849 do this if we didn't find an existing member for the file. */
1850 if (entire || !foundit)
1851 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1852 Vload_history);
1855 static void
1856 readevalloop_1 (int old)
1858 load_convert_to_unibyte = old;
1861 /* Signal an `end-of-file' error, if possible with file name
1862 information. */
1864 static _Noreturn void
1865 end_of_file_error (void)
1867 if (STRINGP (Vload_file_name))
1868 xsignal1 (Qend_of_file, Vload_file_name);
1870 xsignal0 (Qend_of_file);
1873 static Lisp_Object
1874 readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
1876 /* If we macroexpand the toplevel form non-recursively and it ends
1877 up being a `progn' (or if it was a progn to start), treat each
1878 form in the progn as a top-level form. This way, if one form in
1879 the progn defines a macro, that macro is in effect when we expand
1880 the remaining forms. See similar code in bytecomp.el. */
1881 val = call2 (macroexpand, val, Qnil);
1882 if (EQ (CAR_SAFE (val), Qprogn))
1884 Lisp_Object subforms = XCDR (val);
1886 for (val = Qnil; CONSP (subforms); subforms = XCDR (subforms))
1887 val = readevalloop_eager_expand_eval (XCAR (subforms),
1888 macroexpand);
1890 else
1891 val = eval_sub (call2 (macroexpand, val, Qt));
1892 return val;
1895 /* UNIBYTE specifies how to set load_convert_to_unibyte
1896 for this invocation.
1897 READFUN, if non-nil, is used instead of `read'.
1899 START, END specify region to read in current buffer (from eval-region).
1900 If the input is not from a buffer, they must be nil. */
1902 static void
1903 readevalloop (Lisp_Object readcharfun,
1904 struct infile *infile0,
1905 Lisp_Object sourcename,
1906 bool printflag,
1907 Lisp_Object unibyte, Lisp_Object readfun,
1908 Lisp_Object start, Lisp_Object end)
1910 int c;
1911 Lisp_Object val;
1912 ptrdiff_t count = SPECPDL_INDEX ();
1913 struct buffer *b = 0;
1914 bool continue_reading_p;
1915 Lisp_Object lex_bound;
1916 /* True if reading an entire buffer. */
1917 bool whole_buffer = 0;
1918 /* True on the first time around. */
1919 bool first_sexp = 1;
1920 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1922 if (NILP (Ffboundp (macroexpand))
1923 /* Don't macroexpand in .elc files, since it should have been done
1924 already. We actually don't know whether we're in a .elc file or not,
1925 so we use circumstantial evidence: .el files normally go through
1926 Vload_source_file_function -> load-with-code-conversion
1927 -> eval-buffer. */
1928 || EQ (readcharfun, Qget_file_char)
1929 || EQ (readcharfun, Qget_emacs_mule_file_char))
1930 macroexpand = Qnil;
1932 if (MARKERP (readcharfun))
1934 if (NILP (start))
1935 start = readcharfun;
1938 if (BUFFERP (readcharfun))
1939 b = XBUFFER (readcharfun);
1940 else if (MARKERP (readcharfun))
1941 b = XMARKER (readcharfun)->buffer;
1943 /* We assume START is nil when input is not from a buffer. */
1944 if (! NILP (start) && !b)
1945 emacs_abort ();
1947 specbind (Qstandard_input, readcharfun);
1948 specbind (Qcurrent_load_list, Qnil);
1949 record_unwind_protect_int (readevalloop_1, load_convert_to_unibyte);
1950 load_convert_to_unibyte = !NILP (unibyte);
1952 /* If lexical binding is active (either because it was specified in
1953 the file's header, or via a buffer-local variable), create an empty
1954 lexical environment, otherwise, turn off lexical binding. */
1955 lex_bound = find_symbol_value (Qlexical_binding);
1956 specbind (Qinternal_interpreter_environment,
1957 (NILP (lex_bound) || EQ (lex_bound, Qunbound)
1958 ? Qnil : list1 (Qt)));
1960 /* Try to ensure sourcename is a truename, except whilst preloading. */
1961 if (NILP (Vpurify_flag)
1962 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1963 && !NILP (Ffboundp (Qfile_truename)))
1964 sourcename = call1 (Qfile_truename, sourcename) ;
1966 LOADHIST_ATTACH (sourcename);
1968 continue_reading_p = 1;
1969 while (continue_reading_p)
1971 ptrdiff_t count1 = SPECPDL_INDEX ();
1973 if (b != 0 && !BUFFER_LIVE_P (b))
1974 error ("Reading from killed buffer");
1976 if (!NILP (start))
1978 /* Switch to the buffer we are reading from. */
1979 record_unwind_protect_excursion ();
1980 set_buffer_internal (b);
1982 /* Save point in it. */
1983 record_unwind_protect_excursion ();
1984 /* Save ZV in it. */
1985 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1986 /* Those get unbound after we read one expression. */
1988 /* Set point and ZV around stuff to be read. */
1989 Fgoto_char (start);
1990 if (!NILP (end))
1991 Fnarrow_to_region (make_number (BEGV), end);
1993 /* Just for cleanliness, convert END to a marker
1994 if it is an integer. */
1995 if (INTEGERP (end))
1996 end = Fpoint_max_marker ();
1999 /* On the first cycle, we can easily test here
2000 whether we are reading the whole buffer. */
2001 if (b && first_sexp)
2002 whole_buffer = (BUF_PT (b) == BUF_BEG (b) && BUF_ZV (b) == BUF_Z (b));
2004 infile = infile0;
2005 read_next:
2006 c = READCHAR;
2007 if (c == ';')
2009 while ((c = READCHAR) != '\n' && c != -1);
2010 goto read_next;
2012 if (c < 0)
2014 unbind_to (count1, Qnil);
2015 break;
2018 /* Ignore whitespace here, so we can detect eof. */
2019 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
2020 || c == NO_BREAK_SPACE)
2021 goto read_next;
2023 if (! HASH_TABLE_P (read_objects_map)
2024 || XHASH_TABLE (read_objects_map)->count)
2025 read_objects_map
2026 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE,
2027 DEFAULT_REHASH_SIZE, DEFAULT_REHASH_THRESHOLD,
2028 Qnil, false);
2029 if (! HASH_TABLE_P (read_objects_completed)
2030 || XHASH_TABLE (read_objects_completed)->count)
2031 read_objects_completed
2032 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE,
2033 DEFAULT_REHASH_SIZE, DEFAULT_REHASH_THRESHOLD,
2034 Qnil, false);
2035 if (!NILP (Vpurify_flag) && c == '(')
2037 val = read_list (0, readcharfun);
2039 else
2041 UNREAD (c);
2042 if (!NILP (readfun))
2044 val = call1 (readfun, readcharfun);
2046 /* If READCHARFUN has set point to ZV, we should
2047 stop reading, even if the form read sets point
2048 to a different value when evaluated. */
2049 if (BUFFERP (readcharfun))
2051 struct buffer *buf = XBUFFER (readcharfun);
2052 if (BUF_PT (buf) == BUF_ZV (buf))
2053 continue_reading_p = 0;
2056 else if (! NILP (Vload_read_function))
2057 val = call1 (Vload_read_function, readcharfun);
2058 else
2059 val = read_internal_start (readcharfun, Qnil, Qnil);
2061 /* Empty hashes can be reused; otherwise, reset on next call. */
2062 if (HASH_TABLE_P (read_objects_map)
2063 && XHASH_TABLE (read_objects_map)->count > 0)
2064 read_objects_map = Qnil;
2065 if (HASH_TABLE_P (read_objects_completed)
2066 && XHASH_TABLE (read_objects_completed)->count > 0)
2067 read_objects_completed = Qnil;
2069 if (!NILP (start) && continue_reading_p)
2070 start = Fpoint_marker ();
2072 /* Restore saved point and BEGV. */
2073 unbind_to (count1, Qnil);
2075 /* Now eval what we just read. */
2076 if (!NILP (macroexpand))
2077 val = readevalloop_eager_expand_eval (val, macroexpand);
2078 else
2079 val = eval_sub (val);
2081 if (printflag)
2083 Vvalues = Fcons (val, Vvalues);
2084 if (EQ (Vstandard_output, Qt))
2085 Fprin1 (val, Qnil);
2086 else
2087 Fprint (val, Qnil);
2090 first_sexp = 0;
2093 build_load_history (sourcename,
2094 infile0 || whole_buffer);
2096 unbind_to (count, Qnil);
2099 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
2100 doc: /* Execute the accessible portion of current buffer as Lisp code.
2101 You can use \\[narrow-to-region] to limit the part of buffer to be evaluated.
2102 When called from a Lisp program (i.e., not interactively), this
2103 function accepts up to five optional arguments:
2104 BUFFER is the buffer to evaluate (nil means use current buffer),
2105 or a name of a buffer (a string).
2106 PRINTFLAG controls printing of output by any output functions in the
2107 evaluated code, such as `print', `princ', and `prin1':
2108 a value of nil means discard it; anything else is the stream to print to.
2109 See Info node `(elisp)Output Streams' for details on streams.
2110 FILENAME specifies the file name to use for `load-history'.
2111 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
2112 invocation.
2113 DO-ALLOW-PRINT, if non-nil, specifies that output functions in the
2114 evaluated code should work normally even if PRINTFLAG is nil, in
2115 which case the output is displayed in the echo area.
2117 This function preserves the position of point. */)
2118 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
2120 ptrdiff_t count = SPECPDL_INDEX ();
2121 Lisp_Object tem, buf;
2123 if (NILP (buffer))
2124 buf = Fcurrent_buffer ();
2125 else
2126 buf = Fget_buffer (buffer);
2127 if (NILP (buf))
2128 error ("No such buffer");
2130 if (NILP (printflag) && NILP (do_allow_print))
2131 tem = Qsymbolp;
2132 else
2133 tem = printflag;
2135 if (NILP (filename))
2136 filename = BVAR (XBUFFER (buf), filename);
2138 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
2139 specbind (Qstandard_output, tem);
2140 record_unwind_protect_excursion ();
2141 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
2142 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
2143 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
2144 readevalloop (buf, 0, filename,
2145 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
2146 unbind_to (count, Qnil);
2148 return Qnil;
2151 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
2152 doc: /* Execute the region as Lisp code.
2153 When called from programs, expects two arguments,
2154 giving starting and ending indices in the current buffer
2155 of the text to be executed.
2156 Programs can pass third argument PRINTFLAG which controls output:
2157 a value of nil means discard it; anything else is stream for printing it.
2158 See Info node `(elisp)Output Streams' for details on streams.
2159 Also the fourth argument READ-FUNCTION, if non-nil, is used
2160 instead of `read' to read each expression. It gets one argument
2161 which is the input stream for reading characters.
2163 This function does not move point. */)
2164 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
2166 /* FIXME: Do the eval-sexp-add-defvars dance! */
2167 ptrdiff_t count = SPECPDL_INDEX ();
2168 Lisp_Object tem, cbuf;
2170 cbuf = Fcurrent_buffer ();
2172 if (NILP (printflag))
2173 tem = Qsymbolp;
2174 else
2175 tem = printflag;
2176 specbind (Qstandard_output, tem);
2177 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
2179 /* `readevalloop' calls functions which check the type of start and end. */
2180 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
2181 !NILP (printflag), Qnil, read_function,
2182 start, end);
2184 return unbind_to (count, Qnil);
2188 DEFUN ("read", Fread, Sread, 0, 1, 0,
2189 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
2190 If STREAM is nil, use the value of `standard-input' (which see).
2191 STREAM or the value of `standard-input' may be:
2192 a buffer (read from point and advance it)
2193 a marker (read from where it points and advance it)
2194 a function (call it with no arguments for each character,
2195 call it with a char as argument to push a char back)
2196 a string (takes text from string, starting at the beginning)
2197 t (read text line using minibuffer and use it, or read from
2198 standard input in batch mode). */)
2199 (Lisp_Object stream)
2201 if (NILP (stream))
2202 stream = Vstandard_input;
2203 if (EQ (stream, Qt))
2204 stream = Qread_char;
2205 if (EQ (stream, Qread_char))
2206 /* FIXME: ?! When is this used !? */
2207 return call1 (intern ("read-minibuffer"),
2208 build_string ("Lisp expression: "));
2210 return read_internal_start (stream, Qnil, Qnil);
2213 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
2214 doc: /* Read one Lisp expression which is represented as text by STRING.
2215 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
2216 FINAL-STRING-INDEX is an integer giving the position of the next
2217 remaining character in STRING. START and END optionally delimit
2218 a substring of STRING from which to read; they default to 0 and
2219 \(length STRING) respectively. Negative values are counted from
2220 the end of STRING. */)
2221 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
2223 Lisp_Object ret;
2224 CHECK_STRING (string);
2225 /* `read_internal_start' sets `read_from_string_index'. */
2226 ret = read_internal_start (string, start, end);
2227 return Fcons (ret, make_number (read_from_string_index));
2230 /* Function to set up the global context we need in toplevel read
2231 calls. START and END only used when STREAM is a string. */
2232 static Lisp_Object
2233 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
2235 Lisp_Object retval;
2237 readchar_count = 0;
2238 new_backquote_flag = force_new_style_backquotes;
2239 /* We can get called from readevalloop which may have set these
2240 already. */
2241 if (! HASH_TABLE_P (read_objects_map)
2242 || XHASH_TABLE (read_objects_map)->count)
2243 read_objects_map
2244 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE, DEFAULT_REHASH_SIZE,
2245 DEFAULT_REHASH_THRESHOLD, Qnil, false);
2246 if (! HASH_TABLE_P (read_objects_completed)
2247 || XHASH_TABLE (read_objects_completed)->count)
2248 read_objects_completed
2249 = make_hash_table (hashtest_eq, DEFAULT_HASH_SIZE, DEFAULT_REHASH_SIZE,
2250 DEFAULT_REHASH_THRESHOLD, Qnil, false);
2251 if (EQ (Vread_with_symbol_positions, Qt)
2252 || EQ (Vread_with_symbol_positions, stream))
2253 Vread_symbol_positions_list = Qnil;
2255 if (STRINGP (stream)
2256 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2258 ptrdiff_t startval, endval;
2259 Lisp_Object string;
2261 if (STRINGP (stream))
2262 string = stream;
2263 else
2264 string = XCAR (stream);
2266 validate_subarray (string, start, end, SCHARS (string),
2267 &startval, &endval);
2269 read_from_string_index = startval;
2270 read_from_string_index_byte = string_char_to_byte (string, startval);
2271 read_from_string_limit = endval;
2274 retval = read0 (stream);
2275 if (EQ (Vread_with_symbol_positions, Qt)
2276 || EQ (Vread_with_symbol_positions, stream))
2277 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2278 /* Empty hashes can be reused; otherwise, reset on next call. */
2279 if (HASH_TABLE_P (read_objects_map)
2280 && XHASH_TABLE (read_objects_map)->count > 0)
2281 read_objects_map = Qnil;
2282 if (HASH_TABLE_P (read_objects_completed)
2283 && XHASH_TABLE (read_objects_completed)->count > 0)
2284 read_objects_completed = Qnil;
2285 return retval;
2289 /* Signal Qinvalid_read_syntax error.
2290 S is error string of length N (if > 0) */
2292 static _Noreturn void
2293 invalid_syntax (const char *s)
2295 xsignal1 (Qinvalid_read_syntax, build_string (s));
2299 /* Use this for recursive reads, in contexts where internal tokens
2300 are not allowed. */
2302 static Lisp_Object
2303 read0 (Lisp_Object readcharfun)
2305 register Lisp_Object val;
2306 int c;
2308 val = read1 (readcharfun, &c, 0);
2309 if (!c)
2310 return val;
2312 xsignal1 (Qinvalid_read_syntax,
2313 Fmake_string (make_number (1), make_number (c), Qnil));
2316 /* Grow a read buffer BUF that contains OFFSET useful bytes of data,
2317 by at least MAX_MULTIBYTE_LENGTH bytes. Update *BUF_ADDR and
2318 *BUF_SIZE accordingly; 0 <= OFFSET <= *BUF_SIZE. If *BUF_ADDR is
2319 initially null, BUF is on the stack: copy its data to the new heap
2320 buffer. Otherwise, BUF must equal *BUF_ADDR and can simply be
2321 reallocated. Either way, remember the heap allocation (which is at
2322 pdl slot COUNT) so that it can be freed when unwinding the stack.*/
2324 static char *
2325 grow_read_buffer (char *buf, ptrdiff_t offset,
2326 char **buf_addr, ptrdiff_t *buf_size, ptrdiff_t count)
2328 char *p = xpalloc (*buf_addr, buf_size, MAX_MULTIBYTE_LENGTH, -1, 1);
2329 if (!*buf_addr)
2331 memcpy (p, buf, offset);
2332 record_unwind_protect_ptr (xfree, p);
2334 else
2335 set_unwind_protect_ptr (count, xfree, p);
2336 *buf_addr = p;
2337 return p;
2340 /* Return the scalar value that has the Unicode character name NAME.
2341 Raise 'invalid-read-syntax' if there is no such character. */
2342 static int
2343 character_name_to_code (char const *name, ptrdiff_t name_len)
2345 /* For "U+XXXX", pass the leading '+' to string_to_number to reject
2346 monstrosities like "U+-0000". */
2347 Lisp_Object code
2348 = (name[0] == 'U' && name[1] == '+'
2349 ? string_to_number (name + 1, 16, 0)
2350 : call2 (Qchar_from_name, make_unibyte_string (name, name_len), Qt));
2352 if (! RANGED_INTEGERP (0, code, MAX_UNICODE_CHAR)
2353 || char_surrogate_p (XINT (code)))
2355 AUTO_STRING (format, "\\N{%s}");
2356 AUTO_STRING_WITH_LEN (namestr, name, name_len);
2357 xsignal1 (Qinvalid_read_syntax, CALLN (Fformat, format, namestr));
2360 return XINT (code);
2363 /* Bound on the length of a Unicode character name. As of
2364 Unicode 9.0.0 the maximum is 83, so this should be safe. */
2365 enum { UNICODE_CHARACTER_NAME_LENGTH_BOUND = 200 };
2367 /* Read a \-escape sequence, assuming we already read the `\'.
2368 If the escape sequence forces unibyte, return eight-bit char. */
2370 static int
2371 read_escape (Lisp_Object readcharfun, bool stringp)
2373 int c = READCHAR;
2374 /* \u allows up to four hex digits, \U up to eight. Default to the
2375 behavior for \u, and change this value in the case that \U is seen. */
2376 int unicode_hex_count = 4;
2378 switch (c)
2380 case -1:
2381 end_of_file_error ();
2383 case 'a':
2384 return '\007';
2385 case 'b':
2386 return '\b';
2387 case 'd':
2388 return 0177;
2389 case 'e':
2390 return 033;
2391 case 'f':
2392 return '\f';
2393 case 'n':
2394 return '\n';
2395 case 'r':
2396 return '\r';
2397 case 't':
2398 return '\t';
2399 case 'v':
2400 return '\v';
2401 case '\n':
2402 return -1;
2403 case ' ':
2404 if (stringp)
2405 return -1;
2406 return ' ';
2408 case 'M':
2409 c = READCHAR;
2410 if (c != '-')
2411 error ("Invalid escape character syntax");
2412 c = READCHAR;
2413 if (c == '\\')
2414 c = read_escape (readcharfun, 0);
2415 return c | meta_modifier;
2417 case 'S':
2418 c = READCHAR;
2419 if (c != '-')
2420 error ("Invalid escape character syntax");
2421 c = READCHAR;
2422 if (c == '\\')
2423 c = read_escape (readcharfun, 0);
2424 return c | shift_modifier;
2426 case 'H':
2427 c = READCHAR;
2428 if (c != '-')
2429 error ("Invalid escape character syntax");
2430 c = READCHAR;
2431 if (c == '\\')
2432 c = read_escape (readcharfun, 0);
2433 return c | hyper_modifier;
2435 case 'A':
2436 c = READCHAR;
2437 if (c != '-')
2438 error ("Invalid escape character syntax");
2439 c = READCHAR;
2440 if (c == '\\')
2441 c = read_escape (readcharfun, 0);
2442 return c | alt_modifier;
2444 case 's':
2445 c = READCHAR;
2446 if (stringp || c != '-')
2448 UNREAD (c);
2449 return ' ';
2451 c = READCHAR;
2452 if (c == '\\')
2453 c = read_escape (readcharfun, 0);
2454 return c | super_modifier;
2456 case 'C':
2457 c = READCHAR;
2458 if (c != '-')
2459 error ("Invalid escape character syntax");
2460 FALLTHROUGH;
2461 case '^':
2462 c = READCHAR;
2463 if (c == '\\')
2464 c = read_escape (readcharfun, 0);
2465 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2466 return 0177 | (c & CHAR_MODIFIER_MASK);
2467 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2468 return c | ctrl_modifier;
2469 /* ASCII control chars are made from letters (both cases),
2470 as well as the non-letters within 0100...0137. */
2471 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2472 return (c & (037 | ~0177));
2473 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2474 return (c & (037 | ~0177));
2475 else
2476 return c | ctrl_modifier;
2478 case '0':
2479 case '1':
2480 case '2':
2481 case '3':
2482 case '4':
2483 case '5':
2484 case '6':
2485 case '7':
2486 /* An octal escape, as in ANSI C. */
2488 register int i = c - '0';
2489 register int count = 0;
2490 while (++count < 3)
2492 if ((c = READCHAR) >= '0' && c <= '7')
2494 i *= 8;
2495 i += c - '0';
2497 else
2499 UNREAD (c);
2500 break;
2504 if (i >= 0x80 && i < 0x100)
2505 i = BYTE8_TO_CHAR (i);
2506 return i;
2509 case 'x':
2510 /* A hex escape, as in ANSI C. */
2512 unsigned int i = 0;
2513 int count = 0;
2514 while (1)
2516 c = READCHAR;
2517 int digit = char_hexdigit (c);
2518 if (digit < 0)
2520 UNREAD (c);
2521 break;
2523 i = (i << 4) + digit;
2524 /* Allow hex escapes as large as ?\xfffffff, because some
2525 packages use them to denote characters with modifiers. */
2526 if ((CHAR_META | (CHAR_META - 1)) < i)
2527 error ("Hex character out of range: \\x%x...", i);
2528 count += count < 3;
2531 if (count < 3 && i >= 0x80)
2532 return BYTE8_TO_CHAR (i);
2533 return i;
2536 case 'U':
2537 /* Post-Unicode-2.0: Up to eight hex chars. */
2538 unicode_hex_count = 8;
2539 FALLTHROUGH;
2540 case 'u':
2542 /* A Unicode escape. We only permit them in strings and characters,
2543 not arbitrarily in the source code, as in some other languages. */
2545 unsigned int i = 0;
2546 int count = 0;
2548 while (++count <= unicode_hex_count)
2550 c = READCHAR;
2551 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2552 want. */
2553 int digit = char_hexdigit (c);
2554 if (digit < 0)
2555 error ("Non-hex digit used for Unicode escape");
2556 i = (i << 4) + digit;
2558 if (i > 0x10FFFF)
2559 error ("Non-Unicode character: 0x%x", i);
2560 return i;
2563 case 'N':
2564 /* Named character. */
2566 c = READCHAR;
2567 if (c != '{')
2568 invalid_syntax ("Expected opening brace after \\N");
2569 char name[UNICODE_CHARACTER_NAME_LENGTH_BOUND + 1];
2570 bool whitespace = false;
2571 ptrdiff_t length = 0;
2572 while (true)
2574 c = READCHAR;
2575 if (c < 0)
2576 end_of_file_error ();
2577 if (c == '}')
2578 break;
2579 if (! (0 < c && c < 0x80))
2581 AUTO_STRING (format,
2582 "Invalid character U+%04X in character name");
2583 xsignal1 (Qinvalid_read_syntax,
2584 CALLN (Fformat, format, make_natnum (c)));
2586 /* Treat multiple adjacent whitespace characters as a
2587 single space character. This makes it easier to use
2588 character names in e.g. multi-line strings. */
2589 if (c_isspace (c))
2591 if (whitespace)
2592 continue;
2593 c = ' ';
2594 whitespace = true;
2596 else
2597 whitespace = false;
2598 name[length++] = c;
2599 if (length >= sizeof name)
2600 invalid_syntax ("Character name too long");
2602 if (length == 0)
2603 invalid_syntax ("Empty character name");
2604 name[length] = '\0';
2606 /* character_name_to_code can invoke read1, recursively.
2607 This is why read1's buffer is not static. */
2608 return character_name_to_code (name, length);
2611 default:
2612 return c;
2616 /* Return the digit that CHARACTER stands for in the given BASE.
2617 Return -1 if CHARACTER is out of range for BASE,
2618 and -2 if CHARACTER is not valid for any supported BASE. */
2619 static int
2620 digit_to_number (int character, int base)
2622 int digit;
2624 if ('0' <= character && character <= '9')
2625 digit = character - '0';
2626 else if ('a' <= character && character <= 'z')
2627 digit = character - 'a' + 10;
2628 else if ('A' <= character && character <= 'Z')
2629 digit = character - 'A' + 10;
2630 else
2631 return -2;
2633 return digit < base ? digit : -1;
2636 /* Read an integer in radix RADIX using READCHARFUN to read
2637 characters. RADIX must be in the interval [2..36]; if it isn't, a
2638 read error is signaled . Value is the integer read. Signals an
2639 error if encountering invalid read syntax or if RADIX is out of
2640 range. */
2642 static Lisp_Object
2643 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2645 /* Room for sign, leading 0, other digits, trailing null byte.
2646 Also, room for invalid syntax diagnostic. */
2647 char buf[max (1 + 1 + UINTMAX_WIDTH + 1,
2648 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2649 char *p = buf;
2650 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2652 if (radix < 2 || radix > 36)
2653 valid = 0;
2654 else
2656 int c, digit;
2658 c = READCHAR;
2659 if (c == '-' || c == '+')
2661 *p++ = c;
2662 c = READCHAR;
2665 if (c == '0')
2667 *p++ = c;
2668 valid = 1;
2670 /* Ignore redundant leading zeros, so the buffer doesn't
2671 fill up with them. */
2673 c = READCHAR;
2674 while (c == '0');
2677 while ((digit = digit_to_number (c, radix)) >= -1)
2679 if (digit == -1)
2680 valid = 0;
2681 if (valid < 0)
2682 valid = 1;
2683 if (p < buf + sizeof buf)
2684 *p++ = c;
2685 c = READCHAR;
2688 UNREAD (c);
2691 if (valid != 1)
2693 sprintf (buf, "integer, radix %"pI"d", radix);
2694 invalid_syntax (buf);
2697 if (p == buf + sizeof buf)
2699 memset (p - 3, '.', 3);
2700 xsignal1 (Qoverflow_error, make_unibyte_string (buf, sizeof buf));
2703 *p = '\0';
2704 return string_to_number (buf, radix, 0);
2708 /* If the next token is ')' or ']' or '.', we store that character
2709 in *PCH and the return value is not interesting. Else, we store
2710 zero in *PCH and we read and return one lisp object.
2712 FIRST_IN_LIST is true if this is the first element of a list. */
2714 static Lisp_Object
2715 read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
2717 int c;
2718 bool uninterned_symbol = false;
2719 bool multibyte;
2720 char stackbuf[MAX_ALLOCA];
2721 current_thread->stack_top = stackbuf;
2723 *pch = 0;
2725 retry:
2727 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2728 if (c < 0)
2729 end_of_file_error ();
2731 switch (c)
2733 case '(':
2734 return read_list (0, readcharfun);
2736 case '[':
2737 return read_vector (readcharfun, 0);
2739 case ')':
2740 case ']':
2742 *pch = c;
2743 return Qnil;
2746 case '#':
2747 c = READCHAR;
2748 if (c == 's')
2750 c = READCHAR;
2751 if (c == '(')
2753 /* Accept extended format for hash tables (extensible to
2754 other types), e.g.
2755 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2756 Lisp_Object tmp = read_list (0, readcharfun);
2757 Lisp_Object head = CAR_SAFE (tmp);
2758 Lisp_Object data = Qnil;
2759 Lisp_Object val = Qnil;
2760 /* The size is 2 * number of allowed keywords to
2761 make-hash-table. */
2762 Lisp_Object params[12];
2763 Lisp_Object ht;
2764 Lisp_Object key = Qnil;
2765 int param_count = 0;
2767 if (!EQ (head, Qhash_table))
2769 ptrdiff_t size = XINT (Flength (tmp));
2770 Lisp_Object record = Fmake_record (CAR_SAFE (tmp),
2771 make_number (size - 1),
2772 Qnil);
2773 for (int i = 1; i < size; i++)
2775 tmp = Fcdr (tmp);
2776 ASET (record, i, Fcar (tmp));
2778 return record;
2781 tmp = CDR_SAFE (tmp);
2783 /* This is repetitive but fast and simple. */
2784 params[param_count] = QCsize;
2785 params[param_count + 1] = Fplist_get (tmp, Qsize);
2786 if (!NILP (params[param_count + 1]))
2787 param_count += 2;
2789 params[param_count] = QCtest;
2790 params[param_count + 1] = Fplist_get (tmp, Qtest);
2791 if (!NILP (params[param_count + 1]))
2792 param_count += 2;
2794 params[param_count] = QCweakness;
2795 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2796 if (!NILP (params[param_count + 1]))
2797 param_count += 2;
2799 params[param_count] = QCrehash_size;
2800 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2801 if (!NILP (params[param_count + 1]))
2802 param_count += 2;
2804 params[param_count] = QCrehash_threshold;
2805 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2806 if (!NILP (params[param_count + 1]))
2807 param_count += 2;
2809 params[param_count] = QCpurecopy;
2810 params[param_count + 1] = Fplist_get (tmp, Qpurecopy);
2811 if (!NILP (params[param_count + 1]))
2812 param_count += 2;
2814 /* This is the hash table data. */
2815 data = Fplist_get (tmp, Qdata);
2817 /* Now use params to make a new hash table and fill it. */
2818 ht = Fmake_hash_table (param_count, params);
2820 while (CONSP (data))
2822 key = XCAR (data);
2823 data = XCDR (data);
2824 if (!CONSP (data))
2825 error ("Odd number of elements in hash table data");
2826 val = XCAR (data);
2827 data = XCDR (data);
2828 Fputhash (key, val, ht);
2831 return ht;
2833 UNREAD (c);
2834 invalid_syntax ("#");
2836 if (c == '^')
2838 c = READCHAR;
2839 if (c == '[')
2841 Lisp_Object tmp;
2842 tmp = read_vector (readcharfun, 0);
2843 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2844 error ("Invalid size char-table");
2845 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2846 return tmp;
2848 else if (c == '^')
2850 c = READCHAR;
2851 if (c == '[')
2853 /* Sub char-table can't be read as a regular
2854 vector because of a two C integer fields. */
2855 Lisp_Object tbl, tmp = read_list (1, readcharfun);
2856 ptrdiff_t size = XINT (Flength (tmp));
2857 int i, depth, min_char;
2858 struct Lisp_Cons *cell;
2860 if (size == 0)
2861 error ("Zero-sized sub char-table");
2863 if (! RANGED_INTEGERP (1, XCAR (tmp), 3))
2864 error ("Invalid depth in sub char-table");
2865 depth = XINT (XCAR (tmp));
2866 if (chartab_size[depth] != size - 2)
2867 error ("Invalid size in sub char-table");
2868 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2869 free_cons (cell);
2871 if (! RANGED_INTEGERP (0, XCAR (tmp), MAX_CHAR))
2872 error ("Invalid minimum character in sub-char-table");
2873 min_char = XINT (XCAR (tmp));
2874 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2875 free_cons (cell);
2877 tbl = make_uninit_sub_char_table (depth, min_char);
2878 for (i = 0; i < size; i++)
2880 XSUB_CHAR_TABLE (tbl)->contents[i] = XCAR (tmp);
2881 cell = XCONS (tmp), tmp = XCDR (tmp);
2882 free_cons (cell);
2884 return tbl;
2886 invalid_syntax ("#^^");
2888 invalid_syntax ("#^");
2890 if (c == '&')
2892 Lisp_Object length;
2893 length = read1 (readcharfun, pch, first_in_list);
2894 c = READCHAR;
2895 if (c == '"')
2897 Lisp_Object tmp, val;
2898 EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
2899 unsigned char *data;
2901 UNREAD (c);
2902 tmp = read1 (readcharfun, pch, first_in_list);
2903 if (STRING_MULTIBYTE (tmp)
2904 || (size_in_chars != SCHARS (tmp)
2905 /* We used to print 1 char too many
2906 when the number of bits was a multiple of 8.
2907 Accept such input in case it came from an old
2908 version. */
2909 && ! (XFASTINT (length)
2910 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2911 invalid_syntax ("#&...");
2913 val = make_uninit_bool_vector (XFASTINT (length));
2914 data = bool_vector_uchar_data (val);
2915 memcpy (data, SDATA (tmp), size_in_chars);
2916 /* Clear the extraneous bits in the last byte. */
2917 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2918 data[size_in_chars - 1]
2919 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2920 return val;
2922 invalid_syntax ("#&...");
2924 if (c == '[')
2926 /* Accept compiled functions at read-time so that we don't have to
2927 build them using function calls. */
2928 Lisp_Object tmp;
2929 struct Lisp_Vector *vec;
2930 tmp = read_vector (readcharfun, 1);
2931 vec = XVECTOR (tmp);
2932 if (vec->header.size == 0)
2933 invalid_syntax ("Empty byte-code object");
2934 make_byte_code (vec);
2935 return tmp;
2937 if (c == '(')
2939 Lisp_Object tmp;
2940 int ch;
2942 /* Read the string itself. */
2943 tmp = read1 (readcharfun, &ch, 0);
2944 if (ch != 0 || !STRINGP (tmp))
2945 invalid_syntax ("#");
2946 /* Read the intervals and their properties. */
2947 while (1)
2949 Lisp_Object beg, end, plist;
2951 beg = read1 (readcharfun, &ch, 0);
2952 end = plist = Qnil;
2953 if (ch == ')')
2954 break;
2955 if (ch == 0)
2956 end = read1 (readcharfun, &ch, 0);
2957 if (ch == 0)
2958 plist = read1 (readcharfun, &ch, 0);
2959 if (ch)
2960 invalid_syntax ("Invalid string property list");
2961 Fset_text_properties (beg, end, plist, tmp);
2964 return tmp;
2967 /* #@NUMBER is used to skip NUMBER following bytes.
2968 That's used in .elc files to skip over doc strings
2969 and function definitions. */
2970 if (c == '@')
2972 enum { extra = 100 };
2973 ptrdiff_t i, nskip = 0, digits = 0;
2975 /* Read a decimal integer. */
2976 while ((c = READCHAR) >= 0
2977 && c >= '0' && c <= '9')
2979 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2980 string_overflow ();
2981 digits++;
2982 nskip *= 10;
2983 nskip += c - '0';
2984 if (digits == 2 && nskip == 0)
2985 { /* We've just seen #@00, which means "skip to end". */
2986 skip_dyn_eof (readcharfun);
2987 return Qnil;
2990 if (nskip > 0)
2991 /* We can't use UNREAD here, because in the code below we side-step
2992 READCHAR. Instead, assume the first char after #@NNN occupies
2993 a single byte, which is the case normally since it's just
2994 a space. */
2995 nskip--;
2996 else
2997 UNREAD (c);
2999 if (load_force_doc_strings
3000 && (FROM_FILE_P (readcharfun)))
3002 /* If we are supposed to force doc strings into core right now,
3003 record the last string that we skipped,
3004 and record where in the file it comes from. */
3006 /* But first exchange saved_doc_string
3007 with prev_saved_doc_string, so we save two strings. */
3009 char *temp = saved_doc_string;
3010 ptrdiff_t temp_size = saved_doc_string_size;
3011 file_offset temp_pos = saved_doc_string_position;
3012 ptrdiff_t temp_len = saved_doc_string_length;
3014 saved_doc_string = prev_saved_doc_string;
3015 saved_doc_string_size = prev_saved_doc_string_size;
3016 saved_doc_string_position = prev_saved_doc_string_position;
3017 saved_doc_string_length = prev_saved_doc_string_length;
3019 prev_saved_doc_string = temp;
3020 prev_saved_doc_string_size = temp_size;
3021 prev_saved_doc_string_position = temp_pos;
3022 prev_saved_doc_string_length = temp_len;
3025 if (saved_doc_string_size == 0)
3027 saved_doc_string = xmalloc (nskip + extra);
3028 saved_doc_string_size = nskip + extra;
3030 if (nskip > saved_doc_string_size)
3032 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
3033 saved_doc_string_size = nskip + extra;
3036 FILE *instream = infile->stream;
3037 saved_doc_string_position = (file_tell (instream)
3038 - infile->lookahead);
3040 /* Copy that many bytes into saved_doc_string. */
3041 i = 0;
3042 for (int n = min (nskip, infile->lookahead); 0 < n; n--)
3043 saved_doc_string[i++]
3044 = c = infile->buf[--infile->lookahead];
3045 block_input ();
3046 for (; i < nskip && 0 <= c; i++)
3047 saved_doc_string[i] = c = getc_unlocked (instream);
3048 unblock_input ();
3050 saved_doc_string_length = i;
3052 else
3053 /* Skip that many bytes. */
3054 skip_dyn_bytes (readcharfun, nskip);
3056 goto retry;
3058 if (c == '!')
3060 /* #! appears at the beginning of an executable file.
3061 Skip the first line. */
3062 while (c != '\n' && c >= 0)
3063 c = READCHAR;
3064 goto retry;
3066 if (c == '$')
3067 return Vload_file_name;
3068 if (c == '\'')
3069 return list2 (Qfunction, read0 (readcharfun));
3070 /* #:foo is the uninterned symbol named foo. */
3071 if (c == ':')
3073 uninterned_symbol = true;
3074 c = READCHAR;
3075 if (!(c > 040
3076 && c != NO_BREAK_SPACE
3077 && (c >= 0200
3078 || strchr ("\"';()[]#`,", c) == NULL)))
3080 /* No symbol character follows, this is the empty
3081 symbol. */
3082 UNREAD (c);
3083 return Fmake_symbol (empty_unibyte_string);
3085 goto read_symbol;
3087 /* ## is the empty symbol. */
3088 if (c == '#')
3089 return Fintern (empty_unibyte_string, Qnil);
3090 /* Reader forms that can reuse previously read objects. */
3091 if (c >= '0' && c <= '9')
3093 EMACS_INT n = 0;
3094 Lisp_Object tem;
3095 bool overflow = false;
3097 /* Read a non-negative integer. */
3098 while (c >= '0' && c <= '9')
3100 overflow |= INT_MULTIPLY_WRAPV (n, 10, &n);
3101 overflow |= INT_ADD_WRAPV (n, c - '0', &n);
3102 c = READCHAR;
3105 if (!overflow && n <= MOST_POSITIVE_FIXNUM)
3107 if (c == 'r' || c == 'R')
3108 return read_integer (readcharfun, n);
3110 if (! NILP (Vread_circle))
3112 /* #n=object returns object, but associates it with
3113 n for #n#. */
3114 if (c == '=')
3116 /* Make a placeholder for #n# to use temporarily. */
3117 /* Note: We used to use AUTO_CONS to allocate
3118 placeholder, but that is a bad idea, since it
3119 will place a stack-allocated cons cell into
3120 the list in read_objects_map, which is a
3121 staticpro'd global variable, and thus each of
3122 its elements is marked during each GC. A
3123 stack-allocated object will become garbled
3124 when its stack slot goes out of scope, and
3125 some other function reuses it for entirely
3126 different purposes, which will cause crashes
3127 in GC. */
3128 Lisp_Object placeholder = Fcons (Qnil, Qnil);
3129 struct Lisp_Hash_Table *h
3130 = XHASH_TABLE (read_objects_map);
3131 EMACS_UINT hash;
3132 Lisp_Object number = make_number (n);
3134 ptrdiff_t i = hash_lookup (h, number, &hash);
3135 if (i >= 0)
3136 /* Not normal, but input could be malformed. */
3137 set_hash_value_slot (h, i, placeholder);
3138 else
3139 hash_put (h, number, placeholder, hash);
3141 /* Read the object itself. */
3142 tem = read0 (readcharfun);
3144 /* If it can be recursive, remember it for
3145 future substitutions. */
3146 if (! SYMBOLP (tem)
3147 && ! NUMBERP (tem)
3148 && ! (STRINGP (tem) && !string_intervals (tem)))
3150 struct Lisp_Hash_Table *h2
3151 = XHASH_TABLE (read_objects_completed);
3152 i = hash_lookup (h2, tem, &hash);
3153 eassert (i < 0);
3154 hash_put (h2, tem, Qnil, hash);
3157 /* Now put it everywhere the placeholder was... */
3158 if (CONSP (tem))
3160 Fsetcar (placeholder, XCAR (tem));
3161 Fsetcdr (placeholder, XCDR (tem));
3162 return placeholder;
3164 else
3166 Flread__substitute_object_in_subtree
3167 (tem, placeholder, read_objects_completed);
3169 /* ...and #n# will use the real value from now on. */
3170 i = hash_lookup (h, number, &hash);
3171 eassert (i >= 0);
3172 set_hash_value_slot (h, i, tem);
3174 return tem;
3178 /* #n# returns a previously read object. */
3179 if (c == '#')
3181 struct Lisp_Hash_Table *h
3182 = XHASH_TABLE (read_objects_map);
3183 ptrdiff_t i = hash_lookup (h, make_number (n), NULL);
3184 if (i >= 0)
3185 return HASH_VALUE (h, i);
3189 /* Fall through to error message. */
3191 else if (c == 'x' || c == 'X')
3192 return read_integer (readcharfun, 16);
3193 else if (c == 'o' || c == 'O')
3194 return read_integer (readcharfun, 8);
3195 else if (c == 'b' || c == 'B')
3196 return read_integer (readcharfun, 2);
3198 UNREAD (c);
3199 invalid_syntax ("#");
3201 case ';':
3202 while ((c = READCHAR) >= 0 && c != '\n');
3203 goto retry;
3205 case '\'':
3206 return list2 (Qquote, read0 (readcharfun));
3208 case '`':
3210 int next_char = READCHAR;
3211 UNREAD (next_char);
3212 /* Transition from old-style to new-style:
3213 If we see "(`" it used to mean old-style, which usually works
3214 fine because ` should almost never appear in such a position
3215 for new-style. But occasionally we need "(`" to mean new
3216 style, so we try to distinguish the two by the fact that we
3217 can either write "( `foo" or "(` foo", where the first
3218 intends to use new-style whereas the second intends to use
3219 old-style. For Emacs-25, we should completely remove this
3220 first_in_list exception (old-style can still be obtained via
3221 "(\`" anyway). */
3222 if (!new_backquote_flag && first_in_list && next_char == ' ')
3223 load_error_old_style_backquotes ();
3224 else
3226 Lisp_Object value;
3227 bool saved_new_backquote_flag = new_backquote_flag;
3229 new_backquote_flag = 1;
3230 value = read0 (readcharfun);
3231 new_backquote_flag = saved_new_backquote_flag;
3233 return list2 (Qbackquote, value);
3236 case ',':
3238 int next_char = READCHAR;
3239 UNREAD (next_char);
3240 /* Transition from old-style to new-style:
3241 It used to be impossible to have a new-style , other than within
3242 a new-style `. This is sufficient when ` and , are used in the
3243 normal way, but ` and , can also appear in args to macros that
3244 will not interpret them in the usual way, in which case , may be
3245 used without any ` anywhere near.
3246 So we now use the same heuristic as for backquote: old-style
3247 unquotes are only recognized when first on a list, and when
3248 followed by a space.
3249 Because it's more difficult to peek 2 chars ahead, a new-style
3250 ,@ can still not be used outside of a `, unless it's in the middle
3251 of a list. */
3252 if (new_backquote_flag
3253 || !first_in_list
3254 || (next_char != ' ' && next_char != '@'))
3256 Lisp_Object comma_type = Qnil;
3257 Lisp_Object value;
3258 int ch = READCHAR;
3260 if (ch == '@')
3261 comma_type = Qcomma_at;
3262 else if (ch == '.')
3263 comma_type = Qcomma_dot;
3264 else
3266 if (ch >= 0) UNREAD (ch);
3267 comma_type = Qcomma;
3270 value = read0 (readcharfun);
3271 return list2 (comma_type, value);
3273 else
3274 load_error_old_style_backquotes ();
3276 case '?':
3278 int modifiers;
3279 int next_char;
3280 bool ok;
3282 c = READCHAR;
3283 if (c < 0)
3284 end_of_file_error ();
3286 /* Accept `single space' syntax like (list ? x) where the
3287 whitespace character is SPC or TAB.
3288 Other literal whitespace like NL, CR, and FF are not accepted,
3289 as there are well-established escape sequences for these. */
3290 if (c == ' ' || c == '\t')
3291 return make_number (c);
3293 if (c == '(' || c == ')' || c == '[' || c == ']'
3294 || c == '"' || c == ';')
3296 CHECK_LIST (Vlread_unescaped_character_literals);
3297 Lisp_Object char_obj = make_natnum (c);
3298 if (NILP (Fmemq (char_obj, Vlread_unescaped_character_literals)))
3299 Vlread_unescaped_character_literals =
3300 Fcons (char_obj, Vlread_unescaped_character_literals);
3303 if (c == '\\')
3304 c = read_escape (readcharfun, 0);
3305 modifiers = c & CHAR_MODIFIER_MASK;
3306 c &= ~CHAR_MODIFIER_MASK;
3307 if (CHAR_BYTE8_P (c))
3308 c = CHAR_TO_BYTE8 (c);
3309 c |= modifiers;
3311 next_char = READCHAR;
3312 ok = (next_char <= 040
3313 || (next_char < 0200
3314 && strchr ("\"';()[]#?`,.", next_char) != NULL));
3315 UNREAD (next_char);
3316 if (ok)
3317 return make_number (c);
3319 invalid_syntax ("?");
3322 case '"':
3324 ptrdiff_t count = SPECPDL_INDEX ();
3325 char *read_buffer = stackbuf;
3326 ptrdiff_t read_buffer_size = sizeof stackbuf;
3327 char *heapbuf = NULL;
3328 char *p = read_buffer;
3329 char *end = read_buffer + read_buffer_size;
3330 int ch;
3331 /* True if we saw an escape sequence specifying
3332 a multibyte character. */
3333 bool force_multibyte = false;
3334 /* True if we saw an escape sequence specifying
3335 a single-byte character. */
3336 bool force_singlebyte = false;
3337 bool cancel = false;
3338 ptrdiff_t nchars = 0;
3340 while ((ch = READCHAR) >= 0
3341 && ch != '\"')
3343 if (end - p < MAX_MULTIBYTE_LENGTH)
3345 ptrdiff_t offset = p - read_buffer;
3346 read_buffer = grow_read_buffer (read_buffer, offset,
3347 &heapbuf, &read_buffer_size,
3348 count);
3349 p = read_buffer + offset;
3350 end = read_buffer + read_buffer_size;
3353 if (ch == '\\')
3355 int modifiers;
3357 ch = read_escape (readcharfun, 1);
3359 /* CH is -1 if \ newline or \ space has just been seen. */
3360 if (ch == -1)
3362 if (p == read_buffer)
3363 cancel = true;
3364 continue;
3367 modifiers = ch & CHAR_MODIFIER_MASK;
3368 ch = ch & ~CHAR_MODIFIER_MASK;
3370 if (CHAR_BYTE8_P (ch))
3371 force_singlebyte = true;
3372 else if (! ASCII_CHAR_P (ch))
3373 force_multibyte = true;
3374 else /* I.e. ASCII_CHAR_P (ch). */
3376 /* Allow `\C- ' and `\C-?'. */
3377 if (modifiers == CHAR_CTL)
3379 if (ch == ' ')
3380 ch = 0, modifiers = 0;
3381 else if (ch == '?')
3382 ch = 127, modifiers = 0;
3384 if (modifiers & CHAR_SHIFT)
3386 /* Shift modifier is valid only with [A-Za-z]. */
3387 if (ch >= 'A' && ch <= 'Z')
3388 modifiers &= ~CHAR_SHIFT;
3389 else if (ch >= 'a' && ch <= 'z')
3390 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
3393 if (modifiers & CHAR_META)
3395 /* Move the meta bit to the right place for a
3396 string. */
3397 modifiers &= ~CHAR_META;
3398 ch = BYTE8_TO_CHAR (ch | 0x80);
3399 force_singlebyte = true;
3403 /* Any modifiers remaining are invalid. */
3404 if (modifiers)
3405 error ("Invalid modifier in string");
3406 p += CHAR_STRING (ch, (unsigned char *) p);
3408 else
3410 p += CHAR_STRING (ch, (unsigned char *) p);
3411 if (CHAR_BYTE8_P (ch))
3412 force_singlebyte = true;
3413 else if (! ASCII_CHAR_P (ch))
3414 force_multibyte = true;
3416 nchars++;
3419 if (ch < 0)
3420 end_of_file_error ();
3422 /* If purifying, and string starts with \ newline,
3423 return zero instead. This is for doc strings
3424 that we are really going to find in etc/DOC.nn.nn. */
3425 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
3426 return unbind_to (count, make_number (0));
3428 if (! force_multibyte && force_singlebyte)
3430 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3431 forms. Convert it to unibyte. */
3432 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3433 p - read_buffer);
3434 p = read_buffer + nchars;
3437 Lisp_Object result
3438 = make_specified_string (read_buffer, nchars, p - read_buffer,
3439 (force_multibyte
3440 || (p - read_buffer != nchars)));
3441 return unbind_to (count, result);
3444 case '.':
3446 int next_char = READCHAR;
3447 UNREAD (next_char);
3449 if (next_char <= 040
3450 || (next_char < 0200
3451 && strchr ("\"';([#?`,", next_char) != NULL))
3453 *pch = c;
3454 return Qnil;
3457 /* The atom-reading loop below will now loop at least once,
3458 assuring that we will not try to UNREAD two characters in a
3459 row. */
3460 FALLTHROUGH;
3461 default:
3462 if (c <= 040) goto retry;
3463 if (c == NO_BREAK_SPACE)
3464 goto retry;
3466 read_symbol:
3468 ptrdiff_t count = SPECPDL_INDEX ();
3469 char *read_buffer = stackbuf;
3470 ptrdiff_t read_buffer_size = sizeof stackbuf;
3471 char *heapbuf = NULL;
3472 char *p = read_buffer;
3473 char *end = read_buffer + read_buffer_size;
3474 bool quoted = false;
3475 EMACS_INT start_position = readchar_count - 1;
3479 if (end - p < MAX_MULTIBYTE_LENGTH + 1)
3481 ptrdiff_t offset = p - read_buffer;
3482 read_buffer = grow_read_buffer (read_buffer, offset,
3483 &heapbuf, &read_buffer_size,
3484 count);
3485 p = read_buffer + offset;
3486 end = read_buffer + read_buffer_size;
3489 if (c == '\\')
3491 c = READCHAR;
3492 if (c == -1)
3493 end_of_file_error ();
3494 quoted = true;
3497 if (multibyte)
3498 p += CHAR_STRING (c, (unsigned char *) p);
3499 else
3500 *p++ = c;
3501 c = READCHAR;
3503 while (c > 040
3504 && c != NO_BREAK_SPACE
3505 && (c >= 0200
3506 || strchr ("\"';()[]#`,", c) == NULL));
3508 *p = 0;
3509 UNREAD (c);
3511 if (!quoted && !uninterned_symbol)
3513 int flags = (read_integer_overflow_as_float
3514 ? S2N_OVERFLOW_TO_FLOAT : 0);
3515 Lisp_Object result = string_to_number (read_buffer, 10, flags);
3516 if (! NILP (result))
3517 return unbind_to (count, result);
3519 if (!quoted && multibyte)
3521 int ch = STRING_CHAR ((unsigned char *) read_buffer);
3522 if (confusable_symbol_character_p (ch))
3523 xsignal2 (Qinvalid_read_syntax, build_string ("strange quote"),
3524 CALLN (Fstring, make_number (ch)));
3527 Lisp_Object result;
3528 ptrdiff_t nbytes = p - read_buffer;
3529 ptrdiff_t nchars
3530 = (multibyte
3531 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3532 nbytes)
3533 : nbytes);
3535 if (uninterned_symbol)
3537 Lisp_Object name
3538 = ((! NILP (Vpurify_flag)
3539 ? make_pure_string : make_specified_string)
3540 (read_buffer, nchars, nbytes, multibyte));
3541 result = Fmake_symbol (name);
3543 else
3545 /* Don't create the string object for the name unless
3546 we're going to retain it in a new symbol.
3548 Like intern_1 but supports multibyte names. */
3549 Lisp_Object obarray = check_obarray (Vobarray);
3550 Lisp_Object tem = oblookup (obarray, read_buffer,
3551 nchars, nbytes);
3553 if (SYMBOLP (tem))
3554 result = tem;
3555 else
3557 Lisp_Object name
3558 = make_specified_string (read_buffer, nchars, nbytes,
3559 multibyte);
3560 result = intern_driver (name, obarray, tem);
3564 if (EQ (Vread_with_symbol_positions, Qt)
3565 || EQ (Vread_with_symbol_positions, readcharfun))
3566 Vread_symbol_positions_list
3567 = Fcons (Fcons (result, make_number (start_position)),
3568 Vread_symbol_positions_list);
3569 return unbind_to (count, result);
3575 DEFUN ("lread--substitute-object-in-subtree",
3576 Flread__substitute_object_in_subtree,
3577 Slread__substitute_object_in_subtree, 3, 3, 0,
3578 doc: /* In OBJECT, replace every occurrence of PLACEHOLDER with OBJECT.
3579 COMPLETED is a hash table of objects that might be circular, or is t
3580 if any object might be circular. */)
3581 (Lisp_Object object, Lisp_Object placeholder, Lisp_Object completed)
3583 struct subst subst = { object, placeholder, completed, Qnil };
3584 Lisp_Object check_object = substitute_object_recurse (&subst, object);
3586 /* The returned object here is expected to always eq the
3587 original. */
3588 if (!EQ (check_object, object))
3589 error ("Unexpected mutation error in reader");
3590 return Qnil;
3593 static Lisp_Object
3594 substitute_object_recurse (struct subst *subst, Lisp_Object subtree)
3596 /* If we find the placeholder, return the target object. */
3597 if (EQ (subst->placeholder, subtree))
3598 return subst->object;
3600 /* For common object types that can't contain other objects, don't
3601 bother looking them up; we're done. */
3602 if (SYMBOLP (subtree)
3603 || (STRINGP (subtree) && !string_intervals (subtree))
3604 || NUMBERP (subtree))
3605 return subtree;
3607 /* If we've been to this node before, don't explore it again. */
3608 if (!EQ (Qnil, Fmemq (subtree, subst->seen)))
3609 return subtree;
3611 /* If this node can be the entry point to a cycle, remember that
3612 we've seen it. It can only be such an entry point if it was made
3613 by #n=, which means that we can find it as a value in
3614 COMPLETED. */
3615 if (EQ (subst->completed, Qt)
3616 || hash_lookup (XHASH_TABLE (subst->completed), subtree, NULL) >= 0)
3617 subst->seen = Fcons (subtree, subst->seen);
3619 /* Recurse according to subtree's type.
3620 Every branch must return a Lisp_Object. */
3621 switch (XTYPE (subtree))
3623 case Lisp_Vectorlike:
3625 ptrdiff_t i = 0, length = 0;
3626 if (BOOL_VECTOR_P (subtree))
3627 return subtree; /* No sub-objects anyway. */
3628 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3629 || COMPILEDP (subtree) || HASH_TABLE_P (subtree)
3630 || RECORDP (subtree))
3631 length = PVSIZE (subtree);
3632 else if (VECTORP (subtree))
3633 length = ASIZE (subtree);
3634 else
3635 /* An unknown pseudovector may contain non-Lisp fields, so we
3636 can't just blindly traverse all its fields. We used to call
3637 `Flength' which signaled `sequencep', so I just preserved this
3638 behavior. */
3639 wrong_type_argument (Qsequencep, subtree);
3641 if (SUB_CHAR_TABLE_P (subtree))
3642 i = 2;
3643 for ( ; i < length; i++)
3644 ASET (subtree, i,
3645 substitute_object_recurse (subst, AREF (subtree, i)));
3646 return subtree;
3649 case Lisp_Cons:
3650 XSETCAR (subtree, substitute_object_recurse (subst, XCAR (subtree)));
3651 XSETCDR (subtree, substitute_object_recurse (subst, XCDR (subtree)));
3652 return subtree;
3654 case Lisp_String:
3656 /* Check for text properties in each interval.
3657 substitute_in_interval contains part of the logic. */
3659 INTERVAL root_interval = string_intervals (subtree);
3660 traverse_intervals_noorder (root_interval,
3661 substitute_in_interval, subst);
3662 return subtree;
3665 /* Other types don't recurse any further. */
3666 default:
3667 return subtree;
3671 /* Helper function for substitute_object_recurse. */
3672 static void
3673 substitute_in_interval (INTERVAL interval, void *arg)
3675 set_interval_plist (interval,
3676 substitute_object_recurse (arg, interval->plist));
3680 /* Convert STRING to a number, assuming base BASE. When STRING has
3681 floating point syntax and BASE is 10, return a nearest float. When
3682 STRING has integer syntax, return a fixnum if the integer fits, and
3683 signal an overflow otherwise (unless BASE is 10 and STRING ends in
3684 period or FLAGS & S2N_OVERFLOW_TO_FLOAT is nonzero; in this case,
3685 return a nearest float instead). Otherwise, return nil. If FLAGS
3686 & S2N_IGNORE_TRAILING is nonzero, consider just the longest prefix
3687 of STRING that has valid syntax. */
3689 Lisp_Object
3690 string_to_number (char const *string, int base, int flags)
3692 char const *cp = string;
3693 bool float_syntax = 0;
3694 double value = 0;
3696 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3697 IEEE floating point hosts, and works around a formerly-common bug where
3698 atof ("-0.0") drops the sign. */
3699 bool negative = *cp == '-';
3701 bool signedp = negative || *cp == '+';
3702 cp += signedp;
3704 enum { INTOVERFLOW = 1, LEAD_INT = 2, DOT_CHAR = 4, TRAIL_INT = 8,
3705 E_EXP = 16 };
3706 int state = 0;
3707 int leading_digit = digit_to_number (*cp, base);
3708 uintmax_t n = leading_digit;
3709 if (leading_digit >= 0)
3711 state |= LEAD_INT;
3712 for (int digit; 0 <= (digit = digit_to_number (*++cp, base)); )
3714 if (INT_MULTIPLY_OVERFLOW (n, base))
3715 state |= INTOVERFLOW;
3716 n *= base;
3717 if (INT_ADD_OVERFLOW (n, digit))
3718 state |= INTOVERFLOW;
3719 n += digit;
3722 if (*cp == '.')
3724 state |= DOT_CHAR;
3725 cp++;
3728 if (base == 10)
3730 if ('0' <= *cp && *cp <= '9')
3732 state |= TRAIL_INT;
3734 cp++;
3735 while ('0' <= *cp && *cp <= '9');
3737 if (*cp == 'e' || *cp == 'E')
3739 char const *ecp = cp;
3740 cp++;
3741 if (*cp == '+' || *cp == '-')
3742 cp++;
3743 if ('0' <= *cp && *cp <= '9')
3745 state |= E_EXP;
3747 cp++;
3748 while ('0' <= *cp && *cp <= '9');
3750 else if (cp[-1] == '+'
3751 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3753 state |= E_EXP;
3754 cp += 3;
3755 value = INFINITY;
3757 else if (cp[-1] == '+'
3758 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3760 state |= E_EXP;
3761 cp += 3;
3762 /* NAN is a "positive" NaN on all known Emacs hosts. */
3763 value = NAN;
3765 else
3766 cp = ecp;
3769 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3770 || (state & ~INTOVERFLOW) == (LEAD_INT|E_EXP));
3773 /* Return nil if the number uses invalid syntax. If FLAGS &
3774 S2N_IGNORE_TRAILING, accept any prefix that matches. Otherwise,
3775 the entire string must match. */
3776 if (! (flags & S2N_IGNORE_TRAILING
3777 ? ((state & LEAD_INT) != 0 || float_syntax)
3778 : (!*cp && ((state & ~(INTOVERFLOW | DOT_CHAR)) == LEAD_INT
3779 || float_syntax))))
3780 return Qnil;
3782 /* If the number uses integer and not float syntax, and is in C-language
3783 range, use its value, preferably as a fixnum. */
3784 if (leading_digit >= 0 && ! float_syntax)
3786 if (state & INTOVERFLOW)
3788 /* Unfortunately there's no simple and accurate way to convert
3789 non-base-10 numbers that are out of C-language range. */
3790 if (base != 10)
3791 flags = 0;
3793 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3795 EMACS_INT signed_n = n;
3796 return make_number (negative ? -signed_n : signed_n);
3798 else
3799 value = n;
3801 if (! (state & DOT_CHAR) && ! (flags & S2N_OVERFLOW_TO_FLOAT))
3803 AUTO_STRING (fmt, ("%s is out of fixnum range; "
3804 "maybe set `read-integer-overflow-as-float'?"));
3805 AUTO_STRING_WITH_LEN (arg, string, cp - string);
3806 xsignal1 (Qoverflow_error, CALLN (Fformat_message, fmt, arg));
3810 /* Either the number uses float syntax, or it does not fit into a fixnum.
3811 Convert it from string to floating point, unless the value is already
3812 known because it is an infinity, a NAN, or its absolute value fits in
3813 uintmax_t. */
3814 if (! value)
3815 value = atof (string + signedp);
3817 return make_float (negative ? -value : value);
3821 static Lisp_Object
3822 read_vector (Lisp_Object readcharfun, bool bytecodeflag)
3824 ptrdiff_t i, size;
3825 Lisp_Object *ptr;
3826 Lisp_Object tem, item, vector;
3827 struct Lisp_Cons *otem;
3828 Lisp_Object len;
3830 tem = read_list (1, readcharfun);
3831 len = Flength (tem);
3832 if (bytecodeflag && XFASTINT (len) <= COMPILED_STACK_DEPTH)
3833 error ("Invalid byte code");
3834 vector = Fmake_vector (len, Qnil);
3836 size = XFASTINT (len);
3837 ptr = XVECTOR (vector)->contents;
3838 for (i = 0; i < size; i++)
3840 item = Fcar (tem);
3841 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3842 bytecode object, the docstring containing the bytecode and
3843 constants values must be treated as unibyte and passed to
3844 Fread, to get the actual bytecode string and constants vector. */
3845 if (bytecodeflag && load_force_doc_strings)
3847 if (i == COMPILED_BYTECODE)
3849 if (!STRINGP (item))
3850 error ("Invalid byte code");
3852 /* Delay handling the bytecode slot until we know whether
3853 it is lazily-loaded (we can tell by whether the
3854 constants slot is nil). */
3855 ASET (vector, COMPILED_CONSTANTS, item);
3856 item = Qnil;
3858 else if (i == COMPILED_CONSTANTS)
3860 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3862 if (NILP (item))
3864 /* Coerce string to unibyte (like string-as-unibyte,
3865 but without generating extra garbage and
3866 guaranteeing no change in the contents). */
3867 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3868 STRING_SET_UNIBYTE (bytestr);
3870 item = Fread (Fcons (bytestr, readcharfun));
3871 if (!CONSP (item))
3872 error ("Invalid byte code");
3874 otem = XCONS (item);
3875 bytestr = XCAR (item);
3876 item = XCDR (item);
3877 free_cons (otem);
3880 /* Now handle the bytecode slot. */
3881 ASET (vector, COMPILED_BYTECODE, bytestr);
3883 else if (i == COMPILED_DOC_STRING
3884 && STRINGP (item)
3885 && ! STRING_MULTIBYTE (item))
3887 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3888 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3889 else
3890 item = Fstring_as_multibyte (item);
3893 ASET (vector, i, item);
3894 otem = XCONS (tem);
3895 tem = Fcdr (tem);
3896 free_cons (otem);
3898 return vector;
3901 /* FLAG means check for ']' to terminate rather than ')' and '.'. */
3903 static Lisp_Object
3904 read_list (bool flag, Lisp_Object readcharfun)
3906 Lisp_Object val, tail;
3907 Lisp_Object elt, tem;
3908 /* 0 is the normal case.
3909 1 means this list is a doc reference; replace it with the number 0.
3910 2 means this list is a doc reference; replace it with the doc string. */
3911 int doc_reference = 0;
3913 /* Initialize this to 1 if we are reading a list. */
3914 bool first_in_list = flag <= 0;
3916 val = Qnil;
3917 tail = Qnil;
3919 while (1)
3921 int ch;
3922 elt = read1 (readcharfun, &ch, first_in_list);
3924 first_in_list = 0;
3926 /* While building, if the list starts with #$, treat it specially. */
3927 if (EQ (elt, Vload_file_name)
3928 && ! NILP (elt)
3929 && !NILP (Vpurify_flag))
3931 if (NILP (Vdoc_file_name))
3932 /* We have not yet called Snarf-documentation, so assume
3933 this file is described in the DOC file
3934 and Snarf-documentation will fill in the right value later.
3935 For now, replace the whole list with 0. */
3936 doc_reference = 1;
3937 else
3938 /* We have already called Snarf-documentation, so make a relative
3939 file name for this file, so it can be found properly
3940 in the installed Lisp directory.
3941 We don't use Fexpand_file_name because that would make
3942 the directory absolute now. */
3944 AUTO_STRING (dot_dot_lisp, "../lisp/");
3945 elt = concat2 (dot_dot_lisp, Ffile_name_nondirectory (elt));
3948 else if (EQ (elt, Vload_file_name)
3949 && ! NILP (elt)
3950 && load_force_doc_strings)
3951 doc_reference = 2;
3953 if (ch)
3955 if (flag > 0)
3957 if (ch == ']')
3958 return val;
3959 invalid_syntax (") or . in a vector");
3961 if (ch == ')')
3962 return val;
3963 if (ch == '.')
3965 if (!NILP (tail))
3966 XSETCDR (tail, read0 (readcharfun));
3967 else
3968 val = read0 (readcharfun);
3969 read1 (readcharfun, &ch, 0);
3971 if (ch == ')')
3973 if (doc_reference == 1)
3974 return make_number (0);
3975 if (doc_reference == 2 && INTEGERP (XCDR (val)))
3977 char *saved = NULL;
3978 file_offset saved_position;
3979 /* Get a doc string from the file we are loading.
3980 If it's in saved_doc_string, get it from there.
3982 Here, we don't know if the string is a
3983 bytecode string or a doc string. As a
3984 bytecode string must be unibyte, we always
3985 return a unibyte string. If it is actually a
3986 doc string, caller must make it
3987 multibyte. */
3989 /* Position is negative for user variables. */
3990 EMACS_INT pos = eabs (XINT (XCDR (val)));
3991 if (pos >= saved_doc_string_position
3992 && pos < (saved_doc_string_position
3993 + saved_doc_string_length))
3995 saved = saved_doc_string;
3996 saved_position = saved_doc_string_position;
3998 /* Look in prev_saved_doc_string the same way. */
3999 else if (pos >= prev_saved_doc_string_position
4000 && pos < (prev_saved_doc_string_position
4001 + prev_saved_doc_string_length))
4003 saved = prev_saved_doc_string;
4004 saved_position = prev_saved_doc_string_position;
4006 if (saved)
4008 ptrdiff_t start = pos - saved_position;
4009 ptrdiff_t from, to;
4011 /* Process quoting with ^A,
4012 and find the end of the string,
4013 which is marked with ^_ (037). */
4014 for (from = start, to = start;
4015 saved[from] != 037;)
4017 int c = saved[from++];
4018 if (c == 1)
4020 c = saved[from++];
4021 saved[to++] = (c == 1 ? c
4022 : c == '0' ? 0
4023 : c == '_' ? 037
4024 : c);
4026 else
4027 saved[to++] = c;
4030 return make_unibyte_string (saved + start,
4031 to - start);
4033 else
4034 return get_doc_string (val, 1, 0);
4037 return val;
4039 invalid_syntax (". in wrong context");
4041 invalid_syntax ("] in a list");
4043 tem = list1 (elt);
4044 if (!NILP (tail))
4045 XSETCDR (tail, tem);
4046 else
4047 val = tem;
4048 tail = tem;
4052 static Lisp_Object initial_obarray;
4054 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
4056 static size_t oblookup_last_bucket_number;
4058 /* Get an error if OBARRAY is not an obarray.
4059 If it is one, return it. */
4061 Lisp_Object
4062 check_obarray (Lisp_Object obarray)
4064 /* We don't want to signal a wrong-type-argument error when we are
4065 shutting down due to a fatal error, and we don't want to hit
4066 assertions in VECTORP and ASIZE if the fatal error was during GC. */
4067 if (!fatal_error_in_progress
4068 && (!VECTORP (obarray) || ASIZE (obarray) == 0))
4070 /* If Vobarray is now invalid, force it to be valid. */
4071 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
4072 wrong_type_argument (Qvectorp, obarray);
4074 return obarray;
4077 /* Intern symbol SYM in OBARRAY using bucket INDEX. */
4079 static Lisp_Object
4080 intern_sym (Lisp_Object sym, Lisp_Object obarray, Lisp_Object index)
4082 Lisp_Object *ptr;
4084 XSYMBOL (sym)->u.s.interned = (EQ (obarray, initial_obarray)
4085 ? SYMBOL_INTERNED_IN_INITIAL_OBARRAY
4086 : SYMBOL_INTERNED);
4088 if (SREF (SYMBOL_NAME (sym), 0) == ':' && EQ (obarray, initial_obarray))
4090 make_symbol_constant (sym);
4091 XSYMBOL (sym)->u.s.redirect = SYMBOL_PLAINVAL;
4092 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
4095 ptr = aref_addr (obarray, XINT (index));
4096 set_symbol_next (sym, SYMBOLP (*ptr) ? XSYMBOL (*ptr) : NULL);
4097 *ptr = sym;
4098 return sym;
4101 /* Intern a symbol with name STRING in OBARRAY using bucket INDEX. */
4103 Lisp_Object
4104 intern_driver (Lisp_Object string, Lisp_Object obarray, Lisp_Object index)
4106 return intern_sym (Fmake_symbol (string), obarray, index);
4109 /* Intern the C string STR: return a symbol with that name,
4110 interned in the current obarray. */
4112 Lisp_Object
4113 intern_1 (const char *str, ptrdiff_t len)
4115 Lisp_Object obarray = check_obarray (Vobarray);
4116 Lisp_Object tem = oblookup (obarray, str, len, len);
4118 return (SYMBOLP (tem) ? tem
4119 /* The above `oblookup' was done on the basis of nchars==nbytes, so
4120 the string has to be unibyte. */
4121 : intern_driver (make_unibyte_string (str, len),
4122 obarray, tem));
4125 Lisp_Object
4126 intern_c_string_1 (const char *str, ptrdiff_t len)
4128 Lisp_Object obarray = check_obarray (Vobarray);
4129 Lisp_Object tem = oblookup (obarray, str, len, len);
4131 if (!SYMBOLP (tem))
4133 /* Creating a non-pure string from a string literal not implemented yet.
4134 We could just use make_string here and live with the extra copy. */
4135 eassert (!NILP (Vpurify_flag));
4136 tem = intern_driver (make_pure_c_string (str, len), obarray, tem);
4138 return tem;
4141 static void
4142 define_symbol (Lisp_Object sym, char const *str)
4144 ptrdiff_t len = strlen (str);
4145 Lisp_Object string = make_pure_c_string (str, len);
4146 init_symbol (sym, string);
4148 /* Qunbound is uninterned, so that it's not confused with any symbol
4149 'unbound' created by a Lisp program. */
4150 if (! EQ (sym, Qunbound))
4152 Lisp_Object bucket = oblookup (initial_obarray, str, len, len);
4153 eassert (INTEGERP (bucket));
4154 intern_sym (sym, initial_obarray, bucket);
4158 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
4159 doc: /* Return the canonical symbol whose name is STRING.
4160 If there is none, one is created by this function and returned.
4161 A second optional argument specifies the obarray to use;
4162 it defaults to the value of `obarray'. */)
4163 (Lisp_Object string, Lisp_Object obarray)
4165 Lisp_Object tem;
4167 obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
4168 CHECK_STRING (string);
4170 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
4171 if (!SYMBOLP (tem))
4172 tem = intern_driver (NILP (Vpurify_flag) ? string : Fpurecopy (string),
4173 obarray, tem);
4174 return tem;
4177 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
4178 doc: /* Return the canonical symbol named NAME, or nil if none exists.
4179 NAME may be a string or a symbol. If it is a symbol, that exact
4180 symbol is searched for.
4181 A second optional argument specifies the obarray to use;
4182 it defaults to the value of `obarray'. */)
4183 (Lisp_Object name, Lisp_Object obarray)
4185 register Lisp_Object tem, string;
4187 if (NILP (obarray)) obarray = Vobarray;
4188 obarray = check_obarray (obarray);
4190 if (!SYMBOLP (name))
4192 CHECK_STRING (name);
4193 string = name;
4195 else
4196 string = SYMBOL_NAME (name);
4198 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
4199 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
4200 return Qnil;
4201 else
4202 return tem;
4205 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
4206 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
4207 The value is t if a symbol was found and deleted, nil otherwise.
4208 NAME may be a string or a symbol. If it is a symbol, that symbol
4209 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
4210 OBARRAY, if nil, defaults to the value of the variable `obarray'.
4211 usage: (unintern NAME OBARRAY) */)
4212 (Lisp_Object name, Lisp_Object obarray)
4214 register Lisp_Object string, tem;
4215 size_t hash;
4217 if (NILP (obarray)) obarray = Vobarray;
4218 obarray = check_obarray (obarray);
4220 if (SYMBOLP (name))
4221 string = SYMBOL_NAME (name);
4222 else
4224 CHECK_STRING (name);
4225 string = name;
4228 tem = oblookup (obarray, SSDATA (string),
4229 SCHARS (string),
4230 SBYTES (string));
4231 if (INTEGERP (tem))
4232 return Qnil;
4233 /* If arg was a symbol, don't delete anything but that symbol itself. */
4234 if (SYMBOLP (name) && !EQ (name, tem))
4235 return Qnil;
4237 /* There are plenty of other symbols which will screw up the Emacs
4238 session if we unintern them, as well as even more ways to use
4239 `setq' or `fset' or whatnot to make the Emacs session
4240 unusable. Let's not go down this silly road. --Stef */
4241 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
4242 error ("Attempt to unintern t or nil"); */
4244 XSYMBOL (tem)->u.s.interned = SYMBOL_UNINTERNED;
4246 hash = oblookup_last_bucket_number;
4248 if (EQ (AREF (obarray, hash), tem))
4250 if (XSYMBOL (tem)->u.s.next)
4252 Lisp_Object sym;
4253 XSETSYMBOL (sym, XSYMBOL (tem)->u.s.next);
4254 ASET (obarray, hash, sym);
4256 else
4257 ASET (obarray, hash, make_number (0));
4259 else
4261 Lisp_Object tail, following;
4263 for (tail = AREF (obarray, hash);
4264 XSYMBOL (tail)->u.s.next;
4265 tail = following)
4267 XSETSYMBOL (following, XSYMBOL (tail)->u.s.next);
4268 if (EQ (following, tem))
4270 set_symbol_next (tail, XSYMBOL (following)->u.s.next);
4271 break;
4276 return Qt;
4279 /* Return the symbol in OBARRAY whose names matches the string
4280 of SIZE characters (SIZE_BYTE bytes) at PTR.
4281 If there is no such symbol, return the integer bucket number of
4282 where the symbol would be if it were present.
4284 Also store the bucket number in oblookup_last_bucket_number. */
4286 Lisp_Object
4287 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
4289 size_t hash;
4290 size_t obsize;
4291 register Lisp_Object tail;
4292 Lisp_Object bucket, tem;
4294 obarray = check_obarray (obarray);
4295 /* This is sometimes needed in the middle of GC. */
4296 obsize = gc_asize (obarray);
4297 hash = hash_string (ptr, size_byte) % obsize;
4298 bucket = AREF (obarray, hash);
4299 oblookup_last_bucket_number = hash;
4300 if (EQ (bucket, make_number (0)))
4302 else if (!SYMBOLP (bucket))
4303 error ("Bad data in guts of obarray"); /* Like CADR error message. */
4304 else
4305 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->u.s.next))
4307 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
4308 && SCHARS (SYMBOL_NAME (tail)) == size
4309 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
4310 return tail;
4311 else if (XSYMBOL (tail)->u.s.next == 0)
4312 break;
4314 XSETINT (tem, hash);
4315 return tem;
4318 void
4319 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
4321 ptrdiff_t i;
4322 register Lisp_Object tail;
4323 CHECK_VECTOR (obarray);
4324 for (i = ASIZE (obarray) - 1; i >= 0; i--)
4326 tail = AREF (obarray, i);
4327 if (SYMBOLP (tail))
4328 while (1)
4330 (*fn) (tail, arg);
4331 if (XSYMBOL (tail)->u.s.next == 0)
4332 break;
4333 XSETSYMBOL (tail, XSYMBOL (tail)->u.s.next);
4338 static void
4339 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
4341 call1 (function, sym);
4344 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
4345 doc: /* Call FUNCTION on every symbol in OBARRAY.
4346 OBARRAY defaults to the value of `obarray'. */)
4347 (Lisp_Object function, Lisp_Object obarray)
4349 if (NILP (obarray)) obarray = Vobarray;
4350 obarray = check_obarray (obarray);
4352 map_obarray (obarray, mapatoms_1, function);
4353 return Qnil;
4356 #define OBARRAY_SIZE 15121
4358 void
4359 init_obarray (void)
4361 Vobarray = Fmake_vector (make_number (OBARRAY_SIZE), make_number (0));
4362 initial_obarray = Vobarray;
4363 staticpro (&initial_obarray);
4365 for (int i = 0; i < ARRAYELTS (lispsym); i++)
4366 define_symbol (builtin_lisp_symbol (i), defsym_name[i]);
4368 DEFSYM (Qunbound, "unbound");
4370 DEFSYM (Qnil, "nil");
4371 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
4372 make_symbol_constant (Qnil);
4373 XSYMBOL (Qnil)->u.s.declared_special = true;
4375 DEFSYM (Qt, "t");
4376 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
4377 make_symbol_constant (Qt);
4378 XSYMBOL (Qt)->u.s.declared_special = true;
4380 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
4381 Vpurify_flag = Qt;
4383 DEFSYM (Qvariable_documentation, "variable-documentation");
4386 void
4387 defsubr (struct Lisp_Subr *sname)
4389 Lisp_Object sym, tem;
4390 sym = intern_c_string (sname->symbol_name);
4391 XSETPVECTYPE (sname, PVEC_SUBR);
4392 XSETSUBR (tem, sname);
4393 set_symbol_function (sym, tem);
4396 #ifdef NOTDEF /* Use fset in subr.el now! */
4397 void
4398 defalias (struct Lisp_Subr *sname, char *string)
4400 Lisp_Object sym;
4401 sym = intern (string);
4402 XSETSUBR (XSYMBOL (sym)->u.s.function, sname);
4404 #endif /* NOTDEF */
4406 /* Define an "integer variable"; a symbol whose value is forwarded to a
4407 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
4408 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4409 void
4410 defvar_int (struct Lisp_Intfwd *i_fwd,
4411 const char *namestring, EMACS_INT *address)
4413 Lisp_Object sym;
4414 sym = intern_c_string (namestring);
4415 i_fwd->type = Lisp_Fwd_Int;
4416 i_fwd->intvar = address;
4417 XSYMBOL (sym)->u.s.declared_special = true;
4418 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4419 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4422 /* Similar but define a variable whose value is t if address contains 1,
4423 nil if address contains 0. */
4424 void
4425 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4426 const char *namestring, bool *address)
4428 Lisp_Object sym;
4429 sym = intern_c_string (namestring);
4430 b_fwd->type = Lisp_Fwd_Bool;
4431 b_fwd->boolvar = address;
4432 XSYMBOL (sym)->u.s.declared_special = true;
4433 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4434 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4435 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4438 /* Similar but define a variable whose value is the Lisp Object stored
4439 at address. Two versions: with and without gc-marking of the C
4440 variable. The nopro version is used when that variable will be
4441 gc-marked for some other reason, since marking the same slot twice
4442 can cause trouble with strings. */
4443 void
4444 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4445 const char *namestring, Lisp_Object *address)
4447 Lisp_Object sym;
4448 sym = intern_c_string (namestring);
4449 o_fwd->type = Lisp_Fwd_Obj;
4450 o_fwd->objvar = address;
4451 XSYMBOL (sym)->u.s.declared_special = true;
4452 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4453 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4456 void
4457 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4458 const char *namestring, Lisp_Object *address)
4460 defvar_lisp_nopro (o_fwd, namestring, address);
4461 staticpro (address);
4464 /* Similar but define a variable whose value is the Lisp Object stored
4465 at a particular offset in the current kboard object. */
4467 void
4468 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4469 const char *namestring, int offset)
4471 Lisp_Object sym;
4472 sym = intern_c_string (namestring);
4473 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4474 ko_fwd->offset = offset;
4475 XSYMBOL (sym)->u.s.declared_special = true;
4476 XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED;
4477 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4480 /* Check that the elements of lpath exist. */
4482 static void
4483 load_path_check (Lisp_Object lpath)
4485 Lisp_Object path_tail;
4487 /* The only elements that might not exist are those from
4488 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4489 it exists. */
4490 for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
4492 Lisp_Object dirfile;
4493 dirfile = Fcar (path_tail);
4494 if (STRINGP (dirfile))
4496 dirfile = Fdirectory_file_name (dirfile);
4497 if (! file_accessible_directory_p (dirfile))
4498 dir_warning ("Lisp directory", XCAR (path_tail));
4503 /* Return the default load-path, to be used if EMACSLOADPATH is unset.
4504 This does not include the standard site-lisp directories
4505 under the installation prefix (i.e., PATH_SITELOADSEARCH),
4506 but it does (unless no_site_lisp is set) include site-lisp
4507 directories in the source/build directories if those exist and we
4508 are running uninstalled.
4510 Uses the following logic:
4511 If CANNOT_DUMP:
4512 If Vinstallation_directory is not nil (ie, running uninstalled),
4513 use PATH_DUMPLOADSEARCH (ie, build path). Else use PATH_LOADSEARCH.
4514 The remainder is what happens when dumping works:
4515 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4516 Otherwise use PATH_LOADSEARCH.
4518 If !initialized, then just return PATH_DUMPLOADSEARCH.
4519 If initialized:
4520 If Vinstallation_directory is not nil (ie, running uninstalled):
4521 If installation-dir/lisp exists and not already a member,
4522 we must be running uninstalled. Reset the load-path
4523 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4524 refers to the eventual installation directories. Since we
4525 are not yet installed, we should not use them, even if they exist.)
4526 If installation-dir/lisp does not exist, just add
4527 PATH_DUMPLOADSEARCH at the end instead.
4528 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4529 and not already a member) at the front.
4530 If installation-dir != source-dir (ie running an uninstalled,
4531 out-of-tree build) AND install-dir/src/Makefile exists BUT
4532 install-dir/src/Makefile.in does NOT exist (this is a sanity
4533 check), then repeat the above steps for source-dir/lisp, site-lisp. */
4535 static Lisp_Object
4536 load_path_default (void)
4538 Lisp_Object lpath = Qnil;
4539 const char *normal;
4541 #ifdef CANNOT_DUMP
4542 #ifdef HAVE_NS
4543 const char *loadpath = ns_load_path ();
4544 #endif
4546 normal = PATH_LOADSEARCH;
4547 if (!NILP (Vinstallation_directory)) normal = PATH_DUMPLOADSEARCH;
4549 #ifdef HAVE_NS
4550 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4551 #else
4552 lpath = decode_env_path (0, normal, 0);
4553 #endif
4555 #else /* !CANNOT_DUMP */
4557 normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
4559 if (initialized)
4561 #ifdef HAVE_NS
4562 const char *loadpath = ns_load_path ();
4563 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4564 #else
4565 lpath = decode_env_path (0, normal, 0);
4566 #endif
4567 if (!NILP (Vinstallation_directory))
4569 Lisp_Object tem, tem1;
4571 /* Add to the path the lisp subdir of the installation
4572 dir, if it is accessible. Note: in out-of-tree builds,
4573 this directory is empty save for Makefile. */
4574 tem = Fexpand_file_name (build_string ("lisp"),
4575 Vinstallation_directory);
4576 tem1 = Ffile_accessible_directory_p (tem);
4577 if (!NILP (tem1))
4579 if (NILP (Fmember (tem, lpath)))
4581 /* We are running uninstalled. The default load-path
4582 points to the eventual installed lisp directories.
4583 We should not use those now, even if they exist,
4584 so start over from a clean slate. */
4585 lpath = list1 (tem);
4588 else
4589 /* That dir doesn't exist, so add the build-time
4590 Lisp dirs instead. */
4592 Lisp_Object dump_path =
4593 decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
4594 lpath = nconc2 (lpath, dump_path);
4597 /* Add site-lisp under the installation dir, if it exists. */
4598 if (!no_site_lisp)
4600 tem = Fexpand_file_name (build_string ("site-lisp"),
4601 Vinstallation_directory);
4602 tem1 = Ffile_accessible_directory_p (tem);
4603 if (!NILP (tem1))
4605 if (NILP (Fmember (tem, lpath)))
4606 lpath = Fcons (tem, lpath);
4610 /* If Emacs was not built in the source directory,
4611 and it is run from where it was built, add to load-path
4612 the lisp and site-lisp dirs under that directory. */
4614 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4616 Lisp_Object tem2;
4618 tem = Fexpand_file_name (build_string ("src/Makefile"),
4619 Vinstallation_directory);
4620 tem1 = Ffile_exists_p (tem);
4622 /* Don't be fooled if they moved the entire source tree
4623 AFTER dumping Emacs. If the build directory is indeed
4624 different from the source dir, src/Makefile.in and
4625 src/Makefile will not be found together. */
4626 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4627 Vinstallation_directory);
4628 tem2 = Ffile_exists_p (tem);
4629 if (!NILP (tem1) && NILP (tem2))
4631 tem = Fexpand_file_name (build_string ("lisp"),
4632 Vsource_directory);
4634 if (NILP (Fmember (tem, lpath)))
4635 lpath = Fcons (tem, lpath);
4637 if (!no_site_lisp)
4639 tem = Fexpand_file_name (build_string ("site-lisp"),
4640 Vsource_directory);
4641 tem1 = Ffile_accessible_directory_p (tem);
4642 if (!NILP (tem1))
4644 if (NILP (Fmember (tem, lpath)))
4645 lpath = Fcons (tem, lpath);
4649 } /* Vinstallation_directory != Vsource_directory */
4651 } /* if Vinstallation_directory */
4653 else /* !initialized */
4655 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4656 source directory. We used to add ../lisp (ie the lisp dir in
4657 the build directory) at the front here, but that should not
4658 be necessary, since in out of tree builds lisp/ is empty, save
4659 for Makefile. */
4660 lpath = decode_env_path (0, normal, 0);
4662 #endif /* !CANNOT_DUMP */
4664 return lpath;
4667 void
4668 init_lread (void)
4670 if (NILP (Vpurify_flag) && !NILP (Ffboundp (Qfile_truename)))
4671 Vsource_directory = call1 (Qfile_truename, Vsource_directory);
4673 /* First, set Vload_path. */
4675 /* Ignore EMACSLOADPATH when dumping. */
4676 #ifdef CANNOT_DUMP
4677 bool use_loadpath = true;
4678 #else
4679 bool use_loadpath = NILP (Vpurify_flag);
4680 #endif
4682 if (use_loadpath && egetenv ("EMACSLOADPATH"))
4684 Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
4686 /* Check (non-nil) user-supplied elements. */
4687 load_path_check (Vload_path);
4689 /* If no nils in the environment variable, use as-is.
4690 Otherwise, replace any nils with the default. */
4691 if (! NILP (Fmemq (Qnil, Vload_path)))
4693 Lisp_Object elem, elpath = Vload_path;
4694 Lisp_Object default_lpath = load_path_default ();
4696 /* Check defaults, before adding site-lisp. */
4697 load_path_check (default_lpath);
4699 /* Add the site-lisp directories to the front of the default. */
4700 if (!no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4702 Lisp_Object sitelisp;
4703 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4704 if (! NILP (sitelisp))
4705 default_lpath = nconc2 (sitelisp, default_lpath);
4708 Vload_path = Qnil;
4710 /* Replace nils from EMACSLOADPATH by default. */
4711 while (CONSP (elpath))
4713 elem = XCAR (elpath);
4714 elpath = XCDR (elpath);
4715 Vload_path = CALLN (Fappend, Vload_path,
4716 NILP (elem) ? default_lpath : list1 (elem));
4718 } /* Fmemq (Qnil, Vload_path) */
4720 else
4722 Vload_path = load_path_default ();
4724 /* Check before adding site-lisp directories.
4725 The install should have created them, but they are not
4726 required, so no need to warn if they are absent.
4727 Or we might be running before installation. */
4728 load_path_check (Vload_path);
4730 /* Add the site-lisp directories at the front. */
4731 if (initialized && !no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4733 Lisp_Object sitelisp;
4734 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4735 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4739 Vvalues = Qnil;
4741 load_in_progress = 0;
4742 Vload_file_name = Qnil;
4743 Vstandard_input = Qt;
4744 Vloads_in_progress = Qnil;
4747 /* Print a warning that directory intended for use USE and with name
4748 DIRNAME cannot be accessed. On entry, errno should correspond to
4749 the access failure. Print the warning on stderr and put it in
4750 *Messages*. */
4752 void
4753 dir_warning (char const *use, Lisp_Object dirname)
4755 static char const format[] = "Warning: %s '%s': %s\n";
4756 char *diagnostic = emacs_strerror (errno);
4757 fprintf (stderr, format, use, SSDATA (ENCODE_SYSTEM (dirname)), diagnostic);
4759 /* Don't log the warning before we've initialized!! */
4760 if (initialized)
4762 ptrdiff_t diaglen = strlen (diagnostic);
4763 AUTO_STRING_WITH_LEN (diag, diagnostic, diaglen);
4764 if (! NILP (Vlocale_coding_system))
4766 Lisp_Object s
4767 = code_convert_string_norecord (diag, Vlocale_coding_system, false);
4768 diagnostic = SSDATA (s);
4769 diaglen = SBYTES (s);
4771 USE_SAFE_ALLOCA;
4772 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4773 + strlen (use) + SBYTES (dirname) + diaglen);
4774 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4775 diagnostic);
4776 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4777 SAFE_FREE ();
4781 void
4782 syms_of_lread (void)
4784 defsubr (&Sread);
4785 defsubr (&Sread_from_string);
4786 defsubr (&Slread__substitute_object_in_subtree);
4787 defsubr (&Sintern);
4788 defsubr (&Sintern_soft);
4789 defsubr (&Sunintern);
4790 defsubr (&Sget_load_suffixes);
4791 defsubr (&Sload);
4792 defsubr (&Seval_buffer);
4793 defsubr (&Seval_region);
4794 defsubr (&Sread_char);
4795 defsubr (&Sread_char_exclusive);
4796 defsubr (&Sread_event);
4797 defsubr (&Sget_file_char);
4798 defsubr (&Smapatoms);
4799 defsubr (&Slocate_file_internal);
4801 DEFVAR_LISP ("obarray", Vobarray,
4802 doc: /* Symbol table for use by `intern' and `read'.
4803 It is a vector whose length ought to be prime for best results.
4804 The vector's contents don't make sense if examined from Lisp programs;
4805 to find all the symbols in an obarray, use `mapatoms'. */);
4807 DEFVAR_LISP ("values", Vvalues,
4808 doc: /* List of values of all expressions which were read, evaluated and printed.
4809 Order is reverse chronological. */);
4810 XSYMBOL (intern ("values"))->u.s.declared_special = false;
4812 DEFVAR_LISP ("standard-input", Vstandard_input,
4813 doc: /* Stream for read to get input from.
4814 See documentation of `read' for possible values. */);
4815 Vstandard_input = Qt;
4817 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4818 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4820 If this variable is a buffer, then only forms read from that buffer
4821 will be added to `read-symbol-positions-list'.
4822 If this variable is t, then all read forms will be added.
4823 The effect of all other values other than nil are not currently
4824 defined, although they may be in the future.
4826 The positions are relative to the last call to `read' or
4827 `read-from-string'. It is probably a bad idea to set this variable at
4828 the toplevel; bind it instead. */);
4829 Vread_with_symbol_positions = Qnil;
4831 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4832 doc: /* A list mapping read symbols to their positions.
4833 This variable is modified during calls to `read' or
4834 `read-from-string', but only when `read-with-symbol-positions' is
4835 non-nil.
4837 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4838 CHAR-POSITION is an integer giving the offset of that occurrence of the
4839 symbol from the position where `read' or `read-from-string' started.
4841 Note that a symbol will appear multiple times in this list, if it was
4842 read multiple times. The list is in the same order as the symbols
4843 were read in. */);
4844 Vread_symbol_positions_list = Qnil;
4846 DEFVAR_LISP ("read-circle", Vread_circle,
4847 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4848 Vread_circle = Qt;
4850 DEFVAR_BOOL ("read-integer-overflow-as-float",
4851 read_integer_overflow_as_float,
4852 doc: /* Non-nil means `read' quietly treats an out-of-range integer as floating point.
4853 Nil (the default) means signal an overflow unless the integer ends in `.'.
4854 This variable is experimental; email 30408@debbugs.gnu.org if you need it. */);
4855 read_integer_overflow_as_float = false;
4857 DEFVAR_LISP ("load-path", Vload_path,
4858 doc: /* List of directories to search for files to load.
4859 Each element is a string (directory file name) or nil (meaning
4860 `default-directory').
4861 This list is consulted by the `require' function.
4862 Initialized during startup as described in Info node `(elisp)Library Search'.
4863 Use `directory-file-name' when adding items to this path. However, Lisp
4864 programs that process this list should tolerate directories both with
4865 and without trailing slashes. */);
4867 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4868 doc: /* List of suffixes for Emacs Lisp files and dynamic modules.
4869 This list includes suffixes for both compiled and source Emacs Lisp files.
4870 This list should not include the empty string.
4871 `load' and related functions try to append these suffixes, in order,
4872 to the specified file name if a suffix is allowed or required. */);
4873 #ifdef HAVE_MODULES
4874 Vload_suffixes = list3 (build_pure_c_string (".elc"),
4875 build_pure_c_string (".el"),
4876 build_pure_c_string (MODULES_SUFFIX));
4877 #else
4878 Vload_suffixes = list2 (build_pure_c_string (".elc"),
4879 build_pure_c_string (".el"));
4880 #endif
4881 DEFVAR_LISP ("module-file-suffix", Vmodule_file_suffix,
4882 doc: /* Suffix of loadable module file, or nil if modules are not supported. */);
4883 #ifdef HAVE_MODULES
4884 Vmodule_file_suffix = build_pure_c_string (MODULES_SUFFIX);
4885 #else
4886 Vmodule_file_suffix = Qnil;
4887 #endif
4888 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4889 doc: /* List of suffixes that indicate representations of \
4890 the same file.
4891 This list should normally start with the empty string.
4893 Enabling Auto Compression mode appends the suffixes in
4894 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4895 mode removes them again. `load' and related functions use this list to
4896 determine whether they should look for compressed versions of a file
4897 and, if so, which suffixes they should try to append to the file name
4898 in order to do so. However, if you want to customize which suffixes
4899 the loading functions recognize as compression suffixes, you should
4900 customize `jka-compr-load-suffixes' rather than the present variable. */);
4901 Vload_file_rep_suffixes = list1 (empty_unibyte_string);
4903 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4904 doc: /* Non-nil if inside of `load'. */);
4905 DEFSYM (Qload_in_progress, "load-in-progress");
4907 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4908 doc: /* An alist of functions to be evalled when particular files are loaded.
4909 Each element looks like (REGEXP-OR-FEATURE FUNCS...).
4911 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4912 a symbol (a feature name).
4914 When `load' is run and the file-name argument matches an element's
4915 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4916 REGEXP-OR-FEATURE, the FUNCS in the element are called.
4918 An error in FORMS does not undo the load, but does prevent execution of
4919 the rest of the FORMS. */);
4920 Vafter_load_alist = Qnil;
4922 DEFVAR_LISP ("load-history", Vload_history,
4923 doc: /* Alist mapping loaded file names to symbols and features.
4924 Each alist element should be a list (FILE-NAME ENTRIES...), where
4925 FILE-NAME is the name of a file that has been loaded into Emacs.
4926 The file name is absolute and true (i.e. it doesn't contain symlinks).
4927 As an exception, one of the alist elements may have FILE-NAME nil,
4928 for symbols and features not associated with any file.
4930 The remaining ENTRIES in the alist element describe the functions and
4931 variables defined in that file, the features provided, and the
4932 features required. Each entry has the form `(provide . FEATURE)',
4933 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4934 `(defface . SYMBOL)', `(define-type . SYMBOL)',
4935 `(cl-defmethod METHOD SPECIALIZERS)', or `(t . SYMBOL)'.
4936 Entries like `(t . SYMBOL)' may precede a `(defun . FUNCTION)' entry,
4937 and means that SYMBOL was an autoload before this file redefined it
4938 as a function. In addition, entries may also be single symbols,
4939 which means that symbol was defined by `defvar' or `defconst'.
4941 During preloading, the file name recorded is relative to the main Lisp
4942 directory. These file names are converted to absolute at startup. */);
4943 Vload_history = Qnil;
4945 DEFVAR_LISP ("load-file-name", Vload_file_name,
4946 doc: /* Full name of file being loaded by `load'. */);
4947 Vload_file_name = Qnil;
4949 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4950 doc: /* File name, including directory, of user's initialization file.
4951 If the file loaded had extension `.elc', and the corresponding source file
4952 exists, this variable contains the name of source file, suitable for use
4953 by functions like `custom-save-all' which edit the init file.
4954 While Emacs loads and evaluates any init file, value is the real name
4955 of the file, regardless of whether or not it has the `.elc' extension. */);
4956 Vuser_init_file = Qnil;
4958 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4959 doc: /* Used for internal purposes by `load'. */);
4960 Vcurrent_load_list = Qnil;
4962 DEFVAR_LISP ("load-read-function", Vload_read_function,
4963 doc: /* Function used by `load' and `eval-region' for reading expressions.
4964 Called with a single argument (the stream from which to read).
4965 The default is to use the function `read'. */);
4966 DEFSYM (Qread, "read");
4967 Vload_read_function = Qread;
4969 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4970 doc: /* Function called in `load' to load an Emacs Lisp source file.
4971 The value should be a function for doing code conversion before
4972 reading a source file. It can also be nil, in which case loading is
4973 done without any code conversion.
4975 If the value is a function, it is called with four arguments,
4976 FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4977 the file to load, FILE is the non-absolute name (for messages etc.),
4978 and NOERROR and NOMESSAGE are the corresponding arguments passed to
4979 `load'. The function should return t if the file was loaded. */);
4980 Vload_source_file_function = Qnil;
4982 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4983 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4984 This is useful when the file being loaded is a temporary copy. */);
4985 load_force_doc_strings = 0;
4987 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4988 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4989 This is normally bound by `load' and `eval-buffer' to control `read',
4990 and is not meant for users to change. */);
4991 load_convert_to_unibyte = 0;
4993 DEFVAR_LISP ("source-directory", Vsource_directory,
4994 doc: /* Directory in which Emacs sources were found when Emacs was built.
4995 You cannot count on them to still be there! */);
4996 Vsource_directory
4997 = Fexpand_file_name (build_string ("../"),
4998 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
5000 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
5001 doc: /* List of files that were preloaded (when dumping Emacs). */);
5002 Vpreloaded_file_list = Qnil;
5004 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
5005 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
5006 Vbyte_boolean_vars = Qnil;
5008 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
5009 doc: /* Non-nil means load dangerous compiled Lisp files.
5010 Some versions of XEmacs use different byte codes than Emacs. These
5011 incompatible byte codes can make Emacs crash when it tries to execute
5012 them. */);
5013 load_dangerous_libraries = 0;
5015 DEFVAR_BOOL ("force-load-messages", force_load_messages,
5016 doc: /* Non-nil means force printing messages when loading Lisp files.
5017 This overrides the value of the NOMESSAGE argument to `load'. */);
5018 force_load_messages = 0;
5020 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
5021 doc: /* Regular expression matching safe to load compiled Lisp files.
5022 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
5023 from the file, and matches them against this regular expression.
5024 When the regular expression matches, the file is considered to be safe
5025 to load. See also `load-dangerous-libraries'. */);
5026 Vbytecomp_version_regexp
5027 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
5029 DEFSYM (Qlexical_binding, "lexical-binding");
5030 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
5031 doc: /* Whether to use lexical binding when evaluating code.
5032 Non-nil means that the code in the current buffer should be evaluated
5033 with lexical binding.
5034 This variable is automatically set from the file variables of an
5035 interpreted Lisp file read using `load'. Unlike other file local
5036 variables, this must be set in the first line of a file. */);
5037 Vlexical_binding = Qnil;
5038 Fmake_variable_buffer_local (Qlexical_binding);
5040 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
5041 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
5042 Veval_buffer_list = Qnil;
5044 DEFVAR_LISP ("lread--unescaped-character-literals",
5045 Vlread_unescaped_character_literals,
5046 doc: /* List of deprecated unescaped character literals encountered by `read'.
5047 For internal use only. */);
5048 Vlread_unescaped_character_literals = Qnil;
5049 DEFSYM (Qlread_unescaped_character_literals,
5050 "lread--unescaped-character-literals");
5052 DEFSYM (Qlss, "<");
5053 DEFSYM (Qchar, "char");
5054 DEFSYM (Qformat, "format");
5056 DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
5057 doc: /* Non-nil means `load' prefers the newest version of a file.
5058 This applies when a filename suffix is not explicitly specified and
5059 `load' is trying various possible suffixes (see `load-suffixes' and
5060 `load-file-rep-suffixes'). Normally, it stops at the first file
5061 that exists unless you explicitly specify one or the other. If this
5062 option is non-nil, it checks all suffixes and uses whichever file is
5063 newest.
5064 Note that if you customize this, obviously it will not affect files
5065 that are loaded before your customizations are read! */);
5066 load_prefer_newer = 0;
5068 DEFVAR_BOOL ("force-new-style-backquotes", force_new_style_backquotes,
5069 doc: /* Non-nil means to always use the current syntax for backquotes.
5070 If nil, `load' and `read' raise errors when encountering some
5071 old-style variants of backquote and comma. If non-nil, these
5072 constructs are always interpreted as described in the Info node
5073 `(elisp)Backquotes', even if that interpretation is incompatible with
5074 previous versions of Emacs. Setting this variable to non-nil makes
5075 Emacs compatible with the behavior planned for Emacs 28. In Emacs 28,
5076 this variable will become obsolete. */);
5077 force_new_style_backquotes = false;
5079 /* Vsource_directory was initialized in init_lread. */
5081 DEFSYM (Qcurrent_load_list, "current-load-list");
5082 DEFSYM (Qstandard_input, "standard-input");
5083 DEFSYM (Qread_char, "read-char");
5084 DEFSYM (Qget_file_char, "get-file-char");
5086 /* Used instead of Qget_file_char while loading *.elc files compiled
5087 by Emacs 21 or older. */
5088 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
5090 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
5092 DEFSYM (Qbackquote, "`");
5093 DEFSYM (Qcomma, ",");
5094 DEFSYM (Qcomma_at, ",@");
5095 DEFSYM (Qcomma_dot, ",.");
5097 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
5098 DEFSYM (Qascii_character, "ascii-character");
5099 DEFSYM (Qfunction, "function");
5100 DEFSYM (Qload, "load");
5101 DEFSYM (Qload_file_name, "load-file-name");
5102 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
5103 DEFSYM (Qfile_truename, "file-truename");
5104 DEFSYM (Qdir_ok, "dir-ok");
5105 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
5107 staticpro (&read_objects_map);
5108 read_objects_map = Qnil;
5109 staticpro (&read_objects_completed);
5110 read_objects_completed = Qnil;
5112 Vloads_in_progress = Qnil;
5113 staticpro (&Vloads_in_progress);
5115 DEFSYM (Qhash_table, "hash-table");
5116 DEFSYM (Qdata, "data");
5117 DEFSYM (Qtest, "test");
5118 DEFSYM (Qsize, "size");
5119 DEFSYM (Qpurecopy, "purecopy");
5120 DEFSYM (Qweakness, "weakness");
5121 DEFSYM (Qrehash_size, "rehash-size");
5122 DEFSYM (Qrehash_threshold, "rehash-threshold");
5124 DEFSYM (Qchar_from_name, "char-from-name");