Minor edits in CONTRIBUTE
[emacs.git] / src / print.c
blob916276bc961426b454a932c729845e81870e956c
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 "termchar.h"
35 #include "intervals.h"
36 #include "blockinput.h"
37 #include "termhooks.h" /* For struct terminal. */
38 #include "font.h"
40 #include <float.h>
41 #include <ftoastr.h>
43 /* Avoid actual stack overflow in print. */
44 static ptrdiff_t print_depth;
46 /* Level of nesting inside outputting backquote in new style. */
47 static ptrdiff_t new_backquote_output;
49 /* Detect most circularities to print finite output. */
50 #define PRINT_CIRCLE 200
51 static Lisp_Object being_printed[PRINT_CIRCLE];
53 /* Last char printed to stdout by printchar. */
54 static unsigned int printchar_stdout_last;
56 /* When printing into a buffer, first we put the text in this
57 block, then insert it all at once. */
58 static char *print_buffer;
60 /* Size allocated in print_buffer. */
61 static ptrdiff_t print_buffer_size;
62 /* Chars stored in print_buffer. */
63 static ptrdiff_t print_buffer_pos;
64 /* Bytes stored in print_buffer. */
65 static ptrdiff_t print_buffer_pos_byte;
67 /* Vprint_number_table is a table, that keeps objects that are going to
68 be printed, to allow use of #n= and #n# to express sharing.
69 For any given object, the table can give the following values:
70 t the object will be printed only once.
71 -N the object will be printed several times and will take number N.
72 N the object has been printed so we can refer to it as #N#.
73 print_number_index holds the largest N already used.
74 N has to be striclty larger than 0 since we need to distinguish -N. */
75 static ptrdiff_t print_number_index;
76 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
78 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
79 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
82 /* Low level output routines for characters and strings. */
84 /* Lisp functions to do output using a stream
85 must have the stream in a variable called printcharfun
86 and must start with PRINTPREPARE, end with PRINTFINISH.
87 Use printchar to output one character,
88 or call strout to output a block of characters. */
90 #define PRINTPREPARE \
91 struct buffer *old = current_buffer; \
92 ptrdiff_t old_point = -1, start_point = -1; \
93 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
94 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
95 bool free_print_buffer = 0; \
96 bool multibyte \
97 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
98 Lisp_Object original = printcharfun; \
99 if (NILP (printcharfun)) printcharfun = Qt; \
100 if (BUFFERP (printcharfun)) \
102 if (XBUFFER (printcharfun) != current_buffer) \
103 Fset_buffer (printcharfun); \
104 printcharfun = Qnil; \
106 if (MARKERP (printcharfun)) \
108 ptrdiff_t marker_pos; \
109 if (! XMARKER (printcharfun)->buffer) \
110 error ("Marker does not point anywhere"); \
111 if (XMARKER (printcharfun)->buffer != current_buffer) \
112 set_buffer_internal (XMARKER (printcharfun)->buffer); \
113 marker_pos = marker_position (printcharfun); \
114 if (marker_pos < BEGV || marker_pos > ZV) \
115 signal_error ("Marker is outside the accessible " \
116 "part of the buffer", printcharfun); \
117 old_point = PT; \
118 old_point_byte = PT_BYTE; \
119 SET_PT_BOTH (marker_pos, \
120 marker_byte_position (printcharfun)); \
121 start_point = PT; \
122 start_point_byte = PT_BYTE; \
123 printcharfun = Qnil; \
125 if (NILP (printcharfun)) \
127 Lisp_Object string; \
128 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
129 && ! print_escape_multibyte) \
130 specbind (Qprint_escape_multibyte, Qt); \
131 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
132 && ! print_escape_nonascii) \
133 specbind (Qprint_escape_nonascii, Qt); \
134 if (print_buffer != 0) \
136 string = make_string_from_bytes (print_buffer, \
137 print_buffer_pos, \
138 print_buffer_pos_byte); \
139 record_unwind_protect (print_unwind, string); \
141 else \
143 int new_size = 1000; \
144 print_buffer = xmalloc (new_size); \
145 print_buffer_size = new_size; \
146 free_print_buffer = 1; \
148 print_buffer_pos = 0; \
149 print_buffer_pos_byte = 0; \
151 if (EQ (printcharfun, Qt) && ! noninteractive) \
152 setup_echo_area_for_printing (multibyte);
154 #define PRINTFINISH \
155 if (NILP (printcharfun)) \
157 if (print_buffer_pos != print_buffer_pos_byte \
158 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
160 USE_SAFE_ALLOCA; \
161 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
162 copy_text ((unsigned char *) print_buffer, temp, \
163 print_buffer_pos_byte, 1, 0); \
164 insert_1_both ((char *) temp, print_buffer_pos, \
165 print_buffer_pos, 0, 1, 0); \
166 SAFE_FREE (); \
168 else \
169 insert_1_both (print_buffer, print_buffer_pos, \
170 print_buffer_pos_byte, 0, 1, 0); \
171 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
173 if (free_print_buffer) \
175 xfree (print_buffer); \
176 print_buffer = 0; \
178 unbind_to (specpdl_count, Qnil); \
179 if (MARKERP (original)) \
180 set_marker_both (original, Qnil, PT, PT_BYTE); \
181 if (old_point >= 0) \
182 SET_PT_BOTH (old_point + (old_point >= start_point \
183 ? PT - start_point : 0), \
184 old_point_byte + (old_point_byte >= start_point_byte \
185 ? PT_BYTE - start_point_byte : 0)); \
186 set_buffer_internal (old);
188 /* This is used to restore the saved contents of print_buffer
189 when there is a recursive call to print. */
191 static void
192 print_unwind (Lisp_Object saved_text)
194 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
198 /* Print character CH using method FUN. FUN nil means print to
199 print_buffer. FUN t means print to echo area or stdout if
200 non-interactive. If FUN is neither nil nor t, call FUN with CH as
201 argument. */
203 static void
204 printchar (unsigned int ch, Lisp_Object fun)
206 if (!NILP (fun) && !EQ (fun, Qt))
207 call1 (fun, make_number (ch));
208 else
210 unsigned char str[MAX_MULTIBYTE_LENGTH];
211 int len = CHAR_STRING (ch, str);
213 QUIT;
215 if (NILP (fun))
217 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
218 if (incr > 0)
219 print_buffer = xpalloc (print_buffer, &print_buffer_size,
220 incr, -1, 1);
221 memcpy (print_buffer + print_buffer_pos_byte, str, len);
222 print_buffer_pos += 1;
223 print_buffer_pos_byte += len;
225 else if (noninteractive)
227 printchar_stdout_last = ch;
228 fwrite (str, 1, len, stdout);
229 noninteractive_need_newline = 1;
231 else
233 bool multibyte_p
234 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
236 setup_echo_area_for_printing (multibyte_p);
237 insert_char (ch);
238 message_dolog ((char *) str, len, 0, multibyte_p);
244 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
245 method PRINTCHARFUN. PRINTCHARFUN nil means output to
246 print_buffer. PRINTCHARFUN t means output to the echo area or to
247 stdout if non-interactive. If neither nil nor t, call Lisp
248 function PRINTCHARFUN for each character printed. MULTIBYTE
249 non-zero means PTR contains multibyte characters.
251 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
252 to data in a Lisp string. Otherwise that is not safe. */
254 static void
255 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
256 Lisp_Object printcharfun)
258 if (NILP (printcharfun))
260 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
261 if (incr > 0)
262 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
263 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
264 print_buffer_pos += size;
265 print_buffer_pos_byte += size_byte;
267 else if (noninteractive && EQ (printcharfun, Qt))
269 fwrite (ptr, 1, size_byte, stdout);
270 noninteractive_need_newline = 1;
272 else if (EQ (printcharfun, Qt))
274 /* Output to echo area. We're trying to avoid a little overhead
275 here, that's the reason we don't call printchar to do the
276 job. */
277 int i;
278 bool multibyte_p
279 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
281 setup_echo_area_for_printing (multibyte_p);
282 message_dolog (ptr, size_byte, 0, multibyte_p);
284 if (size == size_byte)
286 for (i = 0; i < size; ++i)
287 insert_char ((unsigned char) *ptr++);
289 else
291 int len;
292 for (i = 0; i < size_byte; i += len)
294 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
295 len);
296 insert_char (ch);
300 else
302 /* PRINTCHARFUN is a Lisp function. */
303 ptrdiff_t i = 0;
305 if (size == size_byte)
307 while (i < size_byte)
309 int ch = ptr[i++];
310 printchar (ch, printcharfun);
313 else
315 while (i < size_byte)
317 /* Here, we must convert each multi-byte form to the
318 corresponding character code before handing it to
319 PRINTCHAR. */
320 int len;
321 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
322 len);
323 printchar (ch, printcharfun);
324 i += len;
330 /* Print the contents of a string STRING using PRINTCHARFUN.
331 It isn't safe to use strout in many cases,
332 because printing one char can relocate. */
334 static void
335 print_string (Lisp_Object string, Lisp_Object printcharfun)
337 if (EQ (printcharfun, Qt) || NILP (printcharfun))
339 ptrdiff_t chars;
341 if (print_escape_nonascii)
342 string = string_escape_byte8 (string);
344 if (STRING_MULTIBYTE (string))
345 chars = SCHARS (string);
346 else if (! print_escape_nonascii
347 && (EQ (printcharfun, Qt)
348 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
349 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
351 /* If unibyte string STRING contains 8-bit codes, we must
352 convert STRING to a multibyte string containing the same
353 character codes. */
354 Lisp_Object newstr;
355 ptrdiff_t bytes;
357 chars = SBYTES (string);
358 bytes = count_size_as_multibyte (SDATA (string), chars);
359 if (chars < bytes)
361 newstr = make_uninit_multibyte_string (chars, bytes);
362 memcpy (SDATA (newstr), SDATA (string), chars);
363 str_to_multibyte (SDATA (newstr), bytes, chars);
364 string = newstr;
367 else
368 chars = SBYTES (string);
370 if (EQ (printcharfun, Qt))
372 /* Output to echo area. */
373 ptrdiff_t nbytes = SBYTES (string);
375 /* Copy the string contents so that relocation of STRING by
376 GC does not cause trouble. */
377 USE_SAFE_ALLOCA;
378 char *buffer = SAFE_ALLOCA (nbytes);
379 memcpy (buffer, SDATA (string), nbytes);
381 strout (buffer, chars, nbytes, printcharfun);
383 SAFE_FREE ();
385 else
386 /* No need to copy, since output to print_buffer can't GC. */
387 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
389 else
391 /* Otherwise, string may be relocated by printing one char.
392 So re-fetch the string address for each character. */
393 ptrdiff_t i;
394 ptrdiff_t size = SCHARS (string);
395 ptrdiff_t size_byte = SBYTES (string);
396 struct gcpro gcpro1;
397 GCPRO1 (string);
398 if (size == size_byte)
399 for (i = 0; i < size; i++)
400 printchar (SREF (string, i), printcharfun);
401 else
402 for (i = 0; i < size_byte; )
404 /* Here, we must convert each multi-byte form to the
405 corresponding character code before handing it to PRINTCHAR. */
406 int len;
407 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
408 printchar (ch, printcharfun);
409 i += len;
411 UNGCPRO;
415 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
416 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
417 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
418 (Lisp_Object character, Lisp_Object printcharfun)
420 if (NILP (printcharfun))
421 printcharfun = Vstandard_output;
422 CHECK_NUMBER (character);
423 PRINTPREPARE;
424 printchar (XINT (character), printcharfun);
425 PRINTFINISH;
426 return character;
429 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
430 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
431 Do not use this on the contents of a Lisp string. */
433 static void
434 print_c_string (char const *string, Lisp_Object printcharfun)
436 ptrdiff_t len = strlen (string);
437 strout (string, len, len, printcharfun);
440 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
441 Do not use this on the contents of a Lisp string. */
443 static void
444 write_string_1 (const char *data, Lisp_Object printcharfun)
446 PRINTPREPARE;
447 print_c_string (data, printcharfun);
448 PRINTFINISH;
451 /* Used from outside of print.c to print a C unibyte
452 string at DATA on the default output stream.
453 Do not use this on the contents of a Lisp string. */
455 void
456 write_string (const char *data)
458 write_string_1 (data, Vstandard_output);
462 void
463 temp_output_buffer_setup (const char *bufname)
465 ptrdiff_t count = SPECPDL_INDEX ();
466 register struct buffer *old = current_buffer;
467 register Lisp_Object buf;
469 record_unwind_current_buffer ();
471 Fset_buffer (Fget_buffer_create (build_string (bufname)));
473 Fkill_all_local_variables ();
474 delete_all_overlays (current_buffer);
475 bset_directory (current_buffer, BVAR (old, directory));
476 bset_read_only (current_buffer, Qnil);
477 bset_filename (current_buffer, Qnil);
478 bset_undo_list (current_buffer, Qt);
479 eassert (current_buffer->overlays_before == NULL);
480 eassert (current_buffer->overlays_after == NULL);
481 bset_enable_multibyte_characters
482 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
483 specbind (Qinhibit_read_only, Qt);
484 specbind (Qinhibit_modification_hooks, Qt);
485 Ferase_buffer ();
486 XSETBUFFER (buf, current_buffer);
488 run_hook (Qtemp_buffer_setup_hook);
490 unbind_to (count, Qnil);
492 specbind (Qstandard_output, buf);
495 static void print (Lisp_Object, Lisp_Object, bool);
496 static void print_preprocess (Lisp_Object);
497 static void print_preprocess_string (INTERVAL, Lisp_Object);
498 static void print_object (Lisp_Object, Lisp_Object, bool);
500 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
501 doc: /* Output a newline to stream PRINTCHARFUN.
502 If ENSURE is non-nil only output a newline if not already at the
503 beginning of a line. Value is non-nil if a newline is printed.
504 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
505 (Lisp_Object printcharfun, Lisp_Object ensure)
507 Lisp_Object val;
509 if (NILP (printcharfun))
510 printcharfun = Vstandard_output;
511 PRINTPREPARE;
513 if (NILP (ensure))
514 val = Qt;
515 /* Difficult to check if at line beginning so abort. */
516 else if (FUNCTIONP (printcharfun))
517 signal_error ("Unsupported function argument", printcharfun);
518 else if (noninteractive && !NILP (printcharfun))
519 val = printchar_stdout_last == 10 ? Qnil : Qt;
520 else
521 val = NILP (Fbolp ()) ? Qt : Qnil;
523 if (!NILP (val))
524 printchar ('\n', printcharfun);
525 PRINTFINISH;
526 return val;
529 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
530 doc: /* Output the printed representation of OBJECT, any Lisp object.
531 Quoting characters are printed when needed to make output that `read'
532 can handle, whenever this is possible. For complex objects, the behavior
533 is controlled by `print-level' and `print-length', which see.
535 OBJECT is any of the Lisp data types: a number, a string, a symbol,
536 a list, a buffer, a window, a frame, etc.
538 A printed representation of an object is text which describes that object.
540 Optional argument PRINTCHARFUN is the output stream, which can be one
541 of these:
543 - a buffer, in which case output is inserted into that buffer at point;
544 - a marker, in which case output is inserted at marker's position;
545 - a function, in which case that function is called once for each
546 character of OBJECT's printed representation;
547 - a symbol, in which case that symbol's function definition is called; or
548 - t, in which case the output is displayed in the echo area.
550 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
551 is used instead. */)
552 (Lisp_Object object, Lisp_Object printcharfun)
554 if (NILP (printcharfun))
555 printcharfun = Vstandard_output;
556 PRINTPREPARE;
557 print (object, printcharfun, 1);
558 PRINTFINISH;
559 return object;
562 /* a buffer which is used to hold output being built by prin1-to-string */
563 Lisp_Object Vprin1_to_string_buffer;
565 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
566 doc: /* Return a string containing the printed representation of OBJECT.
567 OBJECT can be any Lisp object. This function outputs quoting characters
568 when necessary to make output that `read' can handle, whenever possible,
569 unless the optional second argument NOESCAPE is non-nil. For complex objects,
570 the behavior is controlled by `print-level' and `print-length', which see.
572 OBJECT is any of the Lisp data types: a number, a string, a symbol,
573 a list, a buffer, a window, a frame, etc.
575 A printed representation of an object is text which describes that object. */)
576 (Lisp_Object object, Lisp_Object noescape)
578 ptrdiff_t count = SPECPDL_INDEX ();
580 specbind (Qinhibit_modification_hooks, Qt);
582 /* Save and restore this: we are altering a buffer
583 but we don't want to deactivate the mark just for that.
584 No need for specbind, since errors deactivate the mark. */
585 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
586 bool prev_abort_on_gc = abort_on_gc;
587 abort_on_gc = true;
589 Lisp_Object printcharfun = Vprin1_to_string_buffer;
590 PRINTPREPARE;
591 print (object, printcharfun, NILP (noescape));
592 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
593 PRINTFINISH;
595 struct buffer *previous = current_buffer;
596 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
597 object = Fbuffer_string ();
598 if (SBYTES (object) == SCHARS (object))
599 STRING_SET_UNIBYTE (object);
601 /* Note that this won't make prepare_to_modify_buffer call
602 ask-user-about-supersession-threat because this buffer
603 does not visit a file. */
604 Ferase_buffer ();
605 set_buffer_internal (previous);
607 Vdeactivate_mark = save_deactivate_mark;
609 abort_on_gc = prev_abort_on_gc;
610 return unbind_to (count, object);
613 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
614 doc: /* Output the printed representation of OBJECT, any Lisp object.
615 No quoting characters are used; no delimiters are printed around
616 the contents of strings.
618 OBJECT is any of the Lisp data types: a number, a string, a symbol,
619 a list, a buffer, a window, a frame, etc.
621 A printed representation of an object is text which describes that object.
623 Optional argument PRINTCHARFUN is the output stream, which can be one
624 of these:
626 - a buffer, in which case output is inserted into that buffer at point;
627 - a marker, in which case output is inserted at marker's position;
628 - a function, in which case that function is called once for each
629 character of OBJECT's printed representation;
630 - a symbol, in which case that symbol's function definition is called; or
631 - t, in which case the output is displayed in the echo area.
633 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
634 is used instead. */)
635 (Lisp_Object object, Lisp_Object printcharfun)
637 if (NILP (printcharfun))
638 printcharfun = Vstandard_output;
639 PRINTPREPARE;
640 print (object, printcharfun, 0);
641 PRINTFINISH;
642 return object;
645 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
646 doc: /* Output the printed representation of OBJECT, with newlines around it.
647 Quoting characters are printed when needed to make output that `read'
648 can handle, whenever this is possible. For complex objects, the behavior
649 is controlled by `print-level' and `print-length', which see.
651 OBJECT is any of the Lisp data types: a number, a string, a symbol,
652 a list, a buffer, a window, a frame, etc.
654 A printed representation of an object is text which describes that object.
656 Optional argument PRINTCHARFUN is the output stream, which can be one
657 of these:
659 - a buffer, in which case output is inserted into that buffer at point;
660 - a marker, in which case output is inserted at marker's position;
661 - a function, in which case that function is called once for each
662 character of OBJECT's printed representation;
663 - a symbol, in which case that symbol's function definition is called; or
664 - t, in which case the output is displayed in the echo area.
666 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
667 is used instead. */)
668 (Lisp_Object object, Lisp_Object printcharfun)
670 struct gcpro gcpro1;
672 if (NILP (printcharfun))
673 printcharfun = Vstandard_output;
674 GCPRO1 (object);
675 PRINTPREPARE;
676 printchar ('\n', printcharfun);
677 print (object, printcharfun, 1);
678 printchar ('\n', printcharfun);
679 PRINTFINISH;
680 UNGCPRO;
681 return object;
684 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
685 doc: /* Write CHARACTER to stderr.
686 You can call print while debugging emacs, and pass it this function
687 to make it write to the debugging output. */)
688 (Lisp_Object character)
690 unsigned int ch;
692 CHECK_NUMBER (character);
693 ch = XINT (character);
694 if (ASCII_CHAR_P (ch))
696 putc (ch, stderr);
697 #ifdef WINDOWSNT
698 /* Send the output to a debugger (nothing happens if there isn't
699 one). */
700 if (print_output_debug_flag)
702 char buf[2] = {(char) XINT (character), '\0'};
703 OutputDebugString (buf);
705 #endif
707 else
709 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
710 ptrdiff_t len = CHAR_STRING (ch, mbstr);
711 Lisp_Object encoded_ch =
712 ENCODE_SYSTEM (make_multibyte_string ((char *) mbstr, 1, len));
714 fwrite (SSDATA (encoded_ch), SBYTES (encoded_ch), 1, stderr);
715 #ifdef WINDOWSNT
716 if (print_output_debug_flag)
717 OutputDebugString (SSDATA (encoded_ch));
718 #endif
721 return character;
724 /* This function is never called. Its purpose is to prevent
725 print_output_debug_flag from being optimized away. */
727 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
728 void
729 debug_output_compilation_hack (bool x)
731 print_output_debug_flag = x;
734 #if defined (GNU_LINUX)
736 /* This functionality is not vitally important in general, so we rely on
737 non-portable ability to use stderr as lvalue. */
739 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
741 static FILE *initial_stderr_stream = NULL;
743 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
744 1, 2,
745 "FDebug output file: \nP",
746 doc: /* Redirect debugging output (stderr stream) to file FILE.
747 If FILE is nil, reset target to the initial stderr stream.
748 Optional arg APPEND non-nil (interactively, with prefix arg) means
749 append to existing target file. */)
750 (Lisp_Object file, Lisp_Object append)
752 if (initial_stderr_stream != NULL)
754 block_input ();
755 fclose (stderr);
756 unblock_input ();
758 stderr = initial_stderr_stream;
759 initial_stderr_stream = NULL;
761 if (STRINGP (file))
763 file = Fexpand_file_name (file, Qnil);
764 initial_stderr_stream = stderr;
765 stderr = emacs_fopen (SSDATA (file), NILP (append) ? "w" : "a");
766 if (stderr == NULL)
768 stderr = initial_stderr_stream;
769 initial_stderr_stream = NULL;
770 report_file_error ("Cannot open debugging output stream", file);
773 return Qnil;
775 #endif /* GNU_LINUX */
778 /* This is the interface for debugging printing. */
780 void
781 debug_print (Lisp_Object arg)
783 Fprin1 (arg, Qexternal_debugging_output);
784 fprintf (stderr, "\r\n");
787 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
788 void
789 safe_debug_print (Lisp_Object arg)
791 int valid = valid_lisp_object_p (arg);
793 if (valid > 0)
794 debug_print (arg);
795 else
796 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
797 !valid ? "INVALID" : "SOME",
798 XLI (arg));
802 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
803 1, 1, 0,
804 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
805 See Info anchor `(elisp)Definition of signal' for some details on how this
806 error message is constructed. */)
807 (Lisp_Object obj)
809 struct buffer *old = current_buffer;
810 Lisp_Object value;
811 struct gcpro gcpro1;
813 /* If OBJ is (error STRING), just return STRING.
814 That is not only faster, it also avoids the need to allocate
815 space here when the error is due to memory full. */
816 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
817 && CONSP (XCDR (obj))
818 && STRINGP (XCAR (XCDR (obj)))
819 && NILP (XCDR (XCDR (obj))))
820 return XCAR (XCDR (obj));
822 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
824 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
825 value = Fbuffer_string ();
827 GCPRO1 (value);
828 Ferase_buffer ();
829 set_buffer_internal (old);
830 UNGCPRO;
832 return value;
835 /* Print an error message for the error DATA onto Lisp output stream
836 STREAM (suitable for the print functions).
837 CONTEXT is a C string describing the context of the error.
838 CALLER is the Lisp function inside which the error was signaled. */
840 void
841 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
842 Lisp_Object caller)
844 Lisp_Object errname, errmsg, file_error, tail;
845 struct gcpro gcpro1;
847 if (context != 0)
848 write_string_1 (context, stream);
850 /* If we know from where the error was signaled, show it in
851 *Messages*. */
852 if (!NILP (caller) && SYMBOLP (caller))
854 Lisp_Object cname = SYMBOL_NAME (caller);
855 ptrdiff_t cnamelen = SBYTES (cname);
856 USE_SAFE_ALLOCA;
857 char *name = SAFE_ALLOCA (cnamelen);
858 memcpy (name, SDATA (cname), cnamelen);
859 message_dolog (name, cnamelen, 0, 0);
860 message_dolog (": ", 2, 0, 0);
861 SAFE_FREE ();
864 errname = Fcar (data);
866 if (EQ (errname, Qerror))
868 data = Fcdr (data);
869 if (!CONSP (data))
870 data = Qnil;
871 errmsg = Fcar (data);
872 file_error = Qnil;
874 else
876 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
877 errmsg = Fget (errname, Qerror_message);
878 file_error = Fmemq (Qfile_error, error_conditions);
881 /* Print an error message including the data items. */
883 tail = Fcdr_safe (data);
884 GCPRO1 (tail);
886 /* For file-error, make error message by concatenating
887 all the data items. They are all strings. */
888 if (!NILP (file_error) && CONSP (tail))
889 errmsg = XCAR (tail), tail = XCDR (tail);
892 const char *sep = ": ";
894 if (!STRINGP (errmsg))
895 write_string_1 ("peculiar error", stream);
896 else if (SCHARS (errmsg))
897 Fprinc (errmsg, stream);
898 else
899 sep = NULL;
901 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
903 Lisp_Object obj;
905 if (sep)
906 write_string_1 (sep, stream);
907 obj = XCAR (tail);
908 if (!NILP (file_error)
909 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
910 Fprinc (obj, stream);
911 else
912 Fprin1 (obj, stream);
916 UNGCPRO;
922 * The buffer should be at least as large as the max string size of the
923 * largest float, printed in the biggest notation. This is undoubtedly
924 * 20d float_output_format, with the negative of the C-constant "HUGE"
925 * from <math.h>.
927 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
929 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
930 * case of -1e307 in 20d float_output_format. What is one to do (short of
931 * re-writing _doprnt to be more sane)?
932 * -wsr
933 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
937 float_to_string (char *buf, double data)
939 char *cp;
940 int width;
941 int len;
943 /* Check for plus infinity in a way that won't lose
944 if there is no plus infinity. */
945 if (data == data / 2 && data > 1.0)
947 static char const infinity_string[] = "1.0e+INF";
948 strcpy (buf, infinity_string);
949 return sizeof infinity_string - 1;
951 /* Likewise for minus infinity. */
952 if (data == data / 2 && data < -1.0)
954 static char const minus_infinity_string[] = "-1.0e+INF";
955 strcpy (buf, minus_infinity_string);
956 return sizeof minus_infinity_string - 1;
958 /* Check for NaN in a way that won't fail if there are no NaNs. */
959 if (! (data * 0.0 >= 0.0))
961 /* Prepend "-" if the NaN's sign bit is negative.
962 The sign bit of a double is the bit that is 1 in -0.0. */
963 static char const NaN_string[] = "0.0e+NaN";
964 int i;
965 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
966 bool negative = 0;
967 u_data.d = data;
968 u_minus_zero.d = - 0.0;
969 for (i = 0; i < sizeof (double); i++)
970 if (u_data.c[i] & u_minus_zero.c[i])
972 *buf = '-';
973 negative = 1;
974 break;
977 strcpy (buf + negative, NaN_string);
978 return negative + sizeof NaN_string - 1;
981 if (NILP (Vfloat_output_format)
982 || !STRINGP (Vfloat_output_format))
983 lose:
985 /* Generate the fewest number of digits that represent the
986 floating point value without losing information. */
987 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
988 /* The decimal point must be printed, or the byte compiler can
989 get confused (Bug#8033). */
990 width = 1;
992 else /* oink oink */
994 /* Check that the spec we have is fully valid.
995 This means not only valid for printf,
996 but meant for floats, and reasonable. */
997 cp = SSDATA (Vfloat_output_format);
999 if (cp[0] != '%')
1000 goto lose;
1001 if (cp[1] != '.')
1002 goto lose;
1004 cp += 2;
1006 /* Check the width specification. */
1007 width = -1;
1008 if ('0' <= *cp && *cp <= '9')
1010 width = 0;
1013 width = (width * 10) + (*cp++ - '0');
1014 if (DBL_DIG < width)
1015 goto lose;
1017 while (*cp >= '0' && *cp <= '9');
1019 /* A precision of zero is valid only for %f. */
1020 if (width == 0 && *cp != 'f')
1021 goto lose;
1024 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1025 goto lose;
1027 if (cp[1] != 0)
1028 goto lose;
1030 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1033 /* Make sure there is a decimal point with digit after, or an
1034 exponent, so that the value is readable as a float. But don't do
1035 this with "%.0f"; it's valid for that not to produce a decimal
1036 point. Note that width can be 0 only for %.0f. */
1037 if (width != 0)
1039 for (cp = buf; *cp; cp++)
1040 if ((*cp < '0' || *cp > '9') && *cp != '-')
1041 break;
1043 if (*cp == '.' && cp[1] == 0)
1045 cp[1] = '0';
1046 cp[2] = 0;
1047 len++;
1049 else if (*cp == 0)
1051 *cp++ = '.';
1052 *cp++ = '0';
1053 *cp++ = 0;
1054 len += 2;
1058 return len;
1062 static void
1063 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1065 new_backquote_output = 0;
1067 /* Reset print_number_index and Vprint_number_table only when
1068 the variable Vprint_continuous_numbering is nil. Otherwise,
1069 the values of these variables will be kept between several
1070 print functions. */
1071 if (NILP (Vprint_continuous_numbering)
1072 || NILP (Vprint_number_table))
1074 print_number_index = 0;
1075 Vprint_number_table = Qnil;
1078 /* Construct Vprint_number_table for print-gensym and print-circle. */
1079 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1081 /* Construct Vprint_number_table.
1082 This increments print_number_index for the objects added. */
1083 print_depth = 0;
1084 print_preprocess (obj);
1086 if (HASH_TABLE_P (Vprint_number_table))
1087 { /* Remove unnecessary objects, which appear only once in OBJ;
1088 that is, whose status is Qt. */
1089 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1090 ptrdiff_t i;
1092 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1093 if (!NILP (HASH_HASH (h, i))
1094 && EQ (HASH_VALUE (h, i), Qt))
1095 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1099 print_depth = 0;
1100 print_object (obj, printcharfun, escapeflag);
1103 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1104 (STRINGP (obj) || CONSP (obj) \
1105 || (VECTORLIKEP (obj) \
1106 && (VECTORP (obj) || COMPILEDP (obj) \
1107 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1108 || HASH_TABLE_P (obj) || FONTP (obj))) \
1109 || (! NILP (Vprint_gensym) \
1110 && SYMBOLP (obj) \
1111 && !SYMBOL_INTERNED_P (obj)))
1113 /* Construct Vprint_number_table according to the structure of OBJ.
1114 OBJ itself and all its elements will be added to Vprint_number_table
1115 recursively if it is a list, vector, compiled function, char-table,
1116 string (its text properties will be traced), or a symbol that has
1117 no obarray (this is for the print-gensym feature).
1118 The status fields of Vprint_number_table mean whether each object appears
1119 more than once in OBJ: Qnil at the first time, and Qt after that. */
1120 static void
1121 print_preprocess (Lisp_Object obj)
1123 int i;
1124 ptrdiff_t size;
1125 int loop_count = 0;
1126 Lisp_Object halftail;
1128 /* Avoid infinite recursion for circular nested structure
1129 in the case where Vprint_circle is nil. */
1130 if (NILP (Vprint_circle))
1132 /* Give up if we go so deep that print_object will get an error. */
1133 /* See similar code in print_object. */
1134 if (print_depth >= PRINT_CIRCLE)
1135 error ("Apparently circular structure being printed");
1137 for (i = 0; i < print_depth; i++)
1138 if (EQ (obj, being_printed[i]))
1139 return;
1140 being_printed[print_depth] = obj;
1143 print_depth++;
1144 halftail = obj;
1146 loop:
1147 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1149 if (!HASH_TABLE_P (Vprint_number_table))
1150 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1152 /* In case print-circle is nil and print-gensym is t,
1153 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1154 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1156 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1157 if (!NILP (num)
1158 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1159 always print the gensym with a number. This is a special for
1160 the lisp function byte-compile-output-docform. */
1161 || (!NILP (Vprint_continuous_numbering)
1162 && SYMBOLP (obj)
1163 && !SYMBOL_INTERNED_P (obj)))
1164 { /* OBJ appears more than once. Let's remember that. */
1165 if (!INTEGERP (num))
1167 print_number_index++;
1168 /* Negative number indicates it hasn't been printed yet. */
1169 Fputhash (obj, make_number (- print_number_index),
1170 Vprint_number_table);
1172 print_depth--;
1173 return;
1175 else
1176 /* OBJ is not yet recorded. Let's add to the table. */
1177 Fputhash (obj, Qt, Vprint_number_table);
1180 switch (XTYPE (obj))
1182 case Lisp_String:
1183 /* A string may have text properties, which can be circular. */
1184 traverse_intervals_noorder (string_intervals (obj),
1185 print_preprocess_string, Qnil);
1186 break;
1188 case Lisp_Cons:
1189 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1190 just as in print_object. */
1191 if (loop_count && EQ (obj, halftail))
1192 break;
1193 print_preprocess (XCAR (obj));
1194 obj = XCDR (obj);
1195 loop_count++;
1196 if (!(loop_count & 1))
1197 halftail = XCDR (halftail);
1198 goto loop;
1200 case Lisp_Vectorlike:
1201 size = ASIZE (obj);
1202 if (size & PSEUDOVECTOR_FLAG)
1203 size &= PSEUDOVECTOR_SIZE_MASK;
1204 for (i = (SUB_CHAR_TABLE_P (obj)
1205 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1206 print_preprocess (AREF (obj, i));
1207 if (HASH_TABLE_P (obj))
1208 { /* For hash tables, the key_and_value slot is past
1209 `size' because it needs to be marked specially in case
1210 the table is weak. */
1211 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1212 print_preprocess (h->key_and_value);
1214 break;
1216 default:
1217 break;
1220 print_depth--;
1223 static void
1224 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1226 print_preprocess (interval->plist);
1229 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1231 #define PRINT_STRING_NON_CHARSET_FOUND 1
1232 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1234 /* Bitwise or of the above macros. */
1235 static int print_check_string_result;
1237 static void
1238 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1240 Lisp_Object val;
1242 if (NILP (interval->plist)
1243 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1244 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1245 return;
1246 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1247 val = XCDR (XCDR (val)));
1248 if (! CONSP (val))
1250 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1251 return;
1253 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1255 if (! EQ (val, interval->plist)
1256 || CONSP (XCDR (XCDR (val))))
1257 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1259 if (NILP (Vprint_charset_text_property)
1260 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1262 int i, c;
1263 ptrdiff_t charpos = interval->position;
1264 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1265 Lisp_Object charset;
1267 charset = XCAR (XCDR (val));
1268 for (i = 0; i < LENGTH (interval); i++)
1270 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1271 if (! ASCII_CHAR_P (c)
1272 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1274 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1275 break;
1281 /* The value is (charset . nil). */
1282 static Lisp_Object print_prune_charset_plist;
1284 static Lisp_Object
1285 print_prune_string_charset (Lisp_Object string)
1287 print_check_string_result = 0;
1288 traverse_intervals (string_intervals (string), 0,
1289 print_check_string_charset_prop, string);
1290 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1292 string = Fcopy_sequence (string);
1293 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1295 if (NILP (print_prune_charset_plist))
1296 print_prune_charset_plist = list1 (Qcharset);
1297 Fremove_text_properties (make_number (0),
1298 make_number (SCHARS (string)),
1299 print_prune_charset_plist, string);
1301 else
1302 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1303 Qnil, string);
1305 return string;
1308 static void
1309 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1311 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1312 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1313 40))];
1315 QUIT;
1317 /* Detect circularities and truncate them. */
1318 if (NILP (Vprint_circle))
1320 /* Simple but incomplete way. */
1321 int i;
1323 /* See similar code in print_preprocess. */
1324 if (print_depth >= PRINT_CIRCLE)
1325 error ("Apparently circular structure being printed");
1327 for (i = 0; i < print_depth; i++)
1328 if (EQ (obj, being_printed[i]))
1330 int len = sprintf (buf, "#%d", i);
1331 strout (buf, len, len, printcharfun);
1332 return;
1334 being_printed[print_depth] = obj;
1336 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1338 /* With the print-circle feature. */
1339 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1340 if (INTEGERP (num))
1342 EMACS_INT n = XINT (num);
1343 if (n < 0)
1344 { /* Add a prefix #n= if OBJ has not yet been printed;
1345 that is, its status field is nil. */
1346 int len = sprintf (buf, "#%"pI"d=", -n);
1347 strout (buf, len, len, printcharfun);
1348 /* OBJ is going to be printed. Remember that fact. */
1349 Fputhash (obj, make_number (- n), Vprint_number_table);
1351 else
1353 /* Just print #n# if OBJ has already been printed. */
1354 int len = sprintf (buf, "#%"pI"d#", n);
1355 strout (buf, len, len, printcharfun);
1356 return;
1361 print_depth++;
1363 switch (XTYPE (obj))
1365 case_Lisp_Int:
1367 int len = sprintf (buf, "%"pI"d", XINT (obj));
1368 strout (buf, len, len, printcharfun);
1370 break;
1372 case Lisp_Float:
1374 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1375 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1376 strout (pigbuf, len, len, printcharfun);
1378 break;
1380 case Lisp_String:
1381 if (!escapeflag)
1382 print_string (obj, printcharfun);
1383 else
1385 register ptrdiff_t i, i_byte;
1386 struct gcpro gcpro1;
1387 ptrdiff_t size_byte;
1388 /* 1 means we must ensure that the next character we output
1389 cannot be taken as part of a hex character escape. */
1390 bool need_nonhex = 0;
1391 bool multibyte = STRING_MULTIBYTE (obj);
1393 GCPRO1 (obj);
1395 if (! EQ (Vprint_charset_text_property, Qt))
1396 obj = print_prune_string_charset (obj);
1398 if (string_intervals (obj))
1399 print_c_string ("#(", printcharfun);
1401 printchar ('\"', printcharfun);
1402 size_byte = SBYTES (obj);
1404 for (i = 0, i_byte = 0; i_byte < size_byte;)
1406 /* Here, we must convert each multi-byte form to the
1407 corresponding character code before handing it to printchar. */
1408 int c;
1410 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1412 QUIT;
1414 if (c == '\n' && print_escape_newlines)
1415 print_c_string ("\\n", printcharfun);
1416 else if (c == '\f' && print_escape_newlines)
1417 print_c_string ("\\f", printcharfun);
1418 else if (multibyte
1419 && (CHAR_BYTE8_P (c)
1420 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1422 /* When multibyte is disabled,
1423 print multibyte string chars using hex escapes.
1424 For a char code that could be in a unibyte string,
1425 when found in a multibyte string, always use a hex escape
1426 so it reads back as multibyte. */
1427 char outbuf[50];
1428 int len;
1430 if (CHAR_BYTE8_P (c))
1431 len = sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1432 else
1434 len = sprintf (outbuf, "\\x%04x", c);
1435 need_nonhex = 1;
1437 strout (outbuf, len, len, printcharfun);
1439 else if (! multibyte
1440 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1441 && print_escape_nonascii)
1443 /* When printing in a multibyte buffer
1444 or when explicitly requested,
1445 print single-byte non-ASCII string chars
1446 using octal escapes. */
1447 char outbuf[5];
1448 int len = sprintf (outbuf, "\\%03o", c);
1449 strout (outbuf, len, len, printcharfun);
1451 else
1453 /* If we just had a hex escape, and this character
1454 could be taken as part of it,
1455 output `\ ' to prevent that. */
1456 if (need_nonhex)
1458 need_nonhex = 0;
1459 if ((c >= 'a' && c <= 'f')
1460 || (c >= 'A' && c <= 'F')
1461 || (c >= '0' && c <= '9'))
1462 print_c_string ("\\ ", printcharfun);
1465 if (c == '\"' || c == '\\')
1466 printchar ('\\', printcharfun);
1467 printchar (c, printcharfun);
1470 printchar ('\"', printcharfun);
1472 if (string_intervals (obj))
1474 traverse_intervals (string_intervals (obj),
1475 0, print_interval, printcharfun);
1476 printchar (')', printcharfun);
1479 UNGCPRO;
1481 break;
1483 case Lisp_Symbol:
1485 bool confusing;
1486 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1487 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1488 int c;
1489 ptrdiff_t i, i_byte;
1490 ptrdiff_t size_byte;
1491 Lisp_Object name;
1493 name = SYMBOL_NAME (obj);
1495 if (p != end && (*p == '-' || *p == '+')) p++;
1496 if (p == end)
1497 confusing = 0;
1498 /* If symbol name begins with a digit, and ends with a digit,
1499 and contains nothing but digits and `e', it could be treated
1500 as a number. So set CONFUSING.
1502 Symbols that contain periods could also be taken as numbers,
1503 but periods are always escaped, so we don't have to worry
1504 about them here. */
1505 else if (*p >= '0' && *p <= '9'
1506 && end[-1] >= '0' && end[-1] <= '9')
1508 while (p != end && ((*p >= '0' && *p <= '9')
1509 /* Needed for \2e10. */
1510 || *p == 'e' || *p == 'E'))
1511 p++;
1512 confusing = (end == p);
1514 else
1515 confusing = 0;
1517 size_byte = SBYTES (name);
1519 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1520 print_c_string ("#:", printcharfun);
1521 else if (size_byte == 0)
1523 print_c_string ("##", printcharfun);
1524 break;
1527 for (i = 0, i_byte = 0; i_byte < size_byte;)
1529 /* Here, we must convert each multi-byte form to the
1530 corresponding character code before handing it to PRINTCHAR. */
1531 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1532 QUIT;
1534 if (escapeflag)
1536 if (c == '\"' || c == '\\' || c == '\''
1537 || c == ';' || c == '#' || c == '(' || c == ')'
1538 || c == ',' || c == '.' || c == '`'
1539 || c == '[' || c == ']' || c == '?' || c <= 040
1540 || confusing)
1542 printchar ('\\', printcharfun);
1543 confusing = false;
1546 printchar (c, printcharfun);
1549 break;
1551 case Lisp_Cons:
1552 /* If deeper than spec'd depth, print placeholder. */
1553 if (INTEGERP (Vprint_level)
1554 && print_depth > XINT (Vprint_level))
1555 print_c_string ("...", printcharfun);
1556 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1557 && (EQ (XCAR (obj), Qquote)))
1559 printchar ('\'', printcharfun);
1560 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1562 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1563 && (EQ (XCAR (obj), Qfunction)))
1565 print_c_string ("#'", printcharfun);
1566 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1568 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1569 && ((EQ (XCAR (obj), Qbackquote))))
1571 print_object (XCAR (obj), printcharfun, 0);
1572 new_backquote_output++;
1573 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1574 new_backquote_output--;
1576 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1577 && new_backquote_output
1578 && ((EQ (XCAR (obj), Qbackquote)
1579 || EQ (XCAR (obj), Qcomma)
1580 || EQ (XCAR (obj), Qcomma_at)
1581 || EQ (XCAR (obj), Qcomma_dot))))
1583 print_object (XCAR (obj), printcharfun, 0);
1584 new_backquote_output--;
1585 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1586 new_backquote_output++;
1588 else
1590 printchar ('(', printcharfun);
1592 Lisp_Object halftail = obj;
1594 /* Negative values of print-length are invalid in CL.
1595 Treat them like nil, as CMUCL does. */
1596 printmax_t print_length = (NATNUMP (Vprint_length)
1597 ? XFASTINT (Vprint_length)
1598 : TYPE_MAXIMUM (printmax_t));
1600 printmax_t i = 0;
1601 while (CONSP (obj))
1603 /* Detect circular list. */
1604 if (NILP (Vprint_circle))
1606 /* Simple but incomplete way. */
1607 if (i != 0 && EQ (obj, halftail))
1609 int len = sprintf (buf, " . #%"pMd, i / 2);
1610 strout (buf, len, len, printcharfun);
1611 goto end_of_list;
1614 else
1616 /* With the print-circle feature. */
1617 if (i != 0)
1619 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1620 if (INTEGERP (num))
1622 print_c_string (" . ", printcharfun);
1623 print_object (obj, printcharfun, escapeflag);
1624 goto end_of_list;
1629 if (i)
1630 printchar (' ', printcharfun);
1632 if (print_length <= i)
1634 print_c_string ("...", printcharfun);
1635 goto end_of_list;
1638 i++;
1639 print_object (XCAR (obj), printcharfun, escapeflag);
1641 obj = XCDR (obj);
1642 if (!(i & 1))
1643 halftail = XCDR (halftail);
1646 /* OBJ non-nil here means it's the end of a dotted list. */
1647 if (!NILP (obj))
1649 print_c_string (" . ", printcharfun);
1650 print_object (obj, printcharfun, escapeflag);
1653 end_of_list:
1654 printchar (')', printcharfun);
1656 break;
1658 case Lisp_Vectorlike:
1659 if (PROCESSP (obj))
1661 if (escapeflag)
1663 print_c_string ("#<process ", printcharfun);
1664 print_string (XPROCESS (obj)->name, printcharfun);
1665 printchar ('>', printcharfun);
1667 else
1668 print_string (XPROCESS (obj)->name, printcharfun);
1670 else if (BOOL_VECTOR_P (obj))
1672 ptrdiff_t i;
1673 unsigned char c;
1674 struct gcpro gcpro1;
1675 EMACS_INT size = bool_vector_size (obj);
1676 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1677 ptrdiff_t real_size_in_chars = size_in_chars;
1678 GCPRO1 (obj);
1680 int len = sprintf (buf, "#&%"pI"d\"", size);
1681 strout (buf, len, len, printcharfun);
1683 /* Don't print more characters than the specified maximum.
1684 Negative values of print-length are invalid. Treat them
1685 like a print-length of nil. */
1686 if (NATNUMP (Vprint_length)
1687 && XFASTINT (Vprint_length) < size_in_chars)
1688 size_in_chars = XFASTINT (Vprint_length);
1690 for (i = 0; i < size_in_chars; i++)
1692 QUIT;
1693 c = bool_vector_uchar_data (obj)[i];
1694 if (c == '\n' && print_escape_newlines)
1695 print_c_string ("\\n", printcharfun);
1696 else if (c == '\f' && print_escape_newlines)
1697 print_c_string ("\\f", printcharfun);
1698 else if (c > '\177')
1700 /* Use octal escapes to avoid encoding issues. */
1701 len = sprintf (buf, "\\%o", c);
1702 strout (buf, len, len, printcharfun);
1704 else
1706 if (c == '\"' || c == '\\')
1707 printchar ('\\', printcharfun);
1708 printchar (c, printcharfun);
1712 if (size_in_chars < real_size_in_chars)
1713 print_c_string (" ...", printcharfun);
1714 printchar ('\"', printcharfun);
1716 UNGCPRO;
1718 else if (SUBRP (obj))
1720 print_c_string ("#<subr ", printcharfun);
1721 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1722 printchar ('>', printcharfun);
1724 else if (WINDOWP (obj))
1726 int len = sprintf (buf, "#<window %"pI"d",
1727 XWINDOW (obj)->sequence_number);
1728 strout (buf, len, len, printcharfun);
1729 if (BUFFERP (XWINDOW (obj)->contents))
1731 print_c_string (" on ", printcharfun);
1732 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1733 printcharfun);
1735 printchar ('>', printcharfun);
1737 else if (TERMINALP (obj))
1739 struct terminal *t = XTERMINAL (obj);
1740 int len = sprintf (buf, "#<terminal %d", t->id);
1741 strout (buf, len, len, printcharfun);
1742 if (t->name)
1744 print_c_string (" on ", printcharfun);
1745 print_c_string (t->name, printcharfun);
1747 printchar ('>', printcharfun);
1749 else if (HASH_TABLE_P (obj))
1751 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1752 ptrdiff_t i;
1753 ptrdiff_t real_size, size;
1754 int len;
1755 #if 0
1756 void *ptr = h;
1757 print_c_string ("#<hash-table", printcharfun);
1758 if (SYMBOLP (h->test))
1760 print_c_string (" '", printcharfun);
1761 print_c_string (SSDATA (SYMBOL_NAME (h->test)), printcharfun);
1762 printchar (' ', printcharfun);
1763 print_c_string (SSDATA (SYMBOL_NAME (h->weak)), printcharfun);
1764 len = sprintf (buf, " %"pD"d/%"pD"d", h->count, ASIZE (h->next));
1765 strout (buf, len, len, printcharfun);
1767 len = sprintf (buf, " %p>", ptr);
1768 strout (buf, len, len, printcharfun);
1769 #endif
1770 /* Implement a readable output, e.g.:
1771 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1772 /* Always print the size. */
1773 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1774 strout (buf, len, len, printcharfun);
1776 if (!NILP (h->test.name))
1778 print_c_string (" test ", printcharfun);
1779 print_object (h->test.name, printcharfun, escapeflag);
1782 if (!NILP (h->weak))
1784 print_c_string (" weakness ", printcharfun);
1785 print_object (h->weak, printcharfun, escapeflag);
1788 if (!NILP (h->rehash_size))
1790 print_c_string (" rehash-size ", printcharfun);
1791 print_object (h->rehash_size, printcharfun, escapeflag);
1794 if (!NILP (h->rehash_threshold))
1796 print_c_string (" rehash-threshold ", printcharfun);
1797 print_object (h->rehash_threshold, printcharfun, escapeflag);
1800 print_c_string (" data ", printcharfun);
1802 /* Print the data here as a plist. */
1803 real_size = HASH_TABLE_SIZE (h);
1804 size = real_size;
1806 /* Don't print more elements than the specified maximum. */
1807 if (NATNUMP (Vprint_length)
1808 && XFASTINT (Vprint_length) < size)
1809 size = XFASTINT (Vprint_length);
1811 printchar ('(', printcharfun);
1812 for (i = 0; i < size; i++)
1813 if (!NILP (HASH_HASH (h, i)))
1815 if (i) printchar (' ', printcharfun);
1816 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1817 printchar (' ', printcharfun);
1818 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1821 if (size < real_size)
1822 print_c_string (" ...", printcharfun);
1824 print_c_string ("))", printcharfun);
1827 else if (BUFFERP (obj))
1829 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1830 print_c_string ("#<killed buffer>", printcharfun);
1831 else if (escapeflag)
1833 print_c_string ("#<buffer ", printcharfun);
1834 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1835 printchar ('>', printcharfun);
1837 else
1838 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1840 else if (WINDOW_CONFIGURATIONP (obj))
1841 print_c_string ("#<window-configuration>", printcharfun);
1842 else if (FRAMEP (obj))
1844 int len;
1845 void *ptr = XFRAME (obj);
1846 Lisp_Object frame_name = XFRAME (obj)->name;
1848 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1849 ? "#<frame "
1850 : "#<dead frame "),
1851 printcharfun);
1852 if (!STRINGP (frame_name))
1854 /* A frame could be too young and have no name yet;
1855 don't crash. */
1856 if (SYMBOLP (frame_name))
1857 frame_name = Fsymbol_name (frame_name);
1858 else /* can't happen: name should be either nil or string */
1859 frame_name = build_string ("*INVALID*FRAME*NAME*");
1861 print_string (frame_name, printcharfun);
1862 len = sprintf (buf, " %p>", ptr);
1863 strout (buf, len, len, printcharfun);
1865 else if (FONTP (obj))
1867 int i;
1869 if (! FONT_OBJECT_P (obj))
1871 if (FONT_SPEC_P (obj))
1872 print_c_string ("#<font-spec", printcharfun);
1873 else
1874 print_c_string ("#<font-entity", printcharfun);
1875 for (i = 0; i < FONT_SPEC_MAX; i++)
1877 printchar (' ', printcharfun);
1878 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1879 print_object (AREF (obj, i), printcharfun, escapeflag);
1880 else
1881 print_object (font_style_symbolic (obj, i, 0),
1882 printcharfun, escapeflag);
1885 else
1887 print_c_string ("#<font-object ", printcharfun);
1888 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1889 escapeflag);
1891 printchar ('>', printcharfun);
1893 else
1895 ptrdiff_t size = ASIZE (obj);
1896 if (COMPILEDP (obj))
1898 printchar ('#', printcharfun);
1899 size &= PSEUDOVECTOR_SIZE_MASK;
1901 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1903 /* We print a char-table as if it were a vector,
1904 lumping the parent and default slots in with the
1905 character slots. But we add #^ as a prefix. */
1907 /* Make each lowest sub_char_table start a new line.
1908 Otherwise we'll make a line extremely long, which
1909 results in slow redisplay. */
1910 if (SUB_CHAR_TABLE_P (obj)
1911 && XSUB_CHAR_TABLE (obj)->depth == 3)
1912 printchar ('\n', printcharfun);
1913 print_c_string ("#^", printcharfun);
1914 if (SUB_CHAR_TABLE_P (obj))
1915 printchar ('^', printcharfun);
1916 size &= PSEUDOVECTOR_SIZE_MASK;
1918 if (size & PSEUDOVECTOR_FLAG)
1919 goto badtype;
1921 printchar ('[', printcharfun);
1923 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1924 Lisp_Object tem;
1925 ptrdiff_t real_size = size;
1927 /* For a sub char-table, print heading non-Lisp data first. */
1928 if (SUB_CHAR_TABLE_P (obj))
1930 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1931 XSUB_CHAR_TABLE (obj)->min_char);
1932 strout (buf, i, i, printcharfun);
1935 /* Don't print more elements than the specified maximum. */
1936 if (NATNUMP (Vprint_length)
1937 && XFASTINT (Vprint_length) < size)
1938 size = XFASTINT (Vprint_length);
1940 for (i = idx; i < size; i++)
1942 if (i) printchar (' ', printcharfun);
1943 tem = AREF (obj, i);
1944 print_object (tem, printcharfun, escapeflag);
1946 if (size < real_size)
1947 print_c_string (" ...", printcharfun);
1949 printchar (']', printcharfun);
1951 break;
1953 case Lisp_Misc:
1954 switch (XMISCTYPE (obj))
1956 case Lisp_Misc_Marker:
1957 print_c_string ("#<marker ", printcharfun);
1958 /* Do you think this is necessary? */
1959 if (XMARKER (obj)->insertion_type != 0)
1960 print_c_string ("(moves after insertion) ", printcharfun);
1961 if (! XMARKER (obj)->buffer)
1962 print_c_string ("in no buffer", printcharfun);
1963 else
1965 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1966 strout (buf, len, len, printcharfun);
1967 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1969 printchar ('>', printcharfun);
1970 break;
1972 case Lisp_Misc_Overlay:
1973 print_c_string ("#<overlay ", printcharfun);
1974 if (! XMARKER (OVERLAY_START (obj))->buffer)
1975 print_c_string ("in no buffer", printcharfun);
1976 else
1978 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
1979 marker_position (OVERLAY_START (obj)),
1980 marker_position (OVERLAY_END (obj)));
1981 strout (buf, len, len, printcharfun);
1982 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
1983 printcharfun);
1985 printchar ('>', printcharfun);
1986 break;
1988 case Lisp_Misc_Finalizer:
1989 print_c_string ("#<finalizer", printcharfun);
1990 if (NILP (XFINALIZER (obj)->function))
1991 print_c_string (" used", printcharfun);
1992 printchar ('>', printcharfun);
1993 break;
1995 /* Remaining cases shouldn't happen in normal usage, but let's
1996 print them anyway for the benefit of the debugger. */
1998 case Lisp_Misc_Free:
1999 print_c_string ("#<misc free cell>", printcharfun);
2000 break;
2002 case Lisp_Misc_Save_Value:
2004 int i;
2005 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2007 print_c_string ("#<save-value ", printcharfun);
2009 if (v->save_type == SAVE_TYPE_MEMORY)
2011 ptrdiff_t amount = v->data[1].integer;
2013 #if GC_MARK_STACK
2015 /* valid_lisp_object_p is reliable, so try to print up
2016 to 8 saved objects. This code is rarely used, so
2017 it's OK that valid_lisp_object_p is slow. */
2019 int limit = min (amount, 8);
2020 Lisp_Object *area = v->data[0].pointer;
2022 i = sprintf (buf, "with %"pD"d objects", amount);
2023 strout (buf, i, i, printcharfun);
2025 for (i = 0; i < limit; i++)
2027 Lisp_Object maybe = area[i];
2028 int valid = valid_lisp_object_p (maybe);
2030 printchar (' ', printcharfun);
2031 if (0 < valid)
2032 print_object (maybe, printcharfun, escapeflag);
2033 else
2034 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2035 printcharfun);
2037 if (i == limit && i < amount)
2038 print_c_string (" ...", printcharfun);
2040 #else /* not GC_MARK_STACK */
2042 /* There is no reliable way to determine whether the objects
2043 are initialized, so do not try to print them. */
2045 i = sprintf (buf, "with %"pD"d objects", amount);
2046 strout (buf, i, i, printcharfun);
2048 #endif /* GC_MARK_STACK */
2050 else
2052 /* Print each slot according to its type. */
2053 int index;
2054 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2056 if (index)
2057 printchar (' ', printcharfun);
2059 switch (save_type (v, index))
2061 case SAVE_UNUSED:
2062 i = sprintf (buf, "<unused>");
2063 break;
2065 case SAVE_POINTER:
2066 i = sprintf (buf, "<pointer %p>",
2067 v->data[index].pointer);
2068 break;
2070 case SAVE_FUNCPOINTER:
2071 i = sprintf (buf, "<funcpointer %p>",
2072 ((void *) (intptr_t)
2073 v->data[index].funcpointer));
2074 break;
2076 case SAVE_INTEGER:
2077 i = sprintf (buf, "<integer %"pD"d>",
2078 v->data[index].integer);
2079 break;
2081 case SAVE_OBJECT:
2082 print_object (v->data[index].object, printcharfun,
2083 escapeflag);
2084 continue;
2086 default:
2087 emacs_abort ();
2090 strout (buf, i, i, printcharfun);
2093 printchar ('>', printcharfun);
2095 break;
2097 default:
2098 goto badtype;
2100 break;
2102 default:
2103 badtype:
2105 int len;
2106 /* We're in trouble if this happens!
2107 Probably should just emacs_abort (). */
2108 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2109 if (MISCP (obj))
2110 len = sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2111 else if (VECTORLIKEP (obj))
2112 len = sprintf (buf, "(PVEC 0x%08"pD"x)", ASIZE (obj));
2113 else
2114 len = sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2115 strout (buf, len, len, printcharfun);
2116 print_c_string ((" Save your buffers immediately"
2117 " and please report this bug>"),
2118 printcharfun);
2122 print_depth--;
2126 /* Print a description of INTERVAL using PRINTCHARFUN.
2127 This is part of printing a string that has text properties. */
2129 static void
2130 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2132 if (NILP (interval->plist))
2133 return;
2134 printchar (' ', printcharfun);
2135 print_object (make_number (interval->position), printcharfun, 1);
2136 printchar (' ', printcharfun);
2137 print_object (make_number (interval->position + LENGTH (interval)),
2138 printcharfun, 1);
2139 printchar (' ', printcharfun);
2140 print_object (interval->plist, printcharfun, 1);
2143 /* Initialize debug_print stuff early to have it working from the very
2144 beginning. */
2146 void
2147 init_print_once (void)
2149 /* The subroutine object for external-debugging-output is kept here
2150 for the convenience of the debugger. */
2151 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2153 defsubr (&Sexternal_debugging_output);
2156 void
2157 syms_of_print (void)
2159 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2161 DEFVAR_LISP ("standard-output", Vstandard_output,
2162 doc: /* Output stream `print' uses by default for outputting a character.
2163 This may be any function of one argument.
2164 It may also be a buffer (output is inserted before point)
2165 or a marker (output is inserted and the marker is advanced)
2166 or the symbol t (output appears in the echo area). */);
2167 Vstandard_output = Qt;
2168 DEFSYM (Qstandard_output, "standard-output");
2170 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2171 doc: /* The format descriptor string used to print floats.
2172 This is a %-spec like those accepted by `printf' in C,
2173 but with some restrictions. It must start with the two characters `%.'.
2174 After that comes an integer precision specification,
2175 and then a letter which controls the format.
2176 The letters allowed are `e', `f' and `g'.
2177 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2178 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2179 Use `g' to choose the shorter of those two formats for the number at hand.
2180 The precision in any of these cases is the number of digits following
2181 the decimal point. With `f', a precision of 0 means to omit the
2182 decimal point. 0 is not allowed with `e' or `g'.
2184 A value of nil means to use the shortest notation
2185 that represents the number without losing information. */);
2186 Vfloat_output_format = Qnil;
2187 DEFSYM (Qfloat_output_format, "float-output-format");
2189 DEFVAR_LISP ("print-length", Vprint_length,
2190 doc: /* Maximum length of list to print before abbreviating.
2191 A value of nil means no limit. See also `eval-expression-print-length'. */);
2192 Vprint_length = Qnil;
2194 DEFVAR_LISP ("print-level", Vprint_level,
2195 doc: /* Maximum depth of list nesting to print before abbreviating.
2196 A value of nil means no limit. See also `eval-expression-print-level'. */);
2197 Vprint_level = Qnil;
2199 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2200 doc: /* Non-nil means print newlines in strings as `\\n'.
2201 Also print formfeeds as `\\f'. */);
2202 print_escape_newlines = 0;
2204 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2205 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2206 \(OOO is the octal representation of the character code.)
2207 Only single-byte characters are affected, and only in `prin1'.
2208 When the output goes in a multibyte buffer, this feature is
2209 enabled regardless of the value of the variable. */);
2210 print_escape_nonascii = 0;
2212 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2213 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2214 \(XXXX is the hex representation of the character code.)
2215 This affects only `prin1'. */);
2216 print_escape_multibyte = 0;
2218 DEFVAR_BOOL ("print-quoted", print_quoted,
2219 doc: /* Non-nil means print quoted forms with reader syntax.
2220 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2221 print_quoted = 0;
2223 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2224 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2225 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2226 When the uninterned symbol appears within a recursive data structure,
2227 and the symbol appears more than once, in addition use the #N# and #N=
2228 constructs as needed, so that multiple references to the same symbol are
2229 shared once again when the text is read back. */);
2230 Vprint_gensym = Qnil;
2232 DEFVAR_LISP ("print-circle", Vprint_circle,
2233 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2234 If nil, printing proceeds recursively and may lead to
2235 `max-lisp-eval-depth' being exceeded or an error may occur:
2236 \"Apparently circular structure being printed.\" Also see
2237 `print-length' and `print-level'.
2238 If non-nil, shared substructures anywhere in the structure are printed
2239 with `#N=' before the first occurrence (in the order of the print
2240 representation) and `#N#' in place of each subsequent occurrence,
2241 where N is a positive decimal integer. */);
2242 Vprint_circle = Qnil;
2244 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2245 doc: /* Non-nil means number continuously across print calls.
2246 This affects the numbers printed for #N= labels and #M# references.
2247 See also `print-circle', `print-gensym', and `print-number-table'.
2248 This variable should not be set with `setq'; bind it with a `let' instead. */);
2249 Vprint_continuous_numbering = Qnil;
2251 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2252 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2253 The Lisp printer uses this vector to detect Lisp objects referenced more
2254 than once.
2256 When you bind `print-continuous-numbering' to t, you should probably
2257 also bind `print-number-table' to nil. This ensures that the value of
2258 `print-number-table' can be garbage-collected once the printing is
2259 done. If all elements of `print-number-table' are nil, it means that
2260 the printing done so far has not found any shared structure or objects
2261 that need to be recorded in the table. */);
2262 Vprint_number_table = Qnil;
2264 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2265 doc: /* A flag to control printing of `charset' text property on printing a string.
2266 The value must be nil, t, or `default'.
2268 If the value is nil, don't print the text property `charset'.
2270 If the value is t, always print the text property `charset'.
2272 If the value is `default', print the text property `charset' only when
2273 the value is different from what is guessed in the current charset
2274 priorities. */);
2275 Vprint_charset_text_property = Qdefault;
2277 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2278 staticpro (&Vprin1_to_string_buffer);
2280 defsubr (&Sprin1);
2281 defsubr (&Sprin1_to_string);
2282 defsubr (&Serror_message_string);
2283 defsubr (&Sprinc);
2284 defsubr (&Sprint);
2285 defsubr (&Sterpri);
2286 defsubr (&Swrite_char);
2287 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2288 defsubr (&Sredirect_debugging_output);
2289 #endif
2291 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2292 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2293 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2295 print_prune_charset_plist = Qnil;
2296 staticpro (&print_prune_charset_plist);