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