Change defvar_int def and vars to use EMACS_INT instead of just int.
[bpt/emacs.git] / src / lread.c
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <errno.h>
29 #include "lisp.h"
30 #include "intervals.h"
31 #include "buffer.h"
32 #include "charset.h"
33 #include <epaths.h>
34 #include "commands.h"
35 #include "keyboard.h"
36 #include "termhooks.h"
37
38 #ifdef lint
39 #include <sys/inode.h>
40 #endif /* lint */
41
42 #ifdef MSDOS
43 #if __DJGPP__ < 2
44 #include <unistd.h> /* to get X_OK */
45 #endif
46 #include "msdos.h"
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #ifndef X_OK
54 #define X_OK 01
55 #endif
56
57 #include <math.h>
58
59 #ifdef HAVE_SETLOCALE
60 #include <locale.h>
61 #endif /* HAVE_SETLOCALE */
62
63 #ifndef O_RDONLY
64 #define O_RDONLY 0
65 #endif
66
67 #ifdef HAVE_FSEEKO
68 #define file_offset off_t
69 #define file_tell ftello
70 #else
71 #define file_offset long
72 #define file_tell ftell
73 #endif
74
75 #ifndef USE_CRT_DLL
76 extern int errno;
77 #endif
78
79 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
80 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
81 Lisp_Object Qascii_character, Qload, Qload_file_name;
82 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
83 Lisp_Object Qinhibit_file_name_operation;
84
85 extern Lisp_Object Qevent_symbol_element_mask;
86 extern Lisp_Object Qfile_exists_p;
87
88 /* non-zero if inside `load' */
89 int load_in_progress;
90
91 /* Directory in which the sources were found. */
92 Lisp_Object Vsource_directory;
93
94 /* Search path and suffixes for files to be loaded. */
95 Lisp_Object Vload_path, Vload_suffixes, default_suffixes;
96
97 /* File name of user's init file. */
98 Lisp_Object Vuser_init_file;
99
100 /* This is the user-visible association list that maps features to
101 lists of defs in their load files. */
102 Lisp_Object Vload_history;
103
104 /* This is used to build the load history. */
105 Lisp_Object Vcurrent_load_list;
106
107 /* List of files that were preloaded. */
108 Lisp_Object Vpreloaded_file_list;
109
110 /* Name of file actually being read by `load'. */
111 Lisp_Object Vload_file_name;
112
113 /* Function to use for reading, in `load' and friends. */
114 Lisp_Object Vload_read_function;
115
116 /* The association list of objects read with the #n=object form.
117 Each member of the list has the form (n . object), and is used to
118 look up the object for the corresponding #n# construct.
119 It must be set to nil before all top-level calls to read0. */
120 Lisp_Object read_objects;
121
122 /* Nonzero means load should forcibly load all dynamic doc strings. */
123 static int load_force_doc_strings;
124
125 /* Nonzero means read should convert strings to unibyte. */
126 static int load_convert_to_unibyte;
127
128 /* Function to use for loading an Emacs lisp source file (not
129 compiled) instead of readevalloop. */
130 Lisp_Object Vload_source_file_function;
131
132 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
133 Lisp_Object Vbyte_boolean_vars;
134
135 /* List of descriptors now open for Fload. */
136 static Lisp_Object load_descriptor_list;
137
138 /* File for get_file_char to read from. Use by load. */
139 static FILE *instream;
140
141 /* When nonzero, read conses in pure space */
142 static int read_pure;
143
144 /* For use within read-from-string (this reader is non-reentrant!!) */
145 static int read_from_string_index;
146 static int read_from_string_index_byte;
147 static int read_from_string_limit;
148
149 /* Number of bytes left to read in the buffer character
150 that `readchar' has already advanced over. */
151 static int readchar_backlog;
152
153 /* This contains the last string skipped with #@. */
154 static char *saved_doc_string;
155 /* Length of buffer allocated in saved_doc_string. */
156 static int saved_doc_string_size;
157 /* Length of actual data in saved_doc_string. */
158 static int saved_doc_string_length;
159 /* This is the file position that string came from. */
160 static file_offset saved_doc_string_position;
161
162 /* This contains the previous string skipped with #@.
163 We copy it from saved_doc_string when a new string
164 is put in saved_doc_string. */
165 static char *prev_saved_doc_string;
166 /* Length of buffer allocated in prev_saved_doc_string. */
167 static int prev_saved_doc_string_size;
168 /* Length of actual data in prev_saved_doc_string. */
169 static int prev_saved_doc_string_length;
170 /* This is the file position that string came from. */
171 static file_offset prev_saved_doc_string_position;
172
173 /* Nonzero means inside a new-style backquote
174 with no surrounding parentheses.
175 Fread initializes this to zero, so we need not specbind it
176 or worry about what happens to it when there is an error. */
177 static int new_backquote_flag;
178
179 /* A list of file names for files being loaded in Fload. Used to
180 check for recursive loads. */
181
182 static Lisp_Object Vloads_in_progress;
183
184 /* Non-zero means load dangerous compiled Lisp files. */
185
186 int load_dangerous_libraries;
187
188 /* A regular expression used to detect files compiled with Emacs. */
189
190 static Lisp_Object Vbytecomp_version_regexp;
191
192 static void to_multibyte P_ ((char **, char **, int *));
193 static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
194 Lisp_Object (*) (), int,
195 Lisp_Object, Lisp_Object));
196 static Lisp_Object load_unwind P_ ((Lisp_Object));
197 static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
198
199 \f
200 /* Handle unreading and rereading of characters.
201 Write READCHAR to read a character,
202 UNREAD(c) to unread c to be read again.
203
204 These macros actually read/unread a byte code, multibyte characters
205 are not handled here. The caller should manage them if necessary.
206 */
207
208 #define READCHAR readchar (readcharfun)
209 #define UNREAD(c) unreadchar (readcharfun, c)
210
211 static int
212 readchar (readcharfun)
213 Lisp_Object readcharfun;
214 {
215 Lisp_Object tem;
216 register int c;
217
218 if (BUFFERP (readcharfun))
219 {
220 register struct buffer *inbuffer = XBUFFER (readcharfun);
221
222 int pt_byte = BUF_PT_BYTE (inbuffer);
223 int orig_pt_byte = pt_byte;
224
225 if (readchar_backlog > 0)
226 /* We get the address of the byte just passed,
227 which is the last byte of the character.
228 The other bytes in this character are consecutive with it,
229 because the gap can't be in the middle of a character. */
230 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
231 - --readchar_backlog);
232
233 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
234 return -1;
235
236 readchar_backlog = -1;
237
238 if (! NILP (inbuffer->enable_multibyte_characters))
239 {
240 /* Fetch the character code from the buffer. */
241 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
242 BUF_INC_POS (inbuffer, pt_byte);
243 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
244 }
245 else
246 {
247 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
248 pt_byte++;
249 }
250 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
251
252 return c;
253 }
254 if (MARKERP (readcharfun))
255 {
256 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
257
258 int bytepos = marker_byte_position (readcharfun);
259 int orig_bytepos = bytepos;
260
261 if (readchar_backlog > 0)
262 /* We get the address of the byte just passed,
263 which is the last byte of the character.
264 The other bytes in this character are consecutive with it,
265 because the gap can't be in the middle of a character. */
266 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
267 - --readchar_backlog);
268
269 if (bytepos >= BUF_ZV_BYTE (inbuffer))
270 return -1;
271
272 readchar_backlog = -1;
273
274 if (! NILP (inbuffer->enable_multibyte_characters))
275 {
276 /* Fetch the character code from the buffer. */
277 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
278 BUF_INC_POS (inbuffer, bytepos);
279 c = STRING_CHAR (p, bytepos - orig_bytepos);
280 }
281 else
282 {
283 c = BUF_FETCH_BYTE (inbuffer, bytepos);
284 bytepos++;
285 }
286
287 XMARKER (readcharfun)->bytepos = bytepos;
288 XMARKER (readcharfun)->charpos++;
289
290 return c;
291 }
292
293 if (EQ (readcharfun, Qlambda))
294 return read_bytecode_char (0);
295
296 if (EQ (readcharfun, Qget_file_char))
297 {
298 c = getc (instream);
299 #ifdef EINTR
300 /* Interrupted reads have been observed while reading over the network */
301 while (c == EOF && ferror (instream) && errno == EINTR)
302 {
303 clearerr (instream);
304 c = getc (instream);
305 }
306 #endif
307 return c;
308 }
309
310 if (STRINGP (readcharfun))
311 {
312 if (read_from_string_index >= read_from_string_limit)
313 c = -1;
314 else
315 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
316 read_from_string_index,
317 read_from_string_index_byte);
318
319 return c;
320 }
321
322 tem = call0 (readcharfun);
323
324 if (NILP (tem))
325 return -1;
326 return XINT (tem);
327 }
328
329 /* Unread the character C in the way appropriate for the stream READCHARFUN.
330 If the stream is a user function, call it with the char as argument. */
331
332 static void
333 unreadchar (readcharfun, c)
334 Lisp_Object readcharfun;
335 int c;
336 {
337 if (c == -1)
338 /* Don't back up the pointer if we're unreading the end-of-input mark,
339 since readchar didn't advance it when we read it. */
340 ;
341 else if (BUFFERP (readcharfun))
342 {
343 struct buffer *b = XBUFFER (readcharfun);
344 int bytepos = BUF_PT_BYTE (b);
345
346 if (readchar_backlog >= 0)
347 readchar_backlog++;
348 else
349 {
350 BUF_PT (b)--;
351 if (! NILP (b->enable_multibyte_characters))
352 BUF_DEC_POS (b, bytepos);
353 else
354 bytepos--;
355
356 BUF_PT_BYTE (b) = bytepos;
357 }
358 }
359 else if (MARKERP (readcharfun))
360 {
361 struct buffer *b = XMARKER (readcharfun)->buffer;
362 int bytepos = XMARKER (readcharfun)->bytepos;
363
364 if (readchar_backlog >= 0)
365 readchar_backlog++;
366 else
367 {
368 XMARKER (readcharfun)->charpos--;
369 if (! NILP (b->enable_multibyte_characters))
370 BUF_DEC_POS (b, bytepos);
371 else
372 bytepos--;
373
374 XMARKER (readcharfun)->bytepos = bytepos;
375 }
376 }
377 else if (STRINGP (readcharfun))
378 {
379 read_from_string_index--;
380 read_from_string_index_byte
381 = string_char_to_byte (readcharfun, read_from_string_index);
382 }
383 else if (EQ (readcharfun, Qlambda))
384 read_bytecode_char (1);
385 else if (EQ (readcharfun, Qget_file_char))
386 ungetc (c, instream);
387 else
388 call1 (readcharfun, make_number (c));
389 }
390
391 static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
392 static int read_multibyte ();
393 static Lisp_Object substitute_object_recurse ();
394 static void substitute_object_in_subtree (), substitute_in_interval ();
395
396 \f
397 /* Get a character from the tty. */
398
399 extern Lisp_Object read_char ();
400
401 /* Read input events until we get one that's acceptable for our purposes.
402
403 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
404 until we get a character we like, and then stuffed into
405 unread_switch_frame.
406
407 If ASCII_REQUIRED is non-zero, we check function key events to see
408 if the unmodified version of the symbol has a Qascii_character
409 property, and use that character, if present.
410
411 If ERROR_NONASCII is non-zero, we signal an error if the input we
412 get isn't an ASCII character with modifiers. If it's zero but
413 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
414 character.
415
416 If INPUT_METHOD is nonzero, we invoke the current input method
417 if the character warrants that. */
418
419 Lisp_Object
420 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
421 input_method)
422 int no_switch_frame, ascii_required, error_nonascii, input_method;
423 {
424 register Lisp_Object val, delayed_switch_frame;
425
426 #ifdef HAVE_WINDOW_SYSTEM
427 if (display_hourglass_p)
428 cancel_hourglass ();
429 #endif
430
431 delayed_switch_frame = Qnil;
432
433 /* Read until we get an acceptable event. */
434 retry:
435 val = read_char (0, 0, 0,
436 (input_method ? Qnil : Qt),
437 0);
438
439 if (BUFFERP (val))
440 goto retry;
441
442 /* switch-frame events are put off until after the next ASCII
443 character. This is better than signaling an error just because
444 the last characters were typed to a separate minibuffer frame,
445 for example. Eventually, some code which can deal with
446 switch-frame events will read it and process it. */
447 if (no_switch_frame
448 && EVENT_HAS_PARAMETERS (val)
449 && EQ (EVENT_HEAD (val), Qswitch_frame))
450 {
451 delayed_switch_frame = val;
452 goto retry;
453 }
454
455 if (ascii_required)
456 {
457 /* Convert certain symbols to their ASCII equivalents. */
458 if (SYMBOLP (val))
459 {
460 Lisp_Object tem, tem1;
461 tem = Fget (val, Qevent_symbol_element_mask);
462 if (!NILP (tem))
463 {
464 tem1 = Fget (Fcar (tem), Qascii_character);
465 /* Merge this symbol's modifier bits
466 with the ASCII equivalent of its basic code. */
467 if (!NILP (tem1))
468 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
469 }
470 }
471
472 /* If we don't have a character now, deal with it appropriately. */
473 if (!INTEGERP (val))
474 {
475 if (error_nonascii)
476 {
477 Vunread_command_events = Fcons (val, Qnil);
478 error ("Non-character input-event");
479 }
480 else
481 goto retry;
482 }
483 }
484
485 if (! NILP (delayed_switch_frame))
486 unread_switch_frame = delayed_switch_frame;
487
488 #if 0
489
490 #ifdef HAVE_WINDOW_SYSTEM
491 if (display_hourglass_p)
492 start_hourglass ();
493 #endif
494
495 #endif
496
497 return val;
498 }
499
500 DEFUN ("read-char", Fread_char, Sread_char, 0, 2, 0,
501 doc: /* Read a character from the command input (keyboard or macro).
502 It is returned as a number.
503 If the user generates an event which is not a character (i.e. a mouse
504 click or function key event), `read-char' signals an error. As an
505 exception, switch-frame events are put off until non-ASCII events can
506 be read.
507 If you want to read non-character events, or ignore them, call
508 `read-event' or `read-char-exclusive' instead.
509
510 If the optional argument PROMPT is non-nil, display that as a prompt.
511 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
512 input method is turned on in the current buffer, that input method
513 is used for reading a character. */)
514 (prompt, inherit_input_method)
515 Lisp_Object prompt, inherit_input_method;
516 {
517 if (! NILP (prompt))
518 message_with_string ("%s", prompt, 0);
519 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method));
520 }
521
522 DEFUN ("read-event", Fread_event, Sread_event, 0, 2, 0,
523 doc: /* Read an event object from the input stream.
524 If the optional argument PROMPT is non-nil, display that as a prompt.
525 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
526 input method is turned on in the current buffer, that input method
527 is used for reading a character. */)
528 (prompt, inherit_input_method)
529 Lisp_Object prompt, inherit_input_method;
530 {
531 if (! NILP (prompt))
532 message_with_string ("%s", prompt, 0);
533 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method));
534 }
535
536 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 2, 0,
537 doc: /* Read a character from the command input (keyboard or macro).
538 It is returned as a number. Non-character events are ignored.
539
540 If the optional argument PROMPT is non-nil, display that as a prompt.
541 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
542 input method is turned on in the current buffer, that input method
543 is used for reading a character. */)
544 (prompt, inherit_input_method)
545 Lisp_Object prompt, inherit_input_method;
546 {
547 if (! NILP (prompt))
548 message_with_string ("%s", prompt, 0);
549 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method));
550 }
551
552 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
553 doc: /* Don't use this yourself. */)
554 ()
555 {
556 register Lisp_Object val;
557 XSETINT (val, getc (instream));
558 return val;
559 }
560
561
562 \f
563 /* Value is non-zero if the file asswociated with file descriptor FD
564 is a compiled Lisp file that's safe to load. Only files compiled
565 with Emacs are safe to load. Files compiled with XEmacs can lead
566 to a crash in Fbyte_code because of an incompatible change in the
567 byte compiler. */
568
569 static int
570 safe_to_load_p (fd)
571 int fd;
572 {
573 char buf[512];
574 int nbytes, i;
575 int safe_p = 1;
576
577 /* Read the first few bytes from the file, and look for a line
578 specifying the byte compiler version used. */
579 nbytes = emacs_read (fd, buf, sizeof buf - 1);
580 if (nbytes > 0)
581 {
582 buf[nbytes] = '\0';
583
584 /* Skip to the next newline, skipping over the initial `ELC'
585 with NUL bytes following it. */
586 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
587 ;
588
589 if (i < nbytes
590 && fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
591 buf + i) < 0)
592 safe_p = 0;
593 }
594
595 lseek (fd, 0, SEEK_SET);
596 return safe_p;
597 }
598
599
600 /* Callback for record_unwind_protect. Restore the old load list OLD,
601 after loading a file successfully. */
602
603 static Lisp_Object
604 record_load_unwind (old)
605 Lisp_Object old;
606 {
607 return Vloads_in_progress = old;
608 }
609
610
611 DEFUN ("load", Fload, Sload, 1, 5, 0,
612 doc: /* Execute a file of Lisp code named FILE.
613 First try FILE with `.elc' appended, then try with `.el',
614 then try FILE unmodified. Environment variable references in FILE
615 are replaced with their values by calling `substitute-in-file-name'.
616 This function searches the directories in `load-path'.
617 If optional second arg NOERROR is non-nil,
618 report no error if FILE doesn't exist.
619 Print messages at start and end of loading unless
620 optional third arg NOMESSAGE is non-nil.
621 If optional fourth arg NOSUFFIX is non-nil, don't try adding
622 suffixes `.elc' or `.el' to the specified name FILE.
623 If optional fifth arg MUST-SUFFIX is non-nil, insist on
624 the suffix `.elc' or `.el'; don't accept just FILE unless
625 it ends in one of those suffixes or includes a directory name.
626 Return t if file exists. */)
627 (file, noerror, nomessage, nosuffix, must_suffix)
628 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
629 {
630 register FILE *stream;
631 register int fd = -1;
632 register Lisp_Object lispstream;
633 int count = specpdl_ptr - specpdl;
634 Lisp_Object temp;
635 struct gcpro gcpro1;
636 Lisp_Object found;
637 /* 1 means we printed the ".el is newer" message. */
638 int newer = 0;
639 /* 1 means we are loading a compiled file. */
640 int compiled = 0;
641 Lisp_Object handler;
642 int safe_p = 1;
643 char *fmode = "r";
644 #ifdef DOS_NT
645 fmode = "rt";
646 #endif /* DOS_NT */
647
648 CHECK_STRING (file);
649
650 /* If file name is magic, call the handler. */
651 /* This shouldn't be necessary any more now that `openp' handles it right.
652 handler = Ffind_file_name_handler (file, Qload);
653 if (!NILP (handler))
654 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
655
656 /* Do this after the handler to avoid
657 the need to gcpro noerror, nomessage and nosuffix.
658 (Below here, we care only whether they are nil or not.)
659 The presence of this call is the result of a historical accident:
660 it used to be in every file-operations and when it got removed
661 everywhere, it accidentally stayed here. Since then, enough people
662 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
663 that it seemed risky to remove. */
664 file = Fsubstitute_in_file_name (file);
665
666 /* Avoid weird lossage with null string as arg,
667 since it would try to load a directory as a Lisp file */
668 if (XSTRING (file)->size > 0)
669 {
670 int size = STRING_BYTES (XSTRING (file));
671 Lisp_Object tmp[2];
672
673 GCPRO1 (file);
674
675 if (! NILP (must_suffix))
676 {
677 /* Don't insist on adding a suffix if FILE already ends with one. */
678 if (size > 3
679 && !strcmp (XSTRING (file)->data + size - 3, ".el"))
680 must_suffix = Qnil;
681 else if (size > 4
682 && !strcmp (XSTRING (file)->data + size - 4, ".elc"))
683 must_suffix = Qnil;
684 /* Don't insist on adding a suffix
685 if the argument includes a directory name. */
686 else if (! NILP (Ffile_name_directory (file)))
687 must_suffix = Qnil;
688 }
689
690 fd = openp (Vload_path, file,
691 (!NILP (nosuffix) ? Qnil
692 : !NILP (must_suffix) ? Vload_suffixes
693 : Fappend (2, (tmp[0] = Vload_suffixes,
694 tmp[1] = default_suffixes,
695 tmp))),
696 &found, 0);
697 UNGCPRO;
698 }
699
700 if (fd == -1)
701 {
702 if (NILP (noerror))
703 while (1)
704 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
705 Fcons (file, Qnil)));
706 else
707 return Qnil;
708 }
709
710 /* Tell startup.el whether or not we found the user's init file. */
711 if (EQ (Qt, Vuser_init_file))
712 Vuser_init_file = found;
713
714 /* If FD is -2, that means openp found a magic file. */
715 if (fd == -2)
716 {
717 if (NILP (Fequal (found, file)))
718 /* If FOUND is a different file name from FILE,
719 find its handler even if we have already inhibited
720 the `load' operation on FILE. */
721 handler = Ffind_file_name_handler (found, Qt);
722 else
723 handler = Ffind_file_name_handler (found, Qload);
724 if (! NILP (handler))
725 return call5 (handler, Qload, found, noerror, nomessage, Qt);
726 }
727
728 /* Check if we're stuck in a recursive load cycle.
729
730 2000-09-21: It's not possible to just check for the file loaded
731 being a member of Vloads_in_progress. This fails because of the
732 way the byte compiler currently works; `provide's are not
733 evaluted, see font-lock.el/jit-lock.el as an example. This
734 leads to a certain amount of ``normal'' recursion.
735
736 Also, just loading a file recursively is not always an error in
737 the general case; the second load may do something different. */
738 {
739 int count = 0;
740 Lisp_Object tem;
741 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
742 if (!NILP (Fequal (found, XCAR (tem))))
743 count++;
744 if (count > 3)
745 Fsignal (Qerror, Fcons (build_string ("Recursive load"),
746 Fcons (found, Vloads_in_progress)));
747 record_unwind_protect (record_load_unwind, Vloads_in_progress);
748 Vloads_in_progress = Fcons (found, Vloads_in_progress);
749 }
750
751 if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
752 ".elc", 4))
753 /* Load .elc files directly, but not when they are
754 remote and have no handler! */
755 {
756 if (fd != -2)
757 {
758 struct stat s1, s2;
759 int result;
760
761 if (!safe_to_load_p (fd))
762 {
763 safe_p = 0;
764 if (!load_dangerous_libraries)
765 error ("File `%s' was not compiled in Emacs",
766 XSTRING (found)->data);
767 else if (!NILP (nomessage))
768 message_with_string ("File `%s' not compiled in Emacs", found, 1);
769 }
770
771 compiled = 1;
772
773 #ifdef DOS_NT
774 fmode = "rb";
775 #endif /* DOS_NT */
776 stat ((char *)XSTRING (found)->data, &s1);
777 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
778 result = stat ((char *)XSTRING (found)->data, &s2);
779 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
780 {
781 /* Make the progress messages mention that source is newer. */
782 newer = 1;
783
784 /* If we won't print another message, mention this anyway. */
785 if (! NILP (nomessage))
786 message_with_string ("Source file `%s' newer than byte-compiled file",
787 found, 1);
788 }
789 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
790 }
791 }
792 else
793 {
794 /* We are loading a source file (*.el). */
795 if (!NILP (Vload_source_file_function))
796 {
797 Lisp_Object val;
798
799 if (fd >= 0)
800 emacs_close (fd);
801 val = call4 (Vload_source_file_function, found, file,
802 NILP (noerror) ? Qnil : Qt,
803 NILP (nomessage) ? Qnil : Qt);
804 return unbind_to (count, val);
805 }
806 }
807
808 #ifdef WINDOWSNT
809 emacs_close (fd);
810 stream = fopen ((char *) XSTRING (found)->data, fmode);
811 #else /* not WINDOWSNT */
812 stream = fdopen (fd, fmode);
813 #endif /* not WINDOWSNT */
814 if (stream == 0)
815 {
816 emacs_close (fd);
817 error ("Failure to create stdio stream for %s", XSTRING (file)->data);
818 }
819
820 if (! NILP (Vpurify_flag))
821 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
822
823 if (NILP (nomessage))
824 {
825 if (!safe_p)
826 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
827 file, 1);
828 else if (!compiled)
829 message_with_string ("Loading %s (source)...", file, 1);
830 else if (newer)
831 message_with_string ("Loading %s (compiled; note, source file is newer)...",
832 file, 1);
833 else /* The typical case; compiled file newer than source file. */
834 message_with_string ("Loading %s...", file, 1);
835 }
836
837 GCPRO1 (file);
838 lispstream = Fcons (Qnil, Qnil);
839 XSETCARFASTINT (lispstream, (EMACS_UINT)stream >> 16);
840 XSETCDRFASTINT (lispstream, (EMACS_UINT)stream & 0xffff);
841 record_unwind_protect (load_unwind, lispstream);
842 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
843 specbind (Qload_file_name, found);
844 specbind (Qinhibit_file_name_operation, Qnil);
845 load_descriptor_list
846 = Fcons (make_number (fileno (stream)), load_descriptor_list);
847 load_in_progress++;
848 readevalloop (Qget_file_char, stream, file, Feval, 0, Qnil, Qnil);
849 unbind_to (count, Qnil);
850
851 /* Run any load-hooks for this file. */
852 temp = Fassoc (file, Vafter_load_alist);
853 if (!NILP (temp))
854 Fprogn (Fcdr (temp));
855 UNGCPRO;
856
857 if (saved_doc_string)
858 free (saved_doc_string);
859 saved_doc_string = 0;
860 saved_doc_string_size = 0;
861
862 if (prev_saved_doc_string)
863 xfree (prev_saved_doc_string);
864 prev_saved_doc_string = 0;
865 prev_saved_doc_string_size = 0;
866
867 if (!noninteractive && NILP (nomessage))
868 {
869 if (!safe_p)
870 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
871 file, 1);
872 else if (!compiled)
873 message_with_string ("Loading %s (source)...done", file, 1);
874 else if (newer)
875 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
876 file, 1);
877 else /* The typical case; compiled file newer than source file. */
878 message_with_string ("Loading %s...done", file, 1);
879 }
880
881 return Qt;
882 }
883
884 static Lisp_Object
885 load_unwind (stream) /* used as unwind-protect function in load */
886 Lisp_Object stream;
887 {
888 fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16
889 | XFASTINT (XCDR (stream))));
890 if (--load_in_progress < 0) load_in_progress = 0;
891 return Qnil;
892 }
893
894 static Lisp_Object
895 load_descriptor_unwind (oldlist)
896 Lisp_Object oldlist;
897 {
898 load_descriptor_list = oldlist;
899 return Qnil;
900 }
901
902 /* Close all descriptors in use for Floads.
903 This is used when starting a subprocess. */
904
905 void
906 close_load_descs ()
907 {
908 #ifndef WINDOWSNT
909 Lisp_Object tail;
910 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
911 emacs_close (XFASTINT (XCAR (tail)));
912 #endif
913 }
914 \f
915 static int
916 complete_filename_p (pathname)
917 Lisp_Object pathname;
918 {
919 register unsigned char *s = XSTRING (pathname)->data;
920 return (IS_DIRECTORY_SEP (s[0])
921 || (XSTRING (pathname)->size > 2
922 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
923 #ifdef ALTOS
924 || *s == '@'
925 #endif
926 #ifdef VMS
927 || index (s, ':')
928 #endif /* VMS */
929 );
930 }
931
932 /* Search for a file whose name is STR, looking in directories
933 in the Lisp list PATH, and trying suffixes from SUFFIX.
934 On success, returns a file descriptor. On failure, returns -1.
935
936 SUFFIXES is a list of strings containing possible suffixes.
937 The empty suffix is automatically added iff the list is empty.
938
939 EXEC_ONLY nonzero means don't open the files,
940 just look for one that is executable. In this case,
941 returns 1 on success.
942
943 If STOREPTR is nonzero, it points to a slot where the name of
944 the file actually found should be stored as a Lisp string.
945 nil is stored there on failure.
946
947 If the file we find is remote, return -2
948 but store the found remote file name in *STOREPTR.
949 We do not check for remote files if EXEC_ONLY is nonzero. */
950
951 int
952 openp (path, str, suffixes, storeptr, exec_only)
953 Lisp_Object path, str;
954 Lisp_Object suffixes;
955 Lisp_Object *storeptr;
956 int exec_only;
957 {
958 register int fd;
959 int fn_size = 100;
960 char buf[100];
961 register char *fn = buf;
962 int absolute = 0;
963 int want_size;
964 Lisp_Object filename;
965 struct stat st;
966 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
967 Lisp_Object string, tail;
968 int max_suffix_len = 0;
969
970 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
971 {
972 CHECK_STRING_CAR (tail);
973 max_suffix_len = max (max_suffix_len,
974 STRING_BYTES (XSTRING (XCAR (tail))));
975 }
976
977 string = filename = Qnil;
978 GCPRO5 (str, string, filename, path, suffixes);
979
980 if (storeptr)
981 *storeptr = Qnil;
982
983 if (complete_filename_p (str))
984 absolute = 1;
985
986 for (; CONSP (path); path = XCDR (path))
987 {
988 filename = Fexpand_file_name (str, XCAR (path));
989 if (!complete_filename_p (filename))
990 /* If there are non-absolute elts in PATH (eg ".") */
991 /* Of course, this could conceivably lose if luser sets
992 default-directory to be something non-absolute... */
993 {
994 filename = Fexpand_file_name (filename, current_buffer->directory);
995 if (!complete_filename_p (filename))
996 /* Give up on this path element! */
997 continue;
998 }
999
1000 /* Calculate maximum size of any filename made from
1001 this path element/specified file name and any possible suffix. */
1002 want_size = max_suffix_len + STRING_BYTES (XSTRING (filename)) + 1;
1003 if (fn_size < want_size)
1004 fn = (char *) alloca (fn_size = 100 + want_size);
1005
1006 /* Loop over suffixes. */
1007 for (tail = NILP (suffixes) ? default_suffixes : suffixes;
1008 CONSP (tail); tail = XCDR (tail))
1009 {
1010 int lsuffix = STRING_BYTES (XSTRING (XCAR (tail)));
1011 Lisp_Object handler;
1012
1013 /* Concatenate path element/specified name with the suffix.
1014 If the directory starts with /:, remove that. */
1015 if (XSTRING (filename)->size > 2
1016 && XSTRING (filename)->data[0] == '/'
1017 && XSTRING (filename)->data[1] == ':')
1018 {
1019 strncpy (fn, XSTRING (filename)->data + 2,
1020 STRING_BYTES (XSTRING (filename)) - 2);
1021 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
1022 }
1023 else
1024 {
1025 strncpy (fn, XSTRING (filename)->data,
1026 STRING_BYTES (XSTRING (filename)));
1027 fn[STRING_BYTES (XSTRING (filename))] = 0;
1028 }
1029
1030 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
1031 strncat (fn, XSTRING (XCAR (tail))->data, lsuffix);
1032
1033 /* Check that the file exists and is not a directory. */
1034 /* We used to only check for handlers on non-absolute file names:
1035 if (absolute)
1036 handler = Qnil;
1037 else
1038 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1039 It's not clear why that was the case and it breaks things like
1040 (load "/bar.el") where the file is actually "/bar.el.gz". */
1041 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1042 if (!NILP (handler) && !exec_only)
1043 {
1044 int exists;
1045
1046 string = build_string (fn);
1047 exists = !NILP (Ffile_readable_p (string));
1048 if (exists && !NILP (Ffile_directory_p (build_string (fn))))
1049 exists = 0;
1050
1051 if (exists)
1052 {
1053 /* We succeeded; return this descriptor and filename. */
1054 if (storeptr)
1055 *storeptr = build_string (fn);
1056 UNGCPRO;
1057 return -2;
1058 }
1059 }
1060 else
1061 {
1062 int exists = (stat (fn, &st) >= 0
1063 && (st.st_mode & S_IFMT) != S_IFDIR);
1064 if (exists)
1065 {
1066 /* Check that we can access or open it. */
1067 if (exec_only)
1068 fd = (access (fn, X_OK) == 0) ? 1 : -1;
1069 else
1070 fd = emacs_open (fn, O_RDONLY, 0);
1071
1072 if (fd >= 0)
1073 {
1074 /* We succeeded; return this descriptor and filename. */
1075 if (storeptr)
1076 *storeptr = build_string (fn);
1077 UNGCPRO;
1078 return fd;
1079 }
1080 }
1081 }
1082 }
1083 if (absolute)
1084 break;
1085 }
1086
1087 UNGCPRO;
1088 return -1;
1089 }
1090
1091 \f
1092 /* Merge the list we've accumulated of globals from the current input source
1093 into the load_history variable. The details depend on whether
1094 the source has an associated file name or not. */
1095
1096 static void
1097 build_load_history (stream, source)
1098 FILE *stream;
1099 Lisp_Object source;
1100 {
1101 register Lisp_Object tail, prev, newelt;
1102 register Lisp_Object tem, tem2;
1103 register int foundit, loading;
1104
1105 loading = stream || !NARROWED;
1106
1107 tail = Vload_history;
1108 prev = Qnil;
1109 foundit = 0;
1110 while (!NILP (tail))
1111 {
1112 tem = Fcar (tail);
1113
1114 /* Find the feature's previous assoc list... */
1115 if (!NILP (Fequal (source, Fcar (tem))))
1116 {
1117 foundit = 1;
1118
1119 /* If we're loading, remove it. */
1120 if (loading)
1121 {
1122 if (NILP (prev))
1123 Vload_history = Fcdr (tail);
1124 else
1125 Fsetcdr (prev, Fcdr (tail));
1126 }
1127
1128 /* Otherwise, cons on new symbols that are not already members. */
1129 else
1130 {
1131 tem2 = Vcurrent_load_list;
1132
1133 while (CONSP (tem2))
1134 {
1135 newelt = Fcar (tem2);
1136
1137 if (NILP (Fmemq (newelt, tem)))
1138 Fsetcar (tail, Fcons (Fcar (tem),
1139 Fcons (newelt, Fcdr (tem))));
1140
1141 tem2 = Fcdr (tem2);
1142 QUIT;
1143 }
1144 }
1145 }
1146 else
1147 prev = tail;
1148 tail = Fcdr (tail);
1149 QUIT;
1150 }
1151
1152 /* If we're loading, cons the new assoc onto the front of load-history,
1153 the most-recently-loaded position. Also do this if we didn't find
1154 an existing member for the current source. */
1155 if (loading || !foundit)
1156 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1157 Vload_history);
1158 }
1159
1160 Lisp_Object
1161 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1162 Lisp_Object junk;
1163 {
1164 read_pure = 0;
1165 return Qnil;
1166 }
1167
1168 static Lisp_Object
1169 readevalloop_1 (old)
1170 Lisp_Object old;
1171 {
1172 load_convert_to_unibyte = ! NILP (old);
1173 return Qnil;
1174 }
1175
1176 /* Signal an `end-of-file' error, if possible with file name
1177 information. */
1178
1179 static void
1180 end_of_file_error ()
1181 {
1182 Lisp_Object data;
1183
1184 if (STRINGP (Vload_file_name))
1185 data = Fcons (Vload_file_name, Qnil);
1186 else
1187 data = Qnil;
1188
1189 Fsignal (Qend_of_file, data);
1190 }
1191
1192 /* UNIBYTE specifies how to set load_convert_to_unibyte
1193 for this invocation.
1194 READFUN, if non-nil, is used instead of `read'. */
1195
1196 static void
1197 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1198 Lisp_Object readcharfun;
1199 FILE *stream;
1200 Lisp_Object sourcename;
1201 Lisp_Object (*evalfun) ();
1202 int printflag;
1203 Lisp_Object unibyte, readfun;
1204 {
1205 register int c;
1206 register Lisp_Object val;
1207 int count = specpdl_ptr - specpdl;
1208 struct gcpro gcpro1;
1209 struct buffer *b = 0;
1210 int continue_reading_p;
1211
1212 if (BUFFERP (readcharfun))
1213 b = XBUFFER (readcharfun);
1214 else if (MARKERP (readcharfun))
1215 b = XMARKER (readcharfun)->buffer;
1216
1217 specbind (Qstandard_input, readcharfun);
1218 specbind (Qcurrent_load_list, Qnil);
1219 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1220 load_convert_to_unibyte = !NILP (unibyte);
1221
1222 readchar_backlog = -1;
1223
1224 GCPRO1 (sourcename);
1225
1226 LOADHIST_ATTACH (sourcename);
1227
1228 continue_reading_p = 1;
1229 while (continue_reading_p)
1230 {
1231 if (b != 0 && NILP (b->name))
1232 error ("Reading from killed buffer");
1233
1234 instream = stream;
1235 c = READCHAR;
1236 if (c == ';')
1237 {
1238 while ((c = READCHAR) != '\n' && c != -1);
1239 continue;
1240 }
1241 if (c < 0) break;
1242
1243 /* Ignore whitespace here, so we can detect eof. */
1244 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1245 continue;
1246
1247 if (!NILP (Vpurify_flag) && c == '(')
1248 {
1249 int count1 = specpdl_ptr - specpdl;
1250 record_unwind_protect (unreadpure, Qnil);
1251 val = read_list (-1, readcharfun);
1252 unbind_to (count1, Qnil);
1253 }
1254 else
1255 {
1256 UNREAD (c);
1257 read_objects = Qnil;
1258 if (!NILP (readfun))
1259 {
1260 val = call1 (readfun, readcharfun);
1261
1262 /* If READCHARFUN has set point to ZV, we should
1263 stop reading, even if the form read sets point
1264 to a different value when evaluated. */
1265 if (BUFFERP (readcharfun))
1266 {
1267 struct buffer *b = XBUFFER (readcharfun);
1268 if (BUF_PT (b) == BUF_ZV (b))
1269 continue_reading_p = 0;
1270 }
1271 }
1272 else if (! NILP (Vload_read_function))
1273 val = call1 (Vload_read_function, readcharfun);
1274 else
1275 val = read0 (readcharfun);
1276 }
1277
1278 val = (*evalfun) (val);
1279
1280 if (printflag)
1281 {
1282 Vvalues = Fcons (val, Vvalues);
1283 if (EQ (Vstandard_output, Qt))
1284 Fprin1 (val, Qnil);
1285 else
1286 Fprint (val, Qnil);
1287 }
1288 }
1289
1290 build_load_history (stream, sourcename);
1291 UNGCPRO;
1292
1293 unbind_to (count, Qnil);
1294 }
1295
1296 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1297 doc: /* Execute the current buffer as Lisp code.
1298 Programs can pass two arguments, BUFFER and PRINTFLAG.
1299 BUFFER is the buffer to evaluate (nil means use current buffer).
1300 PRINTFLAG controls printing of output:
1301 nil means discard it; anything else is stream for print.
1302
1303 If the optional third argument FILENAME is non-nil,
1304 it specifies the file name to use for `load-history'.
1305 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'
1306 for this invocation.
1307
1308 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that
1309 `print' and related functions should work normally even if PRINTFLAG is nil.
1310
1311 This function preserves the position of point. */)
1312 (buffer, printflag, filename, unibyte, do_allow_print)
1313 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1314 {
1315 int count = specpdl_ptr - specpdl;
1316 Lisp_Object tem, buf;
1317
1318 if (NILP (buffer))
1319 buf = Fcurrent_buffer ();
1320 else
1321 buf = Fget_buffer (buffer);
1322 if (NILP (buf))
1323 error ("No such buffer");
1324
1325 if (NILP (printflag) && NILP (do_allow_print))
1326 tem = Qsymbolp;
1327 else
1328 tem = printflag;
1329
1330 if (NILP (filename))
1331 filename = XBUFFER (buf)->filename;
1332
1333 specbind (Qstandard_output, tem);
1334 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1335 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1336 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1337 unbind_to (count, Qnil);
1338
1339 return Qnil;
1340 }
1341
1342 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1343 doc: /* Execute the region as Lisp code.
1344 When called from programs, expects two arguments,
1345 giving starting and ending indices in the current buffer
1346 of the text to be executed.
1347 Programs can pass third argument PRINTFLAG which controls output:
1348 nil means discard it; anything else is stream for printing it.
1349 Also the fourth argument READ-FUNCTION, if non-nil, is used
1350 instead of `read' to read each expression. It gets one argument
1351 which is the input stream for reading characters.
1352
1353 This function does not move point. */)
1354 (start, end, printflag, read_function)
1355 Lisp_Object start, end, printflag, read_function;
1356 {
1357 int count = specpdl_ptr - specpdl;
1358 Lisp_Object tem, cbuf;
1359
1360 cbuf = Fcurrent_buffer ();
1361
1362 if (NILP (printflag))
1363 tem = Qsymbolp;
1364 else
1365 tem = printflag;
1366 specbind (Qstandard_output, tem);
1367
1368 if (NILP (printflag))
1369 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1370 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1371
1372 /* This both uses start and checks its type. */
1373 Fgoto_char (start);
1374 Fnarrow_to_region (make_number (BEGV), end);
1375 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1376 !NILP (printflag), Qnil, read_function);
1377
1378 return unbind_to (count, Qnil);
1379 }
1380
1381 \f
1382 DEFUN ("read", Fread, Sread, 0, 1, 0,
1383 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1384 If STREAM is nil, use the value of `standard-input' (which see).
1385 STREAM or the value of `standard-input' may be:
1386 a buffer (read from point and advance it)
1387 a marker (read from where it points and advance it)
1388 a function (call it with no arguments for each character,
1389 call it with a char as argument to push a char back)
1390 a string (takes text from string, starting at the beginning)
1391 t (read text line using minibuffer and use it, or read from
1392 standard input in batch mode). */)
1393 (stream)
1394 Lisp_Object stream;
1395 {
1396 extern Lisp_Object Fread_minibuffer ();
1397
1398 if (NILP (stream))
1399 stream = Vstandard_input;
1400 if (EQ (stream, Qt))
1401 stream = Qread_char;
1402
1403 readchar_backlog = -1;
1404 new_backquote_flag = 0;
1405 read_objects = Qnil;
1406
1407 if (EQ (stream, Qread_char))
1408 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1409
1410 if (STRINGP (stream))
1411 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1412
1413 return read0 (stream);
1414 }
1415
1416 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1417 doc: /* Read one Lisp expression which is represented as text by STRING.
1418 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
1419 START and END optionally delimit a substring of STRING from which to read;
1420 they default to 0 and (length STRING) respectively. */)
1421 (string, start, end)
1422 Lisp_Object string, start, end;
1423 {
1424 int startval, endval;
1425 Lisp_Object tem;
1426
1427 CHECK_STRING (string);
1428
1429 if (NILP (end))
1430 endval = XSTRING (string)->size;
1431 else
1432 {
1433 CHECK_NUMBER (end);
1434 endval = XINT (end);
1435 if (endval < 0 || endval > XSTRING (string)->size)
1436 args_out_of_range (string, end);
1437 }
1438
1439 if (NILP (start))
1440 startval = 0;
1441 else
1442 {
1443 CHECK_NUMBER (start);
1444 startval = XINT (start);
1445 if (startval < 0 || startval > endval)
1446 args_out_of_range (string, start);
1447 }
1448
1449 read_from_string_index = startval;
1450 read_from_string_index_byte = string_char_to_byte (string, startval);
1451 read_from_string_limit = endval;
1452
1453 new_backquote_flag = 0;
1454 read_objects = Qnil;
1455
1456 tem = read0 (string);
1457 return Fcons (tem, make_number (read_from_string_index));
1458 }
1459 \f
1460 /* Use this for recursive reads, in contexts where internal tokens
1461 are not allowed. */
1462
1463 static Lisp_Object
1464 read0 (readcharfun)
1465 Lisp_Object readcharfun;
1466 {
1467 register Lisp_Object val;
1468 int c;
1469
1470 val = read1 (readcharfun, &c, 0);
1471 if (c)
1472 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1473 make_number (c)),
1474 Qnil));
1475
1476 return val;
1477 }
1478 \f
1479 static int read_buffer_size;
1480 static char *read_buffer;
1481
1482 /* Read multibyte form and return it as a character. C is a first
1483 byte of multibyte form, and rest of them are read from
1484 READCHARFUN. */
1485
1486 static int
1487 read_multibyte (c, readcharfun)
1488 register int c;
1489 Lisp_Object readcharfun;
1490 {
1491 /* We need the actual character code of this multibyte
1492 characters. */
1493 unsigned char str[MAX_MULTIBYTE_LENGTH];
1494 int len = 0;
1495 int bytes;
1496
1497 str[len++] = c;
1498 while ((c = READCHAR) >= 0xA0
1499 && len < MAX_MULTIBYTE_LENGTH)
1500 str[len++] = c;
1501 UNREAD (c);
1502 if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
1503 return STRING_CHAR (str, len);
1504 /* The byte sequence is not valid as multibyte. Unread all bytes
1505 but the first one, and return the first byte. */
1506 while (--len > 0)
1507 UNREAD (str[len]);
1508 return str[0];
1509 }
1510
1511 /* Read a \-escape sequence, assuming we already read the `\'.
1512 If the escape sequence forces unibyte, store 1 into *BYTEREP.
1513 If the escape sequence forces multibyte, store 2 into *BYTEREP.
1514 Otherwise store 0 into *BYTEREP. */
1515
1516 static int
1517 read_escape (readcharfun, stringp, byterep)
1518 Lisp_Object readcharfun;
1519 int stringp;
1520 int *byterep;
1521 {
1522 register int c = READCHAR;
1523
1524 *byterep = 0;
1525
1526 switch (c)
1527 {
1528 case -1:
1529 end_of_file_error ();
1530
1531 case 'a':
1532 return '\007';
1533 case 'b':
1534 return '\b';
1535 case 'd':
1536 return 0177;
1537 case 'e':
1538 return 033;
1539 case 'f':
1540 return '\f';
1541 case 'n':
1542 return '\n';
1543 case 'r':
1544 return '\r';
1545 case 't':
1546 return '\t';
1547 case 'v':
1548 return '\v';
1549 case '\n':
1550 return -1;
1551 case ' ':
1552 if (stringp)
1553 return -1;
1554 return ' ';
1555
1556 case 'M':
1557 c = READCHAR;
1558 if (c != '-')
1559 error ("Invalid escape character syntax");
1560 c = READCHAR;
1561 if (c == '\\')
1562 c = read_escape (readcharfun, 0, byterep);
1563 return c | meta_modifier;
1564
1565 case 'S':
1566 c = READCHAR;
1567 if (c != '-')
1568 error ("Invalid escape character syntax");
1569 c = READCHAR;
1570 if (c == '\\')
1571 c = read_escape (readcharfun, 0, byterep);
1572 return c | shift_modifier;
1573
1574 case 'H':
1575 c = READCHAR;
1576 if (c != '-')
1577 error ("Invalid escape character syntax");
1578 c = READCHAR;
1579 if (c == '\\')
1580 c = read_escape (readcharfun, 0, byterep);
1581 return c | hyper_modifier;
1582
1583 case 'A':
1584 c = READCHAR;
1585 if (c != '-')
1586 error ("Invalid escape character syntax");
1587 c = READCHAR;
1588 if (c == '\\')
1589 c = read_escape (readcharfun, 0, byterep);
1590 return c | alt_modifier;
1591
1592 case 's':
1593 c = READCHAR;
1594 if (c != '-')
1595 error ("Invalid escape character syntax");
1596 c = READCHAR;
1597 if (c == '\\')
1598 c = read_escape (readcharfun, 0, byterep);
1599 return c | super_modifier;
1600
1601 case 'C':
1602 c = READCHAR;
1603 if (c != '-')
1604 error ("Invalid escape character syntax");
1605 case '^':
1606 c = READCHAR;
1607 if (c == '\\')
1608 c = read_escape (readcharfun, 0, byterep);
1609 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1610 return 0177 | (c & CHAR_MODIFIER_MASK);
1611 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1612 return c | ctrl_modifier;
1613 /* ASCII control chars are made from letters (both cases),
1614 as well as the non-letters within 0100...0137. */
1615 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1616 return (c & (037 | ~0177));
1617 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1618 return (c & (037 | ~0177));
1619 else
1620 return c | ctrl_modifier;
1621
1622 case '0':
1623 case '1':
1624 case '2':
1625 case '3':
1626 case '4':
1627 case '5':
1628 case '6':
1629 case '7':
1630 /* An octal escape, as in ANSI C. */
1631 {
1632 register int i = c - '0';
1633 register int count = 0;
1634 while (++count < 3)
1635 {
1636 if ((c = READCHAR) >= '0' && c <= '7')
1637 {
1638 i *= 8;
1639 i += c - '0';
1640 }
1641 else
1642 {
1643 UNREAD (c);
1644 break;
1645 }
1646 }
1647
1648 *byterep = 1;
1649 return i;
1650 }
1651
1652 case 'x':
1653 /* A hex escape, as in ANSI C. */
1654 {
1655 int i = 0;
1656 while (1)
1657 {
1658 c = READCHAR;
1659 if (c >= '0' && c <= '9')
1660 {
1661 i *= 16;
1662 i += c - '0';
1663 }
1664 else if ((c >= 'a' && c <= 'f')
1665 || (c >= 'A' && c <= 'F'))
1666 {
1667 i *= 16;
1668 if (c >= 'a' && c <= 'f')
1669 i += c - 'a' + 10;
1670 else
1671 i += c - 'A' + 10;
1672 }
1673 else
1674 {
1675 UNREAD (c);
1676 break;
1677 }
1678 }
1679
1680 *byterep = 2;
1681 return i;
1682 }
1683
1684 default:
1685 if (BASE_LEADING_CODE_P (c))
1686 c = read_multibyte (c, readcharfun);
1687 return c;
1688 }
1689 }
1690
1691
1692 /* Read an integer in radix RADIX using READCHARFUN to read
1693 characters. RADIX must be in the interval [2..36]; if it isn't, a
1694 read error is signaled . Value is the integer read. Signals an
1695 error if encountering invalid read syntax or if RADIX is out of
1696 range. */
1697
1698 static Lisp_Object
1699 read_integer (readcharfun, radix)
1700 Lisp_Object readcharfun;
1701 int radix;
1702 {
1703 int ndigits = 0, invalid_p, c, sign = 0;
1704 EMACS_INT number = 0;
1705
1706 if (radix < 2 || radix > 36)
1707 invalid_p = 1;
1708 else
1709 {
1710 number = ndigits = invalid_p = 0;
1711 sign = 1;
1712
1713 c = READCHAR;
1714 if (c == '-')
1715 {
1716 c = READCHAR;
1717 sign = -1;
1718 }
1719 else if (c == '+')
1720 c = READCHAR;
1721
1722 while (c >= 0)
1723 {
1724 int digit;
1725
1726 if (c >= '0' && c <= '9')
1727 digit = c - '0';
1728 else if (c >= 'a' && c <= 'z')
1729 digit = c - 'a' + 10;
1730 else if (c >= 'A' && c <= 'Z')
1731 digit = c - 'A' + 10;
1732 else
1733 {
1734 UNREAD (c);
1735 break;
1736 }
1737
1738 if (digit < 0 || digit >= radix)
1739 invalid_p = 1;
1740
1741 number = radix * number + digit;
1742 ++ndigits;
1743 c = READCHAR;
1744 }
1745 }
1746
1747 if (ndigits == 0 || invalid_p)
1748 {
1749 char buf[50];
1750 sprintf (buf, "integer, radix %d", radix);
1751 Fsignal (Qinvalid_read_syntax, Fcons (build_string (buf), Qnil));
1752 }
1753
1754 return make_number (sign * number);
1755 }
1756
1757
1758 /* Convert unibyte text in read_buffer to multibyte.
1759
1760 Initially, *P is a pointer after the end of the unibyte text, and
1761 the pointer *END points after the end of read_buffer.
1762
1763 If read_buffer doesn't have enough room to hold the result
1764 of the conversion, reallocate it and adjust *P and *END.
1765
1766 At the end, make *P point after the result of the conversion, and
1767 return in *NCHARS the number of characters in the converted
1768 text. */
1769
1770 static void
1771 to_multibyte (p, end, nchars)
1772 char **p, **end;
1773 int *nchars;
1774 {
1775 int nbytes;
1776
1777 parse_str_as_multibyte (read_buffer, *p - read_buffer, &nbytes, nchars);
1778 if (read_buffer_size < 2 * nbytes)
1779 {
1780 int offset = *p - read_buffer;
1781 read_buffer_size = 2 * max (read_buffer_size, nbytes);
1782 read_buffer = (char *) xrealloc (read_buffer, read_buffer_size);
1783 *p = read_buffer + offset;
1784 *end = read_buffer + read_buffer_size;
1785 }
1786
1787 if (nbytes != *nchars)
1788 nbytes = str_as_multibyte (read_buffer, read_buffer_size,
1789 *p - read_buffer, nchars);
1790
1791 *p = read_buffer + nbytes;
1792 }
1793
1794
1795 /* If the next token is ')' or ']' or '.', we store that character
1796 in *PCH and the return value is not interesting. Else, we store
1797 zero in *PCH and we read and return one lisp object.
1798
1799 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1800
1801 static Lisp_Object
1802 read1 (readcharfun, pch, first_in_list)
1803 register Lisp_Object readcharfun;
1804 int *pch;
1805 int first_in_list;
1806 {
1807 register int c;
1808 int uninterned_symbol = 0;
1809
1810 *pch = 0;
1811
1812 retry:
1813
1814 c = READCHAR;
1815 if (c < 0)
1816 end_of_file_error ();
1817
1818 switch (c)
1819 {
1820 case '(':
1821 return read_list (0, readcharfun);
1822
1823 case '[':
1824 return read_vector (readcharfun, 0);
1825
1826 case ')':
1827 case ']':
1828 {
1829 *pch = c;
1830 return Qnil;
1831 }
1832
1833 case '#':
1834 c = READCHAR;
1835 if (c == '^')
1836 {
1837 c = READCHAR;
1838 if (c == '[')
1839 {
1840 Lisp_Object tmp;
1841 tmp = read_vector (readcharfun, 0);
1842 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1843 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1844 error ("Invalid size char-table");
1845 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1846 XCHAR_TABLE (tmp)->top = Qt;
1847 return tmp;
1848 }
1849 else if (c == '^')
1850 {
1851 c = READCHAR;
1852 if (c == '[')
1853 {
1854 Lisp_Object tmp;
1855 tmp = read_vector (readcharfun, 0);
1856 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1857 error ("Invalid size char-table");
1858 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1859 XCHAR_TABLE (tmp)->top = Qnil;
1860 return tmp;
1861 }
1862 Fsignal (Qinvalid_read_syntax,
1863 Fcons (make_string ("#^^", 3), Qnil));
1864 }
1865 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1866 }
1867 if (c == '&')
1868 {
1869 Lisp_Object length;
1870 length = read1 (readcharfun, pch, first_in_list);
1871 c = READCHAR;
1872 if (c == '"')
1873 {
1874 Lisp_Object tmp, val;
1875 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1876 / BITS_PER_CHAR);
1877
1878 UNREAD (c);
1879 tmp = read1 (readcharfun, pch, first_in_list);
1880 if (size_in_chars != XSTRING (tmp)->size
1881 /* We used to print 1 char too many
1882 when the number of bits was a multiple of 8.
1883 Accept such input in case it came from an old version. */
1884 && ! (XFASTINT (length)
1885 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1886 Fsignal (Qinvalid_read_syntax,
1887 Fcons (make_string ("#&...", 5), Qnil));
1888
1889 val = Fmake_bool_vector (length, Qnil);
1890 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1891 size_in_chars);
1892 /* Clear the extraneous bits in the last byte. */
1893 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1894 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1895 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1896 return val;
1897 }
1898 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1899 Qnil));
1900 }
1901 if (c == '[')
1902 {
1903 /* Accept compiled functions at read-time so that we don't have to
1904 build them using function calls. */
1905 Lisp_Object tmp;
1906 tmp = read_vector (readcharfun, 1);
1907 return Fmake_byte_code (XVECTOR (tmp)->size,
1908 XVECTOR (tmp)->contents);
1909 }
1910 if (c == '(')
1911 {
1912 Lisp_Object tmp;
1913 struct gcpro gcpro1;
1914 int ch;
1915
1916 /* Read the string itself. */
1917 tmp = read1 (readcharfun, &ch, 0);
1918 if (ch != 0 || !STRINGP (tmp))
1919 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1920 GCPRO1 (tmp);
1921 /* Read the intervals and their properties. */
1922 while (1)
1923 {
1924 Lisp_Object beg, end, plist;
1925
1926 beg = read1 (readcharfun, &ch, 0);
1927 end = plist = Qnil;
1928 if (ch == ')')
1929 break;
1930 if (ch == 0)
1931 end = read1 (readcharfun, &ch, 0);
1932 if (ch == 0)
1933 plist = read1 (readcharfun, &ch, 0);
1934 if (ch)
1935 Fsignal (Qinvalid_read_syntax,
1936 Fcons (build_string ("invalid string property list"),
1937 Qnil));
1938 Fset_text_properties (beg, end, plist, tmp);
1939 }
1940 UNGCPRO;
1941 return tmp;
1942 }
1943
1944 /* #@NUMBER is used to skip NUMBER following characters.
1945 That's used in .elc files to skip over doc strings
1946 and function definitions. */
1947 if (c == '@')
1948 {
1949 int i, nskip = 0;
1950
1951 /* Read a decimal integer. */
1952 while ((c = READCHAR) >= 0
1953 && c >= '0' && c <= '9')
1954 {
1955 nskip *= 10;
1956 nskip += c - '0';
1957 }
1958 if (c >= 0)
1959 UNREAD (c);
1960
1961 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1962 {
1963 /* If we are supposed to force doc strings into core right now,
1964 record the last string that we skipped,
1965 and record where in the file it comes from. */
1966
1967 /* But first exchange saved_doc_string
1968 with prev_saved_doc_string, so we save two strings. */
1969 {
1970 char *temp = saved_doc_string;
1971 int temp_size = saved_doc_string_size;
1972 file_offset temp_pos = saved_doc_string_position;
1973 int temp_len = saved_doc_string_length;
1974
1975 saved_doc_string = prev_saved_doc_string;
1976 saved_doc_string_size = prev_saved_doc_string_size;
1977 saved_doc_string_position = prev_saved_doc_string_position;
1978 saved_doc_string_length = prev_saved_doc_string_length;
1979
1980 prev_saved_doc_string = temp;
1981 prev_saved_doc_string_size = temp_size;
1982 prev_saved_doc_string_position = temp_pos;
1983 prev_saved_doc_string_length = temp_len;
1984 }
1985
1986 if (saved_doc_string_size == 0)
1987 {
1988 saved_doc_string_size = nskip + 100;
1989 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1990 }
1991 if (nskip > saved_doc_string_size)
1992 {
1993 saved_doc_string_size = nskip + 100;
1994 saved_doc_string = (char *) xrealloc (saved_doc_string,
1995 saved_doc_string_size);
1996 }
1997
1998 saved_doc_string_position = file_tell (instream);
1999
2000 /* Copy that many characters into saved_doc_string. */
2001 for (i = 0; i < nskip && c >= 0; i++)
2002 saved_doc_string[i] = c = READCHAR;
2003
2004 saved_doc_string_length = i;
2005 }
2006 else
2007 {
2008 /* Skip that many characters. */
2009 for (i = 0; i < nskip && c >= 0; i++)
2010 c = READCHAR;
2011 }
2012
2013 goto retry;
2014 }
2015 if (c == '$')
2016 return Vload_file_name;
2017 if (c == '\'')
2018 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2019 /* #:foo is the uninterned symbol named foo. */
2020 if (c == ':')
2021 {
2022 uninterned_symbol = 1;
2023 c = READCHAR;
2024 goto default_label;
2025 }
2026 /* Reader forms that can reuse previously read objects. */
2027 if (c >= '0' && c <= '9')
2028 {
2029 int n = 0;
2030 Lisp_Object tem;
2031
2032 /* Read a non-negative integer. */
2033 while (c >= '0' && c <= '9')
2034 {
2035 n *= 10;
2036 n += c - '0';
2037 c = READCHAR;
2038 }
2039 /* #n=object returns object, but associates it with n for #n#. */
2040 if (c == '=')
2041 {
2042 /* Make a placeholder for #n# to use temporarily */
2043 Lisp_Object placeholder;
2044 Lisp_Object cell;
2045
2046 placeholder = Fcons(Qnil, Qnil);
2047 cell = Fcons (make_number (n), placeholder);
2048 read_objects = Fcons (cell, read_objects);
2049
2050 /* Read the object itself. */
2051 tem = read0 (readcharfun);
2052
2053 /* Now put it everywhere the placeholder was... */
2054 substitute_object_in_subtree (tem, placeholder);
2055
2056 /* ...and #n# will use the real value from now on. */
2057 Fsetcdr (cell, tem);
2058
2059 return tem;
2060 }
2061 /* #n# returns a previously read object. */
2062 if (c == '#')
2063 {
2064 tem = Fassq (make_number (n), read_objects);
2065 if (CONSP (tem))
2066 return XCDR (tem);
2067 /* Fall through to error message. */
2068 }
2069 else if (c == 'r' || c == 'R')
2070 return read_integer (readcharfun, n);
2071
2072 /* Fall through to error message. */
2073 }
2074 else if (c == 'x' || c == 'X')
2075 return read_integer (readcharfun, 16);
2076 else if (c == 'o' || c == 'O')
2077 return read_integer (readcharfun, 8);
2078 else if (c == 'b' || c == 'B')
2079 return read_integer (readcharfun, 2);
2080
2081 UNREAD (c);
2082 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2083
2084 case ';':
2085 while ((c = READCHAR) >= 0 && c != '\n');
2086 goto retry;
2087
2088 case '\'':
2089 {
2090 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2091 }
2092
2093 case '`':
2094 if (first_in_list)
2095 goto default_label;
2096 else
2097 {
2098 Lisp_Object value;
2099
2100 new_backquote_flag++;
2101 value = read0 (readcharfun);
2102 new_backquote_flag--;
2103
2104 return Fcons (Qbackquote, Fcons (value, Qnil));
2105 }
2106
2107 case ',':
2108 if (new_backquote_flag)
2109 {
2110 Lisp_Object comma_type = Qnil;
2111 Lisp_Object value;
2112 int ch = READCHAR;
2113
2114 if (ch == '@')
2115 comma_type = Qcomma_at;
2116 else if (ch == '.')
2117 comma_type = Qcomma_dot;
2118 else
2119 {
2120 if (ch >= 0) UNREAD (ch);
2121 comma_type = Qcomma;
2122 }
2123
2124 new_backquote_flag--;
2125 value = read0 (readcharfun);
2126 new_backquote_flag++;
2127 return Fcons (comma_type, Fcons (value, Qnil));
2128 }
2129 else
2130 goto default_label;
2131
2132 case '?':
2133 {
2134 int discard;
2135
2136 c = READCHAR;
2137 if (c < 0)
2138 end_of_file_error ();
2139
2140 if (c == '\\')
2141 c = read_escape (readcharfun, 0, &discard);
2142 else if (BASE_LEADING_CODE_P (c))
2143 c = read_multibyte (c, readcharfun);
2144
2145 return make_number (c);
2146 }
2147
2148 case '"':
2149 {
2150 char *p = read_buffer;
2151 char *end = read_buffer + read_buffer_size;
2152 register int c;
2153 /* 1 if we saw an escape sequence specifying
2154 a multibyte character, or a multibyte character. */
2155 int force_multibyte = 0;
2156 /* 1 if we saw an escape sequence specifying
2157 a single-byte character. */
2158 int force_singlebyte = 0;
2159 /* 1 if read_buffer contains multibyte text now. */
2160 int is_multibyte = 0;
2161 int cancel = 0;
2162 int nchars = 0;
2163
2164 while ((c = READCHAR) >= 0
2165 && c != '\"')
2166 {
2167 if (end - p < MAX_MULTIBYTE_LENGTH)
2168 {
2169 int offset = p - read_buffer;
2170 read_buffer = (char *) xrealloc (read_buffer,
2171 read_buffer_size *= 2);
2172 p = read_buffer + offset;
2173 end = read_buffer + read_buffer_size;
2174 }
2175
2176 if (c == '\\')
2177 {
2178 int byterep;
2179
2180 c = read_escape (readcharfun, 1, &byterep);
2181
2182 /* C is -1 if \ newline has just been seen */
2183 if (c == -1)
2184 {
2185 if (p == read_buffer)
2186 cancel = 1;
2187 continue;
2188 }
2189
2190 if (byterep == 1)
2191 force_singlebyte = 1;
2192 else if (byterep == 2)
2193 force_multibyte = 1;
2194 }
2195
2196 /* A character that must be multibyte forces multibyte. */
2197 if (! SINGLE_BYTE_CHAR_P (c & ~CHAR_MODIFIER_MASK))
2198 force_multibyte = 1;
2199
2200 /* If we just discovered the need to be multibyte,
2201 convert the text accumulated thus far. */
2202 if (force_multibyte && ! is_multibyte)
2203 {
2204 is_multibyte = 1;
2205 to_multibyte (&p, &end, &nchars);
2206 }
2207
2208 /* Allow `\C- ' and `\C-?'. */
2209 if (c == (CHAR_CTL | ' '))
2210 c = 0;
2211 else if (c == (CHAR_CTL | '?'))
2212 c = 127;
2213
2214 if (c & CHAR_SHIFT)
2215 {
2216 /* Shift modifier is valid only with [A-Za-z]. */
2217 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2218 c &= ~CHAR_SHIFT;
2219 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2220 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
2221 }
2222
2223 if (c & CHAR_META)
2224 /* Move the meta bit to the right place for a string. */
2225 c = (c & ~CHAR_META) | 0x80;
2226 if (c & CHAR_MODIFIER_MASK)
2227 error ("Invalid modifier in string");
2228
2229 if (is_multibyte)
2230 p += CHAR_STRING (c, p);
2231 else
2232 *p++ = c;
2233
2234 nchars++;
2235 }
2236
2237 if (c < 0)
2238 end_of_file_error ();
2239
2240 /* If purifying, and string starts with \ newline,
2241 return zero instead. This is for doc strings
2242 that we are really going to find in etc/DOC.nn.nn */
2243 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2244 return make_number (0);
2245
2246 if (is_multibyte || force_singlebyte)
2247 ;
2248 else if (load_convert_to_unibyte)
2249 {
2250 Lisp_Object string;
2251 to_multibyte (&p, &end, &nchars);
2252 if (p - read_buffer != nchars)
2253 {
2254 string = make_multibyte_string (read_buffer, nchars,
2255 p - read_buffer);
2256 return Fstring_make_unibyte (string);
2257 }
2258 /* We can make a unibyte string directly. */
2259 is_multibyte = 0;
2260 }
2261 else if (EQ (readcharfun, Qget_file_char)
2262 || EQ (readcharfun, Qlambda))
2263 {
2264 /* Nowadays, reading directly from a file is used only for
2265 compiled Emacs Lisp files, and those always use the
2266 Emacs internal encoding. Meanwhile, Qlambda is used
2267 for reading dynamic byte code (compiled with
2268 byte-compile-dynamic = t). So make the string multibyte
2269 if the string contains any multibyte sequences.
2270 (to_multibyte is a no-op if not.) */
2271 to_multibyte (&p, &end, &nchars);
2272 is_multibyte = (p - read_buffer) != nchars;
2273 }
2274 else
2275 /* In all other cases, if we read these bytes as
2276 separate characters, treat them as separate characters now. */
2277 ;
2278
2279 if (read_pure)
2280 return make_pure_string (read_buffer, nchars, p - read_buffer,
2281 is_multibyte);
2282 return make_specified_string (read_buffer, nchars, p - read_buffer,
2283 is_multibyte);
2284 }
2285
2286 case '.':
2287 {
2288 int next_char = READCHAR;
2289 UNREAD (next_char);
2290
2291 if (next_char <= 040
2292 || index ("\"'`,(", next_char))
2293 {
2294 *pch = c;
2295 return Qnil;
2296 }
2297
2298 /* Otherwise, we fall through! Note that the atom-reading loop
2299 below will now loop at least once, assuring that we will not
2300 try to UNREAD two characters in a row. */
2301 }
2302 default:
2303 default_label:
2304 if (c <= 040) goto retry;
2305 {
2306 char *p = read_buffer;
2307 int quoted = 0;
2308
2309 {
2310 char *end = read_buffer + read_buffer_size;
2311
2312 while (c > 040
2313 && !(c == '\"' || c == '\'' || c == ';'
2314 || c == '(' || c == ')'
2315 || c == '[' || c == ']' || c == '#'))
2316 {
2317 if (end - p < MAX_MULTIBYTE_LENGTH)
2318 {
2319 int offset = p - read_buffer;
2320 read_buffer = (char *) xrealloc (read_buffer,
2321 read_buffer_size *= 2);
2322 p = read_buffer + offset;
2323 end = read_buffer + read_buffer_size;
2324 }
2325
2326 if (c == '\\')
2327 {
2328 c = READCHAR;
2329 if (c == -1)
2330 end_of_file_error ();
2331 quoted = 1;
2332 }
2333
2334 if (! SINGLE_BYTE_CHAR_P (c))
2335 p += CHAR_STRING (c, p);
2336 else
2337 *p++ = c;
2338
2339 c = READCHAR;
2340 }
2341
2342 if (p == end)
2343 {
2344 int offset = p - read_buffer;
2345 read_buffer = (char *) xrealloc (read_buffer,
2346 read_buffer_size *= 2);
2347 p = read_buffer + offset;
2348 end = read_buffer + read_buffer_size;
2349 }
2350 *p = 0;
2351 if (c >= 0)
2352 UNREAD (c);
2353 }
2354
2355 if (!quoted && !uninterned_symbol)
2356 {
2357 register char *p1;
2358 register Lisp_Object val;
2359 p1 = read_buffer;
2360 if (*p1 == '+' || *p1 == '-') p1++;
2361 /* Is it an integer? */
2362 if (p1 != p)
2363 {
2364 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2365 /* Integers can have trailing decimal points. */
2366 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2367 if (p1 == p)
2368 /* It is an integer. */
2369 {
2370 if (p1[-1] == '.')
2371 p1[-1] = '\0';
2372 if (sizeof (int) == sizeof (EMACS_INT))
2373 XSETINT (val, atoi (read_buffer));
2374 else if (sizeof (long) == sizeof (EMACS_INT))
2375 XSETINT (val, atol (read_buffer));
2376 else
2377 abort ();
2378 return val;
2379 }
2380 }
2381 if (isfloat_string (read_buffer))
2382 {
2383 /* Compute NaN and infinities using 0.0 in a variable,
2384 to cope with compilers that think they are smarter
2385 than we are. */
2386 double zero = 0.0;
2387
2388 double value;
2389
2390 /* Negate the value ourselves. This treats 0, NaNs,
2391 and infinity properly on IEEE floating point hosts,
2392 and works around a common bug where atof ("-0.0")
2393 drops the sign. */
2394 int negative = read_buffer[0] == '-';
2395
2396 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2397 returns 1, is if the input ends in e+INF or e+NaN. */
2398 switch (p[-1])
2399 {
2400 case 'F':
2401 value = 1.0 / zero;
2402 break;
2403 case 'N':
2404 value = zero / zero;
2405 break;
2406 default:
2407 value = atof (read_buffer + negative);
2408 break;
2409 }
2410
2411 return make_float (negative ? - value : value);
2412 }
2413 }
2414
2415 if (uninterned_symbol)
2416 return make_symbol (read_buffer);
2417 else
2418 return intern (read_buffer);
2419 }
2420 }
2421 }
2422 \f
2423
2424 /* List of nodes we've seen during substitute_object_in_subtree. */
2425 static Lisp_Object seen_list;
2426
2427 static void
2428 substitute_object_in_subtree (object, placeholder)
2429 Lisp_Object object;
2430 Lisp_Object placeholder;
2431 {
2432 Lisp_Object check_object;
2433
2434 /* We haven't seen any objects when we start. */
2435 seen_list = Qnil;
2436
2437 /* Make all the substitutions. */
2438 check_object
2439 = substitute_object_recurse (object, placeholder, object);
2440
2441 /* Clear seen_list because we're done with it. */
2442 seen_list = Qnil;
2443
2444 /* The returned object here is expected to always eq the
2445 original. */
2446 if (!EQ (check_object, object))
2447 error ("Unexpected mutation error in reader");
2448 }
2449
2450 /* Feval doesn't get called from here, so no gc protection is needed. */
2451 #define SUBSTITUTE(get_val, set_val) \
2452 { \
2453 Lisp_Object old_value = get_val; \
2454 Lisp_Object true_value \
2455 = substitute_object_recurse (object, placeholder,\
2456 old_value); \
2457 \
2458 if (!EQ (old_value, true_value)) \
2459 { \
2460 set_val; \
2461 } \
2462 }
2463
2464 static Lisp_Object
2465 substitute_object_recurse (object, placeholder, subtree)
2466 Lisp_Object object;
2467 Lisp_Object placeholder;
2468 Lisp_Object subtree;
2469 {
2470 /* If we find the placeholder, return the target object. */
2471 if (EQ (placeholder, subtree))
2472 return object;
2473
2474 /* If we've been to this node before, don't explore it again. */
2475 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2476 return subtree;
2477
2478 /* If this node can be the entry point to a cycle, remember that
2479 we've seen it. It can only be such an entry point if it was made
2480 by #n=, which means that we can find it as a value in
2481 read_objects. */
2482 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2483 seen_list = Fcons (subtree, seen_list);
2484
2485 /* Recurse according to subtree's type.
2486 Every branch must return a Lisp_Object. */
2487 switch (XTYPE (subtree))
2488 {
2489 case Lisp_Vectorlike:
2490 {
2491 int i;
2492 int length = XINT (Flength(subtree));
2493 for (i = 0; i < length; i++)
2494 {
2495 Lisp_Object idx = make_number (i);
2496 SUBSTITUTE (Faref (subtree, idx),
2497 Faset (subtree, idx, true_value));
2498 }
2499 return subtree;
2500 }
2501
2502 case Lisp_Cons:
2503 {
2504 SUBSTITUTE (Fcar_safe (subtree),
2505 Fsetcar (subtree, true_value));
2506 SUBSTITUTE (Fcdr_safe (subtree),
2507 Fsetcdr (subtree, true_value));
2508 return subtree;
2509 }
2510
2511 case Lisp_String:
2512 {
2513 /* Check for text properties in each interval.
2514 substitute_in_interval contains part of the logic. */
2515
2516 INTERVAL root_interval = XSTRING (subtree)->intervals;
2517 Lisp_Object arg = Fcons (object, placeholder);
2518
2519 traverse_intervals_noorder (root_interval,
2520 &substitute_in_interval, arg);
2521
2522 return subtree;
2523 }
2524
2525 /* Other types don't recurse any further. */
2526 default:
2527 return subtree;
2528 }
2529 }
2530
2531 /* Helper function for substitute_object_recurse. */
2532 static void
2533 substitute_in_interval (interval, arg)
2534 INTERVAL interval;
2535 Lisp_Object arg;
2536 {
2537 Lisp_Object object = Fcar (arg);
2538 Lisp_Object placeholder = Fcdr (arg);
2539
2540 SUBSTITUTE(interval->plist, interval->plist = true_value);
2541 }
2542
2543 \f
2544 #define LEAD_INT 1
2545 #define DOT_CHAR 2
2546 #define TRAIL_INT 4
2547 #define E_CHAR 8
2548 #define EXP_INT 16
2549
2550 int
2551 isfloat_string (cp)
2552 register char *cp;
2553 {
2554 register int state;
2555
2556 char *start = cp;
2557
2558 state = 0;
2559 if (*cp == '+' || *cp == '-')
2560 cp++;
2561
2562 if (*cp >= '0' && *cp <= '9')
2563 {
2564 state |= LEAD_INT;
2565 while (*cp >= '0' && *cp <= '9')
2566 cp++;
2567 }
2568 if (*cp == '.')
2569 {
2570 state |= DOT_CHAR;
2571 cp++;
2572 }
2573 if (*cp >= '0' && *cp <= '9')
2574 {
2575 state |= TRAIL_INT;
2576 while (*cp >= '0' && *cp <= '9')
2577 cp++;
2578 }
2579 if (*cp == 'e' || *cp == 'E')
2580 {
2581 state |= E_CHAR;
2582 cp++;
2583 if (*cp == '+' || *cp == '-')
2584 cp++;
2585 }
2586
2587 if (*cp >= '0' && *cp <= '9')
2588 {
2589 state |= EXP_INT;
2590 while (*cp >= '0' && *cp <= '9')
2591 cp++;
2592 }
2593 else if (cp == start)
2594 ;
2595 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2596 {
2597 state |= EXP_INT;
2598 cp += 3;
2599 }
2600 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2601 {
2602 state |= EXP_INT;
2603 cp += 3;
2604 }
2605
2606 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2607 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2608 || state == (DOT_CHAR|TRAIL_INT)
2609 || state == (LEAD_INT|E_CHAR|EXP_INT)
2610 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2611 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2612 }
2613
2614 \f
2615 static Lisp_Object
2616 read_vector (readcharfun, bytecodeflag)
2617 Lisp_Object readcharfun;
2618 int bytecodeflag;
2619 {
2620 register int i;
2621 register int size;
2622 register Lisp_Object *ptr;
2623 register Lisp_Object tem, item, vector;
2624 register struct Lisp_Cons *otem;
2625 Lisp_Object len;
2626
2627 tem = read_list (1, readcharfun);
2628 len = Flength (tem);
2629 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2630
2631 size = XVECTOR (vector)->size;
2632 ptr = XVECTOR (vector)->contents;
2633 for (i = 0; i < size; i++)
2634 {
2635 item = Fcar (tem);
2636 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2637 bytecode object, the docstring containing the bytecode and
2638 constants values must be treated as unibyte and passed to
2639 Fread, to get the actual bytecode string and constants vector. */
2640 if (bytecodeflag && load_force_doc_strings)
2641 {
2642 if (i == COMPILED_BYTECODE)
2643 {
2644 if (!STRINGP (item))
2645 error ("invalid byte code");
2646
2647 /* Delay handling the bytecode slot until we know whether
2648 it is lazily-loaded (we can tell by whether the
2649 constants slot is nil). */
2650 ptr[COMPILED_CONSTANTS] = item;
2651 item = Qnil;
2652 }
2653 else if (i == COMPILED_CONSTANTS)
2654 {
2655 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2656
2657 if (NILP (item))
2658 {
2659 /* Coerce string to unibyte (like string-as-unibyte,
2660 but without generating extra garbage and
2661 guaranteeing no change in the contents). */
2662 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2663 SET_STRING_BYTES (XSTRING (bytestr), -1);
2664
2665 item = Fread (bytestr);
2666 if (!CONSP (item))
2667 error ("invalid byte code");
2668
2669 otem = XCONS (item);
2670 bytestr = XCAR (item);
2671 item = XCDR (item);
2672 free_cons (otem);
2673 }
2674
2675 /* Now handle the bytecode slot. */
2676 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2677 }
2678 }
2679 ptr[i] = read_pure ? Fpurecopy (item) : item;
2680 otem = XCONS (tem);
2681 tem = Fcdr (tem);
2682 free_cons (otem);
2683 }
2684 return vector;
2685 }
2686
2687 /* FLAG = 1 means check for ] to terminate rather than ) and .
2688 FLAG = -1 means check for starting with defun
2689 and make structure pure. */
2690
2691 static Lisp_Object
2692 read_list (flag, readcharfun)
2693 int flag;
2694 register Lisp_Object readcharfun;
2695 {
2696 /* -1 means check next element for defun,
2697 0 means don't check,
2698 1 means already checked and found defun. */
2699 int defunflag = flag < 0 ? -1 : 0;
2700 Lisp_Object val, tail;
2701 register Lisp_Object elt, tem;
2702 struct gcpro gcpro1, gcpro2;
2703 /* 0 is the normal case.
2704 1 means this list is a doc reference; replace it with the number 0.
2705 2 means this list is a doc reference; replace it with the doc string. */
2706 int doc_reference = 0;
2707
2708 /* Initialize this to 1 if we are reading a list. */
2709 int first_in_list = flag <= 0;
2710
2711 val = Qnil;
2712 tail = Qnil;
2713
2714 while (1)
2715 {
2716 int ch;
2717 GCPRO2 (val, tail);
2718 elt = read1 (readcharfun, &ch, first_in_list);
2719 UNGCPRO;
2720
2721 first_in_list = 0;
2722
2723 /* While building, if the list starts with #$, treat it specially. */
2724 if (EQ (elt, Vload_file_name)
2725 && ! NILP (elt)
2726 && !NILP (Vpurify_flag))
2727 {
2728 if (NILP (Vdoc_file_name))
2729 /* We have not yet called Snarf-documentation, so assume
2730 this file is described in the DOC-MM.NN file
2731 and Snarf-documentation will fill in the right value later.
2732 For now, replace the whole list with 0. */
2733 doc_reference = 1;
2734 else
2735 /* We have already called Snarf-documentation, so make a relative
2736 file name for this file, so it can be found properly
2737 in the installed Lisp directory.
2738 We don't use Fexpand_file_name because that would make
2739 the directory absolute now. */
2740 elt = concat2 (build_string ("../lisp/"),
2741 Ffile_name_nondirectory (elt));
2742 }
2743 else if (EQ (elt, Vload_file_name)
2744 && ! NILP (elt)
2745 && load_force_doc_strings)
2746 doc_reference = 2;
2747
2748 if (ch)
2749 {
2750 if (flag > 0)
2751 {
2752 if (ch == ']')
2753 return val;
2754 Fsignal (Qinvalid_read_syntax,
2755 Fcons (make_string (") or . in a vector", 18), Qnil));
2756 }
2757 if (ch == ')')
2758 return val;
2759 if (ch == '.')
2760 {
2761 GCPRO2 (val, tail);
2762 if (!NILP (tail))
2763 XSETCDR (tail, read0 (readcharfun));
2764 else
2765 val = read0 (readcharfun);
2766 read1 (readcharfun, &ch, 0);
2767 UNGCPRO;
2768 if (ch == ')')
2769 {
2770 if (doc_reference == 1)
2771 return make_number (0);
2772 if (doc_reference == 2)
2773 {
2774 /* Get a doc string from the file we are loading.
2775 If it's in saved_doc_string, get it from there. */
2776 int pos = XINT (XCDR (val));
2777 /* Position is negative for user variables. */
2778 if (pos < 0) pos = -pos;
2779 if (pos >= saved_doc_string_position
2780 && pos < (saved_doc_string_position
2781 + saved_doc_string_length))
2782 {
2783 int start = pos - saved_doc_string_position;
2784 int from, to;
2785
2786 /* Process quoting with ^A,
2787 and find the end of the string,
2788 which is marked with ^_ (037). */
2789 for (from = start, to = start;
2790 saved_doc_string[from] != 037;)
2791 {
2792 int c = saved_doc_string[from++];
2793 if (c == 1)
2794 {
2795 c = saved_doc_string[from++];
2796 if (c == 1)
2797 saved_doc_string[to++] = c;
2798 else if (c == '0')
2799 saved_doc_string[to++] = 0;
2800 else if (c == '_')
2801 saved_doc_string[to++] = 037;
2802 }
2803 else
2804 saved_doc_string[to++] = c;
2805 }
2806
2807 return make_string (saved_doc_string + start,
2808 to - start);
2809 }
2810 /* Look in prev_saved_doc_string the same way. */
2811 else if (pos >= prev_saved_doc_string_position
2812 && pos < (prev_saved_doc_string_position
2813 + prev_saved_doc_string_length))
2814 {
2815 int start = pos - prev_saved_doc_string_position;
2816 int from, to;
2817
2818 /* Process quoting with ^A,
2819 and find the end of the string,
2820 which is marked with ^_ (037). */
2821 for (from = start, to = start;
2822 prev_saved_doc_string[from] != 037;)
2823 {
2824 int c = prev_saved_doc_string[from++];
2825 if (c == 1)
2826 {
2827 c = prev_saved_doc_string[from++];
2828 if (c == 1)
2829 prev_saved_doc_string[to++] = c;
2830 else if (c == '0')
2831 prev_saved_doc_string[to++] = 0;
2832 else if (c == '_')
2833 prev_saved_doc_string[to++] = 037;
2834 }
2835 else
2836 prev_saved_doc_string[to++] = c;
2837 }
2838
2839 return make_string (prev_saved_doc_string + start,
2840 to - start);
2841 }
2842 else
2843 return get_doc_string (val, 0, 0);
2844 }
2845
2846 return val;
2847 }
2848 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2849 }
2850 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2851 }
2852 tem = (read_pure && flag <= 0
2853 ? pure_cons (elt, Qnil)
2854 : Fcons (elt, Qnil));
2855 if (!NILP (tail))
2856 XSETCDR (tail, tem);
2857 else
2858 val = tem;
2859 tail = tem;
2860 if (defunflag < 0)
2861 defunflag = EQ (elt, Qdefun);
2862 else if (defunflag > 0)
2863 read_pure = 1;
2864 }
2865 }
2866 \f
2867 Lisp_Object Vobarray;
2868 Lisp_Object initial_obarray;
2869
2870 /* oblookup stores the bucket number here, for the sake of Funintern. */
2871
2872 int oblookup_last_bucket_number;
2873
2874 static int hash_string ();
2875 Lisp_Object oblookup ();
2876
2877 /* Get an error if OBARRAY is not an obarray.
2878 If it is one, return it. */
2879
2880 Lisp_Object
2881 check_obarray (obarray)
2882 Lisp_Object obarray;
2883 {
2884 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2885 {
2886 /* If Vobarray is now invalid, force it to be valid. */
2887 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2888
2889 obarray = wrong_type_argument (Qvectorp, obarray);
2890 }
2891 return obarray;
2892 }
2893
2894 /* Intern the C string STR: return a symbol with that name,
2895 interned in the current obarray. */
2896
2897 Lisp_Object
2898 intern (str)
2899 char *str;
2900 {
2901 Lisp_Object tem;
2902 int len = strlen (str);
2903 Lisp_Object obarray;
2904
2905 obarray = Vobarray;
2906 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2907 obarray = check_obarray (obarray);
2908 tem = oblookup (obarray, str, len, len);
2909 if (SYMBOLP (tem))
2910 return tem;
2911 return Fintern (make_string (str, len), obarray);
2912 }
2913
2914 /* Create an uninterned symbol with name STR. */
2915
2916 Lisp_Object
2917 make_symbol (str)
2918 char *str;
2919 {
2920 int len = strlen (str);
2921
2922 return Fmake_symbol ((!NILP (Vpurify_flag)
2923 ? make_pure_string (str, len, len, 0)
2924 : make_string (str, len)));
2925 }
2926 \f
2927 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2928 doc: /* Return the canonical symbol whose name is STRING.
2929 If there is none, one is created by this function and returned.
2930 A second optional argument specifies the obarray to use;
2931 it defaults to the value of `obarray'. */)
2932 (string, obarray)
2933 Lisp_Object string, obarray;
2934 {
2935 register Lisp_Object tem, sym, *ptr;
2936
2937 if (NILP (obarray)) obarray = Vobarray;
2938 obarray = check_obarray (obarray);
2939
2940 CHECK_STRING (string);
2941
2942 tem = oblookup (obarray, XSTRING (string)->data,
2943 XSTRING (string)->size,
2944 STRING_BYTES (XSTRING (string)));
2945 if (!INTEGERP (tem))
2946 return tem;
2947
2948 if (!NILP (Vpurify_flag))
2949 string = Fpurecopy (string);
2950 sym = Fmake_symbol (string);
2951
2952 if (EQ (obarray, initial_obarray))
2953 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
2954 else
2955 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
2956
2957 if ((XSTRING (string)->data[0] == ':')
2958 && EQ (obarray, initial_obarray))
2959 {
2960 XSYMBOL (sym)->constant = 1;
2961 XSYMBOL (sym)->value = sym;
2962 }
2963
2964 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2965 if (SYMBOLP (*ptr))
2966 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2967 else
2968 XSYMBOL (sym)->next = 0;
2969 *ptr = sym;
2970 return sym;
2971 }
2972
2973 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2974 doc: /* Return the canonical symbol named NAME, or nil if none exists.
2975 NAME may be a string or a symbol. If it is a symbol, that exact
2976 symbol is searched for.
2977 A second optional argument specifies the obarray to use;
2978 it defaults to the value of `obarray'. */)
2979 (name, obarray)
2980 Lisp_Object name, obarray;
2981 {
2982 register Lisp_Object tem;
2983 struct Lisp_String *string;
2984
2985 if (NILP (obarray)) obarray = Vobarray;
2986 obarray = check_obarray (obarray);
2987
2988 if (!SYMBOLP (name))
2989 {
2990 CHECK_STRING (name);
2991 string = XSTRING (name);
2992 }
2993 else
2994 string = XSYMBOL (name)->name;
2995
2996 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2997 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2998 return Qnil;
2999 else
3000 return tem;
3001 }
3002 \f
3003 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3004 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3005 The value is t if a symbol was found and deleted, nil otherwise.
3006 NAME may be a string or a symbol. If it is a symbol, that symbol
3007 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3008 OBARRAY defaults to the value of the variable `obarray'. */)
3009 (name, obarray)
3010 Lisp_Object name, obarray;
3011 {
3012 register Lisp_Object string, tem;
3013 int hash;
3014
3015 if (NILP (obarray)) obarray = Vobarray;
3016 obarray = check_obarray (obarray);
3017
3018 if (SYMBOLP (name))
3019 XSETSTRING (string, XSYMBOL (name)->name);
3020 else
3021 {
3022 CHECK_STRING (name);
3023 string = name;
3024 }
3025
3026 tem = oblookup (obarray, XSTRING (string)->data,
3027 XSTRING (string)->size,
3028 STRING_BYTES (XSTRING (string)));
3029 if (INTEGERP (tem))
3030 return Qnil;
3031 /* If arg was a symbol, don't delete anything but that symbol itself. */
3032 if (SYMBOLP (name) && !EQ (name, tem))
3033 return Qnil;
3034
3035 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3036 XSYMBOL (tem)->constant = 0;
3037 XSYMBOL (tem)->indirect_variable = 0;
3038
3039 hash = oblookup_last_bucket_number;
3040
3041 if (EQ (XVECTOR (obarray)->contents[hash], tem))
3042 {
3043 if (XSYMBOL (tem)->next)
3044 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3045 else
3046 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3047 }
3048 else
3049 {
3050 Lisp_Object tail, following;
3051
3052 for (tail = XVECTOR (obarray)->contents[hash];
3053 XSYMBOL (tail)->next;
3054 tail = following)
3055 {
3056 XSETSYMBOL (following, XSYMBOL (tail)->next);
3057 if (EQ (following, tem))
3058 {
3059 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3060 break;
3061 }
3062 }
3063 }
3064
3065 return Qt;
3066 }
3067 \f
3068 /* Return the symbol in OBARRAY whose names matches the string
3069 of SIZE characters (SIZE_BYTE bytes) at PTR.
3070 If there is no such symbol in OBARRAY, return nil.
3071
3072 Also store the bucket number in oblookup_last_bucket_number. */
3073
3074 Lisp_Object
3075 oblookup (obarray, ptr, size, size_byte)
3076 Lisp_Object obarray;
3077 register char *ptr;
3078 int size, size_byte;
3079 {
3080 int hash;
3081 int obsize;
3082 register Lisp_Object tail;
3083 Lisp_Object bucket, tem;
3084
3085 if (!VECTORP (obarray)
3086 || (obsize = XVECTOR (obarray)->size) == 0)
3087 {
3088 obarray = check_obarray (obarray);
3089 obsize = XVECTOR (obarray)->size;
3090 }
3091 /* This is sometimes needed in the middle of GC. */
3092 obsize &= ~ARRAY_MARK_FLAG;
3093 /* Combining next two lines breaks VMS C 2.3. */
3094 hash = hash_string (ptr, size_byte);
3095 hash %= obsize;
3096 bucket = XVECTOR (obarray)->contents[hash];
3097 oblookup_last_bucket_number = hash;
3098 if (XFASTINT (bucket) == 0)
3099 ;
3100 else if (!SYMBOLP (bucket))
3101 error ("Bad data in guts of obarray"); /* Like CADR error message */
3102 else
3103 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3104 {
3105 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
3106 && XSYMBOL (tail)->name->size == size
3107 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
3108 return tail;
3109 else if (XSYMBOL (tail)->next == 0)
3110 break;
3111 }
3112 XSETINT (tem, hash);
3113 return tem;
3114 }
3115
3116 static int
3117 hash_string (ptr, len)
3118 unsigned char *ptr;
3119 int len;
3120 {
3121 register unsigned char *p = ptr;
3122 register unsigned char *end = p + len;
3123 register unsigned char c;
3124 register int hash = 0;
3125
3126 while (p != end)
3127 {
3128 c = *p++;
3129 if (c >= 0140) c -= 40;
3130 hash = ((hash<<3) + (hash>>28) + c);
3131 }
3132 return hash & 07777777777;
3133 }
3134 \f
3135 void
3136 map_obarray (obarray, fn, arg)
3137 Lisp_Object obarray;
3138 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3139 Lisp_Object arg;
3140 {
3141 register int i;
3142 register Lisp_Object tail;
3143 CHECK_VECTOR (obarray);
3144 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3145 {
3146 tail = XVECTOR (obarray)->contents[i];
3147 if (SYMBOLP (tail))
3148 while (1)
3149 {
3150 (*fn) (tail, arg);
3151 if (XSYMBOL (tail)->next == 0)
3152 break;
3153 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3154 }
3155 }
3156 }
3157
3158 void
3159 mapatoms_1 (sym, function)
3160 Lisp_Object sym, function;
3161 {
3162 call1 (function, sym);
3163 }
3164
3165 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3166 doc: /* Call FUNCTION on every symbol in OBARRAY.
3167 OBARRAY defaults to the value of `obarray'. */)
3168 (function, obarray)
3169 Lisp_Object function, obarray;
3170 {
3171 if (NILP (obarray)) obarray = Vobarray;
3172 obarray = check_obarray (obarray);
3173
3174 map_obarray (obarray, mapatoms_1, function);
3175 return Qnil;
3176 }
3177
3178 #define OBARRAY_SIZE 1511
3179
3180 void
3181 init_obarray ()
3182 {
3183 Lisp_Object oblength;
3184 int hash;
3185 Lisp_Object *tem;
3186
3187 XSETFASTINT (oblength, OBARRAY_SIZE);
3188
3189 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3190 Vobarray = Fmake_vector (oblength, make_number (0));
3191 initial_obarray = Vobarray;
3192 staticpro (&initial_obarray);
3193 /* Intern nil in the obarray */
3194 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3195 XSYMBOL (Qnil)->constant = 1;
3196
3197 /* These locals are to kludge around a pyramid compiler bug. */
3198 hash = hash_string ("nil", 3);
3199 /* Separate statement here to avoid VAXC bug. */
3200 hash %= OBARRAY_SIZE;
3201 tem = &XVECTOR (Vobarray)->contents[hash];
3202 *tem = Qnil;
3203
3204 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3205 XSYMBOL (Qnil)->function = Qunbound;
3206 XSYMBOL (Qunbound)->value = Qunbound;
3207 XSYMBOL (Qunbound)->function = Qunbound;
3208
3209 Qt = intern ("t");
3210 XSYMBOL (Qnil)->value = Qnil;
3211 XSYMBOL (Qnil)->plist = Qnil;
3212 XSYMBOL (Qt)->value = Qt;
3213 XSYMBOL (Qt)->constant = 1;
3214
3215 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3216 Vpurify_flag = Qt;
3217
3218 Qvariable_documentation = intern ("variable-documentation");
3219 staticpro (&Qvariable_documentation);
3220
3221 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3222 read_buffer = (char *) xmalloc (read_buffer_size);
3223 }
3224 \f
3225 void
3226 defsubr (sname)
3227 struct Lisp_Subr *sname;
3228 {
3229 Lisp_Object sym;
3230 sym = intern (sname->symbol_name);
3231 XSETSUBR (XSYMBOL (sym)->function, sname);
3232 }
3233
3234 #ifdef NOTDEF /* use fset in subr.el now */
3235 void
3236 defalias (sname, string)
3237 struct Lisp_Subr *sname;
3238 char *string;
3239 {
3240 Lisp_Object sym;
3241 sym = intern (string);
3242 XSETSUBR (XSYMBOL (sym)->function, sname);
3243 }
3244 #endif /* NOTDEF */
3245
3246 /* Define an "integer variable"; a symbol whose value is forwarded
3247 to a C variable of type int. Sample call: */
3248 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3249 void
3250 defvar_int (namestring, address)
3251 char *namestring;
3252 EMACS_INT *address;
3253 {
3254 Lisp_Object sym, val;
3255 sym = intern (namestring);
3256 val = allocate_misc ();
3257 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3258 XINTFWD (val)->intvar = address;
3259 SET_SYMBOL_VALUE (sym, val);
3260 }
3261
3262 /* Similar but define a variable whose value is t if address contains 1,
3263 nil if address contains 0 */
3264 void
3265 defvar_bool (namestring, address)
3266 char *namestring;
3267 int *address;
3268 {
3269 Lisp_Object sym, val;
3270 sym = intern (namestring);
3271 val = allocate_misc ();
3272 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3273 XBOOLFWD (val)->boolvar = address;
3274 SET_SYMBOL_VALUE (sym, val);
3275 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3276 }
3277
3278 /* Similar but define a variable whose value is the Lisp Object stored
3279 at address. Two versions: with and without gc-marking of the C
3280 variable. The nopro version is used when that variable will be
3281 gc-marked for some other reason, since marking the same slot twice
3282 can cause trouble with strings. */
3283 void
3284 defvar_lisp_nopro (namestring, address)
3285 char *namestring;
3286 Lisp_Object *address;
3287 {
3288 Lisp_Object sym, val;
3289 sym = intern (namestring);
3290 val = allocate_misc ();
3291 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3292 XOBJFWD (val)->objvar = address;
3293 SET_SYMBOL_VALUE (sym, val);
3294 }
3295
3296 void
3297 defvar_lisp (namestring, address)
3298 char *namestring;
3299 Lisp_Object *address;
3300 {
3301 defvar_lisp_nopro (namestring, address);
3302 staticpro (address);
3303 }
3304
3305 /* Similar but define a variable whose value is the Lisp Object stored in
3306 the current buffer. address is the address of the slot in the buffer
3307 that is current now. */
3308
3309 void
3310 defvar_per_buffer (namestring, address, type, doc)
3311 char *namestring;
3312 Lisp_Object *address;
3313 Lisp_Object type;
3314 char *doc;
3315 {
3316 Lisp_Object sym, val;
3317 int offset;
3318 extern struct buffer buffer_local_symbols;
3319
3320 sym = intern (namestring);
3321 val = allocate_misc ();
3322 offset = (char *)address - (char *)current_buffer;
3323
3324 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3325 XBUFFER_OBJFWD (val)->offset = offset;
3326 SET_SYMBOL_VALUE (sym, val);
3327 PER_BUFFER_SYMBOL (offset) = sym;
3328 PER_BUFFER_TYPE (offset) = type;
3329
3330 if (PER_BUFFER_IDX (offset) == 0)
3331 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3332 slot of buffer_local_flags */
3333 abort ();
3334 }
3335
3336
3337 /* Similar but define a variable whose value is the Lisp Object stored
3338 at a particular offset in the current kboard object. */
3339
3340 void
3341 defvar_kboard (namestring, offset)
3342 char *namestring;
3343 int offset;
3344 {
3345 Lisp_Object sym, val;
3346 sym = intern (namestring);
3347 val = allocate_misc ();
3348 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3349 XKBOARD_OBJFWD (val)->offset = offset;
3350 SET_SYMBOL_VALUE (sym, val);
3351 }
3352 \f
3353 /* Record the value of load-path used at the start of dumping
3354 so we can see if the site changed it later during dumping. */
3355 static Lisp_Object dump_path;
3356
3357 void
3358 init_lread ()
3359 {
3360 char *normal;
3361 int turn_off_warning = 0;
3362
3363 /* Compute the default load-path. */
3364 #ifdef CANNOT_DUMP
3365 normal = PATH_LOADSEARCH;
3366 Vload_path = decode_env_path (0, normal);
3367 #else
3368 if (NILP (Vpurify_flag))
3369 normal = PATH_LOADSEARCH;
3370 else
3371 normal = PATH_DUMPLOADSEARCH;
3372
3373 /* In a dumped Emacs, we normally have to reset the value of
3374 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3375 uses ../lisp, instead of the path of the installed elisp
3376 libraries. However, if it appears that Vload_path was changed
3377 from the default before dumping, don't override that value. */
3378 if (initialized)
3379 {
3380 if (! NILP (Fequal (dump_path, Vload_path)))
3381 {
3382 Vload_path = decode_env_path (0, normal);
3383 if (!NILP (Vinstallation_directory))
3384 {
3385 Lisp_Object tem, tem1, sitelisp;
3386
3387 /* Remove site-lisp dirs from path temporarily and store
3388 them in sitelisp, then conc them on at the end so
3389 they're always first in path. */
3390 sitelisp = Qnil;
3391 while (1)
3392 {
3393 tem = Fcar (Vload_path);
3394 tem1 = Fstring_match (build_string ("site-lisp"),
3395 tem, Qnil);
3396 if (!NILP (tem1))
3397 {
3398 Vload_path = Fcdr (Vload_path);
3399 sitelisp = Fcons (tem, sitelisp);
3400 }
3401 else
3402 break;
3403 }
3404
3405 /* Add to the path the lisp subdir of the
3406 installation dir, if it exists. */
3407 tem = Fexpand_file_name (build_string ("lisp"),
3408 Vinstallation_directory);
3409 tem1 = Ffile_exists_p (tem);
3410 if (!NILP (tem1))
3411 {
3412 if (NILP (Fmember (tem, Vload_path)))
3413 {
3414 turn_off_warning = 1;
3415 Vload_path = Fcons (tem, Vload_path);
3416 }
3417 }
3418 else
3419 /* That dir doesn't exist, so add the build-time
3420 Lisp dirs instead. */
3421 Vload_path = nconc2 (Vload_path, dump_path);
3422
3423 /* Add leim under the installation dir, if it exists. */
3424 tem = Fexpand_file_name (build_string ("leim"),
3425 Vinstallation_directory);
3426 tem1 = Ffile_exists_p (tem);
3427 if (!NILP (tem1))
3428 {
3429 if (NILP (Fmember (tem, Vload_path)))
3430 Vload_path = Fcons (tem, Vload_path);
3431 }
3432
3433 /* Add site-list under the installation dir, if it exists. */
3434 tem = Fexpand_file_name (build_string ("site-lisp"),
3435 Vinstallation_directory);
3436 tem1 = Ffile_exists_p (tem);
3437 if (!NILP (tem1))
3438 {
3439 if (NILP (Fmember (tem, Vload_path)))
3440 Vload_path = Fcons (tem, Vload_path);
3441 }
3442
3443 /* If Emacs was not built in the source directory,
3444 and it is run from where it was built, add to load-path
3445 the lisp, leim and site-lisp dirs under that directory. */
3446
3447 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3448 {
3449 Lisp_Object tem2;
3450
3451 tem = Fexpand_file_name (build_string ("src/Makefile"),
3452 Vinstallation_directory);
3453 tem1 = Ffile_exists_p (tem);
3454
3455 /* Don't be fooled if they moved the entire source tree
3456 AFTER dumping Emacs. If the build directory is indeed
3457 different from the source dir, src/Makefile.in and
3458 src/Makefile will not be found together. */
3459 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3460 Vinstallation_directory);
3461 tem2 = Ffile_exists_p (tem);
3462 if (!NILP (tem1) && NILP (tem2))
3463 {
3464 tem = Fexpand_file_name (build_string ("lisp"),
3465 Vsource_directory);
3466
3467 if (NILP (Fmember (tem, Vload_path)))
3468 Vload_path = Fcons (tem, Vload_path);
3469
3470 tem = Fexpand_file_name (build_string ("leim"),
3471 Vsource_directory);
3472
3473 if (NILP (Fmember (tem, Vload_path)))
3474 Vload_path = Fcons (tem, Vload_path);
3475
3476 tem = Fexpand_file_name (build_string ("site-lisp"),
3477 Vsource_directory);
3478
3479 if (NILP (Fmember (tem, Vload_path)))
3480 Vload_path = Fcons (tem, Vload_path);
3481 }
3482 }
3483 if (!NILP (sitelisp))
3484 Vload_path = nconc2 (Fnreverse (sitelisp), Vload_path);
3485 }
3486 }
3487 }
3488 else
3489 {
3490 /* NORMAL refers to the lisp dir in the source directory. */
3491 /* We used to add ../lisp at the front here, but
3492 that caused trouble because it was copied from dump_path
3493 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3494 It should be unnecessary. */
3495 Vload_path = decode_env_path (0, normal);
3496 dump_path = Vload_path;
3497 }
3498 #endif
3499
3500 #ifndef WINDOWSNT
3501 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3502 almost never correct, thereby causing a warning to be printed out that
3503 confuses users. Since PATH_LOADSEARCH is always overridden by the
3504 EMACSLOADPATH environment variable below, disable the warning on NT. */
3505
3506 /* Warn if dirs in the *standard* path don't exist. */
3507 if (!turn_off_warning)
3508 {
3509 Lisp_Object path_tail;
3510
3511 for (path_tail = Vload_path;
3512 !NILP (path_tail);
3513 path_tail = XCDR (path_tail))
3514 {
3515 Lisp_Object dirfile;
3516 dirfile = Fcar (path_tail);
3517 if (STRINGP (dirfile))
3518 {
3519 dirfile = Fdirectory_file_name (dirfile);
3520 if (access (XSTRING (dirfile)->data, 0) < 0)
3521 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3522 XCAR (path_tail));
3523 }
3524 }
3525 }
3526 #endif /* WINDOWSNT */
3527
3528 /* If the EMACSLOADPATH environment variable is set, use its value.
3529 This doesn't apply if we're dumping. */
3530 #ifndef CANNOT_DUMP
3531 if (NILP (Vpurify_flag)
3532 && egetenv ("EMACSLOADPATH"))
3533 #endif
3534 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3535
3536 Vvalues = Qnil;
3537
3538 load_in_progress = 0;
3539 Vload_file_name = Qnil;
3540
3541 load_descriptor_list = Qnil;
3542
3543 Vstandard_input = Qt;
3544 Vloads_in_progress = Qnil;
3545 }
3546
3547 /* Print a warning, using format string FORMAT, that directory DIRNAME
3548 does not exist. Print it on stderr and put it in *Message*. */
3549
3550 void
3551 dir_warning (format, dirname)
3552 char *format;
3553 Lisp_Object dirname;
3554 {
3555 char *buffer
3556 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3557
3558 fprintf (stderr, format, XSTRING (dirname)->data);
3559 sprintf (buffer, format, XSTRING (dirname)->data);
3560 /* Don't log the warning before we've initialized!! */
3561 if (initialized)
3562 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3563 }
3564
3565 void
3566 syms_of_lread ()
3567 {
3568 defsubr (&Sread);
3569 defsubr (&Sread_from_string);
3570 defsubr (&Sintern);
3571 defsubr (&Sintern_soft);
3572 defsubr (&Sunintern);
3573 defsubr (&Sload);
3574 defsubr (&Seval_buffer);
3575 defsubr (&Seval_region);
3576 defsubr (&Sread_char);
3577 defsubr (&Sread_char_exclusive);
3578 defsubr (&Sread_event);
3579 defsubr (&Sget_file_char);
3580 defsubr (&Smapatoms);
3581
3582 DEFVAR_LISP ("obarray", &Vobarray,
3583 doc: /* Symbol table for use by `intern' and `read'.
3584 It is a vector whose length ought to be prime for best results.
3585 The vector's contents don't make sense if examined from Lisp programs;
3586 to find all the symbols in an obarray, use `mapatoms'. */);
3587
3588 DEFVAR_LISP ("values", &Vvalues,
3589 doc: /* List of values of all expressions which were read, evaluated and printed.
3590 Order is reverse chronological. */);
3591
3592 DEFVAR_LISP ("standard-input", &Vstandard_input,
3593 doc: /* Stream for read to get input from.
3594 See documentation of `read' for possible values. */);
3595 Vstandard_input = Qt;
3596
3597 DEFVAR_LISP ("load-path", &Vload_path,
3598 doc: /* *List of directories to search for files to load.
3599 Each element is a string (directory name) or nil (try default directory).
3600 Initialized based on EMACSLOADPATH environment variable, if any,
3601 otherwise to default specified by file `epaths.h' when Emacs was built. */);
3602
3603 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
3604 doc: /* *List of suffixes to try for files to load.
3605 This list should not include the empty string. */);
3606 Vload_suffixes = Fcons (build_string (".elc"),
3607 Fcons (build_string (".el"), Qnil));
3608 /* We don't use empty_string because it's not initialized yet. */
3609 default_suffixes = Fcons (build_string (""), Qnil);
3610 staticpro (&default_suffixes);
3611
3612 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3613 doc: /* Non-nil iff inside of `load'. */);
3614
3615 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3616 doc: /* An alist of expressions to be evalled when particular files are loaded.
3617 Each element looks like (FILENAME FORMS...).
3618 When `load' is run and the file-name argument is FILENAME,
3619 the FORMS in the corresponding element are executed at the end of loading.
3620
3621 FILENAME must match exactly! Normally FILENAME is the name of a library,
3622 with no directory specified, since that is how `load' is normally called.
3623 An error in FORMS does not undo the load,
3624 but does prevent execution of the rest of the FORMS.
3625 FILENAME can also be a symbol (a feature) and FORMS are then executed
3626 when the corresponding call to `provide' is made. */);
3627 Vafter_load_alist = Qnil;
3628
3629 DEFVAR_LISP ("load-history", &Vload_history,
3630 doc: /* Alist mapping source file names to symbols and features.
3631 Each alist element is a list that starts with a file name,
3632 except for one element (optional) that starts with nil and describes
3633 definitions evaluated from buffers not visiting files.
3634 The remaining elements of each list are symbols defined as functions
3635 or variables, and cons cells `(provide . FEATURE)', `(require . FEATURE)',
3636 and `(autoload . SYMBOL)'. */);
3637 Vload_history = Qnil;
3638
3639 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3640 doc: /* Full name of file being loaded by `load'. */);
3641 Vload_file_name = Qnil;
3642
3643 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3644 doc: /* File name, including directory, of user's initialization file.
3645 If the file loaded had extension `.elc' and there was a corresponding `.el'
3646 file, this variable contains the name of the .el file, suitable for use
3647 by functions like `custom-save-all' which edit the init file. */);
3648 Vuser_init_file = Qnil;
3649
3650 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3651 doc: /* Used for internal purposes by `load'. */);
3652 Vcurrent_load_list = Qnil;
3653
3654 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3655 doc: /* Function used by `load' and `eval-region' for reading expressions.
3656 The default is nil, which means use the function `read'. */);
3657 Vload_read_function = Qnil;
3658
3659 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3660 doc: /* Function called in `load' for loading an Emacs lisp source file.
3661 This function is for doing code conversion before reading the source file.
3662 If nil, loading is done without any code conversion.
3663 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
3664 FULLNAME is the full name of FILE.
3665 See `load' for the meaning of the remaining arguments. */);
3666 Vload_source_file_function = Qnil;
3667
3668 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3669 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
3670 This is useful when the file being loaded is a temporary copy. */);
3671 load_force_doc_strings = 0;
3672
3673 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3674 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
3675 This is normally bound by `load' and `eval-buffer' to control `read',
3676 and is not meant for users to change. */);
3677 load_convert_to_unibyte = 0;
3678
3679 DEFVAR_LISP ("source-directory", &Vsource_directory,
3680 doc: /* Directory in which Emacs sources were found when Emacs was built.
3681 You cannot count on them to still be there! */);
3682 Vsource_directory
3683 = Fexpand_file_name (build_string ("../"),
3684 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3685
3686 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3687 doc: /* List of files that were preloaded (when dumping Emacs). */);
3688 Vpreloaded_file_list = Qnil;
3689
3690 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3691 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
3692 Vbyte_boolean_vars = Qnil;
3693
3694 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
3695 doc: /* Non-nil means load dangerous compiled Lisp files.
3696 Some versions of XEmacs use different byte codes than Emacs. These
3697 incompatible byte codes can make Emacs crash when it tries to execute
3698 them. */);
3699 load_dangerous_libraries = 0;
3700
3701 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
3702 doc: /* Regular expression matching safe to load compiled Lisp files.
3703 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
3704 from the file, and matches them against this regular expression.
3705 When the regular expression matches, the file is considered to be safe
3706 to load. See also `load-dangerous-libraries'. */);
3707 Vbytecomp_version_regexp
3708 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
3709
3710 /* Vsource_directory was initialized in init_lread. */
3711
3712 load_descriptor_list = Qnil;
3713 staticpro (&load_descriptor_list);
3714
3715 Qcurrent_load_list = intern ("current-load-list");
3716 staticpro (&Qcurrent_load_list);
3717
3718 Qstandard_input = intern ("standard-input");
3719 staticpro (&Qstandard_input);
3720
3721 Qread_char = intern ("read-char");
3722 staticpro (&Qread_char);
3723
3724 Qget_file_char = intern ("get-file-char");
3725 staticpro (&Qget_file_char);
3726
3727 Qbackquote = intern ("`");
3728 staticpro (&Qbackquote);
3729 Qcomma = intern (",");
3730 staticpro (&Qcomma);
3731 Qcomma_at = intern (",@");
3732 staticpro (&Qcomma_at);
3733 Qcomma_dot = intern (",.");
3734 staticpro (&Qcomma_dot);
3735
3736 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3737 staticpro (&Qinhibit_file_name_operation);
3738
3739 Qascii_character = intern ("ascii-character");
3740 staticpro (&Qascii_character);
3741
3742 Qfunction = intern ("function");
3743 staticpro (&Qfunction);
3744
3745 Qload = intern ("load");
3746 staticpro (&Qload);
3747
3748 Qload_file_name = intern ("load-file-name");
3749 staticpro (&Qload_file_name);
3750
3751 staticpro (&dump_path);
3752
3753 staticpro (&read_objects);
3754 read_objects = Qnil;
3755 staticpro (&seen_list);
3756
3757 Vloads_in_progress = Qnil;
3758 staticpro (&Vloads_in_progress);
3759 }