Commit | Line | Data |
---|---|---|
078e7b4a | 1 | /* Lisp parsing and input streams. |
31c8f881 | 2 | Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 1998 |
33046fc9 | 3 | Free Software Foundation, Inc. |
078e7b4a JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
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 | |
46947372 | 9 | the Free Software Foundation; either version 2, or (at your option) |
078e7b4a JB |
10 | any later version. |
11 | ||
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. | |
16 | ||
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 | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
078e7b4a JB |
21 | |
22 | ||
98280b76 | 23 | #include <config.h> |
078e7b4a JB |
24 | #include <stdio.h> |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/file.h> | |
2c1b5dbe | 28 | #include <errno.h> |
078e7b4a JB |
29 | #include "lisp.h" |
30 | ||
31 | #ifndef standalone | |
32 | #include "buffer.h" | |
fe0e03f3 | 33 | #include "charset.h" |
2a6b3537 | 34 | #include <paths.h> |
078e7b4a | 35 | #include "commands.h" |
e37c0805 | 36 | #include "keyboard.h" |
7bd279cd | 37 | #include "termhooks.h" |
078e7b4a JB |
38 | #endif |
39 | ||
40 | #ifdef lint | |
41 | #include <sys/inode.h> | |
42 | #endif /* lint */ | |
43 | ||
79051982 RS |
44 | #ifdef MSDOS |
45 | #if __DJGPP__ < 2 | |
46 | #include <unistd.h> /* to get X_OK */ | |
47 | #endif | |
48 | #include "msdos.h" | |
49 | #endif | |
50 | ||
078e7b4a JB |
51 | #ifndef X_OK |
52 | #define X_OK 01 | |
53 | #endif | |
54 | ||
55 | #ifdef LISP_FLOAT_TYPE | |
93b91208 JB |
56 | #ifdef STDC_HEADERS |
57 | #include <stdlib.h> | |
58 | #endif | |
23a71bd6 | 59 | |
078e7b4a JB |
60 | #include <math.h> |
61 | #endif /* LISP_FLOAT_TYPE */ | |
62 | ||
c011e9a5 RS |
63 | #ifdef HAVE_SETLOCALE |
64 | #include <locale.h> | |
65 | #endif /* HAVE_SETLOCALE */ | |
66 | ||
f7d279f0 RS |
67 | #ifndef O_RDONLY |
68 | #define O_RDONLY 0 | |
69 | #endif | |
70 | ||
2c1b5dbe KH |
71 | extern int errno; |
72 | ||
8a1f1537 | 73 | Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list; |
078e7b4a | 74 | Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist; |
20ea2964 | 75 | Lisp_Object Qascii_character, Qload, Qload_file_name; |
2b6cae0c | 76 | Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction; |
74549846 | 77 | Lisp_Object Qinhibit_file_name_operation; |
7bd279cd RS |
78 | |
79 | extern Lisp_Object Qevent_symbol_element_mask; | |
74549846 | 80 | extern Lisp_Object Qfile_exists_p; |
078e7b4a JB |
81 | |
82 | /* non-zero if inside `load' */ | |
83 | int load_in_progress; | |
84 | ||
1521a8fa RS |
85 | /* Directory in which the sources were found. */ |
86 | Lisp_Object Vsource_directory; | |
87 | ||
078e7b4a JB |
88 | /* Search path for files to be loaded. */ |
89 | Lisp_Object Vload_path; | |
90 | ||
ae321d28 RS |
91 | /* This is the user-visible association list that maps features to |
92 | lists of defs in their load files. */ | |
93 | Lisp_Object Vload_history; | |
94 | ||
20ea2964 | 95 | /* This is used to build the load history. */ |
ae321d28 RS |
96 | Lisp_Object Vcurrent_load_list; |
97 | ||
4b104c41 RS |
98 | /* List of files that were preloaded. */ |
99 | Lisp_Object Vpreloaded_file_list; | |
100 | ||
20ea2964 RS |
101 | /* Name of file actually being read by `load'. */ |
102 | Lisp_Object Vload_file_name; | |
103 | ||
84a15045 RS |
104 | /* Function to use for reading, in `load' and friends. */ |
105 | Lisp_Object Vload_read_function; | |
106 | ||
4ad679f9 EN |
107 | /* The association list of objects read with the #n=object form. |
108 | Each member of the list has the form (n . object), and is used to | |
109 | look up the object for the corresponding #n# construct. | |
110 | It must be set to nil before all top-level calls to read0. */ | |
111 | Lisp_Object read_objects; | |
112 | ||
b2a30870 RS |
113 | /* Nonzero means load should forcibly load all dynamic doc strings. */ |
114 | static int load_force_doc_strings; | |
115 | ||
fe0e03f3 KH |
116 | /* Function to use for loading an Emacs lisp source file (not |
117 | compiled) instead of readevalloop. */ | |
118 | Lisp_Object Vload_source_file_function; | |
119 | ||
d2c6be7f RS |
120 | /* List of descriptors now open for Fload. */ |
121 | static Lisp_Object load_descriptor_list; | |
122 | ||
b2a30870 | 123 | /* File for get_file_char to read from. Use by load. */ |
078e7b4a JB |
124 | static FILE *instream; |
125 | ||
126 | /* When nonzero, read conses in pure space */ | |
127 | static int read_pure; | |
128 | ||
b2a30870 | 129 | /* For use within read-from-string (this reader is non-reentrant!!) */ |
078e7b4a JB |
130 | static int read_from_string_index; |
131 | static int read_from_string_limit; | |
17634846 | 132 | |
6f7f43d5 RS |
133 | /* Number of bytes left to read in the buffer character |
134 | that `readchar' has already advanced over. */ | |
135 | static int readchar_backlog; | |
136 | ||
d49f0c1a KH |
137 | /* This contains the last string skipped with #@, but only on some systems. |
138 | On other systems we can't put the string here. */ | |
b2a30870 RS |
139 | static char *saved_doc_string; |
140 | /* Length of buffer allocated in saved_doc_string. */ | |
141 | static int saved_doc_string_size; | |
142 | /* Length of actual data in saved_doc_string. */ | |
143 | static int saved_doc_string_length; | |
144 | /* This is the file position that string came from. */ | |
145 | static int saved_doc_string_position; | |
146 | ||
17634846 RS |
147 | /* Nonzero means inside a new-style backquote |
148 | with no surrounding parentheses. | |
149 | Fread initializes this to zero, so we need not specbind it | |
150 | or worry about what happens to it when there is an error. */ | |
151 | static int new_backquote_flag; | |
078e7b4a JB |
152 | \f |
153 | /* Handle unreading and rereading of characters. | |
154 | Write READCHAR to read a character, | |
fe0e03f3 KH |
155 | UNREAD(c) to unread c to be read again. |
156 | ||
157 | These macros actually read/unread a byte code, multibyte characters | |
158 | are not handled here. The caller should manage them if necessary. | |
159 | */ | |
078e7b4a JB |
160 | |
161 | #define READCHAR readchar (readcharfun) | |
162 | #define UNREAD(c) unreadchar (readcharfun, c) | |
163 | ||
164 | static int | |
165 | readchar (readcharfun) | |
166 | Lisp_Object readcharfun; | |
167 | { | |
168 | Lisp_Object tem; | |
169 | register struct buffer *inbuffer; | |
170 | register int c, mpos; | |
171 | ||
cfff016d | 172 | if (BUFFERP (readcharfun)) |
078e7b4a JB |
173 | { |
174 | inbuffer = XBUFFER (readcharfun); | |
175 | ||
6f7f43d5 RS |
176 | if (readchar_backlog == 0) |
177 | { | |
178 | int pt_byte = BUF_PT_BYTE (inbuffer); | |
179 | int orig_pt_byte = pt_byte; | |
180 | ||
181 | if (pt_byte >= BUF_ZV_BYTE (inbuffer)) | |
182 | return -1; | |
078e7b4a | 183 | |
6f7f43d5 RS |
184 | if (! NILP (inbuffer->enable_multibyte_characters)) |
185 | BUF_INC_POS (inbuffer, pt_byte); | |
186 | else | |
187 | pt_byte++; | |
188 | SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte); | |
189 | readchar_backlog = pt_byte - orig_pt_byte; | |
190 | } | |
191 | ||
192 | /* We get the address of the byte just passed, | |
193 | which is the last byte of the character. | |
194 | The other bytes in this character are consecutive with it, | |
195 | because the gap can't be in the middle of a character. */ | |
196 | return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1) | |
197 | - --readchar_backlog); | |
078e7b4a | 198 | } |
cfff016d | 199 | if (MARKERP (readcharfun)) |
078e7b4a JB |
200 | { |
201 | inbuffer = XMARKER (readcharfun)->buffer; | |
202 | ||
6f7f43d5 RS |
203 | if (readchar_backlog == 0) |
204 | { | |
205 | int bytepos = marker_byte_position (readcharfun); | |
206 | int orig_bytepos = bytepos; | |
078e7b4a | 207 | |
6f7f43d5 RS |
208 | if (bytepos >= BUF_ZV_BYTE (inbuffer)) |
209 | return -1; | |
210 | ||
6f7f43d5 RS |
211 | if (! NILP (inbuffer->enable_multibyte_characters)) |
212 | INC_POS (bytepos); | |
213 | else | |
214 | bytepos++; | |
a3123c13 | 215 | XMARKER (readcharfun)->bytepos = bytepos; |
6f7f43d5 RS |
216 | XMARKER (readcharfun)->charpos++; |
217 | ||
218 | readchar_backlog = bytepos - orig_bytepos; | |
219 | } | |
220 | ||
a3123c13 | 221 | return *(BUF_BEG_ADDR (inbuffer) + XMARKER (readcharfun)->bytepos |
6f7f43d5 | 222 | - readchar_backlog--); |
078e7b4a JB |
223 | } |
224 | if (EQ (readcharfun, Qget_file_char)) | |
2c1b5dbe KH |
225 | { |
226 | c = getc (instream); | |
227 | #ifdef EINTR | |
228 | /* Interrupted reads have been observed while reading over the network */ | |
229 | while (c == EOF && ferror (instream) && errno == EINTR) | |
230 | { | |
231 | clearerr (instream); | |
232 | c = getc (instream); | |
233 | } | |
234 | #endif | |
235 | return c; | |
236 | } | |
078e7b4a | 237 | |
cfff016d | 238 | if (STRINGP (readcharfun)) |
078e7b4a JB |
239 | { |
240 | register int c; | |
241 | /* This used to be return of a conditional expression, | |
242 | but that truncated -1 to a char on VMS. */ | |
243 | if (read_from_string_index < read_from_string_limit) | |
244 | c = XSTRING (readcharfun)->data[read_from_string_index++]; | |
245 | else | |
246 | c = -1; | |
6f7f43d5 | 247 | |
078e7b4a JB |
248 | return c; |
249 | } | |
250 | ||
251 | tem = call0 (readcharfun); | |
252 | ||
265a9e55 | 253 | if (NILP (tem)) |
078e7b4a JB |
254 | return -1; |
255 | return XINT (tem); | |
256 | } | |
257 | ||
258 | /* Unread the character C in the way appropriate for the stream READCHARFUN. | |
259 | If the stream is a user function, call it with the char as argument. */ | |
260 | ||
261 | static void | |
262 | unreadchar (readcharfun, c) | |
263 | Lisp_Object readcharfun; | |
264 | int c; | |
265 | { | |
92fddec9 KH |
266 | if (c == -1) |
267 | /* Don't back up the pointer if we're unreading the end-of-input mark, | |
268 | since readchar didn't advance it when we read it. */ | |
269 | ; | |
cfff016d | 270 | else if (BUFFERP (readcharfun)) |
d7760ca9 RS |
271 | { |
272 | if (!SINGLE_BYTE_CHAR_P (c)) | |
273 | readchar_backlog++; | |
274 | else | |
275 | { | |
276 | struct buffer *b = XBUFFER (readcharfun); | |
277 | int bytepos = BUF_PT_BYTE (b); | |
278 | ||
279 | BUF_PT (b)--; | |
280 | if (! NILP (b->enable_multibyte_characters)) | |
281 | BUF_DEC_POS (b, bytepos); | |
282 | else | |
283 | bytepos--; | |
284 | ||
285 | BUF_PT_BYTE (b) = bytepos; | |
286 | } | |
287 | } | |
cfff016d | 288 | else if (MARKERP (readcharfun)) |
d7760ca9 RS |
289 | { |
290 | if (!SINGLE_BYTE_CHAR_P (c)) | |
291 | readchar_backlog++; | |
292 | else | |
293 | { | |
294 | struct buffer *b = XMARKER (readcharfun)->buffer; | |
295 | int bytepos = XMARKER (readcharfun)->bytepos; | |
296 | ||
297 | XMARKER (readcharfun)->charpos--; | |
298 | if (! NILP (b->enable_multibyte_characters)) | |
299 | BUF_DEC_POS (b, bytepos); | |
300 | else | |
301 | bytepos--; | |
302 | ||
303 | XMARKER (readcharfun)->bytepos = bytepos; | |
304 | } | |
305 | } | |
cfff016d | 306 | else if (STRINGP (readcharfun)) |
078e7b4a JB |
307 | read_from_string_index--; |
308 | else if (EQ (readcharfun, Qget_file_char)) | |
309 | ungetc (c, instream); | |
310 | else | |
311 | call1 (readcharfun, make_number (c)); | |
312 | } | |
313 | ||
314 | static Lisp_Object read0 (), read1 (), read_list (), read_vector (); | |
6f7f43d5 | 315 | static int read_multibyte (); |
078e7b4a JB |
316 | \f |
317 | /* get a character from the tty */ | |
318 | ||
3d9b22be JB |
319 | extern Lisp_Object read_char (); |
320 | ||
f42be754 JB |
321 | /* Read input events until we get one that's acceptable for our purposes. |
322 | ||
323 | If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed | |
324 | until we get a character we like, and then stuffed into | |
325 | unread_switch_frame. | |
326 | ||
327 | If ASCII_REQUIRED is non-zero, we check function key events to see | |
328 | if the unmodified version of the symbol has a Qascii_character | |
329 | property, and use that character, if present. | |
330 | ||
331 | If ERROR_NONASCII is non-zero, we signal an error if the input we | |
332 | get isn't an ASCII character with modifiers. If it's zero but | |
333 | ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII | |
334 | character. */ | |
cc39bc38 | 335 | |
f42be754 JB |
336 | Lisp_Object |
337 | read_filtered_event (no_switch_frame, ascii_required, error_nonascii) | |
338 | int no_switch_frame, ascii_required, error_nonascii; | |
339 | { | |
340 | #ifdef standalone | |
341 | return make_number (getchar ()); | |
342 | #else | |
153a17b7 KH |
343 | register Lisp_Object val, delayed_switch_frame; |
344 | ||
345 | delayed_switch_frame = Qnil; | |
f42be754 JB |
346 | |
347 | /* Read until we get an acceptable event. */ | |
348 | retry: | |
349 | val = read_char (0, 0, 0, Qnil, 0); | |
350 | ||
cfff016d | 351 | if (BUFFERP (val)) |
6c82d689 KH |
352 | goto retry; |
353 | ||
f42be754 | 354 | /* switch-frame events are put off until after the next ASCII |
8e6208c5 | 355 | character. This is better than signaling an error just because |
f42be754 JB |
356 | the last characters were typed to a separate minibuffer frame, |
357 | for example. Eventually, some code which can deal with | |
358 | switch-frame events will read it and process it. */ | |
359 | if (no_switch_frame | |
360 | && EVENT_HAS_PARAMETERS (val) | |
361 | && EQ (EVENT_HEAD (val), Qswitch_frame)) | |
362 | { | |
363 | delayed_switch_frame = val; | |
364 | goto retry; | |
365 | } | |
366 | ||
367 | if (ascii_required) | |
368 | { | |
369 | /* Convert certain symbols to their ASCII equivalents. */ | |
cfff016d | 370 | if (SYMBOLP (val)) |
f42be754 JB |
371 | { |
372 | Lisp_Object tem, tem1, tem2; | |
373 | tem = Fget (val, Qevent_symbol_element_mask); | |
374 | if (!NILP (tem)) | |
375 | { | |
376 | tem1 = Fget (Fcar (tem), Qascii_character); | |
377 | /* Merge this symbol's modifier bits | |
378 | with the ASCII equivalent of its basic code. */ | |
379 | if (!NILP (tem1)) | |
baf69866 | 380 | XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem)))); |
f42be754 JB |
381 | } |
382 | } | |
383 | ||
384 | /* If we don't have a character now, deal with it appropriately. */ | |
cfff016d | 385 | if (!INTEGERP (val)) |
f42be754 JB |
386 | { |
387 | if (error_nonascii) | |
388 | { | |
1ec84625 | 389 | Vunread_command_events = Fcons (val, Qnil); |
f42be754 JB |
390 | error ("Non-character input-event"); |
391 | } | |
392 | else | |
393 | goto retry; | |
394 | } | |
395 | } | |
396 | ||
397 | if (! NILP (delayed_switch_frame)) | |
398 | unread_switch_frame = delayed_switch_frame; | |
399 | ||
400 | return val; | |
401 | #endif | |
402 | } | |
403 | ||
078e7b4a JB |
404 | DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0, |
405 | "Read a character from the command input (keyboard or macro).\n\ | |
e51e47f7 JB |
406 | It is returned as a number.\n\ |
407 | If the user generates an event which is not a character (i.e. a mouse\n\ | |
e37c0805 JB |
408 | click or function key event), `read-char' signals an error. As an\n\ |
409 | exception, switch-frame events are put off until non-ASCII events can\n\ | |
410 | be read.\n\ | |
411 | If you want to read non-character events, or ignore them, call\n\ | |
412 | `read-event' or `read-char-exclusive' instead.") | |
078e7b4a JB |
413 | () |
414 | { | |
f42be754 | 415 | return read_filtered_event (1, 1, 1); |
078e7b4a JB |
416 | } |
417 | ||
078e7b4a JB |
418 | DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0, |
419 | "Read an event object from the input stream.") | |
420 | () | |
421 | { | |
f42be754 | 422 | return read_filtered_event (0, 0, 0); |
078e7b4a JB |
423 | } |
424 | ||
425 | DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0, | |
426 | "Read a character from the command input (keyboard or macro).\n\ | |
cc39bc38 | 427 | It is returned as a number. Non-character events are ignored.") |
078e7b4a JB |
428 | () |
429 | { | |
f42be754 | 430 | return read_filtered_event (1, 1, 0); |
078e7b4a | 431 | } |
078e7b4a JB |
432 | |
433 | DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0, | |
434 | "Don't use this yourself.") | |
435 | () | |
436 | { | |
437 | register Lisp_Object val; | |
1805de4f | 438 | XSETINT (val, getc (instream)); |
078e7b4a JB |
439 | return val; |
440 | } | |
441 | \f | |
442 | static void readevalloop (); | |
443 | static Lisp_Object load_unwind (); | |
d2c6be7f | 444 | static Lisp_Object load_descriptor_unwind (); |
078e7b4a | 445 | |
f0a50954 | 446 | DEFUN ("load", Fload, Sload, 1, 5, 0, |
078e7b4a JB |
447 | "Execute a file of Lisp code named FILE.\n\ |
448 | First try FILE with `.elc' appended, then try with `.el',\n\ | |
449 | then try FILE unmodified.\n\ | |
450 | This function searches the directories in `load-path'.\n\ | |
451 | If optional second arg NOERROR is non-nil,\n\ | |
452 | report no error if FILE doesn't exist.\n\ | |
453 | Print messages at start and end of loading unless\n\ | |
454 | optional third arg NOMESSAGE is non-nil.\n\ | |
455 | If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\ | |
456 | suffixes `.elc' or `.el' to the specified name FILE.\n\ | |
211f7dcd | 457 | If optional fifth arg MUST-SUFFIX is non-nil, insist on\n\ |
7b88a4cf | 458 | the suffix `.elc' or `.el'; don't accept just FILE unless\n\ |
211f7dcd | 459 | it ends in one of those suffixes or includes a directory name.\n\ |
078e7b4a | 460 | Return t if file exists.") |
f0a50954 RS |
461 | (file, noerror, nomessage, nosuffix, must_suffix) |
462 | Lisp_Object file, noerror, nomessage, nosuffix, must_suffix; | |
078e7b4a JB |
463 | { |
464 | register FILE *stream; | |
465 | register int fd = -1; | |
466 | register Lisp_Object lispstream; | |
078e7b4a JB |
467 | int count = specpdl_ptr - specpdl; |
468 | Lisp_Object temp; | |
469 | struct gcpro gcpro1; | |
470 | Lisp_Object found; | |
04fc68e7 RS |
471 | /* 1 means we printed the ".el is newer" message. */ |
472 | int newer = 0; | |
473 | /* 1 means we are loading a compiled file. */ | |
474 | int compiled = 0; | |
c2225d00 | 475 | Lisp_Object handler; |
317073d5 | 476 | #ifdef DOS_NT |
23a71bd6 | 477 | char *dosmode = "rt"; |
317073d5 | 478 | #endif /* DOS_NT */ |
078e7b4a | 479 | |
0745dce9 | 480 | CHECK_STRING (file, 0); |
078e7b4a | 481 | |
c2225d00 | 482 | /* If file name is magic, call the handler. */ |
0745dce9 | 483 | handler = Ffind_file_name_handler (file, Qload); |
c2225d00 | 484 | if (!NILP (handler)) |
0745dce9 | 485 | return call5 (handler, Qload, file, noerror, nomessage, nosuffix); |
c2225d00 | 486 | |
07a0bda3 RS |
487 | /* Do this after the handler to avoid |
488 | the need to gcpro noerror, nomessage and nosuffix. | |
489 | (Below here, we care only whether they are nil or not.) */ | |
0745dce9 | 490 | file = Fsubstitute_in_file_name (file); |
07a0bda3 | 491 | |
078e7b4a JB |
492 | /* Avoid weird lossage with null string as arg, |
493 | since it would try to load a directory as a Lisp file */ | |
0745dce9 | 494 | if (XSTRING (file)->size > 0) |
078e7b4a | 495 | { |
211f7dcd RS |
496 | int size = XSTRING (file)->size; |
497 | ||
0745dce9 | 498 | GCPRO1 (file); |
211f7dcd RS |
499 | |
500 | if (! NILP (must_suffix)) | |
501 | { | |
502 | /* Don't insist on adding a suffix if FILE already ends with one. */ | |
503 | if (size > 3 | |
504 | && !strcmp (XSTRING (file)->data + size - 3, ".el")) | |
505 | must_suffix = Qnil; | |
506 | else if (size > 4 | |
507 | && !strcmp (XSTRING (file)->data + size - 4, ".elc")) | |
508 | must_suffix = Qnil; | |
509 | /* Don't insist on adding a suffix | |
510 | if the argument includes a directory name. */ | |
511 | else if (! NILP (Ffile_name_directory (file))) | |
512 | must_suffix = Qnil; | |
513 | } | |
514 | ||
f0a50954 RS |
515 | fd = openp (Vload_path, file, |
516 | (!NILP (nosuffix) ? "" | |
517 | : ! NILP (must_suffix) ? ".elc:.el" | |
518 | : ".elc:.el:"), | |
078e7b4a | 519 | &found, 0); |
5a6e5452 | 520 | UNGCPRO; |
078e7b4a JB |
521 | } |
522 | ||
523 | if (fd < 0) | |
524 | { | |
265a9e55 | 525 | if (NILP (noerror)) |
078e7b4a JB |
526 | while (1) |
527 | Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"), | |
0745dce9 | 528 | Fcons (file, Qnil))); |
078e7b4a JB |
529 | else |
530 | return Qnil; | |
531 | } | |
532 | ||
74549846 RS |
533 | /* If FD is 0, that means openp found a remote file. */ |
534 | if (fd == 0) | |
535 | { | |
536 | handler = Ffind_file_name_handler (found, Qload); | |
537 | return call5 (handler, Qload, found, noerror, nomessage, Qt); | |
538 | } | |
539 | ||
078e7b4a JB |
540 | if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]), |
541 | ".elc", 4)) | |
542 | { | |
543 | struct stat s1, s2; | |
544 | int result; | |
545 | ||
04fc68e7 RS |
546 | compiled = 1; |
547 | ||
317073d5 | 548 | #ifdef DOS_NT |
23a71bd6 | 549 | dosmode = "rb"; |
317073d5 | 550 | #endif /* DOS_NT */ |
4ff37b08 | 551 | stat ((char *)XSTRING (found)->data, &s1); |
078e7b4a | 552 | XSTRING (found)->data[XSTRING (found)->size - 1] = 0; |
4ff37b08 | 553 | result = stat ((char *)XSTRING (found)->data, &s2); |
078e7b4a | 554 | if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime) |
51ac6f83 | 555 | { |
04fc68e7 RS |
556 | /* Make the progress messages mention that source is newer. */ |
557 | newer = 1; | |
558 | ||
559 | /* If we won't print another message, mention this anyway. */ | |
560 | if (! NILP (nomessage)) | |
e28552a4 RS |
561 | message_with_string ("Source file `%s' newer than byte-compiled file", |
562 | found, 1); | |
51ac6f83 | 563 | } |
078e7b4a JB |
564 | XSTRING (found)->data[XSTRING (found)->size - 1] = 'c'; |
565 | } | |
fe0e03f3 KH |
566 | else |
567 | { | |
568 | /* We are loading a source file (*.el). */ | |
569 | if (!NILP (Vload_source_file_function)) | |
570 | { | |
571 | close (fd); | |
7075e5a5 | 572 | return call4 (Vload_source_file_function, found, file, |
fe0e03f3 KH |
573 | NILP (noerror) ? Qnil : Qt, |
574 | NILP (nomessage) ? Qnil : Qt); | |
575 | } | |
576 | } | |
078e7b4a | 577 | |
317073d5 | 578 | #ifdef DOS_NT |
23a71bd6 RS |
579 | close (fd); |
580 | stream = fopen ((char *) XSTRING (found)->data, dosmode); | |
317073d5 | 581 | #else /* not DOS_NT */ |
078e7b4a | 582 | stream = fdopen (fd, "r"); |
317073d5 | 583 | #endif /* not DOS_NT */ |
078e7b4a JB |
584 | if (stream == 0) |
585 | { | |
586 | close (fd); | |
0745dce9 | 587 | error ("Failure to create stdio stream for %s", XSTRING (file)->data); |
078e7b4a JB |
588 | } |
589 | ||
4b104c41 RS |
590 | if (! NILP (Vpurify_flag)) |
591 | Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list); | |
592 | ||
04fc68e7 RS |
593 | if (NILP (nomessage)) |
594 | { | |
db5cae4b | 595 | if (!compiled) |
e28552a4 | 596 | message_with_string ("Loading %s (source)...", file, 1); |
db5cae4b | 597 | else if (newer) |
e28552a4 RS |
598 | message_with_string ("Loading %s (compiled; note, source file is newer)...", |
599 | file, 1); | |
db5cae4b | 600 | else /* The typical case; compiled file newer than source file. */ |
e28552a4 | 601 | message_with_string ("Loading %s...", file, 1); |
04fc68e7 | 602 | } |
078e7b4a | 603 | |
0745dce9 | 604 | GCPRO1 (file); |
838abf56 KH |
605 | lispstream = Fcons (Qnil, Qnil); |
606 | XSETFASTINT (XCONS (lispstream)->car, (EMACS_UINT)stream >> 16); | |
607 | XSETFASTINT (XCONS (lispstream)->cdr, (EMACS_UINT)stream & 0xffff); | |
078e7b4a | 608 | record_unwind_protect (load_unwind, lispstream); |
d2c6be7f | 609 | record_unwind_protect (load_descriptor_unwind, load_descriptor_list); |
20ea2964 | 610 | specbind (Qload_file_name, found); |
74549846 | 611 | specbind (Qinhibit_file_name_operation, Qnil); |
d2c6be7f RS |
612 | load_descriptor_list |
613 | = Fcons (make_number (fileno (stream)), load_descriptor_list); | |
078e7b4a | 614 | load_in_progress++; |
0745dce9 | 615 | readevalloop (Qget_file_char, stream, file, Feval, 0); |
078e7b4a JB |
616 | unbind_to (count, Qnil); |
617 | ||
618 | /* Run any load-hooks for this file. */ | |
0745dce9 | 619 | temp = Fassoc (file, Vafter_load_alist); |
265a9e55 | 620 | if (!NILP (temp)) |
078e7b4a JB |
621 | Fprogn (Fcdr (temp)); |
622 | UNGCPRO; | |
623 | ||
b2a30870 RS |
624 | if (saved_doc_string) |
625 | free (saved_doc_string); | |
626 | saved_doc_string = 0; | |
627 | saved_doc_string_size = 0; | |
628 | ||
265a9e55 | 629 | if (!noninteractive && NILP (nomessage)) |
04fc68e7 | 630 | { |
db5cae4b | 631 | if (!compiled) |
e28552a4 | 632 | message_with_string ("Loading %s (source)...done", file, 1); |
db5cae4b | 633 | else if (newer) |
e28552a4 RS |
634 | message_with_string ("Loading %s (compiled; note, source file is newer)...done", |
635 | file, 1); | |
db5cae4b | 636 | else /* The typical case; compiled file newer than source file. */ |
e28552a4 | 637 | message_with_string ("Loading %s...done", file, 1); |
04fc68e7 | 638 | } |
078e7b4a JB |
639 | return Qt; |
640 | } | |
641 | ||
642 | static Lisp_Object | |
643 | load_unwind (stream) /* used as unwind-protect function in load */ | |
644 | Lisp_Object stream; | |
645 | { | |
c8bdaa8c RS |
646 | fclose ((FILE *) (XFASTINT (XCONS (stream)->car) << 16 |
647 | | XFASTINT (XCONS (stream)->cdr))); | |
078e7b4a JB |
648 | if (--load_in_progress < 0) load_in_progress = 0; |
649 | return Qnil; | |
650 | } | |
651 | ||
d2c6be7f RS |
652 | static Lisp_Object |
653 | load_descriptor_unwind (oldlist) | |
654 | Lisp_Object oldlist; | |
655 | { | |
656 | load_descriptor_list = oldlist; | |
838abf56 | 657 | return Qnil; |
d2c6be7f RS |
658 | } |
659 | ||
660 | /* Close all descriptors in use for Floads. | |
661 | This is used when starting a subprocess. */ | |
662 | ||
663 | void | |
664 | close_load_descs () | |
665 | { | |
f3849f25 | 666 | #ifndef WINDOWSNT |
d2c6be7f RS |
667 | Lisp_Object tail; |
668 | for (tail = load_descriptor_list; !NILP (tail); tail = XCONS (tail)->cdr) | |
669 | close (XFASTINT (XCONS (tail)->car)); | |
f3849f25 | 670 | #endif |
d2c6be7f | 671 | } |
078e7b4a JB |
672 | \f |
673 | static int | |
674 | complete_filename_p (pathname) | |
675 | Lisp_Object pathname; | |
676 | { | |
677 | register unsigned char *s = XSTRING (pathname)->data; | |
317073d5 RS |
678 | return (IS_DIRECTORY_SEP (s[0]) |
679 | || (XSTRING (pathname)->size > 2 | |
680 | && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])) | |
078e7b4a JB |
681 | #ifdef ALTOS |
682 | || *s == '@' | |
683 | #endif | |
684 | #ifdef VMS | |
685 | || index (s, ':') | |
686 | #endif /* VMS */ | |
687 | ); | |
688 | } | |
689 | ||
690 | /* Search for a file whose name is STR, looking in directories | |
691 | in the Lisp list PATH, and trying suffixes from SUFFIX. | |
692 | SUFFIX is a string containing possible suffixes separated by colons. | |
693 | On success, returns a file descriptor. On failure, returns -1. | |
694 | ||
695 | EXEC_ONLY nonzero means don't open the files, | |
696 | just look for one that is executable. In this case, | |
697 | returns 1 on success. | |
698 | ||
699 | If STOREPTR is nonzero, it points to a slot where the name of | |
700 | the file actually found should be stored as a Lisp string. | |
74549846 RS |
701 | nil is stored there on failure. |
702 | ||
703 | If the file we find is remote, return 0 | |
704 | but store the found remote file name in *STOREPTR. | |
705 | We do not check for remote files if EXEC_ONLY is nonzero. */ | |
078e7b4a JB |
706 | |
707 | int | |
708 | openp (path, str, suffix, storeptr, exec_only) | |
709 | Lisp_Object path, str; | |
710 | char *suffix; | |
711 | Lisp_Object *storeptr; | |
712 | int exec_only; | |
713 | { | |
714 | register int fd; | |
715 | int fn_size = 100; | |
716 | char buf[100]; | |
717 | register char *fn = buf; | |
718 | int absolute = 0; | |
719 | int want_size; | |
74549846 | 720 | Lisp_Object filename; |
078e7b4a | 721 | struct stat st; |
5a6e5452 | 722 | struct gcpro gcpro1; |
078e7b4a | 723 | |
5a6e5452 | 724 | GCPRO1 (str); |
078e7b4a JB |
725 | if (storeptr) |
726 | *storeptr = Qnil; | |
727 | ||
728 | if (complete_filename_p (str)) | |
729 | absolute = 1; | |
730 | ||
265a9e55 | 731 | for (; !NILP (path); path = Fcdr (path)) |
078e7b4a JB |
732 | { |
733 | char *nsuffix; | |
734 | ||
735 | filename = Fexpand_file_name (str, Fcar (path)); | |
736 | if (!complete_filename_p (filename)) | |
737 | /* If there are non-absolute elts in PATH (eg ".") */ | |
738 | /* Of course, this could conceivably lose if luser sets | |
739 | default-directory to be something non-absolute... */ | |
740 | { | |
741 | filename = Fexpand_file_name (filename, current_buffer->directory); | |
742 | if (!complete_filename_p (filename)) | |
743 | /* Give up on this path element! */ | |
744 | continue; | |
745 | } | |
746 | ||
747 | /* Calculate maximum size of any filename made from | |
748 | this path element/specified file name and any possible suffix. */ | |
749 | want_size = strlen (suffix) + XSTRING (filename)->size + 1; | |
750 | if (fn_size < want_size) | |
751 | fn = (char *) alloca (fn_size = 100 + want_size); | |
752 | ||
753 | nsuffix = suffix; | |
754 | ||
755 | /* Loop over suffixes. */ | |
756 | while (1) | |
757 | { | |
758 | char *esuffix = (char *) index (nsuffix, ':'); | |
759 | int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix); | |
74549846 | 760 | Lisp_Object handler; |
078e7b4a | 761 | |
c49afcd7 RS |
762 | /* Concatenate path element/specified name with the suffix. |
763 | If the directory starts with /:, remove that. */ | |
764 | if (XSTRING (filename)->size > 2 | |
765 | && XSTRING (filename)->data[0] == '/' | |
766 | && XSTRING (filename)->data[1] == ':') | |
767 | { | |
768 | strncpy (fn, XSTRING (filename)->data + 2, | |
769 | XSTRING (filename)->size - 2); | |
770 | fn[XSTRING (filename)->size - 2] = 0; | |
771 | } | |
772 | else | |
773 | { | |
774 | strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size); | |
775 | fn[XSTRING (filename)->size] = 0; | |
776 | } | |
777 | ||
078e7b4a JB |
778 | if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */ |
779 | strncat (fn, nsuffix, lsuffix); | |
780 | ||
74549846 RS |
781 | /* Check that the file exists and is not a directory. */ |
782 | if (absolute) | |
783 | handler = Qnil; | |
784 | else | |
785 | handler = Ffind_file_name_handler (filename, Qfile_exists_p); | |
786 | if (! NILP (handler) && ! exec_only) | |
078e7b4a | 787 | { |
74549846 RS |
788 | Lisp_Object string; |
789 | int exists; | |
078e7b4a | 790 | |
74549846 RS |
791 | string = build_string (fn); |
792 | exists = ! NILP (exec_only ? Ffile_executable_p (string) | |
793 | : Ffile_readable_p (string)); | |
794 | if (exists | |
795 | && ! NILP (Ffile_directory_p (build_string (fn)))) | |
796 | exists = 0; | |
797 | ||
798 | if (exists) | |
078e7b4a JB |
799 | { |
800 | /* We succeeded; return this descriptor and filename. */ | |
801 | if (storeptr) | |
802 | *storeptr = build_string (fn); | |
5ef2a3c0 | 803 | UNGCPRO; |
74549846 RS |
804 | return 0; |
805 | } | |
806 | } | |
807 | else | |
808 | { | |
809 | int exists = (stat (fn, &st) >= 0 | |
810 | && (st.st_mode & S_IFMT) != S_IFDIR); | |
811 | if (exists) | |
812 | { | |
813 | /* Check that we can access or open it. */ | |
814 | if (exec_only) | |
815 | fd = (access (fn, X_OK) == 0) ? 1 : -1; | |
816 | else | |
817 | fd = open (fn, O_RDONLY, 0); | |
818 | ||
819 | if (fd >= 0) | |
820 | { | |
821 | /* We succeeded; return this descriptor and filename. */ | |
822 | if (storeptr) | |
823 | *storeptr = build_string (fn); | |
824 | UNGCPRO; | |
825 | return fd; | |
826 | } | |
078e7b4a JB |
827 | } |
828 | } | |
829 | ||
830 | /* Advance to next suffix. */ | |
831 | if (esuffix == 0) | |
832 | break; | |
833 | nsuffix += lsuffix + 1; | |
834 | } | |
5a6e5452 | 835 | if (absolute) |
5ef2a3c0 | 836 | break; |
078e7b4a JB |
837 | } |
838 | ||
5ef2a3c0 KH |
839 | UNGCPRO; |
840 | return -1; | |
078e7b4a JB |
841 | } |
842 | ||
843 | \f | |
ae321d28 RS |
844 | /* Merge the list we've accumulated of globals from the current input source |
845 | into the load_history variable. The details depend on whether | |
846 | the source has an associated file name or not. */ | |
847 | ||
848 | static void | |
849 | build_load_history (stream, source) | |
850 | FILE *stream; | |
851 | Lisp_Object source; | |
852 | { | |
853 | register Lisp_Object tail, prev, newelt; | |
854 | register Lisp_Object tem, tem2; | |
855 | register int foundit, loading; | |
856 | ||
8a1f1537 RS |
857 | /* Don't bother recording anything for preloaded files. */ |
858 | if (!NILP (Vpurify_flag)) | |
859 | return; | |
860 | ||
ae321d28 RS |
861 | loading = stream || !NARROWED; |
862 | ||
863 | tail = Vload_history; | |
864 | prev = Qnil; | |
865 | foundit = 0; | |
866 | while (!NILP (tail)) | |
867 | { | |
868 | tem = Fcar (tail); | |
869 | ||
870 | /* Find the feature's previous assoc list... */ | |
871 | if (!NILP (Fequal (source, Fcar (tem)))) | |
872 | { | |
873 | foundit = 1; | |
874 | ||
875 | /* If we're loading, remove it. */ | |
876 | if (loading) | |
877 | { | |
878 | if (NILP (prev)) | |
879 | Vload_history = Fcdr (tail); | |
880 | else | |
881 | Fsetcdr (prev, Fcdr (tail)); | |
882 | } | |
883 | ||
884 | /* Otherwise, cons on new symbols that are not already members. */ | |
885 | else | |
886 | { | |
887 | tem2 = Vcurrent_load_list; | |
888 | ||
889 | while (CONSP (tem2)) | |
890 | { | |
891 | newelt = Fcar (tem2); | |
892 | ||
893 | if (NILP (Fmemq (newelt, tem))) | |
894 | Fsetcar (tail, Fcons (Fcar (tem), | |
895 | Fcons (newelt, Fcdr (tem)))); | |
896 | ||
897 | tem2 = Fcdr (tem2); | |
898 | QUIT; | |
899 | } | |
900 | } | |
901 | } | |
902 | else | |
903 | prev = tail; | |
904 | tail = Fcdr (tail); | |
905 | QUIT; | |
906 | } | |
907 | ||
8a1f1537 RS |
908 | /* If we're loading, cons the new assoc onto the front of load-history, |
909 | the most-recently-loaded position. Also do this if we didn't find | |
910 | an existing member for the current source. */ | |
911 | if (loading || !foundit) | |
912 | Vload_history = Fcons (Fnreverse (Vcurrent_load_list), | |
913 | Vload_history); | |
ae321d28 RS |
914 | } |
915 | ||
078e7b4a JB |
916 | Lisp_Object |
917 | unreadpure () /* Used as unwind-protect function in readevalloop */ | |
918 | { | |
919 | read_pure = 0; | |
920 | return Qnil; | |
921 | } | |
922 | ||
923 | static void | |
ae321d28 | 924 | readevalloop (readcharfun, stream, sourcename, evalfun, printflag) |
078e7b4a | 925 | Lisp_Object readcharfun; |
ae321d28 RS |
926 | FILE *stream; |
927 | Lisp_Object sourcename; | |
078e7b4a JB |
928 | Lisp_Object (*evalfun) (); |
929 | int printflag; | |
930 | { | |
931 | register int c; | |
932 | register Lisp_Object val; | |
933 | int count = specpdl_ptr - specpdl; | |
8a1f1537 | 934 | struct gcpro gcpro1; |
49cf7ff4 RS |
935 | struct buffer *b = 0; |
936 | ||
937 | if (BUFFERP (readcharfun)) | |
938 | b = XBUFFER (readcharfun); | |
939 | else if (MARKERP (readcharfun)) | |
940 | b = XMARKER (readcharfun)->buffer; | |
078e7b4a JB |
941 | |
942 | specbind (Qstandard_input, readcharfun); | |
8a1f1537 | 943 | specbind (Qcurrent_load_list, Qnil); |
078e7b4a | 944 | |
6f7f43d5 RS |
945 | readchar_backlog = 0; |
946 | ||
8a1f1537 | 947 | GCPRO1 (sourcename); |
ae321d28 | 948 | |
ae321d28 RS |
949 | LOADHIST_ATTACH (sourcename); |
950 | ||
078e7b4a JB |
951 | while (1) |
952 | { | |
49cf7ff4 RS |
953 | if (b != 0 && NILP (b->name)) |
954 | error ("Reading from killed buffer"); | |
955 | ||
078e7b4a JB |
956 | instream = stream; |
957 | c = READCHAR; | |
958 | if (c == ';') | |
959 | { | |
960 | while ((c = READCHAR) != '\n' && c != -1); | |
961 | continue; | |
962 | } | |
963 | if (c < 0) break; | |
6069d957 RS |
964 | |
965 | /* Ignore whitespace here, so we can detect eof. */ | |
966 | if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r') | |
967 | continue; | |
078e7b4a | 968 | |
265a9e55 | 969 | if (!NILP (Vpurify_flag) && c == '(') |
078e7b4a | 970 | { |
0e326373 | 971 | int count1 = specpdl_ptr - specpdl; |
078e7b4a JB |
972 | record_unwind_protect (unreadpure, Qnil); |
973 | val = read_list (-1, readcharfun); | |
0e326373 | 974 | unbind_to (count1, Qnil); |
078e7b4a JB |
975 | } |
976 | else | |
977 | { | |
978 | UNREAD (c); | |
4ad679f9 | 979 | read_objects = Qnil; |
84a15045 RS |
980 | if (NILP (Vload_read_function)) |
981 | val = read0 (readcharfun); | |
982 | else | |
983 | val = call1 (Vload_read_function, readcharfun); | |
078e7b4a JB |
984 | } |
985 | ||
986 | val = (*evalfun) (val); | |
987 | if (printflag) | |
988 | { | |
989 | Vvalues = Fcons (val, Vvalues); | |
990 | if (EQ (Vstandard_output, Qt)) | |
991 | Fprin1 (val, Qnil); | |
992 | else | |
993 | Fprint (val, Qnil); | |
994 | } | |
995 | } | |
996 | ||
ae321d28 | 997 | build_load_history (stream, sourcename); |
ae321d28 RS |
998 | UNGCPRO; |
999 | ||
078e7b4a JB |
1000 | unbind_to (count, Qnil); |
1001 | } | |
1002 | ||
1003 | #ifndef standalone | |
1004 | ||
13febd85 | 1005 | DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 3, "", |
94b304d7 RS |
1006 | "Execute the current buffer as Lisp code.\n\ |
1007 | Programs can pass two arguments, BUFFER and PRINTFLAG.\n\ | |
1008 | BUFFER is the buffer to evaluate (nil means use current buffer).\n\ | |
1009 | PRINTFLAG controls printing of output:\n\ | |
228d4b1c JA |
1010 | nil means discard it; anything else is stream for print.\n\ |
1011 | \n\ | |
13febd85 RS |
1012 | If the optional third argument FILENAME is non-nil,\n\ |
1013 | it specifies the file name to use for `load-history'.\n\ | |
1014 | \n\ | |
d331bcad | 1015 | This function preserves the position of point.") |
13febd85 RS |
1016 | (buffer, printflag, filename) |
1017 | Lisp_Object buffer, printflag, filename; | |
228d4b1c JA |
1018 | { |
1019 | int count = specpdl_ptr - specpdl; | |
1020 | Lisp_Object tem, buf; | |
1021 | ||
9391b698 | 1022 | if (NILP (buffer)) |
228d4b1c JA |
1023 | buf = Fcurrent_buffer (); |
1024 | else | |
9391b698 | 1025 | buf = Fget_buffer (buffer); |
dfdb645c | 1026 | if (NILP (buf)) |
13febd85 | 1027 | error ("No such buffer"); |
228d4b1c | 1028 | |
dfdb645c | 1029 | if (NILP (printflag)) |
228d4b1c JA |
1030 | tem = Qsymbolp; |
1031 | else | |
1032 | tem = printflag; | |
13febd85 RS |
1033 | |
1034 | if (NILP (filename)) | |
1035 | filename = XBUFFER (buf)->filename; | |
1036 | ||
228d4b1c JA |
1037 | specbind (Qstandard_output, tem); |
1038 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
1039 | BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf))); | |
13febd85 | 1040 | readevalloop (buf, 0, filename, Feval, !NILP (printflag)); |
cb09ab7a | 1041 | unbind_to (count, Qnil); |
228d4b1c JA |
1042 | |
1043 | return Qnil; | |
1044 | } | |
1045 | ||
1046 | #if 0 | |
c23edb78 | 1047 | XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "", |
078e7b4a JB |
1048 | "Execute the current buffer as Lisp code.\n\ |
1049 | Programs can pass argument PRINTFLAG which controls printing of output:\n\ | |
1050 | nil means discard it; anything else is stream for print.\n\ | |
1051 | \n\ | |
1052 | If there is no error, point does not move. If there is an error,\n\ | |
1053 | point remains at the end of the last character read from the buffer.") | |
1054 | (printflag) | |
1055 | Lisp_Object printflag; | |
1056 | { | |
1057 | int count = specpdl_ptr - specpdl; | |
ae321d28 RS |
1058 | Lisp_Object tem, cbuf; |
1059 | ||
1060 | cbuf = Fcurrent_buffer () | |
078e7b4a | 1061 | |
265a9e55 | 1062 | if (NILP (printflag)) |
078e7b4a JB |
1063 | tem = Qsymbolp; |
1064 | else | |
1065 | tem = printflag; | |
1066 | specbind (Qstandard_output, tem); | |
1067 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
1068 | SET_PT (BEGV); | |
ae321d28 | 1069 | readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag)); |
078e7b4a JB |
1070 | return unbind_to (count, Qnil); |
1071 | } | |
228d4b1c | 1072 | #endif |
078e7b4a JB |
1073 | |
1074 | DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r", | |
1075 | "Execute the region as Lisp code.\n\ | |
1076 | When called from programs, expects two arguments,\n\ | |
1077 | giving starting and ending indices in the current buffer\n\ | |
1078 | of the text to be executed.\n\ | |
1079 | Programs can pass third argument PRINTFLAG which controls output:\n\ | |
1080 | nil means discard it; anything else is stream for printing it.\n\ | |
1081 | \n\ | |
83f0c0c7 | 1082 | This function does not move point.") |
9391b698 EN |
1083 | (start, end, printflag) |
1084 | Lisp_Object start, end, printflag; | |
078e7b4a JB |
1085 | { |
1086 | int count = specpdl_ptr - specpdl; | |
ae321d28 RS |
1087 | Lisp_Object tem, cbuf; |
1088 | ||
1089 | cbuf = Fcurrent_buffer (); | |
078e7b4a | 1090 | |
265a9e55 | 1091 | if (NILP (printflag)) |
078e7b4a JB |
1092 | tem = Qsymbolp; |
1093 | else | |
1094 | tem = printflag; | |
1095 | specbind (Qstandard_output, tem); | |
1096 | ||
265a9e55 | 1097 | if (NILP (printflag)) |
078e7b4a JB |
1098 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); |
1099 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
1100 | ||
9391b698 EN |
1101 | /* This both uses start and checks its type. */ |
1102 | Fgoto_char (start); | |
1103 | Fnarrow_to_region (make_number (BEGV), end); | |
ae321d28 | 1104 | readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag)); |
078e7b4a JB |
1105 | |
1106 | return unbind_to (count, Qnil); | |
1107 | } | |
1108 | ||
1109 | #endif /* standalone */ | |
1110 | \f | |
1111 | DEFUN ("read", Fread, Sread, 0, 1, 0, | |
1112 | "Read one Lisp expression as text from STREAM, return as Lisp object.\n\ | |
1113 | If STREAM is nil, use the value of `standard-input' (which see).\n\ | |
1114 | STREAM or the value of `standard-input' may be:\n\ | |
1115 | a buffer (read from point and advance it)\n\ | |
1116 | a marker (read from where it points and advance it)\n\ | |
1117 | a function (call it with no arguments for each character,\n\ | |
1118 | call it with a char as argument to push a char back)\n\ | |
1119 | a string (takes text from string, starting at the beginning)\n\ | |
1120 | t (read text line using minibuffer and use it).") | |
5be02dff KH |
1121 | (stream) |
1122 | Lisp_Object stream; | |
078e7b4a JB |
1123 | { |
1124 | extern Lisp_Object Fread_minibuffer (); | |
1125 | ||
5be02dff KH |
1126 | if (NILP (stream)) |
1127 | stream = Vstandard_input; | |
1128 | if (EQ (stream, Qt)) | |
1129 | stream = Qread_char; | |
078e7b4a | 1130 | |
6f7f43d5 | 1131 | readchar_backlog = 0; |
17634846 | 1132 | new_backquote_flag = 0; |
4ad679f9 | 1133 | read_objects = Qnil; |
17634846 | 1134 | |
078e7b4a | 1135 | #ifndef standalone |
5be02dff | 1136 | if (EQ (stream, Qread_char)) |
078e7b4a JB |
1137 | return Fread_minibuffer (build_string ("Lisp expression: "), Qnil); |
1138 | #endif | |
1139 | ||
5be02dff KH |
1140 | if (STRINGP (stream)) |
1141 | return Fcar (Fread_from_string (stream, Qnil, Qnil)); | |
078e7b4a | 1142 | |
5be02dff | 1143 | return read0 (stream); |
078e7b4a JB |
1144 | } |
1145 | ||
1146 | DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0, | |
1147 | "Read one Lisp expression which is represented as text by STRING.\n\ | |
1148 | Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\ | |
1149 | START and END optionally delimit a substring of STRING from which to read;\n\ | |
1150 | they default to 0 and (length STRING) respectively.") | |
1151 | (string, start, end) | |
1152 | Lisp_Object string, start, end; | |
1153 | { | |
1154 | int startval, endval; | |
1155 | Lisp_Object tem; | |
1156 | ||
1157 | CHECK_STRING (string,0); | |
1158 | ||
265a9e55 | 1159 | if (NILP (end)) |
fc932ac6 | 1160 | endval = STRING_BYTES (XSTRING (string)); |
078e7b4a | 1161 | else |
e28552a4 RS |
1162 | { |
1163 | CHECK_NUMBER (end, 2); | |
1164 | endval = string_char_to_byte (string, XINT (end)); | |
fc932ac6 | 1165 | if (endval < 0 || endval > STRING_BYTES (XSTRING (string))) |
078e7b4a JB |
1166 | args_out_of_range (string, end); |
1167 | } | |
1168 | ||
265a9e55 | 1169 | if (NILP (start)) |
078e7b4a JB |
1170 | startval = 0; |
1171 | else | |
e28552a4 RS |
1172 | { |
1173 | CHECK_NUMBER (start, 1); | |
1174 | startval = string_char_to_byte (string, XINT (start)); | |
078e7b4a JB |
1175 | if (startval < 0 || startval > endval) |
1176 | args_out_of_range (string, start); | |
1177 | } | |
1178 | ||
1179 | read_from_string_index = startval; | |
1180 | read_from_string_limit = endval; | |
1181 | ||
17634846 | 1182 | new_backquote_flag = 0; |
4ad679f9 | 1183 | read_objects = Qnil; |
17634846 | 1184 | |
078e7b4a | 1185 | tem = read0 (string); |
e28552a4 RS |
1186 | endval = string_byte_to_char (string, |
1187 | read_from_string_index); | |
1188 | return Fcons (tem, make_number (endval)); | |
078e7b4a JB |
1189 | } |
1190 | \f | |
6428369f KH |
1191 | /* Use this for recursive reads, in contexts where internal tokens |
1192 | are not allowed. */ | |
e28552a4 | 1193 | |
078e7b4a JB |
1194 | static Lisp_Object |
1195 | read0 (readcharfun) | |
1196 | Lisp_Object readcharfun; | |
1197 | { | |
1198 | register Lisp_Object val; | |
e28552a4 | 1199 | int c; |
078e7b4a | 1200 | |
17634846 | 1201 | val = read1 (readcharfun, &c, 0); |
6428369f | 1202 | if (c) |
93d75e4c RS |
1203 | Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1), |
1204 | make_number (c)), | |
e28552a4 | 1205 | Qnil)); |
078e7b4a JB |
1206 | |
1207 | return val; | |
1208 | } | |
1209 | \f | |
1210 | static int read_buffer_size; | |
1211 | static char *read_buffer; | |
1212 | ||
fe0e03f3 KH |
1213 | /* Read multibyte form and return it as a character. C is a first |
1214 | byte of multibyte form, and rest of them are read from | |
1215 | READCHARFUN. */ | |
6f7f43d5 | 1216 | |
fe0e03f3 KH |
1217 | static int |
1218 | read_multibyte (c, readcharfun) | |
1219 | register int c; | |
1220 | Lisp_Object readcharfun; | |
1221 | { | |
1222 | /* We need the actual character code of this multibyte | |
1223 | characters. */ | |
1224 | unsigned char str[MAX_LENGTH_OF_MULTI_BYTE_FORM]; | |
1225 | int len = 0; | |
1226 | ||
1227 | str[len++] = c; | |
1228 | while ((c = READCHAR) >= 0xA0 | |
1229 | && len < MAX_LENGTH_OF_MULTI_BYTE_FORM) | |
1230 | str[len++] = c; | |
1231 | UNREAD (c); | |
1232 | return STRING_CHAR (str, len); | |
1233 | } | |
1234 | ||
6f7f43d5 RS |
1235 | /* Read a \-escape sequence, assuming we already read the `\'. */ |
1236 | ||
078e7b4a | 1237 | static int |
e7fc914b | 1238 | read_escape (readcharfun, stringp) |
078e7b4a | 1239 | Lisp_Object readcharfun; |
e7fc914b | 1240 | int stringp; |
078e7b4a JB |
1241 | { |
1242 | register int c = READCHAR; | |
1243 | switch (c) | |
1244 | { | |
f3849f25 RS |
1245 | case -1: |
1246 | error ("End of file"); | |
1247 | ||
078e7b4a | 1248 | case 'a': |
265a9e55 | 1249 | return '\007'; |
078e7b4a JB |
1250 | case 'b': |
1251 | return '\b'; | |
f405a585 RS |
1252 | case 'd': |
1253 | return 0177; | |
078e7b4a JB |
1254 | case 'e': |
1255 | return 033; | |
1256 | case 'f': | |
1257 | return '\f'; | |
1258 | case 'n': | |
1259 | return '\n'; | |
1260 | case 'r': | |
1261 | return '\r'; | |
1262 | case 't': | |
1263 | return '\t'; | |
1264 | case 'v': | |
1265 | return '\v'; | |
1266 | case '\n': | |
1267 | return -1; | |
e28552a4 | 1268 | case ' ': |
e7fc914b KH |
1269 | if (stringp) |
1270 | return -1; | |
1271 | return ' '; | |
078e7b4a JB |
1272 | |
1273 | case 'M': | |
1274 | c = READCHAR; | |
1275 | if (c != '-') | |
1276 | error ("Invalid escape character syntax"); | |
1277 | c = READCHAR; | |
1278 | if (c == '\\') | |
e7fc914b | 1279 | c = read_escape (readcharfun, 0); |
7bd279cd | 1280 | return c | meta_modifier; |
f405a585 RS |
1281 | |
1282 | case 'S': | |
1283 | c = READCHAR; | |
1284 | if (c != '-') | |
1285 | error ("Invalid escape character syntax"); | |
1286 | c = READCHAR; | |
1287 | if (c == '\\') | |
e7fc914b | 1288 | c = read_escape (readcharfun, 0); |
7bd279cd RS |
1289 | return c | shift_modifier; |
1290 | ||
1291 | case 'H': | |
1292 | c = READCHAR; | |
1293 | if (c != '-') | |
1294 | error ("Invalid escape character syntax"); | |
1295 | c = READCHAR; | |
1296 | if (c == '\\') | |
e7fc914b | 1297 | c = read_escape (readcharfun, 0); |
7bd279cd RS |
1298 | return c | hyper_modifier; |
1299 | ||
1300 | case 'A': | |
1301 | c = READCHAR; | |
1302 | if (c != '-') | |
1303 | error ("Invalid escape character syntax"); | |
1304 | c = READCHAR; | |
1305 | if (c == '\\') | |
e7fc914b | 1306 | c = read_escape (readcharfun, 0); |
7bd279cd RS |
1307 | return c | alt_modifier; |
1308 | ||
1309 | case 's': | |
1310 | c = READCHAR; | |
1311 | if (c != '-') | |
1312 | error ("Invalid escape character syntax"); | |
1313 | c = READCHAR; | |
1314 | if (c == '\\') | |
e7fc914b | 1315 | c = read_escape (readcharfun, 0); |
7bd279cd | 1316 | return c | super_modifier; |
078e7b4a JB |
1317 | |
1318 | case 'C': | |
1319 | c = READCHAR; | |
1320 | if (c != '-') | |
1321 | error ("Invalid escape character syntax"); | |
1322 | case '^': | |
1323 | c = READCHAR; | |
1324 | if (c == '\\') | |
e7fc914b | 1325 | c = read_escape (readcharfun, 0); |
f405a585 RS |
1326 | if ((c & 0177) == '?') |
1327 | return 0177 | c; | |
1328 | /* ASCII control chars are made from letters (both cases), | |
1329 | as well as the non-letters within 0100...0137. */ | |
1330 | else if ((c & 0137) >= 0101 && (c & 0137) <= 0132) | |
1331 | return (c & (037 | ~0177)); | |
1332 | else if ((c & 0177) >= 0100 && (c & 0177) <= 0137) | |
1333 | return (c & (037 | ~0177)); | |
078e7b4a | 1334 | else |
7bd279cd | 1335 | return c | ctrl_modifier; |
078e7b4a JB |
1336 | |
1337 | case '0': | |
1338 | case '1': | |
1339 | case '2': | |
1340 | case '3': | |
1341 | case '4': | |
1342 | case '5': | |
1343 | case '6': | |
1344 | case '7': | |
1345 | /* An octal escape, as in ANSI C. */ | |
1346 | { | |
1347 | register int i = c - '0'; | |
1348 | register int count = 0; | |
1349 | while (++count < 3) | |
1350 | { | |
1351 | if ((c = READCHAR) >= '0' && c <= '7') | |
1352 | { | |
1353 | i *= 8; | |
1354 | i += c - '0'; | |
1355 | } | |
1356 | else | |
1357 | { | |
1358 | UNREAD (c); | |
1359 | break; | |
1360 | } | |
1361 | } | |
1362 | return i; | |
1363 | } | |
1364 | ||
1365 | case 'x': | |
1366 | /* A hex escape, as in ANSI C. */ | |
1367 | { | |
1368 | int i = 0; | |
1369 | while (1) | |
1370 | { | |
1371 | c = READCHAR; | |
1372 | if (c >= '0' && c <= '9') | |
1373 | { | |
1374 | i *= 16; | |
1375 | i += c - '0'; | |
1376 | } | |
1377 | else if ((c >= 'a' && c <= 'f') | |
1378 | || (c >= 'A' && c <= 'F')) | |
1379 | { | |
1380 | i *= 16; | |
1381 | if (c >= 'a' && c <= 'f') | |
1382 | i += c - 'a' + 10; | |
1383 | else | |
1384 | i += c - 'A' + 10; | |
1385 | } | |
1386 | else | |
1387 | { | |
1388 | UNREAD (c); | |
1389 | break; | |
1390 | } | |
1391 | } | |
1392 | return i; | |
1393 | } | |
1394 | ||
1395 | default: | |
fe0e03f3 KH |
1396 | if (BASE_LEADING_CODE_P (c)) |
1397 | c = read_multibyte (c, readcharfun); | |
078e7b4a JB |
1398 | return c; |
1399 | } | |
1400 | } | |
1401 | ||
6428369f KH |
1402 | /* If the next token is ')' or ']' or '.', we store that character |
1403 | in *PCH and the return value is not interesting. Else, we store | |
17634846 RS |
1404 | zero in *PCH and we read and return one lisp object. |
1405 | ||
1406 | FIRST_IN_LIST is nonzero if this is the first element of a list. */ | |
1407 | ||
078e7b4a | 1408 | static Lisp_Object |
17634846 | 1409 | read1 (readcharfun, pch, first_in_list) |
078e7b4a | 1410 | register Lisp_Object readcharfun; |
e28552a4 | 1411 | int *pch; |
17634846 | 1412 | int first_in_list; |
078e7b4a JB |
1413 | { |
1414 | register int c; | |
4ad679f9 EN |
1415 | int uninterned_symbol = 0; |
1416 | ||
6428369f | 1417 | *pch = 0; |
078e7b4a JB |
1418 | |
1419 | retry: | |
1420 | ||
1421 | c = READCHAR; | |
1422 | if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
1423 | ||
1424 | switch (c) | |
1425 | { | |
1426 | case '(': | |
1427 | return read_list (0, readcharfun); | |
1428 | ||
1429 | case '[': | |
1430 | return read_vector (readcharfun); | |
1431 | ||
1432 | case ')': | |
1433 | case ']': | |
078e7b4a | 1434 | { |
6428369f KH |
1435 | *pch = c; |
1436 | return Qnil; | |
078e7b4a JB |
1437 | } |
1438 | ||
1439 | case '#': | |
200f684e | 1440 | c = READCHAR; |
c2390933 RS |
1441 | if (c == '^') |
1442 | { | |
1443 | c = READCHAR; | |
1444 | if (c == '[') | |
1445 | { | |
1446 | Lisp_Object tmp; | |
1447 | tmp = read_vector (readcharfun); | |
1448 | if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS | |
1449 | || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10) | |
1450 | error ("Invalid size char-table"); | |
1451 | XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp)); | |
3701b5de | 1452 | XCHAR_TABLE (tmp)->top = Qt; |
c2390933 RS |
1453 | return tmp; |
1454 | } | |
3701b5de KH |
1455 | else if (c == '^') |
1456 | { | |
1457 | c = READCHAR; | |
1458 | if (c == '[') | |
1459 | { | |
1460 | Lisp_Object tmp; | |
1461 | tmp = read_vector (readcharfun); | |
1462 | if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS) | |
1463 | error ("Invalid size char-table"); | |
1464 | XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp)); | |
1465 | XCHAR_TABLE (tmp)->top = Qnil; | |
1466 | return tmp; | |
1467 | } | |
1468 | Fsignal (Qinvalid_read_syntax, | |
1469 | Fcons (make_string ("#^^", 3), Qnil)); | |
1470 | } | |
c2390933 RS |
1471 | Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil)); |
1472 | } | |
1473 | if (c == '&') | |
1474 | { | |
1475 | Lisp_Object length; | |
1476 | length = read1 (readcharfun, pch, first_in_list); | |
1477 | c = READCHAR; | |
1478 | if (c == '"') | |
1479 | { | |
1480 | Lisp_Object tmp, val; | |
90ed3ec5 | 1481 | int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1) |
68be917d | 1482 | / BITS_PER_CHAR); |
c2390933 RS |
1483 | |
1484 | UNREAD (c); | |
1485 | tmp = read1 (readcharfun, pch, first_in_list); | |
90ed3ec5 RS |
1486 | if (size_in_chars != XSTRING (tmp)->size |
1487 | /* We used to print 1 char too many | |
1488 | when the number of bits was a multiple of 8. | |
1489 | Accept such input in case it came from an old version. */ | |
1490 | && ! (XFASTINT (length) | |
1491 | == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR)) | |
c2390933 | 1492 | Fsignal (Qinvalid_read_syntax, |
ec3bbd7d | 1493 | Fcons (make_string ("#&...", 5), Qnil)); |
c2390933 RS |
1494 | |
1495 | val = Fmake_bool_vector (length, Qnil); | |
1496 | bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data, | |
1497 | size_in_chars); | |
1498 | return val; | |
1499 | } | |
ec3bbd7d | 1500 | Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5), |
039dc30b | 1501 | Qnil)); |
c2390933 | 1502 | } |
200f684e RS |
1503 | if (c == '[') |
1504 | { | |
1505 | /* Accept compiled functions at read-time so that we don't have to | |
1506 | build them using function calls. */ | |
748ef62f RS |
1507 | Lisp_Object tmp; |
1508 | tmp = read_vector (readcharfun); | |
1509 | return Fmake_byte_code (XVECTOR (tmp)->size, | |
1510 | XVECTOR (tmp)->contents); | |
200f684e | 1511 | } |
748ef62f RS |
1512 | #ifdef USE_TEXT_PROPERTIES |
1513 | if (c == '(') | |
1514 | { | |
1515 | Lisp_Object tmp; | |
1516 | struct gcpro gcpro1; | |
e28552a4 | 1517 | int ch; |
748ef62f RS |
1518 | |
1519 | /* Read the string itself. */ | |
17634846 | 1520 | tmp = read1 (readcharfun, &ch, 0); |
6428369f | 1521 | if (ch != 0 || !STRINGP (tmp)) |
748ef62f RS |
1522 | Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil)); |
1523 | GCPRO1 (tmp); | |
1524 | /* Read the intervals and their properties. */ | |
1525 | while (1) | |
1526 | { | |
1527 | Lisp_Object beg, end, plist; | |
1528 | ||
17634846 | 1529 | beg = read1 (readcharfun, &ch, 0); |
6428369f KH |
1530 | if (ch == ')') |
1531 | break; | |
1532 | if (ch == 0) | |
17634846 | 1533 | end = read1 (readcharfun, &ch, 0); |
6428369f | 1534 | if (ch == 0) |
17634846 | 1535 | plist = read1 (readcharfun, &ch, 0); |
6428369f | 1536 | if (ch) |
748ef62f | 1537 | Fsignal (Qinvalid_read_syntax, |
6428369f KH |
1538 | Fcons (build_string ("invalid string property list"), |
1539 | Qnil)); | |
748ef62f RS |
1540 | Fset_text_properties (beg, end, plist, tmp); |
1541 | } | |
1542 | UNGCPRO; | |
1543 | return tmp; | |
1544 | } | |
1545 | #endif | |
20ea2964 RS |
1546 | /* #@NUMBER is used to skip NUMBER following characters. |
1547 | That's used in .elc files to skip over doc strings | |
1548 | and function definitions. */ | |
1549 | if (c == '@') | |
1550 | { | |
1551 | int i, nskip = 0; | |
1552 | ||
1553 | /* Read a decimal integer. */ | |
1554 | while ((c = READCHAR) >= 0 | |
1555 | && c >= '0' && c <= '9') | |
1556 | { | |
1557 | nskip *= 10; | |
1558 | nskip += c - '0'; | |
1559 | } | |
1560 | if (c >= 0) | |
1561 | UNREAD (c); | |
1562 | ||
b2a30870 RS |
1563 | #ifndef DOS_NT /* I don't know if filepos works right on MSDOS and Windoze. */ |
1564 | if (load_force_doc_strings && EQ (readcharfun, Qget_file_char)) | |
1565 | { | |
1566 | /* If we are supposed to force doc strings into core right now, | |
1567 | record the last string that we skipped, | |
1568 | and record where in the file it comes from. */ | |
1569 | if (saved_doc_string_size == 0) | |
1570 | { | |
1571 | saved_doc_string_size = nskip + 100; | |
11938f10 | 1572 | saved_doc_string = (char *) xmalloc (saved_doc_string_size); |
b2a30870 RS |
1573 | } |
1574 | if (nskip > saved_doc_string_size) | |
1575 | { | |
1576 | saved_doc_string_size = nskip + 100; | |
11938f10 KH |
1577 | saved_doc_string = (char *) xrealloc (saved_doc_string, |
1578 | saved_doc_string_size); | |
b2a30870 RS |
1579 | } |
1580 | ||
1581 | saved_doc_string_position = ftell (instream); | |
1582 | ||
1583 | /* Copy that many characters into saved_doc_string. */ | |
1584 | for (i = 0; i < nskip && c >= 0; i++) | |
1585 | saved_doc_string[i] = c = READCHAR; | |
1586 | ||
1587 | saved_doc_string_length = i; | |
1588 | } | |
1589 | else | |
1590 | #endif /* not DOS_NT */ | |
1591 | { | |
1592 | /* Skip that many characters. */ | |
1593 | for (i = 0; i < nskip && c >= 0; i++) | |
1594 | c = READCHAR; | |
1595 | } | |
d49f0c1a | 1596 | |
20ea2964 RS |
1597 | goto retry; |
1598 | } | |
1599 | if (c == '$') | |
1600 | return Vload_file_name; | |
2b6cae0c RS |
1601 | if (c == '\'') |
1602 | return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil)); | |
4ad679f9 EN |
1603 | /* #:foo is the uninterned symbol named foo. */ |
1604 | if (c == ':') | |
1605 | { | |
1606 | uninterned_symbol = 1; | |
1607 | c = READCHAR; | |
1608 | goto default_label; | |
1609 | } | |
1610 | /* Reader forms that can reuse previously read objects. */ | |
1611 | if (c >= '0' && c <= '9') | |
1612 | { | |
1613 | int n = 0; | |
1614 | Lisp_Object tem; | |
2b6cae0c | 1615 | |
4ad679f9 EN |
1616 | /* Read a non-negative integer. */ |
1617 | while (c >= '0' && c <= '9') | |
1618 | { | |
1619 | n *= 10; | |
1620 | n += c - '0'; | |
1621 | c = READCHAR; | |
1622 | } | |
1623 | /* #n=object returns object, but associates it with n for #n#. */ | |
1624 | if (c == '=') | |
1625 | { | |
1626 | tem = read0 (readcharfun); | |
1627 | read_objects = Fcons (Fcons (make_number (n), tem), read_objects); | |
1628 | return tem; | |
1629 | } | |
1630 | /* #n# returns a previously read object. */ | |
1631 | if (c == '#') | |
1632 | { | |
1633 | tem = Fassq (make_number (n), read_objects); | |
1634 | if (CONSP (tem)) | |
1635 | return XCDR (tem); | |
1636 | /* Fall through to error message. */ | |
1637 | } | |
1638 | /* Fall through to error message. */ | |
1639 | } | |
20ea2964 | 1640 | |
200f684e | 1641 | UNREAD (c); |
748ef62f | 1642 | Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil)); |
078e7b4a JB |
1643 | |
1644 | case ';': | |
1645 | while ((c = READCHAR) >= 0 && c != '\n'); | |
1646 | goto retry; | |
1647 | ||
1648 | case '\'': | |
1649 | { | |
1650 | return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil)); | |
1651 | } | |
1652 | ||
17634846 RS |
1653 | case '`': |
1654 | if (first_in_list) | |
1655 | goto default_label; | |
1656 | else | |
1657 | { | |
1658 | Lisp_Object value; | |
1659 | ||
1660 | new_backquote_flag = 1; | |
1661 | value = read0 (readcharfun); | |
1662 | new_backquote_flag = 0; | |
1663 | ||
1664 | return Fcons (Qbackquote, Fcons (value, Qnil)); | |
1665 | } | |
1666 | ||
1667 | case ',': | |
1668 | if (new_backquote_flag) | |
1669 | { | |
1670 | Lisp_Object comma_type = Qnil; | |
1671 | Lisp_Object value; | |
1672 | int ch = READCHAR; | |
1673 | ||
1674 | if (ch == '@') | |
1675 | comma_type = Qcomma_at; | |
1676 | else if (ch == '.') | |
1677 | comma_type = Qcomma_dot; | |
1678 | else | |
1679 | { | |
1680 | if (ch >= 0) UNREAD (ch); | |
1681 | comma_type = Qcomma; | |
1682 | } | |
1683 | ||
1684 | new_backquote_flag = 0; | |
1685 | value = read0 (readcharfun); | |
1686 | new_backquote_flag = 1; | |
1687 | return Fcons (comma_type, Fcons (value, Qnil)); | |
1688 | } | |
1689 | else | |
1690 | goto default_label; | |
1691 | ||
078e7b4a JB |
1692 | case '?': |
1693 | { | |
1694 | register Lisp_Object val; | |
1695 | ||
1696 | c = READCHAR; | |
1697 | if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
1698 | ||
1699 | if (c == '\\') | |
e7fc914b | 1700 | c = read_escape (readcharfun, 0); |
fe0e03f3 KH |
1701 | else if (BASE_LEADING_CODE_P (c)) |
1702 | c = read_multibyte (c, readcharfun); | |
078e7b4a | 1703 | |
6f7f43d5 | 1704 | return make_number (c); |
078e7b4a JB |
1705 | } |
1706 | ||
1707 | case '\"': | |
1708 | { | |
1709 | register char *p = read_buffer; | |
1710 | register char *end = read_buffer + read_buffer_size; | |
1711 | register int c; | |
e7fc914b KH |
1712 | /* Nonzero if we saw an escape sequence specifying |
1713 | a multibyte character. */ | |
1714 | int force_multibyte = 0; | |
1715 | /* Nonzero if we saw an escape sequence specifying | |
1716 | a single-byte character. */ | |
1717 | int force_singlebyte = 0; | |
078e7b4a | 1718 | int cancel = 0; |
e7fc914b | 1719 | int nchars; |
078e7b4a JB |
1720 | |
1721 | while ((c = READCHAR) >= 0 | |
1722 | && c != '\"') | |
1723 | { | |
1724 | if (p == end) | |
1725 | { | |
1726 | char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
1727 | p += new - read_buffer; | |
1728 | read_buffer += new - read_buffer; | |
1729 | end = read_buffer + read_buffer_size; | |
1730 | } | |
1731 | if (c == '\\') | |
03e88613 | 1732 | { |
e7fc914b | 1733 | c = read_escape (readcharfun, 1); |
03e88613 RS |
1734 | if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_META))) |
1735 | { | |
d5b28a9d AS |
1736 | unsigned char workbuf[4]; |
1737 | unsigned char *str = workbuf; | |
03e88613 RS |
1738 | int length; |
1739 | ||
1740 | length = non_ascii_char_to_string (c, workbuf, &str); | |
e7fc914b KH |
1741 | if (length > 1) |
1742 | force_multibyte = 1; | |
03e88613 RS |
1743 | |
1744 | if (p + length > end) | |
1745 | { | |
1746 | char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
1747 | p += new - read_buffer; | |
1748 | read_buffer += new - read_buffer; | |
1749 | end = read_buffer + read_buffer_size; | |
1750 | } | |
1751 | ||
1752 | bcopy (str, p, length); | |
1753 | p += length; | |
1754 | continue; | |
1755 | } | |
e7fc914b KH |
1756 | else if (! ASCII_BYTE_P (c)) |
1757 | force_singlebyte = 1; | |
03e88613 | 1758 | } |
6f7f43d5 | 1759 | |
078e7b4a | 1760 | /* c is -1 if \ newline has just been seen */ |
f405a585 | 1761 | if (c == -1) |
078e7b4a JB |
1762 | { |
1763 | if (p == read_buffer) | |
1764 | cancel = 1; | |
1765 | } | |
1766 | else | |
f943104a | 1767 | { |
988c2f83 RS |
1768 | /* Allow `\C- ' and `\C-?'. */ |
1769 | if (c == (CHAR_CTL | ' ')) | |
1770 | c = 0; | |
1771 | else if (c == (CHAR_CTL | '?')) | |
1772 | c = 127; | |
1773 | ||
f943104a KH |
1774 | if (c & CHAR_META) |
1775 | /* Move the meta bit to the right place for a string. */ | |
1776 | c = (c & ~CHAR_META) | 0x80; | |
1777 | if (c & ~0xff) | |
1778 | error ("Invalid modifier in string"); | |
1779 | *p++ = c; | |
1780 | } | |
078e7b4a | 1781 | } |
6f7f43d5 RS |
1782 | if (c < 0) |
1783 | return Fsignal (Qend_of_file, Qnil); | |
078e7b4a JB |
1784 | |
1785 | /* If purifying, and string starts with \ newline, | |
1786 | return zero instead. This is for doc strings | |
08564963 | 1787 | that we are really going to find in etc/DOC.nn.nn */ |
265a9e55 | 1788 | if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel) |
078e7b4a JB |
1789 | return make_number (0); |
1790 | ||
e7fc914b KH |
1791 | if (force_singlebyte && force_multibyte) |
1792 | error ("Multibyte and single-byte escapes in one string constant"); | |
1793 | ||
1794 | if (force_singlebyte) | |
1795 | nchars = p - read_buffer; | |
1796 | else if (! NILP (buffer_defaults.enable_multibyte_characters) | |
1797 | || force_multibyte) | |
1d10ac5b | 1798 | nchars = multibyte_chars_in_text (read_buffer, p - read_buffer); |
e28552a4 | 1799 | else |
e7fc914b KH |
1800 | nchars = p - read_buffer; |
1801 | ||
1802 | if (read_pure) | |
491f16a2 RS |
1803 | return make_pure_string (read_buffer, nchars, p - read_buffer, |
1804 | (force_multibyte | |
1805 | || (p - read_buffer != nchars))); | |
1806 | return make_specified_string (read_buffer, nchars, p - read_buffer, | |
1807 | (force_multibyte | |
1808 | || (p - read_buffer != nchars))); | |
078e7b4a JB |
1809 | } |
1810 | ||
109d300c JB |
1811 | case '.': |
1812 | { | |
1813 | #ifdef LISP_FLOAT_TYPE | |
1814 | /* If a period is followed by a number, then we should read it | |
1815 | as a floating point number. Otherwise, it denotes a dotted | |
1816 | pair. */ | |
1817 | int next_char = READCHAR; | |
1818 | UNREAD (next_char); | |
1819 | ||
075027b1 | 1820 | if (! (next_char >= '0' && next_char <= '9')) |
109d300c JB |
1821 | #endif |
1822 | { | |
6428369f KH |
1823 | *pch = c; |
1824 | return Qnil; | |
109d300c JB |
1825 | } |
1826 | ||
1827 | /* Otherwise, we fall through! Note that the atom-reading loop | |
1828 | below will now loop at least once, assuring that we will not | |
1829 | try to UNREAD two characters in a row. */ | |
1830 | } | |
078e7b4a | 1831 | default: |
17634846 | 1832 | default_label: |
078e7b4a JB |
1833 | if (c <= 040) goto retry; |
1834 | { | |
1835 | register char *p = read_buffer; | |
481c6336 | 1836 | int quoted = 0; |
078e7b4a JB |
1837 | |
1838 | { | |
1839 | register char *end = read_buffer + read_buffer_size; | |
1840 | ||
6f7f43d5 RS |
1841 | while (c > 040 |
1842 | && !(c == '\"' || c == '\'' || c == ';' || c == '?' | |
1843 | || c == '(' || c == ')' | |
109d300c | 1844 | #ifndef LISP_FLOAT_TYPE |
6f7f43d5 RS |
1845 | /* If we have floating-point support, then we need |
1846 | to allow <digits><dot><digits>. */ | |
1847 | || c =='.' | |
078e7b4a | 1848 | #endif /* not LISP_FLOAT_TYPE */ |
6f7f43d5 RS |
1849 | || c == '[' || c == ']' || c == '#' |
1850 | )) | |
078e7b4a JB |
1851 | { |
1852 | if (p == end) | |
1853 | { | |
1854 | register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
1855 | p += new - read_buffer; | |
1856 | read_buffer += new - read_buffer; | |
1857 | end = read_buffer + read_buffer_size; | |
1858 | } | |
1859 | if (c == '\\') | |
481c6336 RS |
1860 | { |
1861 | c = READCHAR; | |
1862 | quoted = 1; | |
1863 | } | |
6f7f43d5 | 1864 | |
078e7b4a | 1865 | *p++ = c; |
6f7f43d5 | 1866 | |
078e7b4a JB |
1867 | c = READCHAR; |
1868 | } | |
1869 | ||
1870 | if (p == end) | |
1871 | { | |
1872 | char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
1873 | p += new - read_buffer; | |
1874 | read_buffer += new - read_buffer; | |
1875 | /* end = read_buffer + read_buffer_size; */ | |
1876 | } | |
1877 | *p = 0; | |
1878 | if (c >= 0) | |
1879 | UNREAD (c); | |
1880 | } | |
1881 | ||
4ad679f9 | 1882 | if (!quoted && !uninterned_symbol) |
481c6336 RS |
1883 | { |
1884 | register char *p1; | |
1885 | register Lisp_Object val; | |
1886 | p1 = read_buffer; | |
1887 | if (*p1 == '+' || *p1 == '-') p1++; | |
1888 | /* Is it an integer? */ | |
1889 | if (p1 != p) | |
1890 | { | |
1891 | while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++; | |
dbc4e1c1 | 1892 | #ifdef LISP_FLOAT_TYPE |
481c6336 RS |
1893 | /* Integers can have trailing decimal points. */ |
1894 | if (p1 > read_buffer && p1 < p && *p1 == '.') p1++; | |
dbc4e1c1 | 1895 | #endif |
481c6336 RS |
1896 | if (p1 == p) |
1897 | /* It is an integer. */ | |
1898 | { | |
dbc4e1c1 | 1899 | #ifdef LISP_FLOAT_TYPE |
481c6336 RS |
1900 | if (p1[-1] == '.') |
1901 | p1[-1] = '\0'; | |
dbc4e1c1 | 1902 | #endif |
faca07fb RS |
1903 | if (sizeof (int) == sizeof (EMACS_INT)) |
1904 | XSETINT (val, atoi (read_buffer)); | |
1905 | else if (sizeof (long) == sizeof (EMACS_INT)) | |
1906 | XSETINT (val, atol (read_buffer)); | |
1907 | else | |
1908 | abort (); | |
481c6336 RS |
1909 | return val; |
1910 | } | |
1911 | } | |
078e7b4a | 1912 | #ifdef LISP_FLOAT_TYPE |
481c6336 | 1913 | if (isfloat_string (read_buffer)) |
eb659c41 RS |
1914 | { |
1915 | double value = atof (read_buffer); | |
1916 | if (read_buffer[0] == '-' && value == 0.0) | |
1917 | value *= -1.0; | |
1918 | /* The only way this can be true, after isfloat_string | |
1919 | returns 1, is if the input ends in e+INF or e+NaN. */ | |
1920 | if (p[-1] == 'F' || p[-1] == 'N') | |
1921 | { | |
1922 | if (p[-1] == 'N') | |
1923 | value = 0.0 / 0.0; | |
1924 | else if (read_buffer[0] == '-') | |
1925 | value = -1.0e999; | |
1926 | else | |
1927 | value = 1.0e999; | |
1928 | } | |
1929 | return make_float (value); | |
1930 | } | |
078e7b4a | 1931 | #endif |
481c6336 | 1932 | } |
078e7b4a | 1933 | |
4ad679f9 EN |
1934 | if (uninterned_symbol) |
1935 | return make_symbol (read_buffer); | |
1936 | else | |
1937 | return intern (read_buffer); | |
078e7b4a JB |
1938 | } |
1939 | } | |
1940 | } | |
1941 | \f | |
1942 | #ifdef LISP_FLOAT_TYPE | |
1943 | ||
078e7b4a JB |
1944 | #define LEAD_INT 1 |
1945 | #define DOT_CHAR 2 | |
1946 | #define TRAIL_INT 4 | |
1947 | #define E_CHAR 8 | |
1948 | #define EXP_INT 16 | |
1949 | ||
1950 | int | |
1951 | isfloat_string (cp) | |
1952 | register char *cp; | |
1953 | { | |
1954 | register state; | |
1955 | ||
1956 | state = 0; | |
1957 | if (*cp == '+' || *cp == '-') | |
1958 | cp++; | |
1959 | ||
075027b1 | 1960 | if (*cp >= '0' && *cp <= '9') |
078e7b4a JB |
1961 | { |
1962 | state |= LEAD_INT; | |
075027b1 RS |
1963 | while (*cp >= '0' && *cp <= '9') |
1964 | cp++; | |
078e7b4a JB |
1965 | } |
1966 | if (*cp == '.') | |
1967 | { | |
1968 | state |= DOT_CHAR; | |
1969 | cp++; | |
1970 | } | |
075027b1 | 1971 | if (*cp >= '0' && *cp <= '9') |
078e7b4a JB |
1972 | { |
1973 | state |= TRAIL_INT; | |
075027b1 | 1974 | while (*cp >= '0' && *cp <= '9') |
078e7b4a JB |
1975 | cp++; |
1976 | } | |
a35f88bf | 1977 | if (*cp == 'e' || *cp == 'E') |
078e7b4a JB |
1978 | { |
1979 | state |= E_CHAR; | |
1980 | cp++; | |
e73997a1 RS |
1981 | if (*cp == '+' || *cp == '-') |
1982 | cp++; | |
078e7b4a | 1983 | } |
078e7b4a | 1984 | |
075027b1 | 1985 | if (*cp >= '0' && *cp <= '9') |
078e7b4a JB |
1986 | { |
1987 | state |= EXP_INT; | |
075027b1 | 1988 | while (*cp >= '0' && *cp <= '9') |
078e7b4a JB |
1989 | cp++; |
1990 | } | |
eb659c41 RS |
1991 | else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F') |
1992 | { | |
1993 | state |= EXP_INT; | |
1994 | cp += 3; | |
1995 | } | |
1996 | else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N') | |
1997 | { | |
1998 | state |= EXP_INT; | |
1999 | cp += 3; | |
2000 | } | |
2001 | ||
37579d7c | 2002 | return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f')) |
078e7b4a | 2003 | && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT) |
151bdc83 | 2004 | || state == (DOT_CHAR|TRAIL_INT) |
078e7b4a | 2005 | || state == (LEAD_INT|E_CHAR|EXP_INT) |
151bdc83 JB |
2006 | || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT) |
2007 | || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT))); | |
078e7b4a JB |
2008 | } |
2009 | #endif /* LISP_FLOAT_TYPE */ | |
2010 | \f | |
2011 | static Lisp_Object | |
2012 | read_vector (readcharfun) | |
2013 | Lisp_Object readcharfun; | |
2014 | { | |
2015 | register int i; | |
2016 | register int size; | |
2017 | register Lisp_Object *ptr; | |
2018 | register Lisp_Object tem, vector; | |
2019 | register struct Lisp_Cons *otem; | |
2020 | Lisp_Object len; | |
2021 | ||
2022 | tem = read_list (1, readcharfun); | |
2023 | len = Flength (tem); | |
2024 | vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil)); | |
2025 | ||
2026 | ||
2027 | size = XVECTOR (vector)->size; | |
2028 | ptr = XVECTOR (vector)->contents; | |
2029 | for (i = 0; i < size; i++) | |
2030 | { | |
2031 | ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem); | |
2032 | otem = XCONS (tem); | |
2033 | tem = Fcdr (tem); | |
2034 | free_cons (otem); | |
2035 | } | |
2036 | return vector; | |
2037 | } | |
2038 | ||
6f7f43d5 RS |
2039 | /* FLAG = 1 means check for ] to terminate rather than ) and . |
2040 | FLAG = -1 means check for starting with defun | |
078e7b4a JB |
2041 | and make structure pure. */ |
2042 | ||
2043 | static Lisp_Object | |
2044 | read_list (flag, readcharfun) | |
2045 | int flag; | |
2046 | register Lisp_Object readcharfun; | |
2047 | { | |
2048 | /* -1 means check next element for defun, | |
2049 | 0 means don't check, | |
2050 | 1 means already checked and found defun. */ | |
2051 | int defunflag = flag < 0 ? -1 : 0; | |
2052 | Lisp_Object val, tail; | |
2053 | register Lisp_Object elt, tem; | |
2054 | struct gcpro gcpro1, gcpro2; | |
821d417e | 2055 | /* 0 is the normal case. |
b2a30870 RS |
2056 | 1 means this list is a doc reference; replace it with the number 0. |
2057 | 2 means this list is a doc reference; replace it with the doc string. */ | |
821d417e | 2058 | int doc_reference = 0; |
078e7b4a | 2059 | |
17634846 RS |
2060 | /* Initialize this to 1 if we are reading a list. */ |
2061 | int first_in_list = flag <= 0; | |
2062 | ||
078e7b4a JB |
2063 | val = Qnil; |
2064 | tail = Qnil; | |
2065 | ||
2066 | while (1) | |
2067 | { | |
e28552a4 | 2068 | int ch; |
078e7b4a | 2069 | GCPRO2 (val, tail); |
17634846 | 2070 | elt = read1 (readcharfun, &ch, first_in_list); |
078e7b4a | 2071 | UNGCPRO; |
20ea2964 | 2072 | |
17634846 RS |
2073 | first_in_list = 0; |
2074 | ||
821d417e | 2075 | /* While building, if the list starts with #$, treat it specially. */ |
20ea2964 | 2076 | if (EQ (elt, Vload_file_name) |
d49f0c1a | 2077 | && ! NILP (elt) |
821d417e RS |
2078 | && !NILP (Vpurify_flag)) |
2079 | { | |
2080 | if (NILP (Vdoc_file_name)) | |
2081 | /* We have not yet called Snarf-documentation, so assume | |
2082 | this file is described in the DOC-MM.NN file | |
2083 | and Snarf-documentation will fill in the right value later. | |
2084 | For now, replace the whole list with 0. */ | |
2085 | doc_reference = 1; | |
2086 | else | |
2087 | /* We have already called Snarf-documentation, so make a relative | |
2088 | file name for this file, so it can be found properly | |
2089 | in the installed Lisp directory. | |
2090 | We don't use Fexpand_file_name because that would make | |
2091 | the directory absolute now. */ | |
2092 | elt = concat2 (build_string ("../lisp/"), | |
2093 | Ffile_name_nondirectory (elt)); | |
2094 | } | |
b2a30870 | 2095 | else if (EQ (elt, Vload_file_name) |
d49f0c1a | 2096 | && ! NILP (elt) |
b2a30870 RS |
2097 | && load_force_doc_strings) |
2098 | doc_reference = 2; | |
20ea2964 | 2099 | |
6428369f | 2100 | if (ch) |
078e7b4a JB |
2101 | { |
2102 | if (flag > 0) | |
2103 | { | |
6428369f | 2104 | if (ch == ']') |
078e7b4a | 2105 | return val; |
821d417e RS |
2106 | Fsignal (Qinvalid_read_syntax, |
2107 | Fcons (make_string (") or . in a vector", 18), Qnil)); | |
078e7b4a | 2108 | } |
6428369f | 2109 | if (ch == ')') |
078e7b4a | 2110 | return val; |
6428369f | 2111 | if (ch == '.') |
078e7b4a JB |
2112 | { |
2113 | GCPRO2 (val, tail); | |
265a9e55 | 2114 | if (!NILP (tail)) |
078e7b4a JB |
2115 | XCONS (tail)->cdr = read0 (readcharfun); |
2116 | else | |
2117 | val = read0 (readcharfun); | |
17634846 | 2118 | read1 (readcharfun, &ch, 0); |
078e7b4a | 2119 | UNGCPRO; |
6428369f | 2120 | if (ch == ')') |
821d417e RS |
2121 | { |
2122 | if (doc_reference == 1) | |
2123 | return make_number (0); | |
b2a30870 RS |
2124 | if (doc_reference == 2) |
2125 | { | |
2126 | /* Get a doc string from the file we are loading. | |
2127 | If it's in saved_doc_string, get it from there. */ | |
2128 | int pos = XINT (XCONS (val)->cdr); | |
2129 | if (pos >= saved_doc_string_position | |
2130 | && pos < (saved_doc_string_position | |
2131 | + saved_doc_string_length)) | |
2132 | { | |
2133 | int start = pos - saved_doc_string_position; | |
2134 | int from, to; | |
2135 | ||
2136 | /* Process quoting with ^A, | |
2137 | and find the end of the string, | |
2138 | which is marked with ^_ (037). */ | |
2139 | for (from = start, to = start; | |
2140 | saved_doc_string[from] != 037;) | |
2141 | { | |
2142 | int c = saved_doc_string[from++]; | |
2143 | if (c == 1) | |
2144 | { | |
2145 | c = saved_doc_string[from++]; | |
2146 | if (c == 1) | |
2147 | saved_doc_string[to++] = c; | |
2148 | else if (c == '0') | |
2149 | saved_doc_string[to++] = 0; | |
2150 | else if (c == '_') | |
2151 | saved_doc_string[to++] = 037; | |
2152 | } | |
2153 | else | |
2154 | saved_doc_string[to++] = c; | |
2155 | } | |
2156 | ||
2157 | return make_string (saved_doc_string + start, | |
2158 | to - start); | |
2159 | } | |
2160 | else | |
2161 | return read_doc_string (val); | |
2162 | } | |
2163 | ||
821d417e RS |
2164 | return val; |
2165 | } | |
078e7b4a JB |
2166 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil)); |
2167 | } | |
2168 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil)); | |
2169 | } | |
2170 | tem = (read_pure && flag <= 0 | |
2171 | ? pure_cons (elt, Qnil) | |
2172 | : Fcons (elt, Qnil)); | |
265a9e55 | 2173 | if (!NILP (tail)) |
078e7b4a JB |
2174 | XCONS (tail)->cdr = tem; |
2175 | else | |
2176 | val = tem; | |
2177 | tail = tem; | |
2178 | if (defunflag < 0) | |
2179 | defunflag = EQ (elt, Qdefun); | |
2180 | else if (defunflag > 0) | |
2181 | read_pure = 1; | |
2182 | } | |
2183 | } | |
2184 | \f | |
2185 | Lisp_Object Vobarray; | |
2186 | Lisp_Object initial_obarray; | |
2187 | ||
d007f5c8 RS |
2188 | /* oblookup stores the bucket number here, for the sake of Funintern. */ |
2189 | ||
2190 | int oblookup_last_bucket_number; | |
2191 | ||
2192 | static int hash_string (); | |
2193 | Lisp_Object oblookup (); | |
2194 | ||
2195 | /* Get an error if OBARRAY is not an obarray. | |
2196 | If it is one, return it. */ | |
2197 | ||
078e7b4a JB |
2198 | Lisp_Object |
2199 | check_obarray (obarray) | |
2200 | Lisp_Object obarray; | |
2201 | { | |
cfff016d | 2202 | while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0) |
078e7b4a JB |
2203 | { |
2204 | /* If Vobarray is now invalid, force it to be valid. */ | |
2205 | if (EQ (Vobarray, obarray)) Vobarray = initial_obarray; | |
2206 | ||
2207 | obarray = wrong_type_argument (Qvectorp, obarray); | |
2208 | } | |
2209 | return obarray; | |
2210 | } | |
2211 | ||
d007f5c8 RS |
2212 | /* Intern the C string STR: return a symbol with that name, |
2213 | interned in the current obarray. */ | |
078e7b4a JB |
2214 | |
2215 | Lisp_Object | |
2216 | intern (str) | |
2217 | char *str; | |
2218 | { | |
2219 | Lisp_Object tem; | |
2220 | int len = strlen (str); | |
153a17b7 | 2221 | Lisp_Object obarray; |
078e7b4a | 2222 | |
153a17b7 | 2223 | obarray = Vobarray; |
cfff016d | 2224 | if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0) |
078e7b4a | 2225 | obarray = check_obarray (obarray); |
e28552a4 | 2226 | tem = oblookup (obarray, str, len, len); |
cfff016d | 2227 | if (SYMBOLP (tem)) |
078e7b4a | 2228 | return tem; |
87631ef7 | 2229 | return Fintern (make_string (str, len), obarray); |
078e7b4a | 2230 | } |
4ad679f9 EN |
2231 | |
2232 | /* Create an uninterned symbol with name STR. */ | |
2233 | ||
2234 | Lisp_Object | |
2235 | make_symbol (str) | |
2236 | char *str; | |
2237 | { | |
2238 | int len = strlen (str); | |
2239 | ||
2240 | return Fmake_symbol ((!NILP (Vpurify_flag) | |
491f16a2 | 2241 | ? make_pure_string (str, len, len, 0) |
4ad679f9 EN |
2242 | : make_string (str, len))); |
2243 | } | |
d007f5c8 | 2244 | \f |
078e7b4a JB |
2245 | DEFUN ("intern", Fintern, Sintern, 1, 2, 0, |
2246 | "Return the canonical symbol whose name is STRING.\n\ | |
2247 | If there is none, one is created by this function and returned.\n\ | |
2248 | A second optional argument specifies the obarray to use;\n\ | |
2249 | it defaults to the value of `obarray'.") | |
9391b698 EN |
2250 | (string, obarray) |
2251 | Lisp_Object string, obarray; | |
078e7b4a JB |
2252 | { |
2253 | register Lisp_Object tem, sym, *ptr; | |
2254 | ||
265a9e55 | 2255 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
2256 | obarray = check_obarray (obarray); |
2257 | ||
9391b698 | 2258 | CHECK_STRING (string, 0); |
078e7b4a | 2259 | |
e28552a4 RS |
2260 | tem = oblookup (obarray, XSTRING (string)->data, |
2261 | XSTRING (string)->size, | |
fc932ac6 | 2262 | STRING_BYTES (XSTRING (string))); |
cfff016d | 2263 | if (!INTEGERP (tem)) |
078e7b4a JB |
2264 | return tem; |
2265 | ||
265a9e55 | 2266 | if (!NILP (Vpurify_flag)) |
9391b698 EN |
2267 | string = Fpurecopy (string); |
2268 | sym = Fmake_symbol (string); | |
4ad679f9 | 2269 | XSYMBOL (sym)->obarray = obarray; |
078e7b4a | 2270 | |
a0549832 RS |
2271 | if (XSTRING (string)->data[0] == ':') |
2272 | XSYMBOL (sym)->value = sym; | |
2273 | ||
078e7b4a | 2274 | ptr = &XVECTOR (obarray)->contents[XINT (tem)]; |
cfff016d | 2275 | if (SYMBOLP (*ptr)) |
078e7b4a JB |
2276 | XSYMBOL (sym)->next = XSYMBOL (*ptr); |
2277 | else | |
2278 | XSYMBOL (sym)->next = 0; | |
2279 | *ptr = sym; | |
2280 | return sym; | |
2281 | } | |
2282 | ||
2283 | DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0, | |
2284 | "Return the canonical symbol whose name is STRING, or nil if none exists.\n\ | |
2285 | A second optional argument specifies the obarray to use;\n\ | |
2286 | it defaults to the value of `obarray'.") | |
9391b698 EN |
2287 | (string, obarray) |
2288 | Lisp_Object string, obarray; | |
078e7b4a JB |
2289 | { |
2290 | register Lisp_Object tem; | |
2291 | ||
265a9e55 | 2292 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
2293 | obarray = check_obarray (obarray); |
2294 | ||
9391b698 | 2295 | CHECK_STRING (string, 0); |
078e7b4a | 2296 | |
e28552a4 RS |
2297 | tem = oblookup (obarray, XSTRING (string)->data, |
2298 | XSTRING (string)->size, | |
fc932ac6 | 2299 | STRING_BYTES (XSTRING (string))); |
cfff016d | 2300 | if (!INTEGERP (tem)) |
078e7b4a JB |
2301 | return tem; |
2302 | return Qnil; | |
2303 | } | |
d007f5c8 RS |
2304 | \f |
2305 | DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0, | |
2306 | "Delete the symbol named NAME, if any, from OBARRAY.\n\ | |
2307 | The value is t if a symbol was found and deleted, nil otherwise.\n\ | |
2308 | NAME may be a string or a symbol. If it is a symbol, that symbol\n\ | |
2309 | is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\ | |
2310 | OBARRAY defaults to the value of the variable `obarray'.") | |
2311 | (name, obarray) | |
2312 | Lisp_Object name, obarray; | |
2313 | { | |
2314 | register Lisp_Object string, tem; | |
2315 | int hash; | |
2316 | ||
2317 | if (NILP (obarray)) obarray = Vobarray; | |
2318 | obarray = check_obarray (obarray); | |
2319 | ||
2320 | if (SYMBOLP (name)) | |
2321 | XSETSTRING (string, XSYMBOL (name)->name); | |
2322 | else | |
2323 | { | |
2324 | CHECK_STRING (name, 0); | |
2325 | string = name; | |
2326 | } | |
2327 | ||
e28552a4 RS |
2328 | tem = oblookup (obarray, XSTRING (string)->data, |
2329 | XSTRING (string)->size, | |
fc932ac6 | 2330 | STRING_BYTES (XSTRING (string))); |
d007f5c8 RS |
2331 | if (INTEGERP (tem)) |
2332 | return Qnil; | |
2333 | /* If arg was a symbol, don't delete anything but that symbol itself. */ | |
2334 | if (SYMBOLP (name) && !EQ (name, tem)) | |
2335 | return Qnil; | |
2336 | ||
ca69c42f RS |
2337 | XSYMBOL (tem)->obarray = Qnil; |
2338 | ||
d007f5c8 RS |
2339 | hash = oblookup_last_bucket_number; |
2340 | ||
2341 | if (EQ (XVECTOR (obarray)->contents[hash], tem)) | |
b2a30870 RS |
2342 | { |
2343 | if (XSYMBOL (tem)->next) | |
2344 | XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next); | |
2345 | else | |
2346 | XSETINT (XVECTOR (obarray)->contents[hash], 0); | |
2347 | } | |
d007f5c8 RS |
2348 | else |
2349 | { | |
2350 | Lisp_Object tail, following; | |
2351 | ||
2352 | for (tail = XVECTOR (obarray)->contents[hash]; | |
2353 | XSYMBOL (tail)->next; | |
2354 | tail = following) | |
2355 | { | |
2356 | XSETSYMBOL (following, XSYMBOL (tail)->next); | |
2357 | if (EQ (following, tem)) | |
2358 | { | |
2359 | XSYMBOL (tail)->next = XSYMBOL (following)->next; | |
2360 | break; | |
2361 | } | |
2362 | } | |
2363 | } | |
2364 | ||
2365 | return Qt; | |
2366 | } | |
2367 | \f | |
2368 | /* Return the symbol in OBARRAY whose names matches the string | |
e28552a4 RS |
2369 | of SIZE characters (SIZE_BYTE bytes) at PTR. |
2370 | If there is no such symbol in OBARRAY, return nil. | |
d007f5c8 RS |
2371 | |
2372 | Also store the bucket number in oblookup_last_bucket_number. */ | |
078e7b4a JB |
2373 | |
2374 | Lisp_Object | |
e28552a4 | 2375 | oblookup (obarray, ptr, size, size_byte) |
078e7b4a JB |
2376 | Lisp_Object obarray; |
2377 | register char *ptr; | |
e28552a4 | 2378 | int size, size_byte; |
078e7b4a | 2379 | { |
7a70b397 RS |
2380 | int hash; |
2381 | int obsize; | |
078e7b4a JB |
2382 | register Lisp_Object tail; |
2383 | Lisp_Object bucket, tem; | |
2384 | ||
cfff016d | 2385 | if (!VECTORP (obarray) |
7c79a684 | 2386 | || (obsize = XVECTOR (obarray)->size) == 0) |
078e7b4a JB |
2387 | { |
2388 | obarray = check_obarray (obarray); | |
2389 | obsize = XVECTOR (obarray)->size; | |
2390 | } | |
519418b3 RS |
2391 | /* This is sometimes needed in the middle of GC. */ |
2392 | obsize &= ~ARRAY_MARK_FLAG; | |
078e7b4a | 2393 | /* Combining next two lines breaks VMS C 2.3. */ |
e28552a4 | 2394 | hash = hash_string (ptr, size_byte); |
078e7b4a JB |
2395 | hash %= obsize; |
2396 | bucket = XVECTOR (obarray)->contents[hash]; | |
d007f5c8 | 2397 | oblookup_last_bucket_number = hash; |
078e7b4a JB |
2398 | if (XFASTINT (bucket) == 0) |
2399 | ; | |
cfff016d | 2400 | else if (!SYMBOLP (bucket)) |
078e7b4a | 2401 | error ("Bad data in guts of obarray"); /* Like CADR error message */ |
d007f5c8 RS |
2402 | else |
2403 | for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next)) | |
078e7b4a | 2404 | { |
fc932ac6 | 2405 | if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte |
e28552a4 RS |
2406 | && XSYMBOL (tail)->name->size == size |
2407 | && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte)) | |
078e7b4a JB |
2408 | return tail; |
2409 | else if (XSYMBOL (tail)->next == 0) | |
2410 | break; | |
2411 | } | |
1805de4f | 2412 | XSETINT (tem, hash); |
078e7b4a JB |
2413 | return tem; |
2414 | } | |
2415 | ||
2416 | static int | |
2417 | hash_string (ptr, len) | |
2418 | unsigned char *ptr; | |
2419 | int len; | |
2420 | { | |
2421 | register unsigned char *p = ptr; | |
2422 | register unsigned char *end = p + len; | |
2423 | register unsigned char c; | |
2424 | register int hash = 0; | |
2425 | ||
2426 | while (p != end) | |
2427 | { | |
2428 | c = *p++; | |
2429 | if (c >= 0140) c -= 40; | |
2430 | hash = ((hash<<3) + (hash>>28) + c); | |
2431 | } | |
2432 | return hash & 07777777777; | |
2433 | } | |
d007f5c8 | 2434 | \f |
078e7b4a JB |
2435 | void |
2436 | map_obarray (obarray, fn, arg) | |
2437 | Lisp_Object obarray; | |
d5b28a9d | 2438 | void (*fn) P_ ((Lisp_Object, Lisp_Object)); |
078e7b4a JB |
2439 | Lisp_Object arg; |
2440 | { | |
2441 | register int i; | |
2442 | register Lisp_Object tail; | |
2443 | CHECK_VECTOR (obarray, 1); | |
2444 | for (i = XVECTOR (obarray)->size - 1; i >= 0; i--) | |
2445 | { | |
2446 | tail = XVECTOR (obarray)->contents[i]; | |
4f5c4403 | 2447 | if (SYMBOLP (tail)) |
078e7b4a JB |
2448 | while (1) |
2449 | { | |
2450 | (*fn) (tail, arg); | |
2451 | if (XSYMBOL (tail)->next == 0) | |
2452 | break; | |
1805de4f | 2453 | XSETSYMBOL (tail, XSYMBOL (tail)->next); |
078e7b4a JB |
2454 | } |
2455 | } | |
2456 | } | |
2457 | ||
d5b28a9d | 2458 | void |
078e7b4a JB |
2459 | mapatoms_1 (sym, function) |
2460 | Lisp_Object sym, function; | |
2461 | { | |
2462 | call1 (function, sym); | |
2463 | } | |
2464 | ||
2465 | DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0, | |
2466 | "Call FUNCTION on every symbol in OBARRAY.\n\ | |
2467 | OBARRAY defaults to the value of `obarray'.") | |
2468 | (function, obarray) | |
2469 | Lisp_Object function, obarray; | |
2470 | { | |
2471 | Lisp_Object tem; | |
2472 | ||
265a9e55 | 2473 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
2474 | obarray = check_obarray (obarray); |
2475 | ||
2476 | map_obarray (obarray, mapatoms_1, function); | |
2477 | return Qnil; | |
2478 | } | |
2479 | ||
5e88a39e | 2480 | #define OBARRAY_SIZE 1511 |
078e7b4a JB |
2481 | |
2482 | void | |
2483 | init_obarray () | |
2484 | { | |
2485 | Lisp_Object oblength; | |
2486 | int hash; | |
2487 | Lisp_Object *tem; | |
2488 | ||
baf69866 | 2489 | XSETFASTINT (oblength, OBARRAY_SIZE); |
078e7b4a | 2490 | |
491f16a2 | 2491 | Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0)); |
078e7b4a JB |
2492 | Vobarray = Fmake_vector (oblength, make_number (0)); |
2493 | initial_obarray = Vobarray; | |
2494 | staticpro (&initial_obarray); | |
2495 | /* Intern nil in the obarray */ | |
4ad679f9 | 2496 | XSYMBOL (Qnil)->obarray = Vobarray; |
078e7b4a JB |
2497 | /* These locals are to kludge around a pyramid compiler bug. */ |
2498 | hash = hash_string ("nil", 3); | |
2499 | /* Separate statement here to avoid VAXC bug. */ | |
2500 | hash %= OBARRAY_SIZE; | |
2501 | tem = &XVECTOR (Vobarray)->contents[hash]; | |
2502 | *tem = Qnil; | |
2503 | ||
491f16a2 | 2504 | Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0)); |
078e7b4a JB |
2505 | XSYMBOL (Qnil)->function = Qunbound; |
2506 | XSYMBOL (Qunbound)->value = Qunbound; | |
2507 | XSYMBOL (Qunbound)->function = Qunbound; | |
2508 | ||
2509 | Qt = intern ("t"); | |
2510 | XSYMBOL (Qnil)->value = Qnil; | |
2511 | XSYMBOL (Qnil)->plist = Qnil; | |
2512 | XSYMBOL (Qt)->value = Qt; | |
2513 | ||
2514 | /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */ | |
2515 | Vpurify_flag = Qt; | |
2516 | ||
2517 | Qvariable_documentation = intern ("variable-documentation"); | |
0f73bb1c | 2518 | staticpro (&Qvariable_documentation); |
078e7b4a JB |
2519 | |
2520 | read_buffer_size = 100; | |
2521 | read_buffer = (char *) malloc (read_buffer_size); | |
2522 | } | |
2523 | \f | |
2524 | void | |
2525 | defsubr (sname) | |
2526 | struct Lisp_Subr *sname; | |
2527 | { | |
2528 | Lisp_Object sym; | |
2529 | sym = intern (sname->symbol_name); | |
1805de4f | 2530 | XSETSUBR (XSYMBOL (sym)->function, sname); |
078e7b4a JB |
2531 | } |
2532 | ||
2533 | #ifdef NOTDEF /* use fset in subr.el now */ | |
2534 | void | |
2535 | defalias (sname, string) | |
2536 | struct Lisp_Subr *sname; | |
2537 | char *string; | |
2538 | { | |
2539 | Lisp_Object sym; | |
2540 | sym = intern (string); | |
1805de4f | 2541 | XSETSUBR (XSYMBOL (sym)->function, sname); |
078e7b4a JB |
2542 | } |
2543 | #endif /* NOTDEF */ | |
2544 | ||
078e7b4a | 2545 | /* Define an "integer variable"; a symbol whose value is forwarded |
1a0f90f7 | 2546 | to a C variable of type int. Sample call: */ |
950c215d | 2547 | /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */ |
078e7b4a | 2548 | void |
e9e00ff2 | 2549 | defvar_int (namestring, address) |
078e7b4a JB |
2550 | char *namestring; |
2551 | int *address; | |
078e7b4a | 2552 | { |
1a0f90f7 | 2553 | Lisp_Object sym, val; |
078e7b4a | 2554 | sym = intern (namestring); |
1a0f90f7 | 2555 | val = allocate_misc (); |
47e28b2c | 2556 | XMISCTYPE (val) = Lisp_Misc_Intfwd; |
fc1e7df5 | 2557 | XINTFWD (val)->intvar = address; |
1a0f90f7 | 2558 | XSYMBOL (sym)->value = val; |
078e7b4a JB |
2559 | } |
2560 | ||
2561 | /* Similar but define a variable whose value is T if address contains 1, | |
1a0f90f7 | 2562 | NIL if address contains 0 */ |
078e7b4a | 2563 | void |
e9e00ff2 | 2564 | defvar_bool (namestring, address) |
078e7b4a JB |
2565 | char *namestring; |
2566 | int *address; | |
078e7b4a | 2567 | { |
1a0f90f7 | 2568 | Lisp_Object sym, val; |
078e7b4a | 2569 | sym = intern (namestring); |
1a0f90f7 | 2570 | val = allocate_misc (); |
47e28b2c | 2571 | XMISCTYPE (val) = Lisp_Misc_Boolfwd; |
fc1e7df5 | 2572 | XBOOLFWD (val)->boolvar = address; |
1a0f90f7 | 2573 | XSYMBOL (sym)->value = val; |
078e7b4a JB |
2574 | } |
2575 | ||
1a0f90f7 KH |
2576 | /* Similar but define a variable whose value is the Lisp Object stored |
2577 | at address. Two versions: with and without gc-marking of the C | |
2578 | variable. The nopro version is used when that variable will be | |
2579 | gc-marked for some other reason, since marking the same slot twice | |
2580 | can cause trouble with strings. */ | |
078e7b4a | 2581 | void |
1a0f90f7 | 2582 | defvar_lisp_nopro (namestring, address) |
078e7b4a JB |
2583 | char *namestring; |
2584 | Lisp_Object *address; | |
078e7b4a | 2585 | { |
1a0f90f7 | 2586 | Lisp_Object sym, val; |
078e7b4a | 2587 | sym = intern (namestring); |
1a0f90f7 | 2588 | val = allocate_misc (); |
47e28b2c | 2589 | XMISCTYPE (val) = Lisp_Misc_Objfwd; |
fc1e7df5 | 2590 | XOBJFWD (val)->objvar = address; |
1a0f90f7 | 2591 | XSYMBOL (sym)->value = val; |
078e7b4a JB |
2592 | } |
2593 | ||
078e7b4a | 2594 | void |
1a0f90f7 | 2595 | defvar_lisp (namestring, address) |
078e7b4a JB |
2596 | char *namestring; |
2597 | Lisp_Object *address; | |
078e7b4a | 2598 | { |
1a0f90f7 KH |
2599 | defvar_lisp_nopro (namestring, address); |
2600 | staticpro (address); | |
078e7b4a JB |
2601 | } |
2602 | ||
2603 | #ifndef standalone | |
2604 | ||
2605 | /* Similar but define a variable whose value is the Lisp Object stored in | |
2836d9a4 KH |
2606 | the current buffer. address is the address of the slot in the buffer |
2607 | that is current now. */ | |
078e7b4a JB |
2608 | |
2609 | void | |
4360b0c6 | 2610 | defvar_per_buffer (namestring, address, type, doc) |
078e7b4a JB |
2611 | char *namestring; |
2612 | Lisp_Object *address; | |
4360b0c6 | 2613 | Lisp_Object type; |
078e7b4a JB |
2614 | char *doc; |
2615 | { | |
1a0f90f7 | 2616 | Lisp_Object sym, val; |
078e7b4a JB |
2617 | int offset; |
2618 | extern struct buffer buffer_local_symbols; | |
2619 | ||
2620 | sym = intern (namestring); | |
1a0f90f7 | 2621 | val = allocate_misc (); |
078e7b4a JB |
2622 | offset = (char *)address - (char *)current_buffer; |
2623 | ||
47e28b2c | 2624 | XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd; |
fc1e7df5 | 2625 | XBUFFER_OBJFWD (val)->offset = offset; |
1a0f90f7 | 2626 | XSYMBOL (sym)->value = val; |
078e7b4a | 2627 | *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym; |
4360b0c6 | 2628 | *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type; |
2836d9a4 | 2629 | if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0) |
078e7b4a JB |
2630 | /* Did a DEFVAR_PER_BUFFER without initializing the corresponding |
2631 | slot of buffer_local_flags */ | |
2632 | abort (); | |
2633 | } | |
2634 | ||
2635 | #endif /* standalone */ | |
950c215d KH |
2636 | |
2637 | /* Similar but define a variable whose value is the Lisp Object stored | |
4ac38690 | 2638 | at a particular offset in the current kboard object. */ |
950c215d KH |
2639 | |
2640 | void | |
4ac38690 | 2641 | defvar_kboard (namestring, offset) |
950c215d KH |
2642 | char *namestring; |
2643 | int offset; | |
2644 | { | |
2645 | Lisp_Object sym, val; | |
2646 | sym = intern (namestring); | |
2647 | val = allocate_misc (); | |
47e28b2c | 2648 | XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd; |
4ac38690 | 2649 | XKBOARD_OBJFWD (val)->offset = offset; |
950c215d KH |
2650 | XSYMBOL (sym)->value = val; |
2651 | } | |
078e7b4a | 2652 | \f |
11938f10 KH |
2653 | /* Record the value of load-path used at the start of dumping |
2654 | so we can see if the site changed it later during dumping. */ | |
2655 | static Lisp_Object dump_path; | |
2656 | ||
d5b28a9d | 2657 | void |
279499f0 | 2658 | init_lread () |
078e7b4a | 2659 | { |
46947372 | 2660 | char *normal; |
e73997a1 | 2661 | int turn_off_warning = 0; |
078e7b4a | 2662 | |
9dff7c53 RS |
2663 | #ifdef HAVE_SETLOCALE |
2664 | /* Make sure numbers are parsed as we expect. */ | |
2665 | setlocale (LC_NUMERIC, "C"); | |
2666 | #endif /* HAVE_SETLOCALE */ | |
2667 | ||
279499f0 | 2668 | /* Compute the default load-path. */ |
46947372 JB |
2669 | #ifdef CANNOT_DUMP |
2670 | normal = PATH_LOADSEARCH; | |
e065a56e | 2671 | Vload_path = decode_env_path (0, normal); |
46947372 JB |
2672 | #else |
2673 | if (NILP (Vpurify_flag)) | |
2674 | normal = PATH_LOADSEARCH; | |
279499f0 | 2675 | else |
46947372 | 2676 | normal = PATH_DUMPLOADSEARCH; |
279499f0 | 2677 | |
46947372 JB |
2678 | /* In a dumped Emacs, we normally have to reset the value of |
2679 | Vload_path from PATH_LOADSEARCH, since the value that was dumped | |
2680 | uses ../lisp, instead of the path of the installed elisp | |
2681 | libraries. However, if it appears that Vload_path was changed | |
2682 | from the default before dumping, don't override that value. */ | |
4746118a JB |
2683 | if (initialized) |
2684 | { | |
4746118a | 2685 | if (! NILP (Fequal (dump_path, Vload_path))) |
80667d53 RS |
2686 | { |
2687 | Vload_path = decode_env_path (0, normal); | |
74180aa4 | 2688 | if (!NILP (Vinstallation_directory)) |
80667d53 | 2689 | { |
74180aa4 | 2690 | /* Add to the path the lisp subdir of the |
3a3056e5 RS |
2691 | installation dir, if it exists. */ |
2692 | Lisp_Object tem, tem1; | |
74180aa4 RS |
2693 | tem = Fexpand_file_name (build_string ("lisp"), |
2694 | Vinstallation_directory); | |
3a3056e5 RS |
2695 | tem1 = Ffile_exists_p (tem); |
2696 | if (!NILP (tem1)) | |
2697 | { | |
2698 | if (NILP (Fmember (tem, Vload_path))) | |
e73997a1 RS |
2699 | { |
2700 | turn_off_warning = 1; | |
2701 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2702 | } | |
3a3056e5 RS |
2703 | } |
2704 | else | |
2705 | /* That dir doesn't exist, so add the build-time | |
2706 | Lisp dirs instead. */ | |
2707 | Vload_path = nconc2 (Vload_path, dump_path); | |
c478f98c | 2708 | |
9fbc0116 RS |
2709 | /* Add leim under the installation dir, if it exists. */ |
2710 | tem = Fexpand_file_name (build_string ("leim"), | |
2711 | Vinstallation_directory); | |
2712 | tem1 = Ffile_exists_p (tem); | |
2713 | if (!NILP (tem1)) | |
2714 | { | |
2715 | if (NILP (Fmember (tem, Vload_path))) | |
2716 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2717 | } | |
2718 | ||
c478f98c RS |
2719 | /* Add site-list under the installation dir, if it exists. */ |
2720 | tem = Fexpand_file_name (build_string ("site-lisp"), | |
2721 | Vinstallation_directory); | |
2722 | tem1 = Ffile_exists_p (tem); | |
2723 | if (!NILP (tem1)) | |
2724 | { | |
2725 | if (NILP (Fmember (tem, Vload_path))) | |
2726 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2727 | } | |
0f337465 RS |
2728 | |
2729 | /* If Emacs was not built in the source directory, | |
9fbc0116 RS |
2730 | and it is run from where it was built, add to load-path |
2731 | the lisp, leim and site-lisp dirs under that directory. */ | |
0f337465 RS |
2732 | |
2733 | if (NILP (Fequal (Vinstallation_directory, Vsource_directory))) | |
2734 | { | |
33046fc9 RS |
2735 | Lisp_Object tem2; |
2736 | ||
0f337465 RS |
2737 | tem = Fexpand_file_name (build_string ("src/Makefile"), |
2738 | Vinstallation_directory); | |
2739 | tem1 = Ffile_exists_p (tem); | |
33046fc9 RS |
2740 | |
2741 | /* Don't be fooled if they moved the entire source tree | |
2742 | AFTER dumping Emacs. If the build directory is indeed | |
2743 | different from the source dir, src/Makefile.in and | |
2744 | src/Makefile will not be found together. */ | |
2745 | tem = Fexpand_file_name (build_string ("src/Makefile.in"), | |
2746 | Vinstallation_directory); | |
2747 | tem2 = Ffile_exists_p (tem); | |
2748 | if (!NILP (tem1) && NILP (tem2)) | |
0f337465 RS |
2749 | { |
2750 | tem = Fexpand_file_name (build_string ("lisp"), | |
2751 | Vsource_directory); | |
2752 | ||
2753 | if (NILP (Fmember (tem, Vload_path))) | |
2754 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2755 | ||
9fbc0116 RS |
2756 | tem = Fexpand_file_name (build_string ("leim"), |
2757 | Vsource_directory); | |
2758 | ||
2759 | if (NILP (Fmember (tem, Vload_path))) | |
2760 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2761 | ||
0f337465 RS |
2762 | tem = Fexpand_file_name (build_string ("site-lisp"), |
2763 | Vsource_directory); | |
2764 | ||
2765 | if (NILP (Fmember (tem, Vload_path))) | |
2766 | Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil)); | |
2767 | } | |
2768 | } | |
80667d53 RS |
2769 | } |
2770 | } | |
4746118a JB |
2771 | } |
2772 | else | |
11938f10 | 2773 | { |
7b396c6c RS |
2774 | /* NORMAL refers to the lisp dir in the source directory. */ |
2775 | /* We used to add ../lisp at the front here, but | |
2776 | that caused trouble because it was copied from dump_path | |
2777 | into Vload_path, aboe, when Vinstallation_directory was non-nil. | |
2778 | It should be unnecessary. */ | |
2779 | Vload_path = decode_env_path (0, normal); | |
11938f10 KH |
2780 | dump_path = Vload_path; |
2781 | } | |
46947372 | 2782 | #endif |
279499f0 | 2783 | |
317073d5 RS |
2784 | #ifndef WINDOWSNT |
2785 | /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is | |
2786 | almost never correct, thereby causing a warning to be printed out that | |
8e6208c5 | 2787 | confuses users. Since PATH_LOADSEARCH is always overridden by the |
317073d5 RS |
2788 | EMACSLOADPATH environment variable below, disable the warning on NT. */ |
2789 | ||
078e7b4a | 2790 | /* Warn if dirs in the *standard* path don't exist. */ |
e73997a1 RS |
2791 | if (!turn_off_warning) |
2792 | { | |
2793 | Lisp_Object path_tail; | |
078e7b4a | 2794 | |
e73997a1 RS |
2795 | for (path_tail = Vload_path; |
2796 | !NILP (path_tail); | |
2797 | path_tail = XCONS (path_tail)->cdr) | |
2798 | { | |
2799 | Lisp_Object dirfile; | |
2800 | dirfile = Fcar (path_tail); | |
2801 | if (STRINGP (dirfile)) | |
2802 | { | |
2803 | dirfile = Fdirectory_file_name (dirfile); | |
2804 | if (access (XSTRING (dirfile)->data, 0) < 0) | |
85496b8c RS |
2805 | dir_warning ("Warning: Lisp directory `%s' does not exist.\n", |
2806 | XCONS (path_tail)->car); | |
e73997a1 RS |
2807 | } |
2808 | } | |
2809 | } | |
317073d5 | 2810 | #endif /* WINDOWSNT */ |
46947372 JB |
2811 | |
2812 | /* If the EMACSLOADPATH environment variable is set, use its value. | |
2813 | This doesn't apply if we're dumping. */ | |
ffd9c2a1 | 2814 | #ifndef CANNOT_DUMP |
46947372 JB |
2815 | if (NILP (Vpurify_flag) |
2816 | && egetenv ("EMACSLOADPATH")) | |
ffd9c2a1 | 2817 | #endif |
279499f0 | 2818 | Vload_path = decode_env_path ("EMACSLOADPATH", normal); |
279499f0 JB |
2819 | |
2820 | Vvalues = Qnil; | |
2821 | ||
078e7b4a | 2822 | load_in_progress = 0; |
4e53f562 | 2823 | Vload_file_name = Qnil; |
d2c6be7f RS |
2824 | |
2825 | load_descriptor_list = Qnil; | |
8f6b0411 RS |
2826 | |
2827 | Vstandard_input = Qt; | |
078e7b4a JB |
2828 | } |
2829 | ||
85496b8c RS |
2830 | /* Print a warning, using format string FORMAT, that directory DIRNAME |
2831 | does not exist. Print it on stderr and put it in *Message*. */ | |
2832 | ||
d5b28a9d | 2833 | void |
85496b8c RS |
2834 | dir_warning (format, dirname) |
2835 | char *format; | |
2836 | Lisp_Object dirname; | |
2837 | { | |
2838 | char *buffer | |
2839 | = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5); | |
2840 | ||
2841 | fprintf (stderr, format, XSTRING (dirname)->data); | |
2842 | sprintf (buffer, format, XSTRING (dirname)->data); | |
e28552a4 | 2843 | message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname)); |
85496b8c RS |
2844 | } |
2845 | ||
078e7b4a | 2846 | void |
279499f0 | 2847 | syms_of_lread () |
078e7b4a JB |
2848 | { |
2849 | defsubr (&Sread); | |
2850 | defsubr (&Sread_from_string); | |
2851 | defsubr (&Sintern); | |
2852 | defsubr (&Sintern_soft); | |
d007f5c8 | 2853 | defsubr (&Sunintern); |
078e7b4a | 2854 | defsubr (&Sload); |
228d4b1c | 2855 | defsubr (&Seval_buffer); |
078e7b4a JB |
2856 | defsubr (&Seval_region); |
2857 | defsubr (&Sread_char); | |
2858 | defsubr (&Sread_char_exclusive); | |
078e7b4a | 2859 | defsubr (&Sread_event); |
078e7b4a JB |
2860 | defsubr (&Sget_file_char); |
2861 | defsubr (&Smapatoms); | |
2862 | ||
2863 | DEFVAR_LISP ("obarray", &Vobarray, | |
2864 | "Symbol table for use by `intern' and `read'.\n\ | |
2865 | It is a vector whose length ought to be prime for best results.\n\ | |
2866 | The vector's contents don't make sense if examined from Lisp programs;\n\ | |
2867 | to find all the symbols in an obarray, use `mapatoms'."); | |
2868 | ||
2869 | DEFVAR_LISP ("values", &Vvalues, | |
2870 | "List of values of all expressions which were read, evaluated and printed.\n\ | |
2871 | Order is reverse chronological."); | |
2872 | ||
2873 | DEFVAR_LISP ("standard-input", &Vstandard_input, | |
2874 | "Stream for read to get input from.\n\ | |
2875 | See documentation of `read' for possible values."); | |
2876 | Vstandard_input = Qt; | |
2877 | ||
2878 | DEFVAR_LISP ("load-path", &Vload_path, | |
2879 | "*List of directories to search for files to load.\n\ | |
2880 | Each element is a string (directory name) or nil (try default directory).\n\ | |
2881 | Initialized based on EMACSLOADPATH environment variable, if any,\n\ | |
692f86ad | 2882 | otherwise to default specified by file `paths.h' when Emacs was built."); |
078e7b4a JB |
2883 | |
2884 | DEFVAR_BOOL ("load-in-progress", &load_in_progress, | |
2885 | "Non-nil iff inside of `load'."); | |
2886 | ||
2887 | DEFVAR_LISP ("after-load-alist", &Vafter_load_alist, | |
2888 | "An alist of expressions to be evalled when particular files are loaded.\n\ | |
2889 | Each element looks like (FILENAME FORMS...).\n\ | |
2890 | When `load' is run and the file-name argument is FILENAME,\n\ | |
2891 | the FORMS in the corresponding element are executed at the end of loading.\n\n\ | |
2892 | FILENAME must match exactly! Normally FILENAME is the name of a library,\n\ | |
2893 | with no directory specified, since that is how `load' is normally called.\n\ | |
2894 | An error in FORMS does not undo the load,\n\ | |
2895 | but does prevent execution of the rest of the FORMS."); | |
2896 | Vafter_load_alist = Qnil; | |
2897 | ||
ae321d28 RS |
2898 | DEFVAR_LISP ("load-history", &Vload_history, |
2899 | "Alist mapping source file names to symbols and features.\n\ | |
2900 | Each alist element is a list that starts with a file name,\n\ | |
2901 | except for one element (optional) that starts with nil and describes\n\ | |
2902 | definitions evaluated from buffers not visiting files.\n\ | |
2903 | The remaining elements of each list are symbols defined as functions\n\ | |
2904 | or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'."); | |
2905 | Vload_history = Qnil; | |
2906 | ||
20ea2964 RS |
2907 | DEFVAR_LISP ("load-file-name", &Vload_file_name, |
2908 | "Full name of file being loaded by `load'."); | |
2909 | Vload_file_name = Qnil; | |
2910 | ||
8a1f1537 RS |
2911 | DEFVAR_LISP ("current-load-list", &Vcurrent_load_list, |
2912 | "Used for internal purposes by `load'."); | |
ae321d28 RS |
2913 | Vcurrent_load_list = Qnil; |
2914 | ||
84a15045 RS |
2915 | DEFVAR_LISP ("load-read-function", &Vload_read_function, |
2916 | "Function used by `load' and `eval-region' for reading expressions.\n\ | |
2917 | The default is nil, which means use the function `read'."); | |
2918 | Vload_read_function = Qnil; | |
2919 | ||
fe0e03f3 KH |
2920 | DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function, |
2921 | "Function called in `load' for loading an Emacs lisp source file.\n\ | |
2922 | This function is for doing code conversion before reading the source file.\n\ | |
2923 | If nil, loading is done without any code conversion.\n\ | |
2924 | Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\ | |
2925 | FULLNAME is the full name of FILE.\n\ | |
2926 | See `load' for the meaning of the remaining arguments."); | |
2927 | Vload_source_file_function = Qnil; | |
2928 | ||
b2a30870 RS |
2929 | DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings, |
2930 | "Non-nil means `load' should force-load all dynamic doc strings.\n\ | |
2931 | This is useful when the file being loaded is a temporary copy."); | |
2932 | load_force_doc_strings = 0; | |
2933 | ||
1521a8fa RS |
2934 | DEFVAR_LISP ("source-directory", &Vsource_directory, |
2935 | "Directory in which Emacs sources were found when Emacs was built.\n\ | |
2936 | You cannot count on them to still be there!"); | |
a90ba1e2 KH |
2937 | Vsource_directory |
2938 | = Fexpand_file_name (build_string ("../"), | |
2939 | Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH))); | |
2940 | ||
4b104c41 RS |
2941 | DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list, |
2942 | "List of files that were preloaded (when dumping Emacs)."); | |
2943 | Vpreloaded_file_list = Qnil; | |
2944 | ||
a90ba1e2 KH |
2945 | /* Vsource_directory was initialized in init_lread. */ |
2946 | ||
d2c6be7f RS |
2947 | load_descriptor_list = Qnil; |
2948 | staticpro (&load_descriptor_list); | |
2949 | ||
8a1f1537 RS |
2950 | Qcurrent_load_list = intern ("current-load-list"); |
2951 | staticpro (&Qcurrent_load_list); | |
2952 | ||
078e7b4a JB |
2953 | Qstandard_input = intern ("standard-input"); |
2954 | staticpro (&Qstandard_input); | |
2955 | ||
2956 | Qread_char = intern ("read-char"); | |
2957 | staticpro (&Qread_char); | |
2958 | ||
2959 | Qget_file_char = intern ("get-file-char"); | |
2960 | staticpro (&Qget_file_char); | |
7bd279cd | 2961 | |
17634846 RS |
2962 | Qbackquote = intern ("`"); |
2963 | staticpro (&Qbackquote); | |
2964 | Qcomma = intern (","); | |
2965 | staticpro (&Qcomma); | |
2966 | Qcomma_at = intern (",@"); | |
2967 | staticpro (&Qcomma_at); | |
2968 | Qcomma_dot = intern (",."); | |
2969 | staticpro (&Qcomma_dot); | |
2970 | ||
74549846 RS |
2971 | Qinhibit_file_name_operation = intern ("inhibit-file-name-operation"); |
2972 | staticpro (&Qinhibit_file_name_operation); | |
2973 | ||
7bd279cd RS |
2974 | Qascii_character = intern ("ascii-character"); |
2975 | staticpro (&Qascii_character); | |
c2225d00 | 2976 | |
2b6cae0c RS |
2977 | Qfunction = intern ("function"); |
2978 | staticpro (&Qfunction); | |
2979 | ||
c2225d00 RS |
2980 | Qload = intern ("load"); |
2981 | staticpro (&Qload); | |
20ea2964 RS |
2982 | |
2983 | Qload_file_name = intern ("load-file-name"); | |
2984 | staticpro (&Qload_file_name); | |
11938f10 KH |
2985 | |
2986 | staticpro (&dump_path); | |
4ad679f9 EN |
2987 | |
2988 | staticpro (&read_objects); | |
2989 | read_objects = Qnil; | |
078e7b4a | 2990 | } |