Honor print-charset-text-property value of nil (Bug#31376)
[emacs.git] / src / print.c
blob839437522042f0832b1f4176895041ebf4bb3a3d
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>
42 #ifdef WINDOWSNT
43 # include <sys/socket.h> /* for F_DUPFD_CLOEXEC */
44 #endif
46 struct terminal;
48 /* Avoid actual stack overflow in print. */
49 static ptrdiff_t print_depth;
51 /* Level of nesting inside outputting backquote in new style. */
52 static ptrdiff_t new_backquote_output;
54 /* Detect most circularities to print finite output. */
55 #define PRINT_CIRCLE 200
56 static Lisp_Object being_printed[PRINT_CIRCLE];
58 /* Last char printed to stdout by printchar. */
59 static unsigned int printchar_stdout_last;
61 /* When printing into a buffer, first we put the text in this
62 block, then insert it all at once. */
63 static char *print_buffer;
65 /* Size allocated in print_buffer. */
66 static ptrdiff_t print_buffer_size;
67 /* Chars stored in print_buffer. */
68 static ptrdiff_t print_buffer_pos;
69 /* Bytes stored in print_buffer. */
70 static ptrdiff_t print_buffer_pos_byte;
72 /* Vprint_number_table is a table, that keeps objects that are going to
73 be printed, to allow use of #n= and #n# to express sharing.
74 For any given object, the table can give the following values:
75 t the object will be printed only once.
76 -N the object will be printed several times and will take number N.
77 N the object has been printed so we can refer to it as #N#.
78 print_number_index holds the largest N already used.
79 N has to be striclty larger than 0 since we need to distinguish -N. */
80 static ptrdiff_t print_number_index;
81 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
83 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
84 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
87 /* Low level output routines for characters and strings. */
89 /* Lisp functions to do output using a stream
90 must have the stream in a variable called printcharfun
91 and must start with PRINTPREPARE, end with PRINTFINISH.
92 Use printchar to output one character,
93 or call strout to output a block of characters. */
95 #define PRINTPREPARE \
96 struct buffer *old = current_buffer; \
97 ptrdiff_t old_point = -1, start_point = -1; \
98 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
99 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
100 bool free_print_buffer = 0; \
101 bool multibyte \
102 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
103 Lisp_Object original = printcharfun; \
104 if (NILP (printcharfun)) printcharfun = Qt; \
105 if (BUFFERP (printcharfun)) \
107 if (XBUFFER (printcharfun) != current_buffer) \
108 Fset_buffer (printcharfun); \
109 printcharfun = Qnil; \
111 if (MARKERP (printcharfun)) \
113 ptrdiff_t marker_pos; \
114 if (! XMARKER (printcharfun)->buffer) \
115 error ("Marker does not point anywhere"); \
116 if (XMARKER (printcharfun)->buffer != current_buffer) \
117 set_buffer_internal (XMARKER (printcharfun)->buffer); \
118 marker_pos = marker_position (printcharfun); \
119 if (marker_pos < BEGV || marker_pos > ZV) \
120 signal_error ("Marker is outside the accessible " \
121 "part of the buffer", printcharfun); \
122 old_point = PT; \
123 old_point_byte = PT_BYTE; \
124 SET_PT_BOTH (marker_pos, \
125 marker_byte_position (printcharfun)); \
126 start_point = PT; \
127 start_point_byte = PT_BYTE; \
128 printcharfun = Qnil; \
130 if (NILP (printcharfun)) \
132 Lisp_Object string; \
133 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
134 && ! print_escape_multibyte) \
135 specbind (Qprint_escape_multibyte, Qt); \
136 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
137 && ! print_escape_nonascii) \
138 specbind (Qprint_escape_nonascii, Qt); \
139 if (print_buffer != 0) \
141 string = make_string_from_bytes (print_buffer, \
142 print_buffer_pos, \
143 print_buffer_pos_byte); \
144 record_unwind_protect (print_unwind, string); \
146 else \
148 int new_size = 1000; \
149 print_buffer = xmalloc (new_size); \
150 print_buffer_size = new_size; \
151 free_print_buffer = 1; \
153 print_buffer_pos = 0; \
154 print_buffer_pos_byte = 0; \
156 if (EQ (printcharfun, Qt) && ! noninteractive) \
157 setup_echo_area_for_printing (multibyte);
159 #define PRINTFINISH \
160 if (NILP (printcharfun)) \
162 if (print_buffer_pos != print_buffer_pos_byte \
163 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
165 USE_SAFE_ALLOCA; \
166 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
167 copy_text ((unsigned char *) print_buffer, temp, \
168 print_buffer_pos_byte, 1, 0); \
169 insert_1_both ((char *) temp, print_buffer_pos, \
170 print_buffer_pos, 0, 1, 0); \
171 SAFE_FREE (); \
173 else \
174 insert_1_both (print_buffer, print_buffer_pos, \
175 print_buffer_pos_byte, 0, 1, 0); \
176 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
178 if (free_print_buffer) \
180 xfree (print_buffer); \
181 print_buffer = 0; \
183 unbind_to (specpdl_count, Qnil); \
184 if (MARKERP (original)) \
185 set_marker_both (original, Qnil, PT, PT_BYTE); \
186 if (old_point >= 0) \
187 SET_PT_BOTH (old_point + (old_point >= start_point \
188 ? PT - start_point : 0), \
189 old_point_byte + (old_point_byte >= start_point_byte \
190 ? PT_BYTE - start_point_byte : 0)); \
191 set_buffer_internal (old);
193 /* This is used to restore the saved contents of print_buffer
194 when there is a recursive call to print. */
196 static void
197 print_unwind (Lisp_Object saved_text)
199 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
202 /* Print character CH to the stdio stream STREAM. */
204 static void
205 printchar_to_stream (unsigned int ch, FILE *stream)
207 Lisp_Object dv UNINIT;
208 ptrdiff_t i = 0, n = 1;
209 Lisp_Object coding_system = Vlocale_coding_system;
210 bool encode_p = false;
212 if (!NILP (Vcoding_system_for_write))
213 coding_system = Vcoding_system_for_write;
214 if (!NILP (coding_system))
215 encode_p = true;
217 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
219 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
220 if (VECTORP (dv))
222 n = ASIZE (dv);
223 goto next_char;
227 while (true)
229 if (ASCII_CHAR_P (ch))
231 putc_unlocked (ch, stream);
232 #ifdef WINDOWSNT
233 /* Send the output to a debugger (nothing happens if there
234 isn't one). */
235 if (print_output_debug_flag && stream == stderr)
236 OutputDebugString ((char []) {ch, '\0'});
237 #endif
239 else
241 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
242 int len = CHAR_STRING (ch, mbstr);
243 Lisp_Object encoded_ch =
244 make_multibyte_string ((char *) mbstr, 1, len);
246 if (encode_p)
247 encoded_ch = code_convert_string_norecord (encoded_ch,
248 coding_system, true);
249 fwrite_unlocked (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
250 #ifdef WINDOWSNT
251 if (print_output_debug_flag && stream == stderr)
252 OutputDebugString (SSDATA (encoded_ch));
253 #endif
256 i++;
258 next_char:
259 for (; i < n; i++)
260 if (CHARACTERP (AREF (dv, i)))
261 break;
262 if (! (i < n))
263 break;
264 ch = XFASTINT (AREF (dv, i));
268 /* Print character CH using method FUN. FUN nil means print to
269 print_buffer. FUN t means print to echo area or stdout if
270 non-interactive. If FUN is neither nil nor t, call FUN with CH as
271 argument. */
273 static void
274 printchar (unsigned int ch, Lisp_Object fun)
276 if (!NILP (fun) && !EQ (fun, Qt))
277 call1 (fun, make_number (ch));
278 else
280 unsigned char str[MAX_MULTIBYTE_LENGTH];
281 int len = CHAR_STRING (ch, str);
283 maybe_quit ();
285 if (NILP (fun))
287 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
288 if (incr > 0)
289 print_buffer = xpalloc (print_buffer, &print_buffer_size,
290 incr, -1, 1);
291 memcpy (print_buffer + print_buffer_pos_byte, str, len);
292 print_buffer_pos += 1;
293 print_buffer_pos_byte += len;
295 else if (noninteractive)
297 printchar_stdout_last = ch;
298 if (DISP_TABLE_P (Vstandard_display_table))
299 printchar_to_stream (ch, stdout);
300 else
301 fwrite_unlocked (str, 1, len, stdout);
302 noninteractive_need_newline = 1;
304 else
306 bool multibyte_p
307 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
309 setup_echo_area_for_printing (multibyte_p);
310 insert_char (ch);
311 message_dolog ((char *) str, len, 0, multibyte_p);
316 /* Output an octal escape for C. If C is less than '\100' consult the
317 following character (if any) to see whether to use three octal
318 digits to avoid misinterpretation of the next character. The next
319 character after C will be taken from DATA, starting at byte
320 location I, if I is less than SIZE. Use PRINTCHARFUN to output
321 each character. */
323 static void
324 octalout (unsigned char c, unsigned char *data, ptrdiff_t i, ptrdiff_t size,
325 Lisp_Object printcharfun)
327 int digits = (c > '\77' || (i < size && '0' <= data[i] && data[i] <= '7')
329 : c > '\7' ? 2 : 1);
330 printchar ('\\', printcharfun);
332 printchar ('0' + ((c >> (3 * --digits)) & 7), printcharfun);
333 while (digits != 0);
336 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
337 method PRINTCHARFUN. PRINTCHARFUN nil means output to
338 print_buffer. PRINTCHARFUN t means output to the echo area or to
339 stdout if non-interactive. If neither nil nor t, call Lisp
340 function PRINTCHARFUN for each character printed. MULTIBYTE
341 non-zero means PTR contains multibyte characters.
343 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
344 to data in a Lisp string. Otherwise that is not safe. */
346 static void
347 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
348 Lisp_Object printcharfun)
350 if (NILP (printcharfun))
352 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
353 if (incr > 0)
354 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
355 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
356 print_buffer_pos += size;
357 print_buffer_pos_byte += size_byte;
359 else if (noninteractive && EQ (printcharfun, Qt))
361 if (DISP_TABLE_P (Vstandard_display_table))
363 int len;
364 for (ptrdiff_t i = 0; i < size_byte; i += len)
366 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
367 len);
368 printchar_to_stream (ch, stdout);
371 else
372 fwrite_unlocked (ptr, 1, size_byte, stdout);
374 noninteractive_need_newline = 1;
376 else if (EQ (printcharfun, Qt))
378 /* Output to echo area. We're trying to avoid a little overhead
379 here, that's the reason we don't call printchar to do the
380 job. */
381 int i;
382 bool multibyte_p
383 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
385 setup_echo_area_for_printing (multibyte_p);
386 message_dolog (ptr, size_byte, 0, multibyte_p);
388 if (size == size_byte)
390 for (i = 0; i < size; ++i)
391 insert_char ((unsigned char) *ptr++);
393 else
395 int len;
396 for (i = 0; i < size_byte; i += len)
398 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
399 len);
400 insert_char (ch);
404 else
406 /* PRINTCHARFUN is a Lisp function. */
407 ptrdiff_t i = 0;
409 if (size == size_byte)
411 while (i < size_byte)
413 int ch = ptr[i++];
414 printchar (ch, printcharfun);
417 else
419 while (i < size_byte)
421 /* Here, we must convert each multi-byte form to the
422 corresponding character code before handing it to
423 PRINTCHAR. */
424 int len;
425 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
426 len);
427 printchar (ch, printcharfun);
428 i += len;
434 /* Print the contents of a string STRING using PRINTCHARFUN.
435 It isn't safe to use strout in many cases,
436 because printing one char can relocate. */
438 static void
439 print_string (Lisp_Object string, Lisp_Object printcharfun)
441 if (EQ (printcharfun, Qt) || NILP (printcharfun))
443 ptrdiff_t chars;
445 if (print_escape_nonascii)
446 string = string_escape_byte8 (string);
448 if (STRING_MULTIBYTE (string))
449 chars = SCHARS (string);
450 else if (! print_escape_nonascii
451 && (EQ (printcharfun, Qt)
452 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
453 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
455 /* If unibyte string STRING contains 8-bit codes, we must
456 convert STRING to a multibyte string containing the same
457 character codes. */
458 Lisp_Object newstr;
459 ptrdiff_t bytes;
461 chars = SBYTES (string);
462 bytes = count_size_as_multibyte (SDATA (string), chars);
463 if (chars < bytes)
465 newstr = make_uninit_multibyte_string (chars, bytes);
466 memcpy (SDATA (newstr), SDATA (string), chars);
467 str_to_multibyte (SDATA (newstr), bytes, chars);
468 string = newstr;
471 else
472 chars = SBYTES (string);
474 if (EQ (printcharfun, Qt))
476 /* Output to echo area. */
477 ptrdiff_t nbytes = SBYTES (string);
479 /* Copy the string contents so that relocation of STRING by
480 GC does not cause trouble. */
481 USE_SAFE_ALLOCA;
482 char *buffer = SAFE_ALLOCA (nbytes);
483 memcpy (buffer, SDATA (string), nbytes);
485 strout (buffer, chars, nbytes, printcharfun);
487 SAFE_FREE ();
489 else
490 /* No need to copy, since output to print_buffer can't GC. */
491 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
493 else
495 /* Otherwise, string may be relocated by printing one char.
496 So re-fetch the string address for each character. */
497 ptrdiff_t i;
498 ptrdiff_t size = SCHARS (string);
499 ptrdiff_t size_byte = SBYTES (string);
500 if (size == size_byte)
501 for (i = 0; i < size; i++)
502 printchar (SREF (string, i), printcharfun);
503 else
504 for (i = 0; i < size_byte; )
506 /* Here, we must convert each multi-byte form to the
507 corresponding character code before handing it to PRINTCHAR. */
508 int len;
509 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
510 printchar (ch, printcharfun);
511 i += len;
516 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
517 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
518 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
519 (Lisp_Object character, Lisp_Object printcharfun)
521 if (NILP (printcharfun))
522 printcharfun = Vstandard_output;
523 CHECK_NUMBER (character);
524 PRINTPREPARE;
525 printchar (XINT (character), printcharfun);
526 PRINTFINISH;
527 return character;
530 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
531 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
532 Do not use this on the contents of a Lisp string. */
534 static void
535 print_c_string (char const *string, Lisp_Object printcharfun)
537 ptrdiff_t len = strlen (string);
538 strout (string, len, len, printcharfun);
541 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
542 Do not use this on the contents of a Lisp string. */
544 static void
545 write_string (const char *data, Lisp_Object printcharfun)
547 PRINTPREPARE;
548 print_c_string (data, printcharfun);
549 PRINTFINISH;
553 void
554 temp_output_buffer_setup (const char *bufname)
556 ptrdiff_t count = SPECPDL_INDEX ();
557 register struct buffer *old = current_buffer;
558 register Lisp_Object buf;
560 record_unwind_current_buffer ();
562 Fset_buffer (Fget_buffer_create (build_string (bufname)));
564 Fkill_all_local_variables ();
565 delete_all_overlays (current_buffer);
566 bset_directory (current_buffer, BVAR (old, directory));
567 bset_read_only (current_buffer, Qnil);
568 bset_filename (current_buffer, Qnil);
569 bset_undo_list (current_buffer, Qt);
570 eassert (current_buffer->overlays_before == NULL);
571 eassert (current_buffer->overlays_after == NULL);
572 bset_enable_multibyte_characters
573 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
574 specbind (Qinhibit_read_only, Qt);
575 specbind (Qinhibit_modification_hooks, Qt);
576 Ferase_buffer ();
577 XSETBUFFER (buf, current_buffer);
579 run_hook (Qtemp_buffer_setup_hook);
581 unbind_to (count, Qnil);
583 specbind (Qstandard_output, buf);
586 static void print (Lisp_Object, Lisp_Object, bool);
587 static void print_preprocess (Lisp_Object);
588 static void print_preprocess_string (INTERVAL, void *);
589 static void print_object (Lisp_Object, Lisp_Object, bool);
591 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
592 doc: /* Output a newline to stream PRINTCHARFUN.
593 If ENSURE is non-nil only output a newline if not already at the
594 beginning of a line. Value is non-nil if a newline is printed.
595 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
596 (Lisp_Object printcharfun, Lisp_Object ensure)
598 Lisp_Object val;
600 if (NILP (printcharfun))
601 printcharfun = Vstandard_output;
602 PRINTPREPARE;
604 if (NILP (ensure))
605 val = Qt;
606 /* Difficult to check if at line beginning so abort. */
607 else if (FUNCTIONP (printcharfun))
608 signal_error ("Unsupported function argument", printcharfun);
609 else if (noninteractive && !NILP (printcharfun))
610 val = printchar_stdout_last == 10 ? Qnil : Qt;
611 else
612 val = NILP (Fbolp ()) ? Qt : Qnil;
614 if (!NILP (val))
615 printchar ('\n', printcharfun);
616 PRINTFINISH;
617 return val;
620 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
621 doc: /* Output the printed representation of OBJECT, any Lisp object.
622 Quoting characters are printed when needed to make output that `read'
623 can handle, whenever this is possible. For complex objects, the behavior
624 is controlled by `print-level' and `print-length', which see.
626 OBJECT is any of the Lisp data types: a number, a string, a symbol,
627 a list, a buffer, a window, a frame, etc.
629 A printed representation of an object is text which describes that object.
631 Optional argument PRINTCHARFUN is the output stream, which can be one
632 of these:
634 - a buffer, in which case output is inserted into that buffer at point;
635 - a marker, in which case output is inserted at marker's position;
636 - a function, in which case that function is called once for each
637 character of OBJECT's printed representation;
638 - a symbol, in which case that symbol's function definition is called; or
639 - t, in which case the output is displayed in the echo area.
641 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
642 is used instead. */)
643 (Lisp_Object object, Lisp_Object printcharfun)
645 if (NILP (printcharfun))
646 printcharfun = Vstandard_output;
647 PRINTPREPARE;
648 print (object, printcharfun, 1);
649 PRINTFINISH;
650 return object;
653 /* A buffer which is used to hold output being built by prin1-to-string. */
654 Lisp_Object Vprin1_to_string_buffer;
656 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
657 doc: /* Return a string containing the printed representation of OBJECT.
658 OBJECT can be any Lisp object. This function outputs quoting characters
659 when necessary to make output that `read' can handle, whenever possible,
660 unless the optional second argument NOESCAPE is non-nil. For complex objects,
661 the behavior is controlled by `print-level' and `print-length', which see.
663 OBJECT is any of the Lisp data types: a number, a string, a symbol,
664 a list, a buffer, a window, a frame, etc.
666 A printed representation of an object is text which describes that object. */)
667 (Lisp_Object object, Lisp_Object noescape)
669 ptrdiff_t count = SPECPDL_INDEX ();
671 specbind (Qinhibit_modification_hooks, Qt);
673 /* Save and restore this: we are altering a buffer
674 but we don't want to deactivate the mark just for that.
675 No need for specbind, since errors deactivate the mark. */
676 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
678 Lisp_Object printcharfun = Vprin1_to_string_buffer;
679 PRINTPREPARE;
680 print (object, printcharfun, NILP (noescape));
681 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
682 PRINTFINISH;
684 struct buffer *previous = current_buffer;
685 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
686 object = Fbuffer_string ();
687 if (SBYTES (object) == SCHARS (object))
688 STRING_SET_UNIBYTE (object);
690 /* Note that this won't make prepare_to_modify_buffer call
691 ask-user-about-supersession-threat because this buffer
692 does not visit a file. */
693 Ferase_buffer ();
694 set_buffer_internal (previous);
696 Vdeactivate_mark = save_deactivate_mark;
698 return unbind_to (count, object);
701 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
702 doc: /* Output the printed representation of OBJECT, any Lisp object.
703 No quoting characters are used; no delimiters are printed around
704 the contents of strings.
706 OBJECT is any of the Lisp data types: a number, a string, a symbol,
707 a list, a buffer, a window, a frame, etc.
709 A printed representation of an object is text which describes that object.
711 Optional argument PRINTCHARFUN is the output stream, which can be one
712 of these:
714 - a buffer, in which case output is inserted into that buffer at point;
715 - a marker, in which case output is inserted at marker's position;
716 - a function, in which case that function is called once for each
717 character of OBJECT's printed representation;
718 - a symbol, in which case that symbol's function definition is called; or
719 - t, in which case the output is displayed in the echo area.
721 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
722 is used instead. */)
723 (Lisp_Object object, Lisp_Object printcharfun)
725 if (NILP (printcharfun))
726 printcharfun = Vstandard_output;
727 PRINTPREPARE;
728 print (object, printcharfun, 0);
729 PRINTFINISH;
730 return object;
733 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
734 doc: /* Output the printed representation of OBJECT, with newlines around it.
735 Quoting characters are printed when needed to make output that `read'
736 can handle, whenever this is possible. For complex objects, the behavior
737 is controlled by `print-level' and `print-length', which see.
739 OBJECT is any of the Lisp data types: a number, a string, a symbol,
740 a list, a buffer, a window, a frame, etc.
742 A printed representation of an object is text which describes that object.
744 Optional argument PRINTCHARFUN is the output stream, which can be one
745 of these:
747 - a buffer, in which case output is inserted into that buffer at point;
748 - a marker, in which case output is inserted at marker's position;
749 - a function, in which case that function is called once for each
750 character of OBJECT's printed representation;
751 - a symbol, in which case that symbol's function definition is called; or
752 - t, in which case the output is displayed in the echo area.
754 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
755 is used instead. */)
756 (Lisp_Object object, Lisp_Object printcharfun)
758 if (NILP (printcharfun))
759 printcharfun = Vstandard_output;
760 PRINTPREPARE;
761 printchar ('\n', printcharfun);
762 print (object, printcharfun, 1);
763 printchar ('\n', printcharfun);
764 PRINTFINISH;
765 return object;
768 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
769 doc: /* Write CHARACTER to stderr.
770 You can call `print' while debugging emacs, and pass it this function
771 to make it write to the debugging output. */)
772 (Lisp_Object character)
774 CHECK_NUMBER (character);
775 printchar_to_stream (XINT (character), stderr);
776 return character;
779 /* This function is never called. Its purpose is to prevent
780 print_output_debug_flag from being optimized away. */
782 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
783 void
784 debug_output_compilation_hack (bool x)
786 print_output_debug_flag = x;
789 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
790 1, 2,
791 "FDebug output file: \nP",
792 doc: /* Redirect debugging output (stderr stream) to file FILE.
793 If FILE is nil, reset target to the initial stderr stream.
794 Optional arg APPEND non-nil (interactively, with prefix arg) means
795 append to existing target file. */)
796 (Lisp_Object file, Lisp_Object append)
798 /* If equal to STDERR_FILENO, stderr has not been duplicated and is OK as-is.
799 Otherwise, this is a close-on-exec duplicate of the original stderr. */
800 static int stderr_dup = STDERR_FILENO;
801 int fd = stderr_dup;
803 if (! NILP (file))
805 file = Fexpand_file_name (file, Qnil);
807 if (stderr_dup == STDERR_FILENO)
809 int n = fcntl (STDERR_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
810 if (n < 0)
811 report_file_error ("dup", file);
812 stderr_dup = n;
815 fd = emacs_open (SSDATA (ENCODE_FILE (file)),
816 (O_WRONLY | O_CREAT
817 | (! NILP (append) ? O_APPEND : O_TRUNC)),
818 0666);
819 if (fd < 0)
820 report_file_error ("Cannot open debugging output stream", file);
823 fflush_unlocked (stderr);
824 if (dup2 (fd, STDERR_FILENO) < 0)
825 report_file_error ("dup2", file);
826 if (fd != stderr_dup)
827 emacs_close (fd);
828 return Qnil;
832 /* This is the interface for debugging printing. */
834 void
835 debug_print (Lisp_Object arg)
837 Fprin1 (arg, Qexternal_debugging_output);
838 fprintf (stderr, "\r\n");
841 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
842 void
843 safe_debug_print (Lisp_Object arg)
845 int valid = valid_lisp_object_p (arg);
847 if (valid > 0)
848 debug_print (arg);
849 else
851 EMACS_UINT n = XLI (arg);
852 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
853 !valid ? "INVALID" : "SOME",
859 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
860 1, 1, 0,
861 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
862 See Info anchor `(elisp)Definition of signal' for some details on how this
863 error message is constructed. */)
864 (Lisp_Object obj)
866 struct buffer *old = current_buffer;
867 Lisp_Object value;
869 /* If OBJ is (error STRING), just return STRING.
870 That is not only faster, it also avoids the need to allocate
871 space here when the error is due to memory full. */
872 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
873 && CONSP (XCDR (obj))
874 && STRINGP (XCAR (XCDR (obj)))
875 && NILP (XCDR (XCDR (obj))))
876 return XCAR (XCDR (obj));
878 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
880 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
881 value = Fbuffer_string ();
883 Ferase_buffer ();
884 set_buffer_internal (old);
886 return value;
889 /* Print an error message for the error DATA onto Lisp output stream
890 STREAM (suitable for the print functions).
891 CONTEXT is a C string describing the context of the error.
892 CALLER is the Lisp function inside which the error was signaled. */
894 void
895 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
896 Lisp_Object caller)
898 Lisp_Object errname, errmsg, file_error, tail;
900 if (context != 0)
901 write_string (context, stream);
903 /* If we know from where the error was signaled, show it in
904 *Messages*. */
905 if (!NILP (caller) && SYMBOLP (caller))
907 Lisp_Object cname = SYMBOL_NAME (caller);
908 ptrdiff_t cnamelen = SBYTES (cname);
909 USE_SAFE_ALLOCA;
910 char *name = SAFE_ALLOCA (cnamelen);
911 memcpy (name, SDATA (cname), cnamelen);
912 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
913 message_dolog (": ", 2, 0, 0);
914 SAFE_FREE ();
917 errname = Fcar (data);
919 if (EQ (errname, Qerror))
921 data = Fcdr (data);
922 if (!CONSP (data))
923 data = Qnil;
924 errmsg = Fcar (data);
925 file_error = Qnil;
927 else
929 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
930 errmsg = Fsubstitute_command_keys (Fget (errname, Qerror_message));
931 file_error = Fmemq (Qfile_error, error_conditions);
934 /* Print an error message including the data items. */
936 tail = Fcdr_safe (data);
938 /* For file-error, make error message by concatenating
939 all the data items. They are all strings. */
940 if (!NILP (file_error) && CONSP (tail))
941 errmsg = XCAR (tail), tail = XCDR (tail);
944 const char *sep = ": ";
946 if (!STRINGP (errmsg))
947 write_string ("peculiar error", stream);
948 else if (SCHARS (errmsg))
949 Fprinc (errmsg, stream);
950 else
951 sep = NULL;
953 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
955 Lisp_Object obj;
957 if (sep)
958 write_string (sep, stream);
959 obj = XCAR (tail);
960 if (!NILP (file_error)
961 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
962 Fprinc (obj, stream);
963 else
964 Fprin1 (obj, stream);
972 * The buffer should be at least as large as the max string size of the
973 * largest float, printed in the biggest notation. This is undoubtedly
974 * 20d float_output_format, with the negative of the C-constant "HUGE"
975 * from <math.h>.
977 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
979 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
980 * case of -1e307 in 20d float_output_format. What is one to do (short of
981 * re-writing _doprnt to be more sane)?
982 * -wsr
983 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
987 float_to_string (char *buf, double data)
989 char *cp;
990 int width;
991 int len;
993 /* Check for plus infinity in a way that won't lose
994 if there is no plus infinity. */
995 if (data == data / 2 && data > 1.0)
997 static char const infinity_string[] = "1.0e+INF";
998 strcpy (buf, infinity_string);
999 return sizeof infinity_string - 1;
1001 /* Likewise for minus infinity. */
1002 if (data == data / 2 && data < -1.0)
1004 static char const minus_infinity_string[] = "-1.0e+INF";
1005 strcpy (buf, minus_infinity_string);
1006 return sizeof minus_infinity_string - 1;
1008 /* Check for NaN in a way that won't fail if there are no NaNs. */
1009 if (! (data * 0.0 >= 0.0))
1011 /* Prepend "-" if the NaN's sign bit is negative.
1012 The sign bit of a double is the bit that is 1 in -0.0. */
1013 static char const NaN_string[] = "0.0e+NaN";
1014 int i;
1015 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1016 bool negative = 0;
1017 u_data.d = data;
1018 u_minus_zero.d = - 0.0;
1019 for (i = 0; i < sizeof (double); i++)
1020 if (u_data.c[i] & u_minus_zero.c[i])
1022 *buf = '-';
1023 negative = 1;
1024 break;
1027 strcpy (buf + negative, NaN_string);
1028 return negative + sizeof NaN_string - 1;
1031 if (NILP (Vfloat_output_format)
1032 || !STRINGP (Vfloat_output_format))
1033 lose:
1035 /* Generate the fewest number of digits that represent the
1036 floating point value without losing information. */
1037 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1038 /* The decimal point must be printed, or the byte compiler can
1039 get confused (Bug#8033). */
1040 width = 1;
1042 else /* oink oink */
1044 /* Check that the spec we have is fully valid.
1045 This means not only valid for printf,
1046 but meant for floats, and reasonable. */
1047 cp = SSDATA (Vfloat_output_format);
1049 if (cp[0] != '%')
1050 goto lose;
1051 if (cp[1] != '.')
1052 goto lose;
1054 cp += 2;
1056 /* Check the width specification. */
1057 width = -1;
1058 if ('0' <= *cp && *cp <= '9')
1060 width = 0;
1063 width = (width * 10) + (*cp++ - '0');
1064 if (DBL_DIG < width)
1065 goto lose;
1067 while (*cp >= '0' && *cp <= '9');
1069 /* A precision of zero is valid only for %f. */
1070 if (width == 0 && *cp != 'f')
1071 goto lose;
1074 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1075 goto lose;
1077 if (cp[1] != 0)
1078 goto lose;
1080 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1083 /* Make sure there is a decimal point with digit after, or an
1084 exponent, so that the value is readable as a float. But don't do
1085 this with "%.0f"; it's valid for that not to produce a decimal
1086 point. Note that width can be 0 only for %.0f. */
1087 if (width != 0)
1089 for (cp = buf; *cp; cp++)
1090 if ((*cp < '0' || *cp > '9') && *cp != '-')
1091 break;
1093 if (*cp == '.' && cp[1] == 0)
1095 cp[1] = '0';
1096 cp[2] = 0;
1097 len++;
1099 else if (*cp == 0)
1101 *cp++ = '.';
1102 *cp++ = '0';
1103 *cp++ = 0;
1104 len += 2;
1108 return len;
1112 static void
1113 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1115 new_backquote_output = 0;
1117 /* Reset print_number_index and Vprint_number_table only when
1118 the variable Vprint_continuous_numbering is nil. Otherwise,
1119 the values of these variables will be kept between several
1120 print functions. */
1121 if (NILP (Vprint_continuous_numbering)
1122 || NILP (Vprint_number_table))
1124 print_number_index = 0;
1125 Vprint_number_table = Qnil;
1128 /* Construct Vprint_number_table for print-gensym and print-circle. */
1129 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1131 /* Construct Vprint_number_table.
1132 This increments print_number_index for the objects added. */
1133 print_depth = 0;
1134 print_preprocess (obj);
1136 if (HASH_TABLE_P (Vprint_number_table))
1137 { /* Remove unnecessary objects, which appear only once in OBJ;
1138 that is, whose status is Qt. */
1139 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1140 ptrdiff_t i;
1142 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1143 if (!NILP (HASH_HASH (h, i))
1144 && EQ (HASH_VALUE (h, i), Qt))
1145 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1149 print_depth = 0;
1150 print_object (obj, printcharfun, escapeflag);
1153 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1154 (STRINGP (obj) || CONSP (obj) \
1155 || (VECTORLIKEP (obj) \
1156 && (VECTORP (obj) || COMPILEDP (obj) \
1157 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1158 || HASH_TABLE_P (obj) || FONTP (obj) \
1159 || RECORDP (obj))) \
1160 || (! NILP (Vprint_gensym) \
1161 && SYMBOLP (obj) \
1162 && !SYMBOL_INTERNED_P (obj)))
1164 /* Construct Vprint_number_table according to the structure of OBJ.
1165 OBJ itself and all its elements will be added to Vprint_number_table
1166 recursively if it is a list, vector, compiled function, char-table,
1167 string (its text properties will be traced), or a symbol that has
1168 no obarray (this is for the print-gensym feature).
1169 The status fields of Vprint_number_table mean whether each object appears
1170 more than once in OBJ: Qnil at the first time, and Qt after that. */
1171 static void
1172 print_preprocess (Lisp_Object obj)
1174 int i;
1175 ptrdiff_t size;
1176 int loop_count = 0;
1177 Lisp_Object halftail;
1179 /* Avoid infinite recursion for circular nested structure
1180 in the case where Vprint_circle is nil. */
1181 if (NILP (Vprint_circle))
1183 /* Give up if we go so deep that print_object will get an error. */
1184 /* See similar code in print_object. */
1185 if (print_depth >= PRINT_CIRCLE)
1186 error ("Apparently circular structure being printed");
1188 for (i = 0; i < print_depth; i++)
1189 if (EQ (obj, being_printed[i]))
1190 return;
1191 being_printed[print_depth] = obj;
1194 print_depth++;
1195 halftail = obj;
1197 loop:
1198 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1200 if (!HASH_TABLE_P (Vprint_number_table))
1201 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1203 /* In case print-circle is nil and print-gensym is t,
1204 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1205 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1207 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1208 if (!NILP (num)
1209 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1210 always print the gensym with a number. This is a special for
1211 the lisp function byte-compile-output-docform. */
1212 || (!NILP (Vprint_continuous_numbering)
1213 && SYMBOLP (obj)
1214 && !SYMBOL_INTERNED_P (obj)))
1215 { /* OBJ appears more than once. Let's remember that. */
1216 if (!INTEGERP (num))
1218 print_number_index++;
1219 /* Negative number indicates it hasn't been printed yet. */
1220 Fputhash (obj, make_number (- print_number_index),
1221 Vprint_number_table);
1223 print_depth--;
1224 return;
1226 else
1227 /* OBJ is not yet recorded. Let's add to the table. */
1228 Fputhash (obj, Qt, Vprint_number_table);
1231 switch (XTYPE (obj))
1233 case Lisp_String:
1234 /* A string may have text properties, which can be circular. */
1235 traverse_intervals_noorder (string_intervals (obj),
1236 print_preprocess_string, NULL);
1237 break;
1239 case Lisp_Cons:
1240 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1241 just as in print_object. */
1242 if (loop_count && EQ (obj, halftail))
1243 break;
1244 print_preprocess (XCAR (obj));
1245 obj = XCDR (obj);
1246 loop_count++;
1247 if (!(loop_count & 1))
1248 halftail = XCDR (halftail);
1249 goto loop;
1251 case Lisp_Vectorlike:
1252 size = ASIZE (obj);
1253 if (size & PSEUDOVECTOR_FLAG)
1254 size &= PSEUDOVECTOR_SIZE_MASK;
1255 for (i = (SUB_CHAR_TABLE_P (obj)
1256 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1257 print_preprocess (AREF (obj, i));
1258 if (HASH_TABLE_P (obj))
1259 { /* For hash tables, the key_and_value slot is past
1260 `size' because it needs to be marked specially in case
1261 the table is weak. */
1262 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1263 print_preprocess (h->key_and_value);
1265 break;
1267 default:
1268 break;
1271 print_depth--;
1274 DEFUN ("print--preprocess", Fprint_preprocess, Sprint_preprocess, 1, 1, 0,
1275 doc: /* Extract sharing info from OBJECT needed to print it.
1276 Fills `print-number-table'. */)
1277 (Lisp_Object object)
1279 print_number_index = 0;
1280 print_preprocess (object);
1281 return Qnil;
1284 static void
1285 print_preprocess_string (INTERVAL interval, void *arg)
1287 print_preprocess (interval->plist);
1290 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1292 #define PRINT_STRING_NON_CHARSET_FOUND 1
1293 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1295 /* Bitwise or of the above macros. */
1296 static int print_check_string_result;
1298 static void
1299 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1301 Lisp_Object val;
1303 if (NILP (interval->plist)
1304 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1305 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1306 return;
1307 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1308 val = XCDR (XCDR (val)));
1309 if (! CONSP (val))
1311 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1312 return;
1314 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1316 if (! EQ (val, interval->plist)
1317 || CONSP (XCDR (XCDR (val))))
1318 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1320 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1322 int i, c;
1323 ptrdiff_t charpos = interval->position;
1324 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1325 Lisp_Object charset;
1327 charset = XCAR (XCDR (val));
1328 for (i = 0; i < LENGTH (interval); i++)
1330 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1331 if (! ASCII_CHAR_P (c)
1332 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1334 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1335 break;
1341 /* The value is (charset . nil). */
1342 static Lisp_Object print_prune_charset_plist;
1344 static Lisp_Object
1345 print_prune_string_charset (Lisp_Object string)
1347 print_check_string_result = 0;
1348 traverse_intervals (string_intervals (string), 0,
1349 print_check_string_charset_prop, string);
1350 if (NILP (Vprint_charset_text_property)
1351 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1353 string = Fcopy_sequence (string);
1354 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1356 if (NILP (print_prune_charset_plist))
1357 print_prune_charset_plist = list1 (Qcharset);
1358 Fremove_text_properties (make_number (0),
1359 make_number (SCHARS (string)),
1360 print_prune_charset_plist, string);
1362 else
1363 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1364 Qnil, string);
1366 return string;
1369 static bool
1370 print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
1371 char *buf)
1373 switch (PSEUDOVECTOR_TYPE (XVECTOR (obj)))
1375 case PVEC_PROCESS:
1376 if (escapeflag)
1378 print_c_string ("#<process ", printcharfun);
1379 print_string (XPROCESS (obj)->name, printcharfun);
1380 printchar ('>', printcharfun);
1382 else
1383 print_string (XPROCESS (obj)->name, printcharfun);
1384 break;
1386 case PVEC_BOOL_VECTOR:
1388 EMACS_INT size = bool_vector_size (obj);
1389 ptrdiff_t size_in_bytes = bool_vector_bytes (size);
1390 ptrdiff_t real_size_in_bytes = size_in_bytes;
1391 unsigned char *data = bool_vector_uchar_data (obj);
1393 int len = sprintf (buf, "#&%"pI"d\"", size);
1394 strout (buf, len, len, printcharfun);
1396 /* Don't print more bytes than the specified maximum.
1397 Negative values of print-length are invalid. Treat them
1398 like a print-length of nil. */
1399 if (NATNUMP (Vprint_length)
1400 && XFASTINT (Vprint_length) < size_in_bytes)
1401 size_in_bytes = XFASTINT (Vprint_length);
1403 for (ptrdiff_t i = 0; i < size_in_bytes; i++)
1405 maybe_quit ();
1406 unsigned char c = data[i];
1407 if (c == '\n' && print_escape_newlines)
1408 print_c_string ("\\n", printcharfun);
1409 else if (c == '\f' && print_escape_newlines)
1410 print_c_string ("\\f", printcharfun);
1411 else if (c > '\177'
1412 || (print_escape_control_characters && c_iscntrl (c)))
1414 /* Use octal escapes to avoid encoding issues. */
1415 octalout (c, data, i + 1, size_in_bytes, printcharfun);
1417 else
1419 if (c == '\"' || c == '\\')
1420 printchar ('\\', printcharfun);
1421 printchar (c, printcharfun);
1425 if (size_in_bytes < real_size_in_bytes)
1426 print_c_string (" ...", printcharfun);
1427 printchar ('\"', printcharfun);
1429 break;
1431 case PVEC_SUBR:
1432 print_c_string ("#<subr ", printcharfun);
1433 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1434 printchar ('>', printcharfun);
1435 break;
1437 case PVEC_XWIDGET: case PVEC_XWIDGET_VIEW:
1438 print_c_string ("#<xwidget ", printcharfun);
1439 printchar ('>', printcharfun);
1440 break;
1442 case PVEC_WINDOW:
1444 int len = sprintf (buf, "#<window %"pI"d",
1445 XWINDOW (obj)->sequence_number);
1446 strout (buf, len, len, printcharfun);
1447 if (BUFFERP (XWINDOW (obj)->contents))
1449 print_c_string (" on ", printcharfun);
1450 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1451 printcharfun);
1453 printchar ('>', printcharfun);
1455 break;
1457 case PVEC_TERMINAL:
1459 struct terminal *t = XTERMINAL (obj);
1460 int len = sprintf (buf, "#<terminal %d", t->id);
1461 strout (buf, len, len, printcharfun);
1462 if (t->name)
1464 print_c_string (" on ", printcharfun);
1465 print_c_string (t->name, printcharfun);
1467 printchar ('>', printcharfun);
1469 break;
1471 case PVEC_HASH_TABLE:
1473 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1474 /* Implement a readable output, e.g.:
1475 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1476 /* Always print the size. */
1477 int len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1478 strout (buf, len, len, printcharfun);
1480 if (!NILP (h->test.name))
1482 print_c_string (" test ", printcharfun);
1483 print_object (h->test.name, printcharfun, escapeflag);
1486 if (!NILP (h->weak))
1488 print_c_string (" weakness ", printcharfun);
1489 print_object (h->weak, printcharfun, escapeflag);
1492 print_c_string (" rehash-size ", printcharfun);
1493 print_object (Fhash_table_rehash_size (obj),
1494 printcharfun, escapeflag);
1496 print_c_string (" rehash-threshold ", printcharfun);
1497 print_object (Fhash_table_rehash_threshold (obj),
1498 printcharfun, escapeflag);
1500 if (h->pure)
1502 print_c_string (" purecopy ", printcharfun);
1503 print_object (h->pure ? Qt : Qnil, printcharfun, escapeflag);
1506 print_c_string (" data ", printcharfun);
1508 /* Print the data here as a plist. */
1509 ptrdiff_t real_size = HASH_TABLE_SIZE (h);
1510 ptrdiff_t size = real_size;
1512 /* Don't print more elements than the specified maximum. */
1513 if (NATNUMP (Vprint_length) && XFASTINT (Vprint_length) < size)
1514 size = XFASTINT (Vprint_length);
1516 printchar ('(', printcharfun);
1517 for (ptrdiff_t i = 0; i < size; i++)
1518 if (!NILP (HASH_HASH (h, i)))
1520 if (i) printchar (' ', printcharfun);
1521 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1522 printchar (' ', printcharfun);
1523 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1526 if (size < real_size)
1527 print_c_string (" ...", printcharfun);
1529 print_c_string ("))", printcharfun);
1531 break;
1533 case PVEC_BUFFER:
1534 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1535 print_c_string ("#<killed buffer>", printcharfun);
1536 else if (escapeflag)
1538 print_c_string ("#<buffer ", printcharfun);
1539 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1540 printchar ('>', printcharfun);
1542 else
1543 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1544 break;
1546 case PVEC_WINDOW_CONFIGURATION:
1547 print_c_string ("#<window-configuration>", printcharfun);
1548 break;
1550 case PVEC_FRAME:
1552 void *ptr = XFRAME (obj);
1553 Lisp_Object frame_name = XFRAME (obj)->name;
1555 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1556 ? "#<frame "
1557 : "#<dead frame "),
1558 printcharfun);
1559 if (!STRINGP (frame_name))
1561 /* A frame could be too young and have no name yet;
1562 don't crash. */
1563 if (SYMBOLP (frame_name))
1564 frame_name = Fsymbol_name (frame_name);
1565 else /* can't happen: name should be either nil or string */
1566 frame_name = build_string ("*INVALID*FRAME*NAME*");
1568 print_string (frame_name, printcharfun);
1569 int len = sprintf (buf, " %p>", ptr);
1570 strout (buf, len, len, printcharfun);
1572 break;
1574 case PVEC_FONT:
1576 if (! FONT_OBJECT_P (obj))
1578 if (FONT_SPEC_P (obj))
1579 print_c_string ("#<font-spec", printcharfun);
1580 else
1581 print_c_string ("#<font-entity", printcharfun);
1582 for (int i = 0; i < FONT_SPEC_MAX; i++)
1584 printchar (' ', printcharfun);
1585 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1586 print_object (AREF (obj, i), printcharfun, escapeflag);
1587 else
1588 print_object (font_style_symbolic (obj, i, 0),
1589 printcharfun, escapeflag);
1592 else
1594 print_c_string ("#<font-object ", printcharfun);
1595 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1596 escapeflag);
1598 printchar ('>', printcharfun);
1600 break;
1602 case PVEC_THREAD:
1603 print_c_string ("#<thread ", printcharfun);
1604 if (STRINGP (XTHREAD (obj)->name))
1605 print_string (XTHREAD (obj)->name, printcharfun);
1606 else
1608 int len = sprintf (buf, "%p", XTHREAD (obj));
1609 strout (buf, len, len, printcharfun);
1611 printchar ('>', printcharfun);
1612 break;
1614 case PVEC_MUTEX:
1615 print_c_string ("#<mutex ", printcharfun);
1616 if (STRINGP (XMUTEX (obj)->name))
1617 print_string (XMUTEX (obj)->name, printcharfun);
1618 else
1620 int len = sprintf (buf, "%p", XMUTEX (obj));
1621 strout (buf, len, len, printcharfun);
1623 printchar ('>', printcharfun);
1624 break;
1626 case PVEC_CONDVAR:
1627 print_c_string ("#<condvar ", printcharfun);
1628 if (STRINGP (XCONDVAR (obj)->name))
1629 print_string (XCONDVAR (obj)->name, printcharfun);
1630 else
1632 int len = sprintf (buf, "%p", XCONDVAR (obj));
1633 strout (buf, len, len, printcharfun);
1635 printchar ('>', printcharfun);
1636 break;
1638 case PVEC_RECORD:
1640 ptrdiff_t size = PVSIZE (obj);
1642 /* Don't print more elements than the specified maximum. */
1643 ptrdiff_t n
1644 = (NATNUMP (Vprint_length) && XFASTINT (Vprint_length) < size
1645 ? XFASTINT (Vprint_length) : size);
1647 print_c_string ("#s(", printcharfun);
1648 for (ptrdiff_t i = 0; i < n; i ++)
1650 if (i) printchar (' ', printcharfun);
1651 print_object (AREF (obj, i), printcharfun, escapeflag);
1653 if (n < size)
1654 print_c_string (" ...", printcharfun);
1655 printchar (')', printcharfun);
1657 break;
1659 case PVEC_SUB_CHAR_TABLE:
1660 case PVEC_COMPILED:
1661 case PVEC_CHAR_TABLE:
1662 case PVEC_NORMAL_VECTOR:
1664 ptrdiff_t size = ASIZE (obj);
1665 if (COMPILEDP (obj))
1667 printchar ('#', printcharfun);
1668 size &= PSEUDOVECTOR_SIZE_MASK;
1670 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1672 /* Print a char-table as if it were a vector,
1673 lumping the parent and default slots in with the
1674 character slots. But add #^ as a prefix. */
1676 /* Make each lowest sub_char_table start a new line.
1677 Otherwise we'll make a line extremely long, which
1678 results in slow redisplay. */
1679 if (SUB_CHAR_TABLE_P (obj)
1680 && XSUB_CHAR_TABLE (obj)->depth == 3)
1681 printchar ('\n', printcharfun);
1682 print_c_string ("#^", printcharfun);
1683 if (SUB_CHAR_TABLE_P (obj))
1684 printchar ('^', printcharfun);
1685 size &= PSEUDOVECTOR_SIZE_MASK;
1687 if (size & PSEUDOVECTOR_FLAG)
1688 return false;
1690 printchar ('[', printcharfun);
1692 int idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1693 Lisp_Object tem;
1694 ptrdiff_t real_size = size;
1696 /* For a sub char-table, print heading non-Lisp data first. */
1697 if (SUB_CHAR_TABLE_P (obj))
1699 int i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1700 XSUB_CHAR_TABLE (obj)->min_char);
1701 strout (buf, i, i, printcharfun);
1704 /* Don't print more elements than the specified maximum. */
1705 if (NATNUMP (Vprint_length)
1706 && XFASTINT (Vprint_length) < size)
1707 size = XFASTINT (Vprint_length);
1709 for (int i = idx; i < size; i++)
1711 if (i) printchar (' ', printcharfun);
1712 tem = AREF (obj, i);
1713 print_object (tem, printcharfun, escapeflag);
1715 if (size < real_size)
1716 print_c_string (" ...", printcharfun);
1717 printchar (']', printcharfun);
1719 break;
1721 #ifdef HAVE_MODULES
1722 case PVEC_MODULE_FUNCTION:
1724 print_c_string ("#<module function ", printcharfun);
1725 void *ptr = XMODULE_FUNCTION (obj)->subr;
1726 const char *file = NULL;
1727 const char *symbol = NULL;
1728 dynlib_addr (ptr, &file, &symbol);
1730 if (symbol == NULL)
1732 print_c_string ("at ", printcharfun);
1733 enum { pointer_bufsize = sizeof ptr * 16 / CHAR_BIT + 2 + 1 };
1734 char buffer[pointer_bufsize];
1735 int needed = snprintf (buffer, sizeof buffer, "%p", ptr);
1736 const char p0x[] = "0x";
1737 eassert (needed <= sizeof buffer);
1738 /* ANSI C doesn't guarantee that %p produces a string that
1739 begins with a "0x". */
1740 if (c_strncasecmp (buffer, p0x, sizeof (p0x) - 1) != 0)
1741 print_c_string (p0x, printcharfun);
1742 print_c_string (buffer, printcharfun);
1744 else
1745 print_c_string (symbol, printcharfun);
1747 if (file != NULL)
1749 print_c_string (" from ", printcharfun);
1750 print_c_string (file, printcharfun);
1753 printchar ('>', printcharfun);
1755 break;
1756 #endif
1758 default:
1759 emacs_abort ();
1762 return true;
1765 static void
1766 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1768 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1769 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1770 40))];
1771 current_thread->stack_top = buf;
1772 maybe_quit ();
1774 /* Detect circularities and truncate them. */
1775 if (NILP (Vprint_circle))
1777 /* Simple but incomplete way. */
1778 int i;
1780 /* See similar code in print_preprocess. */
1781 if (print_depth >= PRINT_CIRCLE)
1782 error ("Apparently circular structure being printed");
1784 for (i = 0; i < print_depth; i++)
1785 if (EQ (obj, being_printed[i]))
1787 int len = sprintf (buf, "#%d", i);
1788 strout (buf, len, len, printcharfun);
1789 return;
1791 being_printed[print_depth] = obj;
1793 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1795 /* With the print-circle feature. */
1796 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1797 if (INTEGERP (num))
1799 EMACS_INT n = XINT (num);
1800 if (n < 0)
1801 { /* Add a prefix #n= if OBJ has not yet been printed;
1802 that is, its status field is nil. */
1803 int len = sprintf (buf, "#%"pI"d=", -n);
1804 strout (buf, len, len, printcharfun);
1805 /* OBJ is going to be printed. Remember that fact. */
1806 Fputhash (obj, make_number (- n), Vprint_number_table);
1808 else
1810 /* Just print #n# if OBJ has already been printed. */
1811 int len = sprintf (buf, "#%"pI"d#", n);
1812 strout (buf, len, len, printcharfun);
1813 return;
1818 print_depth++;
1820 switch (XTYPE (obj))
1822 case_Lisp_Int:
1824 int len = sprintf (buf, "%"pI"d", XINT (obj));
1825 strout (buf, len, len, printcharfun);
1827 break;
1829 case Lisp_Float:
1831 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1832 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1833 strout (pigbuf, len, len, printcharfun);
1835 break;
1837 case Lisp_String:
1838 if (!escapeflag)
1839 print_string (obj, printcharfun);
1840 else
1842 ptrdiff_t i, i_byte;
1843 ptrdiff_t size_byte;
1844 /* True means we must ensure that the next character we output
1845 cannot be taken as part of a hex character escape. */
1846 bool need_nonhex = false;
1847 bool multibyte = STRING_MULTIBYTE (obj);
1849 if (! EQ (Vprint_charset_text_property, Qt))
1850 obj = print_prune_string_charset (obj);
1852 if (string_intervals (obj))
1853 print_c_string ("#(", printcharfun);
1855 printchar ('\"', printcharfun);
1856 size_byte = SBYTES (obj);
1858 for (i = 0, i_byte = 0; i_byte < size_byte;)
1860 /* Here, we must convert each multi-byte form to the
1861 corresponding character code before handing it to printchar. */
1862 int c;
1864 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1866 maybe_quit ();
1868 if (multibyte
1869 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1870 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1871 && print_escape_nonascii))
1873 /* When printing a raw 8-bit byte in a multibyte buffer, or
1874 (when requested) a non-ASCII character in a unibyte buffer,
1875 print single-byte non-ASCII string chars
1876 using octal escapes. */
1877 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1878 need_nonhex = false;
1880 else if (multibyte
1881 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1883 /* When requested, print multibyte chars using hex escapes. */
1884 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1885 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1886 strout (outbuf, len, len, printcharfun);
1887 need_nonhex = true;
1889 else
1891 /* If we just had a hex escape, and this character
1892 could be taken as part of it,
1893 output `\ ' to prevent that. */
1894 if (c_isxdigit (c))
1896 if (need_nonhex)
1897 print_c_string ("\\ ", printcharfun);
1898 printchar (c, printcharfun);
1900 else if (c == '\n' && print_escape_newlines
1901 ? (c = 'n', true)
1902 : c == '\f' && print_escape_newlines
1903 ? (c = 'f', true)
1904 : c == '\"' || c == '\\')
1906 printchar ('\\', printcharfun);
1907 printchar (c, printcharfun);
1909 else if (print_escape_control_characters && c_iscntrl (c))
1910 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1911 else
1912 printchar (c, printcharfun);
1913 need_nonhex = false;
1916 printchar ('\"', printcharfun);
1918 if (string_intervals (obj))
1920 traverse_intervals (string_intervals (obj),
1921 0, print_interval, printcharfun);
1922 printchar (')', printcharfun);
1925 break;
1927 case Lisp_Symbol:
1929 bool confusing;
1930 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1931 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1932 int c;
1933 ptrdiff_t i, i_byte;
1934 ptrdiff_t size_byte;
1935 Lisp_Object name;
1937 name = SYMBOL_NAME (obj);
1939 if (p != end && (*p == '-' || *p == '+')) p++;
1940 if (p == end)
1941 confusing = 0;
1942 /* If symbol name begins with a digit, and ends with a digit,
1943 and contains nothing but digits and `e', it could be treated
1944 as a number. So set CONFUSING.
1946 Symbols that contain periods could also be taken as numbers,
1947 but periods are always escaped, so we don't have to worry
1948 about them here. */
1949 else if (*p >= '0' && *p <= '9'
1950 && end[-1] >= '0' && end[-1] <= '9')
1952 while (p != end && ((*p >= '0' && *p <= '9')
1953 /* Needed for \2e10. */
1954 || *p == 'e' || *p == 'E'))
1955 p++;
1956 confusing = (end == p);
1958 else
1959 confusing = 0;
1961 size_byte = SBYTES (name);
1963 if (! NILP (Vprint_gensym)
1964 && !SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (obj))
1965 print_c_string ("#:", printcharfun);
1966 else if (size_byte == 0)
1968 print_c_string ("##", printcharfun);
1969 break;
1972 for (i = 0, i_byte = 0; i_byte < size_byte;)
1974 /* Here, we must convert each multi-byte form to the
1975 corresponding character code before handing it to PRINTCHAR. */
1976 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1977 maybe_quit ();
1979 if (escapeflag)
1981 if (c == '\"' || c == '\\' || c == '\''
1982 || c == ';' || c == '#' || c == '(' || c == ')'
1983 || c == ',' || c == '.' || c == '`'
1984 || c == '[' || c == ']' || c == '?' || c <= 040
1985 || confusing
1986 || (i == 1 && confusable_symbol_character_p (c)))
1988 printchar ('\\', printcharfun);
1989 confusing = false;
1992 printchar (c, printcharfun);
1995 break;
1997 case Lisp_Cons:
1998 /* If deeper than spec'd depth, print placeholder. */
1999 if (INTEGERP (Vprint_level)
2000 && print_depth > XINT (Vprint_level))
2001 print_c_string ("...", printcharfun);
2002 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2003 && EQ (XCAR (obj), Qquote))
2005 printchar ('\'', printcharfun);
2006 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2008 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2009 && EQ (XCAR (obj), Qfunction))
2011 print_c_string ("#'", printcharfun);
2012 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2014 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2015 && EQ (XCAR (obj), Qbackquote))
2017 printchar ('`', printcharfun);
2018 new_backquote_output++;
2019 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2020 new_backquote_output--;
2022 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2023 && new_backquote_output
2024 && (EQ (XCAR (obj), Qcomma)
2025 || EQ (XCAR (obj), Qcomma_at)
2026 || EQ (XCAR (obj), Qcomma_dot)))
2028 print_object (XCAR (obj), printcharfun, false);
2029 new_backquote_output--;
2030 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2031 new_backquote_output++;
2033 else
2035 printchar ('(', printcharfun);
2037 Lisp_Object halftail = obj;
2039 /* Negative values of print-length are invalid in CL.
2040 Treat them like nil, as CMUCL does. */
2041 printmax_t print_length = (NATNUMP (Vprint_length)
2042 ? XFASTINT (Vprint_length)
2043 : TYPE_MAXIMUM (printmax_t));
2045 printmax_t i = 0;
2046 while (CONSP (obj))
2048 /* Detect circular list. */
2049 if (NILP (Vprint_circle))
2051 /* Simple but incomplete way. */
2052 if (i != 0 && EQ (obj, halftail))
2054 int len = sprintf (buf, " . #%"pMd, i / 2);
2055 strout (buf, len, len, printcharfun);
2056 goto end_of_list;
2059 else
2061 /* With the print-circle feature. */
2062 if (i != 0)
2064 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
2065 if (INTEGERP (num))
2067 print_c_string (" . ", printcharfun);
2068 print_object (obj, printcharfun, escapeflag);
2069 goto end_of_list;
2074 if (i)
2075 printchar (' ', printcharfun);
2077 if (print_length <= i)
2079 print_c_string ("...", printcharfun);
2080 goto end_of_list;
2083 i++;
2084 print_object (XCAR (obj), printcharfun, escapeflag);
2086 obj = XCDR (obj);
2087 if (!(i & 1))
2088 halftail = XCDR (halftail);
2091 /* OBJ non-nil here means it's the end of a dotted list. */
2092 if (!NILP (obj))
2094 print_c_string (" . ", printcharfun);
2095 print_object (obj, printcharfun, escapeflag);
2098 end_of_list:
2099 printchar (')', printcharfun);
2101 break;
2103 case Lisp_Vectorlike:
2104 if (! print_vectorlike (obj, printcharfun, escapeflag, buf))
2105 goto badtype;
2106 break;
2108 case Lisp_Misc:
2109 switch (XMISCTYPE (obj))
2111 case Lisp_Misc_Marker:
2112 print_c_string ("#<marker ", printcharfun);
2113 /* Do you think this is necessary? */
2114 if (XMARKER (obj)->insertion_type != 0)
2115 print_c_string ("(moves after insertion) ", printcharfun);
2116 if (! XMARKER (obj)->buffer)
2117 print_c_string ("in no buffer", printcharfun);
2118 else
2120 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
2121 strout (buf, len, len, printcharfun);
2122 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2124 printchar ('>', printcharfun);
2125 break;
2127 case Lisp_Misc_Overlay:
2128 print_c_string ("#<overlay ", printcharfun);
2129 if (! XMARKER (OVERLAY_START (obj))->buffer)
2130 print_c_string ("in no buffer", printcharfun);
2131 else
2133 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2134 marker_position (OVERLAY_START (obj)),
2135 marker_position (OVERLAY_END (obj)));
2136 strout (buf, len, len, printcharfun);
2137 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2138 printcharfun);
2140 printchar ('>', printcharfun);
2141 break;
2143 #ifdef HAVE_MODULES
2144 case Lisp_Misc_User_Ptr:
2146 print_c_string ("#<user-ptr ", printcharfun);
2147 int i = sprintf (buf, "ptr=%p finalizer=%p",
2148 XUSER_PTR (obj)->p,
2149 XUSER_PTR (obj)->finalizer);
2150 strout (buf, i, i, printcharfun);
2151 printchar ('>', printcharfun);
2152 break;
2154 #endif
2156 case Lisp_Misc_Finalizer:
2157 print_c_string ("#<finalizer", printcharfun);
2158 if (NILP (XFINALIZER (obj)->function))
2159 print_c_string (" used", printcharfun);
2160 printchar ('>', printcharfun);
2161 break;
2163 /* Remaining cases shouldn't happen in normal usage, but let's
2164 print them anyway for the benefit of the debugger. */
2166 case Lisp_Misc_Free:
2167 print_c_string ("#<misc free cell>", printcharfun);
2168 break;
2170 case Lisp_Misc_Save_Value:
2172 int i;
2173 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2175 print_c_string ("#<save-value ", printcharfun);
2177 if (v->save_type == SAVE_TYPE_MEMORY)
2179 ptrdiff_t amount = v->data[1].integer;
2181 /* valid_lisp_object_p is reliable, so try to print up
2182 to 8 saved objects. This code is rarely used, so
2183 it's OK that valid_lisp_object_p is slow. */
2185 int limit = min (amount, 8);
2186 Lisp_Object *area = v->data[0].pointer;
2188 i = sprintf (buf, "with %"pD"d objects", amount);
2189 strout (buf, i, i, printcharfun);
2191 for (i = 0; i < limit; i++)
2193 Lisp_Object maybe = area[i];
2194 int valid = valid_lisp_object_p (maybe);
2196 printchar (' ', printcharfun);
2197 if (0 < valid)
2198 print_object (maybe, printcharfun, escapeflag);
2199 else
2200 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2201 printcharfun);
2203 if (i == limit && i < amount)
2204 print_c_string (" ...", printcharfun);
2206 else
2208 /* Print each slot according to its type. */
2209 int index;
2210 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2212 if (index)
2213 printchar (' ', printcharfun);
2215 switch (save_type (v, index))
2217 case SAVE_UNUSED:
2218 i = sprintf (buf, "<unused>");
2219 break;
2221 case SAVE_POINTER:
2222 i = sprintf (buf, "<pointer %p>",
2223 v->data[index].pointer);
2224 break;
2226 case SAVE_FUNCPOINTER:
2227 i = sprintf (buf, "<funcpointer %p>",
2228 ((void *) (intptr_t)
2229 v->data[index].funcpointer));
2230 break;
2232 case SAVE_INTEGER:
2233 i = sprintf (buf, "<integer %"pD"d>",
2234 v->data[index].integer);
2235 break;
2237 case SAVE_OBJECT:
2238 print_object (v->data[index].object, printcharfun,
2239 escapeflag);
2240 continue;
2242 default:
2243 emacs_abort ();
2246 strout (buf, i, i, printcharfun);
2249 printchar ('>', printcharfun);
2251 break;
2253 default:
2254 goto badtype;
2256 break;
2258 default:
2259 badtype:
2261 int len;
2262 /* We're in trouble if this happens!
2263 Probably should just emacs_abort (). */
2264 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2265 if (MISCP (obj))
2266 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2267 else if (VECTORLIKEP (obj))
2268 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2269 else
2270 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2271 strout (buf, len, len, printcharfun);
2272 print_c_string ((" Save your buffers immediately"
2273 " and please report this bug>"),
2274 printcharfun);
2278 print_depth--;
2282 /* Print a description of INTERVAL using PRINTCHARFUN.
2283 This is part of printing a string that has text properties. */
2285 static void
2286 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2288 if (NILP (interval->plist))
2289 return;
2290 printchar (' ', printcharfun);
2291 print_object (make_number (interval->position), printcharfun, 1);
2292 printchar (' ', printcharfun);
2293 print_object (make_number (interval->position + LENGTH (interval)),
2294 printcharfun, 1);
2295 printchar (' ', printcharfun);
2296 print_object (interval->plist, printcharfun, 1);
2299 /* Initialize debug_print stuff early to have it working from the very
2300 beginning. */
2302 void
2303 init_print_once (void)
2305 /* The subroutine object for external-debugging-output is kept here
2306 for the convenience of the debugger. */
2307 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2309 defsubr (&Sexternal_debugging_output);
2312 void
2313 syms_of_print (void)
2315 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2317 DEFVAR_LISP ("standard-output", Vstandard_output,
2318 doc: /* Output stream `print' uses by default for outputting a character.
2319 This may be any function of one argument.
2320 It may also be a buffer (output is inserted before point)
2321 or a marker (output is inserted and the marker is advanced)
2322 or the symbol t (output appears in the echo area). */);
2323 Vstandard_output = Qt;
2324 DEFSYM (Qstandard_output, "standard-output");
2326 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2327 doc: /* The format descriptor string used to print floats.
2328 This is a %-spec like those accepted by `printf' in C,
2329 but with some restrictions. It must start with the two characters `%.'.
2330 After that comes an integer precision specification,
2331 and then a letter which controls the format.
2332 The letters allowed are `e', `f' and `g'.
2333 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2334 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2335 Use `g' to choose the shorter of those two formats for the number at hand.
2336 The precision in any of these cases is the number of digits following
2337 the decimal point. With `f', a precision of 0 means to omit the
2338 decimal point. 0 is not allowed with `e' or `g'.
2340 A value of nil means to use the shortest notation
2341 that represents the number without losing information. */);
2342 Vfloat_output_format = Qnil;
2344 DEFVAR_LISP ("print-length", Vprint_length,
2345 doc: /* Maximum length of list to print before abbreviating.
2346 A value of nil means no limit. See also `eval-expression-print-length'. */);
2347 Vprint_length = Qnil;
2349 DEFVAR_LISP ("print-level", Vprint_level,
2350 doc: /* Maximum depth of list nesting to print before abbreviating.
2351 A value of nil means no limit. See also `eval-expression-print-level'. */);
2352 Vprint_level = Qnil;
2354 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2355 doc: /* Non-nil means print newlines in strings as `\\n'.
2356 Also print formfeeds as `\\f'. */);
2357 print_escape_newlines = 0;
2359 DEFVAR_BOOL ("print-escape-control-characters", print_escape_control_characters,
2360 doc: /* Non-nil means print control characters in strings as `\\OOO'.
2361 \(OOO is the octal representation of the character code.)*/);
2362 print_escape_control_characters = 0;
2364 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2365 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2366 \(OOO is the octal representation of the character code.)
2367 Only single-byte characters are affected, and only in `prin1'.
2368 When the output goes in a multibyte buffer, this feature is
2369 enabled regardless of the value of the variable. */);
2370 print_escape_nonascii = 0;
2372 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2373 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2374 \(XXXX is the hex representation of the character code.)
2375 This affects only `prin1'. */);
2376 print_escape_multibyte = 0;
2378 DEFVAR_BOOL ("print-quoted", print_quoted,
2379 doc: /* Non-nil means print quoted forms with reader syntax.
2380 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2381 print_quoted = true;
2383 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2384 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2385 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2386 When the uninterned symbol appears multiple times within the printed
2387 expression, and `print-circle' is non-nil, in addition use the #N#
2388 and #N= constructs as needed, so that multiple references to the same
2389 symbol are shared once again when the text is read back. */);
2390 Vprint_gensym = Qnil;
2392 DEFVAR_LISP ("print-circle", Vprint_circle,
2393 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2394 If nil, printing proceeds recursively and may lead to
2395 `max-lisp-eval-depth' being exceeded or an error may occur:
2396 \"Apparently circular structure being printed.\" Also see
2397 `print-length' and `print-level'.
2398 If non-nil, shared substructures anywhere in the structure are printed
2399 with `#N=' before the first occurrence (in the order of the print
2400 representation) and `#N#' in place of each subsequent occurrence,
2401 where N is a positive decimal integer. */);
2402 Vprint_circle = Qnil;
2404 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2405 doc: /* Non-nil means number continuously across print calls.
2406 This affects the numbers printed for #N= labels and #M# references.
2407 See also `print-circle', `print-gensym', and `print-number-table'.
2408 This variable should not be set with `setq'; bind it with a `let' instead. */);
2409 Vprint_continuous_numbering = Qnil;
2411 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2412 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2413 The Lisp printer uses this vector to detect Lisp objects referenced more
2414 than once.
2416 When you bind `print-continuous-numbering' to t, you should probably
2417 also bind `print-number-table' to nil. This ensures that the value of
2418 `print-number-table' can be garbage-collected once the printing is
2419 done. If all elements of `print-number-table' are nil, it means that
2420 the printing done so far has not found any shared structure or objects
2421 that need to be recorded in the table. */);
2422 Vprint_number_table = Qnil;
2424 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2425 doc: /* A flag to control printing of `charset' text property on printing a string.
2426 The value should be nil, t, or `default'.
2428 If the value is nil, don't print the text property `charset'.
2430 If the value is t, always print the text property `charset'.
2432 If the value is `default', print the text property `charset' only when
2433 the value is different from what is guessed in the current charset
2434 priorities. Values other than nil or t are also treated as
2435 `default'. */);
2436 Vprint_charset_text_property = Qdefault;
2438 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2439 staticpro (&Vprin1_to_string_buffer);
2441 defsubr (&Sprin1);
2442 defsubr (&Sprin1_to_string);
2443 defsubr (&Serror_message_string);
2444 defsubr (&Sprinc);
2445 defsubr (&Sprint);
2446 defsubr (&Sterpri);
2447 defsubr (&Swrite_char);
2448 defsubr (&Sredirect_debugging_output);
2449 defsubr (&Sprint_preprocess);
2451 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2452 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2454 print_prune_charset_plist = Qnil;
2455 staticpro (&print_prune_charset_plist);