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