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