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