Reimplement auto-saving to visited files
[emacs.git] / src / print.c
blob872103bd4c2331525ebaae1351b14af2c9811e4f
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2017 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 (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include "sysstdio.h"
25 #include "lisp.h"
26 #include "character.h"
27 #include "coding.h"
28 #include "buffer.h"
29 #include "charset.h"
30 #include "frame.h"
31 #include "process.h"
32 #include "disptab.h"
33 #include "intervals.h"
34 #include "blockinput.h"
35 #include "xwidget.h"
37 #include <c-ctype.h>
38 #include <float.h>
39 #include <ftoastr.h>
41 #ifdef WINDOWSNT
42 # include <sys/socket.h> /* for F_DUPFD_CLOEXEC */
43 #endif
45 struct terminal;
47 /* Avoid actual stack overflow in print. */
48 static ptrdiff_t print_depth;
50 /* Level of nesting inside outputting backquote in new style. */
51 static ptrdiff_t new_backquote_output;
53 /* Detect most circularities to print finite output. */
54 #define PRINT_CIRCLE 200
55 static Lisp_Object being_printed[PRINT_CIRCLE];
57 /* Last char printed to stdout by printchar. */
58 static unsigned int printchar_stdout_last;
60 /* When printing into a buffer, first we put the text in this
61 block, then insert it all at once. */
62 static char *print_buffer;
64 /* Size allocated in print_buffer. */
65 static ptrdiff_t print_buffer_size;
66 /* Chars stored in print_buffer. */
67 static ptrdiff_t print_buffer_pos;
68 /* Bytes stored in print_buffer. */
69 static ptrdiff_t print_buffer_pos_byte;
71 /* Vprint_number_table is a table, that keeps objects that are going to
72 be printed, to allow use of #n= and #n# to express sharing.
73 For any given object, the table can give the following values:
74 t the object will be printed only once.
75 -N the object will be printed several times and will take number N.
76 N the object has been printed so we can refer to it as #N#.
77 print_number_index holds the largest N already used.
78 N has to be striclty larger than 0 since we need to distinguish -N. */
79 static ptrdiff_t print_number_index;
80 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
82 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
83 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
86 /* Low level output routines for characters and strings. */
88 /* Lisp functions to do output using a stream
89 must have the stream in a variable called printcharfun
90 and must start with PRINTPREPARE, end with PRINTFINISH.
91 Use printchar to output one character,
92 or call strout to output a block of characters. */
94 #define PRINTPREPARE \
95 struct buffer *old = current_buffer; \
96 ptrdiff_t old_point = -1, start_point = -1; \
97 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
98 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
99 bool free_print_buffer = 0; \
100 bool multibyte \
101 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
102 Lisp_Object original = printcharfun; \
103 if (NILP (printcharfun)) printcharfun = Qt; \
104 if (BUFFERP (printcharfun)) \
106 if (XBUFFER (printcharfun) != current_buffer) \
107 Fset_buffer (printcharfun); \
108 printcharfun = Qnil; \
110 if (MARKERP (printcharfun)) \
112 ptrdiff_t marker_pos; \
113 if (! XMARKER (printcharfun)->buffer) \
114 error ("Marker does not point anywhere"); \
115 if (XMARKER (printcharfun)->buffer != current_buffer) \
116 set_buffer_internal (XMARKER (printcharfun)->buffer); \
117 marker_pos = marker_position (printcharfun); \
118 if (marker_pos < BEGV || marker_pos > ZV) \
119 signal_error ("Marker is outside the accessible " \
120 "part of the buffer", printcharfun); \
121 old_point = PT; \
122 old_point_byte = PT_BYTE; \
123 SET_PT_BOTH (marker_pos, \
124 marker_byte_position (printcharfun)); \
125 start_point = PT; \
126 start_point_byte = PT_BYTE; \
127 printcharfun = Qnil; \
129 if (NILP (printcharfun)) \
131 Lisp_Object string; \
132 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
133 && ! print_escape_multibyte) \
134 specbind (Qprint_escape_multibyte, Qt); \
135 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
136 && ! print_escape_nonascii) \
137 specbind (Qprint_escape_nonascii, Qt); \
138 if (print_buffer != 0) \
140 string = make_string_from_bytes (print_buffer, \
141 print_buffer_pos, \
142 print_buffer_pos_byte); \
143 record_unwind_protect (print_unwind, string); \
145 else \
147 int new_size = 1000; \
148 print_buffer = xmalloc (new_size); \
149 print_buffer_size = new_size; \
150 free_print_buffer = 1; \
152 print_buffer_pos = 0; \
153 print_buffer_pos_byte = 0; \
155 if (EQ (printcharfun, Qt) && ! noninteractive) \
156 setup_echo_area_for_printing (multibyte);
158 #define PRINTFINISH \
159 if (NILP (printcharfun)) \
161 if (print_buffer_pos != print_buffer_pos_byte \
162 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
164 USE_SAFE_ALLOCA; \
165 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
166 copy_text ((unsigned char *) print_buffer, temp, \
167 print_buffer_pos_byte, 1, 0); \
168 insert_1_both ((char *) temp, print_buffer_pos, \
169 print_buffer_pos, 0, 1, 0); \
170 SAFE_FREE (); \
172 else \
173 insert_1_both (print_buffer, print_buffer_pos, \
174 print_buffer_pos_byte, 0, 1, 0); \
175 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
177 if (free_print_buffer) \
179 xfree (print_buffer); \
180 print_buffer = 0; \
182 unbind_to (specpdl_count, Qnil); \
183 if (MARKERP (original)) \
184 set_marker_both (original, Qnil, PT, PT_BYTE); \
185 if (old_point >= 0) \
186 SET_PT_BOTH (old_point + (old_point >= start_point \
187 ? PT - start_point : 0), \
188 old_point_byte + (old_point_byte >= start_point_byte \
189 ? PT_BYTE - start_point_byte : 0)); \
190 set_buffer_internal (old);
192 /* This is used to restore the saved contents of print_buffer
193 when there is a recursive call to print. */
195 static void
196 print_unwind (Lisp_Object saved_text)
198 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
201 /* Print character CH to the stdio stream STREAM. */
203 static void
204 printchar_to_stream (unsigned int ch, FILE *stream)
206 Lisp_Object dv UNINIT;
207 ptrdiff_t i = 0, n = 1;
208 Lisp_Object coding_system = Vlocale_coding_system;
209 bool encode_p = false;
211 if (!NILP (Vcoding_system_for_write))
212 coding_system = Vcoding_system_for_write;
213 if (!NILP (coding_system))
214 encode_p = true;
216 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
218 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
219 if (VECTORP (dv))
221 n = ASIZE (dv);
222 goto next_char;
226 while (true)
228 if (ASCII_CHAR_P (ch))
230 putc (ch, stream);
231 #ifdef WINDOWSNT
232 /* Send the output to a debugger (nothing happens if there
233 isn't one). */
234 if (print_output_debug_flag && stream == stderr)
235 OutputDebugString ((char []) {ch, '\0'});
236 #endif
238 else
240 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
241 int len = CHAR_STRING (ch, mbstr);
242 Lisp_Object encoded_ch =
243 make_multibyte_string ((char *) mbstr, 1, len);
245 if (encode_p)
246 encoded_ch = code_convert_string_norecord (encoded_ch,
247 coding_system, true);
248 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
249 #ifdef WINDOWSNT
250 if (print_output_debug_flag && stream == stderr)
251 OutputDebugString (SSDATA (encoded_ch));
252 #endif
255 i++;
257 next_char:
258 for (; i < n; i++)
259 if (CHARACTERP (AREF (dv, i)))
260 break;
261 if (! (i < n))
262 break;
263 ch = XFASTINT (AREF (dv, i));
267 /* Print character CH using method FUN. FUN nil means print to
268 print_buffer. FUN t means print to echo area or stdout if
269 non-interactive. If FUN is neither nil nor t, call FUN with CH as
270 argument. */
272 static void
273 printchar (unsigned int ch, Lisp_Object fun)
275 if (!NILP (fun) && !EQ (fun, Qt))
276 call1 (fun, make_number (ch));
277 else
279 unsigned char str[MAX_MULTIBYTE_LENGTH];
280 int len = CHAR_STRING (ch, str);
282 maybe_quit ();
284 if (NILP (fun))
286 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
287 if (incr > 0)
288 print_buffer = xpalloc (print_buffer, &print_buffer_size,
289 incr, -1, 1);
290 memcpy (print_buffer + print_buffer_pos_byte, str, len);
291 print_buffer_pos += 1;
292 print_buffer_pos_byte += len;
294 else if (noninteractive)
296 printchar_stdout_last = ch;
297 if (DISP_TABLE_P (Vstandard_display_table))
298 printchar_to_stream (ch, stdout);
299 else
300 fwrite (str, 1, len, stdout);
301 noninteractive_need_newline = 1;
303 else
305 bool multibyte_p
306 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
308 setup_echo_area_for_printing (multibyte_p);
309 insert_char (ch);
310 message_dolog ((char *) str, len, 0, multibyte_p);
316 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
317 method PRINTCHARFUN. PRINTCHARFUN nil means output to
318 print_buffer. PRINTCHARFUN t means output to the echo area or to
319 stdout if non-interactive. If neither nil nor t, call Lisp
320 function PRINTCHARFUN for each character printed. MULTIBYTE
321 non-zero means PTR contains multibyte characters.
323 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
324 to data in a Lisp string. Otherwise that is not safe. */
326 static void
327 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
328 Lisp_Object printcharfun)
330 if (NILP (printcharfun))
332 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
333 if (incr > 0)
334 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
335 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
336 print_buffer_pos += size;
337 print_buffer_pos_byte += size_byte;
339 else if (noninteractive && EQ (printcharfun, Qt))
341 if (DISP_TABLE_P (Vstandard_display_table))
343 int len;
344 for (ptrdiff_t i = 0; i < size_byte; i += len)
346 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
347 len);
348 printchar_to_stream (ch, stdout);
351 else
352 fwrite (ptr, 1, size_byte, stdout);
354 noninteractive_need_newline = 1;
356 else if (EQ (printcharfun, Qt))
358 /* Output to echo area. We're trying to avoid a little overhead
359 here, that's the reason we don't call printchar to do the
360 job. */
361 int i;
362 bool multibyte_p
363 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
365 setup_echo_area_for_printing (multibyte_p);
366 message_dolog (ptr, size_byte, 0, multibyte_p);
368 if (size == size_byte)
370 for (i = 0; i < size; ++i)
371 insert_char ((unsigned char) *ptr++);
373 else
375 int len;
376 for (i = 0; i < size_byte; i += len)
378 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
379 len);
380 insert_char (ch);
384 else
386 /* PRINTCHARFUN is a Lisp function. */
387 ptrdiff_t i = 0;
389 if (size == size_byte)
391 while (i < size_byte)
393 int ch = ptr[i++];
394 printchar (ch, printcharfun);
397 else
399 while (i < size_byte)
401 /* Here, we must convert each multi-byte form to the
402 corresponding character code before handing it to
403 PRINTCHAR. */
404 int len;
405 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
406 len);
407 printchar (ch, printcharfun);
408 i += len;
414 /* Print the contents of a string STRING using PRINTCHARFUN.
415 It isn't safe to use strout in many cases,
416 because printing one char can relocate. */
418 static void
419 print_string (Lisp_Object string, Lisp_Object printcharfun)
421 if (EQ (printcharfun, Qt) || NILP (printcharfun))
423 ptrdiff_t chars;
425 if (print_escape_nonascii)
426 string = string_escape_byte8 (string);
428 if (STRING_MULTIBYTE (string))
429 chars = SCHARS (string);
430 else if (! print_escape_nonascii
431 && (EQ (printcharfun, Qt)
432 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
433 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
435 /* If unibyte string STRING contains 8-bit codes, we must
436 convert STRING to a multibyte string containing the same
437 character codes. */
438 Lisp_Object newstr;
439 ptrdiff_t bytes;
441 chars = SBYTES (string);
442 bytes = count_size_as_multibyte (SDATA (string), chars);
443 if (chars < bytes)
445 newstr = make_uninit_multibyte_string (chars, bytes);
446 memcpy (SDATA (newstr), SDATA (string), chars);
447 str_to_multibyte (SDATA (newstr), bytes, chars);
448 string = newstr;
451 else
452 chars = SBYTES (string);
454 if (EQ (printcharfun, Qt))
456 /* Output to echo area. */
457 ptrdiff_t nbytes = SBYTES (string);
459 /* Copy the string contents so that relocation of STRING by
460 GC does not cause trouble. */
461 USE_SAFE_ALLOCA;
462 char *buffer = SAFE_ALLOCA (nbytes);
463 memcpy (buffer, SDATA (string), nbytes);
465 strout (buffer, chars, nbytes, printcharfun);
467 SAFE_FREE ();
469 else
470 /* No need to copy, since output to print_buffer can't GC. */
471 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
473 else
475 /* Otherwise, string may be relocated by printing one char.
476 So re-fetch the string address for each character. */
477 ptrdiff_t i;
478 ptrdiff_t size = SCHARS (string);
479 ptrdiff_t size_byte = SBYTES (string);
480 if (size == size_byte)
481 for (i = 0; i < size; i++)
482 printchar (SREF (string, i), printcharfun);
483 else
484 for (i = 0; i < size_byte; )
486 /* Here, we must convert each multi-byte form to the
487 corresponding character code before handing it to PRINTCHAR. */
488 int len;
489 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
490 printchar (ch, printcharfun);
491 i += len;
496 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
497 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
498 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
499 (Lisp_Object character, Lisp_Object printcharfun)
501 if (NILP (printcharfun))
502 printcharfun = Vstandard_output;
503 CHECK_NUMBER (character);
504 PRINTPREPARE;
505 printchar (XINT (character), printcharfun);
506 PRINTFINISH;
507 return character;
510 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
511 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
512 Do not use this on the contents of a Lisp string. */
514 static void
515 print_c_string (char const *string, Lisp_Object printcharfun)
517 ptrdiff_t len = strlen (string);
518 strout (string, len, len, printcharfun);
521 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
522 Do not use this on the contents of a Lisp string. */
524 static void
525 write_string (const char *data, Lisp_Object printcharfun)
527 PRINTPREPARE;
528 print_c_string (data, printcharfun);
529 PRINTFINISH;
533 void
534 temp_output_buffer_setup (const char *bufname)
536 ptrdiff_t count = SPECPDL_INDEX ();
537 register struct buffer *old = current_buffer;
538 register Lisp_Object buf;
540 record_unwind_current_buffer ();
542 Fset_buffer (Fget_buffer_create (build_string (bufname)));
544 Fkill_all_local_variables ();
545 delete_all_overlays (current_buffer);
546 bset_directory (current_buffer, BVAR (old, directory));
547 bset_read_only (current_buffer, Qnil);
548 bset_filename (current_buffer, Qnil);
549 bset_undo_list (current_buffer, Qt);
550 eassert (current_buffer->overlays_before == NULL);
551 eassert (current_buffer->overlays_after == NULL);
552 bset_enable_multibyte_characters
553 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
554 specbind (Qinhibit_read_only, Qt);
555 specbind (Qinhibit_modification_hooks, Qt);
556 Ferase_buffer ();
557 XSETBUFFER (buf, current_buffer);
559 run_hook (Qtemp_buffer_setup_hook);
561 unbind_to (count, Qnil);
563 specbind (Qstandard_output, buf);
566 static void print (Lisp_Object, Lisp_Object, bool);
567 static void print_preprocess (Lisp_Object);
568 static void print_preprocess_string (INTERVAL, Lisp_Object);
569 static void print_object (Lisp_Object, Lisp_Object, bool);
571 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
572 doc: /* Output a newline to stream PRINTCHARFUN.
573 If ENSURE is non-nil only output a newline if not already at the
574 beginning of a line. Value is non-nil if a newline is printed.
575 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
576 (Lisp_Object printcharfun, Lisp_Object ensure)
578 Lisp_Object val;
580 if (NILP (printcharfun))
581 printcharfun = Vstandard_output;
582 PRINTPREPARE;
584 if (NILP (ensure))
585 val = Qt;
586 /* Difficult to check if at line beginning so abort. */
587 else if (FUNCTIONP (printcharfun))
588 signal_error ("Unsupported function argument", printcharfun);
589 else if (noninteractive && !NILP (printcharfun))
590 val = printchar_stdout_last == 10 ? Qnil : Qt;
591 else
592 val = NILP (Fbolp ()) ? Qt : Qnil;
594 if (!NILP (val))
595 printchar ('\n', printcharfun);
596 PRINTFINISH;
597 return val;
600 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
601 doc: /* Output the printed representation of OBJECT, any Lisp object.
602 Quoting characters are printed when needed to make output that `read'
603 can handle, whenever this is possible. For complex objects, the behavior
604 is controlled by `print-level' and `print-length', which see.
606 OBJECT is any of the Lisp data types: a number, a string, a symbol,
607 a list, a buffer, a window, a frame, etc.
609 A printed representation of an object is text which describes that object.
611 Optional argument PRINTCHARFUN is the output stream, which can be one
612 of these:
614 - a buffer, in which case output is inserted into that buffer at point;
615 - a marker, in which case output is inserted at marker's position;
616 - a function, in which case that function is called once for each
617 character of OBJECT's printed representation;
618 - a symbol, in which case that symbol's function definition is called; or
619 - t, in which case the output is displayed in the echo area.
621 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
622 is used instead. */)
623 (Lisp_Object object, Lisp_Object printcharfun)
625 if (NILP (printcharfun))
626 printcharfun = Vstandard_output;
627 PRINTPREPARE;
628 print (object, printcharfun, 1);
629 PRINTFINISH;
630 return object;
633 /* A buffer which is used to hold output being built by prin1-to-string. */
634 Lisp_Object Vprin1_to_string_buffer;
636 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
637 doc: /* Return a string containing the printed representation of OBJECT.
638 OBJECT can be any Lisp object. This function outputs quoting characters
639 when necessary to make output that `read' can handle, whenever possible,
640 unless the optional second argument NOESCAPE is non-nil. For complex objects,
641 the behavior is controlled by `print-level' and `print-length', which see.
643 OBJECT is any of the Lisp data types: a number, a string, a symbol,
644 a list, a buffer, a window, a frame, etc.
646 A printed representation of an object is text which describes that object. */)
647 (Lisp_Object object, Lisp_Object noescape)
649 ptrdiff_t count = SPECPDL_INDEX ();
651 specbind (Qinhibit_modification_hooks, Qt);
653 /* Save and restore this: we are altering a buffer
654 but we don't want to deactivate the mark just for that.
655 No need for specbind, since errors deactivate the mark. */
656 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
658 Lisp_Object printcharfun = Vprin1_to_string_buffer;
659 PRINTPREPARE;
660 print (object, printcharfun, NILP (noescape));
661 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
662 PRINTFINISH;
664 struct buffer *previous = current_buffer;
665 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
666 object = Fbuffer_string ();
667 if (SBYTES (object) == SCHARS (object))
668 STRING_SET_UNIBYTE (object);
670 /* Note that this won't make prepare_to_modify_buffer call
671 ask-user-about-supersession-threat because this buffer
672 does not visit a file. */
673 Ferase_buffer ();
674 set_buffer_internal (previous);
676 Vdeactivate_mark = save_deactivate_mark;
678 return unbind_to (count, object);
681 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
682 doc: /* Output the printed representation of OBJECT, any Lisp object.
683 No quoting characters are used; no delimiters are printed around
684 the contents of strings.
686 OBJECT is any of the Lisp data types: a number, a string, a symbol,
687 a list, a buffer, a window, a frame, etc.
689 A printed representation of an object is text which describes that object.
691 Optional argument PRINTCHARFUN is the output stream, which can be one
692 of these:
694 - a buffer, in which case output is inserted into that buffer at point;
695 - a marker, in which case output is inserted at marker's position;
696 - a function, in which case that function is called once for each
697 character of OBJECT's printed representation;
698 - a symbol, in which case that symbol's function definition is called; or
699 - t, in which case the output is displayed in the echo area.
701 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
702 is used instead. */)
703 (Lisp_Object object, Lisp_Object printcharfun)
705 if (NILP (printcharfun))
706 printcharfun = Vstandard_output;
707 PRINTPREPARE;
708 print (object, printcharfun, 0);
709 PRINTFINISH;
710 return object;
713 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
714 doc: /* Output the printed representation of OBJECT, with newlines around it.
715 Quoting characters are printed when needed to make output that `read'
716 can handle, whenever this is possible. For complex objects, the behavior
717 is controlled by `print-level' and `print-length', which see.
719 OBJECT is any of the Lisp data types: a number, a string, a symbol,
720 a list, a buffer, a window, a frame, etc.
722 A printed representation of an object is text which describes that object.
724 Optional argument PRINTCHARFUN is the output stream, which can be one
725 of these:
727 - a buffer, in which case output is inserted into that buffer at point;
728 - a marker, in which case output is inserted at marker's position;
729 - a function, in which case that function is called once for each
730 character of OBJECT's printed representation;
731 - a symbol, in which case that symbol's function definition is called; or
732 - t, in which case the output is displayed in the echo area.
734 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
735 is used instead. */)
736 (Lisp_Object object, Lisp_Object printcharfun)
738 if (NILP (printcharfun))
739 printcharfun = Vstandard_output;
740 PRINTPREPARE;
741 printchar ('\n', printcharfun);
742 print (object, printcharfun, 1);
743 printchar ('\n', printcharfun);
744 PRINTFINISH;
745 return object;
748 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
749 doc: /* Write CHARACTER to stderr.
750 You can call print while debugging emacs, and pass it this function
751 to make it write to the debugging output. */)
752 (Lisp_Object character)
754 CHECK_NUMBER (character);
755 printchar_to_stream (XINT (character), stderr);
756 return character;
759 /* This function is never called. Its purpose is to prevent
760 print_output_debug_flag from being optimized away. */
762 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
763 void
764 debug_output_compilation_hack (bool x)
766 print_output_debug_flag = x;
769 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
770 1, 2,
771 "FDebug output file: \nP",
772 doc: /* Redirect debugging output (stderr stream) to file FILE.
773 If FILE is nil, reset target to the initial stderr stream.
774 Optional arg APPEND non-nil (interactively, with prefix arg) means
775 append to existing target file. */)
776 (Lisp_Object file, Lisp_Object append)
778 /* If equal to STDERR_FILENO, stderr has not been duplicated and is OK as-is.
779 Otherwise, this is a close-on-exec duplicate of the original stderr. */
780 static int stderr_dup = STDERR_FILENO;
781 int fd = stderr_dup;
783 if (! NILP (file))
785 file = Fexpand_file_name (file, Qnil);
787 if (stderr_dup == STDERR_FILENO)
789 int n = fcntl (STDERR_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
790 if (n < 0)
791 report_file_error ("dup", file);
792 stderr_dup = n;
795 fd = emacs_open (SSDATA (ENCODE_FILE (file)),
796 (O_WRONLY | O_CREAT
797 | (! NILP (append) ? O_APPEND : O_TRUNC)),
798 0666);
799 if (fd < 0)
800 report_file_error ("Cannot open debugging output stream", file);
803 fflush (stderr);
804 if (dup2 (fd, STDERR_FILENO) < 0)
805 report_file_error ("dup2", file);
806 if (fd != stderr_dup)
807 emacs_close (fd);
808 return Qnil;
812 /* This is the interface for debugging printing. */
814 void
815 debug_print (Lisp_Object arg)
817 Fprin1 (arg, Qexternal_debugging_output);
818 fprintf (stderr, "\r\n");
821 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
822 void
823 safe_debug_print (Lisp_Object arg)
825 int valid = valid_lisp_object_p (arg);
827 if (valid > 0)
828 debug_print (arg);
829 else
831 EMACS_UINT n = XLI (arg);
832 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
833 !valid ? "INVALID" : "SOME",
839 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
840 1, 1, 0,
841 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
842 See Info anchor `(elisp)Definition of signal' for some details on how this
843 error message is constructed. */)
844 (Lisp_Object obj)
846 struct buffer *old = current_buffer;
847 Lisp_Object value;
849 /* If OBJ is (error STRING), just return STRING.
850 That is not only faster, it also avoids the need to allocate
851 space here when the error is due to memory full. */
852 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
853 && CONSP (XCDR (obj))
854 && STRINGP (XCAR (XCDR (obj)))
855 && NILP (XCDR (XCDR (obj))))
856 return XCAR (XCDR (obj));
858 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
860 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
861 value = Fbuffer_string ();
863 Ferase_buffer ();
864 set_buffer_internal (old);
866 return value;
869 /* Print an error message for the error DATA onto Lisp output stream
870 STREAM (suitable for the print functions).
871 CONTEXT is a C string describing the context of the error.
872 CALLER is the Lisp function inside which the error was signaled. */
874 void
875 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
876 Lisp_Object caller)
878 Lisp_Object errname, errmsg, file_error, tail;
880 if (context != 0)
881 write_string (context, stream);
883 /* If we know from where the error was signaled, show it in
884 *Messages*. */
885 if (!NILP (caller) && SYMBOLP (caller))
887 Lisp_Object cname = SYMBOL_NAME (caller);
888 ptrdiff_t cnamelen = SBYTES (cname);
889 USE_SAFE_ALLOCA;
890 char *name = SAFE_ALLOCA (cnamelen);
891 memcpy (name, SDATA (cname), cnamelen);
892 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
893 message_dolog (": ", 2, 0, 0);
894 SAFE_FREE ();
897 errname = Fcar (data);
899 if (EQ (errname, Qerror))
901 data = Fcdr (data);
902 if (!CONSP (data))
903 data = Qnil;
904 errmsg = Fcar (data);
905 file_error = Qnil;
907 else
909 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
910 errmsg = Fsubstitute_command_keys (Fget (errname, Qerror_message));
911 file_error = Fmemq (Qfile_error, error_conditions);
914 /* Print an error message including the data items. */
916 tail = Fcdr_safe (data);
918 /* For file-error, make error message by concatenating
919 all the data items. They are all strings. */
920 if (!NILP (file_error) && CONSP (tail))
921 errmsg = XCAR (tail), tail = XCDR (tail);
924 const char *sep = ": ";
926 if (!STRINGP (errmsg))
927 write_string ("peculiar error", stream);
928 else if (SCHARS (errmsg))
929 Fprinc (errmsg, stream);
930 else
931 sep = NULL;
933 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
935 Lisp_Object obj;
937 if (sep)
938 write_string (sep, stream);
939 obj = XCAR (tail);
940 if (!NILP (file_error)
941 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
942 Fprinc (obj, stream);
943 else
944 Fprin1 (obj, stream);
952 * The buffer should be at least as large as the max string size of the
953 * largest float, printed in the biggest notation. This is undoubtedly
954 * 20d float_output_format, with the negative of the C-constant "HUGE"
955 * from <math.h>.
957 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
959 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
960 * case of -1e307 in 20d float_output_format. What is one to do (short of
961 * re-writing _doprnt to be more sane)?
962 * -wsr
963 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
967 float_to_string (char *buf, double data)
969 char *cp;
970 int width;
971 int len;
973 /* Check for plus infinity in a way that won't lose
974 if there is no plus infinity. */
975 if (data == data / 2 && data > 1.0)
977 static char const infinity_string[] = "1.0e+INF";
978 strcpy (buf, infinity_string);
979 return sizeof infinity_string - 1;
981 /* Likewise for minus infinity. */
982 if (data == data / 2 && data < -1.0)
984 static char const minus_infinity_string[] = "-1.0e+INF";
985 strcpy (buf, minus_infinity_string);
986 return sizeof minus_infinity_string - 1;
988 /* Check for NaN in a way that won't fail if there are no NaNs. */
989 if (! (data * 0.0 >= 0.0))
991 /* Prepend "-" if the NaN's sign bit is negative.
992 The sign bit of a double is the bit that is 1 in -0.0. */
993 static char const NaN_string[] = "0.0e+NaN";
994 int i;
995 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
996 bool negative = 0;
997 u_data.d = data;
998 u_minus_zero.d = - 0.0;
999 for (i = 0; i < sizeof (double); i++)
1000 if (u_data.c[i] & u_minus_zero.c[i])
1002 *buf = '-';
1003 negative = 1;
1004 break;
1007 strcpy (buf + negative, NaN_string);
1008 return negative + sizeof NaN_string - 1;
1011 if (NILP (Vfloat_output_format)
1012 || !STRINGP (Vfloat_output_format))
1013 lose:
1015 /* Generate the fewest number of digits that represent the
1016 floating point value without losing information. */
1017 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1018 /* The decimal point must be printed, or the byte compiler can
1019 get confused (Bug#8033). */
1020 width = 1;
1022 else /* oink oink */
1024 /* Check that the spec we have is fully valid.
1025 This means not only valid for printf,
1026 but meant for floats, and reasonable. */
1027 cp = SSDATA (Vfloat_output_format);
1029 if (cp[0] != '%')
1030 goto lose;
1031 if (cp[1] != '.')
1032 goto lose;
1034 cp += 2;
1036 /* Check the width specification. */
1037 width = -1;
1038 if ('0' <= *cp && *cp <= '9')
1040 width = 0;
1043 width = (width * 10) + (*cp++ - '0');
1044 if (DBL_DIG < width)
1045 goto lose;
1047 while (*cp >= '0' && *cp <= '9');
1049 /* A precision of zero is valid only for %f. */
1050 if (width == 0 && *cp != 'f')
1051 goto lose;
1054 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1055 goto lose;
1057 if (cp[1] != 0)
1058 goto lose;
1060 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1063 /* Make sure there is a decimal point with digit after, or an
1064 exponent, so that the value is readable as a float. But don't do
1065 this with "%.0f"; it's valid for that not to produce a decimal
1066 point. Note that width can be 0 only for %.0f. */
1067 if (width != 0)
1069 for (cp = buf; *cp; cp++)
1070 if ((*cp < '0' || *cp > '9') && *cp != '-')
1071 break;
1073 if (*cp == '.' && cp[1] == 0)
1075 cp[1] = '0';
1076 cp[2] = 0;
1077 len++;
1079 else if (*cp == 0)
1081 *cp++ = '.';
1082 *cp++ = '0';
1083 *cp++ = 0;
1084 len += 2;
1088 return len;
1092 static void
1093 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1095 new_backquote_output = 0;
1097 /* Reset print_number_index and Vprint_number_table only when
1098 the variable Vprint_continuous_numbering is nil. Otherwise,
1099 the values of these variables will be kept between several
1100 print functions. */
1101 if (NILP (Vprint_continuous_numbering)
1102 || NILP (Vprint_number_table))
1104 print_number_index = 0;
1105 Vprint_number_table = Qnil;
1108 /* Construct Vprint_number_table for print-gensym and print-circle. */
1109 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1111 /* Construct Vprint_number_table.
1112 This increments print_number_index for the objects added. */
1113 print_depth = 0;
1114 print_preprocess (obj);
1116 if (HASH_TABLE_P (Vprint_number_table))
1117 { /* Remove unnecessary objects, which appear only once in OBJ;
1118 that is, whose status is Qt. */
1119 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1120 ptrdiff_t i;
1122 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1123 if (!NILP (HASH_HASH (h, i))
1124 && EQ (HASH_VALUE (h, i), Qt))
1125 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1129 print_depth = 0;
1130 print_object (obj, printcharfun, escapeflag);
1133 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1134 (STRINGP (obj) || CONSP (obj) \
1135 || (VECTORLIKEP (obj) \
1136 && (VECTORP (obj) || COMPILEDP (obj) \
1137 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1138 || HASH_TABLE_P (obj) || FONTP (obj) \
1139 || RECORDP (obj))) \
1140 || (! NILP (Vprint_gensym) \
1141 && SYMBOLP (obj) \
1142 && !SYMBOL_INTERNED_P (obj)))
1144 /* Construct Vprint_number_table according to the structure of OBJ.
1145 OBJ itself and all its elements will be added to Vprint_number_table
1146 recursively if it is a list, vector, compiled function, char-table,
1147 string (its text properties will be traced), or a symbol that has
1148 no obarray (this is for the print-gensym feature).
1149 The status fields of Vprint_number_table mean whether each object appears
1150 more than once in OBJ: Qnil at the first time, and Qt after that. */
1151 static void
1152 print_preprocess (Lisp_Object obj)
1154 int i;
1155 ptrdiff_t size;
1156 int loop_count = 0;
1157 Lisp_Object halftail;
1159 /* Avoid infinite recursion for circular nested structure
1160 in the case where Vprint_circle is nil. */
1161 if (NILP (Vprint_circle))
1163 /* Give up if we go so deep that print_object will get an error. */
1164 /* See similar code in print_object. */
1165 if (print_depth >= PRINT_CIRCLE)
1166 error ("Apparently circular structure being printed");
1168 for (i = 0; i < print_depth; i++)
1169 if (EQ (obj, being_printed[i]))
1170 return;
1171 being_printed[print_depth] = obj;
1174 print_depth++;
1175 halftail = obj;
1177 loop:
1178 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1180 if (!HASH_TABLE_P (Vprint_number_table))
1181 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1183 /* In case print-circle is nil and print-gensym is t,
1184 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1185 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1187 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1188 if (!NILP (num)
1189 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1190 always print the gensym with a number. This is a special for
1191 the lisp function byte-compile-output-docform. */
1192 || (!NILP (Vprint_continuous_numbering)
1193 && SYMBOLP (obj)
1194 && !SYMBOL_INTERNED_P (obj)))
1195 { /* OBJ appears more than once. Let's remember that. */
1196 if (!INTEGERP (num))
1198 print_number_index++;
1199 /* Negative number indicates it hasn't been printed yet. */
1200 Fputhash (obj, make_number (- print_number_index),
1201 Vprint_number_table);
1203 print_depth--;
1204 return;
1206 else
1207 /* OBJ is not yet recorded. Let's add to the table. */
1208 Fputhash (obj, Qt, Vprint_number_table);
1211 switch (XTYPE (obj))
1213 case Lisp_String:
1214 /* A string may have text properties, which can be circular. */
1215 traverse_intervals_noorder (string_intervals (obj),
1216 print_preprocess_string, Qnil);
1217 break;
1219 case Lisp_Cons:
1220 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1221 just as in print_object. */
1222 if (loop_count && EQ (obj, halftail))
1223 break;
1224 print_preprocess (XCAR (obj));
1225 obj = XCDR (obj);
1226 loop_count++;
1227 if (!(loop_count & 1))
1228 halftail = XCDR (halftail);
1229 goto loop;
1231 case Lisp_Vectorlike:
1232 size = ASIZE (obj);
1233 if (size & PSEUDOVECTOR_FLAG)
1234 size &= PSEUDOVECTOR_SIZE_MASK;
1235 for (i = (SUB_CHAR_TABLE_P (obj)
1236 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1237 print_preprocess (AREF (obj, i));
1238 if (HASH_TABLE_P (obj))
1239 { /* For hash tables, the key_and_value slot is past
1240 `size' because it needs to be marked specially in case
1241 the table is weak. */
1242 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1243 print_preprocess (h->key_and_value);
1245 break;
1247 default:
1248 break;
1251 print_depth--;
1254 DEFUN ("print--preprocess", Fprint_preprocess, Sprint_preprocess, 1, 1, 0,
1255 doc: /* Extract sharing info from OBJECT needed to print it.
1256 Fills `print-number-table'. */)
1257 (Lisp_Object object)
1259 print_number_index = 0;
1260 print_preprocess (object);
1261 return Qnil;
1264 static void
1265 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1267 print_preprocess (interval->plist);
1270 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1272 #define PRINT_STRING_NON_CHARSET_FOUND 1
1273 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1275 /* Bitwise or of the above macros. */
1276 static int print_check_string_result;
1278 static void
1279 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1281 Lisp_Object val;
1283 if (NILP (interval->plist)
1284 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1285 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1286 return;
1287 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1288 val = XCDR (XCDR (val)));
1289 if (! CONSP (val))
1291 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1292 return;
1294 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1296 if (! EQ (val, interval->plist)
1297 || CONSP (XCDR (XCDR (val))))
1298 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1300 if (NILP (Vprint_charset_text_property)
1301 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1303 int i, c;
1304 ptrdiff_t charpos = interval->position;
1305 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1306 Lisp_Object charset;
1308 charset = XCAR (XCDR (val));
1309 for (i = 0; i < LENGTH (interval); i++)
1311 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1312 if (! ASCII_CHAR_P (c)
1313 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1315 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1316 break;
1322 /* The value is (charset . nil). */
1323 static Lisp_Object print_prune_charset_plist;
1325 static Lisp_Object
1326 print_prune_string_charset (Lisp_Object string)
1328 print_check_string_result = 0;
1329 traverse_intervals (string_intervals (string), 0,
1330 print_check_string_charset_prop, string);
1331 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1333 string = Fcopy_sequence (string);
1334 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1336 if (NILP (print_prune_charset_plist))
1337 print_prune_charset_plist = list1 (Qcharset);
1338 Fremove_text_properties (make_number (0),
1339 make_number (SCHARS (string)),
1340 print_prune_charset_plist, string);
1342 else
1343 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1344 Qnil, string);
1346 return string;
1349 static void
1350 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1352 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1353 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1354 40))];
1356 maybe_quit ();
1358 /* Detect circularities and truncate them. */
1359 if (NILP (Vprint_circle))
1361 /* Simple but incomplete way. */
1362 int i;
1364 /* See similar code in print_preprocess. */
1365 if (print_depth >= PRINT_CIRCLE)
1366 error ("Apparently circular structure being printed");
1368 for (i = 0; i < print_depth; i++)
1369 if (EQ (obj, being_printed[i]))
1371 int len = sprintf (buf, "#%d", i);
1372 strout (buf, len, len, printcharfun);
1373 return;
1375 being_printed[print_depth] = obj;
1377 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1379 /* With the print-circle feature. */
1380 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1381 if (INTEGERP (num))
1383 EMACS_INT n = XINT (num);
1384 if (n < 0)
1385 { /* Add a prefix #n= if OBJ has not yet been printed;
1386 that is, its status field is nil. */
1387 int len = sprintf (buf, "#%"pI"d=", -n);
1388 strout (buf, len, len, printcharfun);
1389 /* OBJ is going to be printed. Remember that fact. */
1390 Fputhash (obj, make_number (- n), Vprint_number_table);
1392 else
1394 /* Just print #n# if OBJ has already been printed. */
1395 int len = sprintf (buf, "#%"pI"d#", n);
1396 strout (buf, len, len, printcharfun);
1397 return;
1402 print_depth++;
1404 switch (XTYPE (obj))
1406 case_Lisp_Int:
1408 int len = sprintf (buf, "%"pI"d", XINT (obj));
1409 strout (buf, len, len, printcharfun);
1411 break;
1413 case Lisp_Float:
1415 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1416 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1417 strout (pigbuf, len, len, printcharfun);
1419 break;
1421 case Lisp_String:
1422 if (!escapeflag)
1423 print_string (obj, printcharfun);
1424 else
1426 ptrdiff_t i, i_byte;
1427 ptrdiff_t size_byte;
1428 /* True means we must ensure that the next character we output
1429 cannot be taken as part of a hex character escape. */
1430 bool need_nonhex = false;
1431 bool multibyte = STRING_MULTIBYTE (obj);
1433 if (! EQ (Vprint_charset_text_property, Qt))
1434 obj = print_prune_string_charset (obj);
1436 if (string_intervals (obj))
1437 print_c_string ("#(", printcharfun);
1439 printchar ('\"', printcharfun);
1440 size_byte = SBYTES (obj);
1442 for (i = 0, i_byte = 0; i_byte < size_byte;)
1444 /* Here, we must convert each multi-byte form to the
1445 corresponding character code before handing it to printchar. */
1446 int c;
1448 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1450 maybe_quit ();
1452 if (multibyte
1453 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1454 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1455 && print_escape_nonascii))
1457 /* When printing a raw 8-bit byte in a multibyte buffer, or
1458 (when requested) a non-ASCII character in a unibyte buffer,
1459 print single-byte non-ASCII string chars
1460 using octal escapes. */
1461 char outbuf[5];
1462 int len = sprintf (outbuf, "\\%03o", c + 0u);
1463 strout (outbuf, len, len, printcharfun);
1464 need_nonhex = false;
1466 else if (multibyte
1467 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1469 /* When requested, print multibyte chars using hex escapes. */
1470 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1471 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1472 strout (outbuf, len, len, printcharfun);
1473 need_nonhex = true;
1475 else
1477 /* If we just had a hex escape, and this character
1478 could be taken as part of it,
1479 output `\ ' to prevent that. */
1480 if (need_nonhex && c_isxdigit (c))
1481 print_c_string ("\\ ", printcharfun);
1483 if (c == '\n' && print_escape_newlines
1484 ? (c = 'n', true)
1485 : c == '\f' && print_escape_newlines
1486 ? (c = 'f', true)
1487 : c == '\"' || c == '\\')
1488 printchar ('\\', printcharfun);
1490 printchar (c, printcharfun);
1491 need_nonhex = false;
1494 printchar ('\"', printcharfun);
1496 if (string_intervals (obj))
1498 traverse_intervals (string_intervals (obj),
1499 0, print_interval, printcharfun);
1500 printchar (')', printcharfun);
1503 break;
1505 case Lisp_Symbol:
1507 bool confusing;
1508 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1509 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1510 int c;
1511 ptrdiff_t i, i_byte;
1512 ptrdiff_t size_byte;
1513 Lisp_Object name;
1515 name = SYMBOL_NAME (obj);
1517 if (p != end && (*p == '-' || *p == '+')) p++;
1518 if (p == end)
1519 confusing = 0;
1520 /* If symbol name begins with a digit, and ends with a digit,
1521 and contains nothing but digits and `e', it could be treated
1522 as a number. So set CONFUSING.
1524 Symbols that contain periods could also be taken as numbers,
1525 but periods are always escaped, so we don't have to worry
1526 about them here. */
1527 else if (*p >= '0' && *p <= '9'
1528 && end[-1] >= '0' && end[-1] <= '9')
1530 while (p != end && ((*p >= '0' && *p <= '9')
1531 /* Needed for \2e10. */
1532 || *p == 'e' || *p == 'E'))
1533 p++;
1534 confusing = (end == p);
1536 else
1537 confusing = 0;
1539 size_byte = SBYTES (name);
1541 if (! NILP (Vprint_gensym)
1542 && !SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (obj))
1543 print_c_string ("#:", printcharfun);
1544 else if (size_byte == 0)
1546 print_c_string ("##", printcharfun);
1547 break;
1550 for (i = 0, i_byte = 0; i_byte < size_byte;)
1552 /* Here, we must convert each multi-byte form to the
1553 corresponding character code before handing it to PRINTCHAR. */
1554 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1555 maybe_quit ();
1557 if (escapeflag)
1559 if (c == '\"' || c == '\\' || c == '\''
1560 || c == ';' || c == '#' || c == '(' || c == ')'
1561 || c == ',' || c == '.' || c == '`'
1562 || c == '[' || c == ']' || c == '?' || c <= 040
1563 || confusing)
1565 printchar ('\\', printcharfun);
1566 confusing = false;
1569 printchar (c, printcharfun);
1572 break;
1574 case Lisp_Cons:
1575 /* If deeper than spec'd depth, print placeholder. */
1576 if (INTEGERP (Vprint_level)
1577 && print_depth > XINT (Vprint_level))
1578 print_c_string ("...", printcharfun);
1579 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1580 && EQ (XCAR (obj), Qquote))
1582 printchar ('\'', printcharfun);
1583 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1585 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1586 && EQ (XCAR (obj), Qfunction))
1588 print_c_string ("#'", printcharfun);
1589 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1591 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1592 && EQ (XCAR (obj), Qbackquote))
1594 printchar ('`', printcharfun);
1595 new_backquote_output++;
1596 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1597 new_backquote_output--;
1599 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1600 && new_backquote_output
1601 && (EQ (XCAR (obj), Qcomma)
1602 || EQ (XCAR (obj), Qcomma_at)
1603 || EQ (XCAR (obj), Qcomma_dot)))
1605 print_object (XCAR (obj), printcharfun, false);
1606 new_backquote_output--;
1607 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1608 new_backquote_output++;
1610 else
1612 printchar ('(', printcharfun);
1614 Lisp_Object halftail = obj;
1616 /* Negative values of print-length are invalid in CL.
1617 Treat them like nil, as CMUCL does. */
1618 printmax_t print_length = (NATNUMP (Vprint_length)
1619 ? XFASTINT (Vprint_length)
1620 : TYPE_MAXIMUM (printmax_t));
1622 printmax_t i = 0;
1623 while (CONSP (obj))
1625 /* Detect circular list. */
1626 if (NILP (Vprint_circle))
1628 /* Simple but incomplete way. */
1629 if (i != 0 && EQ (obj, halftail))
1631 int len = sprintf (buf, " . #%"pMd, i / 2);
1632 strout (buf, len, len, printcharfun);
1633 goto end_of_list;
1636 else
1638 /* With the print-circle feature. */
1639 if (i != 0)
1641 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1642 if (INTEGERP (num))
1644 print_c_string (" . ", printcharfun);
1645 print_object (obj, printcharfun, escapeflag);
1646 goto end_of_list;
1651 if (i)
1652 printchar (' ', printcharfun);
1654 if (print_length <= i)
1656 print_c_string ("...", printcharfun);
1657 goto end_of_list;
1660 i++;
1661 print_object (XCAR (obj), printcharfun, escapeflag);
1663 obj = XCDR (obj);
1664 if (!(i & 1))
1665 halftail = XCDR (halftail);
1668 /* OBJ non-nil here means it's the end of a dotted list. */
1669 if (!NILP (obj))
1671 print_c_string (" . ", printcharfun);
1672 print_object (obj, printcharfun, escapeflag);
1675 end_of_list:
1676 printchar (')', printcharfun);
1678 break;
1680 case Lisp_Vectorlike:
1681 switch (PSEUDOVECTOR_TYPE (XVECTOR (obj))) {
1682 case PVEC_PROCESS:
1684 if (escapeflag)
1686 print_c_string ("#<process ", printcharfun);
1687 print_string (XPROCESS (obj)->name, printcharfun);
1688 printchar ('>', printcharfun);
1690 else
1691 print_string (XPROCESS (obj)->name, printcharfun);
1693 break;
1695 case PVEC_BOOL_VECTOR:
1697 ptrdiff_t i;
1698 unsigned char c;
1699 EMACS_INT size = bool_vector_size (obj);
1700 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1701 ptrdiff_t real_size_in_chars = size_in_chars;
1703 int len = sprintf (buf, "#&%"pI"d\"", size);
1704 strout (buf, len, len, printcharfun);
1706 /* Don't print more characters than the specified maximum.
1707 Negative values of print-length are invalid. Treat them
1708 like a print-length of nil. */
1709 if (NATNUMP (Vprint_length)
1710 && XFASTINT (Vprint_length) < size_in_chars)
1711 size_in_chars = XFASTINT (Vprint_length);
1713 for (i = 0; i < size_in_chars; i++)
1715 maybe_quit ();
1716 c = bool_vector_uchar_data (obj)[i];
1717 if (c == '\n' && print_escape_newlines)
1718 print_c_string ("\\n", printcharfun);
1719 else if (c == '\f' && print_escape_newlines)
1720 print_c_string ("\\f", printcharfun);
1721 else if (c > '\177')
1723 /* Use octal escapes to avoid encoding issues. */
1724 len = sprintf (buf, "\\%o", c);
1725 strout (buf, len, len, printcharfun);
1727 else
1729 if (c == '\"' || c == '\\')
1730 printchar ('\\', printcharfun);
1731 printchar (c, printcharfun);
1735 if (size_in_chars < real_size_in_chars)
1736 print_c_string (" ...", printcharfun);
1737 printchar ('\"', printcharfun);
1739 break;
1741 case PVEC_SUBR:
1743 print_c_string ("#<subr ", printcharfun);
1744 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1745 printchar ('>', printcharfun);
1747 break;
1749 case PVEC_XWIDGET: case PVEC_XWIDGET_VIEW:
1751 print_c_string ("#<xwidget ", printcharfun);
1752 printchar ('>', printcharfun);
1754 break;
1756 case PVEC_WINDOW:
1758 int len = sprintf (buf, "#<window %"pI"d",
1759 XWINDOW (obj)->sequence_number);
1760 strout (buf, len, len, printcharfun);
1761 if (BUFFERP (XWINDOW (obj)->contents))
1763 print_c_string (" on ", printcharfun);
1764 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1765 printcharfun);
1767 printchar ('>', printcharfun);
1769 break;
1771 case PVEC_TERMINAL:
1773 struct terminal *t = XTERMINAL (obj);
1774 int len = sprintf (buf, "#<terminal %d", t->id);
1775 strout (buf, len, len, printcharfun);
1776 if (t->name)
1778 print_c_string (" on ", printcharfun);
1779 print_c_string (t->name, printcharfun);
1781 printchar ('>', printcharfun);
1783 break;
1785 case PVEC_HASH_TABLE:
1787 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1788 ptrdiff_t i;
1789 ptrdiff_t real_size, size;
1790 int len;
1791 /* Implement a readable output, e.g.:
1792 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1793 /* Always print the size. */
1794 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1795 strout (buf, len, len, printcharfun);
1797 if (!NILP (h->test.name))
1799 print_c_string (" test ", printcharfun);
1800 print_object (h->test.name, printcharfun, escapeflag);
1803 if (!NILP (h->weak))
1805 print_c_string (" weakness ", printcharfun);
1806 print_object (h->weak, printcharfun, escapeflag);
1809 print_c_string (" rehash-size ", printcharfun);
1810 print_object (Fhash_table_rehash_size (obj),
1811 printcharfun, escapeflag);
1813 print_c_string (" rehash-threshold ", printcharfun);
1814 print_object (Fhash_table_rehash_threshold (obj),
1815 printcharfun, escapeflag);
1817 if (h->pure)
1819 print_c_string (" purecopy ", printcharfun);
1820 print_object (h->pure ? Qt : Qnil, printcharfun, escapeflag);
1823 print_c_string (" data ", printcharfun);
1825 /* Print the data here as a plist. */
1826 real_size = HASH_TABLE_SIZE (h);
1827 size = real_size;
1829 /* Don't print more elements than the specified maximum. */
1830 if (NATNUMP (Vprint_length)
1831 && XFASTINT (Vprint_length) < size)
1832 size = XFASTINT (Vprint_length);
1834 printchar ('(', printcharfun);
1835 for (i = 0; i < size; i++)
1836 if (!NILP (HASH_HASH (h, i)))
1838 if (i) printchar (' ', printcharfun);
1839 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1840 printchar (' ', printcharfun);
1841 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1844 if (size < real_size)
1845 print_c_string (" ...", printcharfun);
1847 print_c_string ("))", printcharfun);
1849 break;
1851 case PVEC_BUFFER:
1853 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1854 print_c_string ("#<killed buffer>", printcharfun);
1855 else if (escapeflag)
1857 print_c_string ("#<buffer ", printcharfun);
1858 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1859 printchar ('>', printcharfun);
1861 else
1862 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1864 break;
1866 case PVEC_WINDOW_CONFIGURATION:
1867 print_c_string ("#<window-configuration>", printcharfun);
1868 break;
1870 case PVEC_FRAME: ;
1872 int len;
1873 void *ptr = XFRAME (obj);
1874 Lisp_Object frame_name = XFRAME (obj)->name;
1876 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1877 ? "#<frame "
1878 : "#<dead frame "),
1879 printcharfun);
1880 if (!STRINGP (frame_name))
1882 /* A frame could be too young and have no name yet;
1883 don't crash. */
1884 if (SYMBOLP (frame_name))
1885 frame_name = Fsymbol_name (frame_name);
1886 else /* can't happen: name should be either nil or string */
1887 frame_name = build_string ("*INVALID*FRAME*NAME*");
1889 print_string (frame_name, printcharfun);
1890 len = sprintf (buf, " %p>", ptr);
1891 strout (buf, len, len, printcharfun);
1893 break;
1895 case PVEC_FONT:
1897 int i;
1899 if (! FONT_OBJECT_P (obj))
1901 if (FONT_SPEC_P (obj))
1902 print_c_string ("#<font-spec", printcharfun);
1903 else
1904 print_c_string ("#<font-entity", printcharfun);
1905 for (i = 0; i < FONT_SPEC_MAX; i++)
1907 printchar (' ', printcharfun);
1908 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1909 print_object (AREF (obj, i), printcharfun, escapeflag);
1910 else
1911 print_object (font_style_symbolic (obj, i, 0),
1912 printcharfun, escapeflag);
1915 else
1917 print_c_string ("#<font-object ", printcharfun);
1918 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1919 escapeflag);
1921 printchar ('>', printcharfun);
1923 break;
1925 case PVEC_THREAD:
1927 print_c_string ("#<thread ", printcharfun);
1928 if (STRINGP (XTHREAD (obj)->name))
1929 print_string (XTHREAD (obj)->name, printcharfun);
1930 else
1932 int len = sprintf (buf, "%p", XTHREAD (obj));
1933 strout (buf, len, len, printcharfun);
1935 printchar ('>', printcharfun);
1937 break;
1939 case PVEC_MUTEX:
1941 print_c_string ("#<mutex ", printcharfun);
1942 if (STRINGP (XMUTEX (obj)->name))
1943 print_string (XMUTEX (obj)->name, printcharfun);
1944 else
1946 int len = sprintf (buf, "%p", XMUTEX (obj));
1947 strout (buf, len, len, printcharfun);
1949 printchar ('>', printcharfun);
1951 break;
1953 case PVEC_CONDVAR:
1955 print_c_string ("#<condvar ", printcharfun);
1956 if (STRINGP (XCONDVAR (obj)->name))
1957 print_string (XCONDVAR (obj)->name, printcharfun);
1958 else
1960 int len = sprintf (buf, "%p", XCONDVAR (obj));
1961 strout (buf, len, len, printcharfun);
1963 printchar ('>', printcharfun);
1965 break;
1967 case PVEC_RECORD:
1969 ptrdiff_t n, size = PVSIZE (obj);
1970 int i;
1972 /* Don't print more elements than the specified maximum. */
1973 if (NATNUMP (Vprint_length)
1974 && XFASTINT (Vprint_length) < size)
1975 n = XFASTINT (Vprint_length);
1976 else
1977 n = size;
1979 print_c_string ("#s(", printcharfun);
1980 for (i = 0; i < n; i ++)
1982 if (i) printchar (' ', printcharfun);
1983 print_object (AREF (obj, i), printcharfun, escapeflag);
1985 if (n < size)
1986 print_c_string (" ...", printcharfun);
1987 printchar (')', printcharfun);
1989 break;
1991 case PVEC_SUB_CHAR_TABLE:
1992 case PVEC_COMPILED:
1993 case PVEC_CHAR_TABLE:
1994 case PVEC_NORMAL_VECTOR: ;
1996 ptrdiff_t size = ASIZE (obj);
1997 if (COMPILEDP (obj))
1999 printchar ('#', printcharfun);
2000 size &= PSEUDOVECTOR_SIZE_MASK;
2002 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
2004 /* We print a char-table as if it were a vector,
2005 lumping the parent and default slots in with the
2006 character slots. But we add #^ as a prefix. */
2008 /* Make each lowest sub_char_table start a new line.
2009 Otherwise we'll make a line extremely long, which
2010 results in slow redisplay. */
2011 if (SUB_CHAR_TABLE_P (obj)
2012 && XSUB_CHAR_TABLE (obj)->depth == 3)
2013 printchar ('\n', printcharfun);
2014 print_c_string ("#^", printcharfun);
2015 if (SUB_CHAR_TABLE_P (obj))
2016 printchar ('^', printcharfun);
2017 size &= PSEUDOVECTOR_SIZE_MASK;
2019 if (size & PSEUDOVECTOR_FLAG)
2020 goto badtype;
2022 printchar ('[', printcharfun);
2024 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
2025 Lisp_Object tem;
2026 ptrdiff_t real_size = size;
2028 /* For a sub char-table, print heading non-Lisp data first. */
2029 if (SUB_CHAR_TABLE_P (obj))
2031 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
2032 XSUB_CHAR_TABLE (obj)->min_char);
2033 strout (buf, i, i, printcharfun);
2036 /* Don't print more elements than the specified maximum. */
2037 if (NATNUMP (Vprint_length)
2038 && XFASTINT (Vprint_length) < size)
2039 size = XFASTINT (Vprint_length);
2041 for (i = idx; i < size; i++)
2043 if (i) printchar (' ', printcharfun);
2044 tem = AREF (obj, i);
2045 print_object (tem, printcharfun, escapeflag);
2047 if (size < real_size)
2048 print_c_string (" ...", printcharfun);
2050 printchar (']', printcharfun);
2052 break;
2054 case PVEC_OTHER:
2055 case PVEC_FREE:
2056 emacs_abort ();
2058 break;
2060 case Lisp_Misc:
2061 switch (XMISCTYPE (obj))
2063 case Lisp_Misc_Marker:
2064 print_c_string ("#<marker ", printcharfun);
2065 /* Do you think this is necessary? */
2066 if (XMARKER (obj)->insertion_type != 0)
2067 print_c_string ("(moves after insertion) ", printcharfun);
2068 if (! XMARKER (obj)->buffer)
2069 print_c_string ("in no buffer", printcharfun);
2070 else
2072 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
2073 strout (buf, len, len, printcharfun);
2074 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2076 printchar ('>', printcharfun);
2077 break;
2079 case Lisp_Misc_Overlay:
2080 print_c_string ("#<overlay ", printcharfun);
2081 if (! XMARKER (OVERLAY_START (obj))->buffer)
2082 print_c_string ("in no buffer", printcharfun);
2083 else
2085 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2086 marker_position (OVERLAY_START (obj)),
2087 marker_position (OVERLAY_END (obj)));
2088 strout (buf, len, len, printcharfun);
2089 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2090 printcharfun);
2092 printchar ('>', printcharfun);
2093 break;
2095 #ifdef HAVE_MODULES
2096 case Lisp_Misc_User_Ptr:
2098 print_c_string ("#<user-ptr ", printcharfun);
2099 int i = sprintf (buf, "ptr=%p finalizer=%p",
2100 XUSER_PTR (obj)->p,
2101 XUSER_PTR (obj)->finalizer);
2102 strout (buf, i, i, printcharfun);
2103 printchar ('>', printcharfun);
2104 break;
2106 #endif
2108 case Lisp_Misc_Finalizer:
2109 print_c_string ("#<finalizer", printcharfun);
2110 if (NILP (XFINALIZER (obj)->function))
2111 print_c_string (" used", printcharfun);
2112 printchar ('>', printcharfun);
2113 break;
2115 /* Remaining cases shouldn't happen in normal usage, but let's
2116 print them anyway for the benefit of the debugger. */
2118 case Lisp_Misc_Free:
2119 print_c_string ("#<misc free cell>", printcharfun);
2120 break;
2122 case Lisp_Misc_Save_Value:
2124 int i;
2125 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2127 print_c_string ("#<save-value ", printcharfun);
2129 if (v->save_type == SAVE_TYPE_MEMORY)
2131 ptrdiff_t amount = v->data[1].integer;
2133 /* valid_lisp_object_p is reliable, so try to print up
2134 to 8 saved objects. This code is rarely used, so
2135 it's OK that valid_lisp_object_p is slow. */
2137 int limit = min (amount, 8);
2138 Lisp_Object *area = v->data[0].pointer;
2140 i = sprintf (buf, "with %"pD"d objects", amount);
2141 strout (buf, i, i, printcharfun);
2143 for (i = 0; i < limit; i++)
2145 Lisp_Object maybe = area[i];
2146 int valid = valid_lisp_object_p (maybe);
2148 printchar (' ', printcharfun);
2149 if (0 < valid)
2150 print_object (maybe, printcharfun, escapeflag);
2151 else
2152 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2153 printcharfun);
2155 if (i == limit && i < amount)
2156 print_c_string (" ...", printcharfun);
2158 else
2160 /* Print each slot according to its type. */
2161 int index;
2162 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2164 if (index)
2165 printchar (' ', printcharfun);
2167 switch (save_type (v, index))
2169 case SAVE_UNUSED:
2170 i = sprintf (buf, "<unused>");
2171 break;
2173 case SAVE_POINTER:
2174 i = sprintf (buf, "<pointer %p>",
2175 v->data[index].pointer);
2176 break;
2178 case SAVE_FUNCPOINTER:
2179 i = sprintf (buf, "<funcpointer %p>",
2180 ((void *) (intptr_t)
2181 v->data[index].funcpointer));
2182 break;
2184 case SAVE_INTEGER:
2185 i = sprintf (buf, "<integer %"pD"d>",
2186 v->data[index].integer);
2187 break;
2189 case SAVE_OBJECT:
2190 print_object (v->data[index].object, printcharfun,
2191 escapeflag);
2192 continue;
2194 default:
2195 emacs_abort ();
2198 strout (buf, i, i, printcharfun);
2201 printchar ('>', printcharfun);
2203 break;
2205 default:
2206 goto badtype;
2208 break;
2210 default:
2211 badtype:
2213 int len;
2214 /* We're in trouble if this happens!
2215 Probably should just emacs_abort (). */
2216 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2217 if (MISCP (obj))
2218 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2219 else if (VECTORLIKEP (obj))
2220 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2221 else
2222 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2223 strout (buf, len, len, printcharfun);
2224 print_c_string ((" Save your buffers immediately"
2225 " and please report this bug>"),
2226 printcharfun);
2230 print_depth--;
2234 /* Print a description of INTERVAL using PRINTCHARFUN.
2235 This is part of printing a string that has text properties. */
2237 static void
2238 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2240 if (NILP (interval->plist))
2241 return;
2242 printchar (' ', printcharfun);
2243 print_object (make_number (interval->position), printcharfun, 1);
2244 printchar (' ', printcharfun);
2245 print_object (make_number (interval->position + LENGTH (interval)),
2246 printcharfun, 1);
2247 printchar (' ', printcharfun);
2248 print_object (interval->plist, printcharfun, 1);
2251 /* Initialize debug_print stuff early to have it working from the very
2252 beginning. */
2254 void
2255 init_print_once (void)
2257 /* The subroutine object for external-debugging-output is kept here
2258 for the convenience of the debugger. */
2259 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2261 defsubr (&Sexternal_debugging_output);
2264 void
2265 syms_of_print (void)
2267 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2269 DEFVAR_LISP ("standard-output", Vstandard_output,
2270 doc: /* Output stream `print' uses by default for outputting a character.
2271 This may be any function of one argument.
2272 It may also be a buffer (output is inserted before point)
2273 or a marker (output is inserted and the marker is advanced)
2274 or the symbol t (output appears in the echo area). */);
2275 Vstandard_output = Qt;
2276 DEFSYM (Qstandard_output, "standard-output");
2278 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2279 doc: /* The format descriptor string used to print floats.
2280 This is a %-spec like those accepted by `printf' in C,
2281 but with some restrictions. It must start with the two characters `%.'.
2282 After that comes an integer precision specification,
2283 and then a letter which controls the format.
2284 The letters allowed are `e', `f' and `g'.
2285 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2286 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2287 Use `g' to choose the shorter of those two formats for the number at hand.
2288 The precision in any of these cases is the number of digits following
2289 the decimal point. With `f', a precision of 0 means to omit the
2290 decimal point. 0 is not allowed with `e' or `g'.
2292 A value of nil means to use the shortest notation
2293 that represents the number without losing information. */);
2294 Vfloat_output_format = Qnil;
2296 DEFVAR_LISP ("print-length", Vprint_length,
2297 doc: /* Maximum length of list to print before abbreviating.
2298 A value of nil means no limit. See also `eval-expression-print-length'. */);
2299 Vprint_length = Qnil;
2301 DEFVAR_LISP ("print-level", Vprint_level,
2302 doc: /* Maximum depth of list nesting to print before abbreviating.
2303 A value of nil means no limit. See also `eval-expression-print-level'. */);
2304 Vprint_level = Qnil;
2306 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2307 doc: /* Non-nil means print newlines in strings as `\\n'.
2308 Also print formfeeds as `\\f'. */);
2309 print_escape_newlines = 0;
2311 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2312 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2313 \(OOO is the octal representation of the character code.)
2314 Only single-byte characters are affected, and only in `prin1'.
2315 When the output goes in a multibyte buffer, this feature is
2316 enabled regardless of the value of the variable. */);
2317 print_escape_nonascii = 0;
2319 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2320 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2321 \(XXXX is the hex representation of the character code.)
2322 This affects only `prin1'. */);
2323 print_escape_multibyte = 0;
2325 DEFVAR_BOOL ("print-quoted", print_quoted,
2326 doc: /* Non-nil means print quoted forms with reader syntax.
2327 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2328 print_quoted = 0;
2330 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2331 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2332 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2333 When the uninterned symbol appears within a recursive data structure,
2334 and the symbol appears more than once, in addition use the #N# and #N=
2335 constructs as needed, so that multiple references to the same symbol are
2336 shared once again when the text is read back. */);
2337 Vprint_gensym = Qnil;
2339 DEFVAR_LISP ("print-circle", Vprint_circle,
2340 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2341 If nil, printing proceeds recursively and may lead to
2342 `max-lisp-eval-depth' being exceeded or an error may occur:
2343 \"Apparently circular structure being printed.\" Also see
2344 `print-length' and `print-level'.
2345 If non-nil, shared substructures anywhere in the structure are printed
2346 with `#N=' before the first occurrence (in the order of the print
2347 representation) and `#N#' in place of each subsequent occurrence,
2348 where N is a positive decimal integer. */);
2349 Vprint_circle = Qnil;
2351 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2352 doc: /* Non-nil means number continuously across print calls.
2353 This affects the numbers printed for #N= labels and #M# references.
2354 See also `print-circle', `print-gensym', and `print-number-table'.
2355 This variable should not be set with `setq'; bind it with a `let' instead. */);
2356 Vprint_continuous_numbering = Qnil;
2358 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2359 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2360 The Lisp printer uses this vector to detect Lisp objects referenced more
2361 than once.
2363 When you bind `print-continuous-numbering' to t, you should probably
2364 also bind `print-number-table' to nil. This ensures that the value of
2365 `print-number-table' can be garbage-collected once the printing is
2366 done. If all elements of `print-number-table' are nil, it means that
2367 the printing done so far has not found any shared structure or objects
2368 that need to be recorded in the table. */);
2369 Vprint_number_table = Qnil;
2371 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2372 doc: /* A flag to control printing of `charset' text property on printing a string.
2373 The value must be nil, t, or `default'.
2375 If the value is nil, don't print the text property `charset'.
2377 If the value is t, always print the text property `charset'.
2379 If the value is `default', print the text property `charset' only when
2380 the value is different from what is guessed in the current charset
2381 priorities. */);
2382 Vprint_charset_text_property = Qdefault;
2384 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2385 staticpro (&Vprin1_to_string_buffer);
2387 defsubr (&Sprin1);
2388 defsubr (&Sprin1_to_string);
2389 defsubr (&Serror_message_string);
2390 defsubr (&Sprinc);
2391 defsubr (&Sprint);
2392 defsubr (&Sterpri);
2393 defsubr (&Swrite_char);
2394 defsubr (&Sredirect_debugging_output);
2395 defsubr (&Sprint_preprocess);
2397 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2398 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2399 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2401 print_prune_charset_plist = Qnil;
2402 staticpro (&print_prune_charset_plist);