]> code.delx.au - gnu-emacs/blob - src/print.c
Ibuffer change marks
[gnu-emacs] / src / print.c
1 /* Lisp object printing and output streams.
2
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2016 Free Software
4 Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include <config.h>
23 #include "sysstdio.h"
24
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
37 #include <c-ctype.h>
38 #include <float.h>
39 #include <ftoastr.h>
40
41 #ifdef WINDOWSNT
42 # include <sys/socket.h> /* for F_DUPFD_CLOEXEC */
43 #endif
44
45 struct terminal;
46
47 /* Avoid actual stack overflow in print. */
48 static ptrdiff_t print_depth;
49
50 /* Level of nesting inside outputting backquote in new style. */
51 static ptrdiff_t new_backquote_output;
52
53 /* Detect most circularities to print finite output. */
54 #define PRINT_CIRCLE 200
55 static Lisp_Object being_printed[PRINT_CIRCLE];
56
57 /* Last char printed to stdout by printchar. */
58 static unsigned int printchar_stdout_last;
59
60 /* When printing into a buffer, first we put the text in this
61 block, then insert it all at once. */
62 static char *print_buffer;
63
64 /* Size allocated in print_buffer. */
65 static ptrdiff_t print_buffer_size;
66 /* Chars stored in print_buffer. */
67 static ptrdiff_t print_buffer_pos;
68 /* Bytes stored in print_buffer. */
69 static ptrdiff_t print_buffer_pos_byte;
70
71 /* Vprint_number_table is a table, that keeps objects that are going to
72 be printed, to allow use of #n= and #n# to express sharing.
73 For any given object, the table can give the following values:
74 t the object will be printed only once.
75 -N the object will be printed several times and will take number N.
76 N the object has been printed so we can refer to it as #N#.
77 print_number_index holds the largest N already used.
78 N has to be striclty larger than 0 since we need to distinguish -N. */
79 static ptrdiff_t print_number_index;
80 static void print_interval (INTERVAL interval, Lisp_Object printcharfun);
81
82 /* GDB resets this to zero on W32 to disable OutputDebugString calls. */
83 bool print_output_debug_flag EXTERNALLY_VISIBLE = 1;
84
85 \f
86 /* Low level output routines for characters and strings. */
87
88 /* Lisp functions to do output using a stream
89 must have the stream in a variable called printcharfun
90 and must start with PRINTPREPARE, end with PRINTFINISH.
91 Use printchar to output one character,
92 or call strout to output a block of characters. */
93
94 #define PRINTPREPARE \
95 struct buffer *old = current_buffer; \
96 ptrdiff_t old_point = -1, start_point = -1; \
97 ptrdiff_t old_point_byte = -1, start_point_byte = -1; \
98 ptrdiff_t specpdl_count = SPECPDL_INDEX (); \
99 bool free_print_buffer = 0; \
100 bool multibyte \
101 = !NILP (BVAR (current_buffer, enable_multibyte_characters)); \
102 Lisp_Object original = printcharfun; \
103 if (NILP (printcharfun)) printcharfun = Qt; \
104 if (BUFFERP (printcharfun)) \
105 { \
106 if (XBUFFER (printcharfun) != current_buffer) \
107 Fset_buffer (printcharfun); \
108 printcharfun = Qnil; \
109 } \
110 if (MARKERP (printcharfun)) \
111 { \
112 ptrdiff_t marker_pos; \
113 if (! XMARKER (printcharfun)->buffer) \
114 error ("Marker does not point anywhere"); \
115 if (XMARKER (printcharfun)->buffer != current_buffer) \
116 set_buffer_internal (XMARKER (printcharfun)->buffer); \
117 marker_pos = marker_position (printcharfun); \
118 if (marker_pos < BEGV || marker_pos > ZV) \
119 signal_error ("Marker is outside the accessible " \
120 "part of the buffer", printcharfun); \
121 old_point = PT; \
122 old_point_byte = PT_BYTE; \
123 SET_PT_BOTH (marker_pos, \
124 marker_byte_position (printcharfun)); \
125 start_point = PT; \
126 start_point_byte = PT_BYTE; \
127 printcharfun = Qnil; \
128 } \
129 if (NILP (printcharfun)) \
130 { \
131 Lisp_Object string; \
132 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) \
133 && ! print_escape_multibyte) \
134 specbind (Qprint_escape_multibyte, Qt); \
135 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)) \
136 && ! print_escape_nonascii) \
137 specbind (Qprint_escape_nonascii, Qt); \
138 if (print_buffer != 0) \
139 { \
140 string = make_string_from_bytes (print_buffer, \
141 print_buffer_pos, \
142 print_buffer_pos_byte); \
143 record_unwind_protect (print_unwind, string); \
144 } \
145 else \
146 { \
147 int new_size = 1000; \
148 print_buffer = xmalloc (new_size); \
149 print_buffer_size = new_size; \
150 free_print_buffer = 1; \
151 } \
152 print_buffer_pos = 0; \
153 print_buffer_pos_byte = 0; \
154 } \
155 if (EQ (printcharfun, Qt) && ! noninteractive) \
156 setup_echo_area_for_printing (multibyte);
157
158 #define PRINTFINISH \
159 if (NILP (printcharfun)) \
160 { \
161 if (print_buffer_pos != print_buffer_pos_byte \
162 && NILP (BVAR (current_buffer, enable_multibyte_characters)))\
163 { \
164 USE_SAFE_ALLOCA; \
165 unsigned char *temp = SAFE_ALLOCA (print_buffer_pos + 1); \
166 copy_text ((unsigned char *) print_buffer, temp, \
167 print_buffer_pos_byte, 1, 0); \
168 insert_1_both ((char *) temp, print_buffer_pos, \
169 print_buffer_pos, 0, 1, 0); \
170 SAFE_FREE (); \
171 } \
172 else \
173 insert_1_both (print_buffer, print_buffer_pos, \
174 print_buffer_pos_byte, 0, 1, 0); \
175 signal_after_change (PT - print_buffer_pos, 0, print_buffer_pos);\
176 } \
177 if (free_print_buffer) \
178 { \
179 xfree (print_buffer); \
180 print_buffer = 0; \
181 } \
182 unbind_to (specpdl_count, Qnil); \
183 if (MARKERP (original)) \
184 set_marker_both (original, Qnil, PT, PT_BYTE); \
185 if (old_point >= 0) \
186 SET_PT_BOTH (old_point + (old_point >= start_point \
187 ? PT - start_point : 0), \
188 old_point_byte + (old_point_byte >= start_point_byte \
189 ? PT_BYTE - start_point_byte : 0)); \
190 set_buffer_internal (old);
191
192 /* This is used to restore the saved contents of print_buffer
193 when there is a recursive call to print. */
194
195 static void
196 print_unwind (Lisp_Object saved_text)
197 {
198 memcpy (print_buffer, SDATA (saved_text), SCHARS (saved_text));
199 }
200
201 /* Print character CH to the stdio stream STREAM. */
202
203 static void
204 printchar_to_stream (unsigned int ch, FILE *stream)
205 {
206 Lisp_Object dv UNINIT;
207 ptrdiff_t i = 0, n = 1;
208 Lisp_Object coding_system = Vlocale_coding_system;
209 bool encode_p = false;
210
211 if (!NILP (Vcoding_system_for_write))
212 coding_system = Vcoding_system_for_write;
213 if (!NILP (coding_system))
214 encode_p = true;
215
216 if (CHAR_VALID_P (ch) && DISP_TABLE_P (Vstandard_display_table))
217 {
218 dv = DISP_CHAR_VECTOR (XCHAR_TABLE (Vstandard_display_table), ch);
219 if (VECTORP (dv))
220 {
221 n = ASIZE (dv);
222 goto next_char;
223 }
224 }
225
226 while (true)
227 {
228 if (ASCII_CHAR_P (ch))
229 {
230 putc (ch, stream);
231 #ifdef WINDOWSNT
232 /* Send the output to a debugger (nothing happens if there
233 isn't one). */
234 if (print_output_debug_flag && stream == stderr)
235 OutputDebugString ((char []) {ch, '\0'});
236 #endif
237 }
238 else
239 {
240 unsigned char mbstr[MAX_MULTIBYTE_LENGTH];
241 int len = CHAR_STRING (ch, mbstr);
242 Lisp_Object encoded_ch =
243 make_multibyte_string ((char *) mbstr, 1, len);
244
245 if (encode_p)
246 encoded_ch = code_convert_string_norecord (encoded_ch,
247 coding_system, true);
248 fwrite (SSDATA (encoded_ch), 1, SBYTES (encoded_ch), stream);
249 #ifdef WINDOWSNT
250 if (print_output_debug_flag && stream == stderr)
251 OutputDebugString (SSDATA (encoded_ch));
252 #endif
253 }
254
255 i++;
256
257 next_char:
258 for (; i < n; i++)
259 if (CHARACTERP (AREF (dv, i)))
260 break;
261 if (! (i < n))
262 break;
263 ch = XFASTINT (AREF (dv, i));
264 }
265 }
266
267 /* Print character CH using method FUN. FUN nil means print to
268 print_buffer. FUN t means print to echo area or stdout if
269 non-interactive. If FUN is neither nil nor t, call FUN with CH as
270 argument. */
271
272 static void
273 printchar (unsigned int ch, Lisp_Object fun)
274 {
275 if (!NILP (fun) && !EQ (fun, Qt))
276 call1 (fun, make_number (ch));
277 else
278 {
279 unsigned char str[MAX_MULTIBYTE_LENGTH];
280 int len = CHAR_STRING (ch, str);
281
282 QUIT;
283
284 if (NILP (fun))
285 {
286 ptrdiff_t incr = len - (print_buffer_size - print_buffer_pos_byte);
287 if (incr > 0)
288 print_buffer = xpalloc (print_buffer, &print_buffer_size,
289 incr, -1, 1);
290 memcpy (print_buffer + print_buffer_pos_byte, str, len);
291 print_buffer_pos += 1;
292 print_buffer_pos_byte += len;
293 }
294 else if (noninteractive)
295 {
296 printchar_stdout_last = ch;
297 if (DISP_TABLE_P (Vstandard_display_table))
298 printchar_to_stream (ch, stdout);
299 else
300 fwrite (str, 1, len, stdout);
301 noninteractive_need_newline = 1;
302 }
303 else
304 {
305 bool multibyte_p
306 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
307
308 setup_echo_area_for_printing (multibyte_p);
309 insert_char (ch);
310 message_dolog ((char *) str, len, 0, multibyte_p);
311 }
312 }
313 }
314
315
316 /* Output SIZE characters, SIZE_BYTE bytes from string PTR using
317 method PRINTCHARFUN. PRINTCHARFUN nil means output to
318 print_buffer. PRINTCHARFUN t means output to the echo area or to
319 stdout if non-interactive. If neither nil nor t, call Lisp
320 function PRINTCHARFUN for each character printed. MULTIBYTE
321 non-zero means PTR contains multibyte characters.
322
323 In the case where PRINTCHARFUN is nil, it is safe for PTR to point
324 to data in a Lisp string. Otherwise that is not safe. */
325
326 static void
327 strout (const char *ptr, ptrdiff_t size, ptrdiff_t size_byte,
328 Lisp_Object printcharfun)
329 {
330 if (NILP (printcharfun))
331 {
332 ptrdiff_t incr = size_byte - (print_buffer_size - print_buffer_pos_byte);
333 if (incr > 0)
334 print_buffer = xpalloc (print_buffer, &print_buffer_size, incr, -1, 1);
335 memcpy (print_buffer + print_buffer_pos_byte, ptr, size_byte);
336 print_buffer_pos += size;
337 print_buffer_pos_byte += size_byte;
338 }
339 else if (noninteractive && EQ (printcharfun, Qt))
340 {
341 if (DISP_TABLE_P (Vstandard_display_table))
342 {
343 int len;
344 for (ptrdiff_t i = 0; i < size_byte; i += len)
345 {
346 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
347 len);
348 printchar_to_stream (ch, stdout);
349 }
350 }
351 else
352 fwrite (ptr, 1, size_byte, stdout);
353
354 noninteractive_need_newline = 1;
355 }
356 else if (EQ (printcharfun, Qt))
357 {
358 /* Output to echo area. We're trying to avoid a little overhead
359 here, that's the reason we don't call printchar to do the
360 job. */
361 int i;
362 bool multibyte_p
363 = !NILP (BVAR (current_buffer, enable_multibyte_characters));
364
365 setup_echo_area_for_printing (multibyte_p);
366 message_dolog (ptr, size_byte, 0, multibyte_p);
367
368 if (size == size_byte)
369 {
370 for (i = 0; i < size; ++i)
371 insert_char ((unsigned char) *ptr++);
372 }
373 else
374 {
375 int len;
376 for (i = 0; i < size_byte; i += len)
377 {
378 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
379 len);
380 insert_char (ch);
381 }
382 }
383 }
384 else
385 {
386 /* PRINTCHARFUN is a Lisp function. */
387 ptrdiff_t i = 0;
388
389 if (size == size_byte)
390 {
391 while (i < size_byte)
392 {
393 int ch = ptr[i++];
394 printchar (ch, printcharfun);
395 }
396 }
397 else
398 {
399 while (i < size_byte)
400 {
401 /* Here, we must convert each multi-byte form to the
402 corresponding character code before handing it to
403 PRINTCHAR. */
404 int len;
405 int ch = STRING_CHAR_AND_LENGTH ((const unsigned char *) ptr + i,
406 len);
407 printchar (ch, printcharfun);
408 i += len;
409 }
410 }
411 }
412 }
413
414 /* Print the contents of a string STRING using PRINTCHARFUN.
415 It isn't safe to use strout in many cases,
416 because printing one char can relocate. */
417
418 static void
419 print_string (Lisp_Object string, Lisp_Object printcharfun)
420 {
421 if (EQ (printcharfun, Qt) || NILP (printcharfun))
422 {
423 ptrdiff_t chars;
424
425 if (print_escape_nonascii)
426 string = string_escape_byte8 (string);
427
428 if (STRING_MULTIBYTE (string))
429 chars = SCHARS (string);
430 else if (! print_escape_nonascii
431 && (EQ (printcharfun, Qt)
432 ? ! NILP (BVAR (&buffer_defaults, enable_multibyte_characters))
433 : ! NILP (BVAR (current_buffer, enable_multibyte_characters))))
434 {
435 /* If unibyte string STRING contains 8-bit codes, we must
436 convert STRING to a multibyte string containing the same
437 character codes. */
438 Lisp_Object newstr;
439 ptrdiff_t bytes;
440
441 chars = SBYTES (string);
442 bytes = count_size_as_multibyte (SDATA (string), chars);
443 if (chars < bytes)
444 {
445 newstr = make_uninit_multibyte_string (chars, bytes);
446 memcpy (SDATA (newstr), SDATA (string), chars);
447 str_to_multibyte (SDATA (newstr), bytes, chars);
448 string = newstr;
449 }
450 }
451 else
452 chars = SBYTES (string);
453
454 if (EQ (printcharfun, Qt))
455 {
456 /* Output to echo area. */
457 ptrdiff_t nbytes = SBYTES (string);
458
459 /* Copy the string contents so that relocation of STRING by
460 GC does not cause trouble. */
461 USE_SAFE_ALLOCA;
462 char *buffer = SAFE_ALLOCA (nbytes);
463 memcpy (buffer, SDATA (string), nbytes);
464
465 strout (buffer, chars, nbytes, printcharfun);
466
467 SAFE_FREE ();
468 }
469 else
470 /* No need to copy, since output to print_buffer can't GC. */
471 strout (SSDATA (string), chars, SBYTES (string), printcharfun);
472 }
473 else
474 {
475 /* Otherwise, string may be relocated by printing one char.
476 So re-fetch the string address for each character. */
477 ptrdiff_t i;
478 ptrdiff_t size = SCHARS (string);
479 ptrdiff_t size_byte = SBYTES (string);
480 if (size == size_byte)
481 for (i = 0; i < size; i++)
482 printchar (SREF (string, i), printcharfun);
483 else
484 for (i = 0; i < size_byte; )
485 {
486 /* Here, we must convert each multi-byte form to the
487 corresponding character code before handing it to PRINTCHAR. */
488 int len;
489 int ch = STRING_CHAR_AND_LENGTH (SDATA (string) + i, len);
490 printchar (ch, printcharfun);
491 i += len;
492 }
493 }
494 }
495 \f
496 DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
497 doc: /* Output character CHARACTER to stream PRINTCHARFUN.
498 PRINTCHARFUN defaults to the value of `standard-output' (which see). */)
499 (Lisp_Object character, Lisp_Object printcharfun)
500 {
501 if (NILP (printcharfun))
502 printcharfun = Vstandard_output;
503 CHECK_NUMBER (character);
504 PRINTPREPARE;
505 printchar (XINT (character), printcharfun);
506 PRINTFINISH;
507 return character;
508 }
509
510 /* Print the contents of a unibyte C string STRING using PRINTCHARFUN.
511 The caller should arrange to put this inside PRINTPREPARE and PRINTFINISH.
512 Do not use this on the contents of a Lisp string. */
513
514 static void
515 print_c_string (char const *string, Lisp_Object printcharfun)
516 {
517 ptrdiff_t len = strlen (string);
518 strout (string, len, len, printcharfun);
519 }
520
521 /* Print unibyte C string at DATA on a specified stream PRINTCHARFUN.
522 Do not use this on the contents of a Lisp string. */
523
524 static void
525 write_string_1 (const char *data, Lisp_Object printcharfun)
526 {
527 PRINTPREPARE;
528 print_c_string (data, printcharfun);
529 PRINTFINISH;
530 }
531
532 /* Used from outside of print.c to print a C unibyte
533 string at DATA on the default output stream.
534 Do not use this on the contents of a Lisp string. */
535
536 void
537 write_string (const char *data)
538 {
539 write_string_1 (data, Vstandard_output);
540 }
541
542
543 void
544 temp_output_buffer_setup (const char *bufname)
545 {
546 ptrdiff_t count = SPECPDL_INDEX ();
547 register struct buffer *old = current_buffer;
548 register Lisp_Object buf;
549
550 record_unwind_current_buffer ();
551
552 Fset_buffer (Fget_buffer_create (build_string (bufname)));
553
554 Fkill_all_local_variables ();
555 delete_all_overlays (current_buffer);
556 bset_directory (current_buffer, BVAR (old, directory));
557 bset_read_only (current_buffer, Qnil);
558 bset_filename (current_buffer, Qnil);
559 bset_undo_list (current_buffer, Qt);
560 eassert (current_buffer->overlays_before == NULL);
561 eassert (current_buffer->overlays_after == NULL);
562 bset_enable_multibyte_characters
563 (current_buffer, BVAR (&buffer_defaults, enable_multibyte_characters));
564 specbind (Qinhibit_read_only, Qt);
565 specbind (Qinhibit_modification_hooks, Qt);
566 Ferase_buffer ();
567 XSETBUFFER (buf, current_buffer);
568
569 run_hook (Qtemp_buffer_setup_hook);
570
571 unbind_to (count, Qnil);
572
573 specbind (Qstandard_output, buf);
574 }
575 \f
576 static void print (Lisp_Object, Lisp_Object, bool);
577 static void print_preprocess (Lisp_Object);
578 static void print_preprocess_string (INTERVAL, Lisp_Object);
579 static void print_object (Lisp_Object, Lisp_Object, bool);
580
581 DEFUN ("terpri", Fterpri, Sterpri, 0, 2, 0,
582 doc: /* Output a newline to stream PRINTCHARFUN.
583 If ENSURE is non-nil only output a newline if not already at the
584 beginning of a line. Value is non-nil if a newline is printed.
585 If PRINTCHARFUN is omitted or nil, the value of `standard-output' is used. */)
586 (Lisp_Object printcharfun, Lisp_Object ensure)
587 {
588 Lisp_Object val;
589
590 if (NILP (printcharfun))
591 printcharfun = Vstandard_output;
592 PRINTPREPARE;
593
594 if (NILP (ensure))
595 val = Qt;
596 /* Difficult to check if at line beginning so abort. */
597 else if (FUNCTIONP (printcharfun))
598 signal_error ("Unsupported function argument", printcharfun);
599 else if (noninteractive && !NILP (printcharfun))
600 val = printchar_stdout_last == 10 ? Qnil : Qt;
601 else
602 val = NILP (Fbolp ()) ? Qt : Qnil;
603
604 if (!NILP (val))
605 printchar ('\n', printcharfun);
606 PRINTFINISH;
607 return val;
608 }
609
610 DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
611 doc: /* Output the printed representation of OBJECT, any Lisp object.
612 Quoting characters are printed when needed to make output that `read'
613 can handle, whenever this is possible. For complex objects, the behavior
614 is controlled by `print-level' and `print-length', which see.
615
616 OBJECT is any of the Lisp data types: a number, a string, a symbol,
617 a list, a buffer, a window, a frame, etc.
618
619 A printed representation of an object is text which describes that object.
620
621 Optional argument PRINTCHARFUN is the output stream, which can be one
622 of these:
623
624 - a buffer, in which case output is inserted into that buffer at point;
625 - a marker, in which case output is inserted at marker's position;
626 - a function, in which case that function is called once for each
627 character of OBJECT's printed representation;
628 - a symbol, in which case that symbol's function definition is called; or
629 - t, in which case the output is displayed in the echo area.
630
631 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
632 is used instead. */)
633 (Lisp_Object object, Lisp_Object printcharfun)
634 {
635 if (NILP (printcharfun))
636 printcharfun = Vstandard_output;
637 PRINTPREPARE;
638 print (object, printcharfun, 1);
639 PRINTFINISH;
640 return object;
641 }
642
643 /* a buffer which is used to hold output being built by prin1-to-string */
644 Lisp_Object Vprin1_to_string_buffer;
645
646 DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
647 doc: /* Return a string containing the printed representation of OBJECT.
648 OBJECT can be any Lisp object. This function outputs quoting characters
649 when necessary to make output that `read' can handle, whenever possible,
650 unless the optional second argument NOESCAPE is non-nil. For complex objects,
651 the behavior is controlled by `print-level' and `print-length', which see.
652
653 OBJECT is any of the Lisp data types: a number, a string, a symbol,
654 a list, a buffer, a window, a frame, etc.
655
656 A printed representation of an object is text which describes that object. */)
657 (Lisp_Object object, Lisp_Object noescape)
658 {
659 ptrdiff_t count = SPECPDL_INDEX ();
660
661 specbind (Qinhibit_modification_hooks, Qt);
662
663 /* Save and restore this: we are altering a buffer
664 but we don't want to deactivate the mark just for that.
665 No need for specbind, since errors deactivate the mark. */
666 Lisp_Object save_deactivate_mark = Vdeactivate_mark;
667 bool prev_abort_on_gc = abort_on_gc;
668 abort_on_gc = true;
669
670 Lisp_Object printcharfun = Vprin1_to_string_buffer;
671 PRINTPREPARE;
672 print (object, printcharfun, NILP (noescape));
673 /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINISH */
674 PRINTFINISH;
675
676 struct buffer *previous = current_buffer;
677 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
678 object = Fbuffer_string ();
679 if (SBYTES (object) == SCHARS (object))
680 STRING_SET_UNIBYTE (object);
681
682 /* Note that this won't make prepare_to_modify_buffer call
683 ask-user-about-supersession-threat because this buffer
684 does not visit a file. */
685 Ferase_buffer ();
686 set_buffer_internal (previous);
687
688 Vdeactivate_mark = save_deactivate_mark;
689
690 abort_on_gc = prev_abort_on_gc;
691 return unbind_to (count, object);
692 }
693
694 DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
695 doc: /* Output the printed representation of OBJECT, any Lisp object.
696 No quoting characters are used; no delimiters are printed around
697 the contents of strings.
698
699 OBJECT is any of the Lisp data types: a number, a string, a symbol,
700 a list, a buffer, a window, a frame, etc.
701
702 A printed representation of an object is text which describes that object.
703
704 Optional argument PRINTCHARFUN is the output stream, which can be one
705 of these:
706
707 - a buffer, in which case output is inserted into that buffer at point;
708 - a marker, in which case output is inserted at marker's position;
709 - a function, in which case that function is called once for each
710 character of OBJECT's printed representation;
711 - a symbol, in which case that symbol's function definition is called; or
712 - t, in which case the output is displayed in the echo area.
713
714 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
715 is used instead. */)
716 (Lisp_Object object, Lisp_Object printcharfun)
717 {
718 if (NILP (printcharfun))
719 printcharfun = Vstandard_output;
720 PRINTPREPARE;
721 print (object, printcharfun, 0);
722 PRINTFINISH;
723 return object;
724 }
725
726 DEFUN ("print", Fprint, Sprint, 1, 2, 0,
727 doc: /* Output the printed representation of OBJECT, with newlines around it.
728 Quoting characters are printed when needed to make output that `read'
729 can handle, whenever this is possible. For complex objects, the behavior
730 is controlled by `print-level' and `print-length', which see.
731
732 OBJECT is any of the Lisp data types: a number, a string, a symbol,
733 a list, a buffer, a window, a frame, etc.
734
735 A printed representation of an object is text which describes that object.
736
737 Optional argument PRINTCHARFUN is the output stream, which can be one
738 of these:
739
740 - a buffer, in which case output is inserted into that buffer at point;
741 - a marker, in which case output is inserted at marker's position;
742 - a function, in which case that function is called once for each
743 character of OBJECT's printed representation;
744 - a symbol, in which case that symbol's function definition is called; or
745 - t, in which case the output is displayed in the echo area.
746
747 If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
748 is used instead. */)
749 (Lisp_Object object, Lisp_Object printcharfun)
750 {
751 if (NILP (printcharfun))
752 printcharfun = Vstandard_output;
753 PRINTPREPARE;
754 printchar ('\n', printcharfun);
755 print (object, printcharfun, 1);
756 printchar ('\n', printcharfun);
757 PRINTFINISH;
758 return object;
759 }
760
761 DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
762 doc: /* Write CHARACTER to stderr.
763 You can call print while debugging emacs, and pass it this function
764 to make it write to the debugging output. */)
765 (Lisp_Object character)
766 {
767 CHECK_NUMBER (character);
768 printchar_to_stream (XINT (character), stderr);
769 return character;
770 }
771
772 /* This function is never called. Its purpose is to prevent
773 print_output_debug_flag from being optimized away. */
774
775 extern void debug_output_compilation_hack (bool) EXTERNALLY_VISIBLE;
776 void
777 debug_output_compilation_hack (bool x)
778 {
779 print_output_debug_flag = x;
780 }
781
782 DEFUN ("redirect-debugging-output", Fredirect_debugging_output, Sredirect_debugging_output,
783 1, 2,
784 "FDebug output file: \nP",
785 doc: /* Redirect debugging output (stderr stream) to file FILE.
786 If FILE is nil, reset target to the initial stderr stream.
787 Optional arg APPEND non-nil (interactively, with prefix arg) means
788 append to existing target file. */)
789 (Lisp_Object file, Lisp_Object append)
790 {
791 /* If equal to STDERR_FILENO, stderr has not been duplicated and is OK as-is.
792 Otherwise, this is a close-on-exec duplicate of the original stderr. */
793 static int stderr_dup = STDERR_FILENO;
794 int fd = stderr_dup;
795
796 if (! NILP (file))
797 {
798 file = Fexpand_file_name (file, Qnil);
799
800 if (stderr_dup == STDERR_FILENO)
801 {
802 int n = fcntl (STDERR_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
803 if (n < 0)
804 report_file_error ("dup", file);
805 stderr_dup = n;
806 }
807
808 fd = emacs_open (SSDATA (ENCODE_FILE (file)),
809 (O_WRONLY | O_CREAT
810 | (! NILP (append) ? O_APPEND : O_TRUNC)),
811 0666);
812 if (fd < 0)
813 report_file_error ("Cannot open debugging output stream", file);
814 }
815
816 fflush (stderr);
817 if (dup2 (fd, STDERR_FILENO) < 0)
818 report_file_error ("dup2", file);
819 if (fd != stderr_dup)
820 emacs_close (fd);
821 return Qnil;
822 }
823
824
825 /* This is the interface for debugging printing. */
826
827 void
828 debug_print (Lisp_Object arg)
829 {
830 Fprin1 (arg, Qexternal_debugging_output);
831 fprintf (stderr, "\r\n");
832 }
833
834 void safe_debug_print (Lisp_Object) EXTERNALLY_VISIBLE;
835 void
836 safe_debug_print (Lisp_Object arg)
837 {
838 int valid = valid_lisp_object_p (arg);
839
840 if (valid > 0)
841 debug_print (arg);
842 else
843 {
844 EMACS_UINT n = XLI (arg);
845 fprintf (stderr, "#<%s_LISP_OBJECT 0x%08"pI"x>\r\n",
846 !valid ? "INVALID" : "SOME",
847 n);
848 }
849 }
850
851 \f
852 DEFUN ("error-message-string", Ferror_message_string, Serror_message_string,
853 1, 1, 0,
854 doc: /* Convert an error value (ERROR-SYMBOL . DATA) to an error message.
855 See Info anchor `(elisp)Definition of signal' for some details on how this
856 error message is constructed. */)
857 (Lisp_Object obj)
858 {
859 struct buffer *old = current_buffer;
860 Lisp_Object value;
861
862 /* If OBJ is (error STRING), just return STRING.
863 That is not only faster, it also avoids the need to allocate
864 space here when the error is due to memory full. */
865 if (CONSP (obj) && EQ (XCAR (obj), Qerror)
866 && CONSP (XCDR (obj))
867 && STRINGP (XCAR (XCDR (obj)))
868 && NILP (XCDR (XCDR (obj))))
869 return XCAR (XCDR (obj));
870
871 print_error_message (obj, Vprin1_to_string_buffer, 0, Qnil);
872
873 set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
874 value = Fbuffer_string ();
875
876 Ferase_buffer ();
877 set_buffer_internal (old);
878
879 return value;
880 }
881
882 /* Print an error message for the error DATA onto Lisp output stream
883 STREAM (suitable for the print functions).
884 CONTEXT is a C string describing the context of the error.
885 CALLER is the Lisp function inside which the error was signaled. */
886
887 void
888 print_error_message (Lisp_Object data, Lisp_Object stream, const char *context,
889 Lisp_Object caller)
890 {
891 Lisp_Object errname, errmsg, file_error, tail;
892
893 if (context != 0)
894 write_string_1 (context, stream);
895
896 /* If we know from where the error was signaled, show it in
897 *Messages*. */
898 if (!NILP (caller) && SYMBOLP (caller))
899 {
900 Lisp_Object cname = SYMBOL_NAME (caller);
901 ptrdiff_t cnamelen = SBYTES (cname);
902 USE_SAFE_ALLOCA;
903 char *name = SAFE_ALLOCA (cnamelen);
904 memcpy (name, SDATA (cname), cnamelen);
905 message_dolog (name, cnamelen, 0, STRING_MULTIBYTE (cname));
906 message_dolog (": ", 2, 0, 0);
907 SAFE_FREE ();
908 }
909
910 errname = Fcar (data);
911
912 if (EQ (errname, Qerror))
913 {
914 data = Fcdr (data);
915 if (!CONSP (data))
916 data = Qnil;
917 errmsg = Fcar (data);
918 file_error = Qnil;
919 }
920 else
921 {
922 Lisp_Object error_conditions = Fget (errname, Qerror_conditions);
923 errmsg = Fget (errname, Qerror_message);
924 file_error = Fmemq (Qfile_error, error_conditions);
925 }
926
927 /* Print an error message including the data items. */
928
929 tail = Fcdr_safe (data);
930
931 /* For file-error, make error message by concatenating
932 all the data items. They are all strings. */
933 if (!NILP (file_error) && CONSP (tail))
934 errmsg = XCAR (tail), tail = XCDR (tail);
935
936 {
937 const char *sep = ": ";
938
939 if (!STRINGP (errmsg))
940 write_string_1 ("peculiar error", stream);
941 else if (SCHARS (errmsg))
942 Fprinc (Fsubstitute_command_keys (errmsg), stream);
943 else
944 sep = NULL;
945
946 for (; CONSP (tail); tail = XCDR (tail), sep = ", ")
947 {
948 Lisp_Object obj;
949
950 if (sep)
951 write_string_1 (sep, stream);
952 obj = XCAR (tail);
953 if (!NILP (file_error)
954 || EQ (errname, Qend_of_file) || EQ (errname, Quser_error))
955 Fprinc (obj, stream);
956 else
957 Fprin1 (obj, stream);
958 }
959 }
960 }
961
962
963 \f
964 /*
965 * The buffer should be at least as large as the max string size of the
966 * largest float, printed in the biggest notation. This is undoubtedly
967 * 20d float_output_format, with the negative of the C-constant "HUGE"
968 * from <math.h>.
969 *
970 * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
971 *
972 * I assume that IEEE-754 format numbers can take 329 bytes for the worst
973 * case of -1e307 in 20d float_output_format. What is one to do (short of
974 * re-writing _doprnt to be more sane)?
975 * -wsr
976 * Given the above, the buffer must be least FLOAT_TO_STRING_BUFSIZE bytes.
977 */
978
979 int
980 float_to_string (char *buf, double data)
981 {
982 char *cp;
983 int width;
984 int len;
985
986 /* Check for plus infinity in a way that won't lose
987 if there is no plus infinity. */
988 if (data == data / 2 && data > 1.0)
989 {
990 static char const infinity_string[] = "1.0e+INF";
991 strcpy (buf, infinity_string);
992 return sizeof infinity_string - 1;
993 }
994 /* Likewise for minus infinity. */
995 if (data == data / 2 && data < -1.0)
996 {
997 static char const minus_infinity_string[] = "-1.0e+INF";
998 strcpy (buf, minus_infinity_string);
999 return sizeof minus_infinity_string - 1;
1000 }
1001 /* Check for NaN in a way that won't fail if there are no NaNs. */
1002 if (! (data * 0.0 >= 0.0))
1003 {
1004 /* Prepend "-" if the NaN's sign bit is negative.
1005 The sign bit of a double is the bit that is 1 in -0.0. */
1006 static char const NaN_string[] = "0.0e+NaN";
1007 int i;
1008 union { double d; char c[sizeof (double)]; } u_data, u_minus_zero;
1009 bool negative = 0;
1010 u_data.d = data;
1011 u_minus_zero.d = - 0.0;
1012 for (i = 0; i < sizeof (double); i++)
1013 if (u_data.c[i] & u_minus_zero.c[i])
1014 {
1015 *buf = '-';
1016 negative = 1;
1017 break;
1018 }
1019
1020 strcpy (buf + negative, NaN_string);
1021 return negative + sizeof NaN_string - 1;
1022 }
1023
1024 if (NILP (Vfloat_output_format)
1025 || !STRINGP (Vfloat_output_format))
1026 lose:
1027 {
1028 /* Generate the fewest number of digits that represent the
1029 floating point value without losing information. */
1030 len = dtoastr (buf, FLOAT_TO_STRING_BUFSIZE - 2, 0, 0, data);
1031 /* The decimal point must be printed, or the byte compiler can
1032 get confused (Bug#8033). */
1033 width = 1;
1034 }
1035 else /* oink oink */
1036 {
1037 /* Check that the spec we have is fully valid.
1038 This means not only valid for printf,
1039 but meant for floats, and reasonable. */
1040 cp = SSDATA (Vfloat_output_format);
1041
1042 if (cp[0] != '%')
1043 goto lose;
1044 if (cp[1] != '.')
1045 goto lose;
1046
1047 cp += 2;
1048
1049 /* Check the width specification. */
1050 width = -1;
1051 if ('0' <= *cp && *cp <= '9')
1052 {
1053 width = 0;
1054 do
1055 {
1056 width = (width * 10) + (*cp++ - '0');
1057 if (DBL_DIG < width)
1058 goto lose;
1059 }
1060 while (*cp >= '0' && *cp <= '9');
1061
1062 /* A precision of zero is valid only for %f. */
1063 if (width == 0 && *cp != 'f')
1064 goto lose;
1065 }
1066
1067 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1068 goto lose;
1069
1070 if (cp[1] != 0)
1071 goto lose;
1072
1073 len = sprintf (buf, SSDATA (Vfloat_output_format), data);
1074 }
1075
1076 /* Make sure there is a decimal point with digit after, or an
1077 exponent, so that the value is readable as a float. But don't do
1078 this with "%.0f"; it's valid for that not to produce a decimal
1079 point. Note that width can be 0 only for %.0f. */
1080 if (width != 0)
1081 {
1082 for (cp = buf; *cp; cp++)
1083 if ((*cp < '0' || *cp > '9') && *cp != '-')
1084 break;
1085
1086 if (*cp == '.' && cp[1] == 0)
1087 {
1088 cp[1] = '0';
1089 cp[2] = 0;
1090 len++;
1091 }
1092 else if (*cp == 0)
1093 {
1094 *cp++ = '.';
1095 *cp++ = '0';
1096 *cp++ = 0;
1097 len += 2;
1098 }
1099 }
1100
1101 return len;
1102 }
1103
1104 \f
1105 static void
1106 print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1107 {
1108 new_backquote_output = 0;
1109
1110 /* Reset print_number_index and Vprint_number_table only when
1111 the variable Vprint_continuous_numbering is nil. Otherwise,
1112 the values of these variables will be kept between several
1113 print functions. */
1114 if (NILP (Vprint_continuous_numbering)
1115 || NILP (Vprint_number_table))
1116 {
1117 print_number_index = 0;
1118 Vprint_number_table = Qnil;
1119 }
1120
1121 /* Construct Vprint_number_table for print-gensym and print-circle. */
1122 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1123 {
1124 /* Construct Vprint_number_table.
1125 This increments print_number_index for the objects added. */
1126 print_depth = 0;
1127 print_preprocess (obj);
1128
1129 if (HASH_TABLE_P (Vprint_number_table))
1130 { /* Remove unnecessary objects, which appear only once in OBJ;
1131 that is, whose status is Qt. */
1132 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1133 ptrdiff_t i;
1134
1135 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1136 if (!NILP (HASH_HASH (h, i))
1137 && EQ (HASH_VALUE (h, i), Qt))
1138 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1139 }
1140 }
1141
1142 print_depth = 0;
1143 print_object (obj, printcharfun, escapeflag);
1144 }
1145
1146 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1147 (STRINGP (obj) || CONSP (obj) \
1148 || (VECTORLIKEP (obj) \
1149 && (VECTORP (obj) || COMPILEDP (obj) \
1150 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1151 || HASH_TABLE_P (obj) || FONTP (obj))) \
1152 || (! NILP (Vprint_gensym) \
1153 && SYMBOLP (obj) \
1154 && !SYMBOL_INTERNED_P (obj)))
1155
1156 /* Construct Vprint_number_table according to the structure of OBJ.
1157 OBJ itself and all its elements will be added to Vprint_number_table
1158 recursively if it is a list, vector, compiled function, char-table,
1159 string (its text properties will be traced), or a symbol that has
1160 no obarray (this is for the print-gensym feature).
1161 The status fields of Vprint_number_table mean whether each object appears
1162 more than once in OBJ: Qnil at the first time, and Qt after that. */
1163 static void
1164 print_preprocess (Lisp_Object obj)
1165 {
1166 int i;
1167 ptrdiff_t size;
1168 int loop_count = 0;
1169 Lisp_Object halftail;
1170
1171 /* Avoid infinite recursion for circular nested structure
1172 in the case where Vprint_circle is nil. */
1173 if (NILP (Vprint_circle))
1174 {
1175 /* Give up if we go so deep that print_object will get an error. */
1176 /* See similar code in print_object. */
1177 if (print_depth >= PRINT_CIRCLE)
1178 error ("Apparently circular structure being printed");
1179
1180 for (i = 0; i < print_depth; i++)
1181 if (EQ (obj, being_printed[i]))
1182 return;
1183 being_printed[print_depth] = obj;
1184 }
1185
1186 print_depth++;
1187 halftail = obj;
1188
1189 loop:
1190 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1191 {
1192 if (!HASH_TABLE_P (Vprint_number_table))
1193 Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
1194
1195 /* In case print-circle is nil and print-gensym is t,
1196 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1197 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1198 {
1199 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1200 if (!NILP (num)
1201 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1202 always print the gensym with a number. This is a special for
1203 the lisp function byte-compile-output-docform. */
1204 || (!NILP (Vprint_continuous_numbering)
1205 && SYMBOLP (obj)
1206 && !SYMBOL_INTERNED_P (obj)))
1207 { /* OBJ appears more than once. Let's remember that. */
1208 if (!INTEGERP (num))
1209 {
1210 print_number_index++;
1211 /* Negative number indicates it hasn't been printed yet. */
1212 Fputhash (obj, make_number (- print_number_index),
1213 Vprint_number_table);
1214 }
1215 print_depth--;
1216 return;
1217 }
1218 else
1219 /* OBJ is not yet recorded. Let's add to the table. */
1220 Fputhash (obj, Qt, Vprint_number_table);
1221 }
1222
1223 switch (XTYPE (obj))
1224 {
1225 case Lisp_String:
1226 /* A string may have text properties, which can be circular. */
1227 traverse_intervals_noorder (string_intervals (obj),
1228 print_preprocess_string, Qnil);
1229 break;
1230
1231 case Lisp_Cons:
1232 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1233 just as in print_object. */
1234 if (loop_count && EQ (obj, halftail))
1235 break;
1236 print_preprocess (XCAR (obj));
1237 obj = XCDR (obj);
1238 loop_count++;
1239 if (!(loop_count & 1))
1240 halftail = XCDR (halftail);
1241 goto loop;
1242
1243 case Lisp_Vectorlike:
1244 size = ASIZE (obj);
1245 if (size & PSEUDOVECTOR_FLAG)
1246 size &= PSEUDOVECTOR_SIZE_MASK;
1247 for (i = (SUB_CHAR_TABLE_P (obj)
1248 ? SUB_CHAR_TABLE_OFFSET : 0); i < size; i++)
1249 print_preprocess (AREF (obj, i));
1250 if (HASH_TABLE_P (obj))
1251 { /* For hash tables, the key_and_value slot is past
1252 `size' because it needs to be marked specially in case
1253 the table is weak. */
1254 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1255 print_preprocess (h->key_and_value);
1256 }
1257 break;
1258
1259 default:
1260 break;
1261 }
1262 }
1263 print_depth--;
1264 }
1265
1266 static void
1267 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1268 {
1269 print_preprocess (interval->plist);
1270 }
1271
1272 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1273
1274 #define PRINT_STRING_NON_CHARSET_FOUND 1
1275 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1276
1277 /* Bitwise or of the above macros. */
1278 static int print_check_string_result;
1279
1280 static void
1281 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1282 {
1283 Lisp_Object val;
1284
1285 if (NILP (interval->plist)
1286 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1287 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1288 return;
1289 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1290 val = XCDR (XCDR (val)));
1291 if (! CONSP (val))
1292 {
1293 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1294 return;
1295 }
1296 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1297 {
1298 if (! EQ (val, interval->plist)
1299 || CONSP (XCDR (XCDR (val))))
1300 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1301 }
1302 if (NILP (Vprint_charset_text_property)
1303 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1304 {
1305 int i, c;
1306 ptrdiff_t charpos = interval->position;
1307 ptrdiff_t bytepos = string_char_to_byte (string, charpos);
1308 Lisp_Object charset;
1309
1310 charset = XCAR (XCDR (val));
1311 for (i = 0; i < LENGTH (interval); i++)
1312 {
1313 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1314 if (! ASCII_CHAR_P (c)
1315 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1316 {
1317 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1318 break;
1319 }
1320 }
1321 }
1322 }
1323
1324 /* The value is (charset . nil). */
1325 static Lisp_Object print_prune_charset_plist;
1326
1327 static Lisp_Object
1328 print_prune_string_charset (Lisp_Object string)
1329 {
1330 print_check_string_result = 0;
1331 traverse_intervals (string_intervals (string), 0,
1332 print_check_string_charset_prop, string);
1333 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1334 {
1335 string = Fcopy_sequence (string);
1336 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1337 {
1338 if (NILP (print_prune_charset_plist))
1339 print_prune_charset_plist = list1 (Qcharset);
1340 Fremove_text_properties (make_number (0),
1341 make_number (SCHARS (string)),
1342 print_prune_charset_plist, string);
1343 }
1344 else
1345 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1346 Qnil, string);
1347 }
1348 return string;
1349 }
1350
1351 static void
1352 print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1353 {
1354 char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
1355 max (sizeof " . #" + INT_STRLEN_BOUND (printmax_t),
1356 40))];
1357
1358 QUIT;
1359
1360 /* Detect circularities and truncate them. */
1361 if (NILP (Vprint_circle))
1362 {
1363 /* Simple but incomplete way. */
1364 int i;
1365
1366 /* See similar code in print_preprocess. */
1367 if (print_depth >= PRINT_CIRCLE)
1368 error ("Apparently circular structure being printed");
1369
1370 for (i = 0; i < print_depth; i++)
1371 if (EQ (obj, being_printed[i]))
1372 {
1373 int len = sprintf (buf, "#%d", i);
1374 strout (buf, len, len, printcharfun);
1375 return;
1376 }
1377 being_printed[print_depth] = obj;
1378 }
1379 else if (PRINT_CIRCLE_CANDIDATE_P (obj))
1380 {
1381 /* With the print-circle feature. */
1382 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1383 if (INTEGERP (num))
1384 {
1385 EMACS_INT n = XINT (num);
1386 if (n < 0)
1387 { /* Add a prefix #n= if OBJ has not yet been printed;
1388 that is, its status field is nil. */
1389 int len = sprintf (buf, "#%"pI"d=", -n);
1390 strout (buf, len, len, printcharfun);
1391 /* OBJ is going to be printed. Remember that fact. */
1392 Fputhash (obj, make_number (- n), Vprint_number_table);
1393 }
1394 else
1395 {
1396 /* Just print #n# if OBJ has already been printed. */
1397 int len = sprintf (buf, "#%"pI"d#", n);
1398 strout (buf, len, len, printcharfun);
1399 return;
1400 }
1401 }
1402 }
1403
1404 print_depth++;
1405
1406 switch (XTYPE (obj))
1407 {
1408 case_Lisp_Int:
1409 {
1410 int len = sprintf (buf, "%"pI"d", XINT (obj));
1411 strout (buf, len, len, printcharfun);
1412 }
1413 break;
1414
1415 case Lisp_Float:
1416 {
1417 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1418 int len = float_to_string (pigbuf, XFLOAT_DATA (obj));
1419 strout (pigbuf, len, len, printcharfun);
1420 }
1421 break;
1422
1423 case Lisp_String:
1424 if (!escapeflag)
1425 print_string (obj, printcharfun);
1426 else
1427 {
1428 ptrdiff_t i, i_byte;
1429 ptrdiff_t size_byte;
1430 /* True means we must ensure that the next character we output
1431 cannot be taken as part of a hex character escape. */
1432 bool need_nonhex = false;
1433 bool multibyte = STRING_MULTIBYTE (obj);
1434
1435 if (! EQ (Vprint_charset_text_property, Qt))
1436 obj = print_prune_string_charset (obj);
1437
1438 if (string_intervals (obj))
1439 print_c_string ("#(", printcharfun);
1440
1441 printchar ('\"', printcharfun);
1442 size_byte = SBYTES (obj);
1443
1444 for (i = 0, i_byte = 0; i_byte < size_byte;)
1445 {
1446 /* Here, we must convert each multi-byte form to the
1447 corresponding character code before handing it to printchar. */
1448 int c;
1449
1450 FETCH_STRING_CHAR_ADVANCE (c, obj, i, i_byte);
1451
1452 QUIT;
1453
1454 if (multibyte
1455 ? (CHAR_BYTE8_P (c) && (c = CHAR_TO_BYTE8 (c), true))
1456 : (SINGLE_BYTE_CHAR_P (c) && ! ASCII_CHAR_P (c)
1457 && print_escape_nonascii))
1458 {
1459 /* When printing a raw 8-bit byte in a multibyte buffer, or
1460 (when requested) a non-ASCII character in a unibyte buffer,
1461 print single-byte non-ASCII string chars
1462 using octal escapes. */
1463 char outbuf[5];
1464 int len = sprintf (outbuf, "\\%03o", c + 0u);
1465 strout (outbuf, len, len, printcharfun);
1466 need_nonhex = false;
1467 }
1468 else if (multibyte
1469 && ! ASCII_CHAR_P (c) && print_escape_multibyte)
1470 {
1471 /* When requested, print multibyte chars using hex escapes. */
1472 char outbuf[sizeof "\\x" + INT_STRLEN_BOUND (c)];
1473 int len = sprintf (outbuf, "\\x%04x", c + 0u);
1474 strout (outbuf, len, len, printcharfun);
1475 need_nonhex = true;
1476 }
1477 else
1478 {
1479 /* If we just had a hex escape, and this character
1480 could be taken as part of it,
1481 output `\ ' to prevent that. */
1482 if (need_nonhex && c_isxdigit (c))
1483 print_c_string ("\\ ", printcharfun);
1484
1485 if (c == '\n' && print_escape_newlines
1486 ? (c = 'n', true)
1487 : c == '\f' && print_escape_newlines
1488 ? (c = 'f', true)
1489 : c == '\"' || c == '\\')
1490 printchar ('\\', printcharfun);
1491
1492 printchar (c, printcharfun);
1493 need_nonhex = false;
1494 }
1495 }
1496 printchar ('\"', printcharfun);
1497
1498 if (string_intervals (obj))
1499 {
1500 traverse_intervals (string_intervals (obj),
1501 0, print_interval, printcharfun);
1502 printchar (')', printcharfun);
1503 }
1504 }
1505 break;
1506
1507 case Lisp_Symbol:
1508 {
1509 bool confusing;
1510 unsigned char *p = SDATA (SYMBOL_NAME (obj));
1511 unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1512 int c;
1513 ptrdiff_t i, i_byte;
1514 ptrdiff_t size_byte;
1515 Lisp_Object name;
1516
1517 name = SYMBOL_NAME (obj);
1518
1519 if (p != end && (*p == '-' || *p == '+')) p++;
1520 if (p == end)
1521 confusing = 0;
1522 /* If symbol name begins with a digit, and ends with a digit,
1523 and contains nothing but digits and `e', it could be treated
1524 as a number. So set CONFUSING.
1525
1526 Symbols that contain periods could also be taken as numbers,
1527 but periods are always escaped, so we don't have to worry
1528 about them here. */
1529 else if (*p >= '0' && *p <= '9'
1530 && end[-1] >= '0' && end[-1] <= '9')
1531 {
1532 while (p != end && ((*p >= '0' && *p <= '9')
1533 /* Needed for \2e10. */
1534 || *p == 'e' || *p == 'E'))
1535 p++;
1536 confusing = (end == p);
1537 }
1538 else
1539 confusing = 0;
1540
1541 size_byte = SBYTES (name);
1542
1543 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1544 print_c_string ("#:", printcharfun);
1545 else if (size_byte == 0)
1546 {
1547 print_c_string ("##", printcharfun);
1548 break;
1549 }
1550
1551 for (i = 0, i_byte = 0; i_byte < size_byte;)
1552 {
1553 /* Here, we must convert each multi-byte form to the
1554 corresponding character code before handing it to PRINTCHAR. */
1555 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1556 QUIT;
1557
1558 if (escapeflag)
1559 {
1560 if (c == '\"' || c == '\\' || c == '\''
1561 || c == ';' || c == '#' || c == '(' || c == ')'
1562 || c == ',' || c == '.' || c == '`'
1563 || c == '[' || c == ']' || c == '?' || c <= 040
1564 || confusing)
1565 {
1566 printchar ('\\', printcharfun);
1567 confusing = false;
1568 }
1569 }
1570 printchar (c, printcharfun);
1571 }
1572 }
1573 break;
1574
1575 case Lisp_Cons:
1576 /* If deeper than spec'd depth, print placeholder. */
1577 if (INTEGERP (Vprint_level)
1578 && print_depth > XINT (Vprint_level))
1579 print_c_string ("...", printcharfun);
1580 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1581 && EQ (XCAR (obj), Qquote))
1582 {
1583 printchar ('\'', printcharfun);
1584 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1585 }
1586 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1587 && EQ (XCAR (obj), Qfunction))
1588 {
1589 print_c_string ("#'", printcharfun);
1590 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1591 }
1592 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1593 && EQ (XCAR (obj), Qbackquote))
1594 {
1595 printchar ('`', printcharfun);
1596 new_backquote_output++;
1597 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1598 new_backquote_output--;
1599 }
1600 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1601 && new_backquote_output
1602 && (EQ (XCAR (obj), Qcomma)
1603 || EQ (XCAR (obj), Qcomma_at)
1604 || EQ (XCAR (obj), Qcomma_dot)))
1605 {
1606 print_object (XCAR (obj), printcharfun, false);
1607 new_backquote_output--;
1608 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1609 new_backquote_output++;
1610 }
1611 else
1612 {
1613 printchar ('(', printcharfun);
1614
1615 Lisp_Object halftail = obj;
1616
1617 /* Negative values of print-length are invalid in CL.
1618 Treat them like nil, as CMUCL does. */
1619 printmax_t print_length = (NATNUMP (Vprint_length)
1620 ? XFASTINT (Vprint_length)
1621 : TYPE_MAXIMUM (printmax_t));
1622
1623 printmax_t i = 0;
1624 while (CONSP (obj))
1625 {
1626 /* Detect circular list. */
1627 if (NILP (Vprint_circle))
1628 {
1629 /* Simple but incomplete way. */
1630 if (i != 0 && EQ (obj, halftail))
1631 {
1632 int len = sprintf (buf, " . #%"pMd, i / 2);
1633 strout (buf, len, len, printcharfun);
1634 goto end_of_list;
1635 }
1636 }
1637 else
1638 {
1639 /* With the print-circle feature. */
1640 if (i != 0)
1641 {
1642 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1643 if (INTEGERP (num))
1644 {
1645 print_c_string (" . ", printcharfun);
1646 print_object (obj, printcharfun, escapeflag);
1647 goto end_of_list;
1648 }
1649 }
1650 }
1651
1652 if (i)
1653 printchar (' ', printcharfun);
1654
1655 if (print_length <= i)
1656 {
1657 print_c_string ("...", printcharfun);
1658 goto end_of_list;
1659 }
1660
1661 i++;
1662 print_object (XCAR (obj), printcharfun, escapeflag);
1663
1664 obj = XCDR (obj);
1665 if (!(i & 1))
1666 halftail = XCDR (halftail);
1667 }
1668
1669 /* OBJ non-nil here means it's the end of a dotted list. */
1670 if (!NILP (obj))
1671 {
1672 print_c_string (" . ", printcharfun);
1673 print_object (obj, printcharfun, escapeflag);
1674 }
1675
1676 end_of_list:
1677 printchar (')', printcharfun);
1678 }
1679 break;
1680
1681 case Lisp_Vectorlike:
1682 if (PROCESSP (obj))
1683 {
1684 if (escapeflag)
1685 {
1686 print_c_string ("#<process ", printcharfun);
1687 print_string (XPROCESS (obj)->name, printcharfun);
1688 printchar ('>', printcharfun);
1689 }
1690 else
1691 print_string (XPROCESS (obj)->name, printcharfun);
1692 }
1693 else if (BOOL_VECTOR_P (obj))
1694 {
1695 ptrdiff_t i;
1696 unsigned char c;
1697 EMACS_INT size = bool_vector_size (obj);
1698 ptrdiff_t size_in_chars = bool_vector_bytes (size);
1699 ptrdiff_t real_size_in_chars = size_in_chars;
1700
1701 int len = sprintf (buf, "#&%"pI"d\"", size);
1702 strout (buf, len, len, printcharfun);
1703
1704 /* Don't print more characters than the specified maximum.
1705 Negative values of print-length are invalid. Treat them
1706 like a print-length of nil. */
1707 if (NATNUMP (Vprint_length)
1708 && XFASTINT (Vprint_length) < size_in_chars)
1709 size_in_chars = XFASTINT (Vprint_length);
1710
1711 for (i = 0; i < size_in_chars; i++)
1712 {
1713 QUIT;
1714 c = bool_vector_uchar_data (obj)[i];
1715 if (c == '\n' && print_escape_newlines)
1716 print_c_string ("\\n", printcharfun);
1717 else if (c == '\f' && print_escape_newlines)
1718 print_c_string ("\\f", printcharfun);
1719 else if (c > '\177')
1720 {
1721 /* Use octal escapes to avoid encoding issues. */
1722 len = sprintf (buf, "\\%o", c);
1723 strout (buf, len, len, printcharfun);
1724 }
1725 else
1726 {
1727 if (c == '\"' || c == '\\')
1728 printchar ('\\', printcharfun);
1729 printchar (c, printcharfun);
1730 }
1731 }
1732
1733 if (size_in_chars < real_size_in_chars)
1734 print_c_string (" ...", printcharfun);
1735 printchar ('\"', printcharfun);
1736 }
1737 else if (SUBRP (obj))
1738 {
1739 print_c_string ("#<subr ", printcharfun);
1740 print_c_string (XSUBR (obj)->symbol_name, printcharfun);
1741 printchar ('>', printcharfun);
1742 }
1743 else if (XWIDGETP (obj) || XWIDGET_VIEW_P (obj))
1744 {
1745 print_c_string ("#<xwidget ", printcharfun);
1746 printchar ('>', printcharfun);
1747 }
1748 else if (WINDOWP (obj))
1749 {
1750 int len = sprintf (buf, "#<window %"pI"d",
1751 XWINDOW (obj)->sequence_number);
1752 strout (buf, len, len, printcharfun);
1753 if (BUFFERP (XWINDOW (obj)->contents))
1754 {
1755 print_c_string (" on ", printcharfun);
1756 print_string (BVAR (XBUFFER (XWINDOW (obj)->contents), name),
1757 printcharfun);
1758 }
1759 printchar ('>', printcharfun);
1760 }
1761 else if (TERMINALP (obj))
1762 {
1763 struct terminal *t = XTERMINAL (obj);
1764 int len = sprintf (buf, "#<terminal %d", t->id);
1765 strout (buf, len, len, printcharfun);
1766 if (t->name)
1767 {
1768 print_c_string (" on ", printcharfun);
1769 print_c_string (t->name, printcharfun);
1770 }
1771 printchar ('>', printcharfun);
1772 }
1773 else if (HASH_TABLE_P (obj))
1774 {
1775 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1776 ptrdiff_t i;
1777 ptrdiff_t real_size, size;
1778 int len;
1779 #if 0
1780 void *ptr = h;
1781 print_c_string ("#<hash-table", printcharfun);
1782 if (SYMBOLP (h->test))
1783 {
1784 print_c_string (" '", printcharfun);
1785 print_c_string (SSDATA (SYMBOL_NAME (h->test)), printcharfun);
1786 printchar (' ', printcharfun);
1787 print_c_string (SSDATA (SYMBOL_NAME (h->weak)), printcharfun);
1788 len = sprintf (buf, " %"pD"d/%"pD"d", h->count, ASIZE (h->next));
1789 strout (buf, len, len, printcharfun);
1790 }
1791 len = sprintf (buf, " %p>", ptr);
1792 strout (buf, len, len, printcharfun);
1793 #endif
1794 /* Implement a readable output, e.g.:
1795 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1796 /* Always print the size. */
1797 len = sprintf (buf, "#s(hash-table size %"pD"d", ASIZE (h->next));
1798 strout (buf, len, len, printcharfun);
1799
1800 if (!NILP (h->test.name))
1801 {
1802 print_c_string (" test ", printcharfun);
1803 print_object (h->test.name, printcharfun, escapeflag);
1804 }
1805
1806 if (!NILP (h->weak))
1807 {
1808 print_c_string (" weakness ", printcharfun);
1809 print_object (h->weak, printcharfun, escapeflag);
1810 }
1811
1812 if (!NILP (h->rehash_size))
1813 {
1814 print_c_string (" rehash-size ", printcharfun);
1815 print_object (h->rehash_size, printcharfun, escapeflag);
1816 }
1817
1818 if (!NILP (h->rehash_threshold))
1819 {
1820 print_c_string (" rehash-threshold ", printcharfun);
1821 print_object (h->rehash_threshold, printcharfun, escapeflag);
1822 }
1823
1824 print_c_string (" data ", printcharfun);
1825
1826 /* Print the data here as a plist. */
1827 real_size = HASH_TABLE_SIZE (h);
1828 size = real_size;
1829
1830 /* Don't print more elements than the specified maximum. */
1831 if (NATNUMP (Vprint_length)
1832 && XFASTINT (Vprint_length) < size)
1833 size = XFASTINT (Vprint_length);
1834
1835 printchar ('(', printcharfun);
1836 for (i = 0; i < size; i++)
1837 if (!NILP (HASH_HASH (h, i)))
1838 {
1839 if (i) printchar (' ', printcharfun);
1840 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1841 printchar (' ', printcharfun);
1842 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1843 }
1844
1845 if (size < real_size)
1846 print_c_string (" ...", printcharfun);
1847
1848 print_c_string ("))", printcharfun);
1849
1850 }
1851 else if (BUFFERP (obj))
1852 {
1853 if (!BUFFER_LIVE_P (XBUFFER (obj)))
1854 print_c_string ("#<killed buffer>", printcharfun);
1855 else if (escapeflag)
1856 {
1857 print_c_string ("#<buffer ", printcharfun);
1858 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1859 printchar ('>', printcharfun);
1860 }
1861 else
1862 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1863 }
1864 else if (WINDOW_CONFIGURATIONP (obj))
1865 print_c_string ("#<window-configuration>", printcharfun);
1866 else if (FRAMEP (obj))
1867 {
1868 int len;
1869 void *ptr = XFRAME (obj);
1870 Lisp_Object frame_name = XFRAME (obj)->name;
1871
1872 print_c_string ((FRAME_LIVE_P (XFRAME (obj))
1873 ? "#<frame "
1874 : "#<dead frame "),
1875 printcharfun);
1876 if (!STRINGP (frame_name))
1877 {
1878 /* A frame could be too young and have no name yet;
1879 don't crash. */
1880 if (SYMBOLP (frame_name))
1881 frame_name = Fsymbol_name (frame_name);
1882 else /* can't happen: name should be either nil or string */
1883 frame_name = build_string ("*INVALID*FRAME*NAME*");
1884 }
1885 print_string (frame_name, printcharfun);
1886 len = sprintf (buf, " %p>", ptr);
1887 strout (buf, len, len, printcharfun);
1888 }
1889 else if (FONTP (obj))
1890 {
1891 int i;
1892
1893 if (! FONT_OBJECT_P (obj))
1894 {
1895 if (FONT_SPEC_P (obj))
1896 print_c_string ("#<font-spec", printcharfun);
1897 else
1898 print_c_string ("#<font-entity", printcharfun);
1899 for (i = 0; i < FONT_SPEC_MAX; i++)
1900 {
1901 printchar (' ', printcharfun);
1902 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1903 print_object (AREF (obj, i), printcharfun, escapeflag);
1904 else
1905 print_object (font_style_symbolic (obj, i, 0),
1906 printcharfun, escapeflag);
1907 }
1908 }
1909 else
1910 {
1911 print_c_string ("#<font-object ", printcharfun);
1912 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1913 escapeflag);
1914 }
1915 printchar ('>', printcharfun);
1916 }
1917 else
1918 {
1919 ptrdiff_t size = ASIZE (obj);
1920 if (COMPILEDP (obj))
1921 {
1922 printchar ('#', printcharfun);
1923 size &= PSEUDOVECTOR_SIZE_MASK;
1924 }
1925 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1926 {
1927 /* We print a char-table as if it were a vector,
1928 lumping the parent and default slots in with the
1929 character slots. But we add #^ as a prefix. */
1930
1931 /* Make each lowest sub_char_table start a new line.
1932 Otherwise we'll make a line extremely long, which
1933 results in slow redisplay. */
1934 if (SUB_CHAR_TABLE_P (obj)
1935 && XSUB_CHAR_TABLE (obj)->depth == 3)
1936 printchar ('\n', printcharfun);
1937 print_c_string ("#^", printcharfun);
1938 if (SUB_CHAR_TABLE_P (obj))
1939 printchar ('^', printcharfun);
1940 size &= PSEUDOVECTOR_SIZE_MASK;
1941 }
1942 if (size & PSEUDOVECTOR_FLAG)
1943 goto badtype;
1944
1945 printchar ('[', printcharfun);
1946 {
1947 int i, idx = SUB_CHAR_TABLE_P (obj) ? SUB_CHAR_TABLE_OFFSET : 0;
1948 Lisp_Object tem;
1949 ptrdiff_t real_size = size;
1950
1951 /* For a sub char-table, print heading non-Lisp data first. */
1952 if (SUB_CHAR_TABLE_P (obj))
1953 {
1954 i = sprintf (buf, "%d %d", XSUB_CHAR_TABLE (obj)->depth,
1955 XSUB_CHAR_TABLE (obj)->min_char);
1956 strout (buf, i, i, printcharfun);
1957 }
1958
1959 /* Don't print more elements than the specified maximum. */
1960 if (NATNUMP (Vprint_length)
1961 && XFASTINT (Vprint_length) < size)
1962 size = XFASTINT (Vprint_length);
1963
1964 for (i = idx; i < size; i++)
1965 {
1966 if (i) printchar (' ', printcharfun);
1967 tem = AREF (obj, i);
1968 print_object (tem, printcharfun, escapeflag);
1969 }
1970 if (size < real_size)
1971 print_c_string (" ...", printcharfun);
1972 }
1973 printchar (']', printcharfun);
1974 }
1975 break;
1976
1977 case Lisp_Misc:
1978 switch (XMISCTYPE (obj))
1979 {
1980 case Lisp_Misc_Marker:
1981 print_c_string ("#<marker ", printcharfun);
1982 /* Do you think this is necessary? */
1983 if (XMARKER (obj)->insertion_type != 0)
1984 print_c_string ("(moves after insertion) ", printcharfun);
1985 if (! XMARKER (obj)->buffer)
1986 print_c_string ("in no buffer", printcharfun);
1987 else
1988 {
1989 int len = sprintf (buf, "at %"pD"d in ", marker_position (obj));
1990 strout (buf, len, len, printcharfun);
1991 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1992 }
1993 printchar ('>', printcharfun);
1994 break;
1995
1996 case Lisp_Misc_Overlay:
1997 print_c_string ("#<overlay ", printcharfun);
1998 if (! XMARKER (OVERLAY_START (obj))->buffer)
1999 print_c_string ("in no buffer", printcharfun);
2000 else
2001 {
2002 int len = sprintf (buf, "from %"pD"d to %"pD"d in ",
2003 marker_position (OVERLAY_START (obj)),
2004 marker_position (OVERLAY_END (obj)));
2005 strout (buf, len, len, printcharfun);
2006 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2007 printcharfun);
2008 }
2009 printchar ('>', printcharfun);
2010 break;
2011
2012 #ifdef HAVE_MODULES
2013 case Lisp_Misc_User_Ptr:
2014 {
2015 print_c_string ("#<user-ptr ", printcharfun);
2016 int i = sprintf (buf, "ptr=%p finalizer=%p",
2017 XUSER_PTR (obj)->p,
2018 XUSER_PTR (obj)->finalizer);
2019 strout (buf, i, i, printcharfun);
2020 printchar ('>', printcharfun);
2021 break;
2022 }
2023 #endif
2024
2025 case Lisp_Misc_Finalizer:
2026 print_c_string ("#<finalizer", printcharfun);
2027 if (NILP (XFINALIZER (obj)->function))
2028 print_c_string (" used", printcharfun);
2029 printchar ('>', printcharfun);
2030 break;
2031
2032 /* Remaining cases shouldn't happen in normal usage, but let's
2033 print them anyway for the benefit of the debugger. */
2034
2035 case Lisp_Misc_Free:
2036 print_c_string ("#<misc free cell>", printcharfun);
2037 break;
2038
2039 case Lisp_Misc_Save_Value:
2040 {
2041 int i;
2042 struct Lisp_Save_Value *v = XSAVE_VALUE (obj);
2043
2044 print_c_string ("#<save-value ", printcharfun);
2045
2046 if (v->save_type == SAVE_TYPE_MEMORY)
2047 {
2048 ptrdiff_t amount = v->data[1].integer;
2049
2050 /* valid_lisp_object_p is reliable, so try to print up
2051 to 8 saved objects. This code is rarely used, so
2052 it's OK that valid_lisp_object_p is slow. */
2053
2054 int limit = min (amount, 8);
2055 Lisp_Object *area = v->data[0].pointer;
2056
2057 i = sprintf (buf, "with %"pD"d objects", amount);
2058 strout (buf, i, i, printcharfun);
2059
2060 for (i = 0; i < limit; i++)
2061 {
2062 Lisp_Object maybe = area[i];
2063 int valid = valid_lisp_object_p (maybe);
2064
2065 printchar (' ', printcharfun);
2066 if (0 < valid)
2067 print_object (maybe, printcharfun, escapeflag);
2068 else
2069 print_c_string (valid < 0 ? "<some>" : "<invalid>",
2070 printcharfun);
2071 }
2072 if (i == limit && i < amount)
2073 print_c_string (" ...", printcharfun);
2074 }
2075 else
2076 {
2077 /* Print each slot according to its type. */
2078 int index;
2079 for (index = 0; index < SAVE_VALUE_SLOTS; index++)
2080 {
2081 if (index)
2082 printchar (' ', printcharfun);
2083
2084 switch (save_type (v, index))
2085 {
2086 case SAVE_UNUSED:
2087 i = sprintf (buf, "<unused>");
2088 break;
2089
2090 case SAVE_POINTER:
2091 i = sprintf (buf, "<pointer %p>",
2092 v->data[index].pointer);
2093 break;
2094
2095 case SAVE_FUNCPOINTER:
2096 i = sprintf (buf, "<funcpointer %p>",
2097 ((void *) (intptr_t)
2098 v->data[index].funcpointer));
2099 break;
2100
2101 case SAVE_INTEGER:
2102 i = sprintf (buf, "<integer %"pD"d>",
2103 v->data[index].integer);
2104 break;
2105
2106 case SAVE_OBJECT:
2107 print_object (v->data[index].object, printcharfun,
2108 escapeflag);
2109 continue;
2110
2111 default:
2112 emacs_abort ();
2113 }
2114
2115 strout (buf, i, i, printcharfun);
2116 }
2117 }
2118 printchar ('>', printcharfun);
2119 }
2120 break;
2121
2122 default:
2123 goto badtype;
2124 }
2125 break;
2126
2127 default:
2128 badtype:
2129 {
2130 int len;
2131 /* We're in trouble if this happens!
2132 Probably should just emacs_abort (). */
2133 print_c_string ("#<EMACS BUG: INVALID DATATYPE ", printcharfun);
2134 if (MISCP (obj))
2135 len = sprintf (buf, "(MISC 0x%04x)", (unsigned) XMISCTYPE (obj));
2136 else if (VECTORLIKEP (obj))
2137 len = sprintf (buf, "(PVEC 0x%08zx)", (size_t) ASIZE (obj));
2138 else
2139 len = sprintf (buf, "(0x%02x)", (unsigned) XTYPE (obj));
2140 strout (buf, len, len, printcharfun);
2141 print_c_string ((" Save your buffers immediately"
2142 " and please report this bug>"),
2143 printcharfun);
2144 }
2145 }
2146
2147 print_depth--;
2148 }
2149 \f
2150
2151 /* Print a description of INTERVAL using PRINTCHARFUN.
2152 This is part of printing a string that has text properties. */
2153
2154 static void
2155 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2156 {
2157 if (NILP (interval->plist))
2158 return;
2159 printchar (' ', printcharfun);
2160 print_object (make_number (interval->position), printcharfun, 1);
2161 printchar (' ', printcharfun);
2162 print_object (make_number (interval->position + LENGTH (interval)),
2163 printcharfun, 1);
2164 printchar (' ', printcharfun);
2165 print_object (interval->plist, printcharfun, 1);
2166 }
2167
2168 /* Initialize debug_print stuff early to have it working from the very
2169 beginning. */
2170
2171 void
2172 init_print_once (void)
2173 {
2174 /* The subroutine object for external-debugging-output is kept here
2175 for the convenience of the debugger. */
2176 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2177
2178 defsubr (&Sexternal_debugging_output);
2179 }
2180
2181 void
2182 syms_of_print (void)
2183 {
2184 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2185
2186 DEFVAR_LISP ("standard-output", Vstandard_output,
2187 doc: /* Output stream `print' uses by default for outputting a character.
2188 This may be any function of one argument.
2189 It may also be a buffer (output is inserted before point)
2190 or a marker (output is inserted and the marker is advanced)
2191 or the symbol t (output appears in the echo area). */);
2192 Vstandard_output = Qt;
2193 DEFSYM (Qstandard_output, "standard-output");
2194
2195 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2196 doc: /* The format descriptor string used to print floats.
2197 This is a %-spec like those accepted by `printf' in C,
2198 but with some restrictions. It must start with the two characters `%.'.
2199 After that comes an integer precision specification,
2200 and then a letter which controls the format.
2201 The letters allowed are `e', `f' and `g'.
2202 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2203 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2204 Use `g' to choose the shorter of those two formats for the number at hand.
2205 The precision in any of these cases is the number of digits following
2206 the decimal point. With `f', a precision of 0 means to omit the
2207 decimal point. 0 is not allowed with `e' or `g'.
2208
2209 A value of nil means to use the shortest notation
2210 that represents the number without losing information. */);
2211 Vfloat_output_format = Qnil;
2212
2213 DEFVAR_LISP ("print-length", Vprint_length,
2214 doc: /* Maximum length of list to print before abbreviating.
2215 A value of nil means no limit. See also `eval-expression-print-length'. */);
2216 Vprint_length = Qnil;
2217
2218 DEFVAR_LISP ("print-level", Vprint_level,
2219 doc: /* Maximum depth of list nesting to print before abbreviating.
2220 A value of nil means no limit. See also `eval-expression-print-level'. */);
2221 Vprint_level = Qnil;
2222
2223 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2224 doc: /* Non-nil means print newlines in strings as `\\n'.
2225 Also print formfeeds as `\\f'. */);
2226 print_escape_newlines = 0;
2227
2228 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2229 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2230 \(OOO is the octal representation of the character code.)
2231 Only single-byte characters are affected, and only in `prin1'.
2232 When the output goes in a multibyte buffer, this feature is
2233 enabled regardless of the value of the variable. */);
2234 print_escape_nonascii = 0;
2235
2236 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2237 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2238 \(XXXX is the hex representation of the character code.)
2239 This affects only `prin1'. */);
2240 print_escape_multibyte = 0;
2241
2242 DEFVAR_BOOL ("print-quoted", print_quoted,
2243 doc: /* Non-nil means print quoted forms with reader syntax.
2244 I.e., (quote foo) prints as \\='foo, (function foo) as #\\='foo. */);
2245 print_quoted = 0;
2246
2247 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2248 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2249 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2250 When the uninterned symbol appears within a recursive data structure,
2251 and the symbol appears more than once, in addition use the #N# and #N=
2252 constructs as needed, so that multiple references to the same symbol are
2253 shared once again when the text is read back. */);
2254 Vprint_gensym = Qnil;
2255
2256 DEFVAR_LISP ("print-circle", Vprint_circle,
2257 doc: /* Non-nil means print recursive structures using #N= and #N# syntax.
2258 If nil, printing proceeds recursively and may lead to
2259 `max-lisp-eval-depth' being exceeded or an error may occur:
2260 \"Apparently circular structure being printed.\" Also see
2261 `print-length' and `print-level'.
2262 If non-nil, shared substructures anywhere in the structure are printed
2263 with `#N=' before the first occurrence (in the order of the print
2264 representation) and `#N#' in place of each subsequent occurrence,
2265 where N is a positive decimal integer. */);
2266 Vprint_circle = Qnil;
2267
2268 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2269 doc: /* Non-nil means number continuously across print calls.
2270 This affects the numbers printed for #N= labels and #M# references.
2271 See also `print-circle', `print-gensym', and `print-number-table'.
2272 This variable should not be set with `setq'; bind it with a `let' instead. */);
2273 Vprint_continuous_numbering = Qnil;
2274
2275 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2276 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2277 The Lisp printer uses this vector to detect Lisp objects referenced more
2278 than once.
2279
2280 When you bind `print-continuous-numbering' to t, you should probably
2281 also bind `print-number-table' to nil. This ensures that the value of
2282 `print-number-table' can be garbage-collected once the printing is
2283 done. If all elements of `print-number-table' are nil, it means that
2284 the printing done so far has not found any shared structure or objects
2285 that need to be recorded in the table. */);
2286 Vprint_number_table = Qnil;
2287
2288 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2289 doc: /* A flag to control printing of `charset' text property on printing a string.
2290 The value must be nil, t, or `default'.
2291
2292 If the value is nil, don't print the text property `charset'.
2293
2294 If the value is t, always print the text property `charset'.
2295
2296 If the value is `default', print the text property `charset' only when
2297 the value is different from what is guessed in the current charset
2298 priorities. */);
2299 Vprint_charset_text_property = Qdefault;
2300
2301 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2302 staticpro (&Vprin1_to_string_buffer);
2303
2304 defsubr (&Sprin1);
2305 defsubr (&Sprin1_to_string);
2306 defsubr (&Serror_message_string);
2307 defsubr (&Sprinc);
2308 defsubr (&Sprint);
2309 defsubr (&Sterpri);
2310 defsubr (&Swrite_char);
2311 defsubr (&Sredirect_debugging_output);
2312
2313 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2314 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2315 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2316
2317 print_prune_charset_plist = Qnil;
2318 staticpro (&print_prune_charset_plist);
2319 }