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