New VC in the NEWS.
[emacs.git] / src / lread.c
blob5418d82a9681a2a6a393fed486a35ba3055b1eb4
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29 #include <errno.h>
30 #include <setjmp.h>
31 #include "lisp.h"
32 #include "intervals.h"
33 #include "buffer.h"
34 #include "charset.h"
35 #include <epaths.h>
36 #include "commands.h"
37 #include "keyboard.h"
38 #include "frame.h"
39 #include "termhooks.h"
40 #include "coding.h"
41 #include "blockinput.h"
43 #ifdef lint
44 #include <sys/inode.h>
45 #endif /* lint */
47 #ifdef MSDOS
48 #if __DJGPP__ < 2
49 #include <unistd.h> /* to get X_OK */
50 #endif
51 #include "msdos.h"
52 #endif
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
58 #ifndef X_OK
59 #define X_OK 01
60 #endif
62 #include <math.h>
64 #ifdef HAVE_SETLOCALE
65 #include <locale.h>
66 #endif /* HAVE_SETLOCALE */
68 #ifdef HAVE_FCNTL_H
69 #include <fcntl.h>
70 #endif
71 #ifndef O_RDONLY
72 #define O_RDONLY 0
73 #endif
75 #ifdef HAVE_FSEEKO
76 #define file_offset off_t
77 #define file_tell ftello
78 #else
79 #define file_offset long
80 #define file_tell ftell
81 #endif
83 #ifndef USE_CRT_DLL
84 extern int errno;
85 #endif
87 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
88 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
89 Lisp_Object Qascii_character, Qload, Qload_file_name;
90 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
91 Lisp_Object Qinhibit_file_name_operation;
92 Lisp_Object Qeval_buffer_list, Veval_buffer_list;
93 Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
95 extern Lisp_Object Qevent_symbol_element_mask;
96 extern Lisp_Object Qfile_exists_p;
98 /* non-zero if inside `load' */
99 int load_in_progress;
101 /* Directory in which the sources were found. */
102 Lisp_Object Vsource_directory;
104 /* Search path and suffixes for files to be loaded. */
105 Lisp_Object Vload_path, Vload_suffixes, Vload_file_rep_suffixes;
107 /* File name of user's init file. */
108 Lisp_Object Vuser_init_file;
110 /* This is the user-visible association list that maps features to
111 lists of defs in their load files. */
112 Lisp_Object Vload_history;
114 /* This is used to build the load history. */
115 Lisp_Object Vcurrent_load_list;
117 /* List of files that were preloaded. */
118 Lisp_Object Vpreloaded_file_list;
120 /* Name of file actually being read by `load'. */
121 Lisp_Object Vload_file_name;
123 /* Function to use for reading, in `load' and friends. */
124 Lisp_Object Vload_read_function;
126 /* The association list of objects read with the #n=object form.
127 Each member of the list has the form (n . object), and is used to
128 look up the object for the corresponding #n# construct.
129 It must be set to nil before all top-level calls to read0. */
130 Lisp_Object read_objects;
132 /* Nonzero means load should forcibly load all dynamic doc strings. */
133 static int load_force_doc_strings;
135 /* Nonzero means read should convert strings to unibyte. */
136 static int load_convert_to_unibyte;
138 /* Function to use for loading an Emacs Lisp source file (not
139 compiled) instead of readevalloop. */
140 Lisp_Object Vload_source_file_function;
142 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
143 Lisp_Object Vbyte_boolean_vars;
145 /* Whether or not to add a `read-positions' property to symbols
146 read. */
147 Lisp_Object Vread_with_symbol_positions;
149 /* List of (SYMBOL . POSITION) accumulated so far. */
150 Lisp_Object Vread_symbol_positions_list;
152 /* List of descriptors now open for Fload. */
153 static Lisp_Object load_descriptor_list;
155 /* File for get_file_char to read from. Use by load. */
156 static FILE *instream;
158 /* When nonzero, read conses in pure space */
159 static int read_pure;
161 /* For use within read-from-string (this reader is non-reentrant!!) */
162 static int read_from_string_index;
163 static int read_from_string_index_byte;
164 static int read_from_string_limit;
166 /* Number of bytes left to read in the buffer character
167 that `readchar' has already advanced over. */
168 static int readchar_backlog;
169 /* Number of characters read in the current call to Fread or
170 Fread_from_string. */
171 static int readchar_count;
173 /* This contains the last string skipped with #@. */
174 static char *saved_doc_string;
175 /* Length of buffer allocated in saved_doc_string. */
176 static int saved_doc_string_size;
177 /* Length of actual data in saved_doc_string. */
178 static int saved_doc_string_length;
179 /* This is the file position that string came from. */
180 static file_offset saved_doc_string_position;
182 /* This contains the previous string skipped with #@.
183 We copy it from saved_doc_string when a new string
184 is put in saved_doc_string. */
185 static char *prev_saved_doc_string;
186 /* Length of buffer allocated in prev_saved_doc_string. */
187 static int prev_saved_doc_string_size;
188 /* Length of actual data in prev_saved_doc_string. */
189 static int prev_saved_doc_string_length;
190 /* This is the file position that string came from. */
191 static file_offset prev_saved_doc_string_position;
193 /* Nonzero means inside a new-style backquote
194 with no surrounding parentheses.
195 Fread initializes this to zero, so we need not specbind it
196 or worry about what happens to it when there is an error. */
197 static int new_backquote_flag;
198 static Lisp_Object Vold_style_backquotes, Qold_style_backquotes;
200 /* A list of file names for files being loaded in Fload. Used to
201 check for recursive loads. */
203 static Lisp_Object Vloads_in_progress;
205 /* Non-zero means load dangerous compiled Lisp files. */
207 int load_dangerous_libraries;
209 /* A regular expression used to detect files compiled with Emacs. */
211 static Lisp_Object Vbytecomp_version_regexp;
213 static void to_multibyte P_ ((char **, char **, int *));
214 static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
215 Lisp_Object (*) (), int,
216 Lisp_Object, Lisp_Object,
217 Lisp_Object, Lisp_Object));
218 static Lisp_Object load_unwind P_ ((Lisp_Object));
219 static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
221 static void invalid_syntax P_ ((const char *, int)) NO_RETURN;
222 static void end_of_file_error P_ (()) NO_RETURN;
225 /* Handle unreading and rereading of characters.
226 Write READCHAR to read a character,
227 UNREAD(c) to unread c to be read again.
229 The READCHAR and UNREAD macros are meant for reading/unreading a
230 byte code; they do not handle multibyte characters. The caller
231 should manage them if necessary.
233 [ Actually that seems to be a lie; READCHAR will definitely read
234 multibyte characters from buffer sources, at least. Is the
235 comment just out of date?
236 -- Colin Walters <walters@gnu.org>, 22 May 2002 16:36:50 -0400 ]
239 #define READCHAR readchar (readcharfun)
240 #define UNREAD(c) unreadchar (readcharfun, c)
242 static int
243 readchar (readcharfun)
244 Lisp_Object readcharfun;
246 Lisp_Object tem;
247 register int c;
249 readchar_count++;
251 if (BUFFERP (readcharfun))
253 register struct buffer *inbuffer = XBUFFER (readcharfun);
255 int pt_byte = BUF_PT_BYTE (inbuffer);
256 int orig_pt_byte = pt_byte;
258 if (readchar_backlog > 0)
259 /* We get the address of the byte just passed,
260 which is the last byte of the character.
261 The other bytes in this character are consecutive with it,
262 because the gap can't be in the middle of a character. */
263 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
264 - --readchar_backlog);
266 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
267 return -1;
269 readchar_backlog = -1;
271 if (! NILP (inbuffer->enable_multibyte_characters))
273 /* Fetch the character code from the buffer. */
274 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
275 BUF_INC_POS (inbuffer, pt_byte);
276 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
278 else
280 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
281 pt_byte++;
283 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
285 return c;
287 if (MARKERP (readcharfun))
289 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
291 int bytepos = marker_byte_position (readcharfun);
292 int orig_bytepos = bytepos;
294 if (readchar_backlog > 0)
295 /* We get the address of the byte just passed,
296 which is the last byte of the character.
297 The other bytes in this character are consecutive with it,
298 because the gap can't be in the middle of a character. */
299 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
300 - --readchar_backlog);
302 if (bytepos >= BUF_ZV_BYTE (inbuffer))
303 return -1;
305 readchar_backlog = -1;
307 if (! NILP (inbuffer->enable_multibyte_characters))
309 /* Fetch the character code from the buffer. */
310 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
311 BUF_INC_POS (inbuffer, bytepos);
312 c = STRING_CHAR (p, bytepos - orig_bytepos);
314 else
316 c = BUF_FETCH_BYTE (inbuffer, bytepos);
317 bytepos++;
320 XMARKER (readcharfun)->bytepos = bytepos;
321 XMARKER (readcharfun)->charpos++;
323 return c;
326 if (EQ (readcharfun, Qlambda))
327 return read_bytecode_char (0);
329 if (EQ (readcharfun, Qget_file_char))
331 BLOCK_INPUT;
332 c = getc (instream);
333 #ifdef EINTR
334 /* Interrupted reads have been observed while reading over the network */
335 while (c == EOF && ferror (instream) && errno == EINTR)
337 UNBLOCK_INPUT;
338 QUIT;
339 BLOCK_INPUT;
340 clearerr (instream);
341 c = getc (instream);
343 #endif
344 UNBLOCK_INPUT;
345 return c;
348 if (STRINGP (readcharfun))
350 if (read_from_string_index >= read_from_string_limit)
351 c = -1;
352 else
353 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
354 read_from_string_index,
355 read_from_string_index_byte);
357 return c;
360 tem = call0 (readcharfun);
362 if (NILP (tem))
363 return -1;
364 return XINT (tem);
367 /* Unread the character C in the way appropriate for the stream READCHARFUN.
368 If the stream is a user function, call it with the char as argument. */
370 static void
371 unreadchar (readcharfun, c)
372 Lisp_Object readcharfun;
373 int c;
375 readchar_count--;
376 if (c == -1)
377 /* Don't back up the pointer if we're unreading the end-of-input mark,
378 since readchar didn't advance it when we read it. */
380 else if (BUFFERP (readcharfun))
382 struct buffer *b = XBUFFER (readcharfun);
383 int bytepos = BUF_PT_BYTE (b);
385 if (readchar_backlog >= 0)
386 readchar_backlog++;
387 else
389 BUF_PT (b)--;
390 if (! NILP (b->enable_multibyte_characters))
391 BUF_DEC_POS (b, bytepos);
392 else
393 bytepos--;
395 BUF_PT_BYTE (b) = bytepos;
398 else if (MARKERP (readcharfun))
400 struct buffer *b = XMARKER (readcharfun)->buffer;
401 int bytepos = XMARKER (readcharfun)->bytepos;
403 if (readchar_backlog >= 0)
404 readchar_backlog++;
405 else
407 XMARKER (readcharfun)->charpos--;
408 if (! NILP (b->enable_multibyte_characters))
409 BUF_DEC_POS (b, bytepos);
410 else
411 bytepos--;
413 XMARKER (readcharfun)->bytepos = bytepos;
416 else if (STRINGP (readcharfun))
418 read_from_string_index--;
419 read_from_string_index_byte
420 = string_char_to_byte (readcharfun, read_from_string_index);
422 else if (EQ (readcharfun, Qlambda))
423 read_bytecode_char (1);
424 else if (EQ (readcharfun, Qget_file_char))
426 BLOCK_INPUT;
427 ungetc (c, instream);
428 UNBLOCK_INPUT;
430 else
431 call1 (readcharfun, make_number (c));
434 static Lisp_Object read_internal_start P_ ((Lisp_Object, Lisp_Object,
435 Lisp_Object));
436 static Lisp_Object read0 P_ ((Lisp_Object));
437 static Lisp_Object read1 P_ ((Lisp_Object, int *, int));
439 static Lisp_Object read_list P_ ((int, Lisp_Object));
440 static Lisp_Object read_vector P_ ((Lisp_Object, int));
441 static int read_multibyte P_ ((int, Lisp_Object));
443 static Lisp_Object substitute_object_recurse P_ ((Lisp_Object, Lisp_Object,
444 Lisp_Object));
445 static void substitute_object_in_subtree P_ ((Lisp_Object,
446 Lisp_Object));
447 static void substitute_in_interval P_ ((INTERVAL, Lisp_Object));
450 /* Get a character from the tty. */
452 /* Read input events until we get one that's acceptable for our purposes.
454 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
455 until we get a character we like, and then stuffed into
456 unread_switch_frame.
458 If ASCII_REQUIRED is non-zero, we check function key events to see
459 if the unmodified version of the symbol has a Qascii_character
460 property, and use that character, if present.
462 If ERROR_NONASCII is non-zero, we signal an error if the input we
463 get isn't an ASCII character with modifiers. If it's zero but
464 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
465 character.
467 If INPUT_METHOD is nonzero, we invoke the current input method
468 if the character warrants that.
470 If SECONDS is a number, we wait that many seconds for input, and
471 return Qnil if no input arrives within that time. */
473 Lisp_Object
474 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
475 input_method, seconds)
476 int no_switch_frame, ascii_required, error_nonascii, input_method;
477 Lisp_Object seconds;
479 Lisp_Object val, delayed_switch_frame;
480 EMACS_TIME end_time;
482 #ifdef HAVE_WINDOW_SYSTEM
483 if (display_hourglass_p)
484 cancel_hourglass ();
485 #endif
487 delayed_switch_frame = Qnil;
489 /* Compute timeout. */
490 if (NUMBERP (seconds))
492 EMACS_TIME wait_time;
493 int sec, usec;
494 double duration = extract_float (seconds);
496 sec = (int) duration;
497 usec = (duration - sec) * 1000000;
498 EMACS_GET_TIME (end_time);
499 EMACS_SET_SECS_USECS (wait_time, sec, usec);
500 EMACS_ADD_TIME (end_time, end_time, wait_time);
503 /* Read until we get an acceptable event. */
504 retry:
506 val = read_char (0, 0, 0, (input_method ? Qnil : Qt), 0,
507 NUMBERP (seconds) ? &end_time : NULL);
508 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
510 if (BUFFERP (val))
511 goto retry;
513 /* switch-frame events are put off until after the next ASCII
514 character. This is better than signaling an error just because
515 the last characters were typed to a separate minibuffer frame,
516 for example. Eventually, some code which can deal with
517 switch-frame events will read it and process it. */
518 if (no_switch_frame
519 && EVENT_HAS_PARAMETERS (val)
520 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
522 delayed_switch_frame = val;
523 goto retry;
526 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
528 /* Convert certain symbols to their ASCII equivalents. */
529 if (SYMBOLP (val))
531 Lisp_Object tem, tem1;
532 tem = Fget (val, Qevent_symbol_element_mask);
533 if (!NILP (tem))
535 tem1 = Fget (Fcar (tem), Qascii_character);
536 /* Merge this symbol's modifier bits
537 with the ASCII equivalent of its basic code. */
538 if (!NILP (tem1))
539 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
543 /* If we don't have a character now, deal with it appropriately. */
544 if (!INTEGERP (val))
546 if (error_nonascii)
548 Vunread_command_events = Fcons (val, Qnil);
549 error ("Non-character input-event");
551 else
552 goto retry;
556 if (! NILP (delayed_switch_frame))
557 unread_switch_frame = delayed_switch_frame;
559 #if 0
561 #ifdef HAVE_WINDOW_SYSTEM
562 if (display_hourglass_p)
563 start_hourglass ();
564 #endif
566 #endif
568 return val;
571 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
572 doc: /* Read a character from the command input (keyboard or macro).
573 It is returned as a number.
574 If the user generates an event which is not a character (i.e. a mouse
575 click or function key event), `read-char' signals an error. As an
576 exception, switch-frame events are put off until non-ASCII events can
577 be read.
578 If you want to read non-character events, or ignore them, call
579 `read-event' or `read-char-exclusive' instead.
581 If the optional argument PROMPT is non-nil, display that as a prompt.
582 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
583 input method is turned on in the current buffer, that input method
584 is used for reading a character.
585 If the optional argument SECONDS is non-nil, it should be a number
586 specifying the maximum number of seconds to wait for input. If no
587 input arrives in that time, return nil. SECONDS may be a
588 floating-point value. */)
589 (prompt, inherit_input_method, seconds)
590 Lisp_Object prompt, inherit_input_method, seconds;
592 if (! NILP (prompt))
593 message_with_string ("%s", prompt, 0);
594 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
597 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
598 doc: /* Read an event object from the input stream.
599 If the optional argument PROMPT is non-nil, display that as a prompt.
600 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
601 input method is turned on in the current buffer, that input method
602 is used for reading a character.
603 If the optional argument SECONDS is non-nil, it should be a number
604 specifying the maximum number of seconds to wait for input. If no
605 input arrives in that time, return nil. SECONDS may be a
606 floating-point value. */)
607 (prompt, inherit_input_method, seconds)
608 Lisp_Object prompt, inherit_input_method, seconds;
610 if (! NILP (prompt))
611 message_with_string ("%s", prompt, 0);
612 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
615 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
616 doc: /* Read a character from the command input (keyboard or macro).
617 It is returned as a number. Non-character events are ignored.
619 If the optional argument PROMPT is non-nil, display that as a prompt.
620 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
621 input method is turned on in the current buffer, that input method
622 is used for reading a character.
623 If the optional argument SECONDS is non-nil, it should be a number
624 specifying the maximum number of seconds to wait for input. If no
625 input arrives in that time, return nil. SECONDS may be a
626 floating-point value. */)
627 (prompt, inherit_input_method, seconds)
628 Lisp_Object prompt, inherit_input_method, seconds;
630 if (! NILP (prompt))
631 message_with_string ("%s", prompt, 0);
632 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
635 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
636 doc: /* Don't use this yourself. */)
639 register Lisp_Object val;
640 BLOCK_INPUT;
641 XSETINT (val, getc (instream));
642 UNBLOCK_INPUT;
643 return val;
648 /* Value is non-zero if the file associated with file descriptor FD
649 is a compiled Lisp file that's safe to load. Only files compiled
650 with Emacs are safe to load. Files compiled with XEmacs can lead
651 to a crash in Fbyte_code because of an incompatible change in the
652 byte compiler. */
654 static int
655 safe_to_load_p (fd)
656 int fd;
658 char buf[512];
659 int nbytes, i;
660 int safe_p = 1;
662 /* Read the first few bytes from the file, and look for a line
663 specifying the byte compiler version used. */
664 nbytes = emacs_read (fd, buf, sizeof buf - 1);
665 if (nbytes > 0)
667 buf[nbytes] = '\0';
669 /* Skip to the next newline, skipping over the initial `ELC'
670 with NUL bytes following it. */
671 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
674 if (i < nbytes
675 && fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
676 buf + i) < 0)
677 safe_p = 0;
680 lseek (fd, 0, SEEK_SET);
681 return safe_p;
685 /* Callback for record_unwind_protect. Restore the old load list OLD,
686 after loading a file successfully. */
688 static Lisp_Object
689 record_load_unwind (old)
690 Lisp_Object old;
692 return Vloads_in_progress = old;
695 /* This handler function is used via internal_condition_case_1. */
697 static Lisp_Object
698 load_error_handler (data)
699 Lisp_Object data;
701 return Qnil;
704 static Lisp_Object
705 load_warn_old_style_backquotes (file)
706 Lisp_Object file;
708 if (!NILP (Vold_style_backquotes))
710 Lisp_Object args[2];
711 args[0] = build_string ("Loading `%s': old-style backquotes detected!");
712 args[1] = file;
713 Fmessage (2, args);
715 return Qnil;
718 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
719 doc: /* Return the suffixes that `load' should try if a suffix is \
720 required.
721 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
724 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
725 while (CONSP (suffixes))
727 Lisp_Object exts = Vload_file_rep_suffixes;
728 suffix = XCAR (suffixes);
729 suffixes = XCDR (suffixes);
730 while (CONSP (exts))
732 ext = XCAR (exts);
733 exts = XCDR (exts);
734 lst = Fcons (concat2 (suffix, ext), lst);
737 return Fnreverse (lst);
740 DEFUN ("load", Fload, Sload, 1, 5, 0,
741 doc: /* Execute a file of Lisp code named FILE.
742 First try FILE with `.elc' appended, then try with `.el',
743 then try FILE unmodified (the exact suffixes in the exact order are
744 determined by `load-suffixes'). Environment variable references in
745 FILE are replaced with their values by calling `substitute-in-file-name'.
746 This function searches the directories in `load-path'.
748 If optional second arg NOERROR is non-nil,
749 report no error if FILE doesn't exist.
750 Print messages at start and end of loading unless
751 optional third arg NOMESSAGE is non-nil.
752 If optional fourth arg NOSUFFIX is non-nil, don't try adding
753 suffixes `.elc' or `.el' to the specified name FILE.
754 If optional fifth arg MUST-SUFFIX is non-nil, insist on
755 the suffix `.elc' or `.el'; don't accept just FILE unless
756 it ends in one of those suffixes or includes a directory name.
758 If this function fails to find a file, it may look for different
759 representations of that file before trying another file.
760 It does so by adding the non-empty suffixes in `load-file-rep-suffixes'
761 to the file name. Emacs uses this feature mainly to find compressed
762 versions of files when Auto Compression mode is enabled.
764 The exact suffixes that this function tries out, in the exact order,
765 are given by the value of the variable `load-file-rep-suffixes' if
766 NOSUFFIX is non-nil and by the return value of the function
767 `get-load-suffixes' if MUST-SUFFIX is non-nil. If both NOSUFFIX and
768 MUST-SUFFIX are nil, this function first tries out the latter suffixes
769 and then the former.
771 Loading a file records its definitions, and its `provide' and
772 `require' calls, in an element of `load-history' whose
773 car is the file name loaded. See `load-history'.
775 Return t if the file exists and loads successfully. */)
776 (file, noerror, nomessage, nosuffix, must_suffix)
777 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
779 register FILE *stream;
780 register int fd = -1;
781 int count = SPECPDL_INDEX ();
782 struct gcpro gcpro1, gcpro2, gcpro3;
783 Lisp_Object found, efound, hist_file_name;
784 /* 1 means we printed the ".el is newer" message. */
785 int newer = 0;
786 /* 1 means we are loading a compiled file. */
787 int compiled = 0;
788 Lisp_Object handler;
789 int safe_p = 1;
790 char *fmode = "r";
791 Lisp_Object tmp[2];
792 #ifdef DOS_NT
793 fmode = "rt";
794 #endif /* DOS_NT */
796 CHECK_STRING (file);
798 /* If file name is magic, call the handler. */
799 /* This shouldn't be necessary any more now that `openp' handles it right.
800 handler = Ffind_file_name_handler (file, Qload);
801 if (!NILP (handler))
802 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
804 /* Do this after the handler to avoid
805 the need to gcpro noerror, nomessage and nosuffix.
806 (Below here, we care only whether they are nil or not.)
807 The presence of this call is the result of a historical accident:
808 it used to be in every file-operation and when it got removed
809 everywhere, it accidentally stayed here. Since then, enough people
810 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
811 that it seemed risky to remove. */
812 if (! NILP (noerror))
814 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
815 Qt, load_error_handler);
816 if (NILP (file))
817 return Qnil;
819 else
820 file = Fsubstitute_in_file_name (file);
823 /* Avoid weird lossage with null string as arg,
824 since it would try to load a directory as a Lisp file */
825 if (SCHARS (file) > 0)
827 int size = SBYTES (file);
829 found = Qnil;
830 GCPRO2 (file, found);
832 if (! NILP (must_suffix))
834 /* Don't insist on adding a suffix if FILE already ends with one. */
835 if (size > 3
836 && !strcmp (SDATA (file) + size - 3, ".el"))
837 must_suffix = Qnil;
838 else if (size > 4
839 && !strcmp (SDATA (file) + size - 4, ".elc"))
840 must_suffix = Qnil;
841 /* Don't insist on adding a suffix
842 if the argument includes a directory name. */
843 else if (! NILP (Ffile_name_directory (file)))
844 must_suffix = Qnil;
847 fd = openp (Vload_path, file,
848 (!NILP (nosuffix) ? Qnil
849 : !NILP (must_suffix) ? Fget_load_suffixes ()
850 : Fappend (2, (tmp[0] = Fget_load_suffixes (),
851 tmp[1] = Vload_file_rep_suffixes,
852 tmp))),
853 &found, Qnil);
854 UNGCPRO;
857 if (fd == -1)
859 if (NILP (noerror))
860 xsignal2 (Qfile_error, build_string ("Cannot open load file"), file);
861 return Qnil;
864 /* Tell startup.el whether or not we found the user's init file. */
865 if (EQ (Qt, Vuser_init_file))
866 Vuser_init_file = found;
868 /* If FD is -2, that means openp found a magic file. */
869 if (fd == -2)
871 if (NILP (Fequal (found, file)))
872 /* If FOUND is a different file name from FILE,
873 find its handler even if we have already inhibited
874 the `load' operation on FILE. */
875 handler = Ffind_file_name_handler (found, Qt);
876 else
877 handler = Ffind_file_name_handler (found, Qload);
878 if (! NILP (handler))
879 return call5 (handler, Qload, found, noerror, nomessage, Qt);
882 /* Check if we're stuck in a recursive load cycle.
884 2000-09-21: It's not possible to just check for the file loaded
885 being a member of Vloads_in_progress. This fails because of the
886 way the byte compiler currently works; `provide's are not
887 evaluted, see font-lock.el/jit-lock.el as an example. This
888 leads to a certain amount of ``normal'' recursion.
890 Also, just loading a file recursively is not always an error in
891 the general case; the second load may do something different. */
893 int count = 0;
894 Lisp_Object tem;
895 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
896 if (!NILP (Fequal (found, XCAR (tem))))
897 count++;
898 if (count > 3)
900 if (fd >= 0)
901 emacs_close (fd);
902 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
904 record_unwind_protect (record_load_unwind, Vloads_in_progress);
905 Vloads_in_progress = Fcons (found, Vloads_in_progress);
908 /* Get the name for load-history. */
909 hist_file_name = (! NILP (Vpurify_flag)
910 ? Fconcat (2, (tmp[0] = Ffile_name_directory (file),
911 tmp[1] = Ffile_name_nondirectory (found),
912 tmp))
913 : found) ;
915 /* Check for the presence of old-style quotes and warn about them. */
916 specbind (Qold_style_backquotes, Qnil);
917 record_unwind_protect (load_warn_old_style_backquotes, file);
919 if (!bcmp (SDATA (found) + SBYTES (found) - 4,
920 ".elc", 4))
921 /* Load .elc files directly, but not when they are
922 remote and have no handler! */
924 if (fd != -2)
926 struct stat s1, s2;
927 int result;
929 GCPRO3 (file, found, hist_file_name);
931 if (!safe_to_load_p (fd))
933 safe_p = 0;
934 if (!load_dangerous_libraries)
936 if (fd >= 0)
937 emacs_close (fd);
938 error ("File `%s' was not compiled in Emacs",
939 SDATA (found));
941 else if (!NILP (nomessage))
942 message_with_string ("File `%s' not compiled in Emacs", found, 1);
945 compiled = 1;
947 efound = ENCODE_FILE (found);
949 #ifdef DOS_NT
950 fmode = "rb";
951 #endif /* DOS_NT */
952 stat ((char *)SDATA (efound), &s1);
953 SSET (efound, SBYTES (efound) - 1, 0);
954 result = stat ((char *)SDATA (efound), &s2);
955 SSET (efound, SBYTES (efound) - 1, 'c');
957 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
959 /* Make the progress messages mention that source is newer. */
960 newer = 1;
962 /* If we won't print another message, mention this anyway. */
963 if (!NILP (nomessage))
965 Lisp_Object msg_file;
966 msg_file = Fsubstring (found, make_number (0), make_number (-1));
967 message_with_string ("Source file `%s' newer than byte-compiled file",
968 msg_file, 1);
971 UNGCPRO;
974 else
976 /* We are loading a source file (*.el). */
977 if (!NILP (Vload_source_file_function))
979 Lisp_Object val;
981 if (fd >= 0)
982 emacs_close (fd);
983 val = call4 (Vload_source_file_function, found, hist_file_name,
984 NILP (noerror) ? Qnil : Qt,
985 NILP (nomessage) ? Qnil : Qt);
986 return unbind_to (count, val);
990 GCPRO3 (file, found, hist_file_name);
992 #ifdef WINDOWSNT
993 emacs_close (fd);
994 efound = ENCODE_FILE (found);
995 stream = fopen ((char *) SDATA (efound), fmode);
996 #else /* not WINDOWSNT */
997 stream = fdopen (fd, fmode);
998 #endif /* not WINDOWSNT */
999 if (stream == 0)
1001 emacs_close (fd);
1002 error ("Failure to create stdio stream for %s", SDATA (file));
1005 if (! NILP (Vpurify_flag))
1006 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
1008 if (NILP (nomessage))
1010 if (!safe_p)
1011 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1012 file, 1);
1013 else if (!compiled)
1014 message_with_string ("Loading %s (source)...", file, 1);
1015 else if (newer)
1016 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1017 file, 1);
1018 else /* The typical case; compiled file newer than source file. */
1019 message_with_string ("Loading %s...", file, 1);
1022 record_unwind_protect (load_unwind, make_save_value (stream, 0));
1023 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
1024 specbind (Qload_file_name, found);
1025 specbind (Qinhibit_file_name_operation, Qnil);
1026 load_descriptor_list
1027 = Fcons (make_number (fileno (stream)), load_descriptor_list);
1028 load_in_progress++;
1029 readevalloop (Qget_file_char, stream, hist_file_name,
1030 Feval, 0, Qnil, Qnil, Qnil, Qnil);
1031 unbind_to (count, Qnil);
1033 /* Run any eval-after-load forms for this file */
1034 if (NILP (Vpurify_flag)
1035 && (!NILP (Ffboundp (Qdo_after_load_evaluation))))
1036 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1038 UNGCPRO;
1040 if (saved_doc_string)
1041 free (saved_doc_string);
1042 saved_doc_string = 0;
1043 saved_doc_string_size = 0;
1045 if (prev_saved_doc_string)
1046 xfree (prev_saved_doc_string);
1047 prev_saved_doc_string = 0;
1048 prev_saved_doc_string_size = 0;
1050 if (!noninteractive && NILP (nomessage))
1052 if (!safe_p)
1053 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1054 file, 1);
1055 else if (!compiled)
1056 message_with_string ("Loading %s (source)...done", file, 1);
1057 else if (newer)
1058 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1059 file, 1);
1060 else /* The typical case; compiled file newer than source file. */
1061 message_with_string ("Loading %s...done", file, 1);
1064 if (!NILP (Fequal (build_string ("obsolete"),
1065 Ffile_name_nondirectory
1066 (Fdirectory_file_name (Ffile_name_directory (found))))))
1067 message_with_string ("Package %s is obsolete", file, 1);
1069 return Qt;
1072 static Lisp_Object
1073 load_unwind (arg) /* used as unwind-protect function in load */
1074 Lisp_Object arg;
1076 FILE *stream = (FILE *) XSAVE_VALUE (arg)->pointer;
1077 if (stream != NULL)
1079 BLOCK_INPUT;
1080 fclose (stream);
1081 UNBLOCK_INPUT;
1083 if (--load_in_progress < 0) load_in_progress = 0;
1084 return Qnil;
1087 static Lisp_Object
1088 load_descriptor_unwind (oldlist)
1089 Lisp_Object oldlist;
1091 load_descriptor_list = oldlist;
1092 return Qnil;
1095 /* Close all descriptors in use for Floads.
1096 This is used when starting a subprocess. */
1098 void
1099 close_load_descs ()
1101 #ifndef WINDOWSNT
1102 Lisp_Object tail;
1103 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
1104 emacs_close (XFASTINT (XCAR (tail)));
1105 #endif
1108 static int
1109 complete_filename_p (pathname)
1110 Lisp_Object pathname;
1112 register const unsigned char *s = SDATA (pathname);
1113 return (IS_DIRECTORY_SEP (s[0])
1114 || (SCHARS (pathname) > 2
1115 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
1116 #ifdef ALTOS
1117 || *s == '@'
1118 #endif
1119 #ifdef VMS
1120 || index (s, ':')
1121 #endif /* VMS */
1125 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1126 doc: /* Search for FILENAME through PATH.
1127 Returns the file's name in absolute form, or nil if not found.
1128 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1129 file name when searching.
1130 If non-nil, PREDICATE is used instead of `file-readable-p'.
1131 PREDICATE can also be an integer to pass to the access(2) function,
1132 in which case file-name-handlers are ignored. */)
1133 (filename, path, suffixes, predicate)
1134 Lisp_Object filename, path, suffixes, predicate;
1136 Lisp_Object file;
1137 int fd = openp (path, filename, suffixes, &file, predicate);
1138 if (NILP (predicate) && fd > 0)
1139 close (fd);
1140 return file;
1144 /* Search for a file whose name is STR, looking in directories
1145 in the Lisp list PATH, and trying suffixes from SUFFIX.
1146 On success, returns a file descriptor. On failure, returns -1.
1148 SUFFIXES is a list of strings containing possible suffixes.
1149 The empty suffix is automatically added if the list is empty.
1151 PREDICATE non-nil means don't open the files,
1152 just look for one that satisfies the predicate. In this case,
1153 returns 1 on success. The predicate can be a lisp function or
1154 an integer to pass to `access' (in which case file-name-handlers
1155 are ignored).
1157 If STOREPTR is nonzero, it points to a slot where the name of
1158 the file actually found should be stored as a Lisp string.
1159 nil is stored there on failure.
1161 If the file we find is remote, return -2
1162 but store the found remote file name in *STOREPTR. */
1165 openp (path, str, suffixes, storeptr, predicate)
1166 Lisp_Object path, str;
1167 Lisp_Object suffixes;
1168 Lisp_Object *storeptr;
1169 Lisp_Object predicate;
1171 register int fd;
1172 int fn_size = 100;
1173 char buf[100];
1174 register char *fn = buf;
1175 int absolute = 0;
1176 int want_size;
1177 Lisp_Object filename;
1178 struct stat st;
1179 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
1180 Lisp_Object string, tail, encoded_fn;
1181 int max_suffix_len = 0;
1183 CHECK_STRING (str);
1185 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1187 CHECK_STRING_CAR (tail);
1188 max_suffix_len = max (max_suffix_len,
1189 SBYTES (XCAR (tail)));
1192 string = filename = encoded_fn = Qnil;
1193 GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
1195 if (storeptr)
1196 *storeptr = Qnil;
1198 if (complete_filename_p (str))
1199 absolute = 1;
1201 for (; CONSP (path); path = XCDR (path))
1203 filename = Fexpand_file_name (str, XCAR (path));
1204 if (!complete_filename_p (filename))
1205 /* If there are non-absolute elts in PATH (eg ".") */
1206 /* Of course, this could conceivably lose if luser sets
1207 default-directory to be something non-absolute... */
1209 filename = Fexpand_file_name (filename, current_buffer->directory);
1210 if (!complete_filename_p (filename))
1211 /* Give up on this path element! */
1212 continue;
1215 /* Calculate maximum size of any filename made from
1216 this path element/specified file name and any possible suffix. */
1217 want_size = max_suffix_len + SBYTES (filename) + 1;
1218 if (fn_size < want_size)
1219 fn = (char *) alloca (fn_size = 100 + want_size);
1221 /* Loop over suffixes. */
1222 for (tail = NILP (suffixes) ? Fcons (empty_unibyte_string, Qnil) : suffixes;
1223 CONSP (tail); tail = XCDR (tail))
1225 int lsuffix = SBYTES (XCAR (tail));
1226 Lisp_Object handler;
1227 int exists;
1229 /* Concatenate path element/specified name with the suffix.
1230 If the directory starts with /:, remove that. */
1231 if (SCHARS (filename) > 2
1232 && SREF (filename, 0) == '/'
1233 && SREF (filename, 1) == ':')
1235 strncpy (fn, SDATA (filename) + 2,
1236 SBYTES (filename) - 2);
1237 fn[SBYTES (filename) - 2] = 0;
1239 else
1241 strncpy (fn, SDATA (filename),
1242 SBYTES (filename));
1243 fn[SBYTES (filename)] = 0;
1246 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
1247 strncat (fn, SDATA (XCAR (tail)), lsuffix);
1249 /* Check that the file exists and is not a directory. */
1250 /* We used to only check for handlers on non-absolute file names:
1251 if (absolute)
1252 handler = Qnil;
1253 else
1254 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1255 It's not clear why that was the case and it breaks things like
1256 (load "/bar.el") where the file is actually "/bar.el.gz". */
1257 string = build_string (fn);
1258 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1259 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1261 if (NILP (predicate))
1262 exists = !NILP (Ffile_readable_p (string));
1263 else
1264 exists = !NILP (call1 (predicate, string));
1265 if (exists && !NILP (Ffile_directory_p (string)))
1266 exists = 0;
1268 if (exists)
1270 /* We succeeded; return this descriptor and filename. */
1271 if (storeptr)
1272 *storeptr = string;
1273 UNGCPRO;
1274 return -2;
1277 else
1279 const char *pfn;
1281 encoded_fn = ENCODE_FILE (string);
1282 pfn = SDATA (encoded_fn);
1283 exists = (stat (pfn, &st) >= 0
1284 && (st.st_mode & S_IFMT) != S_IFDIR);
1285 if (exists)
1287 /* Check that we can access or open it. */
1288 if (NATNUMP (predicate))
1289 fd = (access (pfn, XFASTINT (predicate)) == 0) ? 1 : -1;
1290 else
1291 fd = emacs_open (pfn, O_RDONLY, 0);
1293 if (fd >= 0)
1295 /* We succeeded; return this descriptor and filename. */
1296 if (storeptr)
1297 *storeptr = string;
1298 UNGCPRO;
1299 return fd;
1304 if (absolute)
1305 break;
1308 UNGCPRO;
1309 return -1;
1313 /* Merge the list we've accumulated of globals from the current input source
1314 into the load_history variable. The details depend on whether
1315 the source has an associated file name or not.
1317 FILENAME is the file name that we are loading from.
1318 ENTIRE is 1 if loading that entire file, 0 if evaluating part of it. */
1320 static void
1321 build_load_history (filename, entire)
1322 Lisp_Object filename;
1323 int entire;
1325 register Lisp_Object tail, prev, newelt;
1326 register Lisp_Object tem, tem2;
1327 register int foundit = 0;
1329 tail = Vload_history;
1330 prev = Qnil;
1332 while (CONSP (tail))
1334 tem = XCAR (tail);
1336 /* Find the feature's previous assoc list... */
1337 if (!NILP (Fequal (filename, Fcar (tem))))
1339 foundit = 1;
1341 /* If we're loading the entire file, remove old data. */
1342 if (entire)
1344 if (NILP (prev))
1345 Vload_history = XCDR (tail);
1346 else
1347 Fsetcdr (prev, XCDR (tail));
1350 /* Otherwise, cons on new symbols that are not already members. */
1351 else
1353 tem2 = Vcurrent_load_list;
1355 while (CONSP (tem2))
1357 newelt = XCAR (tem2);
1359 if (NILP (Fmember (newelt, tem)))
1360 Fsetcar (tail, Fcons (XCAR (tem),
1361 Fcons (newelt, XCDR (tem))));
1363 tem2 = XCDR (tem2);
1364 QUIT;
1368 else
1369 prev = tail;
1370 tail = XCDR (tail);
1371 QUIT;
1374 /* If we're loading an entire file, cons the new assoc onto the
1375 front of load-history, the most-recently-loaded position. Also
1376 do this if we didn't find an existing member for the file. */
1377 if (entire || !foundit)
1378 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1379 Vload_history);
1382 Lisp_Object
1383 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1384 Lisp_Object junk;
1386 read_pure = 0;
1387 return Qnil;
1390 static Lisp_Object
1391 readevalloop_1 (old)
1392 Lisp_Object old;
1394 load_convert_to_unibyte = ! NILP (old);
1395 return Qnil;
1398 /* Signal an `end-of-file' error, if possible with file name
1399 information. */
1401 static void
1402 end_of_file_error ()
1404 if (STRINGP (Vload_file_name))
1405 xsignal1 (Qend_of_file, Vload_file_name);
1407 xsignal0 (Qend_of_file);
1410 /* UNIBYTE specifies how to set load_convert_to_unibyte
1411 for this invocation.
1412 READFUN, if non-nil, is used instead of `read'.
1414 START, END specify region to read in current buffer (from eval-region).
1415 If the input is not from a buffer, they must be nil. */
1417 static void
1418 readevalloop (readcharfun, stream, sourcename, evalfun,
1419 printflag, unibyte, readfun, start, end)
1420 Lisp_Object readcharfun;
1421 FILE *stream;
1422 Lisp_Object sourcename;
1423 Lisp_Object (*evalfun) ();
1424 int printflag;
1425 Lisp_Object unibyte, readfun;
1426 Lisp_Object start, end;
1428 register int c;
1429 register Lisp_Object val;
1430 int count = SPECPDL_INDEX ();
1431 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1432 struct buffer *b = 0;
1433 int continue_reading_p;
1434 /* Nonzero if reading an entire buffer. */
1435 int whole_buffer = 0;
1436 /* 1 on the first time around. */
1437 int first_sexp = 1;
1439 if (MARKERP (readcharfun))
1441 if (NILP (start))
1442 start = readcharfun;
1445 if (BUFFERP (readcharfun))
1446 b = XBUFFER (readcharfun);
1447 else if (MARKERP (readcharfun))
1448 b = XMARKER (readcharfun)->buffer;
1450 /* We assume START is nil when input is not from a buffer. */
1451 if (! NILP (start) && !b)
1452 abort ();
1454 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
1455 specbind (Qcurrent_load_list, Qnil);
1456 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1457 load_convert_to_unibyte = !NILP (unibyte);
1459 readchar_backlog = -1;
1461 GCPRO4 (sourcename, readfun, start, end);
1463 /* Try to ensure sourcename is a truename, except whilst preloading. */
1464 if (NILP (Vpurify_flag)
1465 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1466 && !NILP (Ffboundp (Qfile_truename)))
1467 sourcename = call1 (Qfile_truename, sourcename) ;
1469 LOADHIST_ATTACH (sourcename);
1471 continue_reading_p = 1;
1472 while (continue_reading_p)
1474 int count1 = SPECPDL_INDEX ();
1476 if (b != 0 && NILP (b->name))
1477 error ("Reading from killed buffer");
1479 if (!NILP (start))
1481 /* Switch to the buffer we are reading from. */
1482 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1483 set_buffer_internal (b);
1485 /* Save point in it. */
1486 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1487 /* Save ZV in it. */
1488 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1489 /* Those get unbound after we read one expression. */
1491 /* Set point and ZV around stuff to be read. */
1492 Fgoto_char (start);
1493 if (!NILP (end))
1494 Fnarrow_to_region (make_number (BEGV), end);
1496 /* Just for cleanliness, convert END to a marker
1497 if it is an integer. */
1498 if (INTEGERP (end))
1499 end = Fpoint_max_marker ();
1502 /* On the first cycle, we can easily test here
1503 whether we are reading the whole buffer. */
1504 if (b && first_sexp)
1505 whole_buffer = (PT == BEG && ZV == Z);
1507 instream = stream;
1508 read_next:
1509 c = READCHAR;
1510 if (c == ';')
1512 while ((c = READCHAR) != '\n' && c != -1);
1513 goto read_next;
1515 if (c < 0)
1517 unbind_to (count1, Qnil);
1518 break;
1521 /* Ignore whitespace here, so we can detect eof. */
1522 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1523 || c == 0x8a0) /* NBSP */
1524 goto read_next;
1526 if (!NILP (Vpurify_flag) && c == '(')
1528 record_unwind_protect (unreadpure, Qnil);
1529 val = read_list (-1, readcharfun);
1531 else
1533 UNREAD (c);
1534 read_objects = Qnil;
1535 if (!NILP (readfun))
1537 val = call1 (readfun, readcharfun);
1539 /* If READCHARFUN has set point to ZV, we should
1540 stop reading, even if the form read sets point
1541 to a different value when evaluated. */
1542 if (BUFFERP (readcharfun))
1544 struct buffer *b = XBUFFER (readcharfun);
1545 if (BUF_PT (b) == BUF_ZV (b))
1546 continue_reading_p = 0;
1549 else if (! NILP (Vload_read_function))
1550 val = call1 (Vload_read_function, readcharfun);
1551 else
1552 val = read_internal_start (readcharfun, Qnil, Qnil);
1555 if (!NILP (start) && continue_reading_p)
1556 start = Fpoint_marker ();
1558 /* Restore saved point and BEGV. */
1559 unbind_to (count1, Qnil);
1561 /* Now eval what we just read. */
1562 val = (*evalfun) (val);
1564 if (printflag)
1566 Vvalues = Fcons (val, Vvalues);
1567 if (EQ (Vstandard_output, Qt))
1568 Fprin1 (val, Qnil);
1569 else
1570 Fprint (val, Qnil);
1573 first_sexp = 0;
1576 build_load_history (sourcename,
1577 stream || whole_buffer);
1579 UNGCPRO;
1581 unbind_to (count, Qnil);
1584 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1585 doc: /* Execute the current buffer as Lisp code.
1586 Programs can pass two arguments, BUFFER and PRINTFLAG.
1587 BUFFER is the buffer to evaluate (nil means use current buffer).
1588 PRINTFLAG controls printing of output:
1589 A value of nil means discard it; anything else is stream for print.
1591 If the optional third argument FILENAME is non-nil,
1592 it specifies the file name to use for `load-history'.
1593 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'
1594 for this invocation.
1596 The optional fifth argument DO-ALLOW-PRINT, if non-nil, specifies that
1597 `print' and related functions should work normally even if PRINTFLAG is nil.
1599 This function preserves the position of point. */)
1600 (buffer, printflag, filename, unibyte, do_allow_print)
1601 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1603 int count = SPECPDL_INDEX ();
1604 Lisp_Object tem, buf;
1606 if (NILP (buffer))
1607 buf = Fcurrent_buffer ();
1608 else
1609 buf = Fget_buffer (buffer);
1610 if (NILP (buf))
1611 error ("No such buffer");
1613 if (NILP (printflag) && NILP (do_allow_print))
1614 tem = Qsymbolp;
1615 else
1616 tem = printflag;
1618 if (NILP (filename))
1619 filename = XBUFFER (buf)->filename;
1621 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1622 specbind (Qstandard_output, tem);
1623 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1624 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1625 readevalloop (buf, 0, filename, Feval,
1626 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
1627 unbind_to (count, Qnil);
1629 return Qnil;
1632 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1633 doc: /* Execute the region as Lisp code.
1634 When called from programs, expects two arguments,
1635 giving starting and ending indices in the current buffer
1636 of the text to be executed.
1637 Programs can pass third argument PRINTFLAG which controls output:
1638 A value of nil means discard it; anything else is stream for printing it.
1639 Also the fourth argument READ-FUNCTION, if non-nil, is used
1640 instead of `read' to read each expression. It gets one argument
1641 which is the input stream for reading characters.
1643 This function does not move point. */)
1644 (start, end, printflag, read_function)
1645 Lisp_Object start, end, printflag, read_function;
1647 int count = SPECPDL_INDEX ();
1648 Lisp_Object tem, cbuf;
1650 cbuf = Fcurrent_buffer ();
1652 if (NILP (printflag))
1653 tem = Qsymbolp;
1654 else
1655 tem = printflag;
1656 specbind (Qstandard_output, tem);
1657 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
1659 /* readevalloop calls functions which check the type of start and end. */
1660 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1661 !NILP (printflag), Qnil, read_function,
1662 start, end);
1664 return unbind_to (count, Qnil);
1668 DEFUN ("read", Fread, Sread, 0, 1, 0,
1669 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1670 If STREAM is nil, use the value of `standard-input' (which see).
1671 STREAM or the value of `standard-input' may be:
1672 a buffer (read from point and advance it)
1673 a marker (read from where it points and advance it)
1674 a function (call it with no arguments for each character,
1675 call it with a char as argument to push a char back)
1676 a string (takes text from string, starting at the beginning)
1677 t (read text line using minibuffer and use it, or read from
1678 standard input in batch mode). */)
1679 (stream)
1680 Lisp_Object stream;
1682 if (NILP (stream))
1683 stream = Vstandard_input;
1684 if (EQ (stream, Qt))
1685 stream = Qread_char;
1686 if (EQ (stream, Qread_char))
1687 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1689 return read_internal_start (stream, Qnil, Qnil);
1692 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1693 doc: /* Read one Lisp expression which is represented as text by STRING.
1694 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
1695 START and END optionally delimit a substring of STRING from which to read;
1696 they default to 0 and (length STRING) respectively. */)
1697 (string, start, end)
1698 Lisp_Object string, start, end;
1700 Lisp_Object ret;
1701 CHECK_STRING (string);
1702 /* read_internal_start sets read_from_string_index. */
1703 ret = read_internal_start (string, start, end);
1704 return Fcons (ret, make_number (read_from_string_index));
1707 /* Function to set up the global context we need in toplevel read
1708 calls. */
1709 static Lisp_Object
1710 read_internal_start (stream, start, end)
1711 Lisp_Object stream;
1712 Lisp_Object start; /* Only used when stream is a string. */
1713 Lisp_Object end; /* Only used when stream is a string. */
1715 Lisp_Object retval;
1717 readchar_backlog = -1;
1718 readchar_count = 0;
1719 new_backquote_flag = 0;
1720 read_objects = Qnil;
1721 if (EQ (Vread_with_symbol_positions, Qt)
1722 || EQ (Vread_with_symbol_positions, stream))
1723 Vread_symbol_positions_list = Qnil;
1725 if (STRINGP (stream))
1727 int startval, endval;
1728 if (NILP (end))
1729 endval = SCHARS (stream);
1730 else
1732 CHECK_NUMBER (end);
1733 endval = XINT (end);
1734 if (endval < 0 || endval > SCHARS (stream))
1735 args_out_of_range (stream, end);
1738 if (NILP (start))
1739 startval = 0;
1740 else
1742 CHECK_NUMBER (start);
1743 startval = XINT (start);
1744 if (startval < 0 || startval > endval)
1745 args_out_of_range (stream, start);
1747 read_from_string_index = startval;
1748 read_from_string_index_byte = string_char_to_byte (stream, startval);
1749 read_from_string_limit = endval;
1752 retval = read0 (stream);
1753 if (EQ (Vread_with_symbol_positions, Qt)
1754 || EQ (Vread_with_symbol_positions, stream))
1755 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
1756 return retval;
1760 /* Signal Qinvalid_read_syntax error.
1761 S is error string of length N (if > 0) */
1763 static void
1764 invalid_syntax (s, n)
1765 const char *s;
1766 int n;
1768 if (!n)
1769 n = strlen (s);
1770 xsignal1 (Qinvalid_read_syntax, make_string (s, n));
1774 /* Use this for recursive reads, in contexts where internal tokens
1775 are not allowed. */
1777 static Lisp_Object
1778 read0 (readcharfun)
1779 Lisp_Object readcharfun;
1781 register Lisp_Object val;
1782 int c;
1784 val = read1 (readcharfun, &c, 0);
1785 if (!c)
1786 return val;
1788 xsignal1 (Qinvalid_read_syntax,
1789 Fmake_string (make_number (1), make_number (c)));
1792 static int read_buffer_size;
1793 static char *read_buffer;
1795 /* Read multibyte form and return it as a character. C is a first
1796 byte of multibyte form, and rest of them are read from
1797 READCHARFUN. */
1799 static int
1800 read_multibyte (c, readcharfun)
1801 register int c;
1802 Lisp_Object readcharfun;
1804 /* We need the actual character code of this multibyte
1805 characters. */
1806 unsigned char str[MAX_MULTIBYTE_LENGTH];
1807 int len = 0;
1808 int bytes;
1810 if (c < 0)
1811 return c;
1813 str[len++] = c;
1814 while ((c = READCHAR) >= 0xA0
1815 && len < MAX_MULTIBYTE_LENGTH)
1817 str[len++] = c;
1818 readchar_count--;
1820 UNREAD (c);
1821 if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
1822 return STRING_CHAR (str, len);
1823 /* The byte sequence is not valid as multibyte. Unread all bytes
1824 but the first one, and return the first byte. */
1825 while (--len > 0)
1826 UNREAD (str[len]);
1827 return str[0];
1830 /* Read a \-escape sequence, assuming we already read the `\'.
1831 If the escape sequence forces unibyte, store 1 into *BYTEREP.
1832 If the escape sequence forces multibyte, store 2 into *BYTEREP.
1833 Otherwise store 0 into *BYTEREP. */
1835 static int
1836 read_escape (readcharfun, stringp, byterep)
1837 Lisp_Object readcharfun;
1838 int stringp;
1839 int *byterep;
1841 register int c = READCHAR;
1842 /* \u allows up to four hex digits, \U up to eight. Default to the
1843 behaviour for \u, and change this value in the case that \U is seen. */
1844 int unicode_hex_count = 4;
1846 *byterep = 0;
1848 switch (c)
1850 case -1:
1851 end_of_file_error ();
1853 case 'a':
1854 return '\007';
1855 case 'b':
1856 return '\b';
1857 case 'd':
1858 return 0177;
1859 case 'e':
1860 return 033;
1861 case 'f':
1862 return '\f';
1863 case 'n':
1864 return '\n';
1865 case 'r':
1866 return '\r';
1867 case 't':
1868 return '\t';
1869 case 'v':
1870 return '\v';
1871 case '\n':
1872 return -1;
1873 case ' ':
1874 if (stringp)
1875 return -1;
1876 return ' ';
1878 case 'M':
1879 c = READCHAR;
1880 if (c != '-')
1881 error ("Invalid escape character syntax");
1882 c = READCHAR;
1883 if (c == '\\')
1884 c = read_escape (readcharfun, 0, byterep);
1885 return c | meta_modifier;
1887 case 'S':
1888 c = READCHAR;
1889 if (c != '-')
1890 error ("Invalid escape character syntax");
1891 c = READCHAR;
1892 if (c == '\\')
1893 c = read_escape (readcharfun, 0, byterep);
1894 return c | shift_modifier;
1896 case 'H':
1897 c = READCHAR;
1898 if (c != '-')
1899 error ("Invalid escape character syntax");
1900 c = READCHAR;
1901 if (c == '\\')
1902 c = read_escape (readcharfun, 0, byterep);
1903 return c | hyper_modifier;
1905 case 'A':
1906 c = READCHAR;
1907 if (c != '-')
1908 error ("Invalid escape character syntax");
1909 c = READCHAR;
1910 if (c == '\\')
1911 c = read_escape (readcharfun, 0, byterep);
1912 return c | alt_modifier;
1914 case 's':
1915 c = READCHAR;
1916 if (stringp || c != '-')
1918 UNREAD (c);
1919 return ' ';
1921 c = READCHAR;
1922 if (c == '\\')
1923 c = read_escape (readcharfun, 0, byterep);
1924 return c | super_modifier;
1926 case 'C':
1927 c = READCHAR;
1928 if (c != '-')
1929 error ("Invalid escape character syntax");
1930 case '^':
1931 c = READCHAR;
1932 if (c == '\\')
1933 c = read_escape (readcharfun, 0, byterep);
1934 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1935 return 0177 | (c & CHAR_MODIFIER_MASK);
1936 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1937 return c | ctrl_modifier;
1938 /* ASCII control chars are made from letters (both cases),
1939 as well as the non-letters within 0100...0137. */
1940 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1941 return (c & (037 | ~0177));
1942 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1943 return (c & (037 | ~0177));
1944 else
1945 return c | ctrl_modifier;
1947 case '0':
1948 case '1':
1949 case '2':
1950 case '3':
1951 case '4':
1952 case '5':
1953 case '6':
1954 case '7':
1955 /* An octal escape, as in ANSI C. */
1957 register int i = c - '0';
1958 register int count = 0;
1959 while (++count < 3)
1961 if ((c = READCHAR) >= '0' && c <= '7')
1963 i *= 8;
1964 i += c - '0';
1966 else
1968 UNREAD (c);
1969 break;
1973 *byterep = 1;
1974 return i;
1977 case 'x':
1978 /* A hex escape, as in ANSI C. */
1980 int i = 0;
1981 while (1)
1983 c = READCHAR;
1984 if (c >= '0' && c <= '9')
1986 i *= 16;
1987 i += c - '0';
1989 else if ((c >= 'a' && c <= 'f')
1990 || (c >= 'A' && c <= 'F'))
1992 i *= 16;
1993 if (c >= 'a' && c <= 'f')
1994 i += c - 'a' + 10;
1995 else
1996 i += c - 'A' + 10;
1998 else
2000 UNREAD (c);
2001 break;
2005 *byterep = 2;
2006 return i;
2009 case 'U':
2010 /* Post-Unicode-2.0: Up to eight hex chars. */
2011 unicode_hex_count = 8;
2012 case 'u':
2014 /* A Unicode escape. We only permit them in strings and characters,
2015 not arbitrarily in the source code, as in some other languages. */
2017 int i = 0;
2018 int count = 0;
2019 Lisp_Object lisp_char;
2020 struct gcpro gcpro1;
2022 while (++count <= unicode_hex_count)
2024 c = READCHAR;
2025 /* isdigit and isalpha may be locale-specific, which we don't
2026 want. */
2027 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2028 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2029 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2030 else
2032 error ("Non-hex digit used for Unicode escape");
2033 break;
2037 GCPRO1 (readcharfun);
2038 lisp_char = call2 (intern ("decode-char"), intern ("ucs"),
2039 make_number (i));
2040 UNGCPRO;
2042 if (NILP (lisp_char))
2044 error ("Unsupported Unicode code point: U+%x", (unsigned)i);
2047 return XFASTINT (lisp_char);
2050 default:
2051 if (BASE_LEADING_CODE_P (c))
2052 c = read_multibyte (c, readcharfun);
2053 return c;
2057 /* Read an integer in radix RADIX using READCHARFUN to read
2058 characters. RADIX must be in the interval [2..36]; if it isn't, a
2059 read error is signaled . Value is the integer read. Signals an
2060 error if encountering invalid read syntax or if RADIX is out of
2061 range. */
2063 static Lisp_Object
2064 read_integer (readcharfun, radix)
2065 Lisp_Object readcharfun;
2066 int radix;
2068 int ndigits = 0, invalid_p, c, sign = 0;
2069 EMACS_INT number = 0;
2071 if (radix < 2 || radix > 36)
2072 invalid_p = 1;
2073 else
2075 number = ndigits = invalid_p = 0;
2076 sign = 1;
2078 c = READCHAR;
2079 if (c == '-')
2081 c = READCHAR;
2082 sign = -1;
2084 else if (c == '+')
2085 c = READCHAR;
2087 while (c >= 0)
2089 int digit;
2091 if (c >= '0' && c <= '9')
2092 digit = c - '0';
2093 else if (c >= 'a' && c <= 'z')
2094 digit = c - 'a' + 10;
2095 else if (c >= 'A' && c <= 'Z')
2096 digit = c - 'A' + 10;
2097 else
2099 UNREAD (c);
2100 break;
2103 if (digit < 0 || digit >= radix)
2104 invalid_p = 1;
2106 number = radix * number + digit;
2107 ++ndigits;
2108 c = READCHAR;
2112 if (ndigits == 0 || invalid_p)
2114 char buf[50];
2115 sprintf (buf, "integer, radix %d", radix);
2116 invalid_syntax (buf, 0);
2119 return make_number (sign * number);
2123 /* Convert unibyte text in read_buffer to multibyte.
2125 Initially, *P is a pointer after the end of the unibyte text, and
2126 the pointer *END points after the end of read_buffer.
2128 If read_buffer doesn't have enough room to hold the result
2129 of the conversion, reallocate it and adjust *P and *END.
2131 At the end, make *P point after the result of the conversion, and
2132 return in *NCHARS the number of characters in the converted
2133 text. */
2135 static void
2136 to_multibyte (p, end, nchars)
2137 char **p, **end;
2138 int *nchars;
2140 int nbytes;
2142 parse_str_as_multibyte (read_buffer, *p - read_buffer, &nbytes, nchars);
2143 if (read_buffer_size < 2 * nbytes)
2145 int offset = *p - read_buffer;
2146 read_buffer_size = 2 * max (read_buffer_size, nbytes);
2147 read_buffer = (char *) xrealloc (read_buffer, read_buffer_size);
2148 *p = read_buffer + offset;
2149 *end = read_buffer + read_buffer_size;
2152 if (nbytes != *nchars)
2153 nbytes = str_as_multibyte (read_buffer, read_buffer_size,
2154 *p - read_buffer, nchars);
2156 *p = read_buffer + nbytes;
2160 /* If the next token is ')' or ']' or '.', we store that character
2161 in *PCH and the return value is not interesting. Else, we store
2162 zero in *PCH and we read and return one lisp object.
2164 FIRST_IN_LIST is nonzero if this is the first element of a list. */
2166 static Lisp_Object
2167 read1 (readcharfun, pch, first_in_list)
2168 register Lisp_Object readcharfun;
2169 int *pch;
2170 int first_in_list;
2172 register int c;
2173 int uninterned_symbol = 0;
2175 *pch = 0;
2177 retry:
2179 c = READCHAR;
2180 if (c < 0)
2181 end_of_file_error ();
2183 switch (c)
2185 case '(':
2186 return read_list (0, readcharfun);
2188 case '[':
2189 return read_vector (readcharfun, 0);
2191 case ')':
2192 case ']':
2194 *pch = c;
2195 return Qnil;
2198 case '#':
2199 c = READCHAR;
2200 if (c == '^')
2202 c = READCHAR;
2203 if (c == '[')
2205 Lisp_Object tmp;
2206 tmp = read_vector (readcharfun, 0);
2207 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
2208 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
2209 error ("Invalid size char-table");
2210 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
2211 XCHAR_TABLE (tmp)->top = Qt;
2212 return tmp;
2214 else if (c == '^')
2216 c = READCHAR;
2217 if (c == '[')
2219 Lisp_Object tmp;
2220 tmp = read_vector (readcharfun, 0);
2221 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
2222 error ("Invalid size char-table");
2223 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
2224 XCHAR_TABLE (tmp)->top = Qnil;
2225 return tmp;
2227 invalid_syntax ("#^^", 3);
2229 invalid_syntax ("#^", 2);
2231 if (c == '&')
2233 Lisp_Object length;
2234 length = read1 (readcharfun, pch, first_in_list);
2235 c = READCHAR;
2236 if (c == '"')
2238 Lisp_Object tmp, val;
2239 int size_in_chars
2240 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2241 / BOOL_VECTOR_BITS_PER_CHAR);
2243 UNREAD (c);
2244 tmp = read1 (readcharfun, pch, first_in_list);
2245 if (size_in_chars != SCHARS (tmp)
2246 /* We used to print 1 char too many
2247 when the number of bits was a multiple of 8.
2248 Accept such input in case it came from an old version. */
2249 && ! (XFASTINT (length)
2250 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR))
2251 invalid_syntax ("#&...", 5);
2253 val = Fmake_bool_vector (length, Qnil);
2254 bcopy (SDATA (tmp), XBOOL_VECTOR (val)->data,
2255 size_in_chars);
2256 /* Clear the extraneous bits in the last byte. */
2257 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2258 XBOOL_VECTOR (val)->data[size_in_chars - 1]
2259 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2260 return val;
2262 invalid_syntax ("#&...", 5);
2264 if (c == '[')
2266 /* Accept compiled functions at read-time so that we don't have to
2267 build them using function calls. */
2268 Lisp_Object tmp;
2269 tmp = read_vector (readcharfun, 1);
2270 return Fmake_byte_code (XVECTOR (tmp)->size,
2271 XVECTOR (tmp)->contents);
2273 if (c == '(')
2275 Lisp_Object tmp;
2276 struct gcpro gcpro1;
2277 int ch;
2279 /* Read the string itself. */
2280 tmp = read1 (readcharfun, &ch, 0);
2281 if (ch != 0 || !STRINGP (tmp))
2282 invalid_syntax ("#", 1);
2283 GCPRO1 (tmp);
2284 /* Read the intervals and their properties. */
2285 while (1)
2287 Lisp_Object beg, end, plist;
2289 beg = read1 (readcharfun, &ch, 0);
2290 end = plist = Qnil;
2291 if (ch == ')')
2292 break;
2293 if (ch == 0)
2294 end = read1 (readcharfun, &ch, 0);
2295 if (ch == 0)
2296 plist = read1 (readcharfun, &ch, 0);
2297 if (ch)
2298 invalid_syntax ("Invalid string property list", 0);
2299 Fset_text_properties (beg, end, plist, tmp);
2301 UNGCPRO;
2302 return tmp;
2305 /* #@NUMBER is used to skip NUMBER following characters.
2306 That's used in .elc files to skip over doc strings
2307 and function definitions. */
2308 if (c == '@')
2310 int i, nskip = 0;
2312 /* Read a decimal integer. */
2313 while ((c = READCHAR) >= 0
2314 && c >= '0' && c <= '9')
2316 nskip *= 10;
2317 nskip += c - '0';
2319 if (c >= 0)
2320 UNREAD (c);
2322 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
2324 /* If we are supposed to force doc strings into core right now,
2325 record the last string that we skipped,
2326 and record where in the file it comes from. */
2328 /* But first exchange saved_doc_string
2329 with prev_saved_doc_string, so we save two strings. */
2331 char *temp = saved_doc_string;
2332 int temp_size = saved_doc_string_size;
2333 file_offset temp_pos = saved_doc_string_position;
2334 int temp_len = saved_doc_string_length;
2336 saved_doc_string = prev_saved_doc_string;
2337 saved_doc_string_size = prev_saved_doc_string_size;
2338 saved_doc_string_position = prev_saved_doc_string_position;
2339 saved_doc_string_length = prev_saved_doc_string_length;
2341 prev_saved_doc_string = temp;
2342 prev_saved_doc_string_size = temp_size;
2343 prev_saved_doc_string_position = temp_pos;
2344 prev_saved_doc_string_length = temp_len;
2347 if (saved_doc_string_size == 0)
2349 saved_doc_string_size = nskip + 100;
2350 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
2352 if (nskip > saved_doc_string_size)
2354 saved_doc_string_size = nskip + 100;
2355 saved_doc_string = (char *) xrealloc (saved_doc_string,
2356 saved_doc_string_size);
2359 saved_doc_string_position = file_tell (instream);
2361 /* Copy that many characters into saved_doc_string. */
2362 for (i = 0; i < nskip && c >= 0; i++)
2363 saved_doc_string[i] = c = READCHAR;
2365 saved_doc_string_length = i;
2367 else
2369 /* Skip that many characters. */
2370 for (i = 0; i < nskip && c >= 0; i++)
2371 c = READCHAR;
2374 goto retry;
2376 if (c == '!')
2378 /* #! appears at the beginning of an executable file.
2379 Skip the first line. */
2380 while (c != '\n' && c >= 0)
2381 c = READCHAR;
2382 goto retry;
2384 if (c == '$')
2385 return Vload_file_name;
2386 if (c == '\'')
2387 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2388 /* #:foo is the uninterned symbol named foo. */
2389 if (c == ':')
2391 uninterned_symbol = 1;
2392 c = READCHAR;
2393 goto default_label;
2395 /* Reader forms that can reuse previously read objects. */
2396 if (c >= '0' && c <= '9')
2398 int n = 0;
2399 Lisp_Object tem;
2401 /* Read a non-negative integer. */
2402 while (c >= '0' && c <= '9')
2404 n *= 10;
2405 n += c - '0';
2406 c = READCHAR;
2408 /* #n=object returns object, but associates it with n for #n#. */
2409 if (c == '=')
2411 /* Make a placeholder for #n# to use temporarily */
2412 Lisp_Object placeholder;
2413 Lisp_Object cell;
2415 placeholder = Fcons(Qnil, Qnil);
2416 cell = Fcons (make_number (n), placeholder);
2417 read_objects = Fcons (cell, read_objects);
2419 /* Read the object itself. */
2420 tem = read0 (readcharfun);
2422 /* Now put it everywhere the placeholder was... */
2423 substitute_object_in_subtree (tem, placeholder);
2425 /* ...and #n# will use the real value from now on. */
2426 Fsetcdr (cell, tem);
2428 return tem;
2430 /* #n# returns a previously read object. */
2431 if (c == '#')
2433 tem = Fassq (make_number (n), read_objects);
2434 if (CONSP (tem))
2435 return XCDR (tem);
2436 /* Fall through to error message. */
2438 else if (c == 'r' || c == 'R')
2439 return read_integer (readcharfun, n);
2441 /* Fall through to error message. */
2443 else if (c == 'x' || c == 'X')
2444 return read_integer (readcharfun, 16);
2445 else if (c == 'o' || c == 'O')
2446 return read_integer (readcharfun, 8);
2447 else if (c == 'b' || c == 'B')
2448 return read_integer (readcharfun, 2);
2450 UNREAD (c);
2451 invalid_syntax ("#", 1);
2453 case ';':
2454 while ((c = READCHAR) >= 0 && c != '\n');
2455 goto retry;
2457 case '\'':
2459 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2462 case '`':
2463 if (first_in_list)
2465 Vold_style_backquotes = Qt;
2466 goto default_label;
2468 else
2470 Lisp_Object value;
2472 new_backquote_flag++;
2473 value = read0 (readcharfun);
2474 new_backquote_flag--;
2476 return Fcons (Qbackquote, Fcons (value, Qnil));
2479 case ',':
2480 if (new_backquote_flag)
2482 Lisp_Object comma_type = Qnil;
2483 Lisp_Object value;
2484 int ch = READCHAR;
2486 if (ch == '@')
2487 comma_type = Qcomma_at;
2488 else if (ch == '.')
2489 comma_type = Qcomma_dot;
2490 else
2492 if (ch >= 0) UNREAD (ch);
2493 comma_type = Qcomma;
2496 new_backquote_flag--;
2497 value = read0 (readcharfun);
2498 new_backquote_flag++;
2499 return Fcons (comma_type, Fcons (value, Qnil));
2501 else
2503 Vold_style_backquotes = Qt;
2504 goto default_label;
2507 case '?':
2509 int discard;
2510 int next_char;
2511 int ok;
2513 c = READCHAR;
2514 if (c < 0)
2515 end_of_file_error ();
2517 /* Accept `single space' syntax like (list ? x) where the
2518 whitespace character is SPC or TAB.
2519 Other literal whitespace like NL, CR, and FF are not accepted,
2520 as there are well-established escape sequences for these. */
2521 if (c == ' ' || c == '\t')
2522 return make_number (c);
2524 if (c == '\\')
2525 c = read_escape (readcharfun, 0, &discard);
2526 else if (BASE_LEADING_CODE_P (c))
2527 c = read_multibyte (c, readcharfun);
2529 next_char = READCHAR;
2530 if (next_char == '.')
2532 /* Only a dotted-pair dot is valid after a char constant. */
2533 int next_next_char = READCHAR;
2534 UNREAD (next_next_char);
2536 ok = (next_next_char <= 040
2537 || (next_next_char < 0200
2538 && (index ("\"';([#?", next_next_char)
2539 || (!first_in_list && next_next_char == '`')
2540 || (new_backquote_flag && next_next_char == ','))));
2542 else
2544 ok = (next_char <= 040
2545 || (next_char < 0200
2546 && (index ("\"';()[]#?", next_char)
2547 || (!first_in_list && next_char == '`')
2548 || (new_backquote_flag && next_char == ','))));
2550 UNREAD (next_char);
2551 if (ok)
2552 return make_number (c);
2554 invalid_syntax ("?", 1);
2557 case '"':
2559 char *p = read_buffer;
2560 char *end = read_buffer + read_buffer_size;
2561 register int c;
2562 /* 1 if we saw an escape sequence specifying
2563 a multibyte character, or a multibyte character. */
2564 int force_multibyte = 0;
2565 /* 1 if we saw an escape sequence specifying
2566 a single-byte character. */
2567 int force_singlebyte = 0;
2568 /* 1 if read_buffer contains multibyte text now. */
2569 int is_multibyte = 0;
2570 int cancel = 0;
2571 int nchars = 0;
2573 while ((c = READCHAR) >= 0
2574 && c != '\"')
2576 if (end - p < MAX_MULTIBYTE_LENGTH)
2578 int offset = p - read_buffer;
2579 read_buffer = (char *) xrealloc (read_buffer,
2580 read_buffer_size *= 2);
2581 p = read_buffer + offset;
2582 end = read_buffer + read_buffer_size;
2585 if (c == '\\')
2587 int byterep;
2589 c = read_escape (readcharfun, 1, &byterep);
2591 /* C is -1 if \ newline has just been seen */
2592 if (c == -1)
2594 if (p == read_buffer)
2595 cancel = 1;
2596 continue;
2599 if (byterep == 1)
2600 force_singlebyte = 1;
2601 else if (byterep == 2)
2602 force_multibyte = 1;
2605 /* A character that must be multibyte forces multibyte. */
2606 if (! SINGLE_BYTE_CHAR_P (c & ~CHAR_MODIFIER_MASK))
2607 force_multibyte = 1;
2609 /* If we just discovered the need to be multibyte,
2610 convert the text accumulated thus far. */
2611 if (force_multibyte && ! is_multibyte)
2613 is_multibyte = 1;
2614 to_multibyte (&p, &end, &nchars);
2617 /* Allow `\C- ' and `\C-?'. */
2618 if (c == (CHAR_CTL | ' '))
2619 c = 0;
2620 else if (c == (CHAR_CTL | '?'))
2621 c = 127;
2623 if (c & CHAR_SHIFT)
2625 /* Shift modifier is valid only with [A-Za-z]. */
2626 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2627 c &= ~CHAR_SHIFT;
2628 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2629 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
2632 if (c & CHAR_META)
2633 /* Move the meta bit to the right place for a string. */
2634 c = (c & ~CHAR_META) | 0x80;
2635 if (c & CHAR_MODIFIER_MASK)
2636 error ("Invalid modifier in string");
2638 if (is_multibyte)
2639 p += CHAR_STRING (c, p);
2640 else
2641 *p++ = c;
2643 nchars++;
2646 if (c < 0)
2647 end_of_file_error ();
2649 /* If purifying, and string starts with \ newline,
2650 return zero instead. This is for doc strings
2651 that we are really going to find in etc/DOC.nn.nn */
2652 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2653 return make_number (0);
2655 if (is_multibyte || force_singlebyte)
2657 else if (load_convert_to_unibyte)
2659 Lisp_Object string;
2660 to_multibyte (&p, &end, &nchars);
2661 if (p - read_buffer != nchars)
2663 string = make_multibyte_string (read_buffer, nchars,
2664 p - read_buffer);
2665 return Fstring_make_unibyte (string);
2667 /* We can make a unibyte string directly. */
2668 is_multibyte = 0;
2670 else if (EQ (readcharfun, Qget_file_char)
2671 || EQ (readcharfun, Qlambda))
2673 /* Nowadays, reading directly from a file is used only for
2674 compiled Emacs Lisp files, and those always use the
2675 Emacs internal encoding. Meanwhile, Qlambda is used
2676 for reading dynamic byte code (compiled with
2677 byte-compile-dynamic = t). So make the string multibyte
2678 if the string contains any multibyte sequences.
2679 (to_multibyte is a no-op if not.) */
2680 to_multibyte (&p, &end, &nchars);
2681 is_multibyte = (p - read_buffer) != nchars;
2683 else
2684 /* In all other cases, if we read these bytes as
2685 separate characters, treat them as separate characters now. */
2688 /* We want readchar_count to be the number of characters, not
2689 bytes. Hence we adjust for multibyte characters in the
2690 string. ... But it doesn't seem to be necessary, because
2691 READCHAR *does* read multibyte characters from buffers. */
2692 /* readchar_count -= (p - read_buffer) - nchars; */
2693 if (read_pure)
2694 return make_pure_string (read_buffer, nchars, p - read_buffer,
2695 is_multibyte);
2696 return make_specified_string (read_buffer, nchars, p - read_buffer,
2697 is_multibyte);
2700 case '.':
2702 int next_char = READCHAR;
2703 UNREAD (next_char);
2705 if (next_char <= 040
2706 || (next_char < 0200
2707 && (index ("\"';([#?", next_char)
2708 || (!first_in_list && next_char == '`')
2709 || (new_backquote_flag && next_char == ','))))
2711 *pch = c;
2712 return Qnil;
2715 /* Otherwise, we fall through! Note that the atom-reading loop
2716 below will now loop at least once, assuring that we will not
2717 try to UNREAD two characters in a row. */
2719 default:
2720 default_label:
2721 if (c <= 040) goto retry;
2722 if (c == 0x8a0) /* NBSP */
2723 goto retry;
2725 char *p = read_buffer;
2726 int quoted = 0;
2729 char *end = read_buffer + read_buffer_size;
2731 while (c > 040
2732 && c != 0x8a0 /* NBSP */
2733 && (c >= 0200
2734 || (!index ("\"';()[]#", c)
2735 && !(!first_in_list && c == '`')
2736 && !(new_backquote_flag && c == ','))))
2738 if (end - p < MAX_MULTIBYTE_LENGTH)
2740 int offset = p - read_buffer;
2741 read_buffer = (char *) xrealloc (read_buffer,
2742 read_buffer_size *= 2);
2743 p = read_buffer + offset;
2744 end = read_buffer + read_buffer_size;
2747 if (c == '\\')
2749 c = READCHAR;
2750 if (c == -1)
2751 end_of_file_error ();
2752 quoted = 1;
2755 if (! SINGLE_BYTE_CHAR_P (c))
2756 p += CHAR_STRING (c, p);
2757 else
2758 *p++ = c;
2760 c = READCHAR;
2763 if (p == end)
2765 int offset = p - read_buffer;
2766 read_buffer = (char *) xrealloc (read_buffer,
2767 read_buffer_size *= 2);
2768 p = read_buffer + offset;
2769 end = read_buffer + read_buffer_size;
2771 *p = 0;
2772 if (c >= 0)
2773 UNREAD (c);
2776 if (!quoted && !uninterned_symbol)
2778 register char *p1;
2779 register Lisp_Object val;
2780 p1 = read_buffer;
2781 if (*p1 == '+' || *p1 == '-') p1++;
2782 /* Is it an integer? */
2783 if (p1 != p)
2785 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2786 /* Integers can have trailing decimal points. */
2787 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2788 if (p1 == p)
2789 /* It is an integer. */
2791 if (p1[-1] == '.')
2792 p1[-1] = '\0';
2793 if (sizeof (int) == sizeof (EMACS_INT))
2794 XSETINT (val, atoi (read_buffer));
2795 else if (sizeof (long) == sizeof (EMACS_INT))
2796 XSETINT (val, atol (read_buffer));
2797 else
2798 abort ();
2799 return val;
2802 if (isfloat_string (read_buffer))
2804 /* Compute NaN and infinities using 0.0 in a variable,
2805 to cope with compilers that think they are smarter
2806 than we are. */
2807 double zero = 0.0;
2809 double value;
2811 /* Negate the value ourselves. This treats 0, NaNs,
2812 and infinity properly on IEEE floating point hosts,
2813 and works around a common bug where atof ("-0.0")
2814 drops the sign. */
2815 int negative = read_buffer[0] == '-';
2817 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2818 returns 1, is if the input ends in e+INF or e+NaN. */
2819 switch (p[-1])
2821 case 'F':
2822 value = 1.0 / zero;
2823 break;
2824 case 'N':
2825 value = zero / zero;
2827 /* If that made a "negative" NaN, negate it. */
2830 int i;
2831 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
2833 u_data.d = value;
2834 u_minus_zero.d = - 0.0;
2835 for (i = 0; i < sizeof (double); i++)
2836 if (u_data.c[i] & u_minus_zero.c[i])
2838 value = - value;
2839 break;
2842 /* Now VALUE is a positive NaN. */
2843 break;
2844 default:
2845 value = atof (read_buffer + negative);
2846 break;
2849 return make_float (negative ? - value : value);
2853 Lisp_Object result = uninterned_symbol ? make_symbol (read_buffer)
2854 : intern (read_buffer);
2855 if (EQ (Vread_with_symbol_positions, Qt)
2856 || EQ (Vread_with_symbol_positions, readcharfun))
2857 Vread_symbol_positions_list =
2858 /* Kind of a hack; this will probably fail if characters
2859 in the symbol name were escaped. Not really a big
2860 deal, though. */
2861 Fcons (Fcons (result,
2862 make_number (readchar_count
2863 - XFASTINT (Flength (Fsymbol_name (result))))),
2864 Vread_symbol_positions_list);
2865 return result;
2872 /* List of nodes we've seen during substitute_object_in_subtree. */
2873 static Lisp_Object seen_list;
2875 static void
2876 substitute_object_in_subtree (object, placeholder)
2877 Lisp_Object object;
2878 Lisp_Object placeholder;
2880 Lisp_Object check_object;
2882 /* We haven't seen any objects when we start. */
2883 seen_list = Qnil;
2885 /* Make all the substitutions. */
2886 check_object
2887 = substitute_object_recurse (object, placeholder, object);
2889 /* Clear seen_list because we're done with it. */
2890 seen_list = Qnil;
2892 /* The returned object here is expected to always eq the
2893 original. */
2894 if (!EQ (check_object, object))
2895 error ("Unexpected mutation error in reader");
2898 /* Feval doesn't get called from here, so no gc protection is needed. */
2899 #define SUBSTITUTE(get_val, set_val) \
2901 Lisp_Object old_value = get_val; \
2902 Lisp_Object true_value \
2903 = substitute_object_recurse (object, placeholder,\
2904 old_value); \
2906 if (!EQ (old_value, true_value)) \
2908 set_val; \
2912 static Lisp_Object
2913 substitute_object_recurse (object, placeholder, subtree)
2914 Lisp_Object object;
2915 Lisp_Object placeholder;
2916 Lisp_Object subtree;
2918 /* If we find the placeholder, return the target object. */
2919 if (EQ (placeholder, subtree))
2920 return object;
2922 /* If we've been to this node before, don't explore it again. */
2923 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2924 return subtree;
2926 /* If this node can be the entry point to a cycle, remember that
2927 we've seen it. It can only be such an entry point if it was made
2928 by #n=, which means that we can find it as a value in
2929 read_objects. */
2930 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2931 seen_list = Fcons (subtree, seen_list);
2933 /* Recurse according to subtree's type.
2934 Every branch must return a Lisp_Object. */
2935 switch (XTYPE (subtree))
2937 case Lisp_Vectorlike:
2939 int i;
2940 int length = XINT (Flength(subtree));
2941 for (i = 0; i < length; i++)
2943 Lisp_Object idx = make_number (i);
2944 SUBSTITUTE (Faref (subtree, idx),
2945 Faset (subtree, idx, true_value));
2947 return subtree;
2950 case Lisp_Cons:
2952 SUBSTITUTE (Fcar_safe (subtree),
2953 Fsetcar (subtree, true_value));
2954 SUBSTITUTE (Fcdr_safe (subtree),
2955 Fsetcdr (subtree, true_value));
2956 return subtree;
2959 case Lisp_String:
2961 /* Check for text properties in each interval.
2962 substitute_in_interval contains part of the logic. */
2964 INTERVAL root_interval = STRING_INTERVALS (subtree);
2965 Lisp_Object arg = Fcons (object, placeholder);
2967 traverse_intervals_noorder (root_interval,
2968 &substitute_in_interval, arg);
2970 return subtree;
2973 /* Other types don't recurse any further. */
2974 default:
2975 return subtree;
2979 /* Helper function for substitute_object_recurse. */
2980 static void
2981 substitute_in_interval (interval, arg)
2982 INTERVAL interval;
2983 Lisp_Object arg;
2985 Lisp_Object object = Fcar (arg);
2986 Lisp_Object placeholder = Fcdr (arg);
2988 SUBSTITUTE(interval->plist, interval->plist = true_value);
2992 #define LEAD_INT 1
2993 #define DOT_CHAR 2
2994 #define TRAIL_INT 4
2995 #define E_CHAR 8
2996 #define EXP_INT 16
2999 isfloat_string (cp)
3000 register char *cp;
3002 register int state;
3004 char *start = cp;
3006 state = 0;
3007 if (*cp == '+' || *cp == '-')
3008 cp++;
3010 if (*cp >= '0' && *cp <= '9')
3012 state |= LEAD_INT;
3013 while (*cp >= '0' && *cp <= '9')
3014 cp++;
3016 if (*cp == '.')
3018 state |= DOT_CHAR;
3019 cp++;
3021 if (*cp >= '0' && *cp <= '9')
3023 state |= TRAIL_INT;
3024 while (*cp >= '0' && *cp <= '9')
3025 cp++;
3027 if (*cp == 'e' || *cp == 'E')
3029 state |= E_CHAR;
3030 cp++;
3031 if (*cp == '+' || *cp == '-')
3032 cp++;
3035 if (*cp >= '0' && *cp <= '9')
3037 state |= EXP_INT;
3038 while (*cp >= '0' && *cp <= '9')
3039 cp++;
3041 else if (cp == start)
3043 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3045 state |= EXP_INT;
3046 cp += 3;
3048 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3050 state |= EXP_INT;
3051 cp += 3;
3054 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
3055 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
3056 || state == (DOT_CHAR|TRAIL_INT)
3057 || state == (LEAD_INT|E_CHAR|EXP_INT)
3058 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
3059 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
3063 static Lisp_Object
3064 read_vector (readcharfun, bytecodeflag)
3065 Lisp_Object readcharfun;
3066 int bytecodeflag;
3068 register int i;
3069 register int size;
3070 register Lisp_Object *ptr;
3071 register Lisp_Object tem, item, vector;
3072 register struct Lisp_Cons *otem;
3073 Lisp_Object len;
3075 tem = read_list (1, readcharfun);
3076 len = Flength (tem);
3077 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
3079 size = XVECTOR (vector)->size;
3080 ptr = XVECTOR (vector)->contents;
3081 for (i = 0; i < size; i++)
3083 item = Fcar (tem);
3084 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3085 bytecode object, the docstring containing the bytecode and
3086 constants values must be treated as unibyte and passed to
3087 Fread, to get the actual bytecode string and constants vector. */
3088 if (bytecodeflag && load_force_doc_strings)
3090 if (i == COMPILED_BYTECODE)
3092 if (!STRINGP (item))
3093 error ("Invalid byte code");
3095 /* Delay handling the bytecode slot until we know whether
3096 it is lazily-loaded (we can tell by whether the
3097 constants slot is nil). */
3098 ptr[COMPILED_CONSTANTS] = item;
3099 item = Qnil;
3101 else if (i == COMPILED_CONSTANTS)
3103 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3105 if (NILP (item))
3107 /* Coerce string to unibyte (like string-as-unibyte,
3108 but without generating extra garbage and
3109 guaranteeing no change in the contents). */
3110 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3111 STRING_SET_UNIBYTE (bytestr);
3113 item = Fread (bytestr);
3114 if (!CONSP (item))
3115 error ("Invalid byte code");
3117 otem = XCONS (item);
3118 bytestr = XCAR (item);
3119 item = XCDR (item);
3120 free_cons (otem);
3123 /* Now handle the bytecode slot. */
3124 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
3127 ptr[i] = read_pure ? Fpurecopy (item) : item;
3128 otem = XCONS (tem);
3129 tem = Fcdr (tem);
3130 free_cons (otem);
3132 return vector;
3135 /* FLAG = 1 means check for ] to terminate rather than ) and .
3136 FLAG = -1 means check for starting with defun
3137 and make structure pure. */
3139 static Lisp_Object
3140 read_list (flag, readcharfun)
3141 int flag;
3142 register Lisp_Object readcharfun;
3144 /* -1 means check next element for defun,
3145 0 means don't check,
3146 1 means already checked and found defun. */
3147 int defunflag = flag < 0 ? -1 : 0;
3148 Lisp_Object val, tail;
3149 register Lisp_Object elt, tem;
3150 struct gcpro gcpro1, gcpro2;
3151 /* 0 is the normal case.
3152 1 means this list is a doc reference; replace it with the number 0.
3153 2 means this list is a doc reference; replace it with the doc string. */
3154 int doc_reference = 0;
3156 /* Initialize this to 1 if we are reading a list. */
3157 int first_in_list = flag <= 0;
3159 val = Qnil;
3160 tail = Qnil;
3162 while (1)
3164 int ch;
3165 GCPRO2 (val, tail);
3166 elt = read1 (readcharfun, &ch, first_in_list);
3167 UNGCPRO;
3169 first_in_list = 0;
3171 /* While building, if the list starts with #$, treat it specially. */
3172 if (EQ (elt, Vload_file_name)
3173 && ! NILP (elt)
3174 && !NILP (Vpurify_flag))
3176 if (NILP (Vdoc_file_name))
3177 /* We have not yet called Snarf-documentation, so assume
3178 this file is described in the DOC-MM.NN file
3179 and Snarf-documentation will fill in the right value later.
3180 For now, replace the whole list with 0. */
3181 doc_reference = 1;
3182 else
3183 /* We have already called Snarf-documentation, so make a relative
3184 file name for this file, so it can be found properly
3185 in the installed Lisp directory.
3186 We don't use Fexpand_file_name because that would make
3187 the directory absolute now. */
3188 elt = concat2 (build_string ("../lisp/"),
3189 Ffile_name_nondirectory (elt));
3191 else if (EQ (elt, Vload_file_name)
3192 && ! NILP (elt)
3193 && load_force_doc_strings)
3194 doc_reference = 2;
3196 if (ch)
3198 if (flag > 0)
3200 if (ch == ']')
3201 return val;
3202 invalid_syntax (") or . in a vector", 18);
3204 if (ch == ')')
3205 return val;
3206 if (ch == '.')
3208 GCPRO2 (val, tail);
3209 if (!NILP (tail))
3210 XSETCDR (tail, read0 (readcharfun));
3211 else
3212 val = read0 (readcharfun);
3213 read1 (readcharfun, &ch, 0);
3214 UNGCPRO;
3215 if (ch == ')')
3217 if (doc_reference == 1)
3218 return make_number (0);
3219 if (doc_reference == 2)
3221 /* Get a doc string from the file we are loading.
3222 If it's in saved_doc_string, get it from there. */
3223 int pos = XINT (XCDR (val));
3224 /* Position is negative for user variables. */
3225 if (pos < 0) pos = -pos;
3226 if (pos >= saved_doc_string_position
3227 && pos < (saved_doc_string_position
3228 + saved_doc_string_length))
3230 int start = pos - saved_doc_string_position;
3231 int from, to;
3233 /* Process quoting with ^A,
3234 and find the end of the string,
3235 which is marked with ^_ (037). */
3236 for (from = start, to = start;
3237 saved_doc_string[from] != 037;)
3239 int c = saved_doc_string[from++];
3240 if (c == 1)
3242 c = saved_doc_string[from++];
3243 if (c == 1)
3244 saved_doc_string[to++] = c;
3245 else if (c == '0')
3246 saved_doc_string[to++] = 0;
3247 else if (c == '_')
3248 saved_doc_string[to++] = 037;
3250 else
3251 saved_doc_string[to++] = c;
3254 return make_string (saved_doc_string + start,
3255 to - start);
3257 /* Look in prev_saved_doc_string the same way. */
3258 else if (pos >= prev_saved_doc_string_position
3259 && pos < (prev_saved_doc_string_position
3260 + prev_saved_doc_string_length))
3262 int start = pos - prev_saved_doc_string_position;
3263 int from, to;
3265 /* Process quoting with ^A,
3266 and find the end of the string,
3267 which is marked with ^_ (037). */
3268 for (from = start, to = start;
3269 prev_saved_doc_string[from] != 037;)
3271 int c = prev_saved_doc_string[from++];
3272 if (c == 1)
3274 c = prev_saved_doc_string[from++];
3275 if (c == 1)
3276 prev_saved_doc_string[to++] = c;
3277 else if (c == '0')
3278 prev_saved_doc_string[to++] = 0;
3279 else if (c == '_')
3280 prev_saved_doc_string[to++] = 037;
3282 else
3283 prev_saved_doc_string[to++] = c;
3286 return make_string (prev_saved_doc_string + start,
3287 to - start);
3289 else
3290 return get_doc_string (val, 0, 0);
3293 return val;
3295 invalid_syntax (". in wrong context", 18);
3297 invalid_syntax ("] in a list", 11);
3299 tem = (read_pure && flag <= 0
3300 ? pure_cons (elt, Qnil)
3301 : Fcons (elt, Qnil));
3302 if (!NILP (tail))
3303 XSETCDR (tail, tem);
3304 else
3305 val = tem;
3306 tail = tem;
3307 if (defunflag < 0)
3308 defunflag = EQ (elt, Qdefun);
3309 else if (defunflag > 0)
3310 read_pure = 1;
3314 Lisp_Object Vobarray;
3315 Lisp_Object initial_obarray;
3317 /* oblookup stores the bucket number here, for the sake of Funintern. */
3319 int oblookup_last_bucket_number;
3321 static int hash_string ();
3323 /* Get an error if OBARRAY is not an obarray.
3324 If it is one, return it. */
3326 Lisp_Object
3327 check_obarray (obarray)
3328 Lisp_Object obarray;
3330 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3332 /* If Vobarray is now invalid, force it to be valid. */
3333 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3334 wrong_type_argument (Qvectorp, obarray);
3336 return obarray;
3339 /* Intern the C string STR: return a symbol with that name,
3340 interned in the current obarray. */
3342 Lisp_Object
3343 intern (str)
3344 const char *str;
3346 Lisp_Object tem;
3347 int len = strlen (str);
3348 Lisp_Object obarray;
3350 obarray = Vobarray;
3351 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3352 obarray = check_obarray (obarray);
3353 tem = oblookup (obarray, str, len, len);
3354 if (SYMBOLP (tem))
3355 return tem;
3356 return Fintern (make_string (str, len), obarray);
3359 /* Create an uninterned symbol with name STR. */
3361 Lisp_Object
3362 make_symbol (str)
3363 char *str;
3365 int len = strlen (str);
3367 return Fmake_symbol ((!NILP (Vpurify_flag)
3368 ? make_pure_string (str, len, len, 0)
3369 : make_string (str, len)));
3372 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3373 doc: /* Return the canonical symbol whose name is STRING.
3374 If there is none, one is created by this function and returned.
3375 A second optional argument specifies the obarray to use;
3376 it defaults to the value of `obarray'. */)
3377 (string, obarray)
3378 Lisp_Object string, obarray;
3380 register Lisp_Object tem, sym, *ptr;
3382 if (NILP (obarray)) obarray = Vobarray;
3383 obarray = check_obarray (obarray);
3385 CHECK_STRING (string);
3387 tem = oblookup (obarray, SDATA (string),
3388 SCHARS (string),
3389 SBYTES (string));
3390 if (!INTEGERP (tem))
3391 return tem;
3393 if (!NILP (Vpurify_flag))
3394 string = Fpurecopy (string);
3395 sym = Fmake_symbol (string);
3397 if (EQ (obarray, initial_obarray))
3398 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3399 else
3400 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
3402 if ((SREF (string, 0) == ':')
3403 && EQ (obarray, initial_obarray))
3405 XSYMBOL (sym)->constant = 1;
3406 XSYMBOL (sym)->value = sym;
3409 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
3410 if (SYMBOLP (*ptr))
3411 XSYMBOL (sym)->next = XSYMBOL (*ptr);
3412 else
3413 XSYMBOL (sym)->next = 0;
3414 *ptr = sym;
3415 return sym;
3418 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3419 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3420 NAME may be a string or a symbol. If it is a symbol, that exact
3421 symbol is searched for.
3422 A second optional argument specifies the obarray to use;
3423 it defaults to the value of `obarray'. */)
3424 (name, obarray)
3425 Lisp_Object name, obarray;
3427 register Lisp_Object tem, string;
3429 if (NILP (obarray)) obarray = Vobarray;
3430 obarray = check_obarray (obarray);
3432 if (!SYMBOLP (name))
3434 CHECK_STRING (name);
3435 string = name;
3437 else
3438 string = SYMBOL_NAME (name);
3440 tem = oblookup (obarray, SDATA (string), SCHARS (string), SBYTES (string));
3441 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3442 return Qnil;
3443 else
3444 return tem;
3447 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3448 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3449 The value is t if a symbol was found and deleted, nil otherwise.
3450 NAME may be a string or a symbol. If it is a symbol, that symbol
3451 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3452 OBARRAY defaults to the value of the variable `obarray'. */)
3453 (name, obarray)
3454 Lisp_Object name, obarray;
3456 register Lisp_Object string, tem;
3457 int hash;
3459 if (NILP (obarray)) obarray = Vobarray;
3460 obarray = check_obarray (obarray);
3462 if (SYMBOLP (name))
3463 string = SYMBOL_NAME (name);
3464 else
3466 CHECK_STRING (name);
3467 string = name;
3470 tem = oblookup (obarray, SDATA (string),
3471 SCHARS (string),
3472 SBYTES (string));
3473 if (INTEGERP (tem))
3474 return Qnil;
3475 /* If arg was a symbol, don't delete anything but that symbol itself. */
3476 if (SYMBOLP (name) && !EQ (name, tem))
3477 return Qnil;
3479 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3480 XSYMBOL (tem)->constant = 0;
3481 XSYMBOL (tem)->indirect_variable = 0;
3483 hash = oblookup_last_bucket_number;
3485 if (EQ (XVECTOR (obarray)->contents[hash], tem))
3487 if (XSYMBOL (tem)->next)
3488 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3489 else
3490 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3492 else
3494 Lisp_Object tail, following;
3496 for (tail = XVECTOR (obarray)->contents[hash];
3497 XSYMBOL (tail)->next;
3498 tail = following)
3500 XSETSYMBOL (following, XSYMBOL (tail)->next);
3501 if (EQ (following, tem))
3503 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3504 break;
3509 return Qt;
3512 /* Return the symbol in OBARRAY whose names matches the string
3513 of SIZE characters (SIZE_BYTE bytes) at PTR.
3514 If there is no such symbol in OBARRAY, return nil.
3516 Also store the bucket number in oblookup_last_bucket_number. */
3518 Lisp_Object
3519 oblookup (obarray, ptr, size, size_byte)
3520 Lisp_Object obarray;
3521 register const char *ptr;
3522 int size, size_byte;
3524 int hash;
3525 int obsize;
3526 register Lisp_Object tail;
3527 Lisp_Object bucket, tem;
3529 if (!VECTORP (obarray)
3530 || (obsize = XVECTOR (obarray)->size) == 0)
3532 obarray = check_obarray (obarray);
3533 obsize = XVECTOR (obarray)->size;
3535 /* This is sometimes needed in the middle of GC. */
3536 obsize &= ~ARRAY_MARK_FLAG;
3537 /* Combining next two lines breaks VMS C 2.3. */
3538 hash = hash_string (ptr, size_byte);
3539 hash %= obsize;
3540 bucket = XVECTOR (obarray)->contents[hash];
3541 oblookup_last_bucket_number = hash;
3542 if (EQ (bucket, make_number (0)))
3544 else if (!SYMBOLP (bucket))
3545 error ("Bad data in guts of obarray"); /* Like CADR error message */
3546 else
3547 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3549 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3550 && SCHARS (SYMBOL_NAME (tail)) == size
3551 && !bcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
3552 return tail;
3553 else if (XSYMBOL (tail)->next == 0)
3554 break;
3556 XSETINT (tem, hash);
3557 return tem;
3560 static int
3561 hash_string (ptr, len)
3562 const unsigned char *ptr;
3563 int len;
3565 register const unsigned char *p = ptr;
3566 register const unsigned char *end = p + len;
3567 register unsigned char c;
3568 register int hash = 0;
3570 while (p != end)
3572 c = *p++;
3573 if (c >= 0140) c -= 40;
3574 hash = ((hash<<3) + (hash>>28) + c);
3576 return hash & 07777777777;
3579 void
3580 map_obarray (obarray, fn, arg)
3581 Lisp_Object obarray;
3582 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3583 Lisp_Object arg;
3585 register int i;
3586 register Lisp_Object tail;
3587 CHECK_VECTOR (obarray);
3588 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3590 tail = XVECTOR (obarray)->contents[i];
3591 if (SYMBOLP (tail))
3592 while (1)
3594 (*fn) (tail, arg);
3595 if (XSYMBOL (tail)->next == 0)
3596 break;
3597 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3602 void
3603 mapatoms_1 (sym, function)
3604 Lisp_Object sym, function;
3606 call1 (function, sym);
3609 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3610 doc: /* Call FUNCTION on every symbol in OBARRAY.
3611 OBARRAY defaults to the value of `obarray'. */)
3612 (function, obarray)
3613 Lisp_Object function, obarray;
3615 if (NILP (obarray)) obarray = Vobarray;
3616 obarray = check_obarray (obarray);
3618 map_obarray (obarray, mapatoms_1, function);
3619 return Qnil;
3622 #define OBARRAY_SIZE 1511
3624 void
3625 init_obarray ()
3627 Lisp_Object oblength;
3628 int hash;
3629 Lisp_Object *tem;
3631 XSETFASTINT (oblength, OBARRAY_SIZE);
3633 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3634 Vobarray = Fmake_vector (oblength, make_number (0));
3635 initial_obarray = Vobarray;
3636 staticpro (&initial_obarray);
3637 /* Intern nil in the obarray */
3638 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3639 XSYMBOL (Qnil)->constant = 1;
3641 /* These locals are to kludge around a pyramid compiler bug. */
3642 hash = hash_string ("nil", 3);
3643 /* Separate statement here to avoid VAXC bug. */
3644 hash %= OBARRAY_SIZE;
3645 tem = &XVECTOR (Vobarray)->contents[hash];
3646 *tem = Qnil;
3648 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3649 XSYMBOL (Qnil)->function = Qunbound;
3650 XSYMBOL (Qunbound)->value = Qunbound;
3651 XSYMBOL (Qunbound)->function = Qunbound;
3653 Qt = intern ("t");
3654 XSYMBOL (Qnil)->value = Qnil;
3655 XSYMBOL (Qnil)->plist = Qnil;
3656 XSYMBOL (Qt)->value = Qt;
3657 XSYMBOL (Qt)->constant = 1;
3659 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3660 Vpurify_flag = Qt;
3662 Qvariable_documentation = intern ("variable-documentation");
3663 staticpro (&Qvariable_documentation);
3665 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3666 read_buffer = (char *) xmalloc (read_buffer_size);
3669 void
3670 defsubr (sname)
3671 struct Lisp_Subr *sname;
3673 Lisp_Object sym;
3674 sym = intern (sname->symbol_name);
3675 XSETPVECTYPE (sname, PVEC_SUBR);
3676 XSETSUBR (XSYMBOL (sym)->function, sname);
3679 #ifdef NOTDEF /* use fset in subr.el now */
3680 void
3681 defalias (sname, string)
3682 struct Lisp_Subr *sname;
3683 char *string;
3685 Lisp_Object sym;
3686 sym = intern (string);
3687 XSETSUBR (XSYMBOL (sym)->function, sname);
3689 #endif /* NOTDEF */
3691 /* Define an "integer variable"; a symbol whose value is forwarded
3692 to a C variable of type int. Sample call:
3693 DEFVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
3694 void
3695 defvar_int (namestring, address)
3696 char *namestring;
3697 EMACS_INT *address;
3699 Lisp_Object sym, val;
3700 sym = intern (namestring);
3701 val = allocate_misc ();
3702 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3703 XINTFWD (val)->intvar = address;
3704 SET_SYMBOL_VALUE (sym, val);
3707 /* Similar but define a variable whose value is t if address contains 1,
3708 nil if address contains 0. */
3709 void
3710 defvar_bool (namestring, address)
3711 char *namestring;
3712 int *address;
3714 Lisp_Object sym, val;
3715 sym = intern (namestring);
3716 val = allocate_misc ();
3717 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3718 XBOOLFWD (val)->boolvar = address;
3719 SET_SYMBOL_VALUE (sym, val);
3720 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3723 /* Similar but define a variable whose value is the Lisp Object stored
3724 at address. Two versions: with and without gc-marking of the C
3725 variable. The nopro version is used when that variable will be
3726 gc-marked for some other reason, since marking the same slot twice
3727 can cause trouble with strings. */
3728 void
3729 defvar_lisp_nopro (namestring, address)
3730 char *namestring;
3731 Lisp_Object *address;
3733 Lisp_Object sym, val;
3734 sym = intern (namestring);
3735 val = allocate_misc ();
3736 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3737 XOBJFWD (val)->objvar = address;
3738 SET_SYMBOL_VALUE (sym, val);
3741 void
3742 defvar_lisp (namestring, address)
3743 char *namestring;
3744 Lisp_Object *address;
3746 defvar_lisp_nopro (namestring, address);
3747 staticpro (address);
3750 /* Similar but define a variable whose value is the Lisp Object stored
3751 at a particular offset in the current kboard object. */
3753 void
3754 defvar_kboard (namestring, offset)
3755 char *namestring;
3756 int offset;
3758 Lisp_Object sym, val;
3759 sym = intern (namestring);
3760 val = allocate_misc ();
3761 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3762 XKBOARD_OBJFWD (val)->offset = offset;
3763 SET_SYMBOL_VALUE (sym, val);
3766 /* Record the value of load-path used at the start of dumping
3767 so we can see if the site changed it later during dumping. */
3768 static Lisp_Object dump_path;
3770 void
3771 init_lread ()
3773 char *normal;
3774 int turn_off_warning = 0;
3776 /* Compute the default load-path. */
3777 #ifdef CANNOT_DUMP
3778 normal = PATH_LOADSEARCH;
3779 Vload_path = decode_env_path (0, normal);
3780 #else
3781 if (NILP (Vpurify_flag))
3782 normal = PATH_LOADSEARCH;
3783 else
3784 normal = PATH_DUMPLOADSEARCH;
3786 /* In a dumped Emacs, we normally have to reset the value of
3787 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3788 uses ../lisp, instead of the path of the installed elisp
3789 libraries. However, if it appears that Vload_path was changed
3790 from the default before dumping, don't override that value. */
3791 if (initialized)
3793 if (! NILP (Fequal (dump_path, Vload_path)))
3795 Vload_path = decode_env_path (0, normal);
3796 if (!NILP (Vinstallation_directory))
3798 Lisp_Object tem, tem1, sitelisp;
3800 /* Remove site-lisp dirs from path temporarily and store
3801 them in sitelisp, then conc them on at the end so
3802 they're always first in path. */
3803 sitelisp = Qnil;
3804 while (1)
3806 tem = Fcar (Vload_path);
3807 tem1 = Fstring_match (build_string ("site-lisp"),
3808 tem, Qnil);
3809 if (!NILP (tem1))
3811 Vload_path = Fcdr (Vload_path);
3812 sitelisp = Fcons (tem, sitelisp);
3814 else
3815 break;
3818 /* Add to the path the lisp subdir of the
3819 installation dir, if it exists. */
3820 tem = Fexpand_file_name (build_string ("lisp"),
3821 Vinstallation_directory);
3822 tem1 = Ffile_exists_p (tem);
3823 if (!NILP (tem1))
3825 if (NILP (Fmember (tem, Vload_path)))
3827 turn_off_warning = 1;
3828 Vload_path = Fcons (tem, Vload_path);
3831 else
3832 /* That dir doesn't exist, so add the build-time
3833 Lisp dirs instead. */
3834 Vload_path = nconc2 (Vload_path, dump_path);
3836 /* Add leim under the installation dir, if it exists. */
3837 tem = Fexpand_file_name (build_string ("leim"),
3838 Vinstallation_directory);
3839 tem1 = Ffile_exists_p (tem);
3840 if (!NILP (tem1))
3842 if (NILP (Fmember (tem, Vload_path)))
3843 Vload_path = Fcons (tem, Vload_path);
3846 /* Add site-lisp under the installation dir, if it exists. */
3847 tem = Fexpand_file_name (build_string ("site-lisp"),
3848 Vinstallation_directory);
3849 tem1 = Ffile_exists_p (tem);
3850 if (!NILP (tem1))
3852 if (NILP (Fmember (tem, Vload_path)))
3853 Vload_path = Fcons (tem, Vload_path);
3856 /* If Emacs was not built in the source directory,
3857 and it is run from where it was built, add to load-path
3858 the lisp, leim and site-lisp dirs under that directory. */
3860 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3862 Lisp_Object tem2;
3864 tem = Fexpand_file_name (build_string ("src/Makefile"),
3865 Vinstallation_directory);
3866 tem1 = Ffile_exists_p (tem);
3868 /* Don't be fooled if they moved the entire source tree
3869 AFTER dumping Emacs. If the build directory is indeed
3870 different from the source dir, src/Makefile.in and
3871 src/Makefile will not be found together. */
3872 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3873 Vinstallation_directory);
3874 tem2 = Ffile_exists_p (tem);
3875 if (!NILP (tem1) && NILP (tem2))
3877 tem = Fexpand_file_name (build_string ("lisp"),
3878 Vsource_directory);
3880 if (NILP (Fmember (tem, Vload_path)))
3881 Vload_path = Fcons (tem, Vload_path);
3883 tem = Fexpand_file_name (build_string ("leim"),
3884 Vsource_directory);
3886 if (NILP (Fmember (tem, Vload_path)))
3887 Vload_path = Fcons (tem, Vload_path);
3889 tem = Fexpand_file_name (build_string ("site-lisp"),
3890 Vsource_directory);
3892 if (NILP (Fmember (tem, Vload_path)))
3893 Vload_path = Fcons (tem, Vload_path);
3896 if (!NILP (sitelisp))
3897 Vload_path = nconc2 (Fnreverse (sitelisp), Vload_path);
3901 else
3903 /* NORMAL refers to the lisp dir in the source directory. */
3904 /* We used to add ../lisp at the front here, but
3905 that caused trouble because it was copied from dump_path
3906 into Vload_path, above, when Vinstallation_directory was non-nil.
3907 It should be unnecessary. */
3908 Vload_path = decode_env_path (0, normal);
3909 dump_path = Vload_path;
3911 #endif
3913 #if (!(defined(WINDOWSNT) || (defined(HAVE_CARBON))))
3914 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3915 almost never correct, thereby causing a warning to be printed out that
3916 confuses users. Since PATH_LOADSEARCH is always overridden by the
3917 EMACSLOADPATH environment variable below, disable the warning on NT.
3918 Also, when using the "self-contained" option for Carbon Emacs for MacOSX,
3919 the "standard" paths may not exist and would be overridden by
3920 EMACSLOADPATH as on NT. Since this depends on how the executable
3921 was build and packaged, turn off the warnings in general */
3923 /* Warn if dirs in the *standard* path don't exist. */
3924 if (!turn_off_warning)
3926 Lisp_Object path_tail;
3928 for (path_tail = Vload_path;
3929 !NILP (path_tail);
3930 path_tail = XCDR (path_tail))
3932 Lisp_Object dirfile;
3933 dirfile = Fcar (path_tail);
3934 if (STRINGP (dirfile))
3936 dirfile = Fdirectory_file_name (dirfile);
3937 if (access (SDATA (dirfile), 0) < 0)
3938 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3939 XCAR (path_tail));
3943 #endif /* !(WINDOWSNT || HAVE_CARBON) */
3945 /* If the EMACSLOADPATH environment variable is set, use its value.
3946 This doesn't apply if we're dumping. */
3947 #ifndef CANNOT_DUMP
3948 if (NILP (Vpurify_flag)
3949 && egetenv ("EMACSLOADPATH"))
3950 #endif
3951 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3953 Vvalues = Qnil;
3955 load_in_progress = 0;
3956 Vload_file_name = Qnil;
3958 load_descriptor_list = Qnil;
3960 Vstandard_input = Qt;
3961 Vloads_in_progress = Qnil;
3964 /* Print a warning, using format string FORMAT, that directory DIRNAME
3965 does not exist. Print it on stderr and put it in *Messages*. */
3967 void
3968 dir_warning (format, dirname)
3969 char *format;
3970 Lisp_Object dirname;
3972 char *buffer
3973 = (char *) alloca (SCHARS (dirname) + strlen (format) + 5);
3975 fprintf (stderr, format, SDATA (dirname));
3976 sprintf (buffer, format, SDATA (dirname));
3977 /* Don't log the warning before we've initialized!! */
3978 if (initialized)
3979 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3982 void
3983 syms_of_lread ()
3985 defsubr (&Sread);
3986 defsubr (&Sread_from_string);
3987 defsubr (&Sintern);
3988 defsubr (&Sintern_soft);
3989 defsubr (&Sunintern);
3990 defsubr (&Sget_load_suffixes);
3991 defsubr (&Sload);
3992 defsubr (&Seval_buffer);
3993 defsubr (&Seval_region);
3994 defsubr (&Sread_char);
3995 defsubr (&Sread_char_exclusive);
3996 defsubr (&Sread_event);
3997 defsubr (&Sget_file_char);
3998 defsubr (&Smapatoms);
3999 defsubr (&Slocate_file_internal);
4001 DEFVAR_LISP ("obarray", &Vobarray,
4002 doc: /* Symbol table for use by `intern' and `read'.
4003 It is a vector whose length ought to be prime for best results.
4004 The vector's contents don't make sense if examined from Lisp programs;
4005 to find all the symbols in an obarray, use `mapatoms'. */);
4007 DEFVAR_LISP ("values", &Vvalues,
4008 doc: /* List of values of all expressions which were read, evaluated and printed.
4009 Order is reverse chronological. */);
4011 DEFVAR_LISP ("standard-input", &Vstandard_input,
4012 doc: /* Stream for read to get input from.
4013 See documentation of `read' for possible values. */);
4014 Vstandard_input = Qt;
4016 DEFVAR_LISP ("read-with-symbol-positions", &Vread_with_symbol_positions,
4017 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4019 If this variable is a buffer, then only forms read from that buffer
4020 will be added to `read-symbol-positions-list'.
4021 If this variable is t, then all read forms will be added.
4022 The effect of all other values other than nil are not currently
4023 defined, although they may be in the future.
4025 The positions are relative to the last call to `read' or
4026 `read-from-string'. It is probably a bad idea to set this variable at
4027 the toplevel; bind it instead. */);
4028 Vread_with_symbol_positions = Qnil;
4030 DEFVAR_LISP ("read-symbol-positions-list", &Vread_symbol_positions_list,
4031 doc: /* A list mapping read symbols to their positions.
4032 This variable is modified during calls to `read' or
4033 `read-from-string', but only when `read-with-symbol-positions' is
4034 non-nil.
4036 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4037 CHAR-POSITION is an integer giving the offset of that occurrence of the
4038 symbol from the position where `read' or `read-from-string' started.
4040 Note that a symbol will appear multiple times in this list, if it was
4041 read multiple times. The list is in the same order as the symbols
4042 were read in. */);
4043 Vread_symbol_positions_list = Qnil;
4045 DEFVAR_LISP ("load-path", &Vload_path,
4046 doc: /* *List of directories to search for files to load.
4047 Each element is a string (directory name) or nil (try default directory).
4048 Initialized based on EMACSLOADPATH environment variable, if any,
4049 otherwise to default specified by file `epaths.h' when Emacs was built. */);
4051 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
4052 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4053 This list should not include the empty string.
4054 `load' and related functions try to append these suffixes, in order,
4055 to the specified file name if a Lisp suffix is allowed or required. */);
4056 Vload_suffixes = Fcons (build_string (".elc"),
4057 Fcons (build_string (".el"), Qnil));
4058 DEFVAR_LISP ("load-file-rep-suffixes", &Vload_file_rep_suffixes,
4059 doc: /* List of suffixes that indicate representations of \
4060 the same file.
4061 This list should normally start with the empty string.
4063 Enabling Auto Compression mode appends the suffixes in
4064 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4065 mode removes them again. `load' and related functions use this list to
4066 determine whether they should look for compressed versions of a file
4067 and, if so, which suffixes they should try to append to the file name
4068 in order to do so. However, if you want to customize which suffixes
4069 the loading functions recognize as compression suffixes, you should
4070 customize `jka-compr-load-suffixes' rather than the present variable. */);
4071 Vload_file_rep_suffixes = Fcons (empty_unibyte_string, Qnil);
4073 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
4074 doc: /* Non-nil if inside of `load'. */);
4076 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
4077 doc: /* An alist of expressions to be evalled when particular files are loaded.
4078 Each element looks like (REGEXP-OR-FEATURE FORMS...).
4080 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4081 a symbol \(a feature name).
4083 When `load' is run and the file-name argument matches an element's
4084 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4085 REGEXP-OR-FEATURE, the FORMS in the element are executed.
4087 An error in FORMS does not undo the load, but does prevent execution of
4088 the rest of the FORMS. */);
4089 Vafter_load_alist = Qnil;
4091 DEFVAR_LISP ("load-history", &Vload_history,
4092 doc: /* Alist mapping file names to symbols and features.
4093 Each alist element is a list that starts with a file name,
4094 except for one element (optional) that starts with nil and describes
4095 definitions evaluated from buffers not visiting files.
4097 The file name is absolute and is the true file name (i.e. it doesn't
4098 contain symbolic links) of the loaded file.
4100 The remaining elements of each list are symbols defined as variables
4101 and cons cells of the form `(provide . FEATURE)', `(require . FEATURE)',
4102 `(defun . FUNCTION)', `(autoload . SYMBOL)', `(defface . SYMBOL)'
4103 and `(t . SYMBOL)'. An element `(t . SYMBOL)' precedes an entry
4104 `(defun . FUNCTION)', and means that SYMBOL was an autoload before
4105 this file redefined it as a function.
4107 During preloading, the file name recorded is relative to the main Lisp
4108 directory. These file names are converted to absolute at startup. */);
4109 Vload_history = Qnil;
4111 DEFVAR_LISP ("load-file-name", &Vload_file_name,
4112 doc: /* Full name of file being loaded by `load'. */);
4113 Vload_file_name = Qnil;
4115 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
4116 doc: /* File name, including directory, of user's initialization file.
4117 If the file loaded had extension `.elc', and the corresponding source file
4118 exists, this variable contains the name of source file, suitable for use
4119 by functions like `custom-save-all' which edit the init file.
4120 While Emacs loads and evaluates the init file, value is the real name
4121 of the file, regardless of whether or not it has the `.elc' extension. */);
4122 Vuser_init_file = Qnil;
4124 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
4125 doc: /* Used for internal purposes by `load'. */);
4126 Vcurrent_load_list = Qnil;
4128 DEFVAR_LISP ("load-read-function", &Vload_read_function,
4129 doc: /* Function used by `load' and `eval-region' for reading expressions.
4130 The default is nil, which means use the function `read'. */);
4131 Vload_read_function = Qnil;
4133 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
4134 doc: /* Function called in `load' for loading an Emacs Lisp source file.
4135 This function is for doing code conversion before reading the source file.
4136 If nil, loading is done without any code conversion.
4137 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
4138 FULLNAME is the full name of FILE.
4139 See `load' for the meaning of the remaining arguments. */);
4140 Vload_source_file_function = Qnil;
4142 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
4143 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4144 This is useful when the file being loaded is a temporary copy. */);
4145 load_force_doc_strings = 0;
4147 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
4148 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4149 This is normally bound by `load' and `eval-buffer' to control `read',
4150 and is not meant for users to change. */);
4151 load_convert_to_unibyte = 0;
4153 DEFVAR_LISP ("source-directory", &Vsource_directory,
4154 doc: /* Directory in which Emacs sources were found when Emacs was built.
4155 You cannot count on them to still be there! */);
4156 Vsource_directory
4157 = Fexpand_file_name (build_string ("../"),
4158 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
4160 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
4161 doc: /* List of files that were preloaded (when dumping Emacs). */);
4162 Vpreloaded_file_list = Qnil;
4164 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
4165 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4166 Vbyte_boolean_vars = Qnil;
4168 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
4169 doc: /* Non-nil means load dangerous compiled Lisp files.
4170 Some versions of XEmacs use different byte codes than Emacs. These
4171 incompatible byte codes can make Emacs crash when it tries to execute
4172 them. */);
4173 load_dangerous_libraries = 0;
4175 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
4176 doc: /* Regular expression matching safe to load compiled Lisp files.
4177 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4178 from the file, and matches them against this regular expression.
4179 When the regular expression matches, the file is considered to be safe
4180 to load. See also `load-dangerous-libraries'. */);
4181 Vbytecomp_version_regexp
4182 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4184 DEFVAR_LISP ("eval-buffer-list", &Veval_buffer_list,
4185 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4186 Veval_buffer_list = Qnil;
4188 DEFVAR_LISP ("old-style-backquotes", &Vold_style_backquotes,
4189 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4190 Vold_style_backquotes = Qnil;
4191 Qold_style_backquotes = intern ("old-style-backquotes");
4192 staticpro (&Qold_style_backquotes);
4194 /* Vsource_directory was initialized in init_lread. */
4196 load_descriptor_list = Qnil;
4197 staticpro (&load_descriptor_list);
4199 Qcurrent_load_list = intern ("current-load-list");
4200 staticpro (&Qcurrent_load_list);
4202 Qstandard_input = intern ("standard-input");
4203 staticpro (&Qstandard_input);
4205 Qread_char = intern ("read-char");
4206 staticpro (&Qread_char);
4208 Qget_file_char = intern ("get-file-char");
4209 staticpro (&Qget_file_char);
4211 Qbackquote = intern ("`");
4212 staticpro (&Qbackquote);
4213 Qcomma = intern (",");
4214 staticpro (&Qcomma);
4215 Qcomma_at = intern (",@");
4216 staticpro (&Qcomma_at);
4217 Qcomma_dot = intern (",.");
4218 staticpro (&Qcomma_dot);
4220 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
4221 staticpro (&Qinhibit_file_name_operation);
4223 Qascii_character = intern ("ascii-character");
4224 staticpro (&Qascii_character);
4226 Qfunction = intern ("function");
4227 staticpro (&Qfunction);
4229 Qload = intern ("load");
4230 staticpro (&Qload);
4232 Qload_file_name = intern ("load-file-name");
4233 staticpro (&Qload_file_name);
4235 Qeval_buffer_list = intern ("eval-buffer-list");
4236 staticpro (&Qeval_buffer_list);
4238 Qfile_truename = intern ("file-truename");
4239 staticpro (&Qfile_truename) ;
4241 Qdo_after_load_evaluation = intern ("do-after-load-evaluation");
4242 staticpro (&Qdo_after_load_evaluation) ;
4244 staticpro (&dump_path);
4246 staticpro (&read_objects);
4247 read_objects = Qnil;
4248 staticpro (&seen_list);
4249 seen_list = Qnil;
4251 Vloads_in_progress = Qnil;
4252 staticpro (&Vloads_in_progress);
4255 /* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d
4256 (do not change this comment) */