1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
30 #include "intervals.h"
38 #include "termhooks.h"
42 #include <sys/inode.h>
47 #include <unistd.h> /* to get X_OK */
60 #ifdef LISP_FLOAT_TYPE
66 #endif /* LISP_FLOAT_TYPE */
70 #endif /* HAVE_SETLOCALE */
78 Lisp_Object Qread_char
, Qget_file_char
, Qstandard_input
, Qcurrent_load_list
;
79 Lisp_Object Qvariable_documentation
, Vvalues
, Vstandard_input
, Vafter_load_alist
;
80 Lisp_Object Qascii_character
, Qload
, Qload_file_name
;
81 Lisp_Object Qbackquote
, Qcomma
, Qcomma_at
, Qcomma_dot
, Qfunction
;
82 Lisp_Object Qinhibit_file_name_operation
;
84 extern Lisp_Object Qevent_symbol_element_mask
;
85 extern Lisp_Object Qfile_exists_p
;
87 /* non-zero if inside `load' */
90 /* Directory in which the sources were found. */
91 Lisp_Object Vsource_directory
;
93 /* Search path for files to be loaded. */
94 Lisp_Object Vload_path
;
96 /* File name of user's init file. */
97 Lisp_Object Vuser_init_file
;
99 /* This is the user-visible association list that maps features to
100 lists of defs in their load files. */
101 Lisp_Object Vload_history
;
103 /* This is used to build the load history. */
104 Lisp_Object Vcurrent_load_list
;
106 /* List of files that were preloaded. */
107 Lisp_Object Vpreloaded_file_list
;
109 /* Name of file actually being read by `load'. */
110 Lisp_Object Vload_file_name
;
112 /* Function to use for reading, in `load' and friends. */
113 Lisp_Object Vload_read_function
;
115 /* The association list of objects read with the #n=object form.
116 Each member of the list has the form (n . object), and is used to
117 look up the object for the corresponding #n# construct.
118 It must be set to nil before all top-level calls to read0. */
119 Lisp_Object read_objects
;
121 /* Nonzero means load should forcibly load all dynamic doc strings. */
122 static int load_force_doc_strings
;
124 /* Nonzero means read should convert strings to unibyte. */
125 static int load_convert_to_unibyte
;
127 /* Function to use for loading an Emacs lisp source file (not
128 compiled) instead of readevalloop. */
129 Lisp_Object Vload_source_file_function
;
131 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
132 Lisp_Object Vbyte_boolean_vars
;
134 /* List of descriptors now open for Fload. */
135 static Lisp_Object load_descriptor_list
;
137 /* File for get_file_char to read from. Use by load. */
138 static FILE *instream
;
140 /* When nonzero, read conses in pure space */
141 static int read_pure
;
143 /* For use within read-from-string (this reader is non-reentrant!!) */
144 static int read_from_string_index
;
145 static int read_from_string_index_byte
;
146 static int read_from_string_limit
;
148 /* Number of bytes left to read in the buffer character
149 that `readchar' has already advanced over. */
150 static int readchar_backlog
;
152 /* This contains the last string skipped with #@. */
153 static char *saved_doc_string
;
154 /* Length of buffer allocated in saved_doc_string. */
155 static int saved_doc_string_size
;
156 /* Length of actual data in saved_doc_string. */
157 static int saved_doc_string_length
;
158 /* This is the file position that string came from. */
159 static int saved_doc_string_position
;
161 /* This contains the previous string skipped with #@.
162 We copy it from saved_doc_string when a new string
163 is put in saved_doc_string. */
164 static char *prev_saved_doc_string
;
165 /* Length of buffer allocated in prev_saved_doc_string. */
166 static int prev_saved_doc_string_size
;
167 /* Length of actual data in prev_saved_doc_string. */
168 static int prev_saved_doc_string_length
;
169 /* This is the file position that string came from. */
170 static int prev_saved_doc_string_position
;
172 /* Nonzero means inside a new-style backquote
173 with no surrounding parentheses.
174 Fread initializes this to zero, so we need not specbind it
175 or worry about what happens to it when there is an error. */
176 static int new_backquote_flag
;
178 /* Handle unreading and rereading of characters.
179 Write READCHAR to read a character,
180 UNREAD(c) to unread c to be read again.
182 These macros actually read/unread a byte code, multibyte characters
183 are not handled here. The caller should manage them if necessary.
186 #define READCHAR readchar (readcharfun)
187 #define UNREAD(c) unreadchar (readcharfun, c)
190 readchar (readcharfun
)
191 Lisp_Object readcharfun
;
196 if (BUFFERP (readcharfun
))
198 register struct buffer
*inbuffer
= XBUFFER (readcharfun
);
200 int pt_byte
= BUF_PT_BYTE (inbuffer
);
201 int orig_pt_byte
= pt_byte
;
203 if (readchar_backlog
> 0)
204 /* We get the address of the byte just passed,
205 which is the last byte of the character.
206 The other bytes in this character are consecutive with it,
207 because the gap can't be in the middle of a character. */
208 return *(BUF_BYTE_ADDRESS (inbuffer
, BUF_PT_BYTE (inbuffer
) - 1)
209 - --readchar_backlog
);
211 if (pt_byte
>= BUF_ZV_BYTE (inbuffer
))
214 readchar_backlog
= -1;
216 if (! NILP (inbuffer
->enable_multibyte_characters
))
218 unsigned char workbuf
[4];
219 unsigned char *str
= workbuf
;
222 /* Fetch the character code from the buffer. */
223 unsigned char *p
= BUF_BYTE_ADDRESS (inbuffer
, pt_byte
);
224 BUF_INC_POS (inbuffer
, pt_byte
);
225 c
= STRING_CHAR (p
, pt_byte
- orig_pt_byte
);
227 /* Find the byte-sequence representation of that character. */
228 if (SINGLE_BYTE_CHAR_P (c
))
229 length
= 1, workbuf
[0] = c
;
231 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
233 /* If the bytes for this character in the buffer
234 are not identical with what the character code implies,
235 read the bytes one by one from the buffer. */
236 if (length
!= pt_byte
- orig_pt_byte
237 || (length
== 1 ? *str
!= *p
: bcmp (str
, p
, length
)))
239 readchar_backlog
= pt_byte
- orig_pt_byte
;
240 c
= BUF_FETCH_BYTE (inbuffer
, orig_pt_byte
);
246 c
= BUF_FETCH_BYTE (inbuffer
, pt_byte
);
249 SET_BUF_PT_BOTH (inbuffer
, BUF_PT (inbuffer
) + 1, pt_byte
);
253 if (MARKERP (readcharfun
))
255 register struct buffer
*inbuffer
= XMARKER (readcharfun
)->buffer
;
257 int bytepos
= marker_byte_position (readcharfun
);
258 int orig_bytepos
= bytepos
;
260 if (readchar_backlog
> 0)
261 /* We get the address of the byte just passed,
262 which is the last byte of the character.
263 The other bytes in this character are consecutive with it,
264 because the gap can't be in the middle of a character. */
265 return *(BUF_BYTE_ADDRESS (inbuffer
, XMARKER (readcharfun
)->bytepos
- 1)
266 - --readchar_backlog
);
268 if (bytepos
>= BUF_ZV_BYTE (inbuffer
))
271 readchar_backlog
= -1;
273 if (! NILP (inbuffer
->enable_multibyte_characters
))
275 unsigned char workbuf
[4];
276 unsigned char *str
= workbuf
;
279 /* Fetch the character code from the buffer. */
280 unsigned char *p
= BUF_BYTE_ADDRESS (inbuffer
, bytepos
);
281 BUF_INC_POS (inbuffer
, bytepos
);
282 c
= STRING_CHAR (p
, bytepos
- orig_bytepos
);
284 /* Find the byte-sequence representation of that character. */
285 if (SINGLE_BYTE_CHAR_P (c
))
286 length
= 1, workbuf
[0] = c
;
288 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
290 /* If the bytes for this character in the buffer
291 are not identical with what the character code implies,
292 read the bytes one by one from the buffer. */
293 if (length
!= bytepos
- orig_bytepos
294 || (length
== 1 ? *str
!= *p
: bcmp (str
, p
, length
)))
296 readchar_backlog
= bytepos
- orig_bytepos
;
297 c
= BUF_FETCH_BYTE (inbuffer
, orig_bytepos
);
303 c
= BUF_FETCH_BYTE (inbuffer
, bytepos
);
307 XMARKER (readcharfun
)->bytepos
= bytepos
;
308 XMARKER (readcharfun
)->charpos
++;
313 if (EQ (readcharfun
, Qlambda
))
314 return read_bytecode_char (0);
316 if (EQ (readcharfun
, Qget_file_char
))
320 /* Interrupted reads have been observed while reading over the network */
321 while (c
== EOF
&& ferror (instream
) && errno
== EINTR
)
330 if (STRINGP (readcharfun
))
332 if (read_from_string_index
>= read_from_string_limit
)
334 else if (STRING_MULTIBYTE (readcharfun
))
335 FETCH_STRING_CHAR_ADVANCE (c
, readcharfun
,
336 read_from_string_index
,
337 read_from_string_index_byte
);
339 c
= XSTRING (readcharfun
)->data
[read_from_string_index
++];
344 tem
= call0 (readcharfun
);
351 /* Unread the character C in the way appropriate for the stream READCHARFUN.
352 If the stream is a user function, call it with the char as argument. */
355 unreadchar (readcharfun
, c
)
356 Lisp_Object readcharfun
;
360 /* Don't back up the pointer if we're unreading the end-of-input mark,
361 since readchar didn't advance it when we read it. */
363 else if (BUFFERP (readcharfun
))
365 struct buffer
*b
= XBUFFER (readcharfun
);
366 int bytepos
= BUF_PT_BYTE (b
);
368 if (readchar_backlog
>= 0)
373 if (! NILP (b
->enable_multibyte_characters
))
374 BUF_DEC_POS (b
, bytepos
);
378 BUF_PT_BYTE (b
) = bytepos
;
381 else if (MARKERP (readcharfun
))
383 struct buffer
*b
= XMARKER (readcharfun
)->buffer
;
384 int bytepos
= XMARKER (readcharfun
)->bytepos
;
386 if (readchar_backlog
>= 0)
390 XMARKER (readcharfun
)->charpos
--;
391 if (! NILP (b
->enable_multibyte_characters
))
392 BUF_DEC_POS (b
, bytepos
);
396 XMARKER (readcharfun
)->bytepos
= bytepos
;
399 else if (STRINGP (readcharfun
))
401 read_from_string_index
--;
402 read_from_string_index_byte
403 = string_char_to_byte (readcharfun
, read_from_string_index
);
405 else if (EQ (readcharfun
, Qlambda
))
406 read_bytecode_char (1);
407 else if (EQ (readcharfun
, Qget_file_char
))
408 ungetc (c
, instream
);
410 call1 (readcharfun
, make_number (c
));
413 static Lisp_Object
read0 (), read1 (), read_list (), read_vector ();
414 static int read_multibyte ();
415 static Lisp_Object
substitute_object_recurse ();
416 static void substitute_object_in_subtree (), substitute_in_interval ();
419 /* Get a character from the tty. */
421 extern Lisp_Object
read_char ();
423 /* Read input events until we get one that's acceptable for our purposes.
425 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
426 until we get a character we like, and then stuffed into
429 If ASCII_REQUIRED is non-zero, we check function key events to see
430 if the unmodified version of the symbol has a Qascii_character
431 property, and use that character, if present.
433 If ERROR_NONASCII is non-zero, we signal an error if the input we
434 get isn't an ASCII character with modifiers. If it's zero but
435 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
438 If INPUT_METHOD is nonzero, we invoke the current input method
439 if the character warrants that. */
442 read_filtered_event (no_switch_frame
, ascii_required
, error_nonascii
,
444 int no_switch_frame
, ascii_required
, error_nonascii
, input_method
;
447 return make_number (getchar ());
449 register Lisp_Object val
, delayed_switch_frame
;
451 delayed_switch_frame
= Qnil
;
453 /* Read until we get an acceptable event. */
455 val
= read_char (0, 0, 0,
456 (input_method
? Qnil
: Qt
),
462 /* switch-frame events are put off until after the next ASCII
463 character. This is better than signaling an error just because
464 the last characters were typed to a separate minibuffer frame,
465 for example. Eventually, some code which can deal with
466 switch-frame events will read it and process it. */
468 && EVENT_HAS_PARAMETERS (val
)
469 && EQ (EVENT_HEAD (val
), Qswitch_frame
))
471 delayed_switch_frame
= val
;
477 /* Convert certain symbols to their ASCII equivalents. */
480 Lisp_Object tem
, tem1
;
481 tem
= Fget (val
, Qevent_symbol_element_mask
);
484 tem1
= Fget (Fcar (tem
), Qascii_character
);
485 /* Merge this symbol's modifier bits
486 with the ASCII equivalent of its basic code. */
488 XSETFASTINT (val
, XINT (tem1
) | XINT (Fcar (Fcdr (tem
))));
492 /* If we don't have a character now, deal with it appropriately. */
497 Vunread_command_events
= Fcons (val
, Qnil
);
498 error ("Non-character input-event");
505 if (! NILP (delayed_switch_frame
))
506 unread_switch_frame
= delayed_switch_frame
;
512 DEFUN ("read-char", Fread_char
, Sread_char
, 0, 2, 0,
513 "Read a character from the command input (keyboard or macro).\n\
514 It is returned as a number.\n\
515 If the user generates an event which is not a character (i.e. a mouse\n\
516 click or function key event), `read-char' signals an error. As an\n\
517 exception, switch-frame events are put off until non-ASCII events can\n\
519 If you want to read non-character events, or ignore them, call\n\
520 `read-event' or `read-char-exclusive' instead.\n\
522 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
523 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
524 input method is turned on in the current buffer, that input method\n\
525 is used for reading a character.")
526 (prompt
, inherit_input_method
)
527 Lisp_Object prompt
, inherit_input_method
;
530 message_with_string ("%s", prompt
, 0);
531 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method
));
534 DEFUN ("read-event", Fread_event
, Sread_event
, 0, 2, 0,
535 "Read an event object from the input stream.\n\
536 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
537 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
538 input method is turned on in the current buffer, that input method\n\
539 is used for reading a character.")
540 (prompt
, inherit_input_method
)
541 Lisp_Object prompt
, inherit_input_method
;
544 message_with_string ("%s", prompt
, 0);
545 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method
));
548 DEFUN ("read-char-exclusive", Fread_char_exclusive
, Sread_char_exclusive
, 0, 2, 0,
549 "Read a character from the command input (keyboard or macro).\n\
550 It is returned as a number. Non-character events are ignored.\n\
552 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
553 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
554 input method is turned on in the current buffer, that input method\n\
555 is used for reading a character.")
556 (prompt
, inherit_input_method
)
557 Lisp_Object prompt
, inherit_input_method
;
560 message_with_string ("%s", prompt
, 0);
561 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method
));
564 DEFUN ("get-file-char", Fget_file_char
, Sget_file_char
, 0, 0, 0,
565 "Don't use this yourself.")
568 register Lisp_Object val
;
569 XSETINT (val
, getc (instream
));
573 static void readevalloop ();
574 static Lisp_Object
load_unwind ();
575 static Lisp_Object
load_descriptor_unwind ();
577 DEFUN ("load", Fload
, Sload
, 1, 5, 0,
578 "Execute a file of Lisp code named FILE.\n\
579 First try FILE with `.elc' appended, then try with `.el',\n\
580 then try FILE unmodified.\n\
581 This function searches the directories in `load-path'.\n\
582 If optional second arg NOERROR is non-nil,\n\
583 report no error if FILE doesn't exist.\n\
584 Print messages at start and end of loading unless\n\
585 optional third arg NOMESSAGE is non-nil.\n\
586 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
587 suffixes `.elc' or `.el' to the specified name FILE.\n\
588 If optional fifth arg MUST-SUFFIX is non-nil, insist on\n\
589 the suffix `.elc' or `.el'; don't accept just FILE unless\n\
590 it ends in one of those suffixes or includes a directory name.\n\
591 Return t if file exists.")
592 (file
, noerror
, nomessage
, nosuffix
, must_suffix
)
593 Lisp_Object file
, noerror
, nomessage
, nosuffix
, must_suffix
;
595 register FILE *stream
;
596 register int fd
= -1;
597 register Lisp_Object lispstream
;
598 int count
= specpdl_ptr
- specpdl
;
602 /* 1 means we printed the ".el is newer" message. */
604 /* 1 means we are loading a compiled file. */
612 CHECK_STRING (file
, 0);
614 /* If file name is magic, call the handler. */
615 handler
= Ffind_file_name_handler (file
, Qload
);
617 return call5 (handler
, Qload
, file
, noerror
, nomessage
, nosuffix
);
619 /* Do this after the handler to avoid
620 the need to gcpro noerror, nomessage and nosuffix.
621 (Below here, we care only whether they are nil or not.) */
622 file
= Fsubstitute_in_file_name (file
);
624 /* Avoid weird lossage with null string as arg,
625 since it would try to load a directory as a Lisp file */
626 if (XSTRING (file
)->size
> 0)
628 int size
= XSTRING (file
)->size
;
632 if (! NILP (must_suffix
))
634 /* Don't insist on adding a suffix if FILE already ends with one. */
636 && !strcmp (XSTRING (file
)->data
+ size
- 3, ".el"))
639 && !strcmp (XSTRING (file
)->data
+ size
- 4, ".elc"))
641 /* Don't insist on adding a suffix
642 if the argument includes a directory name. */
643 else if (! NILP (Ffile_name_directory (file
)))
647 fd
= openp (Vload_path
, file
,
648 (!NILP (nosuffix
) ? ""
649 : ! NILP (must_suffix
) ? ".elc:.el"
659 Fsignal (Qfile_error
, Fcons (build_string ("Cannot open load file"),
660 Fcons (file
, Qnil
)));
665 if (EQ (Qt
, Vuser_init_file
))
666 Vuser_init_file
= found
;
668 /* If FD is 0, that means openp found a magic file. */
671 if (NILP (Fequal (found
, file
)))
672 /* If FOUND is a different file name from FILE,
673 find its handler even if we have already inhibited
674 the `load' operation on FILE. */
675 handler
= Ffind_file_name_handler (found
, Qt
);
677 handler
= Ffind_file_name_handler (found
, Qload
);
678 if (! NILP (handler
))
679 return call5 (handler
, Qload
, found
, noerror
, nomessage
, Qt
);
682 /* Load .elc files directly, but not when they are
683 remote and have no handler! */
684 if (!bcmp (&(XSTRING (found
)->data
[XSTRING (found
)->size
- 4]),
696 stat ((char *)XSTRING (found
)->data
, &s1
);
697 XSTRING (found
)->data
[XSTRING (found
)->size
- 1] = 0;
698 result
= stat ((char *)XSTRING (found
)->data
, &s2
);
699 if (result
>= 0 && (unsigned) s1
.st_mtime
< (unsigned) s2
.st_mtime
)
701 /* Make the progress messages mention that source is newer. */
704 /* If we won't print another message, mention this anyway. */
705 if (! NILP (nomessage
))
706 message_with_string ("Source file `%s' newer than byte-compiled file",
709 XSTRING (found
)->data
[XSTRING (found
)->size
- 1] = 'c';
713 /* We are loading a source file (*.el). */
714 if (!NILP (Vload_source_file_function
))
718 return call4 (Vload_source_file_function
, found
, file
,
719 NILP (noerror
) ? Qnil
: Qt
,
720 NILP (nomessage
) ? Qnil
: Qt
);
726 stream
= fopen ((char *) XSTRING (found
)->data
, fmode
);
727 #else /* not WINDOWSNT */
728 stream
= fdopen (fd
, fmode
);
729 #endif /* not WINDOWSNT */
733 error ("Failure to create stdio stream for %s", XSTRING (file
)->data
);
736 if (! NILP (Vpurify_flag
))
737 Vpreloaded_file_list
= Fcons (file
, Vpreloaded_file_list
);
739 if (NILP (nomessage
))
742 message_with_string ("Loading %s (source)...", file
, 1);
744 message_with_string ("Loading %s (compiled; note, source file is newer)...",
746 else /* The typical case; compiled file newer than source file. */
747 message_with_string ("Loading %s...", file
, 1);
751 lispstream
= Fcons (Qnil
, Qnil
);
752 XSETFASTINT (XCAR (lispstream
), (EMACS_UINT
)stream
>> 16);
753 XSETFASTINT (XCDR (lispstream
), (EMACS_UINT
)stream
& 0xffff);
754 record_unwind_protect (load_unwind
, lispstream
);
755 record_unwind_protect (load_descriptor_unwind
, load_descriptor_list
);
756 specbind (Qload_file_name
, found
);
757 specbind (Qinhibit_file_name_operation
, Qnil
);
759 = Fcons (make_number (fileno (stream
)), load_descriptor_list
);
761 readevalloop (Qget_file_char
, stream
, file
, Feval
, 0, Qnil
, Qnil
);
762 unbind_to (count
, Qnil
);
764 /* Run any load-hooks for this file. */
765 temp
= Fassoc (file
, Vafter_load_alist
);
767 Fprogn (Fcdr (temp
));
770 if (saved_doc_string
)
771 free (saved_doc_string
);
772 saved_doc_string
= 0;
773 saved_doc_string_size
= 0;
775 if (prev_saved_doc_string
)
776 free (prev_saved_doc_string
);
777 prev_saved_doc_string
= 0;
778 prev_saved_doc_string_size
= 0;
780 if (!noninteractive
&& NILP (nomessage
))
783 message_with_string ("Loading %s (source)...done", file
, 1);
785 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
787 else /* The typical case; compiled file newer than source file. */
788 message_with_string ("Loading %s...done", file
, 1);
794 load_unwind (stream
) /* used as unwind-protect function in load */
797 fclose ((FILE *) (XFASTINT (XCAR (stream
)) << 16
798 | XFASTINT (XCDR (stream
))));
799 if (--load_in_progress
< 0) load_in_progress
= 0;
804 load_descriptor_unwind (oldlist
)
807 load_descriptor_list
= oldlist
;
811 /* Close all descriptors in use for Floads.
812 This is used when starting a subprocess. */
819 for (tail
= load_descriptor_list
; !NILP (tail
); tail
= XCDR (tail
))
820 close (XFASTINT (XCAR (tail
)));
825 complete_filename_p (pathname
)
826 Lisp_Object pathname
;
828 register unsigned char *s
= XSTRING (pathname
)->data
;
829 return (IS_DIRECTORY_SEP (s
[0])
830 || (XSTRING (pathname
)->size
> 2
831 && IS_DEVICE_SEP (s
[1]) && IS_DIRECTORY_SEP (s
[2]))
841 /* Search for a file whose name is STR, looking in directories
842 in the Lisp list PATH, and trying suffixes from SUFFIX.
843 SUFFIX is a string containing possible suffixes separated by colons.
844 On success, returns a file descriptor. On failure, returns -1.
846 EXEC_ONLY nonzero means don't open the files,
847 just look for one that is executable. In this case,
848 returns 1 on success.
850 If STOREPTR is nonzero, it points to a slot where the name of
851 the file actually found should be stored as a Lisp string.
852 nil is stored there on failure.
854 If the file we find is remote, return 0
855 but store the found remote file name in *STOREPTR.
856 We do not check for remote files if EXEC_ONLY is nonzero. */
859 openp (path
, str
, suffix
, storeptr
, exec_only
)
860 Lisp_Object path
, str
;
862 Lisp_Object
*storeptr
;
868 register char *fn
= buf
;
871 Lisp_Object filename
;
879 if (complete_filename_p (str
))
882 for (; !NILP (path
); path
= Fcdr (path
))
886 filename
= Fexpand_file_name (str
, Fcar (path
));
887 if (!complete_filename_p (filename
))
888 /* If there are non-absolute elts in PATH (eg ".") */
889 /* Of course, this could conceivably lose if luser sets
890 default-directory to be something non-absolute... */
892 filename
= Fexpand_file_name (filename
, current_buffer
->directory
);
893 if (!complete_filename_p (filename
))
894 /* Give up on this path element! */
898 /* Calculate maximum size of any filename made from
899 this path element/specified file name and any possible suffix. */
900 want_size
= strlen (suffix
) + XSTRING (filename
)->size
+ 1;
901 if (fn_size
< want_size
)
902 fn
= (char *) alloca (fn_size
= 100 + want_size
);
906 /* Loop over suffixes. */
909 char *esuffix
= (char *) index (nsuffix
, ':');
910 int lsuffix
= esuffix
? esuffix
- nsuffix
: strlen (nsuffix
);
913 /* Concatenate path element/specified name with the suffix.
914 If the directory starts with /:, remove that. */
915 if (XSTRING (filename
)->size
> 2
916 && XSTRING (filename
)->data
[0] == '/'
917 && XSTRING (filename
)->data
[1] == ':')
919 strncpy (fn
, XSTRING (filename
)->data
+ 2,
920 XSTRING (filename
)->size
- 2);
921 fn
[XSTRING (filename
)->size
- 2] = 0;
925 strncpy (fn
, XSTRING (filename
)->data
, XSTRING (filename
)->size
);
926 fn
[XSTRING (filename
)->size
] = 0;
929 if (lsuffix
!= 0) /* Bug happens on CCI if lsuffix is 0. */
930 strncat (fn
, nsuffix
, lsuffix
);
932 /* Check that the file exists and is not a directory. */
936 handler
= Ffind_file_name_handler (filename
, Qfile_exists_p
);
937 if (! NILP (handler
) && ! exec_only
)
942 string
= build_string (fn
);
943 exists
= ! NILP (exec_only
? Ffile_executable_p (string
)
944 : Ffile_readable_p (string
));
946 && ! NILP (Ffile_directory_p (build_string (fn
))))
951 /* We succeeded; return this descriptor and filename. */
953 *storeptr
= build_string (fn
);
960 int exists
= (stat (fn
, &st
) >= 0
961 && (st
.st_mode
& S_IFMT
) != S_IFDIR
);
964 /* Check that we can access or open it. */
966 fd
= (access (fn
, X_OK
) == 0) ? 1 : -1;
968 fd
= open (fn
, O_RDONLY
, 0);
972 /* We succeeded; return this descriptor and filename. */
974 *storeptr
= build_string (fn
);
981 /* Advance to next suffix. */
984 nsuffix
+= lsuffix
+ 1;
995 /* Merge the list we've accumulated of globals from the current input source
996 into the load_history variable. The details depend on whether
997 the source has an associated file name or not. */
1000 build_load_history (stream
, source
)
1004 register Lisp_Object tail
, prev
, newelt
;
1005 register Lisp_Object tem
, tem2
;
1006 register int foundit
, loading
;
1008 loading
= stream
|| !NARROWED
;
1010 tail
= Vload_history
;
1013 while (!NILP (tail
))
1017 /* Find the feature's previous assoc list... */
1018 if (!NILP (Fequal (source
, Fcar (tem
))))
1022 /* If we're loading, remove it. */
1026 Vload_history
= Fcdr (tail
);
1028 Fsetcdr (prev
, Fcdr (tail
));
1031 /* Otherwise, cons on new symbols that are not already members. */
1034 tem2
= Vcurrent_load_list
;
1036 while (CONSP (tem2
))
1038 newelt
= Fcar (tem2
);
1040 if (NILP (Fmemq (newelt
, tem
)))
1041 Fsetcar (tail
, Fcons (Fcar (tem
),
1042 Fcons (newelt
, Fcdr (tem
))));
1055 /* If we're loading, cons the new assoc onto the front of load-history,
1056 the most-recently-loaded position. Also do this if we didn't find
1057 an existing member for the current source. */
1058 if (loading
|| !foundit
)
1059 Vload_history
= Fcons (Fnreverse (Vcurrent_load_list
),
1064 unreadpure () /* Used as unwind-protect function in readevalloop */
1071 readevalloop_1 (old
)
1074 load_convert_to_unibyte
= ! NILP (old
);
1078 /* UNIBYTE specifies how to set load_convert_to_unibyte
1079 for this invocation.
1080 READFUN, if non-nil, is used instead of `read'. */
1083 readevalloop (readcharfun
, stream
, sourcename
, evalfun
, printflag
, unibyte
, readfun
)
1084 Lisp_Object readcharfun
;
1086 Lisp_Object sourcename
;
1087 Lisp_Object (*evalfun
) ();
1089 Lisp_Object unibyte
, readfun
;
1092 register Lisp_Object val
;
1093 int count
= specpdl_ptr
- specpdl
;
1094 struct gcpro gcpro1
;
1095 struct buffer
*b
= 0;
1097 if (BUFFERP (readcharfun
))
1098 b
= XBUFFER (readcharfun
);
1099 else if (MARKERP (readcharfun
))
1100 b
= XMARKER (readcharfun
)->buffer
;
1102 specbind (Qstandard_input
, readcharfun
);
1103 specbind (Qcurrent_load_list
, Qnil
);
1104 record_unwind_protect (readevalloop_1
, load_convert_to_unibyte
? Qt
: Qnil
);
1105 load_convert_to_unibyte
= !NILP (unibyte
);
1107 readchar_backlog
= -1;
1109 GCPRO1 (sourcename
);
1111 LOADHIST_ATTACH (sourcename
);
1115 if (b
!= 0 && NILP (b
->name
))
1116 error ("Reading from killed buffer");
1122 while ((c
= READCHAR
) != '\n' && c
!= -1);
1127 /* Ignore whitespace here, so we can detect eof. */
1128 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r')
1131 if (!NILP (Vpurify_flag
) && c
== '(')
1133 int count1
= specpdl_ptr
- specpdl
;
1134 record_unwind_protect (unreadpure
, Qnil
);
1135 val
= read_list (-1, readcharfun
);
1136 unbind_to (count1
, Qnil
);
1141 read_objects
= Qnil
;
1142 if (! NILP (readfun
))
1143 val
= call1 (readfun
, readcharfun
);
1144 else if (! NILP (Vload_read_function
))
1145 val
= call1 (Vload_read_function
, readcharfun
);
1147 val
= read0 (readcharfun
);
1150 val
= (*evalfun
) (val
);
1153 Vvalues
= Fcons (val
, Vvalues
);
1154 if (EQ (Vstandard_output
, Qt
))
1161 build_load_history (stream
, sourcename
);
1164 unbind_to (count
, Qnil
);
1169 DEFUN ("eval-buffer", Feval_buffer
, Seval_buffer
, 0, 5, "",
1170 "Execute the current buffer as Lisp code.\n\
1171 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1172 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1173 PRINTFLAG controls printing of output:\n\
1174 nil means discard it; anything else is stream for print.\n\
1176 If the optional third argument FILENAME is non-nil,\n\
1177 it specifies the file name to use for `load-history'.\n\
1178 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1179 for this invocation.\n\
1181 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1182 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1184 This function preserves the position of point.")
1185 (buffer
, printflag
, filename
, unibyte
, do_allow_print
)
1186 Lisp_Object buffer
, printflag
, filename
, unibyte
, do_allow_print
;
1188 int count
= specpdl_ptr
- specpdl
;
1189 Lisp_Object tem
, buf
;
1192 buf
= Fcurrent_buffer ();
1194 buf
= Fget_buffer (buffer
);
1196 error ("No such buffer");
1198 if (NILP (printflag
) && NILP (do_allow_print
))
1203 if (NILP (filename
))
1204 filename
= XBUFFER (buf
)->filename
;
1206 specbind (Qstandard_output
, tem
);
1207 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1208 BUF_SET_PT (XBUFFER (buf
), BUF_BEGV (XBUFFER (buf
)));
1209 readevalloop (buf
, 0, filename
, Feval
, !NILP (printflag
), unibyte
, Qnil
);
1210 unbind_to (count
, Qnil
);
1216 XDEFUN ("eval-current-buffer", Feval_current_buffer
, Seval_current_buffer
, 0, 1, "",
1217 "Execute the current buffer as Lisp code.\n\
1218 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1219 nil means discard it; anything else is stream for print.\n\
1221 If there is no error, point does not move. If there is an error,\n\
1222 point remains at the end of the last character read from the buffer.")
1224 Lisp_Object printflag
;
1226 int count
= specpdl_ptr
- specpdl
;
1227 Lisp_Object tem
, cbuf
;
1229 cbuf
= Fcurrent_buffer ()
1231 if (NILP (printflag
))
1235 specbind (Qstandard_output
, tem
);
1236 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1238 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
,
1239 !NILP (printflag
), Qnil
, Qnil
);
1240 return unbind_to (count
, Qnil
);
1244 DEFUN ("eval-region", Feval_region
, Seval_region
, 2, 4, "r",
1245 "Execute the region as Lisp code.\n\
1246 When called from programs, expects two arguments,\n\
1247 giving starting and ending indices in the current buffer\n\
1248 of the text to be executed.\n\
1249 Programs can pass third argument PRINTFLAG which controls output:\n\
1250 nil means discard it; anything else is stream for printing it.\n\
1251 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1252 instead of `read' to read each expression. It gets one argument\n\
1253 which is the input stream for reading characters.\n\
1255 This function does not move point.")
1256 (start
, end
, printflag
, read_function
)
1257 Lisp_Object start
, end
, printflag
, read_function
;
1259 int count
= specpdl_ptr
- specpdl
;
1260 Lisp_Object tem
, cbuf
;
1262 cbuf
= Fcurrent_buffer ();
1264 if (NILP (printflag
))
1268 specbind (Qstandard_output
, tem
);
1270 if (NILP (printflag
))
1271 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1272 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
1274 /* This both uses start and checks its type. */
1276 Fnarrow_to_region (make_number (BEGV
), end
);
1277 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
,
1278 !NILP (printflag
), Qnil
, read_function
);
1280 return unbind_to (count
, Qnil
);
1283 #endif /* standalone */
1285 DEFUN ("read", Fread
, Sread
, 0, 1, 0,
1286 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1287 If STREAM is nil, use the value of `standard-input' (which see).\n\
1288 STREAM or the value of `standard-input' may be:\n\
1289 a buffer (read from point and advance it)\n\
1290 a marker (read from where it points and advance it)\n\
1291 a function (call it with no arguments for each character,\n\
1292 call it with a char as argument to push a char back)\n\
1293 a string (takes text from string, starting at the beginning)\n\
1294 t (read text line using minibuffer and use it).")
1298 extern Lisp_Object
Fread_minibuffer ();
1301 stream
= Vstandard_input
;
1302 if (EQ (stream
, Qt
))
1303 stream
= Qread_char
;
1305 readchar_backlog
= -1;
1306 new_backquote_flag
= 0;
1307 read_objects
= Qnil
;
1310 if (EQ (stream
, Qread_char
))
1311 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil
);
1314 if (STRINGP (stream
))
1315 return Fcar (Fread_from_string (stream
, Qnil
, Qnil
));
1317 return read0 (stream
);
1320 DEFUN ("read-from-string", Fread_from_string
, Sread_from_string
, 1, 3, 0,
1321 "Read one Lisp expression which is represented as text by STRING.\n\
1322 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1323 START and END optionally delimit a substring of STRING from which to read;\n\
1324 they default to 0 and (length STRING) respectively.")
1325 (string
, start
, end
)
1326 Lisp_Object string
, start
, end
;
1328 int startval
, endval
;
1331 CHECK_STRING (string
,0);
1334 endval
= XSTRING (string
)->size
;
1337 CHECK_NUMBER (end
, 2);
1338 endval
= XINT (end
);
1339 if (endval
< 0 || endval
> XSTRING (string
)->size
)
1340 args_out_of_range (string
, end
);
1347 CHECK_NUMBER (start
, 1);
1348 startval
= XINT (start
);
1349 if (startval
< 0 || startval
> endval
)
1350 args_out_of_range (string
, start
);
1353 read_from_string_index
= startval
;
1354 read_from_string_index_byte
= string_char_to_byte (string
, startval
);
1355 read_from_string_limit
= endval
;
1357 new_backquote_flag
= 0;
1358 read_objects
= Qnil
;
1360 tem
= read0 (string
);
1361 return Fcons (tem
, make_number (read_from_string_index
));
1364 /* Use this for recursive reads, in contexts where internal tokens
1369 Lisp_Object readcharfun
;
1371 register Lisp_Object val
;
1374 val
= read1 (readcharfun
, &c
, 0);
1376 Fsignal (Qinvalid_read_syntax
, Fcons (Fmake_string (make_number (1),
1383 static int read_buffer_size
;
1384 static char *read_buffer
;
1386 /* Read multibyte form and return it as a character. C is a first
1387 byte of multibyte form, and rest of them are read from
1391 read_multibyte (c
, readcharfun
)
1393 Lisp_Object readcharfun
;
1395 /* We need the actual character code of this multibyte
1397 unsigned char str
[MAX_LENGTH_OF_MULTI_BYTE_FORM
];
1401 while ((c
= READCHAR
) >= 0xA0
1402 && len
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1405 return STRING_CHAR (str
, len
);
1408 /* Read a \-escape sequence, assuming we already read the `\'. */
1411 read_escape (readcharfun
, stringp
)
1412 Lisp_Object readcharfun
;
1415 register int c
= READCHAR
;
1419 error ("End of file");
1449 error ("Invalid escape character syntax");
1452 c
= read_escape (readcharfun
, 0);
1453 return c
| meta_modifier
;
1458 error ("Invalid escape character syntax");
1461 c
= read_escape (readcharfun
, 0);
1462 return c
| shift_modifier
;
1467 error ("Invalid escape character syntax");
1470 c
= read_escape (readcharfun
, 0);
1471 return c
| hyper_modifier
;
1476 error ("Invalid escape character syntax");
1479 c
= read_escape (readcharfun
, 0);
1480 return c
| alt_modifier
;
1485 error ("Invalid escape character syntax");
1488 c
= read_escape (readcharfun
, 0);
1489 return c
| super_modifier
;
1494 error ("Invalid escape character syntax");
1498 c
= read_escape (readcharfun
, 0);
1499 if ((c
& ~CHAR_MODIFIER_MASK
) == '?')
1500 return 0177 | (c
& CHAR_MODIFIER_MASK
);
1501 else if (! SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
)))
1502 return c
| ctrl_modifier
;
1503 /* ASCII control chars are made from letters (both cases),
1504 as well as the non-letters within 0100...0137. */
1505 else if ((c
& 0137) >= 0101 && (c
& 0137) <= 0132)
1506 return (c
& (037 | ~0177));
1507 else if ((c
& 0177) >= 0100 && (c
& 0177) <= 0137)
1508 return (c
& (037 | ~0177));
1510 return c
| ctrl_modifier
;
1520 /* An octal escape, as in ANSI C. */
1522 register int i
= c
- '0';
1523 register int count
= 0;
1526 if ((c
= READCHAR
) >= '0' && c
<= '7')
1541 /* A hex escape, as in ANSI C. */
1547 if (c
>= '0' && c
<= '9')
1552 else if ((c
>= 'a' && c
<= 'f')
1553 || (c
>= 'A' && c
<= 'F'))
1556 if (c
>= 'a' && c
<= 'f')
1571 if (BASE_LEADING_CODE_P (c
))
1572 c
= read_multibyte (c
, readcharfun
);
1577 /* If the next token is ')' or ']' or '.', we store that character
1578 in *PCH and the return value is not interesting. Else, we store
1579 zero in *PCH and we read and return one lisp object.
1581 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1584 read1 (readcharfun
, pch
, first_in_list
)
1585 register Lisp_Object readcharfun
;
1590 int uninterned_symbol
= 0;
1597 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1602 return read_list (0, readcharfun
);
1605 return read_vector (readcharfun
, 0);
1622 tmp
= read_vector (readcharfun
, 0);
1623 if (XVECTOR (tmp
)->size
< CHAR_TABLE_STANDARD_SLOTS
1624 || XVECTOR (tmp
)->size
> CHAR_TABLE_STANDARD_SLOTS
+ 10)
1625 error ("Invalid size char-table");
1626 XSETCHAR_TABLE (tmp
, XCHAR_TABLE (tmp
));
1627 XCHAR_TABLE (tmp
)->top
= Qt
;
1636 tmp
= read_vector (readcharfun
, 0);
1637 if (XVECTOR (tmp
)->size
!= SUB_CHAR_TABLE_STANDARD_SLOTS
)
1638 error ("Invalid size char-table");
1639 XSETCHAR_TABLE (tmp
, XCHAR_TABLE (tmp
));
1640 XCHAR_TABLE (tmp
)->top
= Qnil
;
1643 Fsignal (Qinvalid_read_syntax
,
1644 Fcons (make_string ("#^^", 3), Qnil
));
1646 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#^", 2), Qnil
));
1651 length
= read1 (readcharfun
, pch
, first_in_list
);
1655 Lisp_Object tmp
, val
;
1656 int size_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
- 1)
1660 tmp
= read1 (readcharfun
, pch
, first_in_list
);
1661 if (size_in_chars
!= XSTRING (tmp
)->size
1662 /* We used to print 1 char too many
1663 when the number of bits was a multiple of 8.
1664 Accept such input in case it came from an old version. */
1665 && ! (XFASTINT (length
)
1666 == (XSTRING (tmp
)->size
- 1) * BITS_PER_CHAR
))
1667 Fsignal (Qinvalid_read_syntax
,
1668 Fcons (make_string ("#&...", 5), Qnil
));
1670 val
= Fmake_bool_vector (length
, Qnil
);
1671 bcopy (XSTRING (tmp
)->data
, XBOOL_VECTOR (val
)->data
,
1673 /* Clear the extraneous bits in the last byte. */
1674 if (XINT (length
) != size_in_chars
* BITS_PER_CHAR
)
1675 XBOOL_VECTOR (val
)->data
[size_in_chars
- 1]
1676 &= (1 << (XINT (length
) % BITS_PER_CHAR
)) - 1;
1679 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#&...", 5),
1684 /* Accept compiled functions at read-time so that we don't have to
1685 build them using function calls. */
1687 tmp
= read_vector (readcharfun
, 1);
1688 return Fmake_byte_code (XVECTOR (tmp
)->size
,
1689 XVECTOR (tmp
)->contents
);
1691 #ifdef USE_TEXT_PROPERTIES
1695 struct gcpro gcpro1
;
1698 /* Read the string itself. */
1699 tmp
= read1 (readcharfun
, &ch
, 0);
1700 if (ch
!= 0 || !STRINGP (tmp
))
1701 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1703 /* Read the intervals and their properties. */
1706 Lisp_Object beg
, end
, plist
;
1708 beg
= read1 (readcharfun
, &ch
, 0);
1712 end
= read1 (readcharfun
, &ch
, 0);
1714 plist
= read1 (readcharfun
, &ch
, 0);
1716 Fsignal (Qinvalid_read_syntax
,
1717 Fcons (build_string ("invalid string property list"),
1719 Fset_text_properties (beg
, end
, plist
, tmp
);
1725 /* #@NUMBER is used to skip NUMBER following characters.
1726 That's used in .elc files to skip over doc strings
1727 and function definitions. */
1732 /* Read a decimal integer. */
1733 while ((c
= READCHAR
) >= 0
1734 && c
>= '0' && c
<= '9')
1742 if (load_force_doc_strings
&& EQ (readcharfun
, Qget_file_char
))
1744 /* If we are supposed to force doc strings into core right now,
1745 record the last string that we skipped,
1746 and record where in the file it comes from. */
1748 /* But first exchange saved_doc_string
1749 with prev_saved_doc_string, so we save two strings. */
1751 char *temp
= saved_doc_string
;
1752 int temp_size
= saved_doc_string_size
;
1753 int temp_pos
= saved_doc_string_position
;
1754 int temp_len
= saved_doc_string_length
;
1756 saved_doc_string
= prev_saved_doc_string
;
1757 saved_doc_string_size
= prev_saved_doc_string_size
;
1758 saved_doc_string_position
= prev_saved_doc_string_position
;
1759 saved_doc_string_length
= prev_saved_doc_string_length
;
1761 prev_saved_doc_string
= temp
;
1762 prev_saved_doc_string_size
= temp_size
;
1763 prev_saved_doc_string_position
= temp_pos
;
1764 prev_saved_doc_string_length
= temp_len
;
1767 if (saved_doc_string_size
== 0)
1769 saved_doc_string_size
= nskip
+ 100;
1770 saved_doc_string
= (char *) xmalloc (saved_doc_string_size
);
1772 if (nskip
> saved_doc_string_size
)
1774 saved_doc_string_size
= nskip
+ 100;
1775 saved_doc_string
= (char *) xrealloc (saved_doc_string
,
1776 saved_doc_string_size
);
1779 saved_doc_string_position
= ftell (instream
);
1781 /* Copy that many characters into saved_doc_string. */
1782 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1783 saved_doc_string
[i
] = c
= READCHAR
;
1785 saved_doc_string_length
= i
;
1789 /* Skip that many characters. */
1790 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1797 return Vload_file_name
;
1799 return Fcons (Qfunction
, Fcons (read0 (readcharfun
), Qnil
));
1800 /* #:foo is the uninterned symbol named foo. */
1803 uninterned_symbol
= 1;
1807 /* Reader forms that can reuse previously read objects. */
1808 if (c
>= '0' && c
<= '9')
1813 /* Read a non-negative integer. */
1814 while (c
>= '0' && c
<= '9')
1820 /* #n=object returns object, but associates it with n for #n#. */
1823 /* Make a placeholder for #n# to use temporarily */
1824 Lisp_Object placeholder
;
1827 placeholder
= Fcons(Qnil
, Qnil
);
1828 cell
= Fcons (make_number (n
), placeholder
);
1829 read_objects
= Fcons (cell
, read_objects
);
1831 /* Read the object itself. */
1832 tem
= read0 (readcharfun
);
1834 /* Now put it everywhere the placeholder was... */
1835 substitute_object_in_subtree (tem
, placeholder
);
1837 /* ...and #n# will use the real value from now on. */
1838 Fsetcdr (cell
, tem
);
1842 /* #n# returns a previously read object. */
1845 tem
= Fassq (make_number (n
), read_objects
);
1848 /* Fall through to error message. */
1850 /* Fall through to error message. */
1854 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1857 while ((c
= READCHAR
) >= 0 && c
!= '\n');
1862 return Fcons (Qquote
, Fcons (read0 (readcharfun
), Qnil
));
1872 new_backquote_flag
= 1;
1873 value
= read0 (readcharfun
);
1874 new_backquote_flag
= 0;
1876 return Fcons (Qbackquote
, Fcons (value
, Qnil
));
1880 if (new_backquote_flag
)
1882 Lisp_Object comma_type
= Qnil
;
1887 comma_type
= Qcomma_at
;
1889 comma_type
= Qcomma_dot
;
1892 if (ch
>= 0) UNREAD (ch
);
1893 comma_type
= Qcomma
;
1896 new_backquote_flag
= 0;
1897 value
= read0 (readcharfun
);
1898 new_backquote_flag
= 1;
1899 return Fcons (comma_type
, Fcons (value
, Qnil
));
1907 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1910 c
= read_escape (readcharfun
, 0);
1911 else if (BASE_LEADING_CODE_P (c
))
1912 c
= read_multibyte (c
, readcharfun
);
1914 return make_number (c
);
1919 register char *p
= read_buffer
;
1920 register char *end
= read_buffer
+ read_buffer_size
;
1922 /* Nonzero if we saw an escape sequence specifying
1923 a multibyte character. */
1924 int force_multibyte
= 0;
1925 /* Nonzero if we saw an escape sequence specifying
1926 a single-byte character. */
1927 int force_singlebyte
= 0;
1931 while ((c
= READCHAR
) >= 0
1934 if (end
- p
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1936 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
1937 p
+= new - read_buffer
;
1938 read_buffer
+= new - read_buffer
;
1939 end
= read_buffer
+ read_buffer_size
;
1944 c
= read_escape (readcharfun
, 1);
1946 /* C is -1 if \ newline has just been seen */
1949 if (p
== read_buffer
)
1954 /* If an escape specifies a non-ASCII single-byte character,
1955 this must be a unibyte string. */
1956 if (SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
))
1957 && ! ASCII_BYTE_P ((c
& ~CHAR_MODIFIER_MASK
)))
1958 force_singlebyte
= 1;
1961 if (! SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
)))
1963 unsigned char workbuf
[4];
1964 unsigned char *str
= workbuf
;
1967 /* Any modifiers for a multibyte character are invalid. */
1968 if (c
& CHAR_MODIFIER_MASK
)
1969 error ("Invalid modifier in string");
1970 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
1972 force_multibyte
= 1;
1974 bcopy (str
, p
, length
);
1979 /* Allow `\C- ' and `\C-?'. */
1980 if (c
== (CHAR_CTL
| ' '))
1982 else if (c
== (CHAR_CTL
| '?'))
1987 /* Shift modifier is valid only with [A-Za-z]. */
1988 if ((c
& 0377) >= 'A' && (c
& 0377) <= 'Z')
1990 else if ((c
& 0377) >= 'a' && (c
& 0377) <= 'z')
1991 c
= (c
& ~CHAR_SHIFT
) - ('a' - 'A');
1995 /* Move the meta bit to the right place for a string. */
1996 c
= (c
& ~CHAR_META
) | 0x80;
1998 error ("Invalid modifier in string");
2003 return Fsignal (Qend_of_file
, Qnil
);
2005 /* If purifying, and string starts with \ newline,
2006 return zero instead. This is for doc strings
2007 that we are really going to find in etc/DOC.nn.nn */
2008 if (!NILP (Vpurify_flag
) && NILP (Vdoc_file_name
) && cancel
)
2009 return make_number (0);
2011 if (force_multibyte
)
2012 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2013 else if (force_singlebyte
)
2014 nchars
= p
- read_buffer
;
2015 else if (load_convert_to_unibyte
)
2018 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2019 if (p
- read_buffer
!= nchars
)
2021 string
= make_multibyte_string (read_buffer
, nchars
,
2023 return Fstring_make_unibyte (string
);
2026 else if (EQ (readcharfun
, Qget_file_char
)
2027 || EQ (readcharfun
, Qlambda
))
2028 /* Nowadays, reading directly from a file
2029 is used only for compiled Emacs Lisp files,
2030 and those always use the Emacs internal encoding.
2031 Meanwhile, Qlambda is used for reading dynamic byte code
2032 (compiled with byte-compile-dynamic = t). */
2033 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2035 /* In all other cases, if we read these bytes as
2036 separate characters, treat them as separate characters now. */
2037 nchars
= p
- read_buffer
;
2040 return make_pure_string (read_buffer
, nchars
, p
- read_buffer
,
2042 || (p
- read_buffer
!= nchars
)));
2043 return make_specified_string (read_buffer
, nchars
, p
- read_buffer
,
2045 || (p
- read_buffer
!= nchars
)));
2050 #ifdef LISP_FLOAT_TYPE
2051 /* If a period is followed by a number, then we should read it
2052 as a floating point number. Otherwise, it denotes a dotted
2054 int next_char
= READCHAR
;
2057 if (! (next_char
>= '0' && next_char
<= '9'))
2064 /* Otherwise, we fall through! Note that the atom-reading loop
2065 below will now loop at least once, assuring that we will not
2066 try to UNREAD two characters in a row. */
2070 if (c
<= 040) goto retry
;
2072 register char *p
= read_buffer
;
2076 register char *end
= read_buffer
+ read_buffer_size
;
2079 && !(c
== '\"' || c
== '\'' || c
== ';' || c
== '?'
2080 || c
== '(' || c
== ')'
2081 #ifndef LISP_FLOAT_TYPE
2082 /* If we have floating-point support, then we need
2083 to allow <digits><dot><digits>. */
2085 #endif /* not LISP_FLOAT_TYPE */
2086 || c
== '[' || c
== ']' || c
== '#'
2089 if (end
- p
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
2091 register char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
2092 p
+= new - read_buffer
;
2093 read_buffer
+= new - read_buffer
;
2094 end
= read_buffer
+ read_buffer_size
;
2102 if (! SINGLE_BYTE_CHAR_P (c
))
2104 unsigned char workbuf
[4];
2105 unsigned char *str
= workbuf
;
2108 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
2110 bcopy (str
, p
, length
);
2121 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
2122 p
+= new - read_buffer
;
2123 read_buffer
+= new - read_buffer
;
2124 /* end = read_buffer + read_buffer_size; */
2131 if (!quoted
&& !uninterned_symbol
)
2134 register Lisp_Object val
;
2136 if (*p1
== '+' || *p1
== '-') p1
++;
2137 /* Is it an integer? */
2140 while (p1
!= p
&& (c
= *p1
) >= '0' && c
<= '9') p1
++;
2141 #ifdef LISP_FLOAT_TYPE
2142 /* Integers can have trailing decimal points. */
2143 if (p1
> read_buffer
&& p1
< p
&& *p1
== '.') p1
++;
2146 /* It is an integer. */
2148 #ifdef LISP_FLOAT_TYPE
2152 if (sizeof (int) == sizeof (EMACS_INT
))
2153 XSETINT (val
, atoi (read_buffer
));
2154 else if (sizeof (long) == sizeof (EMACS_INT
))
2155 XSETINT (val
, atol (read_buffer
));
2161 #ifdef LISP_FLOAT_TYPE
2162 if (isfloat_string (read_buffer
))
2164 /* Compute NaN and infinities using 0.0 in a variable,
2165 to cope with compilers that think they are smarter
2171 /* Negate the value ourselves. This treats 0, NaNs,
2172 and infinity properly on IEEE floating point hosts,
2173 and works around a common bug where atof ("-0.0")
2175 int negative
= read_buffer
[0] == '-';
2177 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2178 returns 1, is if the input ends in e+INF or e+NaN. */
2185 value
= zero
/ zero
;
2188 value
= atof (read_buffer
+ negative
);
2192 return make_float (negative
? - value
: value
);
2197 if (uninterned_symbol
)
2198 return make_symbol (read_buffer
);
2200 return intern (read_buffer
);
2206 /* List of nodes we've seen during substitute_object_in_subtree. */
2207 static Lisp_Object seen_list
;
2210 substitute_object_in_subtree (object
, placeholder
)
2212 Lisp_Object placeholder
;
2214 Lisp_Object check_object
;
2216 /* We haven't seen any objects when we start. */
2219 /* Make all the substitutions. */
2221 = substitute_object_recurse (object
, placeholder
, object
);
2223 /* Clear seen_list because we're done with it. */
2226 /* The returned object here is expected to always eq the
2228 if (!EQ (check_object
, object
))
2229 error ("Unexpected mutation error in reader");
2232 /* Feval doesn't get called from here, so no gc protection is needed. */
2233 #define SUBSTITUTE(get_val, set_val) \
2235 Lisp_Object old_value = get_val; \
2236 Lisp_Object true_value \
2237 = substitute_object_recurse (object, placeholder,\
2240 if (!EQ (old_value, true_value)) \
2247 substitute_object_recurse (object
, placeholder
, subtree
)
2249 Lisp_Object placeholder
;
2250 Lisp_Object subtree
;
2252 /* If we find the placeholder, return the target object. */
2253 if (EQ (placeholder
, subtree
))
2256 /* If we've been to this node before, don't explore it again. */
2257 if (!EQ (Qnil
, Fmemq (subtree
, seen_list
)))
2260 /* If this node can be the entry point to a cycle, remember that
2261 we've seen it. It can only be such an entry point if it was made
2262 by #n=, which means that we can find it as a value in
2264 if (!EQ (Qnil
, Frassq (subtree
, read_objects
)))
2265 seen_list
= Fcons (subtree
, seen_list
);
2267 /* Recurse according to subtree's type.
2268 Every branch must return a Lisp_Object. */
2269 switch (XTYPE (subtree
))
2271 case Lisp_Vectorlike
:
2274 int length
= Flength(subtree
);
2275 for (i
= 0; i
< length
; i
++)
2277 Lisp_Object idx
= make_number (i
);
2278 SUBSTITUTE (Faref (subtree
, idx
),
2279 Faset (subtree
, idx
, true_value
));
2286 SUBSTITUTE (Fcar_safe (subtree
),
2287 Fsetcar (subtree
, true_value
));
2288 SUBSTITUTE (Fcdr_safe (subtree
),
2289 Fsetcdr (subtree
, true_value
));
2293 #ifdef USE_TEXT_PROPERTIES
2296 /* Check for text properties in each interval.
2297 substitute_in_interval contains part of the logic. */
2299 INTERVAL root_interval
= XSTRING (subtree
)->intervals
;
2300 Lisp_Object arg
= Fcons (object
, placeholder
);
2302 traverse_intervals (root_interval
, 1, 0,
2303 &substitute_in_interval
, arg
);
2307 #endif /* defined USE_TEXT_PROPERTIES */
2309 /* Other types don't recurse any further. */
2315 /* Helper function for substitute_object_recurse. */
2317 substitute_in_interval (interval
, arg
)
2321 Lisp_Object object
= Fcar (arg
);
2322 Lisp_Object placeholder
= Fcdr (arg
);
2324 SUBSTITUTE(interval
->plist
, interval
->plist
= true_value
);
2328 #ifdef LISP_FLOAT_TYPE
2345 if (*cp
== '+' || *cp
== '-')
2348 if (*cp
>= '0' && *cp
<= '9')
2351 while (*cp
>= '0' && *cp
<= '9')
2359 if (*cp
>= '0' && *cp
<= '9')
2362 while (*cp
>= '0' && *cp
<= '9')
2365 if (*cp
== 'e' || *cp
== 'E')
2369 if (*cp
== '+' || *cp
== '-')
2373 if (*cp
>= '0' && *cp
<= '9')
2376 while (*cp
>= '0' && *cp
<= '9')
2379 else if (cp
== start
)
2381 else if (cp
[-1] == '+' && cp
[0] == 'I' && cp
[1] == 'N' && cp
[2] == 'F')
2386 else if (cp
[-1] == '+' && cp
[0] == 'N' && cp
[1] == 'a' && cp
[2] == 'N')
2392 return (((*cp
== 0) || (*cp
== ' ') || (*cp
== '\t') || (*cp
== '\n') || (*cp
== '\r') || (*cp
== '\f'))
2393 && (state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
)
2394 || state
== (DOT_CHAR
|TRAIL_INT
)
2395 || state
== (LEAD_INT
|E_CHAR
|EXP_INT
)
2396 || state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)
2397 || state
== (DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)));
2399 #endif /* LISP_FLOAT_TYPE */
2402 read_vector (readcharfun
, bytecodeflag
)
2403 Lisp_Object readcharfun
;
2408 register Lisp_Object
*ptr
;
2409 register Lisp_Object tem
, item
, vector
;
2410 register struct Lisp_Cons
*otem
;
2413 tem
= read_list (1, readcharfun
);
2414 len
= Flength (tem
);
2415 vector
= (read_pure
? make_pure_vector (XINT (len
)) : Fmake_vector (len
, Qnil
));
2417 size
= XVECTOR (vector
)->size
;
2418 ptr
= XVECTOR (vector
)->contents
;
2419 for (i
= 0; i
< size
; i
++)
2422 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2423 bytecode object, the docstring containing the bytecode and
2424 constants values must be treated as unibyte and passed to
2425 Fread, to get the actual bytecode string and constants vector. */
2426 if (bytecodeflag
&& load_force_doc_strings
)
2428 if (i
== COMPILED_BYTECODE
)
2430 if (!STRINGP (item
))
2431 error ("invalid byte code");
2433 /* Delay handling the bytecode slot until we know whether
2434 it is lazily-loaded (we can tell by whether the
2435 constants slot is nil). */
2436 ptr
[COMPILED_CONSTANTS
] = item
;
2439 else if (i
== COMPILED_CONSTANTS
)
2441 Lisp_Object bytestr
= ptr
[COMPILED_CONSTANTS
];
2445 /* Coerce string to unibyte (like string-as-unibyte,
2446 but without generating extra garbage and
2447 guaranteeing no change in the contents). */
2448 XSTRING (bytestr
)->size
= STRING_BYTES (XSTRING (bytestr
));
2449 SET_STRING_BYTES (XSTRING (bytestr
), -1);
2451 item
= Fread (bytestr
);
2453 error ("invalid byte code");
2455 otem
= XCONS (item
);
2456 bytestr
= XCAR (item
);
2461 /* Now handle the bytecode slot. */
2462 ptr
[COMPILED_BYTECODE
] = read_pure
? Fpurecopy (bytestr
) : bytestr
;
2465 ptr
[i
] = read_pure
? Fpurecopy (item
) : item
;
2473 /* FLAG = 1 means check for ] to terminate rather than ) and .
2474 FLAG = -1 means check for starting with defun
2475 and make structure pure. */
2478 read_list (flag
, readcharfun
)
2480 register Lisp_Object readcharfun
;
2482 /* -1 means check next element for defun,
2483 0 means don't check,
2484 1 means already checked and found defun. */
2485 int defunflag
= flag
< 0 ? -1 : 0;
2486 Lisp_Object val
, tail
;
2487 register Lisp_Object elt
, tem
;
2488 struct gcpro gcpro1
, gcpro2
;
2489 /* 0 is the normal case.
2490 1 means this list is a doc reference; replace it with the number 0.
2491 2 means this list is a doc reference; replace it with the doc string. */
2492 int doc_reference
= 0;
2494 /* Initialize this to 1 if we are reading a list. */
2495 int first_in_list
= flag
<= 0;
2504 elt
= read1 (readcharfun
, &ch
, first_in_list
);
2509 /* While building, if the list starts with #$, treat it specially. */
2510 if (EQ (elt
, Vload_file_name
)
2512 && !NILP (Vpurify_flag
))
2514 if (NILP (Vdoc_file_name
))
2515 /* We have not yet called Snarf-documentation, so assume
2516 this file is described in the DOC-MM.NN file
2517 and Snarf-documentation will fill in the right value later.
2518 For now, replace the whole list with 0. */
2521 /* We have already called Snarf-documentation, so make a relative
2522 file name for this file, so it can be found properly
2523 in the installed Lisp directory.
2524 We don't use Fexpand_file_name because that would make
2525 the directory absolute now. */
2526 elt
= concat2 (build_string ("../lisp/"),
2527 Ffile_name_nondirectory (elt
));
2529 else if (EQ (elt
, Vload_file_name
)
2531 && load_force_doc_strings
)
2540 Fsignal (Qinvalid_read_syntax
,
2541 Fcons (make_string (") or . in a vector", 18), Qnil
));
2549 XCDR (tail
) = read0 (readcharfun
);
2551 val
= read0 (readcharfun
);
2552 read1 (readcharfun
, &ch
, 0);
2556 if (doc_reference
== 1)
2557 return make_number (0);
2558 if (doc_reference
== 2)
2560 /* Get a doc string from the file we are loading.
2561 If it's in saved_doc_string, get it from there. */
2562 int pos
= XINT (XCDR (val
));
2563 /* Position is negative for user variables. */
2564 if (pos
< 0) pos
= -pos
;
2565 if (pos
>= saved_doc_string_position
2566 && pos
< (saved_doc_string_position
2567 + saved_doc_string_length
))
2569 int start
= pos
- saved_doc_string_position
;
2572 /* Process quoting with ^A,
2573 and find the end of the string,
2574 which is marked with ^_ (037). */
2575 for (from
= start
, to
= start
;
2576 saved_doc_string
[from
] != 037;)
2578 int c
= saved_doc_string
[from
++];
2581 c
= saved_doc_string
[from
++];
2583 saved_doc_string
[to
++] = c
;
2585 saved_doc_string
[to
++] = 0;
2587 saved_doc_string
[to
++] = 037;
2590 saved_doc_string
[to
++] = c
;
2593 return make_string (saved_doc_string
+ start
,
2596 /* Look in prev_saved_doc_string the same way. */
2597 else if (pos
>= prev_saved_doc_string_position
2598 && pos
< (prev_saved_doc_string_position
2599 + prev_saved_doc_string_length
))
2601 int start
= pos
- prev_saved_doc_string_position
;
2604 /* Process quoting with ^A,
2605 and find the end of the string,
2606 which is marked with ^_ (037). */
2607 for (from
= start
, to
= start
;
2608 prev_saved_doc_string
[from
] != 037;)
2610 int c
= prev_saved_doc_string
[from
++];
2613 c
= prev_saved_doc_string
[from
++];
2615 prev_saved_doc_string
[to
++] = c
;
2617 prev_saved_doc_string
[to
++] = 0;
2619 prev_saved_doc_string
[to
++] = 037;
2622 prev_saved_doc_string
[to
++] = c
;
2625 return make_string (prev_saved_doc_string
+ start
,
2629 return get_doc_string (val
, 0, 0);
2634 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string (". in wrong context", 18), Qnil
));
2636 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("] in a list", 11), Qnil
));
2638 tem
= (read_pure
&& flag
<= 0
2639 ? pure_cons (elt
, Qnil
)
2640 : Fcons (elt
, Qnil
));
2647 defunflag
= EQ (elt
, Qdefun
);
2648 else if (defunflag
> 0)
2653 Lisp_Object Vobarray
;
2654 Lisp_Object initial_obarray
;
2656 /* oblookup stores the bucket number here, for the sake of Funintern. */
2658 int oblookup_last_bucket_number
;
2660 static int hash_string ();
2661 Lisp_Object
oblookup ();
2663 /* Get an error if OBARRAY is not an obarray.
2664 If it is one, return it. */
2667 check_obarray (obarray
)
2668 Lisp_Object obarray
;
2670 while (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
2672 /* If Vobarray is now invalid, force it to be valid. */
2673 if (EQ (Vobarray
, obarray
)) Vobarray
= initial_obarray
;
2675 obarray
= wrong_type_argument (Qvectorp
, obarray
);
2680 /* Intern the C string STR: return a symbol with that name,
2681 interned in the current obarray. */
2688 int len
= strlen (str
);
2689 Lisp_Object obarray
;
2692 if (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
2693 obarray
= check_obarray (obarray
);
2694 tem
= oblookup (obarray
, str
, len
, len
);
2697 return Fintern (make_string (str
, len
), obarray
);
2700 /* Create an uninterned symbol with name STR. */
2706 int len
= strlen (str
);
2708 return Fmake_symbol ((!NILP (Vpurify_flag
)
2709 ? make_pure_string (str
, len
, len
, 0)
2710 : make_string (str
, len
)));
2713 DEFUN ("intern", Fintern
, Sintern
, 1, 2, 0,
2714 "Return the canonical symbol whose name is STRING.\n\
2715 If there is none, one is created by this function and returned.\n\
2716 A second optional argument specifies the obarray to use;\n\
2717 it defaults to the value of `obarray'.")
2719 Lisp_Object string
, obarray
;
2721 register Lisp_Object tem
, sym
, *ptr
;
2723 if (NILP (obarray
)) obarray
= Vobarray
;
2724 obarray
= check_obarray (obarray
);
2726 CHECK_STRING (string
, 0);
2728 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2729 XSTRING (string
)->size
,
2730 STRING_BYTES (XSTRING (string
)));
2731 if (!INTEGERP (tem
))
2734 if (!NILP (Vpurify_flag
))
2735 string
= Fpurecopy (string
);
2736 sym
= Fmake_symbol (string
);
2737 XSYMBOL (sym
)->obarray
= obarray
;
2739 if ((XSTRING (string
)->data
[0] == ':')
2740 && EQ (obarray
, initial_obarray
))
2741 XSYMBOL (sym
)->value
= sym
;
2743 ptr
= &XVECTOR (obarray
)->contents
[XINT (tem
)];
2745 XSYMBOL (sym
)->next
= XSYMBOL (*ptr
);
2747 XSYMBOL (sym
)->next
= 0;
2752 DEFUN ("intern-soft", Fintern_soft
, Sintern_soft
, 1, 2, 0,
2753 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
2754 A second optional argument specifies the obarray to use;\n\
2755 it defaults to the value of `obarray'.")
2757 Lisp_Object string
, obarray
;
2759 register Lisp_Object tem
;
2761 if (NILP (obarray
)) obarray
= Vobarray
;
2762 obarray
= check_obarray (obarray
);
2764 CHECK_STRING (string
, 0);
2766 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2767 XSTRING (string
)->size
,
2768 STRING_BYTES (XSTRING (string
)));
2769 if (!INTEGERP (tem
))
2774 DEFUN ("unintern", Funintern
, Sunintern
, 1, 2, 0,
2775 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2776 The value is t if a symbol was found and deleted, nil otherwise.\n\
2777 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2778 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2779 OBARRAY defaults to the value of the variable `obarray'.")
2781 Lisp_Object name
, obarray
;
2783 register Lisp_Object string
, tem
;
2786 if (NILP (obarray
)) obarray
= Vobarray
;
2787 obarray
= check_obarray (obarray
);
2790 XSETSTRING (string
, XSYMBOL (name
)->name
);
2793 CHECK_STRING (name
, 0);
2797 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2798 XSTRING (string
)->size
,
2799 STRING_BYTES (XSTRING (string
)));
2802 /* If arg was a symbol, don't delete anything but that symbol itself. */
2803 if (SYMBOLP (name
) && !EQ (name
, tem
))
2806 XSYMBOL (tem
)->obarray
= Qnil
;
2808 hash
= oblookup_last_bucket_number
;
2810 if (EQ (XVECTOR (obarray
)->contents
[hash
], tem
))
2812 if (XSYMBOL (tem
)->next
)
2813 XSETSYMBOL (XVECTOR (obarray
)->contents
[hash
], XSYMBOL (tem
)->next
);
2815 XSETINT (XVECTOR (obarray
)->contents
[hash
], 0);
2819 Lisp_Object tail
, following
;
2821 for (tail
= XVECTOR (obarray
)->contents
[hash
];
2822 XSYMBOL (tail
)->next
;
2825 XSETSYMBOL (following
, XSYMBOL (tail
)->next
);
2826 if (EQ (following
, tem
))
2828 XSYMBOL (tail
)->next
= XSYMBOL (following
)->next
;
2837 /* Return the symbol in OBARRAY whose names matches the string
2838 of SIZE characters (SIZE_BYTE bytes) at PTR.
2839 If there is no such symbol in OBARRAY, return nil.
2841 Also store the bucket number in oblookup_last_bucket_number. */
2844 oblookup (obarray
, ptr
, size
, size_byte
)
2845 Lisp_Object obarray
;
2847 int size
, size_byte
;
2851 register Lisp_Object tail
;
2852 Lisp_Object bucket
, tem
;
2854 if (!VECTORP (obarray
)
2855 || (obsize
= XVECTOR (obarray
)->size
) == 0)
2857 obarray
= check_obarray (obarray
);
2858 obsize
= XVECTOR (obarray
)->size
;
2860 /* This is sometimes needed in the middle of GC. */
2861 obsize
&= ~ARRAY_MARK_FLAG
;
2862 /* Combining next two lines breaks VMS C 2.3. */
2863 hash
= hash_string (ptr
, size_byte
);
2865 bucket
= XVECTOR (obarray
)->contents
[hash
];
2866 oblookup_last_bucket_number
= hash
;
2867 if (XFASTINT (bucket
) == 0)
2869 else if (!SYMBOLP (bucket
))
2870 error ("Bad data in guts of obarray"); /* Like CADR error message */
2872 for (tail
= bucket
; ; XSETSYMBOL (tail
, XSYMBOL (tail
)->next
))
2874 if (STRING_BYTES (XSYMBOL (tail
)->name
) == size_byte
2875 && XSYMBOL (tail
)->name
->size
== size
2876 && !bcmp (XSYMBOL (tail
)->name
->data
, ptr
, size_byte
))
2878 else if (XSYMBOL (tail
)->next
== 0)
2881 XSETINT (tem
, hash
);
2886 hash_string (ptr
, len
)
2890 register unsigned char *p
= ptr
;
2891 register unsigned char *end
= p
+ len
;
2892 register unsigned char c
;
2893 register int hash
= 0;
2898 if (c
>= 0140) c
-= 40;
2899 hash
= ((hash
<<3) + (hash
>>28) + c
);
2901 return hash
& 07777777777;
2905 map_obarray (obarray
, fn
, arg
)
2906 Lisp_Object obarray
;
2907 void (*fn
) P_ ((Lisp_Object
, Lisp_Object
));
2911 register Lisp_Object tail
;
2912 CHECK_VECTOR (obarray
, 1);
2913 for (i
= XVECTOR (obarray
)->size
- 1; i
>= 0; i
--)
2915 tail
= XVECTOR (obarray
)->contents
[i
];
2920 if (XSYMBOL (tail
)->next
== 0)
2922 XSETSYMBOL (tail
, XSYMBOL (tail
)->next
);
2928 mapatoms_1 (sym
, function
)
2929 Lisp_Object sym
, function
;
2931 call1 (function
, sym
);
2934 DEFUN ("mapatoms", Fmapatoms
, Smapatoms
, 1, 2, 0,
2935 "Call FUNCTION on every symbol in OBARRAY.\n\
2936 OBARRAY defaults to the value of `obarray'.")
2938 Lisp_Object function
, obarray
;
2940 if (NILP (obarray
)) obarray
= Vobarray
;
2941 obarray
= check_obarray (obarray
);
2943 map_obarray (obarray
, mapatoms_1
, function
);
2947 #define OBARRAY_SIZE 1511
2952 Lisp_Object oblength
;
2956 XSETFASTINT (oblength
, OBARRAY_SIZE
);
2958 Qnil
= Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
2959 Vobarray
= Fmake_vector (oblength
, make_number (0));
2960 initial_obarray
= Vobarray
;
2961 staticpro (&initial_obarray
);
2962 /* Intern nil in the obarray */
2963 XSYMBOL (Qnil
)->obarray
= Vobarray
;
2964 /* These locals are to kludge around a pyramid compiler bug. */
2965 hash
= hash_string ("nil", 3);
2966 /* Separate statement here to avoid VAXC bug. */
2967 hash
%= OBARRAY_SIZE
;
2968 tem
= &XVECTOR (Vobarray
)->contents
[hash
];
2971 Qunbound
= Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
2972 XSYMBOL (Qnil
)->function
= Qunbound
;
2973 XSYMBOL (Qunbound
)->value
= Qunbound
;
2974 XSYMBOL (Qunbound
)->function
= Qunbound
;
2977 XSYMBOL (Qnil
)->value
= Qnil
;
2978 XSYMBOL (Qnil
)->plist
= Qnil
;
2979 XSYMBOL (Qt
)->value
= Qt
;
2981 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2984 Qvariable_documentation
= intern ("variable-documentation");
2985 staticpro (&Qvariable_documentation
);
2987 read_buffer_size
= 100 + MAX_LENGTH_OF_MULTI_BYTE_FORM
;
2988 read_buffer
= (char *) malloc (read_buffer_size
);
2993 struct Lisp_Subr
*sname
;
2996 sym
= intern (sname
->symbol_name
);
2997 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
3000 #ifdef NOTDEF /* use fset in subr.el now */
3002 defalias (sname
, string
)
3003 struct Lisp_Subr
*sname
;
3007 sym
= intern (string
);
3008 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
3012 /* Define an "integer variable"; a symbol whose value is forwarded
3013 to a C variable of type int. Sample call: */
3014 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3016 defvar_int (namestring
, address
)
3020 Lisp_Object sym
, val
;
3021 sym
= intern (namestring
);
3022 val
= allocate_misc ();
3023 XMISCTYPE (val
) = Lisp_Misc_Intfwd
;
3024 XINTFWD (val
)->intvar
= address
;
3025 XSYMBOL (sym
)->value
= val
;
3028 /* Similar but define a variable whose value is T if address contains 1,
3029 NIL if address contains 0 */
3031 defvar_bool (namestring
, address
)
3035 Lisp_Object sym
, val
;
3036 sym
= intern (namestring
);
3037 val
= allocate_misc ();
3038 XMISCTYPE (val
) = Lisp_Misc_Boolfwd
;
3039 XBOOLFWD (val
)->boolvar
= address
;
3040 XSYMBOL (sym
)->value
= val
;
3041 Vbyte_boolean_vars
= Fcons (sym
, Vbyte_boolean_vars
);
3044 /* Similar but define a variable whose value is the Lisp Object stored
3045 at address. Two versions: with and without gc-marking of the C
3046 variable. The nopro version is used when that variable will be
3047 gc-marked for some other reason, since marking the same slot twice
3048 can cause trouble with strings. */
3050 defvar_lisp_nopro (namestring
, address
)
3052 Lisp_Object
*address
;
3054 Lisp_Object sym
, val
;
3055 sym
= intern (namestring
);
3056 val
= allocate_misc ();
3057 XMISCTYPE (val
) = Lisp_Misc_Objfwd
;
3058 XOBJFWD (val
)->objvar
= address
;
3059 XSYMBOL (sym
)->value
= val
;
3063 defvar_lisp (namestring
, address
)
3065 Lisp_Object
*address
;
3067 defvar_lisp_nopro (namestring
, address
);
3068 staticpro (address
);
3073 /* Similar but define a variable whose value is the Lisp Object stored in
3074 the current buffer. address is the address of the slot in the buffer
3075 that is current now. */
3078 defvar_per_buffer (namestring
, address
, type
, doc
)
3080 Lisp_Object
*address
;
3084 Lisp_Object sym
, val
;
3086 extern struct buffer buffer_local_symbols
;
3088 sym
= intern (namestring
);
3089 val
= allocate_misc ();
3090 offset
= (char *)address
- (char *)current_buffer
;
3092 XMISCTYPE (val
) = Lisp_Misc_Buffer_Objfwd
;
3093 XBUFFER_OBJFWD (val
)->offset
= offset
;
3094 XSYMBOL (sym
)->value
= val
;
3095 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_symbols
) = sym
;
3096 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_types
) = type
;
3097 if (XINT (*(Lisp_Object
*)(offset
+ (char *)&buffer_local_flags
)) == 0)
3098 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3099 slot of buffer_local_flags */
3103 #endif /* standalone */
3105 /* Similar but define a variable whose value is the Lisp Object stored
3106 at a particular offset in the current kboard object. */
3109 defvar_kboard (namestring
, offset
)
3113 Lisp_Object sym
, val
;
3114 sym
= intern (namestring
);
3115 val
= allocate_misc ();
3116 XMISCTYPE (val
) = Lisp_Misc_Kboard_Objfwd
;
3117 XKBOARD_OBJFWD (val
)->offset
= offset
;
3118 XSYMBOL (sym
)->value
= val
;
3121 /* Record the value of load-path used at the start of dumping
3122 so we can see if the site changed it later during dumping. */
3123 static Lisp_Object dump_path
;
3129 int turn_off_warning
= 0;
3131 #ifdef HAVE_SETLOCALE
3132 /* Make sure numbers are parsed as we expect. */
3133 setlocale (LC_NUMERIC
, "C");
3134 #endif /* HAVE_SETLOCALE */
3136 /* Compute the default load-path. */
3138 normal
= PATH_LOADSEARCH
;
3139 Vload_path
= decode_env_path (0, normal
);
3141 if (NILP (Vpurify_flag
))
3142 normal
= PATH_LOADSEARCH
;
3144 normal
= PATH_DUMPLOADSEARCH
;
3146 /* In a dumped Emacs, we normally have to reset the value of
3147 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3148 uses ../lisp, instead of the path of the installed elisp
3149 libraries. However, if it appears that Vload_path was changed
3150 from the default before dumping, don't override that value. */
3153 if (! NILP (Fequal (dump_path
, Vload_path
)))
3155 Vload_path
= decode_env_path (0, normal
);
3156 if (!NILP (Vinstallation_directory
))
3158 /* Add to the path the lisp subdir of the
3159 installation dir, if it exists. */
3160 Lisp_Object tem
, tem1
;
3161 tem
= Fexpand_file_name (build_string ("lisp"),
3162 Vinstallation_directory
);
3163 tem1
= Ffile_exists_p (tem
);
3166 if (NILP (Fmember (tem
, Vload_path
)))
3168 turn_off_warning
= 1;
3169 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3173 /* That dir doesn't exist, so add the build-time
3174 Lisp dirs instead. */
3175 Vload_path
= nconc2 (Vload_path
, dump_path
);
3177 /* Add leim under the installation dir, if it exists. */
3178 tem
= Fexpand_file_name (build_string ("leim"),
3179 Vinstallation_directory
);
3180 tem1
= Ffile_exists_p (tem
);
3183 if (NILP (Fmember (tem
, Vload_path
)))
3184 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3187 /* Add site-list under the installation dir, if it exists. */
3188 tem
= Fexpand_file_name (build_string ("site-lisp"),
3189 Vinstallation_directory
);
3190 tem1
= Ffile_exists_p (tem
);
3193 if (NILP (Fmember (tem
, Vload_path
)))
3194 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3197 /* If Emacs was not built in the source directory,
3198 and it is run from where it was built, add to load-path
3199 the lisp, leim and site-lisp dirs under that directory. */
3201 if (NILP (Fequal (Vinstallation_directory
, Vsource_directory
)))
3205 tem
= Fexpand_file_name (build_string ("src/Makefile"),
3206 Vinstallation_directory
);
3207 tem1
= Ffile_exists_p (tem
);
3209 /* Don't be fooled if they moved the entire source tree
3210 AFTER dumping Emacs. If the build directory is indeed
3211 different from the source dir, src/Makefile.in and
3212 src/Makefile will not be found together. */
3213 tem
= Fexpand_file_name (build_string ("src/Makefile.in"),
3214 Vinstallation_directory
);
3215 tem2
= Ffile_exists_p (tem
);
3216 if (!NILP (tem1
) && NILP (tem2
))
3218 tem
= Fexpand_file_name (build_string ("lisp"),
3221 if (NILP (Fmember (tem
, Vload_path
)))
3222 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3224 tem
= Fexpand_file_name (build_string ("leim"),
3227 if (NILP (Fmember (tem
, Vload_path
)))
3228 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3230 tem
= Fexpand_file_name (build_string ("site-lisp"),
3233 if (NILP (Fmember (tem
, Vload_path
)))
3234 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3242 /* NORMAL refers to the lisp dir in the source directory. */
3243 /* We used to add ../lisp at the front here, but
3244 that caused trouble because it was copied from dump_path
3245 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3246 It should be unnecessary. */
3247 Vload_path
= decode_env_path (0, normal
);
3248 dump_path
= Vload_path
;
3253 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3254 almost never correct, thereby causing a warning to be printed out that
3255 confuses users. Since PATH_LOADSEARCH is always overridden by the
3256 EMACSLOADPATH environment variable below, disable the warning on NT. */
3258 /* Warn if dirs in the *standard* path don't exist. */
3259 if (!turn_off_warning
)
3261 Lisp_Object path_tail
;
3263 for (path_tail
= Vload_path
;
3265 path_tail
= XCDR (path_tail
))
3267 Lisp_Object dirfile
;
3268 dirfile
= Fcar (path_tail
);
3269 if (STRINGP (dirfile
))
3271 dirfile
= Fdirectory_file_name (dirfile
);
3272 if (access (XSTRING (dirfile
)->data
, 0) < 0)
3273 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3278 #endif /* WINDOWSNT */
3280 /* If the EMACSLOADPATH environment variable is set, use its value.
3281 This doesn't apply if we're dumping. */
3283 if (NILP (Vpurify_flag
)
3284 && egetenv ("EMACSLOADPATH"))
3286 Vload_path
= decode_env_path ("EMACSLOADPATH", normal
);
3290 load_in_progress
= 0;
3291 Vload_file_name
= Qnil
;
3293 load_descriptor_list
= Qnil
;
3295 Vstandard_input
= Qt
;
3298 /* Print a warning, using format string FORMAT, that directory DIRNAME
3299 does not exist. Print it on stderr and put it in *Message*. */
3302 dir_warning (format
, dirname
)
3304 Lisp_Object dirname
;
3307 = (char *) alloca (XSTRING (dirname
)->size
+ strlen (format
) + 5);
3309 fprintf (stderr
, format
, XSTRING (dirname
)->data
);
3310 sprintf (buffer
, format
, XSTRING (dirname
)->data
);
3311 /* Don't log the warning before we've initialized!! */
3313 message_dolog (buffer
, strlen (buffer
), 0, STRING_MULTIBYTE (dirname
));
3320 defsubr (&Sread_from_string
);
3322 defsubr (&Sintern_soft
);
3323 defsubr (&Sunintern
);
3325 defsubr (&Seval_buffer
);
3326 defsubr (&Seval_region
);
3327 defsubr (&Sread_char
);
3328 defsubr (&Sread_char_exclusive
);
3329 defsubr (&Sread_event
);
3330 defsubr (&Sget_file_char
);
3331 defsubr (&Smapatoms
);
3333 DEFVAR_LISP ("obarray", &Vobarray
,
3334 "Symbol table for use by `intern' and `read'.\n\
3335 It is a vector whose length ought to be prime for best results.\n\
3336 The vector's contents don't make sense if examined from Lisp programs;\n\
3337 to find all the symbols in an obarray, use `mapatoms'.");
3339 DEFVAR_LISP ("values", &Vvalues
,
3340 "List of values of all expressions which were read, evaluated and printed.\n\
3341 Order is reverse chronological.");
3343 DEFVAR_LISP ("standard-input", &Vstandard_input
,
3344 "Stream for read to get input from.\n\
3345 See documentation of `read' for possible values.");
3346 Vstandard_input
= Qt
;
3348 DEFVAR_LISP ("load-path", &Vload_path
,
3349 "*List of directories to search for files to load.\n\
3350 Each element is a string (directory name) or nil (try default directory).\n\
3351 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3352 otherwise to default specified by file `epaths.h' when Emacs was built.");
3354 DEFVAR_BOOL ("load-in-progress", &load_in_progress
,
3355 "Non-nil iff inside of `load'.");
3357 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist
,
3358 "An alist of expressions to be evalled when particular files are loaded.\n\
3359 Each element looks like (FILENAME FORMS...).\n\
3360 When `load' is run and the file-name argument is FILENAME,\n\
3361 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3362 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3363 with no directory specified, since that is how `load' is normally called.\n\
3364 An error in FORMS does not undo the load,\n\
3365 but does prevent execution of the rest of the FORMS.");
3366 Vafter_load_alist
= Qnil
;
3368 DEFVAR_LISP ("load-history", &Vload_history
,
3369 "Alist mapping source file names to symbols and features.\n\
3370 Each alist element is a list that starts with a file name,\n\
3371 except for one element (optional) that starts with nil and describes\n\
3372 definitions evaluated from buffers not visiting files.\n\
3373 The remaining elements of each list are symbols defined as functions\n\
3374 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
3375 Vload_history
= Qnil
;
3377 DEFVAR_LISP ("load-file-name", &Vload_file_name
,
3378 "Full name of file being loaded by `load'.");
3379 Vload_file_name
= Qnil
;
3381 DEFVAR_LISP ("user-init-file", &Vuser_init_file
,
3382 "File name, including directory, of user's initialization file.");
3383 Vuser_init_file
= Qnil
;
3385 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list
,
3386 "Used for internal purposes by `load'.");
3387 Vcurrent_load_list
= Qnil
;
3389 DEFVAR_LISP ("load-read-function", &Vload_read_function
,
3390 "Function used by `load' and `eval-region' for reading expressions.\n\
3391 The default is nil, which means use the function `read'.");
3392 Vload_read_function
= Qnil
;
3394 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function
,
3395 "Function called in `load' for loading an Emacs lisp source file.\n\
3396 This function is for doing code conversion before reading the source file.\n\
3397 If nil, loading is done without any code conversion.\n\
3398 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3399 FULLNAME is the full name of FILE.\n\
3400 See `load' for the meaning of the remaining arguments.");
3401 Vload_source_file_function
= Qnil
;
3403 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings
,
3404 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3405 This is useful when the file being loaded is a temporary copy.");
3406 load_force_doc_strings
= 0;
3408 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte
,
3409 "Non-nil means `load' converts strings to unibyte whenever possible.\n\
3410 This is normally used in `load-with-code-conversion'\n\
3411 for loading non-compiled files.");
3412 load_convert_to_unibyte
= 0;
3414 DEFVAR_LISP ("source-directory", &Vsource_directory
,
3415 "Directory in which Emacs sources were found when Emacs was built.\n\
3416 You cannot count on them to still be there!");
3418 = Fexpand_file_name (build_string ("../"),
3419 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH
)));
3421 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list
,
3422 "List of files that were preloaded (when dumping Emacs).");
3423 Vpreloaded_file_list
= Qnil
;
3425 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars
,
3426 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3427 Vbyte_boolean_vars
= Qnil
;
3429 /* Vsource_directory was initialized in init_lread. */
3431 load_descriptor_list
= Qnil
;
3432 staticpro (&load_descriptor_list
);
3434 Qcurrent_load_list
= intern ("current-load-list");
3435 staticpro (&Qcurrent_load_list
);
3437 Qstandard_input
= intern ("standard-input");
3438 staticpro (&Qstandard_input
);
3440 Qread_char
= intern ("read-char");
3441 staticpro (&Qread_char
);
3443 Qget_file_char
= intern ("get-file-char");
3444 staticpro (&Qget_file_char
);
3446 Qbackquote
= intern ("`");
3447 staticpro (&Qbackquote
);
3448 Qcomma
= intern (",");
3449 staticpro (&Qcomma
);
3450 Qcomma_at
= intern (",@");
3451 staticpro (&Qcomma_at
);
3452 Qcomma_dot
= intern (",.");
3453 staticpro (&Qcomma_dot
);
3455 Qinhibit_file_name_operation
= intern ("inhibit-file-name-operation");
3456 staticpro (&Qinhibit_file_name_operation
);
3458 Qascii_character
= intern ("ascii-character");
3459 staticpro (&Qascii_character
);
3461 Qfunction
= intern ("function");
3462 staticpro (&Qfunction
);
3464 Qload
= intern ("load");
3467 Qload_file_name
= intern ("load-file-name");
3468 staticpro (&Qload_file_name
);
3470 staticpro (&dump_path
);
3472 staticpro (&read_objects
);
3473 read_objects
= Qnil
;
3474 staticpro (&seen_list
);