auto upstream
[emacs.git] / src / print.c
blobb5e454385777a679d3c14d6ded7ac3c1403f4807
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2013 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 <stdio.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"
39 #ifdef HAVE_XWIDGETS
40 #include "xwidget.h"
41 #endif
42 Lisp_Object Qstandard_output;
44 static Lisp_Object Qtemp_buffer_setup_hook;
46 /* These are used to print like we read. */
48 static Lisp_Object Qfloat_output_format;
50 #include <float.h>
51 #include <ftoastr.h>
53 /* Avoid actual stack overflow in print. */
54 static ptrdiff_t print_depth;
56 /* Level of nesting inside outputting backquote in new style. */
57 static ptrdiff_t new_backquote_output;
59 /* Detect most circularities to print finite output. */
60 #define PRINT_CIRCLE 200
61 static Lisp_Object being_printed[PRINT_CIRCLE];
63 /* When printing into a buffer, first we put the text in this
64 block, then insert it all at once. */
65 static char *print_buffer;
67 /* Size allocated in print_buffer. */
68 static ptrdiff_t print_buffer_size;
69 /* Chars stored in print_buffer. */
70 static ptrdiff_t print_buffer_pos;
71 /* Bytes stored in print_buffer. */
72 static ptrdiff_t print_buffer_pos_byte;
74 Lisp_Object Qprint_escape_newlines;
75 static Lisp_Object Qprint_escape_multibyte, Qprint_escape_nonascii;
77 /* Vprint_number_table is a table, that keeps objects that are going to
78 be printed, to allow use of #n= and #n# to express sharing.
79 For any given object, the table can give the following values:
80 t the object will be printed only once.
81 -N the object will be printed several times and will take number N.
82 N the object has been printed so we can refer to it as #N#.
83 print_number_index holds the largest N already used.
84 N has to be striclty larger than 0 since we need to distinguish -N. */
85 static ptrdiff_t print_number_index;
86 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
88 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
89 int print_output_debug_flag EXTERNALLY_VISIBLE = 1;
92 /* Low level output routines for characters and strings. */
94 /* Lisp functions to do output using a stream
95 must have the stream in a variable called printcharfun
96 and must start with PRINTPREPARE, end with PRINTFINISH,
97 and use PRINTDECLARE to declare common variables.
98 Use PRINTCHAR to output one character,
99 or call strout to output a block of characters. */
101 #define PRINTDECLARE \
102 struct buffer *old = current_buffer; \
103 ptrdiff_t old_point = -1, start_point = -1; \
104 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
105 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
106 int free_print_buffer = 0; \
107 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
108 Lisp_Object original
110 #define PRINTPREPARE \
111 original = printcharfun; \
112 if (NILP (printcharfun)) printcharfun = Qt; \
113 if (BUFFERP (printcharfun)) \
115 if (XBUFFER (printcharfun) != current_buffer) \
116 Fset_buffer (printcharfun); \
117 printcharfun = Qnil; \
119 if (MARKERP (printcharfun)) \
121 ptrdiff_t marker_pos; \
122 if (! XMARKER (printcharfun)->buffer) \
123 error ("Marker does not point anywhere"); \
124 if (XMARKER (printcharfun)->buffer != current_buffer) \
125 set_buffer_internal (XMARKER (printcharfun)->buffer); \
126 marker_pos = marker_position (printcharfun); \
127 if (marker_pos < BEGV || marker_pos > ZV) \
128 error ("Marker is outside the accessible part of the buffer"); \
129 old_point = PT; \
130 old_point_byte = PT_BYTE; \
131 SET_PT_BOTH (marker_pos, \
132 marker_byte_position (printcharfun)); \
133 start_point = PT; \
134 start_point_byte = PT_BYTE; \
135 printcharfun = Qnil; \
137 if (NILP (printcharfun)) \
139 Lisp_Object string; \
140 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
141 && ! print_escape_multibyte) \
142 specbind (Qprint_escape_multibyte, Qt); \
143 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
144 && ! print_escape_nonascii) \
145 specbind (Qprint_escape_nonascii, Qt); \
146 if (print_buffer != 0) \
148 string = make_string_from_bytes (print_buffer, \
149 print_buffer_pos, \
150 print_buffer_pos_byte); \
151 record_unwind_protect (print_unwind, string); \
153 else \
155 int new_size = 1000; \
156 print_buffer = xmalloc (new_size); \
157 print_buffer_size = new_size; \
158 free_print_buffer = 1; \
160 print_buffer_pos = 0; \
161 print_buffer_pos_byte = 0; \
163 if (EQ (printcharfun, Qt) && ! noninteractive) \
164 setup_echo_area_for_printing (multibyte);
166 #define PRINTFINISH \
167 if (NILP (printcharfun)) \
169 if (print_buffer_pos != print_buffer_pos_byte \
170 && NILP (BVAR (current_buffer, enable_multibyte_characters))) \
172 unsigned char *temp = alloca (print_buffer_pos + 1); \
173 copy_text ((unsigned char *) print_buffer, temp, \
174 print_buffer_pos_byte, 1, 0); \
175 insert_1_both ((char *) temp, print_buffer_pos, \
176 print_buffer_pos, 0, 1, 0); \
178 else \
179 insert_1_both (print_buffer, print_buffer_pos, \
180 print_buffer_pos_byte, 0, 1, 0); \
181 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
183 if (free_print_buffer) \
185 xfree (print_buffer); \
186 print_buffer = 0; \
188 unbind_to (specpdl_count, Qnil); \
189 if (MARKERP (original)) \
190 set_marker_both (original, Qnil, PT, PT_BYTE); \
191 if (old_point >= 0) \
192 SET_PT_BOTH (old_point + (old_point >= start_point \
193 ? PT - start_point : 0), \
194 old_point_byte + (old_point_byte >= start_point_byte \
195 ? PT_BYTE - start_point_byte : 0)); \
196 set_buffer_internal (old);
198 #define PRINTCHAR(ch) printchar (ch, printcharfun)
200 /* This is used to restore the saved contents of print_buffer
201 when there is a recursive call to print. */
203 static Lisp_Object
204 print_unwind (Lisp_Object saved_text)
206 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
207 return Qnil;
211 /* Print character CH using method FUN. FUN nil means print to
212 print_buffer. FUN t means print to echo area or stdout if
213 non-interactive. If FUN is neither nil nor t, call FUN with CH as
214 argument. */
216 static void
217 printchar (unsigned int ch, Lisp_Object fun)
219 if (!NILP (fun) && !EQ (fun, Qt))
220 call1 (fun, make_number (ch));
221 else
223 unsigned char str[MAX_MULTIBYTE_LENGTH];
224 int len = CHAR_STRING (ch, str);
226 QUIT;
228 if (NILP (fun))
230 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
231 if (0 < incr)
232 print_buffer =
233 xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
234 memcpy (print_buffer + print_buffer_pos_byte, str, len);
235 print_buffer_pos += 1;
236 print_buffer_pos_byte += len;
238 else if (noninteractive)
240 fwrite (str, 1, len, stdout);
241 noninteractive_need_newline = 1;
243 else
245 int multibyte_p
246 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
248 setup_echo_area_for_printing (multibyte_p);
249 insert_char (ch);
250 message_dolog ((char *) str, len, 0, multibyte_p);
256 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
257 method PRINTCHARFUN. If SIZE < 0, use the string length of PTR for
258 both SIZE and SIZE_BYTE. PRINTCHARFUN nil means output to
259 print_buffer. PRINTCHARFUN t means output to the echo area or to
260 stdout if non-interactive. If neither nil nor t, call Lisp
261 function PRINTCHARFUN for each character printed. MULTIBYTE
262 non-zero means PTR contains multibyte characters.
264 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
265 to data in a Lisp string. Otherwise that is not safe. */
267 static void
268 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
269 Lisp_Object printcharfun)
271 if (size < 0)
272 size_byte = size = strlen (ptr);
274 if (NILP (printcharfun))
276 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
277 if (0 < incr)
278 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
279 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
280 print_buffer_pos += size;
281 print_buffer_pos_byte += size_byte;
283 else if (noninteractive && EQ (printcharfun, Qt))
285 fwrite (ptr, 1, size_byte, stdout);
286 noninteractive_need_newline = 1;
288 else if (EQ (printcharfun, Qt))
290 /* Output to echo area. We're trying to avoid a little overhead
291 here, that's the reason we don't call printchar to do the
292 job. */
293 int i;
294 int multibyte_p
295 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
297 setup_echo_area_for_printing (multibyte_p);
298 message_dolog (ptr, size_byte, 0, multibyte_p);
300 if (size == size_byte)
302 for (i = 0; i < size; ++i)
303 insert_char ((unsigned char) *ptr++);
305 else
307 int len;
308 for (i = 0; i < size_byte; i += len)
310 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
311 len);
312 insert_char (ch);
316 else
318 /* PRINTCHARFUN is a Lisp function. */
319 ptrdiff_t i = 0;
321 if (size == size_byte)
323 while (i < size_byte)
325 int ch = ptr[i++];
326 PRINTCHAR (ch);
329 else
331 while (i < size_byte)
333 /* Here, we must convert each multi-byte form to the
334 corresponding character code before handing it to
335 PRINTCHAR. */
336 int len;
337 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
338 len);
339 PRINTCHAR (ch);
340 i += len;
346 /* Print the contents of a string STRING using PRINTCHARFUN.
347 It isn't safe to use strout in many cases,
348 because printing one char can relocate. */
350 static void
351 print_string (Lisp_Object string, Lisp_Object printcharfun)
353 if (EQ (printcharfun, Qt) || NILP (printcharfun))
355 ptrdiff_t chars;
357 if (print_escape_nonascii)
358 string = string_escape_byte8 (string);
360 if (STRING_MULTIBYTE (string))
361 chars = SCHARS (string);
362 else if (! print_escape_nonascii
363 && (EQ (printcharfun, Qt)
364 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
365 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
367 /* If unibyte string STRING contains 8-bit codes, we must
368 convert STRING to a multibyte string containing the same
369 character codes. */
370 Lisp_Object newstr;
371 ptrdiff_t bytes;
373 chars = SBYTES (string);
374 bytes = count_size_as_multibyte (SDATA (string), chars);
375 if (chars < bytes)
377 newstr = make_uninit_multibyte_string (chars, bytes);
378 memcpy (SDATA (newstr), SDATA (string), chars);
379 str_to_multibyte (SDATA (newstr), bytes, chars);
380 string = newstr;
383 else
384 chars = SBYTES (string);
386 if (EQ (printcharfun, Qt))
388 /* Output to echo area. */
389 ptrdiff_t nbytes = SBYTES (string);
391 /* Copy the string contents so that relocation of STRING by
392 GC does not cause trouble. */
393 USE_SAFE_ALLOCA;
394 char *buffer = SAFE_ALLOCA (nbytes);
395 memcpy (buffer, SDATA (string), nbytes);
397 strout (buffer, chars, nbytes, printcharfun);
399 SAFE_FREE ();
401 else
402 /* No need to copy, since output to print_buffer can't GC. */
403 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
405 else
407 /* Otherwise, string may be relocated by printing one char.
408 So re-fetch the string address for each character. */
409 ptrdiff_t i;
410 ptrdiff_t size = SCHARS (string);
411 ptrdiff_t size_byte = SBYTES (string);
412 struct gcpro gcpro1;
413 GCPRO1 (string);
414 if (size == size_byte)
415 for (i = 0; i < size; i++)
416 PRINTCHAR (SREF (string, i));
417 else
418 for (i = 0; i < size_byte; )
420 /* Here, we must convert each multi-byte form to the
421 corresponding character code before handing it to PRINTCHAR. */
422 int len;
423 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
424 PRINTCHAR (ch);
425 i += len;
427 UNGCPRO;
431 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
432 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
433 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
434 (Lisp_Object character, Lisp_Object printcharfun)
436 PRINTDECLARE;
438 if (NILP (printcharfun))
439 printcharfun = Vstandard_output;
440 CHECK_NUMBER (character);
441 PRINTPREPARE;
442 PRINTCHAR (XINT (character));
443 PRINTFINISH;
444 return character;
447 /* Used from outside of print.c to print a block of SIZE
448 single-byte chars at DATA on the default output stream.
449 Do not use this on the contents of a Lisp string. */
451 void
452 write_string (const char *data, int size)
454 PRINTDECLARE;
455 Lisp_Object printcharfun;
457 printcharfun = Vstandard_output;
459 PRINTPREPARE;
460 strout (data, size, size, printcharfun);
461 PRINTFINISH;
464 /* Used to print a block of SIZE single-byte chars at DATA on a
465 specified stream PRINTCHARFUN.
466 Do not use this on the contents of a Lisp string. */
468 static void
469 write_string_1 (const char *data, int size, Lisp_Object printcharfun)
471 PRINTDECLARE;
473 PRINTPREPARE;
474 strout (data, size, size, printcharfun);
475 PRINTFINISH;
479 void
480 temp_output_buffer_setup (const char *bufname)
482 ptrdiff_t count = SPECPDL_INDEX ();
483 register struct buffer *old = current_buffer;
484 register Lisp_Object buf;
486 record_unwind_current_buffer ();
488 Fset_buffer (Fget_buffer_create (build_string (bufname)));
490 Fkill_all_local_variables ();
491 delete_all_overlays (current_buffer);
492 bset_directory (current_buffer, BVAR (old, directory));
493 bset_read_only (current_buffer, Qnil);
494 bset_filename (current_buffer, Qnil);
495 bset_undo_list (current_buffer, Qt);
496 eassert (current_buffer->overlays_before == NULL);
497 eassert (current_buffer->overlays_after == NULL);
498 bset_enable_multibyte_characters
499 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
500 specbind (Qinhibit_read_only, Qt);
501 specbind (Qinhibit_modification_hooks, Qt);
502 Ferase_buffer ();
503 XSETBUFFER (buf, current_buffer);
505 Frun_hooks (1, &Qtemp_buffer_setup_hook);
507 unbind_to (count, Qnil);
509 specbind (Qstandard_output, buf);
512 static void print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
513 static void print_preprocess (Lisp_Object obj);
514 static void print_preprocess_string (INTERVAL interval, Lisp_Object arg);
515 static void print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag);
517 DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
518 doc: /* Output a newline to stream PRINTCHARFUN.
519 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
520 (Lisp_Object printcharfun)
522 PRINTDECLARE;
524 if (NILP (printcharfun))
525 printcharfun = Vstandard_output;
526 PRINTPREPARE;
527 PRINTCHAR ('\n');
528 PRINTFINISH;
529 return Qt;
532 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
533 doc: /* Output the printed representation of OBJECT, any Lisp object.
534 Quoting characters are printed when needed to make output that `read'
535 can handle, whenever this is possible. For complex objects, the behavior
536 is controlled by `print-level' and `print-length', which see.
538 OBJECT is any of the Lisp data types: a number, a string, a symbol,
539 a list, a buffer, a window, a frame, etc.
541 A printed representation of an object is text which describes that object.
543 Optional argument PRINTCHARFUN is the output stream, which can be one
544 of these:
546 - a buffer, in which case output is inserted into that buffer at point;
547 - a marker, in which case output is inserted at marker's position;
548 - a function, in which case that function is called once for each
549 character of OBJECT's printed representation;
550 - a symbol, in which case that symbol's function definition is called; or
551 - t, in which case the output is displayed in the echo area.
553 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
554 is used instead. */)
555 (Lisp_Object object, Lisp_Object printcharfun)
557 PRINTDECLARE;
559 if (NILP (printcharfun))
560 printcharfun = Vstandard_output;
561 PRINTPREPARE;
562 print (object, printcharfun, 1);
563 PRINTFINISH;
564 return object;
567 /* a buffer which is used to hold output being built by prin1-to-string */
568 Lisp_Object Vprin1_to_string_buffer;
570 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
571 doc: /* Return a string containing the printed representation of OBJECT.
572 OBJECT can be any Lisp object. This function outputs quoting characters
573 when necessary to make output that `read' can handle, whenever possible,
574 unless the optional second argument NOESCAPE is non-nil. For complex objects,
575 the behavior is controlled by `print-level' and `print-length', which see.
577 OBJECT is any of the Lisp data types: a number, a string, a symbol,
578 a list, a buffer, a window, a frame, etc.
580 A printed representation of an object is text which describes that object. */)
581 (Lisp_Object object, Lisp_Object noescape)
583 Lisp_Object printcharfun;
584 bool prev_abort_on_gc;
585 /* struct gcpro gcpro1, gcpro2; */
586 Lisp_Object save_deactivate_mark;
587 ptrdiff_t count = SPECPDL_INDEX ();
588 struct buffer *previous;
590 specbind (Qinhibit_modification_hooks, Qt);
593 PRINTDECLARE;
595 /* Save and restore this--we are altering a buffer
596 but we don't want to deactivate the mark just for that.
597 No need for specbind, since errors deactivate the mark. */
598 save_deactivate_mark = Vdeactivate_mark;
599 /* GCPRO2 (object, save_deactivate_mark); */
600 prev_abort_on_gc = abort_on_gc;
601 abort_on_gc = 1;
603 printcharfun = Vprin1_to_string_buffer;
604 PRINTPREPARE;
605 print (object, printcharfun, NILP (noescape));
606 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
607 PRINTFINISH;
610 previous = current_buffer;
611 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
612 object = Fbuffer_string ();
613 if (SBYTES (object) == SCHARS (object))
614 STRING_SET_UNIBYTE (object);
616 /* Note that this won't make prepare_to_modify_buffer call
617 ask-user-about-supersession-threat because this buffer
618 does not visit a file. */
619 Ferase_buffer ();
620 set_buffer_internal (previous);
622 Vdeactivate_mark = save_deactivate_mark;
623 /* UNGCPRO; */
625 abort_on_gc = prev_abort_on_gc;
626 return unbind_to (count, object);
629 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
630 doc: /* Output the printed representation of OBJECT, any Lisp object.
631 No quoting characters are used; no delimiters are printed around
632 the contents of strings.
634 OBJECT is any of the Lisp data types: a number, a string, a symbol,
635 a list, a buffer, a window, a frame, etc.
637 A printed representation of an object is text which describes that object.
639 Optional argument PRINTCHARFUN is the output stream, which can be one
640 of these:
642 - a buffer, in which case output is inserted into that buffer at point;
643 - a marker, in which case output is inserted at marker's position;
644 - a function, in which case that function is called once for each
645 character of OBJECT's printed representation;
646 - a symbol, in which case that symbol's function definition is called; or
647 - t, in which case the output is displayed in the echo area.
649 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
650 is used instead. */)
651 (Lisp_Object object, Lisp_Object printcharfun)
653 PRINTDECLARE;
655 if (NILP (printcharfun))
656 printcharfun = Vstandard_output;
657 PRINTPREPARE;
658 print (object, printcharfun, 0);
659 PRINTFINISH;
660 return object;
663 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
664 doc: /* Output the printed representation of OBJECT, with newlines around it.
665 Quoting characters are printed when needed to make output that `read'
666 can handle, whenever this is possible. For complex objects, the behavior
667 is controlled by `print-level' and `print-length', which see.
669 OBJECT is any of the Lisp data types: a number, a string, a symbol,
670 a list, a buffer, a window, a frame, etc.
672 A printed representation of an object is text which describes that object.
674 Optional argument PRINTCHARFUN is the output stream, which can be one
675 of these:
677 - a buffer, in which case output is inserted into that buffer at point;
678 - a marker, in which case output is inserted at marker's position;
679 - a function, in which case that function is called once for each
680 character of OBJECT's printed representation;
681 - a symbol, in which case that symbol's function definition is called; or
682 - t, in which case the output is displayed in the echo area.
684 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
685 is used instead. */)
686 (Lisp_Object object, Lisp_Object printcharfun)
688 PRINTDECLARE;
689 struct gcpro gcpro1;
691 if (NILP (printcharfun))
692 printcharfun = Vstandard_output;
693 GCPRO1 (object);
694 PRINTPREPARE;
695 PRINTCHAR ('\n');
696 print (object, printcharfun, 1);
697 PRINTCHAR ('\n');
698 PRINTFINISH;
699 UNGCPRO;
700 return object;
703 /* The subroutine object for external-debugging-output is kept here
704 for the convenience of the debugger. */
705 Lisp_Object Qexternal_debugging_output;
707 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
708 doc: /* Write CHARACTER to stderr.
709 You can call print while debugging emacs, and pass it this function
710 to make it write to the debugging output. */)
711 (Lisp_Object character)
713 CHECK_NUMBER (character);
714 putc (XINT (character) & 0xFF, stderr);
716 #ifdef WINDOWSNT
717 /* Send the output to a debugger (nothing happens if there isn't one). */
718 if (print_output_debug_flag)
720 char buf[2] = {(char) XINT (character), '\0'};
721 OutputDebugString (buf);
723 #endif
725 return character;
728 /* This function is never called. Its purpose is to prevent
729 print_output_debug_flag from being optimized away. */
731 extern void debug_output_compilation_hack (int) EXTERNALLY_VISIBLE;
732 void
733 debug_output_compilation_hack (int x)
735 print_output_debug_flag = x;
738 #if defined (GNU_LINUX)
740 /* This functionality is not vitally important in general, so we rely on
741 non-portable ability to use stderr as lvalue. */
743 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
745 static FILE *initial_stderr_stream = NULL;
747 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
748 1, 2,
749 "FDebug output file: \nP",
750 doc: /* Redirect debugging output (stderr stream) to file FILE.
751 If FILE is nil, reset target to the initial stderr stream.
752 Optional arg APPEND non-nil (interactively, with prefix arg) means
753 append to existing target file. */)
754 (Lisp_Object file, Lisp_Object append)
756 if (initial_stderr_stream != NULL)
758 block_input ();
759 fclose (stderr);
760 unblock_input ();
762 stderr = initial_stderr_stream;
763 initial_stderr_stream = NULL;
765 if (STRINGP (file))
767 file = Fexpand_file_name (file, Qnil);
768 initial_stderr_stream = stderr;
769 stderr = fopen (SSDATA (file), NILP (append) ? "w" : "a");
770 if (stderr == NULL)
772 stderr = initial_stderr_stream;
773 initial_stderr_stream = NULL;
774 report_file_error ("Cannot open debugging output stream",
775 Fcons (file, Qnil));
778 return Qnil;
780 #endif /* GNU_LINUX */
783 /* This is the interface for debugging printing. */
785 void
786 debug_print (Lisp_Object arg)
788 Fprin1 (arg, Qexternal_debugging_output);
789 fprintf (stderr, "\r\n");
792 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
793 void
794 safe_debug_print (Lisp_Object arg)
796 int valid = valid_lisp_object_p (arg);
798 if (valid > 0)
799 debug_print (arg);
800 else
801 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
802 !valid ? "INVALID" : "SOME",
803 XLI (arg));
807 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
808 1, 1, 0,
809 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
810 See Info anchor `(elisp)Definition of signal' for some details on how this
811 error message is constructed. */)
812 (Lisp_Object obj)
814 struct buffer *old = current_buffer;
815 Lisp_Object value;
816 struct gcpro gcpro1;
818 /* If OBJ is (error STRING), just return STRING.
819 That is not only faster, it also avoids the need to allocate
820 space here when the error is due to memory full. */
821 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
822 && CONSP (XCDR (obj))
823 && STRINGP (XCAR (XCDR (obj)))
824 && NILP (XCDR (XCDR (obj))))
825 return XCAR (XCDR (obj));
827 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
829 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
830 value = Fbuffer_string ();
832 GCPRO1 (value);
833 Ferase_buffer ();
834 set_buffer_internal (old);
835 UNGCPRO;
837 return value;
840 /* Print an error message for the error DATA onto Lisp output stream
841 STREAM (suitable for the print functions).
842 CONTEXT is a C string describing the context of the error.
843 CALLER is the Lisp function inside which the error was signaled. */
845 void
846 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
847 Lisp_Object caller)
849 Lisp_Object errname, errmsg, file_error, tail;
850 struct gcpro gcpro1;
852 if (context != 0)
853 write_string_1 (context, -1, stream);
855 /* If we know from where the error was signaled, show it in
856 *Messages*. */
857 if (!NILP (caller) && SYMBOLP (caller))
859 Lisp_Object cname = SYMBOL_NAME (caller);
860 ptrdiff_t cnamelen = SBYTES (cname);
861 USE_SAFE_ALLOCA;
862 char *name = SAFE_ALLOCA (cnamelen);
863 memcpy (name, SDATA (cname), cnamelen);
864 message_dolog (name, cnamelen, 0, 0);
865 message_dolog (": ", 2, 0, 0);
866 SAFE_FREE ();
869 errname = Fcar (data);
871 if (EQ (errname, Qerror))
873 data = Fcdr (data);
874 if (!CONSP (data))
875 data = Qnil;
876 errmsg = Fcar (data);
877 file_error = Qnil;
879 else
881 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
882 errmsg = Fget (errname, Qerror_message);
883 file_error = Fmemq (Qfile_error, error_conditions);
886 /* Print an error message including the data items. */
888 tail = Fcdr_safe (data);
889 GCPRO1 (tail);
891 /* For file-error, make error message by concatenating
892 all the data items. They are all strings. */
893 if (!NILP (file_error) && CONSP (tail))
894 errmsg = XCAR (tail), tail = XCDR (tail);
897 const char *sep = ": ";
899 if (!STRINGP (errmsg))
900 write_string_1 ("peculiar error", -1, stream);
901 else if (SCHARS (errmsg))
902 Fprinc (errmsg, stream);
903 else
904 sep = NULL;
906 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
908 Lisp_Object obj;
910 if (sep)
911 write_string_1 (sep, 2, stream);
912 obj = XCAR (tail);
913 if (!NILP (file_error)
914 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
915 Fprinc (obj, stream);
916 else
917 Fprin1 (obj, stream);
921 UNGCPRO;
927 * The buffer should be at least as large as the max string size of the
928 * largest float, printed in the biggest notation. This is undoubtedly
929 * 20d float_output_format, with the negative of the C-constant "HUGE"
930 * from <math.h>.
932 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
934 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
935 * case of -1e307 in 20d float_output_format. What is one to do (short of
936 * re-writing _doprnt to be more sane)?
937 * -wsr
938 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
942 float_to_string (char *buf, double data)
944 char *cp;
945 int width;
946 int len;
948 /* Check for plus infinity in a way that won't lose
949 if there is no plus infinity. */
950 if (data == data / 2 && data > 1.0)
952 static char const infinity_string[] = "1.0e+INF";
953 strcpy (buf, infinity_string);
954 return sizeof infinity_string - 1;
956 /* Likewise for minus infinity. */
957 if (data == data / 2 && data < -1.0)
959 static char const minus_infinity_string[] = "-1.0e+INF";
960 strcpy (buf, minus_infinity_string);
961 return sizeof minus_infinity_string - 1;
963 /* Check for NaN in a way that won't fail if there are no NaNs. */
964 if (! (data * 0.0 >= 0.0))
966 /* Prepend "-" if the NaN's sign bit is negative.
967 The sign bit of a double is the bit that is 1 in -0.0. */
968 static char const NaN_string[] = "0.0e+NaN";
969 int i;
970 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
971 int negative = 0;
972 u_data.d = data;
973 u_minus_zero.d = - 0.0;
974 for (i = 0; i < sizeof (double); i++)
975 if (u_data.c[i] & u_minus_zero.c[i])
977 *buf = '-';
978 negative = 1;
979 break;
982 strcpy (buf + negative, NaN_string);
983 return negative + sizeof NaN_string - 1;
986 if (NILP (Vfloat_output_format)
987 || !STRINGP (Vfloat_output_format))
988 lose:
990 /* Generate the fewest number of digits that represent the
991 floating point value without losing information. */
992 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
993 /* The decimal point must be printed, or the byte compiler can
994 get confused (Bug#8033). */
995 width = 1;
997 else /* oink oink */
999 /* Check that the spec we have is fully valid.
1000 This means not only valid for printf,
1001 but meant for floats, and reasonable. */
1002 cp = SSDATA (Vfloat_output_format);
1004 if (cp[0] != '%')
1005 goto lose;
1006 if (cp[1] != '.')
1007 goto lose;
1009 cp += 2;
1011 /* Check the width specification. */
1012 width = -1;
1013 if ('0' <= *cp && *cp <= '9')
1015 width = 0;
1018 width = (width * 10) + (*cp++ - '0');
1019 if (DBL_DIG < width)
1020 goto lose;
1022 while (*cp >= '0' && *cp <= '9');
1024 /* A precision of zero is valid only for %f. */
1025 if (width == 0 && *cp != 'f')
1026 goto lose;
1029 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1030 goto lose;
1032 if (cp[1] != 0)
1033 goto lose;
1035 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1038 /* Make sure there is a decimal point with digit after, or an
1039 exponent, so that the value is readable as a float. But don't do
1040 this with "%.0f"; it's valid for that not to produce a decimal
1041 point. Note that width can be 0 only for %.0f. */
1042 if (width != 0)
1044 for (cp = buf; *cp; cp++)
1045 if ((*cp < '0' || *cp > '9') && *cp != '-')
1046 break;
1048 if (*cp == '.' && cp[1] == 0)
1050 cp[1] = '0';
1051 cp[2] = 0;
1052 len++;
1054 else if (*cp == 0)
1056 *cp++ = '.';
1057 *cp++ = '0';
1058 *cp++ = 0;
1059 len += 2;
1063 return len;
1067 static void
1068 print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1070 new_backquote_output = 0;
1072 /* Reset print_number_index and Vprint_number_table only when
1073 the variable Vprint_continuous_numbering is nil. Otherwise,
1074 the values of these variables will be kept between several
1075 print functions. */
1076 if (NILP (Vprint_continuous_numbering)
1077 || NILP (Vprint_number_table))
1079 print_number_index = 0;
1080 Vprint_number_table = Qnil;
1083 /* Construct Vprint_number_table for print-gensym and print-circle. */
1084 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1086 /* Construct Vprint_number_table.
1087 This increments print_number_index for the objects added. */
1088 print_depth = 0;
1089 print_preprocess (obj);
1091 if (HASH_TABLE_P (Vprint_number_table))
1092 { /* Remove unnecessary objects, which appear only once in OBJ;
1093 that is, whose status is Qt. */
1094 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1095 ptrdiff_t i;
1097 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1098 if (!NILP (HASH_HASH (h, i))
1099 && EQ (HASH_VALUE (h, i), Qt))
1100 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1104 print_depth = 0;
1105 print_object (obj, printcharfun, escapeflag);
1108 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1109 (STRINGP (obj) || CONSP (obj) \
1110 || (VECTORLIKEP (obj) \
1111 && (VECTORP (obj) || COMPILEDP (obj) \
1112 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1113 || HASH_TABLE_P (obj) || FONTP (obj))) \
1114 || (! NILP (Vprint_gensym) \
1115 && SYMBOLP (obj) \
1116 && !SYMBOL_INTERNED_P (obj)))
1118 /* Construct Vprint_number_table according to the structure of OBJ.
1119 OBJ itself and all its elements will be added to Vprint_number_table
1120 recursively if it is a list, vector, compiled function, char-table,
1121 string (its text properties will be traced), or a symbol that has
1122 no obarray (this is for the print-gensym feature).
1123 The status fields of Vprint_number_table mean whether each object appears
1124 more than once in OBJ: Qnil at the first time, and Qt after that . */
1125 static void
1126 print_preprocess (Lisp_Object obj)
1128 int i;
1129 ptrdiff_t size;
1130 int loop_count = 0;
1131 Lisp_Object halftail;
1133 /* Avoid infinite recursion for circular nested structure
1134 in the case where Vprint_circle is nil. */
1135 if (NILP (Vprint_circle))
1137 /* Give up if we go so deep that print_object will get an error. */
1138 /* See similar code in print_object. */
1139 if (print_depth >= PRINT_CIRCLE)
1140 error ("Apparently circular structure being printed");
1142 for (i = 0; i < print_depth; i++)
1143 if (EQ (obj, being_printed[i]))
1144 return;
1145 being_printed[print_depth] = obj;
1148 print_depth++;
1149 halftail = obj;
1151 loop:
1152 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1154 if (!HASH_TABLE_P (Vprint_number_table))
1156 Lisp_Object args[2];
1157 args[0] = QCtest;
1158 args[1] = Qeq;
1159 Vprint_number_table = Fmake_hash_table (2, args);
1162 /* In case print-circle is nil and print-gensym is t,
1163 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1164 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1166 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1167 if (!NILP (num)
1168 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1169 always print the gensym with a number. This is a special for
1170 the lisp function byte-compile-output-docform. */
1171 || (!NILP (Vprint_continuous_numbering)
1172 && SYMBOLP (obj)
1173 && !SYMBOL_INTERNED_P (obj)))
1174 { /* OBJ appears more than once. Let's remember that. */
1175 if (!INTEGERP (num))
1177 print_number_index++;
1178 /* Negative number indicates it hasn't been printed yet. */
1179 Fputhash (obj, make_number (- print_number_index),
1180 Vprint_number_table);
1182 print_depth--;
1183 return;
1185 else
1186 /* OBJ is not yet recorded. Let's add to the table. */
1187 Fputhash (obj, Qt, Vprint_number_table);
1190 switch (XTYPE (obj))
1192 case Lisp_String:
1193 /* A string may have text properties, which can be circular. */
1194 traverse_intervals_noorder (string_intervals (obj),
1195 print_preprocess_string, Qnil);
1196 break;
1198 case Lisp_Cons:
1199 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1200 just as in print_object. */
1201 if (loop_count && EQ (obj, halftail))
1202 break;
1203 print_preprocess (XCAR (obj));
1204 obj = XCDR (obj);
1205 loop_count++;
1206 if (!(loop_count & 1))
1207 halftail = XCDR (halftail);
1208 goto loop;
1210 case Lisp_Vectorlike:
1211 size = ASIZE (obj);
1212 if (size & PSEUDOVECTOR_FLAG)
1213 size &= PSEUDOVECTOR_SIZE_MASK;
1214 for (i = 0; i < size; i++)
1215 print_preprocess (AREF (obj, i));
1216 if (HASH_TABLE_P (obj))
1217 { /* For hash tables, the key_and_value slot is past
1218 `size' because it needs to be marked specially in case
1219 the table is weak. */
1220 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1221 print_preprocess (h->key_and_value);
1223 break;
1225 default:
1226 break;
1229 print_depth--;
1232 static void
1233 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1235 print_preprocess (interval->plist);
1238 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1240 #define PRINT_STRING_NON_CHARSET_FOUND 1
1241 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1243 /* Bitwise or of the above macros. */
1244 static int print_check_string_result;
1246 static void
1247 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1249 Lisp_Object val;
1251 if (NILP (interval->plist)
1252 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1253 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1254 return;
1255 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1256 val = XCDR (XCDR (val)));
1257 if (! CONSP (val))
1259 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1260 return;
1262 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1264 if (! EQ (val, interval->plist)
1265 || CONSP (XCDR (XCDR (val))))
1266 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1268 if (NILP (Vprint_charset_text_property)
1269 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1271 int i, c;
1272 ptrdiff_t charpos = interval->position;
1273 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1274 Lisp_Object charset;
1276 charset = XCAR (XCDR (val));
1277 for (i = 0; i < LENGTH (interval); i++)
1279 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1280 if (! ASCII_CHAR_P (c)
1281 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1283 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1284 break;
1290 /* The value is (charset . nil). */
1291 static Lisp_Object print_prune_charset_plist;
1293 static Lisp_Object
1294 print_prune_string_charset (Lisp_Object string)
1296 print_check_string_result = 0;
1297 traverse_intervals (string_intervals (string), 0,
1298 print_check_string_charset_prop, string);
1299 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1301 string = Fcopy_sequence (string);
1302 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1304 if (NILP (print_prune_charset_plist))
1305 print_prune_charset_plist = Fcons (Qcharset, Qnil);
1306 Fremove_text_properties (make_number (0),
1307 make_number (SCHARS (string)),
1308 print_prune_charset_plist, string);
1310 else
1311 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1312 Qnil, string);
1314 return string;
1317 static void
1318 print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1320 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1321 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1322 40))];
1324 QUIT;
1326 /* Detect circularities and truncate them. */
1327 if (NILP (Vprint_circle))
1329 /* Simple but incomplete way. */
1330 int i;
1332 /* See similar code in print_preprocess. */
1333 if (print_depth >= PRINT_CIRCLE)
1334 error ("Apparently circular structure being printed");
1336 for (i = 0; i < print_depth; i++)
1337 if (EQ (obj, being_printed[i]))
1339 int len = sprintf (buf, "#%d", i);
1340 strout (buf, len, len, printcharfun);
1341 return;
1343 being_printed[print_depth] = obj;
1345 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1347 /* With the print-circle feature. */
1348 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1349 if (INTEGERP (num))
1351 EMACS_INT n = XINT (num);
1352 if (n < 0)
1353 { /* Add a prefix #n= if OBJ has not yet been printed;
1354 that is, its status field is nil. */
1355 int len = sprintf (buf, "#%"pI"d=", -n);
1356 strout (buf, len, len, printcharfun);
1357 /* OBJ is going to be printed. Remember that fact. */
1358 Fputhash (obj, make_number (- n), Vprint_number_table);
1360 else
1362 /* Just print #n# if OBJ has already been printed. */
1363 int len = sprintf (buf, "#%"pI"d#", n);
1364 strout (buf, len, len, printcharfun);
1365 return;
1370 print_depth++;
1372 switch (XTYPE (obj))
1374 case_Lisp_Int:
1376 int len = sprintf (buf, "%"pI"d", XINT (obj));
1377 strout (buf, len, len, printcharfun);
1379 break;
1381 case Lisp_Float:
1383 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1384 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1385 strout (pigbuf, len, len, printcharfun);
1387 break;
1389 case Lisp_String:
1390 if (!escapeflag)
1391 print_string (obj, printcharfun);
1392 else
1394 register ptrdiff_t i_byte;
1395 struct gcpro gcpro1;
1396 unsigned char *str;
1397 ptrdiff_t size_byte;
1398 /* 1 means we must ensure that the next character we output
1399 cannot be taken as part of a hex character escape. */
1400 int need_nonhex = 0;
1401 int multibyte = STRING_MULTIBYTE (obj);
1403 GCPRO1 (obj);
1405 if (! EQ (Vprint_charset_text_property, Qt))
1406 obj = print_prune_string_charset (obj);
1408 if (string_intervals (obj))
1410 PRINTCHAR ('#');
1411 PRINTCHAR ('(');
1414 PRINTCHAR ('\"');
1415 str = SDATA (obj);
1416 size_byte = SBYTES (obj);
1418 for (i_byte = 0; i_byte < size_byte;)
1420 /* Here, we must convert each multi-byte form to the
1421 corresponding character code before handing it to PRINTCHAR. */
1422 int len;
1423 int c;
1425 if (multibyte)
1427 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1428 i_byte += len;
1430 else
1431 c = str[i_byte++];
1433 QUIT;
1435 if (c == '\n' && print_escape_newlines)
1437 PRINTCHAR ('\\');
1438 PRINTCHAR ('n');
1440 else if (c == '\f' && print_escape_newlines)
1442 PRINTCHAR ('\\');
1443 PRINTCHAR ('f');
1445 else if (multibyte
1446 && (CHAR_BYTE8_P (c)
1447 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1449 /* When multibyte is disabled,
1450 print multibyte string chars using hex escapes.
1451 For a char code that could be in a unibyte string,
1452 when found in a multibyte string, always use a hex escape
1453 so it reads back as multibyte. */
1454 char outbuf[50];
1455 int len;
1457 if (CHAR_BYTE8_P (c))
1458 len = sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1459 else
1461 len = sprintf (outbuf, "\\x%04x", c);
1462 need_nonhex = 1;
1464 strout (outbuf, len, len, printcharfun);
1466 else if (! multibyte
1467 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1468 && print_escape_nonascii)
1470 /* When printing in a multibyte buffer
1471 or when explicitly requested,
1472 print single-byte non-ASCII string chars
1473 using octal escapes. */
1474 char outbuf[5];
1475 int len = sprintf (outbuf, "\\%03o", c);
1476 strout (outbuf, len, len, printcharfun);
1478 else
1480 /* If we just had a hex escape, and this character
1481 could be taken as part of it,
1482 output `\ ' to prevent that. */
1483 if (need_nonhex)
1485 need_nonhex = 0;
1486 if ((c >= 'a' && c <= 'f')
1487 || (c >= 'A' && c <= 'F')
1488 || (c >= '0' && c <= '9'))
1489 strout ("\\ ", -1, -1, printcharfun);
1492 if (c == '\"' || c == '\\')
1493 PRINTCHAR ('\\');
1494 PRINTCHAR (c);
1497 PRINTCHAR ('\"');
1499 if (string_intervals (obj))
1501 traverse_intervals (string_intervals (obj),
1502 0, print_interval, printcharfun);
1503 PRINTCHAR (')');
1506 UNGCPRO;
1508 break;
1510 case Lisp_Symbol:
1512 register int confusing;
1513 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1514 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1515 register int c;
1516 ptrdiff_t i, i_byte;
1517 ptrdiff_t size_byte;
1518 Lisp_Object name;
1520 name = SYMBOL_NAME (obj);
1522 if (p != end && (*p == '-' || *p == '+')) p++;
1523 if (p == end)
1524 confusing = 0;
1525 /* If symbol name begins with a digit, and ends with a digit,
1526 and contains nothing but digits and `e', it could be treated
1527 as a number. So set CONFUSING.
1529 Symbols that contain periods could also be taken as numbers,
1530 but periods are always escaped, so we don't have to worry
1531 about them here. */
1532 else if (*p >= '0' && *p <= '9'
1533 && end[-1] >= '0' && end[-1] <= '9')
1535 while (p != end && ((*p >= '0' && *p <= '9')
1536 /* Needed for \2e10. */
1537 || *p == 'e' || *p == 'E'))
1538 p++;
1539 confusing = (end == p);
1541 else
1542 confusing = 0;
1544 size_byte = SBYTES (name);
1546 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1548 PRINTCHAR ('#');
1549 PRINTCHAR (':');
1551 else if (size_byte == 0)
1553 PRINTCHAR ('#');
1554 PRINTCHAR ('#');
1555 break;
1558 for (i = 0, i_byte = 0; i_byte < size_byte;)
1560 /* Here, we must convert each multi-byte form to the
1561 corresponding character code before handing it to PRINTCHAR. */
1562 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1563 QUIT;
1565 if (escapeflag)
1567 if (c == '\"' || c == '\\' || c == '\''
1568 || c == ';' || c == '#' || c == '(' || c == ')'
1569 || c == ',' || c == '.' || c == '`'
1570 || c == '[' || c == ']' || c == '?' || c <= 040
1571 || confusing)
1572 PRINTCHAR ('\\'), confusing = 0;
1574 PRINTCHAR (c);
1577 break;
1579 case Lisp_Cons:
1580 /* If deeper than spec'd depth, print placeholder. */
1581 if (INTEGERP (Vprint_level)
1582 && print_depth > XINT (Vprint_level))
1583 strout ("...", -1, -1, printcharfun);
1584 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1585 && (EQ (XCAR (obj), Qquote)))
1587 PRINTCHAR ('\'');
1588 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1590 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1591 && (EQ (XCAR (obj), Qfunction)))
1593 PRINTCHAR ('#');
1594 PRINTCHAR ('\'');
1595 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1597 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1598 && ((EQ (XCAR (obj), Qbackquote))))
1600 print_object (XCAR (obj), printcharfun, 0);
1601 new_backquote_output++;
1602 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1603 new_backquote_output--;
1605 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1606 && new_backquote_output
1607 && ((EQ (XCAR (obj), Qbackquote)
1608 || EQ (XCAR (obj), Qcomma)
1609 || EQ (XCAR (obj), Qcomma_at)
1610 || EQ (XCAR (obj), Qcomma_dot))))
1612 print_object (XCAR (obj), printcharfun, 0);
1613 new_backquote_output--;
1614 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1615 new_backquote_output++;
1617 else
1619 PRINTCHAR ('(');
1622 printmax_t i, print_length;
1623 Lisp_Object halftail = obj;
1625 /* Negative values of print-length are invalid in CL.
1626 Treat them like nil, as CMUCL does. */
1627 if (NATNUMP (Vprint_length))
1628 print_length = XFASTINT (Vprint_length);
1629 else
1630 print_length = TYPE_MAXIMUM (printmax_t);
1632 i = 0;
1633 while (CONSP (obj))
1635 /* Detect circular list. */
1636 if (NILP (Vprint_circle))
1638 /* Simple but incomplete way. */
1639 if (i != 0 && EQ (obj, halftail))
1641 int len = sprintf (buf, " . #%"pMd, i / 2);
1642 strout (buf, len, len, printcharfun);
1643 goto end_of_list;
1646 else
1648 /* With the print-circle feature. */
1649 if (i != 0)
1651 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1652 if (INTEGERP (num))
1654 strout (" . ", 3, 3, printcharfun);
1655 print_object (obj, printcharfun, escapeflag);
1656 goto end_of_list;
1661 if (i)
1662 PRINTCHAR (' ');
1664 if (print_length <= i)
1666 strout ("...", 3, 3, printcharfun);
1667 goto end_of_list;
1670 i++;
1671 print_object (XCAR (obj), printcharfun, escapeflag);
1673 obj = XCDR (obj);
1674 if (!(i & 1))
1675 halftail = XCDR (halftail);
1679 /* OBJ non-nil here means it's the end of a dotted list. */
1680 if (!NILP (obj))
1682 strout (" . ", 3, 3, printcharfun);
1683 print_object (obj, printcharfun, escapeflag);
1686 end_of_list:
1687 PRINTCHAR (')');
1689 break;
1691 case Lisp_Vectorlike:
1692 if (PROCESSP (obj))
1694 if (escapeflag)
1696 strout ("#<process ", -1, -1, printcharfun);
1697 print_string (XPROCESS (obj)->name, printcharfun);
1698 PRINTCHAR ('>');
1700 else
1701 print_string (XPROCESS (obj)->name, printcharfun);
1703 else if (BOOL_VECTOR_P (obj))
1705 ptrdiff_t i;
1706 int len;
1707 unsigned char c;
1708 struct gcpro gcpro1;
1709 ptrdiff_t size_in_chars
1710 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1711 / BOOL_VECTOR_BITS_PER_CHAR);
1713 GCPRO1 (obj);
1715 PRINTCHAR ('#');
1716 PRINTCHAR ('&');
1717 len = sprintf (buf, "%"pI"d", XBOOL_VECTOR (obj)->size);
1718 strout (buf, len, len, printcharfun);
1719 PRINTCHAR ('\"');
1721 /* Don't print more characters than the specified maximum.
1722 Negative values of print-length are invalid. Treat them
1723 like a print-length of nil. */
1724 if (NATNUMP (Vprint_length)
1725 && XFASTINT (Vprint_length) < size_in_chars)
1726 size_in_chars = XFASTINT (Vprint_length);
1728 for (i = 0; i < size_in_chars; i++)
1730 QUIT;
1731 c = XBOOL_VECTOR (obj)->data[i];
1732 if (c == '\n' && print_escape_newlines)
1734 PRINTCHAR ('\\');
1735 PRINTCHAR ('n');
1737 else if (c == '\f' && print_escape_newlines)
1739 PRINTCHAR ('\\');
1740 PRINTCHAR ('f');
1742 else if (c > '\177')
1744 /* Use octal escapes to avoid encoding issues. */
1745 PRINTCHAR ('\\');
1746 PRINTCHAR ('0' + ((c >> 6) & 3));
1747 PRINTCHAR ('0' + ((c >> 3) & 7));
1748 PRINTCHAR ('0' + (c & 7));
1750 else
1752 if (c == '\"' || c == '\\')
1753 PRINTCHAR ('\\');
1754 PRINTCHAR (c);
1757 PRINTCHAR ('\"');
1759 UNGCPRO;
1761 else if (SUBRP (obj))
1763 strout ("#<subr ", -1, -1, printcharfun);
1764 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun);
1765 PRINTCHAR ('>');
1767 #ifdef HAVE_XWIDGETS
1768 else if (XXWIDGETP (obj))
1770 strout ("#<xwidget ", -1, -1, printcharfun);
1771 PRINTCHAR ('>');
1773 #endif
1774 else if (WINDOWP (obj))
1776 int len;
1777 strout ("#<window ", -1, -1, printcharfun);
1778 len = sprintf (buf, "%d", XWINDOW (obj)->sequence_number);
1779 strout (buf, len, len, printcharfun);
1780 if (!NILP (XWINDOW (obj)->buffer))
1782 strout (" on ", -1, -1, printcharfun);
1783 print_string (BVAR (XBUFFER (XWINDOW (obj)->buffer), name),
1784 printcharfun);
1786 PRINTCHAR ('>');
1788 else if (TERMINALP (obj))
1790 int len;
1791 struct terminal *t = XTERMINAL (obj);
1792 strout ("#<terminal ", -1, -1, printcharfun);
1793 len = sprintf (buf, "%d", t->id);
1794 strout (buf, len, len, printcharfun);
1795 if (t->name)
1797 strout (" on ", -1, -1, printcharfun);
1798 strout (t->name, -1, -1, printcharfun);
1800 PRINTCHAR ('>');
1802 else if (HASH_TABLE_P (obj))
1804 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1805 ptrdiff_t i;
1806 ptrdiff_t real_size, size;
1807 int len;
1808 #if 0
1809 strout ("#<hash-table", -1, -1, printcharfun);
1810 if (SYMBOLP (h->test))
1812 PRINTCHAR (' ');
1813 PRINTCHAR ('\'');
1814 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun);
1815 PRINTCHAR (' ');
1816 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun);
1817 PRINTCHAR (' ');
1818 len = sprintf (buf, "%"pD"d/%"pD"d", h->count, ASIZE (h->next));
1819 strout (buf, len, len, printcharfun);
1821 len = sprintf (buf, " %p", h);
1822 strout (buf, len, len, printcharfun);
1823 PRINTCHAR ('>');
1824 #endif
1825 /* Implement a readable output, e.g.:
1826 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1827 /* Always print the size. */
1828 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1829 strout (buf, len, len, printcharfun);
1831 if (!NILP (h->test.name))
1833 strout (" test ", -1, -1, printcharfun);
1834 print_object (h->test.name, printcharfun, escapeflag);
1837 if (!NILP (h->weak))
1839 strout (" weakness ", -1, -1, printcharfun);
1840 print_object (h->weak, printcharfun, escapeflag);
1843 if (!NILP (h->rehash_size))
1845 strout (" rehash-size ", -1, -1, printcharfun);
1846 print_object (h->rehash_size, printcharfun, escapeflag);
1849 if (!NILP (h->rehash_threshold))
1851 strout (" rehash-threshold ", -1, -1, printcharfun);
1852 print_object (h->rehash_threshold, printcharfun, escapeflag);
1855 strout (" data ", -1, -1, printcharfun);
1857 /* Print the data here as a plist. */
1858 real_size = HASH_TABLE_SIZE (h);
1859 size = real_size;
1861 /* Don't print more elements than the specified maximum. */
1862 if (NATNUMP (Vprint_length)
1863 && XFASTINT (Vprint_length) < size)
1864 size = XFASTINT (Vprint_length);
1866 PRINTCHAR ('(');
1867 for (i = 0; i < size; i++)
1868 if (!NILP (HASH_HASH (h, i)))
1870 if (i) PRINTCHAR (' ');
1871 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1872 PRINTCHAR (' ');
1873 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1876 if (size < real_size)
1877 strout (" ...", 4, 4, printcharfun);
1879 PRINTCHAR (')');
1880 PRINTCHAR (')');
1883 else if (BUFFERP (obj))
1885 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1886 strout ("#<killed buffer>", -1, -1, printcharfun);
1887 else if (escapeflag)
1889 strout ("#<buffer ", -1, -1, printcharfun);
1890 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1891 PRINTCHAR ('>');
1893 else
1894 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1896 else if (WINDOW_CONFIGURATIONP (obj))
1898 strout ("#<window-configuration>", -1, -1, printcharfun);
1900 else if (FRAMEP (obj))
1902 int len;
1903 Lisp_Object frame_name = XFRAME (obj)->name;
1905 strout ((FRAME_LIVE_P (XFRAME (obj))
1906 ? "#<frame " : "#<dead frame "),
1907 -1, -1, printcharfun);
1908 if (!STRINGP (frame_name))
1910 /* A frame could be too young and have no name yet;
1911 don't crash. */
1912 if (SYMBOLP (frame_name))
1913 frame_name = Fsymbol_name (frame_name);
1914 else /* can't happen: name should be either nil or string */
1915 frame_name = build_string ("*INVALID*FRAME*NAME*");
1917 print_string (frame_name, printcharfun);
1918 len = sprintf (buf, " %p", XFRAME (obj));
1919 strout (buf, len, len, printcharfun);
1920 PRINTCHAR ('>');
1922 else if (FONTP (obj))
1924 int i;
1926 if (! FONT_OBJECT_P (obj))
1928 if (FONT_SPEC_P (obj))
1929 strout ("#<font-spec", -1, -1, printcharfun);
1930 else
1931 strout ("#<font-entity", -1, -1, printcharfun);
1932 for (i = 0; i < FONT_SPEC_MAX; i++)
1934 PRINTCHAR (' ');
1935 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1936 print_object (AREF (obj, i), printcharfun, escapeflag);
1937 else
1938 print_object (font_style_symbolic (obj, i, 0),
1939 printcharfun, escapeflag);
1942 else
1944 strout ("#<font-object ", -1, -1, printcharfun);
1945 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1946 escapeflag);
1948 PRINTCHAR ('>');
1950 else
1952 ptrdiff_t size = ASIZE (obj);
1953 if (COMPILEDP (obj))
1955 PRINTCHAR ('#');
1956 size &= PSEUDOVECTOR_SIZE_MASK;
1958 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1960 /* We print a char-table as if it were a vector,
1961 lumping the parent and default slots in with the
1962 character slots. But we add #^ as a prefix. */
1964 /* Make each lowest sub_char_table start a new line.
1965 Otherwise we'll make a line extremely long, which
1966 results in slow redisplay. */
1967 if (SUB_CHAR_TABLE_P (obj)
1968 && XINT (XSUB_CHAR_TABLE (obj)->depth) == 3)
1969 PRINTCHAR ('\n');
1970 PRINTCHAR ('#');
1971 PRINTCHAR ('^');
1972 if (SUB_CHAR_TABLE_P (obj))
1973 PRINTCHAR ('^');
1974 size &= PSEUDOVECTOR_SIZE_MASK;
1976 if (size & PSEUDOVECTOR_FLAG)
1977 goto badtype;
1979 PRINTCHAR ('[');
1981 register int i;
1982 register Lisp_Object tem;
1983 ptrdiff_t real_size = size;
1985 /* Don't print more elements than the specified maximum. */
1986 if (NATNUMP (Vprint_length)
1987 && XFASTINT (Vprint_length) < size)
1988 size = XFASTINT (Vprint_length);
1990 for (i = 0; i < size; i++)
1992 if (i) PRINTCHAR (' ');
1993 tem = AREF (obj, i);
1994 print_object (tem, printcharfun, escapeflag);
1996 if (size < real_size)
1997 strout (" ...", 4, 4, printcharfun);
1999 PRINTCHAR (']');
2001 break;
2003 case Lisp_Misc:
2004 switch (XMISCTYPE (obj))
2006 case Lisp_Misc_Marker:
2007 strout ("#<marker ", -1, -1, printcharfun);
2008 /* Do you think this is necessary? */
2009 if (XMARKER (obj)->insertion_type != 0)
2010 strout ("(moves after insertion) ", -1, -1, printcharfun);
2011 if (! XMARKER (obj)->buffer)
2012 strout ("in no buffer", -1, -1, printcharfun);
2013 else
2015 int len = sprintf (buf, "at %"pD"d", marker_position (obj));
2016 strout (buf, len, len, printcharfun);
2017 strout (" in ", -1, -1, printcharfun);
2018 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2020 PRINTCHAR ('>');
2021 break;
2023 case Lisp_Misc_Overlay:
2024 strout ("#<overlay ", -1, -1, printcharfun);
2025 if (! XMARKER (OVERLAY_START (obj))->buffer)
2026 strout ("in no buffer", -1, -1, printcharfun);
2027 else
2029 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2030 marker_position (OVERLAY_START (obj)),
2031 marker_position (OVERLAY_END (obj)));
2032 strout (buf, len, len, printcharfun);
2033 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2034 printcharfun);
2036 PRINTCHAR ('>');
2037 break;
2039 /* Remaining cases shouldn't happen in normal usage, but let's print
2040 them anyway for the benefit of the debugger. */
2041 case Lisp_Misc_Free:
2042 strout ("#<misc free cell>", -1, -1, printcharfun);
2043 break;
2045 case Lisp_Misc_Save_Value:
2047 int i;
2048 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2050 strout ("#<save-value ", -1, -1, printcharfun);
2051 if (v->dogc)
2053 int lim = min (v->integer, 8);
2055 /* Try to print up to 8 objects we have saved. Although
2056 valid_lisp_object_p is slow, this shouldn't be a real
2057 bottleneck because such a saved values are quite rare. */
2059 i = sprintf (buf, "with %"pD"d objects", v->integer);
2060 strout (buf, i, i, printcharfun);
2062 for (i = 0; i < lim; i++)
2064 Lisp_Object maybe = ((Lisp_Object *) v->pointer)[i];
2066 if (valid_lisp_object_p (maybe) > 0)
2068 PRINTCHAR (' ');
2069 print_object (maybe, printcharfun, escapeflag);
2071 else
2072 strout (" <invalid>", -1, -1, printcharfun);
2074 if (i == lim && i < v->integer)
2075 strout (" ...", 4, 4, printcharfun);
2077 else
2079 i = sprintf (buf, "ptr=%p int=%"pD"d", v->pointer, v->integer);
2080 strout (buf, i, i, printcharfun);
2082 PRINTCHAR ('>');
2084 break;
2086 default:
2087 goto badtype;
2089 break;
2091 default:
2092 badtype:
2094 int len;
2095 /* We're in trouble if this happens!
2096 Probably should just emacs_abort (). */
2097 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun);
2098 if (MISCP (obj))
2099 len = sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2100 else if (VECTORLIKEP (obj))
2101 len = sprintf (buf, "(PVEC 0x%08"pD"x)", ASIZE (obj));
2102 else
2103 len = sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2104 strout (buf, len, len, printcharfun);
2105 strout (" Save your buffers immediately and please report this bug>",
2106 -1, -1, printcharfun);
2110 print_depth--;
2114 /* Print a description of INTERVAL using PRINTCHARFUN.
2115 This is part of printing a string that has text properties. */
2117 static void
2118 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2120 if (NILP (interval->plist))
2121 return;
2122 PRINTCHAR (' ');
2123 print_object (make_number (interval->position), printcharfun, 1);
2124 PRINTCHAR (' ');
2125 print_object (make_number (interval->position + LENGTH (interval)),
2126 printcharfun, 1);
2127 PRINTCHAR (' ');
2128 print_object (interval->plist, printcharfun, 1);
2132 void
2133 syms_of_print (void)
2135 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2137 DEFVAR_LISP ("standard-output", Vstandard_output,
2138 doc: /* Output stream `print' uses by default for outputting a character.
2139 This may be any function of one argument.
2140 It may also be a buffer (output is inserted before point)
2141 or a marker (output is inserted and the marker is advanced)
2142 or the symbol t (output appears in the echo area). */);
2143 Vstandard_output = Qt;
2144 DEFSYM (Qstandard_output, "standard-output");
2146 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2147 doc: /* The format descriptor string used to print floats.
2148 This is a %-spec like those accepted by `printf' in C,
2149 but with some restrictions. It must start with the two characters `%.'.
2150 After that comes an integer precision specification,
2151 and then a letter which controls the format.
2152 The letters allowed are `e', `f' and `g'.
2153 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2154 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2155 Use `g' to choose the shorter of those two formats for the number at hand.
2156 The precision in any of these cases is the number of digits following
2157 the decimal point. With `f', a precision of 0 means to omit the
2158 decimal point. 0 is not allowed with `e' or `g'.
2160 A value of nil means to use the shortest notation
2161 that represents the number without losing information. */);
2162 Vfloat_output_format = Qnil;
2163 DEFSYM (Qfloat_output_format, "float-output-format");
2165 DEFVAR_LISP ("print-length", Vprint_length,
2166 doc: /* Maximum length of list to print before abbreviating.
2167 A value of nil means no limit. See also `eval-expression-print-length'. */);
2168 Vprint_length = Qnil;
2170 DEFVAR_LISP ("print-level", Vprint_level,
2171 doc: /* Maximum depth of list nesting to print before abbreviating.
2172 A value of nil means no limit. See also `eval-expression-print-level'. */);
2173 Vprint_level = Qnil;
2175 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2176 doc: /* Non-nil means print newlines in strings as `\\n'.
2177 Also print formfeeds as `\\f'. */);
2178 print_escape_newlines = 0;
2180 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2181 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2182 \(OOO is the octal representation of the character code.)
2183 Only single-byte characters are affected, and only in `prin1'.
2184 When the output goes in a multibyte buffer, this feature is
2185 enabled regardless of the value of the variable. */);
2186 print_escape_nonascii = 0;
2188 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2189 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2190 \(XXXX is the hex representation of the character code.)
2191 This affects only `prin1'. */);
2192 print_escape_multibyte = 0;
2194 DEFVAR_BOOL ("print-quoted", print_quoted,
2195 doc: /* Non-nil means print quoted forms with reader syntax.
2196 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2197 print_quoted = 0;
2199 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2200 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2201 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2202 When the uninterned symbol appears within a recursive data structure,
2203 and the symbol appears more than once, in addition use the #N# and #N=
2204 constructs as needed, so that multiple references to the same symbol are
2205 shared once again when the text is read back. */);
2206 Vprint_gensym = Qnil;
2208 DEFVAR_LISP ("print-circle", Vprint_circle,
2209 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2210 If nil, printing proceeds recursively and may lead to
2211 `max-lisp-eval-depth' being exceeded or an error may occur:
2212 \"Apparently circular structure being printed.\" Also see
2213 `print-length' and `print-level'.
2214 If non-nil, shared substructures anywhere in the structure are printed
2215 with `#N=' before the first occurrence (in the order of the print
2216 representation) and `#N#' in place of each subsequent occurrence,
2217 where N is a positive decimal integer. */);
2218 Vprint_circle = Qnil;
2220 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2221 doc: /* Non-nil means number continuously across print calls.
2222 This affects the numbers printed for #N= labels and #M# references.
2223 See also `print-circle', `print-gensym', and `print-number-table'.
2224 This variable should not be set with `setq'; bind it with a `let' instead. */);
2225 Vprint_continuous_numbering = Qnil;
2227 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2228 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2229 The Lisp printer uses this vector to detect Lisp objects referenced more
2230 than once.
2232 When you bind `print-continuous-numbering' to t, you should probably
2233 also bind `print-number-table' to nil. This ensures that the value of
2234 `print-number-table' can be garbage-collected once the printing is
2235 done. If all elements of `print-number-table' are nil, it means that
2236 the printing done so far has not found any shared structure or objects
2237 that need to be recorded in the table. */);
2238 Vprint_number_table = Qnil;
2240 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2241 doc: /* A flag to control printing of `charset' text property on printing a string.
2242 The value must be nil, t, or `default'.
2244 If the value is nil, don't print the text property `charset'.
2246 If the value is t, always print the text property `charset'.
2248 If the value is `default', print the text property `charset' only when
2249 the value is different from what is guessed in the current charset
2250 priorities. */);
2251 Vprint_charset_text_property = Qdefault;
2253 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2254 staticpro (&Vprin1_to_string_buffer);
2256 defsubr (&Sprin1);
2257 defsubr (&Sprin1_to_string);
2258 defsubr (&Serror_message_string);
2259 defsubr (&Sprinc);
2260 defsubr (&Sprint);
2261 defsubr (&Sterpri);
2262 defsubr (&Swrite_char);
2263 defsubr (&Sexternal_debugging_output);
2264 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2265 defsubr (&Sredirect_debugging_output);
2266 #endif
2268 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2269 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2270 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2271 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2273 print_prune_charset_plist = Qnil;
2274 staticpro (&print_prune_charset_plist);