* lisp/nxml/nxml-mode.el: Treat unclosed <[[, <?, comment, and other
[emacs.git] / src / lread.c
blob15821662fc8fc47d1d49489e222ca72d805dfb67
1 /* Lisp parsing and input streams.
3 Copyright (C) 1985-1989, 1993-1995, 1997-2013 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
11 (at 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 <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/file.h>
27 #include <errno.h>
28 #include <limits.h> /* For CHAR_BIT. */
29 #include <stat-time.h>
30 #include "lisp.h"
31 #include "intervals.h"
32 #include "character.h"
33 #include "buffer.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include <epaths.h>
37 #include "commands.h"
38 #include "keyboard.h"
39 #include "frame.h"
40 #include "termhooks.h"
41 #include "coding.h"
42 #include "blockinput.h"
44 #ifdef MSDOS
45 #include "msdos.h"
46 #endif
48 #ifdef HAVE_NS
49 #include "nsterm.h"
50 #endif
52 #include <unistd.h>
54 #ifdef HAVE_SETLOCALE
55 #include <locale.h>
56 #endif /* HAVE_SETLOCALE */
58 #include <fcntl.h>
60 #ifdef HAVE_FSEEKO
61 #define file_offset off_t
62 #define file_tell ftello
63 #else
64 #define file_offset long
65 #define file_tell ftell
66 #endif
68 /* Hash table read constants. */
69 static Lisp_Object Qhash_table, Qdata;
70 static Lisp_Object Qtest, Qsize;
71 static Lisp_Object Qweakness;
72 static Lisp_Object Qrehash_size;
73 static Lisp_Object Qrehash_threshold;
75 static Lisp_Object Qread_char, Qget_file_char, Qcurrent_load_list;
76 Lisp_Object Qstandard_input;
77 Lisp_Object Qvariable_documentation;
78 static Lisp_Object Qascii_character, Qload, Qload_file_name;
79 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
80 static Lisp_Object Qinhibit_file_name_operation;
81 static Lisp_Object Qeval_buffer_list;
82 Lisp_Object Qlexical_binding;
83 static Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
85 /* Used instead of Qget_file_char while loading *.elc files compiled
86 by Emacs 21 or older. */
87 static Lisp_Object Qget_emacs_mule_file_char;
89 static Lisp_Object Qload_force_doc_strings;
91 static Lisp_Object Qload_in_progress;
93 /* The association list of objects read with the #n=object form.
94 Each member of the list has the form (n . object), and is used to
95 look up the object for the corresponding #n# construct.
96 It must be set to nil before all top-level calls to read0. */
97 static Lisp_Object read_objects;
99 /* List of descriptors now open for Fload. */
100 static Lisp_Object load_descriptor_list;
102 /* File for get_file_char to read from. Use by load. */
103 static FILE *instream;
105 /* For use within read-from-string (this reader is non-reentrant!!) */
106 static ptrdiff_t read_from_string_index;
107 static ptrdiff_t read_from_string_index_byte;
108 static ptrdiff_t read_from_string_limit;
110 /* Number of characters read in the current call to Fread or
111 Fread_from_string. */
112 static EMACS_INT readchar_count;
114 /* This contains the last string skipped with #@. */
115 static char *saved_doc_string;
116 /* Length of buffer allocated in saved_doc_string. */
117 static ptrdiff_t saved_doc_string_size;
118 /* Length of actual data in saved_doc_string. */
119 static ptrdiff_t saved_doc_string_length;
120 /* This is the file position that string came from. */
121 static file_offset saved_doc_string_position;
123 /* This contains the previous string skipped with #@.
124 We copy it from saved_doc_string when a new string
125 is put in saved_doc_string. */
126 static char *prev_saved_doc_string;
127 /* Length of buffer allocated in prev_saved_doc_string. */
128 static ptrdiff_t prev_saved_doc_string_size;
129 /* Length of actual data in prev_saved_doc_string. */
130 static ptrdiff_t prev_saved_doc_string_length;
131 /* This is the file position that string came from. */
132 static file_offset prev_saved_doc_string_position;
134 /* True means inside a new-style backquote
135 with no surrounding parentheses.
136 Fread initializes this to false, so we need not specbind it
137 or worry about what happens to it when there is an error. */
138 static bool new_backquote_flag;
139 static Lisp_Object Qold_style_backquotes;
141 /* A list of file names for files being loaded in Fload. Used to
142 check for recursive loads. */
144 static Lisp_Object Vloads_in_progress;
146 static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
147 Lisp_Object);
149 static void readevalloop (Lisp_Object, FILE *, Lisp_Object, bool,
150 Lisp_Object, Lisp_Object,
151 Lisp_Object, Lisp_Object);
152 static Lisp_Object load_unwind (Lisp_Object);
153 static Lisp_Object load_descriptor_unwind (Lisp_Object);
155 /* Functions that read one byte from the current source READCHARFUN
156 or unreads one byte. If the integer argument C is -1, it returns
157 one read byte, or -1 when there's no more byte in the source. If C
158 is 0 or positive, it unreads C, and the return value is not
159 interesting. */
161 static int readbyte_for_lambda (int, Lisp_Object);
162 static int readbyte_from_file (int, Lisp_Object);
163 static int readbyte_from_string (int, Lisp_Object);
165 /* Handle unreading and rereading of characters.
166 Write READCHAR to read a character,
167 UNREAD(c) to unread c to be read again.
169 These macros correctly read/unread multibyte characters. */
171 #define READCHAR readchar (readcharfun, NULL)
172 #define UNREAD(c) unreadchar (readcharfun, c)
174 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
175 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
177 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
178 Qlambda, or a cons, we use this to keep an unread character because
179 a file stream can't handle multibyte-char unreading. The value -1
180 means that there's no unread character. */
181 static int unread_char;
183 static int
184 readchar (Lisp_Object readcharfun, bool *multibyte)
186 Lisp_Object tem;
187 register int c;
188 int (*readbyte) (int, Lisp_Object);
189 unsigned char buf[MAX_MULTIBYTE_LENGTH];
190 int i, len;
191 bool emacs_mule_encoding = 0;
193 if (multibyte)
194 *multibyte = 0;
196 readchar_count++;
198 if (BUFFERP (readcharfun))
200 register struct buffer *inbuffer = XBUFFER (readcharfun);
202 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
204 if (! BUFFER_LIVE_P (inbuffer))
205 return -1;
207 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
208 return -1;
210 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
212 /* Fetch the character code from the buffer. */
213 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
214 BUF_INC_POS (inbuffer, pt_byte);
215 c = STRING_CHAR (p);
216 if (multibyte)
217 *multibyte = 1;
219 else
221 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
222 if (! ASCII_BYTE_P (c))
223 c = BYTE8_TO_CHAR (c);
224 pt_byte++;
226 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
228 return c;
230 if (MARKERP (readcharfun))
232 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
234 ptrdiff_t bytepos = marker_byte_position (readcharfun);
236 if (bytepos >= BUF_ZV_BYTE (inbuffer))
237 return -1;
239 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
241 /* Fetch the character code from the buffer. */
242 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
243 BUF_INC_POS (inbuffer, bytepos);
244 c = STRING_CHAR (p);
245 if (multibyte)
246 *multibyte = 1;
248 else
250 c = BUF_FETCH_BYTE (inbuffer, bytepos);
251 if (! ASCII_BYTE_P (c))
252 c = BYTE8_TO_CHAR (c);
253 bytepos++;
256 XMARKER (readcharfun)->bytepos = bytepos;
257 XMARKER (readcharfun)->charpos++;
259 return c;
262 if (EQ (readcharfun, Qlambda))
264 readbyte = readbyte_for_lambda;
265 goto read_multibyte;
268 if (EQ (readcharfun, Qget_file_char))
270 readbyte = readbyte_from_file;
271 goto read_multibyte;
274 if (STRINGP (readcharfun))
276 if (read_from_string_index >= read_from_string_limit)
277 c = -1;
278 else if (STRING_MULTIBYTE (readcharfun))
280 if (multibyte)
281 *multibyte = 1;
282 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
283 read_from_string_index,
284 read_from_string_index_byte);
286 else
288 c = SREF (readcharfun, read_from_string_index_byte);
289 read_from_string_index++;
290 read_from_string_index_byte++;
292 return c;
295 if (CONSP (readcharfun))
297 /* This is the case that read_vector is reading from a unibyte
298 string that contains a byte sequence previously skipped
299 because of #@NUMBER. The car part of readcharfun is that
300 string, and the cdr part is a value of readcharfun given to
301 read_vector. */
302 readbyte = readbyte_from_string;
303 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
304 emacs_mule_encoding = 1;
305 goto read_multibyte;
308 if (EQ (readcharfun, Qget_emacs_mule_file_char))
310 readbyte = readbyte_from_file;
311 emacs_mule_encoding = 1;
312 goto read_multibyte;
315 tem = call0 (readcharfun);
317 if (NILP (tem))
318 return -1;
319 return XINT (tem);
321 read_multibyte:
322 if (unread_char >= 0)
324 c = unread_char;
325 unread_char = -1;
326 return c;
328 c = (*readbyte) (-1, readcharfun);
329 if (c < 0)
330 return c;
331 if (multibyte)
332 *multibyte = 1;
333 if (ASCII_BYTE_P (c))
334 return c;
335 if (emacs_mule_encoding)
336 return read_emacs_mule_char (c, readbyte, readcharfun);
337 i = 0;
338 buf[i++] = c;
339 len = BYTES_BY_CHAR_HEAD (c);
340 while (i < len)
342 c = (*readbyte) (-1, readcharfun);
343 if (c < 0 || ! TRAILING_CODE_P (c))
345 while (--i > 1)
346 (*readbyte) (buf[i], readcharfun);
347 return BYTE8_TO_CHAR (buf[0]);
349 buf[i++] = c;
351 return STRING_CHAR (buf);
354 #define FROM_FILE_P(readcharfun) \
355 (EQ (readcharfun, Qget_file_char) \
356 || EQ (readcharfun, Qget_emacs_mule_file_char))
358 static void
359 skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
361 if (FROM_FILE_P (readcharfun))
363 block_input (); /* FIXME: Not sure if it's needed. */
364 fseek (instream, n, SEEK_CUR);
365 unblock_input ();
367 else
368 { /* We're not reading directly from a file. In that case, it's difficult
369 to reliably count bytes, since these are usually meant for the file's
370 encoding, whereas we're now typically in the internal encoding.
371 But luckily, skip_dyn_bytes is used to skip over a single
372 dynamic-docstring (or dynamic byte-code) which is always quoted such
373 that \037 is the final char. */
374 int c;
375 do {
376 c = READCHAR;
377 } while (c >= 0 && c != '\037');
381 static void
382 skip_dyn_eof (Lisp_Object readcharfun)
384 if (FROM_FILE_P (readcharfun))
386 block_input (); /* FIXME: Not sure if it's needed. */
387 fseek (instream, 0, SEEK_END);
388 unblock_input ();
390 else
391 while (READCHAR >= 0);
394 /* Unread the character C in the way appropriate for the stream READCHARFUN.
395 If the stream is a user function, call it with the char as argument. */
397 static void
398 unreadchar (Lisp_Object readcharfun, int c)
400 readchar_count--;
401 if (c == -1)
402 /* Don't back up the pointer if we're unreading the end-of-input mark,
403 since readchar didn't advance it when we read it. */
405 else if (BUFFERP (readcharfun))
407 struct buffer *b = XBUFFER (readcharfun);
408 ptrdiff_t charpos = BUF_PT (b);
409 ptrdiff_t bytepos = BUF_PT_BYTE (b);
411 if (! NILP (BVAR (b, enable_multibyte_characters)))
412 BUF_DEC_POS (b, bytepos);
413 else
414 bytepos--;
416 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
418 else if (MARKERP (readcharfun))
420 struct buffer *b = XMARKER (readcharfun)->buffer;
421 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
423 XMARKER (readcharfun)->charpos--;
424 if (! NILP (BVAR (b, enable_multibyte_characters)))
425 BUF_DEC_POS (b, bytepos);
426 else
427 bytepos--;
429 XMARKER (readcharfun)->bytepos = bytepos;
431 else if (STRINGP (readcharfun))
433 read_from_string_index--;
434 read_from_string_index_byte
435 = string_char_to_byte (readcharfun, read_from_string_index);
437 else if (CONSP (readcharfun))
439 unread_char = c;
441 else if (EQ (readcharfun, Qlambda))
443 unread_char = c;
445 else if (FROM_FILE_P (readcharfun))
447 unread_char = c;
449 else
450 call1 (readcharfun, make_number (c));
453 static int
454 readbyte_for_lambda (int c, Lisp_Object readcharfun)
456 return read_bytecode_char (c >= 0);
460 static int
461 readbyte_from_file (int c, Lisp_Object readcharfun)
463 if (c >= 0)
465 block_input ();
466 ungetc (c, instream);
467 unblock_input ();
468 return 0;
471 block_input ();
472 c = getc (instream);
474 /* Interrupted reads have been observed while reading over the network. */
475 while (c == EOF && ferror (instream) && errno == EINTR)
477 unblock_input ();
478 QUIT;
479 block_input ();
480 clearerr (instream);
481 c = getc (instream);
484 unblock_input ();
486 return (c == EOF ? -1 : c);
489 static int
490 readbyte_from_string (int c, Lisp_Object readcharfun)
492 Lisp_Object string = XCAR (readcharfun);
494 if (c >= 0)
496 read_from_string_index--;
497 read_from_string_index_byte
498 = string_char_to_byte (string, read_from_string_index);
501 if (read_from_string_index >= read_from_string_limit)
502 c = -1;
503 else
504 FETCH_STRING_CHAR_ADVANCE (c, string,
505 read_from_string_index,
506 read_from_string_index_byte);
507 return c;
511 /* Read one non-ASCII character from INSTREAM. The character is
512 encoded in `emacs-mule' and the first byte is already read in
513 C. */
515 static int
516 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
518 /* Emacs-mule coding uses at most 4-byte for one character. */
519 unsigned char buf[4];
520 int len = emacs_mule_bytes[c];
521 struct charset *charset;
522 int i;
523 unsigned code;
525 if (len == 1)
526 /* C is not a valid leading-code of `emacs-mule'. */
527 return BYTE8_TO_CHAR (c);
529 i = 0;
530 buf[i++] = c;
531 while (i < len)
533 c = (*readbyte) (-1, readcharfun);
534 if (c < 0xA0)
536 while (--i > 1)
537 (*readbyte) (buf[i], readcharfun);
538 return BYTE8_TO_CHAR (buf[0]);
540 buf[i++] = c;
543 if (len == 2)
545 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
546 code = buf[1] & 0x7F;
548 else if (len == 3)
550 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
551 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
553 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
554 code = buf[2] & 0x7F;
556 else
558 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
559 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
562 else
564 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
565 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
567 c = DECODE_CHAR (charset, code);
568 if (c < 0)
569 Fsignal (Qinvalid_read_syntax,
570 Fcons (build_string ("invalid multibyte form"), Qnil));
571 return c;
575 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
576 Lisp_Object);
577 static Lisp_Object read0 (Lisp_Object);
578 static Lisp_Object read1 (Lisp_Object, int *, bool);
580 static Lisp_Object read_list (bool, Lisp_Object);
581 static Lisp_Object read_vector (Lisp_Object, bool);
583 static Lisp_Object substitute_object_recurse (Lisp_Object, Lisp_Object,
584 Lisp_Object);
585 static void substitute_object_in_subtree (Lisp_Object,
586 Lisp_Object);
587 static void substitute_in_interval (INTERVAL, Lisp_Object);
590 /* Get a character from the tty. */
592 /* Read input events until we get one that's acceptable for our purposes.
594 If NO_SWITCH_FRAME, switch-frame events are stashed
595 until we get a character we like, and then stuffed into
596 unread_switch_frame.
598 If ASCII_REQUIRED, check function key events to see
599 if the unmodified version of the symbol has a Qascii_character
600 property, and use that character, if present.
602 If ERROR_NONASCII, signal an error if the input we
603 get isn't an ASCII character with modifiers. If it's false but
604 ASCII_REQUIRED is true, just re-read until we get an ASCII
605 character.
607 If INPUT_METHOD, invoke the current input method
608 if the character warrants that.
610 If SECONDS is a number, wait that many seconds for input, and
611 return Qnil if no input arrives within that time. */
613 static Lisp_Object
614 read_filtered_event (bool no_switch_frame, bool ascii_required,
615 bool error_nonascii, bool input_method, Lisp_Object seconds)
617 Lisp_Object val, delayed_switch_frame;
618 EMACS_TIME end_time;
620 #ifdef HAVE_WINDOW_SYSTEM
621 if (display_hourglass_p)
622 cancel_hourglass ();
623 #endif
625 delayed_switch_frame = Qnil;
627 /* Compute timeout. */
628 if (NUMBERP (seconds))
630 double duration = extract_float (seconds);
631 EMACS_TIME wait_time = EMACS_TIME_FROM_DOUBLE (duration);
632 end_time = add_emacs_time (current_emacs_time (), wait_time);
635 /* Read until we get an acceptable event. */
636 retry:
638 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
639 NUMBERP (seconds) ? &end_time : NULL);
640 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
642 if (BUFFERP (val))
643 goto retry;
645 /* `switch-frame' events are put off until after the next ASCII
646 character. This is better than signaling an error just because
647 the last characters were typed to a separate minibuffer frame,
648 for example. Eventually, some code which can deal with
649 switch-frame events will read it and process it. */
650 if (no_switch_frame
651 && EVENT_HAS_PARAMETERS (val)
652 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
654 delayed_switch_frame = val;
655 goto retry;
658 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
660 /* Convert certain symbols to their ASCII equivalents. */
661 if (SYMBOLP (val))
663 Lisp_Object tem, tem1;
664 tem = Fget (val, Qevent_symbol_element_mask);
665 if (!NILP (tem))
667 tem1 = Fget (Fcar (tem), Qascii_character);
668 /* Merge this symbol's modifier bits
669 with the ASCII equivalent of its basic code. */
670 if (!NILP (tem1))
671 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
675 /* If we don't have a character now, deal with it appropriately. */
676 if (!INTEGERP (val))
678 if (error_nonascii)
680 Vunread_command_events = Fcons (val, Qnil);
681 error ("Non-character input-event");
683 else
684 goto retry;
688 if (! NILP (delayed_switch_frame))
689 unread_switch_frame = delayed_switch_frame;
691 #if 0
693 #ifdef HAVE_WINDOW_SYSTEM
694 if (display_hourglass_p)
695 start_hourglass ();
696 #endif
698 #endif
700 return val;
703 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
704 doc: /* Read a character from the command input (keyboard or macro).
705 It is returned as a number.
706 If the character has modifiers, they are resolved and reflected to the
707 character code if possible (e.g. C-SPC -> 0).
709 If the user generates an event which is not a character (i.e. a mouse
710 click or function key event), `read-char' signals an error. As an
711 exception, switch-frame events are put off until non-character events
712 can be read.
713 If you want to read non-character events, or ignore them, call
714 `read-event' or `read-char-exclusive' instead.
716 If the optional argument PROMPT is non-nil, display that as a prompt.
717 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
718 input method is turned on in the current buffer, that input method
719 is used for reading a character.
720 If the optional argument SECONDS is non-nil, it should be a number
721 specifying the maximum number of seconds to wait for input. If no
722 input arrives in that time, return nil. SECONDS may be a
723 floating-point value. */)
724 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
726 Lisp_Object val;
728 if (! NILP (prompt))
729 message_with_string ("%s", prompt, 0);
730 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
732 return (NILP (val) ? Qnil
733 : make_number (char_resolve_modifier_mask (XINT (val))));
736 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
737 doc: /* Read an event object from the input stream.
738 If the optional argument PROMPT is non-nil, display that as a prompt.
739 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
740 input method is turned on in the current buffer, that input method
741 is used for reading a character.
742 If the optional argument SECONDS is non-nil, it should be a number
743 specifying the maximum number of seconds to wait for input. If no
744 input arrives in that time, return nil. SECONDS may be a
745 floating-point value. */)
746 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
748 if (! NILP (prompt))
749 message_with_string ("%s", prompt, 0);
750 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
753 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
754 doc: /* Read a character from the command input (keyboard or macro).
755 It is returned as a number. Non-character events are ignored.
756 If the character has modifiers, they are resolved and reflected to the
757 character code if possible (e.g. C-SPC -> 0).
759 If the optional argument PROMPT is non-nil, display that as a prompt.
760 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
761 input method is turned on in the current buffer, that input method
762 is used for reading a character.
763 If the optional argument SECONDS is non-nil, it should be a number
764 specifying the maximum number of seconds to wait for input. If no
765 input arrives in that time, return nil. SECONDS may be a
766 floating-point value. */)
767 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
769 Lisp_Object val;
771 if (! NILP (prompt))
772 message_with_string ("%s", prompt, 0);
774 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
776 return (NILP (val) ? Qnil
777 : make_number (char_resolve_modifier_mask (XINT (val))));
780 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
781 doc: /* Don't use this yourself. */)
782 (void)
784 register Lisp_Object val;
785 block_input ();
786 XSETINT (val, getc (instream));
787 unblock_input ();
788 return val;
794 /* Return true if the lisp code read using READCHARFUN defines a non-nil
795 `lexical-binding' file variable. After returning, the stream is
796 positioned following the first line, if it is a comment or #! line,
797 otherwise nothing is read. */
799 static bool
800 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
802 int ch = READCHAR;
804 if (ch == '#')
806 ch = READCHAR;
807 if (ch != '!')
809 UNREAD (ch);
810 UNREAD ('#');
811 return 0;
813 while (ch != '\n' && ch != EOF)
814 ch = READCHAR;
815 if (ch == '\n') ch = READCHAR;
816 /* It is OK to leave the position after a #! line, since
817 that is what read1 does. */
820 if (ch != ';')
821 /* The first line isn't a comment, just give up. */
823 UNREAD (ch);
824 return 0;
826 else
827 /* Look for an appropriate file-variable in the first line. */
829 bool rv = 0;
830 enum {
831 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX,
832 } beg_end_state = NOMINAL;
833 bool in_file_vars = 0;
835 #define UPDATE_BEG_END_STATE(ch) \
836 if (beg_end_state == NOMINAL) \
837 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
838 else if (beg_end_state == AFTER_FIRST_DASH) \
839 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
840 else if (beg_end_state == AFTER_ASTERIX) \
842 if (ch == '-') \
843 in_file_vars = !in_file_vars; \
844 beg_end_state = NOMINAL; \
847 /* Skip until we get to the file vars, if any. */
850 ch = READCHAR;
851 UPDATE_BEG_END_STATE (ch);
853 while (!in_file_vars && ch != '\n' && ch != EOF);
855 while (in_file_vars)
857 char var[100], val[100];
858 unsigned i;
860 ch = READCHAR;
862 /* Read a variable name. */
863 while (ch == ' ' || ch == '\t')
864 ch = READCHAR;
866 i = 0;
867 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
869 if (i < sizeof var - 1)
870 var[i++] = ch;
871 UPDATE_BEG_END_STATE (ch);
872 ch = READCHAR;
875 /* Stop scanning if no colon was found before end marker. */
876 if (!in_file_vars || ch == '\n' || ch == EOF)
877 break;
879 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
880 i--;
881 var[i] = '\0';
883 if (ch == ':')
885 /* Read a variable value. */
886 ch = READCHAR;
888 while (ch == ' ' || ch == '\t')
889 ch = READCHAR;
891 i = 0;
892 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
894 if (i < sizeof val - 1)
895 val[i++] = ch;
896 UPDATE_BEG_END_STATE (ch);
897 ch = READCHAR;
899 if (! in_file_vars)
900 /* The value was terminated by an end-marker, which remove. */
901 i -= 3;
902 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
903 i--;
904 val[i] = '\0';
906 if (strcmp (var, "lexical-binding") == 0)
907 /* This is it... */
909 rv = (strcmp (val, "nil") != 0);
910 break;
915 while (ch != '\n' && ch != EOF)
916 ch = READCHAR;
918 return rv;
922 /* Value is a version number of byte compiled code if the file
923 associated with file descriptor FD is a compiled Lisp file that's
924 safe to load. Only files compiled with Emacs are safe to load.
925 Files compiled with XEmacs can lead to a crash in Fbyte_code
926 because of an incompatible change in the byte compiler. */
928 static int
929 safe_to_load_version (int fd)
931 char buf[512];
932 int nbytes, i;
933 int version = 1;
935 /* Read the first few bytes from the file, and look for a line
936 specifying the byte compiler version used. */
937 nbytes = emacs_read (fd, buf, sizeof buf);
938 if (nbytes > 0)
940 /* Skip to the next newline, skipping over the initial `ELC'
941 with NUL bytes following it, but note the version. */
942 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
943 if (i == 4)
944 version = buf[i];
946 if (i >= nbytes
947 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
948 buf + i, nbytes - i) < 0)
949 version = 0;
952 lseek (fd, 0, SEEK_SET);
953 return version;
957 /* Callback for record_unwind_protect. Restore the old load list OLD,
958 after loading a file successfully. */
960 static Lisp_Object
961 record_load_unwind (Lisp_Object old)
963 return Vloads_in_progress = old;
966 /* This handler function is used via internal_condition_case_1. */
968 static Lisp_Object
969 load_error_handler (Lisp_Object data)
971 return Qnil;
974 static Lisp_Object
975 load_warn_old_style_backquotes (Lisp_Object file)
977 if (!NILP (Vold_style_backquotes))
979 Lisp_Object args[2];
980 args[0] = build_string ("Loading `%s': old-style backquotes detected!");
981 args[1] = file;
982 Fmessage (2, args);
984 return Qnil;
987 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
988 doc: /* Return the suffixes that `load' should try if a suffix is \
989 required.
990 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
991 (void)
993 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
994 while (CONSP (suffixes))
996 Lisp_Object exts = Vload_file_rep_suffixes;
997 suffix = XCAR (suffixes);
998 suffixes = XCDR (suffixes);
999 while (CONSP (exts))
1001 ext = XCAR (exts);
1002 exts = XCDR (exts);
1003 lst = Fcons (concat2 (suffix, ext), lst);
1006 return Fnreverse (lst);
1009 DEFUN ("load", Fload, Sload, 1, 5, 0,
1010 doc: /* Execute a file of Lisp code named FILE.
1011 First try FILE with `.elc' appended, then try with `.el',
1012 then try FILE unmodified (the exact suffixes in the exact order are
1013 determined by `load-suffixes'). Environment variable references in
1014 FILE are replaced with their values by calling `substitute-in-file-name'.
1015 This function searches the directories in `load-path'.
1017 If optional second arg NOERROR is non-nil,
1018 report no error if FILE doesn't exist.
1019 Print messages at start and end of loading unless
1020 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
1021 overrides that).
1022 If optional fourth arg NOSUFFIX is non-nil, don't try adding
1023 suffixes `.elc' or `.el' to the specified name FILE.
1024 If optional fifth arg MUST-SUFFIX is non-nil, insist on
1025 the suffix `.elc' or `.el'; don't accept just FILE unless
1026 it ends in one of those suffixes or includes a directory name.
1028 If NOSUFFIX is nil, then if a file could not be found, try looking for
1029 a different representation of the file by adding non-empty suffixes to
1030 its name, before trying another file. Emacs uses this feature to find
1031 compressed versions of files when Auto Compression mode is enabled.
1032 If NOSUFFIX is non-nil, disable this feature.
1034 The suffixes that this function tries out, when NOSUFFIX is nil, are
1035 given by the return value of `get-load-suffixes' and the values listed
1036 in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1037 return value of `get-load-suffixes' is used, i.e. the file name is
1038 required to have a non-empty suffix.
1040 Loading a file records its definitions, and its `provide' and
1041 `require' calls, in an element of `load-history' whose
1042 car is the file name loaded. See `load-history'.
1044 While the file is in the process of being loaded, the variable
1045 `load-in-progress' is non-nil and the variable `load-file-name'
1046 is bound to the file's name.
1048 Return t if the file exists and loads successfully. */)
1049 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage, Lisp_Object nosuffix, Lisp_Object must_suffix)
1051 register FILE *stream;
1052 register int fd = -1;
1053 ptrdiff_t count = SPECPDL_INDEX ();
1054 struct gcpro gcpro1, gcpro2, gcpro3;
1055 Lisp_Object found, efound, hist_file_name;
1056 /* True means we printed the ".el is newer" message. */
1057 bool newer = 0;
1058 /* True means we are loading a compiled file. */
1059 bool compiled = 0;
1060 Lisp_Object handler;
1061 bool safe_p = 1;
1062 const char *fmode = "r";
1063 Lisp_Object tmp[2];
1064 int version;
1066 #ifdef DOS_NT
1067 fmode = "rt";
1068 #endif /* DOS_NT */
1070 CHECK_STRING (file);
1072 /* If file name is magic, call the handler. */
1073 /* This shouldn't be necessary any more now that `openp' handles it right.
1074 handler = Ffind_file_name_handler (file, Qload);
1075 if (!NILP (handler))
1076 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1078 /* Do this after the handler to avoid
1079 the need to gcpro noerror, nomessage and nosuffix.
1080 (Below here, we care only whether they are nil or not.)
1081 The presence of this call is the result of a historical accident:
1082 it used to be in every file-operation and when it got removed
1083 everywhere, it accidentally stayed here. Since then, enough people
1084 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1085 that it seemed risky to remove. */
1086 if (! NILP (noerror))
1088 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1089 Qt, load_error_handler);
1090 if (NILP (file))
1091 return Qnil;
1093 else
1094 file = Fsubstitute_in_file_name (file);
1097 /* Avoid weird lossage with null string as arg,
1098 since it would try to load a directory as a Lisp file. */
1099 if (SBYTES (file) > 0)
1101 ptrdiff_t size = SBYTES (file);
1103 found = Qnil;
1104 GCPRO2 (file, found);
1106 if (! NILP (must_suffix))
1108 /* Don't insist on adding a suffix if FILE already ends with one. */
1109 if (size > 3
1110 && !strcmp (SSDATA (file) + size - 3, ".el"))
1111 must_suffix = Qnil;
1112 else if (size > 4
1113 && !strcmp (SSDATA (file) + size - 4, ".elc"))
1114 must_suffix = Qnil;
1115 /* Don't insist on adding a suffix
1116 if the argument includes a directory name. */
1117 else if (! NILP (Ffile_name_directory (file)))
1118 must_suffix = Qnil;
1121 fd = openp (Vload_path, file,
1122 (!NILP (nosuffix) ? Qnil
1123 : !NILP (must_suffix) ? Fget_load_suffixes ()
1124 : Fappend (2, (tmp[0] = Fget_load_suffixes (),
1125 tmp[1] = Vload_file_rep_suffixes,
1126 tmp))),
1127 &found, Qnil);
1128 UNGCPRO;
1131 if (fd == -1)
1133 if (NILP (noerror))
1134 xsignal2 (Qfile_error, build_string ("Cannot open load file"), file);
1135 return Qnil;
1138 /* Tell startup.el whether or not we found the user's init file. */
1139 if (EQ (Qt, Vuser_init_file))
1140 Vuser_init_file = found;
1142 /* If FD is -2, that means openp found a magic file. */
1143 if (fd == -2)
1145 if (NILP (Fequal (found, file)))
1146 /* If FOUND is a different file name from FILE,
1147 find its handler even if we have already inhibited
1148 the `load' operation on FILE. */
1149 handler = Ffind_file_name_handler (found, Qt);
1150 else
1151 handler = Ffind_file_name_handler (found, Qload);
1152 if (! NILP (handler))
1153 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1154 #ifdef DOS_NT
1155 /* Tramp has to deal with semi-broken packages that prepend
1156 drive letters to remote files. For that reason, Tramp
1157 catches file operations that test for file existence, which
1158 makes openp think X:/foo.elc files are remote. However,
1159 Tramp does not catch `load' operations for such files, so we
1160 end up with a nil as the `load' handler above. If we would
1161 continue with fd = -2, we will behave wrongly, and in
1162 particular try reading a .elc file in the "rt" mode instead
1163 of "rb". See bug #9311 for the results. To work around
1164 this, we try to open the file locally, and go with that if it
1165 succeeds. */
1166 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1167 if (fd == -1)
1168 fd = -2;
1169 #endif
1172 /* Check if we're stuck in a recursive load cycle.
1174 2000-09-21: It's not possible to just check for the file loaded
1175 being a member of Vloads_in_progress. This fails because of the
1176 way the byte compiler currently works; `provide's are not
1177 evaluated, see font-lock.el/jit-lock.el as an example. This
1178 leads to a certain amount of ``normal'' recursion.
1180 Also, just loading a file recursively is not always an error in
1181 the general case; the second load may do something different. */
1183 int load_count = 0;
1184 Lisp_Object tem;
1185 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1186 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1188 if (fd >= 0)
1189 emacs_close (fd);
1190 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1192 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1193 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1196 /* All loads are by default dynamic, unless the file itself specifies
1197 otherwise using a file-variable in the first line. This is bound here
1198 so that it takes effect whether or not we use
1199 Vload_source_file_function. */
1200 specbind (Qlexical_binding, Qnil);
1202 /* Get the name for load-history. */
1203 hist_file_name = (! NILP (Vpurify_flag)
1204 ? Fconcat (2, (tmp[0] = Ffile_name_directory (file),
1205 tmp[1] = Ffile_name_nondirectory (found),
1206 tmp))
1207 : found) ;
1209 version = -1;
1211 /* Check for the presence of old-style quotes and warn about them. */
1212 specbind (Qold_style_backquotes, Qnil);
1213 record_unwind_protect (load_warn_old_style_backquotes, file);
1215 if (!memcmp (SDATA (found) + SBYTES (found) - 4, ".elc", 4)
1216 || (fd >= 0 && (version = safe_to_load_version (fd)) > 0))
1217 /* Load .elc files directly, but not when they are
1218 remote and have no handler! */
1220 if (fd != -2)
1222 struct stat s1, s2;
1223 int result;
1225 GCPRO3 (file, found, hist_file_name);
1227 if (version < 0
1228 && ! (version = safe_to_load_version (fd)))
1230 safe_p = 0;
1231 if (!load_dangerous_libraries)
1233 if (fd >= 0)
1234 emacs_close (fd);
1235 error ("File `%s' was not compiled in Emacs",
1236 SDATA (found));
1238 else if (!NILP (nomessage) && !force_load_messages)
1239 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1242 compiled = 1;
1244 efound = ENCODE_FILE (found);
1246 #ifdef DOS_NT
1247 fmode = "rb";
1248 #endif /* DOS_NT */
1249 result = stat (SSDATA (efound), &s1);
1250 if (result == 0)
1252 SSET (efound, SBYTES (efound) - 1, 0);
1253 result = stat (SSDATA (efound), &s2);
1254 SSET (efound, SBYTES (efound) - 1, 'c');
1257 if (result == 0
1258 && EMACS_TIME_LT (get_stat_mtime (&s1), get_stat_mtime (&s2)))
1260 /* Make the progress messages mention that source is newer. */
1261 newer = 1;
1263 /* If we won't print another message, mention this anyway. */
1264 if (!NILP (nomessage) && !force_load_messages)
1266 Lisp_Object msg_file;
1267 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1268 message_with_string ("Source file `%s' newer than byte-compiled file",
1269 msg_file, 1);
1272 UNGCPRO;
1275 else
1277 /* We are loading a source file (*.el). */
1278 if (!NILP (Vload_source_file_function))
1280 Lisp_Object val;
1282 if (fd >= 0)
1283 emacs_close (fd);
1284 val = call4 (Vload_source_file_function, found, hist_file_name,
1285 NILP (noerror) ? Qnil : Qt,
1286 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1287 return unbind_to (count, val);
1291 GCPRO3 (file, found, hist_file_name);
1293 #ifdef WINDOWSNT
1294 efound = ENCODE_FILE (found);
1295 /* If we somehow got here with fd == -2, meaning the file is deemed
1296 to be remote, don't even try to reopen the file locally; just
1297 force a failure instead. */
1298 if (fd >= 0)
1300 emacs_close (fd);
1301 stream = fopen (SSDATA (efound), fmode);
1303 else
1304 stream = NULL;
1305 #else /* not WINDOWSNT */
1306 stream = fdopen (fd, fmode);
1307 #endif /* not WINDOWSNT */
1308 if (stream == 0)
1310 emacs_close (fd);
1311 error ("Failure to create stdio stream for %s", SDATA (file));
1314 if (! NILP (Vpurify_flag))
1315 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1317 if (NILP (nomessage) || force_load_messages)
1319 if (!safe_p)
1320 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1321 file, 1);
1322 else if (!compiled)
1323 message_with_string ("Loading %s (source)...", file, 1);
1324 else if (newer)
1325 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1326 file, 1);
1327 else /* The typical case; compiled file newer than source file. */
1328 message_with_string ("Loading %s...", file, 1);
1331 record_unwind_protect (load_unwind, make_save_pointer (stream));
1332 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
1333 specbind (Qload_file_name, found);
1334 specbind (Qinhibit_file_name_operation, Qnil);
1335 load_descriptor_list
1336 = Fcons (make_number (fileno (stream)), load_descriptor_list);
1337 specbind (Qload_in_progress, Qt);
1339 instream = stream;
1340 if (lisp_file_lexically_bound_p (Qget_file_char))
1341 Fset (Qlexical_binding, Qt);
1343 if (! version || version >= 22)
1344 readevalloop (Qget_file_char, stream, hist_file_name,
1345 0, Qnil, Qnil, Qnil, Qnil);
1346 else
1348 /* We can't handle a file which was compiled with
1349 byte-compile-dynamic by older version of Emacs. */
1350 specbind (Qload_force_doc_strings, Qt);
1351 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name,
1352 0, Qnil, Qnil, Qnil, Qnil);
1354 unbind_to (count, Qnil);
1356 /* Run any eval-after-load forms for this file. */
1357 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1358 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1360 UNGCPRO;
1362 xfree (saved_doc_string);
1363 saved_doc_string = 0;
1364 saved_doc_string_size = 0;
1366 xfree (prev_saved_doc_string);
1367 prev_saved_doc_string = 0;
1368 prev_saved_doc_string_size = 0;
1370 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1372 if (!safe_p)
1373 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1374 file, 1);
1375 else if (!compiled)
1376 message_with_string ("Loading %s (source)...done", file, 1);
1377 else if (newer)
1378 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1379 file, 1);
1380 else /* The typical case; compiled file newer than source file. */
1381 message_with_string ("Loading %s...done", file, 1);
1384 return Qt;
1387 static Lisp_Object
1388 load_unwind (Lisp_Object arg) /* Used as unwind-protect function in load. */
1390 FILE *stream = XSAVE_POINTER (arg, 0);
1391 if (stream != NULL)
1393 block_input ();
1394 fclose (stream);
1395 unblock_input ();
1397 return Qnil;
1400 static Lisp_Object
1401 load_descriptor_unwind (Lisp_Object oldlist)
1403 load_descriptor_list = oldlist;
1404 return Qnil;
1407 /* Close all descriptors in use for Floads.
1408 This is used when starting a subprocess. */
1410 void
1411 close_load_descs (void)
1413 #ifndef WINDOWSNT
1414 Lisp_Object tail;
1415 for (tail = load_descriptor_list; CONSP (tail); tail = XCDR (tail))
1416 emacs_close (XFASTINT (XCAR (tail)));
1417 #endif
1420 static bool
1421 complete_filename_p (Lisp_Object pathname)
1423 const unsigned char *s = SDATA (pathname);
1424 return (IS_DIRECTORY_SEP (s[0])
1425 || (SCHARS (pathname) > 2
1426 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1429 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1430 doc: /* Search for FILENAME through PATH.
1431 Returns the file's name in absolute form, or nil if not found.
1432 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1433 file name when searching.
1434 If non-nil, PREDICATE is used instead of `file-readable-p'.
1435 PREDICATE can also be an integer to pass to the faccessat(2) function,
1436 in which case file-name-handlers are ignored.
1437 This function will normally skip directories, so if you want it to find
1438 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1439 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1441 Lisp_Object file;
1442 int fd = openp (path, filename, suffixes, &file, predicate);
1443 if (NILP (predicate) && fd > 0)
1444 close (fd);
1445 return file;
1448 static Lisp_Object Qdir_ok;
1450 /* Search for a file whose name is STR, looking in directories
1451 in the Lisp list PATH, and trying suffixes from SUFFIX.
1452 On success, returns a file descriptor. On failure, returns -1.
1454 SUFFIXES is a list of strings containing possible suffixes.
1455 The empty suffix is automatically added if the list is empty.
1457 PREDICATE non-nil means don't open the files,
1458 just look for one that satisfies the predicate. In this case,
1459 returns 1 on success. The predicate can be a lisp function or
1460 an integer to pass to `access' (in which case file-name-handlers
1461 are ignored).
1463 If STOREPTR is nonzero, it points to a slot where the name of
1464 the file actually found should be stored as a Lisp string.
1465 nil is stored there on failure.
1467 If the file we find is remote, return -2
1468 but store the found remote file name in *STOREPTR. */
1471 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *storeptr, Lisp_Object predicate)
1473 ptrdiff_t fn_size = 100;
1474 char buf[100];
1475 char *fn = buf;
1476 bool absolute = 0;
1477 ptrdiff_t want_length;
1478 Lisp_Object filename;
1479 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
1480 Lisp_Object string, tail, encoded_fn;
1481 ptrdiff_t max_suffix_len = 0;
1483 CHECK_STRING (str);
1485 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1487 CHECK_STRING_CAR (tail);
1488 max_suffix_len = max (max_suffix_len,
1489 SBYTES (XCAR (tail)));
1492 string = filename = encoded_fn = Qnil;
1493 GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
1495 if (storeptr)
1496 *storeptr = Qnil;
1498 if (complete_filename_p (str))
1499 absolute = 1;
1501 for (; CONSP (path); path = XCDR (path))
1503 filename = Fexpand_file_name (str, XCAR (path));
1504 if (!complete_filename_p (filename))
1505 /* If there are non-absolute elts in PATH (eg "."). */
1506 /* Of course, this could conceivably lose if luser sets
1507 default-directory to be something non-absolute... */
1509 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1510 if (!complete_filename_p (filename))
1511 /* Give up on this path element! */
1512 continue;
1515 /* Calculate maximum length of any filename made from
1516 this path element/specified file name and any possible suffix. */
1517 want_length = max_suffix_len + SBYTES (filename);
1518 if (fn_size <= want_length)
1519 fn = alloca (fn_size = 100 + want_length);
1521 /* Loop over suffixes. */
1522 for (tail = NILP (suffixes) ? Fcons (empty_unibyte_string, Qnil) : suffixes;
1523 CONSP (tail); tail = XCDR (tail))
1525 ptrdiff_t fnlen, lsuffix = SBYTES (XCAR (tail));
1526 Lisp_Object handler;
1528 /* Concatenate path element/specified name with the suffix.
1529 If the directory starts with /:, remove that. */
1530 int prefixlen = ((SCHARS (filename) > 2
1531 && SREF (filename, 0) == '/'
1532 && SREF (filename, 1) == ':')
1533 ? 2 : 0);
1534 fnlen = SBYTES (filename) - prefixlen;
1535 memcpy (fn, SDATA (filename) + prefixlen, fnlen);
1536 memcpy (fn + fnlen, SDATA (XCAR (tail)), lsuffix + 1);
1537 fnlen += lsuffix;
1538 /* Check that the file exists and is not a directory. */
1539 /* We used to only check for handlers on non-absolute file names:
1540 if (absolute)
1541 handler = Qnil;
1542 else
1543 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1544 It's not clear why that was the case and it breaks things like
1545 (load "/bar.el") where the file is actually "/bar.el.gz". */
1546 string = make_string (fn, fnlen);
1547 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1548 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1550 bool exists;
1551 if (NILP (predicate))
1552 exists = !NILP (Ffile_readable_p (string));
1553 else
1555 Lisp_Object tmp = call1 (predicate, string);
1556 exists = !NILP (tmp)
1557 && (EQ (tmp, Qdir_ok)
1558 || NILP (Ffile_directory_p (string)));
1561 if (exists)
1563 /* We succeeded; return this descriptor and filename. */
1564 if (storeptr)
1565 *storeptr = string;
1566 UNGCPRO;
1567 return -2;
1570 else
1572 int fd;
1573 const char *pfn;
1575 encoded_fn = ENCODE_FILE (string);
1576 pfn = SSDATA (encoded_fn);
1578 /* Check that we can access or open it. */
1579 if (NATNUMP (predicate))
1580 fd = (((XFASTINT (predicate) & ~INT_MAX) == 0
1581 && (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
1582 AT_EACCESS)
1583 == 0)
1584 && ! file_directory_p (pfn))
1585 ? 1 : -1);
1586 else
1588 struct stat st;
1589 fd = emacs_open (pfn, O_RDONLY, 0);
1590 if (fd >= 0
1591 && (fstat (fd, &st) != 0 || S_ISDIR (st.st_mode)))
1593 emacs_close (fd);
1594 fd = -1;
1598 if (fd >= 0)
1600 /* We succeeded; return this descriptor and filename. */
1601 if (storeptr)
1602 *storeptr = string;
1603 UNGCPRO;
1604 return fd;
1608 if (absolute)
1609 break;
1612 UNGCPRO;
1613 return -1;
1617 /* Merge the list we've accumulated of globals from the current input source
1618 into the load_history variable. The details depend on whether
1619 the source has an associated file name or not.
1621 FILENAME is the file name that we are loading from.
1623 ENTIRE is true if loading that entire file, false if evaluating
1624 part of it. */
1626 static void
1627 build_load_history (Lisp_Object filename, bool entire)
1629 Lisp_Object tail, prev, newelt;
1630 Lisp_Object tem, tem2;
1631 bool foundit = 0;
1633 tail = Vload_history;
1634 prev = Qnil;
1636 while (CONSP (tail))
1638 tem = XCAR (tail);
1640 /* Find the feature's previous assoc list... */
1641 if (!NILP (Fequal (filename, Fcar (tem))))
1643 foundit = 1;
1645 /* If we're loading the entire file, remove old data. */
1646 if (entire)
1648 if (NILP (prev))
1649 Vload_history = XCDR (tail);
1650 else
1651 Fsetcdr (prev, XCDR (tail));
1654 /* Otherwise, cons on new symbols that are not already members. */
1655 else
1657 tem2 = Vcurrent_load_list;
1659 while (CONSP (tem2))
1661 newelt = XCAR (tem2);
1663 if (NILP (Fmember (newelt, tem)))
1664 Fsetcar (tail, Fcons (XCAR (tem),
1665 Fcons (newelt, XCDR (tem))));
1667 tem2 = XCDR (tem2);
1668 QUIT;
1672 else
1673 prev = tail;
1674 tail = XCDR (tail);
1675 QUIT;
1678 /* If we're loading an entire file, cons the new assoc onto the
1679 front of load-history, the most-recently-loaded position. Also
1680 do this if we didn't find an existing member for the file. */
1681 if (entire || !foundit)
1682 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1683 Vload_history);
1686 static Lisp_Object
1687 readevalloop_1 (Lisp_Object old)
1689 load_convert_to_unibyte = ! NILP (old);
1690 return Qnil;
1693 /* Signal an `end-of-file' error, if possible with file name
1694 information. */
1696 static _Noreturn void
1697 end_of_file_error (void)
1699 if (STRINGP (Vload_file_name))
1700 xsignal1 (Qend_of_file, Vload_file_name);
1702 xsignal0 (Qend_of_file);
1705 /* UNIBYTE specifies how to set load_convert_to_unibyte
1706 for this invocation.
1707 READFUN, if non-nil, is used instead of `read'.
1709 START, END specify region to read in current buffer (from eval-region).
1710 If the input is not from a buffer, they must be nil. */
1712 static void
1713 readevalloop (Lisp_Object readcharfun,
1714 FILE *stream,
1715 Lisp_Object sourcename,
1716 bool printflag,
1717 Lisp_Object unibyte, Lisp_Object readfun,
1718 Lisp_Object start, Lisp_Object end)
1720 register int c;
1721 register Lisp_Object val;
1722 ptrdiff_t count = SPECPDL_INDEX ();
1723 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1724 struct buffer *b = 0;
1725 bool continue_reading_p;
1726 Lisp_Object lex_bound;
1727 /* True if reading an entire buffer. */
1728 bool whole_buffer = 0;
1729 /* True on the first time around. */
1730 bool first_sexp = 1;
1731 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1733 if (NILP (Ffboundp (macroexpand))
1734 /* Don't macroexpand in .elc files, since it should have been done
1735 already. We actually don't know whether we're in a .elc file or not,
1736 so we use circumstantial evidence: .el files normally go through
1737 Vload_source_file_function -> load-with-code-conversion
1738 -> eval-buffer. */
1739 || EQ (readcharfun, Qget_file_char)
1740 || EQ (readcharfun, Qget_emacs_mule_file_char))
1741 macroexpand = Qnil;
1743 if (MARKERP (readcharfun))
1745 if (NILP (start))
1746 start = readcharfun;
1749 if (BUFFERP (readcharfun))
1750 b = XBUFFER (readcharfun);
1751 else if (MARKERP (readcharfun))
1752 b = XMARKER (readcharfun)->buffer;
1754 /* We assume START is nil when input is not from a buffer. */
1755 if (! NILP (start) && !b)
1756 emacs_abort ();
1758 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
1759 specbind (Qcurrent_load_list, Qnil);
1760 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1761 load_convert_to_unibyte = !NILP (unibyte);
1763 /* If lexical binding is active (either because it was specified in
1764 the file's header, or via a buffer-local variable), create an empty
1765 lexical environment, otherwise, turn off lexical binding. */
1766 lex_bound = find_symbol_value (Qlexical_binding);
1767 specbind (Qinternal_interpreter_environment,
1768 NILP (lex_bound) || EQ (lex_bound, Qunbound)
1769 ? Qnil : Fcons (Qt, Qnil));
1771 GCPRO4 (sourcename, readfun, start, end);
1773 /* Try to ensure sourcename is a truename, except whilst preloading. */
1774 if (NILP (Vpurify_flag)
1775 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1776 && !NILP (Ffboundp (Qfile_truename)))
1777 sourcename = call1 (Qfile_truename, sourcename) ;
1779 LOADHIST_ATTACH (sourcename);
1781 continue_reading_p = 1;
1782 while (continue_reading_p)
1784 ptrdiff_t count1 = SPECPDL_INDEX ();
1786 if (b != 0 && !BUFFER_LIVE_P (b))
1787 error ("Reading from killed buffer");
1789 if (!NILP (start))
1791 /* Switch to the buffer we are reading from. */
1792 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1793 set_buffer_internal (b);
1795 /* Save point in it. */
1796 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1797 /* Save ZV in it. */
1798 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1799 /* Those get unbound after we read one expression. */
1801 /* Set point and ZV around stuff to be read. */
1802 Fgoto_char (start);
1803 if (!NILP (end))
1804 Fnarrow_to_region (make_number (BEGV), end);
1806 /* Just for cleanliness, convert END to a marker
1807 if it is an integer. */
1808 if (INTEGERP (end))
1809 end = Fpoint_max_marker ();
1812 /* On the first cycle, we can easily test here
1813 whether we are reading the whole buffer. */
1814 if (b && first_sexp)
1815 whole_buffer = (PT == BEG && ZV == Z);
1817 instream = stream;
1818 read_next:
1819 c = READCHAR;
1820 if (c == ';')
1822 while ((c = READCHAR) != '\n' && c != -1);
1823 goto read_next;
1825 if (c < 0)
1827 unbind_to (count1, Qnil);
1828 break;
1831 /* Ignore whitespace here, so we can detect eof. */
1832 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1833 || c == 0xa0) /* NBSP */
1834 goto read_next;
1836 if (!NILP (Vpurify_flag) && c == '(')
1838 val = read_list (0, readcharfun);
1840 else
1842 UNREAD (c);
1843 read_objects = Qnil;
1844 if (!NILP (readfun))
1846 val = call1 (readfun, readcharfun);
1848 /* If READCHARFUN has set point to ZV, we should
1849 stop reading, even if the form read sets point
1850 to a different value when evaluated. */
1851 if (BUFFERP (readcharfun))
1853 struct buffer *buf = XBUFFER (readcharfun);
1854 if (BUF_PT (buf) == BUF_ZV (buf))
1855 continue_reading_p = 0;
1858 else if (! NILP (Vload_read_function))
1859 val = call1 (Vload_read_function, readcharfun);
1860 else
1861 val = read_internal_start (readcharfun, Qnil, Qnil);
1864 if (!NILP (start) && continue_reading_p)
1865 start = Fpoint_marker ();
1867 /* Restore saved point and BEGV. */
1868 unbind_to (count1, Qnil);
1870 /* Now eval what we just read. */
1871 if (!NILP (macroexpand))
1872 val = call1 (macroexpand, val);
1873 val = eval_sub (val);
1875 if (printflag)
1877 Vvalues = Fcons (val, Vvalues);
1878 if (EQ (Vstandard_output, Qt))
1879 Fprin1 (val, Qnil);
1880 else
1881 Fprint (val, Qnil);
1884 first_sexp = 0;
1887 build_load_history (sourcename,
1888 stream || whole_buffer);
1890 UNGCPRO;
1892 unbind_to (count, Qnil);
1895 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1896 doc: /* Execute the current buffer as Lisp code.
1897 When called from a Lisp program (i.e., not interactively), this
1898 function accepts up to five optional arguments:
1899 BUFFER is the buffer to evaluate (nil means use current buffer).
1900 PRINTFLAG controls printing of output:
1901 A value of nil means discard it; anything else is stream for print.
1902 FILENAME specifies the file name to use for `load-history'.
1903 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
1904 invocation.
1905 DO-ALLOW-PRINT, if non-nil, specifies that `print' and related
1906 functions should work normally even if PRINTFLAG is nil.
1908 This function preserves the position of point. */)
1909 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
1911 ptrdiff_t count = SPECPDL_INDEX ();
1912 Lisp_Object tem, buf;
1914 if (NILP (buffer))
1915 buf = Fcurrent_buffer ();
1916 else
1917 buf = Fget_buffer (buffer);
1918 if (NILP (buf))
1919 error ("No such buffer");
1921 if (NILP (printflag) && NILP (do_allow_print))
1922 tem = Qsymbolp;
1923 else
1924 tem = printflag;
1926 if (NILP (filename))
1927 filename = BVAR (XBUFFER (buf), filename);
1929 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1930 specbind (Qstandard_output, tem);
1931 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1932 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1933 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
1934 readevalloop (buf, 0, filename,
1935 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
1936 unbind_to (count, Qnil);
1938 return Qnil;
1941 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1942 doc: /* Execute the region as Lisp code.
1943 When called from programs, expects two arguments,
1944 giving starting and ending indices in the current buffer
1945 of the text to be executed.
1946 Programs can pass third argument PRINTFLAG which controls output:
1947 A value of nil means discard it; anything else is stream for printing it.
1948 Also the fourth argument READ-FUNCTION, if non-nil, is used
1949 instead of `read' to read each expression. It gets one argument
1950 which is the input stream for reading characters.
1952 This function does not move point. */)
1953 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
1955 /* FIXME: Do the eval-sexp-add-defvars dance! */
1956 ptrdiff_t count = SPECPDL_INDEX ();
1957 Lisp_Object tem, cbuf;
1959 cbuf = Fcurrent_buffer ();
1961 if (NILP (printflag))
1962 tem = Qsymbolp;
1963 else
1964 tem = printflag;
1965 specbind (Qstandard_output, tem);
1966 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
1968 /* `readevalloop' calls functions which check the type of start and end. */
1969 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
1970 !NILP (printflag), Qnil, read_function,
1971 start, end);
1973 return unbind_to (count, Qnil);
1977 DEFUN ("read", Fread, Sread, 0, 1, 0,
1978 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1979 If STREAM is nil, use the value of `standard-input' (which see).
1980 STREAM or the value of `standard-input' may be:
1981 a buffer (read from point and advance it)
1982 a marker (read from where it points and advance it)
1983 a function (call it with no arguments for each character,
1984 call it with a char as argument to push a char back)
1985 a string (takes text from string, starting at the beginning)
1986 t (read text line using minibuffer and use it, or read from
1987 standard input in batch mode). */)
1988 (Lisp_Object stream)
1990 if (NILP (stream))
1991 stream = Vstandard_input;
1992 if (EQ (stream, Qt))
1993 stream = Qread_char;
1994 if (EQ (stream, Qread_char))
1995 /* FIXME: ¿¡ When is this used !? */
1996 return call1 (intern ("read-minibuffer"),
1997 build_string ("Lisp expression: "));
1999 return read_internal_start (stream, Qnil, Qnil);
2002 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
2003 doc: /* Read one Lisp expression which is represented as text by STRING.
2004 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
2005 FINAL-STRING-INDEX is an integer giving the position of the next
2006 remaining character in STRING.
2007 START and END optionally delimit a substring of STRING from which to read;
2008 they default to 0 and (length STRING) respectively. */)
2009 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
2011 Lisp_Object ret;
2012 CHECK_STRING (string);
2013 /* `read_internal_start' sets `read_from_string_index'. */
2014 ret = read_internal_start (string, start, end);
2015 return Fcons (ret, make_number (read_from_string_index));
2018 /* Function to set up the global context we need in toplevel read
2019 calls. */
2020 static Lisp_Object
2021 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
2022 /* `start', `end' only used when stream is a string. */
2024 Lisp_Object retval;
2026 readchar_count = 0;
2027 new_backquote_flag = 0;
2028 read_objects = Qnil;
2029 if (EQ (Vread_with_symbol_positions, Qt)
2030 || EQ (Vread_with_symbol_positions, stream))
2031 Vread_symbol_positions_list = Qnil;
2033 if (STRINGP (stream)
2034 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2036 ptrdiff_t startval, endval;
2037 Lisp_Object string;
2039 if (STRINGP (stream))
2040 string = stream;
2041 else
2042 string = XCAR (stream);
2044 if (NILP (end))
2045 endval = SCHARS (string);
2046 else
2048 CHECK_NUMBER (end);
2049 if (! (0 <= XINT (end) && XINT (end) <= SCHARS (string)))
2050 args_out_of_range (string, end);
2051 endval = XINT (end);
2054 if (NILP (start))
2055 startval = 0;
2056 else
2058 CHECK_NUMBER (start);
2059 if (! (0 <= XINT (start) && XINT (start) <= endval))
2060 args_out_of_range (string, start);
2061 startval = XINT (start);
2063 read_from_string_index = startval;
2064 read_from_string_index_byte = string_char_to_byte (string, startval);
2065 read_from_string_limit = endval;
2068 retval = read0 (stream);
2069 if (EQ (Vread_with_symbol_positions, Qt)
2070 || EQ (Vread_with_symbol_positions, stream))
2071 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2072 return retval;
2076 /* Signal Qinvalid_read_syntax error.
2077 S is error string of length N (if > 0) */
2079 static _Noreturn void
2080 invalid_syntax (const char *s)
2082 xsignal1 (Qinvalid_read_syntax, build_string (s));
2086 /* Use this for recursive reads, in contexts where internal tokens
2087 are not allowed. */
2089 static Lisp_Object
2090 read0 (Lisp_Object readcharfun)
2092 register Lisp_Object val;
2093 int c;
2095 val = read1 (readcharfun, &c, 0);
2096 if (!c)
2097 return val;
2099 xsignal1 (Qinvalid_read_syntax,
2100 Fmake_string (make_number (1), make_number (c)));
2103 static ptrdiff_t read_buffer_size;
2104 static char *read_buffer;
2106 /* Read a \-escape sequence, assuming we already read the `\'.
2107 If the escape sequence forces unibyte, return eight-bit char. */
2109 static int
2110 read_escape (Lisp_Object readcharfun, bool stringp)
2112 int c = READCHAR;
2113 /* \u allows up to four hex digits, \U up to eight. Default to the
2114 behavior for \u, and change this value in the case that \U is seen. */
2115 int unicode_hex_count = 4;
2117 switch (c)
2119 case -1:
2120 end_of_file_error ();
2122 case 'a':
2123 return '\007';
2124 case 'b':
2125 return '\b';
2126 case 'd':
2127 return 0177;
2128 case 'e':
2129 return 033;
2130 case 'f':
2131 return '\f';
2132 case 'n':
2133 return '\n';
2134 case 'r':
2135 return '\r';
2136 case 't':
2137 return '\t';
2138 case 'v':
2139 return '\v';
2140 case '\n':
2141 return -1;
2142 case ' ':
2143 if (stringp)
2144 return -1;
2145 return ' ';
2147 case 'M':
2148 c = READCHAR;
2149 if (c != '-')
2150 error ("Invalid escape character syntax");
2151 c = READCHAR;
2152 if (c == '\\')
2153 c = read_escape (readcharfun, 0);
2154 return c | meta_modifier;
2156 case 'S':
2157 c = READCHAR;
2158 if (c != '-')
2159 error ("Invalid escape character syntax");
2160 c = READCHAR;
2161 if (c == '\\')
2162 c = read_escape (readcharfun, 0);
2163 return c | shift_modifier;
2165 case 'H':
2166 c = READCHAR;
2167 if (c != '-')
2168 error ("Invalid escape character syntax");
2169 c = READCHAR;
2170 if (c == '\\')
2171 c = read_escape (readcharfun, 0);
2172 return c | hyper_modifier;
2174 case 'A':
2175 c = READCHAR;
2176 if (c != '-')
2177 error ("Invalid escape character syntax");
2178 c = READCHAR;
2179 if (c == '\\')
2180 c = read_escape (readcharfun, 0);
2181 return c | alt_modifier;
2183 case 's':
2184 c = READCHAR;
2185 if (stringp || c != '-')
2187 UNREAD (c);
2188 return ' ';
2190 c = READCHAR;
2191 if (c == '\\')
2192 c = read_escape (readcharfun, 0);
2193 return c | super_modifier;
2195 case 'C':
2196 c = READCHAR;
2197 if (c != '-')
2198 error ("Invalid escape character syntax");
2199 case '^':
2200 c = READCHAR;
2201 if (c == '\\')
2202 c = read_escape (readcharfun, 0);
2203 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2204 return 0177 | (c & CHAR_MODIFIER_MASK);
2205 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2206 return c | ctrl_modifier;
2207 /* ASCII control chars are made from letters (both cases),
2208 as well as the non-letters within 0100...0137. */
2209 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2210 return (c & (037 | ~0177));
2211 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2212 return (c & (037 | ~0177));
2213 else
2214 return c | ctrl_modifier;
2216 case '0':
2217 case '1':
2218 case '2':
2219 case '3':
2220 case '4':
2221 case '5':
2222 case '6':
2223 case '7':
2224 /* An octal escape, as in ANSI C. */
2226 register int i = c - '0';
2227 register int count = 0;
2228 while (++count < 3)
2230 if ((c = READCHAR) >= '0' && c <= '7')
2232 i *= 8;
2233 i += c - '0';
2235 else
2237 UNREAD (c);
2238 break;
2242 if (i >= 0x80 && i < 0x100)
2243 i = BYTE8_TO_CHAR (i);
2244 return i;
2247 case 'x':
2248 /* A hex escape, as in ANSI C. */
2250 unsigned int i = 0;
2251 int count = 0;
2252 while (1)
2254 c = READCHAR;
2255 if (c >= '0' && c <= '9')
2257 i *= 16;
2258 i += c - '0';
2260 else if ((c >= 'a' && c <= 'f')
2261 || (c >= 'A' && c <= 'F'))
2263 i *= 16;
2264 if (c >= 'a' && c <= 'f')
2265 i += c - 'a' + 10;
2266 else
2267 i += c - 'A' + 10;
2269 else
2271 UNREAD (c);
2272 break;
2274 /* Allow hex escapes as large as ?\xfffffff, because some
2275 packages use them to denote characters with modifiers. */
2276 if ((CHAR_META | (CHAR_META - 1)) < i)
2277 error ("Hex character out of range: \\x%x...", i);
2278 count += count < 3;
2281 if (count < 3 && i >= 0x80)
2282 return BYTE8_TO_CHAR (i);
2283 return i;
2286 case 'U':
2287 /* Post-Unicode-2.0: Up to eight hex chars. */
2288 unicode_hex_count = 8;
2289 case 'u':
2291 /* A Unicode escape. We only permit them in strings and characters,
2292 not arbitrarily in the source code, as in some other languages. */
2294 unsigned int i = 0;
2295 int count = 0;
2297 while (++count <= unicode_hex_count)
2299 c = READCHAR;
2300 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2301 want. */
2302 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2303 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2304 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2305 else
2306 error ("Non-hex digit used for Unicode escape");
2308 if (i > 0x10FFFF)
2309 error ("Non-Unicode character: 0x%x", i);
2310 return i;
2313 default:
2314 return c;
2318 /* Return the digit that CHARACTER stands for in the given BASE.
2319 Return -1 if CHARACTER is out of range for BASE,
2320 and -2 if CHARACTER is not valid for any supported BASE. */
2321 static int
2322 digit_to_number (int character, int base)
2324 int digit;
2326 if ('0' <= character && character <= '9')
2327 digit = character - '0';
2328 else if ('a' <= character && character <= 'z')
2329 digit = character - 'a' + 10;
2330 else if ('A' <= character && character <= 'Z')
2331 digit = character - 'A' + 10;
2332 else
2333 return -2;
2335 return digit < base ? digit : -1;
2338 /* Read an integer in radix RADIX using READCHARFUN to read
2339 characters. RADIX must be in the interval [2..36]; if it isn't, a
2340 read error is signaled . Value is the integer read. Signals an
2341 error if encountering invalid read syntax or if RADIX is out of
2342 range. */
2344 static Lisp_Object
2345 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2347 /* Room for sign, leading 0, other digits, trailing null byte.
2348 Also, room for invalid syntax diagnostic. */
2349 char buf[max (1 + 1 + sizeof (uintmax_t) * CHAR_BIT + 1,
2350 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2352 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2354 if (radix < 2 || radix > 36)
2355 valid = 0;
2356 else
2358 char *p = buf;
2359 int c, digit;
2361 c = READCHAR;
2362 if (c == '-' || c == '+')
2364 *p++ = c;
2365 c = READCHAR;
2368 if (c == '0')
2370 *p++ = c;
2371 valid = 1;
2373 /* Ignore redundant leading zeros, so the buffer doesn't
2374 fill up with them. */
2376 c = READCHAR;
2377 while (c == '0');
2380 while ((digit = digit_to_number (c, radix)) >= -1)
2382 if (digit == -1)
2383 valid = 0;
2384 if (valid < 0)
2385 valid = 1;
2387 if (p < buf + sizeof buf - 1)
2388 *p++ = c;
2389 else
2390 valid = 0;
2392 c = READCHAR;
2395 UNREAD (c);
2396 *p = '\0';
2399 if (! valid)
2401 sprintf (buf, "integer, radix %"pI"d", radix);
2402 invalid_syntax (buf);
2405 return string_to_number (buf, radix, 0);
2409 /* If the next token is ')' or ']' or '.', we store that character
2410 in *PCH and the return value is not interesting. Else, we store
2411 zero in *PCH and we read and return one lisp object.
2413 FIRST_IN_LIST is true if this is the first element of a list. */
2415 static Lisp_Object
2416 read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
2418 int c;
2419 bool uninterned_symbol = 0;
2420 bool multibyte;
2422 *pch = 0;
2424 retry:
2426 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2427 if (c < 0)
2428 end_of_file_error ();
2430 switch (c)
2432 case '(':
2433 return read_list (0, readcharfun);
2435 case '[':
2436 return read_vector (readcharfun, 0);
2438 case ')':
2439 case ']':
2441 *pch = c;
2442 return Qnil;
2445 case '#':
2446 c = READCHAR;
2447 if (c == 's')
2449 c = READCHAR;
2450 if (c == '(')
2452 /* Accept extended format for hashtables (extensible to
2453 other types), e.g.
2454 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2455 Lisp_Object tmp = read_list (0, readcharfun);
2456 Lisp_Object head = CAR_SAFE (tmp);
2457 Lisp_Object data = Qnil;
2458 Lisp_Object val = Qnil;
2459 /* The size is 2 * number of allowed keywords to
2460 make-hash-table. */
2461 Lisp_Object params[10];
2462 Lisp_Object ht;
2463 Lisp_Object key = Qnil;
2464 int param_count = 0;
2466 if (!EQ (head, Qhash_table))
2467 error ("Invalid extended read marker at head of #s list "
2468 "(only hash-table allowed)");
2470 tmp = CDR_SAFE (tmp);
2472 /* This is repetitive but fast and simple. */
2473 params[param_count] = QCsize;
2474 params[param_count + 1] = Fplist_get (tmp, Qsize);
2475 if (!NILP (params[param_count + 1]))
2476 param_count += 2;
2478 params[param_count] = QCtest;
2479 params[param_count + 1] = Fplist_get (tmp, Qtest);
2480 if (!NILP (params[param_count + 1]))
2481 param_count += 2;
2483 params[param_count] = QCweakness;
2484 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2485 if (!NILP (params[param_count + 1]))
2486 param_count += 2;
2488 params[param_count] = QCrehash_size;
2489 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2490 if (!NILP (params[param_count + 1]))
2491 param_count += 2;
2493 params[param_count] = QCrehash_threshold;
2494 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2495 if (!NILP (params[param_count + 1]))
2496 param_count += 2;
2498 /* This is the hashtable data. */
2499 data = Fplist_get (tmp, Qdata);
2501 /* Now use params to make a new hashtable and fill it. */
2502 ht = Fmake_hash_table (param_count, params);
2504 while (CONSP (data))
2506 key = XCAR (data);
2507 data = XCDR (data);
2508 if (!CONSP (data))
2509 error ("Odd number of elements in hashtable data");
2510 val = XCAR (data);
2511 data = XCDR (data);
2512 Fputhash (key, val, ht);
2515 return ht;
2517 UNREAD (c);
2518 invalid_syntax ("#");
2520 if (c == '^')
2522 c = READCHAR;
2523 if (c == '[')
2525 Lisp_Object tmp;
2526 tmp = read_vector (readcharfun, 0);
2527 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2528 error ("Invalid size char-table");
2529 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2530 return tmp;
2532 else if (c == '^')
2534 c = READCHAR;
2535 if (c == '[')
2537 Lisp_Object tmp;
2538 int depth;
2539 ptrdiff_t size;
2541 tmp = read_vector (readcharfun, 0);
2542 size = ASIZE (tmp);
2543 if (size == 0)
2544 error ("Invalid size char-table");
2545 if (! RANGED_INTEGERP (1, AREF (tmp, 0), 3))
2546 error ("Invalid depth in char-table");
2547 depth = XINT (AREF (tmp, 0));
2548 if (chartab_size[depth] != size - 2)
2549 error ("Invalid size char-table");
2550 XSETPVECTYPE (XVECTOR (tmp), PVEC_SUB_CHAR_TABLE);
2551 return tmp;
2553 invalid_syntax ("#^^");
2555 invalid_syntax ("#^");
2557 if (c == '&')
2559 Lisp_Object length;
2560 length = read1 (readcharfun, pch, first_in_list);
2561 c = READCHAR;
2562 if (c == '"')
2564 Lisp_Object tmp, val;
2565 EMACS_INT size_in_chars
2566 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2567 / BOOL_VECTOR_BITS_PER_CHAR);
2569 UNREAD (c);
2570 tmp = read1 (readcharfun, pch, first_in_list);
2571 if (STRING_MULTIBYTE (tmp)
2572 || (size_in_chars != SCHARS (tmp)
2573 /* We used to print 1 char too many
2574 when the number of bits was a multiple of 8.
2575 Accept such input in case it came from an old
2576 version. */
2577 && ! (XFASTINT (length)
2578 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2579 invalid_syntax ("#&...");
2581 val = Fmake_bool_vector (length, Qnil);
2582 memcpy (XBOOL_VECTOR (val)->data, SDATA (tmp), size_in_chars);
2583 /* Clear the extraneous bits in the last byte. */
2584 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2585 XBOOL_VECTOR (val)->data[size_in_chars - 1]
2586 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2587 return val;
2589 invalid_syntax ("#&...");
2591 if (c == '[')
2593 /* Accept compiled functions at read-time so that we don't have to
2594 build them using function calls. */
2595 Lisp_Object tmp;
2596 tmp = read_vector (readcharfun, 1);
2597 make_byte_code (XVECTOR (tmp));
2598 return tmp;
2600 if (c == '(')
2602 Lisp_Object tmp;
2603 struct gcpro gcpro1;
2604 int ch;
2606 /* Read the string itself. */
2607 tmp = read1 (readcharfun, &ch, 0);
2608 if (ch != 0 || !STRINGP (tmp))
2609 invalid_syntax ("#");
2610 GCPRO1 (tmp);
2611 /* Read the intervals and their properties. */
2612 while (1)
2614 Lisp_Object beg, end, plist;
2616 beg = read1 (readcharfun, &ch, 0);
2617 end = plist = Qnil;
2618 if (ch == ')')
2619 break;
2620 if (ch == 0)
2621 end = read1 (readcharfun, &ch, 0);
2622 if (ch == 0)
2623 plist = read1 (readcharfun, &ch, 0);
2624 if (ch)
2625 invalid_syntax ("Invalid string property list");
2626 Fset_text_properties (beg, end, plist, tmp);
2628 UNGCPRO;
2629 return tmp;
2632 /* #@NUMBER is used to skip NUMBER following bytes.
2633 That's used in .elc files to skip over doc strings
2634 and function definitions. */
2635 if (c == '@')
2637 enum { extra = 100 };
2638 ptrdiff_t i, nskip = 0, digits = 0;
2640 /* Read a decimal integer. */
2641 while ((c = READCHAR) >= 0
2642 && c >= '0' && c <= '9')
2644 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2645 string_overflow ();
2646 digits++;
2647 nskip *= 10;
2648 nskip += c - '0';
2649 if (digits == 2 && nskip == 0)
2650 { /* We've just seen #@00, which means "skip to end". */
2651 skip_dyn_eof (readcharfun);
2652 return Qnil;
2655 if (nskip > 0)
2656 /* We can't use UNREAD here, because in the code below we side-step
2657 READCHAR. Instead, assume the first char after #@NNN occupies
2658 a single byte, which is the case normally since it's just
2659 a space. */
2660 nskip--;
2661 else
2662 UNREAD (c);
2664 if (load_force_doc_strings
2665 && (FROM_FILE_P (readcharfun)))
2667 /* If we are supposed to force doc strings into core right now,
2668 record the last string that we skipped,
2669 and record where in the file it comes from. */
2671 /* But first exchange saved_doc_string
2672 with prev_saved_doc_string, so we save two strings. */
2674 char *temp = saved_doc_string;
2675 ptrdiff_t temp_size = saved_doc_string_size;
2676 file_offset temp_pos = saved_doc_string_position;
2677 ptrdiff_t temp_len = saved_doc_string_length;
2679 saved_doc_string = prev_saved_doc_string;
2680 saved_doc_string_size = prev_saved_doc_string_size;
2681 saved_doc_string_position = prev_saved_doc_string_position;
2682 saved_doc_string_length = prev_saved_doc_string_length;
2684 prev_saved_doc_string = temp;
2685 prev_saved_doc_string_size = temp_size;
2686 prev_saved_doc_string_position = temp_pos;
2687 prev_saved_doc_string_length = temp_len;
2690 if (saved_doc_string_size == 0)
2692 saved_doc_string = xmalloc (nskip + extra);
2693 saved_doc_string_size = nskip + extra;
2695 if (nskip > saved_doc_string_size)
2697 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
2698 saved_doc_string_size = nskip + extra;
2701 saved_doc_string_position = file_tell (instream);
2703 /* Copy that many characters into saved_doc_string. */
2704 block_input ();
2705 for (i = 0; i < nskip && c >= 0; i++)
2706 saved_doc_string[i] = c = getc (instream);
2707 unblock_input ();
2709 saved_doc_string_length = i;
2711 else
2712 /* Skip that many bytes. */
2713 skip_dyn_bytes (readcharfun, nskip);
2715 goto retry;
2717 if (c == '!')
2719 /* #! appears at the beginning of an executable file.
2720 Skip the first line. */
2721 while (c != '\n' && c >= 0)
2722 c = READCHAR;
2723 goto retry;
2725 if (c == '$')
2726 return Vload_file_name;
2727 if (c == '\'')
2728 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2729 /* #:foo is the uninterned symbol named foo. */
2730 if (c == ':')
2732 uninterned_symbol = 1;
2733 c = READCHAR;
2734 if (!(c > 040
2735 && c != 0xa0 /* NBSP */
2736 && (c >= 0200
2737 || strchr ("\"';()[]#`,", c) == NULL)))
2739 /* No symbol character follows, this is the empty
2740 symbol. */
2741 UNREAD (c);
2742 return Fmake_symbol (empty_unibyte_string);
2744 goto read_symbol;
2746 /* ## is the empty symbol. */
2747 if (c == '#')
2748 return Fintern (empty_unibyte_string, Qnil);
2749 /* Reader forms that can reuse previously read objects. */
2750 if (c >= '0' && c <= '9')
2752 EMACS_INT n = 0;
2753 Lisp_Object tem;
2755 /* Read a non-negative integer. */
2756 while (c >= '0' && c <= '9')
2758 if (MOST_POSITIVE_FIXNUM / 10 < n
2759 || MOST_POSITIVE_FIXNUM < n * 10 + c - '0')
2760 n = MOST_POSITIVE_FIXNUM + 1;
2761 else
2762 n = n * 10 + c - '0';
2763 c = READCHAR;
2766 if (n <= MOST_POSITIVE_FIXNUM)
2768 if (c == 'r' || c == 'R')
2769 return read_integer (readcharfun, n);
2771 if (! NILP (Vread_circle))
2773 /* #n=object returns object, but associates it with
2774 n for #n#. */
2775 if (c == '=')
2777 /* Make a placeholder for #n# to use temporarily. */
2778 Lisp_Object placeholder;
2779 Lisp_Object cell;
2781 placeholder = Fcons (Qnil, Qnil);
2782 cell = Fcons (make_number (n), placeholder);
2783 read_objects = Fcons (cell, read_objects);
2785 /* Read the object itself. */
2786 tem = read0 (readcharfun);
2788 /* Now put it everywhere the placeholder was... */
2789 substitute_object_in_subtree (tem, placeholder);
2791 /* ...and #n# will use the real value from now on. */
2792 Fsetcdr (cell, tem);
2794 return tem;
2797 /* #n# returns a previously read object. */
2798 if (c == '#')
2800 tem = Fassq (make_number (n), read_objects);
2801 if (CONSP (tem))
2802 return XCDR (tem);
2806 /* Fall through to error message. */
2808 else if (c == 'x' || c == 'X')
2809 return read_integer (readcharfun, 16);
2810 else if (c == 'o' || c == 'O')
2811 return read_integer (readcharfun, 8);
2812 else if (c == 'b' || c == 'B')
2813 return read_integer (readcharfun, 2);
2815 UNREAD (c);
2816 invalid_syntax ("#");
2818 case ';':
2819 while ((c = READCHAR) >= 0 && c != '\n');
2820 goto retry;
2822 case '\'':
2824 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2827 case '`':
2829 int next_char = READCHAR;
2830 UNREAD (next_char);
2831 /* Transition from old-style to new-style:
2832 If we see "(`" it used to mean old-style, which usually works
2833 fine because ` should almost never appear in such a position
2834 for new-style. But occasionally we need "(`" to mean new
2835 style, so we try to distinguish the two by the fact that we
2836 can either write "( `foo" or "(` foo", where the first
2837 intends to use new-style whereas the second intends to use
2838 old-style. For Emacs-25, we should completely remove this
2839 first_in_list exception (old-style can still be obtained via
2840 "(\`" anyway). */
2841 if (!new_backquote_flag && first_in_list && next_char == ' ')
2843 Vold_style_backquotes = Qt;
2844 goto default_label;
2846 else
2848 Lisp_Object value;
2849 bool saved_new_backquote_flag = new_backquote_flag;
2851 new_backquote_flag = 1;
2852 value = read0 (readcharfun);
2853 new_backquote_flag = saved_new_backquote_flag;
2855 return Fcons (Qbackquote, Fcons (value, Qnil));
2858 case ',':
2860 int next_char = READCHAR;
2861 UNREAD (next_char);
2862 /* Transition from old-style to new-style:
2863 It used to be impossible to have a new-style , other than within
2864 a new-style `. This is sufficient when ` and , are used in the
2865 normal way, but ` and , can also appear in args to macros that
2866 will not interpret them in the usual way, in which case , may be
2867 used without any ` anywhere near.
2868 So we now use the same heuristic as for backquote: old-style
2869 unquotes are only recognized when first on a list, and when
2870 followed by a space.
2871 Because it's more difficult to peek 2 chars ahead, a new-style
2872 ,@ can still not be used outside of a `, unless it's in the middle
2873 of a list. */
2874 if (new_backquote_flag
2875 || !first_in_list
2876 || (next_char != ' ' && next_char != '@'))
2878 Lisp_Object comma_type = Qnil;
2879 Lisp_Object value;
2880 int ch = READCHAR;
2882 if (ch == '@')
2883 comma_type = Qcomma_at;
2884 else if (ch == '.')
2885 comma_type = Qcomma_dot;
2886 else
2888 if (ch >= 0) UNREAD (ch);
2889 comma_type = Qcomma;
2892 value = read0 (readcharfun);
2893 return Fcons (comma_type, Fcons (value, Qnil));
2895 else
2897 Vold_style_backquotes = Qt;
2898 goto default_label;
2901 case '?':
2903 int modifiers;
2904 int next_char;
2905 bool ok;
2907 c = READCHAR;
2908 if (c < 0)
2909 end_of_file_error ();
2911 /* Accept `single space' syntax like (list ? x) where the
2912 whitespace character is SPC or TAB.
2913 Other literal whitespace like NL, CR, and FF are not accepted,
2914 as there are well-established escape sequences for these. */
2915 if (c == ' ' || c == '\t')
2916 return make_number (c);
2918 if (c == '\\')
2919 c = read_escape (readcharfun, 0);
2920 modifiers = c & CHAR_MODIFIER_MASK;
2921 c &= ~CHAR_MODIFIER_MASK;
2922 if (CHAR_BYTE8_P (c))
2923 c = CHAR_TO_BYTE8 (c);
2924 c |= modifiers;
2926 next_char = READCHAR;
2927 ok = (next_char <= 040
2928 || (next_char < 0200
2929 && strchr ("\"';()[]#?`,.", next_char) != NULL));
2930 UNREAD (next_char);
2931 if (ok)
2932 return make_number (c);
2934 invalid_syntax ("?");
2937 case '"':
2939 char *p = read_buffer;
2940 char *end = read_buffer + read_buffer_size;
2941 int ch;
2942 /* True if we saw an escape sequence specifying
2943 a multibyte character. */
2944 bool force_multibyte = 0;
2945 /* True if we saw an escape sequence specifying
2946 a single-byte character. */
2947 bool force_singlebyte = 0;
2948 bool cancel = 0;
2949 ptrdiff_t nchars = 0;
2951 while ((ch = READCHAR) >= 0
2952 && ch != '\"')
2954 if (end - p < MAX_MULTIBYTE_LENGTH)
2956 ptrdiff_t offset = p - read_buffer;
2957 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
2958 memory_full (SIZE_MAX);
2959 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
2960 read_buffer_size *= 2;
2961 p = read_buffer + offset;
2962 end = read_buffer + read_buffer_size;
2965 if (ch == '\\')
2967 int modifiers;
2969 ch = read_escape (readcharfun, 1);
2971 /* CH is -1 if \ newline has just been seen. */
2972 if (ch == -1)
2974 if (p == read_buffer)
2975 cancel = 1;
2976 continue;
2979 modifiers = ch & CHAR_MODIFIER_MASK;
2980 ch = ch & ~CHAR_MODIFIER_MASK;
2982 if (CHAR_BYTE8_P (ch))
2983 force_singlebyte = 1;
2984 else if (! ASCII_CHAR_P (ch))
2985 force_multibyte = 1;
2986 else /* I.e. ASCII_CHAR_P (ch). */
2988 /* Allow `\C- ' and `\C-?'. */
2989 if (modifiers == CHAR_CTL)
2991 if (ch == ' ')
2992 ch = 0, modifiers = 0;
2993 else if (ch == '?')
2994 ch = 127, modifiers = 0;
2996 if (modifiers & CHAR_SHIFT)
2998 /* Shift modifier is valid only with [A-Za-z]. */
2999 if (ch >= 'A' && ch <= 'Z')
3000 modifiers &= ~CHAR_SHIFT;
3001 else if (ch >= 'a' && ch <= 'z')
3002 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
3005 if (modifiers & CHAR_META)
3007 /* Move the meta bit to the right place for a
3008 string. */
3009 modifiers &= ~CHAR_META;
3010 ch = BYTE8_TO_CHAR (ch | 0x80);
3011 force_singlebyte = 1;
3015 /* Any modifiers remaining are invalid. */
3016 if (modifiers)
3017 error ("Invalid modifier in string");
3018 p += CHAR_STRING (ch, (unsigned char *) p);
3020 else
3022 p += CHAR_STRING (ch, (unsigned char *) p);
3023 if (CHAR_BYTE8_P (ch))
3024 force_singlebyte = 1;
3025 else if (! ASCII_CHAR_P (ch))
3026 force_multibyte = 1;
3028 nchars++;
3031 if (ch < 0)
3032 end_of_file_error ();
3034 /* If purifying, and string starts with \ newline,
3035 return zero instead. This is for doc strings
3036 that we are really going to find in etc/DOC.nn.nn. */
3037 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
3038 return make_number (0);
3040 if (! force_multibyte && force_singlebyte)
3042 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3043 forms. Convert it to unibyte. */
3044 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3045 p - read_buffer);
3046 p = read_buffer + nchars;
3049 return make_specified_string (read_buffer, nchars, p - read_buffer,
3050 (force_multibyte
3051 || (p - read_buffer != nchars)));
3054 case '.':
3056 int next_char = READCHAR;
3057 UNREAD (next_char);
3059 if (next_char <= 040
3060 || (next_char < 0200
3061 && strchr ("\"';([#?`,", next_char) != NULL))
3063 *pch = c;
3064 return Qnil;
3067 /* Otherwise, we fall through! Note that the atom-reading loop
3068 below will now loop at least once, assuring that we will not
3069 try to UNREAD two characters in a row. */
3071 default:
3072 default_label:
3073 if (c <= 040) goto retry;
3074 if (c == 0xa0) /* NBSP */
3075 goto retry;
3077 read_symbol:
3079 char *p = read_buffer;
3080 bool quoted = 0;
3081 EMACS_INT start_position = readchar_count - 1;
3084 char *end = read_buffer + read_buffer_size;
3088 if (end - p < MAX_MULTIBYTE_LENGTH)
3090 ptrdiff_t offset = p - read_buffer;
3091 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3092 memory_full (SIZE_MAX);
3093 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3094 read_buffer_size *= 2;
3095 p = read_buffer + offset;
3096 end = read_buffer + read_buffer_size;
3099 if (c == '\\')
3101 c = READCHAR;
3102 if (c == -1)
3103 end_of_file_error ();
3104 quoted = 1;
3107 if (multibyte)
3108 p += CHAR_STRING (c, (unsigned char *) p);
3109 else
3110 *p++ = c;
3111 c = READCHAR;
3113 while (c > 040
3114 && c != 0xa0 /* NBSP */
3115 && (c >= 0200
3116 || strchr ("\"';()[]#`,", c) == NULL));
3118 if (p == end)
3120 ptrdiff_t offset = p - read_buffer;
3121 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3122 memory_full (SIZE_MAX);
3123 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3124 read_buffer_size *= 2;
3125 p = read_buffer + offset;
3126 end = read_buffer + read_buffer_size;
3128 *p = 0;
3129 UNREAD (c);
3132 if (!quoted && !uninterned_symbol)
3134 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3135 if (! NILP (result))
3136 return result;
3139 Lisp_Object name, result;
3140 ptrdiff_t nbytes = p - read_buffer;
3141 ptrdiff_t nchars
3142 = (multibyte
3143 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3144 nbytes)
3145 : nbytes);
3147 name = ((uninterned_symbol && ! NILP (Vpurify_flag)
3148 ? make_pure_string : make_specified_string)
3149 (read_buffer, nchars, nbytes, multibyte));
3150 result = (uninterned_symbol ? Fmake_symbol (name)
3151 : Fintern (name, Qnil));
3153 if (EQ (Vread_with_symbol_positions, Qt)
3154 || EQ (Vread_with_symbol_positions, readcharfun))
3155 Vread_symbol_positions_list
3156 = Fcons (Fcons (result, make_number (start_position)),
3157 Vread_symbol_positions_list);
3158 return result;
3165 /* List of nodes we've seen during substitute_object_in_subtree. */
3166 static Lisp_Object seen_list;
3168 static void
3169 substitute_object_in_subtree (Lisp_Object object, Lisp_Object placeholder)
3171 Lisp_Object check_object;
3173 /* We haven't seen any objects when we start. */
3174 seen_list = Qnil;
3176 /* Make all the substitutions. */
3177 check_object
3178 = substitute_object_recurse (object, placeholder, object);
3180 /* Clear seen_list because we're done with it. */
3181 seen_list = Qnil;
3183 /* The returned object here is expected to always eq the
3184 original. */
3185 if (!EQ (check_object, object))
3186 error ("Unexpected mutation error in reader");
3189 /* Feval doesn't get called from here, so no gc protection is needed. */
3190 #define SUBSTITUTE(get_val, set_val) \
3191 do { \
3192 Lisp_Object old_value = get_val; \
3193 Lisp_Object true_value \
3194 = substitute_object_recurse (object, placeholder, \
3195 old_value); \
3197 if (!EQ (old_value, true_value)) \
3199 set_val; \
3201 } while (0)
3203 static Lisp_Object
3204 substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Object subtree)
3206 /* If we find the placeholder, return the target object. */
3207 if (EQ (placeholder, subtree))
3208 return object;
3210 /* If we've been to this node before, don't explore it again. */
3211 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3212 return subtree;
3214 /* If this node can be the entry point to a cycle, remember that
3215 we've seen it. It can only be such an entry point if it was made
3216 by #n=, which means that we can find it as a value in
3217 read_objects. */
3218 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3219 seen_list = Fcons (subtree, seen_list);
3221 /* Recurse according to subtree's type.
3222 Every branch must return a Lisp_Object. */
3223 switch (XTYPE (subtree))
3225 case Lisp_Vectorlike:
3227 ptrdiff_t i, length = 0;
3228 if (BOOL_VECTOR_P (subtree))
3229 return subtree; /* No sub-objects anyway. */
3230 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3231 || COMPILEDP (subtree))
3232 length = ASIZE (subtree) & PSEUDOVECTOR_SIZE_MASK;
3233 else if (VECTORP (subtree))
3234 length = ASIZE (subtree);
3235 else
3236 /* An unknown pseudovector may contain non-Lisp fields, so we
3237 can't just blindly traverse all its fields. We used to call
3238 `Flength' which signaled `sequencep', so I just preserved this
3239 behavior. */
3240 wrong_type_argument (Qsequencep, subtree);
3242 for (i = 0; i < length; i++)
3243 SUBSTITUTE (AREF (subtree, i),
3244 ASET (subtree, i, true_value));
3245 return subtree;
3248 case Lisp_Cons:
3250 SUBSTITUTE (XCAR (subtree),
3251 XSETCAR (subtree, true_value));
3252 SUBSTITUTE (XCDR (subtree),
3253 XSETCDR (subtree, true_value));
3254 return subtree;
3257 case Lisp_String:
3259 /* Check for text properties in each interval.
3260 substitute_in_interval contains part of the logic. */
3262 INTERVAL root_interval = string_intervals (subtree);
3263 Lisp_Object arg = Fcons (object, placeholder);
3265 traverse_intervals_noorder (root_interval,
3266 &substitute_in_interval, arg);
3268 return subtree;
3271 /* Other types don't recurse any further. */
3272 default:
3273 return subtree;
3277 /* Helper function for substitute_object_recurse. */
3278 static void
3279 substitute_in_interval (INTERVAL interval, Lisp_Object arg)
3281 Lisp_Object object = Fcar (arg);
3282 Lisp_Object placeholder = Fcdr (arg);
3284 SUBSTITUTE (interval->plist, set_interval_plist (interval, true_value));
3288 #define LEAD_INT 1
3289 #define DOT_CHAR 2
3290 #define TRAIL_INT 4
3291 #define E_EXP 16
3294 /* Convert STRING to a number, assuming base BASE. Return a fixnum if CP has
3295 integer syntax and fits in a fixnum, else return the nearest float if CP has
3296 either floating point or integer syntax and BASE is 10, else return nil. If
3297 IGNORE_TRAILING, consider just the longest prefix of CP that has
3298 valid floating point syntax. Signal an overflow if BASE is not 10 and the
3299 number has integer syntax but does not fit. */
3301 Lisp_Object
3302 string_to_number (char const *string, int base, bool ignore_trailing)
3304 int state;
3305 char const *cp = string;
3306 int leading_digit;
3307 bool float_syntax = 0;
3308 double value = 0;
3310 /* Compute NaN and infinities using a variable, to cope with compilers that
3311 think they are smarter than we are. */
3312 double zero = 0;
3314 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3315 IEEE floating point hosts, and works around a formerly-common bug where
3316 atof ("-0.0") drops the sign. */
3317 bool negative = *cp == '-';
3319 bool signedp = negative || *cp == '+';
3320 cp += signedp;
3322 state = 0;
3324 leading_digit = digit_to_number (*cp, base);
3325 if (leading_digit >= 0)
3327 state |= LEAD_INT;
3329 ++cp;
3330 while (digit_to_number (*cp, base) >= 0);
3332 if (*cp == '.')
3334 state |= DOT_CHAR;
3335 cp++;
3338 if (base == 10)
3340 if ('0' <= *cp && *cp <= '9')
3342 state |= TRAIL_INT;
3344 cp++;
3345 while ('0' <= *cp && *cp <= '9');
3347 if (*cp == 'e' || *cp == 'E')
3349 char const *ecp = cp;
3350 cp++;
3351 if (*cp == '+' || *cp == '-')
3352 cp++;
3353 if ('0' <= *cp && *cp <= '9')
3355 state |= E_EXP;
3357 cp++;
3358 while ('0' <= *cp && *cp <= '9');
3360 else if (cp[-1] == '+'
3361 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3363 state |= E_EXP;
3364 cp += 3;
3365 value = 1.0 / zero;
3367 else if (cp[-1] == '+'
3368 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3370 state |= E_EXP;
3371 cp += 3;
3372 value = zero / zero;
3374 /* If that made a "negative" NaN, negate it. */
3376 int i;
3377 union { double d; char c[sizeof (double)]; }
3378 u_data, u_minus_zero;
3379 u_data.d = value;
3380 u_minus_zero.d = -0.0;
3381 for (i = 0; i < sizeof (double); i++)
3382 if (u_data.c[i] & u_minus_zero.c[i])
3384 value = -value;
3385 break;
3388 /* Now VALUE is a positive NaN. */
3390 else
3391 cp = ecp;
3394 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3395 || state == (LEAD_INT|E_EXP));
3398 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3399 any prefix that matches. Otherwise, the entire string must match. */
3400 if (! (ignore_trailing
3401 ? ((state & LEAD_INT) != 0 || float_syntax)
3402 : (!*cp && ((state & ~DOT_CHAR) == LEAD_INT || float_syntax))))
3403 return Qnil;
3405 /* If the number uses integer and not float syntax, and is in C-language
3406 range, use its value, preferably as a fixnum. */
3407 if (leading_digit >= 0 && ! float_syntax)
3409 uintmax_t n;
3411 /* Fast special case for single-digit integers. This also avoids a
3412 glitch when BASE is 16 and IGNORE_TRAILING, because in that
3413 case some versions of strtoumax accept numbers like "0x1" that Emacs
3414 does not allow. */
3415 if (digit_to_number (string[signedp + 1], base) < 0)
3416 return make_number (negative ? -leading_digit : leading_digit);
3418 errno = 0;
3419 n = strtoumax (string + signedp, NULL, base);
3420 if (errno == ERANGE)
3422 /* Unfortunately there's no simple and accurate way to convert
3423 non-base-10 numbers that are out of C-language range. */
3424 if (base != 10)
3425 xsignal1 (Qoverflow_error, build_string (string));
3427 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3429 EMACS_INT signed_n = n;
3430 return make_number (negative ? -signed_n : signed_n);
3432 else
3433 value = n;
3436 /* Either the number uses float syntax, or it does not fit into a fixnum.
3437 Convert it from string to floating point, unless the value is already
3438 known because it is an infinity, a NAN, or its absolute value fits in
3439 uintmax_t. */
3440 if (! value)
3441 value = atof (string + signedp);
3443 return make_float (negative ? -value : value);
3447 static Lisp_Object
3448 read_vector (Lisp_Object readcharfun, bool bytecodeflag)
3450 ptrdiff_t i, size;
3451 Lisp_Object *ptr;
3452 Lisp_Object tem, item, vector;
3453 struct Lisp_Cons *otem;
3454 Lisp_Object len;
3456 tem = read_list (1, readcharfun);
3457 len = Flength (tem);
3458 vector = Fmake_vector (len, Qnil);
3460 size = ASIZE (vector);
3461 ptr = XVECTOR (vector)->contents;
3462 for (i = 0; i < size; i++)
3464 item = Fcar (tem);
3465 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3466 bytecode object, the docstring containing the bytecode and
3467 constants values must be treated as unibyte and passed to
3468 Fread, to get the actual bytecode string and constants vector. */
3469 if (bytecodeflag && load_force_doc_strings)
3471 if (i == COMPILED_BYTECODE)
3473 if (!STRINGP (item))
3474 error ("Invalid byte code");
3476 /* Delay handling the bytecode slot until we know whether
3477 it is lazily-loaded (we can tell by whether the
3478 constants slot is nil). */
3479 ASET (vector, COMPILED_CONSTANTS, item);
3480 item = Qnil;
3482 else if (i == COMPILED_CONSTANTS)
3484 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3486 if (NILP (item))
3488 /* Coerce string to unibyte (like string-as-unibyte,
3489 but without generating extra garbage and
3490 guaranteeing no change in the contents). */
3491 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3492 STRING_SET_UNIBYTE (bytestr);
3494 item = Fread (Fcons (bytestr, readcharfun));
3495 if (!CONSP (item))
3496 error ("Invalid byte code");
3498 otem = XCONS (item);
3499 bytestr = XCAR (item);
3500 item = XCDR (item);
3501 free_cons (otem);
3504 /* Now handle the bytecode slot. */
3505 ASET (vector, COMPILED_BYTECODE, bytestr);
3507 else if (i == COMPILED_DOC_STRING
3508 && STRINGP (item)
3509 && ! STRING_MULTIBYTE (item))
3511 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3512 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3513 else
3514 item = Fstring_as_multibyte (item);
3517 ASET (vector, i, item);
3518 otem = XCONS (tem);
3519 tem = Fcdr (tem);
3520 free_cons (otem);
3522 return vector;
3525 /* FLAG means check for ] to terminate rather than ) and . */
3527 static Lisp_Object
3528 read_list (bool flag, Lisp_Object readcharfun)
3530 Lisp_Object val, tail;
3531 Lisp_Object elt, tem;
3532 struct gcpro gcpro1, gcpro2;
3533 /* 0 is the normal case.
3534 1 means this list is a doc reference; replace it with the number 0.
3535 2 means this list is a doc reference; replace it with the doc string. */
3536 int doc_reference = 0;
3538 /* Initialize this to 1 if we are reading a list. */
3539 bool first_in_list = flag <= 0;
3541 val = Qnil;
3542 tail = Qnil;
3544 while (1)
3546 int ch;
3547 GCPRO2 (val, tail);
3548 elt = read1 (readcharfun, &ch, first_in_list);
3549 UNGCPRO;
3551 first_in_list = 0;
3553 /* While building, if the list starts with #$, treat it specially. */
3554 if (EQ (elt, Vload_file_name)
3555 && ! NILP (elt)
3556 && !NILP (Vpurify_flag))
3558 if (NILP (Vdoc_file_name))
3559 /* We have not yet called Snarf-documentation, so assume
3560 this file is described in the DOC-MM.NN file
3561 and Snarf-documentation will fill in the right value later.
3562 For now, replace the whole list with 0. */
3563 doc_reference = 1;
3564 else
3565 /* We have already called Snarf-documentation, so make a relative
3566 file name for this file, so it can be found properly
3567 in the installed Lisp directory.
3568 We don't use Fexpand_file_name because that would make
3569 the directory absolute now. */
3570 elt = concat2 (build_string ("../lisp/"),
3571 Ffile_name_nondirectory (elt));
3573 else if (EQ (elt, Vload_file_name)
3574 && ! NILP (elt)
3575 && load_force_doc_strings)
3576 doc_reference = 2;
3578 if (ch)
3580 if (flag > 0)
3582 if (ch == ']')
3583 return val;
3584 invalid_syntax (") or . in a vector");
3586 if (ch == ')')
3587 return val;
3588 if (ch == '.')
3590 GCPRO2 (val, tail);
3591 if (!NILP (tail))
3592 XSETCDR (tail, read0 (readcharfun));
3593 else
3594 val = read0 (readcharfun);
3595 read1 (readcharfun, &ch, 0);
3596 UNGCPRO;
3597 if (ch == ')')
3599 if (doc_reference == 1)
3600 return make_number (0);
3601 if (doc_reference == 2 && INTEGERP (XCDR (val)))
3603 char *saved = NULL;
3604 file_offset saved_position;
3605 /* Get a doc string from the file we are loading.
3606 If it's in saved_doc_string, get it from there.
3608 Here, we don't know if the string is a
3609 bytecode string or a doc string. As a
3610 bytecode string must be unibyte, we always
3611 return a unibyte string. If it is actually a
3612 doc string, caller must make it
3613 multibyte. */
3615 /* Position is negative for user variables. */
3616 EMACS_INT pos = eabs (XINT (XCDR (val)));
3617 if (pos >= saved_doc_string_position
3618 && pos < (saved_doc_string_position
3619 + saved_doc_string_length))
3621 saved = saved_doc_string;
3622 saved_position = saved_doc_string_position;
3624 /* Look in prev_saved_doc_string the same way. */
3625 else if (pos >= prev_saved_doc_string_position
3626 && pos < (prev_saved_doc_string_position
3627 + prev_saved_doc_string_length))
3629 saved = prev_saved_doc_string;
3630 saved_position = prev_saved_doc_string_position;
3632 if (saved)
3634 ptrdiff_t start = pos - saved_position;
3635 ptrdiff_t from, to;
3637 /* Process quoting with ^A,
3638 and find the end of the string,
3639 which is marked with ^_ (037). */
3640 for (from = start, to = start;
3641 saved[from] != 037;)
3643 int c = saved[from++];
3644 if (c == 1)
3646 c = saved[from++];
3647 saved[to++] = (c == 1 ? c
3648 : c == '0' ? 0
3649 : c == '_' ? 037
3650 : c);
3652 else
3653 saved[to++] = c;
3656 return make_unibyte_string (saved + start,
3657 to - start);
3659 else
3660 return get_doc_string (val, 1, 0);
3663 return val;
3665 invalid_syntax (". in wrong context");
3667 invalid_syntax ("] in a list");
3669 tem = Fcons (elt, Qnil);
3670 if (!NILP (tail))
3671 XSETCDR (tail, tem);
3672 else
3673 val = tem;
3674 tail = tem;
3678 static Lisp_Object initial_obarray;
3680 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
3682 static size_t oblookup_last_bucket_number;
3684 /* Get an error if OBARRAY is not an obarray.
3685 If it is one, return it. */
3687 Lisp_Object
3688 check_obarray (Lisp_Object obarray)
3690 if (!VECTORP (obarray) || ASIZE (obarray) == 0)
3692 /* If Vobarray is now invalid, force it to be valid. */
3693 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3694 wrong_type_argument (Qvectorp, obarray);
3696 return obarray;
3699 /* Intern the C string STR: return a symbol with that name,
3700 interned in the current obarray. */
3702 Lisp_Object
3703 intern_1 (const char *str, ptrdiff_t len)
3705 Lisp_Object obarray = check_obarray (Vobarray);
3706 Lisp_Object tem = oblookup (obarray, str, len, len);
3708 return SYMBOLP (tem) ? tem : Fintern (make_string (str, len), obarray);
3711 Lisp_Object
3712 intern_c_string_1 (const char *str, ptrdiff_t len)
3714 Lisp_Object obarray = check_obarray (Vobarray);
3715 Lisp_Object tem = oblookup (obarray, str, len, len);
3717 if (SYMBOLP (tem))
3718 return tem;
3720 if (NILP (Vpurify_flag))
3721 /* Creating a non-pure string from a string literal not
3722 implemented yet. We could just use make_string here and live
3723 with the extra copy. */
3724 emacs_abort ();
3726 return Fintern (make_pure_c_string (str, len), obarray);
3729 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3730 doc: /* Return the canonical symbol whose name is STRING.
3731 If there is none, one is created by this function and returned.
3732 A second optional argument specifies the obarray to use;
3733 it defaults to the value of `obarray'. */)
3734 (Lisp_Object string, Lisp_Object obarray)
3736 register Lisp_Object tem, sym, *ptr;
3738 if (NILP (obarray)) obarray = Vobarray;
3739 obarray = check_obarray (obarray);
3741 CHECK_STRING (string);
3743 tem = oblookup (obarray, SSDATA (string),
3744 SCHARS (string),
3745 SBYTES (string));
3746 if (!INTEGERP (tem))
3747 return tem;
3749 if (!NILP (Vpurify_flag))
3750 string = Fpurecopy (string);
3751 sym = Fmake_symbol (string);
3753 if (EQ (obarray, initial_obarray))
3754 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3755 else
3756 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
3758 if ((SREF (string, 0) == ':')
3759 && EQ (obarray, initial_obarray))
3761 XSYMBOL (sym)->constant = 1;
3762 XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
3763 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
3766 ptr = aref_addr (obarray, XINT(tem));
3767 if (SYMBOLP (*ptr))
3768 set_symbol_next (sym, XSYMBOL (*ptr));
3769 else
3770 set_symbol_next (sym, NULL);
3771 *ptr = sym;
3772 return sym;
3775 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3776 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3777 NAME may be a string or a symbol. If it is a symbol, that exact
3778 symbol is searched for.
3779 A second optional argument specifies the obarray to use;
3780 it defaults to the value of `obarray'. */)
3781 (Lisp_Object name, Lisp_Object obarray)
3783 register Lisp_Object tem, string;
3785 if (NILP (obarray)) obarray = Vobarray;
3786 obarray = check_obarray (obarray);
3788 if (!SYMBOLP (name))
3790 CHECK_STRING (name);
3791 string = name;
3793 else
3794 string = SYMBOL_NAME (name);
3796 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3797 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3798 return Qnil;
3799 else
3800 return tem;
3803 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3804 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3805 The value is t if a symbol was found and deleted, nil otherwise.
3806 NAME may be a string or a symbol. If it is a symbol, that symbol
3807 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3808 OBARRAY defaults to the value of the variable `obarray'. */)
3809 (Lisp_Object name, Lisp_Object obarray)
3811 register Lisp_Object string, tem;
3812 size_t hash;
3814 if (NILP (obarray)) obarray = Vobarray;
3815 obarray = check_obarray (obarray);
3817 if (SYMBOLP (name))
3818 string = SYMBOL_NAME (name);
3819 else
3821 CHECK_STRING (name);
3822 string = name;
3825 tem = oblookup (obarray, SSDATA (string),
3826 SCHARS (string),
3827 SBYTES (string));
3828 if (INTEGERP (tem))
3829 return Qnil;
3830 /* If arg was a symbol, don't delete anything but that symbol itself. */
3831 if (SYMBOLP (name) && !EQ (name, tem))
3832 return Qnil;
3834 /* There are plenty of other symbols which will screw up the Emacs
3835 session if we unintern them, as well as even more ways to use
3836 `setq' or `fset' or whatnot to make the Emacs session
3837 unusable. Let's not go down this silly road. --Stef */
3838 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
3839 error ("Attempt to unintern t or nil"); */
3841 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3843 hash = oblookup_last_bucket_number;
3845 if (EQ (AREF (obarray, hash), tem))
3847 if (XSYMBOL (tem)->next)
3849 Lisp_Object sym;
3850 XSETSYMBOL (sym, XSYMBOL (tem)->next);
3851 ASET (obarray, hash, sym);
3853 else
3854 ASET (obarray, hash, make_number (0));
3856 else
3858 Lisp_Object tail, following;
3860 for (tail = AREF (obarray, hash);
3861 XSYMBOL (tail)->next;
3862 tail = following)
3864 XSETSYMBOL (following, XSYMBOL (tail)->next);
3865 if (EQ (following, tem))
3867 set_symbol_next (tail, XSYMBOL (following)->next);
3868 break;
3873 return Qt;
3876 /* Return the symbol in OBARRAY whose names matches the string
3877 of SIZE characters (SIZE_BYTE bytes) at PTR.
3878 If there is no such symbol in OBARRAY, return nil.
3880 Also store the bucket number in oblookup_last_bucket_number. */
3882 Lisp_Object
3883 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
3885 size_t hash;
3886 size_t obsize;
3887 register Lisp_Object tail;
3888 Lisp_Object bucket, tem;
3890 obarray = check_obarray (obarray);
3891 obsize = ASIZE (obarray);
3893 /* This is sometimes needed in the middle of GC. */
3894 obsize &= ~ARRAY_MARK_FLAG;
3895 hash = hash_string (ptr, size_byte) % obsize;
3896 bucket = AREF (obarray, hash);
3897 oblookup_last_bucket_number = hash;
3898 if (EQ (bucket, make_number (0)))
3900 else if (!SYMBOLP (bucket))
3901 error ("Bad data in guts of obarray"); /* Like CADR error message. */
3902 else
3903 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3905 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3906 && SCHARS (SYMBOL_NAME (tail)) == size
3907 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
3908 return tail;
3909 else if (XSYMBOL (tail)->next == 0)
3910 break;
3912 XSETINT (tem, hash);
3913 return tem;
3916 void
3917 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
3919 ptrdiff_t i;
3920 register Lisp_Object tail;
3921 CHECK_VECTOR (obarray);
3922 for (i = ASIZE (obarray) - 1; i >= 0; i--)
3924 tail = AREF (obarray, i);
3925 if (SYMBOLP (tail))
3926 while (1)
3928 (*fn) (tail, arg);
3929 if (XSYMBOL (tail)->next == 0)
3930 break;
3931 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3936 static void
3937 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
3939 call1 (function, sym);
3942 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3943 doc: /* Call FUNCTION on every symbol in OBARRAY.
3944 OBARRAY defaults to the value of `obarray'. */)
3945 (Lisp_Object function, Lisp_Object obarray)
3947 if (NILP (obarray)) obarray = Vobarray;
3948 obarray = check_obarray (obarray);
3950 map_obarray (obarray, mapatoms_1, function);
3951 return Qnil;
3954 #define OBARRAY_SIZE 1511
3956 void
3957 init_obarray (void)
3959 Lisp_Object oblength;
3960 ptrdiff_t size = 100 + MAX_MULTIBYTE_LENGTH;
3962 XSETFASTINT (oblength, OBARRAY_SIZE);
3964 Vobarray = Fmake_vector (oblength, make_number (0));
3965 initial_obarray = Vobarray;
3966 staticpro (&initial_obarray);
3968 Qunbound = Fmake_symbol (build_pure_c_string ("unbound"));
3969 /* Set temporary dummy values to Qnil and Vpurify_flag to satisfy the
3970 NILP (Vpurify_flag) check in intern_c_string. */
3971 Qnil = make_number (-1); Vpurify_flag = make_number (1);
3972 Qnil = intern_c_string ("nil");
3974 /* Fmake_symbol inits fields of new symbols with Qunbound and Qnil,
3975 so those two need to be fixed manually. */
3976 SET_SYMBOL_VAL (XSYMBOL (Qunbound), Qunbound);
3977 set_symbol_function (Qunbound, Qnil);
3978 set_symbol_plist (Qunbound, Qnil);
3979 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
3980 XSYMBOL (Qnil)->constant = 1;
3981 XSYMBOL (Qnil)->declared_special = 1;
3982 set_symbol_plist (Qnil, Qnil);
3983 set_symbol_function (Qnil, Qnil);
3985 Qt = intern_c_string ("t");
3986 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
3987 XSYMBOL (Qnil)->declared_special = 1;
3988 XSYMBOL (Qt)->constant = 1;
3990 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3991 Vpurify_flag = Qt;
3993 DEFSYM (Qvariable_documentation, "variable-documentation");
3995 read_buffer = xmalloc (size);
3996 read_buffer_size = size;
3999 void
4000 defsubr (struct Lisp_Subr *sname)
4002 Lisp_Object sym, tem;
4003 sym = intern_c_string (sname->symbol_name);
4004 XSETPVECTYPE (sname, PVEC_SUBR);
4005 XSETSUBR (tem, sname);
4006 set_symbol_function (sym, tem);
4009 #ifdef NOTDEF /* Use fset in subr.el now! */
4010 void
4011 defalias (struct Lisp_Subr *sname, char *string)
4013 Lisp_Object sym;
4014 sym = intern (string);
4015 XSETSUBR (XSYMBOL (sym)->function, sname);
4017 #endif /* NOTDEF */
4019 /* Define an "integer variable"; a symbol whose value is forwarded to a
4020 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
4021 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4022 void
4023 defvar_int (struct Lisp_Intfwd *i_fwd,
4024 const char *namestring, EMACS_INT *address)
4026 Lisp_Object sym;
4027 sym = intern_c_string (namestring);
4028 i_fwd->type = Lisp_Fwd_Int;
4029 i_fwd->intvar = address;
4030 XSYMBOL (sym)->declared_special = 1;
4031 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4032 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4035 /* Similar but define a variable whose value is t if address contains 1,
4036 nil if address contains 0. */
4037 void
4038 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4039 const char *namestring, bool *address)
4041 Lisp_Object sym;
4042 sym = intern_c_string (namestring);
4043 b_fwd->type = Lisp_Fwd_Bool;
4044 b_fwd->boolvar = address;
4045 XSYMBOL (sym)->declared_special = 1;
4046 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4047 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4048 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4051 /* Similar but define a variable whose value is the Lisp Object stored
4052 at address. Two versions: with and without gc-marking of the C
4053 variable. The nopro version is used when that variable will be
4054 gc-marked for some other reason, since marking the same slot twice
4055 can cause trouble with strings. */
4056 void
4057 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4058 const char *namestring, Lisp_Object *address)
4060 Lisp_Object sym;
4061 sym = intern_c_string (namestring);
4062 o_fwd->type = Lisp_Fwd_Obj;
4063 o_fwd->objvar = address;
4064 XSYMBOL (sym)->declared_special = 1;
4065 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4066 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4069 void
4070 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4071 const char *namestring, Lisp_Object *address)
4073 defvar_lisp_nopro (o_fwd, namestring, address);
4074 staticpro (address);
4077 /* Similar but define a variable whose value is the Lisp Object stored
4078 at a particular offset in the current kboard object. */
4080 void
4081 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4082 const char *namestring, int offset)
4084 Lisp_Object sym;
4085 sym = intern_c_string (namestring);
4086 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4087 ko_fwd->offset = offset;
4088 XSYMBOL (sym)->declared_special = 1;
4089 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4090 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4093 /* Check that the elements of Vload_path exist. */
4095 static void
4096 load_path_check (void)
4098 Lisp_Object path_tail;
4100 /* The only elements that might not exist are those from
4101 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4102 it exists. */
4103 for (path_tail = Vload_path; !NILP (path_tail); path_tail = XCDR (path_tail))
4105 Lisp_Object dirfile;
4106 dirfile = Fcar (path_tail);
4107 if (STRINGP (dirfile))
4109 dirfile = Fdirectory_file_name (dirfile);
4110 if (! file_accessible_directory_p (SSDATA (dirfile)))
4111 dir_warning ("Lisp directory", XCAR (path_tail));
4116 /* Record the value of load-path used at the start of dumping
4117 so we can see if the site changed it later during dumping. */
4118 static Lisp_Object dump_path;
4120 /* Compute the default Vload_path, with the following logic:
4121 If CANNOT_DUMP:
4122 use EMACSLOADPATH env-var if set; otherwise use PATH_LOADSEARCH,
4123 prepending PATH_SITELOADSEARCH unless --no-site-lisp.
4124 The remainder is what happens when dumping works:
4125 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4126 Otherwise use EMACSLOADPATH if set, else PATH_LOADSEARCH.
4128 If !initialized, then just set both Vload_path and dump_path.
4129 If initialized, then if Vload_path != dump_path, do nothing.
4130 (Presumably the load-path has already been changed by something.
4131 This can only be from a site-load file during dumping,
4132 or because EMACSLOADPATH is set.)
4133 If Vinstallation_directory is not nil (ie, running uninstalled):
4134 If installation-dir/lisp exists and not already a member,
4135 we must be running uninstalled. Reset the load-path
4136 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4137 refers to the eventual installation directories. Since we
4138 are not yet installed, we should not use them, even if they exist.)
4139 If installation-dir/lisp does not exist, just add dump_path at the
4140 end instead.
4141 Add installation-dir/leim (if exists and not already a member) at the front.
4142 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4143 and not already a member) at the front.
4144 If installation-dir != source-dir (ie running an uninstalled,
4145 out-of-tree build) AND install-dir/src/Makefile exists BUT
4146 install-dir/src/Makefile.in does NOT exist (this is a sanity
4147 check), then repeat the above steps for source-dir/lisp,
4148 leim and site-lisp.
4149 Finally, add the site-lisp directories at the front (if !no_site_lisp).
4152 void
4153 init_lread (void)
4155 const char *normal;
4157 #ifdef CANNOT_DUMP
4158 #ifdef HAVE_NS
4159 const char *loadpath = ns_load_path ();
4160 #endif
4162 normal = PATH_LOADSEARCH;
4163 #ifdef HAVE_NS
4164 Vload_path = decode_env_path ("EMACSLOADPATH", loadpath ? loadpath : normal);
4165 #else
4166 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
4167 #endif
4169 load_path_check ();
4171 /* FIXME CANNOT_DUMP platforms should get source-dir/lisp etc added
4172 to their load-path too, AFAICS. I don't think we can tell the
4173 difference between initialized and !initialized in this case,
4174 so we'll have to do it unconditionally when Vinstallation_directory
4175 is non-nil. */
4176 if (!no_site_lisp && !egetenv ("EMACSLOADPATH"))
4178 Lisp_Object sitelisp;
4179 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
4180 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4182 #else /* !CANNOT_DUMP */
4183 if (NILP (Vpurify_flag))
4185 normal = PATH_LOADSEARCH;
4186 /* If the EMACSLOADPATH environment variable is set, use its value.
4187 This doesn't apply if we're dumping. */
4188 if (egetenv ("EMACSLOADPATH"))
4189 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
4191 else
4192 normal = PATH_DUMPLOADSEARCH;
4194 /* In a dumped Emacs, we normally reset the value of Vload_path using
4195 PATH_LOADSEARCH, since the value that was dumped uses lisp/ in
4196 the source directory, instead of the path of the installed elisp
4197 libraries. However, if it appears that Vload_path has already been
4198 changed from the default that was saved before dumping, don't
4199 change it further. Changes can only be due to EMACSLOADPATH, or
4200 site-lisp files that were processed during dumping. */
4201 if (initialized)
4203 if (NILP (Fequal (dump_path, Vload_path)))
4205 /* Do not make any changes, just check the elements exist. */
4206 /* Note: --no-site-lisp is ignored.
4207 I don't know what to do about this. */
4208 load_path_check ();
4210 else
4212 #ifdef HAVE_NS
4213 const char *loadpath = ns_load_path ();
4214 Vload_path = decode_env_path (0, loadpath ? loadpath : normal);
4215 #else
4216 Vload_path = decode_env_path (0, normal);
4217 #endif
4218 if (!NILP (Vinstallation_directory))
4220 Lisp_Object tem, tem1;
4222 /* Add to the path the lisp subdir of the installation
4223 dir, if it is accessible. Note: in out-of-tree builds,
4224 this directory is empty save for Makefile. */
4225 tem = Fexpand_file_name (build_string ("lisp"),
4226 Vinstallation_directory);
4227 tem1 = Ffile_accessible_directory_p (tem);
4228 if (!NILP (tem1))
4230 if (NILP (Fmember (tem, Vload_path)))
4232 /* We are running uninstalled. The default load-path
4233 points to the eventual installed lisp, leim
4234 directories. We should not use those now, even
4235 if they exist, so start over from a clean slate. */
4236 Vload_path = Fcons (tem, Qnil);
4239 else
4240 /* That dir doesn't exist, so add the build-time
4241 Lisp dirs instead. */
4242 Vload_path = nconc2 (Vload_path, dump_path);
4244 /* Add leim under the installation dir, if it is accessible. */
4245 tem = Fexpand_file_name (build_string ("leim"),
4246 Vinstallation_directory);
4247 tem1 = Ffile_accessible_directory_p (tem);
4248 if (!NILP (tem1))
4250 if (NILP (Fmember (tem, Vload_path)))
4251 Vload_path = Fcons (tem, Vload_path);
4254 /* Add site-lisp under the installation dir, if it exists. */
4255 if (!no_site_lisp)
4257 tem = Fexpand_file_name (build_string ("site-lisp"),
4258 Vinstallation_directory);
4259 tem1 = Ffile_accessible_directory_p (tem);
4260 if (!NILP (tem1))
4262 if (NILP (Fmember (tem, Vload_path)))
4263 Vload_path = Fcons (tem, Vload_path);
4267 /* If Emacs was not built in the source directory,
4268 and it is run from where it was built, add to load-path
4269 the lisp, leim and site-lisp dirs under that directory. */
4271 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4273 Lisp_Object tem2;
4275 tem = Fexpand_file_name (build_string ("src/Makefile"),
4276 Vinstallation_directory);
4277 tem1 = Ffile_exists_p (tem);
4279 /* Don't be fooled if they moved the entire source tree
4280 AFTER dumping Emacs. If the build directory is indeed
4281 different from the source dir, src/Makefile.in and
4282 src/Makefile will not be found together. */
4283 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4284 Vinstallation_directory);
4285 tem2 = Ffile_exists_p (tem);
4286 if (!NILP (tem1) && NILP (tem2))
4288 tem = Fexpand_file_name (build_string ("lisp"),
4289 Vsource_directory);
4291 if (NILP (Fmember (tem, Vload_path)))
4292 Vload_path = Fcons (tem, Vload_path);
4294 tem = Fexpand_file_name (build_string ("leim"),
4295 Vsource_directory);
4297 if (NILP (Fmember (tem, Vload_path)))
4298 Vload_path = Fcons (tem, Vload_path);
4300 if (!no_site_lisp)
4302 tem = Fexpand_file_name (build_string ("site-lisp"),
4303 Vsource_directory);
4304 tem1 = Ffile_accessible_directory_p (tem);
4305 if (!NILP (tem1))
4307 if (NILP (Fmember (tem, Vload_path)))
4308 Vload_path = Fcons (tem, Vload_path);
4312 } /* Vinstallation_directory != Vsource_directory */
4314 } /* if Vinstallation_directory */
4316 /* Check before adding the site-lisp directories.
4317 The install should have created them, but they are not
4318 required, so no need to warn if they are absent.
4319 Or we might be running before installation. */
4320 load_path_check ();
4322 /* Add the site-lisp directories at the front. */
4323 if (!no_site_lisp)
4325 Lisp_Object sitelisp;
4326 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
4327 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4329 } /* if dump_path == Vload_path */
4331 else /* !initialized */
4333 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4334 source directory. We used to add ../lisp (ie the lisp dir in
4335 the build directory) at the front here, but that caused trouble
4336 because it was copied from dump_path into Vload_path, above,
4337 when Vinstallation_directory was non-nil. It should not be
4338 necessary, since in out of tree builds lisp/ is empty, save
4339 for Makefile. */
4340 Vload_path = decode_env_path (0, normal);
4341 dump_path = Vload_path;
4342 /* No point calling load_path_check; load-path only contains essential
4343 elements from the source directory at this point. They cannot
4344 be missing unless something went extremely (and improbably)
4345 wrong, in which case the build will fail in obvious ways. */
4347 #endif /* !CANNOT_DUMP */
4349 Vvalues = Qnil;
4351 load_in_progress = 0;
4352 Vload_file_name = Qnil;
4354 load_descriptor_list = Qnil;
4356 Vstandard_input = Qt;
4357 Vloads_in_progress = Qnil;
4360 /* Print a warning that directory intended for use USE and with name
4361 DIRNAME cannot be accessed. On entry, errno should correspond to
4362 the access failure. Print the warning on stderr and put it in
4363 *Messages*. */
4365 void
4366 dir_warning (char const *use, Lisp_Object dirname)
4368 static char const format[] = "Warning: %s `%s': %s\n";
4369 int access_errno = errno;
4370 fprintf (stderr, format, use, SSDATA (dirname), strerror (access_errno));
4372 /* Don't log the warning before we've initialized!! */
4373 if (initialized)
4375 char const *diagnostic = emacs_strerror (access_errno);
4376 USE_SAFE_ALLOCA;
4377 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4378 + strlen (use) + SBYTES (dirname)
4379 + strlen (diagnostic));
4380 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4381 diagnostic);
4382 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4383 SAFE_FREE ();
4387 void
4388 syms_of_lread (void)
4390 defsubr (&Sread);
4391 defsubr (&Sread_from_string);
4392 defsubr (&Sintern);
4393 defsubr (&Sintern_soft);
4394 defsubr (&Sunintern);
4395 defsubr (&Sget_load_suffixes);
4396 defsubr (&Sload);
4397 defsubr (&Seval_buffer);
4398 defsubr (&Seval_region);
4399 defsubr (&Sread_char);
4400 defsubr (&Sread_char_exclusive);
4401 defsubr (&Sread_event);
4402 defsubr (&Sget_file_char);
4403 defsubr (&Smapatoms);
4404 defsubr (&Slocate_file_internal);
4406 DEFVAR_LISP ("obarray", Vobarray,
4407 doc: /* Symbol table for use by `intern' and `read'.
4408 It is a vector whose length ought to be prime for best results.
4409 The vector's contents don't make sense if examined from Lisp programs;
4410 to find all the symbols in an obarray, use `mapatoms'. */);
4412 DEFVAR_LISP ("values", Vvalues,
4413 doc: /* List of values of all expressions which were read, evaluated and printed.
4414 Order is reverse chronological. */);
4415 XSYMBOL (intern ("values"))->declared_special = 0;
4417 DEFVAR_LISP ("standard-input", Vstandard_input,
4418 doc: /* Stream for read to get input from.
4419 See documentation of `read' for possible values. */);
4420 Vstandard_input = Qt;
4422 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4423 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4425 If this variable is a buffer, then only forms read from that buffer
4426 will be added to `read-symbol-positions-list'.
4427 If this variable is t, then all read forms will be added.
4428 The effect of all other values other than nil are not currently
4429 defined, although they may be in the future.
4431 The positions are relative to the last call to `read' or
4432 `read-from-string'. It is probably a bad idea to set this variable at
4433 the toplevel; bind it instead. */);
4434 Vread_with_symbol_positions = Qnil;
4436 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4437 doc: /* A list mapping read symbols to their positions.
4438 This variable is modified during calls to `read' or
4439 `read-from-string', but only when `read-with-symbol-positions' is
4440 non-nil.
4442 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4443 CHAR-POSITION is an integer giving the offset of that occurrence of the
4444 symbol from the position where `read' or `read-from-string' started.
4446 Note that a symbol will appear multiple times in this list, if it was
4447 read multiple times. The list is in the same order as the symbols
4448 were read in. */);
4449 Vread_symbol_positions_list = Qnil;
4451 DEFVAR_LISP ("read-circle", Vread_circle,
4452 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4453 Vread_circle = Qt;
4455 DEFVAR_LISP ("load-path", Vload_path,
4456 doc: /* List of directories to search for files to load.
4457 Each element is a string (directory name) or nil (try default directory).
4458 Initialized based on EMACSLOADPATH environment variable, if any,
4459 otherwise to default specified by file `epaths.h' when Emacs was built. */);
4461 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4462 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4463 This list should not include the empty string.
4464 `load' and related functions try to append these suffixes, in order,
4465 to the specified file name if a Lisp suffix is allowed or required. */);
4466 Vload_suffixes = Fcons (build_pure_c_string (".elc"),
4467 Fcons (build_pure_c_string (".el"), Qnil));
4468 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4469 doc: /* List of suffixes that indicate representations of \
4470 the same file.
4471 This list should normally start with the empty string.
4473 Enabling Auto Compression mode appends the suffixes in
4474 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4475 mode removes them again. `load' and related functions use this list to
4476 determine whether they should look for compressed versions of a file
4477 and, if so, which suffixes they should try to append to the file name
4478 in order to do so. However, if you want to customize which suffixes
4479 the loading functions recognize as compression suffixes, you should
4480 customize `jka-compr-load-suffixes' rather than the present variable. */);
4481 Vload_file_rep_suffixes = Fcons (empty_unibyte_string, Qnil);
4483 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4484 doc: /* Non-nil if inside of `load'. */);
4485 DEFSYM (Qload_in_progress, "load-in-progress");
4487 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4488 doc: /* An alist of expressions to be evalled when particular files are loaded.
4489 Each element looks like (REGEXP-OR-FEATURE FORMS...).
4491 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4492 a symbol \(a feature name).
4494 When `load' is run and the file-name argument matches an element's
4495 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4496 REGEXP-OR-FEATURE, the FORMS in the element are executed.
4498 An error in FORMS does not undo the load, but does prevent execution of
4499 the rest of the FORMS. */);
4500 Vafter_load_alist = Qnil;
4502 DEFVAR_LISP ("load-history", Vload_history,
4503 doc: /* Alist mapping loaded file names to symbols and features.
4504 Each alist element should be a list (FILE-NAME ENTRIES...), where
4505 FILE-NAME is the name of a file that has been loaded into Emacs.
4506 The file name is absolute and true (i.e. it doesn't contain symlinks).
4507 As an exception, one of the alist elements may have FILE-NAME nil,
4508 for symbols and features not associated with any file.
4510 The remaining ENTRIES in the alist element describe the functions and
4511 variables defined in that file, the features provided, and the
4512 features required. Each entry has the form `(provide . FEATURE)',
4513 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4514 `(defface . SYMBOL)', or `(t . SYMBOL)'. Entries like `(t . SYMBOL)'
4515 may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
4516 autoload before this file redefined it as a function. In addition,
4517 entries may also be single symbols, which means that SYMBOL was
4518 defined by `defvar' or `defconst'.
4520 During preloading, the file name recorded is relative to the main Lisp
4521 directory. These file names are converted to absolute at startup. */);
4522 Vload_history = Qnil;
4524 DEFVAR_LISP ("load-file-name", Vload_file_name,
4525 doc: /* Full name of file being loaded by `load'. */);
4526 Vload_file_name = Qnil;
4528 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4529 doc: /* File name, including directory, of user's initialization file.
4530 If the file loaded had extension `.elc', and the corresponding source file
4531 exists, this variable contains the name of source file, suitable for use
4532 by functions like `custom-save-all' which edit the init file.
4533 While Emacs loads and evaluates the init file, value is the real name
4534 of the file, regardless of whether or not it has the `.elc' extension. */);
4535 Vuser_init_file = Qnil;
4537 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4538 doc: /* Used for internal purposes by `load'. */);
4539 Vcurrent_load_list = Qnil;
4541 DEFVAR_LISP ("load-read-function", Vload_read_function,
4542 doc: /* Function used by `load' and `eval-region' for reading expressions.
4543 The default is nil, which means use the function `read'. */);
4544 Vload_read_function = Qnil;
4546 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4547 doc: /* Function called in `load' to load an Emacs Lisp source file.
4548 The value should be a function for doing code conversion before
4549 reading a source file. It can also be nil, in which case loading is
4550 done without any code conversion.
4552 If the value is a function, it is called with four arguments,
4553 FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4554 the file to load, FILE is the non-absolute name (for messages etc.),
4555 and NOERROR and NOMESSAGE are the corresponding arguments passed to
4556 `load'. The function should return t if the file was loaded. */);
4557 Vload_source_file_function = Qnil;
4559 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4560 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4561 This is useful when the file being loaded is a temporary copy. */);
4562 load_force_doc_strings = 0;
4564 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4565 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4566 This is normally bound by `load' and `eval-buffer' to control `read',
4567 and is not meant for users to change. */);
4568 load_convert_to_unibyte = 0;
4570 DEFVAR_LISP ("source-directory", Vsource_directory,
4571 doc: /* Directory in which Emacs sources were found when Emacs was built.
4572 You cannot count on them to still be there! */);
4573 Vsource_directory
4574 = Fexpand_file_name (build_string ("../"),
4575 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
4577 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
4578 doc: /* List of files that were preloaded (when dumping Emacs). */);
4579 Vpreloaded_file_list = Qnil;
4581 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
4582 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4583 Vbyte_boolean_vars = Qnil;
4585 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
4586 doc: /* Non-nil means load dangerous compiled Lisp files.
4587 Some versions of XEmacs use different byte codes than Emacs. These
4588 incompatible byte codes can make Emacs crash when it tries to execute
4589 them. */);
4590 load_dangerous_libraries = 0;
4592 DEFVAR_BOOL ("force-load-messages", force_load_messages,
4593 doc: /* Non-nil means force printing messages when loading Lisp files.
4594 This overrides the value of the NOMESSAGE argument to `load'. */);
4595 force_load_messages = 0;
4597 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
4598 doc: /* Regular expression matching safe to load compiled Lisp files.
4599 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4600 from the file, and matches them against this regular expression.
4601 When the regular expression matches, the file is considered to be safe
4602 to load. See also `load-dangerous-libraries'. */);
4603 Vbytecomp_version_regexp
4604 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4606 DEFSYM (Qlexical_binding, "lexical-binding");
4607 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
4608 doc: /* Whether to use lexical binding when evaluating code.
4609 Non-nil means that the code in the current buffer should be evaluated
4610 with lexical binding.
4611 This variable is automatically set from the file variables of an
4612 interpreted Lisp file read using `load'. Unlike other file local
4613 variables, this must be set in the first line of a file. */);
4614 Vlexical_binding = Qnil;
4615 Fmake_variable_buffer_local (Qlexical_binding);
4617 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
4618 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4619 Veval_buffer_list = Qnil;
4621 DEFVAR_LISP ("old-style-backquotes", Vold_style_backquotes,
4622 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4623 Vold_style_backquotes = Qnil;
4624 DEFSYM (Qold_style_backquotes, "old-style-backquotes");
4626 /* Vsource_directory was initialized in init_lread. */
4628 load_descriptor_list = Qnil;
4629 staticpro (&load_descriptor_list);
4631 DEFSYM (Qcurrent_load_list, "current-load-list");
4632 DEFSYM (Qstandard_input, "standard-input");
4633 DEFSYM (Qread_char, "read-char");
4634 DEFSYM (Qget_file_char, "get-file-char");
4635 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
4636 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
4638 DEFSYM (Qbackquote, "`");
4639 DEFSYM (Qcomma, ",");
4640 DEFSYM (Qcomma_at, ",@");
4641 DEFSYM (Qcomma_dot, ",.");
4643 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
4644 DEFSYM (Qascii_character, "ascii-character");
4645 DEFSYM (Qfunction, "function");
4646 DEFSYM (Qload, "load");
4647 DEFSYM (Qload_file_name, "load-file-name");
4648 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
4649 DEFSYM (Qfile_truename, "file-truename");
4650 DEFSYM (Qdir_ok, "dir-ok");
4651 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
4653 staticpro (&dump_path);
4655 staticpro (&read_objects);
4656 read_objects = Qnil;
4657 staticpro (&seen_list);
4658 seen_list = Qnil;
4660 Vloads_in_progress = Qnil;
4661 staticpro (&Vloads_in_progress);
4663 DEFSYM (Qhash_table, "hash-table");
4664 DEFSYM (Qdata, "data");
4665 DEFSYM (Qtest, "test");
4666 DEFSYM (Qsize, "size");
4667 DEFSYM (Qweakness, "weakness");
4668 DEFSYM (Qrehash_size, "rehash-size");
4669 DEFSYM (Qrehash_threshold, "rehash-threshold");