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