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