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