Use new function overflow_error in a few places
[emacs.git] / src / print.c
blobc0c90bc7e9a2586074925ca8cb4c56e3da074e48
1 /* Lisp object printing and output streams.
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2018 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 <https://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"
36 #include "dynlib.h"
38 #include <c-ctype.h>
39 #include <float.h>
40 #include <ftoastr.h>
41 #include <math.h>
43 #if IEEE_FLOATING_POINT
44 # include <ieee754.h>
45 #endif
47 #ifdef WINDOWSNT
48 # include <sys/socket.h> /* for F_DUPFD_CLOEXEC */
49 #endif
51 struct terminal;
53 /* Avoid actual stack overflow in print. */
54 static ptrdiff_t print_depth;
56 /* Level of nesting inside outputting backquote in new style. */
57 static ptrdiff_t new_backquote_output;
59 /* Detect most circularities to print finite output. */
60 #define PRINT_CIRCLE 200
61 static Lisp_Object being_printed[PRINT_CIRCLE];
63 /* Last char printed to stdout by printchar. */
64 static unsigned int printchar_stdout_last;
66 /* When printing into a buffer, first we put the text in this
67 block, then insert it all at once. */
68 static char *print_buffer;
70 /* Size allocated in print_buffer. */
71 static ptrdiff_t print_buffer_size;
72 /* Chars stored in print_buffer. */
73 static ptrdiff_t print_buffer_pos;
74 /* Bytes stored in print_buffer. */
75 static ptrdiff_t print_buffer_pos_byte;
77 /* Vprint_number_table is a table, that keeps objects that are going to
78 be printed, to allow use of #n= and #n# to express sharing.
79 For any given object, the table can give the following values:
80 t the object will be printed only once.
81 -N the object will be printed several times and will take number N.
82 N the object has been printed so we can refer to it as #N#.
83 print_number_index holds the largest N already used.
84 N has to be striclty larger than 0 since we need to distinguish -N. */
85 static ptrdiff_t print_number_index;
86 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
88 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
89 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
92 /* Low level output routines for characters and strings. */
94 /* Lisp functions to do output using a stream
95 must have the stream in a variable called printcharfun
96 and must start with PRINTPREPARE, end with PRINTFINISH.
97 Use printchar to output one character,
98 or call strout to output a block of characters. */
100 #define PRINTPREPARE \
101 struct buffer *old = current_buffer; \
102 ptrdiff_t old_point = -1, start_point = -1; \
103 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
104 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
105 bool free_print_buffer = 0; \
106 bool multibyte \
107 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
108 Lisp_Object original = printcharfun; \
109 if (NILP (printcharfun)) printcharfun = Qt; \
110 if (BUFFERP (printcharfun)) \
112 if (XBUFFER (printcharfun) != current_buffer) \
113 Fset_buffer (printcharfun); \
114 printcharfun = Qnil; \
116 if (MARKERP (printcharfun)) \
118 ptrdiff_t marker_pos; \
119 if (! XMARKER (printcharfun)->buffer) \
120 error ("Marker does not point anywhere"); \
121 if (XMARKER (printcharfun)->buffer != current_buffer) \
122 set_buffer_internal (XMARKER (printcharfun)->buffer); \
123 marker_pos = marker_position (printcharfun); \
124 if (marker_pos < BEGV || marker_pos > ZV) \
125 signal_error ("Marker is outside the accessible " \
126 "part of the buffer", printcharfun); \
127 old_point = PT; \
128 old_point_byte = PT_BYTE; \
129 SET_PT_BOTH (marker_pos, \
130 marker_byte_position (printcharfun)); \
131 start_point = PT; \
132 start_point_byte = PT_BYTE; \
133 printcharfun = Qnil; \
135 if (NILP (printcharfun)) \
137 Lisp_Object string; \
138 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
139 && ! print_escape_multibyte) \
140 specbind (Qprint_escape_multibyte, Qt); \
141 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
142 && ! print_escape_nonascii) \
143 specbind (Qprint_escape_nonascii, Qt); \
144 if (print_buffer != 0) \
146 string = make_string_from_bytes (print_buffer, \
147 print_buffer_pos, \
148 print_buffer_pos_byte); \
149 record_unwind_protect (print_unwind, string); \
151 else \
153 int new_size = 1000; \
154 print_buffer = xmalloc (new_size); \
155 print_buffer_size = new_size; \
156 free_print_buffer = 1; \
158 print_buffer_pos = 0; \
159 print_buffer_pos_byte = 0; \
161 if (EQ (printcharfun, Qt) && ! noninteractive) \
162 setup_echo_area_for_printing (multibyte);
164 #define PRINTFINISH \
165 if (NILP (printcharfun)) \
167 if (print_buffer_pos != print_buffer_pos_byte \
168 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
170 USE_SAFE_ALLOCA; \
171 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
172 copy_text ((unsigned char *) print_buffer, temp, \
173 print_buffer_pos_byte, 1, 0); \
174 insert_1_both ((char *) temp, print_buffer_pos, \
175 print_buffer_pos, 0, 1, 0); \
176 SAFE_FREE (); \
178 else \
179 insert_1_both (print_buffer, print_buffer_pos, \
180 print_buffer_pos_byte, 0, 1, 0); \
181 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
183 if (free_print_buffer) \
185 xfree (print_buffer); \
186 print_buffer = 0; \
188 unbind_to (specpdl_count, Qnil); \
189 if (MARKERP (original)) \
190 set_marker_both (original, Qnil, PT, PT_BYTE); \
191 if (old_point >= 0) \
192 SET_PT_BOTH (old_point + (old_point >= start_point \
193 ? PT - start_point : 0), \
194 old_point_byte + (old_point_byte >= start_point_byte \
195 ? PT_BYTE - start_point_byte : 0)); \
196 set_buffer_internal (old);
198 /* This is used to restore the saved contents of print_buffer
199 when there is a recursive call to print. */
201 static void
202 print_unwind (Lisp_Object saved_text)
204 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
207 /* Print character CH to the stdio stream STREAM. */
209 static void
210 printchar_to_stream (unsigned int ch, FILE *stream)
212 Lisp_Object dv UNINIT;
213 ptrdiff_t i = 0, n = 1;
214 Lisp_Object coding_system = Vlocale_coding_system;
215 bool encode_p = false;
217 if (!NILP (Vcoding_system_for_write))
218 coding_system = Vcoding_system_for_write;
219 if (!NILP (coding_system))
220 encode_p = true;
222 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
224 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
225 if (VECTORP (dv))
227 n = ASIZE (dv);
228 goto next_char;
232 while (true)
234 if (ASCII_CHAR_P (ch))
236 putc_unlocked (ch, stream);
237 #ifdef WINDOWSNT
238 /* Send the output to a debugger (nothing happens if there
239 isn't one). */
240 if (print_output_debug_flag && stream == stderr)
241 OutputDebugString ((char []) {ch, '\0'});
242 #endif
244 else
246 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
247 int len = CHAR_STRING (ch, mbstr);
248 Lisp_Object encoded_ch =
249 make_multibyte_string ((char *) mbstr, 1, len);
251 if (encode_p)
252 encoded_ch = code_convert_string_norecord (encoded_ch,
253 coding_system, true);
254 fwrite_unlocked (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
255 #ifdef WINDOWSNT
256 if (print_output_debug_flag && stream == stderr)
257 OutputDebugString (SSDATA (encoded_ch));
258 #endif
261 i++;
263 next_char:
264 for (; i < n; i++)
265 if (CHARACTERP (AREF (dv, i)))
266 break;
267 if (! (i < n))
268 break;
269 ch = XFIXNAT (AREF (dv, i));
273 /* Print character CH using method FUN. FUN nil means print to
274 print_buffer. FUN t means print to echo area or stdout if
275 non-interactive. If FUN is neither nil nor t, call FUN with CH as
276 argument. */
278 static void
279 printchar (unsigned int ch, Lisp_Object fun)
281 if (!NILP (fun) && !EQ (fun, Qt))
282 call1 (fun, make_fixnum (ch));
283 else
285 unsigned char str[MAX_MULTIBYTE_LENGTH];
286 int len = CHAR_STRING (ch, str);
288 maybe_quit ();
290 if (NILP (fun))
292 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
293 if (incr > 0)
294 print_buffer = xpalloc (print_buffer, &print_buffer_size,
295 incr, -1, 1);
296 memcpy (print_buffer + print_buffer_pos_byte, str, len);
297 print_buffer_pos += 1;
298 print_buffer_pos_byte += len;
300 else if (noninteractive)
302 printchar_stdout_last = ch;
303 if (DISP_TABLE_P (Vstandard_display_table))
304 printchar_to_stream (ch, stdout);
305 else
306 fwrite_unlocked (str, 1, len, stdout);
307 noninteractive_need_newline = 1;
309 else
311 bool multibyte_p
312 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
314 setup_echo_area_for_printing (multibyte_p);
315 insert_char (ch);
316 message_dolog ((char *) str, len, 0, multibyte_p);
321 /* Output an octal escape for C. If C is less than '\100' consult the
322 following character (if any) to see whether to use three octal
323 digits to avoid misinterpretation of the next character. The next
324 character after C will be taken from DATA, starting at byte
325 location I, if I is less than SIZE. Use PRINTCHARFUN to output
326 each character. */
328 static void
329 octalout (unsigned char c, unsigned char *data, ptrdiff_t i, ptrdiff_t size,
330 Lisp_Object printcharfun)
332 int digits = (c > '\77' || (i < size && '0' <= data[i] && data[i] <= '7')
334 : c > '\7' ? 2 : 1);
335 printchar ('\\', printcharfun);
337 printchar ('0' + ((c >> (3 * --digits)) & 7), printcharfun);
338 while (digits != 0);
341 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
342 method PRINTCHARFUN. PRINTCHARFUN nil means output to
343 print_buffer. PRINTCHARFUN t means output to the echo area or to
344 stdout if non-interactive. If neither nil nor t, call Lisp
345 function PRINTCHARFUN for each character printed. MULTIBYTE
346 non-zero means PTR contains multibyte characters.
348 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
349 to data in a Lisp string. Otherwise that is not safe. */
351 static void
352 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
353 Lisp_Object printcharfun)
355 if (NILP (printcharfun))
357 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
358 if (incr > 0)
359 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
360 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
361 print_buffer_pos += size;
362 print_buffer_pos_byte += size_byte;
364 else if (noninteractive && EQ (printcharfun, Qt))
366 if (DISP_TABLE_P (Vstandard_display_table))
368 int len;
369 for (ptrdiff_t i = 0; i < size_byte; i += len)
371 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
372 len);
373 printchar_to_stream (ch, stdout);
376 else
377 fwrite_unlocked (ptr, 1, size_byte, stdout);
379 noninteractive_need_newline = 1;
381 else if (EQ (printcharfun, Qt))
383 /* Output to echo area. We're trying to avoid a little overhead
384 here, that's the reason we don't call printchar to do the
385 job. */
386 int i;
387 bool multibyte_p
388 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
390 setup_echo_area_for_printing (multibyte_p);
391 message_dolog (ptr, size_byte, 0, multibyte_p);
393 if (size == size_byte)
395 for (i = 0; i < size; ++i)
396 insert_char ((unsigned char) *ptr++);
398 else
400 int len;
401 for (i = 0; i < size_byte; i += len)
403 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
404 len);
405 insert_char (ch);
409 else
411 /* PRINTCHARFUN is a Lisp function. */
412 ptrdiff_t i = 0;
414 if (size == size_byte)
416 while (i < size_byte)
418 int ch = ptr[i++];
419 printchar (ch, printcharfun);
422 else
424 while (i < size_byte)
426 /* Here, we must convert each multi-byte form to the
427 corresponding character code before handing it to
428 PRINTCHAR. */
429 int len;
430 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
431 len);
432 printchar (ch, printcharfun);
433 i += len;
439 /* Print the contents of a string STRING using PRINTCHARFUN.
440 It isn't safe to use strout in many cases,
441 because printing one char can relocate. */
443 static void
444 print_string (Lisp_Object string, Lisp_Object printcharfun)
446 if (EQ (printcharfun, Qt) || NILP (printcharfun))
448 ptrdiff_t chars;
450 if (print_escape_nonascii)
451 string = string_escape_byte8 (string);
453 if (STRING_MULTIBYTE (string))
454 chars = SCHARS (string);
455 else if (! print_escape_nonascii
456 && (EQ (printcharfun, Qt)
457 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
458 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
460 /* If unibyte string STRING contains 8-bit codes, we must
461 convert STRING to a multibyte string containing the same
462 character codes. */
463 Lisp_Object newstr;
464 ptrdiff_t bytes;
466 chars = SBYTES (string);
467 bytes = count_size_as_multibyte (SDATA (string), chars);
468 if (chars < bytes)
470 newstr = make_uninit_multibyte_string (chars, bytes);
471 memcpy (SDATA (newstr), SDATA (string), chars);
472 str_to_multibyte (SDATA (newstr), bytes, chars);
473 string = newstr;
476 else
477 chars = SBYTES (string);
479 if (EQ (printcharfun, Qt))
481 /* Output to echo area. */
482 ptrdiff_t nbytes = SBYTES (string);
484 /* Copy the string contents so that relocation of STRING by
485 GC does not cause trouble. */
486 USE_SAFE_ALLOCA;
487 char *buffer = SAFE_ALLOCA (nbytes);
488 memcpy (buffer, SDATA (string), nbytes);
490 strout (buffer, chars, nbytes, printcharfun);
492 SAFE_FREE ();
494 else
495 /* No need to copy, since output to print_buffer can't GC. */
496 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
498 else
500 /* Otherwise, string may be relocated by printing one char.
501 So re-fetch the string address for each character. */
502 ptrdiff_t i;
503 ptrdiff_t size = SCHARS (string);
504 ptrdiff_t size_byte = SBYTES (string);
505 if (size == size_byte)
506 for (i = 0; i < size; i++)
507 printchar (SREF (string, i), printcharfun);
508 else
509 for (i = 0; i < size_byte; )
511 /* Here, we must convert each multi-byte form to the
512 corresponding character code before handing it to PRINTCHAR. */
513 int len;
514 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
515 printchar (ch, printcharfun);
516 i += len;
521 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
522 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
523 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
524 (Lisp_Object character, Lisp_Object printcharfun)
526 if (NILP (printcharfun))
527 printcharfun = Vstandard_output;
528 CHECK_FIXNUM (character);
529 PRINTPREPARE;
530 printchar (XFIXNUM (character), printcharfun);
531 PRINTFINISH;
532 return character;
535 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
536 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
537 Do not use this on the contents of a Lisp string. */
539 static void
540 print_c_string (char const *string, Lisp_Object printcharfun)
542 ptrdiff_t len = strlen (string);
543 strout (string, len, len, printcharfun);
546 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
547 Do not use this on the contents of a Lisp string. */
549 static void
550 write_string (const char *data, Lisp_Object printcharfun)
552 PRINTPREPARE;
553 print_c_string (data, printcharfun);
554 PRINTFINISH;
558 void
559 temp_output_buffer_setup (const char *bufname)
561 ptrdiff_t count = SPECPDL_INDEX ();
562 register struct buffer *old = current_buffer;
563 register Lisp_Object buf;
565 record_unwind_current_buffer ();
567 Fset_buffer (Fget_buffer_create (build_string (bufname)));
569 Fkill_all_local_variables ();
570 delete_all_overlays (current_buffer);
571 bset_directory (current_buffer, BVAR (old, directory));
572 bset_read_only (current_buffer, Qnil);
573 bset_filename (current_buffer, Qnil);
574 bset_undo_list (current_buffer, Qt);
575 eassert (current_buffer->overlays_before == NULL);
576 eassert (current_buffer->overlays_after == NULL);
577 bset_enable_multibyte_characters
578 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
579 specbind (Qinhibit_read_only, Qt);
580 specbind (Qinhibit_modification_hooks, Qt);
581 Ferase_buffer ();
582 XSETBUFFER (buf, current_buffer);
584 run_hook (Qtemp_buffer_setup_hook);
586 unbind_to (count, Qnil);
588 specbind (Qstandard_output, buf);
591 static void print (Lisp_Object, Lisp_Object, bool);
592 static void print_preprocess (Lisp_Object);
593 static void print_preprocess_string (INTERVAL, void *);
594 static void print_object (Lisp_Object, Lisp_Object, bool);
596 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
597 doc: /* Output a newline to stream PRINTCHARFUN.
598 If ENSURE is non-nil only output a newline if not already at the
599 beginning of a line. Value is non-nil if a newline is printed.
600 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
601 (Lisp_Object printcharfun, Lisp_Object ensure)
603 Lisp_Object val;
605 if (NILP (printcharfun))
606 printcharfun = Vstandard_output;
607 PRINTPREPARE;
609 if (NILP (ensure))
610 val = Qt;
611 /* Difficult to check if at line beginning so abort. */
612 else if (FUNCTIONP (printcharfun))
613 signal_error ("Unsupported function argument", printcharfun);
614 else if (noninteractive && !NILP (printcharfun))
615 val = printchar_stdout_last == 10 ? Qnil : Qt;
616 else
617 val = NILP (Fbolp ()) ? Qt : Qnil;
619 if (!NILP (val))
620 printchar ('\n', printcharfun);
621 PRINTFINISH;
622 return val;
625 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
626 doc: /* Output the printed representation of OBJECT, any Lisp object.
627 Quoting characters are printed when needed to make output that `read'
628 can handle, whenever this is possible. For complex objects, the behavior
629 is controlled by `print-level' and `print-length', which see.
631 OBJECT is any of the Lisp data types: a number, a string, a symbol,
632 a list, a buffer, a window, a frame, etc.
634 A printed representation of an object is text which describes that object.
636 Optional argument PRINTCHARFUN is the output stream, which can be one
637 of these:
639 - a buffer, in which case output is inserted into that buffer at point;
640 - a marker, in which case output is inserted at marker's position;
641 - a function, in which case that function is called once for each
642 character of OBJECT's printed representation;
643 - a symbol, in which case that symbol's function definition is called; or
644 - t, in which case the output is displayed in the echo area.
646 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
647 is used instead. */)
648 (Lisp_Object object, Lisp_Object printcharfun)
650 if (NILP (printcharfun))
651 printcharfun = Vstandard_output;
652 PRINTPREPARE;
653 print (object, printcharfun, 1);
654 PRINTFINISH;
655 return object;
658 /* A buffer which is used to hold output being built by prin1-to-string. */
659 Lisp_Object Vprin1_to_string_buffer;
661 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
662 doc: /* Return a string containing the printed representation of OBJECT.
663 OBJECT can be any Lisp object. This function outputs quoting characters
664 when necessary to make output that `read' can handle, whenever possible,
665 unless the optional second argument NOESCAPE is non-nil. For complex objects,
666 the behavior is controlled by `print-level' and `print-length', which see.
668 OBJECT is any of the Lisp data types: a number, a string, a symbol,
669 a list, a buffer, a window, a frame, etc.
671 A printed representation of an object is text which describes that object. */)
672 (Lisp_Object object, Lisp_Object noescape)
674 ptrdiff_t count = SPECPDL_INDEX ();
676 specbind (Qinhibit_modification_hooks, Qt);
678 /* Save and restore this: we are altering a buffer
679 but we don't want to deactivate the mark just for that.
680 No need for specbind, since errors deactivate the mark. */
681 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
683 Lisp_Object printcharfun = Vprin1_to_string_buffer;
684 PRINTPREPARE;
685 print (object, printcharfun, NILP (noescape));
686 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
687 PRINTFINISH;
689 struct buffer *previous = current_buffer;
690 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
691 object = Fbuffer_string ();
692 if (SBYTES (object) == SCHARS (object))
693 STRING_SET_UNIBYTE (object);
695 /* Note that this won't make prepare_to_modify_buffer call
696 ask-user-about-supersession-threat because this buffer
697 does not visit a file. */
698 Ferase_buffer ();
699 set_buffer_internal (previous);
701 Vdeactivate_mark = save_deactivate_mark;
703 return unbind_to (count, object);
706 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
707 doc: /* Output the printed representation of OBJECT, any Lisp object.
708 No quoting characters are used; no delimiters are printed around
709 the contents of strings.
711 OBJECT is any of the Lisp data types: a number, a string, a symbol,
712 a list, a buffer, a window, a frame, etc.
714 A printed representation of an object is text which describes that object.
716 Optional argument PRINTCHARFUN is the output stream, which can be one
717 of these:
719 - a buffer, in which case output is inserted into that buffer at point;
720 - a marker, in which case output is inserted at marker's position;
721 - a function, in which case that function is called once for each
722 character of OBJECT's printed representation;
723 - a symbol, in which case that symbol's function definition is called; or
724 - t, in which case the output is displayed in the echo area.
726 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
727 is used instead. */)
728 (Lisp_Object object, Lisp_Object printcharfun)
730 if (NILP (printcharfun))
731 printcharfun = Vstandard_output;
732 PRINTPREPARE;
733 print (object, printcharfun, 0);
734 PRINTFINISH;
735 return object;
738 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
739 doc: /* Output the printed representation of OBJECT, with newlines around it.
740 Quoting characters are printed when needed to make output that `read'
741 can handle, whenever this is possible. For complex objects, the behavior
742 is controlled by `print-level' and `print-length', which see.
744 OBJECT is any of the Lisp data types: a number, a string, a symbol,
745 a list, a buffer, a window, a frame, etc.
747 A printed representation of an object is text which describes that object.
749 Optional argument PRINTCHARFUN is the output stream, which can be one
750 of these:
752 - a buffer, in which case output is inserted into that buffer at point;
753 - a marker, in which case output is inserted at marker's position;
754 - a function, in which case that function is called once for each
755 character of OBJECT's printed representation;
756 - a symbol, in which case that symbol's function definition is called; or
757 - t, in which case the output is displayed in the echo area.
759 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
760 is used instead. */)
761 (Lisp_Object object, Lisp_Object printcharfun)
763 if (NILP (printcharfun))
764 printcharfun = Vstandard_output;
765 PRINTPREPARE;
766 printchar ('\n', printcharfun);
767 print (object, printcharfun, 1);
768 printchar ('\n', printcharfun);
769 PRINTFINISH;
770 return object;
773 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
774 doc: /* Write CHARACTER to stderr.
775 You can call `print' while debugging emacs, and pass it this function
776 to make it write to the debugging output. */)
777 (Lisp_Object character)
779 CHECK_FIXNUM (character);
780 printchar_to_stream (XFIXNUM (character), stderr);
781 return character;
784 /* This function is never called. Its purpose is to prevent
785 print_output_debug_flag from being optimized away. */
787 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
788 void
789 debug_output_compilation_hack (bool x)
791 print_output_debug_flag = x;
794 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
795 1, 2,
796 "FDebug output file: \nP",
797 doc: /* Redirect debugging output (stderr stream) to file FILE.
798 If FILE is nil, reset target to the initial stderr stream.
799 Optional arg APPEND non-nil (interactively, with prefix arg) means
800 append to existing target file. */)
801 (Lisp_Object file, Lisp_Object append)
803 /* If equal to STDERR_FILENO, stderr has not been duplicated and is OK as-is.
804 Otherwise, this is a close-on-exec duplicate of the original stderr. */
805 static int stderr_dup = STDERR_FILENO;
806 int fd = stderr_dup;
808 if (! NILP (file))
810 file = Fexpand_file_name (file, Qnil);
812 if (stderr_dup == STDERR_FILENO)
814 int n = fcntl (STDERR_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
815 if (n < 0)
816 report_file_error ("dup", file);
817 stderr_dup = n;
820 fd = emacs_open (SSDATA (ENCODE_FILE (file)),
821 (O_WRONLY | O_CREAT
822 | (! NILP (append) ? O_APPEND : O_TRUNC)),
823 0666);
824 if (fd < 0)
825 report_file_error ("Cannot open debugging output stream", file);
828 fflush_unlocked (stderr);
829 if (dup2 (fd, STDERR_FILENO) < 0)
830 report_file_error ("dup2", file);
831 if (fd != stderr_dup)
832 emacs_close (fd);
833 return Qnil;
837 /* This is the interface for debugging printing. */
839 void
840 debug_print (Lisp_Object arg)
842 Fprin1 (arg, Qexternal_debugging_output);
843 fprintf (stderr, "\r\n");
846 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
847 void
848 safe_debug_print (Lisp_Object arg)
850 int valid = valid_lisp_object_p (arg);
852 if (valid > 0)
853 debug_print (arg);
854 else
856 EMACS_UINT n = XLI (arg);
857 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
858 !valid ? "INVALID" : "SOME",
863 /* This function formats the given object and returns the result as a
864 string. Use this in contexts where you can inspect strings, but
865 where stderr output won't work --- e.g., while replaying rr
866 recordings. */
867 const char * debug_format (const char *, Lisp_Object) EXTERNALLY_VISIBLE;
868 const char *
869 debug_format (const char *fmt, Lisp_Object arg)
871 return SSDATA (CALLN (Fformat, build_string (fmt), arg));
875 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
876 1, 1, 0,
877 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
878 See Info anchor `(elisp)Definition of signal' for some details on how this
879 error message is constructed. */)
880 (Lisp_Object obj)
882 struct buffer *old = current_buffer;
883 Lisp_Object value;
885 /* If OBJ is (error STRING), just return STRING.
886 That is not only faster, it also avoids the need to allocate
887 space here when the error is due to memory full. */
888 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
889 && CONSP (XCDR (obj))
890 && STRINGP (XCAR (XCDR (obj)))
891 && NILP (XCDR (XCDR (obj))))
892 return XCAR (XCDR (obj));
894 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
896 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
897 value = Fbuffer_string ();
899 Ferase_buffer ();
900 set_buffer_internal (old);
902 return value;
905 /* Print an error message for the error DATA onto Lisp output stream
906 STREAM (suitable for the print functions).
907 CONTEXT is a C string describing the context of the error.
908 CALLER is the Lisp function inside which the error was signaled. */
910 void
911 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
912 Lisp_Object caller)
914 Lisp_Object errname, errmsg, file_error, tail;
916 if (context != 0)
917 write_string (context, stream);
919 /* If we know from where the error was signaled, show it in
920 *Messages*. */
921 if (!NILP (caller) && SYMBOLP (caller))
923 Lisp_Object cname = SYMBOL_NAME (caller);
924 ptrdiff_t cnamelen = SBYTES (cname);
925 USE_SAFE_ALLOCA;
926 char *name = SAFE_ALLOCA (cnamelen);
927 memcpy (name, SDATA (cname), cnamelen);
928 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
929 message_dolog (": ", 2, 0, 0);
930 SAFE_FREE ();
933 errname = Fcar (data);
935 if (EQ (errname, Qerror))
937 data = Fcdr (data);
938 if (!CONSP (data))
939 data = Qnil;
940 errmsg = Fcar (data);
941 file_error = Qnil;
943 else
945 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
946 errmsg = Fsubstitute_command_keys (Fget (errname, Qerror_message));
947 file_error = Fmemq (Qfile_error, error_conditions);
950 /* Print an error message including the data items. */
952 tail = Fcdr_safe (data);
954 /* For file-error, make error message by concatenating
955 all the data items. They are all strings. */
956 if (!NILP (file_error) && CONSP (tail))
957 errmsg = XCAR (tail), tail = XCDR (tail);
960 const char *sep = ": ";
962 if (!STRINGP (errmsg))
963 write_string ("peculiar error", stream);
964 else if (SCHARS (errmsg))
965 Fprinc (errmsg, stream);
966 else
967 sep = NULL;
969 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
971 Lisp_Object obj;
973 if (sep)
974 write_string (sep, stream);
975 obj = XCAR (tail);
976 if (!NILP (file_error)
977 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
978 Fprinc (obj, stream);
979 else
980 Fprin1 (obj, stream);
988 * The buffer should be at least as large as the max string size of the
989 * largest float, printed in the biggest notation. This is undoubtedly
990 * 20d float_output_format, with the negative of the C-constant "HUGE"
991 * from <math.h>.
993 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
995 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
996 * case of -1e307 in 20d float_output_format. What is one to do (short of
997 * re-writing _doprnt to be more sane)?
998 * -wsr
999 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
1003 float_to_string (char *buf, double data)
1005 char *cp;
1006 int width;
1007 int len;
1009 if (isinf (data))
1011 static char const minus_infinity_string[] = "-1.0e+INF";
1012 bool positive = 0 < data;
1013 strcpy (buf, minus_infinity_string + positive);
1014 return sizeof minus_infinity_string - 1 - positive;
1016 #if IEEE_FLOATING_POINT
1017 if (isnan (data))
1019 union ieee754_double u = { .d = data };
1020 uprintmax_t hi = u.ieee_nan.mantissa0;
1021 return sprintf (buf, &"-%"pMu".0e+NaN"[!u.ieee_nan.negative],
1022 (hi << 31 << 1) + u.ieee_nan.mantissa1);
1024 #endif
1026 if (NILP (Vfloat_output_format)
1027 || !STRINGP (Vfloat_output_format))
1028 lose:
1030 /* Generate the fewest number of digits that represent the
1031 floating point value without losing information. */
1032 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1033 /* The decimal point must be printed, or the byte compiler can
1034 get confused (Bug#8033). */
1035 width = 1;
1037 else /* oink oink */
1039 /* Check that the spec we have is fully valid.
1040 This means not only valid for printf,
1041 but meant for floats, and reasonable. */
1042 cp = SSDATA (Vfloat_output_format);
1044 if (cp[0] != '%')
1045 goto lose;
1046 if (cp[1] != '.')
1047 goto lose;
1049 cp += 2;
1051 /* Check the width specification. */
1052 width = -1;
1053 if ('0' <= *cp && *cp <= '9')
1055 width = 0;
1058 width = (width * 10) + (*cp++ - '0');
1059 if (DBL_DIG < width)
1060 goto lose;
1062 while (*cp >= '0' && *cp <= '9');
1064 /* A precision of zero is valid only for %f. */
1065 if (width == 0 && *cp != 'f')
1066 goto lose;
1069 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1070 goto lose;
1072 if (cp[1] != 0)
1073 goto lose;
1075 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1078 /* Make sure there is a decimal point with digit after, or an
1079 exponent, so that the value is readable as a float. But don't do
1080 this with "%.0f"; it's valid for that not to produce a decimal
1081 point. Note that width can be 0 only for %.0f. */
1082 if (width != 0)
1084 for (cp = buf; *cp; cp++)
1085 if ((*cp < '0' || *cp > '9') && *cp != '-')
1086 break;
1088 if (*cp == '.' && cp[1] == 0)
1090 cp[1] = '0';
1091 cp[2] = 0;
1092 len++;
1094 else if (*cp == 0)
1096 *cp++ = '.';
1097 *cp++ = '0';
1098 *cp++ = 0;
1099 len += 2;
1103 return len;
1107 static void
1108 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1110 new_backquote_output = 0;
1112 /* Reset print_number_index and Vprint_number_table only when
1113 the variable Vprint_continuous_numbering is nil. Otherwise,
1114 the values of these variables will be kept between several
1115 print functions. */
1116 if (NILP (Vprint_continuous_numbering)
1117 || NILP (Vprint_number_table))
1119 print_number_index = 0;
1120 Vprint_number_table = Qnil;
1123 /* Construct Vprint_number_table for print-gensym and print-circle. */
1124 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1126 /* Construct Vprint_number_table.
1127 This increments print_number_index for the objects added. */
1128 print_depth = 0;
1129 print_preprocess (obj);
1131 if (HASH_TABLE_P (Vprint_number_table))
1132 { /* Remove unnecessary objects, which appear only once in OBJ;
1133 that is, whose status is Qt. */
1134 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1135 ptrdiff_t i;
1137 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1138 if (!NILP (HASH_HASH (h, i))
1139 && EQ (HASH_VALUE (h, i), Qt))
1140 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1144 print_depth = 0;
1145 print_object (obj, printcharfun, escapeflag);
1148 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1149 (STRINGP (obj) || CONSP (obj) \
1150 || (VECTORLIKEP (obj) \
1151 && (VECTORP (obj) || COMPILEDP (obj) \
1152 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1153 || HASH_TABLE_P (obj) || FONTP (obj) \
1154 || RECORDP (obj))) \
1155 || (! NILP (Vprint_gensym) \
1156 && SYMBOLP (obj) \
1157 && !SYMBOL_INTERNED_P (obj)))
1159 /* Construct Vprint_number_table according to the structure of OBJ.
1160 OBJ itself and all its elements will be added to Vprint_number_table
1161 recursively if it is a list, vector, compiled function, char-table,
1162 string (its text properties will be traced), or a symbol that has
1163 no obarray (this is for the print-gensym feature).
1164 The status fields of Vprint_number_table mean whether each object appears
1165 more than once in OBJ: Qnil at the first time, and Qt after that. */
1166 static void
1167 print_preprocess (Lisp_Object obj)
1169 int i;
1170 ptrdiff_t size;
1171 int loop_count = 0;
1172 Lisp_Object halftail;
1174 /* Avoid infinite recursion for circular nested structure
1175 in the case where Vprint_circle is nil. */
1176 if (NILP (Vprint_circle))
1178 /* Give up if we go so deep that print_object will get an error. */
1179 /* See similar code in print_object. */
1180 if (print_depth >= PRINT_CIRCLE)
1181 error ("Apparently circular structure being printed");
1183 for (i = 0; i < print_depth; i++)
1184 if (EQ (obj, being_printed[i]))
1185 return;
1186 being_printed[print_depth] = obj;
1189 print_depth++;
1190 halftail = obj;
1192 loop:
1193 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1195 if (!HASH_TABLE_P (Vprint_number_table))
1196 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1198 /* In case print-circle is nil and print-gensym is t,
1199 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1200 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1202 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1203 if (!NILP (num)
1204 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1205 always print the gensym with a number. This is a special for
1206 the lisp function byte-compile-output-docform. */
1207 || (!NILP (Vprint_continuous_numbering)
1208 && SYMBOLP (obj)
1209 && !SYMBOL_INTERNED_P (obj)))
1210 { /* OBJ appears more than once. Let's remember that. */
1211 if (!FIXNUMP (num))
1213 print_number_index++;
1214 /* Negative number indicates it hasn't been printed yet. */
1215 Fputhash (obj, make_fixnum (- print_number_index),
1216 Vprint_number_table);
1218 print_depth--;
1219 return;
1221 else
1222 /* OBJ is not yet recorded. Let's add to the table. */
1223 Fputhash (obj, Qt, Vprint_number_table);
1226 switch (XTYPE (obj))
1228 case Lisp_String:
1229 /* A string may have text properties, which can be circular. */
1230 traverse_intervals_noorder (string_intervals (obj),
1231 print_preprocess_string, NULL);
1232 break;
1234 case Lisp_Cons:
1235 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1236 just as in print_object. */
1237 if (loop_count && EQ (obj, halftail))
1238 break;
1239 print_preprocess (XCAR (obj));
1240 obj = XCDR (obj);
1241 loop_count++;
1242 if (!(loop_count & 1))
1243 halftail = XCDR (halftail);
1244 goto loop;
1246 case Lisp_Vectorlike:
1247 size = ASIZE (obj);
1248 if (size & PSEUDOVECTOR_FLAG)
1249 size &= PSEUDOVECTOR_SIZE_MASK;
1250 for (i = (SUB_CHAR_TABLE_P (obj)
1251 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1252 print_preprocess (AREF (obj, i));
1253 if (HASH_TABLE_P (obj))
1254 { /* For hash tables, the key_and_value slot is past
1255 `size' because it needs to be marked specially in case
1256 the table is weak. */
1257 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1258 print_preprocess (h->key_and_value);
1260 break;
1262 default:
1263 break;
1266 print_depth--;
1269 DEFUN ("print--preprocess", Fprint_preprocess, Sprint_preprocess, 1, 1, 0,
1270 doc: /* Extract sharing info from OBJECT needed to print it.
1271 Fills `print-number-table'. */)
1272 (Lisp_Object object)
1274 print_number_index = 0;
1275 print_preprocess (object);
1276 return Qnil;
1279 static void
1280 print_preprocess_string (INTERVAL interval, void *arg)
1282 print_preprocess (interval->plist);
1285 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1287 #define PRINT_STRING_NON_CHARSET_FOUND 1
1288 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1290 /* Bitwise or of the above macros. */
1291 static int print_check_string_result;
1293 static void
1294 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1296 Lisp_Object val;
1298 if (NILP (interval->plist)
1299 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1300 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1301 return;
1302 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1303 val = XCDR (XCDR (val)));
1304 if (! CONSP (val))
1306 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1307 return;
1309 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1311 if (! EQ (val, interval->plist)
1312 || CONSP (XCDR (XCDR (val))))
1313 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1315 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1317 int i, c;
1318 ptrdiff_t charpos = interval->position;
1319 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1320 Lisp_Object charset;
1322 charset = XCAR (XCDR (val));
1323 for (i = 0; i < LENGTH (interval); i++)
1325 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1326 if (! ASCII_CHAR_P (c)
1327 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1329 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1330 break;
1336 /* The value is (charset . nil). */
1337 static Lisp_Object print_prune_charset_plist;
1339 static Lisp_Object
1340 print_prune_string_charset (Lisp_Object string)
1342 print_check_string_result = 0;
1343 traverse_intervals (string_intervals (string), 0,
1344 print_check_string_charset_prop, string);
1345 if (NILP (Vprint_charset_text_property)
1346 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1348 string = Fcopy_sequence (string);
1349 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1351 if (NILP (print_prune_charset_plist))
1352 print_prune_charset_plist = list1 (Qcharset);
1353 Fremove_text_properties (make_fixnum (0),
1354 make_fixnum (SCHARS (string)),
1355 print_prune_charset_plist, string);
1357 else
1358 Fset_text_properties (make_fixnum (0), make_fixnum (SCHARS (string)),
1359 Qnil, string);
1361 return string;
1364 static bool
1365 print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
1366 char *buf)
1368 switch (PSEUDOVECTOR_TYPE (XVECTOR (obj)))
1370 case PVEC_BIGNUM:
1372 ptrdiff_t size = bignum_bufsize (obj, 10);
1373 USE_SAFE_ALLOCA;
1374 char *str = SAFE_ALLOCA (size);
1375 ptrdiff_t len = bignum_to_c_string (str, size, obj, 10);
1376 strout (str, len, len, printcharfun);
1377 SAFE_FREE ();
1379 break;
1381 case PVEC_MARKER:
1382 print_c_string ("#<marker ", printcharfun);
1383 /* Do you think this is necessary? */
1384 if (XMARKER (obj)->insertion_type != 0)
1385 print_c_string ("(moves after insertion) ", printcharfun);
1386 if (! XMARKER (obj)->buffer)
1387 print_c_string ("in no buffer", printcharfun);
1388 else
1390 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1391 strout (buf, len, len, printcharfun);
1392 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1394 printchar ('>', printcharfun);
1395 break;
1397 case PVEC_OVERLAY:
1398 print_c_string ("#<overlay ", printcharfun);
1399 if (! XMARKER (OVERLAY_START (obj))->buffer)
1400 print_c_string ("in no buffer", printcharfun);
1401 else
1403 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
1404 marker_position (OVERLAY_START (obj)),
1405 marker_position (OVERLAY_END (obj)));
1406 strout (buf, len, len, printcharfun);
1407 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
1408 printcharfun);
1410 printchar ('>', printcharfun);
1411 break;
1413 #ifdef HAVE_MODULES
1414 case PVEC_USER_PTR:
1416 print_c_string ("#<user-ptr ", printcharfun);
1417 int i = sprintf (buf, "ptr=%p finalizer=%p",
1418 XUSER_PTR (obj)->p,
1419 XUSER_PTR (obj)->finalizer);
1420 strout (buf, i, i, printcharfun);
1421 printchar ('>', printcharfun);
1423 break;
1424 #endif
1426 case PVEC_FINALIZER:
1427 print_c_string ("#<finalizer", printcharfun);
1428 if (NILP (XFINALIZER (obj)->function))
1429 print_c_string (" used", printcharfun);
1430 printchar ('>', printcharfun);
1431 break;
1433 case PVEC_MISC_PTR:
1435 /* This shouldn't happen in normal usage, but let's
1436 print it anyway for the benefit of the debugger. */
1437 int i = sprintf (buf, "#<ptr %p>", xmint_pointer (obj));
1438 strout (buf, i, i, printcharfun);
1440 break;
1442 case PVEC_PROCESS:
1443 if (escapeflag)
1445 print_c_string ("#<process ", printcharfun);
1446 print_string (XPROCESS (obj)->name, printcharfun);
1447 printchar ('>', printcharfun);
1449 else
1450 print_string (XPROCESS (obj)->name, printcharfun);
1451 break;
1453 case PVEC_BOOL_VECTOR:
1455 EMACS_INT size = bool_vector_size (obj);
1456 ptrdiff_t size_in_bytes = bool_vector_bytes (size);
1457 ptrdiff_t real_size_in_bytes = size_in_bytes;
1458 unsigned char *data = bool_vector_uchar_data (obj);
1460 int len = sprintf (buf, "#&%"pI"d\"", size);
1461 strout (buf, len, len, printcharfun);
1463 /* Don't print more bytes than the specified maximum.
1464 Negative values of print-length are invalid. Treat them
1465 like a print-length of nil. */
1466 if (FIXNATP (Vprint_length)
1467 && XFIXNAT (Vprint_length) < size_in_bytes)
1468 size_in_bytes = XFIXNAT (Vprint_length);
1470 for (ptrdiff_t i = 0; i < size_in_bytes; i++)
1472 maybe_quit ();
1473 unsigned char c = data[i];
1474 if (c == '\n' && print_escape_newlines)
1475 print_c_string ("\\n", printcharfun);
1476 else if (c == '\f' && print_escape_newlines)
1477 print_c_string ("\\f", printcharfun);
1478 else if (c > '\177'
1479 || (print_escape_control_characters && c_iscntrl (c)))
1481 /* Use octal escapes to avoid encoding issues. */
1482 octalout (c, data, i + 1, size_in_bytes, printcharfun);
1484 else
1486 if (c == '\"' || c == '\\')
1487 printchar ('\\', printcharfun);
1488 printchar (c, printcharfun);
1492 if (size_in_bytes < real_size_in_bytes)
1493 print_c_string (" ...", printcharfun);
1494 printchar ('\"', printcharfun);
1496 break;
1498 case PVEC_SUBR:
1499 print_c_string ("#<subr ", printcharfun);
1500 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1501 printchar ('>', printcharfun);
1502 break;
1504 case PVEC_XWIDGET: case PVEC_XWIDGET_VIEW:
1505 print_c_string ("#<xwidget ", printcharfun);
1506 printchar ('>', printcharfun);
1507 break;
1509 case PVEC_WINDOW:
1511 int len = sprintf (buf, "#<window %"pI"d",
1512 XWINDOW (obj)->sequence_number);
1513 strout (buf, len, len, printcharfun);
1514 if (BUFFERP (XWINDOW (obj)->contents))
1516 print_c_string (" on ", printcharfun);
1517 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1518 printcharfun);
1520 printchar ('>', printcharfun);
1522 break;
1524 case PVEC_TERMINAL:
1526 struct terminal *t = XTERMINAL (obj);
1527 int len = sprintf (buf, "#<terminal %d", t->id);
1528 strout (buf, len, len, printcharfun);
1529 if (t->name)
1531 print_c_string (" on ", printcharfun);
1532 print_c_string (t->name, printcharfun);
1534 printchar ('>', printcharfun);
1536 break;
1538 case PVEC_HASH_TABLE:
1540 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1541 /* Implement a readable output, e.g.:
1542 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1543 /* Always print the size. */
1544 int len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1545 strout (buf, len, len, printcharfun);
1547 if (!NILP (h->test.name))
1549 print_c_string (" test ", printcharfun);
1550 print_object (h->test.name, printcharfun, escapeflag);
1553 if (!NILP (h->weak))
1555 print_c_string (" weakness ", printcharfun);
1556 print_object (h->weak, printcharfun, escapeflag);
1559 print_c_string (" rehash-size ", printcharfun);
1560 print_object (Fhash_table_rehash_size (obj),
1561 printcharfun, escapeflag);
1563 print_c_string (" rehash-threshold ", printcharfun);
1564 print_object (Fhash_table_rehash_threshold (obj),
1565 printcharfun, escapeflag);
1567 if (h->pure)
1569 print_c_string (" purecopy ", printcharfun);
1570 print_object (h->pure ? Qt : Qnil, printcharfun, escapeflag);
1573 print_c_string (" data ", printcharfun);
1575 /* Print the data here as a plist. */
1576 ptrdiff_t real_size = HASH_TABLE_SIZE (h);
1577 ptrdiff_t size = real_size;
1579 /* Don't print more elements than the specified maximum. */
1580 if (FIXNATP (Vprint_length) && XFIXNAT (Vprint_length) < size)
1581 size = XFIXNAT (Vprint_length);
1583 printchar ('(', printcharfun);
1584 for (ptrdiff_t i = 0; i < size; i++)
1585 if (!NILP (HASH_HASH (h, i)))
1587 if (i) printchar (' ', printcharfun);
1588 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1589 printchar (' ', printcharfun);
1590 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1593 if (size < real_size)
1594 print_c_string (" ...", printcharfun);
1596 print_c_string ("))", printcharfun);
1598 break;
1600 case PVEC_BUFFER:
1601 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1602 print_c_string ("#<killed buffer>", printcharfun);
1603 else if (escapeflag)
1605 print_c_string ("#<buffer ", printcharfun);
1606 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1607 printchar ('>', printcharfun);
1609 else
1610 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1611 break;
1613 case PVEC_WINDOW_CONFIGURATION:
1614 print_c_string ("#<window-configuration>", printcharfun);
1615 break;
1617 case PVEC_FRAME:
1619 void *ptr = XFRAME (obj);
1620 Lisp_Object frame_name = XFRAME (obj)->name;
1622 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1623 ? "#<frame "
1624 : "#<dead frame "),
1625 printcharfun);
1626 if (!STRINGP (frame_name))
1628 /* A frame could be too young and have no name yet;
1629 don't crash. */
1630 if (SYMBOLP (frame_name))
1631 frame_name = Fsymbol_name (frame_name);
1632 else /* can't happen: name should be either nil or string */
1633 frame_name = build_string ("*INVALID*FRAME*NAME*");
1635 print_string (frame_name, printcharfun);
1636 int len = sprintf (buf, " %p>", ptr);
1637 strout (buf, len, len, printcharfun);
1639 break;
1641 case PVEC_FONT:
1643 if (! FONT_OBJECT_P (obj))
1645 if (FONT_SPEC_P (obj))
1646 print_c_string ("#<font-spec", printcharfun);
1647 else
1648 print_c_string ("#<font-entity", printcharfun);
1649 for (int i = 0; i < FONT_SPEC_MAX; i++)
1651 printchar (' ', printcharfun);
1652 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1653 print_object (AREF (obj, i), printcharfun, escapeflag);
1654 else
1655 print_object (font_style_symbolic (obj, i, 0),
1656 printcharfun, escapeflag);
1659 else
1661 print_c_string ("#<font-object ", printcharfun);
1662 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1663 escapeflag);
1665 printchar ('>', printcharfun);
1667 break;
1669 case PVEC_THREAD:
1670 print_c_string ("#<thread ", printcharfun);
1671 if (STRINGP (XTHREAD (obj)->name))
1672 print_string (XTHREAD (obj)->name, printcharfun);
1673 else
1675 int len = sprintf (buf, "%p", XTHREAD (obj));
1676 strout (buf, len, len, printcharfun);
1678 printchar ('>', printcharfun);
1679 break;
1681 case PVEC_MUTEX:
1682 print_c_string ("#<mutex ", printcharfun);
1683 if (STRINGP (XMUTEX (obj)->name))
1684 print_string (XMUTEX (obj)->name, printcharfun);
1685 else
1687 int len = sprintf (buf, "%p", XMUTEX (obj));
1688 strout (buf, len, len, printcharfun);
1690 printchar ('>', printcharfun);
1691 break;
1693 case PVEC_CONDVAR:
1694 print_c_string ("#<condvar ", printcharfun);
1695 if (STRINGP (XCONDVAR (obj)->name))
1696 print_string (XCONDVAR (obj)->name, printcharfun);
1697 else
1699 int len = sprintf (buf, "%p", XCONDVAR (obj));
1700 strout (buf, len, len, printcharfun);
1702 printchar ('>', printcharfun);
1703 break;
1705 case PVEC_RECORD:
1707 ptrdiff_t size = PVSIZE (obj);
1709 /* Don't print more elements than the specified maximum. */
1710 ptrdiff_t n
1711 = (FIXNATP (Vprint_length) && XFIXNAT (Vprint_length) < size
1712 ? XFIXNAT (Vprint_length) : size);
1714 print_c_string ("#s(", printcharfun);
1715 for (ptrdiff_t i = 0; i < n; i ++)
1717 if (i) printchar (' ', printcharfun);
1718 print_object (AREF (obj, i), printcharfun, escapeflag);
1720 if (n < size)
1721 print_c_string (" ...", printcharfun);
1722 printchar (')', printcharfun);
1724 break;
1726 case PVEC_SUB_CHAR_TABLE:
1727 case PVEC_COMPILED:
1728 case PVEC_CHAR_TABLE:
1729 case PVEC_NORMAL_VECTOR:
1731 ptrdiff_t size = ASIZE (obj);
1732 if (COMPILEDP (obj))
1734 printchar ('#', printcharfun);
1735 size &= PSEUDOVECTOR_SIZE_MASK;
1737 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1739 /* Print a char-table as if it were a vector,
1740 lumping the parent and default slots in with the
1741 character slots. But add #^ as a prefix. */
1743 /* Make each lowest sub_char_table start a new line.
1744 Otherwise we'll make a line extremely long, which
1745 results in slow redisplay. */
1746 if (SUB_CHAR_TABLE_P (obj)
1747 && XSUB_CHAR_TABLE (obj)->depth == 3)
1748 printchar ('\n', printcharfun);
1749 print_c_string ("#^", printcharfun);
1750 if (SUB_CHAR_TABLE_P (obj))
1751 printchar ('^', printcharfun);
1752 size &= PSEUDOVECTOR_SIZE_MASK;
1754 if (size & PSEUDOVECTOR_FLAG)
1755 return false;
1757 printchar ('[', printcharfun);
1759 int idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1760 Lisp_Object tem;
1761 ptrdiff_t real_size = size;
1763 /* For a sub char-table, print heading non-Lisp data first. */
1764 if (SUB_CHAR_TABLE_P (obj))
1766 int i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1767 XSUB_CHAR_TABLE (obj)->min_char);
1768 strout (buf, i, i, printcharfun);
1771 /* Don't print more elements than the specified maximum. */
1772 if (FIXNATP (Vprint_length)
1773 && XFIXNAT (Vprint_length) < size)
1774 size = XFIXNAT (Vprint_length);
1776 for (int i = idx; i < size; i++)
1778 if (i) printchar (' ', printcharfun);
1779 tem = AREF (obj, i);
1780 print_object (tem, printcharfun, escapeflag);
1782 if (size < real_size)
1783 print_c_string (" ...", printcharfun);
1784 printchar (']', printcharfun);
1786 break;
1788 #ifdef HAVE_MODULES
1789 case PVEC_MODULE_FUNCTION:
1791 print_c_string ("#<module function ", printcharfun);
1792 void *ptr = XMODULE_FUNCTION (obj)->subr;
1793 const char *file = NULL;
1794 const char *symbol = NULL;
1795 dynlib_addr (ptr, &file, &symbol);
1797 if (symbol == NULL)
1799 print_c_string ("at ", printcharfun);
1800 enum { pointer_bufsize = sizeof ptr * 16 / CHAR_BIT + 2 + 1 };
1801 char buffer[pointer_bufsize];
1802 int needed = snprintf (buffer, sizeof buffer, "%p", ptr);
1803 const char p0x[] = "0x";
1804 eassert (needed <= sizeof buffer);
1805 /* ANSI C doesn't guarantee that %p produces a string that
1806 begins with a "0x". */
1807 if (c_strncasecmp (buffer, p0x, sizeof (p0x) - 1) != 0)
1808 print_c_string (p0x, printcharfun);
1809 print_c_string (buffer, printcharfun);
1811 else
1812 print_c_string (symbol, printcharfun);
1814 if (file != NULL)
1816 print_c_string (" from ", printcharfun);
1817 print_c_string (file, printcharfun);
1820 printchar ('>', printcharfun);
1822 break;
1823 #endif
1825 default:
1826 emacs_abort ();
1829 return true;
1832 static void
1833 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1835 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1836 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1837 40))];
1838 current_thread->stack_top = buf;
1839 maybe_quit ();
1841 /* Detect circularities and truncate them. */
1842 if (NILP (Vprint_circle))
1844 /* Simple but incomplete way. */
1845 int i;
1847 /* See similar code in print_preprocess. */
1848 if (print_depth >= PRINT_CIRCLE)
1849 error ("Apparently circular structure being printed");
1851 for (i = 0; i < print_depth; i++)
1852 if (EQ (obj, being_printed[i]))
1854 int len = sprintf (buf, "#%d", i);
1855 strout (buf, len, len, printcharfun);
1856 return;
1858 being_printed[print_depth] = obj;
1860 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1862 /* With the print-circle feature. */
1863 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1864 if (FIXNUMP (num))
1866 EMACS_INT n = XFIXNUM (num);
1867 if (n < 0)
1868 { /* Add a prefix #n= if OBJ has not yet been printed;
1869 that is, its status field is nil. */
1870 int len = sprintf (buf, "#%"pI"d=", -n);
1871 strout (buf, len, len, printcharfun);
1872 /* OBJ is going to be printed. Remember that fact. */
1873 Fputhash (obj, make_fixnum (- n), Vprint_number_table);
1875 else
1877 /* Just print #n# if OBJ has already been printed. */
1878 int len = sprintf (buf, "#%"pI"d#", n);
1879 strout (buf, len, len, printcharfun);
1880 return;
1885 print_depth++;
1887 switch (XTYPE (obj))
1889 case_Lisp_Int:
1891 int len = sprintf (buf, "%"pI"d", XFIXNUM (obj));
1892 strout (buf, len, len, printcharfun);
1894 break;
1896 case Lisp_Float:
1898 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1899 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1900 strout (pigbuf, len, len, printcharfun);
1902 break;
1904 case Lisp_String:
1905 if (!escapeflag)
1906 print_string (obj, printcharfun);
1907 else
1909 ptrdiff_t i, i_byte;
1910 ptrdiff_t size_byte;
1911 /* True means we must ensure that the next character we output
1912 cannot be taken as part of a hex character escape. */
1913 bool need_nonhex = false;
1914 bool multibyte = STRING_MULTIBYTE (obj);
1916 if (! EQ (Vprint_charset_text_property, Qt))
1917 obj = print_prune_string_charset (obj);
1919 if (string_intervals (obj))
1920 print_c_string ("#(", printcharfun);
1922 printchar ('\"', printcharfun);
1923 size_byte = SBYTES (obj);
1925 for (i = 0, i_byte = 0; i_byte < size_byte;)
1927 /* Here, we must convert each multi-byte form to the
1928 corresponding character code before handing it to printchar. */
1929 int c;
1931 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1933 maybe_quit ();
1935 if (multibyte
1936 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1937 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1938 && print_escape_nonascii))
1940 /* When printing a raw 8-bit byte in a multibyte buffer, or
1941 (when requested) a non-ASCII character in a unibyte buffer,
1942 print single-byte non-ASCII string chars
1943 using octal escapes. */
1944 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1945 need_nonhex = false;
1947 else if (multibyte
1948 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1950 /* When requested, print multibyte chars using hex escapes. */
1951 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1952 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1953 strout (outbuf, len, len, printcharfun);
1954 need_nonhex = true;
1956 else
1958 /* If we just had a hex escape, and this character
1959 could be taken as part of it,
1960 output `\ ' to prevent that. */
1961 if (c_isxdigit (c))
1963 if (need_nonhex)
1964 print_c_string ("\\ ", printcharfun);
1965 printchar (c, printcharfun);
1967 else if (c == '\n' && print_escape_newlines
1968 ? (c = 'n', true)
1969 : c == '\f' && print_escape_newlines
1970 ? (c = 'f', true)
1971 : c == '\"' || c == '\\')
1973 printchar ('\\', printcharfun);
1974 printchar (c, printcharfun);
1976 else if (print_escape_control_characters && c_iscntrl (c))
1977 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1978 else
1979 printchar (c, printcharfun);
1980 need_nonhex = false;
1983 printchar ('\"', printcharfun);
1985 if (string_intervals (obj))
1987 traverse_intervals (string_intervals (obj),
1988 0, print_interval, printcharfun);
1989 printchar (')', printcharfun);
1992 break;
1994 case Lisp_Symbol:
1996 bool confusing;
1997 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1998 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1999 int c;
2000 ptrdiff_t i, i_byte;
2001 ptrdiff_t size_byte;
2002 Lisp_Object name;
2004 name = SYMBOL_NAME (obj);
2006 if (p != end && (*p == '-' || *p == '+')) p++;
2007 if (p == end)
2008 confusing = 0;
2009 /* If symbol name begins with a digit, and ends with a digit,
2010 and contains nothing but digits and `e', it could be treated
2011 as a number. So set CONFUSING.
2013 Symbols that contain periods could also be taken as numbers,
2014 but periods are always escaped, so we don't have to worry
2015 about them here. */
2016 else if (*p >= '0' && *p <= '9'
2017 && end[-1] >= '0' && end[-1] <= '9')
2019 while (p != end && ((*p >= '0' && *p <= '9')
2020 /* Needed for \2e10. */
2021 || *p == 'e' || *p == 'E'))
2022 p++;
2023 confusing = (end == p);
2025 else
2026 confusing = 0;
2028 size_byte = SBYTES (name);
2030 if (! NILP (Vprint_gensym)
2031 && !SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (obj))
2032 print_c_string ("#:", printcharfun);
2033 else if (size_byte == 0)
2035 print_c_string ("##", printcharfun);
2036 break;
2039 for (i = 0, i_byte = 0; i_byte < size_byte;)
2041 /* Here, we must convert each multi-byte form to the
2042 corresponding character code before handing it to PRINTCHAR. */
2043 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
2044 maybe_quit ();
2046 if (escapeflag)
2048 if (c == '\"' || c == '\\' || c == '\''
2049 || c == ';' || c == '#' || c == '(' || c == ')'
2050 || c == ',' || c == '.' || c == '`'
2051 || c == '[' || c == ']' || c == '?' || c <= 040
2052 || confusing
2053 || (i == 1 && confusable_symbol_character_p (c)))
2055 printchar ('\\', printcharfun);
2056 confusing = false;
2059 printchar (c, printcharfun);
2062 break;
2064 case Lisp_Cons:
2065 /* If deeper than spec'd depth, print placeholder. */
2066 if (FIXNUMP (Vprint_level)
2067 && print_depth > XFIXNUM (Vprint_level))
2068 print_c_string ("...", printcharfun);
2069 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2070 && EQ (XCAR (obj), Qquote))
2072 printchar ('\'', printcharfun);
2073 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2075 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2076 && EQ (XCAR (obj), Qfunction))
2078 print_c_string ("#'", printcharfun);
2079 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2081 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2082 && EQ (XCAR (obj), Qbackquote))
2084 printchar ('`', printcharfun);
2085 new_backquote_output++;
2086 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2087 new_backquote_output--;
2089 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2090 && new_backquote_output
2091 && (EQ (XCAR (obj), Qcomma)
2092 || EQ (XCAR (obj), Qcomma_at)
2093 || EQ (XCAR (obj), Qcomma_dot)))
2095 print_object (XCAR (obj), printcharfun, false);
2096 new_backquote_output--;
2097 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2098 new_backquote_output++;
2100 else
2102 printchar ('(', printcharfun);
2104 Lisp_Object halftail = obj;
2106 /* Negative values of print-length are invalid in CL.
2107 Treat them like nil, as CMUCL does. */
2108 printmax_t print_length = (FIXNATP (Vprint_length)
2109 ? XFIXNAT (Vprint_length)
2110 : TYPE_MAXIMUM (printmax_t));
2112 printmax_t i = 0;
2113 while (CONSP (obj))
2115 /* Detect circular list. */
2116 if (NILP (Vprint_circle))
2118 /* Simple but incomplete way. */
2119 if (i != 0 && EQ (obj, halftail))
2121 int len = sprintf (buf, " . #%"pMd, i / 2);
2122 strout (buf, len, len, printcharfun);
2123 goto end_of_list;
2126 else
2128 /* With the print-circle feature. */
2129 if (i != 0)
2131 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
2132 if (FIXNUMP (num))
2134 print_c_string (" . ", printcharfun);
2135 print_object (obj, printcharfun, escapeflag);
2136 goto end_of_list;
2141 if (i)
2142 printchar (' ', printcharfun);
2144 if (print_length <= i)
2146 print_c_string ("...", printcharfun);
2147 goto end_of_list;
2150 i++;
2151 print_object (XCAR (obj), printcharfun, escapeflag);
2153 obj = XCDR (obj);
2154 if (!(i & 1))
2155 halftail = XCDR (halftail);
2158 /* OBJ non-nil here means it's the end of a dotted list. */
2159 if (!NILP (obj))
2161 print_c_string (" . ", printcharfun);
2162 print_object (obj, printcharfun, escapeflag);
2165 end_of_list:
2166 printchar (')', printcharfun);
2168 break;
2170 case Lisp_Vectorlike:
2171 if (print_vectorlike (obj, printcharfun, escapeflag, buf))
2172 break;
2173 FALLTHROUGH;
2174 default:
2176 int len;
2177 /* We're in trouble if this happens!
2178 Probably should just emacs_abort (). */
2179 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2180 if (VECTORLIKEP (obj))
2181 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2182 else
2183 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2184 strout (buf, len, len, printcharfun);
2185 print_c_string ((" Save your buffers immediately"
2186 " and please report this bug>"),
2187 printcharfun);
2191 print_depth--;
2195 /* Print a description of INTERVAL using PRINTCHARFUN.
2196 This is part of printing a string that has text properties. */
2198 static void
2199 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2201 if (NILP (interval->plist))
2202 return;
2203 printchar (' ', printcharfun);
2204 print_object (make_fixnum (interval->position), printcharfun, 1);
2205 printchar (' ', printcharfun);
2206 print_object (make_fixnum (interval->position + LENGTH (interval)),
2207 printcharfun, 1);
2208 printchar (' ', printcharfun);
2209 print_object (interval->plist, printcharfun, 1);
2212 /* Initialize debug_print stuff early to have it working from the very
2213 beginning. */
2215 void
2216 init_print_once (void)
2218 /* The subroutine object for external-debugging-output is kept here
2219 for the convenience of the debugger. */
2220 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2222 defsubr (&Sexternal_debugging_output);
2225 void
2226 syms_of_print (void)
2228 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2230 DEFVAR_LISP ("standard-output", Vstandard_output,
2231 doc: /* Output stream `print' uses by default for outputting a character.
2232 This may be any function of one argument.
2233 It may also be a buffer (output is inserted before point)
2234 or a marker (output is inserted and the marker is advanced)
2235 or the symbol t (output appears in the echo area). */);
2236 Vstandard_output = Qt;
2237 DEFSYM (Qstandard_output, "standard-output");
2239 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2240 doc: /* The format descriptor string used to print floats.
2241 This is a %-spec like those accepted by `printf' in C,
2242 but with some restrictions. It must start with the two characters `%.'.
2243 After that comes an integer precision specification,
2244 and then a letter which controls the format.
2245 The letters allowed are `e', `f' and `g'.
2246 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2247 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2248 Use `g' to choose the shorter of those two formats for the number at hand.
2249 The precision in any of these cases is the number of digits following
2250 the decimal point. With `f', a precision of 0 means to omit the
2251 decimal point. 0 is not allowed with `e' or `g'.
2253 A value of nil means to use the shortest notation
2254 that represents the number without losing information. */);
2255 Vfloat_output_format = Qnil;
2257 DEFVAR_LISP ("print-length", Vprint_length,
2258 doc: /* Maximum length of list to print before abbreviating.
2259 A value of nil means no limit. See also `eval-expression-print-length'. */);
2260 Vprint_length = Qnil;
2262 DEFVAR_LISP ("print-level", Vprint_level,
2263 doc: /* Maximum depth of list nesting to print before abbreviating.
2264 A value of nil means no limit. See also `eval-expression-print-level'. */);
2265 Vprint_level = Qnil;
2267 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2268 doc: /* Non-nil means print newlines in strings as `\\n'.
2269 Also print formfeeds as `\\f'. */);
2270 print_escape_newlines = 0;
2272 DEFVAR_BOOL ("print-escape-control-characters", print_escape_control_characters,
2273 doc: /* Non-nil means print control characters in strings as `\\OOO'.
2274 \(OOO is the octal representation of the character code.)*/);
2275 print_escape_control_characters = 0;
2277 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2278 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2279 \(OOO is the octal representation of the character code.)
2280 Only single-byte characters are affected, and only in `prin1'.
2281 When the output goes in a multibyte buffer, this feature is
2282 enabled regardless of the value of the variable. */);
2283 print_escape_nonascii = 0;
2285 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2286 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2287 \(XXXX is the hex representation of the character code.)
2288 This affects only `prin1'. */);
2289 print_escape_multibyte = 0;
2291 DEFVAR_BOOL ("print-quoted", print_quoted,
2292 doc: /* Non-nil means print quoted forms with reader syntax.
2293 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2294 print_quoted = true;
2296 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2297 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2298 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2299 When the uninterned symbol appears multiple times within the printed
2300 expression, and `print-circle' is non-nil, in addition use the #N#
2301 and #N= constructs as needed, so that multiple references to the same
2302 symbol are shared once again when the text is read back. */);
2303 Vprint_gensym = Qnil;
2305 DEFVAR_LISP ("print-circle", Vprint_circle,
2306 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2307 If nil, printing proceeds recursively and may lead to
2308 `max-lisp-eval-depth' being exceeded or an error may occur:
2309 \"Apparently circular structure being printed.\" Also see
2310 `print-length' and `print-level'.
2311 If non-nil, shared substructures anywhere in the structure are printed
2312 with `#N=' before the first occurrence (in the order of the print
2313 representation) and `#N#' in place of each subsequent occurrence,
2314 where N is a positive decimal integer. */);
2315 Vprint_circle = Qnil;
2317 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2318 doc: /* Non-nil means number continuously across print calls.
2319 This affects the numbers printed for #N= labels and #M# references.
2320 See also `print-circle', `print-gensym', and `print-number-table'.
2321 This variable should not be set with `setq'; bind it with a `let' instead. */);
2322 Vprint_continuous_numbering = Qnil;
2324 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2325 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2326 The Lisp printer uses this vector to detect Lisp objects referenced more
2327 than once.
2329 When you bind `print-continuous-numbering' to t, you should probably
2330 also bind `print-number-table' to nil. This ensures that the value of
2331 `print-number-table' can be garbage-collected once the printing is
2332 done. If all elements of `print-number-table' are nil, it means that
2333 the printing done so far has not found any shared structure or objects
2334 that need to be recorded in the table. */);
2335 Vprint_number_table = Qnil;
2337 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2338 doc: /* A flag to control printing of `charset' text property on printing a string.
2339 The value should be nil, t, or `default'.
2341 If the value is nil, don't print the text property `charset'.
2343 If the value is t, always print the text property `charset'.
2345 If the value is `default', print the text property `charset' only when
2346 the value is different from what is guessed in the current charset
2347 priorities. Values other than nil or t are also treated as
2348 `default'. */);
2349 Vprint_charset_text_property = Qdefault;
2351 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2352 staticpro (&Vprin1_to_string_buffer);
2354 defsubr (&Sprin1);
2355 defsubr (&Sprin1_to_string);
2356 defsubr (&Serror_message_string);
2357 defsubr (&Sprinc);
2358 defsubr (&Sprint);
2359 defsubr (&Sterpri);
2360 defsubr (&Swrite_char);
2361 defsubr (&Sredirect_debugging_output);
2362 defsubr (&Sprint_preprocess);
2364 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2365 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2367 print_prune_charset_plist = Qnil;
2368 staticpro (&print_prune_charset_plist);