MS-Windows followup for ASCIIfication of curved quotes
[emacs.git] / src / print.c
blob206466ce68f0db4cd5c93a34d01d78f58cc00237
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 <c-ctype.h>
41 #include <float.h>
42 #include <ftoastr.h>
44 /* Avoid actual stack overflow in print. */
45 static ptrdiff_t print_depth;
47 /* Level of nesting inside outputting backquote in new style. */
48 static ptrdiff_t new_backquote_output;
50 /* Detect most circularities to print finite output. */
51 #define PRINT_CIRCLE 200
52 static Lisp_Object being_printed[PRINT_CIRCLE];
54 /* Last char printed to stdout by printchar. */
55 static unsigned int printchar_stdout_last;
57 /* When printing into a buffer, first we put the text in this
58 block, then insert it all at once. */
59 static char *print_buffer;
61 /* Size allocated in print_buffer. */
62 static ptrdiff_t print_buffer_size;
63 /* Chars stored in print_buffer. */
64 static ptrdiff_t print_buffer_pos;
65 /* Bytes stored in print_buffer. */
66 static ptrdiff_t print_buffer_pos_byte;
68 /* Vprint_number_table is a table, that keeps objects that are going to
69 be printed, to allow use of #n= and #n# to express sharing.
70 For any given object, the table can give the following values:
71 t the object will be printed only once.
72 -N the object will be printed several times and will take number N.
73 N the object has been printed so we can refer to it as #N#.
74 print_number_index holds the largest N already used.
75 N has to be striclty larger than 0 since we need to distinguish -N. */
76 static ptrdiff_t print_number_index;
77 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
79 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
80 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
83 /* Low level output routines for characters and strings. */
85 /* Lisp functions to do output using a stream
86 must have the stream in a variable called printcharfun
87 and must start with PRINTPREPARE, end with PRINTFINISH.
88 Use printchar to output one character,
89 or call strout to output a block of characters. */
91 #define PRINTPREPARE \
92 struct buffer *old = current_buffer; \
93 ptrdiff_t old_point = -1, start_point = -1; \
94 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
95 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
96 bool free_print_buffer = 0; \
97 bool multibyte \
98 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
99 Lisp_Object original = printcharfun; \
100 if (NILP (printcharfun)) printcharfun = Qt; \
101 if (BUFFERP (printcharfun)) \
103 if (XBUFFER (printcharfun) != current_buffer) \
104 Fset_buffer (printcharfun); \
105 printcharfun = Qnil; \
107 if (MARKERP (printcharfun)) \
109 ptrdiff_t marker_pos; \
110 if (! XMARKER (printcharfun)->buffer) \
111 error ("Marker does not point anywhere"); \
112 if (XMARKER (printcharfun)->buffer != current_buffer) \
113 set_buffer_internal (XMARKER (printcharfun)->buffer); \
114 marker_pos = marker_position (printcharfun); \
115 if (marker_pos < BEGV || marker_pos > ZV) \
116 signal_error ("Marker is outside the accessible " \
117 "part of the buffer", printcharfun); \
118 old_point = PT; \
119 old_point_byte = PT_BYTE; \
120 SET_PT_BOTH (marker_pos, \
121 marker_byte_position (printcharfun)); \
122 start_point = PT; \
123 start_point_byte = PT_BYTE; \
124 printcharfun = Qnil; \
126 if (NILP (printcharfun)) \
128 Lisp_Object string; \
129 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
130 && ! print_escape_multibyte) \
131 specbind (Qprint_escape_multibyte, Qt); \
132 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
133 && ! print_escape_nonascii) \
134 specbind (Qprint_escape_nonascii, Qt); \
135 if (print_buffer != 0) \
137 string = make_string_from_bytes (print_buffer, \
138 print_buffer_pos, \
139 print_buffer_pos_byte); \
140 record_unwind_protect (print_unwind, string); \
142 else \
144 int new_size = 1000; \
145 print_buffer = xmalloc (new_size); \
146 print_buffer_size = new_size; \
147 free_print_buffer = 1; \
149 print_buffer_pos = 0; \
150 print_buffer_pos_byte = 0; \
152 if (EQ (printcharfun, Qt) && ! noninteractive) \
153 setup_echo_area_for_printing (multibyte);
155 #define PRINTFINISH \
156 if (NILP (printcharfun)) \
158 if (print_buffer_pos != print_buffer_pos_byte \
159 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
161 USE_SAFE_ALLOCA; \
162 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
163 copy_text ((unsigned char *) print_buffer, temp, \
164 print_buffer_pos_byte, 1, 0); \
165 insert_1_both ((char *) temp, print_buffer_pos, \
166 print_buffer_pos, 0, 1, 0); \
167 SAFE_FREE (); \
169 else \
170 insert_1_both (print_buffer, print_buffer_pos, \
171 print_buffer_pos_byte, 0, 1, 0); \
172 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
174 if (free_print_buffer) \
176 xfree (print_buffer); \
177 print_buffer = 0; \
179 unbind_to (specpdl_count, Qnil); \
180 if (MARKERP (original)) \
181 set_marker_both (original, Qnil, PT, PT_BYTE); \
182 if (old_point >= 0) \
183 SET_PT_BOTH (old_point + (old_point >= start_point \
184 ? PT - start_point : 0), \
185 old_point_byte + (old_point_byte >= start_point_byte \
186 ? PT_BYTE - start_point_byte : 0)); \
187 set_buffer_internal (old);
189 /* This is used to restore the saved contents of print_buffer
190 when there is a recursive call to print. */
192 static void
193 print_unwind (Lisp_Object saved_text)
195 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
199 /* Print character CH using method FUN. FUN nil means print to
200 print_buffer. FUN t means print to echo area or stdout if
201 non-interactive. If FUN is neither nil nor t, call FUN with CH as
202 argument. */
204 static void
205 printchar (unsigned int ch, Lisp_Object fun)
207 if (!NILP (fun) && !EQ (fun, Qt))
208 call1 (fun, make_number (ch));
209 else
211 unsigned char str[MAX_MULTIBYTE_LENGTH];
212 int len = CHAR_STRING (ch, str);
214 QUIT;
216 if (NILP (fun))
218 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
219 if (incr > 0)
220 print_buffer = xpalloc (print_buffer, &print_buffer_size,
221 incr, -1, 1);
222 memcpy (print_buffer + print_buffer_pos_byte, str, len);
223 print_buffer_pos += 1;
224 print_buffer_pos_byte += len;
226 else if (noninteractive)
228 printchar_stdout_last = ch;
229 fwrite (str, 1, len, stdout);
230 noninteractive_need_newline = 1;
232 else
234 bool multibyte_p
235 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
237 setup_echo_area_for_printing (multibyte_p);
238 insert_char (ch);
239 message_dolog ((char *) str, len, 0, multibyte_p);
245 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
246 method PRINTCHARFUN. PRINTCHARFUN nil means output to
247 print_buffer. PRINTCHARFUN t means output to the echo area or to
248 stdout if non-interactive. If neither nil nor t, call Lisp
249 function PRINTCHARFUN for each character printed. MULTIBYTE
250 non-zero means PTR contains multibyte characters.
252 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
253 to data in a Lisp string. Otherwise that is not safe. */
255 static void
256 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
257 Lisp_Object printcharfun)
259 if (NILP (printcharfun))
261 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
262 if (incr > 0)
263 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
264 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
265 print_buffer_pos += size;
266 print_buffer_pos_byte += size_byte;
268 else if (noninteractive && EQ (printcharfun, Qt))
270 fwrite (ptr, 1, size_byte, stdout);
271 noninteractive_need_newline = 1;
273 else if (EQ (printcharfun, Qt))
275 /* Output to echo area. We're trying to avoid a little overhead
276 here, that's the reason we don't call printchar to do the
277 job. */
278 int i;
279 bool multibyte_p
280 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
282 setup_echo_area_for_printing (multibyte_p);
283 message_dolog (ptr, size_byte, 0, multibyte_p);
285 if (size == size_byte)
287 for (i = 0; i < size; ++i)
288 insert_char ((unsigned char) *ptr++);
290 else
292 int len;
293 for (i = 0; i < size_byte; i += len)
295 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
296 len);
297 insert_char (ch);
301 else
303 /* PRINTCHARFUN is a Lisp function. */
304 ptrdiff_t i = 0;
306 if (size == size_byte)
308 while (i < size_byte)
310 int ch = ptr[i++];
311 printchar (ch, printcharfun);
314 else
316 while (i < size_byte)
318 /* Here, we must convert each multi-byte form to the
319 corresponding character code before handing it to
320 PRINTCHAR. */
321 int len;
322 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
323 len);
324 printchar (ch, printcharfun);
325 i += len;
331 /* Print the contents of a string STRING using PRINTCHARFUN.
332 It isn't safe to use strout in many cases,
333 because printing one char can relocate. */
335 static void
336 print_string (Lisp_Object string, Lisp_Object printcharfun)
338 if (EQ (printcharfun, Qt) || NILP (printcharfun))
340 ptrdiff_t chars;
342 if (print_escape_nonascii)
343 string = string_escape_byte8 (string);
345 if (STRING_MULTIBYTE (string))
346 chars = SCHARS (string);
347 else if (! print_escape_nonascii
348 && (EQ (printcharfun, Qt)
349 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
350 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
352 /* If unibyte string STRING contains 8-bit codes, we must
353 convert STRING to a multibyte string containing the same
354 character codes. */
355 Lisp_Object newstr;
356 ptrdiff_t bytes;
358 chars = SBYTES (string);
359 bytes = count_size_as_multibyte (SDATA (string), chars);
360 if (chars < bytes)
362 newstr = make_uninit_multibyte_string (chars, bytes);
363 memcpy (SDATA (newstr), SDATA (string), chars);
364 str_to_multibyte (SDATA (newstr), bytes, chars);
365 string = newstr;
368 else
369 chars = SBYTES (string);
371 if (EQ (printcharfun, Qt))
373 /* Output to echo area. */
374 ptrdiff_t nbytes = SBYTES (string);
376 /* Copy the string contents so that relocation of STRING by
377 GC does not cause trouble. */
378 USE_SAFE_ALLOCA;
379 char *buffer = SAFE_ALLOCA (nbytes);
380 memcpy (buffer, SDATA (string), nbytes);
382 strout (buffer, chars, nbytes, printcharfun);
384 SAFE_FREE ();
386 else
387 /* No need to copy, since output to print_buffer can't GC. */
388 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
390 else
392 /* Otherwise, string may be relocated by printing one char.
393 So re-fetch the string address for each character. */
394 ptrdiff_t i;
395 ptrdiff_t size = SCHARS (string);
396 ptrdiff_t size_byte = SBYTES (string);
397 struct gcpro gcpro1;
398 GCPRO1 (string);
399 if (size == size_byte)
400 for (i = 0; i < size; i++)
401 printchar (SREF (string, i), printcharfun);
402 else
403 for (i = 0; i < size_byte; )
405 /* Here, we must convert each multi-byte form to the
406 corresponding character code before handing it to PRINTCHAR. */
407 int len;
408 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
409 printchar (ch, printcharfun);
410 i += len;
412 UNGCPRO;
416 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
417 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
418 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
419 (Lisp_Object character, Lisp_Object printcharfun)
421 if (NILP (printcharfun))
422 printcharfun = Vstandard_output;
423 CHECK_NUMBER (character);
424 PRINTPREPARE;
425 printchar (XINT (character), printcharfun);
426 PRINTFINISH;
427 return character;
430 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
431 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
432 Do not use this on the contents of a Lisp string. */
434 static void
435 print_c_string (char const *string, Lisp_Object printcharfun)
437 ptrdiff_t len = strlen (string);
438 strout (string, len, len, printcharfun);
441 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
442 Do not use this on the contents of a Lisp string. */
444 static void
445 write_string_1 (const char *data, Lisp_Object printcharfun)
447 PRINTPREPARE;
448 print_c_string (data, printcharfun);
449 PRINTFINISH;
452 /* Used from outside of print.c to print a C unibyte
453 string at DATA on the default output stream.
454 Do not use this on the contents of a Lisp string. */
456 void
457 write_string (const char *data)
459 write_string_1 (data, Vstandard_output);
463 void
464 temp_output_buffer_setup (const char *bufname)
466 ptrdiff_t count = SPECPDL_INDEX ();
467 register struct buffer *old = current_buffer;
468 register Lisp_Object buf;
470 record_unwind_current_buffer ();
472 Fset_buffer (Fget_buffer_create (build_string (bufname)));
474 Fkill_all_local_variables ();
475 delete_all_overlays (current_buffer);
476 bset_directory (current_buffer, BVAR (old, directory));
477 bset_read_only (current_buffer, Qnil);
478 bset_filename (current_buffer, Qnil);
479 bset_undo_list (current_buffer, Qt);
480 eassert (current_buffer->overlays_before == NULL);
481 eassert (current_buffer->overlays_after == NULL);
482 bset_enable_multibyte_characters
483 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
484 specbind (Qinhibit_read_only, Qt);
485 specbind (Qinhibit_modification_hooks, Qt);
486 Ferase_buffer ();
487 XSETBUFFER (buf, current_buffer);
489 run_hook (Qtemp_buffer_setup_hook);
491 unbind_to (count, Qnil);
493 specbind (Qstandard_output, buf);
496 static void print (Lisp_Object, Lisp_Object, bool);
497 static void print_preprocess (Lisp_Object);
498 static void print_preprocess_string (INTERVAL, Lisp_Object);
499 static void print_object (Lisp_Object, Lisp_Object, bool);
501 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
502 doc: /* Output a newline to stream PRINTCHARFUN.
503 If ENSURE is non-nil only output a newline if not already at the
504 beginning of a line. Value is non-nil if a newline is printed.
505 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
506 (Lisp_Object printcharfun, Lisp_Object ensure)
508 Lisp_Object val;
510 if (NILP (printcharfun))
511 printcharfun = Vstandard_output;
512 PRINTPREPARE;
514 if (NILP (ensure))
515 val = Qt;
516 /* Difficult to check if at line beginning so abort. */
517 else if (FUNCTIONP (printcharfun))
518 signal_error ("Unsupported function argument", printcharfun);
519 else if (noninteractive && !NILP (printcharfun))
520 val = printchar_stdout_last == 10 ? Qnil : Qt;
521 else
522 val = NILP (Fbolp ()) ? Qt : Qnil;
524 if (!NILP (val))
525 printchar ('\n', printcharfun);
526 PRINTFINISH;
527 return val;
530 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
531 doc: /* Output the printed representation of OBJECT, any Lisp object.
532 Quoting characters are printed when needed to make output that `read'
533 can handle, whenever this is possible. For complex objects, the behavior
534 is controlled by `print-level' and `print-length', which see.
536 OBJECT is any of the Lisp data types: a number, a string, a symbol,
537 a list, a buffer, a window, a frame, etc.
539 A printed representation of an object is text which describes that object.
541 Optional argument PRINTCHARFUN is the output stream, which can be one
542 of these:
544 - a buffer, in which case output is inserted into that buffer at point;
545 - a marker, in which case output is inserted at marker's position;
546 - a function, in which case that function is called once for each
547 character of OBJECT's printed representation;
548 - a symbol, in which case that symbol's function definition is called; or
549 - t, in which case the output is displayed in the echo area.
551 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
552 is used instead. */)
553 (Lisp_Object object, Lisp_Object printcharfun)
555 if (NILP (printcharfun))
556 printcharfun = Vstandard_output;
557 PRINTPREPARE;
558 print (object, printcharfun, 1);
559 PRINTFINISH;
560 return object;
563 /* a buffer which is used to hold output being built by prin1-to-string */
564 Lisp_Object Vprin1_to_string_buffer;
566 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
567 doc: /* Return a string containing the printed representation of OBJECT.
568 OBJECT can be any Lisp object. This function outputs quoting characters
569 when necessary to make output that `read' can handle, whenever possible,
570 unless the optional second argument NOESCAPE is non-nil. For complex objects,
571 the behavior is controlled by `print-level' and `print-length', which see.
573 OBJECT is any of the Lisp data types: a number, a string, a symbol,
574 a list, a buffer, a window, a frame, etc.
576 A printed representation of an object is text which describes that object. */)
577 (Lisp_Object object, Lisp_Object noescape)
579 ptrdiff_t count = SPECPDL_INDEX ();
581 specbind (Qinhibit_modification_hooks, Qt);
583 /* Save and restore this: we are altering a buffer
584 but we don't want to deactivate the mark just for that.
585 No need for specbind, since errors deactivate the mark. */
586 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
587 bool prev_abort_on_gc = abort_on_gc;
588 abort_on_gc = true;
590 Lisp_Object printcharfun = Vprin1_to_string_buffer;
591 PRINTPREPARE;
592 print (object, printcharfun, NILP (noescape));
593 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
594 PRINTFINISH;
596 struct buffer *previous = current_buffer;
597 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
598 object = Fbuffer_string ();
599 if (SBYTES (object) == SCHARS (object))
600 STRING_SET_UNIBYTE (object);
602 /* Note that this won't make prepare_to_modify_buffer call
603 ask-user-about-supersession-threat because this buffer
604 does not visit a file. */
605 Ferase_buffer ();
606 set_buffer_internal (previous);
608 Vdeactivate_mark = save_deactivate_mark;
610 abort_on_gc = prev_abort_on_gc;
611 return unbind_to (count, object);
614 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
615 doc: /* Output the printed representation of OBJECT, any Lisp object.
616 No quoting characters are used; no delimiters are printed around
617 the contents of strings.
619 OBJECT is any of the Lisp data types: a number, a string, a symbol,
620 a list, a buffer, a window, a frame, etc.
622 A printed representation of an object is text which describes that object.
624 Optional argument PRINTCHARFUN is the output stream, which can be one
625 of these:
627 - a buffer, in which case output is inserted into that buffer at point;
628 - a marker, in which case output is inserted at marker's position;
629 - a function, in which case that function is called once for each
630 character of OBJECT's printed representation;
631 - a symbol, in which case that symbol's function definition is called; or
632 - t, in which case the output is displayed in the echo area.
634 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
635 is used instead. */)
636 (Lisp_Object object, Lisp_Object printcharfun)
638 if (NILP (printcharfun))
639 printcharfun = Vstandard_output;
640 PRINTPREPARE;
641 print (object, printcharfun, 0);
642 PRINTFINISH;
643 return object;
646 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
647 doc: /* Output the printed representation of OBJECT, with newlines around it.
648 Quoting characters are printed when needed to make output that `read'
649 can handle, whenever this is possible. For complex objects, the behavior
650 is controlled by `print-level' and `print-length', which see.
652 OBJECT is any of the Lisp data types: a number, a string, a symbol,
653 a list, a buffer, a window, a frame, etc.
655 A printed representation of an object is text which describes that object.
657 Optional argument PRINTCHARFUN is the output stream, which can be one
658 of these:
660 - a buffer, in which case output is inserted into that buffer at point;
661 - a marker, in which case output is inserted at marker's position;
662 - a function, in which case that function is called once for each
663 character of OBJECT's printed representation;
664 - a symbol, in which case that symbol's function definition is called; or
665 - t, in which case the output is displayed in the echo area.
667 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
668 is used instead. */)
669 (Lisp_Object object, Lisp_Object printcharfun)
671 struct gcpro gcpro1;
673 if (NILP (printcharfun))
674 printcharfun = Vstandard_output;
675 GCPRO1 (object);
676 PRINTPREPARE;
677 printchar ('\n', printcharfun);
678 print (object, printcharfun, 1);
679 printchar ('\n', printcharfun);
680 PRINTFINISH;
681 UNGCPRO;
682 return object;
685 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
686 doc: /* Write CHARACTER to stderr.
687 You can call print while debugging emacs, and pass it this function
688 to make it write to the debugging output. */)
689 (Lisp_Object character)
691 unsigned int ch;
693 CHECK_NUMBER (character);
694 ch = XINT (character);
695 if (ASCII_CHAR_P (ch))
697 putc (ch, stderr);
698 #ifdef WINDOWSNT
699 /* Send the output to a debugger (nothing happens if there isn't
700 one). */
701 if (print_output_debug_flag)
703 char buf[2] = {(char) XINT (character), '\0'};
704 OutputDebugString (buf);
706 #endif
708 else
710 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
711 ptrdiff_t len = CHAR_STRING (ch, mbstr);
712 Lisp_Object encoded_ch =
713 ENCODE_SYSTEM (make_multibyte_string ((char *) mbstr, 1, len));
715 fwrite (SSDATA (encoded_ch), SBYTES (encoded_ch), 1, stderr);
716 #ifdef WINDOWSNT
717 if (print_output_debug_flag)
718 OutputDebugString (SSDATA (encoded_ch));
719 #endif
722 return character;
725 /* This function is never called. Its purpose is to prevent
726 print_output_debug_flag from being optimized away. */
728 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
729 void
730 debug_output_compilation_hack (bool x)
732 print_output_debug_flag = x;
735 #if defined (GNU_LINUX)
737 /* This functionality is not vitally important in general, so we rely on
738 non-portable ability to use stderr as lvalue. */
740 #define WITH_REDIRECT_DEBUGGING_OUTPUT 1
742 static FILE *initial_stderr_stream = NULL;
744 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
745 1, 2,
746 "FDebug output file: \nP",
747 doc: /* Redirect debugging output (stderr stream) to file FILE.
748 If FILE is nil, reset target to the initial stderr stream.
749 Optional arg APPEND non-nil (interactively, with prefix arg) means
750 append to existing target file. */)
751 (Lisp_Object file, Lisp_Object append)
753 if (initial_stderr_stream != NULL)
755 block_input ();
756 fclose (stderr);
757 unblock_input ();
759 stderr = initial_stderr_stream;
760 initial_stderr_stream = NULL;
762 if (STRINGP (file))
764 file = Fexpand_file_name (file, Qnil);
765 initial_stderr_stream = stderr;
766 stderr = emacs_fopen (SSDATA (file), NILP (append) ? "w" : "a");
767 if (stderr == NULL)
769 stderr = initial_stderr_stream;
770 initial_stderr_stream = NULL;
771 report_file_error ("Cannot open debugging output stream", file);
774 return Qnil;
776 #endif /* GNU_LINUX */
779 /* This is the interface for debugging printing. */
781 void
782 debug_print (Lisp_Object arg)
784 Fprin1 (arg, Qexternal_debugging_output);
785 fprintf (stderr, "\r\n");
788 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
789 void
790 safe_debug_print (Lisp_Object arg)
792 int valid = valid_lisp_object_p (arg);
794 if (valid > 0)
795 debug_print (arg);
796 else
798 EMACS_UINT n = XLI (arg);
799 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
800 !valid ? "INVALID" : "SOME",
806 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
807 1, 1, 0,
808 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
809 See Info anchor `(elisp)Definition of signal' for some details on how this
810 error message is constructed. */)
811 (Lisp_Object obj)
813 struct buffer *old = current_buffer;
814 Lisp_Object value;
815 struct gcpro gcpro1;
817 /* If OBJ is (error STRING), just return STRING.
818 That is not only faster, it also avoids the need to allocate
819 space here when the error is due to memory full. */
820 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
821 && CONSP (XCDR (obj))
822 && STRINGP (XCAR (XCDR (obj)))
823 && NILP (XCDR (XCDR (obj))))
824 return XCAR (XCDR (obj));
826 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
828 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
829 value = Fbuffer_string ();
831 GCPRO1 (value);
832 Ferase_buffer ();
833 set_buffer_internal (old);
834 UNGCPRO;
836 return value;
839 /* Print an error message for the error DATA onto Lisp output stream
840 STREAM (suitable for the print functions).
841 CONTEXT is a C string describing the context of the error.
842 CALLER is the Lisp function inside which the error was signaled. */
844 void
845 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
846 Lisp_Object caller)
848 Lisp_Object errname, errmsg, file_error, tail;
849 struct gcpro gcpro1;
851 if (context != 0)
852 write_string_1 (context, stream);
854 /* If we know from where the error was signaled, show it in
855 *Messages*. */
856 if (!NILP (caller) && SYMBOLP (caller))
858 Lisp_Object cname = SYMBOL_NAME (caller);
859 ptrdiff_t cnamelen = SBYTES (cname);
860 USE_SAFE_ALLOCA;
861 char *name = SAFE_ALLOCA (cnamelen);
862 memcpy (name, SDATA (cname), cnamelen);
863 message_dolog (name, cnamelen, 0, 0);
864 message_dolog (": ", 2, 0, 0);
865 SAFE_FREE ();
868 errname = Fcar (data);
870 if (EQ (errname, Qerror))
872 data = Fcdr (data);
873 if (!CONSP (data))
874 data = Qnil;
875 errmsg = Fcar (data);
876 file_error = Qnil;
878 else
880 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
881 errmsg = Fget (errname, Qerror_message);
882 file_error = Fmemq (Qfile_error, error_conditions);
885 /* Print an error message including the data items. */
887 tail = Fcdr_safe (data);
888 GCPRO1 (tail);
890 /* For file-error, make error message by concatenating
891 all the data items. They are all strings. */
892 if (!NILP (file_error) && CONSP (tail))
893 errmsg = XCAR (tail), tail = XCDR (tail);
896 const char *sep = ": ";
898 if (!STRINGP (errmsg))
899 write_string_1 ("peculiar error", stream);
900 else if (SCHARS (errmsg))
901 Fprinc (errmsg, stream);
902 else
903 sep = NULL;
905 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
907 Lisp_Object obj;
909 if (sep)
910 write_string_1 (sep, stream);
911 obj = XCAR (tail);
912 if (!NILP (file_error)
913 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
914 Fprinc (obj, stream);
915 else
916 Fprin1 (obj, stream);
920 UNGCPRO;
926 * The buffer should be at least as large as the max string size of the
927 * largest float, printed in the biggest notation. This is undoubtedly
928 * 20d float_output_format, with the negative of the C-constant "HUGE"
929 * from <math.h>.
931 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
933 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
934 * case of -1e307 in 20d float_output_format. What is one to do (short of
935 * re-writing _doprnt to be more sane)?
936 * -wsr
937 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
941 float_to_string (char *buf, double data)
943 char *cp;
944 int width;
945 int len;
947 /* Check for plus infinity in a way that won't lose
948 if there is no plus infinity. */
949 if (data == data / 2 && data > 1.0)
951 static char const infinity_string[] = "1.0e+INF";
952 strcpy (buf, infinity_string);
953 return sizeof infinity_string - 1;
955 /* Likewise for minus infinity. */
956 if (data == data / 2 && data < -1.0)
958 static char const minus_infinity_string[] = "-1.0e+INF";
959 strcpy (buf, minus_infinity_string);
960 return sizeof minus_infinity_string - 1;
962 /* Check for NaN in a way that won't fail if there are no NaNs. */
963 if (! (data * 0.0 >= 0.0))
965 /* Prepend "-" if the NaN's sign bit is negative.
966 The sign bit of a double is the bit that is 1 in -0.0. */
967 static char const NaN_string[] = "0.0e+NaN";
968 int i;
969 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
970 bool negative = 0;
971 u_data.d = data;
972 u_minus_zero.d = - 0.0;
973 for (i = 0; i < sizeof (double); i++)
974 if (u_data.c[i] & u_minus_zero.c[i])
976 *buf = '-';
977 negative = 1;
978 break;
981 strcpy (buf + negative, NaN_string);
982 return negative + sizeof NaN_string - 1;
985 if (NILP (Vfloat_output_format)
986 || !STRINGP (Vfloat_output_format))
987 lose:
989 /* Generate the fewest number of digits that represent the
990 floating point value without losing information. */
991 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
992 /* The decimal point must be printed, or the byte compiler can
993 get confused (Bug#8033). */
994 width = 1;
996 else /* oink oink */
998 /* Check that the spec we have is fully valid.
999 This means not only valid for printf,
1000 but meant for floats, and reasonable. */
1001 cp = SSDATA (Vfloat_output_format);
1003 if (cp[0] != '%')
1004 goto lose;
1005 if (cp[1] != '.')
1006 goto lose;
1008 cp += 2;
1010 /* Check the width specification. */
1011 width = -1;
1012 if ('0' <= *cp && *cp <= '9')
1014 width = 0;
1017 width = (width * 10) + (*cp++ - '0');
1018 if (DBL_DIG < width)
1019 goto lose;
1021 while (*cp >= '0' && *cp <= '9');
1023 /* A precision of zero is valid only for %f. */
1024 if (width == 0 && *cp != 'f')
1025 goto lose;
1028 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1029 goto lose;
1031 if (cp[1] != 0)
1032 goto lose;
1034 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1037 /* Make sure there is a decimal point with digit after, or an
1038 exponent, so that the value is readable as a float. But don't do
1039 this with "%.0f"; it's valid for that not to produce a decimal
1040 point. Note that width can be 0 only for %.0f. */
1041 if (width != 0)
1043 for (cp = buf; *cp; cp++)
1044 if ((*cp < '0' || *cp > '9') && *cp != '-')
1045 break;
1047 if (*cp == '.' && cp[1] == 0)
1049 cp[1] = '0';
1050 cp[2] = 0;
1051 len++;
1053 else if (*cp == 0)
1055 *cp++ = '.';
1056 *cp++ = '0';
1057 *cp++ = 0;
1058 len += 2;
1062 return len;
1066 static void
1067 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1069 new_backquote_output = 0;
1071 /* Reset print_number_index and Vprint_number_table only when
1072 the variable Vprint_continuous_numbering is nil. Otherwise,
1073 the values of these variables will be kept between several
1074 print functions. */
1075 if (NILP (Vprint_continuous_numbering)
1076 || NILP (Vprint_number_table))
1078 print_number_index = 0;
1079 Vprint_number_table = Qnil;
1082 /* Construct Vprint_number_table for print-gensym and print-circle. */
1083 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1085 /* Construct Vprint_number_table.
1086 This increments print_number_index for the objects added. */
1087 print_depth = 0;
1088 print_preprocess (obj);
1090 if (HASH_TABLE_P (Vprint_number_table))
1091 { /* Remove unnecessary objects, which appear only once in OBJ;
1092 that is, whose status is Qt. */
1093 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1094 ptrdiff_t i;
1096 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1097 if (!NILP (HASH_HASH (h, i))
1098 && EQ (HASH_VALUE (h, i), Qt))
1099 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1103 print_depth = 0;
1104 print_object (obj, printcharfun, escapeflag);
1107 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1108 (STRINGP (obj) || CONSP (obj) \
1109 || (VECTORLIKEP (obj) \
1110 && (VECTORP (obj) || COMPILEDP (obj) \
1111 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1112 || HASH_TABLE_P (obj) || FONTP (obj))) \
1113 || (! NILP (Vprint_gensym) \
1114 && SYMBOLP (obj) \
1115 && !SYMBOL_INTERNED_P (obj)))
1117 /* Construct Vprint_number_table according to the structure of OBJ.
1118 OBJ itself and all its elements will be added to Vprint_number_table
1119 recursively if it is a list, vector, compiled function, char-table,
1120 string (its text properties will be traced), or a symbol that has
1121 no obarray (this is for the print-gensym feature).
1122 The status fields of Vprint_number_table mean whether each object appears
1123 more than once in OBJ: Qnil at the first time, and Qt after that. */
1124 static void
1125 print_preprocess (Lisp_Object obj)
1127 int i;
1128 ptrdiff_t size;
1129 int loop_count = 0;
1130 Lisp_Object halftail;
1132 /* Avoid infinite recursion for circular nested structure
1133 in the case where Vprint_circle is nil. */
1134 if (NILP (Vprint_circle))
1136 /* Give up if we go so deep that print_object will get an error. */
1137 /* See similar code in print_object. */
1138 if (print_depth >= PRINT_CIRCLE)
1139 error ("Apparently circular structure being printed");
1141 for (i = 0; i < print_depth; i++)
1142 if (EQ (obj, being_printed[i]))
1143 return;
1144 being_printed[print_depth] = obj;
1147 print_depth++;
1148 halftail = obj;
1150 loop:
1151 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1153 if (!HASH_TABLE_P (Vprint_number_table))
1154 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1156 /* In case print-circle is nil and print-gensym is t,
1157 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1158 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1160 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1161 if (!NILP (num)
1162 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1163 always print the gensym with a number. This is a special for
1164 the lisp function byte-compile-output-docform. */
1165 || (!NILP (Vprint_continuous_numbering)
1166 && SYMBOLP (obj)
1167 && !SYMBOL_INTERNED_P (obj)))
1168 { /* OBJ appears more than once. Let's remember that. */
1169 if (!INTEGERP (num))
1171 print_number_index++;
1172 /* Negative number indicates it hasn't been printed yet. */
1173 Fputhash (obj, make_number (- print_number_index),
1174 Vprint_number_table);
1176 print_depth--;
1177 return;
1179 else
1180 /* OBJ is not yet recorded. Let's add to the table. */
1181 Fputhash (obj, Qt, Vprint_number_table);
1184 switch (XTYPE (obj))
1186 case Lisp_String:
1187 /* A string may have text properties, which can be circular. */
1188 traverse_intervals_noorder (string_intervals (obj),
1189 print_preprocess_string, Qnil);
1190 break;
1192 case Lisp_Cons:
1193 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1194 just as in print_object. */
1195 if (loop_count && EQ (obj, halftail))
1196 break;
1197 print_preprocess (XCAR (obj));
1198 obj = XCDR (obj);
1199 loop_count++;
1200 if (!(loop_count & 1))
1201 halftail = XCDR (halftail);
1202 goto loop;
1204 case Lisp_Vectorlike:
1205 size = ASIZE (obj);
1206 if (size & PSEUDOVECTOR_FLAG)
1207 size &= PSEUDOVECTOR_SIZE_MASK;
1208 for (i = (SUB_CHAR_TABLE_P (obj)
1209 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1210 print_preprocess (AREF (obj, i));
1211 if (HASH_TABLE_P (obj))
1212 { /* For hash tables, the key_and_value slot is past
1213 `size' because it needs to be marked specially in case
1214 the table is weak. */
1215 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1216 print_preprocess (h->key_and_value);
1218 break;
1220 default:
1221 break;
1224 print_depth--;
1227 static void
1228 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1230 print_preprocess (interval->plist);
1233 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1235 #define PRINT_STRING_NON_CHARSET_FOUND 1
1236 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1238 /* Bitwise or of the above macros. */
1239 static int print_check_string_result;
1241 static void
1242 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1244 Lisp_Object val;
1246 if (NILP (interval->plist)
1247 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1248 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1249 return;
1250 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1251 val = XCDR (XCDR (val)));
1252 if (! CONSP (val))
1254 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1255 return;
1257 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1259 if (! EQ (val, interval->plist)
1260 || CONSP (XCDR (XCDR (val))))
1261 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1263 if (NILP (Vprint_charset_text_property)
1264 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1266 int i, c;
1267 ptrdiff_t charpos = interval->position;
1268 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1269 Lisp_Object charset;
1271 charset = XCAR (XCDR (val));
1272 for (i = 0; i < LENGTH (interval); i++)
1274 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1275 if (! ASCII_CHAR_P (c)
1276 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1278 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1279 break;
1285 /* The value is (charset . nil). */
1286 static Lisp_Object print_prune_charset_plist;
1288 static Lisp_Object
1289 print_prune_string_charset (Lisp_Object string)
1291 print_check_string_result = 0;
1292 traverse_intervals (string_intervals (string), 0,
1293 print_check_string_charset_prop, string);
1294 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1296 string = Fcopy_sequence (string);
1297 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1299 if (NILP (print_prune_charset_plist))
1300 print_prune_charset_plist = list1 (Qcharset);
1301 Fremove_text_properties (make_number (0),
1302 make_number (SCHARS (string)),
1303 print_prune_charset_plist, string);
1305 else
1306 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1307 Qnil, string);
1309 return string;
1312 static void
1313 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1315 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1316 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1317 40))];
1319 QUIT;
1321 /* Detect circularities and truncate them. */
1322 if (NILP (Vprint_circle))
1324 /* Simple but incomplete way. */
1325 int i;
1327 /* See similar code in print_preprocess. */
1328 if (print_depth >= PRINT_CIRCLE)
1329 error ("Apparently circular structure being printed");
1331 for (i = 0; i < print_depth; i++)
1332 if (EQ (obj, being_printed[i]))
1334 int len = sprintf (buf, "#%d", i);
1335 strout (buf, len, len, printcharfun);
1336 return;
1338 being_printed[print_depth] = obj;
1340 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1342 /* With the print-circle feature. */
1343 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1344 if (INTEGERP (num))
1346 EMACS_INT n = XINT (num);
1347 if (n < 0)
1348 { /* Add a prefix #n= if OBJ has not yet been printed;
1349 that is, its status field is nil. */
1350 int len = sprintf (buf, "#%"pI"d=", -n);
1351 strout (buf, len, len, printcharfun);
1352 /* OBJ is going to be printed. Remember that fact. */
1353 Fputhash (obj, make_number (- n), Vprint_number_table);
1355 else
1357 /* Just print #n# if OBJ has already been printed. */
1358 int len = sprintf (buf, "#%"pI"d#", n);
1359 strout (buf, len, len, printcharfun);
1360 return;
1365 print_depth++;
1367 switch (XTYPE (obj))
1369 case_Lisp_Int:
1371 int len = sprintf (buf, "%"pI"d", XINT (obj));
1372 strout (buf, len, len, printcharfun);
1374 break;
1376 case Lisp_Float:
1378 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1379 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1380 strout (pigbuf, len, len, printcharfun);
1382 break;
1384 case Lisp_String:
1385 if (!escapeflag)
1386 print_string (obj, printcharfun);
1387 else
1389 register ptrdiff_t i, i_byte;
1390 struct gcpro gcpro1;
1391 ptrdiff_t size_byte;
1392 /* True means we must ensure that the next character we output
1393 cannot be taken as part of a hex character escape. */
1394 bool need_nonhex = false;
1395 bool multibyte = STRING_MULTIBYTE (obj);
1397 GCPRO1 (obj);
1399 if (! EQ (Vprint_charset_text_property, Qt))
1400 obj = print_prune_string_charset (obj);
1402 if (string_intervals (obj))
1403 print_c_string ("#(", printcharfun);
1405 printchar ('\"', printcharfun);
1406 size_byte = SBYTES (obj);
1408 for (i = 0, i_byte = 0; i_byte < size_byte;)
1410 /* Here, we must convert each multi-byte form to the
1411 corresponding character code before handing it to printchar. */
1412 int c;
1414 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1416 QUIT;
1418 if (multibyte
1419 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1420 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1421 && print_escape_nonascii))
1423 /* When printing a raw 8-bit byte in a multibyte buffer, or
1424 (when requested) a non-ASCII character in a unibyte buffer,
1425 print single-byte non-ASCII string chars
1426 using octal escapes. */
1427 char outbuf[5];
1428 int len = sprintf (outbuf, "\\%03o", c + 0u);
1429 strout (outbuf, len, len, printcharfun);
1430 need_nonhex = false;
1432 else if (multibyte
1433 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1435 /* When requested, print multibyte chars using hex escapes. */
1436 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1437 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1438 strout (outbuf, len, len, printcharfun);
1439 need_nonhex = true;
1441 else
1443 /* If we just had a hex escape, and this character
1444 could be taken as part of it,
1445 output `\ ' to prevent that. */
1446 if (need_nonhex && c_isxdigit (c))
1447 print_c_string ("\\ ", printcharfun);
1449 if (c == '\n' && print_escape_newlines
1450 ? (c = 'n', true)
1451 : c == '\f' && print_escape_newlines
1452 ? (c = 'f', true)
1453 : c == '\"' || c == '\\')
1454 printchar ('\\', printcharfun);
1456 printchar (c, printcharfun);
1457 need_nonhex = false;
1460 printchar ('\"', printcharfun);
1462 if (string_intervals (obj))
1464 traverse_intervals (string_intervals (obj),
1465 0, print_interval, printcharfun);
1466 printchar (')', printcharfun);
1469 UNGCPRO;
1471 break;
1473 case Lisp_Symbol:
1475 bool confusing;
1476 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1477 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1478 int c;
1479 ptrdiff_t i, i_byte;
1480 ptrdiff_t size_byte;
1481 Lisp_Object name;
1483 name = SYMBOL_NAME (obj);
1485 if (p != end && (*p == '-' || *p == '+')) p++;
1486 if (p == end)
1487 confusing = 0;
1488 /* If symbol name begins with a digit, and ends with a digit,
1489 and contains nothing but digits and `e', it could be treated
1490 as a number. So set CONFUSING.
1492 Symbols that contain periods could also be taken as numbers,
1493 but periods are always escaped, so we don't have to worry
1494 about them here. */
1495 else if (*p >= '0' && *p <= '9'
1496 && end[-1] >= '0' && end[-1] <= '9')
1498 while (p != end && ((*p >= '0' && *p <= '9')
1499 /* Needed for \2e10. */
1500 || *p == 'e' || *p == 'E'))
1501 p++;
1502 confusing = (end == p);
1504 else
1505 confusing = 0;
1507 size_byte = SBYTES (name);
1509 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1510 print_c_string ("#:", printcharfun);
1511 else if (size_byte == 0)
1513 print_c_string ("##", printcharfun);
1514 break;
1517 for (i = 0, i_byte = 0; i_byte < size_byte;)
1519 /* Here, we must convert each multi-byte form to the
1520 corresponding character code before handing it to PRINTCHAR. */
1521 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1522 QUIT;
1524 if (escapeflag)
1526 if (c == '\"' || c == '\\' || c == '\''
1527 || c == ';' || c == '#' || c == '(' || c == ')'
1528 || c == ',' || c == '.' || c == '`'
1529 || c == '[' || c == ']' || c == '?' || c <= 040
1530 || confusing)
1532 printchar ('\\', printcharfun);
1533 confusing = false;
1536 printchar (c, printcharfun);
1539 break;
1541 case Lisp_Cons:
1542 /* If deeper than spec'd depth, print placeholder. */
1543 if (INTEGERP (Vprint_level)
1544 && print_depth > XINT (Vprint_level))
1545 print_c_string ("...", printcharfun);
1546 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1547 && (EQ (XCAR (obj), Qquote)))
1549 printchar ('\'', printcharfun);
1550 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1552 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1553 && (EQ (XCAR (obj), Qfunction)))
1555 print_c_string ("#'", printcharfun);
1556 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1558 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1559 && ((EQ (XCAR (obj), Qbackquote))))
1561 print_object (XCAR (obj), printcharfun, 0);
1562 new_backquote_output++;
1563 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1564 new_backquote_output--;
1566 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1567 && new_backquote_output
1568 && ((EQ (XCAR (obj), Qbackquote)
1569 || EQ (XCAR (obj), Qcomma)
1570 || EQ (XCAR (obj), Qcomma_at)
1571 || EQ (XCAR (obj), Qcomma_dot))))
1573 print_object (XCAR (obj), printcharfun, 0);
1574 new_backquote_output--;
1575 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1576 new_backquote_output++;
1578 else
1580 printchar ('(', printcharfun);
1582 Lisp_Object halftail = obj;
1584 /* Negative values of print-length are invalid in CL.
1585 Treat them like nil, as CMUCL does. */
1586 printmax_t print_length = (NATNUMP (Vprint_length)
1587 ? XFASTINT (Vprint_length)
1588 : TYPE_MAXIMUM (printmax_t));
1590 printmax_t i = 0;
1591 while (CONSP (obj))
1593 /* Detect circular list. */
1594 if (NILP (Vprint_circle))
1596 /* Simple but incomplete way. */
1597 if (i != 0 && EQ (obj, halftail))
1599 int len = sprintf (buf, " . #%"pMd, i / 2);
1600 strout (buf, len, len, printcharfun);
1601 goto end_of_list;
1604 else
1606 /* With the print-circle feature. */
1607 if (i != 0)
1609 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1610 if (INTEGERP (num))
1612 print_c_string (" . ", printcharfun);
1613 print_object (obj, printcharfun, escapeflag);
1614 goto end_of_list;
1619 if (i)
1620 printchar (' ', printcharfun);
1622 if (print_length <= i)
1624 print_c_string ("...", printcharfun);
1625 goto end_of_list;
1628 i++;
1629 print_object (XCAR (obj), printcharfun, escapeflag);
1631 obj = XCDR (obj);
1632 if (!(i & 1))
1633 halftail = XCDR (halftail);
1636 /* OBJ non-nil here means it's the end of a dotted list. */
1637 if (!NILP (obj))
1639 print_c_string (" . ", printcharfun);
1640 print_object (obj, printcharfun, escapeflag);
1643 end_of_list:
1644 printchar (')', printcharfun);
1646 break;
1648 case Lisp_Vectorlike:
1649 if (PROCESSP (obj))
1651 if (escapeflag)
1653 print_c_string ("#<process ", printcharfun);
1654 print_string (XPROCESS (obj)->name, printcharfun);
1655 printchar ('>', printcharfun);
1657 else
1658 print_string (XPROCESS (obj)->name, printcharfun);
1660 else if (BOOL_VECTOR_P (obj))
1662 ptrdiff_t i;
1663 unsigned char c;
1664 struct gcpro gcpro1;
1665 EMACS_INT size = bool_vector_size (obj);
1666 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1667 ptrdiff_t real_size_in_chars = size_in_chars;
1668 GCPRO1 (obj);
1670 int len = sprintf (buf, "#&%"pI"d\"", size);
1671 strout (buf, len, len, printcharfun);
1673 /* Don't print more characters than the specified maximum.
1674 Negative values of print-length are invalid. Treat them
1675 like a print-length of nil. */
1676 if (NATNUMP (Vprint_length)
1677 && XFASTINT (Vprint_length) < size_in_chars)
1678 size_in_chars = XFASTINT (Vprint_length);
1680 for (i = 0; i < size_in_chars; i++)
1682 QUIT;
1683 c = bool_vector_uchar_data (obj)[i];
1684 if (c == '\n' && print_escape_newlines)
1685 print_c_string ("\\n", printcharfun);
1686 else if (c == '\f' && print_escape_newlines)
1687 print_c_string ("\\f", printcharfun);
1688 else if (c > '\177')
1690 /* Use octal escapes to avoid encoding issues. */
1691 len = sprintf (buf, "\\%o", c);
1692 strout (buf, len, len, printcharfun);
1694 else
1696 if (c == '\"' || c == '\\')
1697 printchar ('\\', printcharfun);
1698 printchar (c, printcharfun);
1702 if (size_in_chars < real_size_in_chars)
1703 print_c_string (" ...", printcharfun);
1704 printchar ('\"', printcharfun);
1706 UNGCPRO;
1708 else if (SUBRP (obj))
1710 print_c_string ("#<subr ", printcharfun);
1711 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1712 printchar ('>', printcharfun);
1714 else if (WINDOWP (obj))
1716 int len = sprintf (buf, "#<window %"pI"d",
1717 XWINDOW (obj)->sequence_number);
1718 strout (buf, len, len, printcharfun);
1719 if (BUFFERP (XWINDOW (obj)->contents))
1721 print_c_string (" on ", printcharfun);
1722 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1723 printcharfun);
1725 printchar ('>', printcharfun);
1727 else if (TERMINALP (obj))
1729 struct terminal *t = XTERMINAL (obj);
1730 int len = sprintf (buf, "#<terminal %d", t->id);
1731 strout (buf, len, len, printcharfun);
1732 if (t->name)
1734 print_c_string (" on ", printcharfun);
1735 print_c_string (t->name, printcharfun);
1737 printchar ('>', printcharfun);
1739 else if (HASH_TABLE_P (obj))
1741 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1742 ptrdiff_t i;
1743 ptrdiff_t real_size, size;
1744 int len;
1745 #if 0
1746 void *ptr = h;
1747 print_c_string ("#<hash-table", printcharfun);
1748 if (SYMBOLP (h->test))
1750 print_c_string (" '", printcharfun);
1751 print_c_string (SSDATA (SYMBOL_NAME (h->test)), printcharfun);
1752 printchar (' ', printcharfun);
1753 print_c_string (SSDATA (SYMBOL_NAME (h->weak)), printcharfun);
1754 len = sprintf (buf, " %"pD"d/%"pD"d", h->count, ASIZE (h->next));
1755 strout (buf, len, len, printcharfun);
1757 len = sprintf (buf, " %p>", ptr);
1758 strout (buf, len, len, printcharfun);
1759 #endif
1760 /* Implement a readable output, e.g.:
1761 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1762 /* Always print the size. */
1763 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1764 strout (buf, len, len, printcharfun);
1766 if (!NILP (h->test.name))
1768 print_c_string (" test ", printcharfun);
1769 print_object (h->test.name, printcharfun, escapeflag);
1772 if (!NILP (h->weak))
1774 print_c_string (" weakness ", printcharfun);
1775 print_object (h->weak, printcharfun, escapeflag);
1778 if (!NILP (h->rehash_size))
1780 print_c_string (" rehash-size ", printcharfun);
1781 print_object (h->rehash_size, printcharfun, escapeflag);
1784 if (!NILP (h->rehash_threshold))
1786 print_c_string (" rehash-threshold ", printcharfun);
1787 print_object (h->rehash_threshold, printcharfun, escapeflag);
1790 print_c_string (" data ", printcharfun);
1792 /* Print the data here as a plist. */
1793 real_size = HASH_TABLE_SIZE (h);
1794 size = real_size;
1796 /* Don't print more elements than the specified maximum. */
1797 if (NATNUMP (Vprint_length)
1798 && XFASTINT (Vprint_length) < size)
1799 size = XFASTINT (Vprint_length);
1801 printchar ('(', printcharfun);
1802 for (i = 0; i < size; i++)
1803 if (!NILP (HASH_HASH (h, i)))
1805 if (i) printchar (' ', printcharfun);
1806 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1807 printchar (' ', printcharfun);
1808 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1811 if (size < real_size)
1812 print_c_string (" ...", printcharfun);
1814 print_c_string ("))", printcharfun);
1817 else if (BUFFERP (obj))
1819 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1820 print_c_string ("#<killed buffer>", printcharfun);
1821 else if (escapeflag)
1823 print_c_string ("#<buffer ", printcharfun);
1824 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1825 printchar ('>', printcharfun);
1827 else
1828 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1830 else if (WINDOW_CONFIGURATIONP (obj))
1831 print_c_string ("#<window-configuration>", printcharfun);
1832 else if (FRAMEP (obj))
1834 int len;
1835 void *ptr = XFRAME (obj);
1836 Lisp_Object frame_name = XFRAME (obj)->name;
1838 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1839 ? "#<frame "
1840 : "#<dead frame "),
1841 printcharfun);
1842 if (!STRINGP (frame_name))
1844 /* A frame could be too young and have no name yet;
1845 don't crash. */
1846 if (SYMBOLP (frame_name))
1847 frame_name = Fsymbol_name (frame_name);
1848 else /* can't happen: name should be either nil or string */
1849 frame_name = build_string ("*INVALID*FRAME*NAME*");
1851 print_string (frame_name, printcharfun);
1852 len = sprintf (buf, " %p>", ptr);
1853 strout (buf, len, len, printcharfun);
1855 else if (FONTP (obj))
1857 int i;
1859 if (! FONT_OBJECT_P (obj))
1861 if (FONT_SPEC_P (obj))
1862 print_c_string ("#<font-spec", printcharfun);
1863 else
1864 print_c_string ("#<font-entity", printcharfun);
1865 for (i = 0; i < FONT_SPEC_MAX; i++)
1867 printchar (' ', printcharfun);
1868 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1869 print_object (AREF (obj, i), printcharfun, escapeflag);
1870 else
1871 print_object (font_style_symbolic (obj, i, 0),
1872 printcharfun, escapeflag);
1875 else
1877 print_c_string ("#<font-object ", printcharfun);
1878 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1879 escapeflag);
1881 printchar ('>', printcharfun);
1883 else
1885 ptrdiff_t size = ASIZE (obj);
1886 if (COMPILEDP (obj))
1888 printchar ('#', printcharfun);
1889 size &= PSEUDOVECTOR_SIZE_MASK;
1891 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1893 /* We print a char-table as if it were a vector,
1894 lumping the parent and default slots in with the
1895 character slots. But we add #^ as a prefix. */
1897 /* Make each lowest sub_char_table start a new line.
1898 Otherwise we'll make a line extremely long, which
1899 results in slow redisplay. */
1900 if (SUB_CHAR_TABLE_P (obj)
1901 && XSUB_CHAR_TABLE (obj)->depth == 3)
1902 printchar ('\n', printcharfun);
1903 print_c_string ("#^", printcharfun);
1904 if (SUB_CHAR_TABLE_P (obj))
1905 printchar ('^', printcharfun);
1906 size &= PSEUDOVECTOR_SIZE_MASK;
1908 if (size & PSEUDOVECTOR_FLAG)
1909 goto badtype;
1911 printchar ('[', printcharfun);
1913 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1914 Lisp_Object tem;
1915 ptrdiff_t real_size = size;
1917 /* For a sub char-table, print heading non-Lisp data first. */
1918 if (SUB_CHAR_TABLE_P (obj))
1920 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1921 XSUB_CHAR_TABLE (obj)->min_char);
1922 strout (buf, i, i, printcharfun);
1925 /* Don't print more elements than the specified maximum. */
1926 if (NATNUMP (Vprint_length)
1927 && XFASTINT (Vprint_length) < size)
1928 size = XFASTINT (Vprint_length);
1930 for (i = idx; i < size; i++)
1932 if (i) printchar (' ', printcharfun);
1933 tem = AREF (obj, i);
1934 print_object (tem, printcharfun, escapeflag);
1936 if (size < real_size)
1937 print_c_string (" ...", printcharfun);
1939 printchar (']', printcharfun);
1941 break;
1943 case Lisp_Misc:
1944 switch (XMISCTYPE (obj))
1946 case Lisp_Misc_Marker:
1947 print_c_string ("#<marker ", printcharfun);
1948 /* Do you think this is necessary? */
1949 if (XMARKER (obj)->insertion_type != 0)
1950 print_c_string ("(moves after insertion) ", printcharfun);
1951 if (! XMARKER (obj)->buffer)
1952 print_c_string ("in no buffer", printcharfun);
1953 else
1955 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1956 strout (buf, len, len, printcharfun);
1957 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1959 printchar ('>', printcharfun);
1960 break;
1962 case Lisp_Misc_Overlay:
1963 print_c_string ("#<overlay ", printcharfun);
1964 if (! XMARKER (OVERLAY_START (obj))->buffer)
1965 print_c_string ("in no buffer", printcharfun);
1966 else
1968 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
1969 marker_position (OVERLAY_START (obj)),
1970 marker_position (OVERLAY_END (obj)));
1971 strout (buf, len, len, printcharfun);
1972 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
1973 printcharfun);
1975 printchar ('>', printcharfun);
1976 break;
1978 case Lisp_Misc_Finalizer:
1979 print_c_string ("#<finalizer", printcharfun);
1980 if (NILP (XFINALIZER (obj)->function))
1981 print_c_string (" used", printcharfun);
1982 printchar ('>', printcharfun);
1983 break;
1985 /* Remaining cases shouldn't happen in normal usage, but let's
1986 print them anyway for the benefit of the debugger. */
1988 case Lisp_Misc_Free:
1989 print_c_string ("#<misc free cell>", printcharfun);
1990 break;
1992 case Lisp_Misc_Save_Value:
1994 int i;
1995 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
1997 print_c_string ("#<save-value ", printcharfun);
1999 if (v->save_type == SAVE_TYPE_MEMORY)
2001 ptrdiff_t amount = v->data[1].integer;
2003 #if GC_MARK_STACK
2005 /* valid_lisp_object_p is reliable, so try to print up
2006 to 8 saved objects. This code is rarely used, so
2007 it's OK that valid_lisp_object_p is slow. */
2009 int limit = min (amount, 8);
2010 Lisp_Object *area = v->data[0].pointer;
2012 i = sprintf (buf, "with %"pD"d objects", amount);
2013 strout (buf, i, i, printcharfun);
2015 for (i = 0; i < limit; i++)
2017 Lisp_Object maybe = area[i];
2018 int valid = valid_lisp_object_p (maybe);
2020 printchar (' ', printcharfun);
2021 if (0 < valid)
2022 print_object (maybe, printcharfun, escapeflag);
2023 else
2024 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2025 printcharfun);
2027 if (i == limit && i < amount)
2028 print_c_string (" ...", printcharfun);
2030 #else /* not GC_MARK_STACK */
2032 /* There is no reliable way to determine whether the objects
2033 are initialized, so do not try to print them. */
2035 i = sprintf (buf, "with %"pD"d objects", amount);
2036 strout (buf, i, i, printcharfun);
2038 #endif /* GC_MARK_STACK */
2040 else
2042 /* Print each slot according to its type. */
2043 int index;
2044 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2046 if (index)
2047 printchar (' ', printcharfun);
2049 switch (save_type (v, index))
2051 case SAVE_UNUSED:
2052 i = sprintf (buf, "<unused>");
2053 break;
2055 case SAVE_POINTER:
2056 i = sprintf (buf, "<pointer %p>",
2057 v->data[index].pointer);
2058 break;
2060 case SAVE_FUNCPOINTER:
2061 i = sprintf (buf, "<funcpointer %p>",
2062 ((void *) (intptr_t)
2063 v->data[index].funcpointer));
2064 break;
2066 case SAVE_INTEGER:
2067 i = sprintf (buf, "<integer %"pD"d>",
2068 v->data[index].integer);
2069 break;
2071 case SAVE_OBJECT:
2072 print_object (v->data[index].object, printcharfun,
2073 escapeflag);
2074 continue;
2076 default:
2077 emacs_abort ();
2080 strout (buf, i, i, printcharfun);
2083 printchar ('>', printcharfun);
2085 break;
2087 default:
2088 goto badtype;
2090 break;
2092 default:
2093 badtype:
2095 int len;
2096 /* We're in trouble if this happens!
2097 Probably should just emacs_abort (). */
2098 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2099 if (MISCP (obj))
2100 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2101 else if (VECTORLIKEP (obj))
2102 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2103 else
2104 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2105 strout (buf, len, len, printcharfun);
2106 print_c_string ((" Save your buffers immediately"
2107 " and please report this bug>"),
2108 printcharfun);
2112 print_depth--;
2116 /* Print a description of INTERVAL using PRINTCHARFUN.
2117 This is part of printing a string that has text properties. */
2119 static void
2120 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2122 if (NILP (interval->plist))
2123 return;
2124 printchar (' ', printcharfun);
2125 print_object (make_number (interval->position), printcharfun, 1);
2126 printchar (' ', printcharfun);
2127 print_object (make_number (interval->position + LENGTH (interval)),
2128 printcharfun, 1);
2129 printchar (' ', printcharfun);
2130 print_object (interval->plist, printcharfun, 1);
2133 /* Initialize debug_print stuff early to have it working from the very
2134 beginning. */
2136 void
2137 init_print_once (void)
2139 /* The subroutine object for external-debugging-output is kept here
2140 for the convenience of the debugger. */
2141 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2143 defsubr (&Sexternal_debugging_output);
2146 void
2147 syms_of_print (void)
2149 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2151 DEFVAR_LISP ("standard-output", Vstandard_output,
2152 doc: /* Output stream `print' uses by default for outputting a character.
2153 This may be any function of one argument.
2154 It may also be a buffer (output is inserted before point)
2155 or a marker (output is inserted and the marker is advanced)
2156 or the symbol t (output appears in the echo area). */);
2157 Vstandard_output = Qt;
2158 DEFSYM (Qstandard_output, "standard-output");
2160 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2161 doc: /* The format descriptor string used to print floats.
2162 This is a %-spec like those accepted by `printf' in C,
2163 but with some restrictions. It must start with the two characters `%.'.
2164 After that comes an integer precision specification,
2165 and then a letter which controls the format.
2166 The letters allowed are `e', `f' and `g'.
2167 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2168 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2169 Use `g' to choose the shorter of those two formats for the number at hand.
2170 The precision in any of these cases is the number of digits following
2171 the decimal point. With `f', a precision of 0 means to omit the
2172 decimal point. 0 is not allowed with `e' or `g'.
2174 A value of nil means to use the shortest notation
2175 that represents the number without losing information. */);
2176 Vfloat_output_format = Qnil;
2177 DEFSYM (Qfloat_output_format, "float-output-format");
2179 DEFVAR_LISP ("print-length", Vprint_length,
2180 doc: /* Maximum length of list to print before abbreviating.
2181 A value of nil means no limit. See also `eval-expression-print-length'. */);
2182 Vprint_length = Qnil;
2184 DEFVAR_LISP ("print-level", Vprint_level,
2185 doc: /* Maximum depth of list nesting to print before abbreviating.
2186 A value of nil means no limit. See also `eval-expression-print-level'. */);
2187 Vprint_level = Qnil;
2189 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2190 doc: /* Non-nil means print newlines in strings as `\\n'.
2191 Also print formfeeds as `\\f'. */);
2192 print_escape_newlines = 0;
2194 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2195 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2196 \(OOO is the octal representation of the character code.)
2197 Only single-byte characters are affected, and only in `prin1'.
2198 When the output goes in a multibyte buffer, this feature is
2199 enabled regardless of the value of the variable. */);
2200 print_escape_nonascii = 0;
2202 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2203 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2204 \(XXXX is the hex representation of the character code.)
2205 This affects only `prin1'. */);
2206 print_escape_multibyte = 0;
2208 DEFVAR_BOOL ("print-quoted", print_quoted,
2209 doc: /* Non-nil means print quoted forms with reader syntax.
2210 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2211 print_quoted = 0;
2213 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2214 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2215 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2216 When the uninterned symbol appears within a recursive data structure,
2217 and the symbol appears more than once, in addition use the #N# and #N=
2218 constructs as needed, so that multiple references to the same symbol are
2219 shared once again when the text is read back. */);
2220 Vprint_gensym = Qnil;
2222 DEFVAR_LISP ("print-circle", Vprint_circle,
2223 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2224 If nil, printing proceeds recursively and may lead to
2225 `max-lisp-eval-depth' being exceeded or an error may occur:
2226 \"Apparently circular structure being printed.\" Also see
2227 `print-length' and `print-level'.
2228 If non-nil, shared substructures anywhere in the structure are printed
2229 with `#N=' before the first occurrence (in the order of the print
2230 representation) and `#N#' in place of each subsequent occurrence,
2231 where N is a positive decimal integer. */);
2232 Vprint_circle = Qnil;
2234 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2235 doc: /* Non-nil means number continuously across print calls.
2236 This affects the numbers printed for #N= labels and #M# references.
2237 See also `print-circle', `print-gensym', and `print-number-table'.
2238 This variable should not be set with `setq'; bind it with a `let' instead. */);
2239 Vprint_continuous_numbering = Qnil;
2241 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2242 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2243 The Lisp printer uses this vector to detect Lisp objects referenced more
2244 than once.
2246 When you bind `print-continuous-numbering' to t, you should probably
2247 also bind `print-number-table' to nil. This ensures that the value of
2248 `print-number-table' can be garbage-collected once the printing is
2249 done. If all elements of `print-number-table' are nil, it means that
2250 the printing done so far has not found any shared structure or objects
2251 that need to be recorded in the table. */);
2252 Vprint_number_table = Qnil;
2254 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2255 doc: /* A flag to control printing of `charset' text property on printing a string.
2256 The value must be nil, t, or `default'.
2258 If the value is nil, don't print the text property `charset'.
2260 If the value is t, always print the text property `charset'.
2262 If the value is `default', print the text property `charset' only when
2263 the value is different from what is guessed in the current charset
2264 priorities. */);
2265 Vprint_charset_text_property = Qdefault;
2267 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2268 staticpro (&Vprin1_to_string_buffer);
2270 defsubr (&Sprin1);
2271 defsubr (&Sprin1_to_string);
2272 defsubr (&Serror_message_string);
2273 defsubr (&Sprinc);
2274 defsubr (&Sprint);
2275 defsubr (&Sterpri);
2276 defsubr (&Swrite_char);
2277 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2278 defsubr (&Sredirect_debugging_output);
2279 #endif
2281 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2282 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2283 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2285 print_prune_charset_plist = Qnil;
2286 staticpro (&print_prune_charset_plist);