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