(tcl-add-fsf-menu): Use make-lucid-menu-keymap, not
[emacs.git] / src / lread.c
blob55a9df43e74f10c62498bf022d78e9c2767afbd6
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)
10 any later version.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/file.h>
27 #include <errno.h>
28 #include "lisp.h"
30 #ifndef standalone
31 #include "buffer.h"
32 #include <paths.h>
33 #include "commands.h"
34 #include "keyboard.h"
35 #include "termhooks.h"
36 #endif
38 #ifdef lint
39 #include <sys/inode.h>
40 #endif /* lint */
42 #ifndef X_OK
43 #define X_OK 01
44 #endif
46 #ifdef LISP_FLOAT_TYPE
47 #ifdef STDC_HEADERS
48 #include <stdlib.h>
49 #endif
51 #ifdef MSDOS
52 #include "msdos.h"
53 /* These are redefined (correctly, but differently) in values.h. */
54 #undef INTBITS
55 #undef LONGBITS
56 #undef SHORTBITS
57 #endif
59 #include <math.h>
60 #endif /* LISP_FLOAT_TYPE */
62 #ifndef O_RDONLY
63 #define O_RDONLY 0
64 #endif
66 extern int errno;
68 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
69 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
70 Lisp_Object Qascii_character, Qload, Qload_file_name;
71 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot;
73 extern Lisp_Object Qevent_symbol_element_mask;
75 /* non-zero if inside `load' */
76 int load_in_progress;
78 /* Search path for files to be loaded. */
79 Lisp_Object Vload_path;
81 /* This is the user-visible association list that maps features to
82 lists of defs in their load files. */
83 Lisp_Object Vload_history;
85 /* This is used to build the load history. */
86 Lisp_Object Vcurrent_load_list;
88 /* Name of file actually being read by `load'. */
89 Lisp_Object Vload_file_name;
91 /* Function to use for reading, in `load' and friends. */
92 Lisp_Object Vload_read_function;
94 /* List of descriptors now open for Fload. */
95 static Lisp_Object load_descriptor_list;
97 /* File for get_file_char to read from. Use by load */
98 static FILE *instream;
100 /* When nonzero, read conses in pure space */
101 static int read_pure;
103 /* For use within read-from-string (this reader is non-reentrant!!) */
104 static int read_from_string_index;
105 static int read_from_string_limit;
107 /* Nonzero means inside a new-style backquote
108 with no surrounding parentheses.
109 Fread initializes this to zero, so we need not specbind it
110 or worry about what happens to it when there is an error. */
111 static int new_backquote_flag;
113 /* Handle unreading and rereading of characters.
114 Write READCHAR to read a character,
115 UNREAD(c) to unread c to be read again. */
117 #define READCHAR readchar (readcharfun)
118 #define UNREAD(c) unreadchar (readcharfun, c)
120 static int
121 readchar (readcharfun)
122 Lisp_Object readcharfun;
124 Lisp_Object tem;
125 register struct buffer *inbuffer;
126 register int c, mpos;
128 if (BUFFERP (readcharfun))
130 inbuffer = XBUFFER (readcharfun);
132 if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer))
133 return -1;
134 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer));
135 SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1);
137 return c;
139 if (MARKERP (readcharfun))
141 inbuffer = XMARKER (readcharfun)->buffer;
143 mpos = marker_position (readcharfun);
145 if (mpos > BUF_ZV (inbuffer) - 1)
146 return -1;
147 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos);
148 if (mpos != BUF_GPT (inbuffer))
149 XMARKER (readcharfun)->bufpos++;
150 else
151 Fset_marker (readcharfun, make_number (mpos + 1),
152 Fmarker_buffer (readcharfun));
153 return c;
155 if (EQ (readcharfun, Qget_file_char))
157 c = getc (instream);
158 #ifdef EINTR
159 /* Interrupted reads have been observed while reading over the network */
160 while (c == EOF && ferror (instream) && errno == EINTR)
162 clearerr (instream);
163 c = getc (instream);
165 #endif
166 return c;
169 if (STRINGP (readcharfun))
171 register int c;
172 /* This used to be return of a conditional expression,
173 but that truncated -1 to a char on VMS. */
174 if (read_from_string_index < read_from_string_limit)
175 c = XSTRING (readcharfun)->data[read_from_string_index++];
176 else
177 c = -1;
178 return c;
181 tem = call0 (readcharfun);
183 if (NILP (tem))
184 return -1;
185 return XINT (tem);
188 /* Unread the character C in the way appropriate for the stream READCHARFUN.
189 If the stream is a user function, call it with the char as argument. */
191 static void
192 unreadchar (readcharfun, c)
193 Lisp_Object readcharfun;
194 int c;
196 if (c == -1)
197 /* Don't back up the pointer if we're unreading the end-of-input mark,
198 since readchar didn't advance it when we read it. */
200 else if (BUFFERP (readcharfun))
202 if (XBUFFER (readcharfun) == current_buffer)
203 SET_PT (point - 1);
204 else
205 SET_BUF_PT (XBUFFER (readcharfun), BUF_PT (XBUFFER (readcharfun)) - 1);
207 else if (MARKERP (readcharfun))
208 XMARKER (readcharfun)->bufpos--;
209 else if (STRINGP (readcharfun))
210 read_from_string_index--;
211 else if (EQ (readcharfun, Qget_file_char))
212 ungetc (c, instream);
213 else
214 call1 (readcharfun, make_number (c));
217 static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
219 /* get a character from the tty */
221 extern Lisp_Object read_char ();
223 /* Read input events until we get one that's acceptable for our purposes.
225 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
226 until we get a character we like, and then stuffed into
227 unread_switch_frame.
229 If ASCII_REQUIRED is non-zero, we check function key events to see
230 if the unmodified version of the symbol has a Qascii_character
231 property, and use that character, if present.
233 If ERROR_NONASCII is non-zero, we signal an error if the input we
234 get isn't an ASCII character with modifiers. If it's zero but
235 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
236 character. */
237 Lisp_Object
238 read_filtered_event (no_switch_frame, ascii_required, error_nonascii)
239 int no_switch_frame, ascii_required, error_nonascii;
241 #ifdef standalone
242 return make_number (getchar ());
243 #else
244 register Lisp_Object val, delayed_switch_frame;
246 delayed_switch_frame = Qnil;
248 /* Read until we get an acceptable event. */
249 retry:
250 val = read_char (0, 0, 0, Qnil, 0);
252 if (BUFFERP (val))
253 goto retry;
255 /* switch-frame events are put off until after the next ASCII
256 character. This is better than signalling an error just because
257 the last characters were typed to a separate minibuffer frame,
258 for example. Eventually, some code which can deal with
259 switch-frame events will read it and process it. */
260 if (no_switch_frame
261 && EVENT_HAS_PARAMETERS (val)
262 && EQ (EVENT_HEAD (val), Qswitch_frame))
264 delayed_switch_frame = val;
265 goto retry;
268 if (ascii_required)
270 /* Convert certain symbols to their ASCII equivalents. */
271 if (SYMBOLP (val))
273 Lisp_Object tem, tem1, tem2;
274 tem = Fget (val, Qevent_symbol_element_mask);
275 if (!NILP (tem))
277 tem1 = Fget (Fcar (tem), Qascii_character);
278 /* Merge this symbol's modifier bits
279 with the ASCII equivalent of its basic code. */
280 if (!NILP (tem1))
281 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
285 /* If we don't have a character now, deal with it appropriately. */
286 if (!INTEGERP (val))
288 if (error_nonascii)
290 Vunread_command_events = Fcons (val, Qnil);
291 error ("Non-character input-event");
293 else
294 goto retry;
298 if (! NILP (delayed_switch_frame))
299 unread_switch_frame = delayed_switch_frame;
301 return val;
302 #endif
305 DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
306 "Read a character from the command input (keyboard or macro).\n\
307 It is returned as a number.\n\
308 If the user generates an event which is not a character (i.e. a mouse\n\
309 click or function key event), `read-char' signals an error. As an\n\
310 exception, switch-frame events are put off until non-ASCII events can\n\
311 be read.\n\
312 If you want to read non-character events, or ignore them, call\n\
313 `read-event' or `read-char-exclusive' instead.")
316 return read_filtered_event (1, 1, 1);
319 DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0,
320 "Read an event object from the input stream.")
323 return read_filtered_event (0, 0, 0);
326 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0,
327 "Read a character from the command input (keyboard or macro).\n\
328 It is returned as a number. Non character events are ignored.")
331 return read_filtered_event (1, 1, 0);
334 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
335 "Don't use this yourself.")
338 register Lisp_Object val;
339 XSETINT (val, getc (instream));
340 return val;
343 static void readevalloop ();
344 static Lisp_Object load_unwind ();
345 static Lisp_Object load_descriptor_unwind ();
347 DEFUN ("load", Fload, Sload, 1, 4, 0,
348 "Execute a file of Lisp code named FILE.\n\
349 First try FILE with `.elc' appended, then try with `.el',\n\
350 then try FILE unmodified.\n\
351 This function searches the directories in `load-path'.\n\
352 If optional second arg NOERROR is non-nil,\n\
353 report no error if FILE doesn't exist.\n\
354 Print messages at start and end of loading unless\n\
355 optional third arg NOMESSAGE is non-nil.\n\
356 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
357 suffixes `.elc' or `.el' to the specified name FILE.\n\
358 Return t if file exists.")
359 (str, noerror, nomessage, nosuffix)
360 Lisp_Object str, noerror, nomessage, nosuffix;
362 register FILE *stream;
363 register int fd = -1;
364 register Lisp_Object lispstream;
365 int count = specpdl_ptr - specpdl;
366 Lisp_Object temp;
367 struct gcpro gcpro1;
368 Lisp_Object found;
369 /* 1 means inhibit the message at the beginning. */
370 int nomessage1 = 0;
371 Lisp_Object handler;
372 #ifdef DOS_NT
373 char *dosmode = "rt";
374 #endif /* DOS_NT */
376 CHECK_STRING (str, 0);
378 /* If file name is magic, call the handler. */
379 handler = Ffind_file_name_handler (str, Qload);
380 if (!NILP (handler))
381 return call5 (handler, Qload, str, noerror, nomessage, nosuffix);
383 /* Do this after the handler to avoid
384 the need to gcpro noerror, nomessage and nosuffix.
385 (Below here, we care only whether they are nil or not.) */
386 str = Fsubstitute_in_file_name (str);
388 /* Avoid weird lossage with null string as arg,
389 since it would try to load a directory as a Lisp file */
390 if (XSTRING (str)->size > 0)
392 GCPRO1 (str);
393 fd = openp (Vload_path, str, !NILP (nosuffix) ? "" : ".elc:.el:",
394 &found, 0);
395 UNGCPRO;
398 if (fd < 0)
400 if (NILP (noerror))
401 while (1)
402 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
403 Fcons (str, Qnil)));
404 else
405 return Qnil;
408 if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]),
409 ".elc", 4))
411 struct stat s1, s2;
412 int result;
414 #ifdef DOS_NT
415 dosmode = "rb";
416 #endif /* DOS_NT */
417 stat ((char *)XSTRING (found)->data, &s1);
418 XSTRING (found)->data[XSTRING (found)->size - 1] = 0;
419 result = stat ((char *)XSTRING (found)->data, &s2);
420 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
422 message ("Source file `%s' newer than byte-compiled file",
423 XSTRING (found)->data);
424 /* Don't immediately overwrite this message. */
425 if (!noninteractive)
426 nomessage1 = 1;
428 XSTRING (found)->data[XSTRING (found)->size - 1] = 'c';
431 #ifdef DOS_NT
432 close (fd);
433 stream = fopen ((char *) XSTRING (found)->data, dosmode);
434 #else /* not DOS_NT */
435 stream = fdopen (fd, "r");
436 #endif /* not DOS_NT */
437 if (stream == 0)
439 close (fd);
440 error ("Failure to create stdio stream for %s", XSTRING (str)->data);
443 if (NILP (nomessage) && !nomessage1)
444 message ("Loading %s...", XSTRING (str)->data);
446 GCPRO1 (str);
447 lispstream = Fcons (Qnil, Qnil);
448 XSETFASTINT (XCONS (lispstream)->car, (EMACS_UINT)stream >> 16);
449 XSETFASTINT (XCONS (lispstream)->cdr, (EMACS_UINT)stream & 0xffff);
450 record_unwind_protect (load_unwind, lispstream);
451 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
452 specbind (Qload_file_name, found);
453 load_descriptor_list
454 = Fcons (make_number (fileno (stream)), load_descriptor_list);
455 load_in_progress++;
456 readevalloop (Qget_file_char, stream, str, Feval, 0);
457 unbind_to (count, Qnil);
459 /* Run any load-hooks for this file. */
460 temp = Fassoc (str, Vafter_load_alist);
461 if (!NILP (temp))
462 Fprogn (Fcdr (temp));
463 UNGCPRO;
465 if (!noninteractive && NILP (nomessage))
466 message ("Loading %s...done", XSTRING (str)->data);
467 return Qt;
470 static Lisp_Object
471 load_unwind (stream) /* used as unwind-protect function in load */
472 Lisp_Object stream;
474 fclose ((FILE *) (XFASTINT (XCONS (stream)->car) << 16
475 | XFASTINT (XCONS (stream)->cdr)));
476 if (--load_in_progress < 0) load_in_progress = 0;
477 return Qnil;
480 static Lisp_Object
481 load_descriptor_unwind (oldlist)
482 Lisp_Object oldlist;
484 load_descriptor_list = oldlist;
485 return Qnil;
488 /* Close all descriptors in use for Floads.
489 This is used when starting a subprocess. */
491 void
492 close_load_descs ()
494 Lisp_Object tail;
495 for (tail = load_descriptor_list; !NILP (tail); tail = XCONS (tail)->cdr)
496 close (XFASTINT (XCONS (tail)->car));
499 static int
500 complete_filename_p (pathname)
501 Lisp_Object pathname;
503 register unsigned char *s = XSTRING (pathname)->data;
504 return (IS_DIRECTORY_SEP (s[0])
505 || (XSTRING (pathname)->size > 2
506 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
507 #ifdef ALTOS
508 || *s == '@'
509 #endif
510 #ifdef VMS
511 || index (s, ':')
512 #endif /* VMS */
516 /* Search for a file whose name is STR, looking in directories
517 in the Lisp list PATH, and trying suffixes from SUFFIX.
518 SUFFIX is a string containing possible suffixes separated by colons.
519 On success, returns a file descriptor. On failure, returns -1.
521 EXEC_ONLY nonzero means don't open the files,
522 just look for one that is executable. In this case,
523 returns 1 on success.
525 If STOREPTR is nonzero, it points to a slot where the name of
526 the file actually found should be stored as a Lisp string.
527 Nil is stored there on failure. */
530 openp (path, str, suffix, storeptr, exec_only)
531 Lisp_Object path, str;
532 char *suffix;
533 Lisp_Object *storeptr;
534 int exec_only;
536 register int fd;
537 int fn_size = 100;
538 char buf[100];
539 register char *fn = buf;
540 int absolute = 0;
541 int want_size;
542 register Lisp_Object filename;
543 struct stat st;
544 struct gcpro gcpro1;
546 GCPRO1 (str);
547 if (storeptr)
548 *storeptr = Qnil;
550 if (complete_filename_p (str))
551 absolute = 1;
553 for (; !NILP (path); path = Fcdr (path))
555 char *nsuffix;
557 filename = Fexpand_file_name (str, Fcar (path));
558 if (!complete_filename_p (filename))
559 /* If there are non-absolute elts in PATH (eg ".") */
560 /* Of course, this could conceivably lose if luser sets
561 default-directory to be something non-absolute... */
563 filename = Fexpand_file_name (filename, current_buffer->directory);
564 if (!complete_filename_p (filename))
565 /* Give up on this path element! */
566 continue;
569 /* Calculate maximum size of any filename made from
570 this path element/specified file name and any possible suffix. */
571 want_size = strlen (suffix) + XSTRING (filename)->size + 1;
572 if (fn_size < want_size)
573 fn = (char *) alloca (fn_size = 100 + want_size);
575 nsuffix = suffix;
577 /* Loop over suffixes. */
578 while (1)
580 char *esuffix = (char *) index (nsuffix, ':');
581 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
583 /* Concatenate path element/specified name with the suffix. */
584 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
585 fn[XSTRING (filename)->size] = 0;
586 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
587 strncat (fn, nsuffix, lsuffix);
589 /* Ignore file if it's a directory. */
590 if (stat (fn, &st) >= 0
591 && (st.st_mode & S_IFMT) != S_IFDIR)
593 /* Check that we can access or open it. */
594 if (exec_only)
595 fd = (access (fn, X_OK) == 0) ? 1 : -1;
596 else
597 fd = open (fn, O_RDONLY, 0);
599 if (fd >= 0)
601 /* We succeeded; return this descriptor and filename. */
602 if (storeptr)
603 *storeptr = build_string (fn);
604 UNGCPRO;
605 return fd;
609 /* Advance to next suffix. */
610 if (esuffix == 0)
611 break;
612 nsuffix += lsuffix + 1;
614 if (absolute)
615 break;
618 UNGCPRO;
619 return -1;
623 /* Merge the list we've accumulated of globals from the current input source
624 into the load_history variable. The details depend on whether
625 the source has an associated file name or not. */
627 static void
628 build_load_history (stream, source)
629 FILE *stream;
630 Lisp_Object source;
632 register Lisp_Object tail, prev, newelt;
633 register Lisp_Object tem, tem2;
634 register int foundit, loading;
636 /* Don't bother recording anything for preloaded files. */
637 if (!NILP (Vpurify_flag))
638 return;
640 loading = stream || !NARROWED;
642 tail = Vload_history;
643 prev = Qnil;
644 foundit = 0;
645 while (!NILP (tail))
647 tem = Fcar (tail);
649 /* Find the feature's previous assoc list... */
650 if (!NILP (Fequal (source, Fcar (tem))))
652 foundit = 1;
654 /* If we're loading, remove it. */
655 if (loading)
657 if (NILP (prev))
658 Vload_history = Fcdr (tail);
659 else
660 Fsetcdr (prev, Fcdr (tail));
663 /* Otherwise, cons on new symbols that are not already members. */
664 else
666 tem2 = Vcurrent_load_list;
668 while (CONSP (tem2))
670 newelt = Fcar (tem2);
672 if (NILP (Fmemq (newelt, tem)))
673 Fsetcar (tail, Fcons (Fcar (tem),
674 Fcons (newelt, Fcdr (tem))));
676 tem2 = Fcdr (tem2);
677 QUIT;
681 else
682 prev = tail;
683 tail = Fcdr (tail);
684 QUIT;
687 /* If we're loading, cons the new assoc onto the front of load-history,
688 the most-recently-loaded position. Also do this if we didn't find
689 an existing member for the current source. */
690 if (loading || !foundit)
691 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
692 Vload_history);
695 Lisp_Object
696 unreadpure () /* Used as unwind-protect function in readevalloop */
698 read_pure = 0;
699 return Qnil;
702 static void
703 readevalloop (readcharfun, stream, sourcename, evalfun, printflag)
704 Lisp_Object readcharfun;
705 FILE *stream;
706 Lisp_Object sourcename;
707 Lisp_Object (*evalfun) ();
708 int printflag;
710 register int c;
711 register Lisp_Object val;
712 int count = specpdl_ptr - specpdl;
713 struct gcpro gcpro1;
714 struct buffer *b = 0;
716 if (BUFFERP (readcharfun))
717 b = XBUFFER (readcharfun);
718 else if (MARKERP (readcharfun))
719 b = XMARKER (readcharfun)->buffer;
721 specbind (Qstandard_input, readcharfun);
722 specbind (Qcurrent_load_list, Qnil);
724 GCPRO1 (sourcename);
726 LOADHIST_ATTACH (sourcename);
728 while (1)
730 if (b != 0 && NILP (b->name))
731 error ("Reading from killed buffer");
733 instream = stream;
734 c = READCHAR;
735 if (c == ';')
737 while ((c = READCHAR) != '\n' && c != -1);
738 continue;
740 if (c < 0) break;
742 /* Ignore whitespace here, so we can detect eof. */
743 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
744 continue;
746 if (!NILP (Vpurify_flag) && c == '(')
748 int count1 = specpdl_ptr - specpdl;
749 record_unwind_protect (unreadpure, Qnil);
750 val = read_list (-1, readcharfun);
751 unbind_to (count1, Qnil);
753 else
755 UNREAD (c);
756 if (NILP (Vload_read_function))
757 val = read0 (readcharfun);
758 else
759 val = call1 (Vload_read_function, readcharfun);
762 val = (*evalfun) (val);
763 if (printflag)
765 Vvalues = Fcons (val, Vvalues);
766 if (EQ (Vstandard_output, Qt))
767 Fprin1 (val, Qnil);
768 else
769 Fprint (val, Qnil);
773 build_load_history (stream, sourcename);
774 UNGCPRO;
776 unbind_to (count, Qnil);
779 #ifndef standalone
781 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 2, "",
782 "Execute the current buffer as Lisp code.\n\
783 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
784 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
785 PRINTFLAG controls printing of output:\n\
786 nil means discard it; anything else is stream for print.\n\
788 If there is no error, point does not move. If there is an error,\n\
789 point remains at the end of the last character read from the buffer.")
790 (bufname, printflag)
791 Lisp_Object bufname, printflag;
793 int count = specpdl_ptr - specpdl;
794 Lisp_Object tem, buf;
796 if (NILP (bufname))
797 buf = Fcurrent_buffer ();
798 else
799 buf = Fget_buffer (bufname);
800 if (NILP (buf))
801 error ("No such buffer.");
803 if (NILP (printflag))
804 tem = Qsymbolp;
805 else
806 tem = printflag;
807 specbind (Qstandard_output, tem);
808 record_unwind_protect (save_excursion_restore, save_excursion_save ());
809 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
810 readevalloop (buf, 0, XBUFFER (buf)->filename, Feval, !NILP (printflag));
811 unbind_to (count, Qnil);
813 return Qnil;
816 #if 0
817 DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
818 "Execute the current buffer as Lisp code.\n\
819 Programs can pass argument PRINTFLAG which controls printing of output:\n\
820 nil means discard it; anything else is stream for print.\n\
822 If there is no error, point does not move. If there is an error,\n\
823 point remains at the end of the last character read from the buffer.")
824 (printflag)
825 Lisp_Object printflag;
827 int count = specpdl_ptr - specpdl;
828 Lisp_Object tem, cbuf;
830 cbuf = Fcurrent_buffer ()
832 if (NILP (printflag))
833 tem = Qsymbolp;
834 else
835 tem = printflag;
836 specbind (Qstandard_output, tem);
837 record_unwind_protect (save_excursion_restore, save_excursion_save ());
838 SET_PT (BEGV);
839 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
840 return unbind_to (count, Qnil);
842 #endif
844 DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
845 "Execute the region as Lisp code.\n\
846 When called from programs, expects two arguments,\n\
847 giving starting and ending indices in the current buffer\n\
848 of the text to be executed.\n\
849 Programs can pass third argument PRINTFLAG which controls output:\n\
850 nil means discard it; anything else is stream for printing it.\n\
852 If there is no error, point does not move. If there is an error,\n\
853 point remains at the end of the last character read from the buffer.")
854 (b, e, printflag)
855 Lisp_Object b, e, printflag;
857 int count = specpdl_ptr - specpdl;
858 Lisp_Object tem, cbuf;
860 cbuf = Fcurrent_buffer ();
862 if (NILP (printflag))
863 tem = Qsymbolp;
864 else
865 tem = printflag;
866 specbind (Qstandard_output, tem);
868 if (NILP (printflag))
869 record_unwind_protect (save_excursion_restore, save_excursion_save ());
870 record_unwind_protect (save_restriction_restore, save_restriction_save ());
872 /* This both uses b and checks its type. */
873 Fgoto_char (b);
874 Fnarrow_to_region (make_number (BEGV), e);
875 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
877 return unbind_to (count, Qnil);
880 #endif /* standalone */
882 DEFUN ("read", Fread, Sread, 0, 1, 0,
883 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
884 If STREAM is nil, use the value of `standard-input' (which see).\n\
885 STREAM or the value of `standard-input' may be:\n\
886 a buffer (read from point and advance it)\n\
887 a marker (read from where it points and advance it)\n\
888 a function (call it with no arguments for each character,\n\
889 call it with a char as argument to push a char back)\n\
890 a string (takes text from string, starting at the beginning)\n\
891 t (read text line using minibuffer and use it).")
892 (readcharfun)
893 Lisp_Object readcharfun;
895 extern Lisp_Object Fread_minibuffer ();
897 if (NILP (readcharfun))
898 readcharfun = Vstandard_input;
899 if (EQ (readcharfun, Qt))
900 readcharfun = Qread_char;
902 new_backquote_flag = 0;
904 #ifndef standalone
905 if (EQ (readcharfun, Qread_char))
906 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
907 #endif
909 if (STRINGP (readcharfun))
910 return Fcar (Fread_from_string (readcharfun, Qnil, Qnil));
912 return read0 (readcharfun);
915 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
916 "Read one Lisp expression which is represented as text by STRING.\n\
917 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
918 START and END optionally delimit a substring of STRING from which to read;\n\
919 they default to 0 and (length STRING) respectively.")
920 (string, start, end)
921 Lisp_Object string, start, end;
923 int startval, endval;
924 Lisp_Object tem;
926 CHECK_STRING (string,0);
928 if (NILP (end))
929 endval = XSTRING (string)->size;
930 else
931 { CHECK_NUMBER (end,2);
932 endval = XINT (end);
933 if (endval < 0 || endval > XSTRING (string)->size)
934 args_out_of_range (string, end);
937 if (NILP (start))
938 startval = 0;
939 else
940 { CHECK_NUMBER (start,1);
941 startval = XINT (start);
942 if (startval < 0 || startval > endval)
943 args_out_of_range (string, start);
946 read_from_string_index = startval;
947 read_from_string_limit = endval;
949 new_backquote_flag = 0;
951 tem = read0 (string);
952 return Fcons (tem, make_number (read_from_string_index));
955 /* Use this for recursive reads, in contexts where internal tokens
956 are not allowed. */
957 static Lisp_Object
958 read0 (readcharfun)
959 Lisp_Object readcharfun;
961 register Lisp_Object val;
962 char c;
964 val = read1 (readcharfun, &c, 0);
965 if (c)
966 Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
968 return val;
971 static int read_buffer_size;
972 static char *read_buffer;
974 static int
975 read_escape (readcharfun)
976 Lisp_Object readcharfun;
978 register int c = READCHAR;
979 switch (c)
981 case 'a':
982 return '\007';
983 case 'b':
984 return '\b';
985 case 'd':
986 return 0177;
987 case 'e':
988 return 033;
989 case 'f':
990 return '\f';
991 case 'n':
992 return '\n';
993 case 'r':
994 return '\r';
995 case 't':
996 return '\t';
997 case 'v':
998 return '\v';
999 case '\n':
1000 return -1;
1002 case 'M':
1003 c = READCHAR;
1004 if (c != '-')
1005 error ("Invalid escape character syntax");
1006 c = READCHAR;
1007 if (c == '\\')
1008 c = read_escape (readcharfun);
1009 return c | meta_modifier;
1011 case 'S':
1012 c = READCHAR;
1013 if (c != '-')
1014 error ("Invalid escape character syntax");
1015 c = READCHAR;
1016 if (c == '\\')
1017 c = read_escape (readcharfun);
1018 return c | shift_modifier;
1020 case 'H':
1021 c = READCHAR;
1022 if (c != '-')
1023 error ("Invalid escape character syntax");
1024 c = READCHAR;
1025 if (c == '\\')
1026 c = read_escape (readcharfun);
1027 return c | hyper_modifier;
1029 case 'A':
1030 c = READCHAR;
1031 if (c != '-')
1032 error ("Invalid escape character syntax");
1033 c = READCHAR;
1034 if (c == '\\')
1035 c = read_escape (readcharfun);
1036 return c | alt_modifier;
1038 case 's':
1039 c = READCHAR;
1040 if (c != '-')
1041 error ("Invalid escape character syntax");
1042 c = READCHAR;
1043 if (c == '\\')
1044 c = read_escape (readcharfun);
1045 return c | super_modifier;
1047 case 'C':
1048 c = READCHAR;
1049 if (c != '-')
1050 error ("Invalid escape character syntax");
1051 case '^':
1052 c = READCHAR;
1053 if (c == '\\')
1054 c = read_escape (readcharfun);
1055 if ((c & 0177) == '?')
1056 return 0177 | c;
1057 /* ASCII control chars are made from letters (both cases),
1058 as well as the non-letters within 0100...0137. */
1059 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1060 return (c & (037 | ~0177));
1061 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1062 return (c & (037 | ~0177));
1063 else
1064 return c | ctrl_modifier;
1066 case '0':
1067 case '1':
1068 case '2':
1069 case '3':
1070 case '4':
1071 case '5':
1072 case '6':
1073 case '7':
1074 /* An octal escape, as in ANSI C. */
1076 register int i = c - '0';
1077 register int count = 0;
1078 while (++count < 3)
1080 if ((c = READCHAR) >= '0' && c <= '7')
1082 i *= 8;
1083 i += c - '0';
1085 else
1087 UNREAD (c);
1088 break;
1091 return i;
1094 case 'x':
1095 /* A hex escape, as in ANSI C. */
1097 int i = 0;
1098 while (1)
1100 c = READCHAR;
1101 if (c >= '0' && c <= '9')
1103 i *= 16;
1104 i += c - '0';
1106 else if ((c >= 'a' && c <= 'f')
1107 || (c >= 'A' && c <= 'F'))
1109 i *= 16;
1110 if (c >= 'a' && c <= 'f')
1111 i += c - 'a' + 10;
1112 else
1113 i += c - 'A' + 10;
1115 else
1117 UNREAD (c);
1118 break;
1121 return i;
1124 default:
1125 return c;
1129 /* If the next token is ')' or ']' or '.', we store that character
1130 in *PCH and the return value is not interesting. Else, we store
1131 zero in *PCH and we read and return one lisp object.
1133 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1135 static Lisp_Object
1136 read1 (readcharfun, pch, first_in_list)
1137 register Lisp_Object readcharfun;
1138 char *pch;
1139 int first_in_list;
1141 register int c;
1142 *pch = 0;
1144 retry:
1146 c = READCHAR;
1147 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1149 switch (c)
1151 case '(':
1152 return read_list (0, readcharfun);
1154 case '[':
1155 return read_vector (readcharfun);
1157 case ')':
1158 case ']':
1160 *pch = c;
1161 return Qnil;
1164 case '#':
1165 c = READCHAR;
1166 if (c == '[')
1168 /* Accept compiled functions at read-time so that we don't have to
1169 build them using function calls. */
1170 Lisp_Object tmp;
1171 tmp = read_vector (readcharfun);
1172 return Fmake_byte_code (XVECTOR (tmp)->size,
1173 XVECTOR (tmp)->contents);
1175 #ifdef USE_TEXT_PROPERTIES
1176 if (c == '(')
1178 Lisp_Object tmp;
1179 struct gcpro gcpro1;
1180 char ch;
1182 /* Read the string itself. */
1183 tmp = read1 (readcharfun, &ch, 0);
1184 if (ch != 0 || !STRINGP (tmp))
1185 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1186 GCPRO1 (tmp);
1187 /* Read the intervals and their properties. */
1188 while (1)
1190 Lisp_Object beg, end, plist;
1192 beg = read1 (readcharfun, &ch, 0);
1193 if (ch == ')')
1194 break;
1195 if (ch == 0)
1196 end = read1 (readcharfun, &ch, 0);
1197 if (ch == 0)
1198 plist = read1 (readcharfun, &ch, 0);
1199 if (ch)
1200 Fsignal (Qinvalid_read_syntax,
1201 Fcons (build_string ("invalid string property list"),
1202 Qnil));
1203 Fset_text_properties (beg, end, plist, tmp);
1205 UNGCPRO;
1206 return tmp;
1208 #endif
1209 /* #@NUMBER is used to skip NUMBER following characters.
1210 That's used in .elc files to skip over doc strings
1211 and function definitions. */
1212 if (c == '@')
1214 int i, nskip = 0;
1216 /* Read a decimal integer. */
1217 while ((c = READCHAR) >= 0
1218 && c >= '0' && c <= '9')
1220 nskip *= 10;
1221 nskip += c - '0';
1223 if (c >= 0)
1224 UNREAD (c);
1226 /* Skip that many characters. */
1227 for (i = 0; i < nskip && c >= 0; i++)
1228 c = READCHAR;
1229 goto retry;
1231 if (c == '$')
1232 return Vload_file_name;
1234 UNREAD (c);
1235 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1237 case ';':
1238 while ((c = READCHAR) >= 0 && c != '\n');
1239 goto retry;
1241 case '\'':
1243 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1246 case '`':
1247 if (first_in_list)
1248 goto default_label;
1249 else
1251 Lisp_Object value;
1253 new_backquote_flag = 1;
1254 value = read0 (readcharfun);
1255 new_backquote_flag = 0;
1257 return Fcons (Qbackquote, Fcons (value, Qnil));
1260 case ',':
1261 if (new_backquote_flag)
1263 Lisp_Object comma_type = Qnil;
1264 Lisp_Object value;
1265 int ch = READCHAR;
1267 if (ch == '@')
1268 comma_type = Qcomma_at;
1269 else if (ch == '.')
1270 comma_type = Qcomma_dot;
1271 else
1273 if (ch >= 0) UNREAD (ch);
1274 comma_type = Qcomma;
1277 new_backquote_flag = 0;
1278 value = read0 (readcharfun);
1279 new_backquote_flag = 1;
1280 return Fcons (comma_type, Fcons (value, Qnil));
1282 else
1283 goto default_label;
1285 case '?':
1287 register Lisp_Object val;
1289 c = READCHAR;
1290 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1292 if (c == '\\')
1293 XSETINT (val, read_escape (readcharfun));
1294 else
1295 XSETINT (val, c);
1297 return val;
1300 case '\"':
1302 register char *p = read_buffer;
1303 register char *end = read_buffer + read_buffer_size;
1304 register int c;
1305 int cancel = 0;
1307 while ((c = READCHAR) >= 0
1308 && c != '\"')
1310 if (p == end)
1312 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1313 p += new - read_buffer;
1314 read_buffer += new - read_buffer;
1315 end = read_buffer + read_buffer_size;
1317 if (c == '\\')
1318 c = read_escape (readcharfun);
1319 /* c is -1 if \ newline has just been seen */
1320 if (c == -1)
1322 if (p == read_buffer)
1323 cancel = 1;
1325 else
1327 /* Allow `\C- ' and `\C-?'. */
1328 if (c == (CHAR_CTL | ' '))
1329 c = 0;
1330 else if (c == (CHAR_CTL | '?'))
1331 c = 127;
1333 if (c & CHAR_META)
1334 /* Move the meta bit to the right place for a string. */
1335 c = (c & ~CHAR_META) | 0x80;
1336 if (c & ~0xff)
1337 error ("Invalid modifier in string");
1338 *p++ = c;
1341 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1343 /* If purifying, and string starts with \ newline,
1344 return zero instead. This is for doc strings
1345 that we are really going to find in etc/DOC.nn.nn */
1346 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
1347 return make_number (0);
1349 if (read_pure)
1350 return make_pure_string (read_buffer, p - read_buffer);
1351 else
1352 return make_string (read_buffer, p - read_buffer);
1355 case '.':
1357 #ifdef LISP_FLOAT_TYPE
1358 /* If a period is followed by a number, then we should read it
1359 as a floating point number. Otherwise, it denotes a dotted
1360 pair. */
1361 int next_char = READCHAR;
1362 UNREAD (next_char);
1364 if (! (next_char >= '0' && next_char <= '9'))
1365 #endif
1367 *pch = c;
1368 return Qnil;
1371 /* Otherwise, we fall through! Note that the atom-reading loop
1372 below will now loop at least once, assuring that we will not
1373 try to UNREAD two characters in a row. */
1375 default:
1376 default_label:
1377 if (c <= 040) goto retry;
1379 register char *p = read_buffer;
1380 int quoted = 0;
1383 register char *end = read_buffer + read_buffer_size;
1385 while (c > 040 &&
1386 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1387 || c == '(' || c == ')'
1388 #ifndef LISP_FLOAT_TYPE
1389 /* If we have floating-point support, then we need
1390 to allow <digits><dot><digits>. */
1391 || c =='.'
1392 #endif /* not LISP_FLOAT_TYPE */
1393 || c == '[' || c == ']' || c == '#'
1396 if (p == end)
1398 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1399 p += new - read_buffer;
1400 read_buffer += new - read_buffer;
1401 end = read_buffer + read_buffer_size;
1403 if (c == '\\')
1405 c = READCHAR;
1406 quoted = 1;
1408 *p++ = c;
1409 c = READCHAR;
1412 if (p == end)
1414 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1415 p += new - read_buffer;
1416 read_buffer += new - read_buffer;
1417 /* end = read_buffer + read_buffer_size; */
1419 *p = 0;
1420 if (c >= 0)
1421 UNREAD (c);
1424 if (!quoted)
1426 register char *p1;
1427 register Lisp_Object val;
1428 p1 = read_buffer;
1429 if (*p1 == '+' || *p1 == '-') p1++;
1430 /* Is it an integer? */
1431 if (p1 != p)
1433 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
1434 #ifdef LISP_FLOAT_TYPE
1435 /* Integers can have trailing decimal points. */
1436 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
1437 #endif
1438 if (p1 == p)
1439 /* It is an integer. */
1441 #ifdef LISP_FLOAT_TYPE
1442 if (p1[-1] == '.')
1443 p1[-1] = '\0';
1444 #endif
1445 if (sizeof (int) == sizeof (EMACS_INT))
1446 XSETINT (val, atoi (read_buffer));
1447 else if (sizeof (long) == sizeof (EMACS_INT))
1448 XSETINT (val, atol (read_buffer));
1449 else
1450 abort ();
1451 return val;
1454 #ifdef LISP_FLOAT_TYPE
1455 if (isfloat_string (read_buffer))
1456 return make_float (atof (read_buffer));
1457 #endif
1460 return intern (read_buffer);
1465 #ifdef LISP_FLOAT_TYPE
1467 #define LEAD_INT 1
1468 #define DOT_CHAR 2
1469 #define TRAIL_INT 4
1470 #define E_CHAR 8
1471 #define EXP_INT 16
1474 isfloat_string (cp)
1475 register char *cp;
1477 register state;
1479 state = 0;
1480 if (*cp == '+' || *cp == '-')
1481 cp++;
1483 if (*cp >= '0' && *cp <= '9')
1485 state |= LEAD_INT;
1486 while (*cp >= '0' && *cp <= '9')
1487 cp++;
1489 if (*cp == '.')
1491 state |= DOT_CHAR;
1492 cp++;
1494 if (*cp >= '0' && *cp <= '9')
1496 state |= TRAIL_INT;
1497 while (*cp >= '0' && *cp <= '9')
1498 cp++;
1500 if (*cp == 'e')
1502 state |= E_CHAR;
1503 cp++;
1504 if (*cp == '+' || *cp == '-')
1505 cp++;
1508 if (*cp >= '0' && *cp <= '9')
1510 state |= EXP_INT;
1511 while (*cp >= '0' && *cp <= '9')
1512 cp++;
1514 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
1515 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
1516 || state == (DOT_CHAR|TRAIL_INT)
1517 || state == (LEAD_INT|E_CHAR|EXP_INT)
1518 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1519 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
1521 #endif /* LISP_FLOAT_TYPE */
1523 static Lisp_Object
1524 read_vector (readcharfun)
1525 Lisp_Object readcharfun;
1527 register int i;
1528 register int size;
1529 register Lisp_Object *ptr;
1530 register Lisp_Object tem, vector;
1531 register struct Lisp_Cons *otem;
1532 Lisp_Object len;
1534 tem = read_list (1, readcharfun);
1535 len = Flength (tem);
1536 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1539 size = XVECTOR (vector)->size;
1540 ptr = XVECTOR (vector)->contents;
1541 for (i = 0; i < size; i++)
1543 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1544 otem = XCONS (tem);
1545 tem = Fcdr (tem);
1546 free_cons (otem);
1548 return vector;
1551 /* flag = 1 means check for ] to terminate rather than ) and .
1552 flag = -1 means check for starting with defun
1553 and make structure pure. */
1555 static Lisp_Object
1556 read_list (flag, readcharfun)
1557 int flag;
1558 register Lisp_Object readcharfun;
1560 /* -1 means check next element for defun,
1561 0 means don't check,
1562 1 means already checked and found defun. */
1563 int defunflag = flag < 0 ? -1 : 0;
1564 Lisp_Object val, tail;
1565 register Lisp_Object elt, tem;
1566 struct gcpro gcpro1, gcpro2;
1567 int cancel = 0;
1569 /* Initialize this to 1 if we are reading a list. */
1570 int first_in_list = flag <= 0;
1572 val = Qnil;
1573 tail = Qnil;
1575 while (1)
1577 char ch;
1578 GCPRO2 (val, tail);
1579 elt = read1 (readcharfun, &ch, first_in_list);
1580 UNGCPRO;
1582 first_in_list = 0;
1584 /* If purifying, and the list starts with #$,
1585 return 0 instead. This is a doc string reference
1586 and it will be replaced anyway by Snarf-documentation,
1587 so don't waste pure space with it. */
1588 if (EQ (elt, Vload_file_name)
1589 && !NILP (Vpurify_flag) && NILP (Vdoc_file_name))
1590 cancel = 1;
1592 if (ch)
1594 if (flag > 0)
1596 if (ch == ']')
1597 return val;
1598 Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
1600 if (ch == ')')
1601 return val;
1602 if (ch == '.')
1604 GCPRO2 (val, tail);
1605 if (!NILP (tail))
1606 XCONS (tail)->cdr = read0 (readcharfun);
1607 else
1608 val = read0 (readcharfun);
1609 read1 (readcharfun, &ch, 0);
1610 UNGCPRO;
1611 if (ch == ')')
1612 return (cancel ? make_number (0) : val);
1613 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1615 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1617 tem = (read_pure && flag <= 0
1618 ? pure_cons (elt, Qnil)
1619 : Fcons (elt, Qnil));
1620 if (!NILP (tail))
1621 XCONS (tail)->cdr = tem;
1622 else
1623 val = tem;
1624 tail = tem;
1625 if (defunflag < 0)
1626 defunflag = EQ (elt, Qdefun);
1627 else if (defunflag > 0)
1628 read_pure = 1;
1632 Lisp_Object Vobarray;
1633 Lisp_Object initial_obarray;
1635 /* oblookup stores the bucket number here, for the sake of Funintern. */
1637 int oblookup_last_bucket_number;
1639 static int hash_string ();
1640 Lisp_Object oblookup ();
1642 /* Get an error if OBARRAY is not an obarray.
1643 If it is one, return it. */
1645 Lisp_Object
1646 check_obarray (obarray)
1647 Lisp_Object obarray;
1649 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1651 /* If Vobarray is now invalid, force it to be valid. */
1652 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1654 obarray = wrong_type_argument (Qvectorp, obarray);
1656 return obarray;
1659 /* Intern the C string STR: return a symbol with that name,
1660 interned in the current obarray. */
1662 Lisp_Object
1663 intern (str)
1664 char *str;
1666 Lisp_Object tem;
1667 int len = strlen (str);
1668 Lisp_Object obarray;
1670 obarray = Vobarray;
1671 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1672 obarray = check_obarray (obarray);
1673 tem = oblookup (obarray, str, len);
1674 if (SYMBOLP (tem))
1675 return tem;
1676 return Fintern ((!NILP (Vpurify_flag)
1677 ? make_pure_string (str, len)
1678 : make_string (str, len)),
1679 obarray);
1682 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1683 "Return the canonical symbol whose name is STRING.\n\
1684 If there is none, one is created by this function and returned.\n\
1685 A second optional argument specifies the obarray to use;\n\
1686 it defaults to the value of `obarray'.")
1687 (str, obarray)
1688 Lisp_Object str, obarray;
1690 register Lisp_Object tem, sym, *ptr;
1692 if (NILP (obarray)) obarray = Vobarray;
1693 obarray = check_obarray (obarray);
1695 CHECK_STRING (str, 0);
1697 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1698 if (!INTEGERP (tem))
1699 return tem;
1701 if (!NILP (Vpurify_flag))
1702 str = Fpurecopy (str);
1703 sym = Fmake_symbol (str);
1705 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
1706 if (SYMBOLP (*ptr))
1707 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1708 else
1709 XSYMBOL (sym)->next = 0;
1710 *ptr = sym;
1711 return sym;
1714 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1715 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1716 A second optional argument specifies the obarray to use;\n\
1717 it defaults to the value of `obarray'.")
1718 (str, obarray)
1719 Lisp_Object str, obarray;
1721 register Lisp_Object tem;
1723 if (NILP (obarray)) obarray = Vobarray;
1724 obarray = check_obarray (obarray);
1726 CHECK_STRING (str, 0);
1728 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1729 if (!INTEGERP (tem))
1730 return tem;
1731 return Qnil;
1734 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
1735 "Delete the symbol named NAME, if any, from OBARRAY.\n\
1736 The value is t if a symbol was found and deleted, nil otherwise.\n\
1737 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
1738 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
1739 OBARRAY defaults to the value of the variable `obarray'.")
1740 (name, obarray)
1741 Lisp_Object name, obarray;
1743 register Lisp_Object string, tem;
1744 int hash;
1746 if (NILP (obarray)) obarray = Vobarray;
1747 obarray = check_obarray (obarray);
1749 if (SYMBOLP (name))
1750 XSETSTRING (string, XSYMBOL (name)->name);
1751 else
1753 CHECK_STRING (name, 0);
1754 string = name;
1757 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
1758 if (INTEGERP (tem))
1759 return Qnil;
1760 /* If arg was a symbol, don't delete anything but that symbol itself. */
1761 if (SYMBOLP (name) && !EQ (name, tem))
1762 return Qnil;
1764 hash = oblookup_last_bucket_number;
1766 if (EQ (XVECTOR (obarray)->contents[hash], tem))
1767 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
1768 else
1770 Lisp_Object tail, following;
1772 for (tail = XVECTOR (obarray)->contents[hash];
1773 XSYMBOL (tail)->next;
1774 tail = following)
1776 XSETSYMBOL (following, XSYMBOL (tail)->next);
1777 if (EQ (following, tem))
1779 XSYMBOL (tail)->next = XSYMBOL (following)->next;
1780 break;
1785 return Qt;
1788 /* Return the symbol in OBARRAY whose names matches the string
1789 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
1790 return nil.
1792 Also store the bucket number in oblookup_last_bucket_number. */
1794 Lisp_Object
1795 oblookup (obarray, ptr, size, hashp)
1796 Lisp_Object obarray;
1797 register char *ptr;
1798 register int size;
1799 int *hashp;
1801 int hash;
1802 int obsize;
1803 register Lisp_Object tail;
1804 Lisp_Object bucket, tem;
1806 if (!VECTORP (obarray)
1807 || (obsize = XVECTOR (obarray)->size) == 0)
1809 obarray = check_obarray (obarray);
1810 obsize = XVECTOR (obarray)->size;
1812 /* Combining next two lines breaks VMS C 2.3. */
1813 hash = hash_string (ptr, size);
1814 hash %= obsize;
1815 bucket = XVECTOR (obarray)->contents[hash];
1816 oblookup_last_bucket_number = hash;
1817 if (XFASTINT (bucket) == 0)
1819 else if (!SYMBOLP (bucket))
1820 error ("Bad data in guts of obarray"); /* Like CADR error message */
1821 else
1822 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
1824 if (XSYMBOL (tail)->name->size == size
1825 && !bcmp (XSYMBOL (tail)->name->data, ptr, size))
1826 return tail;
1827 else if (XSYMBOL (tail)->next == 0)
1828 break;
1830 XSETINT (tem, hash);
1831 return tem;
1834 static int
1835 hash_string (ptr, len)
1836 unsigned char *ptr;
1837 int len;
1839 register unsigned char *p = ptr;
1840 register unsigned char *end = p + len;
1841 register unsigned char c;
1842 register int hash = 0;
1844 while (p != end)
1846 c = *p++;
1847 if (c >= 0140) c -= 40;
1848 hash = ((hash<<3) + (hash>>28) + c);
1850 return hash & 07777777777;
1853 void
1854 map_obarray (obarray, fn, arg)
1855 Lisp_Object obarray;
1856 int (*fn) ();
1857 Lisp_Object arg;
1859 register int i;
1860 register Lisp_Object tail;
1861 CHECK_VECTOR (obarray, 1);
1862 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
1864 tail = XVECTOR (obarray)->contents[i];
1865 if (XFASTINT (tail) != 0)
1866 while (1)
1868 (*fn) (tail, arg);
1869 if (XSYMBOL (tail)->next == 0)
1870 break;
1871 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1876 mapatoms_1 (sym, function)
1877 Lisp_Object sym, function;
1879 call1 (function, sym);
1882 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
1883 "Call FUNCTION on every symbol in OBARRAY.\n\
1884 OBARRAY defaults to the value of `obarray'.")
1885 (function, obarray)
1886 Lisp_Object function, obarray;
1888 Lisp_Object tem;
1890 if (NILP (obarray)) obarray = Vobarray;
1891 obarray = check_obarray (obarray);
1893 map_obarray (obarray, mapatoms_1, function);
1894 return Qnil;
1897 #define OBARRAY_SIZE 1511
1899 void
1900 init_obarray ()
1902 Lisp_Object oblength;
1903 int hash;
1904 Lisp_Object *tem;
1906 XSETFASTINT (oblength, OBARRAY_SIZE);
1908 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
1909 Vobarray = Fmake_vector (oblength, make_number (0));
1910 initial_obarray = Vobarray;
1911 staticpro (&initial_obarray);
1912 /* Intern nil in the obarray */
1913 /* These locals are to kludge around a pyramid compiler bug. */
1914 hash = hash_string ("nil", 3);
1915 /* Separate statement here to avoid VAXC bug. */
1916 hash %= OBARRAY_SIZE;
1917 tem = &XVECTOR (Vobarray)->contents[hash];
1918 *tem = Qnil;
1920 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
1921 XSYMBOL (Qnil)->function = Qunbound;
1922 XSYMBOL (Qunbound)->value = Qunbound;
1923 XSYMBOL (Qunbound)->function = Qunbound;
1925 Qt = intern ("t");
1926 XSYMBOL (Qnil)->value = Qnil;
1927 XSYMBOL (Qnil)->plist = Qnil;
1928 XSYMBOL (Qt)->value = Qt;
1930 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
1931 Vpurify_flag = Qt;
1933 Qvariable_documentation = intern ("variable-documentation");
1935 read_buffer_size = 100;
1936 read_buffer = (char *) malloc (read_buffer_size);
1939 void
1940 defsubr (sname)
1941 struct Lisp_Subr *sname;
1943 Lisp_Object sym;
1944 sym = intern (sname->symbol_name);
1945 XSETSUBR (XSYMBOL (sym)->function, sname);
1948 #ifdef NOTDEF /* use fset in subr.el now */
1949 void
1950 defalias (sname, string)
1951 struct Lisp_Subr *sname;
1952 char *string;
1954 Lisp_Object sym;
1955 sym = intern (string);
1956 XSETSUBR (XSYMBOL (sym)->function, sname);
1958 #endif /* NOTDEF */
1960 /* Define an "integer variable"; a symbol whose value is forwarded
1961 to a C variable of type int. Sample call: */
1962 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
1963 void
1964 defvar_int (namestring, address)
1965 char *namestring;
1966 int *address;
1968 Lisp_Object sym, val;
1969 sym = intern (namestring);
1970 val = allocate_misc ();
1971 XMISCTYPE (val) = Lisp_Misc_Intfwd;
1972 XINTFWD (val)->intvar = address;
1973 XSYMBOL (sym)->value = val;
1976 /* Similar but define a variable whose value is T if address contains 1,
1977 NIL if address contains 0 */
1978 void
1979 defvar_bool (namestring, address)
1980 char *namestring;
1981 int *address;
1983 Lisp_Object sym, val;
1984 sym = intern (namestring);
1985 val = allocate_misc ();
1986 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
1987 XBOOLFWD (val)->boolvar = address;
1988 XSYMBOL (sym)->value = val;
1991 /* Similar but define a variable whose value is the Lisp Object stored
1992 at address. Two versions: with and without gc-marking of the C
1993 variable. The nopro version is used when that variable will be
1994 gc-marked for some other reason, since marking the same slot twice
1995 can cause trouble with strings. */
1996 void
1997 defvar_lisp_nopro (namestring, address)
1998 char *namestring;
1999 Lisp_Object *address;
2001 Lisp_Object sym, val;
2002 sym = intern (namestring);
2003 val = allocate_misc ();
2004 XMISCTYPE (val) = Lisp_Misc_Objfwd;
2005 XOBJFWD (val)->objvar = address;
2006 XSYMBOL (sym)->value = val;
2009 void
2010 defvar_lisp (namestring, address)
2011 char *namestring;
2012 Lisp_Object *address;
2014 defvar_lisp_nopro (namestring, address);
2015 staticpro (address);
2018 #ifndef standalone
2020 /* Similar but define a variable whose value is the Lisp Object stored in
2021 the current buffer. address is the address of the slot in the buffer
2022 that is current now. */
2024 void
2025 defvar_per_buffer (namestring, address, type, doc)
2026 char *namestring;
2027 Lisp_Object *address;
2028 Lisp_Object type;
2029 char *doc;
2031 Lisp_Object sym, val;
2032 int offset;
2033 extern struct buffer buffer_local_symbols;
2035 sym = intern (namestring);
2036 val = allocate_misc ();
2037 offset = (char *)address - (char *)current_buffer;
2039 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
2040 XBUFFER_OBJFWD (val)->offset = offset;
2041 XSYMBOL (sym)->value = val;
2042 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
2043 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
2044 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
2045 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
2046 slot of buffer_local_flags */
2047 abort ();
2050 #endif /* standalone */
2052 /* Similar but define a variable whose value is the Lisp Object stored
2053 at a particular offset in the current kboard object. */
2055 void
2056 defvar_kboard (namestring, offset)
2057 char *namestring;
2058 int offset;
2060 Lisp_Object sym, val;
2061 sym = intern (namestring);
2062 val = allocate_misc ();
2063 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
2064 XKBOARD_OBJFWD (val)->offset = offset;
2065 XSYMBOL (sym)->value = val;
2068 init_lread ()
2070 char *normal;
2071 int turn_off_warning = 0;
2073 /* Compute the default load-path. */
2074 #ifdef CANNOT_DUMP
2075 normal = PATH_LOADSEARCH;
2076 Vload_path = decode_env_path (0, normal);
2077 #else
2078 if (NILP (Vpurify_flag))
2079 normal = PATH_LOADSEARCH;
2080 else
2081 normal = PATH_DUMPLOADSEARCH;
2083 /* In a dumped Emacs, we normally have to reset the value of
2084 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2085 uses ../lisp, instead of the path of the installed elisp
2086 libraries. However, if it appears that Vload_path was changed
2087 from the default before dumping, don't override that value. */
2088 if (initialized)
2090 Lisp_Object dump_path;
2092 dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH);
2093 if (! NILP (Fequal (dump_path, Vload_path)))
2095 Vload_path = decode_env_path (0, normal);
2096 if (!NILP (Vinstallation_directory))
2098 /* Add to the path the lisp subdir of the
2099 installation dir, if it exists. */
2100 Lisp_Object tem, tem1;
2101 tem = Fexpand_file_name (build_string ("lisp"),
2102 Vinstallation_directory);
2103 tem1 = Ffile_exists_p (tem);
2104 if (!NILP (tem1))
2106 if (NILP (Fmember (tem, Vload_path)))
2108 turn_off_warning = 1;
2109 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2112 else
2113 /* That dir doesn't exist, so add the build-time
2114 Lisp dirs instead. */
2115 Vload_path = nconc2 (Vload_path, dump_path);
2117 /* Add site-list under the installation dir, if it exists. */
2118 tem = Fexpand_file_name (build_string ("site-lisp"),
2119 Vinstallation_directory);
2120 tem1 = Ffile_exists_p (tem);
2121 if (!NILP (tem1))
2123 if (NILP (Fmember (tem, Vload_path)))
2124 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2129 else
2130 Vload_path = decode_env_path (0, normal);
2131 #endif
2133 #ifndef WINDOWSNT
2134 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2135 almost never correct, thereby causing a warning to be printed out that
2136 confuses users. Since PATH_LOADSEARCH is always overriden by the
2137 EMACSLOADPATH environment variable below, disable the warning on NT. */
2139 /* Warn if dirs in the *standard* path don't exist. */
2140 if (!turn_off_warning)
2142 Lisp_Object path_tail;
2144 for (path_tail = Vload_path;
2145 !NILP (path_tail);
2146 path_tail = XCONS (path_tail)->cdr)
2148 Lisp_Object dirfile;
2149 dirfile = Fcar (path_tail);
2150 if (STRINGP (dirfile))
2152 dirfile = Fdirectory_file_name (dirfile);
2153 if (access (XSTRING (dirfile)->data, 0) < 0)
2154 fprintf (stderr,
2155 "Warning: Lisp directory `%s' does not exist.\n",
2156 XSTRING (Fcar (path_tail))->data);
2160 #endif /* WINDOWSNT */
2162 /* If the EMACSLOADPATH environment variable is set, use its value.
2163 This doesn't apply if we're dumping. */
2164 if (NILP (Vpurify_flag)
2165 && egetenv ("EMACSLOADPATH"))
2166 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
2168 Vvalues = Qnil;
2170 load_in_progress = 0;
2172 load_descriptor_list = Qnil;
2175 void
2176 syms_of_lread ()
2178 defsubr (&Sread);
2179 defsubr (&Sread_from_string);
2180 defsubr (&Sintern);
2181 defsubr (&Sintern_soft);
2182 defsubr (&Sunintern);
2183 defsubr (&Sload);
2184 defsubr (&Seval_buffer);
2185 defsubr (&Seval_region);
2186 defsubr (&Sread_char);
2187 defsubr (&Sread_char_exclusive);
2188 defsubr (&Sread_event);
2189 defsubr (&Sget_file_char);
2190 defsubr (&Smapatoms);
2192 DEFVAR_LISP ("obarray", &Vobarray,
2193 "Symbol table for use by `intern' and `read'.\n\
2194 It is a vector whose length ought to be prime for best results.\n\
2195 The vector's contents don't make sense if examined from Lisp programs;\n\
2196 to find all the symbols in an obarray, use `mapatoms'.");
2198 DEFVAR_LISP ("values", &Vvalues,
2199 "List of values of all expressions which were read, evaluated and printed.\n\
2200 Order is reverse chronological.");
2202 DEFVAR_LISP ("standard-input", &Vstandard_input,
2203 "Stream for read to get input from.\n\
2204 See documentation of `read' for possible values.");
2205 Vstandard_input = Qt;
2207 DEFVAR_LISP ("load-path", &Vload_path,
2208 "*List of directories to search for files to load.\n\
2209 Each element is a string (directory name) or nil (try default directory).\n\
2210 Initialized based on EMACSLOADPATH environment variable, if any,\n\
2211 otherwise to default specified by file `paths.h' when Emacs was built.");
2213 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
2214 "Non-nil iff inside of `load'.");
2216 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
2217 "An alist of expressions to be evalled when particular files are loaded.\n\
2218 Each element looks like (FILENAME FORMS...).\n\
2219 When `load' is run and the file-name argument is FILENAME,\n\
2220 the FORMS in the corresponding element are executed at the end of loading.\n\n\
2221 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2222 with no directory specified, since that is how `load' is normally called.\n\
2223 An error in FORMS does not undo the load,\n\
2224 but does prevent execution of the rest of the FORMS.");
2225 Vafter_load_alist = Qnil;
2227 DEFVAR_LISP ("load-history", &Vload_history,
2228 "Alist mapping source file names to symbols and features.\n\
2229 Each alist element is a list that starts with a file name,\n\
2230 except for one element (optional) that starts with nil and describes\n\
2231 definitions evaluated from buffers not visiting files.\n\
2232 The remaining elements of each list are symbols defined as functions\n\
2233 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2234 Vload_history = Qnil;
2236 DEFVAR_LISP ("load-file-name", &Vload_file_name,
2237 "Full name of file being loaded by `load'.");
2238 Vload_file_name = Qnil;
2240 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
2241 "Used for internal purposes by `load'.");
2242 Vcurrent_load_list = Qnil;
2244 DEFVAR_LISP ("load-read-function", &Vload_read_function,
2245 "Function used by `load' and `eval-region' for reading expressions.\n\
2246 The default is nil, which means use the function `read'.");
2247 Vload_read_function = Qnil;
2249 load_descriptor_list = Qnil;
2250 staticpro (&load_descriptor_list);
2252 Qcurrent_load_list = intern ("current-load-list");
2253 staticpro (&Qcurrent_load_list);
2255 Qstandard_input = intern ("standard-input");
2256 staticpro (&Qstandard_input);
2258 Qread_char = intern ("read-char");
2259 staticpro (&Qread_char);
2261 Qget_file_char = intern ("get-file-char");
2262 staticpro (&Qget_file_char);
2264 Qbackquote = intern ("`");
2265 staticpro (&Qbackquote);
2266 Qcomma = intern (",");
2267 staticpro (&Qcomma);
2268 Qcomma_at = intern (",@");
2269 staticpro (&Qcomma_at);
2270 Qcomma_dot = intern (",.");
2271 staticpro (&Qcomma_dot);
2273 Qascii_character = intern ("ascii-character");
2274 staticpro (&Qascii_character);
2276 Qload = intern ("load");
2277 staticpro (&Qload);
2279 Qload_file_name = intern ("load-file-name");
2280 staticpro (&Qload_file_name);