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