Keybindings for Tumme in Dired.
[bpt/emacs.git] / src / lread.c
CommitLineData
078e7b4a 1/* Lisp parsing and input streams.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
aaef169d 4 2005, 2006 Free Software Foundation, Inc.
078e7b4a
JB
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
46947372 10the Free Software Foundation; either version 2, or (at your option)
078e7b4a
JB
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
078e7b4a
JB
22
23
98280b76 24#include <config.h>
078e7b4a
JB
25#include <stdio.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/file.h>
2c1b5dbe 29#include <errno.h>
078e7b4a 30#include "lisp.h"
95384e1e 31#include "intervals.h"
078e7b4a 32#include "buffer.h"
fe0e03f3 33#include "charset.h"
57bda87a 34#include <epaths.h>
078e7b4a 35#include "commands.h"
e37c0805 36#include "keyboard.h"
7bd279cd 37#include "termhooks.h"
eb191db2 38#include "coding.h"
078e7b4a
JB
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
c1a2f60a
AS
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
54
078e7b4a
JB
55#ifndef X_OK
56#define X_OK 01
57#endif
58
078e7b4a 59#include <math.h>
078e7b4a 60
c011e9a5
RS
61#ifdef HAVE_SETLOCALE
62#include <locale.h>
63#endif /* HAVE_SETLOCALE */
64
bcdec28b
DL
65#ifdef HAVE_FCNTL_H
66#include <fcntl.h>
67#endif
f7d279f0
RS
68#ifndef O_RDONLY
69#define O_RDONLY 0
70#endif
71
79cf851c 72#ifdef HAVE_FSEEKO
68c45bf0
PE
73#define file_offset off_t
74#define file_tell ftello
75#else
76#define file_offset long
77#define file_tell ftell
78#endif
79
03695ace 80#ifndef USE_CRT_DLL
2c1b5dbe 81extern int errno;
03695ace 82#endif
2c1b5dbe 83
8a1f1537 84Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
078e7b4a 85Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
20ea2964 86Lisp_Object Qascii_character, Qload, Qload_file_name;
2b6cae0c 87Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
74549846 88Lisp_Object Qinhibit_file_name_operation;
3f39f996 89Lisp_Object Qeval_buffer_list, Veval_buffer_list;
7bd279cd
RS
90
91extern Lisp_Object Qevent_symbol_element_mask;
74549846 92extern Lisp_Object Qfile_exists_p;
078e7b4a 93
9942fa0c 94/* non-zero iff inside `load' */
078e7b4a
JB
95int load_in_progress;
96
1521a8fa
RS
97/* Directory in which the sources were found. */
98Lisp_Object Vsource_directory;
99
e61b9b87
SM
100/* Search path and suffixes for files to be loaded. */
101Lisp_Object Vload_path, Vload_suffixes, default_suffixes;
078e7b4a 102
4116ab9f
KH
103/* File name of user's init file. */
104Lisp_Object Vuser_init_file;
105
ae321d28
RS
106/* This is the user-visible association list that maps features to
107 lists of defs in their load files. */
108Lisp_Object Vload_history;
109
20ea2964 110/* This is used to build the load history. */
ae321d28
RS
111Lisp_Object Vcurrent_load_list;
112
4b104c41
RS
113/* List of files that were preloaded. */
114Lisp_Object Vpreloaded_file_list;
115
20ea2964
RS
116/* Name of file actually being read by `load'. */
117Lisp_Object Vload_file_name;
118
84a15045
RS
119/* Function to use for reading, in `load' and friends. */
120Lisp_Object Vload_read_function;
121
4ad679f9
EN
122/* The association list of objects read with the #n=object form.
123 Each member of the list has the form (n . object), and is used to
124 look up the object for the corresponding #n# construct.
125 It must be set to nil before all top-level calls to read0. */
126Lisp_Object read_objects;
127
b2a30870
RS
128/* Nonzero means load should forcibly load all dynamic doc strings. */
129static int load_force_doc_strings;
130
94e554db
RS
131/* Nonzero means read should convert strings to unibyte. */
132static int load_convert_to_unibyte;
133
fe0e03f3
KH
134/* Function to use for loading an Emacs lisp source file (not
135 compiled) instead of readevalloop. */
136Lisp_Object Vload_source_file_function;
137
1ffcc3b1
DL
138/* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
139Lisp_Object Vbyte_boolean_vars;
140
abb13b09
CW
141/* Whether or not to add a `read-positions' property to symbols
142 read. */
143Lisp_Object Vread_with_symbol_positions;
144
145/* List of (SYMBOL . POSITION) accumulated so far. */
146Lisp_Object Vread_symbol_positions_list;
147
d2c6be7f
RS
148/* List of descriptors now open for Fload. */
149static Lisp_Object load_descriptor_list;
150
b2a30870 151/* File for get_file_char to read from. Use by load. */
078e7b4a
JB
152static FILE *instream;
153
154/* When nonzero, read conses in pure space */
155static int read_pure;
156
b2a30870 157/* For use within read-from-string (this reader is non-reentrant!!) */
078e7b4a 158static int read_from_string_index;
bed23cb2 159static int read_from_string_index_byte;
078e7b4a 160static int read_from_string_limit;
17634846 161
6f7f43d5
RS
162/* Number of bytes left to read in the buffer character
163 that `readchar' has already advanced over. */
164static int readchar_backlog;
abb13b09
CW
165/* Number of characters read in the current call to Fread or
166 Fread_from_string. */
167static int readchar_count;
6f7f43d5 168
c15cfd1f 169/* This contains the last string skipped with #@. */
b2a30870
RS
170static char *saved_doc_string;
171/* Length of buffer allocated in saved_doc_string. */
172static int saved_doc_string_size;
173/* Length of actual data in saved_doc_string. */
174static int saved_doc_string_length;
175/* This is the file position that string came from. */
68c45bf0 176static file_offset saved_doc_string_position;
b2a30870 177
c15cfd1f
RS
178/* This contains the previous string skipped with #@.
179 We copy it from saved_doc_string when a new string
180 is put in saved_doc_string. */
181static char *prev_saved_doc_string;
182/* Length of buffer allocated in prev_saved_doc_string. */
183static int prev_saved_doc_string_size;
184/* Length of actual data in prev_saved_doc_string. */
185static int prev_saved_doc_string_length;
186/* This is the file position that string came from. */
68c45bf0 187static file_offset prev_saved_doc_string_position;
c15cfd1f 188
17634846
RS
189/* Nonzero means inside a new-style backquote
190 with no surrounding parentheses.
191 Fread initializes this to zero, so we need not specbind it
192 or worry about what happens to it when there is an error. */
193static int new_backquote_flag;
7ee3bd7b
GM
194
195/* A list of file names for files being loaded in Fload. Used to
196 check for recursive loads. */
197
9942fa0c 198static Lisp_Object Vloads_in_progress;
7ee3bd7b 199
550a625e
GM
200/* Non-zero means load dangerous compiled Lisp files. */
201
202int load_dangerous_libraries;
203
204/* A regular expression used to detect files compiled with Emacs. */
205
206static Lisp_Object Vbytecomp_version_regexp;
207
a742d646 208static void to_multibyte P_ ((char **, char **, int *));
11d89e5d 209static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
550a625e 210 Lisp_Object (*) (), int,
4c03c46d 211 Lisp_Object, Lisp_Object,
550a625e
GM
212 Lisp_Object, Lisp_Object));
213static Lisp_Object load_unwind P_ ((Lisp_Object));
214static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
215
078e7b4a
JB
216\f
217/* Handle unreading and rereading of characters.
218 Write READCHAR to read a character,
fe0e03f3
KH
219 UNREAD(c) to unread c to be read again.
220
abb13b09
CW
221 The READCHAR and UNREAD macros are meant for reading/unreading a
222 byte code; they do not handle multibyte characters. The caller
223 should manage them if necessary.
177c0ea7 224
abb13b09
CW
225 [ Actually that seems to be a lie; READCHAR will definitely read
226 multibyte characters from buffer sources, at least. Is the
227 comment just out of date?
228 -- Colin Walters <walters@gnu.org>, 22 May 2002 16:36:50 -0400 ]
fe0e03f3 229 */
078e7b4a
JB
230
231#define READCHAR readchar (readcharfun)
232#define UNREAD(c) unreadchar (readcharfun, c)
233
234static int
235readchar (readcharfun)
236 Lisp_Object readcharfun;
237{
238 Lisp_Object tem;
95384e1e 239 register int c;
078e7b4a 240
abb13b09 241 readchar_count++;
177c0ea7 242
cfff016d 243 if (BUFFERP (readcharfun))
078e7b4a 244 {
bed23cb2 245 register struct buffer *inbuffer = XBUFFER (readcharfun);
078e7b4a 246
bed23cb2
RS
247 int pt_byte = BUF_PT_BYTE (inbuffer);
248 int orig_pt_byte = pt_byte;
6f7f43d5 249
00a9a935
RS
250 if (readchar_backlog > 0)
251 /* We get the address of the byte just passed,
252 which is the last byte of the character.
253 The other bytes in this character are consecutive with it,
254 because the gap can't be in the middle of a character. */
255 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
256 - --readchar_backlog);
257
bed23cb2
RS
258 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
259 return -1;
078e7b4a 260
00a9a935
RS
261 readchar_backlog = -1;
262
bed23cb2
RS
263 if (! NILP (inbuffer->enable_multibyte_characters))
264 {
00a9a935 265 /* Fetch the character code from the buffer. */
bed23cb2
RS
266 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
267 BUF_INC_POS (inbuffer, pt_byte);
268 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
269 }
270 else
271 {
272 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
273 pt_byte++;
6f7f43d5 274 }
bed23cb2 275 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
6f7f43d5 276
bed23cb2 277 return c;
078e7b4a 278 }
cfff016d 279 if (MARKERP (readcharfun))
078e7b4a 280 {
bed23cb2 281 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
078e7b4a 282
bed23cb2
RS
283 int bytepos = marker_byte_position (readcharfun);
284 int orig_bytepos = bytepos;
6f7f43d5 285
00a9a935
RS
286 if (readchar_backlog > 0)
287 /* We get the address of the byte just passed,
288 which is the last byte of the character.
289 The other bytes in this character are consecutive with it,
290 because the gap can't be in the middle of a character. */
291 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
292 - --readchar_backlog);
293
bed23cb2
RS
294 if (bytepos >= BUF_ZV_BYTE (inbuffer))
295 return -1;
6f7f43d5 296
00a9a935
RS
297 readchar_backlog = -1;
298
bed23cb2
RS
299 if (! NILP (inbuffer->enable_multibyte_characters))
300 {
00a9a935 301 /* Fetch the character code from the buffer. */
bed23cb2
RS
302 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
303 BUF_INC_POS (inbuffer, bytepos);
304 c = STRING_CHAR (p, bytepos - orig_bytepos);
305 }
306 else
307 {
308 c = BUF_FETCH_BYTE (inbuffer, bytepos);
309 bytepos++;
6f7f43d5
RS
310 }
311
bed23cb2
RS
312 XMARKER (readcharfun)->bytepos = bytepos;
313 XMARKER (readcharfun)->charpos++;
314
315 return c;
078e7b4a 316 }
ad0153e4
RS
317
318 if (EQ (readcharfun, Qlambda))
319 return read_bytecode_char (0);
320
078e7b4a 321 if (EQ (readcharfun, Qget_file_char))
2c1b5dbe
KH
322 {
323 c = getc (instream);
324#ifdef EINTR
325 /* Interrupted reads have been observed while reading over the network */
326 while (c == EOF && ferror (instream) && errno == EINTR)
327 {
495bf630 328 QUIT;
2c1b5dbe
KH
329 clearerr (instream);
330 c = getc (instream);
331 }
332#endif
333 return c;
334 }
078e7b4a 335
cfff016d 336 if (STRINGP (readcharfun))
078e7b4a 337 {
bed23cb2 338 if (read_from_string_index >= read_from_string_limit)
078e7b4a 339 c = -1;
f0354c03 340 else
bed23cb2
RS
341 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
342 read_from_string_index,
343 read_from_string_index_byte);
6f7f43d5 344
078e7b4a
JB
345 return c;
346 }
347
348 tem = call0 (readcharfun);
349
265a9e55 350 if (NILP (tem))
078e7b4a
JB
351 return -1;
352 return XINT (tem);
353}
354
355/* Unread the character C in the way appropriate for the stream READCHARFUN.
356 If the stream is a user function, call it with the char as argument. */
357
358static void
359unreadchar (readcharfun, c)
360 Lisp_Object readcharfun;
361 int c;
362{
abb13b09 363 readchar_count--;
92fddec9
KH
364 if (c == -1)
365 /* Don't back up the pointer if we're unreading the end-of-input mark,
366 since readchar didn't advance it when we read it. */
367 ;
cfff016d 368 else if (BUFFERP (readcharfun))
d7760ca9 369 {
bed23cb2
RS
370 struct buffer *b = XBUFFER (readcharfun);
371 int bytepos = BUF_PT_BYTE (b);
d7760ca9 372
00a9a935
RS
373 if (readchar_backlog >= 0)
374 readchar_backlog++;
bed23cb2 375 else
00a9a935
RS
376 {
377 BUF_PT (b)--;
378 if (! NILP (b->enable_multibyte_characters))
379 BUF_DEC_POS (b, bytepos);
380 else
381 bytepos--;
d7760ca9 382
00a9a935
RS
383 BUF_PT_BYTE (b) = bytepos;
384 }
d7760ca9 385 }
cfff016d 386 else if (MARKERP (readcharfun))
d7760ca9 387 {
bed23cb2
RS
388 struct buffer *b = XMARKER (readcharfun)->buffer;
389 int bytepos = XMARKER (readcharfun)->bytepos;
d7760ca9 390
00a9a935
RS
391 if (readchar_backlog >= 0)
392 readchar_backlog++;
bed23cb2 393 else
00a9a935
RS
394 {
395 XMARKER (readcharfun)->charpos--;
396 if (! NILP (b->enable_multibyte_characters))
397 BUF_DEC_POS (b, bytepos);
398 else
399 bytepos--;
d7760ca9 400
00a9a935
RS
401 XMARKER (readcharfun)->bytepos = bytepos;
402 }
d7760ca9 403 }
cfff016d 404 else if (STRINGP (readcharfun))
bed23cb2
RS
405 {
406 read_from_string_index--;
407 read_from_string_index_byte
408 = string_char_to_byte (readcharfun, read_from_string_index);
409 }
ad0153e4
RS
410 else if (EQ (readcharfun, Qlambda))
411 read_bytecode_char (1);
078e7b4a
JB
412 else if (EQ (readcharfun, Qget_file_char))
413 ungetc (c, instream);
414 else
415 call1 (readcharfun, make_number (c));
416}
417
abb13b09
CW
418static Lisp_Object read_internal_start P_ ((Lisp_Object, Lisp_Object,
419 Lisp_Object));
420static Lisp_Object read0 P_ ((Lisp_Object));
177c0ea7 421static Lisp_Object read1 P_ ((Lisp_Object, int *, int));
abb13b09
CW
422
423static Lisp_Object read_list P_ ((int, Lisp_Object));
424static Lisp_Object read_vector P_ ((Lisp_Object, int));
425static int read_multibyte P_ ((int, Lisp_Object));
426
427static Lisp_Object substitute_object_recurse P_ ((Lisp_Object, Lisp_Object,
428 Lisp_Object));
429static void substitute_object_in_subtree P_ ((Lisp_Object,
430 Lisp_Object));
431static void substitute_in_interval P_ ((INTERVAL, Lisp_Object));
9e062b6c 432
078e7b4a 433\f
bed23cb2 434/* Get a character from the tty. */
078e7b4a 435
3d9b22be
JB
436extern Lisp_Object read_char ();
437
f42be754
JB
438/* Read input events until we get one that's acceptable for our purposes.
439
440 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
441 until we get a character we like, and then stuffed into
442 unread_switch_frame.
443
444 If ASCII_REQUIRED is non-zero, we check function key events to see
445 if the unmodified version of the symbol has a Qascii_character
446 property, and use that character, if present.
447
448 If ERROR_NONASCII is non-zero, we signal an error if the input we
449 get isn't an ASCII character with modifiers. If it's zero but
450 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
bb49a192
RS
451 character.
452
453 If INPUT_METHOD is nonzero, we invoke the current input method
454 if the character warrants that. */
cc39bc38 455
f42be754 456Lisp_Object
bb49a192
RS
457read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
458 input_method)
459 int no_switch_frame, ascii_required, error_nonascii, input_method;
f42be754 460{
153a17b7
KH
461 register Lisp_Object val, delayed_switch_frame;
462
5dbf89bc 463#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
464 if (display_hourglass_p)
465 cancel_hourglass ();
5dbf89bc 466#endif
177c0ea7 467
153a17b7 468 delayed_switch_frame = Qnil;
f42be754
JB
469
470 /* Read until we get an acceptable event. */
471 retry:
bb49a192
RS
472 val = read_char (0, 0, 0,
473 (input_method ? Qnil : Qt),
474 0);
f42be754 475
cfff016d 476 if (BUFFERP (val))
6c82d689
KH
477 goto retry;
478
f42be754 479 /* switch-frame events are put off until after the next ASCII
8e6208c5 480 character. This is better than signaling an error just because
f42be754
JB
481 the last characters were typed to a separate minibuffer frame,
482 for example. Eventually, some code which can deal with
483 switch-frame events will read it and process it. */
484 if (no_switch_frame
485 && EVENT_HAS_PARAMETERS (val)
486 && EQ (EVENT_HEAD (val), Qswitch_frame))
487 {
488 delayed_switch_frame = val;
489 goto retry;
490 }
491
492 if (ascii_required)
493 {
494 /* Convert certain symbols to their ASCII equivalents. */
cfff016d 495 if (SYMBOLP (val))
f42be754 496 {
95384e1e 497 Lisp_Object tem, tem1;
f42be754
JB
498 tem = Fget (val, Qevent_symbol_element_mask);
499 if (!NILP (tem))
500 {
501 tem1 = Fget (Fcar (tem), Qascii_character);
502 /* Merge this symbol's modifier bits
503 with the ASCII equivalent of its basic code. */
504 if (!NILP (tem1))
baf69866 505 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
f42be754
JB
506 }
507 }
177c0ea7 508
f42be754 509 /* If we don't have a character now, deal with it appropriately. */
cfff016d 510 if (!INTEGERP (val))
f42be754
JB
511 {
512 if (error_nonascii)
513 {
1ec84625 514 Vunread_command_events = Fcons (val, Qnil);
f42be754
JB
515 error ("Non-character input-event");
516 }
517 else
518 goto retry;
519 }
520 }
521
522 if (! NILP (delayed_switch_frame))
523 unread_switch_frame = delayed_switch_frame;
524
67868d27
PJ
525#if 0
526
5dbf89bc 527#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
528 if (display_hourglass_p)
529 start_hourglass ();
5dbf89bc 530#endif
67868d27
PJ
531
532#endif
533
f42be754 534 return val;
f42be754
JB
535}
536
bb49a192 537DEFUN ("read-char", Fread_char, Sread_char, 0, 2, 0,
5de38842
PJ
538 doc: /* Read a character from the command input (keyboard or macro).
539It is returned as a number.
540If the user generates an event which is not a character (i.e. a mouse
541click or function key event), `read-char' signals an error. As an
542exception, switch-frame events are put off until non-ASCII events can
543be read.
544If you want to read non-character events, or ignore them, call
545`read-event' or `read-char-exclusive' instead.
546
547If the optional argument PROMPT is non-nil, display that as a prompt.
548If the optional argument INHERIT-INPUT-METHOD is non-nil and some
549input method is turned on in the current buffer, that input method
550is used for reading a character. */)
551 (prompt, inherit_input_method)
fc351d2f 552 Lisp_Object prompt, inherit_input_method;
078e7b4a 553{
bb49a192
RS
554 if (! NILP (prompt))
555 message_with_string ("%s", prompt, 0);
fc351d2f 556 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method));
078e7b4a
JB
557}
558
bb49a192 559DEFUN ("read-event", Fread_event, Sread_event, 0, 2, 0,
5de38842
PJ
560 doc: /* Read an event object from the input stream.
561If the optional argument PROMPT is non-nil, display that as a prompt.
562If the optional argument INHERIT-INPUT-METHOD is non-nil and some
563input method is turned on in the current buffer, that input method
564is used for reading a character. */)
565 (prompt, inherit_input_method)
fc351d2f 566 Lisp_Object prompt, inherit_input_method;
078e7b4a 567{
bb49a192
RS
568 if (! NILP (prompt))
569 message_with_string ("%s", prompt, 0);
fc351d2f 570 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method));
078e7b4a
JB
571}
572
bb49a192 573DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 2, 0,
5de38842
PJ
574 doc: /* Read a character from the command input (keyboard or macro).
575It is returned as a number. Non-character events are ignored.
576
577If the optional argument PROMPT is non-nil, display that as a prompt.
578If the optional argument INHERIT-INPUT-METHOD is non-nil and some
579input method is turned on in the current buffer, that input method
580is used for reading a character. */)
581 (prompt, inherit_input_method)
fc351d2f 582 Lisp_Object prompt, inherit_input_method;
078e7b4a 583{
bb49a192
RS
584 if (! NILP (prompt))
585 message_with_string ("%s", prompt, 0);
fc351d2f 586 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method));
078e7b4a 587}
078e7b4a
JB
588
589DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
5de38842
PJ
590 doc: /* Don't use this yourself. */)
591 ()
078e7b4a
JB
592{
593 register Lisp_Object val;
1805de4f 594 XSETINT (val, getc (instream));
078e7b4a
JB
595 return val;
596}
da84f340
GM
597
598
550a625e 599\f
da84f340
GM
600/* Value is non-zero if the file asswociated with file descriptor FD
601 is a compiled Lisp file that's safe to load. Only files compiled
602 with Emacs are safe to load. Files compiled with XEmacs can lead
603 to a crash in Fbyte_code because of an incompatible change in the
604 byte compiler. */
605
606static int
607safe_to_load_p (fd)
608 int fd;
609{
610 char buf[512];
611 int nbytes, i;
612 int safe_p = 1;
613
614 /* Read the first few bytes from the file, and look for a line
615 specifying the byte compiler version used. */
616 nbytes = emacs_read (fd, buf, sizeof buf - 1);
617 if (nbytes > 0)
618 {
619 buf[nbytes] = '\0';
620
621 /* Skip to the next newline, skipping over the initial `ELC'
622 with NUL bytes following it. */
623 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
624 ;
625
626 if (i < nbytes
627 && fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
628 buf + i) < 0)
629 safe_p = 0;
630 }
631
632 lseek (fd, 0, SEEK_SET);
633 return safe_p;
634}
635
636
7ee3bd7b
GM
637/* Callback for record_unwind_protect. Restore the old load list OLD,
638 after loading a file successfully. */
639
640static Lisp_Object
641record_load_unwind (old)
642 Lisp_Object old;
643{
644 return Vloads_in_progress = old;
645}
646
164c8bfe
RS
647/* This handler function is used via internal_condition_case_1. */
648
649static Lisp_Object
650load_error_handler (data)
651 Lisp_Object data;
652{
653 return Qnil;
654}
7ee3bd7b 655
f0a50954 656DEFUN ("load", Fload, Sload, 1, 5, 0,
5de38842
PJ
657 doc: /* Execute a file of Lisp code named FILE.
658First try FILE with `.elc' appended, then try with `.el',
d77b70e5
MR
659 then try FILE unmodified (the exact suffixes are determined by
660`load-suffixes'). Environment variable references in FILE
5de38842
PJ
661 are replaced with their values by calling `substitute-in-file-name'.
662This function searches the directories in `load-path'.
663If optional second arg NOERROR is non-nil,
664 report no error if FILE doesn't exist.
665Print messages at start and end of loading unless
666 optional third arg NOMESSAGE is non-nil.
667If optional fourth arg NOSUFFIX is non-nil, don't try adding
668 suffixes `.elc' or `.el' to the specified name FILE.
669If optional fifth arg MUST-SUFFIX is non-nil, insist on
670 the suffix `.elc' or `.el'; don't accept just FILE unless
671 it ends in one of those suffixes or includes a directory name.
0fc213e9
RS
672
673Loading a file records its definitions, and its `provide' and
674`require' calls, in an element of `load-history' whose
675car is the file name loaded. See `load-history'.
676
5de38842
PJ
677Return t if file exists. */)
678 (file, noerror, nomessage, nosuffix, must_suffix)
f0a50954 679 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
078e7b4a
JB
680{
681 register FILE *stream;
682 register int fd = -1;
aed13378 683 int count = SPECPDL_INDEX ();
078e7b4a 684 Lisp_Object temp;
0fc213e9 685 struct gcpro gcpro1, gcpro2;
eb191db2 686 Lisp_Object found, efound;
04fc68e7
RS
687 /* 1 means we printed the ".el is newer" message. */
688 int newer = 0;
689 /* 1 means we are loading a compiled file. */
690 int compiled = 0;
c2225d00 691 Lisp_Object handler;
456c67a4 692 int safe_p = 1;
229ba775 693 char *fmode = "r";
317073d5 694#ifdef DOS_NT
229ba775 695 fmode = "rt";
317073d5 696#endif /* DOS_NT */
078e7b4a 697
b7826503 698 CHECK_STRING (file);
078e7b4a 699
c2225d00 700 /* If file name is magic, call the handler. */
e61b9b87
SM
701 /* This shouldn't be necessary any more now that `openp' handles it right.
702 handler = Ffind_file_name_handler (file, Qload);
703 if (!NILP (handler))
704 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
c2225d00 705
07a0bda3
RS
706 /* Do this after the handler to avoid
707 the need to gcpro noerror, nomessage and nosuffix.
e61b9b87
SM
708 (Below here, we care only whether they are nil or not.)
709 The presence of this call is the result of a historical accident:
710 it used to be in every file-operations and when it got removed
711 everywhere, it accidentally stayed here. Since then, enough people
712 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
713 that it seemed risky to remove. */
164c8bfe
RS
714 if (! NILP (noerror))
715 {
716 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
717 Qt, load_error_handler);
718 if (NILP (file))
719 return Qnil;
720 }
721 else
722 file = Fsubstitute_in_file_name (file);
177c0ea7 723
07a0bda3 724
078e7b4a
JB
725 /* Avoid weird lossage with null string as arg,
726 since it would try to load a directory as a Lisp file */
d5db4077 727 if (SCHARS (file) > 0)
078e7b4a 728 {
d5db4077 729 int size = SBYTES (file);
e61b9b87 730 Lisp_Object tmp[2];
211f7dcd 731
0fc213e9
RS
732 found = Qnil;
733 GCPRO2 (file, found);
211f7dcd
RS
734
735 if (! NILP (must_suffix))
736 {
737 /* Don't insist on adding a suffix if FILE already ends with one. */
738 if (size > 3
d5db4077 739 && !strcmp (SDATA (file) + size - 3, ".el"))
211f7dcd
RS
740 must_suffix = Qnil;
741 else if (size > 4
d5db4077 742 && !strcmp (SDATA (file) + size - 4, ".elc"))
211f7dcd
RS
743 must_suffix = Qnil;
744 /* Don't insist on adding a suffix
745 if the argument includes a directory name. */
746 else if (! NILP (Ffile_name_directory (file)))
747 must_suffix = Qnil;
748 }
749
f0a50954 750 fd = openp (Vload_path, file,
e61b9b87
SM
751 (!NILP (nosuffix) ? Qnil
752 : !NILP (must_suffix) ? Vload_suffixes
753 : Fappend (2, (tmp[0] = Vload_suffixes,
754 tmp[1] = default_suffixes,
755 tmp))),
86d00812 756 &found, Qnil);
5a6e5452 757 UNGCPRO;
078e7b4a
JB
758 }
759
96dc0f4e 760 if (fd == -1)
078e7b4a 761 {
265a9e55 762 if (NILP (noerror))
164c8bfe
RS
763 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
764 Fcons (file, Qnil)));
078e7b4a
JB
765 else
766 return Qnil;
767 }
768
ba0d1195 769 /* Tell startup.el whether or not we found the user's init file. */
4116ab9f
KH
770 if (EQ (Qt, Vuser_init_file))
771 Vuser_init_file = found;
772
96dc0f4e
MB
773 /* If FD is -2, that means openp found a magic file. */
774 if (fd == -2)
74549846 775 {
5e24a1f7
KH
776 if (NILP (Fequal (found, file)))
777 /* If FOUND is a different file name from FILE,
778 find its handler even if we have already inhibited
779 the `load' operation on FILE. */
780 handler = Ffind_file_name_handler (found, Qt);
781 else
782 handler = Ffind_file_name_handler (found, Qload);
783 if (! NILP (handler))
784 return call5 (handler, Qload, found, noerror, nomessage, Qt);
74549846
RS
785 }
786
550a625e
GM
787 /* Check if we're stuck in a recursive load cycle.
788
789 2000-09-21: It's not possible to just check for the file loaded
790 being a member of Vloads_in_progress. This fails because of the
791 way the byte compiler currently works; `provide's are not
792 evaluted, see font-lock.el/jit-lock.el as an example. This
793 leads to a certain amount of ``normal'' recursion.
794
795 Also, just loading a file recursively is not always an error in
796 the general case; the second load may do something different. */
3ba6fd9a
RS
797 {
798 int count = 0;
799 Lisp_Object tem;
800 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
801 if (!NILP (Fequal (found, XCAR (tem))))
802 count++;
803 if (count > 3)
3fdf12ca
EZ
804 {
805 if (fd >= 0)
806 emacs_close (fd);
807 Fsignal (Qerror, Fcons (build_string ("Recursive load"),
808 Fcons (found, Vloads_in_progress)));
809 }
3ba6fd9a
RS
810 record_unwind_protect (record_load_unwind, Vloads_in_progress);
811 Vloads_in_progress = Fcons (found, Vloads_in_progress);
812 }
7ee3bd7b 813
a1c89c0a 814 if (!bcmp (SDATA (found) + SBYTES (found) - 4,
96dc0f4e
MB
815 ".elc", 4))
816 /* Load .elc files directly, but not when they are
817 remote and have no handler! */
078e7b4a 818 {
96dc0f4e 819 if (fd != -2)
da84f340 820 {
96dc0f4e
MB
821 struct stat s1, s2;
822 int result;
da84f340 823
0fc213e9
RS
824 GCPRO2 (file, found);
825
96dc0f4e
MB
826 if (!safe_to_load_p (fd))
827 {
828 safe_p = 0;
829 if (!load_dangerous_libraries)
e63304b7
DL
830 {
831 if (fd >= 0)
832 emacs_close (fd);
833 error ("File `%s' was not compiled in Emacs",
834 SDATA (found));
835 }
96dc0f4e
MB
836 else if (!NILP (nomessage))
837 message_with_string ("File `%s' not compiled in Emacs", found, 1);
838 }
04fc68e7 839
96dc0f4e
MB
840 compiled = 1;
841
eb191db2
EZ
842 efound = ENCODE_FILE (found);
843
26fbf20b 844#ifdef DOS_NT
96dc0f4e 845 fmode = "rb";
26fbf20b 846#endif /* DOS_NT */
d5db4077 847 stat ((char *)SDATA (efound), &s1);
a1c89c0a 848 SSET (efound, SBYTES (efound) - 1, 0);
d5db4077 849 result = stat ((char *)SDATA (efound), &s2);
a1c89c0a 850 SSET (efound, SBYTES (efound) - 1, 'c');
eb191db2 851
96dc0f4e
MB
852 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
853 {
854 /* Make the progress messages mention that source is newer. */
855 newer = 1;
04fc68e7 856
96dc0f4e 857 /* If we won't print another message, mention this anyway. */
714d8c39
GM
858 if (!NILP (nomessage))
859 {
0fc213e9
RS
860 Lisp_Object msg_file;
861 msg_file = Fsubstring (found, make_number (0), make_number (-1));
714d8c39 862 message_with_string ("Source file `%s' newer than byte-compiled file",
0fc213e9 863 msg_file, 1);
714d8c39 864 }
96dc0f4e 865 }
0fc213e9 866 UNGCPRO;
51ac6f83 867 }
078e7b4a 868 }
fe0e03f3
KH
869 else
870 {
871 /* We are loading a source file (*.el). */
872 if (!NILP (Vload_source_file_function))
873 {
7ee3bd7b 874 Lisp_Object val;
96dc0f4e
MB
875
876 if (fd >= 0)
68c45bf0 877 emacs_close (fd);
7ee3bd7b
GM
878 val = call4 (Vload_source_file_function, found, file,
879 NILP (noerror) ? Qnil : Qt,
880 NILP (nomessage) ? Qnil : Qt);
881 return unbind_to (count, val);
fe0e03f3
KH
882 }
883 }
078e7b4a 884
0fc213e9
RS
885 GCPRO2 (file, found);
886
229ba775 887#ifdef WINDOWSNT
68c45bf0 888 emacs_close (fd);
eb191db2 889 efound = ENCODE_FILE (found);
d5db4077 890 stream = fopen ((char *) SDATA (efound), fmode);
229ba775
EZ
891#else /* not WINDOWSNT */
892 stream = fdopen (fd, fmode);
893#endif /* not WINDOWSNT */
078e7b4a
JB
894 if (stream == 0)
895 {
68c45bf0 896 emacs_close (fd);
d5db4077 897 error ("Failure to create stdio stream for %s", SDATA (file));
078e7b4a
JB
898 }
899
4b104c41
RS
900 if (! NILP (Vpurify_flag))
901 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
902
04fc68e7
RS
903 if (NILP (nomessage))
904 {
da84f340
GM
905 if (!safe_p)
906 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
907 file, 1);
908 else if (!compiled)
e28552a4 909 message_with_string ("Loading %s (source)...", file, 1);
db5cae4b 910 else if (newer)
e28552a4
RS
911 message_with_string ("Loading %s (compiled; note, source file is newer)...",
912 file, 1);
db5cae4b 913 else /* The typical case; compiled file newer than source file. */
e28552a4 914 message_with_string ("Loading %s...", file, 1);
04fc68e7 915 }
078e7b4a 916
c04d9e70 917 record_unwind_protect (load_unwind, make_save_value (stream, 0));
d2c6be7f 918 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
20ea2964 919 specbind (Qload_file_name, found);
74549846 920 specbind (Qinhibit_file_name_operation, Qnil);
d2c6be7f
RS
921 load_descriptor_list
922 = Fcons (make_number (fileno (stream)), load_descriptor_list);
078e7b4a 923 load_in_progress++;
0fc213e9
RS
924 readevalloop (Qget_file_char, stream, (! NILP (Vpurify_flag) ? file : found),
925 Feval, 0, Qnil, Qnil, Qnil, Qnil);
078e7b4a
JB
926 unbind_to (count, Qnil);
927
928 /* Run any load-hooks for this file. */
0745dce9 929 temp = Fassoc (file, Vafter_load_alist);
265a9e55 930 if (!NILP (temp))
078e7b4a
JB
931 Fprogn (Fcdr (temp));
932 UNGCPRO;
933
b2a30870
RS
934 if (saved_doc_string)
935 free (saved_doc_string);
936 saved_doc_string = 0;
937 saved_doc_string_size = 0;
938
c15cfd1f 939 if (prev_saved_doc_string)
3cb65b0e 940 xfree (prev_saved_doc_string);
c15cfd1f
RS
941 prev_saved_doc_string = 0;
942 prev_saved_doc_string_size = 0;
943
265a9e55 944 if (!noninteractive && NILP (nomessage))
04fc68e7 945 {
da84f340
GM
946 if (!safe_p)
947 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
948 file, 1);
949 else if (!compiled)
e28552a4 950 message_with_string ("Loading %s (source)...done", file, 1);
db5cae4b 951 else if (newer)
e28552a4
RS
952 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
953 file, 1);
db5cae4b 954 else /* The typical case; compiled file newer than source file. */
e28552a4 955 message_with_string ("Loading %s...done", file, 1);
04fc68e7 956 }
7ee3bd7b 957
11810d78
SM
958 if (!NILP (Fequal (build_string ("obsolete"),
959 Ffile_name_nondirectory
960 (Fdirectory_file_name (Ffile_name_directory (found))))))
961 message_with_string ("Package %s is obsolete", file, 1);
962
078e7b4a
JB
963 return Qt;
964}
965
966static Lisp_Object
ebfe97a2
KS
967load_unwind (arg) /* used as unwind-protect function in load */
968 Lisp_Object arg;
078e7b4a 969{
ebfe97a2
KS
970 FILE *stream = (FILE *) XSAVE_VALUE (arg)->pointer;
971 if (stream != NULL)
972 fclose (stream);
078e7b4a
JB
973 if (--load_in_progress < 0) load_in_progress = 0;
974 return Qnil;
975}
976
d2c6be7f
RS
977static Lisp_Object
978load_descriptor_unwind (oldlist)
979 Lisp_Object oldlist;
980{
981 load_descriptor_list = oldlist;
838abf56 982 return Qnil;
d2c6be7f
RS
983}
984
985/* Close all descriptors in use for Floads.
986 This is used when starting a subprocess. */
987
988void
989close_load_descs ()
990{
f3849f25 991#ifndef WINDOWSNT
d2c6be7f 992 Lisp_Object tail;
c1d497be 993 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
68c45bf0 994 emacs_close (XFASTINT (XCAR (tail)));
f3849f25 995#endif
d2c6be7f 996}
078e7b4a
JB
997\f
998static int
999complete_filename_p (pathname)
1000 Lisp_Object pathname;
1001{
4b2dd274 1002 register const unsigned char *s = SDATA (pathname);
317073d5 1003 return (IS_DIRECTORY_SEP (s[0])
d5db4077 1004 || (SCHARS (pathname) > 2
317073d5 1005 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
078e7b4a
JB
1006#ifdef ALTOS
1007 || *s == '@'
1008#endif
1009#ifdef VMS
1010 || index (s, ':')
1011#endif /* VMS */
1012 );
1013}
1014
86d00812
SM
1015DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1016 doc: /* Search for FILENAME through PATH.
57a131fb 1017Returns the file's name in absolute form, or nil if not found.
86d00812
SM
1018If SUFFIXES is non-nil, it should be a list of suffixes to append to
1019file name when searching.
1020If non-nil, PREDICATE is used instead of `file-readable-p'.
1021PREDICATE can also be an integer to pass to the access(2) function,
1022in which case file-name-handlers are ignored. */)
1023 (filename, path, suffixes, predicate)
1024 Lisp_Object filename, path, suffixes, predicate;
1025{
1026 Lisp_Object file;
1027 int fd = openp (path, filename, suffixes, &file, predicate);
1028 if (NILP (predicate) && fd > 0)
1029 close (fd);
1030 return file;
1031}
1032
1033
078e7b4a
JB
1034/* Search for a file whose name is STR, looking in directories
1035 in the Lisp list PATH, and trying suffixes from SUFFIX.
078e7b4a
JB
1036 On success, returns a file descriptor. On failure, returns -1.
1037
e61b9b87
SM
1038 SUFFIXES is a list of strings containing possible suffixes.
1039 The empty suffix is automatically added iff the list is empty.
1040
86d00812
SM
1041 PREDICATE non-nil means don't open the files,
1042 just look for one that satisfies the predicate. In this case,
1043 returns 1 on success. The predicate can be a lisp function or
1044 an integer to pass to `access' (in which case file-name-handlers
1045 are ignored).
078e7b4a
JB
1046
1047 If STOREPTR is nonzero, it points to a slot where the name of
1048 the file actually found should be stored as a Lisp string.
74549846
RS
1049 nil is stored there on failure.
1050
96dc0f4e 1051 If the file we find is remote, return -2
86d00812 1052 but store the found remote file name in *STOREPTR. */
078e7b4a
JB
1053
1054int
86d00812 1055openp (path, str, suffixes, storeptr, predicate)
078e7b4a 1056 Lisp_Object path, str;
e61b9b87 1057 Lisp_Object suffixes;
078e7b4a 1058 Lisp_Object *storeptr;
86d00812 1059 Lisp_Object predicate;
078e7b4a
JB
1060{
1061 register int fd;
1062 int fn_size = 100;
1063 char buf[100];
1064 register char *fn = buf;
1065 int absolute = 0;
1066 int want_size;
74549846 1067 Lisp_Object filename;
078e7b4a 1068 struct stat st;
eb191db2
EZ
1069 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
1070 Lisp_Object string, tail, encoded_fn;
e61b9b87
SM
1071 int max_suffix_len = 0;
1072
6f8eafd1
SM
1073 CHECK_STRING (str);
1074
e61b9b87
SM
1075 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1076 {
f5df591a 1077 CHECK_STRING_CAR (tail);
e61b9b87 1078 max_suffix_len = max (max_suffix_len,
d5db4077 1079 SBYTES (XCAR (tail)));
e61b9b87 1080 }
078e7b4a 1081
8b9d426a 1082 string = filename = encoded_fn = Qnil;
eb191db2
EZ
1083 GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
1084
078e7b4a
JB
1085 if (storeptr)
1086 *storeptr = Qnil;
1087
1088 if (complete_filename_p (str))
1089 absolute = 1;
1090
e61b9b87 1091 for (; CONSP (path); path = XCDR (path))
078e7b4a 1092 {
e61b9b87 1093 filename = Fexpand_file_name (str, XCAR (path));
078e7b4a
JB
1094 if (!complete_filename_p (filename))
1095 /* If there are non-absolute elts in PATH (eg ".") */
1096 /* Of course, this could conceivably lose if luser sets
1097 default-directory to be something non-absolute... */
1098 {
1099 filename = Fexpand_file_name (filename, current_buffer->directory);
1100 if (!complete_filename_p (filename))
1101 /* Give up on this path element! */
1102 continue;
1103 }
1104
1105 /* Calculate maximum size of any filename made from
1106 this path element/specified file name and any possible suffix. */
d5db4077 1107 want_size = max_suffix_len + SBYTES (filename) + 1;
078e7b4a
JB
1108 if (fn_size < want_size)
1109 fn = (char *) alloca (fn_size = 100 + want_size);
1110
078e7b4a 1111 /* Loop over suffixes. */
e61b9b87
SM
1112 for (tail = NILP (suffixes) ? default_suffixes : suffixes;
1113 CONSP (tail); tail = XCDR (tail))
078e7b4a 1114 {
d5db4077 1115 int lsuffix = SBYTES (XCAR (tail));
74549846 1116 Lisp_Object handler;
eb191db2 1117 int exists;
078e7b4a 1118
c49afcd7
RS
1119 /* Concatenate path element/specified name with the suffix.
1120 If the directory starts with /:, remove that. */
d5db4077
KR
1121 if (SCHARS (filename) > 2
1122 && SREF (filename, 0) == '/'
1123 && SREF (filename, 1) == ':')
c49afcd7 1124 {
d5db4077
KR
1125 strncpy (fn, SDATA (filename) + 2,
1126 SBYTES (filename) - 2);
1127 fn[SBYTES (filename) - 2] = 0;
c49afcd7
RS
1128 }
1129 else
1130 {
d5db4077
KR
1131 strncpy (fn, SDATA (filename),
1132 SBYTES (filename));
1133 fn[SBYTES (filename)] = 0;
c49afcd7
RS
1134 }
1135
078e7b4a 1136 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
d5db4077 1137 strncat (fn, SDATA (XCAR (tail)), lsuffix);
eb191db2 1138
74549846 1139 /* Check that the file exists and is not a directory. */
e61b9b87
SM
1140 /* We used to only check for handlers on non-absolute file names:
1141 if (absolute)
1142 handler = Qnil;
1143 else
1144 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1145 It's not clear why that was the case and it breaks things like
1146 (load "/bar.el") where the file is actually "/bar.el.gz". */
eb191db2 1147 string = build_string (fn);
4773b8ca 1148 handler = Ffind_file_name_handler (string, Qfile_exists_p);
86d00812
SM
1149 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1150 {
1151 if (NILP (predicate))
1152 exists = !NILP (Ffile_readable_p (string));
1153 else
1154 exists = !NILP (call1 (predicate, string));
eb191db2 1155 if (exists && !NILP (Ffile_directory_p (string)))
74549846
RS
1156 exists = 0;
1157
1158 if (exists)
078e7b4a
JB
1159 {
1160 /* We succeeded; return this descriptor and filename. */
1161 if (storeptr)
eb191db2 1162 *storeptr = string;
5ef2a3c0 1163 UNGCPRO;
96dc0f4e 1164 return -2;
74549846
RS
1165 }
1166 }
1167 else
1168 {
4b2dd274 1169 const char *pfn;
eb191db2
EZ
1170
1171 encoded_fn = ENCODE_FILE (string);
d5db4077 1172 pfn = SDATA (encoded_fn);
eb191db2
EZ
1173 exists = (stat (pfn, &st) >= 0
1174 && (st.st_mode & S_IFMT) != S_IFDIR);
74549846
RS
1175 if (exists)
1176 {
1177 /* Check that we can access or open it. */
86d00812
SM
1178 if (NATNUMP (predicate))
1179 fd = (access (pfn, XFASTINT (predicate)) == 0) ? 1 : -1;
74549846 1180 else
eb191db2 1181 fd = emacs_open (pfn, O_RDONLY, 0);
74549846
RS
1182
1183 if (fd >= 0)
1184 {
1185 /* We succeeded; return this descriptor and filename. */
1186 if (storeptr)
eb191db2 1187 *storeptr = string;
74549846
RS
1188 UNGCPRO;
1189 return fd;
1190 }
078e7b4a
JB
1191 }
1192 }
078e7b4a 1193 }
5a6e5452 1194 if (absolute)
5ef2a3c0 1195 break;
078e7b4a
JB
1196 }
1197
5ef2a3c0
KH
1198 UNGCPRO;
1199 return -1;
078e7b4a
JB
1200}
1201
1202\f
ae321d28
RS
1203/* Merge the list we've accumulated of globals from the current input source
1204 into the load_history variable. The details depend on whether
b502a9a1
RS
1205 the source has an associated file name or not.
1206
1207 FILENAME is the file name that we are loading from.
1208 ENTIRE is 1 if loading that entire file, 0 if evaluating part of it. */
ae321d28
RS
1209
1210static void
b502a9a1
RS
1211build_load_history (filename, entire)
1212 Lisp_Object filename;
1213 int entire;
ae321d28
RS
1214{
1215 register Lisp_Object tail, prev, newelt;
1216 register Lisp_Object tem, tem2;
b502a9a1 1217 register int foundit = 0;
ae321d28
RS
1218
1219 tail = Vload_history;
1220 prev = Qnil;
b502a9a1 1221
86d00812 1222 while (CONSP (tail))
ae321d28 1223 {
86d00812 1224 tem = XCAR (tail);
ae321d28
RS
1225
1226 /* Find the feature's previous assoc list... */
b502a9a1 1227 if (!NILP (Fequal (filename, Fcar (tem))))
ae321d28
RS
1228 {
1229 foundit = 1;
1230
b502a9a1
RS
1231 /* If we're loading the entire file, remove old data. */
1232 if (entire)
86d00812 1233 {
ae321d28 1234 if (NILP (prev))
86d00812 1235 Vload_history = XCDR (tail);
ae321d28 1236 else
86d00812 1237 Fsetcdr (prev, XCDR (tail));
ae321d28
RS
1238 }
1239
1240 /* Otherwise, cons on new symbols that are not already members. */
1241 else
1242 {
1243 tem2 = Vcurrent_load_list;
1244
1245 while (CONSP (tem2))
1246 {
86d00812 1247 newelt = XCAR (tem2);
ae321d28 1248
d642e4f9 1249 if (NILP (Fmember (newelt, tem)))
86d00812
SM
1250 Fsetcar (tail, Fcons (XCAR (tem),
1251 Fcons (newelt, XCDR (tem))));
ae321d28 1252
86d00812 1253 tem2 = XCDR (tem2);
ae321d28
RS
1254 QUIT;
1255 }
1256 }
1257 }
1258 else
1259 prev = tail;
86d00812 1260 tail = XCDR (tail);
ae321d28
RS
1261 QUIT;
1262 }
1263
b502a9a1
RS
1264 /* If we're loading an entire file, cons the new assoc onto the
1265 front of load-history, the most-recently-loaded position. Also
1266 do this if we didn't find an existing member for the file. */
1267 if (entire || !foundit)
8a1f1537
RS
1268 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1269 Vload_history);
ae321d28
RS
1270}
1271
078e7b4a 1272Lisp_Object
232ccf27
DL
1273unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1274 Lisp_Object junk;
078e7b4a
JB
1275{
1276 read_pure = 0;
1277 return Qnil;
1278}
1279
94e554db
RS
1280static Lisp_Object
1281readevalloop_1 (old)
1282 Lisp_Object old;
1283{
1284 load_convert_to_unibyte = ! NILP (old);
1285 return Qnil;
1286}
1287
9c97398c
GM
1288/* Signal an `end-of-file' error, if possible with file name
1289 information. */
1290
1291static void
1292end_of_file_error ()
1293{
1294 Lisp_Object data;
1295
1296 if (STRINGP (Vload_file_name))
1297 data = Fcons (Vload_file_name, Qnil);
1298 else
1299 data = Qnil;
1300
1301 Fsignal (Qend_of_file, data);
1302}
1303
94e554db 1304/* UNIBYTE specifies how to set load_convert_to_unibyte
976350af 1305 for this invocation.
4c03c46d
KS
1306 READFUN, if non-nil, is used instead of `read'.
1307 START, END is region in current buffer (from eval-region). */
94e554db 1308
078e7b4a 1309static void
4c03c46d
KS
1310readevalloop (readcharfun, stream, sourcename, evalfun,
1311 printflag, unibyte, readfun, start, end)
078e7b4a 1312 Lisp_Object readcharfun;
ae321d28
RS
1313 FILE *stream;
1314 Lisp_Object sourcename;
078e7b4a
JB
1315 Lisp_Object (*evalfun) ();
1316 int printflag;
976350af 1317 Lisp_Object unibyte, readfun;
4c03c46d 1318 Lisp_Object start, end;
078e7b4a
JB
1319{
1320 register int c;
1321 register Lisp_Object val;
aed13378 1322 int count = SPECPDL_INDEX ();
0a37f512 1323 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
49cf7ff4 1324 struct buffer *b = 0;
d11db2c8 1325 int bpos;
f38952fe 1326 int continue_reading_p;
d11db2c8
RS
1327 /* Nonzero if reading an entire buffer. */
1328 int whole_buffer = 0;
1329 /* 1 on the first time around. */
1330 int first_sexp = 1;
1331
1332 if (MARKERP (readcharfun))
1333 {
1334 if (NILP (start))
1335 start = readcharfun;
1336 }
49cf7ff4
RS
1337
1338 if (BUFFERP (readcharfun))
1339 b = XBUFFER (readcharfun);
1340 else if (MARKERP (readcharfun))
1341 b = XMARKER (readcharfun)->buffer;
078e7b4a 1342
0a37f512 1343 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
8a1f1537 1344 specbind (Qcurrent_load_list, Qnil);
94e554db
RS
1345 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1346 load_convert_to_unibyte = !NILP (unibyte);
078e7b4a 1347
00a9a935 1348 readchar_backlog = -1;
6f7f43d5 1349
0a37f512 1350 GCPRO4 (sourcename, readfun, start, end);
ae321d28 1351
ae321d28
RS
1352 LOADHIST_ATTACH (sourcename);
1353
f38952fe
GM
1354 continue_reading_p = 1;
1355 while (continue_reading_p)
078e7b4a 1356 {
4c03c46d
KS
1357 int count1 = SPECPDL_INDEX ();
1358
49cf7ff4
RS
1359 if (b != 0 && NILP (b->name))
1360 error ("Reading from killed buffer");
1361
4c03c46d
KS
1362 if (!NILP (start))
1363 {
721b7d9e
RS
1364 /* Switch to the buffer we are reading from. */
1365 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1366 set_buffer_internal (b);
1367
1368 /* Save point in it. */
4c03c46d 1369 record_unwind_protect (save_excursion_restore, save_excursion_save ());
721b7d9e 1370 /* Save ZV in it. */
4c03c46d 1371 record_unwind_protect (save_restriction_restore, save_restriction_save ());
721b7d9e
RS
1372 /* Those get unbound after we read one expression. */
1373
1374 /* Set point and ZV around stuff to be read. */
4c03c46d 1375 Fgoto_char (start);
d11db2c8
RS
1376 if (!NILP (end))
1377 Fnarrow_to_region (make_number (BEGV), end);
1378
1379 /* Just for cleanliness, convert END to a marker
1380 if it is an integer. */
1381 if (INTEGERP (end))
1382 end = Fpoint_max_marker ();
4c03c46d
KS
1383 }
1384
d11db2c8
RS
1385 /* On the first cycle, we can easily test here
1386 whether we are reading the whole buffer. */
1387 if (b && first_sexp)
1388 whole_buffer = (PT == BEG && ZV == Z);
1389
078e7b4a 1390 instream = stream;
4c03c46d 1391 read_next:
078e7b4a
JB
1392 c = READCHAR;
1393 if (c == ';')
1394 {
1395 while ((c = READCHAR) != '\n' && c != -1);
4c03c46d
KS
1396 goto read_next;
1397 }
1398 if (c < 0)
1399 {
1400 unbind_to (count1, Qnil);
1401 break;
078e7b4a 1402 }
6069d957
RS
1403
1404 /* Ignore whitespace here, so we can detect eof. */
1405 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
4c03c46d 1406 goto read_next;
078e7b4a 1407
265a9e55 1408 if (!NILP (Vpurify_flag) && c == '(')
078e7b4a
JB
1409 {
1410 record_unwind_protect (unreadpure, Qnil);
1411 val = read_list (-1, readcharfun);
078e7b4a
JB
1412 }
1413 else
1414 {
1415 UNREAD (c);
4ad679f9 1416 read_objects = Qnil;
f38952fe
GM
1417 if (!NILP (readfun))
1418 {
1419 val = call1 (readfun, readcharfun);
1420
1421 /* If READCHARFUN has set point to ZV, we should
1422 stop reading, even if the form read sets point
1423 to a different value when evaluated. */
1424 if (BUFFERP (readcharfun))
1425 {
1426 struct buffer *b = XBUFFER (readcharfun);
1427 if (BUF_PT (b) == BUF_ZV (b))
1428 continue_reading_p = 0;
1429 }
1430 }
976350af 1431 else if (! NILP (Vload_read_function))
84a15045 1432 val = call1 (Vload_read_function, readcharfun);
976350af 1433 else
abb13b09 1434 val = read_internal_start (readcharfun, Qnil, Qnil);
078e7b4a
JB
1435 }
1436
4c03c46d
KS
1437 if (!NILP (start) && continue_reading_p)
1438 start = Fpoint_marker ();
d11db2c8
RS
1439
1440 /* Restore saved point and BEGV. */
4c03c46d
KS
1441 unbind_to (count1, Qnil);
1442
d11db2c8 1443 /* Now eval what we just read. */
078e7b4a 1444 val = (*evalfun) (val);
f38952fe 1445
078e7b4a
JB
1446 if (printflag)
1447 {
1448 Vvalues = Fcons (val, Vvalues);
1449 if (EQ (Vstandard_output, Qt))
1450 Fprin1 (val, Qnil);
1451 else
1452 Fprint (val, Qnil);
1453 }
d11db2c8
RS
1454
1455 first_sexp = 0;
078e7b4a
JB
1456 }
1457
b502a9a1 1458 build_load_history (sourcename,
d11db2c8 1459 stream || whole_buffer);
b502a9a1 1460
ae321d28
RS
1461 UNGCPRO;
1462
078e7b4a
JB
1463 unbind_to (count, Qnil);
1464}
1465
1076254d 1466DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
5de38842
PJ
1467 doc: /* Execute the current buffer as Lisp code.
1468Programs can pass two arguments, BUFFER and PRINTFLAG.
1469BUFFER is the buffer to evaluate (nil means use current buffer).
1470PRINTFLAG controls printing of output:
1471nil means discard it; anything else is stream for print.
1472
1473If the optional third argument FILENAME is non-nil,
1474it specifies the file name to use for `load-history'.
1475The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'
1476for this invocation.
1477
3f9ab804 1478The optional fifth argument DO-ALLOW-PRINT, if non-nil, specifies that
5de38842
PJ
1479`print' and related functions should work normally even if PRINTFLAG is nil.
1480
1481This function preserves the position of point. */)
1482 (buffer, printflag, filename, unibyte, do_allow_print)
1076254d 1483 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
228d4b1c 1484{
aed13378 1485 int count = SPECPDL_INDEX ();
228d4b1c
JA
1486 Lisp_Object tem, buf;
1487
9391b698 1488 if (NILP (buffer))
228d4b1c
JA
1489 buf = Fcurrent_buffer ();
1490 else
9391b698 1491 buf = Fget_buffer (buffer);
dfdb645c 1492 if (NILP (buf))
13febd85 1493 error ("No such buffer");
228d4b1c 1494
1076254d 1495 if (NILP (printflag) && NILP (do_allow_print))
228d4b1c
JA
1496 tem = Qsymbolp;
1497 else
1498 tem = printflag;
13febd85
RS
1499
1500 if (NILP (filename))
1501 filename = XBUFFER (buf)->filename;
1502
3f39f996 1503 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
228d4b1c
JA
1504 specbind (Qstandard_output, tem);
1505 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1506 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
4c03c46d
KS
1507 readevalloop (buf, 0, filename, Feval,
1508 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
cb09ab7a 1509 unbind_to (count, Qnil);
228d4b1c
JA
1510
1511 return Qnil;
1512}
1513
976350af 1514DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
5de38842
PJ
1515 doc: /* Execute the region as Lisp code.
1516When called from programs, expects two arguments,
1517giving starting and ending indices in the current buffer
1518of the text to be executed.
1519Programs can pass third argument PRINTFLAG which controls output:
1520nil means discard it; anything else is stream for printing it.
1521Also the fourth argument READ-FUNCTION, if non-nil, is used
1522instead of `read' to read each expression. It gets one argument
1523which is the input stream for reading characters.
1524
1525This function does not move point. */)
1526 (start, end, printflag, read_function)
976350af 1527 Lisp_Object start, end, printflag, read_function;
078e7b4a 1528{
aed13378 1529 int count = SPECPDL_INDEX ();
ae321d28
RS
1530 Lisp_Object tem, cbuf;
1531
1532 cbuf = Fcurrent_buffer ();
078e7b4a 1533
265a9e55 1534 if (NILP (printflag))
078e7b4a
JB
1535 tem = Qsymbolp;
1536 else
1537 tem = printflag;
1538 specbind (Qstandard_output, tem);
3f39f996 1539 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
078e7b4a 1540
4c03c46d 1541 /* readevalloop calls functions which check the type of start and end. */
94e554db 1542 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
4c03c46d
KS
1543 !NILP (printflag), Qnil, read_function,
1544 start, end);
078e7b4a
JB
1545
1546 return unbind_to (count, Qnil);
1547}
1548
078e7b4a
JB
1549\f
1550DEFUN ("read", Fread, Sread, 0, 1, 0,
5de38842
PJ
1551 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
1552If STREAM is nil, use the value of `standard-input' (which see).
1553STREAM or the value of `standard-input' may be:
1554 a buffer (read from point and advance it)
1555 a marker (read from where it points and advance it)
1556 a function (call it with no arguments for each character,
1557 call it with a char as argument to push a char back)
1558 a string (takes text from string, starting at the beginning)
1559 t (read text line using minibuffer and use it, or read from
1560 standard input in batch mode). */)
1561 (stream)
5be02dff 1562 Lisp_Object stream;
078e7b4a 1563{
5be02dff
KH
1564 if (NILP (stream))
1565 stream = Vstandard_input;
1566 if (EQ (stream, Qt))
1567 stream = Qread_char;
5be02dff 1568 if (EQ (stream, Qread_char))
078e7b4a 1569 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
078e7b4a 1570
abb13b09 1571 return read_internal_start (stream, Qnil, Qnil);
078e7b4a
JB
1572}
1573
1574DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
5de38842
PJ
1575 doc: /* Read one Lisp expression which is represented as text by STRING.
1576Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
1577START and END optionally delimit a substring of STRING from which to read;
1578 they default to 0 and (length STRING) respectively. */)
1579 (string, start, end)
078e7b4a
JB
1580 Lisp_Object string, start, end;
1581{
2530ceaf 1582 Lisp_Object ret;
b7826503 1583 CHECK_STRING (string);
2530ceaf
CW
1584 /* read_internal_start sets read_from_string_index. */
1585 ret = read_internal_start (string, start, end);
5135ab39 1586 return Fcons (ret, make_number (read_from_string_index));
abb13b09 1587}
078e7b4a 1588
abb13b09
CW
1589/* Function to set up the global context we need in toplevel read
1590 calls. */
1591static Lisp_Object
1592read_internal_start (stream, start, end)
1593 Lisp_Object stream;
1594 Lisp_Object start; /* Only used when stream is a string. */
1595 Lisp_Object end; /* Only used when stream is a string. */
1596{
1597 Lisp_Object retval;
078e7b4a 1598
abb13b09
CW
1599 readchar_backlog = -1;
1600 readchar_count = 0;
17634846 1601 new_backquote_flag = 0;
4ad679f9 1602 read_objects = Qnil;
abb13b09
CW
1603 if (EQ (Vread_with_symbol_positions, Qt)
1604 || EQ (Vread_with_symbol_positions, stream))
1605 Vread_symbol_positions_list = Qnil;
1606
1607 if (STRINGP (stream))
1608 {
1609 int startval, endval;
1610 if (NILP (end))
d5db4077 1611 endval = SCHARS (stream);
abb13b09
CW
1612 else
1613 {
1614 CHECK_NUMBER (end);
1615 endval = XINT (end);
d5db4077 1616 if (endval < 0 || endval > SCHARS (stream))
abb13b09
CW
1617 args_out_of_range (stream, end);
1618 }
17634846 1619
abb13b09
CW
1620 if (NILP (start))
1621 startval = 0;
1622 else
1623 {
1624 CHECK_NUMBER (start);
1625 startval = XINT (start);
1626 if (startval < 0 || startval > endval)
1627 args_out_of_range (stream, start);
1628 }
1629 read_from_string_index = startval;
1630 read_from_string_index_byte = string_char_to_byte (stream, startval);
1631 read_from_string_limit = endval;
1632 }
177c0ea7 1633
abb13b09
CW
1634 retval = read0 (stream);
1635 if (EQ (Vread_with_symbol_positions, Qt)
1636 || EQ (Vread_with_symbol_positions, stream))
1637 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
1638 return retval;
078e7b4a
JB
1639}
1640\f
6428369f
KH
1641/* Use this for recursive reads, in contexts where internal tokens
1642 are not allowed. */
e28552a4 1643
078e7b4a
JB
1644static Lisp_Object
1645read0 (readcharfun)
1646 Lisp_Object readcharfun;
1647{
1648 register Lisp_Object val;
e28552a4 1649 int c;
078e7b4a 1650
17634846 1651 val = read1 (readcharfun, &c, 0);
6428369f 1652 if (c)
93d75e4c
RS
1653 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1654 make_number (c)),
e28552a4 1655 Qnil));
078e7b4a
JB
1656
1657 return val;
1658}
1659\f
1660static int read_buffer_size;
1661static char *read_buffer;
1662
fe0e03f3
KH
1663/* Read multibyte form and return it as a character. C is a first
1664 byte of multibyte form, and rest of them are read from
1665 READCHARFUN. */
6f7f43d5 1666
fe0e03f3
KH
1667static int
1668read_multibyte (c, readcharfun)
1669 register int c;
1670 Lisp_Object readcharfun;
1671{
1672 /* We need the actual character code of this multibyte
1673 characters. */
449fea39 1674 unsigned char str[MAX_MULTIBYTE_LENGTH];
fe0e03f3 1675 int len = 0;
0d5e5843 1676 int bytes;
fe0e03f3 1677
abb13b09
CW
1678 if (c < 0)
1679 return c;
1680
fe0e03f3
KH
1681 str[len++] = c;
1682 while ((c = READCHAR) >= 0xA0
449fea39 1683 && len < MAX_MULTIBYTE_LENGTH)
abb13b09
CW
1684 {
1685 str[len++] = c;
1686 readchar_count--;
1687 }
fe0e03f3 1688 UNREAD (c);
0d5e5843
KH
1689 if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
1690 return STRING_CHAR (str, len);
1691 /* The byte sequence is not valid as multibyte. Unread all bytes
1692 but the first one, and return the first byte. */
1693 while (--len > 0)
1694 UNREAD (str[len]);
1695 return str[0];
fe0e03f3
KH
1696}
1697
f6f79b37
RS
1698/* Read a \-escape sequence, assuming we already read the `\'.
1699 If the escape sequence forces unibyte, store 1 into *BYTEREP.
1700 If the escape sequence forces multibyte, store 2 into *BYTEREP.
1701 Otherwise store 0 into *BYTEREP. */
6f7f43d5 1702
078e7b4a 1703static int
f6f79b37 1704read_escape (readcharfun, stringp, byterep)
078e7b4a 1705 Lisp_Object readcharfun;
e7fc914b 1706 int stringp;
f6f79b37 1707 int *byterep;
078e7b4a
JB
1708{
1709 register int c = READCHAR;
f6f79b37
RS
1710
1711 *byterep = 0;
1712
078e7b4a
JB
1713 switch (c)
1714 {
f3849f25 1715 case -1:
da3b886d 1716 end_of_file_error ();
f3849f25 1717
078e7b4a 1718 case 'a':
265a9e55 1719 return '\007';
078e7b4a
JB
1720 case 'b':
1721 return '\b';
f405a585
RS
1722 case 'd':
1723 return 0177;
078e7b4a
JB
1724 case 'e':
1725 return 033;
1726 case 'f':
1727 return '\f';
1728 case 'n':
1729 return '\n';
1730 case 'r':
1731 return '\r';
1732 case 't':
1733 return '\t';
1734 case 'v':
1735 return '\v';
1736 case '\n':
1737 return -1;
e28552a4 1738 case ' ':
e7fc914b
KH
1739 if (stringp)
1740 return -1;
1741 return ' ';
078e7b4a
JB
1742
1743 case 'M':
1744 c = READCHAR;
1745 if (c != '-')
1746 error ("Invalid escape character syntax");
1747 c = READCHAR;
1748 if (c == '\\')
f6f79b37 1749 c = read_escape (readcharfun, 0, byterep);
7bd279cd 1750 return c | meta_modifier;
f405a585
RS
1751
1752 case 'S':
1753 c = READCHAR;
1754 if (c != '-')
1755 error ("Invalid escape character syntax");
1756 c = READCHAR;
1757 if (c == '\\')
f6f79b37 1758 c = read_escape (readcharfun, 0, byterep);
7bd279cd
RS
1759 return c | shift_modifier;
1760
1761 case 'H':
1762 c = READCHAR;
1763 if (c != '-')
1764 error ("Invalid escape character syntax");
1765 c = READCHAR;
1766 if (c == '\\')
f6f79b37 1767 c = read_escape (readcharfun, 0, byterep);
7bd279cd
RS
1768 return c | hyper_modifier;
1769
1770 case 'A':
1771 c = READCHAR;
1772 if (c != '-')
1773 error ("Invalid escape character syntax");
1774 c = READCHAR;
1775 if (c == '\\')
f6f79b37 1776 c = read_escape (readcharfun, 0, byterep);
7bd279cd
RS
1777 return c | alt_modifier;
1778
1779 case 's':
1780 c = READCHAR;
010b7eac
RS
1781 if (c != '-')
1782 {
1783 UNREAD (c);
1784 return ' ';
1785 }
7bd279cd
RS
1786 c = READCHAR;
1787 if (c == '\\')
f6f79b37 1788 c = read_escape (readcharfun, 0, byterep);
7bd279cd 1789 return c | super_modifier;
078e7b4a
JB
1790
1791 case 'C':
1792 c = READCHAR;
1793 if (c != '-')
1794 error ("Invalid escape character syntax");
1795 case '^':
1796 c = READCHAR;
1797 if (c == '\\')
f6f79b37 1798 c = read_escape (readcharfun, 0, byterep);
164d590d
KH
1799 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1800 return 0177 | (c & CHAR_MODIFIER_MASK);
1801 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1802 return c | ctrl_modifier;
f405a585
RS
1803 /* ASCII control chars are made from letters (both cases),
1804 as well as the non-letters within 0100...0137. */
1805 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1806 return (c & (037 | ~0177));
1807 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1808 return (c & (037 | ~0177));
078e7b4a 1809 else
7bd279cd 1810 return c | ctrl_modifier;
078e7b4a
JB
1811
1812 case '0':
1813 case '1':
1814 case '2':
1815 case '3':
1816 case '4':
1817 case '5':
1818 case '6':
1819 case '7':
1820 /* An octal escape, as in ANSI C. */
1821 {
1822 register int i = c - '0';
1823 register int count = 0;
1824 while (++count < 3)
1825 {
1826 if ((c = READCHAR) >= '0' && c <= '7')
1827 {
1828 i *= 8;
1829 i += c - '0';
1830 }
1831 else
1832 {
1833 UNREAD (c);
1834 break;
1835 }
1836 }
177c0ea7 1837
f6f79b37 1838 *byterep = 1;
078e7b4a
JB
1839 return i;
1840 }
1841
1842 case 'x':
1843 /* A hex escape, as in ANSI C. */
1844 {
1845 int i = 0;
1846 while (1)
1847 {
1848 c = READCHAR;
1849 if (c >= '0' && c <= '9')
1850 {
1851 i *= 16;
1852 i += c - '0';
1853 }
1854 else if ((c >= 'a' && c <= 'f')
1855 || (c >= 'A' && c <= 'F'))
1856 {
1857 i *= 16;
1858 if (c >= 'a' && c <= 'f')
1859 i += c - 'a' + 10;
1860 else
1861 i += c - 'A' + 10;
1862 }
1863 else
1864 {
1865 UNREAD (c);
1866 break;
1867 }
1868 }
f6f79b37
RS
1869
1870 *byterep = 2;
078e7b4a
JB
1871 return i;
1872 }
1873
1874 default:
fe0e03f3
KH
1875 if (BASE_LEADING_CODE_P (c))
1876 c = read_multibyte (c, readcharfun);
078e7b4a
JB
1877 return c;
1878 }
1879}
1880
bf5d1a17
GM
1881
1882/* Read an integer in radix RADIX using READCHARFUN to read
1883 characters. RADIX must be in the interval [2..36]; if it isn't, a
1884 read error is signaled . Value is the integer read. Signals an
1885 error if encountering invalid read syntax or if RADIX is out of
1886 range. */
1887
1888static Lisp_Object
1889read_integer (readcharfun, radix)
1890 Lisp_Object readcharfun;
1891 int radix;
1892{
5e37dc22
GM
1893 int ndigits = 0, invalid_p, c, sign = 0;
1894 EMACS_INT number = 0;
bf5d1a17
GM
1895
1896 if (radix < 2 || radix > 36)
1897 invalid_p = 1;
1898 else
1899 {
1900 number = ndigits = invalid_p = 0;
1901 sign = 1;
1902
1903 c = READCHAR;
1904 if (c == '-')
1905 {
1906 c = READCHAR;
1907 sign = -1;
1908 }
1909 else if (c == '+')
1910 c = READCHAR;
177c0ea7 1911
bf5d1a17
GM
1912 while (c >= 0)
1913 {
1914 int digit;
177c0ea7 1915
bf5d1a17
GM
1916 if (c >= '0' && c <= '9')
1917 digit = c - '0';
1918 else if (c >= 'a' && c <= 'z')
1919 digit = c - 'a' + 10;
1920 else if (c >= 'A' && c <= 'Z')
1921 digit = c - 'A' + 10;
1922 else
b632fa48
GM
1923 {
1924 UNREAD (c);
1925 break;
1926 }
bf5d1a17
GM
1927
1928 if (digit < 0 || digit >= radix)
1929 invalid_p = 1;
1930
1931 number = radix * number + digit;
1932 ++ndigits;
1933 c = READCHAR;
1934 }
1935 }
1936
1937 if (ndigits == 0 || invalid_p)
1938 {
1939 char buf[50];
1940 sprintf (buf, "integer, radix %d", radix);
1941 Fsignal (Qinvalid_read_syntax, Fcons (build_string (buf), Qnil));
1942 }
1943
1944 return make_number (sign * number);
1945}
1946
1947
a742d646
GM
1948/* Convert unibyte text in read_buffer to multibyte.
1949
1950 Initially, *P is a pointer after the end of the unibyte text, and
1951 the pointer *END points after the end of read_buffer.
1952
1953 If read_buffer doesn't have enough room to hold the result
1954 of the conversion, reallocate it and adjust *P and *END.
1955
1956 At the end, make *P point after the result of the conversion, and
1957 return in *NCHARS the number of characters in the converted
1958 text. */
1959
1960static void
1961to_multibyte (p, end, nchars)
1962 char **p, **end;
1963 int *nchars;
1964{
1965 int nbytes;
1966
1967 parse_str_as_multibyte (read_buffer, *p - read_buffer, &nbytes, nchars);
b4a3be43 1968 if (read_buffer_size < 2 * nbytes)
a742d646
GM
1969 {
1970 int offset = *p - read_buffer;
fe957e65 1971 read_buffer_size = 2 * max (read_buffer_size, nbytes);
a742d646
GM
1972 read_buffer = (char *) xrealloc (read_buffer, read_buffer_size);
1973 *p = read_buffer + offset;
1974 *end = read_buffer + read_buffer_size;
1975 }
1976
1977 if (nbytes != *nchars)
1978 nbytes = str_as_multibyte (read_buffer, read_buffer_size,
1979 *p - read_buffer, nchars);
177c0ea7 1980
a742d646
GM
1981 *p = read_buffer + nbytes;
1982}
1983
1984
6428369f
KH
1985/* If the next token is ')' or ']' or '.', we store that character
1986 in *PCH and the return value is not interesting. Else, we store
17634846
RS
1987 zero in *PCH and we read and return one lisp object.
1988
1989 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1990
078e7b4a 1991static Lisp_Object
17634846 1992read1 (readcharfun, pch, first_in_list)
078e7b4a 1993 register Lisp_Object readcharfun;
e28552a4 1994 int *pch;
17634846 1995 int first_in_list;
078e7b4a
JB
1996{
1997 register int c;
4ad679f9
EN
1998 int uninterned_symbol = 0;
1999
6428369f 2000 *pch = 0;
078e7b4a
JB
2001
2002 retry:
2003
2004 c = READCHAR;
9c97398c
GM
2005 if (c < 0)
2006 end_of_file_error ();
078e7b4a
JB
2007
2008 switch (c)
2009 {
2010 case '(':
2011 return read_list (0, readcharfun);
2012
2013 case '[':
c15cfd1f 2014 return read_vector (readcharfun, 0);
078e7b4a
JB
2015
2016 case ')':
2017 case ']':
078e7b4a 2018 {
6428369f
KH
2019 *pch = c;
2020 return Qnil;
078e7b4a
JB
2021 }
2022
2023 case '#':
200f684e 2024 c = READCHAR;
c2390933
RS
2025 if (c == '^')
2026 {
2027 c = READCHAR;
2028 if (c == '[')
2029 {
2030 Lisp_Object tmp;
c15cfd1f 2031 tmp = read_vector (readcharfun, 0);
c2390933
RS
2032 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
2033 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
2034 error ("Invalid size char-table");
2035 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
3701b5de 2036 XCHAR_TABLE (tmp)->top = Qt;
c2390933
RS
2037 return tmp;
2038 }
3701b5de
KH
2039 else if (c == '^')
2040 {
2041 c = READCHAR;
2042 if (c == '[')
2043 {
2044 Lisp_Object tmp;
c15cfd1f 2045 tmp = read_vector (readcharfun, 0);
3701b5de
KH
2046 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
2047 error ("Invalid size char-table");
2048 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
2049 XCHAR_TABLE (tmp)->top = Qnil;
2050 return tmp;
2051 }
2052 Fsignal (Qinvalid_read_syntax,
2053 Fcons (make_string ("#^^", 3), Qnil));
2054 }
c2390933
RS
2055 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
2056 }
2057 if (c == '&')
2058 {
2059 Lisp_Object length;
2060 length = read1 (readcharfun, pch, first_in_list);
2061 c = READCHAR;
2062 if (c == '"')
2063 {
2064 Lisp_Object tmp, val;
d1ca81d9
AS
2065 int size_in_chars
2066 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2067 / BOOL_VECTOR_BITS_PER_CHAR);
c2390933
RS
2068
2069 UNREAD (c);
2070 tmp = read1 (readcharfun, pch, first_in_list);
d5db4077 2071 if (size_in_chars != SCHARS (tmp)
90ed3ec5
RS
2072 /* We used to print 1 char too many
2073 when the number of bits was a multiple of 8.
2074 Accept such input in case it came from an old version. */
2075 && ! (XFASTINT (length)
d1ca81d9 2076 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR))
c2390933 2077 Fsignal (Qinvalid_read_syntax,
ec3bbd7d 2078 Fcons (make_string ("#&...", 5), Qnil));
177c0ea7 2079
c2390933 2080 val = Fmake_bool_vector (length, Qnil);
d5db4077 2081 bcopy (SDATA (tmp), XBOOL_VECTOR (val)->data,
c2390933 2082 size_in_chars);
67d3b149 2083 /* Clear the extraneous bits in the last byte. */
d1ca81d9 2084 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
67d3b149 2085 XBOOL_VECTOR (val)->data[size_in_chars - 1]
d1ca81d9 2086 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
c2390933
RS
2087 return val;
2088 }
ec3bbd7d 2089 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
039dc30b 2090 Qnil));
c2390933 2091 }
200f684e
RS
2092 if (c == '[')
2093 {
2094 /* Accept compiled functions at read-time so that we don't have to
2095 build them using function calls. */
748ef62f 2096 Lisp_Object tmp;
c15cfd1f 2097 tmp = read_vector (readcharfun, 1);
748ef62f
RS
2098 return Fmake_byte_code (XVECTOR (tmp)->size,
2099 XVECTOR (tmp)->contents);
200f684e 2100 }
748ef62f
RS
2101 if (c == '(')
2102 {
2103 Lisp_Object tmp;
2104 struct gcpro gcpro1;
e28552a4 2105 int ch;
748ef62f
RS
2106
2107 /* Read the string itself. */
17634846 2108 tmp = read1 (readcharfun, &ch, 0);
6428369f 2109 if (ch != 0 || !STRINGP (tmp))
748ef62f
RS
2110 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2111 GCPRO1 (tmp);
2112 /* Read the intervals and their properties. */
2113 while (1)
2114 {
2115 Lisp_Object beg, end, plist;
2116
17634846 2117 beg = read1 (readcharfun, &ch, 0);
7ee3bd7b 2118 end = plist = Qnil;
6428369f
KH
2119 if (ch == ')')
2120 break;
2121 if (ch == 0)
17634846 2122 end = read1 (readcharfun, &ch, 0);
6428369f 2123 if (ch == 0)
17634846 2124 plist = read1 (readcharfun, &ch, 0);
6428369f 2125 if (ch)
748ef62f 2126 Fsignal (Qinvalid_read_syntax,
6428369f
KH
2127 Fcons (build_string ("invalid string property list"),
2128 Qnil));
748ef62f
RS
2129 Fset_text_properties (beg, end, plist, tmp);
2130 }
2131 UNGCPRO;
2132 return tmp;
2133 }
177c0ea7 2134
20ea2964
RS
2135 /* #@NUMBER is used to skip NUMBER following characters.
2136 That's used in .elc files to skip over doc strings
2137 and function definitions. */
2138 if (c == '@')
2139 {
2140 int i, nskip = 0;
2141
2142 /* Read a decimal integer. */
2143 while ((c = READCHAR) >= 0
2144 && c >= '0' && c <= '9')
2145 {
2146 nskip *= 10;
2147 nskip += c - '0';
2148 }
2149 if (c >= 0)
2150 UNREAD (c);
177c0ea7 2151
b2a30870
RS
2152 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
2153 {
2154 /* If we are supposed to force doc strings into core right now,
2155 record the last string that we skipped,
2156 and record where in the file it comes from. */
c15cfd1f
RS
2157
2158 /* But first exchange saved_doc_string
2159 with prev_saved_doc_string, so we save two strings. */
2160 {
2161 char *temp = saved_doc_string;
2162 int temp_size = saved_doc_string_size;
68c45bf0 2163 file_offset temp_pos = saved_doc_string_position;
c15cfd1f
RS
2164 int temp_len = saved_doc_string_length;
2165
2166 saved_doc_string = prev_saved_doc_string;
2167 saved_doc_string_size = prev_saved_doc_string_size;
2168 saved_doc_string_position = prev_saved_doc_string_position;
2169 saved_doc_string_length = prev_saved_doc_string_length;
2170
2171 prev_saved_doc_string = temp;
2172 prev_saved_doc_string_size = temp_size;
2173 prev_saved_doc_string_position = temp_pos;
2174 prev_saved_doc_string_length = temp_len;
2175 }
2176
b2a30870
RS
2177 if (saved_doc_string_size == 0)
2178 {
2179 saved_doc_string_size = nskip + 100;
11938f10 2180 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
b2a30870
RS
2181 }
2182 if (nskip > saved_doc_string_size)
2183 {
2184 saved_doc_string_size = nskip + 100;
11938f10
KH
2185 saved_doc_string = (char *) xrealloc (saved_doc_string,
2186 saved_doc_string_size);
b2a30870
RS
2187 }
2188
68c45bf0 2189 saved_doc_string_position = file_tell (instream);
b2a30870
RS
2190
2191 /* Copy that many characters into saved_doc_string. */
2192 for (i = 0; i < nskip && c >= 0; i++)
2193 saved_doc_string[i] = c = READCHAR;
2194
2195 saved_doc_string_length = i;
2196 }
2197 else
b2a30870
RS
2198 {
2199 /* Skip that many characters. */
2200 for (i = 0; i < nskip && c >= 0; i++)
2201 c = READCHAR;
2202 }
d49f0c1a 2203
20ea2964
RS
2204 goto retry;
2205 }
e2518d02
RS
2206 if (c == '!')
2207 {
2208 /* #! appears at the beginning of an executable file.
2209 Skip the first line. */
225c7a07 2210 while (c != '\n' && c >= 0)
e2518d02
RS
2211 c = READCHAR;
2212 goto retry;
2213 }
20ea2964
RS
2214 if (c == '$')
2215 return Vload_file_name;
2b6cae0c
RS
2216 if (c == '\'')
2217 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
4ad679f9
EN
2218 /* #:foo is the uninterned symbol named foo. */
2219 if (c == ':')
2220 {
2221 uninterned_symbol = 1;
2222 c = READCHAR;
2223 goto default_label;
2224 }
2225 /* Reader forms that can reuse previously read objects. */
2226 if (c >= '0' && c <= '9')
2227 {
2228 int n = 0;
2229 Lisp_Object tem;
2b6cae0c 2230
4ad679f9
EN
2231 /* Read a non-negative integer. */
2232 while (c >= '0' && c <= '9')
2233 {
2234 n *= 10;
2235 n += c - '0';
2236 c = READCHAR;
2237 }
2238 /* #n=object returns object, but associates it with n for #n#. */
2239 if (c == '=')
2240 {
9e062b6c
RS
2241 /* Make a placeholder for #n# to use temporarily */
2242 Lisp_Object placeholder;
2243 Lisp_Object cell;
2244
2245 placeholder = Fcons(Qnil, Qnil);
2246 cell = Fcons (make_number (n), placeholder);
2247 read_objects = Fcons (cell, read_objects);
2248
2249 /* Read the object itself. */
4ad679f9 2250 tem = read0 (readcharfun);
9e062b6c
RS
2251
2252 /* Now put it everywhere the placeholder was... */
2253 substitute_object_in_subtree (tem, placeholder);
2254
2255 /* ...and #n# will use the real value from now on. */
2256 Fsetcdr (cell, tem);
177c0ea7 2257
4ad679f9
EN
2258 return tem;
2259 }
2260 /* #n# returns a previously read object. */
2261 if (c == '#')
2262 {
2263 tem = Fassq (make_number (n), read_objects);
2264 if (CONSP (tem))
2265 return XCDR (tem);
2266 /* Fall through to error message. */
2267 }
bf5d1a17
GM
2268 else if (c == 'r' || c == 'R')
2269 return read_integer (readcharfun, n);
177c0ea7 2270
4ad679f9
EN
2271 /* Fall through to error message. */
2272 }
bf5d1a17
GM
2273 else if (c == 'x' || c == 'X')
2274 return read_integer (readcharfun, 16);
2275 else if (c == 'o' || c == 'O')
2276 return read_integer (readcharfun, 8);
2277 else if (c == 'b' || c == 'B')
2278 return read_integer (readcharfun, 2);
20ea2964 2279
200f684e 2280 UNREAD (c);
748ef62f 2281 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
078e7b4a
JB
2282
2283 case ';':
2284 while ((c = READCHAR) >= 0 && c != '\n');
2285 goto retry;
2286
2287 case '\'':
2288 {
2289 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2290 }
2291
17634846
RS
2292 case '`':
2293 if (first_in_list)
2294 goto default_label;
2295 else
2296 {
2297 Lisp_Object value;
2298
0ffbbdeb 2299 new_backquote_flag++;
17634846 2300 value = read0 (readcharfun);
0ffbbdeb 2301 new_backquote_flag--;
17634846
RS
2302
2303 return Fcons (Qbackquote, Fcons (value, Qnil));
2304 }
2305
2306 case ',':
2307 if (new_backquote_flag)
2308 {
2309 Lisp_Object comma_type = Qnil;
2310 Lisp_Object value;
2311 int ch = READCHAR;
2312
2313 if (ch == '@')
2314 comma_type = Qcomma_at;
2315 else if (ch == '.')
2316 comma_type = Qcomma_dot;
2317 else
2318 {
2319 if (ch >= 0) UNREAD (ch);
2320 comma_type = Qcomma;
2321 }
2322
0ffbbdeb 2323 new_backquote_flag--;
17634846 2324 value = read0 (readcharfun);
0ffbbdeb 2325 new_backquote_flag++;
17634846
RS
2326 return Fcons (comma_type, Fcons (value, Qnil));
2327 }
2328 else
2329 goto default_label;
2330
078e7b4a
JB
2331 case '?':
2332 {
f6f79b37 2333 int discard;
df9c2be7
KS
2334 int next_char;
2335 int ok;
f6f79b37 2336
078e7b4a 2337 c = READCHAR;
9c97398c
GM
2338 if (c < 0)
2339 end_of_file_error ();
078e7b4a 2340
b9284371
KS
2341 /* Accept `single space' syntax like (list ? x) where the
2342 whitespace character is SPC or TAB.
2343 Other literal whitespace like NL, CR, and FF are not accepted,
2344 as there are well-established escape sequences for these. */
2345 if (c == ' ' || c == '\t')
2346 return make_number (c);
2347
078e7b4a 2348 if (c == '\\')
f6f79b37 2349 c = read_escape (readcharfun, 0, &discard);
fe0e03f3
KH
2350 else if (BASE_LEADING_CODE_P (c))
2351 c = read_multibyte (c, readcharfun);
078e7b4a 2352
df9c2be7
KS
2353 next_char = READCHAR;
2354 if (next_char == '.')
2355 {
2356 /* Only a dotted-pair dot is valid after a char constant. */
2357 int next_next_char = READCHAR;
2358 UNREAD (next_next_char);
2359
2360 ok = (next_next_char <= 040
e613ea97
KH
2361 || (next_next_char < 0200
2362 && (index ("\"';([#?", next_next_char)
2363 || (!first_in_list && next_next_char == '`')
2364 || (new_backquote_flag && next_next_char == ','))));
df9c2be7
KS
2365 }
2366 else
2367 {
2368 ok = (next_char <= 040
e613ea97
KH
2369 || (next_char < 0200
2370 && (index ("\"';()[]#?", next_char)
2371 || (!first_in_list && next_char == '`')
2372 || (new_backquote_flag && next_char == ','))));
df9c2be7
KS
2373 }
2374 UNREAD (next_char);
2375 if (!ok)
37cd4238
KS
2376 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("?", 1), Qnil));
2377
6f7f43d5 2378 return make_number (c);
078e7b4a
JB
2379 }
2380
00a9a935 2381 case '"':
078e7b4a 2382 {
a742d646
GM
2383 char *p = read_buffer;
2384 char *end = read_buffer + read_buffer_size;
078e7b4a 2385 register int c;
5150eeec
RS
2386 /* 1 if we saw an escape sequence specifying
2387 a multibyte character, or a multibyte character. */
e7fc914b 2388 int force_multibyte = 0;
5150eeec 2389 /* 1 if we saw an escape sequence specifying
e7fc914b
KH
2390 a single-byte character. */
2391 int force_singlebyte = 0;
5150eeec
RS
2392 /* 1 if read_buffer contains multibyte text now. */
2393 int is_multibyte = 0;
078e7b4a 2394 int cancel = 0;
5150eeec 2395 int nchars = 0;
078e7b4a
JB
2396
2397 while ((c = READCHAR) >= 0
2398 && c != '\"')
2399 {
449fea39 2400 if (end - p < MAX_MULTIBYTE_LENGTH)
078e7b4a 2401 {
5d65df0d
GM
2402 int offset = p - read_buffer;
2403 read_buffer = (char *) xrealloc (read_buffer,
2404 read_buffer_size *= 2);
2405 p = read_buffer + offset;
078e7b4a
JB
2406 end = read_buffer + read_buffer_size;
2407 }
bed23cb2 2408
078e7b4a 2409 if (c == '\\')
03e88613 2410 {
f6f79b37
RS
2411 int byterep;
2412
2413 c = read_escape (readcharfun, 1, &byterep);
bed23cb2
RS
2414
2415 /* C is -1 if \ newline has just been seen */
2416 if (c == -1)
03e88613 2417 {
bed23cb2
RS
2418 if (p == read_buffer)
2419 cancel = 1;
03e88613
RS
2420 continue;
2421 }
bed23cb2 2422
f6f79b37 2423 if (byterep == 1)
e7fc914b 2424 force_singlebyte = 1;
f6f79b37
RS
2425 else if (byterep == 2)
2426 force_multibyte = 1;
03e88613 2427 }
6f7f43d5 2428
5150eeec
RS
2429 /* A character that must be multibyte forces multibyte. */
2430 if (! SINGLE_BYTE_CHAR_P (c & ~CHAR_MODIFIER_MASK))
2431 force_multibyte = 1;
2432
2433 /* If we just discovered the need to be multibyte,
2434 convert the text accumulated thus far. */
2435 if (force_multibyte && ! is_multibyte)
078e7b4a 2436 {
5150eeec
RS
2437 is_multibyte = 1;
2438 to_multibyte (&p, &end, &nchars);
078e7b4a 2439 }
988c2f83 2440
5150eeec
RS
2441 /* Allow `\C- ' and `\C-?'. */
2442 if (c == (CHAR_CTL | ' '))
2443 c = 0;
2444 else if (c == (CHAR_CTL | '?'))
2445 c = 127;
95af0924 2446
5150eeec
RS
2447 if (c & CHAR_SHIFT)
2448 {
2449 /* Shift modifier is valid only with [A-Za-z]. */
2450 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2451 c &= ~CHAR_SHIFT;
2452 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2453 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
f943104a 2454 }
5150eeec
RS
2455
2456 if (c & CHAR_META)
2457 /* Move the meta bit to the right place for a string. */
2458 c = (c & ~CHAR_META) | 0x80;
2459 if (c & CHAR_MODIFIER_MASK)
2460 error ("Invalid modifier in string");
2461
2462 if (is_multibyte)
2463 p += CHAR_STRING (c, p);
2464 else
2465 *p++ = c;
2466
2467 nchars++;
078e7b4a 2468 }
5150eeec 2469
6f7f43d5 2470 if (c < 0)
9c97398c 2471 end_of_file_error ();
078e7b4a
JB
2472
2473 /* If purifying, and string starts with \ newline,
2474 return zero instead. This is for doc strings
08564963 2475 that we are really going to find in etc/DOC.nn.nn */
265a9e55 2476 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
078e7b4a
JB
2477 return make_number (0);
2478
5150eeec
RS
2479 if (is_multibyte || force_singlebyte)
2480 ;
94e554db
RS
2481 else if (load_convert_to_unibyte)
2482 {
2483 Lisp_Object string;
a742d646 2484 to_multibyte (&p, &end, &nchars);
94e554db
RS
2485 if (p - read_buffer != nchars)
2486 {
2487 string = make_multibyte_string (read_buffer, nchars,
2488 p - read_buffer);
2489 return Fstring_make_unibyte (string);
2490 }
5150eeec
RS
2491 /* We can make a unibyte string directly. */
2492 is_multibyte = 0;
94e554db 2493 }
8db9dc66
RS
2494 else if (EQ (readcharfun, Qget_file_char)
2495 || EQ (readcharfun, Qlambda))
a742d646
GM
2496 {
2497 /* Nowadays, reading directly from a file is used only for
2498 compiled Emacs Lisp files, and those always use the
2499 Emacs internal encoding. Meanwhile, Qlambda is used
2500 for reading dynamic byte code (compiled with
81a268b4
RS
2501 byte-compile-dynamic = t). So make the string multibyte
2502 if the string contains any multibyte sequences.
2503 (to_multibyte is a no-op if not.) */
a742d646 2504 to_multibyte (&p, &end, &nchars);
81a268b4 2505 is_multibyte = (p - read_buffer) != nchars;
a742d646 2506 }
e28552a4 2507 else
bed23cb2
RS
2508 /* In all other cases, if we read these bytes as
2509 separate characters, treat them as separate characters now. */
5150eeec 2510 ;
e7fc914b 2511
abb13b09
CW
2512 /* We want readchar_count to be the number of characters, not
2513 bytes. Hence we adjust for multibyte characters in the
2514 string. ... But it doesn't seem to be necessary, because
2515 READCHAR *does* read multibyte characters from buffers. */
2516 /* readchar_count -= (p - read_buffer) - nchars; */
e7fc914b 2517 if (read_pure)
491f16a2 2518 return make_pure_string (read_buffer, nchars, p - read_buffer,
5150eeec 2519 is_multibyte);
491f16a2 2520 return make_specified_string (read_buffer, nchars, p - read_buffer,
5150eeec 2521 is_multibyte);
078e7b4a
JB
2522 }
2523
109d300c
JB
2524 case '.':
2525 {
109d300c
JB
2526 int next_char = READCHAR;
2527 UNREAD (next_char);
2528
035eec48 2529 if (next_char <= 040
e613ea97 2530 || (next_char < 0200
95af0924
KS
2531 && (index ("\"';([#?", next_char)
2532 || (!first_in_list && next_char == '`')
2533 || (new_backquote_flag && next_char == ','))))
109d300c 2534 {
6428369f
KH
2535 *pch = c;
2536 return Qnil;
109d300c
JB
2537 }
2538
2539 /* Otherwise, we fall through! Note that the atom-reading loop
2540 below will now loop at least once, assuring that we will not
2541 try to UNREAD two characters in a row. */
2542 }
078e7b4a 2543 default:
17634846 2544 default_label:
078e7b4a
JB
2545 if (c <= 040) goto retry;
2546 {
38404229 2547 char *p = read_buffer;
481c6336 2548 int quoted = 0;
078e7b4a
JB
2549
2550 {
38404229 2551 char *end = read_buffer + read_buffer_size;
078e7b4a 2552
6f7f43d5 2553 while (c > 040
e613ea97
KH
2554 && (c >= 0200
2555 || (!index ("\"';()[]#", c)
2556 && !(!first_in_list && c == '`')
2557 && !(new_backquote_flag && c == ','))))
078e7b4a 2558 {
449fea39 2559 if (end - p < MAX_MULTIBYTE_LENGTH)
078e7b4a 2560 {
5d65df0d
GM
2561 int offset = p - read_buffer;
2562 read_buffer = (char *) xrealloc (read_buffer,
2563 read_buffer_size *= 2);
2564 p = read_buffer + offset;
078e7b4a
JB
2565 end = read_buffer + read_buffer_size;
2566 }
177c0ea7 2567
078e7b4a 2568 if (c == '\\')
481c6336
RS
2569 {
2570 c = READCHAR;
4ab11c09
GM
2571 if (c == -1)
2572 end_of_file_error ();
481c6336
RS
2573 quoted = 1;
2574 }
6f7f43d5 2575
bed23cb2 2576 if (! SINGLE_BYTE_CHAR_P (c))
449fea39 2577 p += CHAR_STRING (c, p);
bed23cb2
RS
2578 else
2579 *p++ = c;
6f7f43d5 2580
078e7b4a
JB
2581 c = READCHAR;
2582 }
2583
2584 if (p == end)
2585 {
5d65df0d
GM
2586 int offset = p - read_buffer;
2587 read_buffer = (char *) xrealloc (read_buffer,
2588 read_buffer_size *= 2);
2589 p = read_buffer + offset;
2590 end = read_buffer + read_buffer_size;
078e7b4a
JB
2591 }
2592 *p = 0;
2593 if (c >= 0)
2594 UNREAD (c);
2595 }
2596
4ad679f9 2597 if (!quoted && !uninterned_symbol)
481c6336
RS
2598 {
2599 register char *p1;
2600 register Lisp_Object val;
2601 p1 = read_buffer;
2602 if (*p1 == '+' || *p1 == '-') p1++;
2603 /* Is it an integer? */
2604 if (p1 != p)
2605 {
2606 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
481c6336
RS
2607 /* Integers can have trailing decimal points. */
2608 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
481c6336
RS
2609 if (p1 == p)
2610 /* It is an integer. */
2611 {
481c6336
RS
2612 if (p1[-1] == '.')
2613 p1[-1] = '\0';
faca07fb
RS
2614 if (sizeof (int) == sizeof (EMACS_INT))
2615 XSETINT (val, atoi (read_buffer));
2616 else if (sizeof (long) == sizeof (EMACS_INT))
2617 XSETINT (val, atol (read_buffer));
2618 else
2619 abort ();
481c6336
RS
2620 return val;
2621 }
2622 }
481c6336 2623 if (isfloat_string (read_buffer))
eb659c41 2624 {
a8972052
PE
2625 /* Compute NaN and infinities using 0.0 in a variable,
2626 to cope with compilers that think they are smarter
5e24a1f7 2627 than we are. */
3c329963 2628 double zero = 0.0;
a8972052
PE
2629
2630 double value;
2631
2632 /* Negate the value ourselves. This treats 0, NaNs,
2633 and infinity properly on IEEE floating point hosts,
2634 and works around a common bug where atof ("-0.0")
2635 drops the sign. */
2636 int negative = read_buffer[0] == '-';
2637
2638 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
eb659c41 2639 returns 1, is if the input ends in e+INF or e+NaN. */
a8972052 2640 switch (p[-1])
eb659c41 2641 {
a8972052
PE
2642 case 'F':
2643 value = 1.0 / zero;
2644 break;
2645 case 'N':
2646 value = zero / zero;
7690cbb0
RS
2647
2648 /* If that made a "negative" NaN, negate it. */
2649
2650 {
2651 int i;
2652 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
2653
2654 u_data.d = value;
2655 u_minus_zero.d = - 0.0;
2656 for (i = 0; i < sizeof (double); i++)
2657 if (u_data.c[i] & u_minus_zero.c[i])
2658 {
2659 value = - value;
2660 break;
2661 }
2662 }
2663 /* Now VALUE is a positive NaN. */
a8972052
PE
2664 break;
2665 default:
2666 value = atof (read_buffer + negative);
2667 break;
eb659c41 2668 }
a8972052
PE
2669
2670 return make_float (negative ? - value : value);
eb659c41 2671 }
481c6336 2672 }
abb13b09
CW
2673 {
2674 Lisp_Object result = uninterned_symbol ? make_symbol (read_buffer)
2675 : intern (read_buffer);
2676 if (EQ (Vread_with_symbol_positions, Qt)
2677 || EQ (Vread_with_symbol_positions, readcharfun))
177c0ea7 2678 Vread_symbol_positions_list =
abb13b09
CW
2679 /* Kind of a hack; this will probably fail if characters
2680 in the symbol name were escaped. Not really a big
2681 deal, though. */
f74db720
SM
2682 Fcons (Fcons (result,
2683 make_number (readchar_count
2684 - XFASTINT (Flength (Fsymbol_name (result))))),
abb13b09
CW
2685 Vread_symbol_positions_list);
2686 return result;
2687 }
078e7b4a
JB
2688 }
2689 }
2690}
2691\f
9e062b6c
RS
2692
2693/* List of nodes we've seen during substitute_object_in_subtree. */
2694static Lisp_Object seen_list;
2695
2696static void
2697substitute_object_in_subtree (object, placeholder)
2698 Lisp_Object object;
2699 Lisp_Object placeholder;
2700{
2701 Lisp_Object check_object;
2702
2703 /* We haven't seen any objects when we start. */
2704 seen_list = Qnil;
2705
2706 /* Make all the substitutions. */
2707 check_object
2708 = substitute_object_recurse (object, placeholder, object);
177c0ea7 2709
9e062b6c
RS
2710 /* Clear seen_list because we're done with it. */
2711 seen_list = Qnil;
2712
2713 /* The returned object here is expected to always eq the
2714 original. */
2715 if (!EQ (check_object, object))
2716 error ("Unexpected mutation error in reader");
2717}
2718
2719/* Feval doesn't get called from here, so no gc protection is needed. */
2720#define SUBSTITUTE(get_val, set_val) \
2721{ \
2722 Lisp_Object old_value = get_val; \
2723 Lisp_Object true_value \
2724 = substitute_object_recurse (object, placeholder,\
2725 old_value); \
2726 \
2727 if (!EQ (old_value, true_value)) \
2728 { \
2729 set_val; \
2730 } \
2731}
2732
2733static Lisp_Object
2734substitute_object_recurse (object, placeholder, subtree)
2735 Lisp_Object object;
2736 Lisp_Object placeholder;
2737 Lisp_Object subtree;
2738{
2739 /* If we find the placeholder, return the target object. */
2740 if (EQ (placeholder, subtree))
2741 return object;
2742
2743 /* If we've been to this node before, don't explore it again. */
2744 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2745 return subtree;
2746
2747 /* If this node can be the entry point to a cycle, remember that
2748 we've seen it. It can only be such an entry point if it was made
2749 by #n=, which means that we can find it as a value in
2750 read_objects. */
2751 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2752 seen_list = Fcons (subtree, seen_list);
177c0ea7 2753
9e062b6c
RS
2754 /* Recurse according to subtree's type.
2755 Every branch must return a Lisp_Object. */
2756 switch (XTYPE (subtree))
2757 {
2758 case Lisp_Vectorlike:
2759 {
2760 int i;
7c752c80 2761 int length = XINT (Flength(subtree));
9e062b6c
RS
2762 for (i = 0; i < length; i++)
2763 {
2764 Lisp_Object idx = make_number (i);
2765 SUBSTITUTE (Faref (subtree, idx),
177c0ea7 2766 Faset (subtree, idx, true_value));
9e062b6c
RS
2767 }
2768 return subtree;
2769 }
2770
2771 case Lisp_Cons:
2772 {
2773 SUBSTITUTE (Fcar_safe (subtree),
e61b9b87 2774 Fsetcar (subtree, true_value));
9e062b6c 2775 SUBSTITUTE (Fcdr_safe (subtree),
e61b9b87 2776 Fsetcdr (subtree, true_value));
9e062b6c
RS
2777 return subtree;
2778 }
2779
9e062b6c
RS
2780 case Lisp_String:
2781 {
2782 /* Check for text properties in each interval.
e61b9b87 2783 substitute_in_interval contains part of the logic. */
9e062b6c 2784
d5db4077 2785 INTERVAL root_interval = STRING_INTERVALS (subtree);
9e062b6c 2786 Lisp_Object arg = Fcons (object, placeholder);
177c0ea7 2787
0d74b006
SM
2788 traverse_intervals_noorder (root_interval,
2789 &substitute_in_interval, arg);
9e062b6c
RS
2790
2791 return subtree;
2792 }
9e062b6c
RS
2793
2794 /* Other types don't recurse any further. */
2795 default:
2796 return subtree;
2797 }
2798}
2799
2800/* Helper function for substitute_object_recurse. */
2801static void
2802substitute_in_interval (interval, arg)
2803 INTERVAL interval;
2804 Lisp_Object arg;
2805{
2806 Lisp_Object object = Fcar (arg);
2807 Lisp_Object placeholder = Fcdr (arg);
2808
2809 SUBSTITUTE(interval->plist, interval->plist = true_value);
2810}
2811
2812\f
078e7b4a
JB
2813#define LEAD_INT 1
2814#define DOT_CHAR 2
2815#define TRAIL_INT 4
2816#define E_CHAR 8
2817#define EXP_INT 16
2818
2819int
2820isfloat_string (cp)
2821 register char *cp;
2822{
c1a2f60a 2823 register int state;
177c0ea7 2824
d8578e58
RS
2825 char *start = cp;
2826
078e7b4a
JB
2827 state = 0;
2828 if (*cp == '+' || *cp == '-')
2829 cp++;
2830
075027b1 2831 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
2832 {
2833 state |= LEAD_INT;
075027b1
RS
2834 while (*cp >= '0' && *cp <= '9')
2835 cp++;
078e7b4a
JB
2836 }
2837 if (*cp == '.')
2838 {
2839 state |= DOT_CHAR;
2840 cp++;
2841 }
075027b1 2842 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
2843 {
2844 state |= TRAIL_INT;
075027b1 2845 while (*cp >= '0' && *cp <= '9')
078e7b4a
JB
2846 cp++;
2847 }
a35f88bf 2848 if (*cp == 'e' || *cp == 'E')
078e7b4a
JB
2849 {
2850 state |= E_CHAR;
2851 cp++;
e73997a1
RS
2852 if (*cp == '+' || *cp == '-')
2853 cp++;
078e7b4a 2854 }
078e7b4a 2855
075027b1 2856 if (*cp >= '0' && *cp <= '9')
078e7b4a
JB
2857 {
2858 state |= EXP_INT;
075027b1 2859 while (*cp >= '0' && *cp <= '9')
078e7b4a
JB
2860 cp++;
2861 }
d8578e58
RS
2862 else if (cp == start)
2863 ;
eb659c41
RS
2864 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2865 {
2866 state |= EXP_INT;
2867 cp += 3;
2868 }
2869 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2870 {
2871 state |= EXP_INT;
2872 cp += 3;
2873 }
2874
37579d7c 2875 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
078e7b4a 2876 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
151bdc83 2877 || state == (DOT_CHAR|TRAIL_INT)
078e7b4a 2878 || state == (LEAD_INT|E_CHAR|EXP_INT)
151bdc83
JB
2879 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2880 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
078e7b4a 2881}
cc94f3b2 2882
078e7b4a
JB
2883\f
2884static Lisp_Object
c15cfd1f 2885read_vector (readcharfun, bytecodeflag)
078e7b4a 2886 Lisp_Object readcharfun;
c15cfd1f 2887 int bytecodeflag;
078e7b4a
JB
2888{
2889 register int i;
2890 register int size;
2891 register Lisp_Object *ptr;
c15cfd1f 2892 register Lisp_Object tem, item, vector;
078e7b4a
JB
2893 register struct Lisp_Cons *otem;
2894 Lisp_Object len;
2895
2896 tem = read_list (1, readcharfun);
2897 len = Flength (tem);
2898 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2899
078e7b4a
JB
2900 size = XVECTOR (vector)->size;
2901 ptr = XVECTOR (vector)->contents;
2902 for (i = 0; i < size; i++)
2903 {
c15cfd1f
RS
2904 item = Fcar (tem);
2905 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2906 bytecode object, the docstring containing the bytecode and
2907 constants values must be treated as unibyte and passed to
2908 Fread, to get the actual bytecode string and constants vector. */
2909 if (bytecodeflag && load_force_doc_strings)
2910 {
2911 if (i == COMPILED_BYTECODE)
2912 {
2913 if (!STRINGP (item))
6fa2b890 2914 error ("Invalid byte code");
c15cfd1f
RS
2915
2916 /* Delay handling the bytecode slot until we know whether
2917 it is lazily-loaded (we can tell by whether the
2918 constants slot is nil). */
2919 ptr[COMPILED_CONSTANTS] = item;
2920 item = Qnil;
2921 }
2922 else if (i == COMPILED_CONSTANTS)
2923 {
2924 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2925
2926 if (NILP (item))
2927 {
2928 /* Coerce string to unibyte (like string-as-unibyte,
2929 but without generating extra garbage and
2930 guaranteeing no change in the contents). */
bee91904 2931 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
d5db4077 2932 STRING_SET_UNIBYTE (bytestr);
c15cfd1f
RS
2933
2934 item = Fread (bytestr);
2935 if (!CONSP (item))
6fa2b890 2936 error ("Invalid byte code");
c15cfd1f
RS
2937
2938 otem = XCONS (item);
c1d497be
KR
2939 bytestr = XCAR (item);
2940 item = XCDR (item);
c15cfd1f
RS
2941 free_cons (otem);
2942 }
2943
2944 /* Now handle the bytecode slot. */
2945 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2946 }
2947 }
2948 ptr[i] = read_pure ? Fpurecopy (item) : item;
078e7b4a
JB
2949 otem = XCONS (tem);
2950 tem = Fcdr (tem);
2951 free_cons (otem);
2952 }
2953 return vector;
2954}
177c0ea7 2955
6f7f43d5
RS
2956/* FLAG = 1 means check for ] to terminate rather than ) and .
2957 FLAG = -1 means check for starting with defun
078e7b4a
JB
2958 and make structure pure. */
2959
2960static Lisp_Object
2961read_list (flag, readcharfun)
2962 int flag;
2963 register Lisp_Object readcharfun;
2964{
2965 /* -1 means check next element for defun,
2966 0 means don't check,
2967 1 means already checked and found defun. */
2968 int defunflag = flag < 0 ? -1 : 0;
2969 Lisp_Object val, tail;
2970 register Lisp_Object elt, tem;
2971 struct gcpro gcpro1, gcpro2;
821d417e 2972 /* 0 is the normal case.
b2a30870 2973 1 means this list is a doc reference; replace it with the number 0.
177c0ea7 2974 2 means this list is a doc reference; replace it with the doc string. */
821d417e 2975 int doc_reference = 0;
078e7b4a 2976
17634846
RS
2977 /* Initialize this to 1 if we are reading a list. */
2978 int first_in_list = flag <= 0;
2979
078e7b4a
JB
2980 val = Qnil;
2981 tail = Qnil;
2982
2983 while (1)
2984 {
e28552a4 2985 int ch;
078e7b4a 2986 GCPRO2 (val, tail);
17634846 2987 elt = read1 (readcharfun, &ch, first_in_list);
078e7b4a 2988 UNGCPRO;
20ea2964 2989
17634846
RS
2990 first_in_list = 0;
2991
821d417e 2992 /* While building, if the list starts with #$, treat it specially. */
20ea2964 2993 if (EQ (elt, Vload_file_name)
d49f0c1a 2994 && ! NILP (elt)
821d417e
RS
2995 && !NILP (Vpurify_flag))
2996 {
2997 if (NILP (Vdoc_file_name))
2998 /* We have not yet called Snarf-documentation, so assume
2999 this file is described in the DOC-MM.NN file
3000 and Snarf-documentation will fill in the right value later.
3001 For now, replace the whole list with 0. */
3002 doc_reference = 1;
3003 else
3004 /* We have already called Snarf-documentation, so make a relative
3005 file name for this file, so it can be found properly
3006 in the installed Lisp directory.
3007 We don't use Fexpand_file_name because that would make
3008 the directory absolute now. */
3009 elt = concat2 (build_string ("../lisp/"),
3010 Ffile_name_nondirectory (elt));
3011 }
b2a30870 3012 else if (EQ (elt, Vload_file_name)
d49f0c1a 3013 && ! NILP (elt)
b2a30870
RS
3014 && load_force_doc_strings)
3015 doc_reference = 2;
20ea2964 3016
6428369f 3017 if (ch)
078e7b4a
JB
3018 {
3019 if (flag > 0)
3020 {
6428369f 3021 if (ch == ']')
078e7b4a 3022 return val;
821d417e
RS
3023 Fsignal (Qinvalid_read_syntax,
3024 Fcons (make_string (") or . in a vector", 18), Qnil));
078e7b4a 3025 }
6428369f 3026 if (ch == ')')
078e7b4a 3027 return val;
6428369f 3028 if (ch == '.')
078e7b4a
JB
3029 {
3030 GCPRO2 (val, tail);
265a9e55 3031 if (!NILP (tail))
f5df591a 3032 XSETCDR (tail, read0 (readcharfun));
078e7b4a
JB
3033 else
3034 val = read0 (readcharfun);
17634846 3035 read1 (readcharfun, &ch, 0);
078e7b4a 3036 UNGCPRO;
6428369f 3037 if (ch == ')')
821d417e
RS
3038 {
3039 if (doc_reference == 1)
3040 return make_number (0);
b2a30870
RS
3041 if (doc_reference == 2)
3042 {
3043 /* Get a doc string from the file we are loading.
3044 If it's in saved_doc_string, get it from there. */
c1d497be 3045 int pos = XINT (XCDR (val));
c15cfd1f
RS
3046 /* Position is negative for user variables. */
3047 if (pos < 0) pos = -pos;
b2a30870
RS
3048 if (pos >= saved_doc_string_position
3049 && pos < (saved_doc_string_position
3050 + saved_doc_string_length))
3051 {
3052 int start = pos - saved_doc_string_position;
3053 int from, to;
3054
3055 /* Process quoting with ^A,
3056 and find the end of the string,
3057 which is marked with ^_ (037). */
3058 for (from = start, to = start;
3059 saved_doc_string[from] != 037;)
3060 {
3061 int c = saved_doc_string[from++];
3062 if (c == 1)
3063 {
3064 c = saved_doc_string[from++];
3065 if (c == 1)
3066 saved_doc_string[to++] = c;
3067 else if (c == '0')
3068 saved_doc_string[to++] = 0;
3069 else if (c == '_')
3070 saved_doc_string[to++] = 037;
3071 }
3072 else
3073 saved_doc_string[to++] = c;
3074 }
3075
3076 return make_string (saved_doc_string + start,
3077 to - start);
3078 }
c15cfd1f
RS
3079 /* Look in prev_saved_doc_string the same way. */
3080 else if (pos >= prev_saved_doc_string_position
3081 && pos < (prev_saved_doc_string_position
3082 + prev_saved_doc_string_length))
3083 {
3084 int start = pos - prev_saved_doc_string_position;
3085 int from, to;
3086
3087 /* Process quoting with ^A,
3088 and find the end of the string,
3089 which is marked with ^_ (037). */
3090 for (from = start, to = start;
3091 prev_saved_doc_string[from] != 037;)
3092 {
3093 int c = prev_saved_doc_string[from++];
3094 if (c == 1)
3095 {
3096 c = prev_saved_doc_string[from++];
3097 if (c == 1)
3098 prev_saved_doc_string[to++] = c;
3099 else if (c == '0')
3100 prev_saved_doc_string[to++] = 0;
3101 else if (c == '_')
3102 prev_saved_doc_string[to++] = 037;
3103 }
3104 else
3105 prev_saved_doc_string[to++] = c;
3106 }
3107
3108 return make_string (prev_saved_doc_string + start,
3109 to - start);
3110 }
b2a30870 3111 else
6fe9cce5 3112 return get_doc_string (val, 0, 0);
b2a30870
RS
3113 }
3114
821d417e
RS
3115 return val;
3116 }
078e7b4a
JB
3117 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
3118 }
3119 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
3120 }
3121 tem = (read_pure && flag <= 0
3122 ? pure_cons (elt, Qnil)
3123 : Fcons (elt, Qnil));
265a9e55 3124 if (!NILP (tail))
f5df591a 3125 XSETCDR (tail, tem);
078e7b4a
JB
3126 else
3127 val = tem;
3128 tail = tem;
3129 if (defunflag < 0)
3130 defunflag = EQ (elt, Qdefun);
3131 else if (defunflag > 0)
3132 read_pure = 1;
3133 }
3134}
3135\f
3136Lisp_Object Vobarray;
3137Lisp_Object initial_obarray;
3138
d007f5c8
RS
3139/* oblookup stores the bucket number here, for the sake of Funintern. */
3140
3141int oblookup_last_bucket_number;
3142
3143static int hash_string ();
d007f5c8
RS
3144
3145/* Get an error if OBARRAY is not an obarray.
3146 If it is one, return it. */
3147
078e7b4a
JB
3148Lisp_Object
3149check_obarray (obarray)
3150 Lisp_Object obarray;
3151{
cfff016d 3152 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
078e7b4a
JB
3153 {
3154 /* If Vobarray is now invalid, force it to be valid. */
3155 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3156
3157 obarray = wrong_type_argument (Qvectorp, obarray);
3158 }
3159 return obarray;
3160}
3161
d007f5c8
RS
3162/* Intern the C string STR: return a symbol with that name,
3163 interned in the current obarray. */
078e7b4a
JB
3164
3165Lisp_Object
3166intern (str)
4b2dd274 3167 const char *str;
078e7b4a
JB
3168{
3169 Lisp_Object tem;
3170 int len = strlen (str);
153a17b7 3171 Lisp_Object obarray;
078e7b4a 3172
153a17b7 3173 obarray = Vobarray;
cfff016d 3174 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
078e7b4a 3175 obarray = check_obarray (obarray);
e28552a4 3176 tem = oblookup (obarray, str, len, len);
cfff016d 3177 if (SYMBOLP (tem))
078e7b4a 3178 return tem;
87631ef7 3179 return Fintern (make_string (str, len), obarray);
078e7b4a 3180}
4ad679f9
EN
3181
3182/* Create an uninterned symbol with name STR. */
3183
3184Lisp_Object
3185make_symbol (str)
3186 char *str;
3187{
3188 int len = strlen (str);
3189
3190 return Fmake_symbol ((!NILP (Vpurify_flag)
491f16a2 3191 ? make_pure_string (str, len, len, 0)
4ad679f9
EN
3192 : make_string (str, len)));
3193}
d007f5c8 3194\f
078e7b4a 3195DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
5de38842
PJ
3196 doc: /* Return the canonical symbol whose name is STRING.
3197If there is none, one is created by this function and returned.
3198A second optional argument specifies the obarray to use;
3199it defaults to the value of `obarray'. */)
3200 (string, obarray)
9391b698 3201 Lisp_Object string, obarray;
078e7b4a
JB
3202{
3203 register Lisp_Object tem, sym, *ptr;
3204
265a9e55 3205 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
3206 obarray = check_obarray (obarray);
3207
b7826503 3208 CHECK_STRING (string);
078e7b4a 3209
d5db4077
KR
3210 tem = oblookup (obarray, SDATA (string),
3211 SCHARS (string),
3212 SBYTES (string));
cfff016d 3213 if (!INTEGERP (tem))
078e7b4a
JB
3214 return tem;
3215
265a9e55 3216 if (!NILP (Vpurify_flag))
9391b698
EN
3217 string = Fpurecopy (string);
3218 sym = Fmake_symbol (string);
44c6c019
GM
3219
3220 if (EQ (obarray, initial_obarray))
3221 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3222 else
3223 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
078e7b4a 3224
d5db4077 3225 if ((SREF (string, 0) == ':')
a458d45d 3226 && EQ (obarray, initial_obarray))
44c6c019
GM
3227 {
3228 XSYMBOL (sym)->constant = 1;
3229 XSYMBOL (sym)->value = sym;
3230 }
a0549832 3231
078e7b4a 3232 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
cfff016d 3233 if (SYMBOLP (*ptr))
078e7b4a
JB
3234 XSYMBOL (sym)->next = XSYMBOL (*ptr);
3235 else
3236 XSYMBOL (sym)->next = 0;
3237 *ptr = sym;
3238 return sym;
3239}
3240
3241DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
5de38842
PJ
3242 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3243NAME may be a string or a symbol. If it is a symbol, that exact
3244symbol is searched for.
3245A second optional argument specifies the obarray to use;
3246it defaults to the value of `obarray'. */)
3247 (name, obarray)
b55048d4 3248 Lisp_Object name, obarray;
078e7b4a 3249{
c2d47f4b 3250 register Lisp_Object tem, string;
078e7b4a 3251
265a9e55 3252 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
3253 obarray = check_obarray (obarray);
3254
b55048d4
GM
3255 if (!SYMBOLP (name))
3256 {
b7826503 3257 CHECK_STRING (name);
c2d47f4b 3258 string = name;
b55048d4
GM
3259 }
3260 else
c2d47f4b 3261 string = SYMBOL_NAME (name);
078e7b4a 3262
c2d47f4b 3263 tem = oblookup (obarray, SDATA (string), SCHARS (string), SBYTES (string));
b55048d4
GM
3264 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3265 return Qnil;
3266 else
078e7b4a 3267 return tem;
078e7b4a 3268}
d007f5c8
RS
3269\f
3270DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
5de38842
PJ
3271 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3272The value is t if a symbol was found and deleted, nil otherwise.
3273NAME may be a string or a symbol. If it is a symbol, that symbol
3274is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3275OBARRAY defaults to the value of the variable `obarray'. */)
3276 (name, obarray)
d007f5c8
RS
3277 Lisp_Object name, obarray;
3278{
3279 register Lisp_Object string, tem;
3280 int hash;
3281
3282 if (NILP (obarray)) obarray = Vobarray;
3283 obarray = check_obarray (obarray);
3284
3285 if (SYMBOLP (name))
d4c83cae 3286 string = SYMBOL_NAME (name);
d007f5c8
RS
3287 else
3288 {
b7826503 3289 CHECK_STRING (name);
d007f5c8
RS
3290 string = name;
3291 }
3292
d5db4077
KR
3293 tem = oblookup (obarray, SDATA (string),
3294 SCHARS (string),
3295 SBYTES (string));
d007f5c8
RS
3296 if (INTEGERP (tem))
3297 return Qnil;
3298 /* If arg was a symbol, don't delete anything but that symbol itself. */
3299 if (SYMBOLP (name) && !EQ (name, tem))
3300 return Qnil;
3301
44c6c019
GM
3302 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3303 XSYMBOL (tem)->constant = 0;
3304 XSYMBOL (tem)->indirect_variable = 0;
ca69c42f 3305
d007f5c8
RS
3306 hash = oblookup_last_bucket_number;
3307
3308 if (EQ (XVECTOR (obarray)->contents[hash], tem))
b2a30870
RS
3309 {
3310 if (XSYMBOL (tem)->next)
3311 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3312 else
3313 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3314 }
d007f5c8
RS
3315 else
3316 {
3317 Lisp_Object tail, following;
3318
3319 for (tail = XVECTOR (obarray)->contents[hash];
3320 XSYMBOL (tail)->next;
3321 tail = following)
3322 {
3323 XSETSYMBOL (following, XSYMBOL (tail)->next);
3324 if (EQ (following, tem))
3325 {
3326 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3327 break;
3328 }
3329 }
3330 }
3331
3332 return Qt;
3333}
3334\f
3335/* Return the symbol in OBARRAY whose names matches the string
e28552a4
RS
3336 of SIZE characters (SIZE_BYTE bytes) at PTR.
3337 If there is no such symbol in OBARRAY, return nil.
d007f5c8
RS
3338
3339 Also store the bucket number in oblookup_last_bucket_number. */
078e7b4a
JB
3340
3341Lisp_Object
e28552a4 3342oblookup (obarray, ptr, size, size_byte)
078e7b4a 3343 Lisp_Object obarray;
4b2dd274 3344 register const char *ptr;
e28552a4 3345 int size, size_byte;
078e7b4a 3346{
7a70b397
RS
3347 int hash;
3348 int obsize;
078e7b4a
JB
3349 register Lisp_Object tail;
3350 Lisp_Object bucket, tem;
3351
cfff016d 3352 if (!VECTORP (obarray)
7c79a684 3353 || (obsize = XVECTOR (obarray)->size) == 0)
078e7b4a
JB
3354 {
3355 obarray = check_obarray (obarray);
3356 obsize = XVECTOR (obarray)->size;
3357 }
519418b3
RS
3358 /* This is sometimes needed in the middle of GC. */
3359 obsize &= ~ARRAY_MARK_FLAG;
078e7b4a 3360 /* Combining next two lines breaks VMS C 2.3. */
e28552a4 3361 hash = hash_string (ptr, size_byte);
078e7b4a
JB
3362 hash %= obsize;
3363 bucket = XVECTOR (obarray)->contents[hash];
d007f5c8 3364 oblookup_last_bucket_number = hash;
8bc285a2 3365 if (EQ (bucket, make_number (0)))
078e7b4a 3366 ;
cfff016d 3367 else if (!SYMBOLP (bucket))
078e7b4a 3368 error ("Bad data in guts of obarray"); /* Like CADR error message */
d007f5c8
RS
3369 else
3370 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
078e7b4a 3371 {
d5db4077
KR
3372 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3373 && SCHARS (SYMBOL_NAME (tail)) == size
3374 && !bcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
078e7b4a
JB
3375 return tail;
3376 else if (XSYMBOL (tail)->next == 0)
3377 break;
3378 }
1805de4f 3379 XSETINT (tem, hash);
078e7b4a
JB
3380 return tem;
3381}
3382
3383static int
3384hash_string (ptr, len)
4b2dd274 3385 const unsigned char *ptr;
078e7b4a
JB
3386 int len;
3387{
4b2dd274
KR
3388 register const unsigned char *p = ptr;
3389 register const unsigned char *end = p + len;
078e7b4a
JB
3390 register unsigned char c;
3391 register int hash = 0;
3392
3393 while (p != end)
3394 {
3395 c = *p++;
3396 if (c >= 0140) c -= 40;
3397 hash = ((hash<<3) + (hash>>28) + c);
3398 }
3399 return hash & 07777777777;
3400}
d007f5c8 3401\f
078e7b4a
JB
3402void
3403map_obarray (obarray, fn, arg)
3404 Lisp_Object obarray;
d5b28a9d 3405 void (*fn) P_ ((Lisp_Object, Lisp_Object));
078e7b4a
JB
3406 Lisp_Object arg;
3407{
3408 register int i;
3409 register Lisp_Object tail;
b7826503 3410 CHECK_VECTOR (obarray);
078e7b4a
JB
3411 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3412 {
3413 tail = XVECTOR (obarray)->contents[i];
4f5c4403 3414 if (SYMBOLP (tail))
078e7b4a
JB
3415 while (1)
3416 {
3417 (*fn) (tail, arg);
3418 if (XSYMBOL (tail)->next == 0)
3419 break;
1805de4f 3420 XSETSYMBOL (tail, XSYMBOL (tail)->next);
078e7b4a
JB
3421 }
3422 }
3423}
3424
d5b28a9d 3425void
078e7b4a
JB
3426mapatoms_1 (sym, function)
3427 Lisp_Object sym, function;
3428{
3429 call1 (function, sym);
3430}
3431
3432DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
5de38842
PJ
3433 doc: /* Call FUNCTION on every symbol in OBARRAY.
3434OBARRAY defaults to the value of `obarray'. */)
3435 (function, obarray)
078e7b4a
JB
3436 Lisp_Object function, obarray;
3437{
265a9e55 3438 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
3439 obarray = check_obarray (obarray);
3440
3441 map_obarray (obarray, mapatoms_1, function);
3442 return Qnil;
3443}
3444
5e88a39e 3445#define OBARRAY_SIZE 1511
078e7b4a
JB
3446
3447void
3448init_obarray ()
3449{
3450 Lisp_Object oblength;
3451 int hash;
3452 Lisp_Object *tem;
3453
baf69866 3454 XSETFASTINT (oblength, OBARRAY_SIZE);
078e7b4a 3455
491f16a2 3456 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
078e7b4a
JB
3457 Vobarray = Fmake_vector (oblength, make_number (0));
3458 initial_obarray = Vobarray;
3459 staticpro (&initial_obarray);
3460 /* Intern nil in the obarray */
44c6c019
GM
3461 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3462 XSYMBOL (Qnil)->constant = 1;
177c0ea7 3463
078e7b4a
JB
3464 /* These locals are to kludge around a pyramid compiler bug. */
3465 hash = hash_string ("nil", 3);
3466 /* Separate statement here to avoid VAXC bug. */
3467 hash %= OBARRAY_SIZE;
3468 tem = &XVECTOR (Vobarray)->contents[hash];
3469 *tem = Qnil;
3470
491f16a2 3471 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
078e7b4a
JB
3472 XSYMBOL (Qnil)->function = Qunbound;
3473 XSYMBOL (Qunbound)->value = Qunbound;
3474 XSYMBOL (Qunbound)->function = Qunbound;
3475
3476 Qt = intern ("t");
3477 XSYMBOL (Qnil)->value = Qnil;
3478 XSYMBOL (Qnil)->plist = Qnil;
3479 XSYMBOL (Qt)->value = Qt;
44c6c019 3480 XSYMBOL (Qt)->constant = 1;
078e7b4a
JB
3481
3482 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3483 Vpurify_flag = Qt;
3484
3485 Qvariable_documentation = intern ("variable-documentation");
0f73bb1c 3486 staticpro (&Qvariable_documentation);
078e7b4a 3487
449fea39 3488 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3cb65b0e 3489 read_buffer = (char *) xmalloc (read_buffer_size);
078e7b4a
JB
3490}
3491\f
3492void
3493defsubr (sname)
3494 struct Lisp_Subr *sname;
3495{
3496 Lisp_Object sym;
3497 sym = intern (sname->symbol_name);
1805de4f 3498 XSETSUBR (XSYMBOL (sym)->function, sname);
078e7b4a
JB
3499}
3500
3501#ifdef NOTDEF /* use fset in subr.el now */
3502void
3503defalias (sname, string)
3504 struct Lisp_Subr *sname;
3505 char *string;
3506{
3507 Lisp_Object sym;
3508 sym = intern (string);
1805de4f 3509 XSETSUBR (XSYMBOL (sym)->function, sname);
078e7b4a
JB
3510}
3511#endif /* NOTDEF */
3512
078e7b4a 3513/* Define an "integer variable"; a symbol whose value is forwarded
1a0f90f7 3514 to a C variable of type int. Sample call: */
5de38842 3515 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
078e7b4a 3516void
e9e00ff2 3517defvar_int (namestring, address)
078e7b4a 3518 char *namestring;
31ade731 3519 EMACS_INT *address;
078e7b4a 3520{
1a0f90f7 3521 Lisp_Object sym, val;
078e7b4a 3522 sym = intern (namestring);
1a0f90f7 3523 val = allocate_misc ();
47e28b2c 3524 XMISCTYPE (val) = Lisp_Misc_Intfwd;
fc1e7df5 3525 XINTFWD (val)->intvar = address;
44c6c019 3526 SET_SYMBOL_VALUE (sym, val);
078e7b4a
JB
3527}
3528
f0529b5b
PJ
3529/* Similar but define a variable whose value is t if address contains 1,
3530 nil if address contains 0 */
078e7b4a 3531void
e9e00ff2 3532defvar_bool (namestring, address)
078e7b4a
JB
3533 char *namestring;
3534 int *address;
078e7b4a 3535{
1a0f90f7 3536 Lisp_Object sym, val;
078e7b4a 3537 sym = intern (namestring);
1a0f90f7 3538 val = allocate_misc ();
47e28b2c 3539 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
fc1e7df5 3540 XBOOLFWD (val)->boolvar = address;
44c6c019 3541 SET_SYMBOL_VALUE (sym, val);
1ffcc3b1 3542 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
078e7b4a
JB
3543}
3544
1a0f90f7
KH
3545/* Similar but define a variable whose value is the Lisp Object stored
3546 at address. Two versions: with and without gc-marking of the C
3547 variable. The nopro version is used when that variable will be
3548 gc-marked for some other reason, since marking the same slot twice
3549 can cause trouble with strings. */
078e7b4a 3550void
1a0f90f7 3551defvar_lisp_nopro (namestring, address)
078e7b4a
JB
3552 char *namestring;
3553 Lisp_Object *address;
078e7b4a 3554{
1a0f90f7 3555 Lisp_Object sym, val;
078e7b4a 3556 sym = intern (namestring);
1a0f90f7 3557 val = allocate_misc ();
47e28b2c 3558 XMISCTYPE (val) = Lisp_Misc_Objfwd;
fc1e7df5 3559 XOBJFWD (val)->objvar = address;
44c6c019 3560 SET_SYMBOL_VALUE (sym, val);
078e7b4a
JB
3561}
3562
078e7b4a 3563void
1a0f90f7 3564defvar_lisp (namestring, address)
078e7b4a
JB
3565 char *namestring;
3566 Lisp_Object *address;
078e7b4a 3567{
1a0f90f7
KH
3568 defvar_lisp_nopro (namestring, address);
3569 staticpro (address);
078e7b4a
JB
3570}
3571
078e7b4a 3572/* Similar but define a variable whose value is the Lisp Object stored in
2836d9a4
KH
3573 the current buffer. address is the address of the slot in the buffer
3574 that is current now. */
078e7b4a
JB
3575
3576void
4360b0c6 3577defvar_per_buffer (namestring, address, type, doc)
078e7b4a
JB
3578 char *namestring;
3579 Lisp_Object *address;
4360b0c6 3580 Lisp_Object type;
078e7b4a
JB
3581 char *doc;
3582{
1a0f90f7 3583 Lisp_Object sym, val;
078e7b4a 3584 int offset;
078e7b4a
JB
3585
3586 sym = intern (namestring);
1a0f90f7 3587 val = allocate_misc ();
078e7b4a
JB
3588 offset = (char *)address - (char *)current_buffer;
3589
47e28b2c 3590 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
fc1e7df5 3591 XBUFFER_OBJFWD (val)->offset = offset;
44c6c019 3592 SET_SYMBOL_VALUE (sym, val);
11fd416e
GM
3593 PER_BUFFER_SYMBOL (offset) = sym;
3594 PER_BUFFER_TYPE (offset) = type;
177c0ea7 3595
11fd416e 3596 if (PER_BUFFER_IDX (offset) == 0)
078e7b4a
JB
3597 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3598 slot of buffer_local_flags */
3599 abort ();
3600}
3601
950c215d
KH
3602
3603/* Similar but define a variable whose value is the Lisp Object stored
4ac38690 3604 at a particular offset in the current kboard object. */
950c215d
KH
3605
3606void
4ac38690 3607defvar_kboard (namestring, offset)
950c215d
KH
3608 char *namestring;
3609 int offset;
3610{
3611 Lisp_Object sym, val;
3612 sym = intern (namestring);
3613 val = allocate_misc ();
47e28b2c 3614 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
4ac38690 3615 XKBOARD_OBJFWD (val)->offset = offset;
44c6c019 3616 SET_SYMBOL_VALUE (sym, val);
950c215d 3617}
078e7b4a 3618\f
11938f10
KH
3619/* Record the value of load-path used at the start of dumping
3620 so we can see if the site changed it later during dumping. */
3621static Lisp_Object dump_path;
3622
d5b28a9d 3623void
279499f0 3624init_lread ()
078e7b4a 3625{
46947372 3626 char *normal;
e73997a1 3627 int turn_off_warning = 0;
078e7b4a 3628
279499f0 3629 /* Compute the default load-path. */
46947372
JB
3630#ifdef CANNOT_DUMP
3631 normal = PATH_LOADSEARCH;
e065a56e 3632 Vload_path = decode_env_path (0, normal);
46947372
JB
3633#else
3634 if (NILP (Vpurify_flag))
3635 normal = PATH_LOADSEARCH;
279499f0 3636 else
46947372 3637 normal = PATH_DUMPLOADSEARCH;
279499f0 3638
46947372
JB
3639 /* In a dumped Emacs, we normally have to reset the value of
3640 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3641 uses ../lisp, instead of the path of the installed elisp
3642 libraries. However, if it appears that Vload_path was changed
3643 from the default before dumping, don't override that value. */
4746118a
JB
3644 if (initialized)
3645 {
4746118a 3646 if (! NILP (Fequal (dump_path, Vload_path)))
80667d53
RS
3647 {
3648 Vload_path = decode_env_path (0, normal);
74180aa4 3649 if (!NILP (Vinstallation_directory))
80667d53 3650 {
3ddff138
RS
3651 Lisp_Object tem, tem1, sitelisp;
3652
3653 /* Remove site-lisp dirs from path temporarily and store
3654 them in sitelisp, then conc them on at the end so
3655 they're always first in path. */
3656 sitelisp = Qnil;
3657 while (1)
3658 {
3659 tem = Fcar (Vload_path);
3660 tem1 = Fstring_match (build_string ("site-lisp"),
3661 tem, Qnil);
3662 if (!NILP (tem1))
3663 {
3664 Vload_path = Fcdr (Vload_path);
3665 sitelisp = Fcons (tem, sitelisp);
3666 }
3667 else
3668 break;
3669 }
3670
74180aa4 3671 /* Add to the path the lisp subdir of the
3a3056e5 3672 installation dir, if it exists. */
74180aa4
RS
3673 tem = Fexpand_file_name (build_string ("lisp"),
3674 Vinstallation_directory);
3a3056e5
RS
3675 tem1 = Ffile_exists_p (tem);
3676 if (!NILP (tem1))
3677 {
3678 if (NILP (Fmember (tem, Vload_path)))
e73997a1
RS
3679 {
3680 turn_off_warning = 1;
3ddff138 3681 Vload_path = Fcons (tem, Vload_path);
e73997a1 3682 }
3a3056e5
RS
3683 }
3684 else
3685 /* That dir doesn't exist, so add the build-time
3686 Lisp dirs instead. */
3687 Vload_path = nconc2 (Vload_path, dump_path);
c478f98c 3688
9fbc0116
RS
3689 /* Add leim under the installation dir, if it exists. */
3690 tem = Fexpand_file_name (build_string ("leim"),
3691 Vinstallation_directory);
3692 tem1 = Ffile_exists_p (tem);
3693 if (!NILP (tem1))
3694 {
3695 if (NILP (Fmember (tem, Vload_path)))
3ddff138 3696 Vload_path = Fcons (tem, Vload_path);
9fbc0116
RS
3697 }
3698
c478f98c
RS
3699 /* Add site-list under the installation dir, if it exists. */
3700 tem = Fexpand_file_name (build_string ("site-lisp"),
3701 Vinstallation_directory);
3702 tem1 = Ffile_exists_p (tem);
3703 if (!NILP (tem1))
3704 {
3705 if (NILP (Fmember (tem, Vload_path)))
3ddff138 3706 Vload_path = Fcons (tem, Vload_path);
c478f98c 3707 }
0f337465
RS
3708
3709 /* If Emacs was not built in the source directory,
9fbc0116
RS
3710 and it is run from where it was built, add to load-path
3711 the lisp, leim and site-lisp dirs under that directory. */
0f337465
RS
3712
3713 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3714 {
33046fc9
RS
3715 Lisp_Object tem2;
3716
0f337465
RS
3717 tem = Fexpand_file_name (build_string ("src/Makefile"),
3718 Vinstallation_directory);
3719 tem1 = Ffile_exists_p (tem);
33046fc9
RS
3720
3721 /* Don't be fooled if they moved the entire source tree
3722 AFTER dumping Emacs. If the build directory is indeed
3723 different from the source dir, src/Makefile.in and
3724 src/Makefile will not be found together. */
3725 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3726 Vinstallation_directory);
3727 tem2 = Ffile_exists_p (tem);
3728 if (!NILP (tem1) && NILP (tem2))
0f337465
RS
3729 {
3730 tem = Fexpand_file_name (build_string ("lisp"),
3731 Vsource_directory);
3732
3733 if (NILP (Fmember (tem, Vload_path)))
3ddff138 3734 Vload_path = Fcons (tem, Vload_path);
0f337465 3735
9fbc0116
RS
3736 tem = Fexpand_file_name (build_string ("leim"),
3737 Vsource_directory);
3738
3739 if (NILP (Fmember (tem, Vload_path)))
3ddff138 3740 Vload_path = Fcons (tem, Vload_path);
9fbc0116 3741
0f337465
RS
3742 tem = Fexpand_file_name (build_string ("site-lisp"),
3743 Vsource_directory);
3744
3745 if (NILP (Fmember (tem, Vload_path)))
3ddff138 3746 Vload_path = Fcons (tem, Vload_path);
0f337465
RS
3747 }
3748 }
3ddff138
RS
3749 if (!NILP (sitelisp))
3750 Vload_path = nconc2 (Fnreverse (sitelisp), Vload_path);
80667d53
RS
3751 }
3752 }
4746118a
JB
3753 }
3754 else
11938f10 3755 {
7b396c6c
RS
3756 /* NORMAL refers to the lisp dir in the source directory. */
3757 /* We used to add ../lisp at the front here, but
3758 that caused trouble because it was copied from dump_path
3759 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3760 It should be unnecessary. */
3761 Vload_path = decode_env_path (0, normal);
11938f10
KH
3762 dump_path = Vload_path;
3763 }
46947372 3764#endif
279499f0 3765
536d6baa 3766#if (!(defined(WINDOWSNT) || (defined(HAVE_CARBON))))
177c0ea7
JB
3767 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3768 almost never correct, thereby causing a warning to be printed out that
8e6208c5 3769 confuses users. Since PATH_LOADSEARCH is always overridden by the
95af0924 3770 EMACSLOADPATH environment variable below, disable the warning on NT.
f3d5f92d
ST
3771 Also, when using the "self-contained" option for Carbon Emacs for MacOSX,
3772 the "standard" paths may not exist and would be overridden by
3773 EMACSLOADPATH as on NT. Since this depends on how the executable
3774 was build and packaged, turn off the warnings in general */
317073d5 3775
078e7b4a 3776 /* Warn if dirs in the *standard* path don't exist. */
e73997a1
RS
3777 if (!turn_off_warning)
3778 {
3779 Lisp_Object path_tail;
078e7b4a 3780
e73997a1
RS
3781 for (path_tail = Vload_path;
3782 !NILP (path_tail);
c1d497be 3783 path_tail = XCDR (path_tail))
e73997a1
RS
3784 {
3785 Lisp_Object dirfile;
3786 dirfile = Fcar (path_tail);
3787 if (STRINGP (dirfile))
3788 {
3789 dirfile = Fdirectory_file_name (dirfile);
d5db4077 3790 if (access (SDATA (dirfile), 0) < 0)
85496b8c 3791 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
c1d497be 3792 XCAR (path_tail));
e73997a1
RS
3793 }
3794 }
3795 }
536d6baa 3796#endif /* !(WINDOWSNT || HAVE_CARBON) */
46947372
JB
3797
3798 /* If the EMACSLOADPATH environment variable is set, use its value.
3799 This doesn't apply if we're dumping. */
ffd9c2a1 3800#ifndef CANNOT_DUMP
46947372
JB
3801 if (NILP (Vpurify_flag)
3802 && egetenv ("EMACSLOADPATH"))
ffd9c2a1 3803#endif
279499f0 3804 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
279499f0
JB
3805
3806 Vvalues = Qnil;
3807
078e7b4a 3808 load_in_progress = 0;
4e53f562 3809 Vload_file_name = Qnil;
d2c6be7f
RS
3810
3811 load_descriptor_list = Qnil;
8f6b0411
RS
3812
3813 Vstandard_input = Qt;
f74b0705 3814 Vloads_in_progress = Qnil;
078e7b4a
JB
3815}
3816
85496b8c
RS
3817/* Print a warning, using format string FORMAT, that directory DIRNAME
3818 does not exist. Print it on stderr and put it in *Message*. */
3819
d5b28a9d 3820void
85496b8c
RS
3821dir_warning (format, dirname)
3822 char *format;
3823 Lisp_Object dirname;
3824{
3825 char *buffer
d5db4077 3826 = (char *) alloca (SCHARS (dirname) + strlen (format) + 5);
85496b8c 3827
d5db4077
KR
3828 fprintf (stderr, format, SDATA (dirname));
3829 sprintf (buffer, format, SDATA (dirname));
9b69357e
GV
3830 /* Don't log the warning before we've initialized!! */
3831 if (initialized)
3832 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
85496b8c
RS
3833}
3834
078e7b4a 3835void
279499f0 3836syms_of_lread ()
078e7b4a
JB
3837{
3838 defsubr (&Sread);
3839 defsubr (&Sread_from_string);
3840 defsubr (&Sintern);
3841 defsubr (&Sintern_soft);
d007f5c8 3842 defsubr (&Sunintern);
078e7b4a 3843 defsubr (&Sload);
228d4b1c 3844 defsubr (&Seval_buffer);
078e7b4a
JB
3845 defsubr (&Seval_region);
3846 defsubr (&Sread_char);
3847 defsubr (&Sread_char_exclusive);
078e7b4a 3848 defsubr (&Sread_event);
078e7b4a
JB
3849 defsubr (&Sget_file_char);
3850 defsubr (&Smapatoms);
86d00812 3851 defsubr (&Slocate_file_internal);
078e7b4a
JB
3852
3853 DEFVAR_LISP ("obarray", &Vobarray,
5de38842
PJ
3854 doc: /* Symbol table for use by `intern' and `read'.
3855It is a vector whose length ought to be prime for best results.
3856The vector's contents don't make sense if examined from Lisp programs;
3857to find all the symbols in an obarray, use `mapatoms'. */);
078e7b4a
JB
3858
3859 DEFVAR_LISP ("values", &Vvalues,
5de38842
PJ
3860 doc: /* List of values of all expressions which were read, evaluated and printed.
3861Order is reverse chronological. */);
078e7b4a
JB
3862
3863 DEFVAR_LISP ("standard-input", &Vstandard_input,
5de38842
PJ
3864 doc: /* Stream for read to get input from.
3865See documentation of `read' for possible values. */);
078e7b4a
JB
3866 Vstandard_input = Qt;
3867
abb13b09
CW
3868 DEFVAR_LISP ("read-with-symbol-positions", &Vread_with_symbol_positions,
3869 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
3870
3871If this variable is a buffer, then only forms read from that buffer
3872will be added to `read-symbol-positions-list'.
3873If this variable is t, then all read forms will be added.
3874The effect of all other values other than nil are not currently
3875defined, although they may be in the future.
3876
3877The positions are relative to the last call to `read' or
3878`read-from-string'. It is probably a bad idea to set this variable at
3879the toplevel; bind it instead. */);
3880 Vread_with_symbol_positions = Qnil;
3881
3882 DEFVAR_LISP ("read-symbol-positions-list", &Vread_symbol_positions_list,
d6567030 3883 doc: /* A list mapping read symbols to their positions.
abb13b09
CW
3884This variable is modified during calls to `read' or
3885`read-from-string', but only when `read-with-symbol-positions' is
3886non-nil.
3887
3888Each element of the list looks like (SYMBOL . CHAR-POSITION), where
d6567030 3889CHAR-POSITION is an integer giving the offset of that occurrence of the
abb13b09
CW
3890symbol from the position where `read' or `read-from-string' started.
3891
3892Note that a symbol will appear multiple times in this list, if it was
3893read multiple times. The list is in the same order as the symbols
3894were read in. */);
177c0ea7 3895 Vread_symbol_positions_list = Qnil;
abb13b09 3896
078e7b4a 3897 DEFVAR_LISP ("load-path", &Vload_path,
5de38842
PJ
3898 doc: /* *List of directories to search for files to load.
3899Each element is a string (directory name) or nil (try default directory).
3900Initialized based on EMACSLOADPATH environment variable, if any,
3901otherwise to default specified by file `epaths.h' when Emacs was built. */);
078e7b4a 3902
e61b9b87 3903 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
5de38842
PJ
3904 doc: /* *List of suffixes to try for files to load.
3905This list should not include the empty string. */);
e61b9b87
SM
3906 Vload_suffixes = Fcons (build_string (".elc"),
3907 Fcons (build_string (".el"), Qnil));
ddb716ef
SM
3908 /* We don't use empty_string because it's not initialized yet. */
3909 default_suffixes = Fcons (build_string (""), Qnil);
e61b9b87
SM
3910 staticpro (&default_suffixes);
3911
078e7b4a 3912 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
5de38842 3913 doc: /* Non-nil iff inside of `load'. */);
078e7b4a
JB
3914
3915 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
5de38842
PJ
3916 doc: /* An alist of expressions to be evalled when particular files are loaded.
3917Each element looks like (FILENAME FORMS...).
3918When `load' is run and the file-name argument is FILENAME,
3919the FORMS in the corresponding element are executed at the end of loading.
3920
3921FILENAME must match exactly! Normally FILENAME is the name of a library,
3922with no directory specified, since that is how `load' is normally called.
3923An error in FORMS does not undo the load,
3924but does prevent execution of the rest of the FORMS.
3925FILENAME can also be a symbol (a feature) and FORMS are then executed
3926when the corresponding call to `provide' is made. */);
078e7b4a
JB
3927 Vafter_load_alist = Qnil;
3928
ae321d28 3929 DEFVAR_LISP ("load-history", &Vload_history,
0fc213e9 3930 doc: /* Alist mapping file names to symbols and features.
5de38842
PJ
3931Each alist element is a list that starts with a file name,
3932except for one element (optional) that starts with nil and describes
3933definitions evaluated from buffers not visiting files.
1bc2cdb1 3934The remaining elements of each list are symbols defined as variables
d642e4f9 3935and cons cells of the form `(provide . FEATURE)', `(require . FEATURE)',
1bc2cdb1
RS
3936`(defun . FUNCTION)', `(autoload . SYMBOL)', and `(t . SYMBOL)'.
3937An element `(t . SYMBOL)' precedes an entry `(defun . FUNCTION)',
6042a06e 3938and means that SYMBOL was an autoload before this file redefined it
0fc213e9
RS
3939as a function.
3940
b502a9a1
RS
3941During preloading, the file name recorded is relative to the main Lisp
3942directory. These file names are converted to absolute at startup. */);
ae321d28
RS
3943 Vload_history = Qnil;
3944
20ea2964 3945 DEFVAR_LISP ("load-file-name", &Vload_file_name,
5de38842 3946 doc: /* Full name of file being loaded by `load'. */);
20ea2964
RS
3947 Vload_file_name = Qnil;
3948
4116ab9f 3949 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
5de38842 3950 doc: /* File name, including directory, of user's initialization file.
0a25a201
RS
3951If the file loaded had extension `.elc', and the corresponding source file
3952exists, this variable contains the name of source file, suitable for use
5de38842 3953by functions like `custom-save-all' which edit the init file. */);
4116ab9f
KH
3954 Vuser_init_file = Qnil;
3955
8a1f1537 3956 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
5de38842 3957 doc: /* Used for internal purposes by `load'. */);
ae321d28
RS
3958 Vcurrent_load_list = Qnil;
3959
84a15045 3960 DEFVAR_LISP ("load-read-function", &Vload_read_function,
5de38842
PJ
3961 doc: /* Function used by `load' and `eval-region' for reading expressions.
3962The default is nil, which means use the function `read'. */);
84a15045
RS
3963 Vload_read_function = Qnil;
3964
fe0e03f3 3965 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
5de38842
PJ
3966 doc: /* Function called in `load' for loading an Emacs lisp source file.
3967This function is for doing code conversion before reading the source file.
3968If nil, loading is done without any code conversion.
3969Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
3970 FULLNAME is the full name of FILE.
3971See `load' for the meaning of the remaining arguments. */);
fe0e03f3
KH
3972 Vload_source_file_function = Qnil;
3973
b2a30870 3974 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
5de38842
PJ
3975 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
3976This is useful when the file being loaded is a temporary copy. */);
b2a30870
RS
3977 load_force_doc_strings = 0;
3978
94e554db 3979 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
5de38842
PJ
3980 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
3981This is normally bound by `load' and `eval-buffer' to control `read',
3982and is not meant for users to change. */);
94e554db
RS
3983 load_convert_to_unibyte = 0;
3984
1521a8fa 3985 DEFVAR_LISP ("source-directory", &Vsource_directory,
5de38842
PJ
3986 doc: /* Directory in which Emacs sources were found when Emacs was built.
3987You cannot count on them to still be there! */);
a90ba1e2
KH
3988 Vsource_directory
3989 = Fexpand_file_name (build_string ("../"),
3990 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3991
4b104c41 3992 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
5de38842 3993 doc: /* List of files that were preloaded (when dumping Emacs). */);
4b104c41
RS
3994 Vpreloaded_file_list = Qnil;
3995
1ffcc3b1 3996 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
5de38842 3997 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
1ffcc3b1
DL
3998 Vbyte_boolean_vars = Qnil;
3999
da84f340 4000 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
5de38842
PJ
4001 doc: /* Non-nil means load dangerous compiled Lisp files.
4002Some versions of XEmacs use different byte codes than Emacs. These
4003incompatible byte codes can make Emacs crash when it tries to execute
4004them. */);
da84f340
GM
4005 load_dangerous_libraries = 0;
4006
bb970e67 4007 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
5de38842
PJ
4008 doc: /* Regular expression matching safe to load compiled Lisp files.
4009When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4010from the file, and matches them against this regular expression.
4011When the regular expression matches, the file is considered to be safe
4012to load. See also `load-dangerous-libraries'. */);
bb970e67 4013 Vbytecomp_version_regexp
f2e7d5eb 4014 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
da84f340 4015
3f39f996
RS
4016 DEFVAR_LISP ("eval-buffer-list", &Veval_buffer_list,
4017 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4018 Veval_buffer_list = Qnil;
4019
a90ba1e2
KH
4020 /* Vsource_directory was initialized in init_lread. */
4021
d2c6be7f
RS
4022 load_descriptor_list = Qnil;
4023 staticpro (&load_descriptor_list);
4024
8a1f1537
RS
4025 Qcurrent_load_list = intern ("current-load-list");
4026 staticpro (&Qcurrent_load_list);
4027
078e7b4a
JB
4028 Qstandard_input = intern ("standard-input");
4029 staticpro (&Qstandard_input);
4030
4031 Qread_char = intern ("read-char");
4032 staticpro (&Qread_char);
4033
4034 Qget_file_char = intern ("get-file-char");
4035 staticpro (&Qget_file_char);
7bd279cd 4036
17634846
RS
4037 Qbackquote = intern ("`");
4038 staticpro (&Qbackquote);
4039 Qcomma = intern (",");
4040 staticpro (&Qcomma);
4041 Qcomma_at = intern (",@");
4042 staticpro (&Qcomma_at);
4043 Qcomma_dot = intern (",.");
4044 staticpro (&Qcomma_dot);
4045
74549846
RS
4046 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
4047 staticpro (&Qinhibit_file_name_operation);
4048
7bd279cd
RS
4049 Qascii_character = intern ("ascii-character");
4050 staticpro (&Qascii_character);
c2225d00 4051
2b6cae0c
RS
4052 Qfunction = intern ("function");
4053 staticpro (&Qfunction);
4054
c2225d00
RS
4055 Qload = intern ("load");
4056 staticpro (&Qload);
20ea2964
RS
4057
4058 Qload_file_name = intern ("load-file-name");
4059 staticpro (&Qload_file_name);
11938f10 4060
3f39f996
RS
4061 Qeval_buffer_list = intern ("eval-buffer-list");
4062 staticpro (&Qeval_buffer_list);
4063
11938f10 4064 staticpro (&dump_path);
4ad679f9
EN
4065
4066 staticpro (&read_objects);
4067 read_objects = Qnil;
9e062b6c 4068 staticpro (&seen_list);
f153db13 4069 seen_list = Qnil;
177c0ea7 4070
7ee3bd7b
GM
4071 Vloads_in_progress = Qnil;
4072 staticpro (&Vloads_in_progress);
078e7b4a 4073}
ab5796a9
MB
4074
4075/* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d
4076 (do not change this comment) */