1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989,
3 1993, 1994, 1995 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>
36 #include "termhooks.h"
40 #include <sys/inode.h>
45 #include <unistd.h> /* to get X_OK */
54 #ifdef LISP_FLOAT_TYPE
60 #endif /* LISP_FLOAT_TYPE */
64 #endif /* HAVE_SETLOCALE */
72 Lisp_Object Qread_char
, Qget_file_char
, Qstandard_input
, Qcurrent_load_list
;
73 Lisp_Object Qvariable_documentation
, Vvalues
, Vstandard_input
, Vafter_load_alist
;
74 Lisp_Object Qascii_character
, Qload
, Qload_file_name
;
75 Lisp_Object Qbackquote
, Qcomma
, Qcomma_at
, Qcomma_dot
, Qfunction
;
77 extern Lisp_Object Qevent_symbol_element_mask
;
79 /* non-zero if inside `load' */
82 /* Directory in which the sources were found. */
83 Lisp_Object Vsource_directory
;
85 /* Search path for files to be loaded. */
86 Lisp_Object Vload_path
;
88 /* This is the user-visible association list that maps features to
89 lists of defs in their load files. */
90 Lisp_Object Vload_history
;
92 /* This is used to build the load history. */
93 Lisp_Object Vcurrent_load_list
;
95 /* Name of file actually being read by `load'. */
96 Lisp_Object Vload_file_name
;
98 /* Function to use for reading, in `load' and friends. */
99 Lisp_Object Vload_read_function
;
101 /* The association list of objects read with the #n=object form.
102 Each member of the list has the form (n . object), and is used to
103 look up the object for the corresponding #n# construct.
104 It must be set to nil before all top-level calls to read0. */
105 Lisp_Object read_objects
;
107 /* Nonzero means load should forcibly load all dynamic doc strings. */
108 static int load_force_doc_strings
;
110 /* List of descriptors now open for Fload. */
111 static Lisp_Object load_descriptor_list
;
113 /* File for get_file_char to read from. Use by load. */
114 static FILE *instream
;
116 /* When nonzero, read conses in pure space */
117 static int read_pure
;
119 /* For use within read-from-string (this reader is non-reentrant!!) */
120 static int read_from_string_index
;
121 static int read_from_string_limit
;
123 /* This contains the last string skipped with #@. */
124 static char *saved_doc_string
;
125 /* Length of buffer allocated in saved_doc_string. */
126 static int saved_doc_string_size
;
127 /* Length of actual data in saved_doc_string. */
128 static int saved_doc_string_length
;
129 /* This is the file position that string came from. */
130 static int saved_doc_string_position
;
132 /* Nonzero means inside a new-style backquote
133 with no surrounding parentheses.
134 Fread initializes this to zero, so we need not specbind it
135 or worry about what happens to it when there is an error. */
136 static int new_backquote_flag
;
138 /* Handle unreading and rereading of characters.
139 Write READCHAR to read a character,
140 UNREAD(c) to unread c to be read again. */
142 #define READCHAR readchar (readcharfun)
143 #define UNREAD(c) unreadchar (readcharfun, c)
146 readchar (readcharfun
)
147 Lisp_Object readcharfun
;
150 register struct buffer
*inbuffer
;
151 register int c
, mpos
;
153 if (BUFFERP (readcharfun
))
155 inbuffer
= XBUFFER (readcharfun
);
157 if (BUF_PT (inbuffer
) >= BUF_ZV (inbuffer
))
159 c
= *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer
, BUF_PT (inbuffer
));
160 SET_BUF_PT (inbuffer
, BUF_PT (inbuffer
) + 1);
164 if (MARKERP (readcharfun
))
166 inbuffer
= XMARKER (readcharfun
)->buffer
;
168 mpos
= marker_position (readcharfun
);
170 if (mpos
> BUF_ZV (inbuffer
) - 1)
172 c
= *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer
, mpos
);
173 if (mpos
!= BUF_GPT (inbuffer
))
174 XMARKER (readcharfun
)->bufpos
++;
176 Fset_marker (readcharfun
, make_number (mpos
+ 1),
177 Fmarker_buffer (readcharfun
));
180 if (EQ (readcharfun
, Qget_file_char
))
184 /* Interrupted reads have been observed while reading over the network */
185 while (c
== EOF
&& ferror (instream
) && errno
== EINTR
)
194 if (STRINGP (readcharfun
))
197 /* This used to be return of a conditional expression,
198 but that truncated -1 to a char on VMS. */
199 if (read_from_string_index
< read_from_string_limit
)
200 c
= XSTRING (readcharfun
)->data
[read_from_string_index
++];
206 tem
= call0 (readcharfun
);
213 /* Unread the character C in the way appropriate for the stream READCHARFUN.
214 If the stream is a user function, call it with the char as argument. */
217 unreadchar (readcharfun
, c
)
218 Lisp_Object readcharfun
;
222 /* Don't back up the pointer if we're unreading the end-of-input mark,
223 since readchar didn't advance it when we read it. */
225 else if (BUFFERP (readcharfun
))
227 if (XBUFFER (readcharfun
) == current_buffer
)
230 SET_BUF_PT (XBUFFER (readcharfun
), BUF_PT (XBUFFER (readcharfun
)) - 1);
232 else if (MARKERP (readcharfun
))
233 XMARKER (readcharfun
)->bufpos
--;
234 else if (STRINGP (readcharfun
))
235 read_from_string_index
--;
236 else if (EQ (readcharfun
, Qget_file_char
))
237 ungetc (c
, instream
);
239 call1 (readcharfun
, make_number (c
));
242 static Lisp_Object
read0 (), read1 (), read_list (), read_vector ();
244 /* get a character from the tty */
246 extern Lisp_Object
read_char ();
248 /* Read input events until we get one that's acceptable for our purposes.
250 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
251 until we get a character we like, and then stuffed into
254 If ASCII_REQUIRED is non-zero, we check function key events to see
255 if the unmodified version of the symbol has a Qascii_character
256 property, and use that character, if present.
258 If ERROR_NONASCII is non-zero, we signal an error if the input we
259 get isn't an ASCII character with modifiers. If it's zero but
260 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
264 read_filtered_event (no_switch_frame
, ascii_required
, error_nonascii
)
265 int no_switch_frame
, ascii_required
, error_nonascii
;
268 return make_number (getchar ());
270 register Lisp_Object val
, delayed_switch_frame
;
272 delayed_switch_frame
= Qnil
;
274 /* Read until we get an acceptable event. */
276 val
= read_char (0, 0, 0, Qnil
, 0);
281 /* switch-frame events are put off until after the next ASCII
282 character. This is better than signaling an error just because
283 the last characters were typed to a separate minibuffer frame,
284 for example. Eventually, some code which can deal with
285 switch-frame events will read it and process it. */
287 && EVENT_HAS_PARAMETERS (val
)
288 && EQ (EVENT_HEAD (val
), Qswitch_frame
))
290 delayed_switch_frame
= val
;
296 /* Convert certain symbols to their ASCII equivalents. */
299 Lisp_Object tem
, tem1
, tem2
;
300 tem
= Fget (val
, Qevent_symbol_element_mask
);
303 tem1
= Fget (Fcar (tem
), Qascii_character
);
304 /* Merge this symbol's modifier bits
305 with the ASCII equivalent of its basic code. */
307 XSETFASTINT (val
, XINT (tem1
) | XINT (Fcar (Fcdr (tem
))));
311 /* If we don't have a character now, deal with it appropriately. */
316 Vunread_command_events
= Fcons (val
, Qnil
);
317 error ("Non-character input-event");
324 if (! NILP (delayed_switch_frame
))
325 unread_switch_frame
= delayed_switch_frame
;
331 DEFUN ("read-char", Fread_char
, Sread_char
, 0, 0, 0,
332 "Read a character from the command input (keyboard or macro).\n\
333 It is returned as a number.\n\
334 If the user generates an event which is not a character (i.e. a mouse\n\
335 click or function key event), `read-char' signals an error. As an\n\
336 exception, switch-frame events are put off until non-ASCII events can\n\
338 If you want to read non-character events, or ignore them, call\n\
339 `read-event' or `read-char-exclusive' instead.")
342 return read_filtered_event (1, 1, 1);
345 DEFUN ("read-event", Fread_event
, Sread_event
, 0, 0, 0,
346 "Read an event object from the input stream.")
349 return read_filtered_event (0, 0, 0);
352 DEFUN ("read-char-exclusive", Fread_char_exclusive
, Sread_char_exclusive
, 0, 0, 0,
353 "Read a character from the command input (keyboard or macro).\n\
354 It is returned as a number. Non-character events are ignored.")
357 return read_filtered_event (1, 1, 0);
360 DEFUN ("get-file-char", Fget_file_char
, Sget_file_char
, 0, 0, 0,
361 "Don't use this yourself.")
364 register Lisp_Object val
;
365 XSETINT (val
, getc (instream
));
369 static void readevalloop ();
370 static Lisp_Object
load_unwind ();
371 static Lisp_Object
load_descriptor_unwind ();
373 DEFUN ("load", Fload
, Sload
, 1, 4, 0,
374 "Execute a file of Lisp code named FILE.\n\
375 First try FILE with `.elc' appended, then try with `.el',\n\
376 then try FILE unmodified.\n\
377 This function searches the directories in `load-path'.\n\
378 If optional second arg NOERROR is non-nil,\n\
379 report no error if FILE doesn't exist.\n\
380 Print messages at start and end of loading unless\n\
381 optional third arg NOMESSAGE is non-nil.\n\
382 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
383 suffixes `.elc' or `.el' to the specified name FILE.\n\
384 Return t if file exists.")
385 (file
, noerror
, nomessage
, nosuffix
)
386 Lisp_Object file
, noerror
, nomessage
, nosuffix
;
388 register FILE *stream
;
389 register int fd
= -1;
390 register Lisp_Object lispstream
;
391 int count
= specpdl_ptr
- specpdl
;
395 /* 1 means we printed the ".el is newer" message. */
397 /* 1 means we are loading a compiled file. */
401 char *dosmode
= "rt";
404 CHECK_STRING (file
, 0);
406 /* If file name is magic, call the handler. */
407 handler
= Ffind_file_name_handler (file
, Qload
);
409 return call5 (handler
, Qload
, file
, noerror
, nomessage
, nosuffix
);
411 /* Do this after the handler to avoid
412 the need to gcpro noerror, nomessage and nosuffix.
413 (Below here, we care only whether they are nil or not.) */
414 file
= Fsubstitute_in_file_name (file
);
416 /* Avoid weird lossage with null string as arg,
417 since it would try to load a directory as a Lisp file */
418 if (XSTRING (file
)->size
> 0)
421 fd
= openp (Vload_path
, file
, !NILP (nosuffix
) ? "" : ".elc:.el:",
430 Fsignal (Qfile_error
, Fcons (build_string ("Cannot open load file"),
431 Fcons (file
, Qnil
)));
436 if (!bcmp (&(XSTRING (found
)->data
[XSTRING (found
)->size
- 4]),
447 stat ((char *)XSTRING (found
)->data
, &s1
);
448 XSTRING (found
)->data
[XSTRING (found
)->size
- 1] = 0;
449 result
= stat ((char *)XSTRING (found
)->data
, &s2
);
450 if (result
>= 0 && (unsigned) s1
.st_mtime
< (unsigned) s2
.st_mtime
)
452 /* Make the progress messages mention that source is newer. */
455 /* If we won't print another message, mention this anyway. */
456 if (! NILP (nomessage
))
457 message ("Source file `%s' newer than byte-compiled file",
458 XSTRING (found
)->data
);
460 XSTRING (found
)->data
[XSTRING (found
)->size
- 1] = 'c';
465 stream
= fopen ((char *) XSTRING (found
)->data
, dosmode
);
466 #else /* not DOS_NT */
467 stream
= fdopen (fd
, "r");
468 #endif /* not DOS_NT */
472 error ("Failure to create stdio stream for %s", XSTRING (file
)->data
);
475 if (NILP (nomessage
))
478 message ("Loading %s (compiled; note, source file is newer)...",
479 XSTRING (file
)->data
);
481 message ("Loading %s (compiled)...", XSTRING (file
)->data
);
483 message ("Loading %s...", XSTRING (file
)->data
);
487 lispstream
= Fcons (Qnil
, Qnil
);
488 XSETFASTINT (XCONS (lispstream
)->car
, (EMACS_UINT
)stream
>> 16);
489 XSETFASTINT (XCONS (lispstream
)->cdr
, (EMACS_UINT
)stream
& 0xffff);
490 record_unwind_protect (load_unwind
, lispstream
);
491 record_unwind_protect (load_descriptor_unwind
, load_descriptor_list
);
492 specbind (Qload_file_name
, found
);
494 = Fcons (make_number (fileno (stream
)), load_descriptor_list
);
496 readevalloop (Qget_file_char
, stream
, file
, Feval
, 0);
497 unbind_to (count
, Qnil
);
499 /* Run any load-hooks for this file. */
500 temp
= Fassoc (file
, Vafter_load_alist
);
502 Fprogn (Fcdr (temp
));
505 if (saved_doc_string
)
506 free (saved_doc_string
);
507 saved_doc_string
= 0;
508 saved_doc_string_size
= 0;
510 if (!noninteractive
&& NILP (nomessage
))
513 message ("Loading %s (compiled; note, source file is newer)...done",
514 XSTRING (file
)->data
);
516 message ("Loading %s (compiled)...done", XSTRING (file
)->data
);
518 message ("Loading %s...done", XSTRING (file
)->data
);
524 load_unwind (stream
) /* used as unwind-protect function in load */
527 fclose ((FILE *) (XFASTINT (XCONS (stream
)->car
) << 16
528 | XFASTINT (XCONS (stream
)->cdr
)));
529 if (--load_in_progress
< 0) load_in_progress
= 0;
534 load_descriptor_unwind (oldlist
)
537 load_descriptor_list
= oldlist
;
541 /* Close all descriptors in use for Floads.
542 This is used when starting a subprocess. */
549 for (tail
= load_descriptor_list
; !NILP (tail
); tail
= XCONS (tail
)->cdr
)
550 close (XFASTINT (XCONS (tail
)->car
));
555 complete_filename_p (pathname
)
556 Lisp_Object pathname
;
558 register unsigned char *s
= XSTRING (pathname
)->data
;
559 return (IS_DIRECTORY_SEP (s
[0])
560 || (XSTRING (pathname
)->size
> 2
561 && IS_DEVICE_SEP (s
[1]) && IS_DIRECTORY_SEP (s
[2]))
571 /* Search for a file whose name is STR, looking in directories
572 in the Lisp list PATH, and trying suffixes from SUFFIX.
573 SUFFIX is a string containing possible suffixes separated by colons.
574 On success, returns a file descriptor. On failure, returns -1.
576 EXEC_ONLY nonzero means don't open the files,
577 just look for one that is executable. In this case,
578 returns 1 on success.
580 If STOREPTR is nonzero, it points to a slot where the name of
581 the file actually found should be stored as a Lisp string.
582 Nil is stored there on failure. */
585 openp (path
, str
, suffix
, storeptr
, exec_only
)
586 Lisp_Object path
, str
;
588 Lisp_Object
*storeptr
;
594 register char *fn
= buf
;
597 register Lisp_Object filename
;
605 if (complete_filename_p (str
))
608 for (; !NILP (path
); path
= Fcdr (path
))
612 filename
= Fexpand_file_name (str
, Fcar (path
));
613 if (!complete_filename_p (filename
))
614 /* If there are non-absolute elts in PATH (eg ".") */
615 /* Of course, this could conceivably lose if luser sets
616 default-directory to be something non-absolute... */
618 filename
= Fexpand_file_name (filename
, current_buffer
->directory
);
619 if (!complete_filename_p (filename
))
620 /* Give up on this path element! */
624 /* Calculate maximum size of any filename made from
625 this path element/specified file name and any possible suffix. */
626 want_size
= strlen (suffix
) + XSTRING (filename
)->size
+ 1;
627 if (fn_size
< want_size
)
628 fn
= (char *) alloca (fn_size
= 100 + want_size
);
632 /* Loop over suffixes. */
635 char *esuffix
= (char *) index (nsuffix
, ':');
636 int lsuffix
= esuffix
? esuffix
- nsuffix
: strlen (nsuffix
);
638 /* Concatenate path element/specified name with the suffix.
639 If the directory starts with /:, remove that. */
640 if (XSTRING (filename
)->size
> 2
641 && XSTRING (filename
)->data
[0] == '/'
642 && XSTRING (filename
)->data
[1] == ':')
644 strncpy (fn
, XSTRING (filename
)->data
+ 2,
645 XSTRING (filename
)->size
- 2);
646 fn
[XSTRING (filename
)->size
- 2] = 0;
650 strncpy (fn
, XSTRING (filename
)->data
, XSTRING (filename
)->size
);
651 fn
[XSTRING (filename
)->size
] = 0;
654 if (lsuffix
!= 0) /* Bug happens on CCI if lsuffix is 0. */
655 strncat (fn
, nsuffix
, lsuffix
);
657 /* Ignore file if it's a directory. */
658 if (stat (fn
, &st
) >= 0
659 && (st
.st_mode
& S_IFMT
) != S_IFDIR
)
661 /* Check that we can access or open it. */
663 fd
= (access (fn
, X_OK
) == 0) ? 1 : -1;
665 fd
= open (fn
, O_RDONLY
, 0);
669 /* We succeeded; return this descriptor and filename. */
671 *storeptr
= build_string (fn
);
677 /* Advance to next suffix. */
680 nsuffix
+= lsuffix
+ 1;
691 /* Merge the list we've accumulated of globals from the current input source
692 into the load_history variable. The details depend on whether
693 the source has an associated file name or not. */
696 build_load_history (stream
, source
)
700 register Lisp_Object tail
, prev
, newelt
;
701 register Lisp_Object tem
, tem2
;
702 register int foundit
, loading
;
704 /* Don't bother recording anything for preloaded files. */
705 if (!NILP (Vpurify_flag
))
708 loading
= stream
|| !NARROWED
;
710 tail
= Vload_history
;
717 /* Find the feature's previous assoc list... */
718 if (!NILP (Fequal (source
, Fcar (tem
))))
722 /* If we're loading, remove it. */
726 Vload_history
= Fcdr (tail
);
728 Fsetcdr (prev
, Fcdr (tail
));
731 /* Otherwise, cons on new symbols that are not already members. */
734 tem2
= Vcurrent_load_list
;
738 newelt
= Fcar (tem2
);
740 if (NILP (Fmemq (newelt
, tem
)))
741 Fsetcar (tail
, Fcons (Fcar (tem
),
742 Fcons (newelt
, Fcdr (tem
))));
755 /* If we're loading, cons the new assoc onto the front of load-history,
756 the most-recently-loaded position. Also do this if we didn't find
757 an existing member for the current source. */
758 if (loading
|| !foundit
)
759 Vload_history
= Fcons (Fnreverse (Vcurrent_load_list
),
764 unreadpure () /* Used as unwind-protect function in readevalloop */
771 readevalloop (readcharfun
, stream
, sourcename
, evalfun
, printflag
)
772 Lisp_Object readcharfun
;
774 Lisp_Object sourcename
;
775 Lisp_Object (*evalfun
) ();
779 register Lisp_Object val
;
780 int count
= specpdl_ptr
- specpdl
;
782 struct buffer
*b
= 0;
784 if (BUFFERP (readcharfun
))
785 b
= XBUFFER (readcharfun
);
786 else if (MARKERP (readcharfun
))
787 b
= XMARKER (readcharfun
)->buffer
;
789 specbind (Qstandard_input
, readcharfun
);
790 specbind (Qcurrent_load_list
, Qnil
);
794 LOADHIST_ATTACH (sourcename
);
798 if (b
!= 0 && NILP (b
->name
))
799 error ("Reading from killed buffer");
805 while ((c
= READCHAR
) != '\n' && c
!= -1);
810 /* Ignore whitespace here, so we can detect eof. */
811 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r')
814 if (!NILP (Vpurify_flag
) && c
== '(')
816 int count1
= specpdl_ptr
- specpdl
;
817 record_unwind_protect (unreadpure
, Qnil
);
818 val
= read_list (-1, readcharfun
);
819 unbind_to (count1
, Qnil
);
825 if (NILP (Vload_read_function
))
826 val
= read0 (readcharfun
);
828 val
= call1 (Vload_read_function
, readcharfun
);
831 val
= (*evalfun
) (val
);
834 Vvalues
= Fcons (val
, Vvalues
);
835 if (EQ (Vstandard_output
, Qt
))
842 build_load_history (stream
, sourcename
);
845 unbind_to (count
, Qnil
);
850 DEFUN ("eval-buffer", Feval_buffer
, Seval_buffer
, 0, 2, "",
851 "Execute the current buffer as Lisp code.\n\
852 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
853 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
854 PRINTFLAG controls printing of output:\n\
855 nil means discard it; anything else is stream for print.\n\
857 If there is no error, point does not move. If there is an error,\n\
858 point remains at the end of the last character read from the buffer.")
860 Lisp_Object buffer
, printflag
;
862 int count
= specpdl_ptr
- specpdl
;
863 Lisp_Object tem
, buf
;
866 buf
= Fcurrent_buffer ();
868 buf
= Fget_buffer (buffer
);
870 error ("No such buffer.");
872 if (NILP (printflag
))
876 specbind (Qstandard_output
, tem
);
877 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
878 BUF_SET_PT (XBUFFER (buf
), BUF_BEGV (XBUFFER (buf
)));
879 readevalloop (buf
, 0, XBUFFER (buf
)->filename
, Feval
, !NILP (printflag
));
880 unbind_to (count
, Qnil
);
886 DEFUN ("eval-current-buffer", Feval_current_buffer
, Seval_current_buffer
, 0, 1, "",
887 "Execute the current buffer as Lisp code.\n\
888 Programs can pass argument PRINTFLAG which controls printing of output:\n\
889 nil means discard it; anything else is stream for print.\n\
891 If there is no error, point does not move. If there is an error,\n\
892 point remains at the end of the last character read from the buffer.")
894 Lisp_Object printflag
;
896 int count
= specpdl_ptr
- specpdl
;
897 Lisp_Object tem
, cbuf
;
899 cbuf
= Fcurrent_buffer ()
901 if (NILP (printflag
))
905 specbind (Qstandard_output
, tem
);
906 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
908 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
, !NILP (printflag
));
909 return unbind_to (count
, Qnil
);
913 DEFUN ("eval-region", Feval_region
, Seval_region
, 2, 3, "r",
914 "Execute the region as Lisp code.\n\
915 When called from programs, expects two arguments,\n\
916 giving starting and ending indices in the current buffer\n\
917 of the text to be executed.\n\
918 Programs can pass third argument PRINTFLAG which controls output:\n\
919 nil means discard it; anything else is stream for printing it.\n\
921 If there is no error, point does not move. If there is an error,\n\
922 point remains at the end of the last character read from the buffer.")
923 (start
, end
, printflag
)
924 Lisp_Object start
, end
, printflag
;
926 int count
= specpdl_ptr
- specpdl
;
927 Lisp_Object tem
, cbuf
;
929 cbuf
= Fcurrent_buffer ();
931 if (NILP (printflag
))
935 specbind (Qstandard_output
, tem
);
937 if (NILP (printflag
))
938 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
939 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
941 /* This both uses start and checks its type. */
943 Fnarrow_to_region (make_number (BEGV
), end
);
944 readevalloop (cbuf
, 0, XBUFFER (cbuf
)->filename
, Feval
, !NILP (printflag
));
946 return unbind_to (count
, Qnil
);
949 #endif /* standalone */
951 DEFUN ("read", Fread
, Sread
, 0, 1, 0,
952 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
953 If STREAM is nil, use the value of `standard-input' (which see).\n\
954 STREAM or the value of `standard-input' may be:\n\
955 a buffer (read from point and advance it)\n\
956 a marker (read from where it points and advance it)\n\
957 a function (call it with no arguments for each character,\n\
958 call it with a char as argument to push a char back)\n\
959 a string (takes text from string, starting at the beginning)\n\
960 t (read text line using minibuffer and use it).")
964 extern Lisp_Object
Fread_minibuffer ();
967 stream
= Vstandard_input
;
971 new_backquote_flag
= 0;
975 if (EQ (stream
, Qread_char
))
976 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil
);
979 if (STRINGP (stream
))
980 return Fcar (Fread_from_string (stream
, Qnil
, Qnil
));
982 return read0 (stream
);
985 DEFUN ("read-from-string", Fread_from_string
, Sread_from_string
, 1, 3, 0,
986 "Read one Lisp expression which is represented as text by STRING.\n\
987 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
988 START and END optionally delimit a substring of STRING from which to read;\n\
989 they default to 0 and (length STRING) respectively.")
991 Lisp_Object string
, start
, end
;
993 int startval
, endval
;
996 CHECK_STRING (string
,0);
999 endval
= XSTRING (string
)->size
;
1001 { CHECK_NUMBER (end
,2);
1002 endval
= XINT (end
);
1003 if (endval
< 0 || endval
> XSTRING (string
)->size
)
1004 args_out_of_range (string
, end
);
1010 { CHECK_NUMBER (start
,1);
1011 startval
= XINT (start
);
1012 if (startval
< 0 || startval
> endval
)
1013 args_out_of_range (string
, start
);
1016 read_from_string_index
= startval
;
1017 read_from_string_limit
= endval
;
1019 new_backquote_flag
= 0;
1020 read_objects
= Qnil
;
1022 tem
= read0 (string
);
1023 return Fcons (tem
, make_number (read_from_string_index
));
1026 /* Use this for recursive reads, in contexts where internal tokens
1030 Lisp_Object readcharfun
;
1032 register Lisp_Object val
;
1035 val
= read1 (readcharfun
, &c
, 0);
1037 Fsignal (Qinvalid_read_syntax
, Fcons (make_string (&c
, 1), Qnil
));
1042 static int read_buffer_size
;
1043 static char *read_buffer
;
1046 read_escape (readcharfun
)
1047 Lisp_Object readcharfun
;
1049 register int c
= READCHAR
;
1053 error ("End of file");
1079 error ("Invalid escape character syntax");
1082 c
= read_escape (readcharfun
);
1083 return c
| meta_modifier
;
1088 error ("Invalid escape character syntax");
1091 c
= read_escape (readcharfun
);
1092 return c
| shift_modifier
;
1097 error ("Invalid escape character syntax");
1100 c
= read_escape (readcharfun
);
1101 return c
| hyper_modifier
;
1106 error ("Invalid escape character syntax");
1109 c
= read_escape (readcharfun
);
1110 return c
| alt_modifier
;
1115 error ("Invalid escape character syntax");
1118 c
= read_escape (readcharfun
);
1119 return c
| super_modifier
;
1124 error ("Invalid escape character syntax");
1128 c
= read_escape (readcharfun
);
1129 if ((c
& 0177) == '?')
1131 /* ASCII control chars are made from letters (both cases),
1132 as well as the non-letters within 0100...0137. */
1133 else if ((c
& 0137) >= 0101 && (c
& 0137) <= 0132)
1134 return (c
& (037 | ~0177));
1135 else if ((c
& 0177) >= 0100 && (c
& 0177) <= 0137)
1136 return (c
& (037 | ~0177));
1138 return c
| ctrl_modifier
;
1148 /* An octal escape, as in ANSI C. */
1150 register int i
= c
- '0';
1151 register int count
= 0;
1154 if ((c
= READCHAR
) >= '0' && c
<= '7')
1169 /* A hex escape, as in ANSI C. */
1175 if (c
>= '0' && c
<= '9')
1180 else if ((c
>= 'a' && c
<= 'f')
1181 || (c
>= 'A' && c
<= 'F'))
1184 if (c
>= 'a' && c
<= 'f')
1203 /* If the next token is ')' or ']' or '.', we store that character
1204 in *PCH and the return value is not interesting. Else, we store
1205 zero in *PCH and we read and return one lisp object.
1207 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1210 read1 (readcharfun
, pch
, first_in_list
)
1211 register Lisp_Object readcharfun
;
1216 int uninterned_symbol
= 0;
1223 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1228 return read_list (0, readcharfun
);
1231 return read_vector (readcharfun
);
1248 tmp
= read_vector (readcharfun
);
1249 if (XVECTOR (tmp
)->size
< CHAR_TABLE_STANDARD_SLOTS
1250 || XVECTOR (tmp
)->size
> CHAR_TABLE_STANDARD_SLOTS
+ 10)
1251 error ("Invalid size char-table");
1252 XSETCHAR_TABLE (tmp
, XCHAR_TABLE (tmp
));
1255 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#^", 2), Qnil
));
1260 length
= read1 (readcharfun
, pch
, first_in_list
);
1264 Lisp_Object tmp
, val
;
1265 int size_in_chars
= ((XFASTINT (length
) + BITS_PER_CHAR
)
1269 tmp
= read1 (readcharfun
, pch
, first_in_list
);
1270 if (size_in_chars
!= XSTRING (tmp
)->size
)
1271 Fsignal (Qinvalid_read_syntax
,
1272 Fcons (make_string ("#&...", 5), Qnil
));
1274 val
= Fmake_bool_vector (length
, Qnil
);
1275 bcopy (XSTRING (tmp
)->data
, XBOOL_VECTOR (val
)->data
,
1279 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#&...", 5),
1284 /* Accept compiled functions at read-time so that we don't have to
1285 build them using function calls. */
1287 tmp
= read_vector (readcharfun
);
1288 return Fmake_byte_code (XVECTOR (tmp
)->size
,
1289 XVECTOR (tmp
)->contents
);
1291 #ifdef USE_TEXT_PROPERTIES
1295 struct gcpro gcpro1
;
1298 /* Read the string itself. */
1299 tmp
= read1 (readcharfun
, &ch
, 0);
1300 if (ch
!= 0 || !STRINGP (tmp
))
1301 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1303 /* Read the intervals and their properties. */
1306 Lisp_Object beg
, end
, plist
;
1308 beg
= read1 (readcharfun
, &ch
, 0);
1312 end
= read1 (readcharfun
, &ch
, 0);
1314 plist
= read1 (readcharfun
, &ch
, 0);
1316 Fsignal (Qinvalid_read_syntax
,
1317 Fcons (build_string ("invalid string property list"),
1319 Fset_text_properties (beg
, end
, plist
, tmp
);
1325 /* #@NUMBER is used to skip NUMBER following characters.
1326 That's used in .elc files to skip over doc strings
1327 and function definitions. */
1332 /* Read a decimal integer. */
1333 while ((c
= READCHAR
) >= 0
1334 && c
>= '0' && c
<= '9')
1342 #ifndef DOS_NT /* I don't know if filepos works right on MSDOS and Windoze. */
1343 if (load_force_doc_strings
&& EQ (readcharfun
, Qget_file_char
))
1345 /* If we are supposed to force doc strings into core right now,
1346 record the last string that we skipped,
1347 and record where in the file it comes from. */
1348 if (saved_doc_string_size
== 0)
1350 saved_doc_string_size
= nskip
+ 100;
1351 saved_doc_string
= (char *) xmalloc (saved_doc_string_size
);
1353 if (nskip
> saved_doc_string_size
)
1355 saved_doc_string_size
= nskip
+ 100;
1356 saved_doc_string
= (char *) xrealloc (saved_doc_string
,
1357 saved_doc_string_size
);
1360 saved_doc_string_position
= ftell (instream
);
1362 /* Copy that many characters into saved_doc_string. */
1363 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1364 saved_doc_string
[i
] = c
= READCHAR
;
1366 saved_doc_string_length
= i
;
1369 #endif /* not DOS_NT */
1371 /* Skip that many characters. */
1372 for (i
= 0; i
< nskip
&& c
>= 0; i
++)
1378 return Vload_file_name
;
1380 return Fcons (Qfunction
, Fcons (read0 (readcharfun
), Qnil
));
1381 /* #:foo is the uninterned symbol named foo. */
1384 uninterned_symbol
= 1;
1388 /* Reader forms that can reuse previously read objects. */
1389 if (c
>= '0' && c
<= '9')
1394 /* Read a non-negative integer. */
1395 while (c
>= '0' && c
<= '9')
1401 /* #n=object returns object, but associates it with n for #n#. */
1404 tem
= read0 (readcharfun
);
1405 read_objects
= Fcons (Fcons (make_number (n
), tem
), read_objects
);
1408 /* #n# returns a previously read object. */
1411 tem
= Fassq (make_number (n
), read_objects
);
1414 /* Fall through to error message. */
1416 /* Fall through to error message. */
1420 Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("#", 1), Qnil
));
1423 while ((c
= READCHAR
) >= 0 && c
!= '\n');
1428 return Fcons (Qquote
, Fcons (read0 (readcharfun
), Qnil
));
1438 new_backquote_flag
= 1;
1439 value
= read0 (readcharfun
);
1440 new_backquote_flag
= 0;
1442 return Fcons (Qbackquote
, Fcons (value
, Qnil
));
1446 if (new_backquote_flag
)
1448 Lisp_Object comma_type
= Qnil
;
1453 comma_type
= Qcomma_at
;
1455 comma_type
= Qcomma_dot
;
1458 if (ch
>= 0) UNREAD (ch
);
1459 comma_type
= Qcomma
;
1462 new_backquote_flag
= 0;
1463 value
= read0 (readcharfun
);
1464 new_backquote_flag
= 1;
1465 return Fcons (comma_type
, Fcons (value
, Qnil
));
1472 register Lisp_Object val
;
1475 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1478 XSETINT (val
, read_escape (readcharfun
));
1487 register char *p
= read_buffer
;
1488 register char *end
= read_buffer
+ read_buffer_size
;
1492 while ((c
= READCHAR
) >= 0
1497 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
1498 p
+= new - read_buffer
;
1499 read_buffer
+= new - read_buffer
;
1500 end
= read_buffer
+ read_buffer_size
;
1503 c
= read_escape (readcharfun
);
1504 /* c is -1 if \ newline has just been seen */
1507 if (p
== read_buffer
)
1512 /* Allow `\C- ' and `\C-?'. */
1513 if (c
== (CHAR_CTL
| ' '))
1515 else if (c
== (CHAR_CTL
| '?'))
1519 /* Move the meta bit to the right place for a string. */
1520 c
= (c
& ~CHAR_META
) | 0x80;
1522 error ("Invalid modifier in string");
1526 if (c
< 0) return Fsignal (Qend_of_file
, Qnil
);
1528 /* If purifying, and string starts with \ newline,
1529 return zero instead. This is for doc strings
1530 that we are really going to find in etc/DOC.nn.nn */
1531 if (!NILP (Vpurify_flag
) && NILP (Vdoc_file_name
) && cancel
)
1532 return make_number (0);
1535 return make_pure_string (read_buffer
, p
- read_buffer
);
1537 return make_string (read_buffer
, p
- read_buffer
);
1542 #ifdef LISP_FLOAT_TYPE
1543 /* If a period is followed by a number, then we should read it
1544 as a floating point number. Otherwise, it denotes a dotted
1546 int next_char
= READCHAR
;
1549 if (! (next_char
>= '0' && next_char
<= '9'))
1556 /* Otherwise, we fall through! Note that the atom-reading loop
1557 below will now loop at least once, assuring that we will not
1558 try to UNREAD two characters in a row. */
1562 if (c
<= 040) goto retry
;
1564 register char *p
= read_buffer
;
1568 register char *end
= read_buffer
+ read_buffer_size
;
1571 !(c
== '\"' || c
== '\'' || c
== ';' || c
== '?'
1572 || c
== '(' || c
== ')'
1573 #ifndef LISP_FLOAT_TYPE
1574 /* If we have floating-point support, then we need
1575 to allow <digits><dot><digits>. */
1577 #endif /* not LISP_FLOAT_TYPE */
1578 || c
== '[' || c
== ']' || c
== '#'
1583 register char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
1584 p
+= new - read_buffer
;
1585 read_buffer
+= new - read_buffer
;
1586 end
= read_buffer
+ read_buffer_size
;
1599 char *new = (char *) xrealloc (read_buffer
, read_buffer_size
*= 2);
1600 p
+= new - read_buffer
;
1601 read_buffer
+= new - read_buffer
;
1602 /* end = read_buffer + read_buffer_size; */
1609 if (!quoted
&& !uninterned_symbol
)
1612 register Lisp_Object val
;
1614 if (*p1
== '+' || *p1
== '-') p1
++;
1615 /* Is it an integer? */
1618 while (p1
!= p
&& (c
= *p1
) >= '0' && c
<= '9') p1
++;
1619 #ifdef LISP_FLOAT_TYPE
1620 /* Integers can have trailing decimal points. */
1621 if (p1
> read_buffer
&& p1
< p
&& *p1
== '.') p1
++;
1624 /* It is an integer. */
1626 #ifdef LISP_FLOAT_TYPE
1630 if (sizeof (int) == sizeof (EMACS_INT
))
1631 XSETINT (val
, atoi (read_buffer
));
1632 else if (sizeof (long) == sizeof (EMACS_INT
))
1633 XSETINT (val
, atol (read_buffer
));
1639 #ifdef LISP_FLOAT_TYPE
1640 if (isfloat_string (read_buffer
))
1641 return make_float (atof (read_buffer
));
1645 if (uninterned_symbol
)
1646 return make_symbol (read_buffer
);
1648 return intern (read_buffer
);
1653 #ifdef LISP_FLOAT_TYPE
1668 if (*cp
== '+' || *cp
== '-')
1671 if (*cp
>= '0' && *cp
<= '9')
1674 while (*cp
>= '0' && *cp
<= '9')
1682 if (*cp
>= '0' && *cp
<= '9')
1685 while (*cp
>= '0' && *cp
<= '9')
1688 if (*cp
== 'e' || *cp
== 'E')
1692 if (*cp
== '+' || *cp
== '-')
1696 if (*cp
>= '0' && *cp
<= '9')
1699 while (*cp
>= '0' && *cp
<= '9')
1702 return (((*cp
== 0) || (*cp
== ' ') || (*cp
== '\t') || (*cp
== '\n') || (*cp
== '\r') || (*cp
== '\f'))
1703 && (state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
)
1704 || state
== (DOT_CHAR
|TRAIL_INT
)
1705 || state
== (LEAD_INT
|E_CHAR
|EXP_INT
)
1706 || state
== (LEAD_INT
|DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)
1707 || state
== (DOT_CHAR
|TRAIL_INT
|E_CHAR
|EXP_INT
)));
1709 #endif /* LISP_FLOAT_TYPE */
1712 read_vector (readcharfun
)
1713 Lisp_Object readcharfun
;
1717 register Lisp_Object
*ptr
;
1718 register Lisp_Object tem
, vector
;
1719 register struct Lisp_Cons
*otem
;
1722 tem
= read_list (1, readcharfun
);
1723 len
= Flength (tem
);
1724 vector
= (read_pure
? make_pure_vector (XINT (len
)) : Fmake_vector (len
, Qnil
));
1727 size
= XVECTOR (vector
)->size
;
1728 ptr
= XVECTOR (vector
)->contents
;
1729 for (i
= 0; i
< size
; i
++)
1731 ptr
[i
] = read_pure
? Fpurecopy (Fcar (tem
)) : Fcar (tem
);
1739 /* flag = 1 means check for ] to terminate rather than ) and .
1740 flag = -1 means check for starting with defun
1741 and make structure pure. */
1744 read_list (flag
, readcharfun
)
1746 register Lisp_Object readcharfun
;
1748 /* -1 means check next element for defun,
1749 0 means don't check,
1750 1 means already checked and found defun. */
1751 int defunflag
= flag
< 0 ? -1 : 0;
1752 Lisp_Object val
, tail
;
1753 register Lisp_Object elt
, tem
;
1754 struct gcpro gcpro1
, gcpro2
;
1755 /* 0 is the normal case.
1756 1 means this list is a doc reference; replace it with the number 0.
1757 2 means this list is a doc reference; replace it with the doc string. */
1758 int doc_reference
= 0;
1760 /* Initialize this to 1 if we are reading a list. */
1761 int first_in_list
= flag
<= 0;
1770 elt
= read1 (readcharfun
, &ch
, first_in_list
);
1775 /* While building, if the list starts with #$, treat it specially. */
1776 if (EQ (elt
, Vload_file_name
)
1777 && !NILP (Vpurify_flag
))
1779 if (NILP (Vdoc_file_name
))
1780 /* We have not yet called Snarf-documentation, so assume
1781 this file is described in the DOC-MM.NN file
1782 and Snarf-documentation will fill in the right value later.
1783 For now, replace the whole list with 0. */
1786 /* We have already called Snarf-documentation, so make a relative
1787 file name for this file, so it can be found properly
1788 in the installed Lisp directory.
1789 We don't use Fexpand_file_name because that would make
1790 the directory absolute now. */
1791 elt
= concat2 (build_string ("../lisp/"),
1792 Ffile_name_nondirectory (elt
));
1794 else if (EQ (elt
, Vload_file_name
)
1795 && load_force_doc_strings
)
1804 Fsignal (Qinvalid_read_syntax
,
1805 Fcons (make_string (") or . in a vector", 18), Qnil
));
1813 XCONS (tail
)->cdr
= read0 (readcharfun
);
1815 val
= read0 (readcharfun
);
1816 read1 (readcharfun
, &ch
, 0);
1820 if (doc_reference
== 1)
1821 return make_number (0);
1822 if (doc_reference
== 2)
1824 /* Get a doc string from the file we are loading.
1825 If it's in saved_doc_string, get it from there. */
1826 int pos
= XINT (XCONS (val
)->cdr
);
1827 if (pos
>= saved_doc_string_position
1828 && pos
< (saved_doc_string_position
1829 + saved_doc_string_length
))
1831 int start
= pos
- saved_doc_string_position
;
1834 /* Process quoting with ^A,
1835 and find the end of the string,
1836 which is marked with ^_ (037). */
1837 for (from
= start
, to
= start
;
1838 saved_doc_string
[from
] != 037;)
1840 int c
= saved_doc_string
[from
++];
1843 c
= saved_doc_string
[from
++];
1845 saved_doc_string
[to
++] = c
;
1847 saved_doc_string
[to
++] = 0;
1849 saved_doc_string
[to
++] = 037;
1852 saved_doc_string
[to
++] = c
;
1855 return make_string (saved_doc_string
+ start
,
1859 return read_doc_string (val
);
1864 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string (". in wrong context", 18), Qnil
));
1866 return Fsignal (Qinvalid_read_syntax
, Fcons (make_string ("] in a list", 11), Qnil
));
1868 tem
= (read_pure
&& flag
<= 0
1869 ? pure_cons (elt
, Qnil
)
1870 : Fcons (elt
, Qnil
));
1872 XCONS (tail
)->cdr
= tem
;
1877 defunflag
= EQ (elt
, Qdefun
);
1878 else if (defunflag
> 0)
1883 Lisp_Object Vobarray
;
1884 Lisp_Object initial_obarray
;
1886 /* oblookup stores the bucket number here, for the sake of Funintern. */
1888 int oblookup_last_bucket_number
;
1890 static int hash_string ();
1891 Lisp_Object
oblookup ();
1893 /* Get an error if OBARRAY is not an obarray.
1894 If it is one, return it. */
1897 check_obarray (obarray
)
1898 Lisp_Object obarray
;
1900 while (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
1902 /* If Vobarray is now invalid, force it to be valid. */
1903 if (EQ (Vobarray
, obarray
)) Vobarray
= initial_obarray
;
1905 obarray
= wrong_type_argument (Qvectorp
, obarray
);
1910 /* Intern the C string STR: return a symbol with that name,
1911 interned in the current obarray. */
1918 int len
= strlen (str
);
1919 Lisp_Object obarray
;
1922 if (!VECTORP (obarray
) || XVECTOR (obarray
)->size
== 0)
1923 obarray
= check_obarray (obarray
);
1924 tem
= oblookup (obarray
, str
, len
);
1927 return Fintern ((!NILP (Vpurify_flag
)
1928 ? make_pure_string (str
, len
)
1929 : make_string (str
, len
)),
1933 /* Create an uninterned symbol with name STR. */
1939 int len
= strlen (str
);
1941 return Fmake_symbol ((!NILP (Vpurify_flag
)
1942 ? make_pure_string (str
, len
)
1943 : make_string (str
, len
)));
1946 DEFUN ("intern", Fintern
, Sintern
, 1, 2, 0,
1947 "Return the canonical symbol whose name is STRING.\n\
1948 If there is none, one is created by this function and returned.\n\
1949 A second optional argument specifies the obarray to use;\n\
1950 it defaults to the value of `obarray'.")
1952 Lisp_Object string
, obarray
;
1954 register Lisp_Object tem
, sym
, *ptr
;
1956 if (NILP (obarray
)) obarray
= Vobarray
;
1957 obarray
= check_obarray (obarray
);
1959 CHECK_STRING (string
, 0);
1961 tem
= oblookup (obarray
, XSTRING (string
)->data
, XSTRING (string
)->size
);
1962 if (!INTEGERP (tem
))
1965 if (!NILP (Vpurify_flag
))
1966 string
= Fpurecopy (string
);
1967 sym
= Fmake_symbol (string
);
1968 XSYMBOL (sym
)->obarray
= obarray
;
1970 ptr
= &XVECTOR (obarray
)->contents
[XINT (tem
)];
1972 XSYMBOL (sym
)->next
= XSYMBOL (*ptr
);
1974 XSYMBOL (sym
)->next
= 0;
1979 DEFUN ("intern-soft", Fintern_soft
, Sintern_soft
, 1, 2, 0,
1980 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1981 A second optional argument specifies the obarray to use;\n\
1982 it defaults to the value of `obarray'.")
1984 Lisp_Object string
, obarray
;
1986 register Lisp_Object tem
;
1988 if (NILP (obarray
)) obarray
= Vobarray
;
1989 obarray
= check_obarray (obarray
);
1991 CHECK_STRING (string
, 0);
1993 tem
= oblookup (obarray
, XSTRING (string
)->data
, XSTRING (string
)->size
);
1994 if (!INTEGERP (tem
))
1999 DEFUN ("unintern", Funintern
, Sunintern
, 1, 2, 0,
2000 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2001 The value is t if a symbol was found and deleted, nil otherwise.\n\
2002 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2003 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2004 OBARRAY defaults to the value of the variable `obarray'.")
2006 Lisp_Object name
, obarray
;
2008 register Lisp_Object string
, tem
;
2011 if (NILP (obarray
)) obarray
= Vobarray
;
2012 obarray
= check_obarray (obarray
);
2015 XSETSTRING (string
, XSYMBOL (name
)->name
);
2018 CHECK_STRING (name
, 0);
2022 tem
= oblookup (obarray
, XSTRING (string
)->data
, XSTRING (string
)->size
);
2025 /* If arg was a symbol, don't delete anything but that symbol itself. */
2026 if (SYMBOLP (name
) && !EQ (name
, tem
))
2029 hash
= oblookup_last_bucket_number
;
2031 if (EQ (XVECTOR (obarray
)->contents
[hash
], tem
))
2033 if (XSYMBOL (tem
)->next
)
2034 XSETSYMBOL (XVECTOR (obarray
)->contents
[hash
], XSYMBOL (tem
)->next
);
2036 XSETINT (XVECTOR (obarray
)->contents
[hash
], 0);
2040 Lisp_Object tail
, following
;
2042 for (tail
= XVECTOR (obarray
)->contents
[hash
];
2043 XSYMBOL (tail
)->next
;
2046 XSETSYMBOL (following
, XSYMBOL (tail
)->next
);
2047 if (EQ (following
, tem
))
2049 XSYMBOL (tail
)->next
= XSYMBOL (following
)->next
;
2058 /* Return the symbol in OBARRAY whose names matches the string
2059 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
2062 Also store the bucket number in oblookup_last_bucket_number. */
2065 oblookup (obarray
, ptr
, size
)
2066 Lisp_Object obarray
;
2072 register Lisp_Object tail
;
2073 Lisp_Object bucket
, tem
;
2075 if (!VECTORP (obarray
)
2076 || (obsize
= XVECTOR (obarray
)->size
) == 0)
2078 obarray
= check_obarray (obarray
);
2079 obsize
= XVECTOR (obarray
)->size
;
2081 /* This is sometimes needed in the middle of GC. */
2082 obsize
&= ~ARRAY_MARK_FLAG
;
2083 /* Combining next two lines breaks VMS C 2.3. */
2084 hash
= hash_string (ptr
, size
);
2086 bucket
= XVECTOR (obarray
)->contents
[hash
];
2087 oblookup_last_bucket_number
= hash
;
2088 if (XFASTINT (bucket
) == 0)
2090 else if (!SYMBOLP (bucket
))
2091 error ("Bad data in guts of obarray"); /* Like CADR error message */
2093 for (tail
= bucket
; ; XSETSYMBOL (tail
, XSYMBOL (tail
)->next
))
2095 if (XSYMBOL (tail
)->name
->size
== size
2096 && !bcmp (XSYMBOL (tail
)->name
->data
, ptr
, size
))
2098 else if (XSYMBOL (tail
)->next
== 0)
2101 XSETINT (tem
, hash
);
2106 hash_string (ptr
, len
)
2110 register unsigned char *p
= ptr
;
2111 register unsigned char *end
= p
+ len
;
2112 register unsigned char c
;
2113 register int hash
= 0;
2118 if (c
>= 0140) c
-= 40;
2119 hash
= ((hash
<<3) + (hash
>>28) + c
);
2121 return hash
& 07777777777;
2125 map_obarray (obarray
, fn
, arg
)
2126 Lisp_Object obarray
;
2131 register Lisp_Object tail
;
2132 CHECK_VECTOR (obarray
, 1);
2133 for (i
= XVECTOR (obarray
)->size
- 1; i
>= 0; i
--)
2135 tail
= XVECTOR (obarray
)->contents
[i
];
2136 if (XFASTINT (tail
) != 0)
2140 if (XSYMBOL (tail
)->next
== 0)
2142 XSETSYMBOL (tail
, XSYMBOL (tail
)->next
);
2147 mapatoms_1 (sym
, function
)
2148 Lisp_Object sym
, function
;
2150 call1 (function
, sym
);
2153 DEFUN ("mapatoms", Fmapatoms
, Smapatoms
, 1, 2, 0,
2154 "Call FUNCTION on every symbol in OBARRAY.\n\
2155 OBARRAY defaults to the value of `obarray'.")
2157 Lisp_Object function
, obarray
;
2161 if (NILP (obarray
)) obarray
= Vobarray
;
2162 obarray
= check_obarray (obarray
);
2164 map_obarray (obarray
, mapatoms_1
, function
);
2168 #define OBARRAY_SIZE 1511
2173 Lisp_Object oblength
;
2177 XSETFASTINT (oblength
, OBARRAY_SIZE
);
2179 Qnil
= Fmake_symbol (make_pure_string ("nil", 3));
2180 Vobarray
= Fmake_vector (oblength
, make_number (0));
2181 initial_obarray
= Vobarray
;
2182 staticpro (&initial_obarray
);
2183 /* Intern nil in the obarray */
2184 XSYMBOL (Qnil
)->obarray
= Vobarray
;
2185 /* These locals are to kludge around a pyramid compiler bug. */
2186 hash
= hash_string ("nil", 3);
2187 /* Separate statement here to avoid VAXC bug. */
2188 hash
%= OBARRAY_SIZE
;
2189 tem
= &XVECTOR (Vobarray
)->contents
[hash
];
2192 Qunbound
= Fmake_symbol (make_pure_string ("unbound", 7));
2193 XSYMBOL (Qnil
)->function
= Qunbound
;
2194 XSYMBOL (Qunbound
)->value
= Qunbound
;
2195 XSYMBOL (Qunbound
)->function
= Qunbound
;
2198 XSYMBOL (Qnil
)->value
= Qnil
;
2199 XSYMBOL (Qnil
)->plist
= Qnil
;
2200 XSYMBOL (Qt
)->value
= Qt
;
2202 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2205 Qvariable_documentation
= intern ("variable-documentation");
2206 staticpro (&Qvariable_documentation
);
2208 read_buffer_size
= 100;
2209 read_buffer
= (char *) malloc (read_buffer_size
);
2214 struct Lisp_Subr
*sname
;
2217 sym
= intern (sname
->symbol_name
);
2218 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
2221 #ifdef NOTDEF /* use fset in subr.el now */
2223 defalias (sname
, string
)
2224 struct Lisp_Subr
*sname
;
2228 sym
= intern (string
);
2229 XSETSUBR (XSYMBOL (sym
)->function
, sname
);
2233 /* Define an "integer variable"; a symbol whose value is forwarded
2234 to a C variable of type int. Sample call: */
2235 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
2237 defvar_int (namestring
, address
)
2241 Lisp_Object sym
, val
;
2242 sym
= intern (namestring
);
2243 val
= allocate_misc ();
2244 XMISCTYPE (val
) = Lisp_Misc_Intfwd
;
2245 XINTFWD (val
)->intvar
= address
;
2246 XSYMBOL (sym
)->value
= val
;
2249 /* Similar but define a variable whose value is T if address contains 1,
2250 NIL if address contains 0 */
2252 defvar_bool (namestring
, address
)
2256 Lisp_Object sym
, val
;
2257 sym
= intern (namestring
);
2258 val
= allocate_misc ();
2259 XMISCTYPE (val
) = Lisp_Misc_Boolfwd
;
2260 XBOOLFWD (val
)->boolvar
= address
;
2261 XSYMBOL (sym
)->value
= val
;
2264 /* Similar but define a variable whose value is the Lisp Object stored
2265 at address. Two versions: with and without gc-marking of the C
2266 variable. The nopro version is used when that variable will be
2267 gc-marked for some other reason, since marking the same slot twice
2268 can cause trouble with strings. */
2270 defvar_lisp_nopro (namestring
, address
)
2272 Lisp_Object
*address
;
2274 Lisp_Object sym
, val
;
2275 sym
= intern (namestring
);
2276 val
= allocate_misc ();
2277 XMISCTYPE (val
) = Lisp_Misc_Objfwd
;
2278 XOBJFWD (val
)->objvar
= address
;
2279 XSYMBOL (sym
)->value
= val
;
2283 defvar_lisp (namestring
, address
)
2285 Lisp_Object
*address
;
2287 defvar_lisp_nopro (namestring
, address
);
2288 staticpro (address
);
2293 /* Similar but define a variable whose value is the Lisp Object stored in
2294 the current buffer. address is the address of the slot in the buffer
2295 that is current now. */
2298 defvar_per_buffer (namestring
, address
, type
, doc
)
2300 Lisp_Object
*address
;
2304 Lisp_Object sym
, val
;
2306 extern struct buffer buffer_local_symbols
;
2308 sym
= intern (namestring
);
2309 val
= allocate_misc ();
2310 offset
= (char *)address
- (char *)current_buffer
;
2312 XMISCTYPE (val
) = Lisp_Misc_Buffer_Objfwd
;
2313 XBUFFER_OBJFWD (val
)->offset
= offset
;
2314 XSYMBOL (sym
)->value
= val
;
2315 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_symbols
) = sym
;
2316 *(Lisp_Object
*)(offset
+ (char *)&buffer_local_types
) = type
;
2317 if (XINT (*(Lisp_Object
*)(offset
+ (char *)&buffer_local_flags
)) == 0)
2318 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
2319 slot of buffer_local_flags */
2323 #endif /* standalone */
2325 /* Similar but define a variable whose value is the Lisp Object stored
2326 at a particular offset in the current kboard object. */
2329 defvar_kboard (namestring
, offset
)
2333 Lisp_Object sym
, val
;
2334 sym
= intern (namestring
);
2335 val
= allocate_misc ();
2336 XMISCTYPE (val
) = Lisp_Misc_Kboard_Objfwd
;
2337 XKBOARD_OBJFWD (val
)->offset
= offset
;
2338 XSYMBOL (sym
)->value
= val
;
2341 /* Record the value of load-path used at the start of dumping
2342 so we can see if the site changed it later during dumping. */
2343 static Lisp_Object dump_path
;
2348 int turn_off_warning
= 0;
2350 #ifdef HAVE_SETLOCALE
2351 /* Make sure numbers are parsed as we expect. */
2352 setlocale (LC_NUMERIC
, "C");
2353 #endif /* HAVE_SETLOCALE */
2355 /* Compute the default load-path. */
2357 normal
= PATH_LOADSEARCH
;
2358 Vload_path
= decode_env_path (0, normal
);
2360 if (NILP (Vpurify_flag
))
2361 normal
= PATH_LOADSEARCH
;
2363 normal
= PATH_DUMPLOADSEARCH
;
2365 /* In a dumped Emacs, we normally have to reset the value of
2366 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2367 uses ../lisp, instead of the path of the installed elisp
2368 libraries. However, if it appears that Vload_path was changed
2369 from the default before dumping, don't override that value. */
2372 if (! NILP (Fequal (dump_path
, Vload_path
)))
2374 Vload_path
= decode_env_path (0, normal
);
2375 if (!NILP (Vinstallation_directory
))
2377 /* Add to the path the lisp subdir of the
2378 installation dir, if it exists. */
2379 Lisp_Object tem
, tem1
;
2380 tem
= Fexpand_file_name (build_string ("lisp"),
2381 Vinstallation_directory
);
2382 tem1
= Ffile_exists_p (tem
);
2385 if (NILP (Fmember (tem
, Vload_path
)))
2387 turn_off_warning
= 1;
2388 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
2392 /* That dir doesn't exist, so add the build-time
2393 Lisp dirs instead. */
2394 Vload_path
= nconc2 (Vload_path
, dump_path
);
2396 /* Add site-list under the installation dir, if it exists. */
2397 tem
= Fexpand_file_name (build_string ("site-lisp"),
2398 Vinstallation_directory
);
2399 tem1
= Ffile_exists_p (tem
);
2402 if (NILP (Fmember (tem
, Vload_path
)))
2403 Vload_path
= nconc2 (Vload_path
, Fcons (tem
, Qnil
));
2410 /* ../lisp refers to the build directory.
2411 NORMAL refers to the lisp dir in the source directory. */
2412 Vload_path
= Fcons (build_string ("../lisp"),
2413 decode_env_path (0, normal
));
2414 dump_path
= Vload_path
;
2419 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2420 almost never correct, thereby causing a warning to be printed out that
2421 confuses users. Since PATH_LOADSEARCH is always overridden by the
2422 EMACSLOADPATH environment variable below, disable the warning on NT. */
2424 /* Warn if dirs in the *standard* path don't exist. */
2425 if (!turn_off_warning
)
2427 Lisp_Object path_tail
;
2429 for (path_tail
= Vload_path
;
2431 path_tail
= XCONS (path_tail
)->cdr
)
2433 Lisp_Object dirfile
;
2434 dirfile
= Fcar (path_tail
);
2435 if (STRINGP (dirfile
))
2437 dirfile
= Fdirectory_file_name (dirfile
);
2438 if (access (XSTRING (dirfile
)->data
, 0) < 0)
2439 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
2440 XCONS (path_tail
)->car
);
2444 #endif /* WINDOWSNT */
2446 /* If the EMACSLOADPATH environment variable is set, use its value.
2447 This doesn't apply if we're dumping. */
2449 if (NILP (Vpurify_flag
)
2450 && egetenv ("EMACSLOADPATH"))
2452 Vload_path
= decode_env_path ("EMACSLOADPATH", normal
);
2456 load_in_progress
= 0;
2457 Vload_file_name
= Qnil
;
2459 load_descriptor_list
= Qnil
;
2462 /* Print a warning, using format string FORMAT, that directory DIRNAME
2463 does not exist. Print it on stderr and put it in *Message*. */
2465 dir_warning (format
, dirname
)
2467 Lisp_Object dirname
;
2470 = (char *) alloca (XSTRING (dirname
)->size
+ strlen (format
) + 5);
2472 fprintf (stderr
, format
, XSTRING (dirname
)->data
);
2473 sprintf (buffer
, format
, XSTRING (dirname
)->data
);
2474 message_dolog (buffer
, strlen (buffer
), 0);
2481 defsubr (&Sread_from_string
);
2483 defsubr (&Sintern_soft
);
2484 defsubr (&Sunintern
);
2486 defsubr (&Seval_buffer
);
2487 defsubr (&Seval_region
);
2488 defsubr (&Sread_char
);
2489 defsubr (&Sread_char_exclusive
);
2490 defsubr (&Sread_event
);
2491 defsubr (&Sget_file_char
);
2492 defsubr (&Smapatoms
);
2494 DEFVAR_LISP ("obarray", &Vobarray
,
2495 "Symbol table for use by `intern' and `read'.\n\
2496 It is a vector whose length ought to be prime for best results.\n\
2497 The vector's contents don't make sense if examined from Lisp programs;\n\
2498 to find all the symbols in an obarray, use `mapatoms'.");
2500 DEFVAR_LISP ("values", &Vvalues
,
2501 "List of values of all expressions which were read, evaluated and printed.\n\
2502 Order is reverse chronological.");
2504 DEFVAR_LISP ("standard-input", &Vstandard_input
,
2505 "Stream for read to get input from.\n\
2506 See documentation of `read' for possible values.");
2507 Vstandard_input
= Qt
;
2509 DEFVAR_LISP ("load-path", &Vload_path
,
2510 "*List of directories to search for files to load.\n\
2511 Each element is a string (directory name) or nil (try default directory).\n\
2512 Initialized based on EMACSLOADPATH environment variable, if any,\n\
2513 otherwise to default specified by file `paths.h' when Emacs was built.");
2515 DEFVAR_BOOL ("load-in-progress", &load_in_progress
,
2516 "Non-nil iff inside of `load'.");
2518 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist
,
2519 "An alist of expressions to be evalled when particular files are loaded.\n\
2520 Each element looks like (FILENAME FORMS...).\n\
2521 When `load' is run and the file-name argument is FILENAME,\n\
2522 the FORMS in the corresponding element are executed at the end of loading.\n\n\
2523 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2524 with no directory specified, since that is how `load' is normally called.\n\
2525 An error in FORMS does not undo the load,\n\
2526 but does prevent execution of the rest of the FORMS.");
2527 Vafter_load_alist
= Qnil
;
2529 DEFVAR_LISP ("load-history", &Vload_history
,
2530 "Alist mapping source file names to symbols and features.\n\
2531 Each alist element is a list that starts with a file name,\n\
2532 except for one element (optional) that starts with nil and describes\n\
2533 definitions evaluated from buffers not visiting files.\n\
2534 The remaining elements of each list are symbols defined as functions\n\
2535 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2536 Vload_history
= Qnil
;
2538 DEFVAR_LISP ("load-file-name", &Vload_file_name
,
2539 "Full name of file being loaded by `load'.");
2540 Vload_file_name
= Qnil
;
2542 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list
,
2543 "Used for internal purposes by `load'.");
2544 Vcurrent_load_list
= Qnil
;
2546 DEFVAR_LISP ("load-read-function", &Vload_read_function
,
2547 "Function used by `load' and `eval-region' for reading expressions.\n\
2548 The default is nil, which means use the function `read'.");
2549 Vload_read_function
= Qnil
;
2551 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings
,
2552 "Non-nil means `load' should force-load all dynamic doc strings.\n\
2553 This is useful when the file being loaded is a temporary copy.");
2554 load_force_doc_strings
= 0;
2556 DEFVAR_LISP ("source-directory", &Vsource_directory
,
2557 "Directory in which Emacs sources were found when Emacs was built.\n\
2558 You cannot count on them to still be there!");
2560 = Fexpand_file_name (build_string ("../"),
2561 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH
)));
2563 /* Vsource_directory was initialized in init_lread. */
2565 load_descriptor_list
= Qnil
;
2566 staticpro (&load_descriptor_list
);
2568 Qcurrent_load_list
= intern ("current-load-list");
2569 staticpro (&Qcurrent_load_list
);
2571 Qstandard_input
= intern ("standard-input");
2572 staticpro (&Qstandard_input
);
2574 Qread_char
= intern ("read-char");
2575 staticpro (&Qread_char
);
2577 Qget_file_char
= intern ("get-file-char");
2578 staticpro (&Qget_file_char
);
2580 Qbackquote
= intern ("`");
2581 staticpro (&Qbackquote
);
2582 Qcomma
= intern (",");
2583 staticpro (&Qcomma
);
2584 Qcomma_at
= intern (",@");
2585 staticpro (&Qcomma_at
);
2586 Qcomma_dot
= intern (",.");
2587 staticpro (&Qcomma_dot
);
2589 Qascii_character
= intern ("ascii-character");
2590 staticpro (&Qascii_character
);
2592 Qfunction
= intern ("function");
2593 staticpro (&Qfunction
);
2595 Qload
= intern ("load");
2598 Qload_file_name
= intern ("load-file-name");
2599 staticpro (&Qload_file_name
);
2601 staticpro (&dump_path
);
2603 staticpro (&read_objects
);
2604 read_objects
= Qnil
;