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
= STRING_BYTES (XSTRING (file
));
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
[STRING_BYTES (XSTRING (found
)) - 4]),
696 stat ((char *)XSTRING (found
)->data
, &s1
);
697 XSTRING (found
)->data
[STRING_BYTES (XSTRING (found
)) - 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
[STRING_BYTES (XSTRING (found
)) - 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
) + STRING_BYTES (XSTRING (filename
)) + 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 STRING_BYTES (XSTRING (filename
)) - 2);
921 fn
[STRING_BYTES (XSTRING (filename
)) - 2] = 0;
925 strncpy (fn
, XSTRING (filename
)->data
,
926 STRING_BYTES (XSTRING (filename
)));
927 fn
[STRING_BYTES (XSTRING (filename
))] = 0;
930 if (lsuffix
!= 0) /* Bug happens on CCI if lsuffix is 0. */
931 strncat (fn
, nsuffix
, lsuffix
);
933 /* Check that the file exists and is not a directory. */
937 handler
= Ffind_file_name_handler (filename
, Qfile_exists_p
);
938 if (! NILP (handler
) && ! exec_only
)
943 string
= build_string (fn
);
944 exists
= ! NILP (exec_only
? Ffile_executable_p (string
)
945 : Ffile_readable_p (string
));
947 && ! NILP (Ffile_directory_p (build_string (fn
))))
952 /* We succeeded; return this descriptor and filename. */
954 *storeptr
= build_string (fn
);
961 int exists
= (stat (fn
, &st
) >= 0
962 && (st
.st_mode
& S_IFMT
) != S_IFDIR
);
965 /* Check that we can access or open it. */
967 fd
= (access (fn
, X_OK
) == 0) ? 1 : -1;
969 fd
= open (fn
, O_RDONLY
, 0);
973 /* We succeeded; return this descriptor and filename. */
975 *storeptr
= build_string (fn
);
982 /* Advance to next suffix. */
985 nsuffix
+= lsuffix
+ 1;
996 /* Merge the list we've accumulated of globals from the current input source
997 into the load_history variable. The details depend on whether
998 the source has an associated file name or not. */
1001 build_load_history (stream
, source
)
1005 register Lisp_Object tail
, prev
, newelt
;
1006 register Lisp_Object tem
, tem2
;
1007 register int foundit
, loading
;
1009 loading
= stream
|| !NARROWED
;
1011 tail
= Vload_history
;
1014 while (!NILP (tail
))
1018 /* Find the feature's previous assoc list... */
1019 if (!NILP (Fequal (source
, Fcar (tem
))))
1023 /* If we're loading, remove it. */
1027 Vload_history
= Fcdr (tail
);
1029 Fsetcdr (prev
, Fcdr (tail
));
1032 /* Otherwise, cons on new symbols that are not already members. */
1035 tem2
= Vcurrent_load_list
;
1037 while (CONSP (tem2
))
1039 newelt
= Fcar (tem2
);
1041 if (NILP (Fmemq (newelt
, tem
)))
1042 Fsetcar (tail
, Fcons (Fcar (tem
),
1043 Fcons (newelt
, Fcdr (tem
))));
1056 /* If we're loading, cons the new assoc onto the front of load-history,
1057 the most-recently-loaded position. Also do this if we didn't find
1058 an existing member for the current source. */
1059 if (loading
|| !foundit
)
1060 Vload_history
= Fcons (Fnreverse (Vcurrent_load_list
),
1065 unreadpure () /* Used as unwind-protect function in readevalloop */
1072 readevalloop_1 (old
)
1075 load_convert_to_unibyte
= ! NILP (old
);
1079 /* UNIBYTE specifies how to set load_convert_to_unibyte
1080 for this invocation.
1081 READFUN, if non-nil, is used instead of `read'. */
1084 readevalloop (readcharfun
, stream
, sourcename
, evalfun
, printflag
, unibyte
, readfun
)
1085 Lisp_Object readcharfun
;
1087 Lisp_Object sourcename
;
1088 Lisp_Object (*evalfun
) ();
1090 Lisp_Object unibyte
, readfun
;
1093 register Lisp_Object val
;
1094 int count
= specpdl_ptr
- specpdl
;
1095 struct gcpro gcpro1
;
1096 struct buffer
*b
= 0;
1098 if (BUFFERP (readcharfun
))
1099 b
= XBUFFER (readcharfun
);
1100 else if (MARKERP (readcharfun
))
1101 b
= XMARKER (readcharfun
)->buffer
;
1103 specbind (Qstandard_input
, readcharfun
);
1104 specbind (Qcurrent_load_list
, Qnil
);
1105 record_unwind_protect (readevalloop_1
, load_convert_to_unibyte
? Qt
: Qnil
);
1106 load_convert_to_unibyte
= !NILP (unibyte
);
1108 readchar_backlog
= -1;
1110 GCPRO1 (sourcename
);
1112 LOADHIST_ATTACH (sourcename
);
1116 if (b
!= 0 && NILP (b
->name
))
1117 error ("Reading from killed buffer");
1123 while ((c
= READCHAR
) != '\n' && c
!= -1);
1128 /* Ignore whitespace here, so we can detect eof. */
1129 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r')
1132 if (!NILP (Vpurify_flag
) && c
== '(')
1134 int count1
= specpdl_ptr
- specpdl
;
1135 record_unwind_protect (unreadpure
, Qnil
);
1136 val
= read_list (-1, readcharfun
);
1137 unbind_to (count1
, Qnil
);
1142 read_objects
= Qnil
;
1143 if (! NILP (readfun
))
1144 val
= call1 (readfun
, readcharfun
);
1145 else if (! NILP (Vload_read_function
))
1146 val
= call1 (Vload_read_function
, readcharfun
);
1148 val
= read0 (readcharfun
);
1151 val
= (*evalfun
) (val
);
1154 Vvalues
= Fcons (val
, Vvalues
);
1155 if (EQ (Vstandard_output
, Qt
))
1162 build_load_history (stream
, sourcename
);
1165 unbind_to (count
, Qnil
);
1170 DEFUN ("eval-buffer", Feval_buffer
, Seval_buffer
, 0, 5, "",
1171 "Execute the current buffer as Lisp code.\n\
1172 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1173 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1174 PRINTFLAG controls printing of output:\n\
1175 nil means discard it; anything else is stream for print.\n\
1177 If the optional third argument FILENAME is non-nil,\n\
1178 it specifies the file name to use for `load-history'.\n\
1179 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1180 for this invocation.\n\
1182 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1183 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1185 This function preserves the position of point.")
1186 (buffer
, printflag
, filename
, unibyte
, do_allow_print
)
1187 Lisp_Object buffer
, printflag
, filename
, unibyte
, do_allow_print
;
1189 int count
= specpdl_ptr
- specpdl
;
1190 Lisp_Object tem
, buf
;
1193 buf
= Fcurrent_buffer ();
1195 buf
= Fget_buffer (buffer
);
1197 error ("No such buffer");
1199 if (NILP (printflag
) && NILP (do_allow_print
))
1204 if (NILP (filename
))
1205 filename
= XBUFFER (buf
)->filename
;
1207 specbind (Qstandard_output
, tem
);
1208 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1209 BUF_SET_PT (XBUFFER (buf
), BUF_BEGV (XBUFFER (buf
)));
1210 readevalloop (buf
, 0, filename
, Feval
, !NILP (printflag
), unibyte
, Qnil
);
1211 unbind_to (count
, Qnil
);
1217 XDEFUN ("eval-current-buffer", Feval_current_buffer
, Seval_current_buffer
, 0, 1, "",
1218 "Execute the current buffer as Lisp code.\n\
1219 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1220 nil means discard it; anything else is stream for print.\n\
1222 If there is no error, point does not move. If there is an error,\n\
1223 point remains at the end of the last character read from the buffer.")
1225 Lisp_Object printflag
;
1227 int count
= specpdl_ptr
- specpdl
;
1228 Lisp_Object tem
, cbuf
;
1230 cbuf
= Fcurrent_buffer ()
1232 if (NILP (printflag
))
1236 specbind (Qstandard_output
, tem
);
1237 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1239 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
,
1240 !NILP (printflag
), Qnil
, Qnil
);
1241 return unbind_to (count
, Qnil
);
1245 DEFUN ("eval-region", Feval_region
, Seval_region
, 2, 4, "r",
1246 "Execute the region as Lisp code.\n\
1247 When called from programs, expects two arguments,\n\
1248 giving starting and ending indices in the current buffer\n\
1249 of the text to be executed.\n\
1250 Programs can pass third argument PRINTFLAG which controls output:\n\
1251 nil means discard it; anything else is stream for printing it.\n\
1252 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1253 instead of `read' to read each expression. It gets one argument\n\
1254 which is the input stream for reading characters.\n\
1256 This function does not move point.")
1257 (start
, end
, printflag
, read_function
)
1258 Lisp_Object start
, end
, printflag
, read_function
;
1260 int count
= specpdl_ptr
- specpdl
;
1261 Lisp_Object tem
, cbuf
;
1263 cbuf
= Fcurrent_buffer ();
1265 if (NILP (printflag
))
1269 specbind (Qstandard_output
, tem
);
1271 if (NILP (printflag
))
1272 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
1273 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
1275 /* This both uses start and checks its type. */
1277 Fnarrow_to_region (make_number (BEGV
), end
);
1278 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
,
1279 !NILP (printflag
), Qnil
, read_function
);
1281 return unbind_to (count
, Qnil
);
1284 #endif /* standalone */
1286 DEFUN ("read", Fread
, Sread
, 0, 1, 0,
1287 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1288 If STREAM is nil, use the value of `standard-input' (which see).\n\
1289 STREAM or the value of `standard-input' may be:\n\
1290 a buffer (read from point and advance it)\n\
1291 a marker (read from where it points and advance it)\n\
1292 a function (call it with no arguments for each character,\n\
1293 call it with a char as argument to push a char back)\n\
1294 a string (takes text from string, starting at the beginning)\n\
1295 t (read text line using minibuffer and use it).")
1299 extern Lisp_Object
Fread_minibuffer ();
1302 stream
= Vstandard_input
;
1303 if (EQ (stream
, Qt
))
1304 stream
= Qread_char
;
1306 readchar_backlog
= -1;
1307 new_backquote_flag
= 0;
1308 read_objects
= Qnil
;
1311 if (EQ (stream
, Qread_char
))
1312 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil
);
1315 if (STRINGP (stream
))
1316 return Fcar (Fread_from_string (stream
, Qnil
, Qnil
));
1318 return read0 (stream
);
1321 DEFUN ("read-from-string", Fread_from_string
, Sread_from_string
, 1, 3, 0,
1322 "Read one Lisp expression which is represented as text by STRING.\n\
1323 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1324 START and END optionally delimit a substring of STRING from which to read;\n\
1325 they default to 0 and (length STRING) respectively.")
1326 (string
, start
, end
)
1327 Lisp_Object string
, start
, end
;
1329 int startval
, endval
;
1332 CHECK_STRING (string
,0);
1335 endval
= XSTRING (string
)->size
;
1338 CHECK_NUMBER (end
, 2);
1339 endval
= XINT (end
);
1340 if (endval
< 0 || endval
> XSTRING (string
)->size
)
1341 args_out_of_range (string
, end
);
1348 CHECK_NUMBER (start
, 1);
1349 startval
= XINT (start
);
1350 if (startval
< 0 || startval
> endval
)
1351 args_out_of_range (string
, start
);
1354 read_from_string_index
= startval
;
1355 read_from_string_index_byte
= string_char_to_byte (string
, startval
);
1356 read_from_string_limit
= endval
;
1358 new_backquote_flag
= 0;
1359 read_objects
= Qnil
;
1361 tem
= read0 (string
);
1362 return Fcons (tem
, make_number (read_from_string_index
));
1365 /* Use this for recursive reads, in contexts where internal tokens
1370 Lisp_Object readcharfun
;
1372 register Lisp_Object val
;
1375 val
= read1 (readcharfun
, &c
, 0);
1377 Fsignal (Qinvalid_read_syntax
, Fcons (Fmake_string (make_number (1),
1384 static int read_buffer_size
;
1385 static char *read_buffer
;
1387 /* Read multibyte form and return it as a character. C is a first
1388 byte of multibyte form, and rest of them are read from
1392 read_multibyte (c
, readcharfun
)
1394 Lisp_Object readcharfun
;
1396 /* We need the actual character code of this multibyte
1398 unsigned char str
[MAX_LENGTH_OF_MULTI_BYTE_FORM
];
1402 while ((c
= READCHAR
) >= 0xA0
1403 && len
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1406 return STRING_CHAR (str
, len
);
1409 /* Read a \-escape sequence, assuming we already read the `\'. */
1412 read_escape (readcharfun
, stringp
)
1413 Lisp_Object readcharfun
;
1416 register int c
= READCHAR
;
1420 error ("End of file");
1450 error ("Invalid escape character syntax");
1453 c
= read_escape (readcharfun
, 0);
1454 return c
| meta_modifier
;
1459 error ("Invalid escape character syntax");
1462 c
= read_escape (readcharfun
, 0);
1463 return c
| shift_modifier
;
1468 error ("Invalid escape character syntax");
1471 c
= read_escape (readcharfun
, 0);
1472 return c
| hyper_modifier
;
1477 error ("Invalid escape character syntax");
1480 c
= read_escape (readcharfun
, 0);
1481 return c
| alt_modifier
;
1486 error ("Invalid escape character syntax");
1489 c
= read_escape (readcharfun
, 0);
1490 return c
| super_modifier
;
1495 error ("Invalid escape character syntax");
1499 c
= read_escape (readcharfun
, 0);
1500 if ((c
& ~CHAR_MODIFIER_MASK
) == '?')
1501 return 0177 | (c
& CHAR_MODIFIER_MASK
);
1502 else if (! SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
)))
1503 return c
| ctrl_modifier
;
1504 /* ASCII control chars are made from letters (both cases),
1505 as well as the non-letters within 0100...0137. */
1506 else if ((c
& 0137) >= 0101 && (c
& 0137) <= 0132)
1507 return (c
& (037 | ~0177));
1508 else if ((c
& 0177) >= 0100 && (c
& 0177) <= 0137)
1509 return (c
& (037 | ~0177));
1511 return c
| ctrl_modifier
;
1521 /* An octal escape, as in ANSI C. */
1523 register int i
= c
- '0';
1524 register int count
= 0;
1527 if ((c
= READCHAR
) >= '0' && c
<= '7')
1542 /* A hex escape, as in ANSI C. */
1548 if (c
>= '0' && c
<= '9')
1553 else if ((c
>= 'a' && c
<= 'f')
1554 || (c
>= 'A' && c
<= 'F'))
1557 if (c
>= 'a' && c
<= 'f')
1572 if (BASE_LEADING_CODE_P (c
))
1573 c
= read_multibyte (c
, readcharfun
);
1578 /* If the next token is ')' or ']' or '.', we store that character
1579 in *PCH and the return value is not interesting. Else, we store
1580 zero in *PCH and we read and return one lisp object.
1582 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1585 read1 (readcharfun
, pch
, first_in_list
)
1586 register Lisp_Object readcharfun
;
1591 int uninterned_symbol
= 0;
1598 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1603 return read_list (0, readcharfun
);
1606 return read_vector (readcharfun
, 0);
1623 tmp
= read_vector (readcharfun
, 0);
1624 if (XVECTOR (tmp
)->size
< CHAR_TABLE_STANDARD_SLOTS
1625 || XVECTOR (tmp
)->size
> CHAR_TABLE_STANDARD_SLOTS
+ 10)
1626 error ("Invalid size char-table");
1627 XSETCHAR_TABLE (tmp
, XCHAR_TABLE (tmp
));
1628 XCHAR_TABLE (tmp
)->top
= Qt
;
1637 tmp
= read_vector (readcharfun
, 0);
1638 if (XVECTOR (tmp
)->size
!= SUB_CHAR_TABLE_STANDARD_SLOTS
)
1639 error ("Invalid size char-table");
1640 XSETCHAR_TABLE (tmp
, XCHAR_TABLE (tmp
));
1641 XCHAR_TABLE (tmp
)->top
= Qnil
;
1644 Fsignal (Qinvalid_read_syntax
,
1645 Fcons (make_string ("#^^", 3), Qnil
));
1647 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#^", 2), Qnil
));
1652 length
= read1 (readcharfun
, pch
, first_in_list
);
1656 Lisp_Object tmp
, val
;
1657 int size_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
- 1)
1661 tmp
= read1 (readcharfun
, pch
, first_in_list
);
1662 if (size_in_chars
!= XSTRING (tmp
)->size
1663 /* We used to print 1 char too many
1664 when the number of bits was a multiple of 8.
1665 Accept such input in case it came from an old version. */
1666 && ! (XFASTINT (length
)
1667 == (XSTRING (tmp
)->size
- 1) * BITS_PER_CHAR
))
1668 Fsignal (Qinvalid_read_syntax
,
1669 Fcons (make_string ("#&...", 5), Qnil
));
1671 val
= Fmake_bool_vector (length
, Qnil
);
1672 bcopy (XSTRING (tmp
)->data
, XBOOL_VECTOR (val
)->data
,
1674 /* Clear the extraneous bits in the last byte. */
1675 if (XINT (length
) != size_in_chars
* BITS_PER_CHAR
)
1676 XBOOL_VECTOR (val
)->data
[size_in_chars
- 1]
1677 &= (1 << (XINT (length
) % BITS_PER_CHAR
)) - 1;
1680 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#&...", 5),
1685 /* Accept compiled functions at read-time so that we don't have to
1686 build them using function calls. */
1688 tmp
= read_vector (readcharfun
, 1);
1689 return Fmake_byte_code (XVECTOR (tmp
)->size
,
1690 XVECTOR (tmp
)->contents
);
1692 #ifdef USE_TEXT_PROPERTIES
1696 struct gcpro gcpro1
;
1699 /* Read the string itself. */
1700 tmp
= read1 (readcharfun
, &ch
, 0);
1701 if (ch
!= 0 || !STRINGP (tmp
))
1702 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1704 /* Read the intervals and their properties. */
1707 Lisp_Object beg
, end
, plist
;
1709 beg
= read1 (readcharfun
, &ch
, 0);
1713 end
= read1 (readcharfun
, &ch
, 0);
1715 plist
= read1 (readcharfun
, &ch
, 0);
1717 Fsignal (Qinvalid_read_syntax
,
1718 Fcons (build_string ("invalid string property list"),
1720 Fset_text_properties (beg
, end
, plist
, tmp
);
1726 /* #@NUMBER is used to skip NUMBER following characters.
1727 That's used in .elc files to skip over doc strings
1728 and function definitions. */
1733 /* Read a decimal integer. */
1734 while ((c
= READCHAR
) >= 0
1735 && c
>= '0' && c
<= '9')
1743 if (load_force_doc_strings
&& EQ (readcharfun
, Qget_file_char
))
1745 /* If we are supposed to force doc strings into core right now,
1746 record the last string that we skipped,
1747 and record where in the file it comes from. */
1749 /* But first exchange saved_doc_string
1750 with prev_saved_doc_string, so we save two strings. */
1752 char *temp
= saved_doc_string
;
1753 int temp_size
= saved_doc_string_size
;
1754 int temp_pos
= saved_doc_string_position
;
1755 int temp_len
= saved_doc_string_length
;
1757 saved_doc_string
= prev_saved_doc_string
;
1758 saved_doc_string_size
= prev_saved_doc_string_size
;
1759 saved_doc_string_position
= prev_saved_doc_string_position
;
1760 saved_doc_string_length
= prev_saved_doc_string_length
;
1762 prev_saved_doc_string
= temp
;
1763 prev_saved_doc_string_size
= temp_size
;
1764 prev_saved_doc_string_position
= temp_pos
;
1765 prev_saved_doc_string_length
= temp_len
;
1768 if (saved_doc_string_size
== 0)
1770 saved_doc_string_size
= nskip
+ 100;
1771 saved_doc_string
= (char *) xmalloc (saved_doc_string_size
);
1773 if (nskip
> saved_doc_string_size
)
1775 saved_doc_string_size
= nskip
+ 100;
1776 saved_doc_string
= (char *) xrealloc (saved_doc_string
,
1777 saved_doc_string_size
);
1780 saved_doc_string_position
= ftell (instream
);
1782 /* Copy that many characters into saved_doc_string. */
1783 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1784 saved_doc_string
[i
] = c
= READCHAR
;
1786 saved_doc_string_length
= i
;
1790 /* Skip that many characters. */
1791 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1798 return Vload_file_name
;
1800 return Fcons (Qfunction
, Fcons (read0 (readcharfun
), Qnil
));
1801 /* #:foo is the uninterned symbol named foo. */
1804 uninterned_symbol
= 1;
1808 /* Reader forms that can reuse previously read objects. */
1809 if (c
>= '0' && c
<= '9')
1814 /* Read a non-negative integer. */
1815 while (c
>= '0' && c
<= '9')
1821 /* #n=object returns object, but associates it with n for #n#. */
1824 /* Make a placeholder for #n# to use temporarily */
1825 Lisp_Object placeholder
;
1828 placeholder
= Fcons(Qnil
, Qnil
);
1829 cell
= Fcons (make_number (n
), placeholder
);
1830 read_objects
= Fcons (cell
, read_objects
);
1832 /* Read the object itself. */
1833 tem
= read0 (readcharfun
);
1835 /* Now put it everywhere the placeholder was... */
1836 substitute_object_in_subtree (tem
, placeholder
);
1838 /* ...and #n# will use the real value from now on. */
1839 Fsetcdr (cell
, tem
);
1843 /* #n# returns a previously read object. */
1846 tem
= Fassq (make_number (n
), read_objects
);
1849 /* Fall through to error message. */
1851 /* Fall through to error message. */
1855 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1858 while ((c
= READCHAR
) >= 0 && c
!= '\n');
1863 return Fcons (Qquote
, Fcons (read0 (readcharfun
), Qnil
));
1873 new_backquote_flag
= 1;
1874 value
= read0 (readcharfun
);
1875 new_backquote_flag
= 0;
1877 return Fcons (Qbackquote
, Fcons (value
, Qnil
));
1881 if (new_backquote_flag
)
1883 Lisp_Object comma_type
= Qnil
;
1888 comma_type
= Qcomma_at
;
1890 comma_type
= Qcomma_dot
;
1893 if (ch
>= 0) UNREAD (ch
);
1894 comma_type
= Qcomma
;
1897 new_backquote_flag
= 0;
1898 value
= read0 (readcharfun
);
1899 new_backquote_flag
= 1;
1900 return Fcons (comma_type
, Fcons (value
, Qnil
));
1908 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1911 c
= read_escape (readcharfun
, 0);
1912 else if (BASE_LEADING_CODE_P (c
))
1913 c
= read_multibyte (c
, readcharfun
);
1915 return make_number (c
);
1920 register char *p
= read_buffer
;
1921 register char *end
= read_buffer
+ read_buffer_size
;
1923 /* Nonzero if we saw an escape sequence specifying
1924 a multibyte character. */
1925 int force_multibyte
= 0;
1926 /* Nonzero if we saw an escape sequence specifying
1927 a single-byte character. */
1928 int force_singlebyte
= 0;
1932 while ((c
= READCHAR
) >= 0
1935 if (end
- p
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1937 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
1938 p
+= new - read_buffer
;
1939 read_buffer
+= new - read_buffer
;
1940 end
= read_buffer
+ read_buffer_size
;
1945 c
= read_escape (readcharfun
, 1);
1947 /* C is -1 if \ newline has just been seen */
1950 if (p
== read_buffer
)
1955 /* If an escape specifies a non-ASCII single-byte character,
1956 this must be a unibyte string. */
1957 if (SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
))
1958 && ! ASCII_BYTE_P ((c
& ~CHAR_MODIFIER_MASK
)))
1959 force_singlebyte
= 1;
1962 if (! SINGLE_BYTE_CHAR_P ((c
& ~CHAR_MODIFIER_MASK
)))
1964 unsigned char workbuf
[4];
1965 unsigned char *str
= workbuf
;
1968 /* Any modifiers for a multibyte character are invalid. */
1969 if (c
& CHAR_MODIFIER_MASK
)
1970 error ("Invalid modifier in string");
1971 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
1973 force_multibyte
= 1;
1975 bcopy (str
, p
, length
);
1980 /* Allow `\C- ' and `\C-?'. */
1981 if (c
== (CHAR_CTL
| ' '))
1983 else if (c
== (CHAR_CTL
| '?'))
1988 /* Shift modifier is valid only with [A-Za-z]. */
1989 if ((c
& 0377) >= 'A' && (c
& 0377) <= 'Z')
1991 else if ((c
& 0377) >= 'a' && (c
& 0377) <= 'z')
1992 c
= (c
& ~CHAR_SHIFT
) - ('a' - 'A');
1996 /* Move the meta bit to the right place for a string. */
1997 c
= (c
& ~CHAR_META
) | 0x80;
1999 error ("Invalid modifier in string");
2004 return Fsignal (Qend_of_file
, Qnil
);
2006 /* If purifying, and string starts with \ newline,
2007 return zero instead. This is for doc strings
2008 that we are really going to find in etc/DOC.nn.nn */
2009 if (!NILP (Vpurify_flag
) && NILP (Vdoc_file_name
) && cancel
)
2010 return make_number (0);
2012 if (force_multibyte
)
2013 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2014 else if (force_singlebyte
)
2015 nchars
= p
- read_buffer
;
2016 else if (load_convert_to_unibyte
)
2019 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2020 if (p
- read_buffer
!= nchars
)
2022 string
= make_multibyte_string (read_buffer
, nchars
,
2024 return Fstring_make_unibyte (string
);
2027 else if (EQ (readcharfun
, Qget_file_char
)
2028 || EQ (readcharfun
, Qlambda
))
2029 /* Nowadays, reading directly from a file
2030 is used only for compiled Emacs Lisp files,
2031 and those always use the Emacs internal encoding.
2032 Meanwhile, Qlambda is used for reading dynamic byte code
2033 (compiled with byte-compile-dynamic = t). */
2034 nchars
= multibyte_chars_in_text (read_buffer
, p
- read_buffer
);
2036 /* In all other cases, if we read these bytes as
2037 separate characters, treat them as separate characters now. */
2038 nchars
= p
- read_buffer
;
2041 return make_pure_string (read_buffer
, nchars
, p
- read_buffer
,
2043 || (p
- read_buffer
!= nchars
)));
2044 return make_specified_string (read_buffer
, nchars
, p
- read_buffer
,
2046 || (p
- read_buffer
!= nchars
)));
2051 #ifdef LISP_FLOAT_TYPE
2052 /* If a period is followed by a number, then we should read it
2053 as a floating point number. Otherwise, it denotes a dotted
2055 int next_char
= READCHAR
;
2058 if (! (next_char
>= '0' && next_char
<= '9'))
2065 /* Otherwise, we fall through! Note that the atom-reading loop
2066 below will now loop at least once, assuring that we will not
2067 try to UNREAD two characters in a row. */
2071 if (c
<= 040) goto retry
;
2073 register char *p
= read_buffer
;
2077 register char *end
= read_buffer
+ read_buffer_size
;
2080 && !(c
== '\"' || c
== '\'' || c
== ';' || c
== '?'
2081 || c
== '(' || c
== ')'
2082 #ifndef LISP_FLOAT_TYPE
2083 /* If we have floating-point support, then we need
2084 to allow <digits><dot><digits>. */
2086 #endif /* not LISP_FLOAT_TYPE */
2087 || c
== '[' || c
== ']' || c
== '#'
2090 if (end
- p
< MAX_LENGTH_OF_MULTI_BYTE_FORM
)
2092 register char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
2093 p
+= new - read_buffer
;
2094 read_buffer
+= new - read_buffer
;
2095 end
= read_buffer
+ read_buffer_size
;
2103 if (! SINGLE_BYTE_CHAR_P (c
))
2105 unsigned char workbuf
[4];
2106 unsigned char *str
= workbuf
;
2109 length
= non_ascii_char_to_string (c
, workbuf
, &str
);
2111 bcopy (str
, p
, length
);
2122 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
2123 p
+= new - read_buffer
;
2124 read_buffer
+= new - read_buffer
;
2125 /* end = read_buffer + read_buffer_size; */
2132 if (!quoted
&& !uninterned_symbol
)
2135 register Lisp_Object val
;
2137 if (*p1
== '+' || *p1
== '-') p1
++;
2138 /* Is it an integer? */
2141 while (p1
!= p
&& (c
= *p1
) >= '0' && c
<= '9') p1
++;
2142 #ifdef LISP_FLOAT_TYPE
2143 /* Integers can have trailing decimal points. */
2144 if (p1
> read_buffer
&& p1
< p
&& *p1
== '.') p1
++;
2147 /* It is an integer. */
2149 #ifdef LISP_FLOAT_TYPE
2153 if (sizeof (int) == sizeof (EMACS_INT
))
2154 XSETINT (val
, atoi (read_buffer
));
2155 else if (sizeof (long) == sizeof (EMACS_INT
))
2156 XSETINT (val
, atol (read_buffer
));
2162 #ifdef LISP_FLOAT_TYPE
2163 if (isfloat_string (read_buffer
))
2165 /* Compute NaN and infinities using 0.0 in a variable,
2166 to cope with compilers that think they are smarter
2172 /* Negate the value ourselves. This treats 0, NaNs,
2173 and infinity properly on IEEE floating point hosts,
2174 and works around a common bug where atof ("-0.0")
2176 int negative
= read_buffer
[0] == '-';
2178 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2179 returns 1, is if the input ends in e+INF or e+NaN. */
2186 value
= zero
/ zero
;
2189 value
= atof (read_buffer
+ negative
);
2193 return make_float (negative
? - value
: value
);
2198 if (uninterned_symbol
)
2199 return make_symbol (read_buffer
);
2201 return intern (read_buffer
);
2207 /* List of nodes we've seen during substitute_object_in_subtree. */
2208 static Lisp_Object seen_list
;
2211 substitute_object_in_subtree (object
, placeholder
)
2213 Lisp_Object placeholder
;
2215 Lisp_Object check_object
;
2217 /* We haven't seen any objects when we start. */
2220 /* Make all the substitutions. */
2222 = substitute_object_recurse (object
, placeholder
, object
);
2224 /* Clear seen_list because we're done with it. */
2227 /* The returned object here is expected to always eq the
2229 if (!EQ (check_object
, object
))
2230 error ("Unexpected mutation error in reader");
2233 /* Feval doesn't get called from here, so no gc protection is needed. */
2234 #define SUBSTITUTE(get_val, set_val) \
2236 Lisp_Object old_value = get_val; \
2237 Lisp_Object true_value \
2238 = substitute_object_recurse (object, placeholder,\
2241 if (!EQ (old_value, true_value)) \
2248 substitute_object_recurse (object
, placeholder
, subtree
)
2250 Lisp_Object placeholder
;
2251 Lisp_Object subtree
;
2253 /* If we find the placeholder, return the target object. */
2254 if (EQ (placeholder
, subtree
))
2257 /* If we've been to this node before, don't explore it again. */
2258 if (!EQ (Qnil
, Fmemq (subtree
, seen_list
)))
2261 /* If this node can be the entry point to a cycle, remember that
2262 we've seen it. It can only be such an entry point if it was made
2263 by #n=, which means that we can find it as a value in
2265 if (!EQ (Qnil
, Frassq (subtree
, read_objects
)))
2266 seen_list
= Fcons (subtree
, seen_list
);
2268 /* Recurse according to subtree's type.
2269 Every branch must return a Lisp_Object. */
2270 switch (XTYPE (subtree
))
2272 case Lisp_Vectorlike
:
2275 int length
= Flength(subtree
);
2276 for (i
= 0; i
< length
; i
++)
2278 Lisp_Object idx
= make_number (i
);
2279 SUBSTITUTE (Faref (subtree
, idx
),
2280 Faset (subtree
, idx
, true_value
));
2287 SUBSTITUTE (Fcar_safe (subtree
),
2288 Fsetcar (subtree
, true_value
));
2289 SUBSTITUTE (Fcdr_safe (subtree
),
2290 Fsetcdr (subtree
, true_value
));
2294 #ifdef USE_TEXT_PROPERTIES
2297 /* Check for text properties in each interval.
2298 substitute_in_interval contains part of the logic. */
2300 INTERVAL root_interval
= XSTRING (subtree
)->intervals
;
2301 Lisp_Object arg
= Fcons (object
, placeholder
);
2303 traverse_intervals (root_interval
, 1, 0,
2304 &substitute_in_interval
, arg
);
2308 #endif /* defined USE_TEXT_PROPERTIES */
2310 /* Other types don't recurse any further. */
2316 /* Helper function for substitute_object_recurse. */
2318 substitute_in_interval (interval
, arg
)
2322 Lisp_Object object
= Fcar (arg
);
2323 Lisp_Object placeholder
= Fcdr (arg
);
2325 SUBSTITUTE(interval
->plist
, interval
->plist
= true_value
);
2329 #ifdef LISP_FLOAT_TYPE
2346 if (*cp
== '+' || *cp
== '-')
2349 if (*cp
>= '0' && *cp
<= '9')
2352 while (*cp
>= '0' && *cp
<= '9')
2360 if (*cp
>= '0' && *cp
<= '9')
2363 while (*cp
>= '0' && *cp
<= '9')
2366 if (*cp
== 'e' || *cp
== 'E')
2370 if (*cp
== '+' || *cp
== '-')
2374 if (*cp
>= '0' && *cp
<= '9')
2377 while (*cp
>= '0' && *cp
<= '9')
2380 else if (cp
== start
)
2382 else if (cp
[-1] == '+' && cp
[0] == 'I' && cp
[1] == 'N' && cp
[2] == 'F')
2387 else if (cp
[-1] == '+' && cp
[0] == 'N' && cp
[1] == 'a' && cp
[2] == 'N')
2393 return (((*cp
== 0) || (*cp
== ' ') || (*cp
== '\t') || (*cp
== '\n') || (*cp
== '\r') || (*cp
== '\f'))
2394 && (state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
)
2395 || state
== (DOT_CHAR
|TRAIL_INT
)
2396 || state
== (LEAD_INT
|E_CHAR
|EXP_INT
)
2397 || state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)
2398 || state
== (DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)));
2400 #endif /* LISP_FLOAT_TYPE */
2403 read_vector (readcharfun
, bytecodeflag
)
2404 Lisp_Object readcharfun
;
2409 register Lisp_Object
*ptr
;
2410 register Lisp_Object tem
, item
, vector
;
2411 register struct Lisp_Cons
*otem
;
2414 tem
= read_list (1, readcharfun
);
2415 len
= Flength (tem
);
2416 vector
= (read_pure
? make_pure_vector (XINT (len
)) : Fmake_vector (len
, Qnil
));
2418 size
= XVECTOR (vector
)->size
;
2419 ptr
= XVECTOR (vector
)->contents
;
2420 for (i
= 0; i
< size
; i
++)
2423 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2424 bytecode object, the docstring containing the bytecode and
2425 constants values must be treated as unibyte and passed to
2426 Fread, to get the actual bytecode string and constants vector. */
2427 if (bytecodeflag
&& load_force_doc_strings
)
2429 if (i
== COMPILED_BYTECODE
)
2431 if (!STRINGP (item
))
2432 error ("invalid byte code");
2434 /* Delay handling the bytecode slot until we know whether
2435 it is lazily-loaded (we can tell by whether the
2436 constants slot is nil). */
2437 ptr
[COMPILED_CONSTANTS
] = item
;
2440 else if (i
== COMPILED_CONSTANTS
)
2442 Lisp_Object bytestr
= ptr
[COMPILED_CONSTANTS
];
2446 /* Coerce string to unibyte (like string-as-unibyte,
2447 but without generating extra garbage and
2448 guaranteeing no change in the contents). */
2449 XSTRING (bytestr
)->size
= STRING_BYTES (XSTRING (bytestr
));
2450 SET_STRING_BYTES (XSTRING (bytestr
), -1);
2452 item
= Fread (bytestr
);
2454 error ("invalid byte code");
2456 otem
= XCONS (item
);
2457 bytestr
= XCAR (item
);
2462 /* Now handle the bytecode slot. */
2463 ptr
[COMPILED_BYTECODE
] = read_pure
? Fpurecopy (bytestr
) : bytestr
;
2466 ptr
[i
] = read_pure
? Fpurecopy (item
) : item
;
2474 /* FLAG = 1 means check for ] to terminate rather than ) and .
2475 FLAG = -1 means check for starting with defun
2476 and make structure pure. */
2479 read_list (flag
, readcharfun
)
2481 register Lisp_Object readcharfun
;
2483 /* -1 means check next element for defun,
2484 0 means don't check,
2485 1 means already checked and found defun. */
2486 int defunflag
= flag
< 0 ? -1 : 0;
2487 Lisp_Object val
, tail
;
2488 register Lisp_Object elt
, tem
;
2489 struct gcpro gcpro1
, gcpro2
;
2490 /* 0 is the normal case.
2491 1 means this list is a doc reference; replace it with the number 0.
2492 2 means this list is a doc reference; replace it with the doc string. */
2493 int doc_reference
= 0;
2495 /* Initialize this to 1 if we are reading a list. */
2496 int first_in_list
= flag
<= 0;
2505 elt
= read1 (readcharfun
, &ch
, first_in_list
);
2510 /* While building, if the list starts with #$, treat it specially. */
2511 if (EQ (elt
, Vload_file_name
)
2513 && !NILP (Vpurify_flag
))
2515 if (NILP (Vdoc_file_name
))
2516 /* We have not yet called Snarf-documentation, so assume
2517 this file is described in the DOC-MM.NN file
2518 and Snarf-documentation will fill in the right value later.
2519 For now, replace the whole list with 0. */
2522 /* We have already called Snarf-documentation, so make a relative
2523 file name for this file, so it can be found properly
2524 in the installed Lisp directory.
2525 We don't use Fexpand_file_name because that would make
2526 the directory absolute now. */
2527 elt
= concat2 (build_string ("../lisp/"),
2528 Ffile_name_nondirectory (elt
));
2530 else if (EQ (elt
, Vload_file_name
)
2532 && load_force_doc_strings
)
2541 Fsignal (Qinvalid_read_syntax
,
2542 Fcons (make_string (") or . in a vector", 18), Qnil
));
2550 XCDR (tail
) = read0 (readcharfun
);
2552 val
= read0 (readcharfun
);
2553 read1 (readcharfun
, &ch
, 0);
2557 if (doc_reference
== 1)
2558 return make_number (0);
2559 if (doc_reference
== 2)
2561 /* Get a doc string from the file we are loading.
2562 If it's in saved_doc_string, get it from there. */
2563 int pos
= XINT (XCDR (val
));
2564 /* Position is negative for user variables. */
2565 if (pos
< 0) pos
= -pos
;
2566 if (pos
>= saved_doc_string_position
2567 && pos
< (saved_doc_string_position
2568 + saved_doc_string_length
))
2570 int start
= pos
- saved_doc_string_position
;
2573 /* Process quoting with ^A,
2574 and find the end of the string,
2575 which is marked with ^_ (037). */
2576 for (from
= start
, to
= start
;
2577 saved_doc_string
[from
] != 037;)
2579 int c
= saved_doc_string
[from
++];
2582 c
= saved_doc_string
[from
++];
2584 saved_doc_string
[to
++] = c
;
2586 saved_doc_string
[to
++] = 0;
2588 saved_doc_string
[to
++] = 037;
2591 saved_doc_string
[to
++] = c
;
2594 return make_string (saved_doc_string
+ start
,
2597 /* Look in prev_saved_doc_string the same way. */
2598 else if (pos
>= prev_saved_doc_string_position
2599 && pos
< (prev_saved_doc_string_position
2600 + prev_saved_doc_string_length
))
2602 int start
= pos
- prev_saved_doc_string_position
;
2605 /* Process quoting with ^A,
2606 and find the end of the string,
2607 which is marked with ^_ (037). */
2608 for (from
= start
, to
= start
;
2609 prev_saved_doc_string
[from
] != 037;)
2611 int c
= prev_saved_doc_string
[from
++];
2614 c
= prev_saved_doc_string
[from
++];
2616 prev_saved_doc_string
[to
++] = c
;
2618 prev_saved_doc_string
[to
++] = 0;
2620 prev_saved_doc_string
[to
++] = 037;
2623 prev_saved_doc_string
[to
++] = c
;
2626 return make_string (prev_saved_doc_string
+ start
,
2630 return get_doc_string (val
, 0, 0);
2635 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string (". in wrong context", 18), Qnil
));
2637 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("] in a list", 11), Qnil
));
2639 tem
= (read_pure
&& flag
<= 0
2640 ? pure_cons (elt
, Qnil
)
2641 : Fcons (elt
, Qnil
));
2648 defunflag
= EQ (elt
, Qdefun
);
2649 else if (defunflag
> 0)
2654 Lisp_Object Vobarray
;
2655 Lisp_Object initial_obarray
;
2657 /* oblookup stores the bucket number here, for the sake of Funintern. */
2659 int oblookup_last_bucket_number
;
2661 static int hash_string ();
2662 Lisp_Object
oblookup ();
2664 /* Get an error if OBARRAY is not an obarray.
2665 If it is one, return it. */
2668 check_obarray (obarray
)
2669 Lisp_Object obarray
;
2671 while (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
2673 /* If Vobarray is now invalid, force it to be valid. */
2674 if (EQ (Vobarray
, obarray
)) Vobarray
= initial_obarray
;
2676 obarray
= wrong_type_argument (Qvectorp
, obarray
);
2681 /* Intern the C string STR: return a symbol with that name,
2682 interned in the current obarray. */
2689 int len
= strlen (str
);
2690 Lisp_Object obarray
;
2693 if (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
2694 obarray
= check_obarray (obarray
);
2695 tem
= oblookup (obarray
, str
, len
, len
);
2698 return Fintern (make_string (str
, len
), obarray
);
2701 /* Create an uninterned symbol with name STR. */
2707 int len
= strlen (str
);
2709 return Fmake_symbol ((!NILP (Vpurify_flag
)
2710 ? make_pure_string (str
, len
, len
, 0)
2711 : make_string (str
, len
)));
2714 DEFUN ("intern", Fintern
, Sintern
, 1, 2, 0,
2715 "Return the canonical symbol whose name is STRING.\n\
2716 If there is none, one is created by this function and returned.\n\
2717 A second optional argument specifies the obarray to use;\n\
2718 it defaults to the value of `obarray'.")
2720 Lisp_Object string
, obarray
;
2722 register Lisp_Object tem
, sym
, *ptr
;
2724 if (NILP (obarray
)) obarray
= Vobarray
;
2725 obarray
= check_obarray (obarray
);
2727 CHECK_STRING (string
, 0);
2729 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2730 XSTRING (string
)->size
,
2731 STRING_BYTES (XSTRING (string
)));
2732 if (!INTEGERP (tem
))
2735 if (!NILP (Vpurify_flag
))
2736 string
= Fpurecopy (string
);
2737 sym
= Fmake_symbol (string
);
2738 XSYMBOL (sym
)->obarray
= obarray
;
2740 if ((XSTRING (string
)->data
[0] == ':')
2741 && EQ (obarray
, initial_obarray
))
2742 XSYMBOL (sym
)->value
= sym
;
2744 ptr
= &XVECTOR (obarray
)->contents
[XINT (tem
)];
2746 XSYMBOL (sym
)->next
= XSYMBOL (*ptr
);
2748 XSYMBOL (sym
)->next
= 0;
2753 DEFUN ("intern-soft", Fintern_soft
, Sintern_soft
, 1, 2, 0,
2754 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
2755 A second optional argument specifies the obarray to use;\n\
2756 it defaults to the value of `obarray'.")
2758 Lisp_Object string
, obarray
;
2760 register Lisp_Object tem
;
2762 if (NILP (obarray
)) obarray
= Vobarray
;
2763 obarray
= check_obarray (obarray
);
2765 CHECK_STRING (string
, 0);
2767 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2768 XSTRING (string
)->size
,
2769 STRING_BYTES (XSTRING (string
)));
2770 if (!INTEGERP (tem
))
2775 DEFUN ("unintern", Funintern
, Sunintern
, 1, 2, 0,
2776 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2777 The value is t if a symbol was found and deleted, nil otherwise.\n\
2778 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2779 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2780 OBARRAY defaults to the value of the variable `obarray'.")
2782 Lisp_Object name
, obarray
;
2784 register Lisp_Object string
, tem
;
2787 if (NILP (obarray
)) obarray
= Vobarray
;
2788 obarray
= check_obarray (obarray
);
2791 XSETSTRING (string
, XSYMBOL (name
)->name
);
2794 CHECK_STRING (name
, 0);
2798 tem
= oblookup (obarray
, XSTRING (string
)->data
,
2799 XSTRING (string
)->size
,
2800 STRING_BYTES (XSTRING (string
)));
2803 /* If arg was a symbol, don't delete anything but that symbol itself. */
2804 if (SYMBOLP (name
) && !EQ (name
, tem
))
2807 XSYMBOL (tem
)->obarray
= Qnil
;
2809 hash
= oblookup_last_bucket_number
;
2811 if (EQ (XVECTOR (obarray
)->contents
[hash
], tem
))
2813 if (XSYMBOL (tem
)->next
)
2814 XSETSYMBOL (XVECTOR (obarray
)->contents
[hash
], XSYMBOL (tem
)->next
);
2816 XSETINT (XVECTOR (obarray
)->contents
[hash
], 0);
2820 Lisp_Object tail
, following
;
2822 for (tail
= XVECTOR (obarray
)->contents
[hash
];
2823 XSYMBOL (tail
)->next
;
2826 XSETSYMBOL (following
, XSYMBOL (tail
)->next
);
2827 if (EQ (following
, tem
))
2829 XSYMBOL (tail
)->next
= XSYMBOL (following
)->next
;
2838 /* Return the symbol in OBARRAY whose names matches the string
2839 of SIZE characters (SIZE_BYTE bytes) at PTR.
2840 If there is no such symbol in OBARRAY, return nil.
2842 Also store the bucket number in oblookup_last_bucket_number. */
2845 oblookup (obarray
, ptr
, size
, size_byte
)
2846 Lisp_Object obarray
;
2848 int size
, size_byte
;
2852 register Lisp_Object tail
;
2853 Lisp_Object bucket
, tem
;
2855 if (!VECTORP (obarray
)
2856 || (obsize
= XVECTOR (obarray
)->size
) == 0)
2858 obarray
= check_obarray (obarray
);
2859 obsize
= XVECTOR (obarray
)->size
;
2861 /* This is sometimes needed in the middle of GC. */
2862 obsize
&= ~ARRAY_MARK_FLAG
;
2863 /* Combining next two lines breaks VMS C 2.3. */
2864 hash
= hash_string (ptr
, size_byte
);
2866 bucket
= XVECTOR (obarray
)->contents
[hash
];
2867 oblookup_last_bucket_number
= hash
;
2868 if (XFASTINT (bucket
) == 0)
2870 else if (!SYMBOLP (bucket
))
2871 error ("Bad data in guts of obarray"); /* Like CADR error message */
2873 for (tail
= bucket
; ; XSETSYMBOL (tail
, XSYMBOL (tail
)->next
))
2875 if (STRING_BYTES (XSYMBOL (tail
)->name
) == size_byte
2876 && XSYMBOL (tail
)->name
->size
== size
2877 && !bcmp (XSYMBOL (tail
)->name
->data
, ptr
, size_byte
))
2879 else if (XSYMBOL (tail
)->next
== 0)
2882 XSETINT (tem
, hash
);
2887 hash_string (ptr
, len
)
2891 register unsigned char *p
= ptr
;
2892 register unsigned char *end
= p
+ len
;
2893 register unsigned char c
;
2894 register int hash
= 0;
2899 if (c
>= 0140) c
-= 40;
2900 hash
= ((hash
<<3) + (hash
>>28) + c
);
2902 return hash
& 07777777777;
2906 map_obarray (obarray
, fn
, arg
)
2907 Lisp_Object obarray
;
2908 void (*fn
) P_ ((Lisp_Object
, Lisp_Object
));
2912 register Lisp_Object tail
;
2913 CHECK_VECTOR (obarray
, 1);
2914 for (i
= XVECTOR (obarray
)->size
- 1; i
>= 0; i
--)
2916 tail
= XVECTOR (obarray
)->contents
[i
];
2921 if (XSYMBOL (tail
)->next
== 0)
2923 XSETSYMBOL (tail
, XSYMBOL (tail
)->next
);
2929 mapatoms_1 (sym
, function
)
2930 Lisp_Object sym
, function
;
2932 call1 (function
, sym
);
2935 DEFUN ("mapatoms", Fmapatoms
, Smapatoms
, 1, 2, 0,
2936 "Call FUNCTION on every symbol in OBARRAY.\n\
2937 OBARRAY defaults to the value of `obarray'.")
2939 Lisp_Object function
, obarray
;
2941 if (NILP (obarray
)) obarray
= Vobarray
;
2942 obarray
= check_obarray (obarray
);
2944 map_obarray (obarray
, mapatoms_1
, function
);
2948 #define OBARRAY_SIZE 1511
2953 Lisp_Object oblength
;
2957 XSETFASTINT (oblength
, OBARRAY_SIZE
);
2959 Qnil
= Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
2960 Vobarray
= Fmake_vector (oblength
, make_number (0));
2961 initial_obarray
= Vobarray
;
2962 staticpro (&initial_obarray
);
2963 /* Intern nil in the obarray */
2964 XSYMBOL (Qnil
)->obarray
= Vobarray
;
2965 /* These locals are to kludge around a pyramid compiler bug. */
2966 hash
= hash_string ("nil", 3);
2967 /* Separate statement here to avoid VAXC bug. */
2968 hash
%= OBARRAY_SIZE
;
2969 tem
= &XVECTOR (Vobarray
)->contents
[hash
];
2972 Qunbound
= Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
2973 XSYMBOL (Qnil
)->function
= Qunbound
;
2974 XSYMBOL (Qunbound
)->value
= Qunbound
;
2975 XSYMBOL (Qunbound
)->function
= Qunbound
;
2978 XSYMBOL (Qnil
)->value
= Qnil
;
2979 XSYMBOL (Qnil
)->plist
= Qnil
;
2980 XSYMBOL (Qt
)->value
= Qt
;
2982 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2985 Qvariable_documentation
= intern ("variable-documentation");
2986 staticpro (&Qvariable_documentation
);
2988 read_buffer_size
= 100 + MAX_LENGTH_OF_MULTI_BYTE_FORM
;
2989 read_buffer
= (char *) malloc (read_buffer_size
);
2994 struct Lisp_Subr
*sname
;
2997 sym
= intern (sname
->symbol_name
);
2998 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
3001 #ifdef NOTDEF /* use fset in subr.el now */
3003 defalias (sname
, string
)
3004 struct Lisp_Subr
*sname
;
3008 sym
= intern (string
);
3009 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
3013 /* Define an "integer variable"; a symbol whose value is forwarded
3014 to a C variable of type int. Sample call: */
3015 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3017 defvar_int (namestring
, address
)
3021 Lisp_Object sym
, val
;
3022 sym
= intern (namestring
);
3023 val
= allocate_misc ();
3024 XMISCTYPE (val
) = Lisp_Misc_Intfwd
;
3025 XINTFWD (val
)->intvar
= address
;
3026 XSYMBOL (sym
)->value
= val
;
3029 /* Similar but define a variable whose value is T if address contains 1,
3030 NIL if address contains 0 */
3032 defvar_bool (namestring
, address
)
3036 Lisp_Object sym
, val
;
3037 sym
= intern (namestring
);
3038 val
= allocate_misc ();
3039 XMISCTYPE (val
) = Lisp_Misc_Boolfwd
;
3040 XBOOLFWD (val
)->boolvar
= address
;
3041 XSYMBOL (sym
)->value
= val
;
3042 Vbyte_boolean_vars
= Fcons (sym
, Vbyte_boolean_vars
);
3045 /* Similar but define a variable whose value is the Lisp Object stored
3046 at address. Two versions: with and without gc-marking of the C
3047 variable. The nopro version is used when that variable will be
3048 gc-marked for some other reason, since marking the same slot twice
3049 can cause trouble with strings. */
3051 defvar_lisp_nopro (namestring
, address
)
3053 Lisp_Object
*address
;
3055 Lisp_Object sym
, val
;
3056 sym
= intern (namestring
);
3057 val
= allocate_misc ();
3058 XMISCTYPE (val
) = Lisp_Misc_Objfwd
;
3059 XOBJFWD (val
)->objvar
= address
;
3060 XSYMBOL (sym
)->value
= val
;
3064 defvar_lisp (namestring
, address
)
3066 Lisp_Object
*address
;
3068 defvar_lisp_nopro (namestring
, address
);
3069 staticpro (address
);
3074 /* Similar but define a variable whose value is the Lisp Object stored in
3075 the current buffer. address is the address of the slot in the buffer
3076 that is current now. */
3079 defvar_per_buffer (namestring
, address
, type
, doc
)
3081 Lisp_Object
*address
;
3085 Lisp_Object sym
, val
;
3087 extern struct buffer buffer_local_symbols
;
3089 sym
= intern (namestring
);
3090 val
= allocate_misc ();
3091 offset
= (char *)address
- (char *)current_buffer
;
3093 XMISCTYPE (val
) = Lisp_Misc_Buffer_Objfwd
;
3094 XBUFFER_OBJFWD (val
)->offset
= offset
;
3095 XSYMBOL (sym
)->value
= val
;
3096 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_symbols
) = sym
;
3097 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_types
) = type
;
3098 if (XINT (*(Lisp_Object
*)(offset
+ (char *)&buffer_local_flags
)) == 0)
3099 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3100 slot of buffer_local_flags */
3104 #endif /* standalone */
3106 /* Similar but define a variable whose value is the Lisp Object stored
3107 at a particular offset in the current kboard object. */
3110 defvar_kboard (namestring
, offset
)
3114 Lisp_Object sym
, val
;
3115 sym
= intern (namestring
);
3116 val
= allocate_misc ();
3117 XMISCTYPE (val
) = Lisp_Misc_Kboard_Objfwd
;
3118 XKBOARD_OBJFWD (val
)->offset
= offset
;
3119 XSYMBOL (sym
)->value
= val
;
3122 /* Record the value of load-path used at the start of dumping
3123 so we can see if the site changed it later during dumping. */
3124 static Lisp_Object dump_path
;
3130 int turn_off_warning
= 0;
3132 #ifdef HAVE_SETLOCALE
3133 /* Make sure numbers are parsed as we expect. */
3134 setlocale (LC_NUMERIC
, "C");
3135 #endif /* HAVE_SETLOCALE */
3137 /* Compute the default load-path. */
3139 normal
= PATH_LOADSEARCH
;
3140 Vload_path
= decode_env_path (0, normal
);
3142 if (NILP (Vpurify_flag
))
3143 normal
= PATH_LOADSEARCH
;
3145 normal
= PATH_DUMPLOADSEARCH
;
3147 /* In a dumped Emacs, we normally have to reset the value of
3148 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3149 uses ../lisp, instead of the path of the installed elisp
3150 libraries. However, if it appears that Vload_path was changed
3151 from the default before dumping, don't override that value. */
3154 if (! NILP (Fequal (dump_path
, Vload_path
)))
3156 Vload_path
= decode_env_path (0, normal
);
3157 if (!NILP (Vinstallation_directory
))
3159 /* Add to the path the lisp subdir of the
3160 installation dir, if it exists. */
3161 Lisp_Object tem
, tem1
;
3162 tem
= Fexpand_file_name (build_string ("lisp"),
3163 Vinstallation_directory
);
3164 tem1
= Ffile_exists_p (tem
);
3167 if (NILP (Fmember (tem
, Vload_path
)))
3169 turn_off_warning
= 1;
3170 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3174 /* That dir doesn't exist, so add the build-time
3175 Lisp dirs instead. */
3176 Vload_path
= nconc2 (Vload_path
, dump_path
);
3178 /* Add leim under the installation dir, if it exists. */
3179 tem
= Fexpand_file_name (build_string ("leim"),
3180 Vinstallation_directory
);
3181 tem1
= Ffile_exists_p (tem
);
3184 if (NILP (Fmember (tem
, Vload_path
)))
3185 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3188 /* Add site-list under the installation dir, if it exists. */
3189 tem
= Fexpand_file_name (build_string ("site-lisp"),
3190 Vinstallation_directory
);
3191 tem1
= Ffile_exists_p (tem
);
3194 if (NILP (Fmember (tem
, Vload_path
)))
3195 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3198 /* If Emacs was not built in the source directory,
3199 and it is run from where it was built, add to load-path
3200 the lisp, leim and site-lisp dirs under that directory. */
3202 if (NILP (Fequal (Vinstallation_directory
, Vsource_directory
)))
3206 tem
= Fexpand_file_name (build_string ("src/Makefile"),
3207 Vinstallation_directory
);
3208 tem1
= Ffile_exists_p (tem
);
3210 /* Don't be fooled if they moved the entire source tree
3211 AFTER dumping Emacs. If the build directory is indeed
3212 different from the source dir, src/Makefile.in and
3213 src/Makefile will not be found together. */
3214 tem
= Fexpand_file_name (build_string ("src/Makefile.in"),
3215 Vinstallation_directory
);
3216 tem2
= Ffile_exists_p (tem
);
3217 if (!NILP (tem1
) && NILP (tem2
))
3219 tem
= Fexpand_file_name (build_string ("lisp"),
3222 if (NILP (Fmember (tem
, Vload_path
)))
3223 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3225 tem
= Fexpand_file_name (build_string ("leim"),
3228 if (NILP (Fmember (tem
, Vload_path
)))
3229 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3231 tem
= Fexpand_file_name (build_string ("site-lisp"),
3234 if (NILP (Fmember (tem
, Vload_path
)))
3235 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
3243 /* NORMAL refers to the lisp dir in the source directory. */
3244 /* We used to add ../lisp at the front here, but
3245 that caused trouble because it was copied from dump_path
3246 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3247 It should be unnecessary. */
3248 Vload_path
= decode_env_path (0, normal
);
3249 dump_path
= Vload_path
;
3254 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3255 almost never correct, thereby causing a warning to be printed out that
3256 confuses users. Since PATH_LOADSEARCH is always overridden by the
3257 EMACSLOADPATH environment variable below, disable the warning on NT. */
3259 /* Warn if dirs in the *standard* path don't exist. */
3260 if (!turn_off_warning
)
3262 Lisp_Object path_tail
;
3264 for (path_tail
= Vload_path
;
3266 path_tail
= XCDR (path_tail
))
3268 Lisp_Object dirfile
;
3269 dirfile
= Fcar (path_tail
);
3270 if (STRINGP (dirfile
))
3272 dirfile
= Fdirectory_file_name (dirfile
);
3273 if (access (XSTRING (dirfile
)->data
, 0) < 0)
3274 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3279 #endif /* WINDOWSNT */
3281 /* If the EMACSLOADPATH environment variable is set, use its value.
3282 This doesn't apply if we're dumping. */
3284 if (NILP (Vpurify_flag
)
3285 && egetenv ("EMACSLOADPATH"))
3287 Vload_path
= decode_env_path ("EMACSLOADPATH", normal
);
3291 load_in_progress
= 0;
3292 Vload_file_name
= Qnil
;
3294 load_descriptor_list
= Qnil
;
3296 Vstandard_input
= Qt
;
3299 /* Print a warning, using format string FORMAT, that directory DIRNAME
3300 does not exist. Print it on stderr and put it in *Message*. */
3303 dir_warning (format
, dirname
)
3305 Lisp_Object dirname
;
3308 = (char *) alloca (XSTRING (dirname
)->size
+ strlen (format
) + 5);
3310 fprintf (stderr
, format
, XSTRING (dirname
)->data
);
3311 sprintf (buffer
, format
, XSTRING (dirname
)->data
);
3312 /* Don't log the warning before we've initialized!! */
3314 message_dolog (buffer
, strlen (buffer
), 0, STRING_MULTIBYTE (dirname
));
3321 defsubr (&Sread_from_string
);
3323 defsubr (&Sintern_soft
);
3324 defsubr (&Sunintern
);
3326 defsubr (&Seval_buffer
);
3327 defsubr (&Seval_region
);
3328 defsubr (&Sread_char
);
3329 defsubr (&Sread_char_exclusive
);
3330 defsubr (&Sread_event
);
3331 defsubr (&Sget_file_char
);
3332 defsubr (&Smapatoms
);
3334 DEFVAR_LISP ("obarray", &Vobarray
,
3335 "Symbol table for use by `intern' and `read'.\n\
3336 It is a vector whose length ought to be prime for best results.\n\
3337 The vector's contents don't make sense if examined from Lisp programs;\n\
3338 to find all the symbols in an obarray, use `mapatoms'.");
3340 DEFVAR_LISP ("values", &Vvalues
,
3341 "List of values of all expressions which were read, evaluated and printed.\n\
3342 Order is reverse chronological.");
3344 DEFVAR_LISP ("standard-input", &Vstandard_input
,
3345 "Stream for read to get input from.\n\
3346 See documentation of `read' for possible values.");
3347 Vstandard_input
= Qt
;
3349 DEFVAR_LISP ("load-path", &Vload_path
,
3350 "*List of directories to search for files to load.\n\
3351 Each element is a string (directory name) or nil (try default directory).\n\
3352 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3353 otherwise to default specified by file `epaths.h' when Emacs was built.");
3355 DEFVAR_BOOL ("load-in-progress", &load_in_progress
,
3356 "Non-nil iff inside of `load'.");
3358 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist
,
3359 "An alist of expressions to be evalled when particular files are loaded.\n\
3360 Each element looks like (FILENAME FORMS...).\n\
3361 When `load' is run and the file-name argument is FILENAME,\n\
3362 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3363 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3364 with no directory specified, since that is how `load' is normally called.\n\
3365 An error in FORMS does not undo the load,\n\
3366 but does prevent execution of the rest of the FORMS.");
3367 Vafter_load_alist
= Qnil
;
3369 DEFVAR_LISP ("load-history", &Vload_history
,
3370 "Alist mapping source file names to symbols and features.\n\
3371 Each alist element is a list that starts with a file name,\n\
3372 except for one element (optional) that starts with nil and describes\n\
3373 definitions evaluated from buffers not visiting files.\n\
3374 The remaining elements of each list are symbols defined as functions\n\
3375 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
3376 Vload_history
= Qnil
;
3378 DEFVAR_LISP ("load-file-name", &Vload_file_name
,
3379 "Full name of file being loaded by `load'.");
3380 Vload_file_name
= Qnil
;
3382 DEFVAR_LISP ("user-init-file", &Vuser_init_file
,
3383 "File name, including directory, of user's initialization file.");
3384 Vuser_init_file
= Qnil
;
3386 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list
,
3387 "Used for internal purposes by `load'.");
3388 Vcurrent_load_list
= Qnil
;
3390 DEFVAR_LISP ("load-read-function", &Vload_read_function
,
3391 "Function used by `load' and `eval-region' for reading expressions.\n\
3392 The default is nil, which means use the function `read'.");
3393 Vload_read_function
= Qnil
;
3395 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function
,
3396 "Function called in `load' for loading an Emacs lisp source file.\n\
3397 This function is for doing code conversion before reading the source file.\n\
3398 If nil, loading is done without any code conversion.\n\
3399 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3400 FULLNAME is the full name of FILE.\n\
3401 See `load' for the meaning of the remaining arguments.");
3402 Vload_source_file_function
= Qnil
;
3404 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings
,
3405 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3406 This is useful when the file being loaded is a temporary copy.");
3407 load_force_doc_strings
= 0;
3409 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte
,
3410 "Non-nil means `load' converts strings to unibyte whenever possible.\n\
3411 This is normally used in `load-with-code-conversion'\n\
3412 for loading non-compiled files.");
3413 load_convert_to_unibyte
= 0;
3415 DEFVAR_LISP ("source-directory", &Vsource_directory
,
3416 "Directory in which Emacs sources were found when Emacs was built.\n\
3417 You cannot count on them to still be there!");
3419 = Fexpand_file_name (build_string ("../"),
3420 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH
)));
3422 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list
,
3423 "List of files that were preloaded (when dumping Emacs).");
3424 Vpreloaded_file_list
= Qnil
;
3426 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars
,
3427 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3428 Vbyte_boolean_vars
= Qnil
;
3430 /* Vsource_directory was initialized in init_lread. */
3432 load_descriptor_list
= Qnil
;
3433 staticpro (&load_descriptor_list
);
3435 Qcurrent_load_list
= intern ("current-load-list");
3436 staticpro (&Qcurrent_load_list
);
3438 Qstandard_input
= intern ("standard-input");
3439 staticpro (&Qstandard_input
);
3441 Qread_char
= intern ("read-char");
3442 staticpro (&Qread_char
);
3444 Qget_file_char
= intern ("get-file-char");
3445 staticpro (&Qget_file_char
);
3447 Qbackquote
= intern ("`");
3448 staticpro (&Qbackquote
);
3449 Qcomma
= intern (",");
3450 staticpro (&Qcomma
);
3451 Qcomma_at
= intern (",@");
3452 staticpro (&Qcomma_at
);
3453 Qcomma_dot
= intern (",.");
3454 staticpro (&Qcomma_dot
);
3456 Qinhibit_file_name_operation
= intern ("inhibit-file-name-operation");
3457 staticpro (&Qinhibit_file_name_operation
);
3459 Qascii_character
= intern ("ascii-character");
3460 staticpro (&Qascii_character
);
3462 Qfunction
= intern ("function");
3463 staticpro (&Qfunction
);
3465 Qload
= intern ("load");
3468 Qload_file_name
= intern ("load-file-name");
3469 staticpro (&Qload_file_name
);
3471 staticpro (&dump_path
);
3473 staticpro (&read_objects
);
3474 read_objects
= Qnil
;
3475 staticpro (&seen_list
);