; Auto-commit of loaddefs files.
[emacs.git] / src / print.c
blob269d8f250e2d69ef5f8fa2458a2bddb707bd0091
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2016 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 "coding.h"
28 #include "buffer.h"
29 #include "charset.h"
30 #include "frame.h"
31 #include "process.h"
32 #include "disptab.h"
33 #include "intervals.h"
34 #include "blockinput.h"
36 #include <c-ctype.h>
37 #include <float.h>
38 #include <ftoastr.h>
40 struct terminal;
42 /* Avoid actual stack overflow in print. */
43 static ptrdiff_t print_depth;
45 /* Level of nesting inside outputting backquote in new style. */
46 static ptrdiff_t new_backquote_output;
48 /* Detect most circularities to print finite output. */
49 #define PRINT_CIRCLE 200
50 static Lisp_Object being_printed[PRINT_CIRCLE];
52 /* Last char printed to stdout by printchar. */
53 static unsigned int printchar_stdout_last;
55 /* When printing into a buffer, first we put the text in this
56 block, then insert it all at once. */
57 static char *print_buffer;
59 /* Size allocated in print_buffer. */
60 static ptrdiff_t print_buffer_size;
61 /* Chars stored in print_buffer. */
62 static ptrdiff_t print_buffer_pos;
63 /* Bytes stored in print_buffer. */
64 static ptrdiff_t print_buffer_pos_byte;
66 /* Vprint_number_table is a table, that keeps objects that are going to
67 be printed, to allow use of #n= and #n# to express sharing.
68 For any given object, the table can give the following values:
69 t the object will be printed only once.
70 -N the object will be printed several times and will take number N.
71 N the object has been printed so we can refer to it as #N#.
72 print_number_index holds the largest N already used.
73 N has to be striclty larger than 0 since we need to distinguish -N. */
74 static ptrdiff_t print_number_index;
75 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
77 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
78 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
81 /* Low level output routines for characters and strings. */
83 /* Lisp functions to do output using a stream
84 must have the stream in a variable called printcharfun
85 and must start with PRINTPREPARE, end with PRINTFINISH.
86 Use printchar to output one character,
87 or call strout to output a block of characters. */
89 #define PRINTPREPARE \
90 struct buffer *old = current_buffer; \
91 ptrdiff_t old_point = -1, start_point = -1; \
92 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
93 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
94 bool free_print_buffer = 0; \
95 bool multibyte \
96 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
97 Lisp_Object original = printcharfun; \
98 if (NILP (printcharfun)) printcharfun = Qt; \
99 if (BUFFERP (printcharfun)) \
101 if (XBUFFER (printcharfun) != current_buffer) \
102 Fset_buffer (printcharfun); \
103 printcharfun = Qnil; \
105 if (MARKERP (printcharfun)) \
107 ptrdiff_t marker_pos; \
108 if (! XMARKER (printcharfun)->buffer) \
109 error ("Marker does not point anywhere"); \
110 if (XMARKER (printcharfun)->buffer != current_buffer) \
111 set_buffer_internal (XMARKER (printcharfun)->buffer); \
112 marker_pos = marker_position (printcharfun); \
113 if (marker_pos < BEGV || marker_pos > ZV) \
114 signal_error ("Marker is outside the accessible " \
115 "part of the buffer", printcharfun); \
116 old_point = PT; \
117 old_point_byte = PT_BYTE; \
118 SET_PT_BOTH (marker_pos, \
119 marker_byte_position (printcharfun)); \
120 start_point = PT; \
121 start_point_byte = PT_BYTE; \
122 printcharfun = Qnil; \
124 if (NILP (printcharfun)) \
126 Lisp_Object string; \
127 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
128 && ! print_escape_multibyte) \
129 specbind (Qprint_escape_multibyte, Qt); \
130 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
131 && ! print_escape_nonascii) \
132 specbind (Qprint_escape_nonascii, Qt); \
133 if (print_buffer != 0) \
135 string = make_string_from_bytes (print_buffer, \
136 print_buffer_pos, \
137 print_buffer_pos_byte); \
138 record_unwind_protect (print_unwind, string); \
140 else \
142 int new_size = 1000; \
143 print_buffer = xmalloc (new_size); \
144 print_buffer_size = new_size; \
145 free_print_buffer = 1; \
147 print_buffer_pos = 0; \
148 print_buffer_pos_byte = 0; \
150 if (EQ (printcharfun, Qt) && ! noninteractive) \
151 setup_echo_area_for_printing (multibyte);
153 #define PRINTFINISH \
154 if (NILP (printcharfun)) \
156 if (print_buffer_pos != print_buffer_pos_byte \
157 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
159 USE_SAFE_ALLOCA; \
160 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
161 copy_text ((unsigned char *) print_buffer, temp, \
162 print_buffer_pos_byte, 1, 0); \
163 insert_1_both ((char *) temp, print_buffer_pos, \
164 print_buffer_pos, 0, 1, 0); \
165 SAFE_FREE (); \
167 else \
168 insert_1_both (print_buffer, print_buffer_pos, \
169 print_buffer_pos_byte, 0, 1, 0); \
170 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
172 if (free_print_buffer) \
174 xfree (print_buffer); \
175 print_buffer = 0; \
177 unbind_to (specpdl_count, Qnil); \
178 if (MARKERP (original)) \
179 set_marker_both (original, Qnil, PT, PT_BYTE); \
180 if (old_point >= 0) \
181 SET_PT_BOTH (old_point + (old_point >= start_point \
182 ? PT - start_point : 0), \
183 old_point_byte + (old_point_byte >= start_point_byte \
184 ? PT_BYTE - start_point_byte : 0)); \
185 set_buffer_internal (old);
187 /* This is used to restore the saved contents of print_buffer
188 when there is a recursive call to print. */
190 static void
191 print_unwind (Lisp_Object saved_text)
193 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
196 /* Print character CH to the stdio stream STREAM. */
198 static void
199 printchar_to_stream (unsigned int ch, FILE *stream)
201 Lisp_Object dv IF_LINT (= Qnil);
202 ptrdiff_t i = 0, n = 1;
203 Lisp_Object coding_system = Vlocale_coding_system;
204 bool encode_p = false;
206 if (!NILP (Vcoding_system_for_write))
207 coding_system = Vcoding_system_for_write;
208 if (!NILP (coding_system))
209 encode_p = true;
211 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
213 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
214 if (VECTORP (dv))
216 n = ASIZE (dv);
217 goto next_char;
221 while (true)
223 if (ASCII_CHAR_P (ch))
225 putc (ch, stream);
226 #ifdef WINDOWSNT
227 /* Send the output to a debugger (nothing happens if there
228 isn't one). */
229 if (print_output_debug_flag && stream == stderr)
230 OutputDebugString ((char []) {ch, '\0'});
231 #endif
233 else
235 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
236 int len = CHAR_STRING (ch, mbstr);
237 Lisp_Object encoded_ch =
238 make_multibyte_string ((char *) mbstr, 1, len);
240 if (encode_p)
241 encoded_ch = code_convert_string_norecord (encoded_ch,
242 coding_system, true);
243 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
244 #ifdef WINDOWSNT
245 if (print_output_debug_flag && stream == stderr)
246 OutputDebugString (SSDATA (encoded_ch));
247 #endif
250 i++;
252 next_char:
253 for (; i < n; i++)
254 if (CHARACTERP (AREF (dv, i)))
255 break;
256 if (! (i < n))
257 break;
258 ch = XFASTINT (AREF (dv, i));
262 /* Print character CH using method FUN. FUN nil means print to
263 print_buffer. FUN t means print to echo area or stdout if
264 non-interactive. If FUN is neither nil nor t, call FUN with CH as
265 argument. */
267 static void
268 printchar (unsigned int ch, Lisp_Object fun)
270 if (!NILP (fun) && !EQ (fun, Qt))
271 call1 (fun, make_number (ch));
272 else
274 unsigned char str[MAX_MULTIBYTE_LENGTH];
275 int len = CHAR_STRING (ch, str);
277 QUIT;
279 if (NILP (fun))
281 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
282 if (incr > 0)
283 print_buffer = xpalloc (print_buffer, &print_buffer_size,
284 incr, -1, 1);
285 memcpy (print_buffer + print_buffer_pos_byte, str, len);
286 print_buffer_pos += 1;
287 print_buffer_pos_byte += len;
289 else if (noninteractive)
291 printchar_stdout_last = ch;
292 if (DISP_TABLE_P (Vstandard_display_table))
293 printchar_to_stream (ch, stdout);
294 else
295 fwrite (str, 1, len, stdout);
296 noninteractive_need_newline = 1;
298 else
300 bool multibyte_p
301 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
303 setup_echo_area_for_printing (multibyte_p);
304 insert_char (ch);
305 message_dolog ((char *) str, len, 0, multibyte_p);
311 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
312 method PRINTCHARFUN. PRINTCHARFUN nil means output to
313 print_buffer. PRINTCHARFUN t means output to the echo area or to
314 stdout if non-interactive. If neither nil nor t, call Lisp
315 function PRINTCHARFUN for each character printed. MULTIBYTE
316 non-zero means PTR contains multibyte characters.
318 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
319 to data in a Lisp string. Otherwise that is not safe. */
321 static void
322 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
323 Lisp_Object printcharfun)
325 if (NILP (printcharfun))
327 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
328 if (incr > 0)
329 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
330 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
331 print_buffer_pos += size;
332 print_buffer_pos_byte += size_byte;
334 else if (noninteractive && EQ (printcharfun, Qt))
336 if (DISP_TABLE_P (Vstandard_display_table))
338 int len;
339 for (ptrdiff_t i = 0; i < size_byte; i += len)
341 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
342 len);
343 printchar_to_stream (ch, stdout);
346 else
347 fwrite (ptr, 1, size_byte, stdout);
349 noninteractive_need_newline = 1;
351 else if (EQ (printcharfun, Qt))
353 /* Output to echo area. We're trying to avoid a little overhead
354 here, that's the reason we don't call printchar to do the
355 job. */
356 int i;
357 bool multibyte_p
358 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
360 setup_echo_area_for_printing (multibyte_p);
361 message_dolog (ptr, size_byte, 0, multibyte_p);
363 if (size == size_byte)
365 for (i = 0; i < size; ++i)
366 insert_char ((unsigned char) *ptr++);
368 else
370 int len;
371 for (i = 0; i < size_byte; i += len)
373 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
374 len);
375 insert_char (ch);
379 else
381 /* PRINTCHARFUN is a Lisp function. */
382 ptrdiff_t i = 0;
384 if (size == size_byte)
386 while (i < size_byte)
388 int ch = ptr[i++];
389 printchar (ch, printcharfun);
392 else
394 while (i < size_byte)
396 /* Here, we must convert each multi-byte form to the
397 corresponding character code before handing it to
398 PRINTCHAR. */
399 int len;
400 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
401 len);
402 printchar (ch, printcharfun);
403 i += len;
409 /* Print the contents of a string STRING using PRINTCHARFUN.
410 It isn't safe to use strout in many cases,
411 because printing one char can relocate. */
413 static void
414 print_string (Lisp_Object string, Lisp_Object printcharfun)
416 if (EQ (printcharfun, Qt) || NILP (printcharfun))
418 ptrdiff_t chars;
420 if (print_escape_nonascii)
421 string = string_escape_byte8 (string);
423 if (STRING_MULTIBYTE (string))
424 chars = SCHARS (string);
425 else if (! print_escape_nonascii
426 && (EQ (printcharfun, Qt)
427 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
428 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
430 /* If unibyte string STRING contains 8-bit codes, we must
431 convert STRING to a multibyte string containing the same
432 character codes. */
433 Lisp_Object newstr;
434 ptrdiff_t bytes;
436 chars = SBYTES (string);
437 bytes = count_size_as_multibyte (SDATA (string), chars);
438 if (chars < bytes)
440 newstr = make_uninit_multibyte_string (chars, bytes);
441 memcpy (SDATA (newstr), SDATA (string), chars);
442 str_to_multibyte (SDATA (newstr), bytes, chars);
443 string = newstr;
446 else
447 chars = SBYTES (string);
449 if (EQ (printcharfun, Qt))
451 /* Output to echo area. */
452 ptrdiff_t nbytes = SBYTES (string);
454 /* Copy the string contents so that relocation of STRING by
455 GC does not cause trouble. */
456 USE_SAFE_ALLOCA;
457 char *buffer = SAFE_ALLOCA (nbytes);
458 memcpy (buffer, SDATA (string), nbytes);
460 strout (buffer, chars, nbytes, printcharfun);
462 SAFE_FREE ();
464 else
465 /* No need to copy, since output to print_buffer can't GC. */
466 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
468 else
470 /* Otherwise, string may be relocated by printing one char.
471 So re-fetch the string address for each character. */
472 ptrdiff_t i;
473 ptrdiff_t size = SCHARS (string);
474 ptrdiff_t size_byte = SBYTES (string);
475 if (size == size_byte)
476 for (i = 0; i < size; i++)
477 printchar (SREF (string, i), printcharfun);
478 else
479 for (i = 0; i < size_byte; )
481 /* Here, we must convert each multi-byte form to the
482 corresponding character code before handing it to PRINTCHAR. */
483 int len;
484 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
485 printchar (ch, printcharfun);
486 i += len;
491 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
492 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
493 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
494 (Lisp_Object character, Lisp_Object printcharfun)
496 if (NILP (printcharfun))
497 printcharfun = Vstandard_output;
498 CHECK_NUMBER (character);
499 PRINTPREPARE;
500 printchar (XINT (character), printcharfun);
501 PRINTFINISH;
502 return character;
505 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
506 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
507 Do not use this on the contents of a Lisp string. */
509 static void
510 print_c_string (char const *string, Lisp_Object printcharfun)
512 ptrdiff_t len = strlen (string);
513 strout (string, len, len, printcharfun);
516 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
517 Do not use this on the contents of a Lisp string. */
519 static void
520 write_string_1 (const char *data, Lisp_Object printcharfun)
522 PRINTPREPARE;
523 print_c_string (data, printcharfun);
524 PRINTFINISH;
527 /* Used from outside of print.c to print a C unibyte
528 string at DATA on the default output stream.
529 Do not use this on the contents of a Lisp string. */
531 void
532 write_string (const char *data)
534 write_string_1 (data, Vstandard_output);
538 void
539 temp_output_buffer_setup (const char *bufname)
541 ptrdiff_t count = SPECPDL_INDEX ();
542 register struct buffer *old = current_buffer;
543 register Lisp_Object buf;
545 record_unwind_current_buffer ();
547 Fset_buffer (Fget_buffer_create (build_string (bufname)));
549 Fkill_all_local_variables ();
550 delete_all_overlays (current_buffer);
551 bset_directory (current_buffer, BVAR (old, directory));
552 bset_read_only (current_buffer, Qnil);
553 bset_filename (current_buffer, Qnil);
554 bset_undo_list (current_buffer, Qt);
555 eassert (current_buffer->overlays_before == NULL);
556 eassert (current_buffer->overlays_after == NULL);
557 bset_enable_multibyte_characters
558 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
559 specbind (Qinhibit_read_only, Qt);
560 specbind (Qinhibit_modification_hooks, Qt);
561 Ferase_buffer ();
562 XSETBUFFER (buf, current_buffer);
564 run_hook (Qtemp_buffer_setup_hook);
566 unbind_to (count, Qnil);
568 specbind (Qstandard_output, buf);
571 static void print (Lisp_Object, Lisp_Object, bool);
572 static void print_preprocess (Lisp_Object);
573 static void print_preprocess_string (INTERVAL, Lisp_Object);
574 static void print_object (Lisp_Object, Lisp_Object, bool);
576 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
577 doc: /* Output a newline to stream PRINTCHARFUN.
578 If ENSURE is non-nil only output a newline if not already at the
579 beginning of a line. Value is non-nil if a newline is printed.
580 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
581 (Lisp_Object printcharfun, Lisp_Object ensure)
583 Lisp_Object val;
585 if (NILP (printcharfun))
586 printcharfun = Vstandard_output;
587 PRINTPREPARE;
589 if (NILP (ensure))
590 val = Qt;
591 /* Difficult to check if at line beginning so abort. */
592 else if (FUNCTIONP (printcharfun))
593 signal_error ("Unsupported function argument", printcharfun);
594 else if (noninteractive && !NILP (printcharfun))
595 val = printchar_stdout_last == 10 ? Qnil : Qt;
596 else
597 val = NILP (Fbolp ()) ? Qt : Qnil;
599 if (!NILP (val))
600 printchar ('\n', printcharfun);
601 PRINTFINISH;
602 return val;
605 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
606 doc: /* Output the printed representation of OBJECT, any Lisp object.
607 Quoting characters are printed when needed to make output that `read'
608 can handle, whenever this is possible. For complex objects, the behavior
609 is controlled by `print-level' and `print-length', which see.
611 OBJECT is any of the Lisp data types: a number, a string, a symbol,
612 a list, a buffer, a window, a frame, etc.
614 A printed representation of an object is text which describes that object.
616 Optional argument PRINTCHARFUN is the output stream, which can be one
617 of these:
619 - a buffer, in which case output is inserted into that buffer at point;
620 - a marker, in which case output is inserted at marker's position;
621 - a function, in which case that function is called once for each
622 character of OBJECT's printed representation;
623 - a symbol, in which case that symbol's function definition is called; or
624 - t, in which case the output is displayed in the echo area.
626 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
627 is used instead. */)
628 (Lisp_Object object, Lisp_Object printcharfun)
630 if (NILP (printcharfun))
631 printcharfun = Vstandard_output;
632 PRINTPREPARE;
633 print (object, printcharfun, 1);
634 PRINTFINISH;
635 return object;
638 /* a buffer which is used to hold output being built by prin1-to-string */
639 Lisp_Object Vprin1_to_string_buffer;
641 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
642 doc: /* Return a string containing the printed representation of OBJECT.
643 OBJECT can be any Lisp object. This function outputs quoting characters
644 when necessary to make output that `read' can handle, whenever possible,
645 unless the optional second argument NOESCAPE is non-nil. For complex objects,
646 the behavior is controlled by `print-level' and `print-length', which see.
648 OBJECT is any of the Lisp data types: a number, a string, a symbol,
649 a list, a buffer, a window, a frame, etc.
651 A printed representation of an object is text which describes that object. */)
652 (Lisp_Object object, Lisp_Object noescape)
654 ptrdiff_t count = SPECPDL_INDEX ();
656 specbind (Qinhibit_modification_hooks, Qt);
658 /* Save and restore this: we are altering a buffer
659 but we don't want to deactivate the mark just for that.
660 No need for specbind, since errors deactivate the mark. */
661 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
662 bool prev_abort_on_gc = abort_on_gc;
663 abort_on_gc = true;
665 Lisp_Object printcharfun = Vprin1_to_string_buffer;
666 PRINTPREPARE;
667 print (object, printcharfun, NILP (noescape));
668 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
669 PRINTFINISH;
671 struct buffer *previous = current_buffer;
672 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
673 object = Fbuffer_string ();
674 if (SBYTES (object) == SCHARS (object))
675 STRING_SET_UNIBYTE (object);
677 /* Note that this won't make prepare_to_modify_buffer call
678 ask-user-about-supersession-threat because this buffer
679 does not visit a file. */
680 Ferase_buffer ();
681 set_buffer_internal (previous);
683 Vdeactivate_mark = save_deactivate_mark;
685 abort_on_gc = prev_abort_on_gc;
686 return unbind_to (count, object);
689 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
690 doc: /* Output the printed representation of OBJECT, any Lisp object.
691 No quoting characters are used; no delimiters are printed around
692 the contents of strings.
694 OBJECT is any of the Lisp data types: a number, a string, a symbol,
695 a list, a buffer, a window, a frame, etc.
697 A printed representation of an object is text which describes that object.
699 Optional argument PRINTCHARFUN is the output stream, which can be one
700 of these:
702 - a buffer, in which case output is inserted into that buffer at point;
703 - a marker, in which case output is inserted at marker's position;
704 - a function, in which case that function is called once for each
705 character of OBJECT's printed representation;
706 - a symbol, in which case that symbol's function definition is called; or
707 - t, in which case the output is displayed in the echo area.
709 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
710 is used instead. */)
711 (Lisp_Object object, Lisp_Object printcharfun)
713 if (NILP (printcharfun))
714 printcharfun = Vstandard_output;
715 PRINTPREPARE;
716 print (object, printcharfun, 0);
717 PRINTFINISH;
718 return object;
721 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
722 doc: /* Output the printed representation of OBJECT, with newlines around it.
723 Quoting characters are printed when needed to make output that `read'
724 can handle, whenever this is possible. For complex objects, the behavior
725 is controlled by `print-level' and `print-length', which see.
727 OBJECT is any of the Lisp data types: a number, a string, a symbol,
728 a list, a buffer, a window, a frame, etc.
730 A printed representation of an object is text which describes that object.
732 Optional argument PRINTCHARFUN is the output stream, which can be one
733 of these:
735 - a buffer, in which case output is inserted into that buffer at point;
736 - a marker, in which case output is inserted at marker's position;
737 - a function, in which case that function is called once for each
738 character of OBJECT's printed representation;
739 - a symbol, in which case that symbol's function definition is called; or
740 - t, in which case the output is displayed in the echo area.
742 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
743 is used instead. */)
744 (Lisp_Object object, Lisp_Object printcharfun)
746 if (NILP (printcharfun))
747 printcharfun = Vstandard_output;
748 PRINTPREPARE;
749 printchar ('\n', printcharfun);
750 print (object, printcharfun, 1);
751 printchar ('\n', printcharfun);
752 PRINTFINISH;
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;
858 /* If OBJ is (error STRING), just return STRING.
859 That is not only faster, it also avoids the need to allocate
860 space here when the error is due to memory full. */
861 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
862 && CONSP (XCDR (obj))
863 && STRINGP (XCAR (XCDR (obj)))
864 && NILP (XCDR (XCDR (obj))))
865 return XCAR (XCDR (obj));
867 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
869 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
870 value = Fbuffer_string ();
872 Ferase_buffer ();
873 set_buffer_internal (old);
875 return value;
878 /* Print an error message for the error DATA onto Lisp output stream
879 STREAM (suitable for the print functions).
880 CONTEXT is a C string describing the context of the error.
881 CALLER is the Lisp function inside which the error was signaled. */
883 void
884 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
885 Lisp_Object caller)
887 Lisp_Object errname, errmsg, file_error, tail;
889 if (context != 0)
890 write_string_1 (context, stream);
892 /* If we know from where the error was signaled, show it in
893 *Messages*. */
894 if (!NILP (caller) && SYMBOLP (caller))
896 Lisp_Object cname = SYMBOL_NAME (caller);
897 ptrdiff_t cnamelen = SBYTES (cname);
898 USE_SAFE_ALLOCA;
899 char *name = SAFE_ALLOCA (cnamelen);
900 memcpy (name, SDATA (cname), cnamelen);
901 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
902 message_dolog (": ", 2, 0, 0);
903 SAFE_FREE ();
906 errname = Fcar (data);
908 if (EQ (errname, Qerror))
910 data = Fcdr (data);
911 if (!CONSP (data))
912 data = Qnil;
913 errmsg = Fcar (data);
914 file_error = Qnil;
916 else
918 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
919 errmsg = Fget (errname, Qerror_message);
920 file_error = Fmemq (Qfile_error, error_conditions);
923 /* Print an error message including the data items. */
925 tail = Fcdr_safe (data);
927 /* For file-error, make error message by concatenating
928 all the data items. They are all strings. */
929 if (!NILP (file_error) && CONSP (tail))
930 errmsg = XCAR (tail), tail = XCDR (tail);
933 const char *sep = ": ";
935 if (!STRINGP (errmsg))
936 write_string_1 ("peculiar error", stream);
937 else if (SCHARS (errmsg))
938 Fprinc (Fsubstitute_command_keys (errmsg), stream);
939 else
940 sep = NULL;
942 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
944 Lisp_Object obj;
946 if (sep)
947 write_string_1 (sep, stream);
948 obj = XCAR (tail);
949 if (!NILP (file_error)
950 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
951 Fprinc (obj, stream);
952 else
953 Fprin1 (obj, stream);
961 * The buffer should be at least as large as the max string size of the
962 * largest float, printed in the biggest notation. This is undoubtedly
963 * 20d float_output_format, with the negative of the C-constant "HUGE"
964 * from <math.h>.
966 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
968 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
969 * case of -1e307 in 20d float_output_format. What is one to do (short of
970 * re-writing _doprnt to be more sane)?
971 * -wsr
972 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
976 float_to_string (char *buf, double data)
978 char *cp;
979 int width;
980 int len;
982 /* Check for plus infinity in a way that won't lose
983 if there is no plus infinity. */
984 if (data == data / 2 && data > 1.0)
986 static char const infinity_string[] = "1.0e+INF";
987 strcpy (buf, infinity_string);
988 return sizeof infinity_string - 1;
990 /* Likewise for minus infinity. */
991 if (data == data / 2 && data < -1.0)
993 static char const minus_infinity_string[] = "-1.0e+INF";
994 strcpy (buf, minus_infinity_string);
995 return sizeof minus_infinity_string - 1;
997 /* Check for NaN in a way that won't fail if there are no NaNs. */
998 if (! (data * 0.0 >= 0.0))
1000 /* Prepend "-" if the NaN's sign bit is negative.
1001 The sign bit of a double is the bit that is 1 in -0.0. */
1002 static char const NaN_string[] = "0.0e+NaN";
1003 int i;
1004 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1005 bool negative = 0;
1006 u_data.d = data;
1007 u_minus_zero.d = - 0.0;
1008 for (i = 0; i < sizeof (double); i++)
1009 if (u_data.c[i] & u_minus_zero.c[i])
1011 *buf = '-';
1012 negative = 1;
1013 break;
1016 strcpy (buf + negative, NaN_string);
1017 return negative + sizeof NaN_string - 1;
1020 if (NILP (Vfloat_output_format)
1021 || !STRINGP (Vfloat_output_format))
1022 lose:
1024 /* Generate the fewest number of digits that represent the
1025 floating point value without losing information. */
1026 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1027 /* The decimal point must be printed, or the byte compiler can
1028 get confused (Bug#8033). */
1029 width = 1;
1031 else /* oink oink */
1033 /* Check that the spec we have is fully valid.
1034 This means not only valid for printf,
1035 but meant for floats, and reasonable. */
1036 cp = SSDATA (Vfloat_output_format);
1038 if (cp[0] != '%')
1039 goto lose;
1040 if (cp[1] != '.')
1041 goto lose;
1043 cp += 2;
1045 /* Check the width specification. */
1046 width = -1;
1047 if ('0' <= *cp && *cp <= '9')
1049 width = 0;
1052 width = (width * 10) + (*cp++ - '0');
1053 if (DBL_DIG < width)
1054 goto lose;
1056 while (*cp >= '0' && *cp <= '9');
1058 /* A precision of zero is valid only for %f. */
1059 if (width == 0 && *cp != 'f')
1060 goto lose;
1063 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1064 goto lose;
1066 if (cp[1] != 0)
1067 goto lose;
1069 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1072 /* Make sure there is a decimal point with digit after, or an
1073 exponent, so that the value is readable as a float. But don't do
1074 this with "%.0f"; it's valid for that not to produce a decimal
1075 point. Note that width can be 0 only for %.0f. */
1076 if (width != 0)
1078 for (cp = buf; *cp; cp++)
1079 if ((*cp < '0' || *cp > '9') && *cp != '-')
1080 break;
1082 if (*cp == '.' && cp[1] == 0)
1084 cp[1] = '0';
1085 cp[2] = 0;
1086 len++;
1088 else if (*cp == 0)
1090 *cp++ = '.';
1091 *cp++ = '0';
1092 *cp++ = 0;
1093 len += 2;
1097 return len;
1101 static void
1102 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1104 new_backquote_output = 0;
1106 /* Reset print_number_index and Vprint_number_table only when
1107 the variable Vprint_continuous_numbering is nil. Otherwise,
1108 the values of these variables will be kept between several
1109 print functions. */
1110 if (NILP (Vprint_continuous_numbering)
1111 || NILP (Vprint_number_table))
1113 print_number_index = 0;
1114 Vprint_number_table = Qnil;
1117 /* Construct Vprint_number_table for print-gensym and print-circle. */
1118 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1120 /* Construct Vprint_number_table.
1121 This increments print_number_index for the objects added. */
1122 print_depth = 0;
1123 print_preprocess (obj);
1125 if (HASH_TABLE_P (Vprint_number_table))
1126 { /* Remove unnecessary objects, which appear only once in OBJ;
1127 that is, whose status is Qt. */
1128 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1129 ptrdiff_t i;
1131 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1132 if (!NILP (HASH_HASH (h, i))
1133 && EQ (HASH_VALUE (h, i), Qt))
1134 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1138 print_depth = 0;
1139 print_object (obj, printcharfun, escapeflag);
1142 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1143 (STRINGP (obj) || CONSP (obj) \
1144 || (VECTORLIKEP (obj) \
1145 && (VECTORP (obj) || COMPILEDP (obj) \
1146 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1147 || HASH_TABLE_P (obj) || FONTP (obj))) \
1148 || (! NILP (Vprint_gensym) \
1149 && SYMBOLP (obj) \
1150 && !SYMBOL_INTERNED_P (obj)))
1152 /* Construct Vprint_number_table according to the structure of OBJ.
1153 OBJ itself and all its elements will be added to Vprint_number_table
1154 recursively if it is a list, vector, compiled function, char-table,
1155 string (its text properties will be traced), or a symbol that has
1156 no obarray (this is for the print-gensym feature).
1157 The status fields of Vprint_number_table mean whether each object appears
1158 more than once in OBJ: Qnil at the first time, and Qt after that. */
1159 static void
1160 print_preprocess (Lisp_Object obj)
1162 int i;
1163 ptrdiff_t size;
1164 int loop_count = 0;
1165 Lisp_Object halftail;
1167 /* Avoid infinite recursion for circular nested structure
1168 in the case where Vprint_circle is nil. */
1169 if (NILP (Vprint_circle))
1171 /* Give up if we go so deep that print_object will get an error. */
1172 /* See similar code in print_object. */
1173 if (print_depth >= PRINT_CIRCLE)
1174 error ("Apparently circular structure being printed");
1176 for (i = 0; i < print_depth; i++)
1177 if (EQ (obj, being_printed[i]))
1178 return;
1179 being_printed[print_depth] = obj;
1182 print_depth++;
1183 halftail = obj;
1185 loop:
1186 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1188 if (!HASH_TABLE_P (Vprint_number_table))
1189 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1191 /* In case print-circle is nil and print-gensym is t,
1192 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1193 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1195 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1196 if (!NILP (num)
1197 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1198 always print the gensym with a number. This is a special for
1199 the lisp function byte-compile-output-docform. */
1200 || (!NILP (Vprint_continuous_numbering)
1201 && SYMBOLP (obj)
1202 && !SYMBOL_INTERNED_P (obj)))
1203 { /* OBJ appears more than once. Let's remember that. */
1204 if (!INTEGERP (num))
1206 print_number_index++;
1207 /* Negative number indicates it hasn't been printed yet. */
1208 Fputhash (obj, make_number (- print_number_index),
1209 Vprint_number_table);
1211 print_depth--;
1212 return;
1214 else
1215 /* OBJ is not yet recorded. Let's add to the table. */
1216 Fputhash (obj, Qt, Vprint_number_table);
1219 switch (XTYPE (obj))
1221 case Lisp_String:
1222 /* A string may have text properties, which can be circular. */
1223 traverse_intervals_noorder (string_intervals (obj),
1224 print_preprocess_string, Qnil);
1225 break;
1227 case Lisp_Cons:
1228 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1229 just as in print_object. */
1230 if (loop_count && EQ (obj, halftail))
1231 break;
1232 print_preprocess (XCAR (obj));
1233 obj = XCDR (obj);
1234 loop_count++;
1235 if (!(loop_count & 1))
1236 halftail = XCDR (halftail);
1237 goto loop;
1239 case Lisp_Vectorlike:
1240 size = ASIZE (obj);
1241 if (size & PSEUDOVECTOR_FLAG)
1242 size &= PSEUDOVECTOR_SIZE_MASK;
1243 for (i = (SUB_CHAR_TABLE_P (obj)
1244 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1245 print_preprocess (AREF (obj, i));
1246 if (HASH_TABLE_P (obj))
1247 { /* For hash tables, the key_and_value slot is past
1248 `size' because it needs to be marked specially in case
1249 the table is weak. */
1250 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1251 print_preprocess (h->key_and_value);
1253 break;
1255 default:
1256 break;
1259 print_depth--;
1262 static void
1263 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1265 print_preprocess (interval->plist);
1268 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1270 #define PRINT_STRING_NON_CHARSET_FOUND 1
1271 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1273 /* Bitwise or of the above macros. */
1274 static int print_check_string_result;
1276 static void
1277 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1279 Lisp_Object val;
1281 if (NILP (interval->plist)
1282 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1283 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1284 return;
1285 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1286 val = XCDR (XCDR (val)));
1287 if (! CONSP (val))
1289 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1290 return;
1292 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1294 if (! EQ (val, interval->plist)
1295 || CONSP (XCDR (XCDR (val))))
1296 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1298 if (NILP (Vprint_charset_text_property)
1299 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1301 int i, c;
1302 ptrdiff_t charpos = interval->position;
1303 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1304 Lisp_Object charset;
1306 charset = XCAR (XCDR (val));
1307 for (i = 0; i < LENGTH (interval); i++)
1309 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1310 if (! ASCII_CHAR_P (c)
1311 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1313 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1314 break;
1320 /* The value is (charset . nil). */
1321 static Lisp_Object print_prune_charset_plist;
1323 static Lisp_Object
1324 print_prune_string_charset (Lisp_Object string)
1326 print_check_string_result = 0;
1327 traverse_intervals (string_intervals (string), 0,
1328 print_check_string_charset_prop, string);
1329 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1331 string = Fcopy_sequence (string);
1332 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1334 if (NILP (print_prune_charset_plist))
1335 print_prune_charset_plist = list1 (Qcharset);
1336 Fremove_text_properties (make_number (0),
1337 make_number (SCHARS (string)),
1338 print_prune_charset_plist, string);
1340 else
1341 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1342 Qnil, string);
1344 return string;
1347 static void
1348 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1350 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1351 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1352 40))];
1354 QUIT;
1356 /* Detect circularities and truncate them. */
1357 if (NILP (Vprint_circle))
1359 /* Simple but incomplete way. */
1360 int i;
1362 /* See similar code in print_preprocess. */
1363 if (print_depth >= PRINT_CIRCLE)
1364 error ("Apparently circular structure being printed");
1366 for (i = 0; i < print_depth; i++)
1367 if (EQ (obj, being_printed[i]))
1369 int len = sprintf (buf, "#%d", i);
1370 strout (buf, len, len, printcharfun);
1371 return;
1373 being_printed[print_depth] = obj;
1375 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1377 /* With the print-circle feature. */
1378 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1379 if (INTEGERP (num))
1381 EMACS_INT n = XINT (num);
1382 if (n < 0)
1383 { /* Add a prefix #n= if OBJ has not yet been printed;
1384 that is, its status field is nil. */
1385 int len = sprintf (buf, "#%"pI"d=", -n);
1386 strout (buf, len, len, printcharfun);
1387 /* OBJ is going to be printed. Remember that fact. */
1388 Fputhash (obj, make_number (- n), Vprint_number_table);
1390 else
1392 /* Just print #n# if OBJ has already been printed. */
1393 int len = sprintf (buf, "#%"pI"d#", n);
1394 strout (buf, len, len, printcharfun);
1395 return;
1400 print_depth++;
1402 switch (XTYPE (obj))
1404 case_Lisp_Int:
1406 int len = sprintf (buf, "%"pI"d", XINT (obj));
1407 strout (buf, len, len, printcharfun);
1409 break;
1411 case Lisp_Float:
1413 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1414 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1415 strout (pigbuf, len, len, printcharfun);
1417 break;
1419 case Lisp_String:
1420 if (!escapeflag)
1421 print_string (obj, printcharfun);
1422 else
1424 ptrdiff_t i, i_byte;
1425 ptrdiff_t size_byte;
1426 /* True means we must ensure that the next character we output
1427 cannot be taken as part of a hex character escape. */
1428 bool need_nonhex = false;
1429 bool multibyte = STRING_MULTIBYTE (obj);
1431 if (! EQ (Vprint_charset_text_property, Qt))
1432 obj = print_prune_string_charset (obj);
1434 if (string_intervals (obj))
1435 print_c_string ("#(", printcharfun);
1437 printchar ('\"', printcharfun);
1438 size_byte = SBYTES (obj);
1440 for (i = 0, i_byte = 0; i_byte < size_byte;)
1442 /* Here, we must convert each multi-byte form to the
1443 corresponding character code before handing it to printchar. */
1444 int c;
1446 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1448 QUIT;
1450 if (multibyte
1451 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1452 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1453 && print_escape_nonascii))
1455 /* When printing a raw 8-bit byte in a multibyte buffer, or
1456 (when requested) a non-ASCII character in a unibyte buffer,
1457 print single-byte non-ASCII string chars
1458 using octal escapes. */
1459 char outbuf[5];
1460 int len = sprintf (outbuf, "\\%03o", c + 0u);
1461 strout (outbuf, len, len, printcharfun);
1462 need_nonhex = false;
1464 else if (multibyte
1465 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1467 /* When requested, print multibyte chars using hex escapes. */
1468 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1469 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1470 strout (outbuf, len, len, printcharfun);
1471 need_nonhex = true;
1473 else
1475 /* If we just had a hex escape, and this character
1476 could be taken as part of it,
1477 output `\ ' to prevent that. */
1478 if (need_nonhex && c_isxdigit (c))
1479 print_c_string ("\\ ", printcharfun);
1481 if (c == '\n' && print_escape_newlines
1482 ? (c = 'n', true)
1483 : c == '\f' && print_escape_newlines
1484 ? (c = 'f', true)
1485 : c == '\"' || c == '\\')
1486 printchar ('\\', printcharfun);
1488 printchar (c, printcharfun);
1489 need_nonhex = false;
1492 printchar ('\"', printcharfun);
1494 if (string_intervals (obj))
1496 traverse_intervals (string_intervals (obj),
1497 0, print_interval, printcharfun);
1498 printchar (')', printcharfun);
1501 break;
1503 case Lisp_Symbol:
1505 bool confusing;
1506 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1507 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1508 int c;
1509 ptrdiff_t i, i_byte;
1510 ptrdiff_t size_byte;
1511 Lisp_Object name;
1513 name = SYMBOL_NAME (obj);
1515 if (p != end && (*p == '-' || *p == '+')) p++;
1516 if (p == end)
1517 confusing = 0;
1518 /* If symbol name begins with a digit, and ends with a digit,
1519 and contains nothing but digits and `e', it could be treated
1520 as a number. So set CONFUSING.
1522 Symbols that contain periods could also be taken as numbers,
1523 but periods are always escaped, so we don't have to worry
1524 about them here. */
1525 else if (*p >= '0' && *p <= '9'
1526 && end[-1] >= '0' && end[-1] <= '9')
1528 while (p != end && ((*p >= '0' && *p <= '9')
1529 /* Needed for \2e10. */
1530 || *p == 'e' || *p == 'E'))
1531 p++;
1532 confusing = (end == p);
1534 else
1535 confusing = 0;
1537 size_byte = SBYTES (name);
1539 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1540 print_c_string ("#:", printcharfun);
1541 else if (size_byte == 0)
1543 print_c_string ("##", printcharfun);
1544 break;
1547 for (i = 0, i_byte = 0; i_byte < size_byte;)
1549 /* Here, we must convert each multi-byte form to the
1550 corresponding character code before handing it to PRINTCHAR. */
1551 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1552 QUIT;
1554 if (escapeflag)
1556 if (c == '\"' || c == '\\' || c == '\''
1557 || c == ';' || c == '#' || c == '(' || c == ')'
1558 || c == ',' || c == '.' || c == '`'
1559 || c == '[' || c == ']' || c == '?' || c <= 040
1560 || confusing)
1562 printchar ('\\', printcharfun);
1563 confusing = false;
1566 printchar (c, printcharfun);
1569 break;
1571 case Lisp_Cons:
1572 /* If deeper than spec'd depth, print placeholder. */
1573 if (INTEGERP (Vprint_level)
1574 && print_depth > XINT (Vprint_level))
1575 print_c_string ("...", printcharfun);
1576 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1577 && EQ (XCAR (obj), Qquote))
1579 printchar ('\'', printcharfun);
1580 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1582 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1583 && EQ (XCAR (obj), Qfunction))
1585 print_c_string ("#'", printcharfun);
1586 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1588 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1589 && EQ (XCAR (obj), Qbackquote))
1591 printchar ('`', printcharfun);
1592 new_backquote_output++;
1593 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1594 new_backquote_output--;
1596 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1597 && new_backquote_output
1598 && (EQ (XCAR (obj), Qcomma)
1599 || EQ (XCAR (obj), Qcomma_at)
1600 || EQ (XCAR (obj), Qcomma_dot)))
1602 print_object (XCAR (obj), printcharfun, false);
1603 new_backquote_output--;
1604 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1605 new_backquote_output++;
1607 else
1609 printchar ('(', printcharfun);
1611 Lisp_Object halftail = obj;
1613 /* Negative values of print-length are invalid in CL.
1614 Treat them like nil, as CMUCL does. */
1615 printmax_t print_length = (NATNUMP (Vprint_length)
1616 ? XFASTINT (Vprint_length)
1617 : TYPE_MAXIMUM (printmax_t));
1619 printmax_t i = 0;
1620 while (CONSP (obj))
1622 /* Detect circular list. */
1623 if (NILP (Vprint_circle))
1625 /* Simple but incomplete way. */
1626 if (i != 0 && EQ (obj, halftail))
1628 int len = sprintf (buf, " . #%"pMd, i / 2);
1629 strout (buf, len, len, printcharfun);
1630 goto end_of_list;
1633 else
1635 /* With the print-circle feature. */
1636 if (i != 0)
1638 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1639 if (INTEGERP (num))
1641 print_c_string (" . ", printcharfun);
1642 print_object (obj, printcharfun, escapeflag);
1643 goto end_of_list;
1648 if (i)
1649 printchar (' ', printcharfun);
1651 if (print_length <= i)
1653 print_c_string ("...", printcharfun);
1654 goto end_of_list;
1657 i++;
1658 print_object (XCAR (obj), printcharfun, escapeflag);
1660 obj = XCDR (obj);
1661 if (!(i & 1))
1662 halftail = XCDR (halftail);
1665 /* OBJ non-nil here means it's the end of a dotted list. */
1666 if (!NILP (obj))
1668 print_c_string (" . ", printcharfun);
1669 print_object (obj, printcharfun, escapeflag);
1672 end_of_list:
1673 printchar (')', printcharfun);
1675 break;
1677 case Lisp_Vectorlike:
1678 if (PROCESSP (obj))
1680 if (escapeflag)
1682 print_c_string ("#<process ", printcharfun);
1683 print_string (XPROCESS (obj)->name, printcharfun);
1684 printchar ('>', printcharfun);
1686 else
1687 print_string (XPROCESS (obj)->name, printcharfun);
1689 else if (BOOL_VECTOR_P (obj))
1691 ptrdiff_t i;
1692 unsigned char c;
1693 EMACS_INT size = bool_vector_size (obj);
1694 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1695 ptrdiff_t real_size_in_chars = size_in_chars;
1697 int len = sprintf (buf, "#&%"pI"d\"", size);
1698 strout (buf, len, len, printcharfun);
1700 /* Don't print more characters than the specified maximum.
1701 Negative values of print-length are invalid. Treat them
1702 like a print-length of nil. */
1703 if (NATNUMP (Vprint_length)
1704 && XFASTINT (Vprint_length) < size_in_chars)
1705 size_in_chars = XFASTINT (Vprint_length);
1707 for (i = 0; i < size_in_chars; i++)
1709 QUIT;
1710 c = bool_vector_uchar_data (obj)[i];
1711 if (c == '\n' && print_escape_newlines)
1712 print_c_string ("\\n", printcharfun);
1713 else if (c == '\f' && print_escape_newlines)
1714 print_c_string ("\\f", printcharfun);
1715 else if (c > '\177')
1717 /* Use octal escapes to avoid encoding issues. */
1718 len = sprintf (buf, "\\%o", c);
1719 strout (buf, len, len, printcharfun);
1721 else
1723 if (c == '\"' || c == '\\')
1724 printchar ('\\', printcharfun);
1725 printchar (c, printcharfun);
1729 if (size_in_chars < real_size_in_chars)
1730 print_c_string (" ...", printcharfun);
1731 printchar ('\"', printcharfun);
1733 else if (SUBRP (obj))
1735 print_c_string ("#<subr ", printcharfun);
1736 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1737 printchar ('>', printcharfun);
1739 else if (WINDOWP (obj))
1741 int len = sprintf (buf, "#<window %"pI"d",
1742 XWINDOW (obj)->sequence_number);
1743 strout (buf, len, len, printcharfun);
1744 if (BUFFERP (XWINDOW (obj)->contents))
1746 print_c_string (" on ", printcharfun);
1747 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1748 printcharfun);
1750 printchar ('>', printcharfun);
1752 else if (TERMINALP (obj))
1754 struct terminal *t = XTERMINAL (obj);
1755 int len = sprintf (buf, "#<terminal %d", t->id);
1756 strout (buf, len, len, printcharfun);
1757 if (t->name)
1759 print_c_string (" on ", printcharfun);
1760 print_c_string (t->name, printcharfun);
1762 printchar ('>', printcharfun);
1764 else if (HASH_TABLE_P (obj))
1766 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1767 ptrdiff_t i;
1768 ptrdiff_t real_size, size;
1769 int len;
1770 #if 0
1771 void *ptr = h;
1772 print_c_string ("#<hash-table", printcharfun);
1773 if (SYMBOLP (h->test))
1775 print_c_string (" '", printcharfun);
1776 print_c_string (SSDATA (SYMBOL_NAME (h->test)), printcharfun);
1777 printchar (' ', printcharfun);
1778 print_c_string (SSDATA (SYMBOL_NAME (h->weak)), printcharfun);
1779 len = sprintf (buf, " %"pD"d/%"pD"d", h->count, ASIZE (h->next));
1780 strout (buf, len, len, printcharfun);
1782 len = sprintf (buf, " %p>", ptr);
1783 strout (buf, len, len, printcharfun);
1784 #endif
1785 /* Implement a readable output, e.g.:
1786 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1787 /* Always print the size. */
1788 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1789 strout (buf, len, len, printcharfun);
1791 if (!NILP (h->test.name))
1793 print_c_string (" test ", printcharfun);
1794 print_object (h->test.name, printcharfun, escapeflag);
1797 if (!NILP (h->weak))
1799 print_c_string (" weakness ", printcharfun);
1800 print_object (h->weak, printcharfun, escapeflag);
1803 if (!NILP (h->rehash_size))
1805 print_c_string (" rehash-size ", printcharfun);
1806 print_object (h->rehash_size, printcharfun, escapeflag);
1809 if (!NILP (h->rehash_threshold))
1811 print_c_string (" rehash-threshold ", printcharfun);
1812 print_object (h->rehash_threshold, printcharfun, escapeflag);
1815 print_c_string (" data ", printcharfun);
1817 /* Print the data here as a plist. */
1818 real_size = HASH_TABLE_SIZE (h);
1819 size = real_size;
1821 /* Don't print more elements than the specified maximum. */
1822 if (NATNUMP (Vprint_length)
1823 && XFASTINT (Vprint_length) < size)
1824 size = XFASTINT (Vprint_length);
1826 printchar ('(', printcharfun);
1827 for (i = 0; i < size; i++)
1828 if (!NILP (HASH_HASH (h, i)))
1830 if (i) printchar (' ', printcharfun);
1831 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1832 printchar (' ', printcharfun);
1833 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1836 if (size < real_size)
1837 print_c_string (" ...", printcharfun);
1839 print_c_string ("))", printcharfun);
1842 else if (BUFFERP (obj))
1844 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1845 print_c_string ("#<killed buffer>", printcharfun);
1846 else if (escapeflag)
1848 print_c_string ("#<buffer ", printcharfun);
1849 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1850 printchar ('>', printcharfun);
1852 else
1853 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1855 else if (WINDOW_CONFIGURATIONP (obj))
1856 print_c_string ("#<window-configuration>", printcharfun);
1857 else if (FRAMEP (obj))
1859 int len;
1860 void *ptr = XFRAME (obj);
1861 Lisp_Object frame_name = XFRAME (obj)->name;
1863 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1864 ? "#<frame "
1865 : "#<dead frame "),
1866 printcharfun);
1867 if (!STRINGP (frame_name))
1869 /* A frame could be too young and have no name yet;
1870 don't crash. */
1871 if (SYMBOLP (frame_name))
1872 frame_name = Fsymbol_name (frame_name);
1873 else /* can't happen: name should be either nil or string */
1874 frame_name = build_string ("*INVALID*FRAME*NAME*");
1876 print_string (frame_name, printcharfun);
1877 len = sprintf (buf, " %p>", ptr);
1878 strout (buf, len, len, printcharfun);
1880 else if (FONTP (obj))
1882 int i;
1884 if (! FONT_OBJECT_P (obj))
1886 if (FONT_SPEC_P (obj))
1887 print_c_string ("#<font-spec", printcharfun);
1888 else
1889 print_c_string ("#<font-entity", printcharfun);
1890 for (i = 0; i < FONT_SPEC_MAX; i++)
1892 printchar (' ', printcharfun);
1893 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1894 print_object (AREF (obj, i), printcharfun, escapeflag);
1895 else
1896 print_object (font_style_symbolic (obj, i, 0),
1897 printcharfun, escapeflag);
1900 else
1902 print_c_string ("#<font-object ", printcharfun);
1903 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1904 escapeflag);
1906 printchar ('>', printcharfun);
1908 else
1910 ptrdiff_t size = ASIZE (obj);
1911 if (COMPILEDP (obj))
1913 printchar ('#', printcharfun);
1914 size &= PSEUDOVECTOR_SIZE_MASK;
1916 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1918 /* We print a char-table as if it were a vector,
1919 lumping the parent and default slots in with the
1920 character slots. But we add #^ as a prefix. */
1922 /* Make each lowest sub_char_table start a new line.
1923 Otherwise we'll make a line extremely long, which
1924 results in slow redisplay. */
1925 if (SUB_CHAR_TABLE_P (obj)
1926 && XSUB_CHAR_TABLE (obj)->depth == 3)
1927 printchar ('\n', printcharfun);
1928 print_c_string ("#^", printcharfun);
1929 if (SUB_CHAR_TABLE_P (obj))
1930 printchar ('^', printcharfun);
1931 size &= PSEUDOVECTOR_SIZE_MASK;
1933 if (size & PSEUDOVECTOR_FLAG)
1934 goto badtype;
1936 printchar ('[', printcharfun);
1938 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1939 Lisp_Object tem;
1940 ptrdiff_t real_size = size;
1942 /* For a sub char-table, print heading non-Lisp data first. */
1943 if (SUB_CHAR_TABLE_P (obj))
1945 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1946 XSUB_CHAR_TABLE (obj)->min_char);
1947 strout (buf, i, i, printcharfun);
1950 /* Don't print more elements than the specified maximum. */
1951 if (NATNUMP (Vprint_length)
1952 && XFASTINT (Vprint_length) < size)
1953 size = XFASTINT (Vprint_length);
1955 for (i = idx; i < size; i++)
1957 if (i) printchar (' ', printcharfun);
1958 tem = AREF (obj, i);
1959 print_object (tem, printcharfun, escapeflag);
1961 if (size < real_size)
1962 print_c_string (" ...", printcharfun);
1964 printchar (']', printcharfun);
1966 break;
1968 case Lisp_Misc:
1969 switch (XMISCTYPE (obj))
1971 case Lisp_Misc_Marker:
1972 print_c_string ("#<marker ", printcharfun);
1973 /* Do you think this is necessary? */
1974 if (XMARKER (obj)->insertion_type != 0)
1975 print_c_string ("(moves after insertion) ", printcharfun);
1976 if (! XMARKER (obj)->buffer)
1977 print_c_string ("in no buffer", printcharfun);
1978 else
1980 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1981 strout (buf, len, len, printcharfun);
1982 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1984 printchar ('>', printcharfun);
1985 break;
1987 case Lisp_Misc_Overlay:
1988 print_c_string ("#<overlay ", printcharfun);
1989 if (! XMARKER (OVERLAY_START (obj))->buffer)
1990 print_c_string ("in no buffer", printcharfun);
1991 else
1993 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
1994 marker_position (OVERLAY_START (obj)),
1995 marker_position (OVERLAY_END (obj)));
1996 strout (buf, len, len, printcharfun);
1997 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
1998 printcharfun);
2000 printchar ('>', printcharfun);
2001 break;
2003 #ifdef HAVE_MODULES
2004 case Lisp_Misc_User_Ptr:
2006 print_c_string ("#<user-ptr ", printcharfun);
2007 int i = sprintf (buf, "ptr=%p finalizer=%p",
2008 XUSER_PTR (obj)->p,
2009 XUSER_PTR (obj)->finalizer);
2010 strout (buf, i, i, printcharfun);
2011 printchar ('>', printcharfun);
2012 break;
2014 #endif
2016 case Lisp_Misc_Finalizer:
2017 print_c_string ("#<finalizer", printcharfun);
2018 if (NILP (XFINALIZER (obj)->function))
2019 print_c_string (" used", printcharfun);
2020 printchar ('>', printcharfun);
2021 break;
2023 /* Remaining cases shouldn't happen in normal usage, but let's
2024 print them anyway for the benefit of the debugger. */
2026 case Lisp_Misc_Free:
2027 print_c_string ("#<misc free cell>", printcharfun);
2028 break;
2030 case Lisp_Misc_Save_Value:
2032 int i;
2033 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2035 print_c_string ("#<save-value ", printcharfun);
2037 if (v->save_type == SAVE_TYPE_MEMORY)
2039 ptrdiff_t amount = v->data[1].integer;
2041 /* valid_lisp_object_p is reliable, so try to print up
2042 to 8 saved objects. This code is rarely used, so
2043 it's OK that valid_lisp_object_p is slow. */
2045 int limit = min (amount, 8);
2046 Lisp_Object *area = v->data[0].pointer;
2048 i = sprintf (buf, "with %"pD"d objects", amount);
2049 strout (buf, i, i, printcharfun);
2051 for (i = 0; i < limit; i++)
2053 Lisp_Object maybe = area[i];
2054 int valid = valid_lisp_object_p (maybe);
2056 printchar (' ', printcharfun);
2057 if (0 < valid)
2058 print_object (maybe, printcharfun, escapeflag);
2059 else
2060 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2061 printcharfun);
2063 if (i == limit && i < amount)
2064 print_c_string (" ...", printcharfun);
2066 else
2068 /* Print each slot according to its type. */
2069 int index;
2070 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2072 if (index)
2073 printchar (' ', printcharfun);
2075 switch (save_type (v, index))
2077 case SAVE_UNUSED:
2078 i = sprintf (buf, "<unused>");
2079 break;
2081 case SAVE_POINTER:
2082 i = sprintf (buf, "<pointer %p>",
2083 v->data[index].pointer);
2084 break;
2086 case SAVE_FUNCPOINTER:
2087 i = sprintf (buf, "<funcpointer %p>",
2088 ((void *) (intptr_t)
2089 v->data[index].funcpointer));
2090 break;
2092 case SAVE_INTEGER:
2093 i = sprintf (buf, "<integer %"pD"d>",
2094 v->data[index].integer);
2095 break;
2097 case SAVE_OBJECT:
2098 print_object (v->data[index].object, printcharfun,
2099 escapeflag);
2100 continue;
2102 default:
2103 emacs_abort ();
2106 strout (buf, i, i, printcharfun);
2109 printchar ('>', printcharfun);
2111 break;
2113 default:
2114 goto badtype;
2116 break;
2118 default:
2119 badtype:
2121 int len;
2122 /* We're in trouble if this happens!
2123 Probably should just emacs_abort (). */
2124 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2125 if (MISCP (obj))
2126 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2127 else if (VECTORLIKEP (obj))
2128 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2129 else
2130 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2131 strout (buf, len, len, printcharfun);
2132 print_c_string ((" Save your buffers immediately"
2133 " and please report this bug>"),
2134 printcharfun);
2138 print_depth--;
2142 /* Print a description of INTERVAL using PRINTCHARFUN.
2143 This is part of printing a string that has text properties. */
2145 static void
2146 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2148 if (NILP (interval->plist))
2149 return;
2150 printchar (' ', printcharfun);
2151 print_object (make_number (interval->position), printcharfun, 1);
2152 printchar (' ', printcharfun);
2153 print_object (make_number (interval->position + LENGTH (interval)),
2154 printcharfun, 1);
2155 printchar (' ', printcharfun);
2156 print_object (interval->plist, printcharfun, 1);
2159 /* Initialize debug_print stuff early to have it working from the very
2160 beginning. */
2162 void
2163 init_print_once (void)
2165 /* The subroutine object for external-debugging-output is kept here
2166 for the convenience of the debugger. */
2167 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2169 defsubr (&Sexternal_debugging_output);
2172 void
2173 syms_of_print (void)
2175 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2177 DEFVAR_LISP ("standard-output", Vstandard_output,
2178 doc: /* Output stream `print' uses by default for outputting a character.
2179 This may be any function of one argument.
2180 It may also be a buffer (output is inserted before point)
2181 or a marker (output is inserted and the marker is advanced)
2182 or the symbol t (output appears in the echo area). */);
2183 Vstandard_output = Qt;
2184 DEFSYM (Qstandard_output, "standard-output");
2186 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2187 doc: /* The format descriptor string used to print floats.
2188 This is a %-spec like those accepted by `printf' in C,
2189 but with some restrictions. It must start with the two characters `%.'.
2190 After that comes an integer precision specification,
2191 and then a letter which controls the format.
2192 The letters allowed are `e', `f' and `g'.
2193 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2194 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2195 Use `g' to choose the shorter of those two formats for the number at hand.
2196 The precision in any of these cases is the number of digits following
2197 the decimal point. With `f', a precision of 0 means to omit the
2198 decimal point. 0 is not allowed with `e' or `g'.
2200 A value of nil means to use the shortest notation
2201 that represents the number without losing information. */);
2202 Vfloat_output_format = Qnil;
2204 DEFVAR_LISP ("print-length", Vprint_length,
2205 doc: /* Maximum length of list to print before abbreviating.
2206 A value of nil means no limit. See also `eval-expression-print-length'. */);
2207 Vprint_length = Qnil;
2209 DEFVAR_LISP ("print-level", Vprint_level,
2210 doc: /* Maximum depth of list nesting to print before abbreviating.
2211 A value of nil means no limit. See also `eval-expression-print-level'. */);
2212 Vprint_level = Qnil;
2214 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2215 doc: /* Non-nil means print newlines in strings as `\\n'.
2216 Also print formfeeds as `\\f'. */);
2217 print_escape_newlines = 0;
2219 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2220 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2221 (OOO is the octal representation of the character code.)
2222 Only single-byte characters are affected, and only in `prin1'.
2223 When the output goes in a multibyte buffer, this feature is
2224 enabled regardless of the value of the variable. */);
2225 print_escape_nonascii = 0;
2227 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2228 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2229 (XXXX is the hex representation of the character code.)
2230 This affects only `prin1'. */);
2231 print_escape_multibyte = 0;
2233 DEFVAR_BOOL ("print-quoted", print_quoted,
2234 doc: /* Non-nil means print quoted forms with reader syntax.
2235 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2236 print_quoted = 0;
2238 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2239 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2240 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2241 When the uninterned symbol appears within a recursive data structure,
2242 and the symbol appears more than once, in addition use the #N# and #N=
2243 constructs as needed, so that multiple references to the same symbol are
2244 shared once again when the text is read back. */);
2245 Vprint_gensym = Qnil;
2247 DEFVAR_LISP ("print-circle", Vprint_circle,
2248 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2249 If nil, printing proceeds recursively and may lead to
2250 `max-lisp-eval-depth' being exceeded or an error may occur:
2251 \"Apparently circular structure being printed.\" Also see
2252 `print-length' and `print-level'.
2253 If non-nil, shared substructures anywhere in the structure are printed
2254 with `#N=' before the first occurrence (in the order of the print
2255 representation) and `#N#' in place of each subsequent occurrence,
2256 where N is a positive decimal integer. */);
2257 Vprint_circle = Qnil;
2259 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2260 doc: /* Non-nil means number continuously across print calls.
2261 This affects the numbers printed for #N= labels and #M# references.
2262 See also `print-circle', `print-gensym', and `print-number-table'.
2263 This variable should not be set with `setq'; bind it with a `let' instead. */);
2264 Vprint_continuous_numbering = Qnil;
2266 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2267 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2268 The Lisp printer uses this vector to detect Lisp objects referenced more
2269 than once.
2271 When you bind `print-continuous-numbering' to t, you should probably
2272 also bind `print-number-table' to nil. This ensures that the value of
2273 `print-number-table' can be garbage-collected once the printing is
2274 done. If all elements of `print-number-table' are nil, it means that
2275 the printing done so far has not found any shared structure or objects
2276 that need to be recorded in the table. */);
2277 Vprint_number_table = Qnil;
2279 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2280 doc: /* A flag to control printing of `charset' text property on printing a string.
2281 The value must be nil, t, or `default'.
2283 If the value is nil, don't print the text property `charset'.
2285 If the value is t, always print the text property `charset'.
2287 If the value is `default', print the text property `charset' only when
2288 the value is different from what is guessed in the current charset
2289 priorities. */);
2290 Vprint_charset_text_property = Qdefault;
2292 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2293 staticpro (&Vprin1_to_string_buffer);
2295 defsubr (&Sprin1);
2296 defsubr (&Sprin1_to_string);
2297 defsubr (&Serror_message_string);
2298 defsubr (&Sprinc);
2299 defsubr (&Sprint);
2300 defsubr (&Sterpri);
2301 defsubr (&Swrite_char);
2302 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2303 defsubr (&Sredirect_debugging_output);
2304 #endif
2306 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2307 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2308 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2310 print_prune_charset_plist = Qnil;
2311 staticpro (&print_prune_charset_plist);