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