Fix bug #9221 with memory leak in bidi display.
[bpt/emacs.git] / src / print.c
1 /* Lisp object printing and output streams.
2
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2011
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 PRINTFINSH */
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 width = (width * 10) + (*cp++ - '0');
1020 while (*cp >= '0' && *cp <= '9');
1021
1022 /* A precision of zero is valid only for %f. */
1023 if (width > DBL_DIG
1024 || (width == 0 && *cp != 'f'))
1025 goto lose;
1026 }
1027
1028 if (*cp != 'e' && *cp != 'f' && *cp != 'g')
1029 goto lose;
1030
1031 if (cp[1] != 0)
1032 goto lose;
1033
1034 sprintf (buf, SSDATA (Vfloat_output_format), data);
1035 }
1036
1037 /* Make sure there is a decimal point with digit after, or an
1038 exponent, so that the value is readable as a float. But don't do
1039 this with "%.0f"; it's valid for that not to produce a decimal
1040 point. Note that width can be 0 only for %.0f. */
1041 if (width != 0)
1042 {
1043 for (cp = buf; *cp; cp++)
1044 if ((*cp < '0' || *cp > '9') && *cp != '-')
1045 break;
1046
1047 if (*cp == '.' && cp[1] == 0)
1048 {
1049 cp[1] = '0';
1050 cp[2] = 0;
1051 }
1052 else if (*cp == 0)
1053 {
1054 *cp++ = '.';
1055 *cp++ = '0';
1056 *cp++ = 0;
1057 }
1058 }
1059 }
1060
1061 \f
1062 static void
1063 print (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1064 {
1065 new_backquote_output = 0;
1066
1067 /* Reset print_number_index and Vprint_number_table only when
1068 the variable Vprint_continuous_numbering is nil. Otherwise,
1069 the values of these variables will be kept between several
1070 print functions. */
1071 if (NILP (Vprint_continuous_numbering)
1072 || NILP (Vprint_number_table))
1073 {
1074 print_number_index = 0;
1075 Vprint_number_table = Qnil;
1076 }
1077
1078 /* Construct Vprint_number_table for print-gensym and print-circle. */
1079 if (!NILP (Vprint_gensym) || !NILP (Vprint_circle))
1080 {
1081 /* Construct Vprint_number_table.
1082 This increments print_number_index for the objects added. */
1083 print_depth = 0;
1084 print_preprocess (obj);
1085
1086 if (HASH_TABLE_P (Vprint_number_table))
1087 { /* Remove unnecessary objects, which appear only once in OBJ;
1088 that is, whose status is Qt.
1089 Maybe a better way to do that is to copy elements to
1090 a new hash table. */
1091 struct Lisp_Hash_Table *h = XHASH_TABLE (Vprint_number_table);
1092 EMACS_INT i;
1093
1094 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1095 if (!NILP (HASH_HASH (h, i))
1096 && EQ (HASH_VALUE (h, i), Qt))
1097 Fremhash (HASH_KEY (h, i), Vprint_number_table);
1098 }
1099 }
1100
1101 print_depth = 0;
1102 print_object (obj, printcharfun, escapeflag);
1103 }
1104
1105 #define PRINT_CIRCLE_CANDIDATE_P(obj) \
1106 (STRINGP (obj) || CONSP (obj) \
1107 || (VECTORLIKEP (obj) \
1108 && (VECTORP (obj) || COMPILEDP (obj) \
1109 || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1110 || HASH_TABLE_P (obj) || FONTP (obj))) \
1111 || (! NILP (Vprint_gensym) \
1112 && SYMBOLP (obj) \
1113 && !SYMBOL_INTERNED_P (obj)))
1114
1115 /* Construct Vprint_number_table according to the structure of OBJ.
1116 OBJ itself and all its elements will be added to Vprint_number_table
1117 recursively if it is a list, vector, compiled function, char-table,
1118 string (its text properties will be traced), or a symbol that has
1119 no obarray (this is for the print-gensym feature).
1120 The status fields of Vprint_number_table mean whether each object appears
1121 more than once in OBJ: Qnil at the first time, and Qt after that . */
1122 static void
1123 print_preprocess (Lisp_Object obj)
1124 {
1125 int i;
1126 EMACS_INT size;
1127 int loop_count = 0;
1128 Lisp_Object halftail;
1129
1130 /* Give up if we go so deep that print_object will get an error. */
1131 /* See similar code in print_object. */
1132 if (print_depth >= PRINT_CIRCLE)
1133 error ("Apparently circular structure being printed");
1134
1135 /* Avoid infinite recursion for circular nested structure
1136 in the case where Vprint_circle is nil. */
1137 if (NILP (Vprint_circle))
1138 {
1139 for (i = 0; i < print_depth; i++)
1140 if (EQ (obj, being_printed[i]))
1141 return;
1142 being_printed[print_depth] = obj;
1143 }
1144
1145 print_depth++;
1146 halftail = obj;
1147
1148 loop:
1149 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1150 {
1151 if (!HASH_TABLE_P (Vprint_number_table))
1152 {
1153 Lisp_Object args[2];
1154 args[0] = QCtest;
1155 args[1] = Qeq;
1156 Vprint_number_table = Fmake_hash_table (2, args);
1157 }
1158
1159 /* In case print-circle is nil and print-gensym is t,
1160 add OBJ to Vprint_number_table only when OBJ is a symbol. */
1161 if (! NILP (Vprint_circle) || SYMBOLP (obj))
1162 {
1163 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1164 if (!NILP (num)
1165 /* If Vprint_continuous_numbering is non-nil and OBJ is a gensym,
1166 always print the gensym with a number. This is a special for
1167 the lisp function byte-compile-output-docform. */
1168 || (!NILP (Vprint_continuous_numbering)
1169 && SYMBOLP (obj)
1170 && !SYMBOL_INTERNED_P (obj)))
1171 { /* OBJ appears more than once. Let's remember that. */
1172 if (!INTEGERP (num))
1173 {
1174 print_number_index++;
1175 /* Negative number indicates it hasn't been printed yet. */
1176 Fputhash (obj, make_number (- print_number_index),
1177 Vprint_number_table);
1178 }
1179 print_depth--;
1180 return;
1181 }
1182 else
1183 /* OBJ is not yet recorded. Let's add to the table. */
1184 Fputhash (obj, Qt, Vprint_number_table);
1185 }
1186
1187 switch (XTYPE (obj))
1188 {
1189 case Lisp_String:
1190 /* A string may have text properties, which can be circular. */
1191 traverse_intervals_noorder (STRING_INTERVALS (obj),
1192 print_preprocess_string, Qnil);
1193 break;
1194
1195 case Lisp_Cons:
1196 /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
1197 just as in print_object. */
1198 if (loop_count && EQ (obj, halftail))
1199 break;
1200 print_preprocess (XCAR (obj));
1201 obj = XCDR (obj);
1202 loop_count++;
1203 if (!(loop_count & 1))
1204 halftail = XCDR (halftail);
1205 goto loop;
1206
1207 case Lisp_Vectorlike:
1208 size = ASIZE (obj);
1209 if (size & PSEUDOVECTOR_FLAG)
1210 size &= PSEUDOVECTOR_SIZE_MASK;
1211 for (i = 0; i < size; i++)
1212 print_preprocess (XVECTOR (obj)->contents[i]);
1213 if (HASH_TABLE_P (obj))
1214 { /* For hash tables, the key_and_value slot is past
1215 `size' because it needs to be marked specially in case
1216 the table is weak. */
1217 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1218 print_preprocess (h->key_and_value);
1219 }
1220 break;
1221
1222 default:
1223 break;
1224 }
1225 }
1226 print_depth--;
1227 }
1228
1229 static void
1230 print_preprocess_string (INTERVAL interval, Lisp_Object arg)
1231 {
1232 print_preprocess (interval->plist);
1233 }
1234
1235 static void print_check_string_charset_prop (INTERVAL interval, Lisp_Object string);
1236
1237 #define PRINT_STRING_NON_CHARSET_FOUND 1
1238 #define PRINT_STRING_UNSAFE_CHARSET_FOUND 2
1239
1240 /* Bitwise or of the above macros. */
1241 static int print_check_string_result;
1242
1243 static void
1244 print_check_string_charset_prop (INTERVAL interval, Lisp_Object string)
1245 {
1246 Lisp_Object val;
1247
1248 if (NILP (interval->plist)
1249 || (print_check_string_result == (PRINT_STRING_NON_CHARSET_FOUND
1250 | PRINT_STRING_UNSAFE_CHARSET_FOUND)))
1251 return;
1252 for (val = interval->plist; CONSP (val) && ! EQ (XCAR (val), Qcharset);
1253 val = XCDR (XCDR (val)));
1254 if (! CONSP (val))
1255 {
1256 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1257 return;
1258 }
1259 if (! (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND))
1260 {
1261 if (! EQ (val, interval->plist)
1262 || CONSP (XCDR (XCDR (val))))
1263 print_check_string_result |= PRINT_STRING_NON_CHARSET_FOUND;
1264 }
1265 if (NILP (Vprint_charset_text_property)
1266 || ! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1267 {
1268 int i, c;
1269 EMACS_INT charpos = interval->position;
1270 EMACS_INT bytepos = string_char_to_byte (string, charpos);
1271 Lisp_Object charset;
1272
1273 charset = XCAR (XCDR (val));
1274 for (i = 0; i < LENGTH (interval); i++)
1275 {
1276 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1277 if (! ASCII_CHAR_P (c)
1278 && ! EQ (CHARSET_NAME (CHAR_CHARSET (c)), charset))
1279 {
1280 print_check_string_result |= PRINT_STRING_UNSAFE_CHARSET_FOUND;
1281 break;
1282 }
1283 }
1284 }
1285 }
1286
1287 /* The value is (charset . nil). */
1288 static Lisp_Object print_prune_charset_plist;
1289
1290 static Lisp_Object
1291 print_prune_string_charset (Lisp_Object string)
1292 {
1293 print_check_string_result = 0;
1294 traverse_intervals (STRING_INTERVALS (string), 0,
1295 print_check_string_charset_prop, string);
1296 if (! (print_check_string_result & PRINT_STRING_UNSAFE_CHARSET_FOUND))
1297 {
1298 string = Fcopy_sequence (string);
1299 if (print_check_string_result & PRINT_STRING_NON_CHARSET_FOUND)
1300 {
1301 if (NILP (print_prune_charset_plist))
1302 print_prune_charset_plist = Fcons (Qcharset, Qnil);
1303 Fremove_text_properties (make_number (0),
1304 make_number (SCHARS (string)),
1305 print_prune_charset_plist, string);
1306 }
1307 else
1308 Fset_text_properties (make_number (0), make_number (SCHARS (string)),
1309 Qnil, string);
1310 }
1311 return string;
1312 }
1313
1314 static void
1315 print_object (Lisp_Object obj, register Lisp_Object printcharfun, int escapeflag)
1316 {
1317 char buf[40];
1318
1319 QUIT;
1320
1321 /* See similar code in print_preprocess. */
1322 if (print_depth >= PRINT_CIRCLE)
1323 error ("Apparently circular structure being printed");
1324
1325 /* Detect circularities and truncate them. */
1326 if (PRINT_CIRCLE_CANDIDATE_P (obj))
1327 {
1328 if (NILP (Vprint_circle) && NILP (Vprint_gensym))
1329 {
1330 /* Simple but incomplete way. */
1331 int i;
1332 for (i = 0; i < print_depth; i++)
1333 if (EQ (obj, being_printed[i]))
1334 {
1335 sprintf (buf, "#%d", i);
1336 strout (buf, -1, -1, printcharfun);
1337 return;
1338 }
1339 being_printed[print_depth] = obj;
1340 }
1341 else
1342 {
1343 /* With the print-circle feature. */
1344 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1345 if (INTEGERP (num))
1346 {
1347 EMACS_INT n = XINT (num);
1348 if (n < 0)
1349 { /* Add a prefix #n= if OBJ has not yet been printed;
1350 that is, its status field is nil. */
1351 sprintf (buf, "#%"pI"d=", -n);
1352 strout (buf, -1, -1, printcharfun);
1353 /* OBJ is going to be printed. Remember that fact. */
1354 Fputhash (obj, make_number (- n), Vprint_number_table);
1355 }
1356 else
1357 {
1358 /* Just print #n# if OBJ has already been printed. */
1359 sprintf (buf, "#%"pI"d#", n);
1360 strout (buf, -1, -1, printcharfun);
1361 return;
1362 }
1363 }
1364 }
1365 }
1366
1367 print_depth++;
1368
1369 switch (XTYPE (obj))
1370 {
1371 case_Lisp_Int:
1372 sprintf (buf, "%"pI"d", XINT (obj));
1373 strout (buf, -1, -1, printcharfun);
1374 break;
1375
1376 case Lisp_Float:
1377 {
1378 char pigbuf[FLOAT_TO_STRING_BUFSIZE];
1379
1380 float_to_string (pigbuf, XFLOAT_DATA (obj));
1381 strout (pigbuf, -1, -1, printcharfun);
1382 }
1383 break;
1384
1385 case Lisp_String:
1386 if (!escapeflag)
1387 print_string (obj, printcharfun);
1388 else
1389 {
1390 register EMACS_INT i_byte;
1391 struct gcpro gcpro1;
1392 unsigned char *str;
1393 EMACS_INT size_byte;
1394 /* 1 means we must ensure that the next character we output
1395 cannot be taken as part of a hex character escape. */
1396 int need_nonhex = 0;
1397 int multibyte = STRING_MULTIBYTE (obj);
1398
1399 GCPRO1 (obj);
1400
1401 if (! EQ (Vprint_charset_text_property, Qt))
1402 obj = print_prune_string_charset (obj);
1403
1404 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1405 {
1406 PRINTCHAR ('#');
1407 PRINTCHAR ('(');
1408 }
1409
1410 PRINTCHAR ('\"');
1411 str = SDATA (obj);
1412 size_byte = SBYTES (obj);
1413
1414 for (i_byte = 0; i_byte < size_byte;)
1415 {
1416 /* Here, we must convert each multi-byte form to the
1417 corresponding character code before handing it to PRINTCHAR. */
1418 int len;
1419 int c;
1420
1421 if (multibyte)
1422 {
1423 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1424 i_byte += len;
1425 }
1426 else
1427 c = str[i_byte++];
1428
1429 QUIT;
1430
1431 if (c == '\n' && print_escape_newlines)
1432 {
1433 PRINTCHAR ('\\');
1434 PRINTCHAR ('n');
1435 }
1436 else if (c == '\f' && print_escape_newlines)
1437 {
1438 PRINTCHAR ('\\');
1439 PRINTCHAR ('f');
1440 }
1441 else if (multibyte
1442 && (CHAR_BYTE8_P (c)
1443 || (! ASCII_CHAR_P (c) && print_escape_multibyte)))
1444 {
1445 /* When multibyte is disabled,
1446 print multibyte string chars using hex escapes.
1447 For a char code that could be in a unibyte string,
1448 when found in a multibyte string, always use a hex escape
1449 so it reads back as multibyte. */
1450 char outbuf[50];
1451
1452 if (CHAR_BYTE8_P (c))
1453 sprintf (outbuf, "\\%03o", CHAR_TO_BYTE8 (c));
1454 else
1455 {
1456 sprintf (outbuf, "\\x%04x", c);
1457 need_nonhex = 1;
1458 }
1459 strout (outbuf, -1, -1, printcharfun);
1460 }
1461 else if (! multibyte
1462 && SINGLE_BYTE_CHAR_P (c) && ! ASCII_BYTE_P (c)
1463 && print_escape_nonascii)
1464 {
1465 /* When printing in a multibyte buffer
1466 or when explicitly requested,
1467 print single-byte non-ASCII string chars
1468 using octal escapes. */
1469 char outbuf[5];
1470 sprintf (outbuf, "\\%03o", c);
1471 strout (outbuf, -1, -1, printcharfun);
1472 }
1473 else
1474 {
1475 /* If we just had a hex escape, and this character
1476 could be taken as part of it,
1477 output `\ ' to prevent that. */
1478 if (need_nonhex)
1479 {
1480 need_nonhex = 0;
1481 if ((c >= 'a' && c <= 'f')
1482 || (c >= 'A' && c <= 'F')
1483 || (c >= '0' && c <= '9'))
1484 strout ("\\ ", -1, -1, printcharfun);
1485 }
1486
1487 if (c == '\"' || c == '\\')
1488 PRINTCHAR ('\\');
1489 PRINTCHAR (c);
1490 }
1491 }
1492 PRINTCHAR ('\"');
1493
1494 if (!NULL_INTERVAL_P (STRING_INTERVALS (obj)))
1495 {
1496 traverse_intervals (STRING_INTERVALS (obj),
1497 0, print_interval, printcharfun);
1498 PRINTCHAR (')');
1499 }
1500
1501 UNGCPRO;
1502 }
1503 break;
1504
1505 case Lisp_Symbol:
1506 {
1507 register int confusing;
1508 register unsigned char *p = SDATA (SYMBOL_NAME (obj));
1509 register unsigned char *end = p + SBYTES (SYMBOL_NAME (obj));
1510 register int c;
1511 int i, i_byte;
1512 EMACS_INT size_byte;
1513 Lisp_Object name;
1514
1515 name = SYMBOL_NAME (obj);
1516
1517 if (p != end && (*p == '-' || *p == '+')) p++;
1518 if (p == end)
1519 confusing = 0;
1520 /* If symbol name begins with a digit, and ends with a digit,
1521 and contains nothing but digits and `e', it could be treated
1522 as a number. So set CONFUSING.
1523
1524 Symbols that contain periods could also be taken as numbers,
1525 but periods are always escaped, so we don't have to worry
1526 about them here. */
1527 else if (*p >= '0' && *p <= '9'
1528 && end[-1] >= '0' && end[-1] <= '9')
1529 {
1530 while (p != end && ((*p >= '0' && *p <= '9')
1531 /* Needed for \2e10. */
1532 || *p == 'e' || *p == 'E'))
1533 p++;
1534 confusing = (end == p);
1535 }
1536 else
1537 confusing = 0;
1538
1539 size_byte = SBYTES (name);
1540
1541 if (! NILP (Vprint_gensym) && !SYMBOL_INTERNED_P (obj))
1542 {
1543 PRINTCHAR ('#');
1544 PRINTCHAR (':');
1545 }
1546 else if (size_byte == 0)
1547 {
1548 PRINTCHAR ('#');
1549 PRINTCHAR ('#');
1550 break;
1551 }
1552
1553 for (i = 0, i_byte = 0; i_byte < size_byte;)
1554 {
1555 /* Here, we must convert each multi-byte form to the
1556 corresponding character code before handing it to PRINTCHAR. */
1557 FETCH_STRING_CHAR_ADVANCE (c, name, i, i_byte);
1558 QUIT;
1559
1560 if (escapeflag)
1561 {
1562 if (c == '\"' || c == '\\' || c == '\''
1563 || c == ';' || c == '#' || c == '(' || c == ')'
1564 || c == ',' || c == '.' || c == '`'
1565 || c == '[' || c == ']' || c == '?' || c <= 040
1566 || confusing)
1567 PRINTCHAR ('\\'), confusing = 0;
1568 }
1569 PRINTCHAR (c);
1570 }
1571 }
1572 break;
1573
1574 case Lisp_Cons:
1575 /* If deeper than spec'd depth, print placeholder. */
1576 if (INTEGERP (Vprint_level)
1577 && print_depth > XINT (Vprint_level))
1578 strout ("...", -1, -1, printcharfun);
1579 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1580 && (EQ (XCAR (obj), Qquote)))
1581 {
1582 PRINTCHAR ('\'');
1583 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1584 }
1585 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1586 && (EQ (XCAR (obj), Qfunction)))
1587 {
1588 PRINTCHAR ('#');
1589 PRINTCHAR ('\'');
1590 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1591 }
1592 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1593 && ((EQ (XCAR (obj), Qbackquote))))
1594 {
1595 print_object (XCAR (obj), printcharfun, 0);
1596 new_backquote_output++;
1597 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1598 new_backquote_output--;
1599 }
1600 else if (print_quoted && CONSP (XCDR (obj)) && NILP (XCDR (XCDR (obj)))
1601 && new_backquote_output
1602 && ((EQ (XCAR (obj), Qbackquote)
1603 || EQ (XCAR (obj), Qcomma)
1604 || EQ (XCAR (obj), Qcomma_at)
1605 || EQ (XCAR (obj), Qcomma_dot))))
1606 {
1607 print_object (XCAR (obj), printcharfun, 0);
1608 new_backquote_output--;
1609 print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
1610 new_backquote_output++;
1611 }
1612 else
1613 {
1614 PRINTCHAR ('(');
1615
1616 {
1617 EMACS_INT print_length;
1618 int i;
1619 Lisp_Object halftail = obj;
1620
1621 /* Negative values of print-length are invalid in CL.
1622 Treat them like nil, as CMUCL does. */
1623 if (NATNUMP (Vprint_length))
1624 print_length = XFASTINT (Vprint_length);
1625 else
1626 print_length = 0;
1627
1628 i = 0;
1629 while (CONSP (obj))
1630 {
1631 /* Detect circular list. */
1632 if (NILP (Vprint_circle))
1633 {
1634 /* Simple but imcomplete way. */
1635 if (i != 0 && EQ (obj, halftail))
1636 {
1637 sprintf (buf, " . #%d", i / 2);
1638 strout (buf, -1, -1, printcharfun);
1639 goto end_of_list;
1640 }
1641 }
1642 else
1643 {
1644 /* With the print-circle feature. */
1645 if (i != 0)
1646 {
1647 Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
1648 if (INTEGERP (num))
1649 {
1650 strout (" . ", 3, 3, printcharfun);
1651 print_object (obj, printcharfun, escapeflag);
1652 goto end_of_list;
1653 }
1654 }
1655 }
1656
1657 if (i++)
1658 PRINTCHAR (' ');
1659
1660 if (print_length && i > print_length)
1661 {
1662 strout ("...", 3, 3, printcharfun);
1663 goto end_of_list;
1664 }
1665
1666 print_object (XCAR (obj), printcharfun, escapeflag);
1667
1668 obj = XCDR (obj);
1669 if (!(i & 1))
1670 halftail = XCDR (halftail);
1671 }
1672 }
1673
1674 /* OBJ non-nil here means it's the end of a dotted list. */
1675 if (!NILP (obj))
1676 {
1677 strout (" . ", 3, 3, printcharfun);
1678 print_object (obj, printcharfun, escapeflag);
1679 }
1680
1681 end_of_list:
1682 PRINTCHAR (')');
1683 }
1684 break;
1685
1686 case Lisp_Vectorlike:
1687 if (PROCESSP (obj))
1688 {
1689 if (escapeflag)
1690 {
1691 strout ("#<process ", -1, -1, printcharfun);
1692 print_string (XPROCESS (obj)->name, printcharfun);
1693 PRINTCHAR ('>');
1694 }
1695 else
1696 print_string (XPROCESS (obj)->name, printcharfun);
1697 }
1698 else if (BOOL_VECTOR_P (obj))
1699 {
1700 register int i;
1701 register unsigned char c;
1702 struct gcpro gcpro1;
1703 EMACS_INT size_in_chars
1704 = ((XBOOL_VECTOR (obj)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
1705 / BOOL_VECTOR_BITS_PER_CHAR);
1706
1707 GCPRO1 (obj);
1708
1709 PRINTCHAR ('#');
1710 PRINTCHAR ('&');
1711 sprintf (buf, "%"pI"d", XBOOL_VECTOR (obj)->size);
1712 strout (buf, -1, -1, printcharfun);
1713 PRINTCHAR ('\"');
1714
1715 /* Don't print more characters than the specified maximum.
1716 Negative values of print-length are invalid. Treat them
1717 like a print-length of nil. */
1718 if (NATNUMP (Vprint_length)
1719 && XFASTINT (Vprint_length) < size_in_chars)
1720 size_in_chars = XFASTINT (Vprint_length);
1721
1722 for (i = 0; i < size_in_chars; i++)
1723 {
1724 QUIT;
1725 c = XBOOL_VECTOR (obj)->data[i];
1726 if (c == '\n' && print_escape_newlines)
1727 {
1728 PRINTCHAR ('\\');
1729 PRINTCHAR ('n');
1730 }
1731 else if (c == '\f' && print_escape_newlines)
1732 {
1733 PRINTCHAR ('\\');
1734 PRINTCHAR ('f');
1735 }
1736 else if (c > '\177')
1737 {
1738 /* Use octal escapes to avoid encoding issues. */
1739 PRINTCHAR ('\\');
1740 PRINTCHAR ('0' + ((c >> 6) & 3));
1741 PRINTCHAR ('0' + ((c >> 3) & 7));
1742 PRINTCHAR ('0' + (c & 7));
1743 }
1744 else
1745 {
1746 if (c == '\"' || c == '\\')
1747 PRINTCHAR ('\\');
1748 PRINTCHAR (c);
1749 }
1750 }
1751 PRINTCHAR ('\"');
1752
1753 UNGCPRO;
1754 }
1755 else if (SUBRP (obj))
1756 {
1757 strout ("#<subr ", -1, -1, printcharfun);
1758 strout (XSUBR (obj)->symbol_name, -1, -1, printcharfun);
1759 PRINTCHAR ('>');
1760 }
1761 else if (WINDOWP (obj))
1762 {
1763 strout ("#<window ", -1, -1, printcharfun);
1764 sprintf (buf, "%"pI"d", XFASTINT (XWINDOW (obj)->sequence_number));
1765 strout (buf, -1, -1, printcharfun);
1766 if (!NILP (XWINDOW (obj)->buffer))
1767 {
1768 strout (" on ", -1, -1, printcharfun);
1769 print_string (BVAR (XBUFFER (XWINDOW (obj)->buffer), name), printcharfun);
1770 }
1771 PRINTCHAR ('>');
1772 }
1773 else if (TERMINALP (obj))
1774 {
1775 struct terminal *t = XTERMINAL (obj);
1776 strout ("#<terminal ", -1, -1, printcharfun);
1777 sprintf (buf, "%d", t->id);
1778 strout (buf, -1, -1, printcharfun);
1779 if (t->name)
1780 {
1781 strout (" on ", -1, -1, printcharfun);
1782 strout (t->name, -1, -1, printcharfun);
1783 }
1784 PRINTCHAR ('>');
1785 }
1786 else if (HASH_TABLE_P (obj))
1787 {
1788 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
1789 int i;
1790 EMACS_INT real_size, size;
1791 #if 0
1792 strout ("#<hash-table", -1, -1, printcharfun);
1793 if (SYMBOLP (h->test))
1794 {
1795 PRINTCHAR (' ');
1796 PRINTCHAR ('\'');
1797 strout (SDATA (SYMBOL_NAME (h->test)), -1, -1, printcharfun);
1798 PRINTCHAR (' ');
1799 strout (SDATA (SYMBOL_NAME (h->weak)), -1, -1, printcharfun);
1800 PRINTCHAR (' ');
1801 sprintf (buf, "%ld/%ld", (long) h->count,
1802 (long) ASIZE (h->next));
1803 strout (buf, -1, -1, printcharfun);
1804 }
1805 sprintf (buf, " 0x%lx", (unsigned long) h);
1806 strout (buf, -1, -1, printcharfun);
1807 PRINTCHAR ('>');
1808 #endif
1809 /* Implement a readable output, e.g.:
1810 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
1811 /* Always print the size. */
1812 sprintf (buf, "#s(hash-table size %ld",
1813 (long) ASIZE (h->next));
1814 strout (buf, -1, -1, printcharfun);
1815
1816 if (!NILP (h->test))
1817 {
1818 strout (" test ", -1, -1, printcharfun);
1819 print_object (h->test, printcharfun, escapeflag);
1820 }
1821
1822 if (!NILP (h->weak))
1823 {
1824 strout (" weakness ", -1, -1, printcharfun);
1825 print_object (h->weak, printcharfun, escapeflag);
1826 }
1827
1828 if (!NILP (h->rehash_size))
1829 {
1830 strout (" rehash-size ", -1, -1, printcharfun);
1831 print_object (h->rehash_size, printcharfun, escapeflag);
1832 }
1833
1834 if (!NILP (h->rehash_threshold))
1835 {
1836 strout (" rehash-threshold ", -1, -1, printcharfun);
1837 print_object (h->rehash_threshold, printcharfun, escapeflag);
1838 }
1839
1840 strout (" data ", -1, -1, printcharfun);
1841
1842 /* Print the data here as a plist. */
1843 real_size = HASH_TABLE_SIZE (h);
1844 size = real_size;
1845
1846 /* Don't print more elements than the specified maximum. */
1847 if (NATNUMP (Vprint_length)
1848 && XFASTINT (Vprint_length) < size)
1849 size = XFASTINT (Vprint_length);
1850
1851 PRINTCHAR ('(');
1852 for (i = 0; i < size; i++)
1853 if (!NILP (HASH_HASH (h, i)))
1854 {
1855 if (i) PRINTCHAR (' ');
1856 print_object (HASH_KEY (h, i), printcharfun, escapeflag);
1857 PRINTCHAR (' ');
1858 print_object (HASH_VALUE (h, i), printcharfun, escapeflag);
1859 }
1860
1861 if (size < real_size)
1862 strout (" ...", 4, 4, printcharfun);
1863
1864 PRINTCHAR (')');
1865 PRINTCHAR (')');
1866
1867 }
1868 else if (BUFFERP (obj))
1869 {
1870 if (NILP (BVAR (XBUFFER (obj), name)))
1871 strout ("#<killed buffer>", -1, -1, printcharfun);
1872 else if (escapeflag)
1873 {
1874 strout ("#<buffer ", -1, -1, printcharfun);
1875 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1876 PRINTCHAR ('>');
1877 }
1878 else
1879 print_string (BVAR (XBUFFER (obj), name), printcharfun);
1880 }
1881 else if (WINDOW_CONFIGURATIONP (obj))
1882 {
1883 strout ("#<window-configuration>", -1, -1, printcharfun);
1884 }
1885 else if (FRAMEP (obj))
1886 {
1887 strout ((FRAME_LIVE_P (XFRAME (obj))
1888 ? "#<frame " : "#<dead frame "),
1889 -1, -1, printcharfun);
1890 print_string (XFRAME (obj)->name, printcharfun);
1891 sprintf (buf, " %p", XFRAME (obj));
1892 strout (buf, -1, -1, printcharfun);
1893 PRINTCHAR ('>');
1894 }
1895 else if (FONTP (obj))
1896 {
1897 EMACS_INT i;
1898
1899 if (! FONT_OBJECT_P (obj))
1900 {
1901 if (FONT_SPEC_P (obj))
1902 strout ("#<font-spec", -1, -1, printcharfun);
1903 else
1904 strout ("#<font-entity", -1, -1, printcharfun);
1905 for (i = 0; i < FONT_SPEC_MAX; i++)
1906 {
1907 PRINTCHAR (' ');
1908 if (i < FONT_WEIGHT_INDEX || i > FONT_WIDTH_INDEX)
1909 print_object (AREF (obj, i), printcharfun, escapeflag);
1910 else
1911 print_object (font_style_symbolic (obj, i, 0),
1912 printcharfun, escapeflag);
1913 }
1914 }
1915 else
1916 {
1917 strout ("#<font-object ", -1, -1, printcharfun);
1918 print_object (AREF (obj, FONT_NAME_INDEX), printcharfun,
1919 escapeflag);
1920 }
1921 PRINTCHAR ('>');
1922 }
1923 else
1924 {
1925 EMACS_INT size = ASIZE (obj);
1926 if (COMPILEDP (obj))
1927 {
1928 PRINTCHAR ('#');
1929 size &= PSEUDOVECTOR_SIZE_MASK;
1930 }
1931 if (CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj))
1932 {
1933 /* We print a char-table as if it were a vector,
1934 lumping the parent and default slots in with the
1935 character slots. But we add #^ as a prefix. */
1936
1937 /* Make each lowest sub_char_table start a new line.
1938 Otherwise we'll make a line extremely long, which
1939 results in slow redisplay. */
1940 if (SUB_CHAR_TABLE_P (obj)
1941 && XINT (XSUB_CHAR_TABLE (obj)->depth) == 3)
1942 PRINTCHAR ('\n');
1943 PRINTCHAR ('#');
1944 PRINTCHAR ('^');
1945 if (SUB_CHAR_TABLE_P (obj))
1946 PRINTCHAR ('^');
1947 size &= PSEUDOVECTOR_SIZE_MASK;
1948 }
1949 if (size & PSEUDOVECTOR_FLAG)
1950 goto badtype;
1951
1952 PRINTCHAR ('[');
1953 {
1954 register int i;
1955 register Lisp_Object tem;
1956 EMACS_INT real_size = size;
1957
1958 /* Don't print more elements than the specified maximum. */
1959 if (NATNUMP (Vprint_length)
1960 && XFASTINT (Vprint_length) < size)
1961 size = XFASTINT (Vprint_length);
1962
1963 for (i = 0; i < size; i++)
1964 {
1965 if (i) PRINTCHAR (' ');
1966 tem = XVECTOR (obj)->contents[i];
1967 print_object (tem, printcharfun, escapeflag);
1968 }
1969 if (size < real_size)
1970 strout (" ...", 4, 4, printcharfun);
1971 }
1972 PRINTCHAR (']');
1973 }
1974 break;
1975
1976 case Lisp_Misc:
1977 switch (XMISCTYPE (obj))
1978 {
1979 case Lisp_Misc_Marker:
1980 strout ("#<marker ", -1, -1, printcharfun);
1981 /* Do you think this is necessary? */
1982 if (XMARKER (obj)->insertion_type != 0)
1983 strout ("(moves after insertion) ", -1, -1, printcharfun);
1984 if (! XMARKER (obj)->buffer)
1985 strout ("in no buffer", -1, -1, printcharfun);
1986 else
1987 {
1988 sprintf (buf, "at %"pI"d", marker_position (obj));
1989 strout (buf, -1, -1, printcharfun);
1990 strout (" in ", -1, -1, printcharfun);
1991 print_string (BVAR (XMARKER (obj)->buffer, name), printcharfun);
1992 }
1993 PRINTCHAR ('>');
1994 break;
1995
1996 case Lisp_Misc_Overlay:
1997 strout ("#<overlay ", -1, -1, printcharfun);
1998 if (! XMARKER (OVERLAY_START (obj))->buffer)
1999 strout ("in no buffer", -1, -1, printcharfun);
2000 else
2001 {
2002 sprintf (buf, "from %"pI"d to %"pI"d in ",
2003 marker_position (OVERLAY_START (obj)),
2004 marker_position (OVERLAY_END (obj)));
2005 strout (buf, -1, -1, printcharfun);
2006 print_string (BVAR (XMARKER (OVERLAY_START (obj))->buffer, name),
2007 printcharfun);
2008 }
2009 PRINTCHAR ('>');
2010 break;
2011
2012 /* Remaining cases shouldn't happen in normal usage, but let's print
2013 them anyway for the benefit of the debugger. */
2014 case Lisp_Misc_Free:
2015 strout ("#<misc free cell>", -1, -1, printcharfun);
2016 break;
2017
2018 case Lisp_Misc_Save_Value:
2019 strout ("#<save_value ", -1, -1, printcharfun);
2020 sprintf(buf, "ptr=%p int=%"pD"d",
2021 XSAVE_VALUE (obj)->pointer,
2022 XSAVE_VALUE (obj)->integer);
2023 strout (buf, -1, -1, printcharfun);
2024 PRINTCHAR ('>');
2025 break;
2026
2027 default:
2028 goto badtype;
2029 }
2030 break;
2031
2032 default:
2033 badtype:
2034 {
2035 /* We're in trouble if this happens!
2036 Probably should just abort () */
2037 strout ("#<EMACS BUG: INVALID DATATYPE ", -1, -1, printcharfun);
2038 if (MISCP (obj))
2039 sprintf (buf, "(MISC 0x%04x)", (int) XMISCTYPE (obj));
2040 else if (VECTORLIKEP (obj))
2041 sprintf (buf, "(PVEC 0x%08lx)", (unsigned long) ASIZE (obj));
2042 else
2043 sprintf (buf, "(0x%02x)", (int) XTYPE (obj));
2044 strout (buf, -1, -1, printcharfun);
2045 strout (" Save your buffers immediately and please report this bug>",
2046 -1, -1, printcharfun);
2047 }
2048 }
2049
2050 print_depth--;
2051 }
2052 \f
2053
2054 /* Print a description of INTERVAL using PRINTCHARFUN.
2055 This is part of printing a string that has text properties. */
2056
2057 void
2058 print_interval (INTERVAL interval, Lisp_Object printcharfun)
2059 {
2060 if (NILP (interval->plist))
2061 return;
2062 PRINTCHAR (' ');
2063 print_object (make_number (interval->position), printcharfun, 1);
2064 PRINTCHAR (' ');
2065 print_object (make_number (interval->position + LENGTH (interval)),
2066 printcharfun, 1);
2067 PRINTCHAR (' ');
2068 print_object (interval->plist, printcharfun, 1);
2069 }
2070
2071 \f
2072 void
2073 syms_of_print (void)
2074 {
2075 DEFSYM (Qtemp_buffer_setup_hook, "temp-buffer-setup-hook");
2076
2077 DEFVAR_LISP ("standard-output", Vstandard_output,
2078 doc: /* Output stream `print' uses by default for outputting a character.
2079 This may be any function of one argument.
2080 It may also be a buffer (output is inserted before point)
2081 or a marker (output is inserted and the marker is advanced)
2082 or the symbol t (output appears in the echo area). */);
2083 Vstandard_output = Qt;
2084 DEFSYM (Qstandard_output, "standard-output");
2085
2086 DEFVAR_LISP ("float-output-format", Vfloat_output_format,
2087 doc: /* The format descriptor string used to print floats.
2088 This is a %-spec like those accepted by `printf' in C,
2089 but with some restrictions. It must start with the two characters `%.'.
2090 After that comes an integer precision specification,
2091 and then a letter which controls the format.
2092 The letters allowed are `e', `f' and `g'.
2093 Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
2094 Use `f' for decimal point notation \"DIGITS.DIGITS\".
2095 Use `g' to choose the shorter of those two formats for the number at hand.
2096 The precision in any of these cases is the number of digits following
2097 the decimal point. With `f', a precision of 0 means to omit the
2098 decimal point. 0 is not allowed with `e' or `g'.
2099
2100 A value of nil means to use the shortest notation
2101 that represents the number without losing information. */);
2102 Vfloat_output_format = Qnil;
2103 DEFSYM (Qfloat_output_format, "float-output-format");
2104
2105 DEFVAR_LISP ("print-length", Vprint_length,
2106 doc: /* Maximum length of list to print before abbreviating.
2107 A value of nil means no limit. See also `eval-expression-print-length'. */);
2108 Vprint_length = Qnil;
2109
2110 DEFVAR_LISP ("print-level", Vprint_level,
2111 doc: /* Maximum depth of list nesting to print before abbreviating.
2112 A value of nil means no limit. See also `eval-expression-print-level'. */);
2113 Vprint_level = Qnil;
2114
2115 DEFVAR_BOOL ("print-escape-newlines", print_escape_newlines,
2116 doc: /* Non-nil means print newlines in strings as `\\n'.
2117 Also print formfeeds as `\\f'. */);
2118 print_escape_newlines = 0;
2119
2120 DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
2121 doc: /* Non-nil means print unibyte non-ASCII chars in strings as \\OOO.
2122 \(OOO is the octal representation of the character code.)
2123 Only single-byte characters are affected, and only in `prin1'.
2124 When the output goes in a multibyte buffer, this feature is
2125 enabled regardless of the value of the variable. */);
2126 print_escape_nonascii = 0;
2127
2128 DEFVAR_BOOL ("print-escape-multibyte", print_escape_multibyte,
2129 doc: /* Non-nil means print multibyte characters in strings as \\xXXXX.
2130 \(XXXX is the hex representation of the character code.)
2131 This affects only `prin1'. */);
2132 print_escape_multibyte = 0;
2133
2134 DEFVAR_BOOL ("print-quoted", print_quoted,
2135 doc: /* Non-nil means print quoted forms with reader syntax.
2136 I.e., (quote foo) prints as 'foo, (function foo) as #'foo. */);
2137 print_quoted = 0;
2138
2139 DEFVAR_LISP ("print-gensym", Vprint_gensym,
2140 doc: /* Non-nil means print uninterned symbols so they will read as uninterned.
2141 I.e., the value of (make-symbol \"foobar\") prints as #:foobar.
2142 When the uninterned symbol appears within a recursive data structure,
2143 and the symbol appears more than once, in addition use the #N# and #N=
2144 constructs as needed, so that multiple references to the same symbol are
2145 shared once again when the text is read back. */);
2146 Vprint_gensym = Qnil;
2147
2148 DEFVAR_LISP ("print-circle", Vprint_circle,
2149 doc: /* *Non-nil means print recursive structures using #N= and #N# syntax.
2150 If nil, printing proceeds recursively and may lead to
2151 `max-lisp-eval-depth' being exceeded or an error may occur:
2152 \"Apparently circular structure being printed.\" Also see
2153 `print-length' and `print-level'.
2154 If non-nil, shared substructures anywhere in the structure are printed
2155 with `#N=' before the first occurrence (in the order of the print
2156 representation) and `#N#' in place of each subsequent occurrence,
2157 where N is a positive decimal integer. */);
2158 Vprint_circle = Qnil;
2159
2160 DEFVAR_LISP ("print-continuous-numbering", Vprint_continuous_numbering,
2161 doc: /* *Non-nil means number continuously across print calls.
2162 This affects the numbers printed for #N= labels and #M# references.
2163 See also `print-circle', `print-gensym', and `print-number-table'.
2164 This variable should not be set with `setq'; bind it with a `let' instead. */);
2165 Vprint_continuous_numbering = Qnil;
2166
2167 DEFVAR_LISP ("print-number-table", Vprint_number_table,
2168 doc: /* A vector used internally to produce `#N=' labels and `#N#' references.
2169 The Lisp printer uses this vector to detect Lisp objects referenced more
2170 than once.
2171
2172 When you bind `print-continuous-numbering' to t, you should probably
2173 also bind `print-number-table' to nil. This ensures that the value of
2174 `print-number-table' can be garbage-collected once the printing is
2175 done. If all elements of `print-number-table' are nil, it means that
2176 the printing done so far has not found any shared structure or objects
2177 that need to be recorded in the table. */);
2178 Vprint_number_table = Qnil;
2179
2180 DEFVAR_LISP ("print-charset-text-property", Vprint_charset_text_property,
2181 doc: /* A flag to control printing of `charset' text property on printing a string.
2182 The value must be nil, t, or `default'.
2183
2184 If the value is nil, don't print the text property `charset'.
2185
2186 If the value is t, always print the text property `charset'.
2187
2188 If the value is `default', print the text property `charset' only when
2189 the value is different from what is guessed in the current charset
2190 priorities. */);
2191 Vprint_charset_text_property = Qdefault;
2192
2193 /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
2194 staticpro (&Vprin1_to_string_buffer);
2195
2196 defsubr (&Sprin1);
2197 defsubr (&Sprin1_to_string);
2198 defsubr (&Serror_message_string);
2199 defsubr (&Sprinc);
2200 defsubr (&Sprint);
2201 defsubr (&Sterpri);
2202 defsubr (&Swrite_char);
2203 defsubr (&Sexternal_debugging_output);
2204 #ifdef WITH_REDIRECT_DEBUGGING_OUTPUT
2205 defsubr (&Sredirect_debugging_output);
2206 #endif
2207
2208 DEFSYM (Qexternal_debugging_output, "external-debugging-output");
2209 DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
2210 DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
2211 DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
2212
2213 print_prune_charset_plist = Qnil;
2214 staticpro (&print_prune_charset_plist);
2215 }