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