Add 2008 to copyright years.
[bpt/emacs.git] / src / print.c
CommitLineData
38010d50 1/* Lisp object printing and output streams.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
8cabe764 4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
38010d50
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
684d6f5b 10the Free Software Foundation; either version 3, or (at your option)
38010d50
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. */
38010d50
JB
22
23
18160b98 24#include <config.h>
38010d50 25#include <stdio.h>
38010d50 26#include "lisp.h"
38010d50 27#include "buffer.h"
087e3c46 28#include "charset.h"
2538fae4 29#include "keyboard.h"
0137dbf7 30#include "frame.h"
38010d50
JB
31#include "window.h"
32#include "process.h"
33#include "dispextern.h"
34#include "termchar.h"
7651e1f5 35#include "intervals.h"
f9467be3 36#include "blockinput.h"
7651e1f5 37
38010d50
JB
38Lisp_Object Vstandard_output, Qstandard_output;
39
d9c21094
RS
40Lisp_Object Qtemp_buffer_setup_hook;
41
2f100b5c
EN
42/* These are used to print like we read. */
43extern Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
44
38010d50 45Lisp_Object Vfloat_output_format, Qfloat_output_format;
f356c3fb
PE
46
47/* Work around a problem that happens because math.h on hpux 7
48 defines two static variables--which, in Emacs, are not really static,
49 because `static' is defined as nothing. The problem is that they are
50 defined both here and in lread.c.
51 These macros prevent the name conflict. */
52#if defined (HPUX) && !defined (HPUX8)
53#define _MAXLDBL print_maxldbl
54#define _NMAXLDBL print_nmaxldbl
55#endif
56
57#include <math.h>
58
59#if STDC_HEADERS
60#include <float.h>
f356c3fb
PE
61#endif
62
63/* Default to values appropriate for IEEE floating point. */
64#ifndef FLT_RADIX
65#define FLT_RADIX 2
66#endif
67#ifndef DBL_MANT_DIG
68#define DBL_MANT_DIG 53
69#endif
70#ifndef DBL_DIG
71#define DBL_DIG 15
72#endif
b0a1044b
PE
73#ifndef DBL_MIN
74#define DBL_MIN 2.2250738585072014e-308
75#endif
76
77#ifdef DBL_MIN_REPLACEMENT
78#undef DBL_MIN
79#define DBL_MIN DBL_MIN_REPLACEMENT
80#endif
f356c3fb
PE
81
82/* Define DOUBLE_DIGITS_BOUND, an upper bound on the number of decimal digits
83 needed to express a float without losing information.
84 The general-case formula is valid for the usual case, IEEE floating point,
85 but many compilers can't optimize the formula to an integer constant,
86 so make a special case for it. */
87#if FLT_RADIX == 2 && DBL_MANT_DIG == 53
88#define DOUBLE_DIGITS_BOUND 17 /* IEEE floating point */
89#else
90#define DOUBLE_DIGITS_BOUND ((int) ceil (log10 (pow (FLT_RADIX, DBL_MANT_DIG))))
91#endif
92
38010d50
JB
93/* Avoid actual stack overflow in print. */
94int print_depth;
95
a22dec27
SM
96/* Level of nesting inside outputting backquote in new style. */
97int new_backquote_output;
0330bb60 98
ec838c39
RS
99/* Detect most circularities to print finite output. */
100#define PRINT_CIRCLE 200
101Lisp_Object being_printed[PRINT_CIRCLE];
102
6fec5601
RS
103/* When printing into a buffer, first we put the text in this
104 block, then insert it all at once. */
105char *print_buffer;
106
107/* Size allocated in print_buffer. */
108int print_buffer_size;
dc2a0b79 109/* Chars stored in print_buffer. */
6fec5601 110int print_buffer_pos;
dc2a0b79
RS
111/* Bytes stored in print_buffer. */
112int print_buffer_pos_byte;
6fec5601 113
38010d50
JB
114/* Maximum length of list to print in full; noninteger means
115 effectively infinity */
116
117Lisp_Object Vprint_length;
118
119/* Maximum depth of list to print in full; noninteger means
120 effectively infinity. */
121
122Lisp_Object Vprint_level;
123
124/* Nonzero means print newlines in strings as \n. */
125
126int print_escape_newlines;
127
38940e93
RS
128/* Nonzero means to print single-byte non-ascii characters in strings as
129 octal escapes. */
130
131int print_escape_nonascii;
132
835d0be6
RS
133/* Nonzero means to print multibyte characters in strings as hex escapes. */
134
135int print_escape_multibyte;
136
137Lisp_Object Qprint_escape_newlines;
138Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
139
2f100b5c
EN
140/* Nonzero means print (quote foo) forms as 'foo, etc. */
141
142int print_quoted;
143
0f25ecc6 144/* Non-nil means print #: before uninterned symbols. */
081e0581 145
e0f69431 146Lisp_Object Vprint_gensym;
081e0581 147
0f25ecc6 148/* Non-nil means print recursive structures using #n= and #n# syntax. */
081e0581 149
0f25ecc6
RS
150Lisp_Object Vprint_circle;
151
152/* Non-nil means keep continuous number for #n= and #n# syntax
153 between several print functions. */
154
155Lisp_Object Vprint_continuous_numbering;
156
157/* Vprint_number_table is a vector like [OBJ1 STAT1 OBJ2 STAT2 ...],
158 where OBJn are objects going to be printed, and STATn are their status,
159 which may be different meanings during process. See the comments of
160 the functions print and print_preprocess for details.
161 print_number_index keeps the last position the next object should be added,
162 twice of which is the actual vector position in Vprint_number_table. */
163int print_number_index;
164Lisp_Object Vprint_number_table;
165
166/* PRINT_NUMBER_OBJECT returns the I'th object in Vprint_number_table TABLE.
167 PRINT_NUMBER_STATUS returns the status of the I'th object in TABLE.
168 See the comment of the variable Vprint_number_table. */
169#define PRINT_NUMBER_OBJECT(table,i) XVECTOR ((table))->contents[(i) * 2]
170#define PRINT_NUMBER_STATUS(table,i) XVECTOR ((table))->contents[(i) * 2 + 1]
2f100b5c 171
5259c737 172/* Nonzero means print newline to stdout before next minibuffer message.
38010d50
JB
173 Defined in xdisp.c */
174
175extern int noninteractive_need_newline;
5259c737 176
aec2b95b
RS
177extern int minibuffer_auto_raise;
178
38010d50
JB
179#ifdef MAX_PRINT_CHARS
180static int print_chars;
181static int max_print;
182#endif /* MAX_PRINT_CHARS */
7651e1f5
RS
183
184void print_interval ();
38010d50 185
945b0111
EZ
186/* GDB resets this to zero on W32 to disable OutputDebugString calls. */
187int print_output_debug_flag = 1;
188
38010d50 189\f
eb8c3be9 190/* Low level output routines for characters and strings */
38010d50
JB
191
192/* Lisp functions to do output using a stream
081e0581
EN
193 must have the stream in a variable called printcharfun
194 and must start with PRINTPREPARE, end with PRINTFINISH,
195 and use PRINTDECLARE to declare common variables.
196 Use PRINTCHAR to output one character,
177c0ea7 197 or call strout to output a block of characters. */
0788646c
GM
198
199#define PRINTDECLARE \
200 struct buffer *old = current_buffer; \
ee5263af
GM
201 int old_point = -1, start_point = -1; \
202 int old_point_byte = -1, start_point_byte = -1; \
aed13378 203 int specpdl_count = SPECPDL_INDEX (); \
0788646c
GM
204 int free_print_buffer = 0; \
205 int multibyte = !NILP (current_buffer->enable_multibyte_characters); \
081e0581
EN
206 Lisp_Object original
207
0788646c
GM
208#define PRINTPREPARE \
209 original = printcharfun; \
210 if (NILP (printcharfun)) printcharfun = Qt; \
211 if (BUFFERP (printcharfun)) \
212 { \
213 if (XBUFFER (printcharfun) != current_buffer) \
214 Fset_buffer (printcharfun); \
215 printcharfun = Qnil; \
216 } \
217 if (MARKERP (printcharfun)) \
218 { \
1cf21850 219 EMACS_INT marker_pos; \
cd3587c1 220 if (! XMARKER (printcharfun)->buffer) \
0788646c 221 error ("Marker does not point anywhere"); \
1cf21850
LK
222 if (XMARKER (printcharfun)->buffer != current_buffer) \
223 set_buffer_internal (XMARKER (printcharfun)->buffer); \
224 marker_pos = marker_position (printcharfun); \
225 if (marker_pos < BEGV || marker_pos > ZV) \
226 error ("Marker is outside the accessible part of the buffer"); \
0788646c
GM
227 old_point = PT; \
228 old_point_byte = PT_BYTE; \
1cf21850 229 SET_PT_BOTH (marker_pos, \
0788646c
GM
230 marker_byte_position (printcharfun)); \
231 start_point = PT; \
232 start_point_byte = PT_BYTE; \
233 printcharfun = Qnil; \
234 } \
235 if (NILP (printcharfun)) \
236 { \
237 Lisp_Object string; \
238 if (NILP (current_buffer->enable_multibyte_characters) \
239 && ! print_escape_multibyte) \
240 specbind (Qprint_escape_multibyte, Qt); \
d39f07c2
RS
241 if (! NILP (current_buffer->enable_multibyte_characters) \
242 && ! print_escape_nonascii) \
243 specbind (Qprint_escape_nonascii, Qt); \
0788646c
GM
244 if (print_buffer != 0) \
245 { \
246 string = make_string_from_bytes (print_buffer, \
247 print_buffer_pos, \
248 print_buffer_pos_byte); \
249 record_unwind_protect (print_unwind, string); \
250 } \
251 else \
252 { \
253 print_buffer_size = 1000; \
254 print_buffer = (char *) xmalloc (print_buffer_size); \
255 free_print_buffer = 1; \
256 } \
257 print_buffer_pos = 0; \
258 print_buffer_pos_byte = 0; \
259 } \
5494b50f 260 if (EQ (printcharfun, Qt) && ! noninteractive) \
0f25ecc6 261 setup_echo_area_for_printing (multibyte);
38010d50 262
8a2ab0c6
RS
263#define PRINTFINISH \
264 if (NILP (printcharfun)) \
265 { \
266 if (print_buffer_pos != print_buffer_pos_byte \
267 && NILP (current_buffer->enable_multibyte_characters)) \
268 { \
269 unsigned char *temp \
270 = (unsigned char *) alloca (print_buffer_pos + 1); \
271 copy_text (print_buffer, temp, print_buffer_pos_byte, \
272 1, 0); \
273 insert_1_both (temp, print_buffer_pos, \
274 print_buffer_pos, 0, 1, 0); \
275 } \
276 else \
277 insert_1_both (print_buffer, print_buffer_pos, \
278 print_buffer_pos_byte, 0, 1, 0); \
570fab6c 279 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
8a2ab0c6
RS
280 } \
281 if (free_print_buffer) \
282 { \
283 xfree (print_buffer); \
284 print_buffer = 0; \
285 } \
286 unbind_to (specpdl_count, Qnil); \
287 if (MARKERP (original)) \
288 set_marker_both (original, Qnil, PT, PT_BYTE); \
289 if (old_point >= 0) \
290 SET_PT_BOTH (old_point + (old_point >= start_point \
291 ? PT - start_point : 0), \
6ddd6eee 292 old_point_byte + (old_point_byte >= start_point_byte \
cd3587c1 293 ? PT_BYTE - start_point_byte : 0)); \
8a2ab0c6 294 if (old != current_buffer) \
0f25ecc6 295 set_buffer_internal (old);
38010d50
JB
296
297#define PRINTCHAR(ch) printchar (ch, printcharfun)
298
08e8d297
RS
299/* This is used to restore the saved contents of print_buffer
300 when there is a recursive call to print. */
0788646c 301
08e8d297
RS
302static Lisp_Object
303print_unwind (saved_text)
304 Lisp_Object saved_text;
305{
d5db4077 306 bcopy (SDATA (saved_text), print_buffer, SCHARS (saved_text));
ee5263af 307 return Qnil;
08e8d297
RS
308}
309
0788646c
GM
310
311/* Print character CH using method FUN. FUN nil means print to
312 print_buffer. FUN t means print to echo area or stdout if
313 non-interactive. If FUN is neither nil nor t, call FUN with CH as
314 argument. */
38010d50
JB
315
316static void
317printchar (ch, fun)
087e3c46 318 unsigned int ch;
38010d50
JB
319 Lisp_Object fun;
320{
38010d50
JB
321#ifdef MAX_PRINT_CHARS
322 if (max_print)
323 print_chars++;
324#endif /* MAX_PRINT_CHARS */
38010d50 325
0788646c
GM
326 if (!NILP (fun) && !EQ (fun, Qt))
327 call1 (fun, make_number (ch));
328 else
38010d50 329 {
19a86a03
KH
330 unsigned char str[MAX_MULTIBYTE_LENGTH];
331 int len = CHAR_STRING (ch, str);
332
09eddb56 333 QUIT;
177c0ea7 334
0788646c 335 if (NILP (fun))
9a4d01d8 336 {
0788646c
GM
337 if (print_buffer_pos_byte + len >= print_buffer_size)
338 print_buffer = (char *) xrealloc (print_buffer,
339 print_buffer_size *= 2);
340 bcopy (str, print_buffer + print_buffer_pos_byte, len);
341 print_buffer_pos += 1;
342 print_buffer_pos_byte += len;
9a4d01d8 343 }
0788646c 344 else if (noninteractive)
1134b854 345 {
0788646c
GM
346 fwrite (str, 1, len, stdout);
347 noninteractive_need_newline = 1;
1134b854 348 }
0788646c 349 else
d366d2e4 350 {
0788646c
GM
351 int multibyte_p
352 = !NILP (current_buffer->enable_multibyte_characters);
177c0ea7 353
eb7b678b 354 setup_echo_area_for_printing (multibyte_p);
0788646c
GM
355 insert_char (ch);
356 message_dolog (str, len, 0, multibyte_p);
d366d2e4 357 }
38010d50 358 }
38010d50
JB
359}
360
0788646c
GM
361
362/* Output SIZE characters, SIZE_BYTE bytes from string PTR using
363 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
364 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
365 print_buffer. PRINTCHARFUN t means output to the echo area or to
366 stdout if non-interactive. If neither nil nor t, call Lisp
367 function PRINTCHARFUN for each character printed. MULTIBYTE
efb15f96
RS
368 non-zero means PTR contains multibyte characters.
369
370 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
371 to data in a Lisp string. Otherwise that is not safe. */
0788646c 372
38010d50 373static void
dc2a0b79 374strout (ptr, size, size_byte, printcharfun, multibyte)
38010d50 375 char *ptr;
dc2a0b79 376 int size, size_byte;
38010d50 377 Lisp_Object printcharfun;
dc2a0b79 378 int multibyte;
38010d50 379{
087e3c46 380 if (size < 0)
dc2a0b79 381 size_byte = size = strlen (ptr);
087e3c46 382
0788646c 383 if (NILP (printcharfun))
38010d50 384 {
dc2a0b79 385 if (print_buffer_pos_byte + size_byte > print_buffer_size)
6fec5601 386 {
dc2a0b79 387 print_buffer_size = print_buffer_size * 2 + size_byte;
6fec5601
RS
388 print_buffer = (char *) xrealloc (print_buffer,
389 print_buffer_size);
390 }
dc2a0b79 391 bcopy (ptr, print_buffer + print_buffer_pos_byte, size_byte);
6fec5601 392 print_buffer_pos += size;
dc2a0b79 393 print_buffer_pos_byte += size_byte;
6fec5601 394
38010d50
JB
395#ifdef MAX_PRINT_CHARS
396 if (max_print)
6fec5601 397 print_chars += size;
38010d50 398#endif /* MAX_PRINT_CHARS */
38010d50 399 }
7b0cb8a0 400 else if (noninteractive && EQ (printcharfun, Qt))
38010d50 401 {
0788646c
GM
402 fwrite (ptr, 1, size_byte, stdout);
403 noninteractive_need_newline = 1;
404 }
405 else if (EQ (printcharfun, Qt))
406 {
407 /* Output to echo area. We're trying to avoid a little overhead
408 here, that's the reason we don't call printchar to do the
409 job. */
410 int i;
411 int multibyte_p
412 = !NILP (current_buffer->enable_multibyte_characters);
177c0ea7 413
eb7b678b 414 setup_echo_area_for_printing (multibyte_p);
0788646c 415 message_dolog (ptr, size_byte, 0, multibyte_p);
177c0ea7 416
0788646c 417 if (size == size_byte)
38010d50 418 {
0788646c 419 for (i = 0; i < size; ++i)
bcdbfd36 420 insert_char ((unsigned char) *ptr++);
38010d50 421 }
0788646c 422 else
38010d50 423 {
0788646c
GM
424 int len;
425 for (i = 0; i < size_byte; i += len)
aec2b95b 426 {
0788646c
GM
427 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
428 insert_char (ch);
aec2b95b 429 }
38010d50 430 }
177c0ea7 431
0788646c
GM
432#ifdef MAX_PRINT_CHARS
433 if (max_print)
434 print_chars += size;
435#endif /* MAX_PRINT_CHARS */
436 }
437 else
438 {
439 /* PRINTCHARFUN is a Lisp function. */
440 int i = 0;
38010d50 441
0788646c 442 if (size == size_byte)
4ad8bb20 443 {
0788646c 444 while (i < size_byte)
4ad8bb20 445 {
0788646c
GM
446 int ch = ptr[i++];
447 PRINTCHAR (ch);
4ad8bb20 448 }
4ad8bb20 449 }
0788646c 450 else
087e3c46 451 {
0788646c
GM
452 while (i < size_byte)
453 {
454 /* Here, we must convert each multi-byte form to the
455 corresponding character code before handing it to
456 PRINTCHAR. */
457 int len;
458 int ch = STRING_CHAR_AND_LENGTH (ptr + i, size_byte - i, len);
459 PRINTCHAR (ch);
460 i += len;
461 }
087e3c46 462 }
38010d50 463 }
38010d50
JB
464}
465
466/* Print the contents of a string STRING using PRINTCHARFUN.
ed2c35ef
RS
467 It isn't safe to use strout in many cases,
468 because printing one char can relocate. */
38010d50 469
dc2a0b79 470static void
38010d50
JB
471print_string (string, printcharfun)
472 Lisp_Object string;
473 Lisp_Object printcharfun;
474{
6fec5601 475 if (EQ (printcharfun, Qt) || NILP (printcharfun))
375fcc09
KH
476 {
477 int chars;
478
479 if (STRING_MULTIBYTE (string))
d5db4077 480 chars = SCHARS (string);
375fcc09
KH
481 else if (EQ (printcharfun, Qt)
482 ? ! NILP (buffer_defaults.enable_multibyte_characters)
483 : ! NILP (current_buffer->enable_multibyte_characters))
a76ef35d
KH
484 {
485 /* If unibyte string STRING contains 8-bit codes, we must
486 convert STRING to a multibyte string containing the same
487 character codes. */
488 Lisp_Object newstr;
489 int bytes;
490
d5db4077
KR
491 chars = SBYTES (string);
492 bytes = parse_str_to_multibyte (SDATA (string), chars);
a76ef35d
KH
493 if (chars < bytes)
494 {
495 newstr = make_uninit_multibyte_string (chars, bytes);
d5db4077
KR
496 bcopy (SDATA (string), SDATA (newstr), chars);
497 str_to_multibyte (SDATA (newstr), bytes, chars);
a76ef35d
KH
498 string = newstr;
499 }
500 }
375fcc09 501 else
d5db4077 502 chars = SBYTES (string);
375fcc09 503
efb15f96
RS
504 if (EQ (printcharfun, Qt))
505 {
506 /* Output to echo area. */
507 int nbytes = SBYTES (string);
508 char *buffer;
509
510 /* Copy the string contents so that relocation of STRING by
511 GC does not cause trouble. */
512 USE_SAFE_ALLOCA;
513
514 SAFE_ALLOCA (buffer, char *, nbytes);
515 bcopy (SDATA (string), buffer, nbytes);
516
517 strout (buffer, chars, SBYTES (string),
518 printcharfun, STRING_MULTIBYTE (string));
519
520 SAFE_FREE ();
521 }
522 else
523 /* No need to copy, since output to print_buffer can't GC. */
524 strout (SDATA (string),
525 chars, SBYTES (string),
526 printcharfun, STRING_MULTIBYTE (string));
375fcc09 527 }
38010d50
JB
528 else
529 {
dc2a0b79
RS
530 /* Otherwise, string may be relocated by printing one char.
531 So re-fetch the string address for each character. */
38010d50 532 int i;
d5db4077
KR
533 int size = SCHARS (string);
534 int size_byte = SBYTES (string);
38010d50
JB
535 struct gcpro gcpro1;
536 GCPRO1 (string);
dc2a0b79
RS
537 if (size == size_byte)
538 for (i = 0; i < size; i++)
d5db4077 539 PRINTCHAR (SREF (string, i));
dc2a0b79 540 else
1661e56b 541 for (i = 0; i < size_byte; )
dc2a0b79
RS
542 {
543 /* Here, we must convert each multi-byte form to the
544 corresponding character code before handing it to PRINTCHAR. */
545 int len;
d5db4077 546 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i,
765fe1d0 547 size_byte - i, len);
da1ad4e7
KH
548 if (!CHAR_VALID_P (ch, 0))
549 {
d5db4077 550 ch = SREF (string, i);
da1ad4e7
KH
551 len = 1;
552 }
dc2a0b79
RS
553 PRINTCHAR (ch);
554 i += len;
555 }
38010d50
JB
556 UNGCPRO;
557 }
558}
559\f
560DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
8c1a1077
PJ
561 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
562PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
563 (character, printcharfun)
3738a371 564 Lisp_Object character, printcharfun;
38010d50 565{
081e0581 566 PRINTDECLARE;
38010d50 567
10eebdbb 568 if (NILP (printcharfun))
38010d50 569 printcharfun = Vstandard_output;
b7826503 570 CHECK_NUMBER (character);
38010d50 571 PRINTPREPARE;
3738a371 572 PRINTCHAR (XINT (character));
38010d50 573 PRINTFINISH;
3738a371 574 return character;
38010d50
JB
575}
576
dc2a0b79
RS
577/* Used from outside of print.c to print a block of SIZE
578 single-byte chars at DATA on the default output stream.
38010d50
JB
579 Do not use this on the contents of a Lisp string. */
580
dc22f25e 581void
38010d50
JB
582write_string (data, size)
583 char *data;
584 int size;
585{
081e0581 586 PRINTDECLARE;
38010d50 587 Lisp_Object printcharfun;
38010d50
JB
588
589 printcharfun = Vstandard_output;
590
591 PRINTPREPARE;
dc2a0b79 592 strout (data, size, size, printcharfun, 0);
38010d50
JB
593 PRINTFINISH;
594}
595
dc2a0b79
RS
596/* Used from outside of print.c to print a block of SIZE
597 single-byte chars at DATA on a specified stream PRINTCHARFUN.
38010d50
JB
598 Do not use this on the contents of a Lisp string. */
599
dc22f25e 600void
38010d50
JB
601write_string_1 (data, size, printcharfun)
602 char *data;
603 int size;
604 Lisp_Object printcharfun;
605{
081e0581 606 PRINTDECLARE;
38010d50
JB
607
608 PRINTPREPARE;
dc2a0b79 609 strout (data, size, size, printcharfun, 0);
38010d50
JB
610 PRINTFINISH;
611}
612
613
38010d50
JB
614void
615temp_output_buffer_setup (bufname)
3f7e390a 616 const char *bufname;
38010d50 617{
aed13378 618 int count = SPECPDL_INDEX ();
38010d50
JB
619 register struct buffer *old = current_buffer;
620 register Lisp_Object buf;
621
d9c21094
RS
622 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
623
38010d50
JB
624 Fset_buffer (Fget_buffer_create (build_string (bufname)));
625
9756ea0f 626 Fkill_all_local_variables ();
fd531951 627 delete_all_overlays (current_buffer);
2a1c968a 628 current_buffer->directory = old->directory;
38010d50 629 current_buffer->read_only = Qnil;
c5c6d57c
KH
630 current_buffer->filename = Qnil;
631 current_buffer->undo_list = Qt;
b3f6010e
SM
632 eassert (current_buffer->overlays_before == NULL);
633 eassert (current_buffer->overlays_after == NULL);
c5c6d57c
KH
634 current_buffer->enable_multibyte_characters
635 = buffer_defaults.enable_multibyte_characters;
d28eab19
KH
636 specbind (Qinhibit_read_only, Qt);
637 specbind (Qinhibit_modification_hooks, Qt);
38010d50 638 Ferase_buffer ();
633307b5 639 XSETBUFFER (buf, current_buffer);
38010d50 640
98040cf1 641 Frun_hooks (1, &Qtemp_buffer_setup_hook);
d9c21094
RS
642
643 unbind_to (count, Qnil);
644
645 specbind (Qstandard_output, buf);
38010d50
JB
646}
647
648Lisp_Object
649internal_with_output_to_temp_buffer (bufname, function, args)
3f7e390a 650 const char *bufname;
dfcf069d 651 Lisp_Object (*function) P_ ((Lisp_Object));
38010d50
JB
652 Lisp_Object args;
653{
aed13378 654 int count = SPECPDL_INDEX ();
38010d50 655 Lisp_Object buf, val;
0ab39c81 656 struct gcpro gcpro1;
38010d50 657
0ab39c81 658 GCPRO1 (args);
38010d50
JB
659 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
660 temp_output_buffer_setup (bufname);
661 buf = Vstandard_output;
0ab39c81 662 UNGCPRO;
38010d50
JB
663
664 val = (*function) (args);
665
0ab39c81 666 GCPRO1 (val);
38010d50 667 temp_output_buffer_show (buf);
0ab39c81 668 UNGCPRO;
38010d50
JB
669
670 return unbind_to (count, val);
671}
672
ab9ffd19
MB
673DEFUN ("with-output-to-temp-buffer",
674 Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
38010d50 675 1, UNEVALLED, 0,
8c1a1077 676 doc: /* Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.
80281b01
RS
677
678This construct makes buffer BUFNAME empty before running BODY.
679It does not make the buffer current for BODY.
680Instead it binds `standard-output' to that buffer, so that output
681generated with `prin1' and similar functions in BODY goes into
682the buffer.
683
684At the end of BODY, this marks buffer BUFNAME unmodifed and displays
685it in a window, but does not select it. The normal way to do this is
686by calling `display-buffer', then running `temp-buffer-show-hook'.
687However, if `temp-buffer-show-function' is non-nil, it calls that
688function instead (and does not run `temp-buffer-show-hook'). The
689function gets one argument, the buffer to display.
690
691The return value of `with-output-to-temp-buffer' is the value of the
692last form in BODY. If BODY does not finish normally, the buffer
693BUFNAME is not displayed.
694
695This runs the hook `temp-buffer-setup-hook' before BODY,
696with the buffer BUFNAME temporarily current. It runs the hook
697`temp-buffer-show-hook' after displaying buffer BUFNAME, with that
698buffer temporarily current, and the window that was used to display it
699temporarily selected. But it doesn't run `temp-buffer-show-hook'
700if it uses `temp-buffer-show-function'.
ab9ffd19 701
327ffff8 702usage: (with-output-to-temp-buffer BUFNAME BODY...) */)
8c1a1077 703 (args)
38010d50
JB
704 Lisp_Object args;
705{
706 struct gcpro gcpro1;
707 Lisp_Object name;
aed13378 708 int count = SPECPDL_INDEX ();
38010d50
JB
709 Lisp_Object buf, val;
710
711 GCPRO1(args);
712 name = Feval (Fcar (args));
b7826503 713 CHECK_STRING (name);
d5db4077 714 temp_output_buffer_setup (SDATA (name));
38010d50 715 buf = Vstandard_output;
8bbfc258 716 UNGCPRO;
38010d50 717
8bbfc258 718 val = Fprogn (XCDR (args));
38010d50 719
8bbfc258 720 GCPRO1 (val);
38010d50 721 temp_output_buffer_show (buf);
8bbfc258 722 UNGCPRO;
38010d50
JB
723
724 return unbind_to (count, val);
725}
0788646c 726
38010d50
JB
727\f
728static void print ();
0f25ecc6 729static void print_preprocess ();
0f25ecc6 730static void print_preprocess_string ();
0f25ecc6 731static void print_object ();
38010d50
JB
732
733DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
8c1a1077
PJ
734 doc: /* Output a newline to stream PRINTCHARFUN.
735If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
38010d50
JB
736 (printcharfun)
737 Lisp_Object printcharfun;
738{
081e0581 739 PRINTDECLARE;
38010d50 740
10eebdbb 741 if (NILP (printcharfun))
38010d50
JB
742 printcharfun = Vstandard_output;
743 PRINTPREPARE;
744 PRINTCHAR ('\n');
745 PRINTFINISH;
746 return Qt;
747}
748
749DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
8c1a1077
PJ
750 doc: /* Output the printed representation of OBJECT, any Lisp object.
751Quoting characters are printed when needed to make output that `read'
9474c847 752can handle, whenever this is possible. For complex objects, the behavior
7fab9223 753is controlled by `print-level' and `print-length', which see.
8c1a1077
PJ
754
755OBJECT is any of the Lisp data types: a number, a string, a symbol,
756a list, a buffer, a window, a frame, etc.
757
758A printed representation of an object is text which describes that object.
759
760Optional argument PRINTCHARFUN is the output stream, which can be one
761of these:
762
763 - a buffer, in which case output is inserted into that buffer at point;
764 - a marker, in which case output is inserted at marker's position;
765 - a function, in which case that function is called once for each
766 character of OBJECT's printed representation;
767 - a symbol, in which case that symbol's function definition is called; or
768 - t, in which case the output is displayed in the echo area.
769
770If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
771is used instead. */)
772 (object, printcharfun)
3738a371 773 Lisp_Object object, printcharfun;
38010d50 774{
081e0581 775 PRINTDECLARE;
38010d50
JB
776
777#ifdef MAX_PRINT_CHARS
778 max_print = 0;
779#endif /* MAX_PRINT_CHARS */
10eebdbb 780 if (NILP (printcharfun))
38010d50
JB
781 printcharfun = Vstandard_output;
782 PRINTPREPARE;
3738a371 783 print (object, printcharfun, 1);
38010d50 784 PRINTFINISH;
3738a371 785 return object;
38010d50
JB
786}
787
788/* a buffer which is used to hold output being built by prin1-to-string */
789Lisp_Object Vprin1_to_string_buffer;
790
791DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
fd4d156b
RS
792 doc: /* Return a string containing the printed representation of OBJECT.
793OBJECT can be any Lisp object. This function outputs quoting characters
cf393f9b 794when necessary to make output that `read' can handle, whenever possible,
a2612656
EZ
795unless the optional second argument NOESCAPE is non-nil. For complex objects,
796the behavior is controlled by `print-level' and `print-length', which see.
8c1a1077
PJ
797
798OBJECT is any of the Lisp data types: a number, a string, a symbol,
799a list, a buffer, a window, a frame, etc.
800
801A printed representation of an object is text which describes that object. */)
802 (object, noescape)
3738a371 803 Lisp_Object object, noescape;
38010d50 804{
081e0581 805 Lisp_Object printcharfun;
ca2de342
RS
806 /* struct gcpro gcpro1, gcpro2; */
807 Lisp_Object save_deactivate_mark;
1a7de17e 808 int count = SPECPDL_INDEX ();
dd418e06 809 struct buffer *previous;
ca2de342
RS
810
811 specbind (Qinhibit_modification_hooks, Qt);
2a42e8f6 812
dd418e06
RS
813 {
814 PRINTDECLARE;
38010d50 815
dd418e06
RS
816 /* Save and restore this--we are altering a buffer
817 but we don't want to deactivate the mark just for that.
818 No need for specbind, since errors deactivate the mark. */
819 save_deactivate_mark = Vdeactivate_mark;
820 /* GCPRO2 (object, save_deactivate_mark); */
821 abort_on_gc++;
822
823 printcharfun = Vprin1_to_string_buffer;
824 PRINTPREPARE;
825 print (object, printcharfun, NILP (noescape));
826 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
827 PRINTFINISH;
828 }
829
830 previous = current_buffer;
38010d50 831 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
3738a371 832 object = Fbuffer_string ();
b51ee131
SM
833 if (SBYTES (object) == SCHARS (object))
834 STRING_SET_UNIBYTE (object);
38010d50 835
28b8f740 836 /* Note that this won't make prepare_to_modify_buffer call
dd418e06
RS
837 ask-user-about-supersession-threat because this buffer
838 does not visit a file. */
38010d50 839 Ferase_buffer ();
dd418e06 840 set_buffer_internal (previous);
2a42e8f6 841
ca2de342
RS
842 Vdeactivate_mark = save_deactivate_mark;
843 /* UNGCPRO; */
38010d50 844
ca2de342
RS
845 abort_on_gc--;
846 return unbind_to (count, object);
38010d50
JB
847}
848
849DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
8c1a1077
PJ
850 doc: /* Output the printed representation of OBJECT, any Lisp object.
851No quoting characters are used; no delimiters are printed around
852the contents of strings.
853
854OBJECT is any of the Lisp data types: a number, a string, a symbol,
855a list, a buffer, a window, a frame, etc.
856
857A printed representation of an object is text which describes that object.
858
859Optional argument PRINTCHARFUN is the output stream, which can be one
860of these:
861
862 - a buffer, in which case output is inserted into that buffer at point;
863 - a marker, in which case output is inserted at marker's position;
864 - a function, in which case that function is called once for each
865 character of OBJECT's printed representation;
866 - a symbol, in which case that symbol's function definition is called; or
867 - t, in which case the output is displayed in the echo area.
868
869If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
870is used instead. */)
871 (object, printcharfun)
3738a371 872 Lisp_Object object, printcharfun;
38010d50 873{
081e0581 874 PRINTDECLARE;
38010d50 875
10eebdbb 876 if (NILP (printcharfun))
38010d50
JB
877 printcharfun = Vstandard_output;
878 PRINTPREPARE;
3738a371 879 print (object, printcharfun, 0);
38010d50 880 PRINTFINISH;
3738a371 881 return object;
38010d50
JB
882}
883
884DEFUN ("print", Fprint, Sprint, 1, 2, 0,
8c1a1077
PJ
885 doc: /* Output the printed representation of OBJECT, with newlines around it.
886Quoting characters are printed when needed to make output that `read'
9474c847 887can handle, whenever this is possible. For complex objects, the behavior
7fab9223 888is controlled by `print-level' and `print-length', which see.
8c1a1077
PJ
889
890OBJECT is any of the Lisp data types: a number, a string, a symbol,
891a list, a buffer, a window, a frame, etc.
892
893A printed representation of an object is text which describes that object.
894
895Optional argument PRINTCHARFUN is the output stream, which can be one
896of these:
897
898 - a buffer, in which case output is inserted into that buffer at point;
899 - a marker, in which case output is inserted at marker's position;
900 - a function, in which case that function is called once for each
901 character of OBJECT's printed representation;
902 - a symbol, in which case that symbol's function definition is called; or
903 - t, in which case the output is displayed in the echo area.
904
905If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
906is used instead. */)
907 (object, printcharfun)
3738a371 908 Lisp_Object object, printcharfun;
38010d50 909{
081e0581 910 PRINTDECLARE;
38010d50
JB
911 struct gcpro gcpro1;
912
913#ifdef MAX_PRINT_CHARS
914 print_chars = 0;
915 max_print = MAX_PRINT_CHARS;
916#endif /* MAX_PRINT_CHARS */
10eebdbb 917 if (NILP (printcharfun))
38010d50 918 printcharfun = Vstandard_output;
3738a371 919 GCPRO1 (object);
38010d50 920 PRINTPREPARE;
38010d50 921 PRINTCHAR ('\n');
3738a371 922 print (object, printcharfun, 1);
38010d50
JB
923 PRINTCHAR ('\n');
924 PRINTFINISH;
925#ifdef MAX_PRINT_CHARS
926 max_print = 0;
927 print_chars = 0;
928#endif /* MAX_PRINT_CHARS */
929 UNGCPRO;
3738a371 930 return object;
38010d50
JB
931}
932
933/* The subroutine object for external-debugging-output is kept here
934 for the convenience of the debugger. */
935Lisp_Object Qexternal_debugging_output;
936
4746118a 937DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
8c1a1077
PJ
938 doc: /* Write CHARACTER to stderr.
939You can call print while debugging emacs, and pass it this function
940to make it write to the debugging output. */)
941 (character)
4746118a 942 Lisp_Object character;
38010d50 943{
b7826503 944 CHECK_NUMBER (character);
38010d50 945 putc (XINT (character), stderr);
cd22039d
RS
946
947#ifdef WINDOWSNT
948 /* Send the output to a debugger (nothing happens if there isn't one). */
945b0111
EZ
949 if (print_output_debug_flag)
950 {
951 char buf[2] = {(char) XINT (character), '\0'};
952 OutputDebugString (buf);
953 }
cd22039d
RS
954#endif
955
38010d50
JB
956 return character;
957}
cf1bb91b 958
c33f8948
RS
959/* This function is never called. Its purpose is to prevent
960 print_output_debug_flag from being optimized away. */
961
dae581bf 962void
c33f8948
RS
963debug_output_compilation_hack (x)
964 int x;
965{
966 print_output_debug_flag = x;
967}
95e52d88 968
cd3587c1 969#if defined (GNU_LINUX)
95e52d88
KS
970
971/* This functionality is not vitally important in general, so we rely on
972 non-portable ability to use stderr as lvalue. */
973
974#define WITH_REDIRECT_DEBUGGING_OUTPUT 1
975
6a1ff3ba
KS
976FILE *initial_stderr_stream = NULL;
977
978DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
979 1, 2,
980 "FDebug output file: \nP",
981 doc: /* Redirect debugging output (stderr stream) to file FILE.
982If FILE is nil, reset target to the initial stderr stream.
983Optional arg APPEND non-nil (interactively, with prefix arg) means
28b8f740 984append to existing target file. */)
6a1ff3ba
KS
985 (file, append)
986 Lisp_Object file, append;
987{
988 if (initial_stderr_stream != NULL)
f9467be3
YM
989 {
990 BLOCK_INPUT;
991 fclose (stderr);
992 UNBLOCK_INPUT;
993 }
6a1ff3ba
KS
994 stderr = initial_stderr_stream;
995 initial_stderr_stream = NULL;
996
997 if (STRINGP (file))
998 {
999 file = Fexpand_file_name (file, Qnil);
1000 initial_stderr_stream = stderr;
cd3587c1 1001 stderr = fopen (SDATA (file), NILP (append) ? "w" : "a");
6a1ff3ba
KS
1002 if (stderr == NULL)
1003 {
1004 stderr = initial_stderr_stream;
1005 initial_stderr_stream = NULL;
1006 report_file_error ("Cannot open debugging output stream",
1007 Fcons (file, Qnil));
1008 }
1009 }
1010 return Qnil;
1011}
95e52d88
KS
1012#endif /* GNU_LINUX */
1013
6a1ff3ba 1014
cf1bb91b
RS
1015/* This is the interface for debugging printing. */
1016
1017void
1018debug_print (arg)
1019 Lisp_Object arg;
1020{
1021 Fprin1 (arg, Qexternal_debugging_output);
3684eb78 1022 fprintf (stderr, "\r\n");
cf1bb91b 1023}
bd90dcd0
KS
1024
1025void
1026safe_debug_print (arg)
1027 Lisp_Object arg;
1028{
1029 int valid = valid_lisp_object_p (arg);
1030
1031 if (valid > 0)
1032 debug_print (arg);
1033 else
1034 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08lx>\r\n",
1035 !valid ? "INVALID" : "SOME",
1036#ifdef NO_UNION_TYPE
1037 (unsigned long) arg
1038#else
1039 (unsigned long) arg.i
1040#endif
1041 );
1042}
1043
38010d50 1044\f
113620cc
KH
1045DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
1046 1, 1, 0,
f82ec1e6
LT
1047 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
1048See Info anchor `(elisp)Definition of signal' for some details on how this
1049error message is constructed. */)
8c1a1077 1050 (obj)
113620cc
KH
1051 Lisp_Object obj;
1052{
1053 struct buffer *old = current_buffer;
63fbf4ff 1054 Lisp_Object value;
113620cc
KH
1055 struct gcpro gcpro1;
1056
0872e11f
RS
1057 /* If OBJ is (error STRING), just return STRING.
1058 That is not only faster, it also avoids the need to allocate
1059 space here when the error is due to memory full. */
94b342ce
KR
1060 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
1061 && CONSP (XCDR (obj))
1062 && STRINGP (XCAR (XCDR (obj)))
1063 && NILP (XCDR (XCDR (obj))))
1064 return XCAR (XCDR (obj));
0872e11f 1065
240e806c 1066 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
113620cc
KH
1067
1068 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
1069 value = Fbuffer_string ();
1070
1071 GCPRO1 (value);
1072 Ferase_buffer ();
1073 set_buffer_internal (old);
1074 UNGCPRO;
1075
1076 return value;
1077}
1078
c02279de 1079/* Print an error message for the error DATA onto Lisp output stream
7ab56fea
RS
1080 STREAM (suitable for the print functions).
1081 CONTEXT is a C string describing the context of the error.
1082 CALLER is the Lisp function inside which the error was signaled. */
113620cc 1083
dc22f25e 1084void
240e806c 1085print_error_message (data, stream, context, caller)
113620cc 1086 Lisp_Object data, stream;
240e806c
RS
1087 char *context;
1088 Lisp_Object caller;
113620cc
KH
1089{
1090 Lisp_Object errname, errmsg, file_error, tail;
1091 struct gcpro gcpro1;
1092 int i;
1093
240e806c
RS
1094 if (context != 0)
1095 write_string_1 (context, -1, stream);
1096
1097 /* If we know from where the error was signaled, show it in
1098 *Messages*. */
1099 if (!NILP (caller) && SYMBOLP (caller))
1100 {
11fb15d5
KS
1101 Lisp_Object cname = SYMBOL_NAME (caller);
1102 char *name = alloca (SBYTES (cname));
1103 bcopy (SDATA (cname), name, SBYTES (cname));
826256dd 1104 message_dolog (name, SBYTES (cname), 0, 0);
240e806c
RS
1105 message_dolog (": ", 2, 0, 0);
1106 }
1107
113620cc
KH
1108 errname = Fcar (data);
1109
1110 if (EQ (errname, Qerror))
1111 {
1112 data = Fcdr (data);
c02279de
GM
1113 if (!CONSP (data))
1114 data = Qnil;
113620cc
KH
1115 errmsg = Fcar (data);
1116 file_error = Qnil;
1117 }
1118 else
1119 {
c02279de 1120 Lisp_Object error_conditions;
113620cc 1121 errmsg = Fget (errname, Qerror_message);
c02279de
GM
1122 error_conditions = Fget (errname, Qerror_conditions);
1123 file_error = Fmemq (Qfile_error, error_conditions);
113620cc
KH
1124 }
1125
1126 /* Print an error message including the data items. */
1127
1128 tail = Fcdr_safe (data);
1129 GCPRO1 (tail);
1130
1131 /* For file-error, make error message by concatenating
1132 all the data items. They are all strings. */
8c29413d 1133 if (!NILP (file_error) && CONSP (tail))
94b342ce 1134 errmsg = XCAR (tail), tail = XCDR (tail);
113620cc
KH
1135
1136 if (STRINGP (errmsg))
1137 Fprinc (errmsg, stream);
1138 else
1139 write_string_1 ("peculiar error", -1, stream);
1140
c02279de 1141 for (i = 0; CONSP (tail); tail = XCDR (tail), i++)
113620cc 1142 {
c02279de
GM
1143 Lisp_Object obj;
1144
113620cc 1145 write_string_1 (i ? ", " : ": ", 2, stream);
c02279de
GM
1146 obj = XCAR (tail);
1147 if (!NILP (file_error) || EQ (errname, Qend_of_file))
1148 Fprinc (obj, stream);
113620cc 1149 else
c02279de 1150 Fprin1 (obj, stream);
113620cc 1151 }
177c0ea7 1152
113620cc
KH
1153 UNGCPRO;
1154}
38010d50 1155
c02279de
GM
1156
1157\f
38010d50 1158/*
edb2a707 1159 * The buffer should be at least as large as the max string size of the
8e6208c5 1160 * largest float, printed in the biggest notation. This is undoubtedly
38010d50
JB
1161 * 20d float_output_format, with the negative of the C-constant "HUGE"
1162 * from <math.h>.
177c0ea7 1163 *
38010d50 1164 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
177c0ea7 1165 *
38010d50
JB
1166 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
1167 * case of -1e307 in 20d float_output_format. What is one to do (short of
1168 * re-writing _doprnt to be more sane)?
1169 * -wsr
1170 */
edb2a707
RS
1171
1172void
1173float_to_string (buf, data)
8b24d146 1174 unsigned char *buf;
38010d50
JB
1175 double data;
1176{
c7b14277 1177 unsigned char *cp;
322890c4 1178 int width;
177c0ea7 1179
7f45de2d
RS
1180 /* Check for plus infinity in a way that won't lose
1181 if there is no plus infinity. */
1182 if (data == data / 2 && data > 1.0)
1183 {
1184 strcpy (buf, "1.0e+INF");
1185 return;
1186 }
1187 /* Likewise for minus infinity. */
1188 if (data == data / 2 && data < -1.0)
1189 {
1190 strcpy (buf, "-1.0e+INF");
1191 return;
1192 }
1193 /* Check for NaN in a way that won't fail if there are no NaNs. */
1194 if (! (data * 0.0 >= 0.0))
1195 {
68c45bf0
PE
1196 /* Prepend "-" if the NaN's sign bit is negative.
1197 The sign bit of a double is the bit that is 1 in -0.0. */
1198 int i;
1199 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1200 u_data.d = data;
1201 u_minus_zero.d = - 0.0;
1202 for (i = 0; i < sizeof (double); i++)
1203 if (u_data.c[i] & u_minus_zero.c[i])
1204 {
1205 *buf++ = '-';
1206 break;
1207 }
177c0ea7 1208
7f45de2d
RS
1209 strcpy (buf, "0.0e+NaN");
1210 return;
1211 }
1212
10eebdbb 1213 if (NILP (Vfloat_output_format)
d4ae1f7e 1214 || !STRINGP (Vfloat_output_format))
38010d50 1215 lose:
322890c4 1216 {
f356c3fb
PE
1217 /* Generate the fewest number of digits that represent the
1218 floating point value without losing information.
1219 The following method is simple but a bit slow.
1220 For ideas about speeding things up, please see:
1221
1222 Guy L Steele Jr & Jon L White, How to print floating-point numbers
1223 accurately. SIGPLAN notices 25, 6 (June 1990), 112-126.
1224
1225 Robert G Burger & R Kent Dybvig, Printing floating point numbers
1226 quickly and accurately, SIGPLAN notices 31, 5 (May 1996), 108-116. */
1227
1228 width = fabs (data) < DBL_MIN ? 1 : DBL_DIG;
1229 do
1230 sprintf (buf, "%.*g", width, data);
1231 while (width++ < DOUBLE_DIGITS_BOUND && atof (buf) != data);
322890c4 1232 }
38010d50
JB
1233 else /* oink oink */
1234 {
1235 /* Check that the spec we have is fully valid.
1236 This means not only valid for printf,
1237 but meant for floats, and reasonable. */
d5db4077 1238 cp = SDATA (Vfloat_output_format);
38010d50
JB
1239
1240 if (cp[0] != '%')
1241 goto lose;
1242 if (cp[1] != '.')
1243 goto lose;
1244
1245 cp += 2;
c7b14277
JB
1246
1247 /* Check the width specification. */
322890c4 1248 width = -1;
c7b14277 1249 if ('0' <= *cp && *cp <= '9')
381cd4bb
KH
1250 {
1251 width = 0;
1252 do
1253 width = (width * 10) + (*cp++ - '0');
1254 while (*cp >= '0' && *cp <= '9');
1255
1256 /* A precision of zero is valid only for %f. */
1257 if (width > DBL_DIG
1258 || (width == 0 && *cp != 'f'))
1259 goto lose;
1260 }
38010d50
JB
1261
1262 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1263 goto lose;
1264
38010d50
JB
1265 if (cp[1] != 0)
1266 goto lose;
1267
d5db4077 1268 sprintf (buf, SDATA (Vfloat_output_format), data);
38010d50 1269 }
edb2a707 1270
c7b14277
JB
1271 /* Make sure there is a decimal point with digit after, or an
1272 exponent, so that the value is readable as a float. But don't do
322890c4
RS
1273 this with "%.0f"; it's valid for that not to produce a decimal
1274 point. Note that width can be 0 only for %.0f. */
1275 if (width != 0)
0601fd3d 1276 {
c7b14277
JB
1277 for (cp = buf; *cp; cp++)
1278 if ((*cp < '0' || *cp > '9') && *cp != '-')
1279 break;
0601fd3d 1280
c7b14277
JB
1281 if (*cp == '.' && cp[1] == 0)
1282 {
1283 cp[1] = '0';
1284 cp[2] = 0;
1285 }
1286
1287 if (*cp == 0)
1288 {
1289 *cp++ = '.';
1290 *cp++ = '0';
1291 *cp++ = 0;
1292 }
edb2a707 1293 }
38010d50 1294}
cc94f3b2 1295
38010d50
JB
1296\f
1297static void
1298print (obj, printcharfun, escapeflag)
38010d50 1299 Lisp_Object obj;
38010d50
JB
1300 register Lisp_Object printcharfun;
1301 int escapeflag;
1302{
a22dec27 1303 new_backquote_output = 0;
38010d50 1304
0f25ecc6
RS
1305 /* Reset print_number_index and Vprint_number_table only when
1306 the variable Vprint_continuous_numbering is nil. Otherwise,
1307 the values of these variables will be kept between several
1308 print functions. */
e026eb89
KS
1309 if (NILP (Vprint_continuous_numbering)
1310 || NILP (Vprint_number_table))
0f25ecc6
RS
1311 {
1312 print_number_index = 0;
1313 Vprint_number_table = Qnil;
1314 }
38010d50 1315
0f25ecc6
RS
1316 /* Construct Vprint_number_table for print-gensym and print-circle. */
1317 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
ec838c39 1318 {
73fb36f1 1319 int i, start, index;
73fb36f1 1320 start = index = print_number_index;
e6d4cddd
RS
1321 /* Construct Vprint_number_table.
1322 This increments print_number_index for the objects added. */
9a6a4c40 1323 print_depth = 0;
0f25ecc6 1324 print_preprocess (obj);
e6d4cddd 1325
0f25ecc6 1326 /* Remove unnecessary objects, which appear only once in OBJ;
e6d4cddd 1327 that is, whose status is Qnil. Compactify the necessary objects. */
73fb36f1 1328 for (i = start; i < print_number_index; i++)
0f25ecc6
RS
1329 if (!NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1330 {
1331 PRINT_NUMBER_OBJECT (Vprint_number_table, index)
1332 = PRINT_NUMBER_OBJECT (Vprint_number_table, i);
0f25ecc6
RS
1333 index++;
1334 }
e6d4cddd
RS
1335
1336 /* Clear out objects outside the active part of the table. */
1337 for (i = index; i < print_number_index; i++)
1338 PRINT_NUMBER_OBJECT (Vprint_number_table, i) = Qnil;
1339
1340 /* Reset the status field for the next print step. Now this
1341 field means whether the object has already been printed. */
1342 for (i = start; i < print_number_index; i++)
1343 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qnil;
1344
0f25ecc6
RS
1345 print_number_index = index;
1346 }
1347
9a6a4c40 1348 print_depth = 0;
0f25ecc6
RS
1349 print_object (obj, printcharfun, escapeflag);
1350}
1351
1352/* Construct Vprint_number_table according to the structure of OBJ.
1353 OBJ itself and all its elements will be added to Vprint_number_table
1354 recursively if it is a list, vector, compiled function, char-table,
1355 string (its text properties will be traced), or a symbol that has
1356 no obarray (this is for the print-gensym feature).
1357 The status fields of Vprint_number_table mean whether each object appears
1358 more than once in OBJ: Qnil at the first time, and Qt after that . */
1359static void
1360print_preprocess (obj)
1361 Lisp_Object obj;
1362{
3164abe6
AS
1363 int i;
1364 EMACS_INT size;
c1132671
RS
1365 int loop_count = 0;
1366 Lisp_Object halftail;
1367
15479e8b
RS
1368 /* Give up if we go so deep that print_object will get an error. */
1369 /* See similar code in print_object. */
6b8dfbf7
CY
1370 if (print_depth >= PRINT_CIRCLE)
1371 error ("Apparently circular structure being printed");
15479e8b 1372
c1132671
RS
1373 /* Avoid infinite recursion for circular nested structure
1374 in the case where Vprint_circle is nil. */
1375 if (NILP (Vprint_circle))
1376 {
1377 for (i = 0; i < print_depth; i++)
1378 if (EQ (obj, being_printed[i]))
1379 return;
1380 being_printed[print_depth] = obj;
1381 }
1382
c1132671
RS
1383 print_depth++;
1384 halftail = obj;
0f25ecc6
RS
1385
1386 loop:
1387 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1388 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1389 || (! NILP (Vprint_gensym)
bf9f2aab
GM
1390 && SYMBOLP (obj)
1391 && !SYMBOL_INTERNED_P (obj)))
0f25ecc6 1392 {
aca2020b
KH
1393 /* In case print-circle is nil and print-gensym is t,
1394 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1395 if (! NILP (Vprint_circle) || SYMBOLP (obj))
0f25ecc6 1396 {
0f25ecc6 1397 for (i = 0; i < print_number_index; i++)
7c752c80 1398 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
aca2020b
KH
1399 {
1400 /* OBJ appears more than once. Let's remember that. */
1401 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
aa0b0cd9
KL
1402 print_depth--;
1403 return;
aca2020b
KH
1404 }
1405
1406 /* OBJ is not yet recorded. Let's add to the table. */
1407 if (print_number_index == 0)
1408 {
1409 /* Initialize the table. */
1410 Vprint_number_table = Fmake_vector (make_number (40), Qnil);
1411 }
1412 else if (XVECTOR (Vprint_number_table)->size == print_number_index * 2)
0f25ecc6 1413 {
aca2020b
KH
1414 /* Reallocate the table. */
1415 int i = print_number_index * 4;
1416 Lisp_Object old_table = Vprint_number_table;
1417 Vprint_number_table = Fmake_vector (make_number (i), Qnil);
1418 for (i = 0; i < print_number_index; i++)
1419 {
1420 PRINT_NUMBER_OBJECT (Vprint_number_table, i)
1421 = PRINT_NUMBER_OBJECT (old_table, i);
1422 PRINT_NUMBER_STATUS (Vprint_number_table, i)
1423 = PRINT_NUMBER_STATUS (old_table, i);
1424 }
0f25ecc6 1425 }
aca2020b
KH
1426 PRINT_NUMBER_OBJECT (Vprint_number_table, print_number_index) = obj;
1427 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1428 always print the gensym with a number. This is a special for
1429 the lisp function byte-compile-output-docform. */
bf9f2aab
GM
1430 if (!NILP (Vprint_continuous_numbering)
1431 && SYMBOLP (obj)
1432 && !SYMBOL_INTERNED_P (obj))
aca2020b
KH
1433 PRINT_NUMBER_STATUS (Vprint_number_table, print_number_index) = Qt;
1434 print_number_index++;
0f25ecc6 1435 }
0f25ecc6
RS
1436
1437 switch (XGCTYPE (obj))
1438 {
1439 case Lisp_String:
0f25ecc6 1440 /* A string may have text properties, which can be circular. */
d5db4077 1441 traverse_intervals_noorder (STRING_INTERVALS (obj),
8bbfc258 1442 print_preprocess_string, Qnil);
0f25ecc6
RS
1443 break;
1444
1445 case Lisp_Cons:
c1132671
RS
1446 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1447 just as in print_object. */
1448 if (loop_count && EQ (obj, halftail))
1449 break;
0f25ecc6
RS
1450 print_preprocess (XCAR (obj));
1451 obj = XCDR (obj);
c1132671
RS
1452 loop_count++;
1453 if (!(loop_count & 1))
1454 halftail = XCDR (halftail);
0f25ecc6
RS
1455 goto loop;
1456
1457 case Lisp_Vectorlike:
3803eb32
AS
1458 size = XVECTOR (obj)->size;
1459 if (size & PSEUDOVECTOR_FLAG)
1460 size &= PSEUDOVECTOR_SIZE_MASK;
0f25ecc6
RS
1461 for (i = 0; i < size; i++)
1462 print_preprocess (XVECTOR (obj)->contents[i]);
ee5263af
GM
1463 break;
1464
1465 default:
1466 break;
0f25ecc6
RS
1467 }
1468 }
c1132671 1469 print_depth--;
0f25ecc6
RS
1470}
1471
0f25ecc6
RS
1472static void
1473print_preprocess_string (interval, arg)
1474 INTERVAL interval;
1475 Lisp_Object arg;
1476{
1477 print_preprocess (interval->plist);
1478}
0f25ecc6
RS
1479
1480static void
1481print_object (obj, printcharfun, escapeflag)
1482 Lisp_Object obj;
1483 register Lisp_Object printcharfun;
1484 int escapeflag;
1485{
28b8f740 1486 char buf[40];
0f25ecc6
RS
1487
1488 QUIT;
1489
1490 /* Detect circularities and truncate them. */
1491 if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
1492 || COMPILEDP (obj) || CHAR_TABLE_P (obj)
1493 || (! NILP (Vprint_gensym)
bf9f2aab
GM
1494 && SYMBOLP (obj)
1495 && !SYMBOL_INTERNED_P (obj)))
0f25ecc6
RS
1496 {
1497 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1498 {
1499 /* Simple but incomplete way. */
1500 int i;
1501 for (i = 0; i < print_depth; i++)
1502 if (EQ (obj, being_printed[i]))
1503 {
1504 sprintf (buf, "#%d", i);
1505 strout (buf, -1, -1, printcharfun, 0);
1506 return;
1507 }
1508 being_printed[print_depth] = obj;
1509 }
1510 else
1511 {
1512 /* With the print-circle feature. */
1513 int i;
1514 for (i = 0; i < print_number_index; i++)
7c752c80 1515 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i), obj))
0f25ecc6
RS
1516 {
1517 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1518 {
1519 /* Add a prefix #n= if OBJ has not yet been printed;
1520 that is, its status field is nil. */
1521 sprintf (buf, "#%d=", i + 1);
1522 strout (buf, -1, -1, printcharfun, 0);
1523 /* OBJ is going to be printed. Set the status to t. */
1524 PRINT_NUMBER_STATUS (Vprint_number_table, i) = Qt;
1525 break;
1526 }
1527 else
1528 {
1529 /* Just print #n# if OBJ has already been printed. */
1530 sprintf (buf, "#%d#", i + 1);
1531 strout (buf, -1, -1, printcharfun, 0);
1532 return;
1533 }
1534 }
1535 }
ec838c39 1536 }
ec838c39 1537
38010d50
JB
1538 print_depth++;
1539
c1132671 1540 /* See similar code in print_preprocess. */
ec838c39 1541 if (print_depth > PRINT_CIRCLE)
38010d50
JB
1542 error ("Apparently circular structure being printed");
1543#ifdef MAX_PRINT_CHARS
1544 if (max_print && print_chars > max_print)
1545 {
1546 PRINTCHAR ('\n');
1547 print_chars = 0;
1548 }
1549#endif /* MAX_PRINT_CHARS */
1550
ca0569ad 1551 switch (XGCTYPE (obj))
38010d50 1552 {
ca0569ad 1553 case Lisp_Int:
b8180922
RS
1554 if (sizeof (int) == sizeof (EMACS_INT))
1555 sprintf (buf, "%d", XINT (obj));
1556 else if (sizeof (long) == sizeof (EMACS_INT))
63fbf4ff 1557 sprintf (buf, "%ld", (long) XINT (obj));
b8180922
RS
1558 else
1559 abort ();
dc2a0b79 1560 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1561 break;
1562
ca0569ad
RS
1563 case Lisp_Float:
1564 {
1565 char pigbuf[350]; /* see comments in float_to_string */
38010d50 1566
94b342ce 1567 float_to_string (pigbuf, XFLOAT_DATA (obj));
dc2a0b79 1568 strout (pigbuf, -1, -1, printcharfun, 0);
ca0569ad
RS
1569 }
1570 break;
ca0569ad
RS
1571
1572 case Lisp_String:
38010d50
JB
1573 if (!escapeflag)
1574 print_string (obj, printcharfun);
1575 else
1576 {
dc2a0b79 1577 register int i, i_byte;
38010d50 1578 struct gcpro gcpro1;
872a36d2 1579 unsigned char *str;
dc2a0b79 1580 int size_byte;
453fa987
RS
1581 /* 1 means we must ensure that the next character we output
1582 cannot be taken as part of a hex character escape. */
1583 int need_nonhex = 0;
db300f59 1584 int multibyte = STRING_MULTIBYTE (obj);
38010d50 1585
7651e1f5
RS
1586 GCPRO1 (obj);
1587
d5db4077 1588 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
7651e1f5
RS
1589 {
1590 PRINTCHAR ('#');
1591 PRINTCHAR ('(');
1592 }
38010d50
JB
1593
1594 PRINTCHAR ('\"');
d5db4077
KR
1595 str = SDATA (obj);
1596 size_byte = SBYTES (obj);
dc2a0b79
RS
1597
1598 for (i = 0, i_byte = 0; i_byte < size_byte;)
38010d50 1599 {
6ddd6eee
RS
1600 /* Here, we must convert each multi-byte form to the
1601 corresponding character code before handing it to PRINTCHAR. */
1602 int len;
dc2a0b79
RS
1603 int c;
1604
db300f59 1605 if (multibyte)
872a36d2 1606 {
765fe1d0
KH
1607 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1608 size_byte - i_byte, len);
da1ad4e7
KH
1609 if (CHAR_VALID_P (c, 0))
1610 i_byte += len;
1611 else
1612 c = str[i_byte++];
872a36d2 1613 }
dc2a0b79 1614 else
872a36d2 1615 c = str[i_byte++];
dc2a0b79 1616
38010d50 1617 QUIT;
6ddd6eee 1618
38010d50
JB
1619 if (c == '\n' && print_escape_newlines)
1620 {
1621 PRINTCHAR ('\\');
1622 PRINTCHAR ('n');
1623 }
c6f7982f
RM
1624 else if (c == '\f' && print_escape_newlines)
1625 {
1626 PRINTCHAR ('\\');
1627 PRINTCHAR ('f');
1628 }
ae7367d3
RS
1629 else if (multibyte
1630 && ! ASCII_BYTE_P (c)
1631 && (SINGLE_BYTE_CHAR_P (c) || print_escape_multibyte))
dc2a0b79
RS
1632 {
1633 /* When multibyte is disabled,
ae7367d3
RS
1634 print multibyte string chars using hex escapes.
1635 For a char code that could be in a unibyte string,
1636 when found in a multibyte string, always use a hex escape
1637 so it reads back as multibyte. */
dc2a0b79
RS
1638 unsigned char outbuf[50];
1639 sprintf (outbuf, "\\x%x", c);
1640 strout (outbuf, -1, -1, printcharfun, 0);
453fa987 1641 need_nonhex = 1;
dc2a0b79 1642 }
db300f59
RS
1643 else if (! multibyte
1644 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
835d0be6 1645 && print_escape_nonascii)
974a6ff5 1646 {
835d0be6
RS
1647 /* When printing in a multibyte buffer
1648 or when explicitly requested,
974a6ff5
KH
1649 print single-byte non-ASCII string chars
1650 using octal escapes. */
1651 unsigned char outbuf[5];
1652 sprintf (outbuf, "\\%03o", c);
1653 strout (outbuf, -1, -1, printcharfun, 0);
1654 }
38010d50
JB
1655 else
1656 {
453fa987
RS
1657 /* If we just had a hex escape, and this character
1658 could be taken as part of it,
1659 output `\ ' to prevent that. */
1b62edd6
KH
1660 if (need_nonhex)
1661 {
1662 need_nonhex = 0;
1663 if ((c >= 'a' && c <= 'f')
453fa987 1664 || (c >= 'A' && c <= 'F')
1b62edd6
KH
1665 || (c >= '0' && c <= '9'))
1666 strout ("\\ ", -1, -1, printcharfun, 0);
1667 }
453fa987 1668
38010d50
JB
1669 if (c == '\"' || c == '\\')
1670 PRINTCHAR ('\\');
1671 PRINTCHAR (c);
1672 }
1673 }
1674 PRINTCHAR ('\"');
7651e1f5 1675
d5db4077 1676 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
7651e1f5 1677 {
d5db4077 1678 traverse_intervals (STRING_INTERVALS (obj),
8bbfc258 1679 0, print_interval, printcharfun);
7651e1f5
RS
1680 PRINTCHAR (')');
1681 }
7651e1f5 1682
38010d50
JB
1683 UNGCPRO;
1684 }
ca0569ad 1685 break;
38010d50 1686
ca0569ad
RS
1687 case Lisp_Symbol:
1688 {
1689 register int confusing;
d5db4077
KR
1690 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1691 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
2190a05e 1692 register int c;
dc2a0b79
RS
1693 int i, i_byte, size_byte;
1694 Lisp_Object name;
1695
76d0b3ae 1696 name = SYMBOL_NAME (obj);
ca0569ad
RS
1697
1698 if (p != end && (*p == '-' || *p == '+')) p++;
1699 if (p == end)
1700 confusing = 0;
d27497e3
RS
1701 /* If symbol name begins with a digit, and ends with a digit,
1702 and contains nothing but digits and `e', it could be treated
1703 as a number. So set CONFUSING.
1704
1705 Symbols that contain periods could also be taken as numbers,
1706 but periods are always escaped, so we don't have to worry
1707 about them here. */
1708 else if (*p >= '0' && *p <= '9'
1709 && end[-1] >= '0' && end[-1] <= '9')
ca0569ad 1710 {
e837058b
RS
1711 while (p != end && ((*p >= '0' && *p <= '9')
1712 /* Needed for \2e10. */
1713 || *p == 'e'))
ca0569ad
RS
1714 p++;
1715 confusing = (end == p);
1716 }
d27497e3
RS
1717 else
1718 confusing = 0;
ca0569ad 1719
bf9f2aab 1720 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
081e0581 1721 {
081e0581
EN
1722 PRINTCHAR ('#');
1723 PRINTCHAR (':');
1724 }
1725
d5db4077 1726 size_byte = SBYTES (name);
dc2a0b79
RS
1727
1728 for (i = 0, i_byte = 0; i_byte < size_byte;)
ca0569ad 1729 {
6ddd6eee
RS
1730 /* Here, we must convert each multi-byte form to the
1731 corresponding character code before handing it to PRINTCHAR. */
eba90784 1732 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
ca0569ad 1733 QUIT;
09eddb56 1734
ca0569ad
RS
1735 if (escapeflag)
1736 {
09eddb56
RS
1737 if (c == '\"' || c == '\\' || c == '\''
1738 || c == ';' || c == '#' || c == '(' || c == ')'
1739 || c == ',' || c =='.' || c == '`'
1740 || c == '[' || c == ']' || c == '?' || c <= 040
1741 || confusing)
ca0569ad
RS
1742 PRINTCHAR ('\\'), confusing = 0;
1743 }
1744 PRINTCHAR (c);
1745 }
1746 }
1747 break;
1748
1749 case Lisp_Cons:
38010d50 1750 /* If deeper than spec'd depth, print placeholder. */
d4ae1f7e 1751 if (INTEGERP (Vprint_level)
38010d50 1752 && print_depth > XINT (Vprint_level))
dc2a0b79 1753 strout ("...", -1, -1, printcharfun, 0);
2f100b5c
EN
1754 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1755 && (EQ (XCAR (obj), Qquote)))
1756 {
1757 PRINTCHAR ('\'');
0f25ecc6 1758 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2f100b5c
EN
1759 }
1760 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1761 && (EQ (XCAR (obj), Qfunction)))
1762 {
1763 PRINTCHAR ('#');
1764 PRINTCHAR ('\'');
0f25ecc6 1765 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2f100b5c
EN
1766 }
1767 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
a22dec27
SM
1768 && ((EQ (XCAR (obj), Qbackquote))))
1769 {
1770 print_object (XCAR (obj), printcharfun, 0);
1771 new_backquote_output++;
1772 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1773 new_backquote_output--;
1774 }
1775 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1776 && new_backquote_output
2f100b5c
EN
1777 && ((EQ (XCAR (obj), Qbackquote)
1778 || EQ (XCAR (obj), Qcomma)
1779 || EQ (XCAR (obj), Qcomma_at)
1780 || EQ (XCAR (obj), Qcomma_dot))))
1781 {
0f25ecc6 1782 print_object (XCAR (obj), printcharfun, 0);
a22dec27 1783 new_backquote_output--;
0f25ecc6 1784 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
a22dec27 1785 new_backquote_output++;
2f100b5c 1786 }
e0f93814 1787 else
38010d50 1788 {
e0f93814 1789 PRINTCHAR ('(');
177c0ea7 1790
0330bb60
RS
1791 /* If the first element is a backquote form,
1792 print it old-style so it won't be misunderstood. */
1793 if (print_quoted && CONSP (XCAR (obj))
1794 && CONSP (XCDR (XCAR (obj)))
1795 && NILP (XCDR (XCDR (XCAR (obj))))
1796 && EQ (XCAR (XCAR (obj)), Qbackquote))
1797 {
1798 Lisp_Object tem;
1799 tem = XCAR (obj);
1800 PRINTCHAR ('(');
1801
1802 print_object (Qbackquote, printcharfun, 0);
1803 PRINTCHAR (' ');
1804
0330bb60 1805 print_object (XCAR (XCDR (tem)), printcharfun, 0);
0330bb60
RS
1806 PRINTCHAR (')');
1807
1808 obj = XCDR (obj);
1809 }
1810
38010d50 1811 {
42ac1ed4 1812 int print_length, i;
1eab22b5 1813 Lisp_Object halftail = obj;
e0f93814 1814
9ab8560d 1815 /* Negative values of print-length are invalid in CL.
42ac1ed4
GM
1816 Treat them like nil, as CMUCL does. */
1817 if (NATNUMP (Vprint_length))
1818 print_length = XFASTINT (Vprint_length);
1819 else
1820 print_length = 0;
1821
1822 i = 0;
e0f93814 1823 while (CONSP (obj))
38010d50 1824 {
1eab22b5 1825 /* Detect circular list. */
0f25ecc6 1826 if (NILP (Vprint_circle))
1eab22b5 1827 {
0f25ecc6
RS
1828 /* Simple but imcomplete way. */
1829 if (i != 0 && EQ (obj, halftail))
1830 {
1831 sprintf (buf, " . #%d", i / 2);
1832 strout (buf, -1, -1, printcharfun, 0);
1833 goto end_of_list;
1834 }
1835 }
1836 else
1837 {
1838 /* With the print-circle feature. */
1839 if (i != 0)
1840 {
1841 int i;
1842 for (i = 0; i < print_number_index; i++)
42ac1ed4
GM
1843 if (EQ (PRINT_NUMBER_OBJECT (Vprint_number_table, i),
1844 obj))
0f25ecc6
RS
1845 {
1846 if (NILP (PRINT_NUMBER_STATUS (Vprint_number_table, i)))
1847 {
1848 strout (" . ", 3, 3, printcharfun, 0);
1849 print_object (obj, printcharfun, escapeflag);
1850 }
1851 else
1852 {
1853 sprintf (buf, " . #%d#", i + 1);
1854 strout (buf, -1, -1, printcharfun, 0);
1855 }
1856 goto end_of_list;
1857 }
1858 }
1eab22b5 1859 }
177c0ea7 1860
e0f93814
KH
1861 if (i++)
1862 PRINTCHAR (' ');
177c0ea7 1863
f4fe72d5 1864 if (print_length && i > print_length)
e0f93814 1865 {
dc2a0b79 1866 strout ("...", 3, 3, printcharfun, 0);
0f25ecc6 1867 goto end_of_list;
e0f93814 1868 }
177c0ea7 1869
0f25ecc6 1870 print_object (XCAR (obj), printcharfun, escapeflag);
177c0ea7 1871
2f100b5c 1872 obj = XCDR (obj);
1eab22b5
RS
1873 if (!(i & 1))
1874 halftail = XCDR (halftail);
38010d50 1875 }
38010d50 1876 }
42ac1ed4
GM
1877
1878 /* OBJ non-nil here means it's the end of a dotted list. */
2f100b5c 1879 if (!NILP (obj))
e0f93814 1880 {
dc2a0b79 1881 strout (" . ", 3, 3, printcharfun, 0);
0f25ecc6 1882 print_object (obj, printcharfun, escapeflag);
e0f93814 1883 }
177c0ea7 1884
0f25ecc6 1885 end_of_list:
e0f93814 1886 PRINTCHAR (')');
38010d50 1887 }
ca0569ad
RS
1888 break;
1889
1890 case Lisp_Vectorlike:
1891 if (PROCESSP (obj))
1892 {
1893 if (escapeflag)
1894 {
dc2a0b79 1895 strout ("#<process ", -1, -1, printcharfun, 0);
ca0569ad
RS
1896 print_string (XPROCESS (obj)->name, printcharfun);
1897 PRINTCHAR ('>');
1898 }
1899 else
1900 print_string (XPROCESS (obj)->name, printcharfun);
1901 }
ed2c35ef
RS
1902 else if (BOOL_VECTOR_P (obj))
1903 {
1904 register int i;
1905 register unsigned char c;
1906 struct gcpro gcpro1;
ed2c35ef 1907 int size_in_chars
4b5af5e4
AS
1908 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1909 / BOOL_VECTOR_BITS_PER_CHAR);
ed2c35ef
RS
1910
1911 GCPRO1 (obj);
1912
1913 PRINTCHAR ('#');
1914 PRINTCHAR ('&');
474f84d9 1915 sprintf (buf, "%ld", (long) XBOOL_VECTOR (obj)->size);
dc2a0b79 1916 strout (buf, -1, -1, printcharfun, 0);
ed2c35ef 1917 PRINTCHAR ('\"');
a40384bc 1918
42ac1ed4 1919 /* Don't print more characters than the specified maximum.
9ab8560d 1920 Negative values of print-length are invalid. Treat them
42ac1ed4
GM
1921 like a print-length of nil. */
1922 if (NATNUMP (Vprint_length)
1923 && XFASTINT (Vprint_length) < size_in_chars)
1924 size_in_chars = XFASTINT (Vprint_length);
a40384bc 1925
ed2c35ef
RS
1926 for (i = 0; i < size_in_chars; i++)
1927 {
1928 QUIT;
1929 c = XBOOL_VECTOR (obj)->data[i];
1930 if (c == '\n' && print_escape_newlines)
1931 {
1932 PRINTCHAR ('\\');
1933 PRINTCHAR ('n');
1934 }
1935 else if (c == '\f' && print_escape_newlines)
1936 {
1937 PRINTCHAR ('\\');
1938 PRINTCHAR ('f');
1939 }
4b5af5e4
AS
1940 else if (c > '\177')
1941 {
1942 /* Use octal escapes to avoid encoding issues. */
1943 PRINTCHAR ('\\');
1944 PRINTCHAR ('0' + ((c >> 6) & 3));
1945 PRINTCHAR ('0' + ((c >> 3) & 7));
1946 PRINTCHAR ('0' + (c & 7));
1947 }
ed2c35ef
RS
1948 else
1949 {
1950 if (c == '\"' || c == '\\')
1951 PRINTCHAR ('\\');
1952 PRINTCHAR (c);
1953 }
1954 }
1955 PRINTCHAR ('\"');
1956
1957 UNGCPRO;
1958 }
ca0569ad
RS
1959 else if (SUBRP (obj))
1960 {
dc2a0b79
RS
1961 strout ("#<subr ", -1, -1, printcharfun, 0);
1962 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun, 0);
ca0569ad
RS
1963 PRINTCHAR ('>');
1964 }
ca0569ad
RS
1965 else if (WINDOWP (obj))
1966 {
dc2a0b79 1967 strout ("#<window ", -1, -1, printcharfun, 0);
474f84d9 1968 sprintf (buf, "%ld", (long) XFASTINT (XWINDOW (obj)->sequence_number));
dc2a0b79 1969 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
1970 if (!NILP (XWINDOW (obj)->buffer))
1971 {
dc2a0b79 1972 strout (" on ", -1, -1, printcharfun, 0);
ca0569ad
RS
1973 print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
1974 }
1975 PRINTCHAR ('>');
1976 }
7eb03302
GM
1977 else if (HASH_TABLE_P (obj))
1978 {
1979 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1980 strout ("#<hash-table", -1, -1, printcharfun, 0);
1981 if (SYMBOLP (h->test))
1982 {
1983 PRINTCHAR (' ');
1984 PRINTCHAR ('\'');
d5db4077 1985 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun, 0);
7eb03302 1986 PRINTCHAR (' ');
d5db4077 1987 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun, 0);
7eb03302 1988 PRINTCHAR (' ');
474f84d9
AS
1989 sprintf (buf, "%ld/%ld", (long) XFASTINT (h->count),
1990 (long) XVECTOR (h->next)->size);
7eb03302
GM
1991 strout (buf, -1, -1, printcharfun, 0);
1992 }
1993 sprintf (buf, " 0x%lx", (unsigned long) h);
1994 strout (buf, -1, -1, printcharfun, 0);
1995 PRINTCHAR ('>');
1996 }
908b0ae5
RS
1997 else if (BUFFERP (obj))
1998 {
1999 if (NILP (XBUFFER (obj)->name))
dc2a0b79 2000 strout ("#<killed buffer>", -1, -1, printcharfun, 0);
908b0ae5
RS
2001 else if (escapeflag)
2002 {
dc2a0b79 2003 strout ("#<buffer ", -1, -1, printcharfun, 0);
908b0ae5
RS
2004 print_string (XBUFFER (obj)->name, printcharfun);
2005 PRINTCHAR ('>');
2006 }
2007 else
2008 print_string (XBUFFER (obj)->name, printcharfun);
2009 }
ca0569ad
RS
2010 else if (WINDOW_CONFIGURATIONP (obj))
2011 {
dc2a0b79 2012 strout ("#<window-configuration>", -1, -1, printcharfun, 0);
ca0569ad 2013 }
ca0569ad
RS
2014 else if (FRAMEP (obj))
2015 {
2016 strout ((FRAME_LIVE_P (XFRAME (obj))
2017 ? "#<frame " : "#<dead frame "),
dc2a0b79 2018 -1, -1, printcharfun, 0);
ca0569ad 2019 print_string (XFRAME (obj)->name, printcharfun);
aab3aa14 2020 sprintf (buf, " 0x%lx", (unsigned long) (XFRAME (obj)));
dc2a0b79 2021 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
2022 PRINTCHAR ('>');
2023 }
ca0569ad
RS
2024 else
2025 {
3164abe6 2026 EMACS_INT size = XVECTOR (obj)->size;
ca0569ad
RS
2027 if (COMPILEDP (obj))
2028 {
2029 PRINTCHAR ('#');
2030 size &= PSEUDOVECTOR_SIZE_MASK;
2031 }
ed2c35ef
RS
2032 if (CHAR_TABLE_P (obj))
2033 {
2034 /* We print a char-table as if it were a vector,
2035 lumping the parent and default slots in with the
2036 character slots. But we add #^ as a prefix. */
2037 PRINTCHAR ('#');
2038 PRINTCHAR ('^');
3701b5de
KH
2039 if (SUB_CHAR_TABLE_P (obj))
2040 PRINTCHAR ('^');
ed2c35ef
RS
2041 size &= PSEUDOVECTOR_SIZE_MASK;
2042 }
00d76abc
KH
2043 if (size & PSEUDOVECTOR_FLAG)
2044 goto badtype;
ca0569ad
RS
2045
2046 PRINTCHAR ('[');
38010d50 2047 {
ca0569ad
RS
2048 register int i;
2049 register Lisp_Object tem;
d6ac884e 2050 int real_size = size;
a40384bc
RS
2051
2052 /* Don't print more elements than the specified maximum. */
42ac1ed4
GM
2053 if (NATNUMP (Vprint_length)
2054 && XFASTINT (Vprint_length) < size)
2055 size = XFASTINT (Vprint_length);
a40384bc 2056
ca0569ad
RS
2057 for (i = 0; i < size; i++)
2058 {
2059 if (i) PRINTCHAR (' ');
2060 tem = XVECTOR (obj)->contents[i];
0f25ecc6 2061 print_object (tem, printcharfun, escapeflag);
ca0569ad 2062 }
d6ac884e
KH
2063 if (size < real_size)
2064 strout (" ...", 4, 4, printcharfun, 0);
38010d50 2065 }
ca0569ad
RS
2066 PRINTCHAR (']');
2067 }
2068 break;
2069
ca0569ad 2070 case Lisp_Misc:
5db20f08 2071 switch (XMISCTYPE (obj))
38010d50 2072 {
00d76abc 2073 case Lisp_Misc_Marker:
dc2a0b79 2074 strout ("#<marker ", -1, -1, printcharfun, 0);
087e3c46
KH
2075 /* Do you think this is necessary? */
2076 if (XMARKER (obj)->insertion_type != 0)
210ebd3d 2077 strout ("(moves after insertion) ", -1, -1, printcharfun, 0);
cd3587c1 2078 if (! XMARKER (obj)->buffer)
dc2a0b79 2079 strout ("in no buffer", -1, -1, printcharfun, 0);
ca0569ad
RS
2080 else
2081 {
2082 sprintf (buf, "at %d", marker_position (obj));
dc2a0b79
RS
2083 strout (buf, -1, -1, printcharfun, 0);
2084 strout (" in ", -1, -1, printcharfun, 0);
ca0569ad
RS
2085 print_string (XMARKER (obj)->buffer->name, printcharfun);
2086 }
38010d50 2087 PRINTCHAR ('>');
908b0ae5 2088 break;
00d76abc
KH
2089
2090 case Lisp_Misc_Overlay:
dc2a0b79 2091 strout ("#<overlay ", -1, -1, printcharfun, 0);
cd3587c1 2092 if (! XMARKER (OVERLAY_START (obj))->buffer)
dc2a0b79 2093 strout ("in no buffer", -1, -1, printcharfun, 0);
ca0569ad
RS
2094 else
2095 {
2096 sprintf (buf, "from %d to %d in ",
2097 marker_position (OVERLAY_START (obj)),
2098 marker_position (OVERLAY_END (obj)));
dc2a0b79 2099 strout (buf, -1, -1, printcharfun, 0);
ca0569ad
RS
2100 print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
2101 printcharfun);
2102 }
2103 PRINTCHAR ('>');
908b0ae5 2104 break;
00d76abc
KH
2105
2106 /* Remaining cases shouldn't happen in normal usage, but let's print
2107 them anyway for the benefit of the debugger. */
2108 case Lisp_Misc_Free:
dc2a0b79 2109 strout ("#<misc free cell>", -1, -1, printcharfun, 0);
00d76abc
KH
2110 break;
2111
2112 case Lisp_Misc_Intfwd:
474f84d9 2113 sprintf (buf, "#<intfwd to %ld>", (long) *XINTFWD (obj)->intvar);
dc2a0b79 2114 strout (buf, -1, -1, printcharfun, 0);
00d76abc
KH
2115 break;
2116
2117 case Lisp_Misc_Boolfwd:
2118 sprintf (buf, "#<boolfwd to %s>",
2119 (*XBOOLFWD (obj)->boolvar ? "t" : "nil"));
dc2a0b79 2120 strout (buf, -1, -1, printcharfun, 0);
00d76abc
KH
2121 break;
2122
2123 case Lisp_Misc_Objfwd:
dc2a0b79 2124 strout ("#<objfwd to ", -1, -1, printcharfun, 0);
0f25ecc6 2125 print_object (*XOBJFWD (obj)->objvar, printcharfun, escapeflag);
00d76abc
KH
2126 PRINTCHAR ('>');
2127 break;
2128
2129 case Lisp_Misc_Buffer_Objfwd:
dc2a0b79 2130 strout ("#<buffer_objfwd to ", -1, -1, printcharfun, 0);
f6cd0527
GM
2131 print_object (PER_BUFFER_VALUE (current_buffer,
2132 XBUFFER_OBJFWD (obj)->offset),
c3279ad4 2133 printcharfun, escapeflag);
3ac613c1
KH
2134 PRINTCHAR ('>');
2135 break;
2136
fb917148 2137 case Lisp_Misc_Kboard_Objfwd:
dc2a0b79 2138 strout ("#<kboard_objfwd to ", -1, -1, printcharfun, 0);
cd3587c1
AS
2139 print_object (*(Lisp_Object *) ((char *) current_kboard
2140 + XKBOARD_OBJFWD (obj)->offset),
c3279ad4 2141 printcharfun, escapeflag);
00d76abc
KH
2142 PRINTCHAR ('>');
2143 break;
2144
2145 case Lisp_Misc_Buffer_Local_Value:
dc2a0b79 2146 strout ("#<buffer_local_value ", -1, -1, printcharfun, 0);
00d76abc
KH
2147 goto do_buffer_local;
2148 case Lisp_Misc_Some_Buffer_Local_Value:
dc2a0b79 2149 strout ("#<some_buffer_local_value ", -1, -1, printcharfun, 0);
00d76abc 2150 do_buffer_local:
dc2a0b79 2151 strout ("[realvalue] ", -1, -1, printcharfun, 0);
0f25ecc6
RS
2152 print_object (XBUFFER_LOCAL_VALUE (obj)->realvalue,
2153 printcharfun, escapeflag);
03153771
RS
2154 if (XBUFFER_LOCAL_VALUE (obj)->found_for_buffer)
2155 strout ("[local in buffer] ", -1, -1, printcharfun, 0);
2156 else
2157 strout ("[buffer] ", -1, -1, printcharfun, 0);
0f25ecc6
RS
2158 print_object (XBUFFER_LOCAL_VALUE (obj)->buffer,
2159 printcharfun, escapeflag);
03153771
RS
2160 if (XBUFFER_LOCAL_VALUE (obj)->check_frame)
2161 {
2162 if (XBUFFER_LOCAL_VALUE (obj)->found_for_frame)
2163 strout ("[local in frame] ", -1, -1, printcharfun, 0);
2164 else
2165 strout ("[frame] ", -1, -1, printcharfun, 0);
0f25ecc6
RS
2166 print_object (XBUFFER_LOCAL_VALUE (obj)->frame,
2167 printcharfun, escapeflag);
03153771 2168 }
dc2a0b79 2169 strout ("[alist-elt] ", -1, -1, printcharfun, 0);
94b342ce 2170 print_object (XCAR (XBUFFER_LOCAL_VALUE (obj)->cdr),
0f25ecc6 2171 printcharfun, escapeflag);
dc2a0b79 2172 strout ("[default-value] ", -1, -1, printcharfun, 0);
94b342ce 2173 print_object (XCDR (XBUFFER_LOCAL_VALUE (obj)->cdr),
0f25ecc6 2174 printcharfun, escapeflag);
00d76abc
KH
2175 PRINTCHAR ('>');
2176 break;
2177
c069fee4
KS
2178 case Lisp_Misc_Save_Value:
2179 strout ("#<save_value ", -1, -1, printcharfun, 0);
44455197 2180 sprintf(buf, "ptr=0x%08lx int=%d",
c069fee4
KS
2181 (unsigned long) XSAVE_VALUE (obj)->pointer,
2182 XSAVE_VALUE (obj)->integer);
2183 strout (buf, -1, -1, printcharfun, 0);
2184 PRINTCHAR ('>');
2185 break;
2186
00d76abc
KH
2187 default:
2188 goto badtype;
e0f93814 2189 }
00d76abc 2190 break;
ca0569ad
RS
2191
2192 default:
00d76abc 2193 badtype:
ca0569ad
RS
2194 {
2195 /* We're in trouble if this happens!
2196 Probably should just abort () */
dc2a0b79 2197 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun, 0);
00d76abc 2198 if (MISCP (obj))
5db20f08 2199 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
00d76abc
KH
2200 else if (VECTORLIKEP (obj))
2201 sprintf (buf, "(PVEC 0x%08x)", (int) XVECTOR (obj)->size);
2202 else
2203 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
dc2a0b79 2204 strout (buf, -1, -1, printcharfun, 0);
ca0569ad 2205 strout (" Save your buffers immediately and please report this bug>",
dc2a0b79 2206 -1, -1, printcharfun, 0);
ca0569ad 2207 }
38010d50
JB
2208 }
2209
2210 print_depth--;
2211}
2212\f
7651e1f5
RS
2213
2214/* Print a description of INTERVAL using PRINTCHARFUN.
2215 This is part of printing a string that has text properties. */
2216
2217void
2218print_interval (interval, printcharfun)
2219 INTERVAL interval;
2220 Lisp_Object printcharfun;
2221{
30503c0b 2222 PRINTCHAR (' ');
0f25ecc6 2223 print_object (make_number (interval->position), printcharfun, 1);
7651e1f5 2224 PRINTCHAR (' ');
0f25ecc6 2225 print_object (make_number (interval->position + LENGTH (interval)),
cd3587c1 2226 printcharfun, 1);
7651e1f5 2227 PRINTCHAR (' ');
0f25ecc6 2228 print_object (interval->plist, printcharfun, 1);
7651e1f5
RS
2229}
2230
7651e1f5 2231\f
38010d50
JB
2232void
2233syms_of_print ()
2234{
d9c21094
RS
2235 Qtemp_buffer_setup_hook = intern ("temp-buffer-setup-hook");
2236 staticpro (&Qtemp_buffer_setup_hook);
2237
38010d50 2238 DEFVAR_LISP ("standard-output", &Vstandard_output,
8c1a1077
PJ
2239 doc: /* Output stream `print' uses by default for outputting a character.
2240This may be any function of one argument.
2241It may also be a buffer (output is inserted before point)
2242or a marker (output is inserted and the marker is advanced)
2243or the symbol t (output appears in the echo area). */);
38010d50
JB
2244 Vstandard_output = Qt;
2245 Qstandard_output = intern ("standard-output");
2246 staticpro (&Qstandard_output);
2247
38010d50 2248 DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
8c1a1077
PJ
2249 doc: /* The format descriptor string used to print floats.
2250This is a %-spec like those accepted by `printf' in C,
2251but with some restrictions. It must start with the two characters `%.'.
2252After that comes an integer precision specification,
2253and then a letter which controls the format.
2254The letters allowed are `e', `f' and `g'.
2255Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2256Use `f' for decimal point notation \"DIGITS.DIGITS\".
2257Use `g' to choose the shorter of those two formats for the number at hand.
2258The precision in any of these cases is the number of digits following
2259the decimal point. With `f', a precision of 0 means to omit the
2260decimal point. 0 is not allowed with `e' or `g'.
2261
2262A value of nil means to use the shortest notation
2263that represents the number without losing information. */);
38010d50
JB
2264 Vfloat_output_format = Qnil;
2265 Qfloat_output_format = intern ("float-output-format");
2266 staticpro (&Qfloat_output_format);
38010d50
JB
2267
2268 DEFVAR_LISP ("print-length", &Vprint_length,
8c1a1077
PJ
2269 doc: /* Maximum length of list to print before abbreviating.
2270A value of nil means no limit. See also `eval-expression-print-length'. */);
38010d50
JB
2271 Vprint_length = Qnil;
2272
2273 DEFVAR_LISP ("print-level", &Vprint_level,
8c1a1077
PJ
2274 doc: /* Maximum depth of list nesting to print before abbreviating.
2275A value of nil means no limit. See also `eval-expression-print-level'. */);
38010d50
JB
2276 Vprint_level = Qnil;
2277
2278 DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
8c1a1077
PJ
2279 doc: /* Non-nil means print newlines in strings as `\\n'.
2280Also print formfeeds as `\\f'. */);
38010d50
JB
2281 print_escape_newlines = 0;
2282
38940e93 2283 DEFVAR_BOOL ("print-escape-nonascii", &print_escape_nonascii,
8c1a1077
PJ
2284 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2285\(OOO is the octal representation of the character code.)
249c0f71
RS
2286Only single-byte characters are affected, and only in `prin1'.
2287When the output goes in a multibyte buffer, this feature is
2288enabled regardless of the value of the variable. */);
38940e93
RS
2289 print_escape_nonascii = 0;
2290
835d0be6 2291 DEFVAR_BOOL ("print-escape-multibyte", &print_escape_multibyte,
8c1a1077
PJ
2292 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2293\(XXXX is the hex representation of the character code.)
2294This affects only `prin1'. */);
835d0be6
RS
2295 print_escape_multibyte = 0;
2296
2f100b5c 2297 DEFVAR_BOOL ("print-quoted", &print_quoted,
8c1a1077
PJ
2298 doc: /* Non-nil means print quoted forms with reader syntax.
2299I.e., (quote foo) prints as 'foo, (function foo) as #'foo, and backquoted
2300forms print as in the new syntax. */);
2f100b5c
EN
2301 print_quoted = 0;
2302
e0f69431 2303 DEFVAR_LISP ("print-gensym", &Vprint_gensym,
8c1a1077
PJ
2304 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2305I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2306When the uninterned symbol appears within a recursive data structure,
2307and the symbol appears more than once, in addition use the #N# and #N=
2308constructs as needed, so that multiple references to the same symbol are
2309shared once again when the text is read back. */);
e0f69431
RS
2310 Vprint_gensym = Qnil;
2311
0f25ecc6 2312 DEFVAR_LISP ("print-circle", &Vprint_circle,
8c1a1077
PJ
2313 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2314If nil, printing proceeds recursively and may lead to
2315`max-lisp-eval-depth' being exceeded or an error may occur:
2316\"Apparently circular structure being printed.\" Also see
2317`print-length' and `print-level'.
2318If non-nil, shared substructures anywhere in the structure are printed
2319with `#N=' before the first occurrence (in the order of the print
2320representation) and `#N#' in place of each subsequent occurrence,
2321where N is a positive decimal integer. */);
0f25ecc6
RS
2322 Vprint_circle = Qnil;
2323
2324 DEFVAR_LISP ("print-continuous-numbering", &Vprint_continuous_numbering,
8c1a1077
PJ
2325 doc: /* *Non-nil means number continuously across print calls.
2326This affects the numbers printed for #N= labels and #M# references.
2327See also `print-circle', `print-gensym', and `print-number-table'.
2328This variable should not be set with `setq'; bind it with a `let' instead. */);
0f25ecc6
RS
2329 Vprint_continuous_numbering = Qnil;
2330
2331 DEFVAR_LISP ("print-number-table", &Vprint_number_table,
8c1a1077
PJ
2332 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2333The Lisp printer uses this vector to detect Lisp objects referenced more
e6d4cddd
RS
2334than once.
2335
2336When you bind `print-continuous-numbering' to t, you should probably
2337also bind `print-number-table' to nil. This ensures that the value of
2338`print-number-table' can be garbage-collected once the printing is
2339done. If all elements of `print-number-table' are nil, it means that
2340the printing done so far has not found any shared structure or objects
2341that need to be recorded in the table. */);
0f25ecc6 2342 Vprint_number_table = Qnil;
081e0581 2343
38010d50
JB
2344 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2345 staticpro (&Vprin1_to_string_buffer);
2346
2347 defsubr (&Sprin1);
2348 defsubr (&Sprin1_to_string);
113620cc 2349 defsubr (&Serror_message_string);
38010d50
JB
2350 defsubr (&Sprinc);
2351 defsubr (&Sprint);
2352 defsubr (&Sterpri);
2353 defsubr (&Swrite_char);
2354 defsubr (&Sexternal_debugging_output);
95e52d88 2355#ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
6a1ff3ba 2356 defsubr (&Sredirect_debugging_output);
95e52d88 2357#endif
38010d50
JB
2358
2359 Qexternal_debugging_output = intern ("external-debugging-output");
2360 staticpro (&Qexternal_debugging_output);
2361
2f100b5c
EN
2362 Qprint_escape_newlines = intern ("print-escape-newlines");
2363 staticpro (&Qprint_escape_newlines);
2364
835d0be6
RS
2365 Qprint_escape_multibyte = intern ("print-escape-multibyte");
2366 staticpro (&Qprint_escape_multibyte);
2367
2368 Qprint_escape_nonascii = intern ("print-escape-nonascii");
2369 staticpro (&Qprint_escape_nonascii);
2370
38010d50 2371 defsubr (&Swith_output_to_temp_buffer);
38010d50 2372}
ab5796a9
MB
2373
2374/* arch-tag: bc797170-94ae-41de-86e3-75e20f8f7a39
2375 (do not change this comment) */