Mercurial > emacs
annotate src/lread.c @ 672:f81a3cf6ec22
*** empty log message ***
author | Joseph Arceneaux <jla@gnu.org> |
---|---|
date | Tue, 02 Jun 1992 04:15:32 +0000 |
parents | 40b255f55df3 |
children | 6217fa6e2cab |
rev | line source |
---|---|
341 | 1 /* Lisp parsing and input streams. |
604 | 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. |
341 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
617 | 8 the Free Software Foundation; either version 2, or (at your option) |
341 | 9 any later version. |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
21 #include <stdio.h> | |
22 #include <sys/types.h> | |
23 #include <sys/stat.h> | |
24 #include <sys/file.h> | |
25 #undef NULL | |
26 #include "config.h" | |
27 #include "lisp.h" | |
28 | |
29 #ifndef standalone | |
30 #include "buffer.h" | |
31 #include "paths.h" | |
32 #include "commands.h" | |
33 #endif | |
34 | |
35 #ifdef lint | |
36 #include <sys/inode.h> | |
37 #endif /* lint */ | |
38 | |
39 #ifndef X_OK | |
40 #define X_OK 01 | |
41 #endif | |
42 | |
43 #ifdef LISP_FLOAT_TYPE | |
44 #include <math.h> | |
45 #endif /* LISP_FLOAT_TYPE */ | |
46 | |
47 Lisp_Object Qread_char, Qget_file_char, Qstandard_input; | |
48 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist; | |
49 | |
50 /* non-zero if inside `load' */ | |
51 int load_in_progress; | |
52 | |
53 /* Search path for files to be loaded. */ | |
54 Lisp_Object Vload_path; | |
55 | |
56 /* File for get_file_char to read from. Use by load */ | |
57 static FILE *instream; | |
58 | |
59 /* When nonzero, read conses in pure space */ | |
60 static int read_pure; | |
61 | |
62 /* For use within read-from-string (this reader is non-reentrant!!) */ | |
63 static int read_from_string_index; | |
64 static int read_from_string_limit; | |
65 | |
66 /* Handle unreading and rereading of characters. | |
67 Write READCHAR to read a character, | |
68 UNREAD(c) to unread c to be read again. */ | |
69 | |
70 #define READCHAR readchar (readcharfun) | |
71 #define UNREAD(c) unreadchar (readcharfun, c) | |
72 | |
73 static int | |
74 readchar (readcharfun) | |
75 Lisp_Object readcharfun; | |
76 { | |
77 Lisp_Object tem; | |
78 register struct buffer *inbuffer; | |
79 register int c, mpos; | |
80 | |
81 if (XTYPE (readcharfun) == Lisp_Buffer) | |
82 { | |
83 inbuffer = XBUFFER (readcharfun); | |
84 | |
85 if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer)) | |
86 return -1; | |
87 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer)); | |
88 SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1); | |
89 | |
90 return c; | |
91 } | |
92 if (XTYPE (readcharfun) == Lisp_Marker) | |
93 { | |
94 inbuffer = XMARKER (readcharfun)->buffer; | |
95 | |
96 mpos = marker_position (readcharfun); | |
97 | |
98 if (mpos > BUF_ZV (inbuffer) - 1) | |
99 return -1; | |
100 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos); | |
101 if (mpos != BUF_GPT (inbuffer)) | |
102 XMARKER (readcharfun)->bufpos++; | |
103 else | |
104 Fset_marker (readcharfun, make_number (mpos + 1), | |
105 Fmarker_buffer (readcharfun)); | |
106 return c; | |
107 } | |
108 if (EQ (readcharfun, Qget_file_char)) | |
109 return getc (instream); | |
110 | |
111 if (XTYPE (readcharfun) == Lisp_String) | |
112 { | |
113 register int c; | |
114 /* This used to be return of a conditional expression, | |
115 but that truncated -1 to a char on VMS. */ | |
116 if (read_from_string_index < read_from_string_limit) | |
117 c = XSTRING (readcharfun)->data[read_from_string_index++]; | |
118 else | |
119 c = -1; | |
120 return c; | |
121 } | |
122 | |
123 tem = call0 (readcharfun); | |
124 | |
485 | 125 if (NILP (tem)) |
341 | 126 return -1; |
127 return XINT (tem); | |
128 } | |
129 | |
130 /* Unread the character C in the way appropriate for the stream READCHARFUN. | |
131 If the stream is a user function, call it with the char as argument. */ | |
132 | |
133 static void | |
134 unreadchar (readcharfun, c) | |
135 Lisp_Object readcharfun; | |
136 int c; | |
137 { | |
138 if (XTYPE (readcharfun) == Lisp_Buffer) | |
139 { | |
140 if (XBUFFER (readcharfun) == current_buffer) | |
141 SET_PT (point - 1); | |
142 else | |
143 SET_BUF_PT (XBUFFER (readcharfun), BUF_PT (XBUFFER (readcharfun)) - 1); | |
144 } | |
145 else if (XTYPE (readcharfun) == Lisp_Marker) | |
146 XMARKER (readcharfun)->bufpos--; | |
147 else if (XTYPE (readcharfun) == Lisp_String) | |
148 read_from_string_index--; | |
149 else if (EQ (readcharfun, Qget_file_char)) | |
150 ungetc (c, instream); | |
151 else | |
152 call1 (readcharfun, make_number (c)); | |
153 } | |
154 | |
155 static Lisp_Object read0 (), read1 (), read_list (), read_vector (); | |
156 | |
157 /* get a character from the tty */ | |
158 | |
159 DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0, | |
160 "Read a character from the command input (keyboard or macro).\n\ | |
161 It is returned as a number.") | |
162 () | |
163 { | |
164 register Lisp_Object val; | |
165 | |
166 #ifndef standalone | |
167 val = read_char (0); | |
168 if (XTYPE (val) != Lisp_Int) | |
169 { | |
170 unread_command_char = val; | |
171 error ("Object read was not a character"); | |
172 } | |
173 #else | |
174 val = getchar (); | |
175 #endif | |
176 | |
177 return val; | |
178 } | |
179 | |
180 #ifdef HAVE_X_WINDOWS | |
181 DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0, | |
182 "Read an event object from the input stream.") | |
183 () | |
184 { | |
185 register Lisp_Object val; | |
186 | |
187 val = read_char (0); | |
188 return val; | |
189 } | |
190 | |
191 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0, | |
192 "Read a character from the command input (keyboard or macro).\n\ | |
193 It is returned as a number. Non character events are ignored.") | |
194 () | |
195 { | |
196 register Lisp_Object val; | |
197 | |
198 #ifndef standalone | |
199 val = read_char (0); | |
200 while (XTYPE (val) != Lisp_Int) | |
201 val = read_char (0); | |
202 #else | |
203 val = getchar (); | |
204 #endif | |
205 | |
206 return val; | |
207 } | |
208 #endif /* HAVE_X_WINDOWS */ | |
209 | |
210 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0, | |
211 "Don't use this yourself.") | |
212 () | |
213 { | |
214 register Lisp_Object val; | |
215 XSET (val, Lisp_Int, getc (instream)); | |
216 return val; | |
217 } | |
218 | |
219 static void readevalloop (); | |
220 static Lisp_Object load_unwind (); | |
221 | |
222 DEFUN ("load", Fload, Sload, 1, 4, 0, | |
223 "Execute a file of Lisp code named FILE.\n\ | |
224 First try FILE with `.elc' appended, then try with `.el',\n\ | |
225 then try FILE unmodified.\n\ | |
226 This function searches the directories in `load-path'.\n\ | |
227 If optional second arg NOERROR is non-nil,\n\ | |
228 report no error if FILE doesn't exist.\n\ | |
229 Print messages at start and end of loading unless\n\ | |
230 optional third arg NOMESSAGE is non-nil.\n\ | |
231 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\ | |
232 suffixes `.elc' or `.el' to the specified name FILE.\n\ | |
233 Return t if file exists.") | |
234 (str, noerror, nomessage, nosuffix) | |
235 Lisp_Object str, noerror, nomessage, nosuffix; | |
236 { | |
237 register FILE *stream; | |
238 register int fd = -1; | |
239 register Lisp_Object lispstream; | |
240 register FILE **ptr; | |
241 int count = specpdl_ptr - specpdl; | |
242 Lisp_Object temp; | |
243 struct gcpro gcpro1; | |
244 Lisp_Object found; | |
245 | |
246 CHECK_STRING (str, 0); | |
247 str = Fsubstitute_in_file_name (str); | |
248 | |
249 /* Avoid weird lossage with null string as arg, | |
250 since it would try to load a directory as a Lisp file */ | |
251 if (XSTRING (str)->size > 0) | |
252 { | |
485 | 253 fd = openp (Vload_path, str, !NILP (nosuffix) ? "" : ".elc:.el:", |
341 | 254 &found, 0); |
255 } | |
256 | |
257 if (fd < 0) | |
258 { | |
485 | 259 if (NILP (noerror)) |
341 | 260 while (1) |
261 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"), | |
262 Fcons (str, Qnil))); | |
263 else | |
264 return Qnil; | |
265 } | |
266 | |
267 if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]), | |
268 ".elc", 4)) | |
269 { | |
270 struct stat s1, s2; | |
271 int result; | |
272 | |
273 stat (XSTRING (found)->data, &s1); | |
274 XSTRING (found)->data[XSTRING (found)->size - 1] = 0; | |
275 result = stat (XSTRING (found)->data, &s2); | |
276 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime) | |
277 message ("Source file `%s' newer than byte-compiled file", | |
278 XSTRING (found)->data); | |
279 XSTRING (found)->data[XSTRING (found)->size - 1] = 'c'; | |
280 } | |
281 | |
282 stream = fdopen (fd, "r"); | |
283 if (stream == 0) | |
284 { | |
285 close (fd); | |
286 error ("Failure to create stdio stream for %s", XSTRING (str)->data); | |
287 } | |
288 | |
485 | 289 if (NILP (nomessage)) |
341 | 290 message ("Loading %s...", XSTRING (str)->data); |
291 | |
292 GCPRO1 (str); | |
293 /* We may not be able to store STREAM itself as a Lisp_Object pointer | |
294 since that is guaranteed to work only for data that has been malloc'd. | |
295 So malloc a full-size pointer, and record the address of that pointer. */ | |
296 ptr = (FILE **) xmalloc (sizeof (FILE *)); | |
297 *ptr = stream; | |
298 XSET (lispstream, Lisp_Internal_Stream, (int) ptr); | |
299 record_unwind_protect (load_unwind, lispstream); | |
300 load_in_progress++; | |
301 readevalloop (Qget_file_char, stream, Feval, 0); | |
302 unbind_to (count, Qnil); | |
303 | |
304 /* Run any load-hooks for this file. */ | |
305 temp = Fassoc (str, Vafter_load_alist); | |
485 | 306 if (!NILP (temp)) |
341 | 307 Fprogn (Fcdr (temp)); |
308 UNGCPRO; | |
309 | |
485 | 310 if (!noninteractive && NILP (nomessage)) |
341 | 311 message ("Loading %s...done", XSTRING (str)->data); |
312 return Qt; | |
313 } | |
314 | |
315 static Lisp_Object | |
316 load_unwind (stream) /* used as unwind-protect function in load */ | |
317 Lisp_Object stream; | |
318 { | |
319 fclose (*(FILE **) XSTRING (stream)); | |
320 free (XPNTR (stream)); | |
321 if (--load_in_progress < 0) load_in_progress = 0; | |
322 return Qnil; | |
323 } | |
324 | |
325 | |
326 static int | |
327 complete_filename_p (pathname) | |
328 Lisp_Object pathname; | |
329 { | |
330 register unsigned char *s = XSTRING (pathname)->data; | |
331 return (*s == '/' | |
332 #ifdef ALTOS | |
333 || *s == '@' | |
334 #endif | |
335 #ifdef VMS | |
336 || index (s, ':') | |
337 #endif /* VMS */ | |
338 ); | |
339 } | |
340 | |
341 /* Search for a file whose name is STR, looking in directories | |
342 in the Lisp list PATH, and trying suffixes from SUFFIX. | |
343 SUFFIX is a string containing possible suffixes separated by colons. | |
344 On success, returns a file descriptor. On failure, returns -1. | |
345 | |
346 EXEC_ONLY nonzero means don't open the files, | |
347 just look for one that is executable. In this case, | |
348 returns 1 on success. | |
349 | |
350 If STOREPTR is nonzero, it points to a slot where the name of | |
351 the file actually found should be stored as a Lisp string. | |
352 Nil is stored there on failure. */ | |
353 | |
354 int | |
355 openp (path, str, suffix, storeptr, exec_only) | |
356 Lisp_Object path, str; | |
357 char *suffix; | |
358 Lisp_Object *storeptr; | |
359 int exec_only; | |
360 { | |
361 register int fd; | |
362 int fn_size = 100; | |
363 char buf[100]; | |
364 register char *fn = buf; | |
365 int absolute = 0; | |
366 int want_size; | |
367 register Lisp_Object filename; | |
368 struct stat st; | |
369 | |
370 if (storeptr) | |
371 *storeptr = Qnil; | |
372 | |
373 if (complete_filename_p (str)) | |
374 absolute = 1; | |
375 | |
485 | 376 for (; !NILP (path); path = Fcdr (path)) |
341 | 377 { |
378 char *nsuffix; | |
379 | |
380 filename = Fexpand_file_name (str, Fcar (path)); | |
381 if (!complete_filename_p (filename)) | |
382 /* If there are non-absolute elts in PATH (eg ".") */ | |
383 /* Of course, this could conceivably lose if luser sets | |
384 default-directory to be something non-absolute... */ | |
385 { | |
386 filename = Fexpand_file_name (filename, current_buffer->directory); | |
387 if (!complete_filename_p (filename)) | |
388 /* Give up on this path element! */ | |
389 continue; | |
390 } | |
391 | |
392 /* Calculate maximum size of any filename made from | |
393 this path element/specified file name and any possible suffix. */ | |
394 want_size = strlen (suffix) + XSTRING (filename)->size + 1; | |
395 if (fn_size < want_size) | |
396 fn = (char *) alloca (fn_size = 100 + want_size); | |
397 | |
398 nsuffix = suffix; | |
399 | |
400 /* Loop over suffixes. */ | |
401 while (1) | |
402 { | |
403 char *esuffix = (char *) index (nsuffix, ':'); | |
404 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix); | |
405 | |
406 /* Concatenate path element/specified name with the suffix. */ | |
407 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size); | |
408 fn[XSTRING (filename)->size] = 0; | |
409 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */ | |
410 strncat (fn, nsuffix, lsuffix); | |
411 | |
412 /* Ignore file if it's a directory. */ | |
413 if (stat (fn, &st) >= 0 | |
414 && (st.st_mode & S_IFMT) != S_IFDIR) | |
415 { | |
416 /* Check that we can access or open it. */ | |
417 if (exec_only) | |
418 fd = (access (fn, X_OK) == 0) ? 1 : -1; | |
419 else | |
420 fd = open (fn, 0, 0); | |
421 | |
422 if (fd >= 0) | |
423 { | |
424 /* We succeeded; return this descriptor and filename. */ | |
425 if (storeptr) | |
426 *storeptr = build_string (fn); | |
427 return fd; | |
428 } | |
429 } | |
430 | |
431 /* Advance to next suffix. */ | |
432 if (esuffix == 0) | |
433 break; | |
434 nsuffix += lsuffix + 1; | |
435 } | |
436 if (absolute) return -1; | |
437 } | |
438 | |
439 return -1; | |
440 } | |
441 | |
442 | |
443 Lisp_Object | |
444 unreadpure () /* Used as unwind-protect function in readevalloop */ | |
445 { | |
446 read_pure = 0; | |
447 return Qnil; | |
448 } | |
449 | |
450 static void | |
451 readevalloop (readcharfun, stream, evalfun, printflag) | |
452 Lisp_Object readcharfun; | |
453 FILE *stream; | |
454 Lisp_Object (*evalfun) (); | |
455 int printflag; | |
456 { | |
457 register int c; | |
458 register Lisp_Object val; | |
459 int count = specpdl_ptr - specpdl; | |
460 | |
461 specbind (Qstandard_input, readcharfun); | |
462 | |
463 while (1) | |
464 { | |
465 instream = stream; | |
466 c = READCHAR; | |
467 if (c == ';') | |
468 { | |
469 while ((c = READCHAR) != '\n' && c != -1); | |
470 continue; | |
471 } | |
472 if (c < 0) break; | |
473 if (c == ' ' || c == '\t' || c == '\n' || c == '\f') continue; | |
474 | |
485 | 475 if (!NILP (Vpurify_flag) && c == '(') |
341 | 476 { |
477 record_unwind_protect (unreadpure, Qnil); | |
478 val = read_list (-1, readcharfun); | |
479 unbind_to (count + 1, Qnil); | |
480 } | |
481 else | |
482 { | |
483 UNREAD (c); | |
484 val = read0 (readcharfun); | |
485 } | |
486 | |
487 val = (*evalfun) (val); | |
488 if (printflag) | |
489 { | |
490 Vvalues = Fcons (val, Vvalues); | |
491 if (EQ (Vstandard_output, Qt)) | |
492 Fprin1 (val, Qnil); | |
493 else | |
494 Fprint (val, Qnil); | |
495 } | |
496 } | |
497 | |
498 unbind_to (count, Qnil); | |
499 } | |
500 | |
501 #ifndef standalone | |
502 | |
672 | 503 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 2, "bBuffer: ", |
504 "Execute BUFFER as Lisp code. If BUFFER is nil, use the current buffer.\n\ | |
505 Programs can pass argument PRINTFLAG which controls printing of output:\n\ | |
506 nil means discard it; anything else is stream for print.\n\ | |
507 \n\ | |
508 If there is no error, point does not move. If there is an error,\n\ | |
509 point remains at the end of the last character read from the buffer.") | |
510 (bufname, printflag) | |
511 Lisp_Object bufname, printflag; | |
512 { | |
513 int count = specpdl_ptr - specpdl; | |
514 Lisp_Object tem, buf; | |
515 | |
516 if (NIL_P (bufname)) | |
517 buf = Fcurrent_buffer (); | |
518 else | |
519 buf = Fget_buffer (bufname); | |
520 if (NIL_P (buf)) | |
521 error ("No such buffer."); | |
522 | |
523 if (NIL_P (printflag)) | |
524 tem = Qsymbolp; | |
525 else | |
526 tem = printflag; | |
527 specbind (Qstandard_output, tem); | |
528 record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
529 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf))); | |
530 readevalloop (buf, 0, Feval, !NIL_P (printflag)); | |
531 unbind_to (count); | |
532 | |
533 return Qnil; | |
534 } | |
535 | |
536 #if 0 | |
341 | 537 DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "", |
538 "Execute the current buffer as Lisp code.\n\ | |
539 Programs can pass argument PRINTFLAG which controls printing of output:\n\ | |
540 nil means discard it; anything else is stream for print.\n\ | |
541 \n\ | |
542 If there is no error, point does not move. If there is an error,\n\ | |
543 point remains at the end of the last character read from the buffer.") | |
544 (printflag) | |
545 Lisp_Object printflag; | |
546 { | |
547 int count = specpdl_ptr - specpdl; | |
548 Lisp_Object tem; | |
549 | |
485 | 550 if (NILP (printflag)) |
341 | 551 tem = Qsymbolp; |
552 else | |
553 tem = printflag; | |
554 specbind (Qstandard_output, tem); | |
555 record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
556 SET_PT (BEGV); | |
485 | 557 readevalloop (Fcurrent_buffer (), 0, Feval, !NILP (printflag)); |
341 | 558 return unbind_to (count, Qnil); |
559 } | |
672 | 560 #endif |
341 | 561 |
562 DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r", | |
563 "Execute the region as Lisp code.\n\ | |
564 When called from programs, expects two arguments,\n\ | |
565 giving starting and ending indices in the current buffer\n\ | |
566 of the text to be executed.\n\ | |
567 Programs can pass third argument PRINTFLAG which controls output:\n\ | |
568 nil means discard it; anything else is stream for printing it.\n\ | |
569 \n\ | |
570 If there is no error, point does not move. If there is an error,\n\ | |
571 point remains at the end of the last character read from the buffer.") | |
572 (b, e, printflag) | |
573 Lisp_Object b, e, printflag; | |
574 { | |
575 int count = specpdl_ptr - specpdl; | |
576 Lisp_Object tem; | |
577 | |
485 | 578 if (NILP (printflag)) |
341 | 579 tem = Qsymbolp; |
580 else | |
581 tem = printflag; | |
582 specbind (Qstandard_output, tem); | |
583 | |
485 | 584 if (NILP (printflag)) |
341 | 585 record_unwind_protect (save_excursion_restore, save_excursion_save ()); |
586 record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
587 | |
588 /* This both uses b and checks its type. */ | |
589 Fgoto_char (b); | |
590 Fnarrow_to_region (make_number (BEGV), e); | |
485 | 591 readevalloop (Fcurrent_buffer (), 0, Feval, !NILP (printflag)); |
341 | 592 |
593 return unbind_to (count, Qnil); | |
594 } | |
595 | |
596 #endif /* standalone */ | |
597 | |
598 DEFUN ("read", Fread, Sread, 0, 1, 0, | |
599 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\ | |
600 If STREAM is nil, use the value of `standard-input' (which see).\n\ | |
601 STREAM or the value of `standard-input' may be:\n\ | |
602 a buffer (read from point and advance it)\n\ | |
603 a marker (read from where it points and advance it)\n\ | |
604 a function (call it with no arguments for each character,\n\ | |
605 call it with a char as argument to push a char back)\n\ | |
606 a string (takes text from string, starting at the beginning)\n\ | |
607 t (read text line using minibuffer and use it).") | |
608 (readcharfun) | |
609 Lisp_Object readcharfun; | |
610 { | |
611 extern Lisp_Object Fread_minibuffer (); | |
612 | |
485 | 613 if (NILP (readcharfun)) |
341 | 614 readcharfun = Vstandard_input; |
615 if (EQ (readcharfun, Qt)) | |
616 readcharfun = Qread_char; | |
617 | |
618 #ifndef standalone | |
619 if (EQ (readcharfun, Qread_char)) | |
620 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil); | |
621 #endif | |
622 | |
623 if (XTYPE (readcharfun) == Lisp_String) | |
624 return Fcar (Fread_from_string (readcharfun, Qnil, Qnil)); | |
625 | |
626 return read0 (readcharfun); | |
627 } | |
628 | |
629 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0, | |
630 "Read one Lisp expression which is represented as text by STRING.\n\ | |
631 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\ | |
632 START and END optionally delimit a substring of STRING from which to read;\n\ | |
633 they default to 0 and (length STRING) respectively.") | |
634 (string, start, end) | |
635 Lisp_Object string, start, end; | |
636 { | |
637 int startval, endval; | |
638 Lisp_Object tem; | |
639 | |
640 CHECK_STRING (string,0); | |
641 | |
485 | 642 if (NILP (end)) |
341 | 643 endval = XSTRING (string)->size; |
644 else | |
645 { CHECK_NUMBER (end,2); | |
646 endval = XINT (end); | |
647 if (endval < 0 || endval > XSTRING (string)->size) | |
648 args_out_of_range (string, end); | |
649 } | |
650 | |
485 | 651 if (NILP (start)) |
341 | 652 startval = 0; |
653 else | |
654 { CHECK_NUMBER (start,1); | |
655 startval = XINT (start); | |
656 if (startval < 0 || startval > endval) | |
657 args_out_of_range (string, start); | |
658 } | |
659 | |
660 read_from_string_index = startval; | |
661 read_from_string_limit = endval; | |
662 | |
663 tem = read0 (string); | |
664 return Fcons (tem, make_number (read_from_string_index)); | |
665 } | |
666 | |
667 /* Use this for recursive reads, in contexts where internal tokens are not allowed. */ | |
668 | |
669 static Lisp_Object | |
670 read0 (readcharfun) | |
671 Lisp_Object readcharfun; | |
672 { | |
673 register Lisp_Object val; | |
674 char c; | |
675 | |
676 val = read1 (readcharfun); | |
677 if (XTYPE (val) == Lisp_Internal) | |
678 { | |
679 c = XINT (val); | |
680 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil)); | |
681 } | |
682 | |
683 return val; | |
684 } | |
685 | |
686 static int read_buffer_size; | |
687 static char *read_buffer; | |
688 | |
689 static int | |
690 read_escape (readcharfun) | |
691 Lisp_Object readcharfun; | |
692 { | |
693 register int c = READCHAR; | |
694 switch (c) | |
695 { | |
696 case 'a': | |
485 | 697 return '\007'; |
341 | 698 case 'b': |
699 return '\b'; | |
700 case 'e': | |
701 return 033; | |
702 case 'f': | |
703 return '\f'; | |
704 case 'n': | |
705 return '\n'; | |
706 case 'r': | |
707 return '\r'; | |
708 case 't': | |
709 return '\t'; | |
710 case 'v': | |
711 return '\v'; | |
712 case '\n': | |
713 return -1; | |
714 | |
715 case 'M': | |
716 c = READCHAR; | |
717 if (c != '-') | |
718 error ("Invalid escape character syntax"); | |
719 c = READCHAR; | |
720 if (c == '\\') | |
721 c = read_escape (readcharfun); | |
722 return c | 0200; | |
723 | |
724 case 'C': | |
725 c = READCHAR; | |
726 if (c != '-') | |
727 error ("Invalid escape character syntax"); | |
728 case '^': | |
729 c = READCHAR; | |
730 if (c == '\\') | |
731 c = read_escape (readcharfun); | |
732 if (c == '?') | |
733 return 0177; | |
734 else | |
735 return (c & (0200 | 037)); | |
736 | |
737 case '0': | |
738 case '1': | |
739 case '2': | |
740 case '3': | |
741 case '4': | |
742 case '5': | |
743 case '6': | |
744 case '7': | |
745 /* An octal escape, as in ANSI C. */ | |
746 { | |
747 register int i = c - '0'; | |
748 register int count = 0; | |
749 while (++count < 3) | |
750 { | |
751 if ((c = READCHAR) >= '0' && c <= '7') | |
752 { | |
753 i *= 8; | |
754 i += c - '0'; | |
755 } | |
756 else | |
757 { | |
758 UNREAD (c); | |
759 break; | |
760 } | |
761 } | |
762 return i; | |
763 } | |
764 | |
765 case 'x': | |
766 /* A hex escape, as in ANSI C. */ | |
767 { | |
768 int i = 0; | |
769 while (1) | |
770 { | |
771 c = READCHAR; | |
772 if (c >= '0' && c <= '9') | |
773 { | |
774 i *= 16; | |
775 i += c - '0'; | |
776 } | |
777 else if ((c >= 'a' && c <= 'f') | |
778 || (c >= 'A' && c <= 'F')) | |
779 { | |
780 i *= 16; | |
781 if (c >= 'a' && c <= 'f') | |
782 i += c - 'a' + 10; | |
783 else | |
784 i += c - 'A' + 10; | |
785 } | |
786 else | |
787 { | |
788 UNREAD (c); | |
789 break; | |
790 } | |
791 } | |
792 return i; | |
793 } | |
794 | |
795 default: | |
796 return c; | |
797 } | |
798 } | |
799 | |
800 static Lisp_Object | |
801 read1 (readcharfun) | |
802 register Lisp_Object readcharfun; | |
803 { | |
804 register int c; | |
805 | |
806 retry: | |
807 | |
808 c = READCHAR; | |
809 if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
810 | |
811 switch (c) | |
812 { | |
813 case '(': | |
814 return read_list (0, readcharfun); | |
815 | |
816 case '[': | |
817 return read_vector (readcharfun); | |
818 | |
819 case ')': | |
820 case ']': | |
821 case '.': | |
822 { | |
823 register Lisp_Object val; | |
824 XSET (val, Lisp_Internal, c); | |
825 return val; | |
826 } | |
827 | |
828 case '#': | |
373
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
829 c = READCHAR; |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
830 if (c == '[') |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
831 { |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
832 /* Accept compiled functions at read-time so that we don't have to |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
833 build them using function calls. */ |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
834 Lisp_Object tmp = read_vector (readcharfun); |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
835 return Fmake_byte_code (XVECTOR(tmp)->size, XVECTOR (tmp)->contents); |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
836 } |
7c6f74ef31a3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
364
diff
changeset
|
837 UNREAD (c); |
341 | 838 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil)); |
839 | |
840 case ';': | |
841 while ((c = READCHAR) >= 0 && c != '\n'); | |
842 goto retry; | |
843 | |
844 case '\'': | |
845 { | |
846 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil)); | |
847 } | |
848 | |
849 case '?': | |
850 { | |
851 register Lisp_Object val; | |
852 | |
853 c = READCHAR; | |
854 if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
855 | |
856 if (c == '\\') | |
857 XSET (val, Lisp_Int, read_escape (readcharfun)); | |
858 else | |
859 XSET (val, Lisp_Int, c); | |
860 | |
861 return val; | |
862 } | |
863 | |
864 case '\"': | |
865 { | |
866 register char *p = read_buffer; | |
867 register char *end = read_buffer + read_buffer_size; | |
868 register int c; | |
869 int cancel = 0; | |
870 | |
871 while ((c = READCHAR) >= 0 | |
872 && c != '\"') | |
873 { | |
874 if (p == end) | |
875 { | |
876 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
877 p += new - read_buffer; | |
878 read_buffer += new - read_buffer; | |
879 end = read_buffer + read_buffer_size; | |
880 } | |
881 if (c == '\\') | |
882 c = read_escape (readcharfun); | |
883 /* c is -1 if \ newline has just been seen */ | |
884 if (c < 0) | |
885 { | |
886 if (p == read_buffer) | |
887 cancel = 1; | |
888 } | |
889 else | |
890 *p++ = c; | |
891 } | |
892 if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
893 | |
894 /* If purifying, and string starts with \ newline, | |
895 return zero instead. This is for doc strings | |
604 | 896 that we are really going to find in etc/DOC.nn.nn */ |
485 | 897 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel) |
341 | 898 return make_number (0); |
899 | |
900 if (read_pure) | |
901 return make_pure_string (read_buffer, p - read_buffer); | |
902 else | |
903 return make_string (read_buffer, p - read_buffer); | |
904 } | |
905 | |
906 default: | |
907 if (c <= 040) goto retry; | |
908 { | |
909 register char *p = read_buffer; | |
910 | |
911 { | |
912 register char *end = read_buffer + read_buffer_size; | |
913 | |
914 while (c > 040 && | |
915 !(c == '\"' || c == '\'' || c == ';' || c == '?' | |
916 || c == '(' || c == ')' | |
917 #ifndef LISP_FLOAT_TYPE /* we need to see <number><dot><number> */ | |
918 || c =='.' | |
919 #endif /* not LISP_FLOAT_TYPE */ | |
920 || c == '[' || c == ']' || c == '#' | |
921 )) | |
922 { | |
923 if (p == end) | |
924 { | |
925 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
926 p += new - read_buffer; | |
927 read_buffer += new - read_buffer; | |
928 end = read_buffer + read_buffer_size; | |
929 } | |
930 if (c == '\\') | |
931 c = READCHAR; | |
932 *p++ = c; | |
933 c = READCHAR; | |
934 } | |
935 | |
936 if (p == end) | |
937 { | |
938 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
939 p += new - read_buffer; | |
940 read_buffer += new - read_buffer; | |
941 /* end = read_buffer + read_buffer_size; */ | |
942 } | |
943 *p = 0; | |
944 if (c >= 0) | |
945 UNREAD (c); | |
946 } | |
947 | |
948 /* Is it an integer? */ | |
949 { | |
950 register char *p1; | |
951 register Lisp_Object val; | |
952 p1 = read_buffer; | |
953 if (*p1 == '+' || *p1 == '-') p1++; | |
954 if (p1 != p) | |
955 { | |
956 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++; | |
957 if (p1 == p) | |
958 /* It is. */ | |
959 { | |
960 XSET (val, Lisp_Int, atoi (read_buffer)); | |
961 return val; | |
962 } | |
963 } | |
964 #ifdef LISP_FLOAT_TYPE | |
965 if (isfloat_string (read_buffer)) | |
966 return make_float (atof (read_buffer)); | |
967 #endif | |
968 } | |
969 | |
970 return intern (read_buffer); | |
971 } | |
972 } | |
973 } | |
974 | |
975 #ifdef LISP_FLOAT_TYPE | |
976 | |
977 #include <ctype.h> | |
978 #define LEAD_INT 1 | |
979 #define DOT_CHAR 2 | |
980 #define TRAIL_INT 4 | |
981 #define E_CHAR 8 | |
982 #define EXP_INT 16 | |
983 | |
984 int | |
985 isfloat_string (cp) | |
986 register char *cp; | |
987 { | |
988 register state; | |
989 | |
990 state = 0; | |
991 if (*cp == '+' || *cp == '-') | |
992 cp++; | |
993 | |
994 if (isdigit(*cp)) | |
995 { | |
996 state |= LEAD_INT; | |
997 while (isdigit (*cp)) | |
998 cp ++; | |
999 } | |
1000 if (*cp == '.') | |
1001 { | |
1002 state |= DOT_CHAR; | |
1003 cp++; | |
1004 } | |
1005 if (isdigit(*cp)) | |
1006 { | |
1007 state |= TRAIL_INT; | |
1008 while (isdigit (*cp)) | |
1009 cp++; | |
1010 } | |
1011 if (*cp == 'e') | |
1012 { | |
1013 state |= E_CHAR; | |
1014 cp++; | |
1015 } | |
1016 if ((*cp == '+') || (*cp == '-')) | |
1017 cp++; | |
1018 | |
1019 if (isdigit (*cp)) | |
1020 { | |
1021 state |= EXP_INT; | |
1022 while (isdigit (*cp)) | |
1023 cp++; | |
1024 } | |
1025 return (*cp == 0 | |
1026 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT) | |
1027 || state == (LEAD_INT|E_CHAR|EXP_INT) | |
1028 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT))); | |
1029 } | |
1030 #endif /* LISP_FLOAT_TYPE */ | |
1031 | |
1032 static Lisp_Object | |
1033 read_vector (readcharfun) | |
1034 Lisp_Object readcharfun; | |
1035 { | |
1036 register int i; | |
1037 register int size; | |
1038 register Lisp_Object *ptr; | |
1039 register Lisp_Object tem, vector; | |
1040 register struct Lisp_Cons *otem; | |
1041 Lisp_Object len; | |
1042 | |
1043 tem = read_list (1, readcharfun); | |
1044 len = Flength (tem); | |
1045 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil)); | |
1046 | |
1047 | |
1048 size = XVECTOR (vector)->size; | |
1049 ptr = XVECTOR (vector)->contents; | |
1050 for (i = 0; i < size; i++) | |
1051 { | |
1052 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem); | |
1053 otem = XCONS (tem); | |
1054 tem = Fcdr (tem); | |
1055 free_cons (otem); | |
1056 } | |
1057 return vector; | |
1058 } | |
1059 | |
1060 /* flag = 1 means check for ] to terminate rather than ) and . | |
1061 flag = -1 means check for starting with defun | |
1062 and make structure pure. */ | |
1063 | |
1064 static Lisp_Object | |
1065 read_list (flag, readcharfun) | |
1066 int flag; | |
1067 register Lisp_Object readcharfun; | |
1068 { | |
1069 /* -1 means check next element for defun, | |
1070 0 means don't check, | |
1071 1 means already checked and found defun. */ | |
1072 int defunflag = flag < 0 ? -1 : 0; | |
1073 Lisp_Object val, tail; | |
1074 register Lisp_Object elt, tem; | |
1075 struct gcpro gcpro1, gcpro2; | |
1076 | |
1077 val = Qnil; | |
1078 tail = Qnil; | |
1079 | |
1080 while (1) | |
1081 { | |
1082 GCPRO2 (val, tail); | |
1083 elt = read1 (readcharfun); | |
1084 UNGCPRO; | |
1085 if (XTYPE (elt) == Lisp_Internal) | |
1086 { | |
1087 if (flag > 0) | |
1088 { | |
1089 if (XINT (elt) == ']') | |
1090 return val; | |
1091 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil)); | |
1092 } | |
1093 if (XINT (elt) == ')') | |
1094 return val; | |
1095 if (XINT (elt) == '.') | |
1096 { | |
1097 GCPRO2 (val, tail); | |
485 | 1098 if (!NILP (tail)) |
341 | 1099 XCONS (tail)->cdr = read0 (readcharfun); |
1100 else | |
1101 val = read0 (readcharfun); | |
1102 elt = read1 (readcharfun); | |
1103 UNGCPRO; | |
1104 if (XTYPE (elt) == Lisp_Internal && XINT (elt) == ')') | |
1105 return val; | |
1106 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil)); | |
1107 } | |
1108 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil)); | |
1109 } | |
1110 tem = (read_pure && flag <= 0 | |
1111 ? pure_cons (elt, Qnil) | |
1112 : Fcons (elt, Qnil)); | |
485 | 1113 if (!NILP (tail)) |
341 | 1114 XCONS (tail)->cdr = tem; |
1115 else | |
1116 val = tem; | |
1117 tail = tem; | |
1118 if (defunflag < 0) | |
1119 defunflag = EQ (elt, Qdefun); | |
1120 else if (defunflag > 0) | |
1121 read_pure = 1; | |
1122 } | |
1123 } | |
1124 | |
1125 Lisp_Object Vobarray; | |
1126 Lisp_Object initial_obarray; | |
1127 | |
1128 Lisp_Object | |
1129 check_obarray (obarray) | |
1130 Lisp_Object obarray; | |
1131 { | |
1132 while (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0) | |
1133 { | |
1134 /* If Vobarray is now invalid, force it to be valid. */ | |
1135 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray; | |
1136 | |
1137 obarray = wrong_type_argument (Qvectorp, obarray); | |
1138 } | |
1139 return obarray; | |
1140 } | |
1141 | |
1142 static int hash_string (); | |
1143 Lisp_Object oblookup (); | |
1144 | |
1145 Lisp_Object | |
1146 intern (str) | |
1147 char *str; | |
1148 { | |
1149 Lisp_Object tem; | |
1150 int len = strlen (str); | |
1151 Lisp_Object obarray = Vobarray; | |
1152 | |
1153 if (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0) | |
1154 obarray = check_obarray (obarray); | |
1155 tem = oblookup (obarray, str, len); | |
1156 if (XTYPE (tem) == Lisp_Symbol) | |
1157 return tem; | |
485 | 1158 return Fintern ((!NILP (Vpurify_flag) |
341 | 1159 ? make_pure_string (str, len) |
1160 : make_string (str, len)), | |
1161 obarray); | |
1162 } | |
1163 | |
1164 DEFUN ("intern", Fintern, Sintern, 1, 2, 0, | |
1165 "Return the canonical symbol whose name is STRING.\n\ | |
1166 If there is none, one is created by this function and returned.\n\ | |
1167 A second optional argument specifies the obarray to use;\n\ | |
1168 it defaults to the value of `obarray'.") | |
1169 (str, obarray) | |
1170 Lisp_Object str, obarray; | |
1171 { | |
1172 register Lisp_Object tem, sym, *ptr; | |
1173 | |
485 | 1174 if (NILP (obarray)) obarray = Vobarray; |
341 | 1175 obarray = check_obarray (obarray); |
1176 | |
1177 CHECK_STRING (str, 0); | |
1178 | |
1179 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size); | |
1180 if (XTYPE (tem) != Lisp_Int) | |
1181 return tem; | |
1182 | |
485 | 1183 if (!NILP (Vpurify_flag)) |
341 | 1184 str = Fpurecopy (str); |
1185 sym = Fmake_symbol (str); | |
1186 | |
1187 ptr = &XVECTOR (obarray)->contents[XINT (tem)]; | |
1188 if (XTYPE (*ptr) == Lisp_Symbol) | |
1189 XSYMBOL (sym)->next = XSYMBOL (*ptr); | |
1190 else | |
1191 XSYMBOL (sym)->next = 0; | |
1192 *ptr = sym; | |
1193 return sym; | |
1194 } | |
1195 | |
1196 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0, | |
1197 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\ | |
1198 A second optional argument specifies the obarray to use;\n\ | |
1199 it defaults to the value of `obarray'.") | |
1200 (str, obarray) | |
1201 Lisp_Object str, obarray; | |
1202 { | |
1203 register Lisp_Object tem; | |
1204 | |
485 | 1205 if (NILP (obarray)) obarray = Vobarray; |
341 | 1206 obarray = check_obarray (obarray); |
1207 | |
1208 CHECK_STRING (str, 0); | |
1209 | |
1210 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size); | |
1211 if (XTYPE (tem) != Lisp_Int) | |
1212 return tem; | |
1213 return Qnil; | |
1214 } | |
1215 | |
1216 Lisp_Object | |
1217 oblookup (obarray, ptr, size) | |
1218 Lisp_Object obarray; | |
1219 register char *ptr; | |
1220 register int size; | |
1221 { | |
1222 int hash, obsize; | |
1223 register Lisp_Object tail; | |
1224 Lisp_Object bucket, tem; | |
1225 | |
1226 if (XTYPE (obarray) != Lisp_Vector || | |
1227 (obsize = XVECTOR (obarray)->size) == 0) | |
1228 { | |
1229 obarray = check_obarray (obarray); | |
1230 obsize = XVECTOR (obarray)->size; | |
1231 } | |
1232 /* Combining next two lines breaks VMS C 2.3. */ | |
1233 hash = hash_string (ptr, size); | |
1234 hash %= obsize; | |
1235 bucket = XVECTOR (obarray)->contents[hash]; | |
1236 if (XFASTINT (bucket) == 0) | |
1237 ; | |
1238 else if (XTYPE (bucket) != Lisp_Symbol) | |
1239 error ("Bad data in guts of obarray"); /* Like CADR error message */ | |
1240 else for (tail = bucket; ; XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next)) | |
1241 { | |
1242 if (XSYMBOL (tail)->name->size == size && | |
1243 !bcmp (XSYMBOL (tail)->name->data, ptr, size)) | |
1244 return tail; | |
1245 else if (XSYMBOL (tail)->next == 0) | |
1246 break; | |
1247 } | |
1248 XSET (tem, Lisp_Int, hash); | |
1249 return tem; | |
1250 } | |
1251 | |
1252 static int | |
1253 hash_string (ptr, len) | |
1254 unsigned char *ptr; | |
1255 int len; | |
1256 { | |
1257 register unsigned char *p = ptr; | |
1258 register unsigned char *end = p + len; | |
1259 register unsigned char c; | |
1260 register int hash = 0; | |
1261 | |
1262 while (p != end) | |
1263 { | |
1264 c = *p++; | |
1265 if (c >= 0140) c -= 40; | |
1266 hash = ((hash<<3) + (hash>>28) + c); | |
1267 } | |
1268 return hash & 07777777777; | |
1269 } | |
1270 | |
1271 void | |
1272 map_obarray (obarray, fn, arg) | |
1273 Lisp_Object obarray; | |
1274 int (*fn) (); | |
1275 Lisp_Object arg; | |
1276 { | |
1277 register int i; | |
1278 register Lisp_Object tail; | |
1279 CHECK_VECTOR (obarray, 1); | |
1280 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--) | |
1281 { | |
1282 tail = XVECTOR (obarray)->contents[i]; | |
1283 if (XFASTINT (tail) != 0) | |
1284 while (1) | |
1285 { | |
1286 (*fn) (tail, arg); | |
1287 if (XSYMBOL (tail)->next == 0) | |
1288 break; | |
1289 XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next); | |
1290 } | |
1291 } | |
1292 } | |
1293 | |
1294 mapatoms_1 (sym, function) | |
1295 Lisp_Object sym, function; | |
1296 { | |
1297 call1 (function, sym); | |
1298 } | |
1299 | |
1300 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0, | |
1301 "Call FUNCTION on every symbol in OBARRAY.\n\ | |
1302 OBARRAY defaults to the value of `obarray'.") | |
1303 (function, obarray) | |
1304 Lisp_Object function, obarray; | |
1305 { | |
1306 Lisp_Object tem; | |
1307 | |
485 | 1308 if (NILP (obarray)) obarray = Vobarray; |
341 | 1309 obarray = check_obarray (obarray); |
1310 | |
1311 map_obarray (obarray, mapatoms_1, function); | |
1312 return Qnil; | |
1313 } | |
1314 | |
1315 #define OBARRAY_SIZE 509 | |
1316 | |
1317 void | |
1318 init_obarray () | |
1319 { | |
1320 Lisp_Object oblength; | |
1321 int hash; | |
1322 Lisp_Object *tem; | |
1323 | |
1324 XFASTINT (oblength) = OBARRAY_SIZE; | |
1325 | |
1326 Qnil = Fmake_symbol (make_pure_string ("nil", 3)); | |
1327 Vobarray = Fmake_vector (oblength, make_number (0)); | |
1328 initial_obarray = Vobarray; | |
1329 staticpro (&initial_obarray); | |
1330 /* Intern nil in the obarray */ | |
1331 /* These locals are to kludge around a pyramid compiler bug. */ | |
1332 hash = hash_string ("nil", 3); | |
1333 /* Separate statement here to avoid VAXC bug. */ | |
1334 hash %= OBARRAY_SIZE; | |
1335 tem = &XVECTOR (Vobarray)->contents[hash]; | |
1336 *tem = Qnil; | |
1337 | |
1338 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7)); | |
1339 XSYMBOL (Qnil)->function = Qunbound; | |
1340 XSYMBOL (Qunbound)->value = Qunbound; | |
1341 XSYMBOL (Qunbound)->function = Qunbound; | |
1342 | |
1343 Qt = intern ("t"); | |
1344 XSYMBOL (Qnil)->value = Qnil; | |
1345 XSYMBOL (Qnil)->plist = Qnil; | |
1346 XSYMBOL (Qt)->value = Qt; | |
1347 | |
1348 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */ | |
1349 Vpurify_flag = Qt; | |
1350 | |
1351 Qvariable_documentation = intern ("variable-documentation"); | |
1352 | |
1353 read_buffer_size = 100; | |
1354 read_buffer = (char *) malloc (read_buffer_size); | |
1355 } | |
1356 | |
1357 void | |
1358 defsubr (sname) | |
1359 struct Lisp_Subr *sname; | |
1360 { | |
1361 Lisp_Object sym; | |
1362 sym = intern (sname->symbol_name); | |
1363 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname); | |
1364 } | |
1365 | |
1366 #ifdef NOTDEF /* use fset in subr.el now */ | |
1367 void | |
1368 defalias (sname, string) | |
1369 struct Lisp_Subr *sname; | |
1370 char *string; | |
1371 { | |
1372 Lisp_Object sym; | |
1373 sym = intern (string); | |
1374 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname); | |
1375 } | |
1376 #endif /* NOTDEF */ | |
1377 | |
1378 /* New replacement for DefIntVar; it ignores the doc string argument | |
1379 on the assumption that make-docfile will handle that. */ | |
1380 /* Define an "integer variable"; a symbol whose value is forwarded | |
1381 to a C variable of type int. Sample call: */ | |
1382 /* DEFVARINT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */ | |
1383 | |
1384 void | |
1385 defvar_int (namestring, address, doc) | |
1386 char *namestring; | |
1387 int *address; | |
1388 char *doc; | |
1389 { | |
1390 Lisp_Object sym; | |
1391 sym = intern (namestring); | |
1392 XSET (XSYMBOL (sym)->value, Lisp_Intfwd, address); | |
1393 } | |
1394 | |
1395 /* Similar but define a variable whose value is T if address contains 1, | |
1396 NIL if address contains 0 */ | |
1397 | |
1398 void | |
1399 defvar_bool (namestring, address, doc) | |
1400 char *namestring; | |
1401 int *address; | |
1402 char *doc; | |
1403 { | |
1404 Lisp_Object sym; | |
1405 sym = intern (namestring); | |
1406 XSET (XSYMBOL (sym)->value, Lisp_Boolfwd, address); | |
1407 } | |
1408 | |
1409 /* Similar but define a variable whose value is the Lisp Object stored at address. */ | |
1410 | |
1411 void | |
1412 defvar_lisp (namestring, address, doc) | |
1413 char *namestring; | |
1414 Lisp_Object *address; | |
1415 char *doc; | |
1416 { | |
1417 Lisp_Object sym; | |
1418 sym = intern (namestring); | |
1419 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address); | |
1420 staticpro (address); | |
1421 } | |
1422 | |
1423 /* Similar but don't request gc-marking of the C variable. | |
1424 Used when that variable will be gc-marked for some other reason, | |
1425 since marking the same slot twice can cause trouble with strings. */ | |
1426 | |
1427 void | |
1428 defvar_lisp_nopro (namestring, address, doc) | |
1429 char *namestring; | |
1430 Lisp_Object *address; | |
1431 char *doc; | |
1432 { | |
1433 Lisp_Object sym; | |
1434 sym = intern (namestring); | |
1435 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address); | |
1436 } | |
1437 | |
1438 #ifndef standalone | |
1439 | |
1440 /* Similar but define a variable whose value is the Lisp Object stored in | |
1441 the current buffer. address is the address of the slot in the buffer that is current now. */ | |
1442 | |
1443 void | |
1444 defvar_per_buffer (namestring, address, doc) | |
1445 char *namestring; | |
1446 Lisp_Object *address; | |
1447 char *doc; | |
1448 { | |
1449 Lisp_Object sym; | |
1450 int offset; | |
1451 extern struct buffer buffer_local_symbols; | |
1452 | |
1453 sym = intern (namestring); | |
1454 offset = (char *)address - (char *)current_buffer; | |
1455 | |
1456 XSET (XSYMBOL (sym)->value, Lisp_Buffer_Objfwd, | |
1457 (Lisp_Object *) offset); | |
1458 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym; | |
1459 if (*(int *)(offset + (char *)&buffer_local_flags) == 0) | |
1460 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding | |
1461 slot of buffer_local_flags */ | |
1462 abort (); | |
1463 } | |
1464 | |
1465 #endif /* standalone */ | |
1466 | |
364 | 1467 init_lread () |
341 | 1468 { |
617 | 1469 char *normal; |
341 | 1470 |
364 | 1471 /* Compute the default load-path. */ |
617 | 1472 #ifdef CANNOT_DUMP |
1473 normal = PATH_LOADSEARCH; | |
638 | 1474 Vload_path = decode_env_path (0, normal); |
617 | 1475 #else |
1476 if (NILP (Vpurify_flag)) | |
1477 normal = PATH_LOADSEARCH; | |
1478 else | |
1479 normal = PATH_DUMPLOADSEARCH; | |
1480 | |
1481 /* In a dumped Emacs, we normally have to reset the value of | |
1482 Vload_path from PATH_LOADSEARCH, since the value that was dumped | |
1483 uses ../lisp, instead of the path of the installed elisp | |
1484 libraries. However, if it appears that Vload_path was changed | |
1485 from the default before dumping, don't override that value. */ | |
621 | 1486 if (initialized) |
1487 { | |
1488 Lisp_Object dump_path; | |
617 | 1489 |
638 | 1490 dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH); |
621 | 1491 if (! NILP (Fequal (dump_path, Vload_path))) |
638 | 1492 Vload_path = decode_env_path (0, normal); |
621 | 1493 } |
1494 else | |
638 | 1495 Vload_path = decode_env_path (0, normal); |
364 | 1496 #endif |
1497 | |
341 | 1498 /* Warn if dirs in the *standard* path don't exist. */ |
617 | 1499 { |
1500 Lisp_Object path_tail; | |
341 | 1501 |
617 | 1502 for (path_tail = Vload_path; |
1503 !NILP (path_tail); | |
1504 path_tail = XCONS (path_tail)->cdr) | |
1505 { | |
1506 Lisp_Object dirfile; | |
1507 dirfile = Fcar (path_tail); | |
1508 if (XTYPE (dirfile) == Lisp_String) | |
1509 { | |
1510 dirfile = Fdirectory_file_name (dirfile); | |
1511 if (access (XSTRING (dirfile)->data, 0) < 0) | |
1512 printf ("Warning: lisp library (%s) does not exist.\n", | |
1513 XSTRING (Fcar (path_tail))->data); | |
1514 } | |
1515 } | |
1516 } | |
1517 | |
1518 /* If the EMACSLOADPATH environment variable is set, use its value. | |
1519 This doesn't apply if we're dumping. */ | |
1520 if (NILP (Vpurify_flag) | |
1521 && egetenv ("EMACSLOADPATH")) | |
364 | 1522 Vload_path = decode_env_path ("EMACSLOADPATH", normal); |
1523 | |
1524 Vvalues = Qnil; | |
1525 | |
341 | 1526 load_in_progress = 0; |
1527 } | |
1528 | |
1529 void | |
364 | 1530 syms_of_lread () |
341 | 1531 { |
1532 defsubr (&Sread); | |
1533 defsubr (&Sread_from_string); | |
1534 defsubr (&Sintern); | |
1535 defsubr (&Sintern_soft); | |
1536 defsubr (&Sload); | |
672 | 1537 defsubr (&Seval_buffer); |
341 | 1538 defsubr (&Seval_region); |
1539 defsubr (&Sread_char); | |
1540 defsubr (&Sread_char_exclusive); | |
1541 #ifdef HAVE_X_WINDOWS | |
1542 defsubr (&Sread_event); | |
1543 #endif /* HAVE_X_WINDOWS */ | |
1544 defsubr (&Sget_file_char); | |
1545 defsubr (&Smapatoms); | |
1546 | |
1547 DEFVAR_LISP ("obarray", &Vobarray, | |
1548 "Symbol table for use by `intern' and `read'.\n\ | |
1549 It is a vector whose length ought to be prime for best results.\n\ | |
1550 The vector's contents don't make sense if examined from Lisp programs;\n\ | |
1551 to find all the symbols in an obarray, use `mapatoms'."); | |
1552 | |
1553 DEFVAR_LISP ("values", &Vvalues, | |
1554 "List of values of all expressions which were read, evaluated and printed.\n\ | |
1555 Order is reverse chronological."); | |
1556 | |
1557 DEFVAR_LISP ("standard-input", &Vstandard_input, | |
1558 "Stream for read to get input from.\n\ | |
1559 See documentation of `read' for possible values."); | |
1560 Vstandard_input = Qt; | |
1561 | |
1562 DEFVAR_LISP ("load-path", &Vload_path, | |
1563 "*List of directories to search for files to load.\n\ | |
1564 Each element is a string (directory name) or nil (try default directory).\n\ | |
1565 Initialized based on EMACSLOADPATH environment variable, if any,\n\ | |
1566 otherwise to default specified in by file `paths.h' when Emacs was built."); | |
1567 | |
1568 DEFVAR_BOOL ("load-in-progress", &load_in_progress, | |
1569 "Non-nil iff inside of `load'."); | |
1570 | |
1571 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist, | |
1572 "An alist of expressions to be evalled when particular files are loaded.\n\ | |
1573 Each element looks like (FILENAME FORMS...).\n\ | |
1574 When `load' is run and the file-name argument is FILENAME,\n\ | |
1575 the FORMS in the corresponding element are executed at the end of loading.\n\n\ | |
1576 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\ | |
1577 with no directory specified, since that is how `load' is normally called.\n\ | |
1578 An error in FORMS does not undo the load,\n\ | |
1579 but does prevent execution of the rest of the FORMS."); | |
1580 Vafter_load_alist = Qnil; | |
1581 | |
1582 Qstandard_input = intern ("standard-input"); | |
1583 staticpro (&Qstandard_input); | |
1584 | |
1585 Qread_char = intern ("read-char"); | |
1586 staticpro (&Qread_char); | |
1587 | |
1588 Qget_file_char = intern ("get-file-char"); | |
1589 staticpro (&Qget_file_char); | |
1590 } |