Fix Bug#20943.
[emacs.git] / src / print.c
blobf396151eaa1c82dbebe8011c116f4ed01a3313c2
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2015 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include "sysstdio.h"
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "process.h"
33 #include "dispextern.h"
34 #include "disptab.h"
35 #include "termchar.h"
36 #include "intervals.h"
37 #include "blockinput.h"
38 #include "termhooks.h" /* For struct terminal. */
39 #include "font.h"
41 #include <c-ctype.h>
42 #include <float.h>
43 #include <ftoastr.h>
45 /* Avoid actual stack overflow in print. */
46 static ptrdiff_t print_depth;
48 /* Level of nesting inside outputting backquote in new style. */
49 static ptrdiff_t new_backquote_output;
51 /* Detect most circularities to print finite output. */
52 #define PRINT_CIRCLE 200
53 static Lisp_Object being_printed[PRINT_CIRCLE];
55 /* Last char printed to stdout by printchar. */
56 static unsigned int printchar_stdout_last;
58 /* When printing into a buffer, first we put the text in this
59 block, then insert it all at once. */
60 static char *print_buffer;
62 /* Size allocated in print_buffer. */
63 static ptrdiff_t print_buffer_size;
64 /* Chars stored in print_buffer. */
65 static ptrdiff_t print_buffer_pos;
66 /* Bytes stored in print_buffer. */
67 static ptrdiff_t print_buffer_pos_byte;
69 /* Vprint_number_table is a table, that keeps objects that are going to
70 be printed, to allow use of #n= and #n# to express sharing.
71 For any given object, the table can give the following values:
72 t the object will be printed only once.
73 -N the object will be printed several times and will take number N.
74 N the object has been printed so we can refer to it as #N#.
75 print_number_index holds the largest N already used.
76 N has to be striclty larger than 0 since we need to distinguish -N. */
77 static ptrdiff_t print_number_index;
78 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
80 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
81 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
84 /* Low level output routines for characters and strings. */
86 /* Lisp functions to do output using a stream
87 must have the stream in a variable called printcharfun
88 and must start with PRINTPREPARE, end with PRINTFINISH.
89 Use printchar to output one character,
90 or call strout to output a block of characters. */
92 #define PRINTPREPARE \
93 struct buffer *old = current_buffer; \
94 ptrdiff_t old_point = -1, start_point = -1; \
95 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
96 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
97 bool free_print_buffer = 0; \
98 bool multibyte \
99 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
100 Lisp_Object original = printcharfun; \
101 if (NILP (printcharfun)) printcharfun = Qt; \
102 if (BUFFERP (printcharfun)) \
104 if (XBUFFER (printcharfun) != current_buffer) \
105 Fset_buffer (printcharfun); \
106 printcharfun = Qnil; \
108 if (MARKERP (printcharfun)) \
110 ptrdiff_t marker_pos; \
111 if (! XMARKER (printcharfun)->buffer) \
112 error ("Marker does not point anywhere"); \
113 if (XMARKER (printcharfun)->buffer != current_buffer) \
114 set_buffer_internal (XMARKER (printcharfun)->buffer); \
115 marker_pos = marker_position (printcharfun); \
116 if (marker_pos < BEGV || marker_pos > ZV) \
117 signal_error ("Marker is outside the accessible " \
118 "part of the buffer", printcharfun); \
119 old_point = PT; \
120 old_point_byte = PT_BYTE; \
121 SET_PT_BOTH (marker_pos, \
122 marker_byte_position (printcharfun)); \
123 start_point = PT; \
124 start_point_byte = PT_BYTE; \
125 printcharfun = Qnil; \
127 if (NILP (printcharfun)) \
129 Lisp_Object string; \
130 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
131 && ! print_escape_multibyte) \
132 specbind (Qprint_escape_multibyte, Qt); \
133 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
134 && ! print_escape_nonascii) \
135 specbind (Qprint_escape_nonascii, Qt); \
136 if (print_buffer != 0) \
138 string = make_string_from_bytes (print_buffer, \
139 print_buffer_pos, \
140 print_buffer_pos_byte); \
141 record_unwind_protect (print_unwind, string); \
143 else \
145 int new_size = 1000; \
146 print_buffer = xmalloc (new_size); \
147 print_buffer_size = new_size; \
148 free_print_buffer = 1; \
150 print_buffer_pos = 0; \
151 print_buffer_pos_byte = 0; \
153 if (EQ (printcharfun, Qt) && ! noninteractive) \
154 setup_echo_area_for_printing (multibyte);
156 #define PRINTFINISH \
157 if (NILP (printcharfun)) \
159 if (print_buffer_pos != print_buffer_pos_byte \
160 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
162 USE_SAFE_ALLOCA; \
163 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
164 copy_text ((unsigned char *) print_buffer, temp, \
165 print_buffer_pos_byte, 1, 0); \
166 insert_1_both ((char *) temp, print_buffer_pos, \
167 print_buffer_pos, 0, 1, 0); \
168 SAFE_FREE (); \
170 else \
171 insert_1_both (print_buffer, print_buffer_pos, \
172 print_buffer_pos_byte, 0, 1, 0); \
173 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
175 if (free_print_buffer) \
177 xfree (print_buffer); \
178 print_buffer = 0; \
180 unbind_to (specpdl_count, Qnil); \
181 if (MARKERP (original)) \
182 set_marker_both (original, Qnil, PT, PT_BYTE); \
183 if (old_point >= 0) \
184 SET_PT_BOTH (old_point + (old_point >= start_point \
185 ? PT - start_point : 0), \
186 old_point_byte + (old_point_byte >= start_point_byte \
187 ? PT_BYTE - start_point_byte : 0)); \
188 set_buffer_internal (old);
190 /* This is used to restore the saved contents of print_buffer
191 when there is a recursive call to print. */
193 static void
194 print_unwind (Lisp_Object saved_text)
196 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
199 /* Print character CH to the stdio stream STREAM. */
201 static void
202 printchar_to_stream (unsigned int ch, FILE *stream)
204 Lisp_Object dv IF_LINT (= Qnil);
205 ptrdiff_t i = 0, n = 1;
207 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
209 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
210 if (VECTORP (dv))
212 n = ASIZE (dv);
213 goto next_char;
217 while (true)
219 if (ASCII_CHAR_P (ch))
221 putc (ch, stream);
222 #ifdef WINDOWSNT
223 /* Send the output to a debugger (nothing happens if there
224 isn't one). */
225 if (print_output_debug_flag && stream == stderr)
226 OutputDebugString ((char []) {ch, '\0'});
227 #endif
229 else
231 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
232 int len = CHAR_STRING (ch, mbstr);
233 Lisp_Object encoded_ch =
234 ENCODE_SYSTEM (make_multibyte_string ((char *) mbstr, 1, len));
236 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
237 #ifdef WINDOWSNT
238 if (print_output_debug_flag && stream == stderr)
239 OutputDebugString (SSDATA (encoded_ch));
240 #endif
243 i++;
245 next_char:
246 for (; i < n; i++)
247 if (CHARACTERP (AREF (dv, i)))
248 break;
249 if (! (i < n))
250 break;
251 ch = XFASTINT (AREF (dv, i));
255 /* Print character CH using method FUN. FUN nil means print to
256 print_buffer. FUN t means print to echo area or stdout if
257 non-interactive. If FUN is neither nil nor t, call FUN with CH as
258 argument. */
260 static void
261 printchar (unsigned int ch, Lisp_Object fun)
263 if (!NILP (fun) && !EQ (fun, Qt))
264 call1 (fun, make_number (ch));
265 else
267 unsigned char str[MAX_MULTIBYTE_LENGTH];
268 int len = CHAR_STRING (ch, str);
270 QUIT;
272 if (NILP (fun))
274 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
275 if (incr > 0)
276 print_buffer = xpalloc (print_buffer, &print_buffer_size,
277 incr, -1, 1);
278 memcpy (print_buffer + print_buffer_pos_byte, str, len);
279 print_buffer_pos += 1;
280 print_buffer_pos_byte += len;
282 else if (noninteractive)
284 printchar_stdout_last = ch;
285 if (DISP_TABLE_P (Vstandard_display_table))
286 printchar_to_stream (ch, stdout);
287 else
288 fwrite (str, 1, len, stdout);
289 noninteractive_need_newline = 1;
291 else
293 bool multibyte_p
294 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
296 setup_echo_area_for_printing (multibyte_p);
297 insert_char (ch);
298 message_dolog ((char *) str, len, 0, multibyte_p);
304 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
305 method PRINTCHARFUN. PRINTCHARFUN nil means output to
306 print_buffer. PRINTCHARFUN t means output to the echo area or to
307 stdout if non-interactive. If neither nil nor t, call Lisp
308 function PRINTCHARFUN for each character printed. MULTIBYTE
309 non-zero means PTR contains multibyte characters.
311 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
312 to data in a Lisp string. Otherwise that is not safe. */
314 static void
315 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
316 Lisp_Object printcharfun)
318 if (NILP (printcharfun))
320 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
321 if (incr > 0)
322 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
323 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
324 print_buffer_pos += size;
325 print_buffer_pos_byte += size_byte;
327 else if (noninteractive && EQ (printcharfun, Qt))
329 if (DISP_TABLE_P (Vstandard_display_table))
331 int len;
332 for (ptrdiff_t i = 0; i < size_byte; i += len)
334 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
335 len);
336 printchar_to_stream (ch, stdout);
339 else
340 fwrite (ptr, 1, size_byte, stdout);
342 noninteractive_need_newline = 1;
344 else if (EQ (printcharfun, Qt))
346 /* Output to echo area. We're trying to avoid a little overhead
347 here, that's the reason we don't call printchar to do the
348 job. */
349 int i;
350 bool multibyte_p
351 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
353 setup_echo_area_for_printing (multibyte_p);
354 message_dolog (ptr, size_byte, 0, multibyte_p);
356 if (size == size_byte)
358 for (i = 0; i < size; ++i)
359 insert_char ((unsigned char) *ptr++);
361 else
363 int len;
364 for (i = 0; i < size_byte; i += len)
366 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
367 len);
368 insert_char (ch);
372 else
374 /* PRINTCHARFUN is a Lisp function. */
375 ptrdiff_t i = 0;
377 if (size == size_byte)
379 while (i < size_byte)
381 int ch = ptr[i++];
382 printchar (ch, printcharfun);
385 else
387 while (i < size_byte)
389 /* Here, we must convert each multi-byte form to the
390 corresponding character code before handing it to
391 PRINTCHAR. */
392 int len;
393 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
394 len);
395 printchar (ch, printcharfun);
396 i += len;
402 /* Print the contents of a string STRING using PRINTCHARFUN.
403 It isn't safe to use strout in many cases,
404 because printing one char can relocate. */
406 static void
407 print_string (Lisp_Object string, Lisp_Object printcharfun)
409 if (EQ (printcharfun, Qt) || NILP (printcharfun))
411 ptrdiff_t chars;
413 if (print_escape_nonascii)
414 string = string_escape_byte8 (string);
416 if (STRING_MULTIBYTE (string))
417 chars = SCHARS (string);
418 else if (! print_escape_nonascii
419 && (EQ (printcharfun, Qt)
420 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
421 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
423 /* If unibyte string STRING contains 8-bit codes, we must
424 convert STRING to a multibyte string containing the same
425 character codes. */
426 Lisp_Object newstr;
427 ptrdiff_t bytes;
429 chars = SBYTES (string);
430 bytes = count_size_as_multibyte (SDATA (string), chars);
431 if (chars < bytes)
433 newstr = make_uninit_multibyte_string (chars, bytes);
434 memcpy (SDATA (newstr), SDATA (string), chars);
435 str_to_multibyte (SDATA (newstr), bytes, chars);
436 string = newstr;
439 else
440 chars = SBYTES (string);
442 if (EQ (printcharfun, Qt))
444 /* Output to echo area. */
445 ptrdiff_t nbytes = SBYTES (string);
447 /* Copy the string contents so that relocation of STRING by
448 GC does not cause trouble. */
449 USE_SAFE_ALLOCA;
450 char *buffer = SAFE_ALLOCA (nbytes);
451 memcpy (buffer, SDATA (string), nbytes);
453 strout (buffer, chars, nbytes, printcharfun);
455 SAFE_FREE ();
457 else
458 /* No need to copy, since output to print_buffer can't GC. */
459 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
461 else
463 /* Otherwise, string may be relocated by printing one char.
464 So re-fetch the string address for each character. */
465 ptrdiff_t i;
466 ptrdiff_t size = SCHARS (string);
467 ptrdiff_t size_byte = SBYTES (string);
468 struct gcpro gcpro1;
469 GCPRO1 (string);
470 if (size == size_byte)
471 for (i = 0; i < size; i++)
472 printchar (SREF (string, i), printcharfun);
473 else
474 for (i = 0; i < size_byte; )
476 /* Here, we must convert each multi-byte form to the
477 corresponding character code before handing it to PRINTCHAR. */
478 int len;
479 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
480 printchar (ch, printcharfun);
481 i += len;
483 UNGCPRO;
487 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
488 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
489 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
490 (Lisp_Object character, Lisp_Object printcharfun)
492 if (NILP (printcharfun))
493 printcharfun = Vstandard_output;
494 CHECK_NUMBER (character);
495 PRINTPREPARE;
496 printchar (XINT (character), printcharfun);
497 PRINTFINISH;
498 return character;
501 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
502 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
503 Do not use this on the contents of a Lisp string. */
505 static void
506 print_c_string (char const *string, Lisp_Object printcharfun)
508 ptrdiff_t len = strlen (string);
509 strout (string, len, len, printcharfun);
512 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
513 Do not use this on the contents of a Lisp string. */
515 static void
516 write_string_1 (const char *data, Lisp_Object printcharfun)
518 PRINTPREPARE;
519 print_c_string (data, printcharfun);
520 PRINTFINISH;
523 /* Used from outside of print.c to print a C unibyte
524 string at DATA on the default output stream.
525 Do not use this on the contents of a Lisp string. */
527 void
528 write_string (const char *data)
530 write_string_1 (data, Vstandard_output);
534 void
535 temp_output_buffer_setup (const char *bufname)
537 ptrdiff_t count = SPECPDL_INDEX ();
538 register struct buffer *old = current_buffer;
539 register Lisp_Object buf;
541 record_unwind_current_buffer ();
543 Fset_buffer (Fget_buffer_create (build_string (bufname)));
545 Fkill_all_local_variables ();
546 delete_all_overlays (current_buffer);
547 bset_directory (current_buffer, BVAR (old, directory));
548 bset_read_only (current_buffer, Qnil);
549 bset_filename (current_buffer, Qnil);
550 bset_undo_list (current_buffer, Qt);
551 eassert (current_buffer->overlays_before == NULL);
552 eassert (current_buffer->overlays_after == NULL);
553 bset_enable_multibyte_characters
554 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
555 specbind (Qinhibit_read_only, Qt);
556 specbind (Qinhibit_modification_hooks, Qt);
557 Ferase_buffer ();
558 XSETBUFFER (buf, current_buffer);
560 run_hook (Qtemp_buffer_setup_hook);
562 unbind_to (count, Qnil);
564 specbind (Qstandard_output, buf);
567 static void print (Lisp_Object, Lisp_Object, bool);
568 static void print_preprocess (Lisp_Object);
569 static void print_preprocess_string (INTERVAL, Lisp_Object);
570 static void print_object (Lisp_Object, Lisp_Object, bool);
572 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
573 doc: /* Output a newline to stream PRINTCHARFUN.
574 If ENSURE is non-nil only output a newline if not already at the
575 beginning of a line. Value is non-nil if a newline is printed.
576 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
577 (Lisp_Object printcharfun, Lisp_Object ensure)
579 Lisp_Object val;
581 if (NILP (printcharfun))
582 printcharfun = Vstandard_output;
583 PRINTPREPARE;
585 if (NILP (ensure))
586 val = Qt;
587 /* Difficult to check if at line beginning so abort. */
588 else if (FUNCTIONP (printcharfun))
589 signal_error ("Unsupported function argument", printcharfun);
590 else if (noninteractive && !NILP (printcharfun))
591 val = printchar_stdout_last == 10 ? Qnil : Qt;
592 else
593 val = NILP (Fbolp ()) ? Qt : Qnil;
595 if (!NILP (val))
596 printchar ('\n', printcharfun);
597 PRINTFINISH;
598 return val;
601 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
602 doc: /* Output the printed representation of OBJECT, any Lisp object.
603 Quoting characters are printed when needed to make output that `read'
604 can handle, whenever this is possible. For complex objects, the behavior
605 is controlled by `print-level' and `print-length', which see.
607 OBJECT is any of the Lisp data types: a number, a string, a symbol,
608 a list, a buffer, a window, a frame, etc.
610 A printed representation of an object is text which describes that object.
612 Optional argument PRINTCHARFUN is the output stream, which can be one
613 of these:
615 - a buffer, in which case output is inserted into that buffer at point;
616 - a marker, in which case output is inserted at marker's position;
617 - a function, in which case that function is called once for each
618 character of OBJECT's printed representation;
619 - a symbol, in which case that symbol's function definition is called; or
620 - t, in which case the output is displayed in the echo area.
622 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
623 is used instead. */)
624 (Lisp_Object object, Lisp_Object printcharfun)
626 if (NILP (printcharfun))
627 printcharfun = Vstandard_output;
628 PRINTPREPARE;
629 print (object, printcharfun, 1);
630 PRINTFINISH;
631 return object;
634 /* a buffer which is used to hold output being built by prin1-to-string */
635 Lisp_Object Vprin1_to_string_buffer;
637 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
638 doc: /* Return a string containing the printed representation of OBJECT.
639 OBJECT can be any Lisp object. This function outputs quoting characters
640 when necessary to make output that `read' can handle, whenever possible,
641 unless the optional second argument NOESCAPE is non-nil. For complex objects,
642 the behavior is controlled by `print-level' and `print-length', which see.
644 OBJECT is any of the Lisp data types: a number, a string, a symbol,
645 a list, a buffer, a window, a frame, etc.
647 A printed representation of an object is text which describes that object. */)
648 (Lisp_Object object, Lisp_Object noescape)
650 ptrdiff_t count = SPECPDL_INDEX ();
652 specbind (Qinhibit_modification_hooks, Qt);
654 /* Save and restore this: we are altering a buffer
655 but we don't want to deactivate the mark just for that.
656 No need for specbind, since errors deactivate the mark. */
657 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
658 bool prev_abort_on_gc = abort_on_gc;
659 abort_on_gc = true;
661 Lisp_Object printcharfun = Vprin1_to_string_buffer;
662 PRINTPREPARE;
663 print (object, printcharfun, NILP (noescape));
664 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
665 PRINTFINISH;
667 struct buffer *previous = current_buffer;
668 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
669 object = Fbuffer_string ();
670 if (SBYTES (object) == SCHARS (object))
671 STRING_SET_UNIBYTE (object);
673 /* Note that this won't make prepare_to_modify_buffer call
674 ask-user-about-supersession-threat because this buffer
675 does not visit a file. */
676 Ferase_buffer ();
677 set_buffer_internal (previous);
679 Vdeactivate_mark = save_deactivate_mark;
681 abort_on_gc = prev_abort_on_gc;
682 return unbind_to (count, object);
685 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
686 doc: /* Output the printed representation of OBJECT, any Lisp object.
687 No quoting characters are used; no delimiters are printed around
688 the contents of strings.
690 OBJECT is any of the Lisp data types: a number, a string, a symbol,
691 a list, a buffer, a window, a frame, etc.
693 A printed representation of an object is text which describes that object.
695 Optional argument PRINTCHARFUN is the output stream, which can be one
696 of these:
698 - a buffer, in which case output is inserted into that buffer at point;
699 - a marker, in which case output is inserted at marker's position;
700 - a function, in which case that function is called once for each
701 character of OBJECT's printed representation;
702 - a symbol, in which case that symbol's function definition is called; or
703 - t, in which case the output is displayed in the echo area.
705 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
706 is used instead. */)
707 (Lisp_Object object, Lisp_Object printcharfun)
709 if (NILP (printcharfun))
710 printcharfun = Vstandard_output;
711 PRINTPREPARE;
712 print (object, printcharfun, 0);
713 PRINTFINISH;
714 return object;
717 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
718 doc: /* Output the printed representation of OBJECT, with newlines around it.
719 Quoting characters are printed when needed to make output that `read'
720 can handle, whenever this is possible. For complex objects, the behavior
721 is controlled by `print-level' and `print-length', which see.
723 OBJECT is any of the Lisp data types: a number, a string, a symbol,
724 a list, a buffer, a window, a frame, etc.
726 A printed representation of an object is text which describes that object.
728 Optional argument PRINTCHARFUN is the output stream, which can be one
729 of these:
731 - a buffer, in which case output is inserted into that buffer at point;
732 - a marker, in which case output is inserted at marker's position;
733 - a function, in which case that function is called once for each
734 character of OBJECT's printed representation;
735 - a symbol, in which case that symbol's function definition is called; or
736 - t, in which case the output is displayed in the echo area.
738 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
739 is used instead. */)
740 (Lisp_Object object, Lisp_Object printcharfun)
742 struct gcpro gcpro1;
744 if (NILP (printcharfun))
745 printcharfun = Vstandard_output;
746 GCPRO1 (object);
747 PRINTPREPARE;
748 printchar ('\n', printcharfun);
749 print (object, printcharfun, 1);
750 printchar ('\n', printcharfun);
751 PRINTFINISH;
752 UNGCPRO;
753 return object;
756 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
757 doc: /* Write CHARACTER to stderr.
758 You can call print while debugging emacs, and pass it this function
759 to make it write to the debugging output. */)
760 (Lisp_Object character)
762 CHECK_NUMBER (character);
763 printchar_to_stream (XINT (character), stderr);
764 return character;
767 /* This function is never called. Its purpose is to prevent
768 print_output_debug_flag from being optimized away. */
770 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
771 void
772 debug_output_compilation_hack (bool x)
774 print_output_debug_flag = x;
777 #if defined (GNU_LINUX)
779 /* This functionality is not vitally important in general, so we rely on
780 non-portable ability to use stderr as lvalue. */
782 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
784 static FILE *initial_stderr_stream = NULL;
786 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
787 1, 2,
788 "FDebug output file: \nP",
789 doc: /* Redirect debugging output (stderr stream) to file FILE.
790 If FILE is nil, reset target to the initial stderr stream.
791 Optional arg APPEND non-nil (interactively, with prefix arg) means
792 append to existing target file. */)
793 (Lisp_Object file, Lisp_Object append)
795 if (initial_stderr_stream != NULL)
797 block_input ();
798 fclose (stderr);
799 unblock_input ();
801 stderr = initial_stderr_stream;
802 initial_stderr_stream = NULL;
804 if (STRINGP (file))
806 file = Fexpand_file_name (file, Qnil);
807 initial_stderr_stream = stderr;
808 stderr = emacs_fopen (SSDATA (file), NILP (append) ? "w" : "a");
809 if (stderr == NULL)
811 stderr = initial_stderr_stream;
812 initial_stderr_stream = NULL;
813 report_file_error ("Cannot open debugging output stream", file);
816 return Qnil;
818 #endif /* GNU_LINUX */
821 /* This is the interface for debugging printing. */
823 void
824 debug_print (Lisp_Object arg)
826 Fprin1 (arg, Qexternal_debugging_output);
827 fprintf (stderr, "\r\n");
830 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
831 void
832 safe_debug_print (Lisp_Object arg)
834 int valid = valid_lisp_object_p (arg);
836 if (valid > 0)
837 debug_print (arg);
838 else
840 EMACS_UINT n = XLI (arg);
841 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
842 !valid ? "INVALID" : "SOME",
848 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
849 1, 1, 0,
850 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
851 See Info anchor `(elisp)Definition of signal' for some details on how this
852 error message is constructed. */)
853 (Lisp_Object obj)
855 struct buffer *old = current_buffer;
856 Lisp_Object value;
857 struct gcpro gcpro1;
859 /* If OBJ is (error STRING), just return STRING.
860 That is not only faster, it also avoids the need to allocate
861 space here when the error is due to memory full. */
862 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
863 && CONSP (XCDR (obj))
864 && STRINGP (XCAR (XCDR (obj)))
865 && NILP (XCDR (XCDR (obj))))
866 return XCAR (XCDR (obj));
868 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
870 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
871 value = Fbuffer_string ();
873 GCPRO1 (value);
874 Ferase_buffer ();
875 set_buffer_internal (old);
876 UNGCPRO;
878 return value;
881 /* Print an error message for the error DATA onto Lisp output stream
882 STREAM (suitable for the print functions).
883 CONTEXT is a C string describing the context of the error.
884 CALLER is the Lisp function inside which the error was signaled. */
886 void
887 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
888 Lisp_Object caller)
890 Lisp_Object errname, errmsg, file_error, tail;
891 struct gcpro gcpro1;
893 if (context != 0)
894 write_string_1 (context, stream);
896 /* If we know from where the error was signaled, show it in
897 *Messages*. */
898 if (!NILP (caller) && SYMBOLP (caller))
900 Lisp_Object cname = SYMBOL_NAME (caller);
901 ptrdiff_t cnamelen = SBYTES (cname);
902 USE_SAFE_ALLOCA;
903 char *name = SAFE_ALLOCA (cnamelen);
904 memcpy (name, SDATA (cname), cnamelen);
905 message_dolog (name, cnamelen, 0, 0);
906 message_dolog (": ", 2, 0, 0);
907 SAFE_FREE ();
910 errname = Fcar (data);
912 if (EQ (errname, Qerror))
914 data = Fcdr (data);
915 if (!CONSP (data))
916 data = Qnil;
917 errmsg = Fcar (data);
918 file_error = Qnil;
920 else
922 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
923 errmsg = Fget (errname, Qerror_message);
924 file_error = Fmemq (Qfile_error, error_conditions);
927 /* Print an error message including the data items. */
929 tail = Fcdr_safe (data);
930 GCPRO1 (tail);
932 /* For file-error, make error message by concatenating
933 all the data items. They are all strings. */
934 if (!NILP (file_error) && CONSP (tail))
935 errmsg = XCAR (tail), tail = XCDR (tail);
938 const char *sep = ": ";
940 if (!STRINGP (errmsg))
941 write_string_1 ("peculiar error", stream);
942 else if (SCHARS (errmsg))
943 Fprinc (errmsg, stream);
944 else
945 sep = NULL;
947 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
949 Lisp_Object obj;
951 if (sep)
952 write_string_1 (sep, stream);
953 obj = XCAR (tail);
954 if (!NILP (file_error)
955 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
956 Fprinc (obj, stream);
957 else
958 Fprin1 (obj, stream);
962 UNGCPRO;
968 * The buffer should be at least as large as the max string size of the
969 * largest float, printed in the biggest notation. This is undoubtedly
970 * 20d float_output_format, with the negative of the C-constant "HUGE"
971 * from <math.h>.
973 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
975 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
976 * case of -1e307 in 20d float_output_format. What is one to do (short of
977 * re-writing _doprnt to be more sane)?
978 * -wsr
979 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
983 float_to_string (char *buf, double data)
985 char *cp;
986 int width;
987 int len;
989 /* Check for plus infinity in a way that won't lose
990 if there is no plus infinity. */
991 if (data == data / 2 && data > 1.0)
993 static char const infinity_string[] = "1.0e+INF";
994 strcpy (buf, infinity_string);
995 return sizeof infinity_string - 1;
997 /* Likewise for minus infinity. */
998 if (data == data / 2 && data < -1.0)
1000 static char const minus_infinity_string[] = "-1.0e+INF";
1001 strcpy (buf, minus_infinity_string);
1002 return sizeof minus_infinity_string - 1;
1004 /* Check for NaN in a way that won't fail if there are no NaNs. */
1005 if (! (data * 0.0 >= 0.0))
1007 /* Prepend "-" if the NaN's sign bit is negative.
1008 The sign bit of a double is the bit that is 1 in -0.0. */
1009 static char const NaN_string[] = "0.0e+NaN";
1010 int i;
1011 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1012 bool negative = 0;
1013 u_data.d = data;
1014 u_minus_zero.d = - 0.0;
1015 for (i = 0; i < sizeof (double); i++)
1016 if (u_data.c[i] & u_minus_zero.c[i])
1018 *buf = '-';
1019 negative = 1;
1020 break;
1023 strcpy (buf + negative, NaN_string);
1024 return negative + sizeof NaN_string - 1;
1027 if (NILP (Vfloat_output_format)
1028 || !STRINGP (Vfloat_output_format))
1029 lose:
1031 /* Generate the fewest number of digits that represent the
1032 floating point value without losing information. */
1033 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1034 /* The decimal point must be printed, or the byte compiler can
1035 get confused (Bug#8033). */
1036 width = 1;
1038 else /* oink oink */
1040 /* Check that the spec we have is fully valid.
1041 This means not only valid for printf,
1042 but meant for floats, and reasonable. */
1043 cp = SSDATA (Vfloat_output_format);
1045 if (cp[0] != '%')
1046 goto lose;
1047 if (cp[1] != '.')
1048 goto lose;
1050 cp += 2;
1052 /* Check the width specification. */
1053 width = -1;
1054 if ('0' <= *cp && *cp <= '9')
1056 width = 0;
1059 width = (width * 10) + (*cp++ - '0');
1060 if (DBL_DIG < width)
1061 goto lose;
1063 while (*cp >= '0' && *cp <= '9');
1065 /* A precision of zero is valid only for %f. */
1066 if (width == 0 && *cp != 'f')
1067 goto lose;
1070 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1071 goto lose;
1073 if (cp[1] != 0)
1074 goto lose;
1076 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1079 /* Make sure there is a decimal point with digit after, or an
1080 exponent, so that the value is readable as a float. But don't do
1081 this with "%.0f"; it's valid for that not to produce a decimal
1082 point. Note that width can be 0 only for %.0f. */
1083 if (width != 0)
1085 for (cp = buf; *cp; cp++)
1086 if ((*cp < '0' || *cp > '9') && *cp != '-')
1087 break;
1089 if (*cp == '.' && cp[1] == 0)
1091 cp[1] = '0';
1092 cp[2] = 0;
1093 len++;
1095 else if (*cp == 0)
1097 *cp++ = '.';
1098 *cp++ = '0';
1099 *cp++ = 0;
1100 len += 2;
1104 return len;
1108 static void
1109 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1111 new_backquote_output = 0;
1113 /* Reset print_number_index and Vprint_number_table only when
1114 the variable Vprint_continuous_numbering is nil. Otherwise,
1115 the values of these variables will be kept between several
1116 print functions. */
1117 if (NILP (Vprint_continuous_numbering)
1118 || NILP (Vprint_number_table))
1120 print_number_index = 0;
1121 Vprint_number_table = Qnil;
1124 /* Construct Vprint_number_table for print-gensym and print-circle. */
1125 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1127 /* Construct Vprint_number_table.
1128 This increments print_number_index for the objects added. */
1129 print_depth = 0;
1130 print_preprocess (obj);
1132 if (HASH_TABLE_P (Vprint_number_table))
1133 { /* Remove unnecessary objects, which appear only once in OBJ;
1134 that is, whose status is Qt. */
1135 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1136 ptrdiff_t i;
1138 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1139 if (!NILP (HASH_HASH (h, i))
1140 && EQ (HASH_VALUE (h, i), Qt))
1141 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1145 print_depth = 0;
1146 print_object (obj, printcharfun, escapeflag);
1149 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1150 (STRINGP (obj) || CONSP (obj) \
1151 || (VECTORLIKEP (obj) \
1152 && (VECTORP (obj) || COMPILEDP (obj) \
1153 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1154 || HASH_TABLE_P (obj) || FONTP (obj))) \
1155 || (! NILP (Vprint_gensym) \
1156 && SYMBOLP (obj) \
1157 && !SYMBOL_INTERNED_P (obj)))
1159 /* Construct Vprint_number_table according to the structure of OBJ.
1160 OBJ itself and all its elements will be added to Vprint_number_table
1161 recursively if it is a list, vector, compiled function, char-table,
1162 string (its text properties will be traced), or a symbol that has
1163 no obarray (this is for the print-gensym feature).
1164 The status fields of Vprint_number_table mean whether each object appears
1165 more than once in OBJ: Qnil at the first time, and Qt after that. */
1166 static void
1167 print_preprocess (Lisp_Object obj)
1169 int i;
1170 ptrdiff_t size;
1171 int loop_count = 0;
1172 Lisp_Object halftail;
1174 /* Avoid infinite recursion for circular nested structure
1175 in the case where Vprint_circle is nil. */
1176 if (NILP (Vprint_circle))
1178 /* Give up if we go so deep that print_object will get an error. */
1179 /* See similar code in print_object. */
1180 if (print_depth >= PRINT_CIRCLE)
1181 error ("Apparently circular structure being printed");
1183 for (i = 0; i < print_depth; i++)
1184 if (EQ (obj, being_printed[i]))
1185 return;
1186 being_printed[print_depth] = obj;
1189 print_depth++;
1190 halftail = obj;
1192 loop:
1193 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1195 if (!HASH_TABLE_P (Vprint_number_table))
1196 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1198 /* In case print-circle is nil and print-gensym is t,
1199 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1200 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1202 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1203 if (!NILP (num)
1204 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1205 always print the gensym with a number. This is a special for
1206 the lisp function byte-compile-output-docform. */
1207 || (!NILP (Vprint_continuous_numbering)
1208 && SYMBOLP (obj)
1209 && !SYMBOL_INTERNED_P (obj)))
1210 { /* OBJ appears more than once. Let's remember that. */
1211 if (!INTEGERP (num))
1213 print_number_index++;
1214 /* Negative number indicates it hasn't been printed yet. */
1215 Fputhash (obj, make_number (- print_number_index),
1216 Vprint_number_table);
1218 print_depth--;
1219 return;
1221 else
1222 /* OBJ is not yet recorded. Let's add to the table. */
1223 Fputhash (obj, Qt, Vprint_number_table);
1226 switch (XTYPE (obj))
1228 case Lisp_String:
1229 /* A string may have text properties, which can be circular. */
1230 traverse_intervals_noorder (string_intervals (obj),
1231 print_preprocess_string, Qnil);
1232 break;
1234 case Lisp_Cons:
1235 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1236 just as in print_object. */
1237 if (loop_count && EQ (obj, halftail))
1238 break;
1239 print_preprocess (XCAR (obj));
1240 obj = XCDR (obj);
1241 loop_count++;
1242 if (!(loop_count & 1))
1243 halftail = XCDR (halftail);
1244 goto loop;
1246 case Lisp_Vectorlike:
1247 size = ASIZE (obj);
1248 if (size & PSEUDOVECTOR_FLAG)
1249 size &= PSEUDOVECTOR_SIZE_MASK;
1250 for (i = (SUB_CHAR_TABLE_P (obj)
1251 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1252 print_preprocess (AREF (obj, i));
1253 if (HASH_TABLE_P (obj))
1254 { /* For hash tables, the key_and_value slot is past
1255 `size' because it needs to be marked specially in case
1256 the table is weak. */
1257 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1258 print_preprocess (h->key_and_value);
1260 break;
1262 default:
1263 break;
1266 print_depth--;
1269 static void
1270 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1272 print_preprocess (interval->plist);
1275 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1277 #define PRINT_STRING_NON_CHARSET_FOUND 1
1278 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1280 /* Bitwise or of the above macros. */
1281 static int print_check_string_result;
1283 static void
1284 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1286 Lisp_Object val;
1288 if (NILP (interval->plist)
1289 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1290 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1291 return;
1292 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1293 val = XCDR (XCDR (val)));
1294 if (! CONSP (val))
1296 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1297 return;
1299 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1301 if (! EQ (val, interval->plist)
1302 || CONSP (XCDR (XCDR (val))))
1303 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1305 if (NILP (Vprint_charset_text_property)
1306 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1308 int i, c;
1309 ptrdiff_t charpos = interval->position;
1310 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1311 Lisp_Object charset;
1313 charset = XCAR (XCDR (val));
1314 for (i = 0; i < LENGTH (interval); i++)
1316 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1317 if (! ASCII_CHAR_P (c)
1318 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1320 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1321 break;
1327 /* The value is (charset . nil). */
1328 static Lisp_Object print_prune_charset_plist;
1330 static Lisp_Object
1331 print_prune_string_charset (Lisp_Object string)
1333 print_check_string_result = 0;
1334 traverse_intervals (string_intervals (string), 0,
1335 print_check_string_charset_prop, string);
1336 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1338 string = Fcopy_sequence (string);
1339 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1341 if (NILP (print_prune_charset_plist))
1342 print_prune_charset_plist = list1 (Qcharset);
1343 Fremove_text_properties (make_number (0),
1344 make_number (SCHARS (string)),
1345 print_prune_charset_plist, string);
1347 else
1348 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1349 Qnil, string);
1351 return string;
1354 static void
1355 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1357 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1358 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1359 40))];
1361 QUIT;
1363 /* Detect circularities and truncate them. */
1364 if (NILP (Vprint_circle))
1366 /* Simple but incomplete way. */
1367 int i;
1369 /* See similar code in print_preprocess. */
1370 if (print_depth >= PRINT_CIRCLE)
1371 error ("Apparently circular structure being printed");
1373 for (i = 0; i < print_depth; i++)
1374 if (EQ (obj, being_printed[i]))
1376 int len = sprintf (buf, "#%d", i);
1377 strout (buf, len, len, printcharfun);
1378 return;
1380 being_printed[print_depth] = obj;
1382 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1384 /* With the print-circle feature. */
1385 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1386 if (INTEGERP (num))
1388 EMACS_INT n = XINT (num);
1389 if (n < 0)
1390 { /* Add a prefix #n= if OBJ has not yet been printed;
1391 that is, its status field is nil. */
1392 int len = sprintf (buf, "#%"pI"d=", -n);
1393 strout (buf, len, len, printcharfun);
1394 /* OBJ is going to be printed. Remember that fact. */
1395 Fputhash (obj, make_number (- n), Vprint_number_table);
1397 else
1399 /* Just print #n# if OBJ has already been printed. */
1400 int len = sprintf (buf, "#%"pI"d#", n);
1401 strout (buf, len, len, printcharfun);
1402 return;
1407 print_depth++;
1409 switch (XTYPE (obj))
1411 case_Lisp_Int:
1413 int len = sprintf (buf, "%"pI"d", XINT (obj));
1414 strout (buf, len, len, printcharfun);
1416 break;
1418 case Lisp_Float:
1420 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1421 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1422 strout (pigbuf, len, len, printcharfun);
1424 break;
1426 case Lisp_String:
1427 if (!escapeflag)
1428 print_string (obj, printcharfun);
1429 else
1431 register ptrdiff_t i, i_byte;
1432 struct gcpro gcpro1;
1433 ptrdiff_t size_byte;
1434 /* True means we must ensure that the next character we output
1435 cannot be taken as part of a hex character escape. */
1436 bool need_nonhex = false;
1437 bool multibyte = STRING_MULTIBYTE (obj);
1439 GCPRO1 (obj);
1441 if (! EQ (Vprint_charset_text_property, Qt))
1442 obj = print_prune_string_charset (obj);
1444 if (string_intervals (obj))
1445 print_c_string ("#(", printcharfun);
1447 printchar ('\"', printcharfun);
1448 size_byte = SBYTES (obj);
1450 for (i = 0, i_byte = 0; i_byte < size_byte;)
1452 /* Here, we must convert each multi-byte form to the
1453 corresponding character code before handing it to printchar. */
1454 int c;
1456 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1458 QUIT;
1460 if (multibyte
1461 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1462 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1463 && print_escape_nonascii))
1465 /* When printing a raw 8-bit byte in a multibyte buffer, or
1466 (when requested) a non-ASCII character in a unibyte buffer,
1467 print single-byte non-ASCII string chars
1468 using octal escapes. */
1469 char outbuf[5];
1470 int len = sprintf (outbuf, "\\%03o", c + 0u);
1471 strout (outbuf, len, len, printcharfun);
1472 need_nonhex = false;
1474 else if (multibyte
1475 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1477 /* When requested, print multibyte chars using hex escapes. */
1478 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1479 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1480 strout (outbuf, len, len, printcharfun);
1481 need_nonhex = true;
1483 else
1485 /* If we just had a hex escape, and this character
1486 could be taken as part of it,
1487 output `\ ' to prevent that. */
1488 if (need_nonhex && c_isxdigit (c))
1489 print_c_string ("\\ ", printcharfun);
1491 if (c == '\n' && print_escape_newlines
1492 ? (c = 'n', true)
1493 : c == '\f' && print_escape_newlines
1494 ? (c = 'f', true)
1495 : c == '\"' || c == '\\')
1496 printchar ('\\', printcharfun);
1498 printchar (c, printcharfun);
1499 need_nonhex = false;
1502 printchar ('\"', printcharfun);
1504 if (string_intervals (obj))
1506 traverse_intervals (string_intervals (obj),
1507 0, print_interval, printcharfun);
1508 printchar (')', printcharfun);
1511 UNGCPRO;
1513 break;
1515 case Lisp_Symbol:
1517 bool confusing;
1518 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1519 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1520 int c;
1521 ptrdiff_t i, i_byte;
1522 ptrdiff_t size_byte;
1523 Lisp_Object name;
1525 name = SYMBOL_NAME (obj);
1527 if (p != end && (*p == '-' || *p == '+')) p++;
1528 if (p == end)
1529 confusing = 0;
1530 /* If symbol name begins with a digit, and ends with a digit,
1531 and contains nothing but digits and `e', it could be treated
1532 as a number. So set CONFUSING.
1534 Symbols that contain periods could also be taken as numbers,
1535 but periods are always escaped, so we don't have to worry
1536 about them here. */
1537 else if (*p >= '0' && *p <= '9'
1538 && end[-1] >= '0' && end[-1] <= '9')
1540 while (p != end && ((*p >= '0' && *p <= '9')
1541 /* Needed for \2e10. */
1542 || *p == 'e' || *p == 'E'))
1543 p++;
1544 confusing = (end == p);
1546 else
1547 confusing = 0;
1549 size_byte = SBYTES (name);
1551 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1552 print_c_string ("#:", printcharfun);
1553 else if (size_byte == 0)
1555 print_c_string ("##", printcharfun);
1556 break;
1559 for (i = 0, i_byte = 0; i_byte < size_byte;)
1561 /* Here, we must convert each multi-byte form to the
1562 corresponding character code before handing it to PRINTCHAR. */
1563 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1564 QUIT;
1566 if (escapeflag)
1568 if (c == '\"' || c == '\\' || c == '\''
1569 || c == ';' || c == '#' || c == '(' || c == ')'
1570 || c == ',' || c == '.' || c == '`'
1571 || c == '[' || c == ']' || c == '?' || c <= 040
1572 || confusing)
1574 printchar ('\\', printcharfun);
1575 confusing = false;
1578 printchar (c, printcharfun);
1581 break;
1583 case Lisp_Cons:
1584 /* If deeper than spec'd depth, print placeholder. */
1585 if (INTEGERP (Vprint_level)
1586 && print_depth > XINT (Vprint_level))
1587 print_c_string ("...", printcharfun);
1588 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1589 && EQ (XCAR (obj), Qquote))
1591 printchar ('\'', printcharfun);
1592 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1594 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1595 && EQ (XCAR (obj), Qfunction))
1597 print_c_string ("#'", printcharfun);
1598 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1600 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1601 && EQ (XCAR (obj), Qbackquote))
1603 printchar ('`', printcharfun);
1604 new_backquote_output++;
1605 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1606 new_backquote_output--;
1608 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1609 && new_backquote_output
1610 && (EQ (XCAR (obj), Qcomma)
1611 || EQ (XCAR (obj), Qcomma_at)
1612 || EQ (XCAR (obj), Qcomma_dot)))
1614 print_object (XCAR (obj), printcharfun, false);
1615 new_backquote_output--;
1616 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1617 new_backquote_output++;
1619 else
1621 printchar ('(', printcharfun);
1623 Lisp_Object halftail = obj;
1625 /* Negative values of print-length are invalid in CL.
1626 Treat them like nil, as CMUCL does. */
1627 printmax_t print_length = (NATNUMP (Vprint_length)
1628 ? XFASTINT (Vprint_length)
1629 : TYPE_MAXIMUM (printmax_t));
1631 printmax_t i = 0;
1632 while (CONSP (obj))
1634 /* Detect circular list. */
1635 if (NILP (Vprint_circle))
1637 /* Simple but incomplete way. */
1638 if (i != 0 && EQ (obj, halftail))
1640 int len = sprintf (buf, " . #%"pMd, i / 2);
1641 strout (buf, len, len, printcharfun);
1642 goto end_of_list;
1645 else
1647 /* With the print-circle feature. */
1648 if (i != 0)
1650 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1651 if (INTEGERP (num))
1653 print_c_string (" . ", printcharfun);
1654 print_object (obj, printcharfun, escapeflag);
1655 goto end_of_list;
1660 if (i)
1661 printchar (' ', printcharfun);
1663 if (print_length <= i)
1665 print_c_string ("...", printcharfun);
1666 goto end_of_list;
1669 i++;
1670 print_object (XCAR (obj), printcharfun, escapeflag);
1672 obj = XCDR (obj);
1673 if (!(i & 1))
1674 halftail = XCDR (halftail);
1677 /* OBJ non-nil here means it's the end of a dotted list. */
1678 if (!NILP (obj))
1680 print_c_string (" . ", printcharfun);
1681 print_object (obj, printcharfun, escapeflag);
1684 end_of_list:
1685 printchar (')', printcharfun);
1687 break;
1689 case Lisp_Vectorlike:
1690 if (PROCESSP (obj))
1692 if (escapeflag)
1694 print_c_string ("#<process ", printcharfun);
1695 print_string (XPROCESS (obj)->name, printcharfun);
1696 printchar ('>', printcharfun);
1698 else
1699 print_string (XPROCESS (obj)->name, printcharfun);
1701 else if (BOOL_VECTOR_P (obj))
1703 ptrdiff_t i;
1704 unsigned char c;
1705 struct gcpro gcpro1;
1706 EMACS_INT size = bool_vector_size (obj);
1707 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1708 ptrdiff_t real_size_in_chars = size_in_chars;
1709 GCPRO1 (obj);
1711 int len = sprintf (buf, "#&%"pI"d\"", size);
1712 strout (buf, len, len, printcharfun);
1714 /* Don't print more characters than the specified maximum.
1715 Negative values of print-length are invalid. Treat them
1716 like a print-length of nil. */
1717 if (NATNUMP (Vprint_length)
1718 && XFASTINT (Vprint_length) < size_in_chars)
1719 size_in_chars = XFASTINT (Vprint_length);
1721 for (i = 0; i < size_in_chars; i++)
1723 QUIT;
1724 c = bool_vector_uchar_data (obj)[i];
1725 if (c == '\n' && print_escape_newlines)
1726 print_c_string ("\\n", printcharfun);
1727 else if (c == '\f' && print_escape_newlines)
1728 print_c_string ("\\f", printcharfun);
1729 else if (c > '\177')
1731 /* Use octal escapes to avoid encoding issues. */
1732 len = sprintf (buf, "\\%o", c);
1733 strout (buf, len, len, printcharfun);
1735 else
1737 if (c == '\"' || c == '\\')
1738 printchar ('\\', printcharfun);
1739 printchar (c, printcharfun);
1743 if (size_in_chars < real_size_in_chars)
1744 print_c_string (" ...", printcharfun);
1745 printchar ('\"', printcharfun);
1747 UNGCPRO;
1749 else if (SUBRP (obj))
1751 print_c_string ("#<subr ", printcharfun);
1752 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1753 printchar ('>', printcharfun);
1755 else if (WINDOWP (obj))
1757 int len = sprintf (buf, "#<window %"pI"d",
1758 XWINDOW (obj)->sequence_number);
1759 strout (buf, len, len, printcharfun);
1760 if (BUFFERP (XWINDOW (obj)->contents))
1762 print_c_string (" on ", printcharfun);
1763 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1764 printcharfun);
1766 printchar ('>', printcharfun);
1768 else if (TERMINALP (obj))
1770 struct terminal *t = XTERMINAL (obj);
1771 int len = sprintf (buf, "#<terminal %d", t->id);
1772 strout (buf, len, len, printcharfun);
1773 if (t->name)
1775 print_c_string (" on ", printcharfun);
1776 print_c_string (t->name, printcharfun);
1778 printchar ('>', printcharfun);
1780 else if (HASH_TABLE_P (obj))
1782 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1783 ptrdiff_t i;
1784 ptrdiff_t real_size, size;
1785 int len;
1786 #if 0
1787 void *ptr = h;
1788 print_c_string ("#<hash-table", printcharfun);
1789 if (SYMBOLP (h->test))
1791 print_c_string (" '", printcharfun);
1792 print_c_string (SSDATA (SYMBOL_NAME (h->test)), printcharfun);
1793 printchar (' ', printcharfun);
1794 print_c_string (SSDATA (SYMBOL_NAME (h->weak)), printcharfun);
1795 len = sprintf (buf, " %"pD"d/%"pD"d", h->count, ASIZE (h->next));
1796 strout (buf, len, len, printcharfun);
1798 len = sprintf (buf, " %p>", ptr);
1799 strout (buf, len, len, printcharfun);
1800 #endif
1801 /* Implement a readable output, e.g.:
1802 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1803 /* Always print the size. */
1804 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1805 strout (buf, len, len, printcharfun);
1807 if (!NILP (h->test.name))
1809 print_c_string (" test ", printcharfun);
1810 print_object (h->test.name, printcharfun, escapeflag);
1813 if (!NILP (h->weak))
1815 print_c_string (" weakness ", printcharfun);
1816 print_object (h->weak, printcharfun, escapeflag);
1819 if (!NILP (h->rehash_size))
1821 print_c_string (" rehash-size ", printcharfun);
1822 print_object (h->rehash_size, printcharfun, escapeflag);
1825 if (!NILP (h->rehash_threshold))
1827 print_c_string (" rehash-threshold ", printcharfun);
1828 print_object (h->rehash_threshold, printcharfun, escapeflag);
1831 print_c_string (" data ", printcharfun);
1833 /* Print the data here as a plist. */
1834 real_size = HASH_TABLE_SIZE (h);
1835 size = real_size;
1837 /* Don't print more elements than the specified maximum. */
1838 if (NATNUMP (Vprint_length)
1839 && XFASTINT (Vprint_length) < size)
1840 size = XFASTINT (Vprint_length);
1842 printchar ('(', printcharfun);
1843 for (i = 0; i < size; i++)
1844 if (!NILP (HASH_HASH (h, i)))
1846 if (i) printchar (' ', printcharfun);
1847 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1848 printchar (' ', printcharfun);
1849 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1852 if (size < real_size)
1853 print_c_string (" ...", printcharfun);
1855 print_c_string ("))", printcharfun);
1858 else if (BUFFERP (obj))
1860 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1861 print_c_string ("#<killed buffer>", printcharfun);
1862 else if (escapeflag)
1864 print_c_string ("#<buffer ", printcharfun);
1865 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1866 printchar ('>', printcharfun);
1868 else
1869 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1871 else if (WINDOW_CONFIGURATIONP (obj))
1872 print_c_string ("#<window-configuration>", printcharfun);
1873 else if (FRAMEP (obj))
1875 int len;
1876 void *ptr = XFRAME (obj);
1877 Lisp_Object frame_name = XFRAME (obj)->name;
1879 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1880 ? "#<frame "
1881 : "#<dead frame "),
1882 printcharfun);
1883 if (!STRINGP (frame_name))
1885 /* A frame could be too young and have no name yet;
1886 don't crash. */
1887 if (SYMBOLP (frame_name))
1888 frame_name = Fsymbol_name (frame_name);
1889 else /* can't happen: name should be either nil or string */
1890 frame_name = build_string ("*INVALID*FRAME*NAME*");
1892 print_string (frame_name, printcharfun);
1893 len = sprintf (buf, " %p>", ptr);
1894 strout (buf, len, len, printcharfun);
1896 else if (FONTP (obj))
1898 int i;
1900 if (! FONT_OBJECT_P (obj))
1902 if (FONT_SPEC_P (obj))
1903 print_c_string ("#<font-spec", printcharfun);
1904 else
1905 print_c_string ("#<font-entity", printcharfun);
1906 for (i = 0; i < FONT_SPEC_MAX; i++)
1908 printchar (' ', printcharfun);
1909 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1910 print_object (AREF (obj, i), printcharfun, escapeflag);
1911 else
1912 print_object (font_style_symbolic (obj, i, 0),
1913 printcharfun, escapeflag);
1916 else
1918 print_c_string ("#<font-object ", printcharfun);
1919 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1920 escapeflag);
1922 printchar ('>', printcharfun);
1924 else
1926 ptrdiff_t size = ASIZE (obj);
1927 if (COMPILEDP (obj))
1929 printchar ('#', printcharfun);
1930 size &= PSEUDOVECTOR_SIZE_MASK;
1932 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1934 /* We print a char-table as if it were a vector,
1935 lumping the parent and default slots in with the
1936 character slots. But we add #^ as a prefix. */
1938 /* Make each lowest sub_char_table start a new line.
1939 Otherwise we'll make a line extremely long, which
1940 results in slow redisplay. */
1941 if (SUB_CHAR_TABLE_P (obj)
1942 && XSUB_CHAR_TABLE (obj)->depth == 3)
1943 printchar ('\n', printcharfun);
1944 print_c_string ("#^", printcharfun);
1945 if (SUB_CHAR_TABLE_P (obj))
1946 printchar ('^', printcharfun);
1947 size &= PSEUDOVECTOR_SIZE_MASK;
1949 if (size & PSEUDOVECTOR_FLAG)
1950 goto badtype;
1952 printchar ('[', printcharfun);
1954 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1955 Lisp_Object tem;
1956 ptrdiff_t real_size = size;
1958 /* For a sub char-table, print heading non-Lisp data first. */
1959 if (SUB_CHAR_TABLE_P (obj))
1961 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1962 XSUB_CHAR_TABLE (obj)->min_char);
1963 strout (buf, i, i, printcharfun);
1966 /* Don't print more elements than the specified maximum. */
1967 if (NATNUMP (Vprint_length)
1968 && XFASTINT (Vprint_length) < size)
1969 size = XFASTINT (Vprint_length);
1971 for (i = idx; i < size; i++)
1973 if (i) printchar (' ', printcharfun);
1974 tem = AREF (obj, i);
1975 print_object (tem, printcharfun, escapeflag);
1977 if (size < real_size)
1978 print_c_string (" ...", printcharfun);
1980 printchar (']', printcharfun);
1982 break;
1984 case Lisp_Misc:
1985 switch (XMISCTYPE (obj))
1987 case Lisp_Misc_Marker:
1988 print_c_string ("#<marker ", printcharfun);
1989 /* Do you think this is necessary? */
1990 if (XMARKER (obj)->insertion_type != 0)
1991 print_c_string ("(moves after insertion) ", printcharfun);
1992 if (! XMARKER (obj)->buffer)
1993 print_c_string ("in no buffer", printcharfun);
1994 else
1996 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1997 strout (buf, len, len, printcharfun);
1998 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2000 printchar ('>', printcharfun);
2001 break;
2003 case Lisp_Misc_Overlay:
2004 print_c_string ("#<overlay ", printcharfun);
2005 if (! XMARKER (OVERLAY_START (obj))->buffer)
2006 print_c_string ("in no buffer", printcharfun);
2007 else
2009 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2010 marker_position (OVERLAY_START (obj)),
2011 marker_position (OVERLAY_END (obj)));
2012 strout (buf, len, len, printcharfun);
2013 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2014 printcharfun);
2016 printchar ('>', printcharfun);
2017 break;
2019 case Lisp_Misc_Finalizer:
2020 print_c_string ("#<finalizer", printcharfun);
2021 if (NILP (XFINALIZER (obj)->function))
2022 print_c_string (" used", printcharfun);
2023 printchar ('>', printcharfun);
2024 break;
2026 /* Remaining cases shouldn't happen in normal usage, but let's
2027 print them anyway for the benefit of the debugger. */
2029 case Lisp_Misc_Free:
2030 print_c_string ("#<misc free cell>", printcharfun);
2031 break;
2033 case Lisp_Misc_Save_Value:
2035 int i;
2036 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2038 print_c_string ("#<save-value ", printcharfun);
2040 if (v->save_type == SAVE_TYPE_MEMORY)
2042 ptrdiff_t amount = v->data[1].integer;
2044 #if GC_MARK_STACK
2046 /* valid_lisp_object_p is reliable, so try to print up
2047 to 8 saved objects. This code is rarely used, so
2048 it's OK that valid_lisp_object_p is slow. */
2050 int limit = min (amount, 8);
2051 Lisp_Object *area = v->data[0].pointer;
2053 i = sprintf (buf, "with %"pD"d objects", amount);
2054 strout (buf, i, i, printcharfun);
2056 for (i = 0; i < limit; i++)
2058 Lisp_Object maybe = area[i];
2059 int valid = valid_lisp_object_p (maybe);
2061 printchar (' ', printcharfun);
2062 if (0 < valid)
2063 print_object (maybe, printcharfun, escapeflag);
2064 else
2065 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2066 printcharfun);
2068 if (i == limit && i < amount)
2069 print_c_string (" ...", printcharfun);
2071 #else /* not GC_MARK_STACK */
2073 /* There is no reliable way to determine whether the objects
2074 are initialized, so do not try to print them. */
2076 i = sprintf (buf, "with %"pD"d objects", amount);
2077 strout (buf, i, i, printcharfun);
2079 #endif /* GC_MARK_STACK */
2081 else
2083 /* Print each slot according to its type. */
2084 int index;
2085 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2087 if (index)
2088 printchar (' ', printcharfun);
2090 switch (save_type (v, index))
2092 case SAVE_UNUSED:
2093 i = sprintf (buf, "<unused>");
2094 break;
2096 case SAVE_POINTER:
2097 i = sprintf (buf, "<pointer %p>",
2098 v->data[index].pointer);
2099 break;
2101 case SAVE_FUNCPOINTER:
2102 i = sprintf (buf, "<funcpointer %p>",
2103 ((void *) (intptr_t)
2104 v->data[index].funcpointer));
2105 break;
2107 case SAVE_INTEGER:
2108 i = sprintf (buf, "<integer %"pD"d>",
2109 v->data[index].integer);
2110 break;
2112 case SAVE_OBJECT:
2113 print_object (v->data[index].object, printcharfun,
2114 escapeflag);
2115 continue;
2117 default:
2118 emacs_abort ();
2121 strout (buf, i, i, printcharfun);
2124 printchar ('>', printcharfun);
2126 break;
2128 default:
2129 goto badtype;
2131 break;
2133 default:
2134 badtype:
2136 int len;
2137 /* We're in trouble if this happens!
2138 Probably should just emacs_abort (). */
2139 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2140 if (MISCP (obj))
2141 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2142 else if (VECTORLIKEP (obj))
2143 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2144 else
2145 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2146 strout (buf, len, len, printcharfun);
2147 print_c_string ((" Save your buffers immediately"
2148 " and please report this bug>"),
2149 printcharfun);
2153 print_depth--;
2157 /* Print a description of INTERVAL using PRINTCHARFUN.
2158 This is part of printing a string that has text properties. */
2160 static void
2161 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2163 if (NILP (interval->plist))
2164 return;
2165 printchar (' ', printcharfun);
2166 print_object (make_number (interval->position), printcharfun, 1);
2167 printchar (' ', printcharfun);
2168 print_object (make_number (interval->position + LENGTH (interval)),
2169 printcharfun, 1);
2170 printchar (' ', printcharfun);
2171 print_object (interval->plist, printcharfun, 1);
2174 /* Initialize debug_print stuff early to have it working from the very
2175 beginning. */
2177 void
2178 init_print_once (void)
2180 /* The subroutine object for external-debugging-output is kept here
2181 for the convenience of the debugger. */
2182 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2184 defsubr (&Sexternal_debugging_output);
2187 void
2188 syms_of_print (void)
2190 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2192 DEFVAR_LISP ("standard-output", Vstandard_output,
2193 doc: /* Output stream `print' uses by default for outputting a character.
2194 This may be any function of one argument.
2195 It may also be a buffer (output is inserted before point)
2196 or a marker (output is inserted and the marker is advanced)
2197 or the symbol t (output appears in the echo area). */);
2198 Vstandard_output = Qt;
2199 DEFSYM (Qstandard_output, "standard-output");
2201 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2202 doc: /* The format descriptor string used to print floats.
2203 This is a %-spec like those accepted by `printf' in C,
2204 but with some restrictions. It must start with the two characters `%.'.
2205 After that comes an integer precision specification,
2206 and then a letter which controls the format.
2207 The letters allowed are `e', `f' and `g'.
2208 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2209 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2210 Use `g' to choose the shorter of those two formats for the number at hand.
2211 The precision in any of these cases is the number of digits following
2212 the decimal point. With `f', a precision of 0 means to omit the
2213 decimal point. 0 is not allowed with `e' or `g'.
2215 A value of nil means to use the shortest notation
2216 that represents the number without losing information. */);
2217 Vfloat_output_format = Qnil;
2219 DEFVAR_LISP ("print-length", Vprint_length,
2220 doc: /* Maximum length of list to print before abbreviating.
2221 A value of nil means no limit. See also `eval-expression-print-length'. */);
2222 Vprint_length = Qnil;
2224 DEFVAR_LISP ("print-level", Vprint_level,
2225 doc: /* Maximum depth of list nesting to print before abbreviating.
2226 A value of nil means no limit. See also `eval-expression-print-level'. */);
2227 Vprint_level = Qnil;
2229 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2230 doc: /* Non-nil means print newlines in strings as `\\n'.
2231 Also print formfeeds as `\\f'. */);
2232 print_escape_newlines = 0;
2234 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2235 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2236 \(OOO is the octal representation of the character code.)
2237 Only single-byte characters are affected, and only in `prin1'.
2238 When the output goes in a multibyte buffer, this feature is
2239 enabled regardless of the value of the variable. */);
2240 print_escape_nonascii = 0;
2242 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2243 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2244 \(XXXX is the hex representation of the character code.)
2245 This affects only `prin1'. */);
2246 print_escape_multibyte = 0;
2248 DEFVAR_BOOL ("print-quoted", print_quoted,
2249 doc: /* Non-nil means print quoted forms with reader syntax.
2250 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2251 print_quoted = 0;
2253 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2254 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2255 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2256 When the uninterned symbol appears within a recursive data structure,
2257 and the symbol appears more than once, in addition use the #N# and #N=
2258 constructs as needed, so that multiple references to the same symbol are
2259 shared once again when the text is read back. */);
2260 Vprint_gensym = Qnil;
2262 DEFVAR_LISP ("print-circle", Vprint_circle,
2263 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2264 If nil, printing proceeds recursively and may lead to
2265 `max-lisp-eval-depth' being exceeded or an error may occur:
2266 \"Apparently circular structure being printed.\" Also see
2267 `print-length' and `print-level'.
2268 If non-nil, shared substructures anywhere in the structure are printed
2269 with `#N=' before the first occurrence (in the order of the print
2270 representation) and `#N#' in place of each subsequent occurrence,
2271 where N is a positive decimal integer. */);
2272 Vprint_circle = Qnil;
2274 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2275 doc: /* Non-nil means number continuously across print calls.
2276 This affects the numbers printed for #N= labels and #M# references.
2277 See also `print-circle', `print-gensym', and `print-number-table'.
2278 This variable should not be set with `setq'; bind it with a `let' instead. */);
2279 Vprint_continuous_numbering = Qnil;
2281 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2282 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2283 The Lisp printer uses this vector to detect Lisp objects referenced more
2284 than once.
2286 When you bind `print-continuous-numbering' to t, you should probably
2287 also bind `print-number-table' to nil. This ensures that the value of
2288 `print-number-table' can be garbage-collected once the printing is
2289 done. If all elements of `print-number-table' are nil, it means that
2290 the printing done so far has not found any shared structure or objects
2291 that need to be recorded in the table. */);
2292 Vprint_number_table = Qnil;
2294 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2295 doc: /* A flag to control printing of `charset' text property on printing a string.
2296 The value must be nil, t, or `default'.
2298 If the value is nil, don't print the text property `charset'.
2300 If the value is t, always print the text property `charset'.
2302 If the value is `default', print the text property `charset' only when
2303 the value is different from what is guessed in the current charset
2304 priorities. */);
2305 Vprint_charset_text_property = Qdefault;
2307 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2308 staticpro (&Vprin1_to_string_buffer);
2310 defsubr (&Sprin1);
2311 defsubr (&Sprin1_to_string);
2312 defsubr (&Serror_message_string);
2313 defsubr (&Sprinc);
2314 defsubr (&Sprint);
2315 defsubr (&Sterpri);
2316 defsubr (&Swrite_char);
2317 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2318 defsubr (&Sredirect_debugging_output);
2319 #endif
2321 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2322 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2323 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2325 print_prune_charset_plist = Qnil;
2326 staticpro (&print_prune_charset_plist);