; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / src / print.c
blob71591952a23a20db72e7e2e7af53ae1d106ab0eb
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",
858 /* This function formats the given object and returns the result as a
859 string. Use this in contexts where you can inspect strings, but
860 where stderr output won't work --- e.g., while replaying rr
861 recordings. */
862 const char * debug_format (const char *, Lisp_Object) EXTERNALLY_VISIBLE;
863 const char *
864 debug_format (const char *fmt, Lisp_Object arg)
866 return SSDATA (CALLN (Fformat, build_string (fmt), arg));
870 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
871 1, 1, 0,
872 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
873 See Info anchor `(elisp)Definition of signal' for some details on how this
874 error message is constructed. */)
875 (Lisp_Object obj)
877 struct buffer *old = current_buffer;
878 Lisp_Object value;
880 /* If OBJ is (error STRING), just return STRING.
881 That is not only faster, it also avoids the need to allocate
882 space here when the error is due to memory full. */
883 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
884 && CONSP (XCDR (obj))
885 && STRINGP (XCAR (XCDR (obj)))
886 && NILP (XCDR (XCDR (obj))))
887 return XCAR (XCDR (obj));
889 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
891 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
892 value = Fbuffer_string ();
894 Ferase_buffer ();
895 set_buffer_internal (old);
897 return value;
900 /* Print an error message for the error DATA onto Lisp output stream
901 STREAM (suitable for the print functions).
902 CONTEXT is a C string describing the context of the error.
903 CALLER is the Lisp function inside which the error was signaled. */
905 void
906 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
907 Lisp_Object caller)
909 Lisp_Object errname, errmsg, file_error, tail;
911 if (context != 0)
912 write_string (context, stream);
914 /* If we know from where the error was signaled, show it in
915 *Messages*. */
916 if (!NILP (caller) && SYMBOLP (caller))
918 Lisp_Object cname = SYMBOL_NAME (caller);
919 ptrdiff_t cnamelen = SBYTES (cname);
920 USE_SAFE_ALLOCA;
921 char *name = SAFE_ALLOCA (cnamelen);
922 memcpy (name, SDATA (cname), cnamelen);
923 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
924 message_dolog (": ", 2, 0, 0);
925 SAFE_FREE ();
928 errname = Fcar (data);
930 if (EQ (errname, Qerror))
932 data = Fcdr (data);
933 if (!CONSP (data))
934 data = Qnil;
935 errmsg = Fcar (data);
936 file_error = Qnil;
938 else
940 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
941 errmsg = Fsubstitute_command_keys (Fget (errname, Qerror_message));
942 file_error = Fmemq (Qfile_error, error_conditions);
945 /* Print an error message including the data items. */
947 tail = Fcdr_safe (data);
949 /* For file-error, make error message by concatenating
950 all the data items. They are all strings. */
951 if (!NILP (file_error) && CONSP (tail))
952 errmsg = XCAR (tail), tail = XCDR (tail);
955 const char *sep = ": ";
957 if (!STRINGP (errmsg))
958 write_string ("peculiar error", stream);
959 else if (SCHARS (errmsg))
960 Fprinc (errmsg, stream);
961 else
962 sep = NULL;
964 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
966 Lisp_Object obj;
968 if (sep)
969 write_string (sep, stream);
970 obj = XCAR (tail);
971 if (!NILP (file_error)
972 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
973 Fprinc (obj, stream);
974 else
975 Fprin1 (obj, stream);
983 * The buffer should be at least as large as the max string size of the
984 * largest float, printed in the biggest notation. This is undoubtedly
985 * 20d float_output_format, with the negative of the C-constant "HUGE"
986 * from <math.h>.
988 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
990 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
991 * case of -1e307 in 20d float_output_format. What is one to do (short of
992 * re-writing _doprnt to be more sane)?
993 * -wsr
994 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
998 float_to_string (char *buf, double data)
1000 char *cp;
1001 int width;
1002 int len;
1004 /* Check for plus infinity in a way that won't lose
1005 if there is no plus infinity. */
1006 if (data == data / 2 && data > 1.0)
1008 static char const infinity_string[] = "1.0e+INF";
1009 strcpy (buf, infinity_string);
1010 return sizeof infinity_string - 1;
1012 /* Likewise for minus infinity. */
1013 if (data == data / 2 && data < -1.0)
1015 static char const minus_infinity_string[] = "-1.0e+INF";
1016 strcpy (buf, minus_infinity_string);
1017 return sizeof minus_infinity_string - 1;
1019 /* Check for NaN in a way that won't fail if there are no NaNs. */
1020 if (! (data * 0.0 >= 0.0))
1022 /* Prepend "-" if the NaN's sign bit is negative.
1023 The sign bit of a double is the bit that is 1 in -0.0. */
1024 static char const NaN_string[] = "0.0e+NaN";
1025 int i;
1026 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1027 bool negative = 0;
1028 u_data.d = data;
1029 u_minus_zero.d = - 0.0;
1030 for (i = 0; i < sizeof (double); i++)
1031 if (u_data.c[i] & u_minus_zero.c[i])
1033 *buf = '-';
1034 negative = 1;
1035 break;
1038 strcpy (buf + negative, NaN_string);
1039 return negative + sizeof NaN_string - 1;
1042 if (NILP (Vfloat_output_format)
1043 || !STRINGP (Vfloat_output_format))
1044 lose:
1046 /* Generate the fewest number of digits that represent the
1047 floating point value without losing information. */
1048 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1049 /* The decimal point must be printed, or the byte compiler can
1050 get confused (Bug#8033). */
1051 width = 1;
1053 else /* oink oink */
1055 /* Check that the spec we have is fully valid.
1056 This means not only valid for printf,
1057 but meant for floats, and reasonable. */
1058 cp = SSDATA (Vfloat_output_format);
1060 if (cp[0] != '%')
1061 goto lose;
1062 if (cp[1] != '.')
1063 goto lose;
1065 cp += 2;
1067 /* Check the width specification. */
1068 width = -1;
1069 if ('0' <= *cp && *cp <= '9')
1071 width = 0;
1074 width = (width * 10) + (*cp++ - '0');
1075 if (DBL_DIG < width)
1076 goto lose;
1078 while (*cp >= '0' && *cp <= '9');
1080 /* A precision of zero is valid only for %f. */
1081 if (width == 0 && *cp != 'f')
1082 goto lose;
1085 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1086 goto lose;
1088 if (cp[1] != 0)
1089 goto lose;
1091 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1094 /* Make sure there is a decimal point with digit after, or an
1095 exponent, so that the value is readable as a float. But don't do
1096 this with "%.0f"; it's valid for that not to produce a decimal
1097 point. Note that width can be 0 only for %.0f. */
1098 if (width != 0)
1100 for (cp = buf; *cp; cp++)
1101 if ((*cp < '0' || *cp > '9') && *cp != '-')
1102 break;
1104 if (*cp == '.' && cp[1] == 0)
1106 cp[1] = '0';
1107 cp[2] = 0;
1108 len++;
1110 else if (*cp == 0)
1112 *cp++ = '.';
1113 *cp++ = '0';
1114 *cp++ = 0;
1115 len += 2;
1119 return len;
1123 static void
1124 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1126 new_backquote_output = 0;
1128 /* Reset print_number_index and Vprint_number_table only when
1129 the variable Vprint_continuous_numbering is nil. Otherwise,
1130 the values of these variables will be kept between several
1131 print functions. */
1132 if (NILP (Vprint_continuous_numbering)
1133 || NILP (Vprint_number_table))
1135 print_number_index = 0;
1136 Vprint_number_table = Qnil;
1139 /* Construct Vprint_number_table for print-gensym and print-circle. */
1140 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1142 /* Construct Vprint_number_table.
1143 This increments print_number_index for the objects added. */
1144 print_depth = 0;
1145 print_preprocess (obj);
1147 if (HASH_TABLE_P (Vprint_number_table))
1148 { /* Remove unnecessary objects, which appear only once in OBJ;
1149 that is, whose status is Qt. */
1150 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1151 ptrdiff_t i;
1153 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1154 if (!NILP (HASH_HASH (h, i))
1155 && EQ (HASH_VALUE (h, i), Qt))
1156 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1160 print_depth = 0;
1161 print_object (obj, printcharfun, escapeflag);
1164 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1165 (STRINGP (obj) || CONSP (obj) \
1166 || (VECTORLIKEP (obj) \
1167 && (VECTORP (obj) || COMPILEDP (obj) \
1168 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1169 || HASH_TABLE_P (obj) || FONTP (obj) \
1170 || RECORDP (obj))) \
1171 || (! NILP (Vprint_gensym) \
1172 && SYMBOLP (obj) \
1173 && !SYMBOL_INTERNED_P (obj)))
1175 /* Construct Vprint_number_table according to the structure of OBJ.
1176 OBJ itself and all its elements will be added to Vprint_number_table
1177 recursively if it is a list, vector, compiled function, char-table,
1178 string (its text properties will be traced), or a symbol that has
1179 no obarray (this is for the print-gensym feature).
1180 The status fields of Vprint_number_table mean whether each object appears
1181 more than once in OBJ: Qnil at the first time, and Qt after that. */
1182 static void
1183 print_preprocess (Lisp_Object obj)
1185 int i;
1186 ptrdiff_t size;
1187 int loop_count = 0;
1188 Lisp_Object halftail;
1190 /* Avoid infinite recursion for circular nested structure
1191 in the case where Vprint_circle is nil. */
1192 if (NILP (Vprint_circle))
1194 /* Give up if we go so deep that print_object will get an error. */
1195 /* See similar code in print_object. */
1196 if (print_depth >= PRINT_CIRCLE)
1197 error ("Apparently circular structure being printed");
1199 for (i = 0; i < print_depth; i++)
1200 if (EQ (obj, being_printed[i]))
1201 return;
1202 being_printed[print_depth] = obj;
1205 print_depth++;
1206 halftail = obj;
1208 loop:
1209 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1211 if (!HASH_TABLE_P (Vprint_number_table))
1212 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1214 /* In case print-circle is nil and print-gensym is t,
1215 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1216 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1218 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1219 if (!NILP (num)
1220 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1221 always print the gensym with a number. This is a special for
1222 the lisp function byte-compile-output-docform. */
1223 || (!NILP (Vprint_continuous_numbering)
1224 && SYMBOLP (obj)
1225 && !SYMBOL_INTERNED_P (obj)))
1226 { /* OBJ appears more than once. Let's remember that. */
1227 if (!INTEGERP (num))
1229 print_number_index++;
1230 /* Negative number indicates it hasn't been printed yet. */
1231 Fputhash (obj, make_number (- print_number_index),
1232 Vprint_number_table);
1234 print_depth--;
1235 return;
1237 else
1238 /* OBJ is not yet recorded. Let's add to the table. */
1239 Fputhash (obj, Qt, Vprint_number_table);
1242 switch (XTYPE (obj))
1244 case Lisp_String:
1245 /* A string may have text properties, which can be circular. */
1246 traverse_intervals_noorder (string_intervals (obj),
1247 print_preprocess_string, NULL);
1248 break;
1250 case Lisp_Cons:
1251 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1252 just as in print_object. */
1253 if (loop_count && EQ (obj, halftail))
1254 break;
1255 print_preprocess (XCAR (obj));
1256 obj = XCDR (obj);
1257 loop_count++;
1258 if (!(loop_count & 1))
1259 halftail = XCDR (halftail);
1260 goto loop;
1262 case Lisp_Vectorlike:
1263 size = ASIZE (obj);
1264 if (size & PSEUDOVECTOR_FLAG)
1265 size &= PSEUDOVECTOR_SIZE_MASK;
1266 for (i = (SUB_CHAR_TABLE_P (obj)
1267 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1268 print_preprocess (AREF (obj, i));
1269 if (HASH_TABLE_P (obj))
1270 { /* For hash tables, the key_and_value slot is past
1271 `size' because it needs to be marked specially in case
1272 the table is weak. */
1273 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1274 print_preprocess (h->key_and_value);
1276 break;
1278 default:
1279 break;
1282 print_depth--;
1285 DEFUN ("print--preprocess", Fprint_preprocess, Sprint_preprocess, 1, 1, 0,
1286 doc: /* Extract sharing info from OBJECT needed to print it.
1287 Fills `print-number-table'. */)
1288 (Lisp_Object object)
1290 print_number_index = 0;
1291 print_preprocess (object);
1292 return Qnil;
1295 static void
1296 print_preprocess_string (INTERVAL interval, void *arg)
1298 print_preprocess (interval->plist);
1301 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1303 #define PRINT_STRING_NON_CHARSET_FOUND 1
1304 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1306 /* Bitwise or of the above macros. */
1307 static int print_check_string_result;
1309 static void
1310 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1312 Lisp_Object val;
1314 if (NILP (interval->plist)
1315 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1316 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1317 return;
1318 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1319 val = XCDR (XCDR (val)));
1320 if (! CONSP (val))
1322 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1323 return;
1325 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1327 if (! EQ (val, interval->plist)
1328 || CONSP (XCDR (XCDR (val))))
1329 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1331 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1333 int i, c;
1334 ptrdiff_t charpos = interval->position;
1335 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1336 Lisp_Object charset;
1338 charset = XCAR (XCDR (val));
1339 for (i = 0; i < LENGTH (interval); i++)
1341 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1342 if (! ASCII_CHAR_P (c)
1343 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1345 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1346 break;
1352 /* The value is (charset . nil). */
1353 static Lisp_Object print_prune_charset_plist;
1355 static Lisp_Object
1356 print_prune_string_charset (Lisp_Object string)
1358 print_check_string_result = 0;
1359 traverse_intervals (string_intervals (string), 0,
1360 print_check_string_charset_prop, string);
1361 if (NILP (Vprint_charset_text_property)
1362 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1364 string = Fcopy_sequence (string);
1365 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1367 if (NILP (print_prune_charset_plist))
1368 print_prune_charset_plist = list1 (Qcharset);
1369 Fremove_text_properties (make_number (0),
1370 make_number (SCHARS (string)),
1371 print_prune_charset_plist, string);
1373 else
1374 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1375 Qnil, string);
1377 return string;
1380 static bool
1381 print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
1382 char *buf)
1384 switch (PSEUDOVECTOR_TYPE (XVECTOR (obj)))
1386 case PVEC_PROCESS:
1387 if (escapeflag)
1389 print_c_string ("#<process ", printcharfun);
1390 print_string (XPROCESS (obj)->name, printcharfun);
1391 printchar ('>', printcharfun);
1393 else
1394 print_string (XPROCESS (obj)->name, printcharfun);
1395 break;
1397 case PVEC_BOOL_VECTOR:
1399 EMACS_INT size = bool_vector_size (obj);
1400 ptrdiff_t size_in_bytes = bool_vector_bytes (size);
1401 ptrdiff_t real_size_in_bytes = size_in_bytes;
1402 unsigned char *data = bool_vector_uchar_data (obj);
1404 int len = sprintf (buf, "#&%"pI"d\"", size);
1405 strout (buf, len, len, printcharfun);
1407 /* Don't print more bytes than the specified maximum.
1408 Negative values of print-length are invalid. Treat them
1409 like a print-length of nil. */
1410 if (NATNUMP (Vprint_length)
1411 && XFASTINT (Vprint_length) < size_in_bytes)
1412 size_in_bytes = XFASTINT (Vprint_length);
1414 for (ptrdiff_t i = 0; i < size_in_bytes; i++)
1416 maybe_quit ();
1417 unsigned char c = data[i];
1418 if (c == '\n' && print_escape_newlines)
1419 print_c_string ("\\n", printcharfun);
1420 else if (c == '\f' && print_escape_newlines)
1421 print_c_string ("\\f", printcharfun);
1422 else if (c > '\177'
1423 || (print_escape_control_characters && c_iscntrl (c)))
1425 /* Use octal escapes to avoid encoding issues. */
1426 octalout (c, data, i + 1, size_in_bytes, printcharfun);
1428 else
1430 if (c == '\"' || c == '\\')
1431 printchar ('\\', printcharfun);
1432 printchar (c, printcharfun);
1436 if (size_in_bytes < real_size_in_bytes)
1437 print_c_string (" ...", printcharfun);
1438 printchar ('\"', printcharfun);
1440 break;
1442 case PVEC_SUBR:
1443 print_c_string ("#<subr ", printcharfun);
1444 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1445 printchar ('>', printcharfun);
1446 break;
1448 case PVEC_XWIDGET: case PVEC_XWIDGET_VIEW:
1449 print_c_string ("#<xwidget ", printcharfun);
1450 printchar ('>', printcharfun);
1451 break;
1453 case PVEC_WINDOW:
1455 int len = sprintf (buf, "#<window %"pI"d",
1456 XWINDOW (obj)->sequence_number);
1457 strout (buf, len, len, printcharfun);
1458 if (BUFFERP (XWINDOW (obj)->contents))
1460 print_c_string (" on ", printcharfun);
1461 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1462 printcharfun);
1464 printchar ('>', printcharfun);
1466 break;
1468 case PVEC_TERMINAL:
1470 struct terminal *t = XTERMINAL (obj);
1471 int len = sprintf (buf, "#<terminal %d", t->id);
1472 strout (buf, len, len, printcharfun);
1473 if (t->name)
1475 print_c_string (" on ", printcharfun);
1476 print_c_string (t->name, printcharfun);
1478 printchar ('>', printcharfun);
1480 break;
1482 case PVEC_HASH_TABLE:
1484 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1485 /* Implement a readable output, e.g.:
1486 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1487 /* Always print the size. */
1488 int len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1489 strout (buf, len, len, printcharfun);
1491 if (!NILP (h->test.name))
1493 print_c_string (" test ", printcharfun);
1494 print_object (h->test.name, printcharfun, escapeflag);
1497 if (!NILP (h->weak))
1499 print_c_string (" weakness ", printcharfun);
1500 print_object (h->weak, printcharfun, escapeflag);
1503 print_c_string (" rehash-size ", printcharfun);
1504 print_object (Fhash_table_rehash_size (obj),
1505 printcharfun, escapeflag);
1507 print_c_string (" rehash-threshold ", printcharfun);
1508 print_object (Fhash_table_rehash_threshold (obj),
1509 printcharfun, escapeflag);
1511 if (h->pure)
1513 print_c_string (" purecopy ", printcharfun);
1514 print_object (h->pure ? Qt : Qnil, printcharfun, escapeflag);
1517 print_c_string (" data ", printcharfun);
1519 /* Print the data here as a plist. */
1520 ptrdiff_t real_size = HASH_TABLE_SIZE (h);
1521 ptrdiff_t size = real_size;
1523 /* Don't print more elements than the specified maximum. */
1524 if (NATNUMP (Vprint_length) && XFASTINT (Vprint_length) < size)
1525 size = XFASTINT (Vprint_length);
1527 printchar ('(', printcharfun);
1528 for (ptrdiff_t i = 0; i < size; i++)
1529 if (!NILP (HASH_HASH (h, i)))
1531 if (i) printchar (' ', printcharfun);
1532 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1533 printchar (' ', printcharfun);
1534 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1537 if (size < real_size)
1538 print_c_string (" ...", printcharfun);
1540 print_c_string ("))", printcharfun);
1542 break;
1544 case PVEC_BUFFER:
1545 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1546 print_c_string ("#<killed buffer>", printcharfun);
1547 else if (escapeflag)
1549 print_c_string ("#<buffer ", printcharfun);
1550 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1551 printchar ('>', printcharfun);
1553 else
1554 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1555 break;
1557 case PVEC_WINDOW_CONFIGURATION:
1558 print_c_string ("#<window-configuration>", printcharfun);
1559 break;
1561 case PVEC_FRAME:
1563 void *ptr = XFRAME (obj);
1564 Lisp_Object frame_name = XFRAME (obj)->name;
1566 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1567 ? "#<frame "
1568 : "#<dead frame "),
1569 printcharfun);
1570 if (!STRINGP (frame_name))
1572 /* A frame could be too young and have no name yet;
1573 don't crash. */
1574 if (SYMBOLP (frame_name))
1575 frame_name = Fsymbol_name (frame_name);
1576 else /* can't happen: name should be either nil or string */
1577 frame_name = build_string ("*INVALID*FRAME*NAME*");
1579 print_string (frame_name, printcharfun);
1580 int len = sprintf (buf, " %p>", ptr);
1581 strout (buf, len, len, printcharfun);
1583 break;
1585 case PVEC_FONT:
1587 if (! FONT_OBJECT_P (obj))
1589 if (FONT_SPEC_P (obj))
1590 print_c_string ("#<font-spec", printcharfun);
1591 else
1592 print_c_string ("#<font-entity", printcharfun);
1593 for (int i = 0; i < FONT_SPEC_MAX; i++)
1595 printchar (' ', printcharfun);
1596 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1597 print_object (AREF (obj, i), printcharfun, escapeflag);
1598 else
1599 print_object (font_style_symbolic (obj, i, 0),
1600 printcharfun, escapeflag);
1603 else
1605 print_c_string ("#<font-object ", printcharfun);
1606 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1607 escapeflag);
1609 printchar ('>', printcharfun);
1611 break;
1613 case PVEC_THREAD:
1614 print_c_string ("#<thread ", printcharfun);
1615 if (STRINGP (XTHREAD (obj)->name))
1616 print_string (XTHREAD (obj)->name, printcharfun);
1617 else
1619 int len = sprintf (buf, "%p", XTHREAD (obj));
1620 strout (buf, len, len, printcharfun);
1622 printchar ('>', printcharfun);
1623 break;
1625 case PVEC_MUTEX:
1626 print_c_string ("#<mutex ", printcharfun);
1627 if (STRINGP (XMUTEX (obj)->name))
1628 print_string (XMUTEX (obj)->name, printcharfun);
1629 else
1631 int len = sprintf (buf, "%p", XMUTEX (obj));
1632 strout (buf, len, len, printcharfun);
1634 printchar ('>', printcharfun);
1635 break;
1637 case PVEC_CONDVAR:
1638 print_c_string ("#<condvar ", printcharfun);
1639 if (STRINGP (XCONDVAR (obj)->name))
1640 print_string (XCONDVAR (obj)->name, printcharfun);
1641 else
1643 int len = sprintf (buf, "%p", XCONDVAR (obj));
1644 strout (buf, len, len, printcharfun);
1646 printchar ('>', printcharfun);
1647 break;
1649 case PVEC_RECORD:
1651 ptrdiff_t size = PVSIZE (obj);
1653 /* Don't print more elements than the specified maximum. */
1654 ptrdiff_t n
1655 = (NATNUMP (Vprint_length) && XFASTINT (Vprint_length) < size
1656 ? XFASTINT (Vprint_length) : size);
1658 print_c_string ("#s(", printcharfun);
1659 for (ptrdiff_t i = 0; i < n; i ++)
1661 if (i) printchar (' ', printcharfun);
1662 print_object (AREF (obj, i), printcharfun, escapeflag);
1664 if (n < size)
1665 print_c_string (" ...", printcharfun);
1666 printchar (')', printcharfun);
1668 break;
1670 case PVEC_SUB_CHAR_TABLE:
1671 case PVEC_COMPILED:
1672 case PVEC_CHAR_TABLE:
1673 case PVEC_NORMAL_VECTOR:
1675 ptrdiff_t size = ASIZE (obj);
1676 if (COMPILEDP (obj))
1678 printchar ('#', printcharfun);
1679 size &= PSEUDOVECTOR_SIZE_MASK;
1681 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1683 /* Print a char-table as if it were a vector,
1684 lumping the parent and default slots in with the
1685 character slots. But add #^ as a prefix. */
1687 /* Make each lowest sub_char_table start a new line.
1688 Otherwise we'll make a line extremely long, which
1689 results in slow redisplay. */
1690 if (SUB_CHAR_TABLE_P (obj)
1691 && XSUB_CHAR_TABLE (obj)->depth == 3)
1692 printchar ('\n', printcharfun);
1693 print_c_string ("#^", printcharfun);
1694 if (SUB_CHAR_TABLE_P (obj))
1695 printchar ('^', printcharfun);
1696 size &= PSEUDOVECTOR_SIZE_MASK;
1698 if (size & PSEUDOVECTOR_FLAG)
1699 return false;
1701 printchar ('[', printcharfun);
1703 int idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1704 Lisp_Object tem;
1705 ptrdiff_t real_size = size;
1707 /* For a sub char-table, print heading non-Lisp data first. */
1708 if (SUB_CHAR_TABLE_P (obj))
1710 int i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1711 XSUB_CHAR_TABLE (obj)->min_char);
1712 strout (buf, i, i, printcharfun);
1715 /* Don't print more elements than the specified maximum. */
1716 if (NATNUMP (Vprint_length)
1717 && XFASTINT (Vprint_length) < size)
1718 size = XFASTINT (Vprint_length);
1720 for (int i = idx; i < size; i++)
1722 if (i) printchar (' ', printcharfun);
1723 tem = AREF (obj, i);
1724 print_object (tem, printcharfun, escapeflag);
1726 if (size < real_size)
1727 print_c_string (" ...", printcharfun);
1728 printchar (']', printcharfun);
1730 break;
1732 #ifdef HAVE_MODULES
1733 case PVEC_MODULE_FUNCTION:
1735 print_c_string ("#<module function ", printcharfun);
1736 void *ptr = XMODULE_FUNCTION (obj)->subr;
1737 const char *file = NULL;
1738 const char *symbol = NULL;
1739 dynlib_addr (ptr, &file, &symbol);
1741 if (symbol == NULL)
1743 print_c_string ("at ", printcharfun);
1744 enum { pointer_bufsize = sizeof ptr * 16 / CHAR_BIT + 2 + 1 };
1745 char buffer[pointer_bufsize];
1746 int needed = snprintf (buffer, sizeof buffer, "%p", ptr);
1747 const char p0x[] = "0x";
1748 eassert (needed <= sizeof buffer);
1749 /* ANSI C doesn't guarantee that %p produces a string that
1750 begins with a "0x". */
1751 if (c_strncasecmp (buffer, p0x, sizeof (p0x) - 1) != 0)
1752 print_c_string (p0x, printcharfun);
1753 print_c_string (buffer, printcharfun);
1755 else
1756 print_c_string (symbol, printcharfun);
1758 if (file != NULL)
1760 print_c_string (" from ", printcharfun);
1761 print_c_string (file, printcharfun);
1764 printchar ('>', printcharfun);
1766 break;
1767 #endif
1769 default:
1770 emacs_abort ();
1773 return true;
1776 static void
1777 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1779 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1780 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1781 40))];
1782 current_thread->stack_top = buf;
1783 maybe_quit ();
1785 /* Detect circularities and truncate them. */
1786 if (NILP (Vprint_circle))
1788 /* Simple but incomplete way. */
1789 int i;
1791 /* See similar code in print_preprocess. */
1792 if (print_depth >= PRINT_CIRCLE)
1793 error ("Apparently circular structure being printed");
1795 for (i = 0; i < print_depth; i++)
1796 if (EQ (obj, being_printed[i]))
1798 int len = sprintf (buf, "#%d", i);
1799 strout (buf, len, len, printcharfun);
1800 return;
1802 being_printed[print_depth] = obj;
1804 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1806 /* With the print-circle feature. */
1807 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1808 if (INTEGERP (num))
1810 EMACS_INT n = XINT (num);
1811 if (n < 0)
1812 { /* Add a prefix #n= if OBJ has not yet been printed;
1813 that is, its status field is nil. */
1814 int len = sprintf (buf, "#%"pI"d=", -n);
1815 strout (buf, len, len, printcharfun);
1816 /* OBJ is going to be printed. Remember that fact. */
1817 Fputhash (obj, make_number (- n), Vprint_number_table);
1819 else
1821 /* Just print #n# if OBJ has already been printed. */
1822 int len = sprintf (buf, "#%"pI"d#", n);
1823 strout (buf, len, len, printcharfun);
1824 return;
1829 print_depth++;
1831 switch (XTYPE (obj))
1833 case_Lisp_Int:
1835 int len = sprintf (buf, "%"pI"d", XINT (obj));
1836 strout (buf, len, len, printcharfun);
1838 break;
1840 case Lisp_Float:
1842 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1843 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1844 strout (pigbuf, len, len, printcharfun);
1846 break;
1848 case Lisp_String:
1849 if (!escapeflag)
1850 print_string (obj, printcharfun);
1851 else
1853 ptrdiff_t i, i_byte;
1854 ptrdiff_t size_byte;
1855 /* True means we must ensure that the next character we output
1856 cannot be taken as part of a hex character escape. */
1857 bool need_nonhex = false;
1858 bool multibyte = STRING_MULTIBYTE (obj);
1860 if (! EQ (Vprint_charset_text_property, Qt))
1861 obj = print_prune_string_charset (obj);
1863 if (string_intervals (obj))
1864 print_c_string ("#(", printcharfun);
1866 printchar ('\"', printcharfun);
1867 size_byte = SBYTES (obj);
1869 for (i = 0, i_byte = 0; i_byte < size_byte;)
1871 /* Here, we must convert each multi-byte form to the
1872 corresponding character code before handing it to printchar. */
1873 int c;
1875 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1877 maybe_quit ();
1879 if (multibyte
1880 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1881 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1882 && print_escape_nonascii))
1884 /* When printing a raw 8-bit byte in a multibyte buffer, or
1885 (when requested) a non-ASCII character in a unibyte buffer,
1886 print single-byte non-ASCII string chars
1887 using octal escapes. */
1888 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1889 need_nonhex = false;
1891 else if (multibyte
1892 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1894 /* When requested, print multibyte chars using hex escapes. */
1895 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1896 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1897 strout (outbuf, len, len, printcharfun);
1898 need_nonhex = true;
1900 else
1902 /* If we just had a hex escape, and this character
1903 could be taken as part of it,
1904 output `\ ' to prevent that. */
1905 if (c_isxdigit (c))
1907 if (need_nonhex)
1908 print_c_string ("\\ ", printcharfun);
1909 printchar (c, printcharfun);
1911 else if (c == '\n' && print_escape_newlines
1912 ? (c = 'n', true)
1913 : c == '\f' && print_escape_newlines
1914 ? (c = 'f', true)
1915 : c == '\"' || c == '\\')
1917 printchar ('\\', printcharfun);
1918 printchar (c, printcharfun);
1920 else if (print_escape_control_characters && c_iscntrl (c))
1921 octalout (c, SDATA (obj), i_byte, size_byte, printcharfun);
1922 else
1923 printchar (c, printcharfun);
1924 need_nonhex = false;
1927 printchar ('\"', printcharfun);
1929 if (string_intervals (obj))
1931 traverse_intervals (string_intervals (obj),
1932 0, print_interval, printcharfun);
1933 printchar (')', printcharfun);
1936 break;
1938 case Lisp_Symbol:
1940 bool confusing;
1941 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1942 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1943 int c;
1944 ptrdiff_t i, i_byte;
1945 ptrdiff_t size_byte;
1946 Lisp_Object name;
1948 name = SYMBOL_NAME (obj);
1950 if (p != end && (*p == '-' || *p == '+')) p++;
1951 if (p == end)
1952 confusing = 0;
1953 /* If symbol name begins with a digit, and ends with a digit,
1954 and contains nothing but digits and `e', it could be treated
1955 as a number. So set CONFUSING.
1957 Symbols that contain periods could also be taken as numbers,
1958 but periods are always escaped, so we don't have to worry
1959 about them here. */
1960 else if (*p >= '0' && *p <= '9'
1961 && end[-1] >= '0' && end[-1] <= '9')
1963 while (p != end && ((*p >= '0' && *p <= '9')
1964 /* Needed for \2e10. */
1965 || *p == 'e' || *p == 'E'))
1966 p++;
1967 confusing = (end == p);
1969 else
1970 confusing = 0;
1972 size_byte = SBYTES (name);
1974 if (! NILP (Vprint_gensym)
1975 && !SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (obj))
1976 print_c_string ("#:", printcharfun);
1977 else if (size_byte == 0)
1979 print_c_string ("##", printcharfun);
1980 break;
1983 for (i = 0, i_byte = 0; i_byte < size_byte;)
1985 /* Here, we must convert each multi-byte form to the
1986 corresponding character code before handing it to PRINTCHAR. */
1987 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1988 maybe_quit ();
1990 if (escapeflag)
1992 if (c == '\"' || c == '\\' || c == '\''
1993 || c == ';' || c == '#' || c == '(' || c == ')'
1994 || c == ',' || c == '.' || c == '`'
1995 || c == '[' || c == ']' || c == '?' || c <= 040
1996 || confusing
1997 || (i == 1 && confusable_symbol_character_p (c)))
1999 printchar ('\\', printcharfun);
2000 confusing = false;
2003 printchar (c, printcharfun);
2006 break;
2008 case Lisp_Cons:
2009 /* If deeper than spec'd depth, print placeholder. */
2010 if (INTEGERP (Vprint_level)
2011 && print_depth > XINT (Vprint_level))
2012 print_c_string ("...", printcharfun);
2013 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2014 && EQ (XCAR (obj), Qquote))
2016 printchar ('\'', printcharfun);
2017 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2019 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2020 && EQ (XCAR (obj), Qfunction))
2022 print_c_string ("#'", printcharfun);
2023 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2025 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2026 && EQ (XCAR (obj), Qbackquote))
2028 printchar ('`', printcharfun);
2029 new_backquote_output++;
2030 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2031 new_backquote_output--;
2033 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
2034 && new_backquote_output
2035 && (EQ (XCAR (obj), Qcomma)
2036 || EQ (XCAR (obj), Qcomma_at)
2037 || EQ (XCAR (obj), Qcomma_dot)))
2039 print_object (XCAR (obj), printcharfun, false);
2040 new_backquote_output--;
2041 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
2042 new_backquote_output++;
2044 else
2046 printchar ('(', printcharfun);
2048 Lisp_Object halftail = obj;
2050 /* Negative values of print-length are invalid in CL.
2051 Treat them like nil, as CMUCL does. */
2052 printmax_t print_length = (NATNUMP (Vprint_length)
2053 ? XFASTINT (Vprint_length)
2054 : TYPE_MAXIMUM (printmax_t));
2056 printmax_t i = 0;
2057 while (CONSP (obj))
2059 /* Detect circular list. */
2060 if (NILP (Vprint_circle))
2062 /* Simple but incomplete way. */
2063 if (i != 0 && EQ (obj, halftail))
2065 int len = sprintf (buf, " . #%"pMd, i / 2);
2066 strout (buf, len, len, printcharfun);
2067 goto end_of_list;
2070 else
2072 /* With the print-circle feature. */
2073 if (i != 0)
2075 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
2076 if (INTEGERP (num))
2078 print_c_string (" . ", printcharfun);
2079 print_object (obj, printcharfun, escapeflag);
2080 goto end_of_list;
2085 if (i)
2086 printchar (' ', printcharfun);
2088 if (print_length <= i)
2090 print_c_string ("...", printcharfun);
2091 goto end_of_list;
2094 i++;
2095 print_object (XCAR (obj), printcharfun, escapeflag);
2097 obj = XCDR (obj);
2098 if (!(i & 1))
2099 halftail = XCDR (halftail);
2102 /* OBJ non-nil here means it's the end of a dotted list. */
2103 if (!NILP (obj))
2105 print_c_string (" . ", printcharfun);
2106 print_object (obj, printcharfun, escapeflag);
2109 end_of_list:
2110 printchar (')', printcharfun);
2112 break;
2114 case Lisp_Vectorlike:
2115 if (! print_vectorlike (obj, printcharfun, escapeflag, buf))
2116 goto badtype;
2117 break;
2119 case Lisp_Misc:
2120 switch (XMISCTYPE (obj))
2122 case Lisp_Misc_Marker:
2123 print_c_string ("#<marker ", printcharfun);
2124 /* Do you think this is necessary? */
2125 if (XMARKER (obj)->insertion_type != 0)
2126 print_c_string ("(moves after insertion) ", printcharfun);
2127 if (! XMARKER (obj)->buffer)
2128 print_c_string ("in no buffer", printcharfun);
2129 else
2131 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
2132 strout (buf, len, len, printcharfun);
2133 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
2135 printchar ('>', printcharfun);
2136 break;
2138 case Lisp_Misc_Overlay:
2139 print_c_string ("#<overlay ", printcharfun);
2140 if (! XMARKER (OVERLAY_START (obj))->buffer)
2141 print_c_string ("in no buffer", printcharfun);
2142 else
2144 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2145 marker_position (OVERLAY_START (obj)),
2146 marker_position (OVERLAY_END (obj)));
2147 strout (buf, len, len, printcharfun);
2148 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2149 printcharfun);
2151 printchar ('>', printcharfun);
2152 break;
2154 #ifdef HAVE_MODULES
2155 case Lisp_Misc_User_Ptr:
2157 print_c_string ("#<user-ptr ", printcharfun);
2158 int i = sprintf (buf, "ptr=%p finalizer=%p",
2159 XUSER_PTR (obj)->p,
2160 XUSER_PTR (obj)->finalizer);
2161 strout (buf, i, i, printcharfun);
2162 printchar ('>', printcharfun);
2163 break;
2165 #endif
2167 case Lisp_Misc_Finalizer:
2168 print_c_string ("#<finalizer", printcharfun);
2169 if (NILP (XFINALIZER (obj)->function))
2170 print_c_string (" used", printcharfun);
2171 printchar ('>', printcharfun);
2172 break;
2174 /* Remaining cases shouldn't happen in normal usage, but let's
2175 print them anyway for the benefit of the debugger. */
2177 case Lisp_Misc_Free:
2178 print_c_string ("#<misc free cell>", printcharfun);
2179 break;
2181 case Lisp_Misc_Ptr:
2183 int i = sprintf (buf, "#<ptr %p>", xmint_pointer (obj));
2184 strout (buf, i, i, printcharfun);
2186 break;
2188 default:
2189 goto badtype;
2191 break;
2193 default:
2194 badtype:
2196 int len;
2197 /* We're in trouble if this happens!
2198 Probably should just emacs_abort (). */
2199 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2200 if (MISCP (obj))
2201 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2202 else if (VECTORLIKEP (obj))
2203 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2204 else
2205 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2206 strout (buf, len, len, printcharfun);
2207 print_c_string ((" Save your buffers immediately"
2208 " and please report this bug>"),
2209 printcharfun);
2213 print_depth--;
2217 /* Print a description of INTERVAL using PRINTCHARFUN.
2218 This is part of printing a string that has text properties. */
2220 static void
2221 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2223 if (NILP (interval->plist))
2224 return;
2225 printchar (' ', printcharfun);
2226 print_object (make_number (interval->position), printcharfun, 1);
2227 printchar (' ', printcharfun);
2228 print_object (make_number (interval->position + LENGTH (interval)),
2229 printcharfun, 1);
2230 printchar (' ', printcharfun);
2231 print_object (interval->plist, printcharfun, 1);
2234 /* Initialize debug_print stuff early to have it working from the very
2235 beginning. */
2237 void
2238 init_print_once (void)
2240 /* The subroutine object for external-debugging-output is kept here
2241 for the convenience of the debugger. */
2242 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2244 defsubr (&Sexternal_debugging_output);
2247 void
2248 syms_of_print (void)
2250 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2252 DEFVAR_LISP ("standard-output", Vstandard_output,
2253 doc: /* Output stream `print' uses by default for outputting a character.
2254 This may be any function of one argument.
2255 It may also be a buffer (output is inserted before point)
2256 or a marker (output is inserted and the marker is advanced)
2257 or the symbol t (output appears in the echo area). */);
2258 Vstandard_output = Qt;
2259 DEFSYM (Qstandard_output, "standard-output");
2261 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2262 doc: /* The format descriptor string used to print floats.
2263 This is a %-spec like those accepted by `printf' in C,
2264 but with some restrictions. It must start with the two characters `%.'.
2265 After that comes an integer precision specification,
2266 and then a letter which controls the format.
2267 The letters allowed are `e', `f' and `g'.
2268 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2269 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2270 Use `g' to choose the shorter of those two formats for the number at hand.
2271 The precision in any of these cases is the number of digits following
2272 the decimal point. With `f', a precision of 0 means to omit the
2273 decimal point. 0 is not allowed with `e' or `g'.
2275 A value of nil means to use the shortest notation
2276 that represents the number without losing information. */);
2277 Vfloat_output_format = Qnil;
2279 DEFVAR_LISP ("print-length", Vprint_length,
2280 doc: /* Maximum length of list to print before abbreviating.
2281 A value of nil means no limit. See also `eval-expression-print-length'. */);
2282 Vprint_length = Qnil;
2284 DEFVAR_LISP ("print-level", Vprint_level,
2285 doc: /* Maximum depth of list nesting to print before abbreviating.
2286 A value of nil means no limit. See also `eval-expression-print-level'. */);
2287 Vprint_level = Qnil;
2289 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2290 doc: /* Non-nil means print newlines in strings as `\\n'.
2291 Also print formfeeds as `\\f'. */);
2292 print_escape_newlines = 0;
2294 DEFVAR_BOOL ("print-escape-control-characters", print_escape_control_characters,
2295 doc: /* Non-nil means print control characters in strings as `\\OOO'.
2296 \(OOO is the octal representation of the character code.)*/);
2297 print_escape_control_characters = 0;
2299 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2300 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2301 \(OOO is the octal representation of the character code.)
2302 Only single-byte characters are affected, and only in `prin1'.
2303 When the output goes in a multibyte buffer, this feature is
2304 enabled regardless of the value of the variable. */);
2305 print_escape_nonascii = 0;
2307 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2308 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2309 \(XXXX is the hex representation of the character code.)
2310 This affects only `prin1'. */);
2311 print_escape_multibyte = 0;
2313 DEFVAR_BOOL ("print-quoted", print_quoted,
2314 doc: /* Non-nil means print quoted forms with reader syntax.
2315 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2316 print_quoted = true;
2318 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2319 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2320 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2321 When the uninterned symbol appears multiple times within the printed
2322 expression, and `print-circle' is non-nil, in addition use the #N#
2323 and #N= constructs as needed, so that multiple references to the same
2324 symbol are shared once again when the text is read back. */);
2325 Vprint_gensym = Qnil;
2327 DEFVAR_LISP ("print-circle", Vprint_circle,
2328 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2329 If nil, printing proceeds recursively and may lead to
2330 `max-lisp-eval-depth' being exceeded or an error may occur:
2331 \"Apparently circular structure being printed.\" Also see
2332 `print-length' and `print-level'.
2333 If non-nil, shared substructures anywhere in the structure are printed
2334 with `#N=' before the first occurrence (in the order of the print
2335 representation) and `#N#' in place of each subsequent occurrence,
2336 where N is a positive decimal integer. */);
2337 Vprint_circle = Qnil;
2339 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2340 doc: /* Non-nil means number continuously across print calls.
2341 This affects the numbers printed for #N= labels and #M# references.
2342 See also `print-circle', `print-gensym', and `print-number-table'.
2343 This variable should not be set with `setq'; bind it with a `let' instead. */);
2344 Vprint_continuous_numbering = Qnil;
2346 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2347 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2348 The Lisp printer uses this vector to detect Lisp objects referenced more
2349 than once.
2351 When you bind `print-continuous-numbering' to t, you should probably
2352 also bind `print-number-table' to nil. This ensures that the value of
2353 `print-number-table' can be garbage-collected once the printing is
2354 done. If all elements of `print-number-table' are nil, it means that
2355 the printing done so far has not found any shared structure or objects
2356 that need to be recorded in the table. */);
2357 Vprint_number_table = Qnil;
2359 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2360 doc: /* A flag to control printing of `charset' text property on printing a string.
2361 The value should be nil, t, or `default'.
2363 If the value is nil, don't print the text property `charset'.
2365 If the value is t, always print the text property `charset'.
2367 If the value is `default', print the text property `charset' only when
2368 the value is different from what is guessed in the current charset
2369 priorities. Values other than nil or t are also treated as
2370 `default'. */);
2371 Vprint_charset_text_property = Qdefault;
2373 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2374 staticpro (&Vprin1_to_string_buffer);
2376 defsubr (&Sprin1);
2377 defsubr (&Sprin1_to_string);
2378 defsubr (&Serror_message_string);
2379 defsubr (&Sprinc);
2380 defsubr (&Sprint);
2381 defsubr (&Sterpri);
2382 defsubr (&Swrite_char);
2383 defsubr (&Sredirect_debugging_output);
2384 defsubr (&Sprint_preprocess);
2386 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2387 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2389 print_prune_charset_plist = Qnil;
2390 staticpro (&print_prune_charset_plist);